前言:之前在 (四十四)Android O WiFi启动流程梳理 梳理到了WifiNative的setupForClientMode ,没有继续梳理下去,现在接着梳理。
1. 流程梳理
1.1 WifiNative
setupForClientMode
/**
* Setup wifi native for Client mode operations.
*
* 1. Starts the Wifi HAL and configures it in client/STA mode.
* 2. Setup Wificond to operate in client mode and retrieve the handle to use for client
* operations.
*
* @return Pair of <Integer, IClientInterface> to indicate the status and the associated wificond
* client interface binder handler (will be null on failure).
*/
public Pair<Integer, IClientInterface> setupForClientMode() {
if (!startHalIfNecessary(true)) {
Log.e(mTAG, "Failed to start HAL for client mode");
return Pair.create(SETUP_FAILURE_HAL, null);
}
IClientInterface iClientInterface = mWificondControl.setupDriverForClientMode();
if (iClientInterface == null) {
return Pair.create(SETUP_FAILURE_WIFICOND, null);
}
return Pair.create(SETUP_SUCCESS, iClientInterface);
}
startHalIfNecessary
/**
* Bring up the Vendor HAL and configure for STA mode or AP mode, if vendor HAL is supported.
*
* @param isStaMode true to start HAL in STA mode, false to start in AP mode.
* @return false if the HAL start fails, true if successful or if vendor HAL not supported.
*/
private boolean startHalIfNecessary(boolean isStaMode) {
if (!mWifiVendorHal.isVendorHalSupported()) {
Log.i(mTAG, "Vendor HAL not supported, Ignore start...");
return true;
}
return mWifiVendorHal.startVendorHal(isStaMode);
}
1.2 WifiVendorHal
startVendorHal
/**
* Bring up the HIDL Vendor HAL and configure for STA mode or AP mode.
*
* @param isStaMode true to start HAL in STA mode, false to start in AP mode.
*/
public boolean startVendorHal(boolean isStaMode) {
synchronized (sLock) {
if (mIWifiStaIface != null) return boolResult(false);
if (mIWifiApIface != null) return boolResult(false);
if (!mHalDeviceManager.start()) {
return startFailedTo("start the vendor HAL");
}
IWifiIface iface;
if (isStaMode) {
mIWifiStaIface = mHalDeviceManager.createStaIface(null, null);
if (mIWifiStaIface == null) {
return startFailedTo("create STA Iface");
}
iface = (IWifiIface) mIWifiStaIface;
if (!registerStaIfaceCallback()) {
return startFailedTo("register sta iface callback");
}
mIWifiRttController = mHalDeviceManager.createRttController(iface);
if (mIWifiRttController == null) {
return startFailedTo("create RTT controller");
}
if (!registerRttEventCallback()) {
return startFailedTo("register RTT iface callback");
}
enableLinkLayerStats();
} else {
mIWifiApIface = mHalDeviceManager.createApIface(null, null);
if (mIWifiApIface == null) {
return startFailedTo("create AP Iface");
}
iface = (IWifiIface) mIWifiApIface;
}
mIWifiChip = mHalDeviceManager.getChip(iface);
if (mIWifiChip == null) {
return startFailedTo("get the chip created for the Iface");
}
if (!registerChipCallback()) {
return startFailedTo("register chip callback");
}
mLog.i("Vendor Hal started successfully");
return true;
}
}
先看下HalDeviceManager的start方法
1.3 HalDeviceManager
start
/**
* Attempts to start Wi-Fi (using HIDL). Returns the success (true) or failure (false) or
* the start operation. Will also dispatch any registered ManagerStatusCallback.onStart() on
* success.
*
* Note: direct call to HIDL.
*/
public boolean start() {
return startWifi();
}
startWifi
private boolean startWifi() {
if (DBG) Log.d(TAG, "startWifi");
synchronized (mLock) {
try {
if (mWifi == null) {
Log.w(TAG, "startWifi called but mWifi is null!?");
return false;
} else {
int triedCount = 0;
while (triedCount <= START_HAL_RETRY_TIMES) {
WifiStatus status = mWifi.start();
if (status.code == WifiStatusCode.SUCCESS) {
initIWifiChipDebugListeners();
managerStatusListenerDispatch();
if (triedCount != 0) {
Log.d(TAG, "start IWifi succeeded after trying "
+ triedCount + " times");
}
return true;
} else if (status.code == WifiStatusCode.ERROR_NOT_AVAILABLE) {
// Should retry. Hal might still be stopping.
Log.e(TAG, "Cannot start IWifi: " + statusString(status)
+ ", Retrying...");
try {
Thread.sleep(START_HAL_RETRY_INTERVAL_MS);
} catch (InterruptedException ignore) {
// no-op
}
triedCount++;
} else {
// Should not retry on other failures.
Log.e(TAG, "Cannot start IWifi: " + statusString(status));
return false;
}
}
Log.e(TAG, "Cannot start IWifi after trying " + triedCount + " times");
return false;
}
} catch (RemoteException e) {
Log.e(TAG, "startWifi exception: " + e);
return false;
}
}
}
看下mWifi是什么
/**
* Wrapper function to access the HIDL services. Created to be mockable in unit-tests.
*/
protected IWifi getWifiServiceMockable() {
try {
return IWifi.getService();
} catch (RemoteException e) {
Log.e(TAG, "Exception getting IWifi service: " + e);
return null;
}
}
hidl对应的服务端。
看下IWifi的导包:import android.hardware.wifi.V1_0.IWifi;
然后全局搜了下代码发现:
算了,还是先去看下hidl和hal是个啥吧。。。
待续。。。