• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 com.android.server.wifi.mockwifi;
18 
19 import android.content.Context;
20 import android.net.wifi.WifiScanner;
21 import android.net.wifi.nl80211.WifiNl80211Manager;
22 import android.os.IBinder;
23 import android.util.Log;
24 
25 import com.android.modules.utils.build.SdkLevel;
26 import com.android.server.wifi.WifiMonitor;
27 
28 import java.util.HashSet;
29 import java.util.Set;
30 
31 /**
32  * Mocked WifiNl80211Manager
33  */
34 public class MockWifiNl80211Manager {
35     private static final String TAG = "MockWifiNl80211Manager";
36 
37     private Context mContext;
38     private WifiNl80211Manager mMockWifiNl80211Manager;
39     private final WifiMonitor mWifiMonitor;
40     private Set<String> mConfiguredMethodSet = new HashSet<>();
41 
MockWifiNl80211Manager(IBinder wificondBinder, Context context, WifiMonitor wifiMonitor)42     public MockWifiNl80211Manager(IBinder wificondBinder, Context context,
43             WifiMonitor wifiMonitor) {
44         mContext = context;
45         mWifiMonitor = wifiMonitor;
46         if (SdkLevel.isAtLeastU()) {
47             mMockWifiNl80211Manager = new WifiNl80211Manager(mContext, wificondBinder);
48             for (String ifaceName : mWifiMonitor.getMonitoredIfaceNames()) {
49                 Log.i(TAG, "Mock setupInterfaceForClientMode for iface: " + ifaceName);
50                 mMockWifiNl80211Manager.setupInterfaceForClientMode(ifaceName, Runnable::run,
51                         new NormalScanEventCallback(ifaceName),
52                         new PnoScanEventCallback(ifaceName));
53             }
54         }
55     }
56 
getWifiNl80211Manager()57     public WifiNl80211Manager getWifiNl80211Manager() {
58         return mMockWifiNl80211Manager;
59     }
60 
61     /**
62      * Reset mocked methods.
63      */
resetMockedMethods()64     public void resetMockedMethods() {
65         mConfiguredMethodSet.clear();
66     }
67 
68     /**
69      * Adds mocked method
70      *
71      * @param method the method name is updated
72      */
addMockedMethod(String method)73     public void addMockedMethod(String method) {
74         mConfiguredMethodSet.add(method);
75     }
76 
77     /**
78      * Whether or not the method is mocked. (i.e. The framework should call this mocked method)
79      *
80      * @param method the method name.
81      */
isMethodConfigured(String method)82     public boolean isMethodConfigured(String method) {
83         return mConfiguredMethodSet.contains(method);
84     }
85 
86     private class NormalScanEventCallback implements WifiNl80211Manager.ScanEventCallback {
87         private String mIfaceName;
88 
NormalScanEventCallback(String ifaceName)89         NormalScanEventCallback(String ifaceName) {
90             mIfaceName = ifaceName;
91         }
92 
93         @Override
onScanResultReady()94         public void onScanResultReady() {
95             Log.d(TAG, "Scan result ready event " + mIfaceName);
96             mWifiMonitor.broadcastScanResultEvent(mIfaceName);
97         }
98 
99         @Override
onScanFailed()100         public void onScanFailed() {
101             Log.d(TAG, "Scan failed event " + mIfaceName);
102             mWifiMonitor.broadcastScanFailedEvent(mIfaceName, WifiScanner.REASON_UNSPECIFIED);
103         }
104 
105         @Override
onScanFailed(int errorCode)106         public void onScanFailed(int errorCode) {
107             Log.d(TAG, "Scan failed event: errorCode: " + errorCode);
108             mWifiMonitor.broadcastScanFailedEvent(mIfaceName, errorCode);
109         }
110     }
111 
112     private class PnoScanEventCallback implements WifiNl80211Manager.ScanEventCallback {
113         private String mIfaceName;
114 
PnoScanEventCallback(String ifaceName)115         PnoScanEventCallback(String ifaceName) {
116             mIfaceName = ifaceName;
117         }
118 
119         @Override
onScanResultReady()120         public void onScanResultReady() {
121             Log.d(TAG, "Pno scan result event");
122             mWifiMonitor.broadcastPnoScanResultEvent(mIfaceName);
123         }
124 
125         @Override
onScanFailed()126         public void onScanFailed() {
127             Log.d(TAG, "Pno Scan failed event");
128         }
129     }
130 }
131