STA模式-wpa_cli使用

简介:

        wpa_supplicant包含两个主要的可执行工具:wpa_supplicant和wpa_cli。wpa_supplicant是核心程序,它和wpa_cli的关系就是服务和客户端的关系:后台运行wpa_supplicant,使用wpa_cli来搜索、设置、和连接网络。

        wpa_cli依赖于 wpa_supplicant,因此需要先启动 wpa_supplicant。

一、常用命令的使用

命令

返回值

含义

scan

ok

搜索wifi

scan_results

wifi名称

搜到到的全部wifi名称等信息

set_network

ok

设置wifi的SSID和psk

list_network

列出所有的配置文件中的信息

列出保存的文件中的所有信息

add_network

添加一个网络

添加一个网络

remove_network

删除一个网络,根据网络ID删除

删除一个网络ID,根据网络ID删除

select_netowrk

OK

选择网络,相当于连接命令。并且会把配置项中其它的网络 disable

enable_network

使能

使能

status

查询状态

查询状态

save_config

OK

保存配置

1、扫描 - scan

[root@demo]# wpa_cli -i wlan0 scan
OK

2、查看扫描结果 - scan_result

[root@demo]# wpa_cli -i wlan0 scan_result
bssid / frequency / signal level / flags / ssid
dc:fe:18:19:bf:4d       2462    -47     [WPA-PSK-CCMP][WPA2-PSK-CCMP][ESS]      TP-LINK_BF4D
34:0a:98:97:9a:14       2427    -66     [WPA-PSK-CCMP+TKIP][WPA2-PSK-CCMP+TKIP][WPS][ESS]       CMCC-3KUu
d4:62:ea:80:84:10       2462    -71     [WPA2-PSK-CCMP][WPS][ESS]       HUAWEI-H
68:db:54:6b:25:8f       2422    -75     [WPA-PSK-CCMP+TKIP][WPA2-PSK-CCMP+TKIP][ESS]    @PHICOMM_8D
50:fa:84:88:6a:85       2462    -75     [WPA-PSK-CCMP+TKIP][WPA2-PSK-CCMP+TKIP][ESS]    TP-LINK_6856
d0:76:e7:35:4a:03       2437    -77     [WPA-PSK-CCMP][WPA2-PSK-CCMP][ESS]      TP-LINK_903

3、设置network - set_network

[root@demo]$ wpa_cli -i wlan0 set_network 0 ssid '"name"'
[root@demo]$ wpa_cli -i wlan0 set_network 0 psk '"psk"'
[root@demo]$ wpa_cli -i wlan0 enable_network 0

4、查看networks - list_network

[root@demo]$ wpa_cli list_networks
Selected interface 'wlan0'
network        id    /ssid    /bssid/    flags
0                    any            [CURRENT]
1                    any            [DISABLE]

5、添加网络 - add_network

[root@demo]#wpa_cli -i wlan0 add_network
0
[root@demo]#wpa_cli -i wlan0 add_network
1

6、移除网络 - remove_network

# wpa_cli -i mlan0 remove_network 1

7、选择网络 - select_netowrk

# wpa_cli -i mlan0 select_network 1

8、使能 - enable_network

[root@demo]    $ wpa_cli -i wlan0 enable_network 0
OK

9、查询连接情况

[root@demo]    $ wpa_cli -i wlan0 status

二、功能使用举例

1、新建一个网络

/etc/wpa_supplicant.conf 配置 update_config=1,表示允许更新此配置文件,不然save_config会失败

连接WPA-PSK/WPA2-PSK密钥热点将key_mgmt密钥管理协议配置为WPA-PSK/WPA2-PSK;同时用psk属性来配置密码。

注:不管是WPA-PSK还是WPA2-PSK,其key_mgmt都为WPA-PSK

/ # wpa_cli -i mlan0 add_network        //添加网络,自动生成1
1
/ # wpa_cli -i mlan0 set_network 1 ssid '"name"'    //设置网络1的ssic
OK
/ # wpa_cli -i mlan0 set_network 1 key_mgmt WPA-PSK
OK
/ # wpa_cli -i mlan0 set_network 1 psk '"12345678"' //设置网络1的密码,要达到一定长度才OK
OK
/ # wpa_cli -i mlan0 enable_network 1               //使能网络1
OK
/ # wpa_cli -i mlan0 save_config        //save_config会将所有改动都存入wpa_supplicant.conf
OK

save_config后,wpa_supplicant.conf 多了一项

