趁做Android WIFI驱动移植,对Android WIFI框架做了深刻的分析,并做此文档共同学习。
对上层WIFI的应用,基本流程为:(1)WIFI初始化 (2)Wifi启动 (3)开始扫描AP (4)显示扫描的AP (5)配置AP (6)连接AP (7)获取IP地址 (8)上网
一、Android WIFI模块初始化
上文Android系统启动分析 讲到在SystemServer中实例化了ConnectivityService,接着的便是WIFI初始化:
frameworks/base/services/java/com/android/server/ConnectivityService.java
ConnectivityService 的构造函数会创建WifiService,
if (DBG) Log.v(TAG, "Starting Wifi Service.");
WifiStateTracker wst = new WifiStateTracker(context, mHandler);
WifiService wifiService = new WifiService(context, wst);
ServiceManager.addService(Context.WIFI_SERVICE, wifiService);
WifiStateTracker 会创建WifiMonitor 接收来自底层的事件,WifiService 和WifiMonitor 是整个模块的核心。WifiService 负责启动关闭wpa_supplicant、启动关闭WifiMonitor 监视线程和把命令下发给wpa_supplicant,而WifiMonitor 则负责从wpa_supplicant 接收事件通知。
具体流程图如下:
![Android <wbr>WIFI框架分析(1)](https://i-blog.csdnimg.cn/blog_migrate/a4c26d1e5885305701be709a3d33442f.gif)
二、WIFI模块启动
WirelessSettings 在初始化的时候配置了由WifiEnabler 来处理Wifi 按钮 WirelessSettings 在初始化的时候配置了由WifiEnabler 来处理Wifi 按钮
Packages\apps\settings\src\com\android\settings\WirelessSettings.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.wireless_settings);
initToggles();
mAirplaneModePreference = (CheckBoxPreference) findPreference(KEY_TOGGLE_AIRPLANE);
}
private void initToggles() {
…………
mWifiEnabler = new WifiEnabler(
this, (WifiManager) getSystemService(WIFI_SERVICE),
(CheckBoxPreference) wifiPreference);
…………
}
![Android <wbr>WIFI框架分析(1)](https://i-blog.csdnimg.cn/blog_migrate/a4c26d1e5885305701be709a3d33442f.gif)
当用户按下Wifi 按钮后,Android 会调用WifiEnabler 的onPreferenceChange,再由WifiEnabler调用WifiManager 的setWifiEnabled 接口函数,通过AIDL,实际调用的是WifiService 的setWifiEnabled 函数,WifiService 接着向自身发送一条MESSAGE_ENABLE_WIFI 消息,在处理该消息的代码中做真正的使能工作:
首先装载WIFI 内核模块(该模块的位置硬编码为"/system/lib/modules/libertas_sdio.ko" ),
然后启动wpa_supplicant ( 配置文件硬编码为"/data/misc/wifi/wpa_supplicant.conf"),再通过WifiStateTracker 来启动WifiMonitor 中的监视线程。
WifiEnabler.java
public boolean onPreferenceChange(Preference preference, Object value) {
// Turn on/off Wi-Fi
setWifiEnabled((Boolean) value);
}
private void setWifiEnabled(final boolean enable) {
if (!mWifiManager.setWifiEnabled(enable)) {
}
}
WifiManager.java
public boolean setWifiEnabled(boolean enabled) {
return mService.setWifiEnabled(enabled);
}
IWifiManager mService;
interface IWifiManager{
…………
boolean startScan(boolean forceActive);
boolean setWifiEnabled(boolean enable);
…………
}
IWifiManger.aidl编译后生成了IWifiManger.java,并生成IWifiManger.Stub(服务器端抽象类)和IWifiManger.Stub.Proxy(客户端代理实现类)。WifiService通过继承IWifiManger.Stub实现,而客户端通过getService()函数获取IWifiManger.Stub.Proxy(即Service的代理类),将其作为参数传递给WifiManger,供其与WifiService通信时使用。
WifiService.java
public boolean setWifiEnabled(boolean enable) {
…………
sendEnableMessage(enable, true, Binder.getCallingUid());
…………
}
private void sendEnableMessage(boolean enable, boolean persist, int uid) {
Message msg = Message.obtain(mWifiHandler,
(enable ? MESSAGE_ENABLE_WIFI : MESSAGE_DISABLE_WIFI),
(persist ? 1 : 0), uid);
msg.sendToTarget();
}
public void handleMessage(Message msg) {
switch (msg.what) {
case MESSAGE_ENABLE_WIFI:
setWifiEnabledBlocking(true, msg.arg1 == 1, msg.arg2);
case MESSAGE_START_WIFI:
mWifiStateTracker.restart();
case MESSAGE_DISABLE_WIFI:
setWifiEnabledBlocking(false, msg.arg1 == 1, msg.arg2);
case MESSAGE_STOP_WIFI:
mWifiStateTracker.disconnectAndStop();
break;
}
}
}
private boolean setWifiEnabledBlocking(boolean enable, boolean persist, int uid) {
if (enable) {
if (!WifiNative.loadDriver()) {
setWifiEnabledState(WIFI_STATE_UNKNOWN, uid);
}
if (!WifiNative.startSupplicant()) {
WifiNative.unloadDriver();
setWifiEnabledState(WIFI_STATE_UNKNOWN, uid);
}
registerForBroadcasts();
mWifiStateTracker.startEventLoop();
} else {
………………
}
// Success!
setWifiEnabledState(eventualWifiState, uid);
}
private void setWifiEnabledState(int wifiState, int uid) {
// Broadcast
final Intent intent = new Intent(WifiManager.WIFI_STATE_CHANGED_ACTION);
}
JNI android_net_wifi_wifi.cpp
// ----------------------------------------------------------------------------
static JNINativeMethod gWifiMethods[] = {
{ "loadDriver", "()Z", (void *)android_net_wifi_loadDriver },
{ "startSupplicant", "()Z", (void *)android_net_wifi_startSupplicant },
{ "scanResultsCommand", "()Ljava/lang/String;", (void*) android_net_wifi_scanResultsCommand },
{ "reconnectCommand", "()Z", (void *)android_net_wifi_reconnectCommand },
{ "scanCommand", "(Z)Z", (void*) android_net_wifi_scanCommand },
}
1)static jboolean android_net_wifi_loadDriver(JNIEnv* env, jobject clazz)
{
return (jboolean)(::wifi_load_driver() == 0);
}
2)static jboolean android_net_wifi_startSupplicant(JNIEnv* env, jobject clazz)
{
return (jboolean)(::wifi_start_supplicant() == 0);
}
3)WifiStateTracker.java
mWifiStateTracker.startEventLoop();
public void startEventLoop() {
mWifiMonitor.startMonitoring();
}
//WifiMonitor.java
public void startMonitoring() {
new MonitorThread().start();
}
当使能成功后,会广播发送WIFI_STATE_CHANGED_ACTION 这个Intent 通知外界WIFI已经成功使能了。//Success!后广播的。