WiFi loading process after system startup

Source: Internet
Author: User

====================================== WiFi startup code process ========== ================


1. Start the system firstLoad init. RC, This fileAll services are loaded.,Init is the first user space application started in Linux.(It belongs to a Linux Process, not an android application ).

2. There are the following statements in init. RC:

Service wpa_supplicant/system/bin/wpa_supplicant-dwext-iwlan0-D-C/data/MISC/WiFi/wpa_supplicant.conf

3,Load the Linux kernel module/system/lib/modules/WLAN. KO. This WiFi module is defined in/hardware/libhardware_legacy/WiFi/Wifi. c

4.SystemserverAt startup,ConnectivityserviceInstance,
The connectivityservice constructor will createWifiservice,

See how to start the WiFi service:
If (DBG) log. V (TAG, "Starting WiFi service .");
Mwifistatetracker = new wifistatetracker (context, Handler );
Wifiservice = new wifiservice (context, mwifistatetracker );
Servicemanager. addservice (context. wifi_service, wifiservice );

WifistatetrackerWill createReceive wifimonitorEvents from the underlying layer,Wifiservice and wifimonitorIs the core of the entire module.WifiserviceStart to close wpa_supplicant, start to close the wifimonitor monitoring thread andSend the command
Wpa_supplicant,
WhileWifimonitor is responsible for receiving Event Notifications from wpa_supplicant. They are connected to the local database throughJNI Method,The specific implementation method is in android_net_wifi_wifi.cpp.In this file, we can roughly see which commands the app will give to wpa_supplicant. These commands are sent to wpa_supplicant through the wifi_command of wifi. C. During the command sending process, wpa_ctrl_request is called to complete the command sending. wpa_ctrl_request is sent throughSocketWpa_supplicant. Then, wpa_ctrl_recv is used to receive the command from wpa_supplicant, and wifi_wait_for_event is returned.

--------------------------------------

The source code path corresponding to the flowchart is:

The path of wifienabler and wifisettings is as follows:

Rootfs/packages/apps/settings/src/COM/Android/settings/WiFi/

 

Wifimanager, wifimonitor, wifistatetracker, and wifinative. The source code path is as follows:

Rootfs/frameworrks/base/WiFi/Java/Android/NET/WiFi/

 

Location of the code corresponding to wifiservice

Rootfs/frameworks/base/services/Java/COM/Android/Server/

 

Android_net_wifi_wifi source code path:

Rootfs/frameworks/base/CORE/JNI/

 

The source code path of wifi_command and wifi_wait_for_envent is as follows:

/Hardware/libhardware_legacy/WiFi/wifi. c

 

Wpa_ctrl _ source code path:

Rootfs/external/wpa_supplicant/wpa_ctrl.c

 

The source code path of wpa_supplicant is as follows:

Rootfs/external/wpa_supplicant/


WiFi startup flowchart:


1. Enabling WiFi
Wireless Settings inInitializationConfiguredWifienabler to process the WiFi button,When the user presses the WiFi buttonAndroid will call the onpreferencechange of wifienabler, and thenWifienabler calls the setwifienabled interface function of wifimanager. Through aidl, wifiservice actually calls
Setwifienabled Function
,Wifiservice then sends a message_enable_wifi message to itself. In the code that processes the message, do the real enable work: first load the WiFi kernel module(The module is located at "/system/lib/modules/WLAN. Ko "),Then start wpa_supplicant
(
The configuration file is "/data/MISC/WiFi/wpa_supplicant.conf "),Then, wifistatetracker is used to start the monitoring thread in the wifimonitor..

The Code is as follows:
Wifiservice. java (frameworks/base/services/Java/COM/Android/Server) calls 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 (); the message sent to itself.

WhenEnableYesSetwifienabledblocking, This function will do setwifienabledstate, and then do four things:

1. Call wifinative of JNI.Loaddriver --> load the WiFi driver

2. Call wifinative of JNI.Startsupplicant -->StartWifi_start_supplicant

 3. Start event loop.

4. Update the WiFi status

After WiFi is started successfullySetwifienabledblockingRun mwifistatetracker. starteventloop (); event loop, To monitor the event mwifimonitor. startmonitoring ();
Monitorthread (). Start () has been calling wifinative cyclically in the thread.Waitforevent ( );


WhenAfter successful enabling,Broadcast and send
Wifi_state_changed_actionThis intent notifies the outside world that WiFi has been successfully enabled
.WifienablerWhen a wifi_state_changed_action is registered with AndroidThe intent is received to start scanning..


2. Search for an AP
Entry function for scanning
Is startscan of wifiservice, which is actuallySend scan command to wpa_supplicant.
After wpa_supplicant processes the scan command, it sends an event notification to the control channel to complete the scan, so that the wifi_wait_for_event function will receive the event. Therefore, the monitorthread in wifimonitor will be executed to process the event.For each AP returned by a scan,Wifilayer will call wifisettings
To add the AP to the GUI display list.
.


3. Configure AP Parameters
After you select an AP on the wifiset interface, a dialog box for configuring AP parameters is displayed. This dialog box displays the signal strength of the selected AP, if a password is set for this AP, you must enter the password to log on. After the user is configured, click
The onclick function is called.


Iv. Connection
After you select the encryption method and enter the key in acesspointdialog, and then click the connect button, Android will connect to the AP.
Wifilayer checks whether the AP has been configured before.Send the list_network command to wpa_supplicantAndCompare return values to achieve,
// Need wificonfiguration for the AP
Wificonfiguration Config = findconfigurednetwork (State );
If wpa_supplicant does not have the configuration information of this AP, the add_network command will be sent to wpa_supplicant to add this AP,
If (Config = NULL)
{
// Connecting for the first time, need to create it
Config = addconfiguration (State, add_configuration_enable | add_configuration_save );
}
The add_network command returns an ID. wifilayer then uses the returned ID as the parameter to send the enable_network command to wpa_supplicant, so that wpa_supplicant can connect to the AP..


5. Configure the IP address
After wpa_supplicant successfully connects to the AP, it will send an event notification to the control channel to connect to the AP,

Wifi_wait_for_event function of wifi. cBlockingSo that the wifi_wait_for_event function will receive the event. Therefore, the monitorthread in the wifimonitor will be executed to process the event,
Wifimonitor then calls the policystatechange of wifistatetracker,Wifistatetracker then sends the event_dhcp_start message to start DHCP to obtain the IP address., And thenBroadcast againNetwork_state_changed_action
This intent
Finally, the wifisettings Class responds and changes the status and interface information.


Note: wpa_ctrl_request sends the command to wpa_supplicant in Socket mode, and blocks sending and receiving in wpa_supplicant in select mode.

Share:
  • Previous Article: USB WiFi driver transplantation and sdiowifi for Android platform
  • Next article: 5wpa_supplicant Program-Details
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.