Przeglądaj źródła

1、解网口检测bug

guohui 2 lat temu
rodzic
commit
82d471f703
6 zmienionych plików z 214 dodań i 89 usunięć
  1. 1 1
      output/push/push.bat
  2. 8 9
      pro/config/cfg.h
  3. 70 28
      pro/src/mqtt.c
  4. 13 5
      pro/src/mqtt.h
  5. 64 21
      pro/src/netswitch.c
  6. 58 25
      pro/src/sqlite_handle.c

+ 1 - 1
output/push/push.bat

@@ -19,7 +19,7 @@ SET PWD=123456
 
 ::send file
 ECHO send %TARGET% to device ...
-%PSCP% %OUTDIR%\%TARGET% root@%IP%:%DEVDIR%
+ECHO  y  | %PSCP% %OUTDIR%\%TARGET% root@%IP%:%DEVDIR%
 ECHO send finished!
 
 ECHO release %TARGET% now ...

+ 8 - 9
pro/config/cfg.h

@@ -21,9 +21,9 @@
 
 
 /////////////customer demannd//////////////////////
-#define CUST_GOM037     //xian
+//#define CUST_GOM037     //xian
 //#define CUST_USASL
-//#define CUST_ANDERSON
+#define CUST_ANDERSON
 
 
 #ifdef CUST_GOM037      //xian
@@ -50,10 +50,12 @@
     #define SW_PORT_INDEX_START         0
     #define SW_PORT_INDEX_END           21
 
-    #define USE_STATIC_IP
-    #ifdef USE_STATIC_IP
-        #define SW_HOST_IP              "192.168.8.1"
-        #define SW_PDU_IP               "192.168.8.11"
+    #define SW_STATIC_IP
+    #define SW_STATIC_PORT_START
+
+    #ifdef SW_STATIC_IP
+        #define SW_HOST_IP              "192.168.1.1"
+        #define SW_PDU_IP               "192.168.1.132"
     #endif
     
     #undef MAX_CHN_COUNT
@@ -67,9 +69,6 @@
 //define KEY_PARAS_IN_FILE		//mask it mean store paras in database
 //#define USE_WEBAPP
 
-#define USER_MAX            50
-
-
 
 
 #endif

+ 70 - 28
pro/src/mqtt.c

@@ -13,8 +13,33 @@
     #define LOGW            printf
 #endif
 
+
 #define MQTT_SERVER_URL     "XXXXXXXX"
 
+
+char *topic_sub[]={
+    "/pdu/%d/control/power/all_channel",
+    "/pdu/%d/control/power/%d",
+    "/pdu/%d/control/sensor/%d",
+    "/pdu/%d/control/device/restart",
+    "/pdu/%d/control/device/reset",
+    "/PDU/%d/control/service",
+};
+char *topic_pub[]={
+    "/pdu/%d/info/device",
+    "/PDU/%d/status/network",
+    "/pdu/%d/status/power/all_channel",
+    "/pdu/%d/status/power/%d",
+    "/pdu/%d/status/sensor/%d",
+    "/pdu/%d/alarm/network",
+    "/pdu/%d/alarm/power",
+    "/pdu/%d/alarm/sensor",
+    "/PDU/%d/status/service",
+};
+
+
+
+
 typedef struct mg_mgr mgr_t;
 typedef struct mg_mqtt_opts mg_opts_t;
 typedef struct mg_connection mg_conn_t;
