Преглед изворни кода

继续完善wifi与外部接口

guohui пре 1 година
родитељ
комит
a31ec002cf
13 измењених фајлова са 533 додато и 398 уклоњено
  1. 1 0
      pro/config/cfg.h
  2. 7 24
      pro/src/Cellular.c
  3. 0 3
      pro/src/Cellular.h
  4. 1 1
      pro/src/app.c
  5. 8 2
      pro/src/appweb_handle.c
  6. 0 28
      pro/src/common.c
  7. 13 12
      pro/src/common.h
  8. 6 0
      pro/src/dflt.c
  9. 76 44
      pro/src/paras.c
  10. 19 0
      pro/src/sys.c
  11. 1 0
      pro/src/sys.h
  12. 390 276
      pro/src/wifi.c
  13. 11 8
      pro/src/wifi.h

+ 1 - 0
pro/config/cfg.h

@@ -42,6 +42,7 @@
 #define USE_MQTT
 #define MQTT_SERVER_BUILTIN
 
+//#define USE_CELL
 //#define USE_WIFI
 
 

+ 7 - 24
pro/src/Cellular.c

@@ -3,6 +3,7 @@
 #include "Cellular.h"
 #include <pthread.h>
 #include "thread.h"
+#include "paras.h"
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -282,9 +283,13 @@ static void* Cellular_thread(void *arg)
  */
 int Cellular_init(void)
 {
+    CellularManager *pcell=&_globalCellularManager;
+
+    memset(pcell, 0, sizeof(CellularManager));
+    pcell->mode = __globalDeviceManage._globalDevInfo.cell;
     if (0==thread_is_running(THREAD_ID_Cellular))
     {
-        InitCellularManager();
+        
         thread_start(THREAD_ID_Cellular, Cellular_thread, &_globalCellularManager);
     }
     return 0;
@@ -292,32 +297,10 @@ int Cellular_init(void)
 
 int Cellular_deinit(void)
 {
-    InitCellularManager();
+    
     return 0;
 }
 
-int WIFI_init(void)
-{
-    int r = -1;
-
-    InitWIFIManager();
-    if (_globalWIFIManager.mode == 1)
-    {
-        //thread_start(THREAD_ID_Cellular, Cellular_thread, &_globalCellularManager);
-        r = 0;
-    }
 
-    return r;
-}
-
-int WIFI_deinit(void)
-{
-        int r=-1;
-        if (_globalWIFIManager.mode == 1)
-        {
-            r = 0;
-        }
-        return r;
-}
 
 

+ 0 - 3
pro/src/Cellular.h

@@ -4,9 +4,6 @@
 int Cellular_init(void);
 int Cellular_deinit(void);
 
-int WIFI_init(void);
-int WIFI_deinit(void);
-
 void send_at_command(int fd, const char *cmd);
 int set_serial_attributes(int fd, int speed);
 

+ 1 - 1
pro/src/app.c

@@ -2608,7 +2608,7 @@ int app_init(void)
     appweb_init();
 
     //Cellular_init();
-   // WIFI_init();
+   // wifi_init();
 
     upg_init();
     dev_Alarm_Run_message(_globalDeviceManager,language_alarm_Init_Success[0],"Server Thread");

+ 8 - 2
pro/src/appweb_handle.c

@@ -17,6 +17,7 @@
 #include "websocket_handle.h"
 #include "breaker_detection.h"
 #include "Cellular.h"
+#include "wifi.h"
 
 #define TOTAL_COUNT 60
 #define FN_MAX  30
@@ -3889,7 +3890,6 @@ static void communicationManagement(void *conn)
     {
 #if (CHIP_TYPE != CHIP_T113s)
         _globalCellularManager.mode = reqInfo._4g_wifi_info.Cellular_mode;
-        CellularWIFI_paras_save();
         if (_globalCellularManager.mode)
         {
             Cellular_init();
@@ -3898,6 +3898,9 @@ static void communicationManagement(void *conn)
         {
             thread_stop(THREAD_ID_Cellular);
         }
+        __globalDeviceManage._globalDevInfo.cell = _globalCellularManager.mode;
+        paras_save();
+
          ack_str = ctrl_sts_ack_to_json(); 
 #endif
     }
@@ -3913,7 +3916,10 @@ static void communicationManagement(void *conn)
         _globalWIFIManager.mode = reqInfo._4g_wifi_info.WIFI_mode;
          strcpy(_globalWIFIManager.WIFI_name, reqInfo._4g_wifi_info.WIFI_name);
          strcpy(_globalWIFIManager.WIFI_code, reqInfo._4g_wifi_info.WIFI_code);
-         CellularWIFI_paras_save();
+
+         wifi_set(&_globalWIFIManager);
+         __globalDeviceManage._globalDevInfo.wifi = _globalWIFIManager;
+         paras_save();
          ack_str = ctrl_sts_ack_to_json(); 
 #endif
     }

+ 0 - 28
pro/src/common.c

@@ -957,31 +957,3 @@ void SetMenuAuthority(int RoleID)
     
 }
 
-void InitCellularManager()
-{
-    //_globalCellularManager.mode = 0;
-    _globalCellularManager.Link_status = false; // 4G网络连接状态
-    _globalCellularManager.Signal_strength = 0; // 4G信号强度
-    sprintf(_globalCellularManager.Cellular_name, "-");
-    sprintf(_globalCellularManager.Cellular_Num, "-");
-    //_globalCellularManager.Cellular_IMEI[64]="-"; // IMEI号
-
-   // _globalCellularManager.GPS_status = false;  // GPS功能启用
-    _globalCellularManager.GPS_latitude = 0.0;  // GPS��度
-    _globalCellularManager.GPS_longitude = 0.0; // GPS经度
-    _globalCellularManager.GPS_altitude = 0.0;  // GPS海��高度
-    _globalCellularManager.GPS_speed = 0.0;     // GPS速度,单位km/h
-    _globalCellularManager.GPS_nsat = 0;        // GPS��星数量
-    sprintf(_globalCellularManager.GPS_utc, "-");
-
-    //_globalCellularManager.SMS_status = false; // 短信功能启用
-}
-
-void InitWIFIManager()
-{
-    //_globalWIFIManager.mode = 0;
-    _globalWIFIManager.Link_status = false; // wifi网络连接状态
-    sprintf(_globalWIFIManager.WIFI_ip, "0.0.0.0");
-    //sprintf(_globalWIFIManager.WIFI_name, "-");
-    //sprintf(_globalWIFIManager.WIFI_code, "-");
-}

+ 13 - 12
pro/src/common.h

@@ -401,6 +401,16 @@ typedef struct
     int          brdMax;
 }PowerTotal_t; 
 
