Sfoglia il codice sorgente

更新mongoose到最新版本
添加文件上传
将升级接口独立出来

guohui 2 anni fa
parent
commit
275ad6baf2

+ 1 - 1
output/t113i/etc/init.d/rcS

@@ -30,7 +30,7 @@ mdev -s
 
 /etc/adb_conf.sh start &
 #/usr/sbin/dnsmasq &
-#/sbin/udhcpc –i eth0 –s /usr/share/udhcpc/default.script &
+#/sbin/udhcpc ¨Ci eth0 ¨Cs /usr/share/udhcpc/default.script &
 /usr/sbin/smartMonitor.sh &
 
 tinymix set "ADC2 Input MIC2 Boost Switch" 1 > /dev/null

+ 1 - 1
pro/.vscode/launch.json

@@ -16,7 +16,7 @@
             "externalConsole": false,
             "MIMode": "gdb",
             "miDebuggerPath": "/home/tronlong/T113/T113-i_v1.0/out/t113_i/tlt113-evm-emmc/longan/buildroot/host/opt/ext-toolchain/bin/arm-linux-gnueabi-gdb",
-            "miDebuggerServerAddress": "192.168.1.131:1234"
+            "miDebuggerServerAddress": "192.168.1.132:1234"
         }
     ]
 }

+ 1 - 0
pro/CMakeLists.txt

