============================ Wifi boot code flow ====================
1, the system boot first load init.rc, this file will load all service,Init is the first Linux boot user space application (belongs to the Linux process, does not belong to Android applications).
2, init.rc in the following sentence:
Service wpa_supplicant/system/bin/wpa_supplicant–dwext–iwlan0–d–c/data/misc/wifi/wpa_supplicant.conf
3. load Linux kernel module/system/lib/modules/wlan.ko This WiFi module is defined in the/HARDWARE/LIBHARDWARE_LEGACY/WIFI/WIFI.C
4. When the Systemserver is started, a connectivityservice instance is generated,
The Connectivityservice constructor creates the Wifiservice,
See how the WiFi service is started:
if (DBG) log.v (TAG, "starting Wifi service.");
Mwifistatetracker = new Wifistatetracker (context, handler);
Wifiservice wifiservice = new Wifiservice (context, mwifistatetracker);
Servicemanager.addservice (Context.wifi_service, wifiservice);
wifistatetracker creates Wifimonitor to receive events from the underlying, Wifiservice and Wifimonitor are the core of the entire module. Wifiservice is responsible for initiating shutdown wpa_supplicant, initiating shutdown wifimonitor monitoring threads, and sending commands to Wpa_supplicant, while Wifimonitor is responsible for Receive event notifications from Wpa_supplicant . Their connection to the local library is through the Jni method , the concrete implementation method in the Android_net_wifi_wifi.cpp , in this file can see roughly what the app gives wpa_supplicant commands. These commands are sent to wpa_supplicant through the Wifi.c Wifi_command, which actually calls Wpa_ctrl_request to complete the command sent in the process of sending the command, wpa_ctrl_request through the Socket to communicate with the wpa_supplicant, and then through the WPA_CTRL_RECV to receive commands from Wpa_supplicant, and return the identity to Wifi_wait_for_event.
--------------------------------------
the source code path for the flowchart is:
The wifienabler,wifisettings corresponds to the following path:
rootfs/packages/apps/settings/src/com/android/settings/wifi/
Wifimanager,wifimonitor,wifistatetracker,wifinative. The corresponding source code path is as follows:
rootfs/frameworrks/base/wifi/java/android/net/wifi/
Wifiservice the location of the corresponding code
rootfs/frameworks/base/services/java/com/android/server/
The Android_net_wifi_wifi source code path is as follows:
rootfs/frameworks/base/core/jni/
The Wifi_command,wifi_wait_for_envent source code path is as follows:
/hardware/libhardware_legacy/wifi/wifi.c
The Wpa_ctrl_ source code path is as follows:
Rootfs/external/wpa_supplicant/wpa_ctrl.c
The Wpa_supplicant source code path is as follows:
rootfs/external/wpa_supplicant/
WiFi boot Flowchart:
One, to enable WIFI
Wireless Settings inInitialization ofThe time configured by theWifienabler to handle the Wifi button.,when the user presses the Wifi button, Android will call Wifienabler's Onpreferencechange, and thencall Wifimanager's setwifienabled interface function by Wifienabler, and by Aidl, the Wifiservice function is actually invoked .,Wifiservice then sends a MESSAGE_ENABLE_WIFI message to itself, doing real work in the code that handles the message: first load the WIFI kernel module(The location of the module is "/system/lib/modules/wlan.ko"),then start wpa_supplicant (Configuration file is "/data/misc/wifi/wpa_supplicant.conf"),and then wifistatetracker to start the monitoring thread in the wifimonitor.。
The code is as follows:
Wifiservice.java (Frameworks/base/services/java/com/android/server) calls setwifienabled () inside the Sendenablemessage ( Enable, True, Binder.getcallinguid ()); To send a message
Message msg = Message.obtain (Mwifihandler,(Enable? Message_enable_wifi:message_disable_wifi), (Persist 1:0), UID);
Msg.sendtotarget () a message sent to itself.
Whenwhen the Enableis invokedsetwifienabledblocking This function, this function will do setwifienabledstate, and then do four things:
1. Invoke the wifinative of JNI. loaddriver--> load WiFi drive
2. Invoke the wifinative of JNI. startsupplicant--> boot wifi_start_supplicant
3. Start Event loop.
4. Update WiFi status after successful boot WiFi setwifienabledblocking run mwifistatetracker.starteventloop (); event loop, To monitor the event mwifimonitor.startmonitoring (); Monitorthread () the start () is called wifinative in the thread loop. Waitforevent ();
When it is successful , it will broadcast the Wifi_state_changed_action this Intent notify the outside WIFI has been successful . When Wifienabler is created, it registers with Android to receive wifi_state_changed_action, so it receives the Intent and begins the scan .
Second, find AP
The scan entry function is the Wifiservice Startscan, which is actually sending SCAN commands to the wpa_supplicant.
when wpa_supplicant finishes processing the SCAN command, it sends an event notification scan to the control channel so that the Wifi_wait_for_event function receives the event, which Wifimonitor Monitorthrea D will be executed to handle this event. for each AP returned by the scan, Wifilayer calls the Wifisettings onaccesspointsetchanged function, which eventually adds the AP to the GUI display list .
Third, configure AP parameters
When a user selects an AP in the Wifisettings interface, a dialog box is displayed that configures the AP parameter, which displays the currently selected AP signal strength and requires the user to enter a password to log in if the AP has a password set. When the user is configured, click the Connect button and the onclick function will be invoked.
Four, the connection
When the user chooses the encryption method in the Acesspointdialog and enters the key, and then clicks the connection button, Android will connect to the AP.
Wifilayer will first detect if the AP was configured before, and this is done by sending the list_network command to the Wpa_supplicant and comparing the return value .
Need Wificonfiguration for the AP
wificonfiguration config = findconfigurednetwork (state);
If Wpa_supplicant does not have the configuration information for this AP, the Add_network command is sent to Wpa_supplicant to add the AP.
if (config = = null)
{
Connecting for the "the" "the", need to create it
Config = addconfiguration (state, add_configuration_enable| Add_configuration_save);
}
The add_network command returns an ID, wifilayer the returned ID as a parameter to send the Enable_network command to the wpa_supplicant, allowing Wpa_supplicant to connect to the ap .
v. Configure IP addresses
When Wpa_supplicant successfully connects to the AP, it sends an event notification to the control channel to connect to the AP.
The wifi.c wifi_wait_for_event function blocks calls so that the Wifi_wait_for_event function receives the event, and Monitorthread in this wifimonitor is executed to handle the This event, Wifimonitor then invokes Wifistatetracker's Notifystatechange, Wifistatetracker then sends a EVENT_DHCP_START message to itself to start DHCP Get the IP address , and then broadcast the Send network_state_changed_action this Intent, finally by the Wifisettings class to respond, change the state and interface information.
Note: Wpa_ctrl_request sends commands to wpa_supplicant via the socket, blocking the sending and receiving in the Wpa_supplicant in select mode.
Share: Previous: Android platform USB WiFi drive porting and use Sdiowifi Next: 5wpa_supplicant Program--Detailed