+// WIFI信息
+typedef struct
+{
+    int mode;         // WIFI模块启用状态
+    bool Link_status; // wifi网络连接状态
+    char WIFI_ip[64]; // wifi ip address
+    char WIFI_name[64];// WIFI网络SSID名称
+    char WIFI_code[64];// WIFI密码
+} WIFIManager;
+
 typedef struct
 {
     ModbusInfo_t    relay;              //继电器控制端口
@@ -411,6 +421,9 @@ typedef struct
     WebcfgInfo_t    webcfg;
     ServiceInfo_t   service;            //服务信息
     MiscInfo_t      misc;
+
+    int             cell;
+    WIFIManager     wifi;
 }GlobalDeviceInfo;
 ///////////////////////////////////////////////////////////////////////////////////
 //自定义共享内存类
@@ -564,16 +577,6 @@ typedef struct
 
 } CellularManager;
 
-// WIFI信息
-typedef struct
-{
-    int mode;         // WIFI模块启用状态
-    bool Link_status; // wifi网络连接状态
-    char WIFI_ip[64]; // wifi ip address
-    char WIFI_name[64];// WIFI网络SSID名称
-    char WIFI_code[64];// WIFI密码
-} WIFIManager;
-
 //电源部分
 typedef struct 
 {
@@ -1334,6 +1337,4 @@ void add_day_to_date(char *date_str, int *year, int *month, int *day);
 
 void SetMenuAuthority(int RoleID);
 
-void InitCellularManager();
-void InitWIFIManager();
 #endif

+ 6 - 0
pro/src/dflt.c

@@ -85,6 +85,12 @@ GlobalDeviceInfo DFLT_PARAS={
             .rotate = 0,
         #endif
         },
+
+        .cell = 0,
+
+        .wifi = {
+            .mode = 0,
+        },
 };
 
 

+ 76 - 44
pro/src/paras.c

@@ -963,20 +963,62 @@ static int proc_misc(dictionary *dic, int op, MiscInfo_t *misc)
 
     return r;
 }