@@ -73,6 +73,7 @@ set(PDU_SRC_LIST
 	${PROJECT_SOURCE_DIR}/src/thread.c
 	${PROJECT_SOURCE_DIR}/src/cascade.c
     ${PROJECT_SOURCE_DIR}/src/cascade_slave.c
+	${PROJECT_SOURCE_DIR}/src/upg.c
 	${PROJECT_SOURCE_DIR}/src/main.c
 
 	${PROJECT_SOURCE_DIR}/src/language_hashmap.c

File diff suppressed because it is too large
+ 4703 - 2442
pro/mongoose/mongoose.c


File diff suppressed because it is too large
+ 601 - 525
pro/mongoose/mongoose.h


+ 6 - 42
pro/src/app.c

@@ -13,6 +13,8 @@
 #include "paras.h"
 #include "thread.h"
 #include "cascade.h"
+#include "upg.h"
+#include "sys.h"
 
 #include <sys/mman.h>
 #include <string.h>
@@ -20,10 +22,8 @@
 #include <sys/stat.h>
 #include <sys/types.h>
 #include <unistd.h>
-static int keep_running;
-int get_path(char *path, char *name);
-int sys_get_path(char *path, char *name);
 
+static int keep_running;
 RETSIGTYPE stop_server(int __attribute__((unused)) a)
 {
     keep_running = 0;
@@ -1136,43 +1136,6 @@ void* snmp_thread(void *arg)
 }
 
 
-
-#include <errno.h>
-#include <sys/ipc.h> 
-#include <sys/msg.h>
-#include "upgrade.h"
-static key_t upgkey=0;
-int upg_send(int evt)
-{
-    int r,msgid;
-    upgrade_msg_t msg={.type=1, .evt=evt};
-
-    msgid = msgget(upgkey, 0666| IPC_CREAT);
-    log_e("+++++++++++ key: %d, msgid: %d\n", upgkey, msgid);
-    
-    r = msgsnd(msgid, &msg, sizeof(msg), IPC_NOWAIT);
-    if(r<0) {
-        log_e("+++++++++++ msgsnd failed, %d, errno: %d\n", r, errno);
-    }
-    
-    return r;
-}
-static int upg_init(void)
-{
-    int r,msgid;
-    
-    upgkey = ftok(UPG_KEY_PATH, UPG_KEY_SN);
-    if(upgkey<0) {
-        log_e("ftok %s failed, errno: %d\n", UPG_KEY_PATH, errno);
-        return -1;
-    }
-    upg_send(UPGRADE_EVT_APP_RUNNING);
-    
-    return 0;
-}
-
-
-
 /// @brief 全局结构体初始化
 /// @param _globalDeviceManager 
 /// @return 
@@ -1919,8 +1882,9 @@ int _global_device_manage_init(GlobalDeviceManager* _globalDeviceManager)
     appweb_init();
 
     cascade_init();
-    //通知upgd,app已经运行起来
-    //upg_init();
+
+    //通知smartUPG,app已经运行起来
+    upg_init();
     
     while(1)
     {

+ 55 - 59
pro/src/appweb_handle.c

@@ -10,6 +10,7 @@
 #include "cfg.h"
 #include "cascade.h"
 #include "appweb.h"
+#include "upg.h"
 #include "language_common.h"
 #include "websocket_handle.h"
 
@@ -151,6 +152,11 @@ static void *web_thread(void *arg)
 
 #else
 
+typedef struct {
+  size_t expected;  // POST data length, bytes
+  size_t received;  // Already received bytes
+  void *fp;         // Opened uploaded file
+}upload_stat_t;
 static void http_mg_cb(struct mg_connection *c, int ev, void *ev_data)
 {
     int i,j;
@@ -170,6 +176,10 @@ static void http_mg_cb(struct mg_connection *c, int ev, void *ev_data)
             mg_http_serve_dir(c, ev_data, &opts);
         }
     }
+    else if (ev == MG_EV_HTTP_HDRS) {
+        printf("____ MG_EV_HTTP_HDRS\n");
+
+    }
 }
 static void *web_thread(void *arg)
 {
@@ -217,7 +227,7 @@ static void systemConfiguration(void* conn)
 #else
     mg_stream_t *s = (mg_stream_t *)conn;
     struct mg_http_message *msg = s->ev_data;
-    rx_Head_buf = (char *)msg->head.ptr;
+    rx_Head_buf = (char *)msg->head.buf;
 #endif
 
     g_token = json_to_Token_info(rx_Head_buf);
@@ -252,7 +262,7 @@ static void changeLanguages(void* conn)
 #else
     mg_stream_t *s=(mg_stream_t*)conn;
     struct mg_http_message *msg=s->ev_data;
-    rx_body_buf = (char*)msg->body.ptr;
+    rx_body_buf = (char*)msg->body.buf;
 #endif
     json_to_switch_language(rx_body_buf);
     dev_update_device_genera_info(__globalDeviceManage.db,__globalDeviceManage._global_device_info);
@@ -274,7 +284,7 @@ static void loginFunc(void* conn)
 #else
     mg_stream_t *s=(mg_stream_t*)conn;
     struct mg_http_message *msg=s->ev_data;
-    rx_body_buf = msg->body.ptr;
+    rx_body_buf = msg->body.buf;
 #endif
 
     //用户信息创建-未析构
@@ -356,7 +366,7 @@ static void loginOutFunc(void* conn)
 #else
     mg_stream_t *s=(mg_stream_t*)conn;
     struct mg_http_message *msg=s->ev_data;
-    rx_body_buf = (char*)msg->body.ptr;
+    rx_body_buf = (char*)msg->body.buf;
 #endif
 
     //转换json参数
@@ -403,7 +413,7 @@ static void getUserRouters(void* conn)
 #else
     mg_stream_t *s=(mg_stream_t*)conn;
     struct mg_http_message *msg=s->ev_data;
-    rx_Head_buf = (char*)msg->head.ptr;
+    rx_Head_buf = (char*)msg->head.buf;
 #endif
 
     g_token = json_to_Token_info(rx_Head_buf);
@@ -439,7 +449,7 @@ static void overallStatusMonitoring(void* conn)
 #else
     mg_stream_t *s=(mg_stream_t*)conn;
     struct mg_http_message *msg=s->ev_data;
-    rx_body_buf = (char*)msg->body.ptr;
+    rx_body_buf = (char*)msg->body.buf;
 #endif
 
     //转换json参数
@@ -865,7 +875,7 @@ static void channelStatusMonitoring(void* conn)
 #else
     mg_stream_t *s=(mg_stream_t*)conn;
     struct mg_http_message *msg=s->ev_data;
-    rx_body_buf=(char*)msg->body.ptr;
+    rx_body_buf=(char*)msg->body.buf;
 #endif
 
     //转换json参数
@@ -1379,7 +1389,7 @@ static void sensorStatusMonitoring(void* conn)
 #else
     mg_stream_t *s=(mg_stream_t*)conn;
     struct mg_http_message *msg=s->ev_data;
-    rx_body_buf=(char*)msg->body.ptr;
+    rx_body_buf=(char*)msg->body.buf;
 #endif
     //转换json参数
     cjson = json_to_sensor_manage(rx_body_buf,&_over_all_sensor_request);
@@ -1869,7 +1879,7 @@ static void alarmProtect(void *conn)
 #else
     mg_stream_t *s=(mg_stream_t*)conn;
     struct mg_http_message *msg=s->ev_data;
-    rx_body_buf=(char*)msg->body.ptr;
+    rx_body_buf=(char*)msg->body.buf;
 #endif
 
     memset(&_alarm_request_info, 0, sizeof(_alarm_request_info));
@@ -2146,7 +2156,7 @@ static void logInformation(void *conn)
 #else
     mg_stream_t *s=(mg_stream_t*)conn;
     struct mg_http_message *msg=s->ev_data;
-    rx_body_buf=(char*)msg->body.ptr;
+    rx_body_buf=(char*)msg->body.buf;
 #endif
     
     memset(&__logInformationRequestInfo,0,sizeof(_LogInformationRequestInfo));
@@ -2341,7 +2351,7 @@ static void communicationManagement(void *conn)
 #else
     mg_stream_t *s=(mg_stream_t*)conn;
     struct mg_http_message *msg=s->ev_data;
-    rx_body_buf=(char*)msg->body.ptr;
+    rx_body_buf=(char*)msg->body.buf;
 #endif
 
     //转换json参数
@@ -2429,7 +2439,7 @@ static void serviceManagement(void *conn)
 #else
     mg_stream_t *s=(mg_stream_t*)conn;
     struct mg_http_message *msg=s->ev_data;
-    rx_body_buf = (char*)msg->body.ptr;
+    rx_body_buf = (char*)msg->body.buf;
 #endif
 
     //转换json参数
@@ -3157,7 +3167,7 @@ static void systemManagent(void *conn)
 #else
     mg_stream_t *s=(mg_stream_t*)conn;
     struct mg_http_message *msg=s->ev_data;
-    rx_body_buf = (char*)msg->body.ptr;
+    rx_body_buf = (char*)msg->body.buf;
 #endif
 
     //转换json参数
@@ -3323,7 +3333,7 @@ static void equipmentMintenance(void *conn)
 #else
     mg_stream_t *s=(mg_stream_t*)conn;
     struct mg_http_message *msg=s->ev_data;
-    rx_body_buf = (char*)msg->body.ptr;
+    rx_body_buf = (char*)msg->body.buf;
 #endif
 
     //转换json参数
@@ -3415,59 +3425,45 @@ static void equipmentMintenance(void *conn)
     http_reply(conn, 200, ack_str);
     cJSON_free((void*)ack_str);
 }
