Wifi启动以及Wifi状态机

本文详细介绍了Android系统中WiFi服务的启动过程,包括ServerThread如何启动一系列服务,重点解析了WifiService和WifiStateMachine。WifiService是框架核心,处理应用层事件,而WifiStateMachine通过状态机管理WiFi状态,如驱动加载、wpa_supplicant启动、扫描AP等。每个WiFi状态对应一个内部类,通过消息处理进行状态切换,如加载驱动、启动扫描等。
摘要由CSDN通过智能技术生成

第一部分 启动service

</pre><pre name="code" class="java">public class SystemServer {
	//...
	//...
    public static final void init2() {
        Slog.i(TAG, "Entered the Android system server!");
        Thread thr = new ServerThread();
        thr.setName("android.server.ServerThread");
        thr.start();
    }
}



init2()创建了一个线程,线程启动后在run中创建一系列的android services,将网络相关的services注册。

class ServerThread extends Thread {
    private static final String TAG = "SystemServer";
    
    public void run() {

    //...
        try {
        Slog.i(TAG, "Wi-Fi Service");
        wifi = new WifiService(context);
        ServiceManager.addService(Context.WIFI_SERVICE, wifi);
    } catch (Throwable e) {
        reportWtf("starting Wi-Fi Service", e);
    }

    try {
        Slog.i(TAG, "Connectivity Service");
        connectivity = new ConnectivityService(
                context, networkManagement, networkStats, networkPolicy);
        ServiceManager.addService(Context.CONNECTIVITY_SERVICE, connectivity);
        networkStats.bindConnectivityManager(connectivity);
        networkPolicy.bindConnectivityManager(connectivity);
        wifi.checkAndStartWifi();
        wifiP2p.connectivityServiceReady();
    } catch (Throwable e) {
        reportWtf("starting Connectivity Service", e);
    }
    //...
    }

}

第二部分 WifiService
WifiService的代码位置:
frameworks/base/services/java/com/android/server/WifiService.java

WifiService是整个框架的核心。当应用层触发了事件,会调用WifiService里面的方法来处理。
包括加载驱动,启动wpa_supplicant,扫描AP等。
但是实际上都是调用WifiStateMachine里面的方法发送消息。由WifiStateMachine里面描述wifi的内部类去处理消息。
在WifiService.java这个程序中,定义了WifiService这个类,以及它的构造函数(不懂java,不知道是不是叫构造),各种方法的实现。
在WifiService这个类的构造函数中,初始化了WifiStateMachine成员mWifiStateMachine,并重新写了广播事件的onReceiver函数。



第三部分 WifiStateMachine
WifiStateMachine的代码位置:
frameworks/base/wifi/java/android/net/wifi/WifiStateMachine.java
我们知道WifiService中最终都是调用WifiStateMachine里面的方法来实现的。在WifiStateMachine中,需要传递到
wpa_supplicant来实现wifi功能的命令都在这里做了定义。针对每一个wifi状态,都声明了一个内部类去描述。比如:加载驱动,开启wpa_supplicant等。在这个类中可以看到很多状态,

在WifiStateMachine这个类中定义了一些私有类成员,他们都是State类的。这些类成员有各自的初始化的函数。比如:
private State mDriverLoadingState = new DriverLoadingState();
驱动加载时的状态为mDriverLoadingState,它对应的处理类为DriverLoadingState,依次类推。

