Android WiFi Module Analysis

Source: Internet
Author: User
Tags get ip gtk

One: what is WiFi

WiFi is a wireless connection technology, can be used in mobile phones, computers, PDA and other terminals. The purpose of WiFi technology is to improve the interoperability of wireless network products based on the IEEE802.11 standard, which means that WiFi is based on the 802.11 standard, but WiFi is not the same as wireless network.

Second:the WiFi module under the Android platform

Simply introduce the basic features of the WiFi module:

1. Switch WiFi
In addition to the WiFi Settings screen can switch WiFi, there are other ways to set, to see whether these switch status is consistent. There is also the impact of the flight mode on the WiFi switch, because wifi on and off both have a time process, and the flight mode switch instantaneous completion, so there are sometimes conflicts.

2. Switch to new available network alerts
The new available network is defined as an encrypted network that has never been discovered since the WiFi module was turned on. Reminders are only available if the definition of a new network is met.

3. Connect to Disconnect Network
Connection disconnects various types of encryption networks (specific types are explained below)

4. Manually add a network
Requires the router to turn off SIID broadcasts. You can manually enter SIID, network encryption type, password. For Opal phones, the router hides the SSID, and the manually added network cannot be connected.

5. Search the Network
Click the Search button manually to search the network, or wait for the WiFi module to search the network automatically.

6. Hibernate settings
Due to the WiFi module is a large power, all in order to save power, Android WiFi plus a sleep policy, you can set to always open, charging when not disconnected and lock screen when disconnected. To test whether the hibernation setting is valid, you can ping the phone's ip,ping on the router to be connected. Opal mobile phone's sleep strategy is completely invalid, and now the situation is that regardless of which will remain connected, lock screen after 15 minutes to hibernate.

7. Set the static IP
There is a problem with the input restrictions on the IP settings in the Android system, which I have always considered to be the limit of mental retardation. The normal IP range is between 0-255 and Android limits the IP input to an integer of 0 to an integer of 255, meaning that 0000.000200.001.001 such an IP can be legitimately entered.

Three: in-depth understanding of WiFi module a little bit

1, the basic operating process of WiFi

"Initialize"

1 when Systemserver is started, an instance of Connectivityservice is generated.

The constructor for the 2 Connectivityservice creates the Wifiservice.

3 Wifistatetracker creates wifimonitor to receive events from the underlying, Wifiservice and Wifimonitor are the core of the entire module. Wifiservice is responsible for initiating the shutdown wpa_supplicant, starting the Shutdown Wifimonitor monitoring thread and sending the command to Wpa_supplicant, while Wifimonitor is responsible for receiving event notifications from Wpa_supplicant.

"Connect AP"

1 wirelesssettings configured by Wifienabler to handle the WiFi button at initialization time.