+static int upload_handle(void *conn, char *upload_path)
+{
+    int r=-1;
+    struct mg_http_part part;
+    mg_stream_t *s=(mg_stream_t*)conn;
+    struct mg_http_message *hm = (struct mg_http_message *)s->ev_data;
+    
+    MG_INFO(("New request to: [%.*s], body size: %lu", (int) hm->uri.len,
+             hm->uri.buf, (unsigned long) hm->body.len));
+    size_t pos = 0, total_bytes = 0, num_files = 0;
+    while ((pos = mg_http_next_multipart(hm->body, pos, &part)) > 0) {
+        MG_INFO(("Chunk name: [%.*s] filename: [%.*s] length: %lu bytes",
+                (int) part.name.len, part.name.buf, (int) part.filename.len,
+                part.filename.buf, (unsigned long) part.body.len));
 
-
-typedef struct {
-  size_t expected;  // POST data length, bytes
-  size_t received;  // Already received bytes
-  void *fp;         // Opened uploaded file
-}upload_stat_t;
+        if (mg_path_is_sane(mg_str(upload_path))) {
+            mg_file_write(&mg_fs_posix, upload_path, part.body.buf, part.body.len);
+            total_bytes += part.body.len;
+            num_files++;
+            r = 0;
+        }
+    }
+    return r;
+}
 static void equipmentMintenanceUpload(void *conn)
 {
-    int ret = -1 ;
-    char* ack_str = "OK" ;
-    int len,tlen=0;
+    int r = -1 ;
 
 #ifdef USE_WEBAPP
     HttpStream *s=(HttpStream*)conn;
     http_reply(conn, 200, NULL);
 #else
-/*
-    mg_stream_t *s=(mg_stream_t*)conn;
-    upload_stat_t *us = (upload_stat_t *) s->c->data;
-    struct mg_fs *fs = &mg_fs_posix;
-    
-    if (s->ev == MG_EV_HTTP_HDRS) {
-        struct mg_http_message *hm = (struct mg_http_message *) s->ev_data;
-        char path[MG_PATH_MAX];
-        mg_snprintf(path, sizeof(path), "%s/%.*s", "/root/dl", hm->uri.len - 8,
-                    hm->uri.buf + 8);
-        us->expected = hm->body.len;  // Store number of bytes we expect
-         mg_iobuf_del(&s->c->recv, 0, hm->head.len);
-        mg_iobuf_del(&s->c->recv, 0, hm->head.len);  // Delete HTTP headers
-        s->c->pfn = NULL;  // Silence HTTP protocol handler, we'll use MG_EV_READ
-        if (mg_path_is_sane(mg_str(path))) {
-          fs->rm(path);                        // Delete file if it exists
-          us->fp = fs->op(path, MG_FS_WRITE);  // Open file for writing
-        }
-    }
-
-    // Catch uploaded file data for both MG_EV_READ and MG_EV_HTTP_HDRS
-    if (us->expected > 0 && s->c->recv.len > 0) {
-        us->received += s->c->recv.len;
-        if (us->fp) fs->wr(us->fp, s->c->recv.buf, s->c->recv.len);  // Write to file
-        s->c->recv.len = 0;  // Delete received data
-        if (us->received >= us->expected) {
-            // Uploaded everything. Send response back
-            MG_INFO(("Uploaded %lu bytes", us->received));
-            mg_http_reply(s->c, 200, NULL, "%lu ok\n", us->received);
-            if (us->fp) fs->cl(us->fp);  // Close file
-            memset(us, 0, sizeof(*us));  // Cleanup upload state
-            s->c->is_draining = 1;          // Close connection when response gets sent
-        }
-    }  
-
-    */
+    char path[200];
+    sprintf(path, "%s/%s", DOWN_FOLDER, UPG_PKG_NAME);
+    r = upload_handle(conn, path);    
+    if(r==0) {
+        upg_start();
+    }
+    http_reply(conn, 200, "");
 #endif
 }
 

