From: http://blog.csdn.net/eastmoon502136/article/details/8721510
WLAN Composition
Call the Wi-Fi application layer interface
First, from the application of Android wifi on the upper layer, a mwifimanager object will be instantiated Based on the wifimanager class of Android. This object processes all the tasks that need to be processed by WiFi, for example, enabling wifi, then mwifimanager will be called. iswifienabled (); determines whether WiFi is enabled. If not, mwifimanager is called. setwifienabled (true); To enable wifi. The wifiservice method will be called here, which is to set the WiFi enabling, mservice. setwifienabled. Then this function will continue to call the mwifistatemachine Method for enabling settings in the WiFi state machine. setwifienabled. In the WiFi state machine, this method mainly sends two messages to the state machine, cmd_load_driver and cmd_start_supplicant. It means loading the driver and starting wpa_supplicant. The rest is what is done in the WiFi state machine. This is analyzed below. There are other things, such as disabling WiFi, scanning, connecting to the network, and disconnecting the network. You can refer to the following simple flowchart. The specific implementation can trace the code.
About the framework layer state machine of WiFi
For the main state machine of the WiFi framework layer, see the figure below.
Here we will mainly introduce the implementation of some major state machines of wifistatemachine.
The initial status is initial, so the initial enter () is executed at the beginning. Because the WiFi driver is compiled into the kernel during porting on our platform, wifinative. the isdriverloaded () function must be true. Then the status is converted to transitionto (mdriverloadedstate ).
The status of the driver is loaded. The processmessage here will process the message. Combined with the message sent by the above app when enabling WiFi, pai_start_supplicant will be processed here. Next, we will download firmware.
Mnwservice. wififirmwarereload (minterfacename, "sta"); Because firmware of Our wifi driver is implemented internally, it is ignored here. In wifinative. startsupplicant (); The Hal layer is called to enable wpa_supplicant. Next is mwifimonitor. startmonitoring (); to enable a monitor, the main task is to process the events reported by wpa_supplicant. Then, the status changes again, that is
Transitionto (msupplicantstartingstate );
Supplicantstartingstate. If wpa_supplicant is successfully started, the monitor will report a wifimonitor. sup_connection_event, And the status will be changed, that is, transitionto (mdriverstartedstate). The details are as follows:
Case wifimonitor. sup_connection_event:
If (DBG) log ("supplicant connection established ");
Setwifistate (wifi_state_enabled );
Msupplicantrestartcount = 0;
/* Reset the supplicantstate to indicate the supplicant
* State is not known atthis time */
Msupplicantstatetracker. sendmessage (pai_reset_supplicant_state );
Mwpsstatemachine. sendmessage (pai_reset_wps_state );
/* Initialize datastructures */
Mlastbssid = NULL;
Mlastnetworkid = wificonfiguration. invalid_network_id;
Mlastsignallevel =-1;
Mwifiinfo. setmacaddress (wifinative. getmacaddresscommand ());
Wificonfigstore. initialize (mcontext );
Sendsupplicantconnectionchangedbroadcast (true );
Transitionto (mdriverstartedstate );
It is in driverstartedstate. A lot of processing is done here: one of them is pai_start_scan, which is to start scanning.
For the state machine, most of the state diagrams are shown below.
About the JNI layer of Wi-Fi
The JNI layer encapsulates many interfaces and allows upper-layer Java code to call the C or C ++ code of the Hal layer. Here, he encapsulates the commands and some controls. Its main interfaces can be viewed.
About the Hal layer of WiFi
The Hal layer of wifi is mainly called by the upper layer JNI to wpa_supplicant, as shown in.
General workflow of wpa_supplicant
About wpa_supplicantSocket and Hal communication process
The above briefly introduces the entire WiFi mechanism of Android in the form of flow and graphic text. You can analyze the code. Because there have been a lot of instructions on the internet, it won't be too much analysis here.
Linux driver process for WiFi
For more information, see "Learning Linux WiFi with cainiao". Here we have made a simple analysis of the sdio WiFi data process ..
So far, we have made a simple analysis of the entire WiFi mechanism of Android from the macro to the micro level. Based on these, we believe that the porting and Development of androidwifi is no longer so mysterious and invisible.