2、连接已有网络

# wpa_cli -i mlan0 list_network                  //列举保存过得连接
# wpa_cli -i mlan0 select_network  <network id>  //连接指定的ssid ,相当于连接命令
# wpa_cli -i mlan0 enable_network  <network id>  //使能制定的ssid 

3、扫描wifi

/ # wpa_cli -i mlan0 scan                //扫描
OK
/ # wpa_cli -i mlan0 scan_result         //扫描结果
bssid / frequency / signal level / flags / ssid
c0:f6:ec:7b:28:51    2432    -90    [WPA-PSK-CCMP][WPA2-PSK-CCMP][ESS]    X4#-Internet
c0:f6:ec:7b:28:50    2432    -88    [WPA-PSK-CCMP][WPA2-PSK-CCMP][ESS]    X4#-Office
c0:f6:ec:7b:28:52    2432    -89    [WPA-PSK-CCMP][WPA2-PSK-CCMP][ESS]    X4#-DMZ
c0:f6:ec:7b:28:53    2432    -90    [WPA-PSK-CCMP][WPA2-PSK-CCMP][ESS]    X4#-DYJ
88:40:3b:9b:00:c1    2462    -94    [WPA2-PSK-CCMP][ESS]    hu
72:77:24:37:2b:f6    2462    -94    [WPA-PSK-CCMP][WPA2-PSK-CCMP][ESS]    
68:77:24:37:2b:f6    2462    -93    [WPA-PSK-CCMP][WPA2-PSK-CCMP][ESS]    TP-LINK_2BF6
04:d9:f5:14:11:e0    2412    -93    [WPA2-PSK-CCMP][ESS]    ASUS_E0_2G

4、自动连接

若希望wifi启动后自动连接到预先设置的ap,只需按如下设置脚本即可以,如果预先设置里多个ap则每个网络需要设置优先级参数priority。

ctrl_interface=/var/run/wpa_supplicant
update_config=1
 
network={
ssid="xd_test"
psk="12345678"
priority=2
}

三、代码实例

        实现station模式wifi扫描、wifi连接、查看wifi连接状态等功能

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>

#define WIFI_STA_CMD_SCAN           "wpa_cli -i %s scan"
#define WIFI_STA_CMD_SCAN_RESULT    "wpa_cli -i %s scan_result"
#define WIFI_STA_CMD_LIST_NETWORK   "wpa_cli -i %s list_network"
#define WIFI_STA_CMD_SELECT_NETWORK "wpa_cli -i %s select_network %d"
#define WIFI_STA_CMD_SET_NETWORK    "wpa_cli -i %s set_network %d %s \'\"%s\"\'"
#define WIFI_STA_CMD_ADD_NETWORK    "wpa_cli -i %s add_network"
#define WIFI_STA_CMD_ENABLE_NETWORK "wpa_cli -i %s enable_network %d"
#define WIFI_STA_CMD_SAVE_CONFIG    "wpa_cli -i %s save_config"
#define WIFI_STA_CMD_STATUS         "wpa_cli -i %s status"

typedef enum{
    WPA_CLI_ACK,
    WPA_CLI_GET_ID,
    WPA_CLI_GET_STR,
}WPA_CLI_CMD_E;

typedef struct WIFI_STA_NETWORK_ITEM{
    u8 u8NetworkId;
    u8 au8Ssid[WIFI_STA_SCAN_SSID_LEN];
}WIFI_STA_NETWORK_ITEM_S;

typedef struct WIFI_STA_LIST_NETWORK_ITEM{
    u8 u8Num;
    WIFI_STA_NETWORK_ITEM_S stItem[WIFI_STA_SCAN_ITEM_NUM];
}WIFI_STA_LIST_NETWORK_ITEM_S;