@@ -22,7 +47,7 @@ typedef struct {
     mgr_t         mgr;
     mg_opts_t     opts;
     mg_conn_t     *conn;
-    conn_para_t   para;
+    mqtt_para_t   para;
     
     pthread_mutex_t mutex;
     int           inited;
@@ -31,30 +56,38 @@ typedef struct {
 }mqtt_handle_t;
 static mqtt_handle_t mqHandle;
 
-static void mqtt_fn(struct mg_connection *c, int ev, void *ev_data)
+static void mqtt_fn(mg_conn_t *c, int ev, void *ev_data)
 {
     mqtt_handle_t *h=&mqHandle;
     
     if (ev == MG_EV_OPEN) {
         // c->is_hexdumping = 1;
     } else if (ev == MG_EV_CONNECT) {
-        if (mg_url_is_ssl(h->para.url)) {
+        if (mg_url_is_ssl(h->para.user.url)) {
             struct mg_tls_opts opts = {.ca = mg_unpacked("/certs/ca.pem"),
-                                     .name = mg_url_host(h->para.url)};
+                                       .name = mg_url_host(h->para.user.url)};
             mg_tls_init(c, &opts);
         }
     } else if (ev == MG_EV_ERROR) {
         // On error, log error message
         MG_ERROR(("%p %s", c->fd, (char *) ev_data));
     } else if (ev == MG_EV_MQTT_OPEN) {
-        
+        mg_opts_t opts={
+            .client_id = mg_str(h->para.user.url),
+            .user = mg_str(h->para.user.name),
+            .pass = mg_str(h->para.user.pass),
+        };
+        size_t len=c->send.len;
+
+        mg_mqtt_login(c, &opts);
+        mg_ws_wrap(c, c->send.len - len, WEBSOCKET_OP_BINARY);
     } else if (ev == MG_EV_MQTT_MSG) {
         // When we receive MQTT message, print it
         struct mg_mqtt_message *mm = (struct mg_mqtt_message *) ev_data;
-        MG_INFO(("Received on %.*s : %.*s", (int) mm->topic.len, mm->topic.buf,
-             (int) mm->data.len, mm->data.buf));
-    } else if (ev == MG_EV_POLL && c->data[0] == 'X') {
+        //MG_INFO(("Received on %.*s : %.*s", (int) mm->topic.len, mm->topic.buf, (int) mm->data.len, mm->data.buf));
         
+
+
     }
 
     if (ev == MG_EV_ERROR || ev == MG_EV_CLOSE) {
@@ -65,20 +98,22 @@ static void mqtt_fn(struct mg_connection *c, int ev, void *ev_data)
 static void* mqtt_thread(void *arg)
 {
     int r;
-    thread_handle_t *h=(thread_handle_t*)arg;
-    mqtt_handle_t   *mh=(mqtt_handle_t*)h->arg;
+    thread_handle_t *th=(thread_handle_t*)arg;
+    mqtt_handle_t   *h=(mqtt_handle_t*)th->arg;
     
-    while(h->quit==0) {
-        pthread_mutex_lock(&mh->mutex);
-        if (mh->isover) {
-            mh->conn = mg_mqtt_connect(&mh->mgr, mh->para.url, &mh->opts, mqtt_fn, &mh->isover);
+    while(th->quit==0) {
+        if(h->inited) {
+            pthread_mutex_lock(&h->mutex);
+            if (h->isover) {
+                h->conn = mg_mqtt_connect(&h->mgr, h->para.user.url, &h->opts, mqtt_fn, &h->isover);
+            }
+            else {
+                mg_mgr_poll(&h->mgr, 300);
+            }
+            pthread_mutex_unlock(&h->mutex);
         }
-        else {
-            mg_mgr_poll(&mh->mgr, 300);
-        }
-        pthread_mutex_unlock(&mh->mutex);
         
-        if(mh->isover) usleep(1000);
+        if(h->isover) usleep(1000);
     }
     pthread_exit(NULL);
 }
@@ -91,9 +126,10 @@ int mqtt_init(void)
     memset(h, 9, sizeof(mqtt_handle_t));
     mg_mgr_init(&h->mgr);
     
-    h->para.ver = 4;
-    h->para.qos = 1;
-    strcpy(h->para.url, MQTT_SERVER_URL);
+    //h->para.user = ;
+
+    h->para.conn.ver = 4;
+    h->para.conn.qos = 1;
     
     r = pthread_mutex_init(&h->mutex, NULL);
     if(r) {
@@ -120,11 +156,17 @@ int mqtt_deinit(void)
     return 0;
 }
 
-int mqtt_set(conn_para_t *para)
+
+int mqtt_set(mqtt_para_t *para)
 {
     mqtt_handle_t *h=&mqHandle;
     
-    if(!para || para->ver<3 || para->ver>5) {
+    if(!para) {
+        return -1;
+    }
+
+    //para check
+    if(para->conn.ver<3 || para->conn.ver>5) {
         return -1;
     }
     h->para = *para;
@@ -144,9 +186,9 @@ int mqtt_conn(void)
     
     memset(&h->opts, 0, sizeof(h->opts));
     h->opts.clean = true,
-    h->opts.qos = h->para.qos,
+    h->opts.qos = h->para.conn.qos,
     h->opts.topic = mg_str(""),
-    h->opts.version = h->para.ver,
+    h->opts.version = h->para.conn.ver,
     h->opts.message = mg_str("bye");
     
     if(h->conn) {
@@ -154,7 +196,7 @@ int mqtt_conn(void)
     }
     
     h->isover = false;
-    h->conn = mg_mqtt_connect(&h->mgr, h->para.url, &h->opts, mqtt_fn, &h->isover);
+    h->conn = mg_mqtt_connect(&h->mgr, h->para.user.url, &h->opts, mqtt_fn, &h->isover);
     
 quit:
     pthread_mutex_unlock(&h->mutex);
@@ -221,7 +263,7 @@ int mqtt_pub(char *topic, int qos, char *data, int dlen)
     opts.message.buf = data;
     opts.message.len = dlen;
     opts.retain = false;
-    mg_mqtt_pub(h->conn, &opts);
+    r = mg_mqtt_pub(h->conn, &opts);
     
 quit:
     pthread_mutex_unlock(&h->mutex);

+ 13 - 5
pro/src/mqtt.h

@@ -3,19 +3,27 @@
 
 #include <stdint.h>
 
-
+typedef struct {
+    char  url[1024];        //
+    char  cid[64];          //clientID
+    char  name[64];         //user name
+    char  pass[64];         //password
+}mqtt_user_t;
 typedef struct {
     uint8_t qos;
     uint8_t ver;
     uint8_t clean;
-    char    url[1024];
-}conn_para_t;
-
+    uint8_t retain;
+}mqtt_conn_t;
+typedef struct {
+    mqtt_user_t  user;
+    mqtt_conn_t  conn;
+}mqtt_para_t;
 
 int mqtt_init(void);
 int mqtt_deinit(void);
 
-int mqtt_set(conn_para_t *para);
+int mqtt_set(mqtt_para_t *para);
 int mqtt_conn(void);
 int mqtt_disconn(void);
 

+ 64 - 21
pro/src/netswitch.c

@@ -31,7 +31,7 @@ typedef struct {
     netswitch_info_t sw2;
 
     int              ipflag;
-    char             ipaddr[64];
+    char             host_ip[64];
     int              port_start;         
 }ns_handle_t;
 static ns_handle_t nsHandle;
@@ -424,6 +424,10 @@ static int get_port_start(char *ip)
     int port=-1,val;
     netsnmp_variable_list *var,*pvar;
 
+#ifdef SW_STATIC_PORT_START
+    return SW_PORT_START;
+#endif
+
     char *ifEntry="1.3.6.1.2.1.2.2";
     var = snmp_getbulk(SNMP_VERSION_2c, "public", ip, ifEntry);
     if(var) {
@@ -457,30 +461,56 @@ static int get_port_start(char *ip)
 
     return port;
 }
+static int get_hostnet(char *hnet, char *hip)
+{
+    char *p=NULL;
+    
+    strcpy(hnet, hip);
+    p = strrchr(hnet, '.');
+    if(!p) {
+        p = strrchr(hnet, ':');
+        if(!p) {
+            return -1;
+        }
+    }
+    p[1] = '0'; p[2] = 0;
+
+    return 0;
+}
+static int set_ip(char *ip)
+{
+    char temp[200];
+    sprintf(temp, "ip addr add dev eth0 %s/24", ip);
+    return system(temp);
+}
+static int set_gw(char *gw)
+{
+    char temp[200];
+    sprintf(temp, "route del default; route add default gw %s", gw);
+    return system(temp);
+}
 static int sw_get(netswitch_info_t *sw)
 {
     int i,j,r,find=0;
-    char temp[100];
+    char temp[200];
     netswitch_info_t tmp;
     netsnmp_variable_list *var,*pvar;
     ns_handle_t *h=&nsHandle;
     
     if(h->ipflag==0) {
-#ifdef USE_STATIC_IP
-        strcpy(h->ipaddr, SW_HOST_IP);
-        sprintf(temp, "%ip addr add %s", SW_PDU_IP);
-        r = system(temp);
-        if(r) {
-            LOGE("____set pdu ip failed\n");
-            return -1;
-        }
+#ifdef SW_STATIC_IP
+        char hnet[100];
+        strcpy(h->host_ip, SW_HOST_IP);
+        //get_hostnet(hnet, h->host_ip);
+        //sprintf(temp, "ip addr add dev eth0 %s/24; ip route add %s/24 via %s", SW_PDU_IP, hnet, h->host_ip);
+        //set_ip(SW_PDU_IP); set_gw(h->host_ip);
         h->ipflag = 1;
 #else
-        r = get_ipaddr(AF_INET, "eth0", h->ipaddr);
+        r = get_ipaddr(AF_INET, "eth0", h->host_ip);
         if(r) {
             LOGE("____get ip4 failed\n");
 
-            r = get_ipaddr(AF_INET6, "eth0", h->ipaddr);
+            r = get_ipaddr(AF_INET6, "eth0", h->host_ip);
             if(r) {
                 LOGE("____get ip6 failed\n");
                 return -1;
@@ -489,17 +519,17 @@ static int sw_get(netswitch_info_t *sw)
         h->ipflag = 1;
 #endif
     }
-    //LOGD("___local ip: %s\n", h->ipaddr);
+    //LOGD("___local ip: %s\n", h->host_ip);
 
     if(h->port_start==-1) {
-        h->port_start = get_port_start(h->ipaddr);
+        h->port_start = get_port_start(h->host_ip);
         if(h->port_start==-1) {
             return -1;
         }
     }
 
     char *dot1dTpFdbEntryOid="1.3.6.1.2.1.17.4.3.1";
-    var = snmp_getbulk(SNMP_VERSION_2c, "public", h->ipaddr, dot1dTpFdbEntryOid);
+    var = snmp_getbulk(SNMP_VERSION_2c, "public", h->host_ip, dot1dTpFdbEntryOid);
     if(var) {
         char *tok_mac="Hex-STRING: ", *tok_id="INTEGER: ";
 
@@ -525,6 +555,8 @@ static int sw_get(netswitch_info_t *sw)
                     if(p) {
                         p += strlen(tok_id);
                         tmp.port[j].id = atoi(p) - h->port_start;
+                        //LOGD("____%d___id: %s, port_start: %d, id: %d\n", j, p, h->port_start, tmp.port[j].id); 
+
                         j++;
                     }
                 }
@@ -547,7 +579,7 @@ static int sw_get(netswitch_info_t *sw)
     }
 
     char *ipNetToMediaEntryOid="1.3.6.1.2.1.4.22.1";
-    var = snmp_getbulk(SNMP_VERSION_2c, "public", h->ipaddr, ipNetToMediaEntryOid);
+    var = snmp_getbulk(SNMP_VERSION_2c, "public", h->host_ip, ipNetToMediaEntryOid);
     if(var) {
         char *tok_mac="STRING: ", *tok_ip="IpAddress: ";
 
@@ -595,7 +627,7 @@ static int sw_get(netswitch_info_t *sw)
     char *ifEntry="1.3.6.1.2.1.2.2.1.8";
     for(i=0; i<SW_PORT_MAX; i++) {
         sprintf(temp, "%s.%d", ifEntry, i+h->port_start);
-        var = snmp_get(SNMP_VERSION_2c, "public", h->ipaddr, temp);
+        var = snmp_get(SNMP_VERSION_2c, "public", h->host_ip, temp);
         if(var) {
             int id;
             char *p,*tok_ifo="ifOperStatus.";
@@ -687,6 +719,18 @@ static int get_prod_id(void)
 {
     return __globalDeviceManage._globalDevInfo.product_id;
 }
+static int sw_save(sqlite3 *db, netswitch_info_t *old, netswitch_info_t *new)
+{
+    int i;
+    for(i=0; i<SW_PORT_MAX; i++) {
+        if(memcmp(&old->port[i], &new->port[i], sizeof(netswitch_info_t))) {
+            dev_switch_update_port(db, &new->port[i]);
+            //LOGD("____ i: %d, id: %d\n", i, new->port[i].id);
+        }
+    }
+
+    return 0;
+}
 static int sw_alarm(netswitch_evts_t *evts, netswitch_info_t *old, netswitch_info_t *new)
 {
     int r=-1;
@@ -701,9 +745,6 @@ static int sw_alarm(netswitch_evts_t *evts, netswitch_info_t *old, netswitch_inf
     
     alarm_str[0] = 0;
     for(i=0; i<SW_PORT_MAX; i++) {
-        if(i<SW_PORT_INDEX_START || i>SW_PORT_INDEX_END) {
-            continue;
-        }
         
         temp[0] = 0;
         if(evts->evt[i].cab) {
@@ -725,7 +766,7 @@ static int sw_alarm(netswitch_evts_t *evts, netswitch_info_t *old, netswitch_inf
     }
 
     if(flag) {
-        r = dev_insert_alarm_info(get_db(), get_prod_id(), 1, time_str, alarm_str);
+        r = dev_insert_alarm_info(get_db(), get_prod_id(), 0, time_str, alarm_str);
     }
 
     return r;
@@ -746,6 +787,8 @@ static void* sw_thread(void *arg)
         r = sw_get(&nh->sw2);
         if(r==0 && sw_chk(&nh->sw, &nh->sw2, &evts)) {
             sw_alarm(&evts, &nh->sw, &nh->sw2);
+            sw_save(db, &nh->sw, &nh->sw2);
+
             sw_cpy(&nh->sw, &nh->sw2);
         }
         sleep(1);

+ 58 - 25
pro/src/sqlite_handle.c

@@ -3479,23 +3479,43 @@ static int tab_exists(sqlite3 *db, char *table)
  
     return exists;
 }
+static int tab_sw_create(sqlite3 *db, char *table)
+{
+    int r;
+    char temp[1024] = {0};
+    char *err_msg = NULL;
 
+    sprintf(temp, "CREATE TABLE IF NOT EXISTS %s("
+                        "id INTEGER PRIMARY KEY,"
+                        "up INTEGER,"
+                        "ip TEXT,"
+                        "mac TEXT,"
+                        "desc TEXT);", table);
+    r = sqlite3_exec(db, temp, 0, 0, &err_msg);
+    if(r!=SQLITE_OK) {
+        log_e("___ %s create failed, %s\n%s\n", table, temp, err_msg); sqlite3_free(err_msg);
+        return -1;
+    }
+
+    return 0;
+}
 
 static int sw_load(sqlite3 *db, netswitch_info_t *sw)
 {
-    int i,r;
+    int i=0,r;
     char temp[1024],*p;
     char *table=SWITCH_TABLE;
-    sqlite3_stmt *stmt;
+    sqlite3_stmt *stmt=NULL;
 
     sprintf(temp, "SELECT * FROM %s;", table);
+
+#if 0
     r = sqlite3_prepare_v2(db, temp, -1, &stmt, 0);
     if (r != SQLITE_OK) {
         log_e("___switch_load, sqlite3_prepare_v2 failed\n");
         return -1;
     }
     
-    i = 0;
     memset(sw, 0, sizeof(netswitch_info_t));
     while (sqlite3_step(stmt) == SQLITE_ROW) {
         port_info_t *pt=&sw->port[i];
@@ -3515,7 +3535,30 @@ static int sw_load(sqlite3 *db, netswitch_info_t *sw)
         i++;
     }
     sqlite3_finalize(stmt);
+#else
+    int nrow = 0;               //满足条件的记录数目
+    int ncol = 0;            //每条记录包含的字段数据
+    char** pResult = NULL;  //用来指向sql执行结果的指针
+    char *err_msg=NULL;
 
+    r = sqlite3_get_table(db, temp, &pResult, &nrow, &ncol, &err_msg);
+    if(r != SQLITE_OK) {
+        log_e("sqlite handle error: %s \n%s\n", temp, err_msg); sqlite3_free(err_msg);
+        return -1 ;
+    }
+
+    for(i=1; i<=nrow; i++) {
+        port_info_t *pt=&sw->port[i-1];
+
+        pt->id = atoi(pResult[i*ncol+0]);
+        pt->up = atoi(pResult[i*ncol+1]);
+
+        strcpy(pt->ip, pResult[i*ncol+2]);
+        strcpy(pt->mac, pResult[i*ncol+3]);
+        strcpy(pt->desc, pResult[i*ncol+4]);
+    }
+    sqlite3_free_table(pResult);
+#endif
     return 0;
 }
 static int sw_update_port(sqlite3 *db, port_info_t *pt)
@@ -3526,11 +3569,12 @@ static int sw_update_port(sqlite3 *db, port_info_t *pt)
     char *err_msg=NULL;
     char *table=SWITCH_TABLE;
 
-    sprintf(temp, "UPDATE %s id=%d,up=%d,ip=%s,mac=%s,desc=%s WHERE id=%d;", 
-            table,pt->id,pt->up,pt->ip,pt->mac,pt->desc,pt->id);
+    sprintf(temp, "UPDATE %s SET up=%d,ip='%s',mac='%s',desc='%s' WHERE id=%d;", 
+            table,pt->up,pt->ip,pt->mac,pt->desc,pt->id);
     r = sqlite3_exec(db, temp, NULL, 0, &err_msg);
     if(r != SQLITE_OK) {
-        log_e("___sw_update_port failed, %s\n", err_msg);
+        log_e("___sw_update_port failed, %s, %s\n\n", temp, err_msg);
+
         sqlite3_free(err_msg);
         return -1;
     }
@@ -3577,24 +3621,13 @@ int dev_switch_init(sqlite3 *db, netswitch_info_t *sw)
 {
     int r;
     char *table=SWITCH_TABLE;
-    char temp[1000] = {0};
-    char *err_msg = NULL;
-
-    sprintf(temp, "CREATE TABLE IF NOT EXISTS %s("
-                        "id INTEGER PRIMARY KEY AUTOINCREMENT,"
-                        "up INTEGER,"
-                        "ip TEXT,"
-                        "mac TEXT,"
-                        "desc TEXT);", table);
-    r = sqlite3_exec(db, temp, 0, 0, &err_msg);
-    if(r!=SQLITE_OK) {
-        log_e("___ %s create failed, %s\n%s\n", table, temp, err_msg); sqlite3_free(err_msg);
-        return -1;
-    }
-
-    if(sw) {
-        memset(sw, 0, sizeof(netswitch_info_t));
-        sw_update_all(db, sw);
+    
+    if(!tab_exists(db, table)) {
+        r = tab_sw_create(db, table);
+        if(r==0) {
+            memset(sw, 0, sizeof(netswitch_info_t));
+            sw_update_all(db, sw);
+        }
     }
 
     r = sw_load(db, sw);
@@ -3609,7 +3642,7 @@ int dev_switch_update_port(sqlite3 *db, port_info_t *pt)
     r = sw_update_port(db, pt);
     pthread_mutex_unlock(&__globalDeviceManage.dbThreadlock);
 
-    return 0;
+    return r;
 }
 int dev_switch_update_all(sqlite3 *db, netswitch_info_t *sw)
 {