13 Commits

21 changed files with 1259 additions and 958 deletions

View File

@@ -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

View File

@@ -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]

1
.version Normal file
View File

@@ -0,0 +1 @@
v1.0.79

View File

@@ -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"
@@ -55,6 +60,8 @@ const struct httpd_wsgi_call g_app_handlers[];
char power_info_json[2560] = {0};
char up_time[16] = "00:00:00";
#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)
@@ -255,6 +262,90 @@ static int HttpSetButtonEvent(httpd_request_t *req) {
return err;
}
#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;
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);
// 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);
}
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);
// 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);
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);
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 (buffer) free(buffer);
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) {
OSStatus err = kNoErr;
@@ -344,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 {
@@ -643,6 +777,7 @@ const struct httpd_wsgi_call g_app_handlers[] = {
{"/childLock", HTTPD_HDR_DEFORT, 0, NULL, HttpSetChildLock, NULL, NULL},
{"/deviceName", HTTPD_HDR_DEFORT, 0, NULL, HttpSetDeviceName, NULL, NULL},
{"/buttonEvents", HTTPD_HDR_DEFORT, 0, HttpGetButtonEvents, HttpSetButtonEvent, NULL, NULL},
{"/ota/fileUpload", HTTPD_HDR_DEFORT, 0, NULL, HttpSetOTAFile, NULL, NULL},
};
static int g_app_handlers_no = sizeof(g_app_handlers) / sizeof(struct httpd_wsgi_call);

View File

@@ -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();

View File

@@ -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&#45;&#45;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&#45;&#45;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&#45;&#45;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&#45;&#45;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>
@@ -113,7 +113,7 @@
<use xlink:href="#icon-cloud_download"/>
</svg>
</i>
<span class="lang" langKey="OTA">在线升级</span>
<span class="lang" langKey="OTA">固件升级</span>
</a>
<a class="mdl-navigation__link" href="javascript:ShowPage(7);">
<i class="mdl-color-text--blue-grey-400 material-icons" role="presentation">
@@ -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>
@@ -519,10 +519,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 +539,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 +598,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>
@@ -655,14 +655,33 @@
</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>
<div class="page page6 mdl-cell mdl-cell--12-col demo-card-square mdl-card mdl-shadow--2dp">
<div class="mdl-card__title mdl-card--expand">
<h2 class="mdl-card__title-text">本地升级</h2>
</div>
<div class="mdl-card__supporting-text">
<form id="otaForm" action="/ota/fileUpload" method="post" enctype="multipart/form-data">
<div class="mdl-textfield mdl-js-textfield mdl-textfield--floating-label">
<input class="mdl-textfield__input" type="file" id="ota_file" name="ota_file">
</div>
</form>
</div>
<div class="mdl-card__actions mdl-card--border">
<button type="button" onclick="OtaFileUpload();" class="mdl-button mdl-button--colored " id="submit-ota-file">
上传 OTA 文件
</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">
<div class="mdl-card__title mdl-card--expand">
<h2 class="mdl-card__title-text">系统日志</h2>
@@ -671,10 +690,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>
@@ -777,7 +796,7 @@
Config: {en: "Config", cn: "设置"},
TimedTask: {en: "Timed Task", cn: "定时任务"},
SystemStatus: {en: "System Status", cn: "系统状态"},
OTA: {en: "OTA", cn: "在线升级"},
OTA: {en: "OTA", cn: "固件升级"},
SystemLog: {en: "System Log", cn: "系统日志"},
About: {en: "About", cn: "关于"},
//主页
@@ -957,6 +976,7 @@
BTN_OPERATIONS.push("重新配网");
BTN_OPERATIONS.push("重置系统");
BTN_OPERATIONS.push("切换童锁");
BTN_OPERATIONS.push("重启网页");
$('#btn_action_selector').append(
$('<li>')
@@ -1216,15 +1236,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);
@@ -1240,6 +1260,7 @@ componentHandler.upgradeDom();
var w = document.getElementById("w");
var w_t = document.getElementById("w_t");
var w_y = document.getElementById("w_y");
var powerTimerId;
function GetPowerRecord() {
HttpPost("/power", function (re) {
@@ -1305,7 +1326,7 @@ componentHandler.upgradeDom();
switch_lables[i+3].MaterialSwitch.off();
}
}
window.powerTimeout= window.setTimeout(GetPowerRecord, 3000);
powerTimerId= window.setTimeout(GetPowerRecord, 3000);
}, power_idx.toString());
}
$(document).ready(function(){
@@ -1484,12 +1505,51 @@ HttpPost("/shortClickEvent", function (re) {
var ota_url = document.getElementById("ota_url").value;
var protocol = window.location.protocol;
var baseUrl = protocol+"//"+window.location.host;
clearTimeout(window.powerTimeout);
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) {
ShowToast("请选择要上传的 OTA 文件");
return;
}
clearTimeout(powerTimerId);
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() {
this.MaterialProgress.setProgress(0);

File diff suppressed because it is too large Load Diff

View File

@@ -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;
}

View File

@@ -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,10 +185,6 @@ int application_start(void) {
}
}
KeyInit();
if (!(MQTT_SERVER[0] < 0x20 || MQTT_SERVER[0] > 0x7f || MQTT_SERVER_PORT < 1)){
err = UserMqttInit();
require_noerr(err, exit);
}
err = UserRtcInit();
require_noerr(err, exit);
PowerInit();
@@ -199,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.");
@@ -213,7 +208,6 @@ int application_start(void) {
if (user_config->task_top && now >= user_config->task_top->prs_time) {
ProcessTask();
}
mico_thread_msleep(1000);
}

View File

@@ -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.7"
#define VERSION "v2.2.0"
#define TYPE 1
#define TYPE_NAME "TC1"

View File

@@ -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,6 +99,15 @@ void UserMqttTimerFunc(void *arg) {
}
}
OSStatus UserMqttDeInit(void) {
OSStatus err = kNoErr;
// 1. 请求线程退出
mqtt_thread_should_exit = true;
return err;
}
/* Application entrance */
OSStatus UserMqttInit(void) {
OSStatus err = kNoErr;
@@ -204,14 +214,14 @@ void MqttClientThread(mico_thread_arg_t arg) {
msg_send_event_fd = mico_create_event_fd(mqtt_msg_send_queue);
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)
@@ -272,7 +282,7 @@ void MqttClientThread(mico_thread_arg_t arg) {
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);
@@ -329,10 +339,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
@@ -431,7 +443,7 @@ 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){
if(mqtt_msg_send_queue == NULL|| !isconnect){
return err;
}
@@ -579,7 +591,6 @@ void UserMqttHassAutoRebootButton(void) {
"\"object_id\":\"tc1_%s_reboot\","
"\"cmd_t\":\"device/ztc1/set\","
"\"pl_prs\":\"reboot\","
"\"device_class\":\"outlet\","
"\"device\":{"
"\"identifiers\":[\"tc1_%s\"],"
"\"name\":\"%s\","
@@ -776,7 +787,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);

View File

@@ -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);

View File

@@ -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);

View File

@@ -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;

View File

@@ -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];

View File

@@ -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)

0
git版本.txt Normal file
View File

View File

@@ -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

View File

@@ -50,7 +50,8 @@ httpd_state_t httpd_state;
static mico_thread_t httpd_main_thread;
#define http_server_thread_stack_size 0x2000
// 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.

View File

@@ -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

View File

@@ -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;