主机操作系统:Centos 6.7
交叉编译器版本:buildroot-2012.08 
开发板平台: FL2440 
Linux内核版本: linux-3.0 
开发模块:WIFI-STA模式使开发板上网



RT3070有两种工作模式:STA(station)模式和SoftAP(Access Point)模式。

STA模式就是笔记本上无线网卡模式,用来连接到无线路由器上上网。就是客户端模式(笔记本)。

linux3.0以上的内核中已经含有了RT3070的通用mac80211驱动框架,所以我首先直接基于此并借助wpa_suppicant软件来实现Wifi的STA模式。


一、配置Linux内核支持wireless无线上网




[*] Networking support  --->  
        -*-   Wireless  --->
     <*>   cfg80211 - wireless configuration API                                            
     [*]     enable powersave by default                            
     [ ]     cfg80211 DebugFS entries                                
     [*]     cfg80211 wireless extensions compatibility                
     [*]   Wireless extensions sysfs files                               
     {*}   Common routines for IEEE802.11 drivers                         
     [ ]   lib80211 debugging messages                              
     <*>   Generic IEEE 802.11 Networking Stack (mac80211)                
Device Drivers  --->
       Generic Driver Options  --->
      (/sbin/hotplug) path to uevent helper                                  
      -*- Userspace firmware loading support                                
     [*]   Include in-kernel firmware blobs in kernel binary              
      ()    External firmware blobs to build into the kernel binary         
    [*] Network device support  --->
           [*]   Wireless LAN  --->
               <*>   Ralink driver support  --->                                                            
               <*>   Ralink rt27xx/rt28xx/rt30xx (USB) support                      
                 [*]   Ralink debug output







检查以上选项在内核中都选好后,make编译,将编译好的内核烧录到开发板中,并插入rt3070无线网卡,打印如下信息。







华硕主板bios里的网络堆栈_开发板



>: lsusb
Bus 001 Device 001: ID 1d6b:0001
Bus 001 Device 002: ID 05e3:0606
Bus 001 Device 003: ID 148f:3070




二、下载无线网络配置工具Wireless tools和Wpa_supplicant以及Openssl移植


目前可以使用wireless-tools或wpa_supplicant工具来配置无线网络。但要注意对无线网络的配置是全局性的,而非针对具体的接口。wpa_supplicant是一个较好的选择,但缺点是它不支持所有的驱动。另外,wpa_supplicant目前只能连接到那些你已经配置好ESSID的无线网络。而wireless-tools支持几乎所有的无线网卡和驱动,但它不能连接到那些只支持WPA的AP。

1、  Wireless tools移植

在PC上下载压缩包


解压后修改Makefile

[fanmaolin@Centeros wireless_tools.29]$ vim Makefile 
11 ## Compiler to use (modify this for cross compile).
 12 CC = /opt/buildroot-2012.08/arm920t/usr/bin/arm-linux-gcc	//根据自己的路径修改
 13 ## Other tools you need to modify for cross compile (static lib only).
 14 AR = /opt/buildroot-2012.08/arm920t/usr/bin/arm-linux-ar
 15 RANLIB = /opt/buildroot-2012.08/arm920t/usr/bin/arm-linux-ranlib
[fanmaolin@Centeros wireless_tools.29]$ make







华硕主板bios里的网络堆栈_开发板_02



将ifrename、iwconfig、iwevent等绿色标记的工具下载到开发板的/bin和/lib 目录下,并给777权限


chmod 777 iw* libiw.so.29



2、 Openssl工具移植




现在的wifi网络大多都是WPA加密,所以wpa_supplicant对我们来说除了配置无线网络的需求之外,它更主要是用来支持WEP,WPA/WPA2和WAPI无线协议和加密认证的。而要加密就要使用到相关的加密算法,常用的密钥等,所以我们要先移植Openssl工具再移植wpa-supplicant。因为后面编译wpa_supplicant要依赖openssl中的lib和include




pc上下载压缩包




[fanmaolin@Centeros wifi]$ wget https://coding.net/u/sfantree/p/self_use_OSS/git/raw/master/source/openssl-1.0.0s.tar.gz



解压后在 openssl-1.0.0s目录下创建安装目录


[fanmaolin@Centeros openssl-1.0.0s]$ mkdir install


修改Makefile