2 When the user presses the WiFi button, Android calls Wifienabler's Onpreferencechange, which is then called by Wifienabler Wifimanager setwifienabled interface function, via Aidl, The actual call is Wifiservice's setwifienabled function, Wifiservice then sends itself a MESSAGE_ENABLE_WIFI message, doing the real work in the code that handles the message: first load the WiFi kernel module ( The module's position is hardcoded to "/system/lib/modules/wlan.ko"), and then start Wpa_supplicant (the configuration file is hardcoded to "/data/misc/wifi/wpa_supplicant.conf") and then the W Ifistatetracker to start the monitoring thread in the wifimonitor.

3 when enabled to be successful, the broadcast sends wifi_state_changed_action this intent notifies the outside WiFi has been successfully enabled. When Wifienabler is created, it registers with Android to receive wifi_state_changed_action, so it receives the intent and starts the scan.

"Find AP"

The entry function for the 1 scan is the Wifiservice Startscan, which in fact sends the scan command to Wpa_supplicant.

2 when wpa_supplicant finishes processing the scan command, it sends an event notification scan to the control channel to complete, so that the Wifi_wait_for_event function receives the event. The monitorthread in this wifimonitor will be executed to come out of this event.

3 Wifistatetracker then the broadcast sends scan_results_available_action this intent.

4 Wifilayer registered to receive scan_results_available_action this intent, so its related handler function handlescanresultsavailable will be called, in this function, We'll get the results of the scan first (and finally the wpa_supplicant send the Scan_result command and read the return value),list<scanresult> List = Mwifimanager.getscanresults (); The ap,wifilayer that is returned for each scan calls Wifisettings's onaccesspointsetchanged function, This will eventually add the AP to the GUI display list.

"Configure AP Parameters"

When the user selects an AP on the Wifisettings interface, a dialog box is displayed that configures the AP parameters.

Showaccesspointdialog (state, Accesspointdialog.mode_info);

Connection

When the user chooses the encryption method and enters the key in the Acesspointdialog, then clicks the connection button, the Android will connect this AP.

1 Wifilayer detects if the AP was previously configured, this is done by sending the list_network command to 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

The 2 add_network command returns a Id,wifilayer and then sends the Enable_network command to wpa_supplicant with the returned ID as a parameter, allowing Wpa_supplicant to connect to the AP.

"Configure IP Address"

1 when the Wpa_supplicant successfully connects to the AP, it sends an event notification to the control channel to connect to the AP, thus the Wifi_wait_for_event function receives the event, The monitorthread in this wifimonitor will be executed to come out of this event.

2 Wifimonitor again calls Wifistatetracker Notifystatechange,wifistatetracker then sends itself a EVENT_DHCP_START message to start DHCP to obtain an IP address.

3 then broadcast to send network_state_changed_action this intent.

4 Wifilayer Register the receive network_state_changed_action this intent, so its related processing function handlenetworkstatechanged will be called, when DHCP gets the IP address, The event_dhcp_succeeded message will be sent again.

5 Wifilayer processing the event_dhcp_succeeded message, it will broadcast the send again.

So far, the entire connection process is complete.

2, Wpa_supplicant

The WiFi control framework used by the Android platform is based on the famous wpa_supplicant, which is a security middleware that provides a unified security mechanism for a variety of wireless cards, as shown in:

Corresponding to the above structure, the WiFi control in Android-based phones is divided into three main components:

1 client programs, including the WPA_CLI command line or Java GUI program, communicate with the Wpa_supplicant Daemon service via a UNIX local socket, sending commands and receiving results.

2 wpa_supplicant daemon Service, corresponding to the above intermediate section, the function is "upload release". All clients control the hardware network card through it, send a string command to control whether the AP is scanned, extract the scan results and whether the AP is associated with the operation, while the driver's execution status is sent to the user. The service is designed to support a variety of wireless card chips, so each vendor provides a common interface to wpa_supplicant calls.

3 NIC driver.

In the/etc/wpa_supplicant.conf of cell phone memory, we can directly see the type of network supported by WiFi, each type has examples, such as:

#Both Wpa-psk and Wpa-eap is accepted. Only CCMP is accepted as pairwise and
# Group Cipher.
#network ={
# ssid= "Example"
# bssid=00:11:22:33:44:55
# PROTO=WPA RSN
# KEY_MGMT=WPA-PSK Wpa-eap
# pairwise=ccmp
# group=ccmp
# PSK=06B4BE19DA289F475AA46A33CB793029D4AB3DB7A23EE92382EB0106C72AC7BB
#}

Different types of networks, different parameters and so on, everything.

Four: learn a little bit more about the WiFi module log

We already know the WiFi start process above, in the function run will also output the corresponding log information, the following to learn more about. (Please note that when WiFi is turned on, other states such as battery status will change.) When WiFi is turned off, the Android strategy is to offload the drive to save power. If there is missing is the problem. But the following is the deletion of WiFi-independent log! )

1. Turn on wifi& Auto Search

E/WIFIHW (1201): ==john debug==: [WIFI] Load Driver

Load Driver
D/settingswifienabler (1321): Received WiFi state changed from Disabled to enabling

Receive broadcast: WiFi is on
D/wifiservice (1201): Action_battery_changed pluggedtype:2

Battery status change
E/WIFIHW (1201): ==johndebug==:moduleaddress:4b938008 Filename:/system/lib/modules/dhd.ko args:firmware_path=/ System/wlan/broadcom/rtecdc.bin Nvram_path=/system/wlan/broadcom/nvram.txt

WiFi Hardware: Load kernel modules
I/wpa_supplicant (2490): Ctrl-event-state-change id=-1 state=2

