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

1、添加获取ip接口
2、cfg.h中添加USE_CELL和USE_WIFI宏,启用则使用相关模块

guohui пре 1 година
родитељ
комит
b82413e422
6 измењених фајлова са 172 додато и 42 уклоњено
  1. 4 0
      pro/src/Cellular.c
  2. 13 9
      pro/src/appweb_handle.c
  3. 19 0
      pro/src/common.c
  4. 1 0
      pro/src/json_handle.c
  5. 128 32
      pro/src/wifi.c
  6. 7 1
      pro/src/wifi.h

+ 4 - 0
pro/src/Cellular.c

@@ -1,4 +1,5 @@
 #include <time.h>
+#include "cfg.h"
 #include "common.h"
 #include "Cellular.h"
 #include <pthread.h>
@@ -459,6 +460,7 @@ int Cellular_init(void)
 {
     CellularManager *pcell=&_globalCellularManager;
 
+#ifdef USE_CELL
     memset(pcell, 0, sizeof(CellularManager));
     pcell->mode = __globalDeviceManage._globalDevInfo.cell;
     if (0==thread_is_running(THREAD_ID_Cellular))
@@ -466,6 +468,8 @@ int Cellular_init(void)
         
         thread_start(THREAD_ID_Cellular, Cellular_thread, &_globalCellularManager);
     }
+#endif
+
     return 0;
 }
 

+ 13 - 9
pro/src/appweb_handle.c

@@ -3880,15 +3880,13 @@ static void communicationManagement(void *conn)
 
         ack_str = ctrl_sts_ack_to_json();
     }
+#ifdef USE_CELL
     else if (strcmp(reqInfo.type, "listfourgPZQuery") == 0) // 4G网络处理
     {
-#if (CHIP_TYPE != CHIP_T113s)
         ack_str = communication_4G_ack_to_json(&_globalCellularManager,0);
-#endif
     }
     else if (strcmp(reqInfo.type, "fourgPZSave") == 0) // 4G网络处理
     {
-#if (CHIP_TYPE != CHIP_T113s)
         _globalCellularManager.mode = reqInfo._4g_wifi_info.Cellular_mode;
         if (_globalCellularManager.mode)
         {
@@ -3901,18 +3899,23 @@ static void communicationManagement(void *conn)
         __globalDeviceManage._globalDevInfo.cell = _globalCellularManager.mode;
         paras_save();
 
-         ack_str = ctrl_sts_ack_to_json(); 
-#endif
+        ack_str = ctrl_sts_ack_to_json(); 
     }
+#endif
+
+#ifdef USE_WIFI
     else if(strcmp(reqInfo.type,"listWIFIPZQuery")==0) // WIFI处理
     {
-#if (CHIP_TYPE != CHIP_T113s)
+        ap_stat_t stat;
+        int r = wifi_sta_stat(&stat);
+        if(r==0) {
+            _globalWIFIManager.Link_status = stat.link;
+            strcpy(_globalWIFIManager.WIFI_ip, stat.ipaddr);
+        }
         ack_str = communication_WIFI_ack_to_json(&_globalWIFIManager,0);
-#endif
     }
      else if (strcmp(reqInfo.type, "WIFIPZSave") == 0) // WIFI处理
     {
-#if (CHIP_TYPE != CHIP_T113s)
         _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);
@@ -3921,8 +3924,9 @@ static void communicationManagement(void *conn)
          __globalDeviceManage._globalDevInfo.wifi = _globalWIFIManager;
          paras_save();
          ack_str = ctrl_sts_ack_to_json(); 
-#endif
     }
+#endif
+
     cJSON_Delete(cjson);
     http_reply(conn, 200, ack_str);
     cJSON_free((void*)ack_str); 

+ 19 - 0
pro/src/common.c

@@ -810,8 +810,18 @@ void SetMenuAuthority(int RoleID)
         // 6、通信管理菜单
         _GlobalMenuAuthority.WkszEnabled = true;
         _GlobalMenuAuthority.RsszEnabled = true;
+#ifdef USE_CELL
+        _GlobalMenuAuthority.CellularEnabled = true;
+#else
         _GlobalMenuAuthority.CellularEnabled = false;
+#endif
+
+#ifdef USE_WIFI
+        _GlobalMenuAuthority.WifiEnabled = true;
+#else
         _GlobalMenuAuthority.WifiEnabled = false;
+#endif
+
         // 7、服务管理菜单
         _GlobalMenuAuthority.SmtpszEnabled = true;
         _GlobalMenuAuthority.SnmpszEnabled = true;
@@ -872,8 +882,17 @@ void SetMenuAuthority(int RoleID)
         // 6、通信管理菜单
         _GlobalMenuAuthority.WkszEnabled = false;
         _GlobalMenuAuthority.RsszEnabled = true;
