linux下WIFI模块使用:wpa_supplicant工具交叉编译以及配置

编译环境:ubuntu14.04或ubuntu16.04

交叉编译工具链:arm-linux-gcc-4.3.2

安装依赖库openssl

源码下载:https://github.com/openssl/openssl/releases/tag/OpenSSL_1_1_1b

1) 配置

cd /root/wifi
tar -xvf openssl-OpenSSL_1_1_1b.tar.gz 
cd openssl-OpenSSL_1_1_1b/
mkdir build
./config shared no-asm --prefix=$PWD/build

./config表示根据后面的编译选项生成一Makefile文件,“shared”表示生成共享库;“no-asm”表示去掉汇编部分;“–prefix=”后面跟着生成的项目所存放的路径。

2)修改Makefile:

PLATFORM=arm-linux
CROSS_COMPILE=arm-linux-
CNF_CFLAGS=-pthread
CNF_CXXFLAGS=-std=c++11 -pthread

3)编译&&安装

make 
make install

编译安装后在/root/wifi/openssl-OpenSSL_1_1_1b/build/lib/目录内容如下:

# ls /root/wifi/openssl-OpenSSL_1_1_1b/build/lib/
engines-1.1  libcrypto.a  libcrypto.so  libcrypto.so.1.1  libssl.a  libssl.so  libssl.so.1.1  pkgconfig

 4) 复制库文件到开发板文件系统/usr/lib目录下

这里需要注意的是,拷贝时使用cp -rf,连同软链接一起拷贝。

 

交叉编译wpa_supplicant

源码下载:http://w1.fi/wpa_supplicant/

下载最新版本:http://w1.fi/releases/wpa_supplicant-2.9.tar.gz

1)创建一个.config文件

cd /root/wifi/
tar -xvf wpa_supplicant-2.9.tar.gz
cd wpa_supplicant-2.9/wpa_supplicant
cp defconfig .config

 

2) 修改.config文件

修改如下部分:

# Additional directories for cross-compilation on Linux host for mingw target
#CFLAGS += -I/opt/mingw/mingw32/include/ddk
#LIBS += -L/opt/mingw/mingw32/lib
#CC=mingw32-gcc

 # Additional directories for cross-compilation on Linux host for mingw target
 CFLAGS += -I/root/wifi/openssl-OpenSSL_1_1_1b/build/include
 LIBS += -L/root/wifi/openssl-OpenSSL_1_1_1b/build/lib
 CFLAGS += -I/root/wifi/libnl-3.2.23/build/include
 LIBS += -L/root/wifi/libnl-3.2.23/build/lib
 CC=arm-linux-gcc

CFLAGS为编译安装好的openssl的include目录 和 libnl的include目录
LIBS为编译安装好的openssl的lib目录 和 libnl的include目录
CC为交叉编译工具

libnl库的交叉编译见:https://www.cnblogs.com/wanglouxiaozi/p/12828823.html

3)添加环境变量

export PKG_CONFIG_PATH=/root/wifi/libnl-3.2.23/build/lib/pkgconfig:$PKG_CONFIG_PATH

4)编译

执行make后会出现以下错误:

dbus/dbus_dict_helpers.c:10:23: warning: dbus/dbus.h: No such file or directory
In file included from dbus/dbus_dict_helpers.c:14:

修改.config文件,注释掉以下两个选项:

#CONFIG_CTRL_IFACE_DBUS_NEW=y
#CONFIG_CTRL_IFACE_DBUS_INTRO=y

继续执行make,又出现如下错误:

../src/drivers/driver_wext.c: In function 'wpa_driver_wext_set_key_ext':
../src/drivers/driver_wext.c:1780: error: 'IW_ENCODE_ALG_AES_CMAC' undeclared (first use in this function)
../src/drivers/driver_wext.c:1780: error: (Each undeclared identifier is reported only once
../src/drivers/driver_wext.c:1780: error: for each function it appears in.)
../src/drivers/driver_wext.c: In function 'wpa_driver_wext_associate':
../src/drivers/driver_wext.c:2215: error: 'IW_AUTH_MFP_DISABLED' undeclared (first use in this function)
../src/drivers/driver_wext.c:2218: error: 'IW_AUTH_MFP_OPTIONAL' undeclared (first use in this function)
../src/drivers/driver_wext.c:2221: error: 'IW_AUTH_MFP_REQUIRED' undeclared (first use in this function)
../src/drivers/driver_wext.c:2224: error: 'IW_AUTH_MFP' undeclared (first use in this function)
Makefile:1990: recipe for target '../src/drivers/driver_wext.o' failed