Wpa_supplicant Issuing event notifications

V/wifimonitor (1201): Event [Ctrl-event-state-change id=-1 state=2]

Wifimonitor receiving event notifications from Wpa_supplicant

I/wpa_supplicant (2490): Ctrl-event-scan-results Ready

Wpa_supplicant Event Notification: Ready to start searching the network

E/wpa_supplicant (2490): wpa_driver_priv_driver_cmd scan-active len = 4096

Wpa_supplicant issuing event notification: drive command line. Active search. LEN

E/wpa_supplicant (2490): wpa_driver_priv_driver_cmd scan-active len = 0, 11

Wpa_supplicant issuing event notification: drive command line. Active search. LEN

E/wpa_supplicant (2490): wpa_driver_priv_driver_cmd scan-passive len = 4096

Wpa_supplicant issuing event notification: drive command line. Passive search. LEN

E/wpa_supplicant (2490): wpa_driver_priv_driver_cmd scan-passive len = 0, 12

Wpa_supplicant issuing event notification: drive command line. Passive search. len=0.12

D/settingswifienabler (1321): Received WiFi state changed from enabling to Enabled

Receive broadcast: WiFi is turned on

E/wpa_supplicant (2490): wpa_driver_priv_driver_cmd RSSI len = 4096

Wpa_supplicant Issuing event notifications:

E/wpa_supplicant (2490): wpa_driver_priv_driver_cmd RSSI len = 4, 4

Wpa_supplicant Issuing event notifications:

E/wpa_supplicant (2490): wpa_driver_priv_driver_cmd linkspeed len = 4096

Wpa_supplicant Issuing event notifications:

E/wpa_supplicant (2490): Wpa_driver_priv_driver_cmd linkspeed, len = 12, 12

Wpa_supplicant Issuing event notifications:

E/wpa_supplicant (2490): wpa_driver_priv_driver_cmd macaddr len = 4096

Wpa_supplicant Issue notification: drive command line. MAC address. LEN

E/wpa_supplicant (2490): wpa_driver_priv_driver_cmd macaddr = 44:a4:2d:27:25:be

Wpa_supplicant Issue notification: drive command line. MAC address

E/wpa_supplicant (2490): len = 28, 28

Wpa_supplicant Issuing event notifications:

V/wifistatetracker (1201): Connection to supplicant established, state=scanning

WiFi Status Tracking: Connection request Acknowledgement, status = Search

D/networkstatetracker (1201): setdetailed state, LD =idle and new state=scanning

Network Status Tracking: Updates appear as search status

V/wifistatetracker (1201): changing supplicant state:scanning ==> INACTIVE

WiFi Status Tracking: Change request Status: Not active in search


2. Click Connect & get Status

E/WIFIHW (1201): ==john debug==: [WIFI] Load Driver

WiFi Hardware: Load driver
D/settingswifienabler (1321): Received WiFi state changed from Disabled to enabling

Receive broadcast, WiFi status is on
E/WIFIHW (1201): ==johndebug==:moduleaddress:4b938008 Filename:/system/lib/modules/dhd.ko args:firmware_path=/ System/wlan/broadcom/rtecdc.bin Nvram_path=/system/wlan/broadcom/nvram.txt

WiFi Hardware: Load kernel modules
E/WIFIHW (1201): ==john debug==: Return of Insmod:ret = 0, Unknown error:0

WiFi Hardware: Return load Module report: Return instruction 0, unknown error 0

......

I/wpa_supplicant (2490): Trying to associate with 1c:bd:b9:f6:a7:9f (ssid= ' losangeles ' freq=2412 MHz)

Wpa_supplicant Event Notification: Attempt to connect, (ssid= ' LosAngeles ' band =2412 MHz)

V/wifimonitor (1201): Event[trying to associate with 1c:bd:b9:f6:a7:9f (ssid= ' losangeles ' freq=2412 MHz)]

Wifimonitor Receiving Wpa_supplicant Events

V/wifimonitor (1201): Event [Ctrl-event-state-change id=-1 state=3]

Wifimonitor Receiving Events

V/wifistatetracker (1201): changing supplicant state:scanning ==> associating

WiFi Status Tracking: Change request Status: Search in Match

