Android WIFI 移植

來源:互聯網
上載者:User
Android WIFI 移植




在 Android 2.2上移植了2個wifi模組,vt6656和rt2070,總結一下要點。首先,將wifi linux驅動編譯成模組,並將驅動(vntwusb.ko或rt3070sta.ko放到/system/lib/modules/中。然後,做如下修改:

1。修改 init.rc:很多文章都有描述,但還是有些說明不清的地方,我先列出增加項,然後作些說明。
增加: mkdir /system/etc/wifi 0771 wifi wifi
           chmod 0771 /system/etc/wifi
           chmod 0660 /system/etc/wifi/wpa_supplicant.conf
           chown wifi wifi /system/etc/wifi/wpa_supplicant.conf  #wifi的原始設定檔
           # wpa_supplicant socket
           mkdir /data/system/wpa_supplicant 0771 wifi wifi
           chmod 0771 /data/system/wpa_supplicant  #放置wifi interface的地方
           mkdir /data/misc/wifi 0771 wifi wifi
          chmod 0771 /data/misc/wifi
          chmod 0660 /data/misc/wifi/wpa_supplicant.conf  #wifi的設定檔,將由wpa_supplicant根據實際配置寫入該檔案
          mkdir /data/misc/wifi/sockets 0777
wifi wifi  #與上層通過socket通訊的路徑
           # Prepare for wifi
          
setprop wifi.interface ra0 
#intreface名稱設定,這在framework/base/wifi/java/android/net/wifi
/WifiStateTracker.java中會用到,以處理dhcp。rt2070用ra0,而vt6656使用eth1。
這裡0771對目
錄許可權的處理是為了所有使用者能對下一級進行搜尋,而紅字特別提醒的許可權配置,是因為/data/misc/wifi/sockets目錄不僅為wifi擁
有者服務,還因為通訊的原因要和其他使用者聯絡,要不然,將會出現Unable to open connection to supplicant on
"/data/system/wpa_supplicant/ra0": Connection refused,或permission
denied的錯誤。很多人乾脆將上述所有的許可權都設為0777,當然也行,但總覺得有些粗糙。
service的修改:
service wpa_supplicant /system/bin/logwrapper /system/bin/wpa_supplicant /
    -Dwext -ira0 -c/data/misc/wifi/wpa_supplicant.conf  #也可以用/system/etc/wifi/wpa_supplicant.conf代替
    user root
    group system wifi inet
#    socket wpa_wlan0 dgram 660 wifi wifi   #屏蔽該項是因為這項是用於UDP串連的
    disable
    oneshot

service dhcpcd /system/bin/logwrapper /system/bin/dhcpcd -d -B ra0
    group system dhcp wifi
    disabled
    oneshot
2。修改system/etc/wifi/wpa_supplicant.conf (在源碼中是修改external/wpa_supplicant/wpa_supplicant.conf)
將ctrl_interface=wlan0改成ctrl_interface=DIR=/data/system/wpa_supplicant GROUP=wifi  #這個路徑在wifi.c中用到。
3。修改system/etc/dhcpcd/dhcpcd.conf
將其中的interface名稱改成ra0
4。修改晶片廠商的配置 BoardConfig.mk。
例如,Freeescale 是在device/fsl/imx51_bbg/,加入:
HAVE_CUSTOM_WIFI_DRIVER_2 := true
BOARD_WPA_SUPPLICANT_DRIVER := WEXT
而下面已經定義的wifi驅動的路徑我感到還是屏蔽為好,直接在wifi.c中修改不是更直觀些。
5。修改hardware/libhardware_legacy/wifi/wifi.c
ifndef WIFI_DRIVER_MODULE_PATH
#define WIFI_DRIVER_MODULE_PATH         "/system/lib/modules/rt3070sta.ko"
#endif
#ifndef WIFI_DRIVER_MODULE_NAME
#define WIFI_DRIVER_MODULE_NAME         "rt3070sta"
#endif

檔案中還可以看到其他一些資訊,如IFACE_DIR[]           = "/data/system/wpa_supplicant",
就是interface的安放的路徑。MODULE_FILE[]         =
"/proc/modules",這是insmod安放module的路徑。在調試時可以查詢是否已經安裝了模組,介面是否啟動。
6。源碼修改

改external/wpa_supplicant/wpa_ctrl.c:
找到chmod那行,將chmod(ctrl->local.sun_path,
S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP);改成chmod(ctrl->local.sun_path,
S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH);  #增加許可權
這是為了防止出現ioctl 寫message的錯誤。
修改external/wpa_supplicant/driver_wext.c:

是為了避免wpa_supplicant與下層驅動通訊時出現ioctl[SIOCSIWPRIV]錯誤,因為現在大部分wifi模組對
SIOCSIWPRIV命令不處理,而這個命令要用於偵測wifi強度RSSI的,比較簡單的方法是在wifi驅動中增加個空函數。例如,對於
rt2070,有一個sta_ioctl.c,找到SIOCSIWPRIV,將其對應個空函數
static int handler_SIOCSIWPRIV(struct net_device *dev, struct iw_request_info *info,
                         union iwreq_data *wrqu, char *extra)
{
       return 0;
}
不過,這樣做就有些資訊,如RSSI,MAC地址等就沒法在上層顯示了。比較好的方法如下:
在struct wpa_driver_wext_data 中增加
    u8 ssid[32];
    unsigned int ssid_len;
2個變數。將原來的wpa_driver_priv_driver_cmd函數改成如下函數(我從Porting wifi driver to Android抄來稍作修改):
static int wpa_driver_priv_driver_cmd(void *priv, char *cmd, char *buf, size_t buf_len)
{

    struct wpa_driver_wext_data *drv = priv;
    int ret = -1;

    wpa_printf(MSG_DEBUG, "AWEXT: %s %s", __func__, cmd);

    if (os_strcasecmp(cmd, "start") == 0) {
        wpa_printf(MSG_DEBUG,"Start command");
        return (ret);
    }

    if (os_strcasecmp(cmd, "stop") == 0) {
        wpa_printf(MSG_DEBUG,"Stop command");
    }
    else if (os_strcasecmp(cmd, "macaddr") == 0) {
        struct ifreq ifr;
        os_memset(&ifr, 0, sizeof(ifr));
        os_strncpy(ifr.ifr_name, drv->ifname, IFNAMSIZ);

        if (ioctl(drv->ioctl_sock, SIOCGIFHWADDR, &ifr) < 0) {
            perror("ioctl[SIOCGIFHWADDR]");
            ret = -1;
        } else {
            u8 *macaddr = (u8 *) ifr.ifr_hwaddr.sa_data;
            ret = snprintf(buf, buf_len, "Macaddr = " MACSTR "/n",
                           MAC2STR(macaddr));
        }
    }
    else if (os_strcasecmp(cmd, "scan-passive") == 0) {
        wpa_printf(MSG_DEBUG,"Scan Passive command");
    }
    else if (os_strcasecmp(cmd, "scan-active") == 0) {
        wpa_printf(MSG_DEBUG,"Scan Active command");
    }
    else if (os_strcasecmp(cmd, "linkspeed") == 0) {
        struct iwreq wrq;
        unsigned int linkspeed;
        os_strncpy(wrq.ifr_name, drv->ifname, IFNAMSIZ);
        wpa_printf(MSG_DEBUG,"Link Speed command");
        if (ioctl(drv->ioctl_sock, SIOCGIWRATE, &wrq) < 0) {
            perror("ioctl[SIOCGIWRATE]");
            ret = -1;
        } else {
            linkspeed = wrq.u.bitrate.value / 1000000;
            ret = snprintf(buf, buf_len, "LinkSpeed %d/n", linkspeed);
        }
    }
    else if (os_strncasecmp(cmd, "scan-channels", 13) == 0) {
    }
    else if ((os_strcasecmp(cmd, "rssi") == 0) || (os_strcasecmp(cmd, "rssi-approx") == 0)) {
        struct iwreq wrq;
        struct iw_statistics stats;
        signed int rssi;
        wpa_printf(MSG_DEBUG, ">>>. DRIVER AWEXT RSSI ");
        wrq.u.data.pointer = (caddr_t) &stats;
        wrq.u.data.length = sizeof(stats);
        wrq.u.data.flags = 1; /* Clear updated flag */
        strncpy(wrq.ifr_name, drv->ifname, IFNAMSIZ);

        if (ioctl(drv->ioctl_sock, SIOCGIWSTATS, &wrq) < 0) {
            perror("ioctl[SIOCGIWSTATS]");
            ret = -1;
        } else {
            if (stats.qual.updated & IW_QUAL_DBM) {
                /* Values in dBm, stored in u8 with range 63 : -192 */
                rssi = ( stats.qual.level > 63 ) ?
                    stats.qual.level - 0x100 :
                    stats.qual.level;
            } else {
                rssi = stats.qual.level;
            }

            if (drv->ssid_len != 0 && drv->ssid_len < buf_len) {
                os_memcpy((void *) buf, (void *) (drv->ssid),
                        drv->ssid_len );
                ret = drv->ssid_len;
                ret += snprintf(&buf[ret], buf_len-ret,
                        " rssi %d/n", rssi);
                if (ret < (int)buf_len) {
                    return( ret );
                }
                ret = -1;
            }
        }
    }
    else if (os_strncasecmp(cmd, "powermode", 9) == 0) {
    }
    else if (os_strncasecmp(cmd, "getpower", 8) == 0) {
    }
    else if (os_strncasecmp(cmd, "get-rts-threshold", 17) == 0) {
        struct iwreq wrq;
        unsigned int rtsThreshold;

        strncpy(wrq.ifr_name, drv->ifname, IFNAMSIZ);

        if (ioctl(drv->ioctl_sock, SIOCGIWRTS, &wrq) < 0) {
            perror("ioctl[SIOCGIWRTS]");
            ret = -1;
        } else {
            rtsThreshold = wrq.u.rts.value;
            wpa_printf(MSG_DEBUG,"Get RTS Threshold command = %d",
                rtsThreshold);
            ret = snprintf(buf, buf_len, "rts-threshold = %u/n",
                rtsThreshold);
            if (ret < (int)buf_len) {
                return( ret );
            }
        }
    }
    else if (os_strncasecmp(cmd, "set-rts-threshold", 17) == 0) {
        struct iwreq wrq;
        unsigned int rtsThreshold;
        char *cp = cmd + 17;
        char *endp;

        strncpy(wrq.ifr_name, drv->ifname, IFNAMSIZ);

        if (*cp != '/0') {
            rtsThreshold = (unsigned int)strtol(cp, &endp, 0);
            if (endp != cp) {
                wrq.u.rts.value = rtsThreshold;
                wrq.u.rts.fixed = 1;
                wrq.u.rts.disabled = 0;

                if (ioctl(drv->ioctl_sock, SIOCSIWRTS, &wrq) < 0) {
                    perror("ioctl[SIOCGIWRTS]");
                    ret = -1;
                } else {
                    rtsThreshold = wrq.u.rts.value;
                    wpa_printf(MSG_DEBUG,"Set RTS Threshold command = %d", rtsThreshold);
                    ret = 0;
                }
            }
        }
    }
    else if (os_strcasecmp(cmd, "btcoexscan-start") == 0) {
    }
    else if (os_strcasecmp(cmd, "btcoexscan-stop") == 0) {
    }
    else if (os_strcasecmp(cmd, "rxfilter-start") == 0) {
        wpa_printf(MSG_DEBUG,"Rx Data Filter Start command");
    }
    else if (os_strcasecmp(cmd, "rxfilter-stop") == 0) {
        wpa_printf(MSG_DEBUG,"Rx Data Filter Stop command");
    }
    else if (os_strcasecmp(cmd, "rxfilter-statistics") == 0) {
    }
    else if (os_strncasecmp(cmd, "rxfilter-add", 12) == 0 ) {
    }
    else if (os_strncasecmp(cmd, "rxfilter-remove",15) == 0) {
    }
    else if (os_strcasecmp(cmd, "snr") == 0) {
        struct iwreq wrq;
        struct iw_statistics stats;
        int snr, rssi, noise;

        wrq.u.data.pointer = (caddr_t) &stats;
        wrq.u.data.length = sizeof(stats);
        wrq.u.data.flags = 1; /* Clear updated flag */
        strncpy(wrq.ifr_name, drv->ifname, IFNAMSIZ);

        if (ioctl(drv->ioctl_sock, SIOCGIWSTATS, &wrq) < 0) {
            perror("ioctl[SIOCGIWSTATS]");
            ret = -1;
        } else {
            if (stats.qual.updated & IW_QUAL_DBM) {
                /* Values in dBm, stored in u8 with range 63 : -192 */
                rssi = ( stats.qual.level > 63 ) ?
                    stats.qual.level - 0x100 :
                    stats.qual.level;
                noise = ( stats.qual.noise > 63 ) ?
                    stats.qual.noise - 0x100 :
                    stats.qual.noise;
            } else {
                rssi = stats.qual.level;
                noise = stats.qual.noise;
            }

            snr = rssi - noise;

            ret = snprintf(buf, buf_len, "snr = %u/n", (unsigned int)snr);
            if (ret < (int)buf_len) {
                return( ret );
            }
        }
    }
    else if (os_strncasecmp(cmd, "btcoexmode", 10) == 0) {
    }
    else if( os_strcasecmp(cmd, "btcoexstat") == 0 ) {
    }
    else {
        wpa_printf(MSG_DEBUG,"Unsupported command");
    }
    return (ret);
}
經過這些修改後,wifi應該可以上去了。
問題留存:

1。這次修改的是5.x版本的wpa_supplicant,android已經出了6.x版本,我在那裡沒有發現driver_wext.c!,那我上面的修改如何??看來還要研究。
2。修改好的driver_wext.c可以顯示MAC地址,在上層的設定介面也能顯示訊號強度,但在首頁面上卻沒有顯示強度,估計還要在java程式中找找原因。
3。在設定介面,如果將wifi關閉後再開啟,wifi連不上,需要手工在調試終端打ifconfig ra0 up才能觸發串連上。估計是java程式在關閉wifi時沒有將wifi設定進行初始化操作,要去看看設定的資料庫修改情況。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.