/* list_network命令 */
static s32 wifi_sta_cmd_list_network(u8 *pu8Cmd, WIFI_STA_LIST_NETWORK_ITEM_S *pstList)
{
    s32 s32Ret = 0;
    FILE *pFile = NULL;
    u8 au8Buf[1024] = {0};
    u8 *pu8Start = NULL, *pu8End = NULL;
    int i = 0;

    if (pu8Cmd == NULL || pstList == NULL){
        return HAL_ERROR_CODE_PARAMETER_ERROR;
    }
    
    printf("[CMD] %s\n", pu8Cmd);

    pFile = popen((const char *)pu8Cmd, "r");
    if(pFile == NULL){
        perror("scan_result");
        return HAL_ERROR_CODE_WIFI_STA_POPEN;
    }

    int fd = fileno(pFile);
    int flags;
    flags = fcntl(fd,F_GETFL,0);
    flags |= O_NONBLOCK;        //不阻塞,防止在fgets一直阻塞
    fcntl(fd, F_SETFL, flags);
    sleep(1);  //这里必须要等待一下,否则fgets立即返回NULL,捕获不到数据

    memset(pstList, 0, sizeof(WIFI_STA_LIST_NETWORK_ITEM_S));
    memset(au8Buf, 0, sizeof(au8Buf));
    while(fgets((char *)au8Buf, sizeof(au8Buf), pFile) != NULL){
        if(strstr((const char *)au8Buf, "bssid") != NULL){     //忽略第一行
            continue;
        }
        
        pstList->stItem[i].u8NetworkId = atoi((const char *)&au8Buf[0]);
        pu8Start = (u8 *)strchr((const char *)au8Buf, '\t');
        if(pu8Start == NULL){
            s32Ret = HAL_ERROR_CODE_WIFI_STA_ACK_ERROR;
            break;
        }
        pu8Start = pu8Start + 1;

        pu8End = (u8 *)strchr((const char *)pu8Start, '\t');
        if(pu8End == NULL){
            s32Ret = HAL_ERROR_CODE_WIFI_STA_ACK_ERROR;
            break;
        }

        if (pu8End - pu8Start >= WIFI_STA_SCAN_SSID_LEN){
            s32Ret = HAL_ERROR_CODE_WIFI_STA_ACK_ERROR;
            break;
        }

        memcpy(pstList->stItem[i].au8Ssid, pu8Start, pu8End - pu8Start);
//        printf("%d - %s\n", pstList->stItem[i].u8NetworkId, pstList->stItem[i].au8Ssid);

        if (++i > WIFI_STA_SCAN_ITEM_NUM){
            break;
        }
    }

    pstList->u8Num = i;

    pclose(pFile);
    return s32Ret;
}

/* 判断命令的应答、获取命令参数 */
static s32 wpa_cli_cmd(u8 *pu8Cmd, WPA_CLI_CMD_E eType, char *pu8Data)
{
    s32 s32Ret;
    FILE *pFile = NULL;
    u8 au8Buf[128];

     if (pu8Cmd == NULL || pu8Data == NULL){
        return HAL_ERROR_CODE_PARAMETER_ERROR;
    }

    printf("[CMD] %s\n", pu8Cmd);

    pFile = popen((const char *)pu8Cmd, "r");
    if(pFile == NULL){
        perror("cmd_ack");
        return HAL_ERROR_CODE_WIFI_STA_POPEN;
    }

    int fd = fileno(pFile);
    int flags;
    flags = fcntl(fd,F_GETFL,0);    
    flags |= O_NONBLOCK;        //不阻塞
    fcntl(fd, F_SETFL, flags);
    usleep(100 * 1000);  //这里必须要等待一下,否则fgets立即返回NULL,捕获不到数据

    s32Ret = HAL_ERROR_CODE_WIFI_STA_ACK_ERROR;
    while(fgets((char *)au8Buf, sizeof(au8Buf), pFile) != NULL){
        if (eType == WPA_CLI_ACK){
            if (strstr((const char *)au8Buf, (const char *)pu8Data)){
                s32Ret = 0;
                break;
            }
        }else if (eType == WPA_CLI_GET_ID){
            *pu8Data = atoi((const char *)&au8Buf[0]);
            s32Ret = 0;
            break;
        }
    }

    pclose(pFile);
    return s32Ret;
}

/* scan命令 */
static s32 wifi_sta_cmd_scan(u8 *pu8Cmd)
{
    s32 s32Ret = HAL_ERROR_CODE_WIFI_STA_ACK_ERROR;
    FILE *pFile = NULL;
    u8 au8Buf[16] = {0};
    u8 *pu8Str = NULL;
    u8 u8Cnt = 10;
    
    while(--u8Cnt){
        printf("[CMD] %s\n", pu8Cmd);
        pFile = popen((const char *)pu8Cmd, "r");
        if(pFile == NULL){
            perror("scan");
            return HAL_ERROR_CODE_WIFI_STA_POPEN;
        }

        while(fgets((char *)au8Buf, sizeof(au8Buf), pFile) != NULL){
            pu8Str = (u8 *)strstr((const char *)au8Buf, "OK");
            if(pu8Str != NULL){
                s32Ret = 0;
                break;
            }
        }

        pclose(pFile);
        if (s32Ret == 0){
            break;
        }
        sleep(1);
    }
    
    return s32Ret;
}

