午夜视频在线网站,日韩视频精品在线,中文字幕精品一区二区三区在线,在线播放精品,1024你懂我懂的旧版人,欧美日韩一级黄色片,一区二区三区在线观看视频

分享

Android WIFI 移植

 techres 2012-03-29

Android WIFI 移植  

2010-12-30 22:51:30|  分類: 技術(shù) |  標(biāo)簽: |字號 訂閱

在 Android 2.2上移植了2個wifi模塊,vt6656和rt2070,總結(jié)一下要點。
首先,將wifi linux驅(qū)動編譯成模塊,并將驅(qū)動(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根據(jù)實際配置寫入該文件
          mkdir /data/misc/wifi/sockets 0777 wifi wifi  #與上層通過socket通信的路徑
           # Prepare for wifi
           setprop wifi.interface ra0  #intreface名稱設(shè)置,這在framework/base/wifi/java/android/net/wifi/WifiStateTracker.java中會用到,以處理dhcp。rt2070用ra0,而vt6656使用eth1。
這里0771對目錄權(quán)限的處理是為了所有用戶能對下一級進(jìn)行搜索,而紅字特別提醒的權(quán)限配置,是因為/data/misc/wifi/sockets目錄不僅為wifi擁有者服務(wù),還因為通信的原因要和其他用戶聯(lián)系,要不然,將會出現(xiàn)Unable to open connection to supplicant on "/data/system/wpa_supplicant/ra0": Connection refused,或permission denied的錯誤。很多人干脆將上述所有的權(quán)限都設(shè)為0777,當(dāng)然也行,但總覺得有些粗糙。
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。
例如,F(xiàn)reeescale 是在device/fsl/imx51_bbg/,加入:
HAVE_CUSTOM_WIFI_DRIVER_2 := true
BOARD_WPA_SUPPLICANT_DRIVER := WEXT
而下面已經(jīng)定義的wifi驅(qū)動的路徑我感到還是屏蔽為好,直接在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的路徑。在調(diào)試時可以查詢是否已經(jīng)安裝了模塊,接口是否啟動。
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);  #增加權(quán)限
這是為了防止出現(xiàn)ioctl 寫message的錯誤。
修改external/wpa_supplicant/driver_wext.c:
這是為了避免wpa_supplicant與下層驅(qū)動通訊時出現(xiàn)ioctl[SIOCSIWPRIV]錯誤,因為現(xiàn)在大部分wifi模塊對SIOCSIWPRIV命令不處理,而這個命令要用于偵測wifi強(qiáng)度RSSI的,比較簡單的方法是在wifi驅(qū)動中增加個空函數(shù)。例如,對于rt2070,有一個sta_ioctl.c,找到SIOCSIWPRIV,將其對應(yīng)個空函數(shù)
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函數(shù)改成如下函數(shù)(我從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);
}
經(jīng)過這些修改后,wifi應(yīng)該可以上去了。
問題留存:
1。這次修改的是5.x版本的wpa_supplicant,android已經(jīng)出了6.x版本,我在那里沒有發(fā)現(xiàn)driver_wext.c!,那我上面的修改如何實現(xiàn)?看來還要研究。
2。修改好的driver_wext.c可以顯示MAC地址,在上層的設(shè)置界面也能顯示信號強(qiáng)度,但在主頁面上卻沒有顯示強(qiáng)度,估計還要在java程序中找找原因。
3。在設(shè)置界面,如果將wifi關(guān)閉后再打開,wifi連不上,需要手工在調(diào)試終端打ifconfig ra0 up才能觸發(fā)連接上。估計是java程序在關(guān)閉wifi時沒有將wifi設(shè)置進(jìn)行初始化操作,要去看看設(shè)置的數(shù)據(jù)庫修改情況。

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多