Class WifiStateMachine{

    //...
    //...
    
   /* Default parent state */
    private State mDefaultState = new DefaultState();
    /* Temporary initial state */
    private State mInitialState = new InitialState();
    /* Unloading the driver */
    private State mDriverUnloadingState = new DriverUnloadingState();
    /* Loading the driver */
    private State mDriverUnloadedState = new DriverUnloadedState();
    /* Driver load/unload failed */
    private State mDriverFailedState = new DriverFailedState();
    /* Driver loading */
    private State mDriverLoadingState = new DriverLoadingState();
    /* Driver loaded */
    private State mDriverLoadedState = new DriverLoadedState();
    /* Driver loaded, waiting for supplicant to start */
    private State mSupplicantStartingState = new SupplicantStartingState();
    /* Driver loaded and supplicant ready */
    private State mSupplicantStartedState = new SupplicantStartedState();
    /* Waiting for supplicant to stop and monitor to exit */
    private State mSupplicantStoppingState = new SupplicantStoppingState();
    /* Driver start issued, waiting for completed event */
    private State mDriverStartingState = new DriverStartingState();
    /* Driver started */
    private State mDriverStartedState = new DriverStartedState();
    /* Driver stopping */
    private State mDriverStoppingState = new DriverStoppingState();
    /* Driver stopped */
    private State mDriverStoppedState = new DriverStoppedState();
    /* Scan for networks, no connection will be established */
    private State mScanModeState = new ScanModeState();
    /* Connecting to an access point */
    private State mConnectModeState = new ConnectModeState();
    /* Fetching IP after network connection (assoc+auth complete) */
    private State mConnectingState = new ConnectingState();
    /* Connected with IP addr */
    private State mConnectedState = new ConnectedState();
    /* disconnect issued, waiting for network disconnect confirmation */
    private State mDisconnectingState = new DisconnectingState();
    /* Network is not connected, supplicant assoc+auth is not complete */
    private State mDisconnectedState = new DisconnectedState();
    /* Waiting for WPS to be completed*/
    private State mWaitForWpsCompletionState = new WaitForWpsCompletionState();

    /* Soft ap is starting up */
    private State mSoftApStartingState = new SoftApStartingState();
    /* Soft ap is running */
    private State mSoftApStartedState = new SoftApStartedState();
    /* Soft ap is running and we are waiting for tether notification */
    private State mTetheringState = new TetheringState();
    /* Soft ap is running and we are tethered through connectivity service */
    private State mTetheredState = new TetheredState();
    /* Waiting for untether confirmation to stop soft Ap */
    private State mSoftApStoppingState = new SoftApStoppingState();

    /* Wait till p2p is disabled */
    private State mWaitForP2pDisableState = new WaitForP2pDisableState();
    
    //...
    //...
}

//驱动正在加载的状态
class DriverLoadingState extends State{
        //...
        //...
}
//驱动已经加载的状态
class DriverLoadedState extends State {
        //...
        //...
}
//驱动正在卸载的状态
class DriverUnloadingState extends State {
        //...
        //...
}
//驱动已经卸载的状态
class DriverUnloadedState extends State {
        //...
        //...
}
//wpa_supplicant正在开启的状态
class SupplicantStartingState extends State {
        //...
        //...
}
//wpa_supplicant已经开启的状态
class SupplicantStartedState extends State {
        //...
        //...
}

还有很多类似的状态类,它们的状态不同,但是它们的结构却是相同的,每一个状态类都是由
State, enter, processMessage组成。以加载驱动为例:

class DriverLoadingState extends State {
    @Override
    public void enter() {
            //...
            //...
    }

    @Override
    public boolean processMessage(Message message) {
            //...
            //...
    }
}


WifiStateMachine使用sendMessage()来发送消息,使用transitionTo()来转换状态。
状态切换后就根据相应状态里面的processMessage来做一些相应的操作,然后再切换状态。

我们以开启wifi并开始扫描AP为例,(略过了通过WifiNative加载驱动那一段,在另一篇博文中有记录),看一下整个状态机的流程。





1.在WifiService这个类中,有一个方法 setWifiEnabled,这个就是开启wifi的接口
代码位置:frameworks/base/services/java/com/android/server/WifiService.java

    public synchronized boolean setWifiEnabled(boolean enable) {
        enforceChangePermission();
        if (DBG) {
            Slog.e(TAG, "Invoking mWifiStateMachine.setWifiEnabled\n");
        }

        if (enable) {
            reportStartWorkSource();
        }
        mWifiStateMachine.setWifiEnabled(enable); //注意这里,mWifiStateMachine是WifiService这个类中的一个成员。这里调用这个类成员的setWifiEnabled方法去使能wifi.
                                                  //所以这里我们要去WifiStateMachine去找个方法。
        /*
         * Caller might not have WRITE_SECURE_SETTINGS,
         * only CHANGE_WIFI_STATE is enforced
         */

        /* Avoids overriding of airplane state when wifi is already in the expected state */
        if (enable != mWifiEnabled) {
            long ident = Binder.clearCallingIdentity();
            persistWifiState(enable);
            Binder.restoreCalling
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值