/* scan_result命令 */
static s32 wifi_sta_cmd_scan_result(u8 *pu8Cmd, HAL_DEV_WIFI_STA_SCAN_S *pstScanInfo)
{
    FILE *pFile = NULL;
    u8 au8Buf[1024] = {0};
    u8 au8Freq[128];
    u8 au8Signal[128];
    int i = 0;

    printf("[CMD] %s\n", pu8Cmd);

    pFile = popen((const char *)pu8Cmd, "r");
    if(pFile == NULL){
        perror("scan_result");
        return HAL_ERROR_CODE_WIFI_STA_POPEN;
    }

    int fd = fileno(pFile);
    int flags;
    flags = fcntl(fd,F_GETFL,0);
    flags |= O_NONBLOCK;        //不阻塞
    fcntl(fd, F_SETFL, flags);
    sleep(1);  //这里必须要等待一下,否则fgets立即返回NULL,捕获不到数据

    memset(pstScanInfo, 0, sizeof(HAL_DEV_WIFI_STA_SCAN_S));
    memset(au8Buf, 0, sizeof(au8Buf));
    while(fgets((char *)au8Buf, sizeof(au8Buf), pFile) != NULL){
        if(strstr((const char *)au8Buf, "bssid") != NULL){     //忽略第一行
            continue;
        }

        sscanf((const char *)au8Buf, "%s\t%s\t%s\t%s\t%s", pstScanInfo->stItem[i].au8Bssid, au8Freq,
                                            au8Signal, pstScanInfo->stItem[i].au8Flags, pstScanInfo->stItem[i].au8Ssid);
        pstScanInfo->stItem[i].u16Frequency = atoi((const char *)au8Freq);
        pstScanInfo->stItem[i].s16SignalLevel = atoi((const char *)au8Signal);

        printf("%s %d %d %s %s\n", pstScanInfo->stItem[i].au8Bssid, pstScanInfo->stItem[i].u16Frequency, 
                                    pstScanInfo->stItem[i].s16SignalLevel, pstScanInfo->stItem[i].au8Flags, 
                                    pstScanInfo->stItem[i].au8Ssid);

        if (++i > WIFI_STA_SCAN_ITEM_NUM){
            break;
        }
    }

    pstScanInfo->u8Num = i;
    
    pclose(pFile);
    return 0;
}

/*
*********************************************************************************************************
*    函 数 名: wifi_sta_scan
*    功能说明: wifi station模式扫描
*    形    参: pu8Node:无线节点,如:mlan0
*            pstScan    :扫描结构体
*    返 回 值: 
*            0:成功。 其他:失败
*********************************************************************************************************
*/
s32 wifi_sta_scan(u8 *pu8Node, HAL_DEV_WIFI_STA_SCAN_S *pstScan)
{
    s32 s32Ret = 0;
    u8 au8Cmd[128];
    WIFI_STA_LIST_NETWORK_ITEM_S stListNetwork;

    if (pu8Node == NULL || pstScan == NULL){
        return HAL_ERROR_CODE_PARAMETER_ERROR;
    }

    /* 扫描命令 */
    sprintf((char *)au8Cmd, WIFI_STA_CMD_SCAN, pu8Node);
    s32Ret = wifi_sta_cmd_scan(au8Cmd);
    if (s32Ret != 0){
        return HAL_ERROR_CODE_WIFI_STA_SCAN;
    }
    
    /* 扫描结果 */
    sprintf((char *)au8Cmd, WIFI_STA_CMD_SCAN_RESULT, pu8Node);
    s32Ret = wifi_sta_cmd_scan_result(au8Cmd, pstScan);
    if (s32Ret != 0){
        return HAL_ERROR_CODE_WIFI_STA_SCAN_RESULT;
    }

    /* 使能 wpa_supplicant.conf 中所有的network,自动连接 */
    sprintf((char *)au8Cmd, WIFI_STA_CMD_LIST_NETWORK, pu8Node);
    s32Ret = wifi_sta_cmd_list_network(au8Cmd, &stListNetwork);
    for (int i = 0; i < stListNetwork.u8Num; i++){
        sprintf((char *)au8Cmd, WIFI_STA_CMD_ENABLE_NETWORK, pu8Node, stListNetwork.stItem[i].u8NetworkId);
        wpa_cli_cmd(au8Cmd, WPA_CLI_ACK, "OK");
    }

    return s32Ret;
}

