1 /*
2  * Copyright (C) 2022 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 
17 package android.wifi.mockwifi.nl80211;
18 
19 import android.content.Context;
20 import android.net.wifi.nl80211.DeviceWiphyCapabilities;
21 import android.net.wifi.nl80211.IApInterface;
22 import android.net.wifi.nl80211.IClientInterface;
23 import android.net.wifi.nl80211.IInterfaceEventCallback;
24 import android.net.wifi.nl80211.IWificond;
25 import android.net.wifi.nl80211.IWificondEventCallback;
26 import android.os.IBinder;
27 import android.util.Log;
28 import android.wifi.mockwifi.MockWifiModemService;
29 
30 import java.util.HashMap;
31 import java.util.HashSet;
32 import java.util.List;
33 import java.util.Set;
34 
35 public class WifiNL80211ManagerImp extends IWificond.Stub {
36     private static final String TAG = "WifiNL80211ManagerImp";
37 
38     private static Context sContext;
39     Set<String> mConfiguredMethodSet;
40     private HashMap<String, IClientInterfaceImp> mMockIClientInterfaces = new HashMap<>();
41     private HashMap<String, IWifiScannerImp> mMockIWifiScanners = new HashMap<>();
42 
WifiNL80211ManagerImp(Context context)43     public WifiNL80211ManagerImp(Context context) {
44         sContext = context;
45         mConfiguredMethodSet = new HashSet<>();
46     }
47 
48     @Override
createApInterface(String ifaceName)49     public IApInterface createApInterface(String ifaceName) {
50         // TODO: Mock it when we have a use (test) case.
51         return null;
52     }
53 
54     @Override
createClientInterface(String ifaceName)55     public IClientInterface createClientInterface(String ifaceName) {
56         IClientInterfaceImp mockIClientInterface = new IClientInterfaceImp(ifaceName);
57         IWifiScannerImp mockIWifiScanner = (IWifiScannerImp) mockIClientInterface
58                                             .getWifiScannerImpl();
59         mMockIClientInterfaces.put(ifaceName, mockIClientInterface);
60         mMockIWifiScanners.put(ifaceName, mockIWifiScanner);
61         return mockIClientInterface;
62     }
63 
64     @Override
tearDownApInterface(String ifaceName)65     public boolean tearDownApInterface(String ifaceName) {
66         // TODO: Mock it when we have a use (test) case.
67         return true;
68     }
69 
70     @Override
tearDownClientInterface(String ifaceName)71     public boolean tearDownClientInterface(String ifaceName) {
72         return mMockIClientInterfaces.remove(ifaceName) != null;
73     }
74 
75     @Override
tearDownInterfaces()76     public void tearDownInterfaces() {
77         // TODO: Mock it when we have a use (test) case.
78     }
79 
80     @Override
getAvailable2gChannels()81     public int[] getAvailable2gChannels() {
82         // TODO: Mock it when we have a use (test) case.
83         return null;
84     }
85 
86     @Override
getAvailable5gNonDFSChannels()87     public int[] getAvailable5gNonDFSChannels() {
88         // TODO: Mock it when we have a use (test) case.
89         return null;
90     }
91 
92     @Override
getAvailableDFSChannels()93     public int[] getAvailableDFSChannels() {
94         // TODO: Mock it when we have a use (test) case.
95         return null;
96     }
97 
98     @Override
getAvailable6gChannels()99     public int[] getAvailable6gChannels() {
100         // TODO: Mock it when we have a use (test) case.
101         return null;
102     }
103 
104     @Override
getAvailable60gChannels()105     public int[] getAvailable60gChannels() {
106         // TODO: Mock it when we have a use (test) case.
107         return null;
108     }
109 
110     @Override
registerWificondEventCallback(IWificondEventCallback callback)111     public void registerWificondEventCallback(IWificondEventCallback callback) {
112         // TODO: Mock it when we have a use (test) case.
113     }
114 
115     @Override
unregisterWificondEventCallback(IWificondEventCallback callback)116     public void unregisterWificondEventCallback(IWificondEventCallback callback) {
117         // TODO: Mock it when we have a use (test) case.
118     }
119 
120     @Override
getDeviceWiphyCapabilities(String ifaceName)121     public DeviceWiphyCapabilities getDeviceWiphyCapabilities(String ifaceName) {
122         // TODO: Mock it when we have a use (test) case.
123         return null;
124     }
125 
126     @Override
notifyCountryCodeChanged()127     public void notifyCountryCodeChanged() {
128         // TODO: Mock it when we have a use (test) case.
129     }
130 
131     // CHECKSTYLE:OFF Generated code
132     @Override
UnregisterCallback(IInterfaceEventCallback callback)133     public void UnregisterCallback(IInterfaceEventCallback callback) {
134         // TODO: Mock it when we have a use (test) case.
135     }
136 
137     @Override
RegisterCallback(IInterfaceEventCallback callback)138     public void RegisterCallback(IInterfaceEventCallback callback) {
139         // TODO: Mock it when we have a use (test) case.
140     }
141 
142     @Override
GetClientInterfaces()143     public List<IBinder> GetClientInterfaces() {
144         // TODO: Mock it when we have a use (test) case.
145         return null;
146     }
147 
148     @Override
GetApInterfaces()149     public List<IBinder> GetApInterfaces() {
150         // TODO: Mock it when we have a use (test) case.
151         return null;
152     }
153     // CHECKSTYLE:ON Generated code
154 
configureClientInterfaceMock(String ifaceName, IClientInterfaceImp.ClientInterfaceMock clientInterfaceMock)155     public boolean configureClientInterfaceMock(String ifaceName,
156             IClientInterfaceImp.ClientInterfaceMock clientInterfaceMock) {
157         if (mMockIClientInterfaces ==  null) return false;
158         IClientInterfaceImp clientInterface = mMockIClientInterfaces.get(ifaceName);
159         if (clientInterface == null) return false;
160         Set<String> configuredMethods = clientInterface.setClientInterfaceMock(clientInterfaceMock);
161         if (configuredMethods.isEmpty()) {
162             Log.e(TAG, "No methods overridden in the mock ClientInterface!?");
163             return false;
164         }
165         mConfiguredMethodSet.addAll(configuredMethods);
166         return true;
167     }
168 
169     /**
170      * Configures a mock Wifi scanner interface.
171      */
configureWifiScannerInterfaceMock(String ifaceName, IWifiScannerImp.WifiScannerInterfaceMock wifiScannerInterfaceMock)172     public boolean configureWifiScannerInterfaceMock(String ifaceName,
173             IWifiScannerImp.WifiScannerInterfaceMock wifiScannerInterfaceMock) {
174         if (mMockIWifiScanners == null) {
175             return false;
176         }
177         IWifiScannerImp wifiscanner = mMockIWifiScanners.get(ifaceName);
178         if (wifiscanner == null || wifiScannerInterfaceMock == null) {
179             Log.e(TAG, "WifiScanner interface mock: Null!");
180             return false;
181         }
182         Set<String> configuredMethods = wifiscanner.setWifiScannerInterfaceMock(
183                                         wifiScannerInterfaceMock);
184         if (configuredMethods.isEmpty()) {
185             Log.e(TAG, "No methods overridden in the mock WifiScanner!?");
186             return false;
187         }
188         mConfiguredMethodSet.addAll(configuredMethods);
189         return true;
190     }
191 
getConfiguredMethods()192     public String getConfiguredMethods() {
193         StringBuilder sbuf = new StringBuilder();
194         for (String methodName : mConfiguredMethodSet) {
195             sbuf.append(TAG + MockWifiModemService.CLASS_IDENTIFIER + methodName
196                     + MockWifiModemService.METHOD_IDENTIFIER);
197         }
198         return sbuf.toString();
199     }
200 
201     /**
202      * Trigger the scan ready event to force frameworks to get scan result again.
203      * Otherwise the mocked scan result may not work because the frameworks keep use cache data
204      * since there is no scan ready event.
205      */
mockScanResultReadyEvent(String ifaceName, boolean isPno)206     public void mockScanResultReadyEvent(String ifaceName, boolean isPno) {
207         if (mMockIWifiScanners == null || ifaceName == null) {
208             return;
209         }
210         IWifiScannerImp wifiscanner = mMockIWifiScanners.get(ifaceName);
211         if (wifiscanner == null) {
212             Log.e(TAG, "WifiScanner interface mock: Null!");
213             return;
214         }
215         wifiscanner.mockScanResultReadyEvent(isPno);
216     }
217 }
218