android wifi之WifiMonitor

通过在网上的学习已经了解了wifi的大致流程,现在学习了wifi的核心类之一WifiMomitor,监控wpa_supplicant的消息并实时作出处理。

首先看该类官方的解释:Listens for events from the wpa_supplicant server, and passes them on to the {@link StateMachine} for handling. Runs in its own thread.专门负责接收来自wpa_supplicant的事件,并将这些消息进行分类再交予StateMachine处理。在WifiStateMachine里面的内部类DriverLoadedState中启动wpa_supplicant之后调用了WifiMonitor的startMonitor()方法。该方法开启了一个新的线程MonitorThread,接受来自wpa_supplicant的事件。该线程源码如下:

class MonitorThread extends Thread {
        public MonitorThread() {
            super("WifiMonitor");
        }

        public void run() {

            if (connectToSupplicant()) {
                // Send a message indicating that it is now possible to send commands
                // to the supplicant
                mStateMachine.sendMessage(SUP_CONNECTION_EVENT);
            } else {
                mStateMachine.sendMessage(SUP_DISCONNECTION_EVENT);
                return;
            }

            //noinspection InfiniteLoopStatement
            for (;;) {
                String eventStr = WifiNative.waitForEvent();

                // Skip logging the common but mostly uninteresting scan-results event
                if (false && eventStr.indexOf(SCAN_RESULTS_STR) == -1) {
                    Log.d(TAG, "Event [" + eventStr + "]");
                }
                if (!eventStr.startsWith(EVENT_PREFIX_STR)) {
                }
               .........
               .........
               .........
            }}}

String eventStr = WifiNative.waitForEvent();这就是接收来自wpa_supplicant的事件然后对该字符串进行分析处理。下面将分别省略号部分的代码进行分析,他们都是一些if和else if的块:

               if (!eventStr.startsWith(EVENT_PREFIX_STR)) { 
                   
if (eventStr.startsWith(WPA_EVENT_PREFIX_STR) &&0 < eventStr.indexOf(PASSWORD_MAY_BE_INCORRECT_STR)) { mStateMachine.sendMessage(AUTHENTICATION_FAILURE_EVENT); } else if (eventStr.startsWith(WPS_OVERLAP_STR)) { mStateMachine.sendMessage(WPS_OVERLAP_EVENT); } else if (eventStr.startsWith(P2P_EVENT_PREFIX_STR)) { handleP2pEvents(eventStr); } else if (eventStr.startsWith(HOST_AP_EVENT_PREFIX_STR)) { handleHostApEvents(eventStr); } continue; }

这种情况里面的第一个if代表该ap密码可能错误。2.在PBC模式下发现WPS重叠错误。3.P2P 事件 4.在handleHostApEvents(eventStr)函数里面分别向wifiStateMachine发送AP-STA-CONNECTED和AP-STA-DISCONNECTED消息。

                String eventName = eventStr.substring(EVENT_PREFIX_LEN_STR);
                int nameEnd = eventName.indexOf(' ');
                if (nameEnd != -1)
                    eventName = eventName.substring(0, nameEnd);
                if (eventName.length() == 0) {
                    if (false) Log.i(TAG, "Received wpa_supplicant event with empty event name");
                    continue;
                }

eventName = eventStr.substring(EVENT_PREFIX_LEN_STR);eventStr去除“CTRL-EVENT-”前缀,后得到eventName字符串。经第一个if处理后eventName变为从开始到空格间的所有字符。若处理后的eventName是空字符串则返回。重新取值。

                int event;
                if (eventName.equals(CONNECTED_STR)) 
                    event
= CONNECTED; else if (eventName.equals(DISCONNECTED_STR)) event = DISCONNECTED; else if (eventName.equals(STATE_CHANGE_STR)) event = STATE_CHANGE; else if (eventName.equals(SCAN_RESULTS_STR)) event = SCAN_RESULTS; else if (eventName.equals(LINK_SPEED_STR)) event = LINK_SPEED; else if (eventName.equals(TERMINATING_STR)) event = TERMINATING; else if (eventName.equals(DRIVER_STATE_STR)) event = DRIVER_STATE; else if (eventName.equals(EAP_FAILURE_STR)) event = EAP_FAILURE; else event = UNKNOWN;

得到事件的内容,将其放进event,以便操作。(1)ap(access point)密码验证已成功完成,数据链接可以使用了。(2)链接失败数据链接不可用。(3)state change指的是supplicant的状态改变了,接下来会调用handleSupplicantChang方法返回现在supplicant的状态,handleSupplicantChang方法的最后调用了notifySupplicantStateChange方法(4)新的ap扫描结果可用了。(5)wpa_supplicant退出了。(8)EAP认证失败。

在该thread方法的最后调用了handleEvent方法处理了其余的event包含DISCONNECTED和CONNECTED(他俩调用了handleNetWorkStateChange方法该方法得到了新的网络状态后调用了notifyNetWorktStateChange方法向wifiStateMachine发送了新状态message)以及SCAN_RESULTS(ap扫描结果,向wifiStateMachine发送了message),unknown(不做任何处理)。两个notify方法都是向wifiStateMachine发送message以告知supplicant或者network状态改变了。在message中加上了新的网络状态。





 

posted on 2012-04-10 09:15  菊者潜之爱  阅读(2631)  评论(0编辑  收藏  举报

导航