[fanmaolin@Centeros openssl-1.0.0s]$ vim Makefile
 29 INSTALLTOP=/home/fanmaolin/fl2440/3rdparty/wifi/openssl-1.0.0s/install/
 30
 31 # Do not edit this manually. Use Configure --openssldir=DIR do change this!
 32 OPENSSLDIR=/home/fanmaolin/fl2440/3rdparty/wifi/openssl-1.0.0s/install/
 62 CC= /opt/buildroot-2012.08/arm920t/usr/bin/arm-linux-gcc
 63 CFLAG= -O
 64 DEPFLAG= -DOPENSSL_NO_GMP -DOPENSSL_NO_JPAKE -DOPENSSL_NO_MD2 -DOPENSSL_NO_RC5 -DOPENSSL_NO_RFC3779 -           DOPENSSL_NO_STORE
 65 PEX_LIBS=
 66 EX_LIBS=
 67 EXE_EXT=
 68 ARFLAGS=
 69 AR= /opt/buildroot-2012.08/arm920t/usr/bin/arm-linux-ar $(ARFLAGS) r
 70 RANLIB= /opt/buildroot-2012.08/arm920t/usr/bin/arm-linux-ranlib




[fanmaolin@Centeros openssl-1.0.0s]$ make&&make install
 
[fanmaolin@Centeros openssl-1.0.0s]$ ls install/lib/
 
engines libcrypto.a  libssl.a




把 libcrypto.a  libssl.a放到开发板/lib下




3、wpa_supplicant驱动移植




pc下载wpa_supplicant压缩包




[fanmaolin@Centeros wifi]$ wget https://coding.net/u/sfantree/p/self_use_OSS/git/raw/master/source/wpa_supplicant-0.7.3.tar.gz


解压后修改.config


[fanmaolin@Centeros wpa_supplicant-0.7.3]$ cd wpa_supplicant/
[fanmaolin@Centeros wpa_supplicant]$ cp defconfig .config
[fanmaolin@Centeros wpa_supplicant]$ vim .config

 27 #CC=/opt/brcm/hndtools-mipsel-uclibc/bin/mipsel-uclibc-gcc
 28 CC =/opt/buildroot-2012.08/arm920t/usr/bin/arm-linux-gcc -L/home/fanmaolin/fl2440/3rdparty/wifi/openssl-1.0.0s/install/lib/
 29 #CFLAGS += -Os
 30 CFLAGS +=-I/home/fanmaolin/fl2440/3rdparty/wifi/openssl-1.0.0s/install/include/
 31 #CPPFLAGS += -I../src/include -I../../src/router/openssl/include
 32 #LIBS += -L/opt/brcm/hndtools-mipsel-uclibc-0.9.19/lib -lssl
 33 LIBS +=-L/home/fanmaolin/fl2440/3rdparty/wifi/openssl-1.0.0s/install/lib/

[fanmaolin@Centeros wpa_supplicant]$ make


华硕主板bios里的网络堆栈_linux_03





然后将wpa_cli,wpa_supplicant,wpa_passphrase拷贝到开发板的/bin目录下并都给777权限






三、使能rt3070无线网卡






在开发板下查看网卡信息


>: ifconfig wlan0
wlan0     Link encap:Ethernet  HWaddr 00:A1:B0:40:6E:F7 
          BROADCAST MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0   //RX、TX均为0,表示没有数据
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)




第一行:连接类型:Ethernet(以太网)HWaddr(硬件mac地址) 
第三行:MULTICAST(支持组播)MTU:1500(最大传输单元):1500字节 
第四、五行:接收、发送数据包情况统计 


第七行:接收、发送数据字节数统计信息



在pc上下载3070固件,放到开发板/lib/firmware下,通常情况下,load firmware的动作是通过用户空间的hotplug handler,也就是/proc/sys/kernel/hotplug里设定的执行档,来完成。root_qtopia默认会在/etc/init.d/rcS里将/proc/sys/kernel/hotplug设定为/sbin/mdev,即busybox的mdev模块。它在收到来自内核的请求加载rt3070.bin这个firmware消息后,会从/lib/firmware目录下去找rt3070.bin,如果能找到就加载




wget  https://coding.net/u/sfantree/p/self_use_OSS/git/raw/master/source/rt2870.bin



