1、修复APP控制回传栈溢出导致软复位,改为轻量打包路径

2、MCU ACK回传不再走大desc/encode,改用wrap_raw;otadata改为指针并增大mcu_bridge栈,避免soft_reset_exception。
This commit is contained in:
2026-08-12 10:07:35 +08:00
parent 8581d15f46
commit 95973ec52d
10 changed files with 254 additions and 148 deletions
+1
View File
@@ -0,0 +1 @@
PY32F040开发框架/*
@@ -532,12 +532,12 @@ void usr_ble_send_data(u8* data,u16 len)
}
//printf("send rsp:");
// printf_buf(data,len);
printf("\n");
for(u8 i=0;i<len;i++)
{
printf("%c",data[i]);
}
printf("\n");
// printf("\n");
// for(u8 i=0;i<len;i++)
// {
// printf("%c",data[i]);
// }
// printf("\n");
}
static int app_send_user_data(u16 handle, u8 *data, u16 len, u8 handle_type)
@@ -97,7 +97,7 @@ const char log_tag_const_e_APP_POWER AT(.LOG_TAG_CONST) = TRUE;
const char log_tag_const_v_APP AT(.LOG_TAG_CONST) = FALSE;
const char log_tag_const_i_APP AT(.LOG_TAG_CONST) = TRUE;
const char log_tag_const_d_APP AT(.LOG_TAG_CONST) = TRUE; /* MCU_BR 等 APP 标签的 log_debug 开关 */
const char log_tag_const_d_APP AT(.LOG_TAG_CONST) = FALSE; /* MCU_BR 等 APP 标签的 log_debug 开关 */
const char log_tag_const_w_APP AT(.LOG_TAG_CONST) = TRUE;
const char log_tag_const_e_APP AT(.LOG_TAG_CONST) = TRUE;
@@ -234,10 +234,7 @@ int ble_proto_unpack(const uint8_t *buf, size_t len, ble_proto_packet_t *pkt)
}
if (pkt->bytes_data && pkt->device_bytes_len > 0) {
printf("bytes_data = ");
for (size_t i = 0; i < pkt->device_bytes_len; i++) {
printf("%02X ", *(pkt->bytes_data + i));
}
printf("\n");
printf_buf(pkt->bytes_data, pkt->device_bytes_len);
}
printf("crc = %04X", crc_recv);
printf("\n");
+37 -30
View File
@@ -222,106 +222,113 @@ int bleproto_appdata_encode(uint8_t *txbuf,
int rc;
uint16_t txbuf_size = size;
bleproto_appdata_t appdata;
/*
* appdata.data[] 在 SMALL 模式下约 2.3KB,供 JSON/OTA 编码中间缓冲。
* MCU 55AA 透传已改走 bleproto_appdata_wrap_raw,不再经过本函数。
* 此处用 static,避免在调用方任务栈上临时分配。
*/
static bleproto_appdata_t s_enc_appdata;
bleproto_appdata_t *appdata = &s_enc_appdata;
/* header */
{
appdata.header = desc->header;
appdata->header = desc->header;
}
/* data */
if (appdata.header.msgtype == BLEPROTO_APPDATA_MSGTYPE_REQ)
if (appdata->header.msgtype == BLEPROTO_APPDATA_MSGTYPE_REQ)
{
switch (appdata.header.serviceid)
switch (appdata->header.serviceid)
{
case E_BLEPROTO_SERVICE_ID_DEVICEINFO:
rc = bleproto_encjson_deviceinfo_req(&desc->datast.deviceinfo_req,
appdata.data);
appdata->data);
break;
case E_BLEPROTO_SERVICE_ID_AUTHSETUP:
rc = bleproto_encjson_authsetup_req(&desc->datast.authsetup_req,
appdata.data);
appdata->data);
break;
case E_BLEPROTO_SERVICE_ID_AUTHDELETE:
rc = bleproto_encjson_authdelete_req(&desc->datast.authdelete_req,
appdata.data);
appdata->data);
break;
case E_BLEPROTO_SERVICE_ID_RUNCMD:
rc = bleproto_encjson_runcmd_req(&desc->datast.runcmd_req,
appdata.data);
appdata->data);
break;
case E_BLEPROTO_SERVICE_ID_REPORTCMD:
rc = bleproto_encjson_reportcmd_req(&desc->datast.reportcmd_req,
appdata.data);
appdata->data);
break;
case E_BLEPROTO_SERVICE_ID_DOACTION:
rc = bleproto_encjson_doaction_req(&desc->datast.doaction_req,
appdata.data);
appdata->data);
break;
case E_BLEPROTO_SERVICE_ID_OTANOTIFY:
rc = bleproto_encjson_otanotify_req(&desc->datast.otanotify_req,
appdata.data);
appdata->data);
break;
case E_BLEPROTO_SERVICE_ID_OTADATA:
rc = bleproto_encjson_otadata_req(&desc->datast.otadata_req,
appdata.data);
appdata->data);
break;
case E_BLEPROTO_SERVICE_ID_RUNCMD_V2:
rc = ble_proto_pack(&desc->datast.runcmd_v2_req, appdata.data, sizeof(appdata.data));
rc = ble_proto_pack(&desc->datast.runcmd_v2_req, appdata->data, sizeof(appdata->data));
break;
case E_BLEPROTO_SERVICE_ID_REPORTCMD_V2:
rc = ble_proto_pack(&desc->datast.reportcmd_v2_req, appdata.data, sizeof(appdata.data));
rc = ble_proto_pack(&desc->datast.reportcmd_v2_req, appdata->data, sizeof(appdata->data));
break;
case E_BLEPROTO_SERVICE_ID_OTADATA_V2:
rc = ble_proto_pack(&desc->datast.otadata_v2_req, appdata.data, sizeof(appdata.data));
rc = ble_proto_pack(&desc->datast.otadata_v2_req, appdata->data, sizeof(appdata->data));
break;
default:
/* unkown serviceid */
return (-1);
}
}
else if (appdata.header.msgtype == BLEPROTO_APPDATA_MSGTYPE_RSP)
else if (appdata->header.msgtype == BLEPROTO_APPDATA_MSGTYPE_RSP)
{
switch (appdata.header.serviceid)
switch (appdata->header.serviceid)
{
case E_BLEPROTO_SERVICE_ID_DEVICEINFO:
rc = bleproto_encjson_deviceinfo_rsp(&desc->datast.deviceinfo_rsp,
appdata.data);
appdata->data);
break;
case E_BLEPROTO_SERVICE_ID_AUTHSETUP:
rc = bleproto_encjson_authsetup_rsp(&desc->datast.authsetup_rsp,
appdata.data);
appdata->data);
break;
case E_BLEPROTO_SERVICE_ID_AUTHDELETE:
rc = bleproto_encjson_authdelete_rsp(&desc->datast.authdelete_rsp,
appdata.data);
appdata->data);
break;
case E_BLEPROTO_SERVICE_ID_RUNCMD:
rc = bleproto_encjson_runcmd_rsp(&desc->datast.runcmd_rsp,
appdata.data);
appdata->data);
break;
case E_BLEPROTO_SERVICE_ID_REPORTCMD:
rc = bleproto_encjson_reportcmd_rsp(&desc->datast.reportcmd_rsp,
appdata.data);
appdata->data);
break;
case E_BLEPROTO_SERVICE_ID_DOACTION:
rc = bleproto_encjson_doaction_rsp(&desc->datast.doaction_rsp,
appdata.data);
appdata->data);
break;
case E_BLEPROTO_SERVICE_ID_OTANOTIFY:
rc = bleproto_encjson_otanotify_rsp(&desc->datast.otanotify_rsp,
appdata.data);
appdata->data);
break;
case E_BLEPROTO_SERVICE_ID_OTADATA:
rc = bleproto_encbin_otadata_rsp(&desc->datast.otadata_rsp,
appdata.data);
appdata->data);
break;
case E_BLEPROTO_SERVICE_ID_RUNCMD_V2:
rc = ble_proto_pack(&desc->datast.runcmd_v2_rsp, appdata.data, sizeof(appdata.data));
rc = ble_proto_pack(&desc->datast.runcmd_v2_rsp, appdata->data, sizeof(appdata->data));
break;
case E_BLEPROTO_SERVICE_ID_REPORTCMD_V2:
rc = ble_proto_pack(&desc->datast.reportcmd_v2_rsp, appdata.data, sizeof(appdata.data));
rc = ble_proto_pack(&desc->datast.reportcmd_v2_rsp, appdata->data, sizeof(appdata->data));
break;
case E_BLEPROTO_SERVICE_ID_OTADATA_V2:
rc = ble_proto_pack(&desc->datast.otadata_v2_rsp, appdata.data, sizeof(appdata.data));
rc = ble_proto_pack(&desc->datast.otadata_v2_rsp, appdata->data, sizeof(appdata->data));
break;
default:
/* unkown serviceid */
@@ -340,10 +347,10 @@ int bleproto_appdata_encode(uint8_t *txbuf,
}
/* datalen */
appdata.header.datalen = (uint16_t)rc;
appdata->header.datalen = (uint16_t)rc;
/* appdata_serialize */
return bleproto_appdata_serialize(&appdata, packetlen, txbuf, txbuf_size);
return bleproto_appdata_serialize(appdata, packetlen, txbuf, txbuf_size);
}
int bleproto_appdata_decode(uint8_t *rxbuf,
@@ -671,6 +671,55 @@ int bleproto_appdata_serialize(bleproto_appdata_t *appdata,
return (offset);
}
int bleproto_appdata_wrap_raw(uint8_t *txbuf,
uint16_t txbuf_size,
const bleproto_appdata_header_t *header,
const uint8_t *payload,
uint16_t payload_len)
{
uint16_t total;
if (!txbuf || !header) {
BLEPROTO_T_E("Invalid parameters");
return -1;
}
if (payload_len > 0 && !payload) {
BLEPROTO_T_E("Invalid payload");
return -1;
}
/* 单帧 this_frame_len 为 uint8,超过 255 需走分包 serialize */
if (payload_len > 255u) {
BLEPROTO_T_E("payload too long for single frame");
return -1;
}
total = (uint16_t)(BLEPROTO_APPDATA_HEADR_LEN + payload_len);
if (txbuf_size < total) {
BLEPROTO_T_E("txbuf too short");
return -1;
}
/* buf[0]: version[2:0] | datafmt[3:3] | msgtype[7:4] */
txbuf[0] = UTILS_BITFIELD_SET(header->version, 2, 0) |
UTILS_BITFIELD_SET(header->datafmt, 3, 3) |
UTILS_BITFIELD_SET(header->msgtype, 7, 4);
/* buf[1]: msgid[3:0] | encrypt[4:4] | reserve[7:5] */
txbuf[1] = UTILS_BITFIELD_SET(header->msgid, 3, 0) |
UTILS_BITFIELD_SET(header->encrypt, 4, 4) |
UTILS_BITFIELD_SET(header->reserve, 7, 5);
txbuf[2] = 1; /* totalframe */
txbuf[3] = 0; /* frameseq:单帧为 0 */
txbuf[4] = (uint8_t)payload_len; /* this frame payload len */
txbuf[5] = header->serviceid;
proto_bytes_put_le16(&txbuf[6], payload_len);
if (payload_len > 0) {
memcpy(&txbuf[BLEPROTO_APPDATA_HEADR_LEN], payload, payload_len);
}
return (int)total;
}
int bleproto_appdata_deserialize(uint8_t * buf,
uint16_t len,
bleproto_appdata_t *appdata)
@@ -1958,8 +2007,12 @@ int bleproto_encbin_otadata_rsp(const bleproto_otadata_rsp_t *p,
data[offset++] = (uint8_t)((p->len >> 16) & 0xFF);
data[offset++] = (uint8_t)((p->len >> 24) & 0xFF);
/* serialize data */
/* serialize datadata 为外部指针,不再内嵌数组) */
if (p->len > 0 && p->len <= BLEPROTO_APPDATA_OTADATA_SZ) {
if (!p->data) {
BLEPROTO_T_E("otadata_rsp.data is NULL");
return -1;
}
memcpy(&data[offset], p->data, p->len);
offset += p->len;
}
@@ -2984,10 +3037,12 @@ int bleproto_debin_otadata_rsp(const uint8_t * data,
return -1;
}
/* deserialize data */
/* 零拷贝:指向输入缓冲,调用方须在下次 decode 前用完 */
if (rsp->len > 0) {
memcpy(rsp->data, &data[offset], rsp->len);
rsp->data = &data[offset];
offset += rsp->len;
} else {
rsp->data = NULL;
}
return offset;
@@ -488,7 +488,12 @@ typedef struct bleproto_otadata_rsp {
int id; ///< file_id
int32_t offset; ///< 当前读取到的数据偏移
uint32_t len; ///< 当次读取数据的大小
uint8_t data[BLEPROTO_APPDATA_OTADATA_SZ]; ///< 读取的数据
/**
* 固件数据指针(不内嵌大数组,避免 bleproto_appdata_desc_t 膨胀到 2KB+
* - decode:指向 appdata 内部缓冲(零拷贝),下次 decode 前有效
* - encode:由调用方提供可读缓冲
*/
const uint8_t *data;
} bleproto_otadata_rsp_t;
/****************************************************************************/
/* Public Data */
@@ -585,6 +590,22 @@ int bleproto_appdata_serialize(bleproto_appdata_t *appdata,
uint8_t * buf,
uint16_t size);
/**
* @brief 将已打包好的 payload 包装成单帧 appdata(不分包)
* @param txbuf 输出缓冲
* @param txbuf_size 输出缓冲大小
* @param header 应用层头(datalen/totalframe/frameseq 由本函数填写)
* @param payload 已序列化的 service 数据;可为 NULLpayload_len==0
* @param payload_len payload 长度,须 <=255(单帧 pthis_frame_len 为 u8
* @return >0 总字节数;<0 失败
* @note 小包场景(MCU 55AA 透传等)用此接口,避免 bleproto_appdata_t 大缓冲
*/
int bleproto_appdata_wrap_raw(uint8_t *txbuf,
uint16_t txbuf_size,
const bleproto_appdata_header_t *header,
const uint8_t *payload,
uint16_t payload_len);
/**
* @brief 反序列化应用数据
*
@@ -62,7 +62,8 @@ void usr_le_data_recieve(u8 *data, u16 len) /// BLE数据接收
// log_info("le recieve: %d",len);
// printf_buf(data, len);
static u16 usr_len = 0;
bleproto_appdata_desc_t desc;
/* desc 含 ycmd 等 union,放 static,避免每次收包在栈上开大块 */
static bleproto_appdata_desc_t s_rx_desc;
int rc_result = 0;
@@ -76,7 +77,7 @@ void usr_le_data_recieve(u8 *data, u16 len) /// BLE数据接收
memcpy(&rxbuf[usr_len], data, len);
usr_len += len;
rc_result = bleproto_appdata_decode(rxbuf, usr_len, &desc);
rc_result = bleproto_appdata_decode(rxbuf, usr_len, &s_rx_desc);
// printf("rc_result_le## = %d\n",rc_result);
if (rc_result < 0)
{
@@ -94,13 +95,16 @@ void usr_le_data_recieve(u8 *data, u16 len) /// BLE数据接收
// 数据解析成功
printf("### pack recieved ok\n");
usr_len = 0;
usr_data_ana(&desc);
usr_data_ana(&s_rx_desc);
}
}
void usr_data_ana(bleproto_appdata_desc_t *desc)
{
bleproto_appdata_desc_t encode_desc = {0};
/* 应答侧 desc 同样较大,用 static,避免 BLE 任务栈峰值 */
static bleproto_appdata_desc_t s_encode_desc;
memset(&s_encode_desc, 0, sizeof(s_encode_desc));
printf("#############334 desc->header.serviceid = %d\n", desc->header.serviceid);
if (desc->header.msgtype /*desc.header.msgtype*/ == BLEPROTO_APPDATA_MSGTYPE_REQ)
{
@@ -109,15 +113,15 @@ void usr_data_ana(bleproto_appdata_desc_t *desc)
case E_BLEPROTO_SERVICE_ID_DEVICEINFO:
encode_desc.header.version = 0;
encode_desc.header.datafmt = BLEPROTO_APPDATA_DATAFMT_JSON;
encode_desc.header.msgtype = BLEPROTO_APPDATA_MSGTYPE_RSP;
encode_desc.header.msgid = desc->header.msgid; // 这里的msg_id为上面解析到的包的msg_id;
encode_desc.header.encrypt = 0;
encode_desc.header.serviceid = E_BLEPROTO_SERVICE_ID_DEVICEINFO;
s_encode_desc.header.version = 0;
s_encode_desc.header.datafmt = BLEPROTO_APPDATA_DATAFMT_JSON;
s_encode_desc.header.msgtype = BLEPROTO_APPDATA_MSGTYPE_RSP;
s_encode_desc.header.msgid = desc->header.msgid; // 这里的msg_id为上面解析到的包的msg_id;
s_encode_desc.header.encrypt = 0;
s_encode_desc.header.serviceid = E_BLEPROTO_SERVICE_ID_DEVICEINFO;
// deviceinfo rsp序列化
bleproto_deviceinfo_rsp_t *rsp = &encode_desc.datast.deviceinfo_rsp;
bleproto_deviceinfo_rsp_t *rsp = &s_encode_desc.datast.deviceinfo_rsp;
{
// 填写设备相关信息
rsp->hbcycle = 1000;
@@ -126,7 +130,7 @@ void usr_data_ana(bleproto_appdata_desc_t *desc)
rsp->longcon = 1;
rsp->prototype = 4;
}
encode_len = bleproto_appdata_encode(txbuf, sizeof(txbuf), sizeof(txbuf), &encode_desc);
encode_len = bleproto_appdata_encode(txbuf, sizeof(txbuf), sizeof(txbuf), &s_encode_desc);
if (encode_len > 0)
{
printf("encode_len = %d\n", encode_len);
@@ -146,14 +150,14 @@ void usr_data_ana(bleproto_appdata_desc_t *desc)
case E_BLEPROTO_SERVICE_ID_AUTHSETUP:
// 设备处理,回复响应
printf("E_BLEPROTO_SERVICE_ID_AUTHSETUP\n");
encode_desc.header.version = 0;
encode_desc.header.datafmt = BLEPROTO_APPDATA_DATAFMT_JSON;
encode_desc.header.msgtype = BLEPROTO_APPDATA_MSGTYPE_RSP;
encode_desc.header.msgid = desc->header.msgid; // 这里的msg_id为上面解析到的包的msg_id;
encode_desc.header.encrypt = 0;
encode_desc.header.serviceid = E_BLEPROTO_SERVICE_ID_AUTHSETUP;
s_encode_desc.header.version = 0;
s_encode_desc.header.datafmt = BLEPROTO_APPDATA_DATAFMT_JSON;
s_encode_desc.header.msgtype = BLEPROTO_APPDATA_MSGTYPE_RSP;
s_encode_desc.header.msgid = desc->header.msgid; // 这里的msg_id为上面解析到的包的msg_id;
s_encode_desc.header.encrypt = 0;
s_encode_desc.header.serviceid = E_BLEPROTO_SERVICE_ID_AUTHSETUP;
bleproto_authsetup_rsp_t *au_rsp = &encode_desc.datast.authsetup_rsp;
bleproto_authsetup_rsp_t *au_rsp = &s_encode_desc.datast.authsetup_rsp;
au_rsp->errcode = 0;
memcpy(&usr_auth_data, &desc->datast.authsetup_req, sizeof(usr_auth_data));
@@ -168,7 +172,7 @@ void usr_data_ana(bleproto_appdata_desc_t *desc)
}
}
encode_len = bleproto_appdata_encode(txbuf, sizeof(txbuf), sizeof(txbuf), &encode_desc);
encode_len = bleproto_appdata_encode(txbuf, sizeof(txbuf), sizeof(txbuf), &s_encode_desc);
if (encode_len > 0)
{
printf("encode_len = %d\n", encode_len);
@@ -182,14 +186,14 @@ void usr_data_ana(bleproto_appdata_desc_t *desc)
case E_BLEPROTO_SERVICE_ID_AUTHDELETE:
// 设备处理,回复响应
printf("E_BLEPROTO_SERVICE_ID_AUTHDELETE\n");
encode_desc.header.version = 0;
encode_desc.header.datafmt = BLEPROTO_APPDATA_DATAFMT_JSON;
encode_desc.header.msgtype = BLEPROTO_APPDATA_MSGTYPE_RSP;
encode_desc.header.msgid = desc->header.msgid; // 这里的msg_id为上面解析到的包的msg_id;
encode_desc.header.encrypt = 0;
encode_desc.header.serviceid = E_BLEPROTO_SERVICE_ID_AUTHSETUP;
s_encode_desc.header.version = 0;
s_encode_desc.header.datafmt = BLEPROTO_APPDATA_DATAFMT_JSON;
s_encode_desc.header.msgtype = BLEPROTO_APPDATA_MSGTYPE_RSP;
s_encode_desc.header.msgid = desc->header.msgid; // 这里的msg_id为上面解析到的包的msg_id;
s_encode_desc.header.encrypt = 0;
s_encode_desc.header.serviceid = E_BLEPROTO_SERVICE_ID_AUTHSETUP;
bleproto_authdelete_rsp_t *aude_rsp = &encode_desc.datast.authdelete_rsp;
bleproto_authdelete_rsp_t *aude_rsp = &s_encode_desc.datast.authdelete_rsp;
memcpy(aude_rsp->gwmac, usr_auth_data.gwmac, 6);
if (!memcmp(usr_auth_data.authcode, desc->datast.authdelete_req.authcode, 32))
{
@@ -201,7 +205,7 @@ void usr_data_ana(bleproto_appdata_desc_t *desc)
aude_rsp->errcode = 501;
}
encode_len = bleproto_appdata_encode(txbuf, sizeof(txbuf), sizeof(txbuf), &encode_desc);
encode_len = bleproto_appdata_encode(txbuf, sizeof(txbuf), sizeof(txbuf), &s_encode_desc);
if (encode_len > 0)
{
printf("encode_len = %d\n", encode_len);
@@ -222,15 +226,15 @@ void usr_data_ana(bleproto_appdata_desc_t *desc)
break;
case E_BLEPROTO_SERVICE_ID_OTANOTIFY:
printf("E_BLEPROTO_SERVICE_ID_OTANOTIFY\n");
encode_desc.header.version = 0;
encode_desc.header.datafmt = BLEPROTO_APPDATA_DATAFMT_JSON;
encode_desc.header.msgtype = BLEPROTO_APPDATA_MSGTYPE_RSP;
encode_desc.header.msgid = desc->header.msgid; // 这里的msg_id为上面解析到的包的msg_id;
encode_desc.header.encrypt = 0;
encode_desc.header.serviceid = E_BLEPROTO_SERVICE_ID_OTANOTIFY;
encode_desc.datast.doaction_rsp.errcode = 0;
s_encode_desc.header.version = 0;
s_encode_desc.header.datafmt = BLEPROTO_APPDATA_DATAFMT_JSON;
s_encode_desc.header.msgtype = BLEPROTO_APPDATA_MSGTYPE_RSP;
s_encode_desc.header.msgid = desc->header.msgid; // 这里的msg_id为上面解析到的包的msg_id;
s_encode_desc.header.encrypt = 0;
s_encode_desc.header.serviceid = E_BLEPROTO_SERVICE_ID_OTANOTIFY;
s_encode_desc.datast.doaction_rsp.errcode = 0;
bleproto_otanotify_rsp_t *in_ota_rsp = &encode_desc.datast.otanotify_rsp;
bleproto_otanotify_rsp_t *in_ota_rsp = &s_encode_desc.datast.otanotify_rsp;
bleproto_otanotify_req_t *in_ota_data2 = &desc->datast.otanotify_req;
printf("in_ota_data id = %d\n", in_ota_data2->id);
@@ -244,7 +248,7 @@ void usr_data_ana(bleproto_appdata_desc_t *desc)
usr_var.usr_ota_data_id = in_ota_data2->id; // int
usr_var.usr_ota_data_size = in_ota_data2->size;
encode_len = bleproto_appdata_encode(txbuf, sizeof(txbuf), sizeof(txbuf), &encode_desc);
encode_len = bleproto_appdata_encode(txbuf, sizeof(txbuf), sizeof(txbuf), &s_encode_desc);
if (encode_len > 0)
{
printf("encode_len = %d\n", encode_len);
@@ -265,18 +269,18 @@ void usr_data_ana(bleproto_appdata_desc_t *desc)
printf("#### buf total size = %d\n", usr_var.usr_ota_data_size);
printf_buf(start_data, 10);
encode_desc.header.version = 0;
encode_desc.header.datafmt = BLEPROTO_APPDATA_DATAFMT_JSON;
encode_desc.header.msgtype = BLEPROTO_APPDATA_MSGTYPE_REQ;
encode_desc.header.msgid = 0; // desc->header.msgid;
encode_desc.header.encrypt = 0;
encode_desc.header.serviceid = E_BLEPROTO_SERVICE_ID_OTADATA;
encode_desc.datast.doaction_rsp.errcode = 0;
s_encode_desc.header.version = 0;
s_encode_desc.header.datafmt = BLEPROTO_APPDATA_DATAFMT_JSON;
s_encode_desc.header.msgtype = BLEPROTO_APPDATA_MSGTYPE_REQ;
s_encode_desc.header.msgid = 0; // desc->header.msgid;
s_encode_desc.header.encrypt = 0;
s_encode_desc.header.serviceid = E_BLEPROTO_SERVICE_ID_OTADATA;
s_encode_desc.datast.doaction_rsp.errcode = 0;
encode_desc.datast.otadata_req.id = usr_var.usr_ota_data_id;
encode_desc.datast.otadata_req.offset = usr_var.usr_data_index;
encode_desc.datast.otadata_req.maxsize = FIRM_PACK_LEN;
encode_len = bleproto_appdata_encode(txbuf, sizeof(txbuf), sizeof(txbuf), &encode_desc);
s_encode_desc.datast.otadata_req.id = usr_var.usr_ota_data_id;
s_encode_desc.datast.otadata_req.offset = usr_var.usr_data_index;
s_encode_desc.datast.otadata_req.maxsize = FIRM_PACK_LEN;
encode_len = bleproto_appdata_encode(txbuf, sizeof(txbuf), sizeof(txbuf), &s_encode_desc);
if (encode_len > 0)
{
usr_ble_send_data(txbuf, encode_len);
@@ -334,7 +338,7 @@ void usr_data_ana(bleproto_appdata_desc_t *desc)
case E_BLEPROTO_SERVICE_ID_OTADATA:
bleproto_otadata_rsp_t *ota_data_rsp = &desc->datast.otadata_rsp;
// printf("errcode = %d offset = %d len = %d\n",ota_data_rsp->errcode,ota_data_rsp->offset,ota_data_rsp->len);
uart_tr_send_data(ota_data_rsp->data, ota_data_rsp->len);
uart_tr_send_data((u8 *)ota_data_rsp->data, ota_data_rsp->len);
memcpy(&ota_source_data[usr_var.usr_allow_to_flash], ota_data_rsp->data, ota_data_rsp->len);
usr_var.usr_allow_to_flash += ota_data_rsp->len;
usr_var.usr_start_write_flash = 0;
@@ -354,18 +358,18 @@ void usr_data_ana(bleproto_appdata_desc_t *desc)
break;
}
// bleproto_appdata_desc_t encode_desc = { 0 };
encode_desc.header.version = 0;
encode_desc.header.datafmt = BLEPROTO_APPDATA_DATAFMT_JSON;
encode_desc.header.msgtype = BLEPROTO_APPDATA_MSGTYPE_REQ;
encode_desc.header.msgid = 0; // desc->header.msgid;
encode_desc.header.encrypt = 0;
encode_desc.header.serviceid = E_BLEPROTO_SERVICE_ID_OTADATA;
encode_desc.datast.doaction_rsp.errcode = 0;
s_encode_desc.header.version = 0;
s_encode_desc.header.datafmt = BLEPROTO_APPDATA_DATAFMT_JSON;
s_encode_desc.header.msgtype = BLEPROTO_APPDATA_MSGTYPE_REQ;
s_encode_desc.header.msgid = 0; // desc->header.msgid;
s_encode_desc.header.encrypt = 0;
s_encode_desc.header.serviceid = E_BLEPROTO_SERVICE_ID_OTADATA;
s_encode_desc.datast.doaction_rsp.errcode = 0;
encode_desc.datast.otadata_req.id = usr_var.usr_ota_data_id;
encode_desc.datast.otadata_req.offset = usr_var.usr_data_index;
encode_desc.datast.otadata_req.maxsize = FIRM_PACK_LEN;
encode_len = bleproto_appdata_encode(txbuf, sizeof(txbuf), sizeof(txbuf), &encode_desc);
s_encode_desc.datast.otadata_req.id = usr_var.usr_ota_data_id;
s_encode_desc.datast.otadata_req.offset = usr_var.usr_data_index;
s_encode_desc.datast.otadata_req.maxsize = FIRM_PACK_LEN;
encode_len = bleproto_appdata_encode(txbuf, sizeof(txbuf), sizeof(txbuf), &s_encode_desc);
if (encode_len > 0)
{
usr_ble_send_data(txbuf, encode_len);
@@ -386,7 +390,7 @@ void usr_data_ana(bleproto_appdata_desc_t *desc)
void usr_ota_deal_task()
{
os_time_dly(5);
bleproto_appdata_desc_t encode_desc = {0};
static bleproto_appdata_desc_t s_ota_encode_desc;
while (1)
{
if (usr_var.usr_ota_succ_flag == 0)
@@ -402,18 +406,19 @@ void usr_ota_deal_task()
// printf("usr_var.usr_allow_to_flash = %d\n",usr_var.usr_allow_to_flash);
usr_var.usr_allow_to_flash = 0;
encode_desc.header.version = 0;
encode_desc.header.datafmt = BLEPROTO_APPDATA_DATAFMT_JSON;
encode_desc.header.msgtype = BLEPROTO_APPDATA_MSGTYPE_REQ;
encode_desc.header.msgid = 0; // desc->header.msgid;
encode_desc.header.encrypt = 0;
encode_desc.header.serviceid = E_BLEPROTO_SERVICE_ID_OTADATA;
encode_desc.datast.doaction_rsp.errcode = 0;
memset(&s_ota_encode_desc, 0, sizeof(s_ota_encode_desc));
s_ota_encode_desc.header.version = 0;
s_ota_encode_desc.header.datafmt = BLEPROTO_APPDATA_DATAFMT_JSON;
s_ota_encode_desc.header.msgtype = BLEPROTO_APPDATA_MSGTYPE_REQ;
s_ota_encode_desc.header.msgid = 0; // desc->header.msgid;
s_ota_encode_desc.header.encrypt = 0;
s_ota_encode_desc.header.serviceid = E_BLEPROTO_SERVICE_ID_OTADATA;
s_ota_encode_desc.datast.doaction_rsp.errcode = 0;
encode_desc.datast.otadata_req.id = usr_var.usr_ota_data_id;
encode_desc.datast.otadata_req.offset = usr_var.usr_data_index;
encode_desc.datast.otadata_req.maxsize = FIRM_PACK_LEN;
encode_len = bleproto_appdata_encode(txbuf, sizeof(txbuf), sizeof(txbuf), &encode_desc);
s_ota_encode_desc.datast.otadata_req.id = usr_var.usr_ota_data_id;
s_ota_encode_desc.datast.otadata_req.offset = usr_var.usr_data_index;
s_ota_encode_desc.datast.otadata_req.maxsize = FIRM_PACK_LEN;
encode_len = bleproto_appdata_encode(txbuf, sizeof(txbuf), sizeof(txbuf), &s_ota_encode_desc);
if (encode_len > 0)
{
usr_ble_send_data(txbuf, encode_len);
@@ -475,13 +480,18 @@ void usr_run_cmd_code_v2(ble_proto_packet_t *runcmd_par, u8 usr_msg_id)
* @param data 55AA 完整帧
* @param len 帧长度
* @note 主动上报(0x07/0x34/0x38) → REPORTCMD_V2;其余 → RUNCMD_V2 应答
* @note 轻量路径:栈上仅 ble_proto_packet_t + 小 v2 缓冲,
* 不走 bleproto_appdata_desc_t / bleproto_appdata_encode(约 2.3KB×2
*/
void usr_le_send_mcu_frame(u8 *data, u16 len)
{
bleproto_appdata_desc_t encode_desc = {0};
ble_proto_packet_t *pkt = NULL;
ble_proto_packet_t pkt;
bleproto_appdata_header_t hdr;
/* V2开销:Hdr6+gw_cnt1+gw_int8+dev6+CRC2=23MCU帧最大128 → 需约151 */
u8 v2_buf[MCU_FRAME_MAX_LEN + 32];
u8 mcu_cmd;
u8 is_active_send;
int plen;
int elen;
if (!data || len < 5)
@@ -489,50 +499,60 @@ void usr_le_send_mcu_frame(u8 *data, u16 len)
printf("usr_le_send_mcu_frame invalid param, len=%d\n", len);
return;
}
if (len > MCU_FRAME_MAX_LEN)
{
printf("usr_le_send_mcu_frame frame too long %u\n", len);
return;
}
mcu_cmd = data[4];
is_active_send = (mcu_cmd == 0x07) || (mcu_cmd == 0x34) || (mcu_cmd == 0x38);
encode_desc.header.version = 0;
encode_desc.header.datafmt = BLEPROTO_APPDATA_DATAFMT_JSON;
encode_desc.header.encrypt = 0;
memset(&pkt, 0, sizeof(pkt));
memset(&hdr, 0, sizeof(hdr));
hdr.version = 0;
hdr.datafmt = BLEPROTO_APPDATA_DATAFMT_JSON;
hdr.encrypt = 0;
if (is_active_send)
{
encode_desc.header.msgtype = BLEPROTO_APPDATA_MSGTYPE_REQ;
encode_desc.header.msgid = 0;
encode_desc.header.serviceid = E_BLEPROTO_SERVICE_ID_REPORTCMD_V2;
hdr.msgtype = BLEPROTO_APPDATA_MSGTYPE_REQ;
hdr.msgid = 0;
hdr.serviceid = E_BLEPROTO_SERVICE_ID_REPORTCMD_V2;
pkt = &encode_desc.datast.reportcmd_v2_req;
pkt->cmd = 100;
pkt->reserved = 0;
pkt->gateway_int_cnt = 0;
pkt.cmd = 100;
pkt.gateway_int_cnt = 0;
}
else
{
encode_desc.header.msgtype = BLEPROTO_APPDATA_MSGTYPE_RSP;
encode_desc.header.msgid = s_last_runcmd_msgid;
encode_desc.header.serviceid = E_BLEPROTO_SERVICE_ID_RUNCMD_V2;
hdr.msgtype = BLEPROTO_APPDATA_MSGTYPE_RSP;
hdr.msgid = s_last_runcmd_msgid;
hdr.serviceid = E_BLEPROTO_SERVICE_ID_RUNCMD_V2;
pkt = &encode_desc.datast.runcmd_v2_rsp;
pkt->cmd = 110;
pkt->reserved = 0;
pkt->gateway_int_cnt = 2;
pkt->gateway_int[0] = 0;
pkt->gateway_int[1] = 0;
pkt.cmd = 110;
pkt.gateway_int_cnt = 2;
pkt.gateway_int[0] = 0;
pkt.gateway_int[1] = 0;
}
pkt->device_int_len = 0;
pkt->device_str_len = 0;
pkt->device_bytes_len = len;
pkt->bytes_data = data;
pkt.device_bytes_len = len;
pkt.bytes_data = data;
elen = bleproto_appdata_encode(txbuf, sizeof(txbuf), sizeof(txbuf), &encode_desc);
plen = ble_proto_pack(&pkt, v2_buf, sizeof(v2_buf));
if (plen <= 0)
{
printf("usr_le_send_mcu_frame pack fail %d\n", plen);
return;
}
elen = bleproto_appdata_wrap_raw(txbuf, sizeof(txbuf), &hdr, v2_buf, (uint16_t)plen);
printf("usr_le_send_mcu_frame cmd=0x%02X active=%d encode_len=%d msgid=%d\n",
mcu_cmd, is_active_send, elen, encode_desc.header.msgid);
mcu_cmd, is_active_send, elen, hdr.msgid);
if (elen > 0)
{
usr_ble_send_data(txbuf, elen);
printf("send data:");
printf_buf(txbuf, elen);
}
}
@@ -52,7 +52,8 @@
#define MCU_BRIDGE_TICK_OS ((MCU_BRIDGE_TICK_MS + 9u) / 10u)
#define MCU_EVT_QUEUE_DEPTH (8u) /* 事件队列深度 */
#define MCU_BRIDGE_TASK_PRIO (31u)
#define MCU_BRIDGE_TASK_STACK (1024u)
/* 周期逻辑有 MCU_FRAME_MAX_LEN 局部缓冲;BLE 回传大结构已改为 static2KB 留余量 */
#define MCU_BRIDGE_TASK_STACK (2048u)
/*============================================================================*/
/* 外部符号 */
@@ -1000,6 +1001,8 @@ void mcu_bridge_on_uart_rx(const uint8_t *data, uint16_t len)
evt.type = MCU_EVT_UART_FRAME;
evt.len = out_len;
memcpy(evt.buf, s_parse_out, out_len);
printf("UART valid data:");
printf_buf(evt.buf, evt.len);
mcu_bridge_post_event(&evt);
}
}
@@ -73,9 +73,8 @@ static void mcu_uart_rx_task(void *arg)
if (rxcnt > 0) {
mcu_uart_rx_keep_awake(); /* 真正收到数据,禁止立刻再睡 */
log_debug("uart rx cnt=%d", rxcnt);
for (int i = 0; i < (int)rxcnt; i++) {
log_debug("uart rx data[%d]=%02X", i, s_rx_tmp[i]);
}
printf("uart rx data:");
printf_buf(s_rx_tmp, rxcnt);
mcu_bridge_on_uart_rx(s_rx_tmp, (uint16_t)rxcnt);
}
}
@@ -155,6 +154,9 @@ McuUart_Ret_t mcu_uart_send(const uint8_t *data, uint16_t len)
s_uart_bus->write((void *)data, len);
os_mutex_post(&s_tx_lock);
printf("uart tx data:");
printf_buf(data, len);
return MCU_UART_OK;
}