Control WIFI through C framework layer in Android environment [Turn]

This article is reproduced from: https://blog.csdn.net/edw200/article/details/52192631

I am engaged in Linux embedded development. Android wifi control is very mature in Android JAVA layer, but I need to write a program to control wifi, and I need to write a lot of JAVA. I don't understand JAVA, so I decided to skip Through the JAVA framework and application layer, start directly from the C framework layer.

 

1.Android WIFI framework

 

The basic structure of WIFI:

1. Programs and libraries for wifi user space:

      external/wpa_supplicant/

      Generate library libwpaclient.so and daemon wpa_supplicant

2. hardware/libhardware_legary/wifi/ is the wifi management library

3. JNI part:

      frameworks/base/core/jni/android_net_wifi_Wifi.cpp

4. JAVA part:

      frameworks/base/services/java/com/android/server/

      frameworks/base/wifi/java/android/net/wifi/

5. The WIFI Settings app is located at:

      packages/apps/Settings/src/com/android/settings/wifi/

6, WIFI driver wlan.ko

      wpa_supplicant communicates with the driver through the wireless_ext interface

7. WIFI hardware module

2.Hardware layer

2.1. Loading and unloading drivers

 

Load: insmod /system/lib/modules/bcmdhd.ko iface_name=wlan0

Uninstall: rmmod bcmdhd

 

2.2. Start wpa_supplicant

Execute setprop ctl.start wpa_supplicant

Note: When the above command is executed, the Android system will call the following statement in /init.macallan.rc:

service wpa_supplicant /system/bin/wpa_supplicant \

    -iwlan0 -Dnl80211 -c/data/misc/wifi/wpa_supplicant.conf \

    -I/system/etc/wifi/wpa_supplicant_overlay.conf \

    -O/data/misc/wifi/sockets \

    -e/data/misc/wifi/entropy.bin -g@android:wpa_wlan0

    # we will start as root and wpa_supplicant will switch to user wifi

    # after setting up the capabilities required for WEXT

    # user wifi

    # group wifi inet keystore

    class main

    socket wpa_wlan0 dgram 660 wifi wifi

    disabled

    oneshot

 

In order to prevent wpa_supplicant from being activated and causing wifi to not work properly, turn off wpa_supplicant and then start it again.

 

2.3. Close wpa_supplicant

Execute setprop ctl.stop wpa_supplicant

See Wifi.c (\android\hardware\libhardware_legacy\wifi) for details

 

2.4. Search AP

Execute wpa_cli -iwlan0 IFNAME=wlan0 scan to start the search

Execute wpa_cli -iwlan0 IFNAME=wlan0 scan_result to display the search results

Description: This command is different from the standard (Linux system) wpa_supplicant. The standard is to execute:

wpa_cli -iwlan0 scan because the wpa_supplicant tool for Android is tailored.

The search results are as follows:

bssid                / frequency  / signal level       / flags                                         / ssid

MAC Address Frequency Signal Encryption Mode AP Name  

30:fc:68:19:57:70  2462  -41   [WPA-PSK-CCMP][WPA2-PSK-CCMP][ESS]    aaaa

30:fc:68:72:36:d2  2462  - 44  [WPA-PSK-CCMP][WPA2-PSK-CCMP][ESS]  TP-LINK_dd

b8:20:e7:00:32:9e  2437  -46  [WPA-PSK-CCMP][WPA2-PSK-CCMP][ESS]   ccc

b8:20:e7:00:0c:ce  2422  -46   [WPA-PSK-CCMP][WPA2-PSK-CCMP][ESS]   dd

e0:05:c5:ac:6d:fc   2472  -49   [WPA-PSK-CCMP][WPA2-PSK-CCMP][ESS]   Tee

14:cf:92:6e:1d:5e  2437   -50  [WPA-PSK-CCMP][WPA2-PSK-CCMP][ESS]    Xdaf

bc:46:99:73:1d:42  2412  -80   [WPA-PSK-CCMP][WPA2-PSK-CCMP][ESS]    Testdfege

00:0c:43:30:52:a8       2437    -43     [WEP][ESS]      G5000_JOYKOLN

b8:20:e7:00:0a:62       2437    -65     [WEP][ESS]      G5000_JOYKOLN

28: f0: 76: 18: c1: 16 2462 -48 [ESS] iMacQIU \ xe7 \ x9a \ x84iMac

 

2.5.wpa_supplicant.conf configuration file

Located at /data/misc/wifi/wpa_supplicant.conf   

    

When wifi is not enabled (wpa_supplicant is not enabled) the file shows:

ctrl_interface=/data/misc/wifi/sockets

disable_scan_offload=1

update_config=1

device_name=macallan

manufacturer=NVIDIA

model_name=Macallan

model_number=Macallan

serial_number=JKD01V20030000022

device_type=10-0050F204-5

config_methods=physical_display virtual_push_button

p2p_disabled=1

 

When the wifi is turned on, the file shows:

ctrl_interface=/data/misc/wifi/sockets

disable_scan_offload=1

update_config=1

device_name=macallan

manufacturer=NVIDIA

model_name=Macallan

model_number=Macallan

serial_number=JKD01V20030000022

device_type=10-0050F204-5

config_methods=physical_display virtual_push_button

p2p_disabled=1

 

network={

        ssid="JET-1"

        psk="abcd1234"

        key_mgmt=WPA-PSK

        priority=1

}

 

2.6. Restart wpa_supplicant

执行wpa_cli  -iwlan0  IFNAME=wlan0  reconfigure

Note: When wpa_supplicant is initially started, because the AP information has not yet been connected, you must obtain the currently searched AP through the scan_result command, and write the custom AP into the configuration file, and then execute the reconfigure command to restart wpa_supplicant.

 

2.7. Start DHCP and start the shutdown network

You need to start the network before starting wpa_supplicant

execute netcfg wlan0 up

Start DHCP. When connected to the AP, you need to set the IP here. Generally, dynamic IP is used.

execute netcfg wlan0 dhcp

After closing wpa_supplicant, the network card network needs to be closed at the same time

execute netcfg wlan0 down

 

Such a process is completed. Of course, a small program needs to be written here to search all AP information, and write the AP information to be connected into the configuration file.

 

This method is an example of controlling the WIFI function that bypasses the Android JAVA framework layer and the JAVA application layer, and directly communicates with the C framework layer, so that the user can completely control the wifi function and use the automatic switching function with the wired network card.

Copyright statement: This article is an original article by the blogger and may not be reproduced without the blogger's permission. https://blog.csdn.net/edw200/article/details/52192631

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325354992&siteId=291194637