+ 45 - 0
pro/src/upg.c

@@ -0,0 +1,45 @@
+#include <errno.h>
+#include <sys/ipc.h> 
+#include <sys/msg.h>
+#include "elog.h"
+#include "upg.h"
+
+
+static key_t upgkey=0;
+
+static int upg_send(int evt)
+{
+    int r,msgid;
+    upgrade_msg_t msg={.type=1, .evt=evt};
+
+    msgid = msgget(upgkey, 0666| IPC_CREAT);
+    log_e("+++++++++++ key: %d, msgid: %d\n", upgkey, msgid);
+    
+    r = msgsnd(msgid, &msg, sizeof(msg), IPC_NOWAIT);
+    if(r<0) {
+        log_e("+++++++++++ msgsnd failed, %d, errno: %d\n", r, errno);
+    }
+    
+    return r;
+}
+
+
+int upg_init(void)
+{
+    int r,msgid;
+    
+    upgkey = ftok(UPG_KEY_PATH, UPG_KEY_SN);
+    if(upgkey<0) {
+        log_e("ftok %s failed, errno: %d\n", UPG_KEY_PATH, errno);
+        return -1;
+    }
+    
+    return upg_send(UPGRADE_EVT_APP_RUNNING);
+}
+
+
+int upg_start(void)
+{
+	return upg_send(UPGRADE_EVT_UPLOAD_FINISH);
+}
+

+ 10 - 0
pro/src/upg.h

@@ -0,0 +1,10 @@
+#ifndef __UPG_Hx__
+#define __UPG_Hx__
+
+#include "upgrade.h"
+
+int upg_init(void);
+int upg_start(void);
+
+#endif
+

+ 46 - 49
pro/src/websocket_handle.c

