android9:wifi开关打开流程(一)
背景:查看Android9原生系统设置WiFi源码,查看WiFi开关打开工作流程。
借鉴:https://blog.csdn.net/weixin_42093428/article/details/80822632?utm_medium=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.nonecase&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.nonecase
一、框架
1、应用层
/packages/apps/Settings/src/com/android/settings/wifi
2、java框架层
/frameworks/base/wifi/java/android/net/wifi
/frameworks/opt/net/wifi/service/java/com/android/server/wifi
3、HIDL层
Wifi到AndoidO之后不再使用jni,所以AndroidP也一样不再使用jni来实现Java代码与本地的C/C++代码交互,而是使用HIDL,Hardware Interface Define Language。
/hardware/interfaces/wifi
4、WPA适配层(硬件抽象层 HAL)
/frameworks/opt/net/wifi/libwifi_hal
5、wpa_supplicant
/external/wpa_supplicant_8
6、Linux Kernel(路径应该不对,后面再看)
kernel/driver/net/wireless/
vendor/gcom/opensource/wlan/
二、启动流程:
wifi服务在SystemService中启动,源码路径:
frameworks/base/services/java/com/android/server/SystemServer.java
先从打开开关入手:
(1)WifiSettings.java WiFi开关入口
/packages/apps/Settings/src/com/android/settings/wifi/WifiSettings.java
@Override
public void onStart() {
super.onStart();
// On/off switch is hidden for Setup Wizard (returns null)
mWifiEnabler = createWifiEnabler();
}
private WifiEnabler createWifiEnabler() {
final SettingsActivity activity = (SettingsActivity) getActivity();
return new WifiEnabler(activity, new SwitchBarController(activity.getSwitchBar()),
mMetricsFeatureProvider);
}
(2)WifiEnabler.java重写onSwitchToggled()方法
/packages/apps/Settings/src/com/android/settings/wifi/WifiEnabler.java
@Override
public boolean onSwitchToggled(boolean isChecked) {
if (!mWifiManager.setWifiEnabled(isChecked)) {
// Error
mSwitchWidget.setEnabled(true);
Toast.makeText(mContext, R.string.wifi_error, Toast.LENGTH_SHORT).show();
}
return true;
}
(3)WifiManager.java 调用Wifiservice方法。
/frameworks/base/wifi/java/android/net/wifi/WifiManager.java
public boolean setWifiEnabled(boolean enabled) {
try {
return mService.setWifiEnabled(mContext.getOpPackageName(), enabled);
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
(4)IWifiManager.java 声明setWifiEnabled()方法
/frameworks/base/wifi/java/android/net/wifi/IWifiManager.aidl
boolean setWifiEnabled(String packageName, boolean enable);
(5)WifiServiceImpl.java setWifiEnabled()方法得实现
/frameworks/opt/net/wifi/service/java/com/android/server/wifi/WifiServiceImpl.java
@Override
public synchronized boolean setWifiEnabled(String packageName, boolean enable)
throws RemoteException {
mWifiController.sendMessage(CMD_WIFI_TOGGLED);
return true;
}
(5)WifiController.java,处理CMD_WIFI_TOGGLED消息
/frameworks/opt/net/wifi/service/java/com/android/server/wifi/WifiController.java
class StaDisabledState extends State {
@Override
public boolean processMessage(Message msg) {
switch (msg.what) {
case CMD_WIFI_TOGGLED:
//切换到DeviceActiveState状态
transitionTo(mDeviceActiveState);
break;
}
}
class DeviceActiveState extends State {
@Override
public void enter() {
mWifiStateMachinePrime.enterClientMode();
mWifiStateMachine.setHighPerfModeEnabled(false);
}
}
(6)WifiStateMachinePrime.java,enterClientMode()方法给ModeStateMachine状态机发送CMD_START_CLIENT_MODE消息
/frameworks/opt/net/wifi/service/java/com/android/server/wifi/WifiStateMachinePrime.java
public void enterClientMode() {
changeMode(ModeStateMachine.CMD_START_CLIENT_MODE);
}
private class ModeStateMachine extends StateMachine {
private boolean checkForAndHandleModeChange(Message message) {
switch(message.what) {
case ModeStateMachine.CMD_START_CLIENT_MODE:
Log.d(TAG, "Switching from " + getCurrentMode() + " to ClientMode");
mModeStateMachine.transitionTo(mClientModeActiveState);
break;
}
}
class ClientModeActiveState extends ModeActiveState {
@Override
public void enter() {
Log.d(TAG, "Entering ClientModeActiveState");
mListener = new ClientListener();
mManager = mWifiInjector.makeClientModeManager(mListener);
mManager.start();
mActiveModeManagers.add(mManager);
updateBatteryStatsWifiState(true);
}
}
(7)WifiInjector.Java
/frameworks/opt/net/wifi/service/java/com/android/server/wifi/WifiStateMachinePrime.java
public ClientModeManager makeClientModeManager(ClientModeManager.Listener listener) {
return new ClientModeManager(mContext, mWifiStateMachineHandlerThread.getLooper(),
mWifiNative, listener, mWifiMetrics, mScanRequestProxy, mWifiStateMachine);
}
(8)ClientModeManager.java ,调用start函数,发送CMD_START消息
/frameworks/opt/net/wifi/service/java/com/android/server/wifi/ClientModeManager.java
/**
* Start client mode.
*/
public void start() {
mStateMachine.sendMessage(ClientModeStateMachine.CMD_START);
}
private class IdleState extends State {
@Override
public boolean processMessage(Message message) {
switch (message.what) {
case CMD_START:
//调用wifi native 方法
mClientInterfaceName = mWifiNative.setupInterfaceForClientMode(
false /* not low priority */, mWifiNativeInterfaceCallback);
break;
}
}
}
(9)WifiNative.java ,启动Supplicant
/frameworks/opt/net/wifi/service/java/com/android/server/wifi/ClientModeManager.java
public String setupInterfaceForClientMode(boolean lowPrioritySta,
@NonNull InterfaceCallback interfaceCallback) {
if (!startSupplicant()) {
Log.e(TAG, "Failed to start supplicant");
mWifiMetrics.incrementNumSetupClientInterfaceFailureDueToSupplicant();
return null;
}
}
本文地址:https://blog.csdn.net/zhuowalun8427/article/details/107179900