-
-
-static int proc_CellularWIFIcfg(dictionary *dic, int op, CellularManager *pcellularManager,WIFIManager *pWIFIManager)
+static int proc_cellular(dictionary *dic, int op, int *cell)
 {
     int r=0;
     char temp[100];
     char *p,*token=NULL;
 
     if(op==PARAS_CHECK) {
-        if(pcellularManager->mode<0 || pcellularManager->mode>1) {
-            LOGE("___ check CellularManager, mode is invalid, correct value is 0/1\n");
+        if(*cell<0 || *cell>1) {
+            LOGE("___ check cellular, mode is invalid, correct value is 0/1\n");
             r = -1; 
         }
+        return r;
+    }
+    else if (op==PARAS_CREATE) {
+
+    }
+    else if (op==PARAS_WRITE) {
+        r = proc_cellular(dic, PARAS_CHECK, cell);
+        if(r) {
+            return -1;
+        }
+    }
+
+    token = "cellular:mode";
+    if(op==PARAS_READ) {
+        r = iniparser_getint(dic, token, -1);
+        if(r==-1) {
+            LOGE("___ iniparser_getint %s failed\n", token);
+            return -1;
+        } else {
+            *cell = r;
+        }
+    }
+    else {
+        sprintf(temp, "%d",  *cell);
+        r = iniparser_set(dic, token, temp);
+        if(r) {
+            LOGE("___ iniparser_set %s failed\n", token);
+            return -1;
+        }
+    }
+
+    r = 0;
+    if(op==PARAS_READ) {
+        r = proc_cellular(dic, PARAS_CHECK, cell);
+    }
+
+    return r;
+}
+static int proc_wifi(dictionary *dic, int op, WIFIManager *pWIFIManager)
+{
+    int r=0;
+    char temp[100];
+    char *p,*token=NULL;
 
+    if(op==PARAS_CHECK) {
         if (pWIFIManager->mode < 0 || pWIFIManager->mode > 1)
         {
             LOGE("___ check WIFIManager, mode is invalid, correct value is 0/1\n");
@@ -988,25 +1030,25 @@ static int proc_CellularWIFIcfg(dictionary *dic, int op, CellularManager *pcellu
 
     }
     else if (op==PARAS_WRITE) {
-        r = proc_CellularWIFIcfg(dic, PARAS_CHECK, pcellularManager,pWIFIManager);
+        r = proc_wifi(dic, PARAS_CHECK, pWIFIManager);
         if(r) {
             return -1;
         }
 
     }
 
-    token = "CellularWIFIcfg:Cellular";
+    token = "wifi:mode";
     if(op==PARAS_READ) {
         r = iniparser_getint(dic, token, -1);
         if(r==-1) {
             LOGE("___ iniparser_getint %s failed\n", token);
             return -1;
         } else {
-            pcellularManager->mode = r;
+            pWIFIManager->mode = r;
         }
     }
     else {
-        sprintf(temp, "%d",  pcellularManager->mode);
+        sprintf(temp, "%d", pWIFIManager->mode);
         r = iniparser_set(dic, token, temp);
         if(r) {
             LOGE("___ iniparser_set %s failed\n", token);
@@ -1014,26 +1056,25 @@ static int proc_CellularWIFIcfg(dictionary *dic, int op, CellularManager *pcellu
         }
     }
 
-    token = "CellularWIFIcfg:WIFI";
+    token = "wifi:ip";
     if(op==PARAS_READ) {
-        r = iniparser_getint(dic, token, -1);
-        if(r==-1) {
-            LOGE("___ iniparser_getint %s failed\n", token);
+        p = (char*)iniparser_getstring(dic, token, NULL);
+        if(!p) {
+            LOGE("___ iniparser_getstring %s failed\n", token);
             return -1;
         } else {
-            pWIFIManager->mode = r;
+            strcpy(pWIFIManager->WIFI_ip, p);
         }
     }
     else {
-        sprintf(temp, "%d", pWIFIManager->mode);
-        r = iniparser_set(dic, token, temp);
+        r = iniparser_set(dic, token, pWIFIManager->WIFI_ip);
         if(r) {
             LOGE("___ iniparser_set %s failed\n", token);
             return -1;
         }
     }
 
-    token = "CellularWIFIcfg:WIFI_ssid";
+    token = "wifi:ssid";
     if(op==PARAS_READ) {
         p = (char*)iniparser_getstring(dic, token, NULL);
         if(!p) {
@@ -1051,7 +1092,7 @@ static int proc_CellularWIFIcfg(dictionary *dic, int op, CellularManager *pcellu
         }
     }
 
-    token = "CellularWIFIcfg:WIFI_pw";
+    token = "wifi:password";
     if(op==PARAS_READ) {
         p = (char*)iniparser_getstring(dic, token, NULL);
         if(!p) {
@@ -1071,12 +1112,11 @@ static int proc_CellularWIFIcfg(dictionary *dic, int op, CellularManager *pcellu
 
     r = 0;
     if(op==PARAS_READ) {
-        r = proc_CellularWIFIcfg(dic, PARAS_CHECK, pcellularManager,pWIFIManager);
+        r = proc_wifi(dic, PARAS_CHECK, pWIFIManager);
     }
 
     return r;
 }
-
 static int proc_ntp(char *path, int op, NTPManager *ntp)
 {
     int r=0;
@@ -3430,9 +3470,14 @@ static dictionary* ini_load(char *path, GlobalDeviceInfo *info)
         LOGE("___proc_webcfg failed\n");
     }
 
-    r = proc_CellularWIFIcfg(dic, PARAS_READ, &_globalCellularManager,&_globalWIFIManager);
+    r = proc_cellular(dic, PARAS_READ, &info->cell);
     if(r) {
-        LOGE("___proc_CellularWIFI failed\n");
+        LOGE("___proc_cellular failed\n");
+    }
+
+    r = proc_wifi(dic, PARAS_READ, &info->wifi);
+    if(r) {
+        LOGE("___proc_wifi failed\n");
     }
 
     r = proc_misc(dic, PARAS_READ, &info->misc);
@@ -3509,8 +3554,16 @@ static int ini_reset(paras_handle_t *h)
     fprintf(fp, "beep        = \"%d\"\n", info->misc.beep);
     fprintf(fp, "rotate      = \"%d\"\n", info->misc.rotate);
 
-    fprintf(fp, "\n");
+    fprintf(fp, "[cell]\n");
+    fprintf(fp, "mode        = \"%d\"\n", info->cell);
+    
+    fprintf(fp, "[wifi]\n");
+    fprintf(fp, "mode        = \"%d\"\n", info->wifi.mode);
+    fprintf(fp, "ip          = \"%s\"\n", info->wifi.WIFI_ip);
+    fprintf(fp, "ssid        = \"%s\"\n", info->wifi.WIFI_name);
+    fprintf(fp, "password    = \"%s\"\n", info->wifi.WIFI_code);
 
+    fprintf(fp, "\n");
     fclose(fp);
 
     sys_get_path(tmp, INI_FILE_BAK_NAME);
@@ -3656,28 +3709,7 @@ int paras_save(void)
     
     return r;
 }
-int CellularWIFI_paras_save(void)
-{
-    int r = 0;
-    paras_handle_t *h = &paraHandle;
-
-    r = proc_CellularWIFIcfg(h->dic, PARAS_WRITE, &_globalCellularManager, &_globalWIFIManager);
-    if (r)
-    {
-        LOGE("___ini_w_dev failed\n");
-    }
 
-    FILE *fp = fopen(h->path, "w");
-    if (!fp)
-    {
-        LOGE("___%s open failed\n", h->path);
-        return -1;
-    }
-    iniparser_dump_ini(h->dic, fp);
-    fclose(fp);
-
-    return r;
-}
 
 int paras_serv_xport(char *path, int io)
 {

+ 19 - 0
pro/src/sys.c

@@ -21,6 +21,7 @@
 #include "common.h"
 #include "sqlite_handle.h"
 #include "language_hashmap.h"
+#include "wifi.h"
 #if (CHIP_TYPE != CHIP_T113s)
 #include <sys/sysinfo.h>
 #endif
@@ -59,8 +60,19 @@ static void nic_restart(void)
         system("ifdown eth0;ifup eth0;sleep 2");
     }
 }
+static int is_mac_fixed(void)
+{
+    const char *path="/usr/bin/docker";
+    if(access(path,0)==0) { //docker存在则是老版镜像,每次重启mac不固定, 为随机值
+        return 0;
+    }
+    return 1;
+}
 static void nic_set_mac(void)
 {
+#if(CHIP_TYPE == CHIP_T113i)
+    //if(is_mac_fixed()) return;     //如果是t113i新版镜像,mac地址跟id绑定,不需要写固定值进去
+#endif
     system("ip link set dev eth0 address `cat /sys/class/addr_mgt/addr_eth`");
 }
 
@@ -1540,6 +1552,12 @@ int sys_chmod(void)
 }
 
 
+int sys_get_ip_ver(char *addr)
+{
+    return ip_check(addr);
+}
+
+
 static int net_check(void)
 {
     int r,pid;
@@ -1670,6 +1688,7 @@ int sys_init(void)
     init_hash_table();
     
     net_init();
+
 #if (CHIP_TYPE != CHIP_T113s)
     ui_init();
 #endif

+ 1 - 0
pro/src/sys.h

@@ -34,5 +34,6 @@ int sys_ip_check(char *ip);
 int sys_get_prod(GlobalDeviceInfo *info);
 int sys_kill(char *proc);
 int sys_restart_app(void);
+int sys_get_ip_ver(char *addr);
 
 #endif

+ 390 - 276
pro/src/wifi.c

@@ -7,22 +7,39 @@
 #include <netlink/attr.h>
 #include <linux/nl80211.h>
 #include <net/if.h>
+#include <unistd.h>
+#include <sys/ioctl.h>
 #include <asm/types.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <errno.h>
+#include "common.h"
 #include "lock.h"
+#include "sys.h"
 #include "wifi.h"
+#include "paras.h"
+#include "common.h"
 
+#define USE_WIFI
+#define USE_LIBNL
 
 #define ONCE_CNT            20
 #define WLAN_IFNAME         "wlan0"
+#define AP_CONF             "/etc/hostapd.conf"
+#define STA_CONF            "/etc/wpa_supplicant.conf"
+#define AP_IP               "192.168.2.132"
+
+enum {
+    EVT_ERR=0,
+    EVT_ACK,
+    EVT_FIN,
+};
 
 typedef struct {
-    char      *ssid;
+    int      evt;
     ap_list_t list;
-}ap_res_t;
+}result_t;
 typedef struct {
     int             ifindex;
     char            *ifname;
@@ -32,9 +49,105 @@ typedef struct {
     struct nl_sock  *sock;
 }nl_handle_t;
 typedef struct {
-    int flag;
+    int             flag;
 }wifi_handle_t;
 static wifi_handle_t wfHandle={0};
+const char *auth_str[AUTH_MAX]={
+    "NONE",
+    "ERR",
+    "PSK",
+    "EAP",
+};
+const char *crypt_str[CRYPT_MAX]={
+    "NONE",
+    "ERR",
+    "WEP",
+    "WPA",
+    "WPA2",
+};
+
+static void res_print(result_t *res)
+{
+    int i;
+    ap_info_t *info;
+    if(res && res->list.cnt>0) {
+        for(i=0; i<res->list.cnt; i++) {
+            info = &res->list.info[i];
+            printf("___[%d].ssid: %s\n", i, info->ssid);
+            printf("___[%d].bssid: %s\n", i, info->bssid);
+            printf("___[%d].freq: %dMHz\n", i, info->freq);
+            printf("___[%d].intensity: %d\n", i, info->intensity);
+            printf("___[%d].auth: %s\n", i, auth_str[info->auth]);
+            printf("___[%d].crypt: %s\n", i, crypt_str[info->crypt]);
+            printf("\n");
+        }
+    }
+}
+static void res_free(result_t *res)
+{
+    if(res) {
+        if(res->list.info) {
+            free(res->list.info);
+        }
+    }
+}
+static int sta_start(ap_info_t *info, int on)
+{
+    int r,ipv,mask=24;
+    char tmp[400],txt[200];
+    
+    //https://www.cnblogs.com/lidabo/p/5076560.html
+    r = system("killall wpa_supplicant");
+    if(on) {
+        snprintf(txt, sizeof(txt), "ctrl_interface=/var/run/wpa_supplicant\n"
+                               "network={\n"
+                               "ssid=\"%s\"\n"
+                               "psk=\"%s\"\n"
+                               "}\n", info->ssid, info->password);
+
+        sprintf(tmp, "echo '%s' > %s", txt, STA_CONF);
+        r = system(tmp);
+
+        sprintf(tmp, "wpa_supplicant -B -i %s -c %s", WLAN_IFNAME, STA_CONF);
+        r = system(tmp);
+
+        ipv = sys_get_ip_ver(info->ipaddr);
+        if(ipv==IP_V6) {
+            mask = 64;
+        }
+        sprintf(tmp, "ip addr add %s/%d dev %s", info->ipaddr, mask, WLAN_IFNAME);
+        r = system(tmp);
+
+        sprintf(tmp, "udhcpc -b -i %s", WLAN_IFNAME);
+        r = system(tmp);
+    }
+    else {
+        r = system("wpa_cli disconnect");
+        r = system("killall wpa_supplicant udhcpc");
+    }
+
+    return r;
+}
+static int ap_start(ap_info_t *info, int on)
+{
+    int r;
+    char tmp[100];
+
+    //https://blog.csdn.net/qq_40311939/article/details/139552431
+    r = system("killall hostapd udhcpd");
+    if(on) {
+        sprintf(tmp, "hostapd -B %s", AP_CONF);
+        r = system(tmp);
+
+        //ipv = sys_get_ip_ver(info->ipaddr);
+        sprintf(tmp, "ip addr add %s/24 dev %s", info->ipaddr, WLAN_IFNAME);
+        r = system(tmp);
+
+        sprintf(tmp, "udhcpd -i %s -SC /etc/udhcpd.conf", WLAN_IFNAME);
+        r = system(tmp);
+    }
+    return r;
+}
 
 #ifdef USE_WIFI
 static void priv_parse(uint8_t *ie, size_t ie_len, ap_info_t *info)
@@ -48,18 +161,17 @@ static void priv_parse(uint8_t *ie, size_t ie_len, ap_info_t *info)
         uint8_t len = pos[1];
 
         if (pos + 2 + len > ie + ie_len) {
-            info->auth  = AUTH_WRONG;
-            info->crypt = CRYPT_WRONG;
+            info->auth  = AUTH_ERR;
+            info->crypt = CRYPT_ERR;
             break;  // ���� IE
         }
 
         // ���� RSN (WPA2) IE
         if (id == 48) {  // RSN IE ID
-            uint16_t *auth_suites = (uint16_t *)(pos + 12);  // RSN ��֤�׼�ƫ��
+            uint16_t auth = *((uint16_t *)(pos + 12));  // RSN ��֤�׼�ƫ��
             
-            printf("Encryption: WPA2\n");
             info->crypt = CRYPT_WPA2;
-            switch (*auth_suites) {
+            switch (auth) {
                 case 0x0001:
                     info->auth  = AUTH_PSK;
                     break;
@@ -67,17 +179,16 @@ static void priv_parse(uint8_t *ie, size_t ie_len, ap_info_t *info)
                     info->auth  = AUTH_EAP;
                     break;
                 default:
-                    info->auth  = AUTH_WRONG;
+                    info->auth  = AUTH_ERR;
                     break;
             }
             break;
         }
         else if (id == 221 && len >= 4 && pos[2] == 0x00 && pos[3] == 0x50 && pos[4] == 0xF2 && pos[5] == 0x01) {
-            uint16_t *auth_suites = (uint16_t *)(pos + 12);  // WPA ��֤�׼�ƫ��
+            uint16_t auth = *((uint16_t *)(pos + 12));  // WPA ��֤�׼�ƫ��
             
-            printf("Encryption: WPA\n");
             info->crypt = CRYPT_WPA;
-            switch (*auth_suites) {
+            switch (auth) {
                 case 0x0001:
                     info->auth  = AUTH_PSK;
                     break;
@@ -85,7 +196,7 @@ static void priv_parse(uint8_t *ie, size_t ie_len, ap_info_t *info)
                     info->auth  = AUTH_EAP;
                     break;
                 default:
-                    info->auth  = AUTH_WRONG;
+                    info->auth  = AUTH_ERR;
                     break;
             }
             break;
@@ -99,97 +210,200 @@ static void priv_parse(uint8_t *ie, size_t ie_len, ap_info_t *info)
         pos += 2 + len;
     }
 }
-static int nl_callback(struct nl_msg *msg, void *arg)
+
+
+struct trigger_results {
+    int done;
+    int aborted;
+};
+static int error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err, void *arg)
 {
-    int flag=0;
-    struct nlattr *tb[NL80211_ATTR_MAX + 1];
+    // Callback for errors.
+    printf("error_handler() called.\n");
+    int *ret = arg;
+    *ret = err->error;
+    return NL_STOP;
+}
+static int finish_handler(struct nl_msg *msg, void *arg)
+{
+    // Callback for NL_CB_FINISH.
+    int *ret = arg;
+    *ret = 0;
+    return NL_SKIP;
+}
+static int ack_handler(struct nl_msg *msg, void *arg)
+{
+    // Callback for NL_CB_ACK.
+    int *ret = arg;
+    *ret = 0;
+    return NL_STOP;
+}
+static int no_seq_check(struct nl_msg *msg, void *arg)
+{
+    // Callback for NL_CB_SEQ_CHECK.
+    return NL_OK;
+}
+static int callback_trigger(struct nl_msg *msg, void *arg)
+{
+    // Called by the kernel when the scan is done or has been aborted.
+    struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
+    struct trigger_results *results = arg;
+
+    //printf("Got something.\n");
+    //printf("%d\n", arg);
+    //nl_msg_dump(msg, stdout);
+
+    if (gnlh->cmd == NL80211_CMD_SCAN_ABORTED) {
+        printf("Got NL80211_CMD_SCAN_ABORTED.\n");
+        results->done = 1;
+        results->aborted = 1;
+    } else if (gnlh->cmd == NL80211_CMD_NEW_SCAN_RESULTS) {
+        printf("Got NL80211_CMD_NEW_SCAN_RESULTS.\n");
+        results->done = 1;
+        results->aborted = 0;
+    }  // else probably an uninteresting multicast message.
+
+    return NL_SKIP;
+}
+static int callback_dump(struct nl_msg *msg, void *arg)
+{
+    // Called by the kernel with a dump of the successful scan's data. Called for each SSID.
     struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
+    char mac_addr[20];
+    struct nlattr *tb[NL80211_ATTR_MAX + 1];
     struct nlattr *bss[NL80211_BSS_MAX + 1];
-    struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = {
-        [NL80211_BSS_BSSID] = { .type = NLA_UNSPEC },
-        [NL80211_BSS_INFORMATION_ELEMENTS] = { .type = NLA_UNSPEC },
+    static struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = {
+        [NL80211_BSS_TSF] = { .type = NLA_U64 },
         [NL80211_BSS_FREQUENCY] = { .type = NLA_U32 },
+        [NL80211_BSS_BSSID] = { },
+        [NL80211_BSS_BEACON_INTERVAL] = { .type = NLA_U16 },
+        [NL80211_BSS_CAPABILITY] = { .type = NLA_U16 },
+        [NL80211_BSS_INFORMATION_ELEMENTS] = { },
         [NL80211_BSS_SIGNAL_MBM] = { .type = NLA_U32 },
+        [NL80211_BSS_SIGNAL_UNSPEC] = { .type = NLA_U8 },
+        [NL80211_BSS_STATUS] = { .type = NLA_U32 },
+        [NL80211_BSS_SEEN_MS_AGO] = { .type = NLA_U32 },
+        [NL80211_BSS_BEACON_IES] = { },
     };
-    ap_res_t *res=(ap_res_t*)arg;
+    result_t *res=(result_t*)arg;
     ap_info_t *info=&res->list.info[res->list.cnt];
-    
-    if(res->ssid) {
-        info = &res->list.info[0];
-    }
 
-    // ������Ϣ����
+    // Parse and error check.
     nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), genlmsg_attrlen(gnlh, 0), NULL);
+    if (!tb[NL80211_ATTR_BSS]) {
+        printf("bss info missing!\n");
+        return NL_SKIP;
+    }
+    if (nla_parse_nested(bss, NL80211_BSS_MAX, tb[NL80211_ATTR_BSS], bss_policy)) {
+        printf("failed to parse nested attributes!\n");
+        return NL_SKIP;
+    }
 
-    if (tb[NL80211_ATTR_BSS]) {
-        // ����Ƕ�׵� BSS ����
-        nla_parse_nested(bss, NL80211_BSS_MAX, tb[NL80211_ATTR_BSS], bss_policy);
-
-        // ��ӡ BSSID
-        if (bss[NL80211_BSS_BSSID]) {
-            uint8_t *bssid = nla_data(bss[NL80211_BSS_BSSID]);
-            sprintf(info->bssid, "%02x:%02x:%02x:%02x:%02x:%02x", bssid[0], bssid[1], bssid[2], bssid[3], bssid[4], bssid[5]);
-        }
+    if (bss[NL80211_BSS_BSSID]) {
+        uint8_t *bssid = nla_data(bss[NL80211_BSS_BSSID]);
+        sprintf(info->bssid, "%02x:%02x:%02x:%02x:%02x:%02x", bssid[0], bssid[1], bssid[2], bssid[3], bssid[4], bssid[5]);
+    }
 
-        // ��ӡ�ź�ǿ��
-        if (bss[NL80211_BSS_SIGNAL_MBM]) {
-            int32_t signal = nla_get_u32(bss[NL80211_BSS_SIGNAL_MBM]);
-            info->intensity = (signal/100);
-        }
+    if (bss[NL80211_BSS_SIGNAL_MBM]) {
+        int32_t signal = nla_get_u32(bss[NL80211_BSS_SIGNAL_MBM]);
+        info->intensity = (signal/100);
+    }
 
-        // ��ӡƵ��
-        if (bss[NL80211_BSS_FREQUENCY]) {
-            uint32_t freq = nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
-            info->freq = freq;
-        }
-        
-        // ������ϢԪ�أ�IEs���Ի�ȡ SSID
-        if (bss[NL80211_BSS_INFORMATION_ELEMENTS]) {
-            uint8_t *ies = nla_data(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
-            int ies_len = nla_len(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
-
-             priv_parse(ies, nla_len(bss[NL80211_BSS_INFORMATION_ELEMENTS]), info);
-             
-            // ���� SSID����ϢԪ�� ID Ϊ 0��
-            int offset = 0;
-            while (offset < ies_len) {
-                uint8_t ie_id = ies[offset];
-                uint8_t ie_len = ies[offset + 1];
-
-                if (ie_id == 0 && ie_len > 0 && ie_len <= 32) { // SSID �� ID Ϊ 0
-                    memcpy(info->ssid, &ies[offset + 2], ie_len);
-                    info->ssid[ie_len] = 0;
-                    if(res->ssid && strcmp(res->ssid, info->ssid)==0) {
-                        flag = 1;
-                        fprintf(stderr, "%s find\n", res->ssid);
-                    }
-                    break;
-                }
-                offset += ie_len + 2; // �ƶ�����һ����ϢԪ��
+    if (bss[NL80211_BSS_FREQUENCY]) {
+        uint32_t freq = nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
+        info->freq = freq;
+    }
+    
+    if (bss[NL80211_BSS_INFORMATION_ELEMENTS]) {
+        uint8_t *ies = nla_data(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
+        int ies_len = nla_len(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
+
+         priv_parse(ies, nla_len(bss[NL80211_BSS_INFORMATION_ELEMENTS]), info);
+         
+        // ���� SSID����ϢԪ�� ID Ϊ 0��
+        int offset = 0;
+        while (offset < ies_len) {
+            uint8_t ie_id = ies[offset];
+            uint8_t ie_len = ies[offset + 1];
+
+            if (ie_id == 0 && ie_len > 0 && ie_len <= 32) { // SSID �� ID Ϊ 0
+                memcpy(info->ssid, &ies[offset + 2], ie_len);
+                info->ssid[ie_len] = 0;
+                break;
             }
+            offset += ie_len + 2;
         }
+    }
 
-        res->list.cnt++;
-        if(res->list.cnt>=res->list.max) {
-            int max = res->list.max+ONCE_CNT;
-            res->list.info = (ap_info_t*)realloc(res->list.info, sizeof(ap_info_t)*max);
-            if(res->list.info) {
-                res->list.max = max;
-            }
-            else {
-                fprintf(stderr, "res->info realloc failed\n");
-                return NL_STOP;
-            }
+    res->list.cnt++;
+    if(res->list.cnt>=res->list.max) {
+        int max = res->list.max+ONCE_CNT;
+        res->list.info = (ap_info_t*)realloc(res->list.info, sizeof(ap_info_t)*max);
+        if(res->list.info) {
+            res->list.max = max;
         }
-        
-        if(flag) {
+        else {
+            fprintf(stderr, "res->info realloc failed\n");
             return NL_STOP;
         }
     }
 
     return NL_SKIP;
 }
-static int nl_init(nl_handle_t *h)
+static int scan_trigger(nl_handle_t *h)
+{
+    // Starts the scan and waits for it to finish. Does not return until the scan is done or has been aborted.
+    struct trigger_results results = { .done = 0, .aborted = 0 };
+    struct nl_msg *ssids_to_scan;
+    int err,ret;
+    int mcid = genl_ctrl_resolve_grp(h->sock, "nl80211", "scan");
+    nl_socket_add_membership(h->sock, mcid);  // Without this, callback_trigger() won't be called.
+
+    // Allocate the messages and callback handler.
+    ssids_to_scan = nlmsg_alloc();
+    if (!ssids_to_scan) {
+        printf("ERROR: Failed to allocate netlink message for ssids_to_scan.\n");
+        return -ENOMEM;
+    }
+
+    // Setup the messages and callback handler.
+    genlmsg_put(h->msg, 0, 0, h->nl_id, 0, 0, NL80211_CMD_TRIGGER_SCAN, 0);  // Setup which command to run.
+    nla_put_u32(h->msg, NL80211_ATTR_IFINDEX, h->ifindex);  // Add message attribute, which interface to use.
+    nla_put(ssids_to_scan, 1, 0, "");  // Scan all SSIDs.
+    nla_put_nested(h->msg, NL80211_ATTR_SCAN_SSIDS, ssids_to_scan);  // Add message attribute, which SSIDs to scan for.
+    nlmsg_free(ssids_to_scan);  // Copied to `msg` above, no longer need this.
+    nl_cb_set(h->cb, NL_CB_VALID, NL_CB_CUSTOM, callback_trigger, &results);  // Add the callback.
+    nl_cb_err(h->cb, NL_CB_CUSTOM, error_handler, &err);
+    nl_cb_set(h->cb, NL_CB_FINISH, NL_CB_CUSTOM, finish_handler, &err);
+    nl_cb_set(h->cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, &err);
+    nl_cb_set(h->cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, no_seq_check, NULL);  // No sequence checking for multicast messages.
+
+    // Send NL80211_CMD_TRIGGER_SCAN to start the scan. The kernel may reply with NL80211_CMD_NEW_SCAN_RESULTS on
+    // success or NL80211_CMD_SCAN_ABORTED if another scan was started by another process.
+    err = 1;
+    ret = nl_send_auto(h->sock, h->msg);  // Send the message.
+    printf("NL80211_CMD_TRIGGER_SCAN sent %d bytes to the kernel.\n", ret);
+    printf("Waiting for scan to complete...\n");
+    while (err > 0) ret = nl_recvmsgs(h->sock, h->cb);  // First wait for ack_handler(). This helps with basic errors.
+    if (err < 0) {
+        printf("WARNING: err has a value of %d.\n", err);
+    }
+    if (ret < 0) {
+        printf("ERROR: nl_recvmsgs() returned %d (%s).\n", ret, nl_geterror(-ret));
+        return ret;
+    }
+    while (!results.done) nl_recvmsgs(h->sock, h->cb);  // Now wait until the scan is done or aborted.
+    if (results.aborted) {
+        printf("ERROR: Kernel aborted scan.\n");
+        return 1;
+    }
+    printf("Scan is done.\n");
+
+    // Cleanup.
+    nl_socket_drop_membership(h->sock, mcid);  // No longer need this.
+    return 0;
+}
+static int nl80211_init(nl_handle_t *h)
 {
     h->ifname = WLAN_IFNAME;
     h->ifindex = if_nametoindex(h->ifname);
@@ -197,7 +411,6 @@ static int nl_init(nl_handle_t *h)
         fprintf(stderr, "Failed to get interface index\n");
         return -1;
     }
-    h->nl_id = genl_ctrl_resolve(h->sock, "nl80211");
     
     h->cb = nl_cb_alloc(NL_CB_DEFAULT);
     if (!h->cb) {
@@ -221,12 +434,14 @@ static int nl_init(nl_handle_t *h)
         fprintf(stderr, "Failed to allocate netlink message\n");
         goto fail;
     }
+    h->nl_id = genl_ctrl_resolve(h->sock, "nl80211");
+
     return 0;
     
 fail:
     return -1;
 }
-static int nl_free(nl_handle_t *h)
+static int nl80211_free(nl_handle_t *h)
 {
     if(h->cb)   nl_cb_put(h->cb);
     if(h->msg)  nlmsg_free(h->msg);
@@ -234,12 +449,13 @@ static int nl_free(nl_handle_t *h)
     
     return 0;
 }
-static int nl_scan(ap_res_t *res)
+
+static int nl_scan(result_t *res)
 {
     int r;
     nl_handle_t nl,*h;
     
-    r = nl_init(&nl);
+    r = nl80211_init(&nl);
     if(r) {
         return -1;
     }
@@ -249,63 +465,61 @@ static int nl_scan(ap_res_t *res)
     res->list.max = 0;
     res->list.info = (ap_info_t*)malloc(sizeof(ap_info_t)*ONCE_CNT);
     if(!res->list.info) {
-        nl_free(h);
+        nl80211_free(h);
         return -1;
     }
     res->list.max = ONCE_CNT;
-
-    nl_cb_set(h->cb, NL_CB_VALID, NL_CB_CUSTOM, nl_callback, NULL);
-    genlmsg_put(h->msg, 0, 0, h->nl_id, 0, NLM_F_DUMP, NL80211_CMD_GET_SCAN, 0);
-    nla_put_u32(h->msg, NL80211_ATTR_IFINDEX, if_nametoindex(WLAN_IFNAME)); // �滻Ϊ������߽ӿ�����
-
-    // ������Ϣ
-    if (nl_send_auto(h->sock, h->msg) < 0) {
-        fprintf(stderr, "Failed to send message.\n");
-        r = -1;
-    }
-    else {
-        nl_recvmsgs(h->sock, h->cb);
+    
+    r = scan_trigger(h);
+    if (r != 0) {
+        printf("scan_trigger() failed with %d.\n", r);
+        return -1;
     }
 
-    nl_free(h);
+    genlmsg_put(h->msg, 0, 0, h->nl_id, 0, NLM_F_DUMP, NL80211_CMD_GET_SCAN, 0);  // Setup which command to run.
+    nla_put_u32(h->msg, NL80211_ATTR_IFINDEX, h->ifindex);  // Add message attribute, which interface to use.
+    nl_socket_modify_cb(h->sock, NL_CB_VALID, NL_CB_CUSTOM, callback_dump, res);  // Add the callback.
+    r = nl_send_auto(h->sock, h->msg);
+    r = nl_recvmsgs_default(h->sock);
+    if (r < 0) {
+        printf("ERROR: nl_recvmsgs_default() returned %d (%s).\n", r, nl_geterror(-r));
+    }
+    nl80211_free(h);
 
     return r;
 }
-static int nl_conn(ap_info_t *info, char *password)
+static int nl_conn(ap_info_t *info)
 {
     int err,r=0;
     nl_handle_t nl,*h;
     
-    r = nl_init(&nl);
+    r = nl80211_init(&nl);
     if(r) {
         return r;
     }
     h = &nl;
     
-    genlmsg_put(h->msg, 0, 0, h->nl_id, 0, 0, NL80211_CMD_CONNECT, 0);
+    genlmsg_put(h->msg, 0, 0, h->nl_id, 0, (NLM_F_REQUEST|NLM_F_ACK), NL80211_CMD_CONNECT, 0);
     nla_put_u32(h->msg, NL80211_ATTR_IFINDEX, h->ifindex);
     nla_put(h->msg, NL80211_ATTR_SSID, strlen(info->ssid), info->ssid);
 
-    // ����ṩ�� BSSID��������
     if (strlen(info->bssid)>0) {
         nla_put(h->msg, NL80211_ATTR_MAC, 6, info->bssid);
     }
 
-    // ����ṩ�� PSK�����룩��������
-    if (password) {
+    if (strlen(info->password)>0) {
         struct nlattr *sae_data = nla_nest_start(h->msg, NL80211_ATTR_AUTH_DATA);
         if (!sae_data) {
             fprintf(stderr, "Failed to nest auth data\n");
             r = -1;
         }
         nla_put(h->msg, NL80211_ATTR_AKM_SUITES, 2, "\x00\x0F");  // WPA2-PSK
-        nla_put(h->msg, NL80211_ATTR_IE, strlen(password), password);
+        nla_put(h->msg, NL80211_ATTR_IE, strlen(info->password), info->password);
         nla_nest_end(h->msg, sae_data);
     }
 
-    // ������Ϣ
-    if (r==0 && nl_send_auto(h->sock, h->msg) < 0) {
-        fprintf(stderr, "Failed to send connect request\n");
+    if (nl_send_auto_complete(h->sock, h->msg) < 0) {
+        fprintf(stderr, "nl_send_auto_complete failed\n");
         r = -1;
     }
 
@@ -313,7 +527,7 @@ static int nl_conn(ap_info_t *info, char *password)
         fprintf(stderr, "Failed to receive ack: %s\n", nl_geterror(err));
         r = -1;
     }
-    nl_free(h);
+    nl80211_free(h);
     
     return r;
 }
@@ -322,7 +536,7 @@ static int nl_disconn(void)
     int err,r;
     nl_handle_t nl,*h;
     
-    r = nl_init(&nl);
+    r = nl80211_init(&nl);
     if(r) {
         return r;
     }
@@ -342,167 +556,61 @@ static int nl_disconn(void)
         fprintf(stderr, "Failed to receive ack: %s\n", nl_geterror(err));
         r = -1;
     }
-    nl_free(h);
+    nl80211_free(h);
     
     return r;
 }
 static int nl_set_link(int on)
 {
-    struct rtnl_link *link;
-    struct nl_cache *cache;
-    struct nl_sock  *sock;
-    int err,r=0;
-
-#if 0
-    sock = nl_socket_alloc();
-    if(!sock) {
-        fprintf(stderr, "Failed to alloc sock.\n");
-        r = -1;
-    }
-    
-    // ���ӵ� netlink
-    if ((err = nl_connect(sock, NETLINK_ROUTE)) < 0) {
-        fprintf(stderr, "Failed to connect to netlink: %s\n", nl_geterror(err));
-        r = -1;
-        goto fail;
-    }
-
-    // ��ȡ��·����
-    if ((err = rtnl_link_alloc_cache(sock, AF_UNSPEC, &cache)) < 0) {
-        fprintf(stderr, "Failed to allocate link cache: %s\n", nl_geterror(err));
-        r = -1;
-        goto fail;
-    }
-    
-    link = rtnl_link_get_by_name(cache, WLAN_IFNAME);
-    if (!link) {
-        fprintf(stderr, "Failed to find link: %s\n", WLAN_IFNAME);
-        r = -1;
-        goto fail;
-    }
-
-    if(r==0) {
-        rtnl_link_set_flags(link, on ? IFF_UP : 0, IFF_UP);
-        if ((err=rtnl_link_change(sock, link, link, 0)) < 0) {
-            fprintf(stderr, "Failed to change link state: %s\n", nl_geterror(err));
-            r = -1;
+    int r,fd;
+    struct ifreq ifr;
+
+    fd = socket(AF_INET, SOCK_DGRAM, 0); 
+    if (fd>=0) { 
+        strcpy(ifr.ifr_name, WLAN_IFNAME); 
+        r = ioctl(fd, SIOCGIFFLAGS, &ifr);
+        if(r>=0) {
+            if(on) {
+                ifr.ifr_flags |= IFF_UP;
+            }
+            else {
+                ifr.ifr_flags &= ~IFF_UP;
+            }
+            r = ioctl(fd, SIOCSIFFLAGS, &ifr);
         }
+        close(fd);
+        r = (r>=0)?0:-1;
     }
 
-fail:
-    // �ͷ���Դ
-    if(link) {
-        rtnl_link_put(link);
-        nl_close(sock);
-    }
-    if(cache) nl_cache_free(cache);
-    if(sock) nl_socket_free(sock);
-#endif
-
-    return r;
-}
-static int nl_set_mode(int mode)
-{
-    int nl_mode=(mode==MODE_AP)?NL80211_IFTYPE_AP:NL80211_IFTYPE_STATION;
-    nl_handle_t nl,*h;
-    int err,r=0;
-    
-    r = nl_init(&nl);
-    if(r) {
-        return -1;
-    }
-    h = &nl;
-
-    // ��������ģʽ����
-    genlmsg_put(h->msg, 0, 0, h->nl_id, 0, 0, NL80211_CMD_SET_INTERFACE, 0);
-    nla_put_u32(h->msg, NL80211_ATTR_IFINDEX, h->ifindex);
-    nla_put_u32(h->msg, NL80211_ATTR_IFTYPE, nl_mode);
-
-    // ������Ϣ
-    if (nl_send_auto(h->sock, h->msg) < 0) {
-        fprintf(stderr, "Failed to send set mode request\n");
-        r = -1;
-    }
-    
-    if (r==0 && (err=nl_wait_for_ack(h->sock)) < 0) {
-        fprintf(stderr, "Failed to receive ack: %s\n", nl_geterror(err));
-        r = -1;
-    }
-    nl_free(h);
-
     return r;
 }
-static int nl_set_ap(const char *ssid, const char *password)
-{
-    nl_handle_t nl,*h;
-    int err,r;
-    
-    r = nl_init(&nl);
-    if(r) {
-        return -1;
-    }
-    h = &nl;
-
-    // ������Ϣ
-    genlmsg_put(h->msg, 0, 0, genl_ctrl_resolve(h->sock, "nl80211"), 0, 0, NL80211_CMD_NEW_INTERFACE, 0);
-
-    // ���ýӿ�����
-    nla_put_u32(h->msg, NL80211_ATTR_IFINDEX, h->ifindex);
-
-    // ���� SSID
-    nla_put(h->msg, NL80211_ATTR_SSID, strlen(ssid), ssid);
-
-    // �������루WPA2-PSK��
-    if (password) {
-        nla_put_u32(h->msg, NL80211_ATTR_KEY_TYPE, NL80211_KEYTYPE_PAIRWISE);
-        nla_put(h->msg, NL80211_ATTR_KEY_DATA, strlen(password), password);
-        nla_put_u8(h->msg, NL80211_ATTR_KEY_IDX, 0);
-        nla_put_u32(h->msg, NL80211_ATTR_KEY_CIPHER, 0x000FAC04); // WPA2 cipher suite
-    }
-
-    if ((err=nl_send_auto(h->sock, h->msg)) < 0) {
-        fprintf(stderr, "Failed to send netlink message: %s\n", nl_geterror(err));
-        r = -1;
-    }
-    nl_free(h);
-
-    return r;
-}
-static void res_free(ap_res_t *res)
-{
-    if(res) {
-        if(res->list.info) {
-            free(res->list.info);
-        }
-    }
-}
 #endif
 
 ////////////////////////////////////////////////////////////////
-
 int wifi_init(void)
 {
-    int r;
+    int r=-1;
+    WIFIManager *wm=&_globalWIFIManager;
     wifi_handle_t *h=&wfHandle;
- 
+
+    *wm = __globalDeviceManage._globalDevInfo.wifi;
 #ifdef USE_WIFI
-    r = nl_set_link(1);
-    nl_set_mode(MODE_STA);
+    {
+        r = wifi_set(wm);
+    }
 #endif
     
     return r;
 }
 
 
-
-int wifi_sta_scan(ap_list_t *list)
+int wifi_scan(ap_list_t *list)
 {
     int r=-1;
-    ap_res_t res;
+    result_t res;
     wifi_handle_t *h=&wfHandle;
 
 #ifdef USE_WIFI
-    res.ssid = NULL;
     r = nl_scan(&res);
     if(r==0) {
         if(list) {
@@ -515,55 +623,52 @@ int wifi_sta_scan(ap_list_t *list)
 }
 
 
-int wifi_sta_scan_ssid(char *ssid, ap_info_t *info)
-{
-    int r=-1;
-    ap_res_t res;
-    wifi_handle_t *h=&wfHandle;
-    
-#ifdef USE_WIFI
-    res.ssid = ssid;
-    r = nl_scan(&res);
-    if(r==0) {
-        if(info && res.list.cnt==1) {
-            *info = res.list.info[0];
-        }
-        res_free(&res);
-    }
-#endif
-
-    return r;
-}
-
-
-int wifi_sta_conn(ap_info_t *info, char *password)
+int wifi_set(WIFIManager *wm)
 {
     int r;
+    ap_info_t info;
     wifi_handle_t *h=&wfHandle;
 
 #ifdef USE_WIFI
-    r = nl_conn(info, password);
+    r = nl_set_link(wm->mode);
+    if(wm->mode) {
+        memset(&info, 0, sizeof(info));
+
+    #if 0
+        strcpy(info.ssid, wm->WIFI_name);
+        strcpy(info.password, wm->WIFI_code);
+        //strcpy(info.ipaddr, wm->WIFI_ip);
+    #else
+        strcpy(info.ssid, "ChinaNet-Gowone");
+        strcpy(info.password, "gowone520");
+        //strcpy(info.ipaddr, "");
+    #endif
+        r = sta_start(&info, 1);
+    }  
+    else {
+        sta_start(&info, 0);
+    } 
 #endif
 
     return r;
 }
 
 
-int wifi_sta_disconn(void)
+
+int wifi_sta_info(ap_info_t *info)
 {
     int r=-1;
     wifi_handle_t *h=&wfHandle;
-
+    
 #ifdef USE_WIFI
-    r = nl_disconn();
-#endif
 
-    return r;
+#endif
+    
+    return 0;
 }
 
 
-
-int wifi_ap_set(char *ssid, char *password)
+int wifi_ap_info(ap_info_t *info)
 {
     int r=-1;
     wifi_handle_t *h=&wfHandle;
@@ -576,4 +681,13 @@ int wifi_ap_set(char *ssid, char *password)
 }
 
 
-
+int wifi_list_free(ap_list_t *list)
+{
+    if(!list || !list->info) {
+        return -1;
+    }
+    free(list->info);
+    list->cnt = 0;
+    list->info = NULL;
+    return 0;
+}

+ 11 - 8
pro/src/wifi.h

@@ -2,21 +2,23 @@
 #define __WIFI_Hx__
 
 #include <stdint.h>
+#include "common.h"
 
 enum {
     CRYPT_NONE=0,
+    CRYPT_ERR,
     CRYPT_WEP,
     CRYPT_WPA,
     CRYPT_WPA2,
-    CRYPT_WRONG,
+    CRYPT_WPA3,
     
     CRYPT_MAX,
 };
 enum {
     AUTH_NONE=0,
+    AUTH_ERR,
     AUTH_PSK,
     AUTH_EAP,
-    AUTH_WRONG,
     
     AUTH_MAX,
 };
@@ -31,6 +33,8 @@ enum {
 typedef struct {
     char ssid[40];      //���32���ַ�
     char bssid[16];     //���12���ַ�
+    char password[40];
+    char ipaddr[64];
     int  auth;
     int  crypt;
     int  intensity;     //dB
@@ -45,12 +49,11 @@ typedef struct {
 
 
 int wifi_init(void);
+int wifi_scan(ap_list_t *list);
+int wifi_list_free(ap_list_t *list);
 
-int wifi_sta_scan(ap_list_t *list);
-int wifi_sta_scan_ssid(char *ssid, ap_info_t *info);
-int wifi_sta_conn(ap_info_t *info, char *password);
-int wifi_sta_disconn(void);
-
-int wifi_ap_set(char *ssid, char *password);
+int wifi_set(WIFIManager *wm);
+int wifi_sta_info(ap_info_t *info);
+int wifi_ap_info(ap_info_t *info);
 
 #endif