android_wifi读书笔记之4-HAL层分析

本文为读书笔记,整理自网络文献和源码

4 HAL层分析

Hardware/libhardware_legacy/wifi/wifi.c

这个文件主要是实现驱动的加载和卸载,wpa_supplicant的开启和关闭

4.1 驱动的加载卸载:

wifi_load_driver():驱动的加载。wifi驱动是编译成ko文件, ko文件储存的位置#defineWIFI_DRIVER_MODULE_PATH "/system/lib/modules/bcmdhd.ko" 。

这个方法会被上层调用加载驱动。

卸载方法 wpa_unload_driver()

4.2 Wpa_supplicant相关( 这也是wpa_supplicant的上行接口):

wifi_start_supplicant():上层要开启wpa_supplicant,直接调用的是这个方法,这个方法实际上是调用wifi_start_supplicant_common(const char *config_file)

wifi_start_supplicant_common(const char*config_file):这个方法实际上为wifi.c进程与wpa_supplicant进程相连做一些准备工作。

wifi_connect_to_supplicant():wifi.c进程与wpa_supplicant进程进行通信,他们之间采用socket的方式来实现进程间通信。wifi_connect_to_supplicant()方法两次调用的是wpa_ctrl.c里面的wpa_ctrl_open(constchar *ctrl_path)方法来实现,一个为ctrl interface,用于发送命令,获取信息。将另外一个接口作为参数,调用wpa_ctrl_attach,成为 monitor interface,用于监听接收来自于wpa_supplicant的event事件。此举可以降低通信的耦合性,避免response和event的相互干扰。wifi_connect_to_supplicant()主要实现代码:

ctrl_conn = wpa_ctrl_open(ifname);

    if (ctrl_conn == NULL) {

       LOGE("Unable to open connection to supplicant on \"%s\":%s",

            ifname, strerror(errno));

       return -1;

    }

   monitor_conn = wpa_ctrl_open(ifname);

   if (monitor_conn == NULL) {

       wpa_ctrl_close(ctrl_conn);

       ctrl_conn = NULL;

       return -1;

    }

   if (wpa_ctrl_attach(monitor_conn) != 0) {

       wpa_ctrl_close(monitor_conn);

       wpa_ctrl_close(ctrl_conn);

       ctrl_conn = monitor_conn = NULL;

       return -1;

    }

 

   if (socketpair(AF_UNIX, SOCK_STREAM, 0, exit_sockets) == -1) {

       wpa_ctrl_close(monitor_conn);

       wpa_ctrl_close(ctrl_conn);

       ctrl_conn = monitor_conn = NULL;

       return -1;

}

 

4.3数据的传输:(涉及wpa_supplicant上行接口)

4.3.1、发送数据:

wifi_command(const char *command, char*reply, size_t *reply_len):这个方法被jni调用,主要是上层发送命令通过这个方法传递给wpa_supplicant。这个方法实际上是调

用wifi_send_command(ctrl_conn, command, reply, reply_len)方法。

wifi_send_command(struct wpa_ctrl *ctrl,const char *cmd, char *reply, size_t *reply_len):发送上层传递过来的命令给wpa_supplicant;

4.3.2、接收数据:

wifi_wait_for_event(char *buf, size_tbuflen):上层调用这个方法阻塞等待wpa_supplicant传递上来的信息,主要是调用wifi_ctrl_recv(struct wpa_ctrl *ctrl, char

*reply, size_t *reply_len)来实现。

wifi_ctrl_recv(struct wpa_ctrl *ctrl, char*reply, size_t *reply_len):调用wpa_ctrl.c中的wpa_ctrl_recv(struct wpa_ctrl *ctrl, char *reply, size_t *reply_len)

来接收wpa_supplicant传递过来的信息。

 

DRIVER CMD, 与驱动相关命令,由上层下发往wifi驱动的私有命令以 “DRIVER ”开头,在此涉及有16个方法,24个命令:

setCountryCodeCommand            "DRIVER COUNTRY"

doSetScanMode                         "DRIVERSCAN-ACTIVE" /

                                   "DRIVER SCAN-PASSIVE"

startDriverCommand                 "DRIVER START"

stopDriverCommand                  "DRIVER STOP"

startMultiV4Filtering                  "DRIVER RXFILTER-STOP"/

                                   "DRIVER RXFILTER-REMOVE 2" /

                                   "DRIVER RXFILTER-START"

stopMultiV4Filtering                  "DRIVER RXFILTER-ADD2" /

                                   "DRIVERRXFILTER-START"

startMultiV6Filtering                 "DRIVERRXFILTER-STOP" /

                                  "DRIVERRXFILTER-REMOVE 3" /

                                  "DRIVERRXFILTER-START"

stopMultiV6Filtering                 "DRIVER RXFILTER-ADD3" /

                                  "DRIVERRXFILTER-START"

getMacAddressCommand            "DRIVER MACADDR"

setPowerModeCommand             "DRIVER POWERMODE %d"

getPowerModeCommand             "DRIVER GETPOWER"

setBandCommand                    "DRIVER SETBAND"

getBandCommand                   "DRIVER GETBAND"

setBluetoothCoexistenceModeCommand    "DRIVER BTCOEXMODE"

setBluetoothCoexistenceScanModeCommand     "DRIVER BTCOEXSCAN-START" /

                                             "DRIVER BTCOEXSCAN-STOP"

setSuspendOptimizationsCommand         "DRIVER SETSUSPENDOPT %d"

 

你可能感兴趣的:(android,WLAN)