wpa_supplicant是一款配置无线网络的开源工具,功能很强大,编译出来只有2个工具,一个wpa_supplicant和wpa_cli。
wpa_supplicant用来做后台守护进程,wpa_cli做实际配置用。关于配置网络的常用命令这里我就不多讲了,很多地方都能找到
,这里我们要讲的是另外一种情况:能不能把wpa_cli的功能部分做成库文件,通过一个接口来实现wpa_cli命令的功能,执行后
的状态信息返回到这个接口的buffer中,在代码中分析buffer中的内容做相应的处理?
网上也有类似的处理,如 浓咖啡jy https://blog.csdn.net/jy1075518049/article/details/51304346,这里我详细介绍下具体实现过程。
1.在wpa_supplicant源码目录下,复制一份wpa_cli3.c
2.修改wpa_cli3.c中的代码,我们注意到,所有的命令最终实现都是在_wpa_ctrl_command函数中,因此,需要对其进行改造,加入char *buf,接收返回的状态信息。如下:
//static int _wpa_ctrl_command(struct wpa_ctrl *ctrl, char *cmd, int print)
static int _wpa_ctrl_command(struct wpa_ctrl *ctrl, char *buf ,char *cmd, int print)
{
char buf_ret[2048];
size_t len;
int ret;
if (ctrl_conn == NULL) {
printf("Not connected to wpa_supplicant - command dropped.\n");
return -1;
}
len = sizeof(buf_ret) - 1;
ret = wpa_ctrl_request(ctrl, cmd, os_strlen(cmd), buf_ret, &len,
wpa_cli_msg_cb);
if (ret == -2) {
printf("'%s' command timed out.\n", cmd);
return -2;
} else if (ret < 0) {
printf("'%s' command failed.\n", cmd);
return -1;
}
if(buf){ //复制状态信息
buf_ret[len] = '\0';
memcpy(buf,buf_ret,2048);
}
if (print) {
buf_ret[len] = '\0';
printf("%s", buf_ret);
if (interactive && len > 0 && buf_ret[len - 1] != '\n')
printf("\n");
}
return 0;
}
3.代码中其余部分有调用_wpa_ctrl_command函数的都需要添加char* buf参数进行相应的修改。如下
static int wpa_cli_cmd_scan(struct wpa_ctrl *ctrl, int argc, char *buf, char *argv[])
{
return wpa_ctrl_command(ctrl, buf, "SCAN");
}
4.上述修改完成后。在wpa_cli3.c目录下,新建一个wpa_cli3.h,内容如下:
#ifndef __WPA_CLI3_H
#define __WPA_CLI3_H
//外部调用的接口函数
//pcm:字符串 buf:返回的信息缓冲 argu:附加命令
int wpa_cli_cmd_function(char *pcm, char *buf, char* argu);
#endif
5.wpa_cli3.c中新建 int wpa_cli_cmd_function(char *pcm, char *buf, char* argu);接口函数,下面是实现
//初始化部分
static int wpa_cli_start(void)
{
//1.变量初始化
int conectNum = 0;
ctrl_ifname = NULL;
ctrl_conn = NULL;
mon_conn = NULL;
if (os_program_init())
return -1;
if (eloop_init())
return -1;
eloop_register_signal_terminate(wpa_cli_terminate, NULL);
//2.与wpa_supplicant建立连接
while(1)
{
if(ctrl_ifname==NULL)
{
ctrl_ifname = wpa_cli_get_default_ifname();
if(ctrl_ifname == NULL){
printf("failed to wpa_cli_get_default_ifname");
return -1;
}
}
wpa_cli_open_connection(ctrl_ifname,0);
if (ctrl_conn || mon_conn){
printf("wpa_supplicant connection established");
break;
}else{
if(conectNum++ >=3){
printf("wpa_supplicant connection established err");
return -1;
}
printf("wpa_supplicant connection established err, we will try agin");
usleep(20000);
continue;
}
}
return 0;
}
int wpa_cli_cmd_function(char *pcm, char *buf, char *argu)
{
int ret = 0;
char augubuf[64];
ret = wpa_cli_start();
if(ret<0)
{
perror("wpa_cli_start fail!\n");
}
if(argu == NULL)
{
if( strcmp(pcm,"status")==0)
{
ret = wpa_ctrl_command(ctrl_conn, buf, "STATUS");
}
else if(strcmp(pcm,"list_networks")==0)
{
ret = wpa_ctrl_command(ctrl_conn, buf, "LIST_NETWORKS");
}
else if(strcmp(pcm,"ping")==0)
{
ret = wpa_ctrl_command(ctrl_conn, buf, "PING");
}
else if(strcmp(pcm,"disconnect")==0)
{
ret = wpa_ctrl_command(ctrl_conn, buf, "DISCONNECT");
}
else if(strcmp(pcm,"reconnect")==0)
{
ret = wpa_ctrl_command(ctrl_conn, buf, "RECONNECT");
}
else if(strcmp(pcm,"save_config")==0)
{
ret = wpa_ctrl_command(ctrl_conn, buf, "SAVE_CONFIG");
}
else if(strcmp(pcm,"scan")==0)
{
ret = wpa_ctrl_command(ctrl_conn, buf, "SCAN");
}
else if(strcmp(pcm,"scan_results")==0)
{
ret = wpa_ctrl_command(ctrl_conn, buf, "SCAN_RESULTS");
}
else if(strcmp(pcm,"logon")==0)
{
ret = wpa_ctrl_command(ctrl_conn, buf, "LOGON");
}
else if(strcmp(pcm,"logoff")==0)
{
ret = wpa_ctrl_command(ctrl_conn, buf, "LOGOFF");
}
else if(strcmp(pcm,"add_network")==0)
{
ret = wpa_ctrl_command(ctrl_conn, buf, "ADD_NETWORK");
}
}
else if(argu)
{
if( strcmp(pcm,"set_network")==0)
{
sprintf(augubuf,"SET_NETWORK %s",argu);
ret = wpa_ctrl_command(ctrl_conn, buf, augubuf);
}
else if(strcmp(pcm,"enable_network")==0)
{
sprintf(augubuf,"ENABLE_NETWORK %s",argu);
ret = wpa_ctrl_command(ctrl_conn, buf, augubuf);
}
else if(strcmp(pcm,"disable_network")==0)
{
sprintf(augubuf,"DISABLE_NETWORK %s",argu);
ret = wpa_ctrl_command(ctrl_conn, buf, augubuf);
}
}
os_free(ctrl_ifname);
eloop_destroy();
wpa_cli_cleanup();
return ret;
}
6.修改wpa_cli3.c目录下的Makefile,使其编译生成库文件,这里我们就定义名称为wpa_cli3.a。
7.make,在当前目录生成libwpa_cli3.a,就是我们需要的静态库文件,配上wpa_cli3.h就可以在代码中进行接口调用了。
8.写代码测试。这里只实现扫描,和显示扫描结果的功能。
int main(int argc, char *argv[])
{
int ret;
char buff[2048];
ret = wpa_cli_start();
if(ret<0)
{
perror("wpa_cli_start fail!\n");
}
wpa_cli_cmd_function("scan",buff,NULL);
sleep(3);
wpa_cli_cmd_function("scan_results",buff,NULL);
buff[2047]='\0';
printf("buff=%s\n",buff);
return 0;
}
实际测试效果,可见打印的buff=有信息输出了。