解决办法:

在kernel源码里搜索上述宏:

grep "IW_ENCODE_ALG_AES_CMAC" * -nR /root/linux-3.4.2

得到以下内容:

/root/linux-3.4.2/include/linux/wireless.h:626:#define IW_ENCODE_ALG_AES_CMAC    5
/root/linux-3.4.2/net/wireless/wext-compat.c:699:    case IW_ENCODE_ALG_AES_CMAC:

在/root/linux-3.4.2/include/linux/wireless.h里找出上述未定义的几个宏,添加到../src/drivers/driver_wext.c的开头。

下面列出以下几个宏:

#define IW_ENCODE_ALG_AES_CMAC  5
/* IW_AUTH_MFP (management frame protection) values */
#define IW_AUTH_MFP_DISABLED    0   /* MFP disabled */
#define IW_AUTH_MFP_OPTIONAL    1   /* MFP optional */
#define IW_AUTH_MFP_REQUIRED    2   /* MFP required */
#define IW_AUTH_MFP         12

继续make,编译成功。

在当前目录下(/root/wifi/wpa_supplicant-2.9/wpa_supplicant)生成了wpa_cli、wpa_passphrase、wpa_supplicant可执行程序。

wpa_supplicant是核心程序,它和wpa_cli的关系就是服务和客户端的关系

wpa_cli用来搜索、设置、和连接网络。

wpa_passphrase网络配置可以使用wpa_passphrase工具自动生成并添加到配置文件中。
把这三个文件复制到开发板的根文件/user/bin目录下就可以使用了。

 使用wpa_supplicant工具连接WIFI

    1) 创建配置文件
wpa_supplicant连接WIFI的时候会从配置文件中读取账号和密码,以及加密方式等, 所以我们再运行wpa_supplicant工具的时候要提前写好配置文件。
配置文件名称自定,但是要以 .conf 为后缀,并保存在 /etc/ 目录下,这边给出一个配置文件的例子。

ctrl_interface=/var/run/wpa_supplicant  # 一个目录,用于wpa_supplicant和wpa_cli的socket通信
network={
        ssid="dswei" # WIFI名称
        psk="baiwenwang123" # WIFI密码
}

在一般使用中我们只要设置ssidpsk,其他全部使用默认的,就可以连接上WIFI。

2)打开wlan0

ifconfig wlan0 up

3)创建一个socket通信的目录

mkdir -p /var/run/wpa_supplicant

4)测试和连接WIFI

wpa_supplicant -B -c/etc/plaintext.conf -iwlan0

-B为后台运行,-c为配置文件目录,plaintext.conf为配置文件的名称,根据实际情况填写。其他配置选项可以看wpa_supplicant-2.9/wpa_supplicant/README,然后我们就可以看到开发板和路由器连接上了。

5)测试wifi网络连通性

ifconfig wlan0 192.168.0.10
ping -I wlan0 192.168.0.1 //192.168.0.1为路由器的IP

 

访问外网

1)修改/etc/resolv.conf添加DNS:

nameserver 192.168.0.1  #192.168.0.1是路由器的LAN口IP地址

下面两个DNS是通用的:

nameserver 8.8.8.8 #google域名服务器 
nameserver 8.8.4.4 #google域名服务器

2)设置网关:

route add default gw 192.168.0.1 dev wlan0
route add default gw 192.168.0.1 dev eth0

设置完成后可以查看一下网关是否设置正确:

route -n

或者

netstat -r

3)测试网络连通性

ping -I wlan0 www.baidu.com
ping -I eth0 www.baidu.com
posted @ 2020-05-05 21:27  闹闹爸爸  阅读(4821)  评论(8编辑  收藏  举报