Recently, in order to verify the energy consumption of an algorithm, we studied how to enable wireless network card automatically on the open-source Android platform and automatically scan and associate wpa_supplicant-based AP, and record and share it.
Objectives:
Enable the wireless network card automatically, scan the specified channel list, and automatically associate it when an AP is found.
1. WiFi framework of the Android platform
The wifi control framework used by the Android platform is based on wpa_supplicant, a well-known security middleware that provides a unified security mechanism for various wireless network adapters, as shown in:
Figure
1
:
Android
Platform
WiFi
Framework
According to the above structure, Wi-Fi control in Android G1 mobile phones is divided into three components:
1) The client program, including the wpa_cli command line or Java graphical interface program
Wpa_supplicant DAEMON service communication, send commands and receive results;
2) wpa_supplicant
The Daemon Service corresponds to the intermediate part of the preceding Section. The function is "upload and release ". All clients use it to control the hardware Nic, send string commands to control whether to scan the AP, extract scan results, and determine whether to associate
And send the execution status of the driver to the user. This service is designed to support a variety of wireless Nic chips, so each vendor provides a universal interface for wpa_supplicant
Where G1 uses Ti chips;
3) NIC Driver, which is not covered in this article.
2. Enable the NIC automatically and start wpa_supplicant.
The GUI can start the nic and start ap scanning normally, but wpa_cli cannot start through the command line. Therefore, modify the wpa_supplicant program, output all argv parameters, and then enable AP scanning through GUI settings, set> wireless interface> Start WiFi and output the following parameters:
1257946571.827087:/system/bin/wpa_supplicant
1257946571.827453:-dtiwlan0
1257946571.827545:-itiwlan0
1257946571.827636:-C/data/MISC/WiFi/wpa_supplicant.conf
Manually enter wpa_supplicant-dtiwlan0-itiwlan0
-C/data/MISC/WiFi/wpa_supplicant.conf
Or start failed. By tracking the Java code of the graphic interface, locate Android/packages/apps/settings/src/COM/android
The Java file in/settings/WiFi is identified as wifienabler. java. The call path
Is: wifienabler-> wifimanager. setwifienabled
Interface function, which sends a message_enable_wifi message to itself through the aidl-> wifiservic. setwifienabled Function
Message, processing code handlemessage calls setwifienabledblocking. The function code is as follows:
...
If (enable ){
If (! Wifinative. loaddriver ()){}
If (! Wifinative. startsupplicant ()){}
...} Else {
If (! Wifinative. stopsupplicant ()){}
If (! Wifinative. unloaddriver ()){}
}
}
Wifinative. Java uses the JNI mechanism. The corresponding C file is Android/hardware/libhardware_legacy/WiFi/wifi. C, which includes the following core functions:
Int wifi_load_driver ();
Int wifi_unload_driver ();
Int wifi_start_supplicant ();
Int wifi_stop_supplicant ();
It's great! Link the file to our code to load the nic and start wpa_supplicant.
3. Automatically scans the specified channel and associates it with the specified AP
The existing wpa_cli is an interactive command environment that works in the request-response mode after being connected to the wpa_supplicant service. Therefore, we need to rewrite a client wpa_myclient, and use the aforementioned wifi. C code to automatically open the nic and start wpa_supplicant. The involved code files include:
Android/external/wpa_supplicant/
Android/system/WLAN/Ti/wpa_supplicant_lib/: a unified interface provided by TI to wpa_supplicant.
1) analyze the working principle of wpa_cli and determine how to associate the AP with a manual command
The term networks is a WLAN network that is remembered by the current Nic. Similar to the concept of "preferred network" in Windows wireless interface settings, networks can be manually configured through wpa_supplicant.conf.
The term scan_results is the AP detected by the current Nic scan.
The network adapter uses the following command sequence to associate a network:
Scan
Scan_results % get scan results, including SSID and signal strength
Add_network % must first create a network to obtain a new network ID
Set_network id ssid "***" % maps the network to the SSID of the AP to be associated
Set_network ID key_mgmt none % sets security information, which is for Open System
Set_network ID pairwise none
Select_network ID % activates the network and starts the association process.
2) analyze the communication protocol between wpa_cli and supplicant, add a new command myscan to scan by the specified channel list, and modify the core data structure struct
Wpa_supplicant adds a member to save the list of channels to be scanned. The main modification files include ctrl_iface.c and wpa_supplicant_ I .h.
3)
Wpa_supplicant_scan ()-> driver_ti.c
Wpa_driver_tista_scan->
Ti_init_scan_params () initializes scan parameters and sets pscanparams-> channelentry to the specified channel list;
4)
After scanning, the driver notifies the caller through the wpa_driver_tista_receive_driver_event () of driver_ti.c.
The wpa_supplicant_event () event distribution interface that is passed to events. c step by step,
Wpa_supplicant_event_scan_results () is used to notify the wpa_cli client that the user can use scan_results
To obtain the scan results. In addition, if the processing function finds that the scan results include the user's preferred network networks, the association process is automatically started (this is also the case in Windows ).
5) Modify wpa_supplicant_event_scan_results () of events. C to simulate the user's preferred network.
Wpa_config_remove_network method to delete wpa_s-> conf-> SSID
Select the most suitable network from wpa_s-> scan_results [] according to a rule, and then
Wpa_config_add_network and wpa_config_set set the network according to the preceding manual command format, and then it will automatically follow the normal process
.
Hints and tips:
1) run the. Build/envsetup. Sh command in the android root directory, and then use the MM target_product = htc_dream command to accelerate compilation.
2) run the set_network id ssid "***" command. The double quotation marks of the SSID cannot be missing.
3)
To output debugging information to a file, you must enable the config_debug_file compilation switch in. config and initialize the file in Main. C.
Params. wpa_debug_file_path is a file (my files are/sdcard/wpadbg). Finally, you can customize the message optimization of a wpa_printf file.
Level 1, and open wpa_debug_print_timestamp, the output Timestamp