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

1、修改web和websocket的ipv6绑定
遗留问题:ipv6修改后需重新启动程序才能打开网页,ipv4无此问题,原因待查

guohui пре 2 година
родитељ
комит
334c24a872

+ 1 - 1
pro/mongoose/mongoose.h

@@ -707,7 +707,7 @@ struct timeval {
 #endif
 
 #ifndef MG_ENABLE_IPV6
-#define MG_ENABLE_IPV6 0
+#define MG_ENABLE_IPV6 1
 #endif
 
 #ifndef MG_IPV6_V6ONLY

+ 2 - 2
pro/src/appweb_handle.c

@@ -208,7 +208,7 @@ static void *web_thread(void *arg)
     struct mg_connection *c;
     mg_handle_t *mh=&webHandle.mg;
     thread_handle_t *h=(thread_handle_t*)arg;
-    const char *listen_addr="http://0.0.0.0:80";
+    const char *listen_addr="http://[::]:80";
 
     //mg_log_set(MG_LL_DEBUG);
     mg_mgr_init(&mgr);
@@ -2514,7 +2514,7 @@ static void communicationManagement(void *conn)
         int ipv=strstr(reqInfo.type, "WkIPV4Save")?IP_V4:IP_V6;
         
         int mode = atoi(info2->mode);
-        if(ipv==IP_V4 && mode==IP_STATIC) {
+        if(mode==IP_STATIC) {
             info->mode = mode;
             
             if(info2->ipaddr)   strcpy(info->ip_address, info2->ipaddr);

+ 3 - 2
pro/src/json_handle.c

@@ -1809,7 +1809,7 @@ char* over_all_pwr_monitor_Tree_AC_to_json(_OverAllPwrAckInfo* _over_all_pwr_bac
 
     sprintf(temp,"%0.3f",_over_all_pwr_back_info_L3->freq);
     cJSON_AddStringToObject(data_filed2,"productPowerFreq",temp);
-    sprintf(temp,"%0.3f",_over_all_pwr_back_info_L3->consumption);  
+    sprintf(temp,"%0.3f",_over_all_pwr_back_info_L3->consumption*1000);  
     cJSON_AddStringToObject(data_filed2,"productConsumption",temp);
     sprintf(temp,"%0.3f",_over_all_pwr_back_info_L3->factor);                 
     cJSON_AddStringToObject(data_filed2,"productPowerFactor",temp);
@@ -4097,7 +4097,8 @@ cJSON* json_to_communication_manage(const char* str,_CommunicationManageRequestI
     if(cjson_type!=NULL)
         _communicationRequestInfo->type = cjson_type->valuestring ;
     //网络配置上传参数
-    if(strcmp(_communicationRequestInfo->type,"WkIPV4Save")==0)
+    if(strcmp(_communicationRequestInfo->type,"WkIPV4Save")==0 || 
+       strcmp(_communicationRequestInfo->type,"WkIPV6Save")==0)
     {     
         cJSON* cjson_net_mode = cJSON_GetObjectItem(cjson,"mode");
         cJSON* cjson_net_ipaddr = cJSON_GetObjectItem(cjson,"ipaddress");  

+ 41 - 3
pro/src/sqlite_handle.c

@@ -9,6 +9,14 @@
 
 #define	TOTAL_COUNT	60//统计总数
 
+
+typedef struct {
+    sqlite3             *db;
+    pthread_mutex_t     mutex;
+}sql_handle_t;
+static sql_handle_t sqlHandle={0};
+
+
 /// @brief 初始化SQLite数据库
 /// @param db 数据库接口          
 /// @param daba_name 数据库名称
@@ -17,6 +25,8 @@ int sqlite_init(sqlite3** db,pthread_mutex_t* pDBMutex, const char* daba_name)
 {
     int ret = 0;
     char *errorMsg = NULL;
+    //sql_handle_t *h=&sqlHandle;
+
     if (pthread_mutex_init(pDBMutex, NULL) != 0) {  
         log_d("DB Mutex init has failed\n");  
         return -1;  
@@ -3417,19 +3427,47 @@ int dev_Alarm_Run_message(GlobalDeviceManager* __globalDeviceManger,const char*
 }
 
 
-int dev_switch_init(sqlite3 *db)
+int dev_switch_init(sqlite3 *db, switch_data_t *sw)
 {
+    int r;
     char *tab_name="Table_SwitchPort";
     char select_sql[1000] = {0};
+    char *errorMsg = NULL;
+
+    sprintf(select_sql, "SELECT name FROM sqlite_master WHERE type='table' AND name='%s'", tab_name);
+    r = sqlite3_exec(db, select_sql, 0, 0, NULL);
+    if(r!=SQLITE_OK) {
+        log_w("___ %s not exist, create it\n", tab_name);
+
+        sprintf(select_sql, "CREATE TABLE %s(id INTEGER PRIMARY KEY AUTOINCREMENT,"
+                            "up INTEGER, mac TEXT, ip4 TEXT, ip6 TEXT);", tab_name);
+        r = sqlite3_exec(db, select_sql, 0, 0, NULL);
+        if(r!=SQLITE_OK) {
+            log_w("___ %s create failed\n", tab_name);
+            return -1;
+        }
+    }
+
+    //
+
+
 
-    sprintf(select_sql,"SELECT name FROM sqlite_master WHERE type='table' AND name='Table_SwitchPort'");
-    //sqlite3_exec();
     return 0;
 }
 
 
 int dev_switch_update(sqlite3 *db, switch_data_t *sw)
 {
+    char **result;
+    char *errMsg = 0;
+    int r,nrow=0,ncol=0;
+
+    pthread_mutex_lock(&__globalDeviceManage.dbThreadlock);
+
+    //r = sqlite3_get_table2(db, sql, &result, &nrow, &ncol, &errMsg);
+
+    pthread_mutex_unlock(&__globalDeviceManage.dbThreadlock);
+
     return 0;
 }
 

+ 1 - 1
pro/src/sqlite_handle.h

@@ -138,7 +138,7 @@ int dev_insert_breaker_genera_manage(sqlite3 *db,GlobalBreakerManager* _globalBr
 int dev_update_breaker_genera_manage(sqlite3 *db,int product_id,int breaker_id,GlobalBreakerManager* _globalBreakerManager);
 int dev_delete_breaker_manage(sqlite3 *db,int product_id,int breaker_id);
 
-int dev_switch_init(sqlite3 *db);
+int dev_switch_init(sqlite3 *db, switch_data_t *sw);
 int dev_switch_update(sqlite3 *db, switch_data_t *sw);
 
 #endif

+ 38 - 63
pro/src/sys.c

@@ -24,7 +24,7 @@
 #endif
 
 
-//#define USE_IPV6
+#define USE_IPV6
 
 //#define USE_BASH
 #define USE_FILE
@@ -490,60 +490,29 @@ typedef struct {
     int  len;
 }nic_t;
 
-static char* find_str(char *buf, char *head, char *tail, char *nic)
-{
-    int len;
-    char *p,*p1,*p2,*ptr=NULL;
-
-    p1 = strstr(buf, head);
-    p2 = strstr(buf, tail);
-    if(p1 && p2) {
-        p1 = p1 + sizeof(head);
-        len = XLEN(p2,p1);
-        if(len>0) {
-            p = malloc(len+1);
-            if(p) {
-                memcpy(p, p1, len); p[len] = 0;
-                if(strstr(p, nic)) {
-                    ptr = p1;
-                }
-                free(p);
-            }
-        }
-    }
-    return ptr;
-}
+
 static int get_nic(char *nbuf, int nlen, char *nic, char *fbuf, int flen)
 {
-    int r,len;
-    char *p,*p1,*p2;
+    int r,xlen;
+    char tmp[20];
+    char *p1,*p2;
     char *fend=fbuf+flen-1;
 
-    p = fbuf;
-    while(p<fend) {
-        p1 = find_str(p, "auto", "\n", nic);
-        if(p1) {
-            p2 = strstr(p1+1, "auto");
-            if(!p2) {
-                p2 = fend;
-            }
-
-            len = XLEN(p2,p1);
-            if(nlen>len) {
-                memcpy(nbuf, p1, len);
-                nbuf[len] = 0;
-                //LOGD("__x__%s:\n %s\n", nic, nbuf);
-
-                return 0;
-            }
-            else {
-                LOGD("___ nbuf is too small\n");
-            }
-        }
-        p = p+1;
+    sprintf(tmp, "auto %s", nic);
+    p1 = strstr(fbuf, tmp);
+    if(!p1) {
+        return -1;
+    }
+    p2 = strstr(p1+1, "auto");
+    if(!p2 || p2>fend) {
+        p2 = fend;
     }
 
-    return -1;
+    xlen = XLEN(p2,p1);
+    memcpy(nbuf, p1, xlen);
+    nbuf[xlen] = 0;
+
+    return 0;
 }
 static int get_ip(char *nbuf, char *token, char *ipaddr, int ipv)
 {
@@ -564,7 +533,9 @@ static int get_ip(char *nbuf, char *token, char *ipaddr, int ipv)
 
     p = strstr(pbuf, token);
     if(p) {
-        while(!isdigit(*p)) p++;
+        p += strlen(token);
+
+        while(isspace(*p)) p++;
         p1 = p;
         while(!isspace(*p)) p++;
         p2 = p;
@@ -614,14 +585,19 @@ static int get_dns(char *nbuf, char *dns1, char *dns2)
 
     return 0;
 }
-static int set_dns(char *dns1, char *dns2)
+static int set_dns(NetworkInfo_t *para)
 {
     int r;
-    char buf[4000];
+    char buf[200];
     
     LOGD("_____ set_dns\n");
+
+    if(strlen(para->dns1)==0 && strlen(para->dns2)==0) {
+        return -1;
+    }
+
     memset(buf, 0, sizeof(buf));
-    snprintf(buf, sizeof(buf), "interface=eth0\nserver=%s\nserver=%s\n", dns1, dns2);
+    snprintf(buf, sizeof(buf), "interface=eth0\nserver=%s\nserver=%s\n", para->dns1, para->dns2);
     r = file_save(FILE_DNS, buf, strlen(buf))+1;
     if(r==0) {
         system("killall dnsmasq; /usr/sbin/dnsmasq &");
@@ -743,16 +719,7 @@ static int set_net(NetworkInfo_t *para, int ipv)
 #endif
     //LOGD("___interfaces___:\n%s", fbuf);
 
-    r = file_save(FILE_NET, fbuf, strlen(fbuf));
-    if(r==0) {
-        //system("ip addr flush dev eth0");
-        //sprintf(eth0, "ip link set dev eth0 %s/%d && ip link set eth0 up && ip link set eth0 down", info->ip_address, info->netmask);
-        //system(eth0);
-        system("ifdown eth0 && ifup eth0");
-    }
-    free(fbuf);
-    
-    //set_dns(para->dns1, para->dns2);
+    r = file_save(FILE_NET, fbuf, strlen(fbuf)); free(fbuf);
 
     return r;
 }
@@ -801,7 +768,15 @@ int sys_set_net(NetworkInfo_t *para, int ipVersion)
             LOGW("%s NetworkInfo is same, no need to set\n", DFLT_NIC);
             return -1;
         }
+        set_dns(para);
         r = set_net(para, ipVersion);
+        if(r==0) {
+            //system("ip addr flush dev eth0");
+            //sprintf(eth0, "ip link set dev eth0 %s/%d && ip link set eth0 up && ip link set eth0 down", info->ip_address, info->netmask);
+            //system(eth0);
+            system("ifdown eth0 && ifup eth0");
+            if(ipVersion==IP_V6) exit(0);
+        }
     }
     else {
         LOGE("get_net failed\n");

+ 1 - 1
pro/src/websocket_handle.c

@@ -260,7 +260,7 @@ void* websocket_thread(void* arg)
     int r;
     thread_handle_t *h=(thread_handle_t*)arg;
     ws_handle_t *wh=(ws_handle_t*)h->arg;
-    const char *listen_addr="ws://0.0.0.0:6785";
+    const char *listen_addr="ws://[::]:6785";
 
     mg_mgr_init(&wh->mgr);  // Initialise event manager
     mg_http_listen(&wh->mgr, listen_addr, fn, NULL);  // Create HTTP listener