Forráskód Böngészése

1、添加全局锁文件
2、解网口检测的bug

guohui 2 éve%!(EXTRA string=óta)
szülő
commit
c1f75d598b
9 módosított fájl, 179 hozzáadás és 65 törlés
  1. 2 1
      pro/CMakeLists.txt
  2. 1 1
      pro/config/cfg.h
  3. 2 1
      pro/src/app.c
  4. 2 3
      pro/src/appweb_handle.c
  5. 60 0
      pro/src/lock.c
  6. 22 0
      pro/src/lock.h
  7. 85 51
      pro/src/netswitch.c
  8. 2 8
      pro/src/sqlite_handle.c
  9. 3 0
      pro/src/sys.c

+ 2 - 1
pro/CMakeLists.txt

@@ -77,9 +77,10 @@ set(PDU_SRC_LIST
 	${PROJECT_SOURCE_DIR}/src/xlist.c
 	${PROJECT_SOURCE_DIR}/src/thread.c
 	${PROJECT_SOURCE_DIR}/src/cascade.c
-        ${PROJECT_SOURCE_DIR}/src/cascade_slave.c
+    ${PROJECT_SOURCE_DIR}/src/cascade_slave.c
 	${PROJECT_SOURCE_DIR}/src/upg.c
 	${PROJECT_SOURCE_DIR}/src/file.c
+	${PROJECT_SOURCE_DIR}/src/lock.c
 	${PROJECT_SOURCE_DIR}/src/smtp.c
 	${PROJECT_SOURCE_DIR}/src/mqtt.c
 	${PROJECT_SOURCE_DIR}/src/netswitch.c

+ 1 - 1
pro/config/cfg.h

@@ -60,7 +60,7 @@
     #endif
     
     #undef MAX_CHN_COUNT
-    #define MAX_CHN_COUNT               24
+    #define MAX_CHN_COUNT               4//24
 #endif
 
 ////////////////////////////////////////////////////

+ 2 - 1
pro/src/app.c

@@ -16,6 +16,7 @@
 #include "upg.h"
 #include "sys.h"
 #include "cfg.h"
+#include "lock.h"
 #include "netswitch.h"
 #include "breaker_detection.h"
 #include <sys/mman.h>
@@ -2415,7 +2416,7 @@ int _global_device_manage_init(GlobalDeviceManager* _globalDeviceManager)
 
     thread_start(THREAD_ID_NTP, ntp_thread, NULL, 10 * MB, 0);
     dev_Alarm_Run_message(_globalDeviceManager, language_alarm_Init_Success[0], "NTP");
-    thread_start(THREAD_ID_SNMP, snmp_thread, _globalDeviceManager, 10 * MB, 0);
+    //thread_start(THREAD_ID_SNMP, snmp_thread, _globalDeviceManager, 10 * MB, 0);
     dev_Alarm_Run_message(_globalDeviceManager, language_alarm_Init_Success[0], "SNMP");
  #endif
 

+ 2 - 3
pro/src/appweb_handle.c

@@ -2705,14 +2705,13 @@ static void serviceManagement(void *conn)
     else if(strcmp(_serviceManageRequestInfo.type,"listSmtpQuery")==0)
     {
     #ifdef USE_SMTP
-         //ack_str = communication_net_ack_to_json(__globalDeviceManage.db, &globalSMTPManager);
+         //ack_str = service_smtp_ack_to_json(&globalSMTPManager);
     #endif
     }   
     else if(strcmp(_serviceManageRequestInfo.type,"SmtpSave")==0)
     {
     #ifdef USE_SMTP
-
-
+        
     #endif
     }   
     //查询snmp信息

+ 60 - 0
pro/src/lock.c

@@ -0,0 +1,60 @@
+#include <pthread.h>
+#include <stdio.h>
+#include "lock.h"
+#include "elog.h"
+
+#if 0
+#define LOGD  log_d
+#define LOGE  log_e
+#else
+#define LOGD  printf
+#define LOGE  printf
+#endif
+
+
+//static global lock 
+
+static pthread_mutex_t mutex_pool[LOCK_ID_MAX];
+int lock_s_init(void)
+{
+    int i,r;
+    
+    for(i=0; i<LOCK_ID_MAX; i++) {
+        r = pthread_mutex_init(&mutex_pool[i], NULL);
+    }
+    
+    return 0;
+}
+
+
+int lock_s_deinit(void)
+{
+    int i,r;
+    
+    for(i=0; i<LOCK_ID_MAX; i++) {
+        r = pthread_mutex_destroy(&mutex_pool[i]);
+    }
+    
+    return 0;
+}
+
+
+int lock_s_hold(int id)
+{
+    if(id<0 || id>=LOCK_ID_MAX) {
+        return -1;
+    }
+    
+     return pthread_mutex_lock(&mutex_pool[id]);
+}
+
+
+int lock_s_release(int id)
+{
+    if(id<0 || id>=LOCK_ID_MAX) {
+        return -1;
+    }
+    
+    return pthread_mutex_unlock(&mutex_pool[id]);
+}
+

+ 22 - 0
pro/src/lock.h

@@ -0,0 +1,22 @@
+#ifndef __LOCK_Hx__
+#define __LOCK_Hx__
+
+#include "upgrade.h"
+
+enum {
+    
+    LOCK_ID_SNMP=0,
+    
+    
+    
+    LOCK_ID_MAX
+};
+
+
+int lock_s_init(void);
+int lock_s_deinit(void);
+int lock_s_hold(int id);
+int lock_s_release(int id);
+
+#endif
+

+ 85 - 51
pro/src/netswitch.c

@@ -5,6 +5,7 @@
 #include "elog.h"
 #include "thread.h"
 #include "cfg.h"
+#include "lock.h"
 #include "sqlite_handle.h"
 #include <net/if.h>
 #include <ifaddrs.h>
@@ -48,23 +49,24 @@ static netsnmp_variable_list* snmp_getx(int version, char *community, char *ip,
     netsnmp_variable_list *vars=NULL,*pvar=NULL;
     
     snmp_sess_init(&session);
+    session.timeout = 200000;   //us
     session.version  = version;
     session.peername = ip;
     session.community = (unsigned char*)community;
     session.community_len = strlen(community);
 
-    ss = snmp_open(&session);
-    if (ss == NULL) {
-        LOGE("___snmp_open failed\n");
-        return NULL;
-    }
-    
     oid *poid = snmp_parse_oid(oid_str, name, &namelen);
     if(poid==NULL) {
         LOGE("___snmp_parse_oid %s failed\n", oid_str);
-        goto quit;
+        goto out;
     }
 
+    ss = snmp_open(&session);
+    if (ss == NULL) {
+        LOGE("___snmp_open failed\n");
+        goto out;
+    }
+    
     pdu = snmp_pdu_create(msgtype);
     if(!pdu) {
         LOGE("___snmp_pdu_create failed\n");
@@ -77,7 +79,7 @@ static netsnmp_variable_list* snmp_getx(int version, char *community, char *ip,
     }
     snmp_add_null_var(pdu, name, namelen);
 
-    retry:
+ retry:
     status = snmp_synch_response(ss, pdu, &rsp);
     if (status == STAT_SUCCESS) {
         if (rsp->errstat == SNMP_ERR_NOERROR) {
@@ -105,18 +107,19 @@ static netsnmp_variable_list* snmp_getx(int version, char *community, char *ip,
             }
         }
     } else if (status == STAT_TIMEOUT) {
-        LOGE("Timeout: No Response from %s.\n", session.peername);
+        LOGE("snmp_getx timeout: No response from %s\n", session.peername);
     } else {                    /* status == STAT_ERROR */
-        snmp_sess_perror("snmpget", ss);
+        snmp_sess_perror("snmp_getx", ss);
     }
-    
+
     if (rsp) {
         snmp_free_pdu(rsp);
     }
-    
+
 quit:
     snmp_close(ss);
-    
+
+out:
     return pvar;
 }
 
@@ -346,7 +349,6 @@ static void sw_sync_ip_by_mac(netswitch_info_t *dst, netswitch_info_t *src)
     for(i=0; i<SW_PORT_MAX; i++) {
         id = get_id_by_mac(dst, &src->port[i]);
         if(id>=0) {
-            src->port[id] = src->port[i];
             src->port[id].id = id;
         }
     }
@@ -447,7 +449,7 @@ static int get_port_start(char *ip)
                 }
                 free(pstr);
 
-                if(port>2) {
+                if(port>1000) {
                     LOGD("_____start port: %d\n", port);
                     break;
                 }
@@ -503,7 +505,7 @@ static int sw_get(netswitch_info_t *sw)
         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);
+//set_ip(SW_PDU_IP); set_gw(h->host_ip);
         h->ipflag = 1;
 #else
         r = get_ipaddr(AF_INET, "eth0", h->host_ip);
@@ -531,7 +533,7 @@ static int sw_get(netswitch_info_t *sw)
     char *dot1dTpFdbEntryOid="1.3.6.1.2.1.17.4.3.1";
     var = snmp_getbulk(SNMP_VERSION_2c, "public", h->host_ip, dot1dTpFdbEntryOid);
     if(var) {
-        char *tok_mac="Hex-STRING: ", *tok_id="INTEGER: ";
+        char *tok;
 
         sw_init(&tmp);
 
@@ -541,25 +543,30 @@ static int sw_get(netswitch_info_t *sw)
             if(pstr) {
                 //LOGD("__1__%s\n", pstr);
 
-                if(strstr(pstr, "2.17.4.3.1.1")) {  //mac
-                    char *p=strstr(pstr, tok_mac);
+                if(strstr(pstr, "2.17.4.3.1.1") || strstr(pstr, "3.6.1.2.1.17.4.3.1.1")) {  //mac
+                    tok = "Hex-STRING: ";
+                    char *p=strstr(pstr, tok);
                     if(p) {
-                        p += strlen(tok_mac);
+                        p += strlen(tok);
                         get_mac(p, tmp.port[i].mac);
+                        //LOGD("____%d___mac: %s\n", i, tmp.port[i].mac); 
+
                         i++;
                     }
                 }
 
-                if(strstr(pstr, "2.17.4.3.1.2")) {  //port
-                    char *p=strstr(pstr, tok_id);
+                if(strstr(pstr, "2.17.4.3.1.2") || strstr(pstr, "3.6.1.2.1.17.4.3.1.2")) {  //port
+                    tok = "INTEGER: ";
+                    char *p=strstr(pstr, tok);
                     if(p) {
-                        p += strlen(tok_id);
+                        p += strlen(tok);
                         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++;
                     }
                 }
+
                 free(pstr);
             }
         }
@@ -581,7 +588,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->host_ip, ipNetToMediaEntryOid);
     if(var) {
-        char *tok_mac="STRING: ", *tok_ip="IpAddress: ";
+        char *tok=NULL;
 
         sw_init(&tmp);
 
@@ -592,24 +599,40 @@ static int sw_get(netswitch_info_t *sw)
                 //LOGD("__2__%s\n", pstr);
 
                 if(strstr(pstr, "ipNetToMediaPhysAddress")) {  //mac addr
-                    char *p=strstr(pstr, tok_mac);
+                    tok = "STRING: ";
+                    char *p=strstr(pstr, tok);
+                    if(p) {
+                        p += strlen(tok);
+                        get_mac(p, tmp.port[i].mac);
+                        //LOGD("__%d__%s\n", i, tmp.port[i].mac);
+
+                        i++;
+                    }
+                }
+                else if(strstr(pstr, "3.6.1.2.1.4.22.1.2")) {
+                    tok = "Hex-STRING: ";
+                    char *p=strstr(pstr, tok);
                     if(p) {
-                        p += strlen(tok_mac);
+                        p += strlen(tok);
                         get_mac(p, tmp.port[i].mac);
+                        //LOGD("_____%d___mac:%s\n", i, tmp.port[i].mac);
+
                         i++;
                     }
                 }
 
-                if(strstr(pstr, "ipNetToMediaNetAddress")) {  //ip addr
-                    char *p=strstr(pstr, tok_ip);
+                if(strstr(pstr, "ipNetToMediaNetAddress") || strstr(pstr, "3.6.1.2.1.4.22.1.3")) {  //ip addr
+                    tok = "IpAddress: ";
+                    char *p=strstr(pstr, tok);
                     if(p) {
-                        p += strlen(tok_ip);
+                        p += strlen(tok);
                         strcpy(tmp.port[j].ip, p);
                         j++;
 
                         //LOGD("__%d__ %s\n", j, p);
                     }
                 }
+
                 free(pstr);
             }
         }
@@ -630,24 +653,28 @@ static int sw_get(netswitch_info_t *sw)
         var = snmp_get(SNMP_VERSION_2c, "public", h->host_ip, temp);
         if(var) {
             int id;
-            char *p,*tok_ifo="ifOperStatus.";
+            char *p,*tok="ifOperStatus.";
 
             for (pvar=var; pvar; pvar=pvar->next_variable) {
                 char *pstr = get_string(pvar);
                 if(pstr) {
                     //LOGD("__3__%s\n", pstr);
 
-                    p = strstr(pstr, tok_ifo);
-                    if(p) {  //ip addr
-                        p += strlen(tok_ifo);
-                        id = atoi(p)-h->port_start;
-                        if(strstr(p, "up")) {
-                            sw->port[id].up = 1;
+                    p = strstr(pstr, tok);
+                    if(!p) {  //ip addr
+                        tok = "INTEGER: ";
+                        p = strstr(pstr, tok);
+                        if(p) {
+                            p += strlen(tok);
+                            id = atoi(p)-h->port_start;
+                            if(strstr(p, "up")) {
+                                sw->port[id].up = 1;
+                            }
+                            else if(strstr(p, "down")) {
+                                sw->port[id].up = 0;
+                            }
+                            j++;
                         }
-                        else if(strstr(p, "down")) {
-                            sw->port[id].up = 0;
-                        }
-                        j++;
                     }
                     free(pstr);
                 }
@@ -668,29 +695,36 @@ static int sw_chk(netswitch_info_t *sw1, netswitch_info_t *sw2, netswitch_evts_t
 
     memset(evts, 0, sizeof(netswitch_evts_t));
     for(i=0; i<SW_PORT_MAX; i++) {
+
+        if(i<SW_PORT_INDEX_START || i>SW_PORT_INDEX_END) {
+            continue;
+        }
+
+#if 0
         if(sw1->port[i].up != sw2->port[i].up) {
             if(sw2->port[i].up) {
                 LOGD("____ port %d plug in\n", i+1);
-                evts->evt[i].cab = EVT_CAB_IN;
+                //evts->evt[i].cab = EVT_CAB_IN;
             }
             else {
                 LOGD("____ port %d plug out\n", i+1);
-                evts->evt[i].cab = EVT_CAB_OUT;
+                //evts->evt[i].cab = EVT_CAB_OUT;
             }
-            flag = 1;
+            //flag = 1;
+        }
+
+        if(strlen(sw2->port[i].ip) && strcmp(sw1->port[i].ip, sw2->port[i].ip)) {
+            LOGD("____ port %d ip changed from: %s, to: %s\n", i+1, sw1->port[i].ip, sw2->port[i].ip);
+            //evts->evt[i].dev = EVT_IP_CHG;
+            //flag = 1;
         }
+#endif
         
         if(strlen(sw2->port[i].mac) && strcmp(sw1->port[i].mac, sw2->port[i].mac)) {
             LOGD("____ port %d mac changed from: %s, to: %s\n", i+1, sw1->port[i].mac, sw2->port[i].mac);
             evts->evt[i].dev = EVT_MAC_CHG;
             flag = 1;
         }
-
-        if(strlen(sw2->port[i].ip) && strcmp(sw1->port[i].ip, sw2->port[i].ip)) {
-            LOGD("____ port %d ip changed from: %s, to: %s\n", i+1, sw1->port[i].ip, sw2->port[i].ip);
-            evts->evt[i].dev = EVT_IP_CHG;
-            flag = 1;
-        }
     }
 
     return flag;
@@ -759,7 +793,7 @@ static int sw_alarm(netswitch_evts_t *evts, netswitch_info_t *old, netswitch_inf
                 snprintf(temp, sizeof(temp), "port[%d] device ip changed from %s to %s\n", i+1, old->port[i].ip, new->port[i].ip);
             }
             else {
-                snprintf(temp, sizeof(temp), "port[%d] device mac changed from %s to %s\n", i+1, old->port[i].mac, new->port[i].mac);
+                //snprintf(temp, sizeof(temp), "port[%d] device mac changed from %s to %s\n", i+1, old->port[i].mac, new->port[i].mac);
             }
             strcat(alarm_str, temp);
         }
@@ -833,7 +867,7 @@ int netswitch_get(netswitch_info_t *info)
     if(!info) {
         return -1;
     }
-    *info = h->sw;
+    *info = h->sw2;
 #endif
 
     return 0;

+ 2 - 8
pro/src/sqlite_handle.c

@@ -483,14 +483,8 @@ int dev_search_latest_power_All_info(GlobalDeviceManager* globalDeviceManger,_Ov
             #ifdef USE_NETSWITCH
                 idx = _overChnPwrBackInfoTemp->productChId-1;
                 port_info_t *info=&swInfo.port[idx];
-                if(info->up) {
-                    _overChnPwrBackInfoTemp->mac_addr = swInfo.port[idx].mac;
-                    _overChnPwrBackInfoTemp->ip_addr = swInfo.port[idx].ip;
-                }
-                else {
-                    _overChnPwrBackInfoTemp->mac_addr = "";
-                    _overChnPwrBackInfoTemp->ip_addr = "";
-                }
+                _overChnPwrBackInfoTemp->mac_addr = info->mac?info->mac:"";
+                _overChnPwrBackInfoTemp->ip_addr = info->ip?info->ip:"";
             #endif
 
                 list_add_tail(&_overChnPwrBackInfoTemp->list, &__overChnPwrAckInfo->list);

+ 3 - 0
pro/src/sys.c

@@ -10,6 +10,7 @@
 #include "paras.h"
 #include "cfg.h"
 #include "file.h"
+#include "lock.h"
 #include "thread.h"
 #include "sqlite_handle.h"
 
@@ -1014,6 +1015,8 @@ quit:
 
 int sys_init(void)
 {
+    lock_s_init();
+    
     //thread_start(THREAD_ID_PLUG, plug_thread, NULL, 4*MB, 0);
 
     return 0;