+#ifdef USE_CELL
+        _GlobalMenuAuthority.CellularEnabled = true;
+#else
         _GlobalMenuAuthority.CellularEnabled = false;
+#endif
+
+#ifdef USE_WIFI
+        _GlobalMenuAuthority.WifiEnabled = true;
+#else
         _GlobalMenuAuthority.WifiEnabled = false;
+#endif
         // 7、服务管理菜单
         _GlobalMenuAuthority.SmtpszEnabled = false;
         _GlobalMenuAuthority.SnmpszEnabled = false;

+ 1 - 0
pro/src/json_handle.c

@@ -4711,6 +4711,7 @@ char* communication_WIFI_ack_to_json(WIFIManager* _WIFIManagertemp,int nWSType)
 {
     char temp[32] = {0};
     char* str = NULL;
+
     // 创建 JSON 对象
     cJSON* root = cJSON_CreateObject();   
     //创建数据

+ 128 - 32
pro/src/wifi.c

@@ -1,11 +1,15 @@
 #include <linux/netlink.h>
 #include <linux/rtnetlink.h>
 #include <netlink/netlink.h>
+#include <netlink/cache.h>
 #include <netlink/genl/genl.h>
 #include <netlink/genl/ctrl.h>
+#include <netlink/route/link.h>
+#include <netlink/route/addr.h>
 #include <netlink/msg.h>
 #include <netlink/attr.h>
 #include <linux/nl80211.h>
+#include <ifaddrs.h>
 #include <net/if.h>
 #include <unistd.h>
 #include <sys/ioctl.h>
@@ -21,8 +25,7 @@
 #include "paras.h"
 #include "common.h"
 
-#define USE_WIFI
-#define USE_LIBNL
+//#define USE_WIFI
 
 #define ONCE_CNT            20
 #define WLAN_IFNAME         "wlan0"
@@ -76,7 +79,6 @@ static void res_print(result_t *res)
             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");
@@ -111,13 +113,6 @@ static int sta_start(ap_info_t *info, int on)
         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);
     }
@@ -130,7 +125,7 @@ static int sta_start(ap_info_t *info, int on)
 }
 static int ap_start(ap_info_t *info, int on)
 {
-    int r;
+    int r,ipv,mask=24;
     char tmp[100];
 
     //https://blog.csdn.net/qq_40311939/article/details/139552431
@@ -139,8 +134,11 @@ static int ap_start(ap_info_t *info, int 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);
+        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, "udhcpd -i %s -SC /etc/udhcpd.conf", WLAN_IFNAME);
@@ -243,6 +241,37 @@ static int no_seq_check(struct nl_msg *msg, void *arg)
     // Callback for NL_CB_SEQ_CHECK.
     return NL_OK;
 }
+static int callback_stat(struct nl_msg *msg, void *arg)
+{
+    struct nlattr *tb[NL80211_ATTR_MAX + 1];
+    struct nlattr *bss[NL80211_BSS_MAX + 1];
+    struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1];
+    struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
+    struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
+        [NL80211_STA_INFO_SIGNAL] = { .type = NLA_U8 }, // 信号强度
+        [NL80211_STA_INFO_CONNECTED_TIME] = { .type = NLA_U32 }, // 连接时间
+    };
+    ap_stat_t *stat=(ap_stat_t*)arg;
+
+    nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
+              genlmsg_attrlen(gnlh, 0), NULL);
+
+    if (tb[NL80211_ATTR_STA_INFO]) {
+        stat->link = 1;
+        nla_parse(sinfo, NL80211_STA_INFO_MAX, nla_data(tb[NL80211_ATTR_STA_INFO]),
+                  nla_len(tb[NL80211_ATTR_STA_INFO]),stats_policy);
+        if (sinfo[NL80211_STA_INFO_SIGNAL]) {
+            stat->signal = nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL]);
+        }
+        else {
+            stat->signal = 0;
+        }
+    } else {
+        stat->link = 0;
+    }
+
+    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.
@@ -269,7 +298,6 @@ 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];
     static struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = {
@@ -304,11 +332,6 @@ static int callback_dump(struct nl_msg *msg, void *arg)
         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_FREQUENCY]) {
         uint32_t freq = nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
         info->freq = freq;
@@ -429,7 +452,7 @@ static int nl80211_init(nl_handle_t *h)
         goto fail;
     }
     
-    h->msg  = nlmsg_alloc();
+    h->msg = nlmsg_alloc();
     if (!h->msg) {
         fprintf(stderr, "Failed to allocate netlink message\n");
         goto fail;
@@ -449,7 +472,74 @@ static int nl80211_free(nl_handle_t *h)
     
     return 0;
 }