>: cd lib/
>: mkdir fireware
>: cd lib/
>: mkdir fireware
>: cd firmware/
>: ls
rt2870.bin

>: ifconfig wlan0 up    //开启无线网卡




四、rt3070无线模块连接路由器




>: cd apps/
>: mkdir etc
>: cd etc/
>: mkdir wireless
>: cd wireless




在开发板上输入命令:wpa_passphrase ssid psk>>wpa_supplicant.conf  (ssid就是你wifi的名字 psk就是密码)


>: wpa_passphrase Router_LingYun lingyun_emb>>wpa_supplicant.conf
# WPA-PSK/TKIP
# wpa_supplicant -Dwext -iwlan0 -c/apps/etc/wireless/wpa_supplicant.conf
ctrl_interface=/var/run/wpa_supplicant
network={
        ssid="Router_LingYun"
        key_mgmt=WPA-PSK 
        proto=WPA                                                           
        pairwise=TKIP CCMP                                                 
        #psk="lingyun_emb"                                                 
        psk=3083bacdc70aa9a917269b3fb19412d44a4464ecafbca12d5916f091d9d0450a
}
~

>: wpa_supplicant -Dwext -iwlan0 -c/apps/etc/wireless/wpa_supplicant.conf &//连接路由器




-Dwext :wext,驱动名称

-iwlan0 :wlan0,网络接口名称


-c/apps/etc/wireless/wpa_supplicant.conf :wpa_supplicant的配置文件path






若成功连接路由器,则打印如下信息:


>: Trying to associate with c0:61:18:f5:3e:62 (SSID='Router_LingYun' freq=2462 MHz)
cfg80211: Calling CRDA for country: CN
Associated with c0:61:18:f5:3e:62
cfg80211: Regulatory domain changed to country: CN
cfg80211:     (start_freq - end_freq @ bandwidth), (max_antenna_gain, max_eirp)
cfg80211:     (2402000 KHz - 2482000 KHz @ 40000 KHz), (N/A, 2000 mBm)
cfg80211:     (5735000 KHz - 5835000 KHz @ 40000 KHz), (N/A, 3000 mBm)
cfg80211:     (57240000 KHz - 59400000 KHz @ 2160000 KHz), (N/A, 2800 mBm)
cfg80211:     (59400000 KHz - 63720000 KHz @ 2160000 KHz), (N/A, 4400 mBm)
cfg80211:     (63720000 KHz - 65880000 KHz @ 2160000 KHz), (N/A, 2800 mBm)
WPA: Key negotiation completed with c0:61:18:f5:3e:62 [PTK=CCMP GTK=TKIP]
CTRL-EVENT-CONNECTED - Connection to c0:61:18:f5:3e:62 completed (auth) [id=0 id_str=]




设置你的无限网卡IP和子网掩码//要注意不能和自己的eth0的Ip在同一网段内


>: ifconfig wlan0 192.168.2.50 netmask 255.255.255.0
>: ping 192.168.2.105    //ping 自己的主机IP
PING 192.168.2.105 (192.168.2.105): 56 data bytes
64 bytes from 192.168.2.105: seq=0 ttl=128 time=268.957 ms
64 bytes from 192.168.2.105: seq=1 ttl=128 time=68.606 ms
64 bytes from 192.168.2.105: seq=2 ttl=128 time=51.662 ms
64 bytes from 192.168.2.105: seq=3 ttl=128 time=56.586 ms
64 bytes from 192.168.2.105: seq=4 ttl=128 time=45.697 ms
64 bytes from 192.168.2.105: seq=5 ttl=128 time=4.613 ms
64 bytes from 192.168.2.105: seq=6 ttl=128 time=47.652 ms
64 bytes from 192.168.2.105: seq=7 ttl=128 time=44.600 ms
64 bytes from 192.168.2.105: seq=8 ttl=128 time=46.711 ms




连接外网 在/etc/创建resolv.conf的文件


因为在内核启动的时候内核会在resolv.conf文件中找dns


