1 /* 2 * Copyright (C) 2023 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.hardware.wifi.supplicant.ISupplicant; 20 import android.hardware.wifi.supplicant.ISupplicantStaIface; 21 import android.os.IBinder; 22 import android.util.Log; 23 24 import com.android.server.wifi.WifiMonitor; 25 26 import java.util.HashMap; 27 import java.util.HashSet; 28 import java.util.Map; 29 import java.util.Set; 30 31 /** 32 * Mock Supplicant 33 */ 34 public class MockSupplicantManager { 35 private static final String TAG = "MockSupplicantManager"; 36 private ISupplicant mISupplicant; 37 private final WifiMonitor mWifiMonitor; 38 private Set<String> mConfiguredMethodSet = new HashSet<>(); 39 private Map<String, ISupplicantStaIface> mMockISupplicantStaIfaces = new HashMap<>(); 40 MockSupplicantManager(IBinder supplicantBinder, WifiMonitor wifiMonitor)41 public MockSupplicantManager(IBinder supplicantBinder, WifiMonitor wifiMonitor) { 42 mWifiMonitor = wifiMonitor; 43 mISupplicant = ISupplicant.Stub.asInterface(supplicantBinder); 44 for (String ifaceName : mWifiMonitor.getMonitoredIfaceNames()) { 45 Log.i(TAG, "Mock setupInterfaceForSupplicant for iface: " + ifaceName); 46 try { 47 mMockISupplicantStaIfaces.put(ifaceName, mISupplicant.addStaInterface(ifaceName)); 48 } catch (Exception e) { 49 Log.e(TAG, "Failed to create ISupplicantStaIface due to exception - " + e); 50 } 51 } 52 } 53 54 /** 55 * Return mocked ISupplicantStaIface. 56 */ getMockSupplicantStaIface(String ifaceName)57 public ISupplicantStaIface getMockSupplicantStaIface(String ifaceName) { 58 return mMockISupplicantStaIfaces.get(ifaceName); 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 // TODO: b/315088283 - Need to handle same method name from different aidl. 75 mConfiguredMethodSet.add(method); 76 } 77 78 /** 79 * Whether or not the method is mocked. (i.e. The framework should call this mocked method) 80 * 81 * @param method the method name. 82 */ isMethodConfigured(String method)83 public boolean isMethodConfigured(String method) { 84 return mConfiguredMethodSet.contains(method); 85 } 86 } 87