mirror of
https://github.com/oopuuu/zTC1.git
synced 2026-03-19 02:20:04 +08:00
Merge branch 'dev_file_ota'
# Conflicts: # TC1/http_server/app_httpd.c # TC1/http_server/web/index.html # TC1/mqtt_server/user_mqtt_client.c # TC1/ota_server/ota_server.c # mico-os/libraries/daemons/http_server/httpd.c
This commit is contained in:
2
.gdbinit
2
.gdbinit
@@ -1,2 +1,2 @@
|
||||
set remotetimeout 20
|
||||
shell start /B C:\Users\ooo\Desktop\MiCoder/OpenOCD/Win32/openocd_mico.exe -s ./ -f ./mico-os/makefiles/OpenOCD/interface/jlink_swd.cfg -f ./mico-os/makefiles/OpenOCD/mw3xx/mw3xx.cfg -f ./mico-os/makefiles/OpenOCD/mw3xx/mw3xx_gdb_jtag.cfg -l ./build/openocd_log.txt
|
||||
shell start /B F:\TC1\MiCoder_v1.3_Win32-64\MiCoder/OpenOCD/Win32/openocd_mico.exe -s ./ -f ./mico-os/makefiles/OpenOCD/interface/jlink_swd.cfg -f ./mico-os/makefiles/OpenOCD/mw3xx/mw3xx.cfg -f ./mico-os/makefiles/OpenOCD/mw3xx/mw3xx_gdb_jtag.cfg -l ./build/openocd_log.txt
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
source [find C:\Users\ooo\Desktop\MiCoder/OpenOCD/jlink_swd.cfg]
|
||||
source [find C:\Users\ooo\Desktop\MiCoder/OpenOCD/mw3xx.cfg]
|
||||
source [find C:\Users\ooo\Desktop\MiCoder/OpenOCD/mw3xx_gdb_jtag.cfg]
|
||||
source [find F:\TC1\MiCoder_v1.3_Win32-64\MiCoder/OpenOCD/jlink_swd.cfg]
|
||||
source [find F:\TC1\MiCoder_v1.3_Win32-64\MiCoder/OpenOCD/mw3xx.cfg]
|
||||
source [find F:\TC1\MiCoder_v1.3_Win32-64\MiCoder/OpenOCD/mw3xx_gdb_jtag.cfg]
|
||||
|
||||
@@ -36,6 +36,11 @@
|
||||
#include <http-strings.h>
|
||||
#include "stdlib.h"
|
||||
|
||||
#include <ctype.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "mico.h"
|
||||
#include "httpd_priv.h"
|
||||
#include "app_httpd.h"
|
||||
@@ -54,7 +59,9 @@ static bool is_handlers_registered;
|
||||
const struct httpd_wsgi_call g_app_handlers[];
|
||||
char power_info_json[2560] = {0};
|
||||
char up_time[16] = "00:00:00";
|
||||
#define OTA_BUFFER_SIZE 512 // 每次写入的缓存大小
|
||||
#define CHUNK_SIZE 512 // 每次发送 512 字节,避免 buffer 太大
|
||||
#define OTA_BUFFER_SIZE 512
|
||||
#define MAX_OTA_SIZE 1024*1024
|
||||
|
||||
/*
|
||||
void GetPraFromUrl(char* url, char* pra, char* val)
|
||||
@@ -91,27 +98,30 @@ void GetPraFromUrl(char* url, char* pra, char* val)
|
||||
}
|
||||
*/
|
||||
|
||||
static int HttpGetIndexPage(httpd_request_t *req) {
|
||||
|
||||
static OSStatus send_in_chunks(int sock, const uint8_t *data, int total_len) {
|
||||
OSStatus err = kNoErr;
|
||||
|
||||
err = httpd_send_all_header(req, HTTP_RES_200, sizeof(web_index_html), HTTP_CONTENT_HTML_ZIP);
|
||||
require_noerr_action(err, exit, http_log("ERROR: Unable to send http index headers."));
|
||||
|
||||
err = httpd_send_body(req->sock, web_index_html, sizeof(web_index_html));
|
||||
require_noerr_action(err, exit, http_log("ERROR: Unable to send http index body."));
|
||||
|
||||
exit:
|
||||
for (int offset = 0; offset < total_len; offset += CHUNK_SIZE) {
|
||||
int chunk_len = (total_len - offset > CHUNK_SIZE) ? CHUNK_SIZE : (total_len - offset);
|
||||
err = httpd_send_body(sock, data + offset, chunk_len);
|
||||
require_noerr_action(err, exit, http_log("ERROR: Send chunk failed at offset %d", offset));
|
||||
}
|
||||
exit:
|
||||
return err;
|
||||
}
|
||||
|
||||
static int HttpGetDemoPage(httpd_request_t *req) {
|
||||
static int HttpGetIndexPage(httpd_request_t *req) {
|
||||
OSStatus err = kNoErr;
|
||||
err = httpd_send_all_header(req, HTTP_RES_200, sizeof(web_index_html), HTTP_CONTENT_HTML_ZIP);
|
||||
require_noerr_action(err, exit, http_log("ERROR: Unable to send http demo headers."));
|
||||
int total_sz = sizeof(web_index_html);
|
||||
|
||||
err = httpd_send_body(req->sock, web_index_html, sizeof(web_index_html));
|
||||
require_noerr_action(err, exit, http_log("ERROR: Unable to send http demo body."));
|
||||
exit:
|
||||
|
||||
err = httpd_send_all_header(req, HTTP_RES_200, total_sz, HTTP_CONTENT_HTML_ZIP);
|
||||
require_noerr_action(err, exit, http_log("ERROR: Unable to send index headers."));
|
||||
|
||||
err = send_in_chunks(req->sock, web_index_html, total_sz);
|
||||
require_noerr_action(err, exit, http_log("ERROR: Unable to send index body."));
|
||||
|
||||
exit:
|
||||
return err;
|
||||
}
|
||||
|
||||
@@ -119,14 +129,15 @@ static int HttpGetAssets(httpd_request_t *req) {
|
||||
OSStatus err = kNoErr;
|
||||
|
||||
char *file_name = strstr(req->filename, "/assets/");
|
||||
if (!file_name) { http_log("HttpGetAssets url[%s] err", req->filename);
|
||||
if (!file_name) {
|
||||
http_log("HttpGetAssets url[%s] err", req->filename);
|
||||
return err;
|
||||
}
|
||||
//http_log("HttpGetAssets url[%s] file_name[%s]", req->filename, file_name);
|
||||
|
||||
int total_sz = 0;
|
||||
const unsigned char *file_data = NULL;
|
||||
const char *content_type = HTTP_CONTENT_JS_ZIP;
|
||||
|
||||
if (strcmp(file_name + 8, "js_pack.js") == 0) {
|
||||
total_sz = sizeof(js_pack);
|
||||
file_data = js_pack;
|
||||
@@ -134,24 +145,32 @@ static int HttpGetAssets(httpd_request_t *req) {
|
||||
total_sz = sizeof(css_pack);
|
||||
file_data = css_pack;
|
||||
content_type = HTTP_CONTENT_CSS_ZIP;
|
||||
} else if (strcmp(file_name + 8, "index.html") == 0) {
|
||||
total_sz = sizeof(web_index_html);
|
||||
file_data = web_index_html;
|
||||
content_type = HTTP_CONTENT_HTML_ZIP;
|
||||
}
|
||||
|
||||
if (total_sz == 0 || file_data == NULL) {
|
||||
http_log("File not found: %s", req->filename);
|
||||
return err;
|
||||
}
|
||||
|
||||
if (total_sz == 0) return err;
|
||||
|
||||
err = httpd_send_all_header(req, HTTP_RES_200, total_sz, content_type);
|
||||
require_noerr_action(err, exit, http_log("ERROR: Unable to send http assets headers."));
|
||||
require_noerr_action(err, exit, http_log("ERROR: Unable to send asset headers."));
|
||||
|
||||
err = httpd_send_body(req->sock, file_data, total_sz);
|
||||
require_noerr_action(err, exit, http_log("ERROR: Unable to send http assets body."));
|
||||
err = send_in_chunks(req->sock, file_data, total_sz);
|
||||
require_noerr_action(err, exit, http_log("ERROR: Unable to send asset body."));
|
||||
|
||||
exit:
|
||||
exit:
|
||||
return err;
|
||||
}
|
||||
|
||||
static int HttpGetTc1Status(httpd_request_t *req) {
|
||||
char *sockets = GetSocketStatus();
|
||||
char *short_click_config = GetButtonClickConfig();
|
||||
char *tc1_status = malloc(2048);
|
||||
char *tc1_status = malloc(1500);
|
||||
char *socket_names = malloc(512);
|
||||
sprintf(socket_names, "%s,%s,%s,%s,%s,%s",
|
||||
user_config->socket_names[0],
|
||||
@@ -243,62 +262,88 @@ static int HttpSetButtonEvent(httpd_request_t *req) {
|
||||
return err;
|
||||
}
|
||||
|
||||
static int HttpSetOTAFile(httpd_request_t *req) {
|
||||
#define OTA_BUF_SIZE 5120
|
||||
|
||||
static int HttpSetOTAFile(httpd_request_t *req)
|
||||
{
|
||||
tc1_log("[OTA] hdr_parsed=%d, remaining=%d, body_nbytes=%d, req.chunked=%d",
|
||||
req->hdr_parsed, req->remaining_bytes, req->body_nbytes, req->chunked);
|
||||
OSStatus err = kNoErr;
|
||||
uint32_t total = 0, ota_offset = 0;
|
||||
char *buffer = malloc(OTA_BUFFER_SIZE);
|
||||
if (!buffer) return kGeneralErr;
|
||||
|
||||
// mico_logic_partition_t* ota_partition = MicoFlashGetInfo(MICO_PARTITION_OTA_TEMP);
|
||||
// MicoFlashErase(MICO_PARTITION_OTA_TEMP, 0x0, ota_partition->partition_length);
|
||||
int total = 0;
|
||||
int ret = 0;
|
||||
|
||||
// req->chunked = 1;
|
||||
|
||||
int total1 = req->remaining_bytes;
|
||||
char *buffer = malloc(OTA_BUF_SIZE);
|
||||
if (!buffer) return kNoMemoryErr;
|
||||
uint32_t offset = 0;
|
||||
|
||||
mico_logic_partition_t* ota_partition = MicoFlashGetInfo(MICO_PARTITION_OTA_TEMP);
|
||||
MicoFlashErase(MICO_PARTITION_OTA_TEMP, 0x0, ota_partition->partition_length);
|
||||
CRC16_Context crc_context;
|
||||
CRC16_Init(&crc_context);
|
||||
// 尝试读取全部 POST 数据
|
||||
while (1) {
|
||||
ret = httpd_get_data2(req, buffer,OTA_BUF_SIZE);
|
||||
|
||||
tc1_log("開始接收 OTA 數據...");
|
||||
struct timeval timeout = {60, 0}; // 60秒
|
||||
setsockopt(req->sock, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));
|
||||
int remaining = -1;
|
||||
while (remaining!=0) {
|
||||
int readSize = remaining>OTA_BUFFER_SIZE?OTA_BUFFER_SIZE:(remaining>0?remaining:OTA_BUFFER_SIZE);
|
||||
remaining = httpd_get_data(req, buffer, readSize);
|
||||
if (remaining < 0) {
|
||||
err = kConnectionErr;
|
||||
tc1_log("httpd_get_data 失敗");
|
||||
goto exit;
|
||||
break;
|
||||
// ret = httpd_recv(req->sock, buffer, 128, 0);
|
||||
total += ret;
|
||||
// req->remaining_bytes -= ret;
|
||||
|
||||
if (ret > 0) {
|
||||
CRC16_Update(&crc_context, buffer, ret);
|
||||
err = MicoFlashWrite(MICO_PARTITION_OTA_TEMP, &offset, (uint8_t *)buffer, ret);
|
||||
require_noerr_quiet(err, exit);
|
||||
tc1_log("[OTA] 本次读取 %d 字节,累计 %d 字节", ret, total);
|
||||
}
|
||||
|
||||
// CRC16_Update(&crc_context, buffer, readSize);
|
||||
if (ret == 0 || req->remaining_bytes <= 0) {
|
||||
// 读取完毕
|
||||
tc1_log("[OTA] 数据读取完成, 总计 %d 字节", total);
|
||||
break;
|
||||
} else if (ret < 0) {
|
||||
tc1_log("[OTA] 数据读取失败, ret=%d", ret);
|
||||
err = kConnectionErr;
|
||||
break;
|
||||
}
|
||||
|
||||
mico_rtos_thread_msleep(100);
|
||||
|
||||
// err = MicoFlashWrite(MICO_PARTITION_OTA_TEMP, &crc_context->offset, (uint8_t *)buffer, readSize);
|
||||
// require_noerr_quiet(err, exit);
|
||||
total+=readSize;
|
||||
mico_thread_msleep(10);
|
||||
// tc1_log("[OTA] %x", buffer);
|
||||
// tc1_log("[OTA] hdr_parsed=%d, remaining=%d, body_nbytes=%d",
|
||||
// req->hdr_parsed, req->remaining_bytes, req->body_nbytes);
|
||||
}
|
||||
|
||||
// if (buffer) free(buffer);
|
||||
uint16_t crc16;
|
||||
// CRC16_Final(&crc_context, &crc16);
|
||||
char response[64];
|
||||
CRC16_Final(&crc_context, &crc16);
|
||||
|
||||
snprintf(response, sizeof(response), "OK, total: %ld bytes, CRC: 0x%04X", total, crc16);
|
||||
send_http(response, strlen(response), exit, &err);
|
||||
return 0;
|
||||
|
||||
err = mico_ota_switch_to_new_fw(ota_offset, crc16);
|
||||
err = mico_ota_switch_to_new_fw(total, crc16);
|
||||
tc1_log("[OTA] mico_ota_switch_to_new_fw err=%d", err);
|
||||
require_noerr(err, exit);
|
||||
|
||||
tc1_log("OTA 完成,重啟系統");
|
||||
mico_system_power_perform(mico_system_context_get(), eState_Software_Reset);
|
||||
char resp[128];
|
||||
snprintf(resp, sizeof(resp), "OK, total: %d bytes, req %d %d", total, req->body_nbytes, total1);
|
||||
send_http(resp, strlen(resp), exit, &err);
|
||||
|
||||
mico_system_power_perform(mico_system_context_get(), eState_Software_Reset);
|
||||
exit:
|
||||
if (req->sock >= 0) {
|
||||
close(req->sock);
|
||||
req->sock = -1;
|
||||
}
|
||||
if (buffer) free(buffer);
|
||||
tc1_log("OTA 結束,狀態: %d", err);
|
||||
return err;
|
||||
|
||||
// ota_file_req = req;
|
||||
|
||||
// OSStatus err = kNoErr;
|
||||
// err = mico_rtos_create_thread(NULL, MICO_APPLICATION_PRIORITY, "OtaFileThread", OtaFileThread, 0x1000, 0);
|
||||
// char buf[16] = {0};
|
||||
// sprintf(buf, "%d", sizeof(ota_file_req));
|
||||
// send_http(buf, strlen(buf), exit, &err);
|
||||
|
||||
// exit:
|
||||
// if (buf) free(buf);
|
||||
// return err;
|
||||
}
|
||||
|
||||
static int HttpSetDeviceName(httpd_request_t *req) {
|
||||
@@ -390,19 +435,62 @@ static int HttpGetWifiConfig(httpd_request_t *req) {
|
||||
return err;
|
||||
}
|
||||
|
||||
|
||||
// 单个十六进制字符转数字(安全)
|
||||
static int hex_char_to_int(char c) {
|
||||
if ('0' <= c && c <= '9') return c - '0';
|
||||
if ('a' <= c && c <= 'f') return c - 'a' + 10;
|
||||
if ('A' <= c && c <= 'F') return c - 'A' + 10;
|
||||
return -1;
|
||||
}
|
||||
|
||||
// 健壮版 URL 解码函数
|
||||
void url_decode(const char *src, char *dest, size_t max_len) {
|
||||
size_t i = 0;
|
||||
while (*src && i < max_len - 1) {
|
||||
if (*src == '%') {
|
||||
if (isxdigit((unsigned char)src[1]) && isxdigit((unsigned char)src[2])) {
|
||||
int high = hex_char_to_int(src[1]);
|
||||
int low = hex_char_to_int(src[2]);
|
||||
if (high >= 0 && low >= 0) {
|
||||
dest[i++] = (char)((high << 4) | low);
|
||||
src += 3;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
// 非法编码,跳过 %
|
||||
src++;
|
||||
} else if (*src == '+') {
|
||||
dest[i++] = ' ';
|
||||
src++;
|
||||
} else {
|
||||
dest[i++] = *src++;
|
||||
}
|
||||
}
|
||||
dest[i] = '\0';
|
||||
}
|
||||
|
||||
static int HttpSetWifiConfig(httpd_request_t *req) {
|
||||
OSStatus err = kNoErr;
|
||||
|
||||
int buf_size = 97;
|
||||
char *buf = malloc(buf_size);
|
||||
int mode = -1;
|
||||
char *wifi_ssid = malloc(32);
|
||||
char *wifi_key = malloc(32);
|
||||
char *buf = malloc(256);
|
||||
char *ssid_enc = malloc(128);
|
||||
char *key_enc = malloc(128);
|
||||
char *wifi_ssid = malloc(128);
|
||||
char *wifi_key = malloc(128);
|
||||
int mode = -1;
|
||||
|
||||
err = httpd_get_data(req, buf, buf_size);
|
||||
|
||||
|
||||
err = httpd_get_data(req, buf, 256);
|
||||
require_noerr(err, exit);
|
||||
|
||||
sscanf(buf, "%d %s %s", &mode, wifi_ssid, wifi_key);
|
||||
// 假设 httpd_get_data(req, buf, 256);
|
||||
// tc1_log("wifi config %s",buf);
|
||||
sscanf(buf, "%d %s %s", &mode, ssid_enc, key_enc);
|
||||
// tc1_log("wifi config %s %s",ssid_enc,key_enc);
|
||||
url_decode(ssid_enc, wifi_ssid,128);
|
||||
url_decode(key_enc, wifi_key,128);
|
||||
// tc1_log("wifi config decode %s %s",wifi_ssid,wifi_key);
|
||||
if (mode == 1) {
|
||||
WifiConnect(wifi_ssid, wifi_key);
|
||||
} else {
|
||||
@@ -462,7 +550,10 @@ static int HttpSetMqttConfig(httpd_request_t *req) {
|
||||
|
||||
sscanf(buf, "%s %d %s %s", MQTT_SERVER, &MQTT_SERVER_PORT, MQTT_SERVER_USR, MQTT_SERVER_PWD);
|
||||
mico_system_context_update(sys_config);
|
||||
|
||||
if (!(MQTT_SERVER[0] < 0x20 || MQTT_SERVER[0] > 0x7f || MQTT_SERVER_PORT < 1)){
|
||||
err = UserMqttInit();
|
||||
require_noerr(err, exit);
|
||||
}
|
||||
send_http("OK", 2, exit, &err);
|
||||
|
||||
exit:
|
||||
@@ -667,7 +758,6 @@ static int OtaStart(httpd_request_t *req) {
|
||||
|
||||
const struct httpd_wsgi_call g_app_handlers[] = {
|
||||
{"/", HTTPD_HDR_DEFORT, 0, HttpGetIndexPage, NULL, NULL, NULL},
|
||||
{"/demo", HTTPD_HDR_DEFORT, 0, HttpGetDemoPage, NULL, NULL, NULL},
|
||||
{"/assets", HTTPD_HDR_ADD_SERVER |
|
||||
HTTPD_HDR_ADD_CONN_CLOSE, APP_HTTP_FLAGS_NO_EXACT_MATCH, HttpGetAssets, NULL, NULL, NULL},
|
||||
{"/socket", HTTPD_HDR_DEFORT, 0, NULL, HttpSetSocketStatus, NULL, NULL},
|
||||
|
||||
@@ -73,7 +73,7 @@
|
||||
|
||||
#define POWER_INFO_JSON "{'sockets':'%s','idx':%d,'len':%d,'p_count':%ld,'powers':[%s],'up_time':'%s','led_enabled':%d,'total_switch_on':%d,'socketNames':'%s','p_count_1_day_ago':%d,'p_count_2_days_ago':%d,'child_lock_enabled':%d,'deviceName':'%s','btnClicks':%s}"
|
||||
|
||||
int AppHttpdStart(void);
|
||||
extern int AppHttpdStart(void);
|
||||
|
||||
int AppHttpdStop();
|
||||
extern int AppHttpdStop();
|
||||
|
||||
|
||||
@@ -31,7 +31,7 @@
|
||||
<div class="demo-layout mdl-layout mdl-js-layout mdl-layout--fixed-drawer mdl-layout--fixed-header">
|
||||
<header class="demo-header mdl-layout__header">
|
||||
<div class="mdl-layout__header-row">
|
||||
<button class="mdl-button mdl-js-button mdl-js-ripple-effect mdl-button--icon edit-device-name">
|
||||
<button class="mdl-button mdl-button--icon edit-device-name">
|
||||
<i class="material-icons">
|
||||
<svg>
|
||||
<use xlink:href="#icon-edit"/>
|
||||
@@ -40,22 +40,22 @@
|
||||
</button>
|
||||
<span class="mdl-layout-title">TC1智能插座</span>
|
||||
<div class="mdl-layout-spacer"></div>
|
||||
<!-- <button class="mdl-button mdl-js-button mdl-js-ripple-effect mdl-button--icon"-->
|
||||
<!-- id="hdrbtn">-->
|
||||
<!-- <i class="material-icons">-->
|
||||
<!-- <svg>-->
|
||||
<!-- <use xlink:href="#icon-translate"/>-->
|
||||
<!-- </svg>-->
|
||||
<!-- </i>-->
|
||||
<!-- </button>-->
|
||||
<!-- <ul class="mdl-menu mdl-js-menu mdl-js-ripple-effect mdl-menu--bottom-right"-->
|
||||
<!-- for="hdrbtn">-->
|
||||
<!-- <li class="mdl-menu__item" onclick="ChangeLanguage('en')">English</li>-->
|
||||
<!-- <li class="mdl-menu__item" onclick="ChangeLanguage('cn')">中文</li>-->
|
||||
<!-- <li class="mdl-menu__item" onclick="ChangeLanguage('jp')">日本語</li>-->
|
||||
<!-- </ul>-->
|
||||
<!-- <button class="mdl-button mdl-button--icon"-->
|
||||
<!-- id="hdrbtn">-->
|
||||
<!-- <i class="material-icons">-->
|
||||
<!-- <svg>-->
|
||||
<!-- <use xlink:href="#icon-translate"/>-->
|
||||
<!-- </svg>-->
|
||||
<!-- </i>-->
|
||||
<!-- </button>-->
|
||||
<!-- <ul class="mdl-menu mdl-js-menu mdl-js-ripple-effect mdl-menu--bottom-right"-->
|
||||
<!-- for="hdrbtn">-->
|
||||
<!-- <li class="mdl-menu__item" onclick="ChangeLanguage('en')">English</li>-->
|
||||
<!-- <li class="mdl-menu__item" onclick="ChangeLanguage('cn')">中文</li>-->
|
||||
<!-- <li class="mdl-menu__item" onclick="ChangeLanguage('jp')">日本語</li>-->
|
||||
<!-- </ul>-->
|
||||
<button onclick="reboot()"
|
||||
class="mdl-button mdl-js-button mdl-js-ripple-effect mdl-button--icon"
|
||||
class="mdl-button mdl-button--icon"
|
||||
id="rebootbtn">
|
||||
<i class="material-icons">
|
||||
<svg>
|
||||
@@ -176,7 +176,7 @@
|
||||
</li>
|
||||
<li class="mdl-list__item">
|
||||
<span class="mdl-list__item-primary-content">
|
||||
<button class="mdl-button mdl-js-button mdl-js-ripple-effect mdl-button--icon edit-socket-name">
|
||||
<button class="mdl-button mdl-button--icon edit-socket-name">
|
||||
<i class="material-icons">
|
||||
<svg>
|
||||
<use xlink:href="#icon-edit"/>
|
||||
@@ -195,7 +195,7 @@
|
||||
</li>
|
||||
<li class="mdl-list__item">
|
||||
<span class="mdl-list__item-primary-content">
|
||||
<button class="mdl-button mdl-js-button mdl-js-ripple-effect mdl-button--icon edit-socket-name">
|
||||
<button class="mdl-button mdl-button--icon edit-socket-name">
|
||||
<i class="material-icons">
|
||||
<svg>
|
||||
<use xlink:href="#icon-edit"/>
|
||||
@@ -214,7 +214,7 @@
|
||||
</li>
|
||||
<li class="mdl-list__item">
|
||||
<span class="mdl-list__item-primary-content">
|
||||
<button class="mdl-button mdl-js-button mdl-js-ripple-effect mdl-button--icon edit-socket-name">
|
||||
<button class="mdl-button mdl-button--icon edit-socket-name">
|
||||
<i class="material-icons">
|
||||
<svg>
|
||||
<use xlink:href="#icon-edit"/>
|
||||
@@ -233,7 +233,7 @@
|
||||
</li>
|
||||
<li class="mdl-list__item">
|
||||
<span class="mdl-list__item-primary-content">
|
||||
<button class="mdl-button mdl-js-button mdl-js-ripple-effect mdl-button--icon edit-socket-name">
|
||||
<button class="mdl-button mdl-button--icon edit-socket-name">
|
||||
<i class="material-icons">
|
||||
<svg>
|
||||
<use xlink:href="#icon-edit"/>
|
||||
@@ -252,7 +252,7 @@
|
||||
</li>
|
||||
<li class="mdl-list__item">
|
||||
<span class="mdl-list__item-primary-content">
|
||||
<button class="mdl-button mdl-js-button mdl-js-ripple-effect mdl-button--icon edit-socket-name">
|
||||
<button class="mdl-button mdl-button--icon edit-socket-name">
|
||||
<i class="material-icons">
|
||||
<svg>
|
||||
<use xlink:href="#icon-edit"/>
|
||||
@@ -271,7 +271,7 @@
|
||||
</li>
|
||||
<li class="mdl-list__item">
|
||||
<span class="mdl-list__item-primary-content">
|
||||
<button class="mdl-button mdl-js-button mdl-js-ripple-effect mdl-button--icon edit-socket-name">
|
||||
<button class="mdl-button mdl-button--icon edit-socket-name">
|
||||
<i class="material-icons">
|
||||
<svg>
|
||||
<use xlink:href="#icon-edit"/>
|
||||
@@ -291,30 +291,30 @@
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="page page1 mdl-cell mdl-cell--4-col mdl-cell--8-col-tablet mdl-grid mdl-grid--no-spacing">
|
||||
<div class="demo-options mdl-card mdl-shadow--2dp mdl-cell mdl-cell--4-col mdl-cell--3-col-tablet mdl-cell--12-col-desktop">
|
||||
<div class="mdl-card__supporting-text">
|
||||
<div class="mdl-card__title mdl-card--expand">
|
||||
<h2 class="mdl-card__title-text">功率图</h2>
|
||||
</div>
|
||||
<div class="mdl-card__title mdl-card--expand">
|
||||
<h2 class="mdl-card__title-text">功率图</h2>
|
||||
</div>
|
||||
|
||||
<div id="ct-chart-par"
|
||||
class="page page1 mdl-card__supporting-text mdl-shadow--2dp"
|
||||
style="height:315px;overflow-x:scroll;overflow-y:hidden;">
|
||||
<table class="pw">
|
||||
<tr>
|
||||
<td>当前功率: <span id="p" class="success">0</span> W</td>
|
||||
<td>今日电量: <span id="w_t" class="error">0</span> kW·h</td>
|
||||
<td>昨日电量: <span id="w_y" class="error">0</span> kW·h</td>
|
||||
<td>总电量: <span id="w" class="error">0</span> kW·h</td>
|
||||
</tr>
|
||||
</table>
|
||||
<div id="ct-chart" class="ct-chart ct-perfect-fourth"
|
||||
style="height:280px;margin-top:40px;"></div>
|
||||
</div>
|
||||
</div>
|
||||
<div id="ct-chart-par"
|
||||
class="page page1 mdl-card__supporting-text mdl-shadow--2dp"
|
||||
style="height:315px;overflow-x:scroll;overflow-y:hidden;">
|
||||
<table class="pw">
|
||||
<tr>
|
||||
<td>当前功率: <span id="p" class="success">0</span> W</td>
|
||||
<td>今日电量: <span id="w_t" class="error">0</span> kW·h</td>
|
||||
<td>昨日电量: <span id="w_y" class="error">0</span> kW·h</td>
|
||||
<td>总电量: <span id="w" class="error">0</span> kW·h</td>
|
||||
</tr>
|
||||
</table>
|
||||
<div id="ct-chart" class="ct-chart ct-perfect-fourth"
|
||||
style="height:280px;margin-top:40px;"></div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -368,10 +368,10 @@
|
||||
</form>
|
||||
</div>
|
||||
<div class="mdl-card__actions mdl-card--border">
|
||||
<a id="wifi_submit"
|
||||
class="mdl-button mdl-button--colored mdl-js-button mdl-js-ripple-effect">
|
||||
<button type="button" id="wifi_submit"
|
||||
class="mdl-button mdl-button--colored ">
|
||||
提交
|
||||
</a>
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -401,10 +401,10 @@
|
||||
</form>
|
||||
</div>
|
||||
<div class="mdl-card__actions mdl-card--border">
|
||||
<a id="mqtt_submit"
|
||||
class="mdl-button mdl-button--colored mdl-js-button mdl-js-ripple-effect">
|
||||
<button type="button" id="mqtt_submit"
|
||||
class="mdl-button mdl-button--colored ">
|
||||
提交
|
||||
</a>
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -422,10 +422,10 @@
|
||||
</form>
|
||||
</div>
|
||||
<div class="mdl-card__actions mdl-card--border">
|
||||
<a id="mqtt_freq_submit"
|
||||
class="mdl-button mdl-button--colored mdl-js-button mdl-js-ripple-effect">
|
||||
<button type="button" id="mqtt_freq_submit"
|
||||
class="mdl-button mdl-button--colored ">
|
||||
提交
|
||||
</a>
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -438,20 +438,6 @@
|
||||
<th>循环</th>
|
||||
<th>操作</th>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>02-15 07:11</td>
|
||||
<td>1</td>
|
||||
<td>0</td>
|
||||
<td>0</td>
|
||||
<td><a>删除</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>2020-02-15<br>07:11:08</td>
|
||||
<td>1</td>
|
||||
<td>0</td>
|
||||
<td>6</td>
|
||||
<td><a>删除</a></td>
|
||||
</tr>
|
||||
</table>
|
||||
</div>
|
||||
|
||||
@@ -519,10 +505,10 @@
|
||||
</form>
|
||||
</div>
|
||||
<div class="mdl-card__actions mdl-card--border">
|
||||
<a href="javascript:AddTimedTask();"
|
||||
class="mdl-button mdl-button--colored mdl-js-button mdl-js-ripple-effect">
|
||||
<button type="button" onclick="AddTimedTask();"
|
||||
class="mdl-button mdl-button--colored ">
|
||||
提交
|
||||
</a>
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -539,7 +525,7 @@
|
||||
<th>操作</th>
|
||||
</tr>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="page page4 over-unset mdl-cell mdl-cell--5-col demo-card-square mdl-card mdl-shadow--2dp"
|
||||
style="z-index: unset;">
|
||||
@@ -598,10 +584,10 @@
|
||||
</form>
|
||||
</div>
|
||||
<div class="mdl-card__actions mdl-card--border">
|
||||
<a href="javascript:addButtonEvent();"
|
||||
class="mdl-button mdl-button--colored mdl-js-button mdl-js-ripple-effect">
|
||||
<button type="button" onclick="addButtonEvent();"
|
||||
class="mdl-button mdl-button--colored ">
|
||||
添加
|
||||
</a>
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -610,23 +596,23 @@
|
||||
<table class="mdl-data-table mdl-js-data-table">
|
||||
<tr>
|
||||
<th>版本</th>
|
||||
<th id="info_version">v1.0.33</th>
|
||||
<th id="info_version"></th>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>IP</td>
|
||||
<td id="info_ip">192.168.33.222</td>
|
||||
<td id="info_ip"></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>子网掩码</td>
|
||||
<td id="info_mask">255.255.255.0</td>
|
||||
<td id="info_mask"></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>网关</td>
|
||||
<td id="info_gateway">192.168.33.1</td>
|
||||
<td id="info_gateway"></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>启动时间</td>
|
||||
<td id="uptime">10:13:43</td>
|
||||
<td id="uptime"></td>
|
||||
</tr>
|
||||
</table>
|
||||
<div class="mdl-card__actions mdl-card--border" style="display: flex; align-items: center; flex-wrap: wrap;">
|
||||
@@ -638,7 +624,7 @@
|
||||
</svg>
|
||||
</i>
|
||||
<!-- <span>2020-02-22</span> -->
|
||||
<a class="mdl-button" id="st-date">2020-02-22</a>
|
||||
<a class="mdl-button" id="st-date"></a>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -655,10 +641,10 @@
|
||||
</form>
|
||||
</div>
|
||||
<div class="mdl-card__actions mdl-card--border">
|
||||
<a href="javascript:OtaStart();"
|
||||
class="mdl-button mdl-button--colored mdl-js-button mdl-js-ripple-effect">
|
||||
<button type="button" onclick="OtaStart();" id="submit-ota-link"
|
||||
class="mdl-button mdl-button--colored ">
|
||||
提交
|
||||
</a>
|
||||
</button>
|
||||
</div>
|
||||
<div id="ota_status" class="mdl-progress mdl-js-progress"></div>
|
||||
</div>
|
||||
@@ -675,10 +661,11 @@
|
||||
</form>
|
||||
</div>
|
||||
<div class="mdl-card__actions mdl-card--border">
|
||||
<a href="javascript:OtaFileUpload();" class="mdl-button mdl-button--colored mdl-js-button mdl-js-ripple-effect">
|
||||
<button type="button" onclick="OtaFileUpload();" class="mdl-button mdl-button--colored " id="submit-ota-file">
|
||||
上传 OTA 文件
|
||||
</a>
|
||||
</button>
|
||||
</div>
|
||||
<div id="upload_status" class="mdl-progress mdl-js-progress"></div>
|
||||
</div>
|
||||
|
||||
<div class="page page7 mdl-cell mdl-cell--12-col demo-card-square mdl-card mdl-shadow--2dp">
|
||||
@@ -689,10 +676,10 @@
|
||||
<pre id="sys_log"></pre>
|
||||
</div>
|
||||
<div class="mdl-card__actions mdl-card--border">
|
||||
<a href="javascript:GetSysLog();"
|
||||
class="mdl-button mdl-button--colored mdl-js-button mdl-js-ripple-effect">
|
||||
<button type="button" onclick="GetSysLog();"
|
||||
class="mdl-button mdl-button--colored ">
|
||||
刷新
|
||||
</a>
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -975,6 +962,7 @@
|
||||
BTN_OPERATIONS.push("重新配网");
|
||||
BTN_OPERATIONS.push("重置系统");
|
||||
BTN_OPERATIONS.push("切换童锁");
|
||||
BTN_OPERATIONS.push("重启网页");
|
||||
|
||||
$('#btn_action_selector').append(
|
||||
$('<li>')
|
||||
@@ -1234,15 +1222,15 @@ componentHandler.upgradeDom();
|
||||
var mode = $("#custom_station").prop("checked") ? 1 : 0;
|
||||
var ssid = $("#custom_ssid").val();
|
||||
var passwd = $("#custom_password").val();
|
||||
if (ContainQM(ssid) || ContainQM(passwd)) {
|
||||
alert(qm_mess);
|
||||
return;
|
||||
}
|
||||
//if (ContainQM(ssid) || ContainQM(passwd)) {
|
||||
// alert(qm_mess);
|
||||
// return;
|
||||
//}
|
||||
if (passwd.length < 8) {
|
||||
alert(le_mess);
|
||||
return;
|
||||
}
|
||||
var params = mode + " " + ssid + " " + passwd;
|
||||
var params = mode + " " + encodeURIComponent(ssid) + " " + encodeURIComponent(passwd);
|
||||
HttpPost("/wifi/config", function (re) {
|
||||
ShowToast(re);
|
||||
}, params);
|
||||
@@ -1503,27 +1491,50 @@ HttpPost("/shortClickEvent", function (re) {
|
||||
var ota_url = document.getElementById("ota_url").value;
|
||||
var protocol = window.location.protocol;
|
||||
var baseUrl = protocol+"//"+window.location.host;
|
||||
|
||||
document.getElementById("submit-ota-link").disabled = true;
|
||||
HttpPost("/ota", function (re) {
|
||||
OtaStatus();
|
||||
}, ota_url);
|
||||
}
|
||||
|
||||
var upload_status = document.querySelector('#upload_status');
|
||||
upload_status.addEventListener('mdl-componentupgraded', function() {
|
||||
this.MaterialProgress.setProgress(0);
|
||||
});
|
||||
function OtaFileUpload() {
|
||||
//alert("假的假的是假的,忽略");
|
||||
//return;
|
||||
var fileInput = document.getElementById("ota_file");
|
||||
if (fileInput.files.length === 0) {
|
||||
alert("请选择要上传的 OTA 文件");
|
||||
ShowToast("请选择要上传的 OTA 文件");
|
||||
return;
|
||||
}
|
||||
clearTimeout(powerTimerId);
|
||||
fetch("/ota/fileUpload", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/octet-stream" },
|
||||
body: fileInput.files[0]
|
||||
}).then(r => r.text()).then(alert).catch(alert);
|
||||
}
|
||||
console.log(fileInput.files[0].size); // 应显示 647168 左右
|
||||
document.getElementById("submit-ota-file").disabled = true;
|
||||
const xhr = new XMLHttpRequest();
|
||||
xhr.open("POST", "/ota/fileUpload");
|
||||
|
||||
xhr.upload.onprogress = function (e) {
|
||||
if (e.lengthComputable) {
|
||||
let percent = (e.loaded / e.total * 100).toFixed(1);
|
||||
upload_status.MaterialProgress.setProgress(percent);
|
||||
console.log(`上传进度:${percent}%`);
|
||||
}
|
||||
};
|
||||
|
||||
xhr.onload = function () {
|
||||
alert("上传完成:" + xhr.responseText);
|
||||
document.getElementById("submit-ota-file").disabled = false;
|
||||
};
|
||||
|
||||
xhr.onerror = function () {
|
||||
alert("上传失败");
|
||||
document.getElementById("submit-ota-file").disabled = false;
|
||||
};
|
||||
|
||||
xhr.setRequestHeader("Content-Type", "application/octet-stream");
|
||||
xhr.send(fileInput.files[0]);
|
||||
}
|
||||
|
||||
var ota_status = document.querySelector('#ota_status');
|
||||
ota_status.addEventListener('mdl-componentupgraded', function() {
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -2,6 +2,8 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdarg.h>
|
||||
#include "mico.h"
|
||||
|
||||
|
||||
#include"http_server/web_log.h"
|
||||
|
||||
@@ -36,6 +38,9 @@ char* GetLogRecord()
|
||||
tmp += strlen(tmp);
|
||||
if (!log_record.logs[i%LOG_NUM]) continue;
|
||||
sprintf(tmp, "%s\n", log_record.logs[i%LOG_NUM]);
|
||||
if(i == log_record.idx){
|
||||
sprintf(tmp, "%s\nFreeMem %d bytes\n",log_record.logs[i%LOG_NUM],MicoGetMemoryInfo()->free_memory);
|
||||
}
|
||||
}
|
||||
return log_record_str;
|
||||
}
|
||||
|
||||
@@ -10,7 +10,6 @@
|
||||
#include "user_wifi.h"
|
||||
#include "time_server/user_rtc.h"
|
||||
#include "user_power.h"
|
||||
#include "mqtt_server/user_mqtt_client.h"
|
||||
#include "http_server/app_httpd.h"
|
||||
#include "timed_task/timed_task.h"
|
||||
|
||||
@@ -186,8 +185,6 @@ int application_start(void) {
|
||||
}
|
||||
}
|
||||
KeyInit();
|
||||
err = UserMqttInit();
|
||||
require_noerr(err, exit);
|
||||
err = UserRtcInit();
|
||||
require_noerr(err, exit);
|
||||
PowerInit();
|
||||
@@ -197,12 +194,12 @@ int application_start(void) {
|
||||
|
||||
err = mico_rtos_create_thread(NULL, MICO_APPLICATION_PRIORITY, "p_count",
|
||||
(mico_thread_function_t) schedule_p_count_task,
|
||||
0x2000, 0);
|
||||
0x800, 0);
|
||||
require_noerr_string(err, exit, "ERROR: Unable to start the p_count thread.");
|
||||
|
||||
err = mico_rtos_create_thread(NULL, MICO_APPLICATION_PRIORITY, "mqtt_power_report",
|
||||
(mico_thread_function_t) reportMqttPowerInfoThread,
|
||||
0x2000, 0);
|
||||
0x800, 0);
|
||||
require_noerr_string(err, exit, "ERROR: Unable to start the mqtt_power_report thread.");
|
||||
|
||||
|
||||
@@ -211,7 +208,6 @@ int application_start(void) {
|
||||
if (user_config->task_top && now >= user_config->task_top->prs_time) {
|
||||
ProcessTask();
|
||||
}
|
||||
|
||||
mico_thread_msleep(1000);
|
||||
}
|
||||
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
#define wifi_log(M, ...) custom_log("WIFI", M, ##__VA_ARGS__); web_log("WIFI", M, ##__VA_ARGS__);
|
||||
#define power_log(M, ...) custom_log("POWER", M, ##__VA_ARGS__); web_log("POWER", M, ##__VA_ARGS__);
|
||||
|
||||
#define VERSION "v2.1.6"
|
||||
#define VERSION "v2.2.0"
|
||||
|
||||
#define TYPE 1
|
||||
#define TYPE_NAME "TC1"
|
||||
|
||||
@@ -53,6 +53,7 @@ mico_queue_t mqtt_msg_send_queue = NULL;
|
||||
|
||||
Client c; // mqtt client object
|
||||
Network n; // socket network for mqtt client
|
||||
volatile bool mqtt_thread_should_exit = false;
|
||||
|
||||
static mico_worker_thread_t mqtt_client_worker_thread; /* Worker thread to manage send/recv events */
|
||||
//static mico_timed_event_t mqtt_client_send_event;
|
||||
@@ -98,10 +99,32 @@ void UserMqttTimerFunc(void *arg) {
|
||||
}
|
||||
}
|
||||
|
||||
OSStatus UserMqttDeInit(void) {
|
||||
OSStatus err = kNoErr;
|
||||
|
||||
// 1. 请求线程退出
|
||||
mqtt_thread_should_exit = true;
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
void clear_mqtt_msg_send_queue(void) {
|
||||
if(mqtt_msg_send_queue == NULL){
|
||||
return;
|
||||
}
|
||||
void *msg = NULL;
|
||||
while (mico_rtos_is_queue_empty(&mqtt_msg_send_queue) == false) {
|
||||
if (mico_rtos_pop_from_queue(&mqtt_msg_send_queue, &msg, 0) == kNoErr) {
|
||||
if (msg) free(msg); // 释放消息内存,避免泄漏
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Application entrance */
|
||||
OSStatus UserMqttInit(void) {
|
||||
OSStatus err = kNoErr;
|
||||
|
||||
if(mqtt_msg_send_queue != NULL)
|
||||
return err;
|
||||
sprintf(topic_set, MQTT_CLIENT_SUB_TOPIC1);
|
||||
sprintf(topic_state, MQTT_CLIENT_PUB_TOPIC, str_mac);
|
||||
//TODO size:0x800
|
||||
@@ -136,13 +159,32 @@ OSStatus UserMqttInit(void) {
|
||||
static OSStatus UserMqttClientRelease(Client *c, Network *n) {
|
||||
OSStatus err = kNoErr;
|
||||
|
||||
if (c->isconnected) MQTTDisconnect(c);
|
||||
if (c == NULL || n == NULL) return kParamErr;
|
||||
|
||||
n->disconnect(n); // close connection
|
||||
if (c->isconnected) {
|
||||
MQTTDisconnect(c);
|
||||
c->isconnected = 0;
|
||||
}
|
||||
|
||||
if (MQTT_SUCCESS != MQTTClientDeinit(c)) { mqtt_log("MQTTClientDeinit failed!");
|
||||
if (c->buf) {
|
||||
free(c->buf);
|
||||
c->buf = NULL;
|
||||
}
|
||||
|
||||
if (c->readbuf) {
|
||||
free(c->readbuf);
|
||||
c->readbuf = NULL;
|
||||
}
|
||||
|
||||
if (n->disconnect) {
|
||||
n->disconnect(n);
|
||||
}
|
||||
|
||||
if (MQTT_SUCCESS != MQTTClientDeinit(c)) {
|
||||
mqtt_log("MQTTClientDeinit failed!");
|
||||
err = kDeletedErr;
|
||||
}
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
@@ -177,6 +219,9 @@ static OSStatus MqttMsgPublish(Client *c, const char *topic, char qos, char reta
|
||||
}
|
||||
|
||||
void registerMqttEvents(void) {
|
||||
if(timer_status !=0){
|
||||
mico_stop_timer(&timer_handle);
|
||||
}
|
||||
timer_status = 0;
|
||||
mico_start_timer(&timer_handle);
|
||||
}
|
||||
@@ -202,16 +247,18 @@ void MqttClientThread(mico_thread_arg_t arg) {
|
||||
|
||||
/* create msg send queue event fd */
|
||||
msg_send_event_fd = mico_create_event_fd(mqtt_msg_send_queue);
|
||||
mico_init_timer(&timer_handle, 150, UserMqttTimerFunc, &arg);
|
||||
|
||||
require_action(msg_send_event_fd >= 0, exit,
|
||||
mqtt_log("ERROR: create msg send queue event fd failed!!!"));
|
||||
|
||||
mqtt_thread_should_exit = false;
|
||||
MQTT_start:
|
||||
|
||||
isconnect = false;
|
||||
/* 1. create network connection */
|
||||
ssl_settings.ssl_enable = false;
|
||||
LinkStatusTypeDef LinkStatus;
|
||||
while (1) {
|
||||
while (!mqtt_thread_should_exit) {
|
||||
isconnect = false;
|
||||
mico_rtos_thread_sleep(3);
|
||||
if (MQTT_SERVER[0] < 0x20 || MQTT_SERVER[0] > 0x7f || MQTT_SERVER_PORT < 1)
|
||||
@@ -228,7 +275,8 @@ void MqttClientThread(mico_thread_arg_t arg) {
|
||||
if (rc == MQTT_SUCCESS) break;
|
||||
|
||||
//mqtt_log("ERROR: MQTT network connect err=%d, reconnect after 3s...", rc);
|
||||
}mqtt_log("MQTT network connect success!");
|
||||
}
|
||||
mqtt_log("MQTT network connect success!");
|
||||
|
||||
/* 2. init mqtt client */
|
||||
//c.heartbeat_retry_max = 2;
|
||||
@@ -249,7 +297,7 @@ void MqttClientThread(mico_thread_arg_t arg) {
|
||||
rc = MQTTConnect(&c, &connectData);
|
||||
require_noerr_string(rc, MQTT_reconnect, "ERROR: MQTT client connect err.");
|
||||
|
||||
mqtt_log("MQTT client connect success!");
|
||||
mqtt_log("MQTT client connect success, result: %d ", rc);
|
||||
|
||||
UserLedSet(RelayOut() && user_config->power_led_enabled);
|
||||
|
||||
@@ -269,10 +317,9 @@ void MqttClientThread(mico_thread_arg_t arg) {
|
||||
UserMqttSendTotalSocketState();
|
||||
UserMqttSendChildLockState();
|
||||
|
||||
mico_init_timer(&timer_handle, 150, UserMqttTimerFunc, &arg);
|
||||
registerMqttEvents();
|
||||
/* 5. client loop for recv msg && keepalive */
|
||||
while (1) {
|
||||
while (!mqtt_thread_should_exit) {
|
||||
isconnect = true;
|
||||
no_mqtt_msg_exchange = true;
|
||||
FD_ZERO(&readfds);
|
||||
@@ -298,13 +345,12 @@ void MqttClientThread(mico_thread_arg_t arg) {
|
||||
err = MqttMsgPublish(&c, p_send_msg->topic, p_send_msg->qos, p_send_msg->retained,
|
||||
(const unsigned char *) p_send_msg->data,
|
||||
p_send_msg->datalen);
|
||||
|
||||
free(p_send_msg);
|
||||
p_send_msg = NULL;
|
||||
require_noerr_string(err, MQTT_reconnect, "ERROR: MQTT publish data err");
|
||||
|
||||
//mqtt_log("MQTT publish data success! send_topic=[%s], msg=[%ld].", p_send_msg->topic, p_send_msg->datalen);
|
||||
no_mqtt_msg_exchange = false;
|
||||
free(p_send_msg);
|
||||
p_send_msg = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -320,7 +366,7 @@ void MqttClientThread(mico_thread_arg_t arg) {
|
||||
mqtt_log("Disconnect MQTT client, and reconnect after 5s, reason: mqtt_rc = %d, err = %d", rc, err);
|
||||
|
||||
timer_status = 100;
|
||||
|
||||
clear_mqtt_msg_send_queue();
|
||||
UserMqttClientRelease(&c, &n);
|
||||
isconnect = false;
|
||||
UserLedSet(-1);
|
||||
@@ -329,10 +375,12 @@ mqtt_log("Disconnect MQTT client, and reconnect after 5s, reason: mqtt_rc = %d,
|
||||
mico_rtos_thread_sleep(5);
|
||||
goto MQTT_start;
|
||||
|
||||
exit:
|
||||
isconnect = false;mqtt_log("EXIT: MQTT client exit with err = %d.", err);
|
||||
exit:
|
||||
isconnect = false;
|
||||
mqtt_log("EXIT: MQTT client exit with err = %d.", err);
|
||||
UserMqttClientRelease(&c, &n);
|
||||
mico_rtos_delete_thread(NULL);
|
||||
mico_rtos_delete_thread(NULL); // 自删
|
||||
return;
|
||||
}
|
||||
|
||||
// callback, msg received from mqtt server
|
||||
@@ -423,7 +471,9 @@ void ProcessHaCmd(char *cmd) {
|
||||
childLockEnabled = on;
|
||||
UserMqttSendChildLockState();
|
||||
mico_system_context_update(sys_config);
|
||||
}else if (strcmp(cmd, "reboot") == 0) {
|
||||
}else if (strcmp(cmd, "reboot") == ' ') {
|
||||
sscanf(cmd, "reboot %s", mac);
|
||||
if (strcmp(mac, str_mac)) return;
|
||||
MicoSystemReboot(); // 立即重启设备
|
||||
}
|
||||
}
|
||||
@@ -431,6 +481,9 @@ void ProcessHaCmd(char *cmd) {
|
||||
OSStatus UserMqttSendTopic(char *topic, char *arg, char retained) {
|
||||
OSStatus err = kUnknownErr;
|
||||
p_mqtt_send_msg_t p_send_msg = NULL;
|
||||
if(mqtt_msg_send_queue == NULL|| !isconnect){
|
||||
return err;
|
||||
}
|
||||
|
||||
// mqtt_log("======App prepare to send ![%d]======", MicoGetMemoryInfo()->free_memory);
|
||||
|
||||
@@ -533,7 +586,7 @@ void UserMqttHassAuto(char socket_id) {
|
||||
socket_id--;
|
||||
char *send_buf = NULL;
|
||||
char *topic_buf = NULL;
|
||||
send_buf = (char *) malloc(800);
|
||||
send_buf = (char *) malloc(600);
|
||||
topic_buf = (char *) malloc(64);
|
||||
if (send_buf != NULL && topic_buf != NULL) {
|
||||
sprintf(topic_buf, "homeassistant/switch/%s/socket_%d/config", str_mac, socket_id);
|
||||
@@ -545,6 +598,7 @@ void UserMqttHassAuto(char socket_id) {
|
||||
"\"cmd_t\":\"device/ztc1/set\","
|
||||
"\"pl_on\":\"set socket %s %d 1\","
|
||||
"\"pl_off\":\"set socket %s %d 0\","
|
||||
"\"device_class\":\"outlet\","
|
||||
"\"device\":{"
|
||||
"\"identifiers\":[\"tc1_%s\"],"
|
||||
"\"name\":\"%s\","
|
||||
@@ -574,13 +628,13 @@ void UserMqttHassAutoRebootButton(void) {
|
||||
"\"uniq_id\":\"tc1_%s_reboot\","
|
||||
"\"object_id\":\"tc1_%s_reboot\","
|
||||
"\"cmd_t\":\"device/ztc1/set\","
|
||||
"\"pl_prs\":\"reboot\","
|
||||
"\"pl_prs\":\"reboot %s\","
|
||||
"\"device\":{"
|
||||
"\"identifiers\":[\"tc1_%s\"],"
|
||||
"\"name\":\"%s\","
|
||||
"\"model\":\"TC1\","
|
||||
"\"manufacturer\":\"PHICOMM\"}}",
|
||||
str_mac,str_mac,str_mac, sys_config->micoSystemConfig.name);
|
||||
str_mac,str_mac,str_mac,str_mac, sys_config->micoSystemConfig.name);
|
||||
UserMqttSendTopic(topic_buf, send_buf, 1);
|
||||
}
|
||||
if (send_buf) free(send_buf);
|
||||
@@ -602,6 +656,7 @@ void UserMqttHassAutoLed(void) {
|
||||
"\"cmd_t\":\"device/ztc1/set\","
|
||||
"\"pl_on\":\"set led %s 1\","
|
||||
"\"pl_off\":\"set led %s 0\","
|
||||
"\"device_class\":\"outlet\","
|
||||
"\"device\":{"
|
||||
"\"identifiers\":[\"tc1_%s\"],"
|
||||
"\"name\":\"%s\","
|
||||
@@ -631,6 +686,7 @@ void UserMqttHassAutoChildLock(void) {
|
||||
"\"cmd_t\":\"device/ztc1/set\","
|
||||
"\"pl_on\":\"set childLock %s 1\","
|
||||
"\"pl_off\":\"set childLock %s 0\","
|
||||
"\"device_class\":\"outlet\","
|
||||
"\"device\":{"
|
||||
"\"identifiers\":[\"tc1_%s\"],"
|
||||
"\"name\":\"%s\","
|
||||
@@ -660,6 +716,7 @@ void UserMqttHassAutoTotalSocket(void) {
|
||||
"\"cmd_t\":\"device/ztc1/set\","
|
||||
"\"pl_on\":\"set total_socket %s 1\","
|
||||
"\"pl_off\":\"set total_socket %s 0\","
|
||||
"\"device_class\":\"outlet\","
|
||||
"\"device\":{"
|
||||
"\"identifiers\":[\"tc1_%s\"],"
|
||||
"\"name\":\"%s\","
|
||||
@@ -768,7 +825,7 @@ void UserMqttHassAutoPower(void) {
|
||||
char topic_buf[128] = {0};
|
||||
char send_buf[128] = {0};
|
||||
|
||||
void UserMqttHassPower(void) {
|
||||
extern void UserMqttHassPower(void) {
|
||||
sprintf(topic_buf, "homeassistant/sensor/%s/power/state", str_mac);
|
||||
sprintf(send_buf, "{\"power\":\"%.3f\"}", real_time_power / 10);
|
||||
UserMqttSendTopic(topic_buf, send_buf, 0);
|
||||
|
||||
@@ -22,6 +22,7 @@
|
||||
#define MQTT_LED_ENABLED user_config->power_led_enabled
|
||||
|
||||
extern OSStatus UserMqttInit(void);
|
||||
extern OSStatus UserMqttDeInit(void);
|
||||
|
||||
extern OSStatus UserMqttSend(char *arg);
|
||||
|
||||
|
||||
@@ -29,7 +29,6 @@
|
||||
* IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
******************************************************************************
|
||||
*/
|
||||
#include "mico.h"
|
||||
#include "main.h"
|
||||
#include "HTTPUtils.h"
|
||||
#include "SocketUtils.h"
|
||||
@@ -37,12 +36,6 @@
|
||||
#include "url.h"
|
||||
#include "http_server/web_log.h"
|
||||
|
||||
#if OTA_DEBUG
|
||||
#define ota_server_log(M, ...) custom_log("OTA", M, ##__VA_ARGS__)
|
||||
#else
|
||||
#define ota_server_log(M, ...)
|
||||
#endif
|
||||
|
||||
static ota_server_context_t *ota_server_context = NULL;
|
||||
static HTTPHeader_t *httpHeader = NULL;
|
||||
|
||||
@@ -50,155 +43,155 @@ static CRC16_Context crc_context;
|
||||
static md5_context md5;
|
||||
static uint32_t offset = 0;
|
||||
|
||||
static OSStatus onReceivedData( struct _HTTPHeader_t * httpHeader,
|
||||
static OSStatus OnReceivedData(struct _HTTPHeader_t * httpHeader,
|
||||
uint32_t pos,
|
||||
uint8_t *data,
|
||||
size_t len,
|
||||
void * userContext );
|
||||
void * userContext);
|
||||
|
||||
static void hex2str(uint8_t *hex, int hex_len, char *str)
|
||||
static void Hex2Str(char *hex, int hex_len, char *str)
|
||||
{
|
||||
int i = 0;
|
||||
for(i=0; i<hex_len; i++){
|
||||
sprintf(str+i*2, "%02x", hex[i]);
|
||||
}
|
||||
int i = 0;
|
||||
for(i=0; i<hex_len; i++){
|
||||
sprintf(str+i*2, "%02x", hex[i]);
|
||||
}
|
||||
}
|
||||
|
||||
static void upper2lower(char *str, int len)
|
||||
static void Upper2Ower(char *str, int len)
|
||||
{
|
||||
int i = 0;
|
||||
for(i=0; i<len; i++)
|
||||
{
|
||||
if( (*(str+i) >= 'A') && (*(str+i) <= 'Z') ){
|
||||
*(str+i) += 32;
|
||||
}
|
||||
int i = 0;
|
||||
for(i=0; i<len; i++)
|
||||
{
|
||||
if((*(str+i) >= 'A') && (*(str+i) <= 'Z')){
|
||||
*(str+i) += 32;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static int ota_server_send( uint8_t *data, int datalen )
|
||||
static int OtaServerSend(char *data, int datalen)
|
||||
{
|
||||
int res = 0;
|
||||
if( ota_server_context->download_url.HTTP_SECURITY == HTTP_SECURITY_HTTP ){
|
||||
res = send( ota_server_context->download_url.ota_fd, data, datalen, 0 );
|
||||
if(ota_server_context->download_url.HTTP_SECURITY == HTTP_SECURITY_HTTP){
|
||||
res = send(ota_server_context->download_url.ota_fd, data, datalen, 0);
|
||||
}
|
||||
#if OTA_USE_HTTPS
|
||||
if( ota_server_context->download_url.HTTP_SECURITY == HTTP_SECURITY_HTTPS ){
|
||||
res = ssl_send( ota_server_context->download_url.ota_ssl, data, datalen);
|
||||
if(ota_server_context->download_url.HTTP_SECURITY == HTTP_SECURITY_HTTPS){
|
||||
res = ssl_send(ota_server_context->download_url.ota_ssl, data, datalen);
|
||||
}
|
||||
#endif
|
||||
return res;
|
||||
}
|
||||
|
||||
static OSStatus ota_server_connect( struct sockaddr_in *addr, socklen_t addrlen )
|
||||
static OSStatus OtaServerConnect(struct sockaddr_in *addr, socklen_t addrlen)
|
||||
{
|
||||
OSStatus err = kNoErr;
|
||||
#if OTA_USE_HTTPS
|
||||
int ssl_errno = 0;
|
||||
#endif
|
||||
|
||||
err = connect( ota_server_context->download_url.ota_fd, (struct sockaddr *)addr, addrlen );
|
||||
require_noerr_string( err, exit, "ERROR: connect ota server failed" );
|
||||
err = connect(ota_server_context->download_url.ota_fd, (struct sockaddr *)addr, addrlen);
|
||||
require_noerr_string(err, exit, "ERROR: connect ota server failed");
|
||||
|
||||
#if OTA_USE_HTTPS
|
||||
if( ota_server_context->download_url.HTTP_SECURITY == HTTP_SECURITY_HTTPS ){
|
||||
ota_server_context->download_url.ota_ssl = ssl_connect( ota_server_context->download_url.ota_fd, 0, NULL, &ssl_errno );
|
||||
require_action_string( ota_server_context->download_url.ota_ssl != NULL, exit, err = kConnectionErr,"ERROR: ssl disconnect" );
|
||||
if(ota_server_context->download_url.HTTP_SECURITY == HTTP_SECURITY_HTTPS){
|
||||
ota_server_context->download_url.ota_ssl = ssl_connect(ota_server_context->download_url.ota_fd, 0, NULL, &ssl_errno);
|
||||
require_action_string(ota_server_context->download_url.ota_ssl != NULL, exit, err = kConnectionErr,"ERROR: ssl disconnect");
|
||||
}
|
||||
#endif
|
||||
|
||||
exit:
|
||||
exit:
|
||||
return err;
|
||||
}
|
||||
|
||||
static int ota_server_read_header( HTTPHeader_t *httpHeader )
|
||||
static int OtaServerReadHeader(HTTPHeader_t *httpHeader)
|
||||
{
|
||||
int res = 0;
|
||||
if( ota_server_context->download_url.HTTP_SECURITY == HTTP_SECURITY_HTTP ){
|
||||
res = SocketReadHTTPHeader( ota_server_context->download_url.ota_fd, httpHeader );
|
||||
if(ota_server_context->download_url.HTTP_SECURITY == HTTP_SECURITY_HTTP){
|
||||
res = SocketReadHTTPHeader(ota_server_context->download_url.ota_fd, httpHeader);
|
||||
}
|
||||
if( ota_server_context->download_url.HTTP_SECURITY == HTTP_SECURITY_HTTPS ){
|
||||
if(ota_server_context->download_url.HTTP_SECURITY == HTTP_SECURITY_HTTPS){
|
||||
#if OTA_USE_HTTPS
|
||||
res = SocketReadHTTPSHeader( ota_server_context->download_url.ota_ssl, httpHeader );
|
||||
res = SocketReadHTTPSHeader(ota_server_context->download_url.ota_ssl, httpHeader);
|
||||
#endif
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
static int ota_server_read_body( HTTPHeader_t *httpHeader )
|
||||
static int OtaServerReadBody(HTTPHeader_t *httpHeader)
|
||||
{
|
||||
int res = 0;
|
||||
if( ota_server_context->download_url.HTTP_SECURITY == HTTP_SECURITY_HTTP ){
|
||||
res = SocketReadHTTPBody( ota_server_context->download_url.ota_fd, httpHeader );
|
||||
if(ota_server_context->download_url.HTTP_SECURITY == HTTP_SECURITY_HTTP){
|
||||
res = SocketReadHTTPBody(ota_server_context->download_url.ota_fd, httpHeader);
|
||||
}
|
||||
if( ota_server_context->download_url.HTTP_SECURITY == HTTP_SECURITY_HTTPS ){
|
||||
if(ota_server_context->download_url.HTTP_SECURITY == HTTP_SECURITY_HTTPS){
|
||||
#if OTA_USE_HTTPS
|
||||
res = SocketReadHTTPSBody( ota_server_context->download_url.ota_ssl, httpHeader );
|
||||
res = SocketReadHTTPSBody(ota_server_context->download_url.ota_ssl, httpHeader);
|
||||
#endif
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
static int ota_server_send_header( void )
|
||||
static int OtaServerSendHeader(void)
|
||||
{
|
||||
char *header = NULL;
|
||||
int j = 0;
|
||||
int ret = 0;
|
||||
header = malloc( OTA_SEND_HEAD_SIZE );
|
||||
memset( header, 0x00, OTA_SEND_HEAD_SIZE );
|
||||
header = malloc(OTA_SEND_HEAD_SIZE);
|
||||
memset(header, 0x00, OTA_SEND_HEAD_SIZE);
|
||||
|
||||
j = sprintf( header, "GET " );
|
||||
j += sprintf( header + j, "/%s HTTP/1.1\r\n", ota_server_context->download_url.url );
|
||||
j = sprintf(header, "GET ");
|
||||
j += sprintf(header + j, "/%s HTTP/1.1\r\n", ota_server_context->download_url.url);
|
||||
|
||||
if ( ota_server_context->download_url.port == 0 )
|
||||
if (ota_server_context->download_url.port == 0)
|
||||
{
|
||||
j += sprintf( header + j, "Host: %s\r\n", ota_server_context->download_url.host );
|
||||
j += sprintf(header + j, "Host: %s\r\n", ota_server_context->download_url.host);
|
||||
} else
|
||||
{
|
||||
j += sprintf( header + j, "Host: %s:%d\r\n", ota_server_context->download_url.host, ota_server_context->download_url.port );
|
||||
j += sprintf(header + j, "Host: %s:%d\r\n", ota_server_context->download_url.host, ota_server_context->download_url.port);
|
||||
}
|
||||
|
||||
j += sprintf( header + j, "Connection: close\r\n" ); //Keep-Alive close
|
||||
j += sprintf(header + j, "Connection: close\r\n"); //Keep-Alive close
|
||||
|
||||
//Range: bytes=start-end
|
||||
if ( ota_server_context->download_state.download_begin_pos > 0 )
|
||||
if (ota_server_context->download_state.download_begin_pos > 0)
|
||||
{
|
||||
if ( ota_server_context->download_state.download_end_pos > 0 )
|
||||
if (ota_server_context->download_state.download_end_pos > 0)
|
||||
{
|
||||
j += sprintf( header + j, "Range: bytes=%d-%d\r\n", ota_server_context->download_state.download_begin_pos,
|
||||
ota_server_context->download_state.download_end_pos );
|
||||
j += sprintf(header + j, "Range: bytes=%d-%d\r\n", ota_server_context->download_state.download_begin_pos,
|
||||
ota_server_context->download_state.download_end_pos);
|
||||
} else
|
||||
{
|
||||
j += sprintf( header + j, "Range: bytes=%d-\r\n", ota_server_context->download_state.download_begin_pos );
|
||||
j += sprintf(header + j, "Range: bytes=%d-\r\n", ota_server_context->download_state.download_begin_pos);
|
||||
}
|
||||
}
|
||||
|
||||
j += sprintf( header + j, "\r\n" );
|
||||
j += sprintf(header + j, "\r\n");
|
||||
|
||||
ret = ota_server_send( (uint8_t *) header, strlen( header ) );
|
||||
ret = OtaServerSend((char *) header, strlen(header));
|
||||
|
||||
// ota_server_log("send: %d\r\n%s", strlen(header), header);
|
||||
if ( header != NULL ) free( header );
|
||||
// ota_log("send: %d\r\n%s", strlen(header), header);
|
||||
if (header != NULL) free(header);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static void ota_server_socket_close( void )
|
||||
static void OtaServerSocketClose(void)
|
||||
{
|
||||
#if OTA_USE_HTTPS
|
||||
if ( ota_server_context->download_url.ota_ssl ) ssl_close( ota_server_context->download_url.ota_ssl );
|
||||
if (ota_server_context->download_url.ota_ssl) ssl_close(ota_server_context->download_url.ota_ssl);
|
||||
#endif
|
||||
SocketClose( &(ota_server_context->download_url.ota_fd) );
|
||||
SocketClose(&(ota_server_context->download_url.ota_fd));
|
||||
ota_server_context->download_url.ota_fd = -1;
|
||||
}
|
||||
|
||||
static int ota_server_connect_server( struct in_addr in_addr )
|
||||
static int OtaServerConnectServer(struct in_addr in_addr)
|
||||
{
|
||||
int err = 0;
|
||||
struct sockaddr_in server_address;
|
||||
|
||||
if ( ota_server_context->download_url.port == 0 )
|
||||
if (ota_server_context->download_url.port == 0)
|
||||
{
|
||||
if ( ota_server_context->download_url.HTTP_SECURITY == HTTP_SECURITY_HTTP )
|
||||
if (ota_server_context->download_url.HTTP_SECURITY == HTTP_SECURITY_HTTP)
|
||||
{
|
||||
server_address.sin_port = htons(80);
|
||||
} else
|
||||
@@ -213,28 +206,28 @@ static int ota_server_connect_server( struct in_addr in_addr )
|
||||
server_address.sin_family = AF_INET;
|
||||
server_address.sin_addr = in_addr;
|
||||
|
||||
err = ota_server_connect( &server_address, sizeof(server_address) );
|
||||
if ( err != 0 )
|
||||
err = OtaServerConnect(&server_address, sizeof(server_address));
|
||||
if (err != 0)
|
||||
{
|
||||
mico_thread_sleep( 1 );
|
||||
mico_thread_sleep(1);
|
||||
return -1;
|
||||
}
|
||||
|
||||
ota_server_log("ota server connected!");
|
||||
ota_log("ota server connected!");
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void ota_server_progress_set( OTA_STATE_E state )
|
||||
static void OtaServerProgressSet(OTA_STATE_E state)
|
||||
{
|
||||
float progress = 0.00;
|
||||
|
||||
progress =(float) ota_server_context->download_state.download_begin_pos / ota_server_context->download_state.download_len;
|
||||
progress = progress*100;
|
||||
if( ota_server_context->ota_server_cb != NULL )
|
||||
if(ota_server_context->ota_server_cb != NULL)
|
||||
ota_server_context->ota_server_cb(state, progress);
|
||||
}
|
||||
|
||||
static void ota_server_thread( mico_thread_arg_t arg )
|
||||
static void OtaServerThread(mico_thread_arg_t arg)
|
||||
{
|
||||
OSStatus err;
|
||||
uint16_t crc16 = 0;
|
||||
@@ -245,108 +238,109 @@ static void ota_server_thread( mico_thread_arg_t arg )
|
||||
char **pptr = NULL;
|
||||
struct in_addr in_addr;
|
||||
|
||||
mico_logic_partition_t* ota_partition = MicoFlashGetInfo( MICO_PARTITION_OTA_TEMP );
|
||||
|
||||
mico_logic_partition_t* ota_partition = MicoFlashGetInfo(MICO_PARTITION_OTA_TEMP);
|
||||
|
||||
ota_server_context->ota_control = OTA_CONTROL_START;
|
||||
|
||||
hostent_content = gethostbyname( ota_server_context->download_url.host );
|
||||
require_action_quiet( hostent_content != NULL, DELETE, ota_server_progress_set(OTA_FAIL));
|
||||
hostent_content = gethostbyname(ota_server_context->download_url.host);
|
||||
require_action_quiet(hostent_content != NULL, DELETE, OtaServerProgressSet(OTA_FAIL));
|
||||
pptr=hostent_content->h_addr_list;
|
||||
in_addr.s_addr = *(uint32_t *)(*pptr);
|
||||
strcpy( ota_server_context->download_url.ip, inet_ntoa(in_addr));
|
||||
ota_server_log("OTA server address: %s, host ip: %s", ota_server_context->download_url.host, ota_server_context->download_url.ip);
|
||||
strcpy(ota_server_context->download_url.ip, inet_ntoa(in_addr));
|
||||
ota_log("OTA server address: %s, host ip: %s", ota_server_context->download_url.host, ota_server_context->download_url.ip);
|
||||
|
||||
offset = 0;
|
||||
MicoFlashErase( MICO_PARTITION_OTA_TEMP, 0x0, ota_partition->partition_length );
|
||||
|
||||
CRC16_Init( &crc_context );
|
||||
if( ota_server_context->ota_check.is_md5 == true ){
|
||||
InitMd5( &md5 );
|
||||
MicoFlashErase(MICO_PARTITION_OTA_TEMP, 0x0, ota_partition->partition_length);
|
||||
|
||||
CRC16_Init(&crc_context);
|
||||
if(ota_server_context->ota_check.is_md5 == true){
|
||||
InitMd5(&md5);
|
||||
}
|
||||
|
||||
httpHeader = HTTPHeaderCreateWithCallback(512, OnReceivedData, NULL, NULL);
|
||||
require_action(httpHeader, DELETE, OtaServerProgressSet(OTA_FAIL));
|
||||
|
||||
httpHeader = HTTPHeaderCreateWithCallback( 1024, onReceivedData, NULL, NULL );
|
||||
require_action( httpHeader, DELETE, ota_server_progress_set(OTA_FAIL) );
|
||||
|
||||
while ( 1 )
|
||||
while (1)
|
||||
{
|
||||
if ( ota_server_context->ota_control == OTA_CONTROL_PAUSE ){
|
||||
mico_thread_sleep( 1 );
|
||||
if (ota_server_context->ota_control == OTA_CONTROL_PAUSE){
|
||||
mico_thread_sleep(1);
|
||||
continue;
|
||||
}else if( ota_server_context->ota_control == OTA_CONTROL_STOP ){
|
||||
}else if(ota_server_context->ota_control == OTA_CONTROL_STOP){
|
||||
goto DELETE;
|
||||
}
|
||||
|
||||
ota_server_context->download_url.ota_fd = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
|
||||
err = ota_server_connect_server( in_addr );
|
||||
require_noerr_action( err, RECONNECTED, ota_server_progress_set(OTA_FAIL));
|
||||
ota_server_context->download_url.ota_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
|
||||
err = OtaServerConnectServer(in_addr);
|
||||
require_noerr_action(err, RECONNECTED, OtaServerProgressSet(OTA_FAIL));
|
||||
|
||||
/* Send HTTP Request */
|
||||
ota_server_send_header( );
|
||||
OtaServerSendHeader();
|
||||
|
||||
FD_ZERO( &readfds );
|
||||
FD_SET( ota_server_context->download_url.ota_fd, &readfds );
|
||||
FD_ZERO(&readfds);
|
||||
FD_SET(ota_server_context->download_url.ota_fd, &readfds);
|
||||
|
||||
select( ota_server_context->download_url.ota_fd + 1, &readfds, NULL, NULL, NULL );
|
||||
if ( FD_ISSET( ota_server_context->download_url.ota_fd, &readfds ) )
|
||||
select(ota_server_context->download_url.ota_fd + 1, &readfds, NULL, NULL, NULL);
|
||||
if (FD_ISSET(ota_server_context->download_url.ota_fd, &readfds))
|
||||
{
|
||||
/*parse header*/
|
||||
err = ota_server_read_header( httpHeader );
|
||||
if ( ota_server_context->ota_control == OTA_CONTROL_START )
|
||||
err = OtaServerReadHeader(httpHeader);
|
||||
if (ota_server_context->ota_control == OTA_CONTROL_START)
|
||||
{
|
||||
ota_server_context->download_state.download_len = httpHeader->contentLength;
|
||||
ota_server_context->ota_control = OTA_CONTROL_CONTINUE;
|
||||
}
|
||||
switch ( err )
|
||||
switch (err)
|
||||
{
|
||||
case kNoErr:
|
||||
#if OTA_DEBUG
|
||||
PrintHTTPHeader( httpHeader );
|
||||
PrintHTTPHeader(httpHeader);
|
||||
#endif
|
||||
err = ota_server_read_body( httpHeader );/*get body data*/
|
||||
require_noerr( err, RECONNECTED );
|
||||
err = OtaServerReadBody(httpHeader);/*get body data*/
|
||||
require_noerr(err, RECONNECTED);
|
||||
/*get data and print*/
|
||||
break;
|
||||
case EWOULDBLOCK:
|
||||
case kNoSpaceErr:
|
||||
case kConnectionErr:
|
||||
default:
|
||||
ota_server_log("ERROR: HTTP Header parse error: %d", err);
|
||||
ota_log("ERROR: HTTP Header parse error: %d", err);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if ( ota_server_context->download_state.download_len == ota_server_context->download_state.download_begin_pos )
|
||||
if (ota_server_context->download_state.download_len == ota_server_context->download_state.download_begin_pos)
|
||||
{
|
||||
if( httpHeader->statusCode != 200 ){
|
||||
if(httpHeader->statusCode != 200){
|
||||
OtaServerProgressSet(OTA_FAIL);
|
||||
goto DELETE;
|
||||
}
|
||||
CRC16_Final( &crc_context, &crc16 );
|
||||
if( ota_server_context->ota_check.is_md5 == true ){
|
||||
Md5Final( &md5, (unsigned char *) md5_value );
|
||||
hex2str((uint8_t *)md5_value, 16, md5_value_string);
|
||||
CRC16_Final(&crc_context, &crc16);
|
||||
if(ota_server_context->ota_check.is_md5 == true){
|
||||
Md5Final(&md5, (unsigned char *) md5_value);
|
||||
Hex2Str((char *)md5_value, 16, md5_value_string);
|
||||
}
|
||||
if ( memcmp( md5_value_string, ota_server_context->ota_check.md5, OTA_MD5_LENTH ) == 0 ){
|
||||
ota_server_progress_set(OTA_SUCCE);
|
||||
mico_ota_switch_to_new_fw( ota_server_context->download_state.download_len, crc16 );
|
||||
mico_system_power_perform( mico_system_context_get( ), eState_Software_Reset );
|
||||
if (memcmp(md5_value_string, ota_server_context->ota_check.md5, OTA_MD5_LENTH) == 0){
|
||||
OtaServerProgressSet(OTA_SUCCE);
|
||||
mico_ota_switch_to_new_fw(ota_server_context->download_state.download_len, crc16);
|
||||
mico_system_power_perform(mico_system_context_get(), eState_Software_Reset);
|
||||
}else{
|
||||
ota_server_log("OTA md5 check err, Calculation:%s, Get:%s", md5_value_string, ota_server_context->ota_check.md5);
|
||||
ota_server_progress_set(OTA_FAIL);
|
||||
ota_log("OTA md5 check err, Calculation:%s, Get:%s", md5_value_string, ota_server_context->ota_check.md5);
|
||||
OtaServerProgressSet(OTA_FAIL);
|
||||
}
|
||||
goto DELETE;
|
||||
}
|
||||
|
||||
RECONNECTED:
|
||||
ota_server_socket_close( );
|
||||
RECONNECTED:
|
||||
OtaServerSocketClose();
|
||||
mico_thread_sleep(2);
|
||||
continue;
|
||||
|
||||
}
|
||||
DELETE:
|
||||
HTTPHeaderDestory( &httpHeader );
|
||||
ota_server_socket_close( );
|
||||
if( ota_server_context != NULL ){
|
||||
if( ota_server_context->download_url.url != NULL ){
|
||||
DELETE:
|
||||
HTTPHeaderDestory(&httpHeader);
|
||||
OtaServerSocketClose();
|
||||
if(ota_server_context != NULL){
|
||||
if(ota_server_context->download_url.url != NULL){
|
||||
free(ota_server_context->download_url.url);
|
||||
ota_server_context->download_url.url = NULL;
|
||||
}
|
||||
@@ -354,57 +348,57 @@ static void ota_server_thread( mico_thread_arg_t arg )
|
||||
ota_server_context = NULL;
|
||||
}
|
||||
|
||||
ota_server_log("ota server thread will delete");
|
||||
ota_log("ota server thread will delete");
|
||||
mico_rtos_delete_thread(NULL);
|
||||
}
|
||||
|
||||
/*one request may receive multi reply*/
|
||||
static OSStatus onReceivedData( struct _HTTPHeader_t * inHeader, uint32_t inPos, uint8_t * inData,
|
||||
size_t inLen, void * inUserContext )
|
||||
static OSStatus OnReceivedData(struct _HTTPHeader_t * inHeader, uint32_t inPos, uint8_t * inData,
|
||||
size_t inLen, void * inUserContext)
|
||||
{
|
||||
OSStatus err = kNoErr;
|
||||
|
||||
if ( inLen == 0 )
|
||||
if (inLen == 0)
|
||||
return err;
|
||||
|
||||
ota_server_context->download_state.download_begin_pos += inLen;
|
||||
|
||||
CRC16_Update( &crc_context, inData, inLen );
|
||||
if( ota_server_context->ota_check.is_md5 == true ){
|
||||
Md5Update( &md5, inData, inLen );
|
||||
CRC16_Update(&crc_context, inData, inLen);
|
||||
if(ota_server_context->ota_check.is_md5 == true){
|
||||
Md5Update(&md5, inData, inLen);
|
||||
}
|
||||
|
||||
MicoFlashWrite( MICO_PARTITION_OTA_TEMP, &offset, (uint8_t *) inData, inLen );
|
||||
MicoFlashWrite(MICO_PARTITION_OTA_TEMP, &offset, inData, inLen);
|
||||
|
||||
ota_server_progress_set(OTA_LOADING);
|
||||
OtaServerProgressSet(OTA_LOADING);
|
||||
|
||||
if( ota_server_context->ota_control == OTA_CONTROL_PAUSE ){
|
||||
while( 1 ){
|
||||
if( ota_server_context->ota_control != OTA_CONTROL_PAUSE )
|
||||
if(ota_server_context->ota_control == OTA_CONTROL_PAUSE){
|
||||
while(1){
|
||||
if(ota_server_context->ota_control != OTA_CONTROL_PAUSE)
|
||||
break;
|
||||
mico_thread_msleep(100);
|
||||
}
|
||||
}
|
||||
|
||||
if( ota_server_context->ota_control == OTA_CONTROL_STOP ){
|
||||
if(ota_server_context->ota_control == OTA_CONTROL_STOP){
|
||||
err = kUnsupportedErr;
|
||||
}
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
static OSStatus ota_server_set_url( char *url )
|
||||
static OSStatus OtaServerSetUrl(char *url)
|
||||
{
|
||||
OSStatus err = kNoErr;
|
||||
url_field_t *url_t;
|
||||
char *pos = NULL;
|
||||
|
||||
url_t = url_parse( url );
|
||||
url_t = url_parse(url);
|
||||
require_action(url, exit, err = kParamErr);
|
||||
#if OTA_DEBUG
|
||||
url_field_print( url_t );
|
||||
url_field_print(url_t);
|
||||
#endif
|
||||
if ( !strcmp( url_t->schema, "https" ) )
|
||||
if (!strcmp(url_t->schema, "https"))
|
||||
{
|
||||
ota_server_context->download_url.HTTP_SECURITY = HTTP_SECURITY_HTTPS;
|
||||
} else
|
||||
@@ -412,30 +406,30 @@ static OSStatus ota_server_set_url( char *url )
|
||||
ota_server_context->download_url.HTTP_SECURITY = HTTP_SECURITY_HTTP;
|
||||
}
|
||||
|
||||
strcpy( ota_server_context->download_url.host, url_t->host );
|
||||
ota_server_context->download_url.port = atoi( url_t->port );
|
||||
pos = strstr( url, url_t->path );
|
||||
if ( pos == NULL )
|
||||
strcpy(ota_server_context->download_url.host, url_t->host);
|
||||
ota_server_context->download_url.port = atoi(url_t->port);
|
||||
pos = strstr(url, url_t->path);
|
||||
if (pos == NULL)
|
||||
{
|
||||
strcpy( ota_server_context->download_url.url, "" );
|
||||
strcpy(ota_server_context->download_url.url, "");
|
||||
} else
|
||||
{
|
||||
strcpy( ota_server_context->download_url.url, pos );
|
||||
strcpy(ota_server_context->download_url.url, pos);
|
||||
}
|
||||
|
||||
exit:
|
||||
url_free( url_t );
|
||||
exit:
|
||||
url_free(url_t);
|
||||
return err;
|
||||
}
|
||||
|
||||
OSStatus ota_server_start( char *url, char *md5, ota_server_cb_fn call_back )
|
||||
OSStatus OtaServerStart(char *url, char *md5, ota_server_cb_fn call_back)
|
||||
{
|
||||
OSStatus err = kNoErr;
|
||||
|
||||
require_action(url, exit, err = kParamErr);
|
||||
|
||||
if( ota_server_context != NULL ){
|
||||
if( ota_server_context->download_url.url != NULL ){
|
||||
if(ota_server_context != NULL){
|
||||
if(ota_server_context->download_url.url != NULL){
|
||||
free(ota_server_context->download_url.url);
|
||||
ota_server_context->download_url.url = NULL;
|
||||
}
|
||||
@@ -451,38 +445,38 @@ OSStatus ota_server_start( char *url, char *md5, ota_server_cb_fn call_back )
|
||||
require_action(ota_server_context->download_url.url, exit, err = kNoMemoryErr);
|
||||
memset(ota_server_context->download_url.url, 0x00, strlen(url));
|
||||
|
||||
err = ota_server_set_url(url);
|
||||
err = OtaServerSetUrl(url);
|
||||
require_noerr(err, exit);
|
||||
|
||||
if( md5 != NULL ){
|
||||
if(md5 != NULL){
|
||||
ota_server_context->ota_check.is_md5 = true;
|
||||
memcpy(ota_server_context->ota_check.md5, md5, OTA_MD5_LENTH);
|
||||
upper2lower(ota_server_context->ota_check.md5, OTA_MD5_LENTH);
|
||||
Upper2Ower(ota_server_context->ota_check.md5, OTA_MD5_LENTH);
|
||||
}
|
||||
|
||||
ota_server_context->ota_server_cb = call_back;
|
||||
|
||||
err = mico_rtos_create_thread( NULL, MICO_APPLICATION_PRIORITY, "OTA", ota_server_thread, OTA_SERVER_THREAD_STACK_SIZE, 0 );
|
||||
exit:
|
||||
err = mico_rtos_create_thread(NULL, MICO_APPLICATION_PRIORITY, "OTA", OtaServerThread, OTA_SERVER_THREAD_STACK_SIZE, 0);
|
||||
exit:
|
||||
return err;
|
||||
}
|
||||
|
||||
void ota_server_pause( void )
|
||||
void OtaServerPause(void)
|
||||
{
|
||||
ota_server_context->ota_control = OTA_CONTROL_PAUSE;
|
||||
}
|
||||
|
||||
void ota_server_continue( void )
|
||||
void OtaServerContinue(void)
|
||||
{
|
||||
ota_server_context->ota_control = OTA_CONTROL_CONTINUE;
|
||||
}
|
||||
|
||||
void ota_server_stop( void )
|
||||
void OtaServerStop(void)
|
||||
{
|
||||
ota_server_context->ota_control = OTA_CONTROL_STOP;
|
||||
}
|
||||
|
||||
OTA_CONTROL_E ota_server_get( void )
|
||||
OTA_CONTROL_E OtaServerGet(void)
|
||||
{
|
||||
return ota_server_context->ota_control;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -67,7 +67,7 @@ OSStatus UserRtcInit(void)
|
||||
/* start rtc client */
|
||||
err = mico_rtos_create_thread(NULL, MICO_APPLICATION_PRIORITY, "rtc",
|
||||
(mico_thread_function_t) RtcThread,
|
||||
0x1000, 0);
|
||||
0x800, 0);
|
||||
require_noerr_string(err, exit, "ERROR: Unable to start the rtc thread.");
|
||||
|
||||
if (kNoErr != err) rtc_log("ERROR1, app thread exit err: %d kNoErr[%d]", err, kNoErr);
|
||||
|
||||
@@ -46,6 +46,8 @@ char* get_func_name(char func_code) {
|
||||
return "Toggle LED";
|
||||
case REBOOT_SYSTEM:
|
||||
return "Reboot";
|
||||
case REBOOT_HTTP:
|
||||
return "REBOOT_HTTP";
|
||||
case CONFIG_WIFI:
|
||||
return "WiFi Config";
|
||||
case RESET_SYSTEM:
|
||||
@@ -226,10 +228,18 @@ static void KeyEventHandler(int num, boolean longPress) {
|
||||
break;
|
||||
MicoSystemReboot();
|
||||
break;
|
||||
case REBOOT_HTTP:
|
||||
if (childLockEnabled)
|
||||
break;
|
||||
AppHttpdStop();
|
||||
mico_rtos_thread_sleep(1);
|
||||
AppHttpdStart();
|
||||
break;
|
||||
case CONFIG_WIFI:
|
||||
if (childLockEnabled)
|
||||
break;
|
||||
StartLedBlink(3);
|
||||
UserMqttDeInit();
|
||||
micoWlanSuspendStation();
|
||||
ApInit(true);
|
||||
break;
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
#define CONFIG_WIFI 9
|
||||
#define RESET_SYSTEM 10
|
||||
#define SWITCH_CHILD_LOCK_ENABLE 11
|
||||
#define REBOOT_HTTP 12
|
||||
|
||||
extern char socket_status[32];
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
#include "mico_socket.h"
|
||||
#include "user_gpio.h"
|
||||
#include "http_server/web_log.h"
|
||||
#include "mqtt_server/user_mqtt_client.h"
|
||||
|
||||
char wifi_status = WIFI_STATE_NOCONNECT;
|
||||
|
||||
@@ -122,6 +123,9 @@ static void WifiLedTimerCallback(void* arg)
|
||||
UserLedSet(-1);
|
||||
break;
|
||||
case WIFI_STATE_CONNECTED:
|
||||
if (!(MQTT_SERVER[0] < 0x20 || MQTT_SERVER[0] > 0x7f || MQTT_SERVER_PORT < 1)){
|
||||
UserMqttInit();
|
||||
}
|
||||
UserLedSet(0);
|
||||
mico_rtos_stop_timer(&wifi_led_timer);
|
||||
if (RelayOut()&&user_config->power_led_enabled)
|
||||
|
||||
@@ -39,17 +39,11 @@ OSStatus system_discovery_init( system_context_t * const inContext )
|
||||
init.service_name = "_easylink._tcp.local.";
|
||||
|
||||
/* name#xxxxxx.local. */
|
||||
snprintf( temp_txt, 100, "%s#%c%c%c%c%c%c.local.", inContext->flashContentInRam.micoSystemConfig.name,
|
||||
inContext->micoStatus.mac[9], inContext->micoStatus.mac[10], \
|
||||
inContext->micoStatus.mac[12], inContext->micoStatus.mac[13], \
|
||||
inContext->micoStatus.mac[15], inContext->micoStatus.mac[16] );
|
||||
snprintf( temp_txt, 100, "%s.local.", inContext->flashContentInRam.micoSystemConfig.name);
|
||||
init.host_name = (char*)__strdup(temp_txt);
|
||||
|
||||
/* name#xxxxxx. */
|
||||
snprintf( temp_txt, 100, "%s#%c%c%c%c%c%c", inContext->flashContentInRam.micoSystemConfig.name,
|
||||
inContext->micoStatus.mac[9], inContext->micoStatus.mac[10], \
|
||||
inContext->micoStatus.mac[12], inContext->micoStatus.mac[13], \
|
||||
inContext->micoStatus.mac[15], inContext->micoStatus.mac[16] );
|
||||
snprintf( temp_txt, 100, "%s", inContext->flashContentInRam.micoSystemConfig.name);
|
||||
init.instance_name = (char*)__strdup(temp_txt);
|
||||
|
||||
#ifndef MICO_LOCAL_SERVER_PORT
|
||||
|
||||
@@ -50,7 +50,8 @@ httpd_state_t httpd_state;
|
||||
|
||||
static mico_thread_t httpd_main_thread;
|
||||
|
||||
#define http_server_thread_stack_size 0x3000
|
||||
// 0x8000 不行
|
||||
#define http_server_thread_stack_size 0x6000
|
||||
|
||||
/* Why HTTPD_MAX_MESSAGE + 2?
|
||||
* Handlers are allowed to use HTTPD_MAX_MESSAGE bytes of this buffer.
|
||||
@@ -59,7 +60,7 @@ static mico_thread_t httpd_main_thread;
|
||||
*/
|
||||
static bool httpd_stop_req;
|
||||
|
||||
#define HTTPD_CLIENT_SOCK_TIMEOUT 100
|
||||
#define HTTPD_CLIENT_SOCK_TIMEOUT 10
|
||||
#define HTTPD_TIMEOUT_EVENT 0
|
||||
|
||||
/** Maximum number of backlogged http connections
|
||||
|
||||
@@ -1125,6 +1125,8 @@ int httpd_parse_hdr_tags(httpd_request_t *req, int sock,
|
||||
*/
|
||||
int httpd_get_data(httpd_request_t *req, char *content, int length);
|
||||
|
||||
int httpd_get_data2(httpd_request_t *req, char *content, int length);
|
||||
|
||||
/** @brief Get the incoming JSON data in case of HTTP POST request
|
||||
*
|
||||
* @note This function is an extension to \ref httpd_get_data. Additionally this
|
||||
|
||||
@@ -543,6 +543,52 @@ out:
|
||||
return req->remaining_bytes;
|
||||
}
|
||||
|
||||
int httpd_get_data2(httpd_request_t *req, char *content, int length)
|
||||
{
|
||||
int ret;
|
||||
char *buf;
|
||||
|
||||
// /* Is this condition required? */
|
||||
// if (req->body_nbytes >= HTTPD_MAX_MESSAGE - 2)
|
||||
// return -kInProgressErr;
|
||||
|
||||
if (!req->hdr_parsed) {
|
||||
buf = malloc(HTTPD_MAX_MESSAGE);
|
||||
if (!buf) {
|
||||
system_log("Failed to allocate memory for buffer");
|
||||
return -kInProgressErr;
|
||||
}
|
||||
|
||||
ret = httpd_parse_hdr_tags(req, req->sock, buf,
|
||||
HTTPD_MAX_MESSAGE);
|
||||
|
||||
if (ret != kNoErr) {
|
||||
system_log("Unable to parse header tags");
|
||||
goto out;
|
||||
} else {
|
||||
system_log("Headers parsed successfully\r\n");
|
||||
req->hdr_parsed = 1;
|
||||
}
|
||||
|
||||
free(buf);
|
||||
}
|
||||
|
||||
/* handle here */
|
||||
ret = httpd_recv(req->sock, content,
|
||||
length, 0);
|
||||
if (ret == -1) {
|
||||
system_log("Failed to read POST data");
|
||||
goto out;
|
||||
}
|
||||
/* scratch will now have the JSON data */
|
||||
// content[ret] = '\0';
|
||||
req->remaining_bytes -= ret;
|
||||
system_log("Read %d bytes and remaining %d bytes",
|
||||
ret, req->remaining_bytes);
|
||||
out:
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int get_matching_chars(const char *s1, const char *s2)
|
||||
{
|
||||
int match = 0;
|
||||
|
||||
Reference in New Issue
Block a user