D/networkstatetracker (1201): setdetailed state, LD =scanning and new state=connecting

Network status Tracking: Updates are displayed as connecting status

D/connectivityservice (1201): Connectivitychange for Wifi:connecting/connecting

Connection Management Service: Change WiFi connection Status: Connecting/connecting

V/wifistatetracker (1201): changing supplicant state:associating ==> associated

WiFi Status Tracking: Change request Status: match-to-match

D/networkstatetracker (1201): setdetailed state, LD =connecting and new state=connecting

Network status Tracking: Updates are displayed as connecting status

I/wpa_supplicant (2490): associated with 1c:bd:b9:f6:a7:9f

Wpa_supplicant Event notification: matched with 1c:bd:b9:f6:a7:9f

V/wifimonitor (1201): Event [associated with 1c:bd:b9:f6:a7:9f]

Wifimonitor Receiving Wpa_supplicant Events

V/wifistatetracker (1201): changing supplicant state:associated ==> Four_way_handshake

WiFi Status Tracking: Change request Status: matched->tcp interrupt Connection

D/networkstatetracker (1201): setdetailed state, LD =connecting and new state=authenticating

Network status Tracking: Updates are displayed as authentication

D/connectivityservice (1201): Dropping Connectivitychange for wifi:connecting/authenticating

Connection Management Service: Throw WiFi Connection Status change: Connected/authenticated

V/wifistatetracker (1201): changing supplicant state:four_way_handshake ==> Group_handshake

WiFi Status Tracking: Change request Status: TCP Interrupt connection, confirm flag bit

D/networkstatetracker (1201): setdetailed state, LD =authenticating and new state=authenticating

Network status Tracking: Updates are displayed as authentication

I/wpa_supplicant (2490): Wpa:key negotiation completed with 1c:bd:b9:f6:a7:9f [ptk=ccmp Gtk=tkip]

Wpa_supplicant Event Notification: WPA: With 1c:bd:b9:f6:a7:9f to determine flag bit

I/wpa_supplicant (2490): Ctrl-event-state-change id=0 state=7

Wpa_supplicant Issuing event notifications:

I/wpa_supplicant (2490): Ctrl-event-connected-connection to 1c:bd:b9:f6:a7:9f completed (auth) [Id=0 id_str=]

Wpa_supplicant issuing Event notification: Connection complete

V/wifimonitor (1201): Event [Wpa:key negotiation completed with 1c:bd:b9:f6:a7:9f [ptk=ccmp Gtk=tkip]]

Wifimonitor Receiving Wpa_supplicant Events

V/wifimonitor (1201): Event [Ctrl-event-state-change id=0 state=7]

Wifimonitor Receiving Wpa_supplicant Events

V/wifimonitor (1201): Event [ctrl-event-connected-connection to 1c:bd:b9:f6:a7:9f completed (auth) [Id=0 id_str=]]

Wifimonitor Receiving Wpa_supplicant Events

V/wifistatetracker (1201): changing supplicant state:group_handshake ==> completed

WiFi Status Tracking: Change request Status: Confirm Sign-On completion

V/wifistatetracker (1201): New network state is CONNECTED

WiFi Status tracking: New network status is connected

D/networkstatetracker (1201): setdetailed state, LD =authenticating and new state=obtaining_ipaddr

Network Status Tracking: Updates appear to get IP addresses

D/connectivityservice (1201): Dropping Connectivitychange for WIFI:CONNECTING/OBTAINING_IPADDR

Connection Management Service: throwing WiFi

Read (32) | Comments (0) | Forwards (0) |0

Previous: Android WiFi learning (2)------WiFi connection

Next article: the gets () function and the fgets () function in Linux

Related Popular articles
    • Common Linux Service ports
    • Xmanager 2.0 for Linux configuration
    • "Rootfs build" BusyBox httpd ...
    • OpenWrt in Luci study notes
    • What is a shell?
    • Linux DHCP Peizhi ROC
    • Soft links to Unix files
    • What does this command mean, I'm new ...
    • What does sed-e "/grep/d" mean ...
    • Who can help me solve Linux 2.6 10 ...
Leave something to the owner! ~~ Comment on the hot topic

Android WiFi Module Analysis

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.