1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.android.server.wifi;
17 
18 import android.annotation.NonNull;
19 import android.content.Context;
20 import android.hardware.wifi.supplicant.V1_0.ISupplicantStaIfaceCallback;
21 import android.hardware.wifi.supplicant.V1_2.DppAkm;
22 import android.hardware.wifi.supplicant.V1_2.DppFailureCode;
23 import android.hardware.wifi.supplicant.V1_3.DppSuccessCode;
24 import android.net.wifi.WifiConfiguration;
25 import android.net.wifi.WifiSsid;
26 import android.os.Process;
27 import android.util.Log;
28 
29 import com.android.server.wifi.util.NativeUtil;
30 
31 import java.util.ArrayList;
32 
33 abstract class SupplicantStaIfaceCallbackV1_2Impl extends
34         android.hardware.wifi.supplicant.V1_2.ISupplicantStaIfaceCallback.Stub {
35     private static final String TAG = SupplicantStaIfaceCallbackV1_2Impl.class.getSimpleName();
36     private final SupplicantStaIfaceHal mStaIfaceHal;
37     private final String mIfaceName;
38     private final Context mContext;
39     private final SupplicantStaIfaceHal.SupplicantStaIfaceHalCallbackV1_1 mCallbackV11;
40 
SupplicantStaIfaceCallbackV1_2Impl(@onNull SupplicantStaIfaceHal staIfaceHal, @NonNull String ifaceName, @NonNull Context context)41     SupplicantStaIfaceCallbackV1_2Impl(@NonNull SupplicantStaIfaceHal staIfaceHal,
42             @NonNull String ifaceName,
43             @NonNull Context context) {
44         mStaIfaceHal = staIfaceHal;
45         mIfaceName = ifaceName;
46         mContext = context;
47         // Create an older callback for function delegation,
48         // and it would cascadingly create older one.
49         mCallbackV11 = mStaIfaceHal.new SupplicantStaIfaceHalCallbackV1_1(mIfaceName);
50     }
51 
52     @Override
onNetworkAdded(int id)53     public void onNetworkAdded(int id) {
54         mCallbackV11.onNetworkAdded(id);
55     }
56 
57     @Override
onNetworkRemoved(int id)58     public void onNetworkRemoved(int id) {
59         mCallbackV11.onNetworkRemoved(id);
60     }
61 
62     /**
63      * Added to plumb the new {@code filsHlpSent} param from the V1.3 callback version.
64      */
onStateChanged(int newState, byte[ ] bssid, int id, ArrayList<Byte> ssid, boolean filsHlpSent)65     public void onStateChanged(int newState, byte[/* 6 */] bssid, int id, ArrayList<Byte> ssid,
66             boolean filsHlpSent) {
67         mCallbackV11.onStateChanged(newState, bssid, id, ssid, filsHlpSent);
68     }
69 
70     @Override
onStateChanged(int newState, byte[ ] bssid, int id, ArrayList<Byte> ssid)71     public void onStateChanged(int newState, byte[/* 6 */] bssid, int id,
72             ArrayList<Byte> ssid) {
73         onStateChanged(newState, bssid, id, ssid, false);
74     }
75 
76     @Override
onAnqpQueryDone(byte[ ] bssid, ISupplicantStaIfaceCallback.AnqpData data, ISupplicantStaIfaceCallback.Hs20AnqpData hs20Data)77     public void onAnqpQueryDone(byte[/* 6 */] bssid,
78             ISupplicantStaIfaceCallback.AnqpData data,
79             ISupplicantStaIfaceCallback.Hs20AnqpData hs20Data) {
80         mCallbackV11.onAnqpQueryDone(bssid, data, hs20Data);
81     }
82 
83     @Override
onHs20IconQueryDone(byte[ ] bssid, String fileName, ArrayList<Byte> data)84     public void onHs20IconQueryDone(byte[/* 6 */] bssid, String fileName,
85             ArrayList<Byte> data) {
86         mCallbackV11.onHs20IconQueryDone(bssid, fileName, data);
87     }
88 
89     @Override
onHs20SubscriptionRemediation(byte[ ] bssid, byte osuMethod, String url)90     public void onHs20SubscriptionRemediation(byte[/* 6 */] bssid,
91             byte osuMethod, String url) {
92         mCallbackV11.onHs20SubscriptionRemediation(bssid, osuMethod, url);
93     }
94 
95     @Override
onHs20DeauthImminentNotice(byte[ ] bssid, int reasonCode, int reAuthDelayInSec, String url)96     public void onHs20DeauthImminentNotice(byte[/* 6 */] bssid, int reasonCode,
97             int reAuthDelayInSec, String url) {
98         mCallbackV11.onHs20DeauthImminentNotice(bssid, reasonCode, reAuthDelayInSec, url);
99     }
100 
101     @Override
onDisconnected(byte[ ] bssid, boolean locallyGenerated, int reasonCode)102     public void onDisconnected(byte[/* 6 */] bssid, boolean locallyGenerated,
103             int reasonCode) {
104         mCallbackV11.onDisconnected(bssid, locallyGenerated, reasonCode);
105     }
106 
107     @Override
onAssociationRejected(byte[ ] bssid, int statusCode, boolean timedOut)108     public void onAssociationRejected(byte[/* 6 */] bssid, int statusCode,
109             boolean timedOut) {
110         mCallbackV11.onAssociationRejected(bssid, statusCode, timedOut);
111     }
112 
113     @Override
onAuthenticationTimeout(byte[ ] bssid)114     public void onAuthenticationTimeout(byte[/* 6 */] bssid) {
115         mCallbackV11.onAuthenticationTimeout(bssid);
116     }
117 
118     @Override
onBssidChanged(byte reason, byte[ ] bssid)119     public void onBssidChanged(byte reason, byte[/* 6 */] bssid) {
120         mCallbackV11.onBssidChanged(reason, bssid);
121     }
122 
123     @Override
onEapFailure()124     public void onEapFailure() {
125         mCallbackV11.onEapFailure();
126     }
127 
128     @Override
onEapFailure_1_1(int code)129     public void onEapFailure_1_1(int code) {
130         mCallbackV11.onEapFailure_1_1(code);
131     }
132 
133     @Override
onWpsEventSuccess()134     public void onWpsEventSuccess() {
135         mCallbackV11.onWpsEventSuccess();
136     }
137 
138     @Override
onWpsEventFail(byte[ ] bssid, short configError, short errorInd)139     public void onWpsEventFail(byte[/* 6 */] bssid, short configError, short errorInd) {
140         mCallbackV11.onWpsEventFail(bssid, configError, errorInd);
141     }
142 
143     @Override
onWpsEventPbcOverlap()144     public void onWpsEventPbcOverlap() {
145         mCallbackV11.onWpsEventPbcOverlap();
146     }
147 
148     @Override
onExtRadioWorkStart(int id)149     public void onExtRadioWorkStart(int id) {
150         mCallbackV11.onExtRadioWorkStart(id);
151     }
152 
153     @Override
onExtRadioWorkTimeout(int id)154     public void onExtRadioWorkTimeout(int id) {
155         mCallbackV11.onExtRadioWorkTimeout(id);
156     }
157 
158     @Override
onDppSuccessConfigReceived(ArrayList<Byte> ssid, String password, byte[] psk, int securityAkm)159     public void onDppSuccessConfigReceived(ArrayList<Byte> ssid, String password,
160             byte[] psk, int securityAkm) {
161         if (mStaIfaceHal.getDppCallback() == null) {
162             Log.e(TAG, "onDppSuccessConfigReceived callback is null");
163             return;
164         }
165 
166         WifiConfiguration newWifiConfiguration = new WifiConfiguration();
167 
168         // Set up SSID
169         WifiSsid wifiSsid =
170                 WifiSsid.createFromByteArray(NativeUtil.byteArrayFromArrayList(ssid));
171 
172         newWifiConfiguration.SSID = "\"" + wifiSsid.toString() + "\"";
173 
174         // Set up password or PSK
175         if (password != null) {
176             newWifiConfiguration.preSharedKey = "\"" + password + "\"";
177         } else if (psk != null) {
178             newWifiConfiguration.preSharedKey = psk.toString();
179         }
180 
181         // Set up key management: SAE or PSK
182         if (securityAkm == DppAkm.SAE) {
183             newWifiConfiguration.setSecurityParams(WifiConfiguration.SECURITY_TYPE_SAE);
184         } else if (securityAkm == DppAkm.PSK_SAE || securityAkm == DppAkm.PSK) {
185             newWifiConfiguration.setSecurityParams(WifiConfiguration.SECURITY_TYPE_PSK);
186         } else {
187             // No other AKMs are currently supported
188             onDppFailure(DppFailureCode.NOT_SUPPORTED);
189             return;
190         }
191 
192         // Set up default values
193         newWifiConfiguration.creatorName = mContext.getPackageManager()
194                 .getNameForUid(Process.WIFI_UID);
195         newWifiConfiguration.status = WifiConfiguration.Status.ENABLED;
196 
197         mStaIfaceHal.getDppCallback().onSuccessConfigReceived(newWifiConfiguration);
198     }
199 
200     @Override
onDppSuccessConfigSent()201     public void onDppSuccessConfigSent() {
202         if (mStaIfaceHal.getDppCallback() != null) {
203             mStaIfaceHal.getDppCallback().onSuccess(DppSuccessCode.CONFIGURATION_SENT);
204         } else {
205             Log.e(TAG, "onSuccessConfigSent callback is null");
206         }
207     }
208 
209     @Override
onDppProgress(int code)210     public void onDppProgress(int code) {
211         if (mStaIfaceHal.getDppCallback() != null) {
212             mStaIfaceHal.getDppCallback().onProgress(code);
213         } else {
214             Log.e(TAG, "onDppProgress callback is null");
215         }
216     }
217 
218     @Override
onDppFailure(int code)219     public void onDppFailure(int code) {
220         if (mStaIfaceHal.getDppCallback() != null) {
221             mStaIfaceHal.getDppCallback().onFailure(code, null, null, null);
222         } else {
223             Log.e(TAG, "onDppFailure callback is null");
224         }
225     }
226 }
227