@@ -62,54 +62,52 @@ static void web_update(ws_handle_t *wh)
 	
     if(wh->ws_cnt>0)
     {
-        //if(gdm->view_page==1)
-        { 
-            if(gdm->_global_device_info->product_pwr_type==SmartPDU_Tree_AC_Tree ||
-                gdm->_global_device_info->product_pwr_type==SmartPDU_Tree_AC_One)
+        //overall info
+        if(gdm->_global_device_info->product_pwr_type==SmartPDU_Tree_AC_Tree ||
+            gdm->_global_device_info->product_pwr_type==SmartPDU_Tree_AC_One)
+        {
+            //三相
+            _OverAllPwrAckInfo l1,l2,l3;
+            if (cur_dev_addr == 0)
             {
-                //三相
-                _OverAllPwrAckInfo l1,l2,l3;
-                if (cur_dev_addr == 0)
-                {
-                    pthread_mutex_lock(&__globalDeviceManage._power_update); 
-                    dev_search_latest_t_ac_power_statistic_info(0, 0, &l1, gdm);
-                    dev_search_latest_t_ac_power_statistic_info(0, 1, &l2, gdm);
-                    dev_search_latest_t_ac_power_statistic_info(0, 2, &l3, gdm);
-                    pthread_mutex_unlock(&__globalDeviceManage._power_update);
-                }
-                else
-                {
-                    cascade_lock();
-                    dev_search_latest_t_ac_power_statistic_info(0, 0, &l1, gdm2);
-                    dev_search_latest_t_ac_power_statistic_info(0, 1, &l2, gdm2);
-                    dev_search_latest_t_ac_power_statistic_info(0, 2, &l3, gdm2);
-                    cascade_unlock();
-                }
-                json_str = over_all_pwr_monitor_Tree_AC_to_json(&l1, &l2, &l3);
-
-                ws_broadcast(wh, json_str, strlen(json_str));
-                cJSON_free((void*)json_str);
+                pthread_mutex_lock(&__globalDeviceManage._power_update); 
+                dev_search_latest_t_ac_power_statistic_info(0, 0, &l1, gdm);
+                dev_search_latest_t_ac_power_statistic_info(0, 1, &l2, gdm);
+                dev_search_latest_t_ac_power_statistic_info(0, 2, &l3, gdm);
+                pthread_mutex_unlock(&__globalDeviceManage._power_update);
             }
             else
             {
-                if(cur_dev_addr==0) {
-                    pthread_mutex_lock(&__globalDeviceManage._power_update); 
-                    dev_search_latest_power_statistic_info(gdm, &allInfo);
-                    pthread_mutex_unlock(&__globalDeviceManage._power_update);
-                }
-                else {
-                    cascade_lock();
-                    dev_search_latest_power_statistic_info(gdm2, &allInfo);
-                    cascade_unlock();  
-                }
-                json_str = ws_over_status_ack_to_json(0, &allInfo);
-
-                ws_broadcast(wh, json_str, strlen(json_str));
-                cJSON_free(json_str);
-            }                
+                cascade_lock();
+                dev_search_latest_t_ac_power_statistic_info(0, 0, &l1, gdm2);
+                dev_search_latest_t_ac_power_statistic_info(0, 1, &l2, gdm2);
+                dev_search_latest_t_ac_power_statistic_info(0, 2, &l3, gdm2);
+                cascade_unlock();
+            }
+            json_str = over_all_pwr_monitor_Tree_AC_to_json(&l1, &l2, &l3);
+
+            ws_broadcast(wh, json_str, strlen(json_str));
+            cJSON_free((void*)json_str);
         }
-        //推送通道信息
-        //else if(gdm->view_page==2)
+        else
+        {
+            if(cur_dev_addr==0) {
+                pthread_mutex_lock(&__globalDeviceManage._power_update); 
+                dev_search_latest_power_statistic_info(gdm, &allInfo);
+                pthread_mutex_unlock(&__globalDeviceManage._power_update);
+            }
+            else {
+                cascade_lock();
+                dev_search_latest_power_statistic_info(gdm2, &allInfo);
+                cascade_unlock();  
+            }
+            json_str = ws_over_status_ack_to_json(0, &allInfo);
+
+            ws_broadcast(wh, json_str, strlen(json_str));
+            cJSON_free(json_str);
+        }
+
+        //channel info
         {
             INIT_LIST_HEAD(&chInfo.list);
 
@@ -126,10 +124,8 @@ static void web_update(ws_handle_t *wh)
 
             json_str = ws_chn_status_ack_to_json(1,&chInfo);
             ws_broadcast(wh, json_str, strlen(json_str));
-              
-            //释放资源,一定要记得释放json字符串!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
             cJSON_free((void*)json_str);
-            //释放资源,一定要记得释放json字符串!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
+            
             _OverChnPwrAckInfo *node,*next;
             list_for_each_entry_safe(node, next, &chInfo.list, list)
             {
@@ -137,7 +133,8 @@ static void web_update(ws_handle_t *wh)
                 free(node);
             } 
         }
-        //else if (gdm->view_page==3)
+
+        //sensor info
         {
             json_str = ws_sensor_status_ack_to_json(&__globalDeviceManage._globalSensorManger);
             ws_broadcast(wh, json_str, strlen(json_str));
@@ -177,10 +174,10 @@ static void fn(mg_conn_t *c, int ev, void *ev_data)
     case MG_EV_HTTP_MSG:
     {
         mg_http_msg_t *hm = (mg_http_msg_t *) ev_data;
-        if (mg_http_match_uri(hm, "/websocket/PDU-WebSocket")) {
+        if (mg_match(hm->uri, mg_str("/websocket/PDU-WebSocket"), NULL)) {
             mg_ws_upgrade(c, hm, NULL);
         }
-        else if (mg_http_match_uri(hm, "/rest")) {
+        else if (mg_match(hm->uri, mg_str("/rest"), NULL)) {
             mg_http_reply(c, 200, "", "{\"result\": %d}\n", 123);
         }
     }