/*
*********************************************************************************************************
*    函 数 名: wifi_sta_connect
*    功能说明: wifi station模式连接指定的AP
*    形    参: pu8Node:无线节点,如:mlan0
*            pstCfg    :需要连接的AP信息
*    返 回 值:
*            0:成功。 其他:失败
*********************************************************************************************************
*/
s32 wifi_sta_connect(u8 *pu8Node, HAL_DEV_WIFI_STA_CONNECT_S *pstCfg)
{
    s32 s32Ret = 0;
    s8 s8NetworkId = -1; 
    u8 au8Cmd[128];
    u8 *pu8Ssid;
    u8 *pu8Psk;
    u8 *pu8Key_mgmt;
    WIFI_STA_LIST_NETWORK_ITEM_S stListNetwork;

    if (pu8Node == NULL || pstCfg == NULL){
        return HAL_ERROR_CODE_PARAMETER_ERROR;
    }

    pu8Ssid = pstCfg->au8Ssid;
    pu8Psk = pstCfg->au8Psk;
    pu8Key_mgmt = pstCfg->au8Key_mgmt;

    /* 获取 wpa_supplicant.conf 中所有的network,比较要连接的AP是否存在 */
    sprintf((char *)au8Cmd, WIFI_STA_CMD_LIST_NETWORK, pu8Node);
    s32Ret = wifi_sta_cmd_list_network(au8Cmd, &stListNetwork);
    for (int i = 0; i < stListNetwork.u8Num; i++){
        if (strcmp((const char *)stListNetwork.stItem[i].au8Ssid, (const char *)pu8Ssid) == 0){
            s8NetworkId = i;
            break;
        }
    }
    
    /* 存在,则修改配置参数。不存在,则添加此network */
    if (s8NetworkId >= 0){        //存在,则修改配置参数
        sprintf((char *)au8Cmd, WIFI_STA_CMD_SET_NETWORK, pu8Node, s8NetworkId, "psk", pu8Psk);
        if (wpa_cli_cmd(au8Cmd, WPA_CLI_ACK, "OK") < 0){
            s32Ret = HAL_ERROR_CODE_WIFI_STA_SET_NETWORK;
            goto error;
        }

        if (pu8Key_mgmt != NULL){
            sprintf((char *)au8Cmd, "wpa_cli -i %s set_network %d %s %s", pu8Node, s8NetworkId, "key_mgmt", pu8Key_mgmt);
            wpa_cli_cmd(au8Cmd, WPA_CLI_ACK, "OK");
        }

        sprintf((char *)au8Cmd, WIFI_STA_CMD_ENABLE_NETWORK, pu8Node, s8NetworkId);
        if (wpa_cli_cmd(au8Cmd, WPA_CLI_ACK, "OK") < 0 ){
            s32Ret = HAL_ERROR_CODE_WIFI_STA_ENABLE_NETWORK;
            goto error;
        }

        sprintf((char *)au8Cmd, WIFI_STA_CMD_SAVE_CONFIG, pu8Node);
        if (wpa_cli_cmd(au8Cmd, WPA_CLI_ACK, "OK") < 0 ){
            s32Ret = HAL_ERROR_CODE_WIFI_STA_SAVE_CONFIG;
            goto error;
        }

        sprintf((char *)au8Cmd, WIFI_STA_CMD_SELECT_NETWORK, pu8Node, s8NetworkId);
        if (wpa_cli_cmd(au8Cmd, WPA_CLI_ACK, "OK") < 0 ){
            s32Ret = HAL_ERROR_CODE_WIFI_STA_SELECT_NETWORK;
            goto error;
        }
    }else{                        //不存在,则添加此network
        sprintf((char *)au8Cmd, WIFI_STA_CMD_ADD_NETWORK, pu8Node);
        if (wpa_cli_cmd(au8Cmd, WPA_CLI_GET_ID, (char *)&s8NetworkId) < 0 ){
            s32Ret = HAL_ERROR_CODE_WIFI_STA_ADD_NETWORK;
            goto error;
        }
    
        sprintf((char *)au8Cmd, WIFI_STA_CMD_SET_NETWORK, pu8Node, s8NetworkId, "ssid", pu8Ssid);
        if (wpa_cli_cmd(au8Cmd, WPA_CLI_ACK, "OK") < 0){
            s32Ret = HAL_ERROR_CODE_WIFI_STA_SET_NETWORK;
            goto error;
        }

        sprintf((char *)au8Cmd, "wpa_cli -i %s set_network %d %s %s", pu8Node, s8NetworkId, "key_mgmt", pu8Key_mgmt);
        if (wpa_cli_cmd(au8Cmd, WPA_CLI_ACK, "OK") < 0){
            s32Ret = HAL_ERROR_CODE_WIFI_STA_SET_NETWORK;
            goto error;
        }

        sprintf((char *)au8Cmd, WIFI_STA_CMD_SET_NETWORK, pu8Node, s8NetworkId, "psk", pu8Psk);
        if (wpa_cli_cmd(au8Cmd, WPA_CLI_ACK, "OK") < 0){
            s32Ret = HAL_ERROR_CODE_WIFI_STA_SET_NETWORK;
            goto error;
        }

        sprintf((char *)au8Cmd, WIFI_STA_CMD_ENABLE_NETWORK, pu8Node, s8NetworkId);
        if (wpa_cli_cmd(au8Cmd, WPA_CLI_ACK, "OK") < 0){
            s32Ret = HAL_ERROR_CODE_WIFI_STA_ENABLE_NETWORK;
            goto error;
        }

        sprintf((char *)au8Cmd, WIFI_STA_CMD_SAVE_CONFIG, pu8Node);
        if (wpa_cli_cmd(au8Cmd, WPA_CLI_ACK, "OK") < 0 ){
            s32Ret = HAL_ERROR_CODE_WIFI_STA_SAVE_CONFIG;
            goto error;
        }

        sprintf((char *)au8Cmd, WIFI_STA_CMD_SELECT_NETWORK, pu8Node, s8NetworkId);
        if (wpa_cli_cmd(au8Cmd, WPA_CLI_ACK, "OK") < 0){
            s32Ret = HAL_ERROR_CODE_WIFI_STA_SELECT_NETWORK;
            goto error;
        }
    }
    
error:
    return s32Ret;
}