+static int get_ipaddr(char *ifname, char *ipaddr)
+{
+    int r=-1;
+    struct ifaddrs *ifap, *ifa;
 
+    if (getifaddrs(&ifap) == -1) {
+        printf("___getifaddrs() error\n");
+        return -1;
+    }
+
+    for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) {
+        if (ifa->ifa_addr != NULL && strcmp(ifa->ifa_name, ifname)==0) {
+            if(ifa->ifa_addr->sa_family==AF_INET) {
+                struct sockaddr_in *sa,*msk;
+                sa = (struct sockaddr_in *)  ifa->ifa_addr;
+                msk = (struct sockaddr_in *) ifa->ifa_netmask;
+
+                char *p = (char*)inet_ntop(ifa->ifa_addr->sa_family, &sa->sin_addr, ipaddr, INET6_ADDRSTRLEN);
+                if(p) r = 0;
+                if(r==0) break;
+            }
+            else {
+                struct sockaddr_in6 *sa,*msk;
+                sa = (struct sockaddr_in6 *)  ifa->ifa_addr;
+                msk = (struct sockaddr_in6 *) ifa->ifa_netmask;
+                
+                char *p = (char*)inet_ntop(ifa->ifa_addr->sa_family, &sa->sin6_addr, ipaddr, INET6_ADDRSTRLEN);
+                if(p) r = 0;
+                if(r==0) break;
+            }
+        }
+    }
+    freeifaddrs(ifap);
+    
+    return r;
+}
+static int nl_get_ip(ap_stat_t *stat)
+{
+    int r;
+
+    stat->ipaddr[0] = 0;
+    r = get_ipaddr(WLAN_IFNAME, stat->ipaddr);
+
+    return r;
+}
+static int nl_get_signal(ap_stat_t *stat)
+{
+    int r;
+    nl_handle_t nl,*h;
+    
+    r = nl80211_init(&nl);
+    if(r) {
+        return -1;
+    }
+    h = &nl;
+
+    stat->link = stat->signal = 0;
+    genlmsg_put(h->msg, 0, 0, h->nl_id, 0, NLM_F_DUMP, NL80211_CMD_GET_STATION, 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_stat, stat);
+    r = nl_send_auto(h->sock, h->msg);
+    if(r>=0) {
+        nl_recvmsgs_default(h->sock);
+        r = 0;
+    }
+    nl80211_free(h);
+    return r;
+}
 static int nl_scan(result_t *res)
 {
     int r;
@@ -595,9 +685,7 @@ int wifi_init(void)
 
     *wm = __globalDeviceManage._globalDevInfo.wifi;
 #ifdef USE_WIFI
-    {
-        r = wifi_set(wm);
-    }
+    r = wifi_set(wm);
 #endif
     
     return r;
@@ -625,7 +713,7 @@ int wifi_scan(ap_list_t *list)
 
 int wifi_set(WIFIManager *wm)
 {
-    int r;
+    int r=-1;
     ap_info_t info;
     wifi_handle_t *h=&wfHandle;
 
@@ -634,15 +722,8 @@ int wifi_set(WIFIManager *wm)
     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 {
@@ -661,13 +742,28 @@ int wifi_sta_info(ap_info_t *info)
     wifi_handle_t *h=&wfHandle;
     
 #ifdef USE_WIFI
-
+    
 #endif
     
     return 0;
 }
 
 
+int wifi_sta_stat(ap_stat_t *stat)
+{
+    int r=-1;
+    wifi_handle_t *h=&wfHandle;
+    
+#ifdef USE_WIFI
+    r = nl_get_signal(stat);
+    r *= nl_get_ip(stat);
+#endif
+    
+    return r;
+}
+
+
+
 int wifi_ap_info(ap_info_t *info)
 {
     int r=-1;

+ 7 - 1
pro/src/wifi.h

@@ -37,10 +37,15 @@ typedef struct {
     char ipaddr[64];
     int  auth;
     int  crypt;
-    int  intensity;     //dB
     uint32_t freq;      //MHz
 }ap_info_t;
 
+typedef struct {
+    int  link;
+    int  signal;     //dB
+    char ipaddr[64];
+}ap_stat_t;
+
 typedef struct {
     int       cnt;
     int       max;
@@ -53,6 +58,7 @@ int wifi_scan(ap_list_t *list);
 int wifi_list_free(ap_list_t *list);
 
 int wifi_set(WIFIManager *wm);
+int wifi_sta_stat(ap_stat_t *stat);
 int wifi_sta_info(ap_info_t *info);
 int wifi_ap_info(ap_info_t *info);