Address: http://hi.baidu.com/wuguohu/blog/item/6b518726d6ffda018b82a176.html
I. Basic WiFi framework
The Wi-Fi system of Android contains the following content from top to bottom:
The wifi part is used as the Network Part in the Android system in the same way as the normal network. The only special part is the WiFi-related settings in the settings program. They call the Java layer interface provided by the WiFi architecture.
2. Local implementation of WiFi
Android WiFi local implementation mainly includes wpa_spplicant and wpa_supplicant adaptation layer.
WPA is short for WiFi protected access. Its Chinese meaning is "Wi-Fi network security access ". WPA is a standard-based and interoperable WLAN Security Enhancement solution that greatly enhances data protection and access control for existing and future wireless local area networks.
1. wpa_supplicant
Wpa_supplicant is an open-source project that has been transplanted to Linux, windows, and many embedded systems. It is the application-layer authentication client of WPA and is responsible for logon and encryption related to authentication. The source code directory of wpa_supplicant is:
. \ External \ wpa_supplicant \
. \ External \ wpa_supplicant_6 \
After the content in this project is compiled, the main result is to generate the dynamic library libwpa_client.so and the executable program wpa_supplicant.
Wpa_supplicant is a self-running daemon. Its core is a message loop that processes WPA state machines, control commands, drive events, and configuration information in a message loop. Wpa_supplicant has many control interfaces and provides control modes for command lines and communication interfaces. The communication between Android and wpa_supplicant is completed through socket.
For more information about wpa_supplicant, see: http://w1.fi/wpa_supplicant/
Libwpa_client.so is a database that provides a single link and call to the customer. It is used to communicate with the wpa_supplicant daemon.
2. wpa_supplicant Adaptation Layer
The wpa_supplicant adaptation layer is a universal wpa_supplicant encapsulation, which is used as the hardware abstraction layer of the WiFi part in Android. The wpa_supplicant adaptation layer is mainly used to communicate with the wpa_supplicant daemon for Android framework. It provides loading, control, and message monitoring functions.
The source code path of the wpa_supplicant adaptation layer is:
. \ Hardware \ libhardware_legacy \ include \ hardware_legacy \ wifi. h
. \ Hardware \ libhardware_legacy \ WiFi \
The wpa_supplicant adaptation layer is part of libhardware_legacy.so. It must contain the wpa_supplicant header file wpa_ctrl.h, and the dynamic link library libwpa_client.so. Use the WiFi. h file to call the interface for the last layer of the provider (actually the JNI part of WiFi ).
The composition of the wpa_supplicant adaptation layer is very simple. Apart from some loading and connection interfaces, the most important part of the layer is the following two interfaces:
Int wifi_command (const char * command, char * reply, size_t * reply_len );
Int wifi_wait_for_event (char * Buf, size_t Len );
Wifi_command () provides the function to send bright light to the lower layer of the WiFi system. wifi_wait_for_event () is responsible for the channel for the event to enter. This function will be blocked until a WiFi event is received, and return it as a string.
In the implementation of wifi. C:
1) wifi_command () is the encapsulation of wifi_send_command (). wifi_send_command () is directly forwarded to the wpa_supplicant process through the wpa_ctrl_request () Command and the result is returned.
2) wifi_wait_for_event () only calls wpa_ctrl_recv () to accept the event reported by the previous wpa_supplicant. If no event exists, the upper layer will read each reported event through loops.
This layer also provides some operations related to DHCP (Dynamic Host Allocation Protocol.
Iii. JNI layer of WiFi
The source code file of the JNI Implementation of the WiFi system in Android is:
. \ Frameworks \ base \ core \ JNI \ android_net_wifi_wifi.cpp
The local functions implemented here are implemented by calling the interface of the wpa_supplicant adaptation layer (including the header file wifi. H of the Adaptation Layer ).
The source code file registered to the Java layer through the JNI layer interface is:
. \ Frameworks \ base \ WiFi \ Java \ Android \ net \ WiFi \ wifinative. Java
Wifinative provides underlying Operation Support for components in the WiFi framework, such as wifiservice, wifistatetracker, and wifimonitor.
4. Java framework layer of WiFi
The Java code of the WiFi system is implemented in the following directory:
. \ Frameworks \ base \ WiFi \ Java \ Android \ net \ WiFi \ // WiFi service layer content
. \ Frameworks \ base \ Services \ Java \ com \ Android \ Server \ // WiFi Interface
The core of the Java layer of the WiFi system is the binder server and client created based on the iwifimanger interface. The server is wifiservice and the client is wifimanger.
After iwifimanger. aidl is compiled, iwifimanger. Java is generated, and iwifimanger. Stub (server abstract class) and iwifimanger. stub. Proxy (client proxy implementation class) are generated ). Wifiservice inherits iwifimanger. stub implementation, while the client obtains iwifimanger through the getservice () function. stub. proxy (the Service proxy class), which is passed as a parameter to wifimanger for communication with wifiservice.
1. wifimanger is the interface between the Wi-Fi part and the outside world. You can use it to access the core functions of wifi. Wifiwatchdogservice is a system component that uses wifimanger to perform specific operations.
2. wifiservice is the implementation of the server. as the core of the WiFi part, wifiservice handles commands such as loading, scanning, linking, and disconnecting the driver and reports events at the underlying layer. For active command control, WiFi is a simple encapsulation. For control commands from clients, the underlying implementation of wifinative is called.
Generally, after receiving the Client Command, convert it into the corresponding own message into the message queue, so that the client can return the call in time, and then in the handlemessage () of wifihandler () process the corresponding message. Wifiservice starts wifistatetracker to handle underlying events.
1) In addition to functions such as WiFi power management mode, wifistatetracker focuses on the event polling mechanism implemented by wifimonitor and the handlemessage () function of message processing ().
2) wifimonitor Enables event polling by enabling a monitorthread. The key function of polling is the blocking function wifinative. waitforevent () mentioned above (). After obtaining the event, wifimonitor sends a series of handler notifications to wifistatetracker. Here, the wifimonitor notification mechanism converts underlying events into messages that can be identified by wifistatetracker, inserts them into the message loop of wifistatetracker, and finally completes corresponding processing by wifistatetracker in handlemessage.
Note: wifistatetracker is also a Wi-Fi part of the interface with the outside world, it is not directly instantiated as wifimanger to operate, but through the intent mechanism to send a message notification to the client registered broadcastreceiver, to complete the interface with the client.
3. wifiwatchdogservice is a service started by connectivityservice, but it is not a service implemented by binder. It monitors access points in the same network. If the DNS of the current access point cannot be pinged, the system automatically switches to the next access point. Wifiwatchdogservice uses wifimanger and wifistatetracker to assist in specific control actions. During wifiwatchdogservice initialization, you can use registerforwifibroadcasts to register the broadcastreceiver with network changes, that is, capture the notification message sent by wifistatetracker, and enable a wifiwatchdogthread to handle. You can enable or disable wifiwatchdogservice by changing the configuration of setting. Secure. wifi_warchdog_on.
5. WiFi settings in setting
The settings application of Android uses WiFi, which is a typical WiFi application and a visible Android WiFi management program. The Directory of the source code is:
. \ Packages \ apps \ Settings \ SRC \ com \ Android \ Settings \ WiFi \
The wifi part in setting is a user-visible setting interface that provides basic functions such as WiFi switch, AP scan, and connection/disconnection. In addition, I provide a set of callback functions by implementing the wifilayer. Callback interface, which is used to change the Wi-Fi status that users are concerned about.
Both wifienabler and wifilayer are components of the wifisettings. They also use wifimanger to complete the actual functions. They also register a broadcastreceiver to respond to notifications sent by wifistatetracker. Wifienabler is actually a simple class that provides the function of enabling and disabling wifi. It is used to set the outer Wi-Fi switch menu; wifilayer provides more complex WiFi functions, such as AP selection, for user customization.