/*
*********************************************************************************************************
*    函 数 名: wifi_sta_get_connect_info
*    功能说明: wifi station模式获取连接信息
*    形    参: pu8Node:无线节点,如:mlan0
*            pstConnectInfo    :获取到的连接信息。
                            STATE_SCANNING:未连接,还在尝试连接
                            STATE_COMPLETED:已连接上
*    返 回 值:
*            0:成功。 其他:失败
*********************************************************************************************************
*/
s32 wifi_sta_get_connect_info(u8 *pu8Node, HAL_DEV_WIFI_STA_CONNECT_INFO_S *pstConnectInfo)
{
    s32 s32Ret = 0;
    FILE *pFile = NULL;
    u8 au8Buf[1024] = {0};
    u8 au8Result[16];
    u8 au8Cmd[128];

    /* 获取状态 */
    sprintf((char *)au8Cmd, WIFI_STA_CMD_STATUS, pu8Node);
    pFile = popen((const char *)au8Cmd, "r");
    if(pFile == NULL){
        perror("scan_result");
        return HAL_ERROR_CODE_WIFI_STA_POPEN;
    }

    int fd = fileno(pFile);
    int flags;
    flags = fcntl(fd,F_GETFL,0);
    flags |= O_NONBLOCK;            //不阻塞
    fcntl(fd, F_SETFL, flags);
    sleep(1);  //这里必须要等待一下,否则fgets立即返回NULL,捕获不到数据

    memset(au8Buf, 0, sizeof(au8Buf));
    while(fgets((char *)au8Buf, sizeof(au8Buf), pFile) != NULL){
        sscanf((const char *)au8Buf, "ssid=%s", pstConnectInfo->au8Ssid);
        sscanf((const char *)au8Buf, "key_mgmt=%s", pstConnectInfo->au8key_mgmt);
        memset(au8Result, 0, sizeof(au8Result));
        sscanf((const char *)au8Buf, "wpa_state=%s", au8Result);
        if (!strcmp((const char *)au8Result, "SCANNING")){
            pstConnectInfo->eState = STATE_SCANNING;
            printf("SCANNING\n");
        }else if (!strcmp((const char *)au8Result, "COMPLETED")){
            pstConnectInfo->eState = STATE_COMPLETED;
            printf("COMPLETED\n");
        }
    }

    pclose(pFile);
    return s32Ret;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值