配置DNS:echo "nameserver 8.8.8.8" > /etc/resolv.conf
>: cat /etc/resolv.conf
nameserver 8.8.8.8
配置默认网关:route add default gw 192.168.2.1
ping百度官网
>: ping www.baidu.com
PING www.baidu.com (180.97.33.107): 56 data bytes
64 bytes from 180.97.33.107: seq=1 ttl=54 time=185.751 ms
64 bytes from 180.97.33.107: seq=2 ttl=54 time=68.647 ms
64 bytes from 180.97.33.107: seq=3 ttl=54 time=72.669 ms
64 bytes from 180.97.33.107: seq=4 ttl=54 time=78.230 ms

--- www.baidu.com ping statistics ---
5 packets transmitted, 4 packets received, 20% packet loss
round-trip min/avg/max = 68.647/101.324/185.751 ms


到这里表示开发板已经能够上网了,OK





查看自己的网卡信息:


>: ifconfig
eth0      Link encap:Ethernet  HWaddr 06:16:C9:53:1E:95 
          inet addr:192.168.3.1  Bcast:192.168.3.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:128 errors:0 dropped:0 overruns:0 frame:0
          TX packets:261 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:12605 (12.3 KiB)  TX bytes:10962 (10.7 KiB)
          Interrupt:51 Base address:0x4300

wlan0     Link encap:Ethernet  HWaddr 00:A1:B0:40:6E:F7 
          inet addr:192.168.2.50  Bcast:192.168.2.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:429 errors:0 dropped:143 overruns:0 frame:0			//RX,TX有数据传输
          TX packets:70 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:35705 (34.8 KiB)  TX bytes:8877 (8.6 KiB)






现在总结一下开发板上网的流程:




首先下载安装好各种工具,上面有详细步骤




1、ifconfig wlan0 up//使能网卡
  
   2、连你想连的WIFIwpa_passphrase ssid psk>>wpa_supplicant.conf  
  
3、wpa_passphrase Router_LingYun lingyun_emb>>wpa_supplicant.conf//修改脚本,生成秘钥
  
4、wpa_supplicant -Dwext -iwlan0 -c/apps/etc/wireless/wpa_supplicant.conf &//连接路由器


5、

设置你的无限网卡IP和子网掩码//要注意不能和自己的eth0的Ip在同一网段内


>: ifconfig wlan0 192.168.2.50 netmask 255.255.255.0


6、配置DNS:echo "nameserver 8.8.8.8" > /etc/resolv.conf   //8.8.8.8是Google的

7、配置默认网关:route add default gw 192.168.2.1//路由器ip



如果你设置过一次WIFI,那么就执行1、4、5、6、7就可以通过开发板上网了




下面会用DHCP自动获取IP地址来上网









问题总结:



1、无法连接wifi,明明已经设置了wifi名字和密码,错误信息如下:




Failed to initiate AP scan.
   
ioctl[SIOCSIWSCAN]: Device or resource busy
   
Failed to initiate AP scan.
   
Trying to associate with d4:ee:07:22:6b:5c (SSID='HiWiFi_LingYun' freq=2427 MHz)
   
cfg80211: Calling CRDA for country: CN
   
Associated with d4:ee:07:22:6b:5c
   
cfg80211: Regulatory domain changed to country: CN
   
cfg80211:     (start_freq - end_freq @ bandwidth), (max_antenna_gain, max_eirp)
   
cfg80211:     (2402000 KHz - 2482000 KHz @ 40000 KHz), (N/A, 2000 mBm)
   
cfg80211:     (5735000 KHz - 5835000 KHz @ 40000 KHz), (N/A, 3000 mBm)
   
cfg80211:     (57240000 KHz - 59400000 KHz @ 2160000 KHz), (N/A, 2800 mBm)
   
cfg80211:     (59400000 KHz - 63720000 KHz @ 2160000 KHz), (N/A, 4400 mBm)
   
cfg80211:     (63720000 KHz - 65880000 KHz @ 2160000 KHz), (N/A, 2800 mBm)
   
cfg80211: Calling CRDA to update world regulatory domain
   
CTRL-EVENT-DISCONNECTED bssid=d4:ee:07:22:6b:5c reason=0



可能是我之前改错了某些地方,但是检查了几遍也没有发现,于是重新设置了另一个WIFI,就是我上面用的wifi,要重新生成秘钥,就可以连接了。





2、


>: ping www.baidu.com
   
ping: bad address 'www.baidu.com'



可以Ping自己的主机Ip却不能ping百度,分析dns和网关,发现网关没有配置,配置后解决问题


route add default  gw 192.168.2.1