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; 18 19 import android.app.Service; 20 import android.content.Context; 21 import android.content.Intent; 22 import android.os.Binder; 23 import android.os.IBinder; 24 import android.util.Log; 25 import android.wifi.mockwifi.nl80211.WifiNL80211ManagerImp; 26 27 import androidx.test.platform.app.InstrumentationRegistry; 28 29 import java.util.concurrent.CountDownLatch; 30 import java.util.concurrent.TimeUnit; 31 32 public class MockWifiModemService extends Service { 33 34 private static final String TAG = "MockWifiModemService"; 35 public static final int TEST_TIMEOUT_MS = 30000; 36 37 public static final int LATCH_MOCK_WIFI_MODEM_SERVICE_READY = 0; 38 public static final int LATCH_WIFI_INTERFACES_READY = 1; 39 public static final int LATCH_MAX = 2; 40 41 public static final String METHOD_IDENTIFIER = ","; 42 public static final String CLASS_IDENTIFIER = "-"; 43 private static final int NUM_MOCKED_INTERFACES = 1; // The number of HAL, now only support 44 // nl80211 HAL 45 46 public static final String NL80211_INTERFACE_NAME = "android.wifi.mockwifimodem.nl80211"; 47 48 private static CountDownLatch[] sLatches; 49 private Object mLock = new Object();; 50 private static Context sContext; 51 private LocalBinder mBinder; 52 53 private static WifiNL80211ManagerImp sWifiNL80211ManagerImp; 54 55 // For local access to this Service. 56 class LocalBinder extends Binder { getService()57 MockWifiModemService getService() { 58 return MockWifiModemService.this; 59 } 60 } 61 62 @Override onCreate()63 public void onCreate() { 64 Log.d(TAG, "Mock Wifi Modem Service Created"); 65 sContext = InstrumentationRegistry.getInstrumentation().getContext(); 66 sLatches = new CountDownLatch[LATCH_MAX]; 67 for (int i = 0; i < LATCH_MAX; i++) { 68 if (i == LATCH_WIFI_INTERFACES_READY) { 69 sLatches[i] = new CountDownLatch(NUM_MOCKED_INTERFACES); // make sure all HAL 70 // simulation is ready 71 } else { 72 sLatches[i] = new CountDownLatch(1); 73 } 74 } 75 mBinder = new LocalBinder(); 76 sWifiNL80211ManagerImp = new WifiNL80211ManagerImp(sContext); 77 } 78 79 @Override onBind(Intent intent)80 public IBinder onBind(Intent intent) { 81 if (NL80211_INTERFACE_NAME.equals(intent.getAction())) { 82 Log.i(TAG, "onBind-NL80211"); 83 countDownLatch(LATCH_WIFI_INTERFACES_READY); 84 return sWifiNL80211ManagerImp; 85 } 86 countDownLatch(LATCH_MOCK_WIFI_MODEM_SERVICE_READY); 87 Log.i(TAG, "onBind-Local"); 88 return mBinder; 89 } 90 countDownLatch(int latchIndex)91 public void countDownLatch(int latchIndex) { 92 if (latchIndex < 0 || latchIndex >= sLatches.length) { 93 Log.e(TAG, "invalid latch index: " + latchIndex); 94 return; 95 } 96 synchronized (mLock) { 97 sLatches[latchIndex].countDown(); 98 } 99 } 100 waitForLatchCountdown(int latchIndex)101 public boolean waitForLatchCountdown(int latchIndex) { 102 return waitForLatchCountdown(latchIndex, TEST_TIMEOUT_MS); 103 } 104 waitForLatchCountdown(int latchIndex, long waitMs)105 public boolean waitForLatchCountdown(int latchIndex, long waitMs) { 106 if (latchIndex < 0 || latchIndex >= sLatches.length) { 107 Log.e(TAG, "invalid latch index: " + latchIndex); 108 return false; 109 } 110 boolean complete = false; 111 try { 112 CountDownLatch latch; 113 synchronized (mLock) { 114 latch = sLatches[latchIndex]; 115 } 116 complete = latch.await(waitMs, TimeUnit.MILLISECONDS); 117 } catch (InterruptedException e) { 118 } 119 synchronized (mLock) { 120 sLatches[latchIndex] = new CountDownLatch(1); 121 } 122 return complete; 123 } 124 configureSignalPoll(String ifaceName, int currentRssiDbm, int txBitrateMbps, int rxBitrateMbps, int associationFrequencyMHz)125 public boolean configureSignalPoll(String ifaceName, int currentRssiDbm, int txBitrateMbps, 126 int rxBitrateMbps, int associationFrequencyMHz) { 127 if (sWifiNL80211ManagerImp == null) { 128 return false; 129 } 130 if (sWifiNL80211ManagerImp == null) return false; 131 return sWifiNL80211ManagerImp.configureSignalPoll(ifaceName, currentRssiDbm, txBitrateMbps, 132 rxBitrateMbps, associationFrequencyMHz); 133 } 134 135 /** 136 * Gets all configured methods from all mocked HALs. 137 */ getAllConfiguredMethods()138 public String getAllConfiguredMethods() { 139 // Get configured methods from all mocked HALs. (Now only supports WifiNL80211ManagerImp). 140 return sWifiNL80211ManagerImp.getConfiguredMethods(); 141 } 142 } 143