Parcourir la source

1、修改mqtt连接不上时,关闭服务器导致程序崩溃的bug;
2、修改4G启用配置方式及连接不上时CPU占用过高的bug

liyuezong il y a 1 an
Parent
commit
f2c550a09b
10 fichiers modifiés avec 153 ajouts et 100 suppressions
  1. 50 30
      cust_data/smartPDU.ini
  2. 0 1
      pro/config/cfg.h
  3. 61 49
      pro/src/Cellular.c
  4. 1 2
      pro/src/appweb_handle.c
  5. 9 10
      pro/src/common.c
  6. 2 1
      pro/src/common.h
  7. 1 0
      pro/src/dflt.c
  8. 1 1
      pro/src/mqtt.c
  9. 20 0
      pro/src/paras.c
  10. 8 6
      pro/src/websocket_handle.c

+ 50 - 30
cust_data/smartPDU.ini

@@ -1,40 +1,60 @@
+
 [product]
-id          = "0"
-number      = "X00001"
-name        = "Smart PDU"
-type        = "1"
-pwrtype     = "5"
-language    = "0"
-samptime    = "30"
-savetime    = "60000"
-ph_group    = "8"
-ch_delay    = "8"
+id                             = "0"
+number                         = "X00001"
+name                           = "Smart PDU"
+type                           = "0"
+pwrtype                        = "0"
+language                       = "0"
+samptime                       = "30"
+savetime                       = "60000"
+ph_group                       = "8"
+ch_delay                       = "8"
+
 
 [modbus]
-mode        = "1"
-address     = "1"
-baudrate    = "115200"
+mode                           = "1"
+address                        = "1"
+baudrate                       = "115200"
+
 
 [ptotal]
-voltage     = "500"
-current     = "100"
-power       = "100"
-brd_max     = "24"
+voltage                        = "500"
+current                        = "100"
+power                          = "100"
+brd_max                        = "24"
+
 
 [webcfg]
-monway      = "2"
-netsw       = "0"
-nwire       = "0"
-ipv4        = "1"
-ipv6        = "1"
-
-[CellularWIFIcfg]
-Cellular = "0"
-WIFI = "0"
-WIFI_ssid= "-"
-WIFI_pw= "-"
+monway                         = "2"
+netsw                          = "0"
+nwire                          = "0"
+ipv4                           = "1"
+ipv6                           = "1"
+brdupg                         = "1"
+
 
 [misc]
-watchdog    = "0"
-fliptime    = "5"
+watchdog                       = "0"
+fliptime                       = "0"
+led                            = "0"
+beep                           = "0"
+rotate                         = "0"
+
+
+[cellular]
+enable                         = "1"
+mode                           = "1"
+apn                            = "-"
+username                       = ""
+password                       = ""
+
+
+[wifi]
+enable                         = "1"
+mode                           = "0"
+ip                             = ""
+ssid                           = ""
+password                       = ""
+
 

+ 0 - 1
pro/config/cfg.h

@@ -47,7 +47,6 @@
 #define USE_MQTT
 #define MQTT_SERVER_BUILTIN
 
-#define USE_CELL
 //#define USE_WIFI
 //#define GNSS_ENABLE               //EC200N-CN, ²»Ö§³Ögnss
 

+ 61 - 49
pro/src/Cellular.c

@@ -453,9 +453,6 @@ static void conn_init(cell_handle_t *h, mqtt_conn_t *conn);
 
 
 
-
-
-#ifdef USE_CELL
 static int port_setup(cell_handle_t *h, int speed);
 
 static void msleep(int ms)
@@ -588,39 +585,52 @@ static int send_cmd(cell_handle_t *h, int cmd, char *para, char *rcv, int rcvlen
     sprintf(buf, "%s%s\r", pcmd->cmd, para?para:"");
 
     LOGD("[tx]: %s\n", buf);
-    for(i=0; i<RETRY_TIMES; i++) {
+    for (i = 0; i < RETRY_TIMES; i++)
+    {
         tout = 0;
         port_write(h, buf, strlen(buf));
-        while(tout<pcmd->timeout) {
+        while (tout < pcmd->timeout)
+        {
             l = port_wait(h, wait_step);
-            if(l>0) {
-                if(rcv && rcvlen>0) {
+            if (l > 0)
+            {
+                if (rcv && rcvlen > 0)
+                {
                     pbuf = rcv;
                     buflen = rcvlen;
                 }
-                else {
+                else
+                {
                     pbuf = buf;
                     buflen = sizeof(buf);
                 }
 
-                l = port_read(h, pbuf+tlen, buflen);
-                if(l>0) {
-                    tlen += l; pbuf[tlen] = 0;
+                l = port_read(h, pbuf + tlen, buflen);
+                if (l > 0)
+                {
+                    tlen += l;
+                    pbuf[tlen] = 0;
 
                     LOGD("[rx]: %s\n", pbuf);
-                    if(pcmd->ok && strlen(pcmd->ok) && strstr(pbuf, pcmd->ok)) {
+                    if (pcmd->ok && strlen(pcmd->ok) && strstr(pbuf, pcmd->ok))
+                    {
                         return AT_OK;
                     }
-                    
-                    if(pcmd->err && strlen(pcmd->err) && strstr(pbuf, pcmd->err)) {
+
+                    if (pcmd->err && strlen(pcmd->err) && strstr(pbuf, pcmd->err))
+                    {
                         return AT_ERR;
                     }
                 }
             }
             tout += wait_step;
+            usleep(1000);
         }
     }
-    cell_reset(h);
+    if (cmd != AT_CMD_CFUN )
+    {
+        cell_reset(h);
+    }
 
     return AT_TOUT;
 }
@@ -654,11 +664,12 @@ static int send_data(cell_handle_t *h, void *data, int len, int timeout)
 static int cell_reset(cell_handle_t *h)
 {
     int r;
-
+    reset_var(h);
+     h->stat = AT_STAT_UNINIT;
     r = send_cmd(h, AT_CMD_CFUN, "=1,1", NULL, 0);
-    if(r==AT_OK) {
-        h->stat = AT_STAT_UNINIT;
-    }
+   // if(r==AT_OK) {
+      //  h->stat = AT_STAT_UNINIT;
+    //}
     return (r==AT_OK)?0:-1;
 }
 static int cell_init(cell_handle_t *h)
@@ -697,11 +708,14 @@ static int cell_init(cell_handle_t *h)
 }
 static int cell_deinit(cell_handle_t *h)
 {
-    if(h->stat==AT_STAT_UNINIT) {
-        return -1;
+    int r=-1;
+    char buf[400];
+    r = send_cmd(h, AT_CMD_QIDEACT, NULL, buf, sizeof(buf));
+    if(r==AT_OK) {
+        return r;
     }
 
-    return 0;
+    return r;
 }
 static int cell_get_info(cell_handle_t *h, cell_info_t *info)
 {
@@ -802,7 +816,7 @@ static int cell_get_ping(cell_handle_t *h, cell_stat_t *stat)
         return -1;
     }
 
-    r = send_cmd(h, AT_CMD_QPING, "=1,\"163.com\",3,2,", buf, sizeof(buf));
+    r = send_cmd(h, AT_CMD_QPING, "=1,\"www.baidu.com\",3,2,", buf, sizeof(buf));
     if(r!=AT_OK) {
         return -1;
     }
@@ -811,6 +825,8 @@ static int cell_get_ping(cell_handle_t *h, cell_stat_t *stat)
     if(r<1) {
         return -1;
     }
+
+     LOGD("QPING: %d\n", ret);
     if(ret==0) {
         stat->linked = 1;
     }
@@ -1286,6 +1302,7 @@ static void* cellular_thread(void *arg)
         }
         else
         {
+            //if(cnt%5==0) cell_deinit(h);
             reset_var(h);
         }
         UpdateCellularManager(&h->all);
@@ -1296,17 +1313,13 @@ static void* cellular_thread(void *arg)
 
     pthread_exit(NULL);
 }
-#endif
+
 
 int cellular_reset(void)
 {
     int r=-1;
     cell_handle_t *h=&ceHandle;
-
-#ifdef USE_CELL
     r = cell_reset(h);
-#endif
-
     return r;
 }
 
@@ -1317,17 +1330,18 @@ int cellular_init(void)
     int r;
     cell_handle_t *h=&ceHandle;
 
-#ifdef USE_CELL
-    memset(h, 0, sizeof(cell_handle_t));
-    
-    h->dm = &__globalDeviceManage;
-    h->pcell = &h->dm->_globalDevInfo.cell;
-    h->prod_id = h->dm->_globalDevInfo.product.id;
-    h->lck = lock_d_init();
-    nic_set(NIC_USB, 1);
+    if (__globalDeviceManage._globalDevInfo.cell.Cellular_enable)
+    {
+        memset(h, 0, sizeof(cell_handle_t));
 
-    thread_start(THREAD_ID_CELLULAR, cellular_thread, h);
-#endif
+        h->dm = &__globalDeviceManage;
+        h->pcell = &h->dm->_globalDevInfo.cell;
+        h->prod_id = h->dm->_globalDevInfo.product.id;
+        h->lck = lock_d_init();
+        nic_set(NIC_USB, 1);
+
+        thread_start(THREAD_ID_CELLULAR, cellular_thread, h);
+    }
 
     return 0;
 }
@@ -1336,10 +1350,11 @@ int cellular_deinit(void)
 {
     cell_handle_t *h=&ceHandle;
 
-#ifdef USE_CELL
-    lock_d_deinit(h->lck);
-    thread_stop(THREAD_ID_CELLULAR);
-#endif
+    if (__globalDeviceManage._globalDevInfo.cell.Cellular_enable)
+    {
+        lock_d_deinit(h->lck);
+        thread_stop(THREAD_ID_CELLULAR);
+    }
 
     return 0;
 }
@@ -1350,12 +1365,10 @@ int cellular_get_all(cell_all_t *all)
     int r=-1;
     cell_handle_t *h=&ceHandle;
 
-#ifdef USE_CELL
     if(all) {
         *all = h->all;
         r = 0;
     }
-#endif
 
     return r;
 }
@@ -1366,9 +1379,10 @@ int cellular_is_connected(void)
     int r=0;
     cell_handle_t *h=&ceHandle;
 
-#ifdef USE_CELL
-    r = h->all.stat.linked;
-#endif
+    if (__globalDeviceManage._globalDevInfo.cell.Cellular_enable)
+    {
+        r = h->all.stat.linked;
+    }
 
     return r;
 }
@@ -1392,9 +1406,7 @@ int cellular_test(void)
 {
     cell_handle_t *h=&ceHandle;
 
-#ifdef USE_CELL
     cell_test(h);
-#endif
 
     return 0;
 }

+ 1 - 2
pro/src/appweb_handle.c

@@ -3887,7 +3887,7 @@ static void communicationManagement(void *conn)
 
         ack_str = ctrl_sts_ack_to_json();
     }
-#ifdef USE_CELL
+
     else if (strcmp(reqInfo.type, "listfourgPZQuery") == 0) // 4G网络处理
     {
         ack_str = communication_4G_ack_to_json(& __globalDeviceManage._globalDevInfo.cell,0);
@@ -3899,7 +3899,6 @@ static void communicationManagement(void *conn)
 
         ack_str = ctrl_sts_ack_to_json(); 
     }
-#endif
 
 #ifdef USE_WIFI
     else if(strcmp(reqInfo.type,"listWIFIPZQuery")==0) // WIFI处理

+ 9 - 10
pro/src/common.c

@@ -809,11 +809,11 @@ void SetMenuAuthority(int RoleID)
         // 6、通信管理菜单
         _GlobalMenuAuthority.WkszEnabled = true;
         _GlobalMenuAuthority.RsszEnabled = true;
-#ifdef USE_CELL
-        _GlobalMenuAuthority.CellularEnabled = true;
-#else
-        _GlobalMenuAuthority.CellularEnabled = false;
-#endif
+        if (__globalDeviceManage._globalDevInfo.cell.Cellular_enable)
+
+            _GlobalMenuAuthority.CellularEnabled = true;
+        else
+            _GlobalMenuAuthority.CellularEnabled = false;
 
 #ifdef USE_WIFI
         _GlobalMenuAuthority.WifiEnabled = true;
@@ -881,11 +881,10 @@ void SetMenuAuthority(int RoleID)
         // 6、通信管理菜单
         _GlobalMenuAuthority.WkszEnabled = false;
         _GlobalMenuAuthority.RsszEnabled = true;
-#ifdef USE_CELL
-        _GlobalMenuAuthority.CellularEnabled = true;
-#else
-        _GlobalMenuAuthority.CellularEnabled = false;
-#endif
+        if (__globalDeviceManage._globalDevInfo.cell.Cellular_enable)
+            _GlobalMenuAuthority.CellularEnabled = true;
+        else
+            _GlobalMenuAuthority.CellularEnabled = false;
 
 #ifdef USE_WIFI
         _GlobalMenuAuthority.WifiEnabled = true;

+ 2 - 1
pro/src/common.h

@@ -390,7 +390,8 @@ typedef struct {
 // 4G网络及GPS信息
 typedef struct
 {
-    int mode;         // 4G模块启用状态
+    bool Cellular_enable; // 4G模块启用状态
+    int mode;         // 4G模块默认模式
     bool Link_status; // 4G网络连接状态
     int Signal_strength; // 信号强度
     char Cellular_name[32];// 4G网络运营商名称

+ 1 - 0
pro/src/dflt.c

@@ -113,6 +113,7 @@ GlobalDeviceInfo DFLT_PARAS={
         },
 
         .cell = {
+            .Cellular_enable=false,
             .mode = 0,
             .apn  = "cmnet",
             .username = "",

+ 1 - 1
pro/src/mqtt.c

@@ -541,7 +541,7 @@ static void* client_thread(void *arg)
     while(conn->quit==0) {
         mg_mgr_poll(&conn->mgr, 1000);
     }
-    mg_mqtt_disconnect(conn->c, NULL);
+    if(conn->c)mg_mqtt_disconnect(conn->c, NULL);
     mg_mgr_free(&conn->mgr);
     conn->c = NULL;
 

+ 20 - 0
pro/src/paras.c

@@ -1011,6 +1011,25 @@ static int proc_cellular(dictionary *dic, int op, CellInfo_t *cell)
         }
     }
 
+    token = "cellular:enable";
+    if(op==PARAS_READ) {
+        r = iniparser_getint(dic, token, -1);
+        if(r==-1) {
+            LOGE("___ iniparser_getint %s failed\n", token);
+            return -1;
+        } else {
+            cell->Cellular_enable = r;
+        }
+    }
+    else {
+        //sprintf(temp, "%d",  cell->Cellular_enable);
+        //r = iniparser_set(dic, token, temp);
+        //if(r) {
+           // LOGE("___ iniparser_set %s failed\n", token);
+           // return -1;
+       // }
+    }
+
     token = "cellular:mode";
     if(op==PARAS_READ) {
         r = iniparser_getint(dic, token, -1);
@@ -3636,6 +3655,7 @@ static int ini_reset(paras_handle_t *h)
     fprintf(fp, "\n");
 
     fprintf(fp, "[cellular]\n");
+     fprintf(fp, "enable        = \"%d\"\n", info->cell.Cellular_enable);
     fprintf(fp, "mode        = \"%d\"\n", info->cell.mode);
     fprintf(fp, "apn         = \"%s\"\n", info->cell.apn);
     fprintf(fp, "username    = \"%s\"\n", info->cell.username);

+ 8 - 6
pro/src/websocket_handle.c

@@ -247,13 +247,15 @@ static int ws_Cellular_update(ws_handle_t *h)
 {
     char *json;
 
-#ifdef USE_CELL
-    json = communication_4G_ack_to_json(& __globalDeviceManage._globalDevInfo.cell,1);
-    if(json) {
-        ws_send_data(h, json, strlen(json));
-        cJSON_free(json);
+    if (__globalDeviceManage._globalDevInfo.cell.Cellular_enable)
+    {
+        json = communication_4G_ack_to_json(&__globalDeviceManage._globalDevInfo.cell, 1);
+        if (json)
+        {
+            ws_send_data(h, json, strlen(json));
+            cJSON_free(json);
+        }
     }
-#endif
 
     return 0;
 }