1、关闭1分钟没重连就切慢广播的功能,修复1分钟之后重连不上网关的问题
2、调整最大数据包长度至512,优化BLE数据打包函数以支持分包
This commit is contained in:
@@ -673,51 +673,89 @@ int bleproto_appdata_serialize(bleproto_appdata_t *appdata,
|
||||
|
||||
int bleproto_appdata_wrap_raw(uint8_t *txbuf,
|
||||
uint16_t txbuf_size,
|
||||
uint8_t packetlen,
|
||||
const bleproto_appdata_header_t *header,
|
||||
const uint8_t *payload,
|
||||
uint16_t payload_len)
|
||||
{
|
||||
uint16_t total;
|
||||
uint8_t frame_max;
|
||||
uint16_t totalframe;
|
||||
uint16_t need;
|
||||
uint16_t offset = 0;
|
||||
uint16_t data_offset = 0;
|
||||
uint16_t i;
|
||||
|
||||
if (!txbuf || !header) {
|
||||
BLEPROTO_T_E("Invalid parameters");
|
||||
if (!txbuf || !header || packetlen <= BLEPROTO_APPDATA_HEADR_LEN) {
|
||||
printf("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");
|
||||
printf("Invalid payload");
|
||||
return -1;
|
||||
}
|
||||
|
||||
total = (uint16_t)(BLEPROTO_APPDATA_HEADR_LEN + payload_len);
|
||||
if (txbuf_size < total) {
|
||||
BLEPROTO_T_E("txbuf too short");
|
||||
/* 与 serialize 相同:一帧总长 packetlen,其中 8B 头,其余为 payload */
|
||||
frame_max = (uint8_t)(packetlen - BLEPROTO_APPDATA_HEADR_LEN);
|
||||
|
||||
if (payload_len == 0) {
|
||||
totalframe = 1;
|
||||
} else {
|
||||
totalframe = (uint16_t)((payload_len + frame_max - 1u) / frame_max);
|
||||
}
|
||||
if (totalframe > 255u) {
|
||||
printf("payload too long, frames %u>255\n", (unsigned)totalframe);
|
||||
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);
|
||||
need = (uint16_t)((uint16_t)totalframe * BLEPROTO_APPDATA_HEADR_LEN + payload_len);
|
||||
if (txbuf_size < need) {
|
||||
printf("txbuf too short %u<%u\n", (unsigned)txbuf_size, (unsigned)need);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return (int)total;
|
||||
for (i = 0; i < totalframe; i++) {
|
||||
uint8_t this_len;
|
||||
uint8_t frameseq;
|
||||
|
||||
if (totalframe == 1u) {
|
||||
this_len = (uint8_t)payload_len;
|
||||
frameseq = 0; /* 单帧序号为 0 */
|
||||
} else if ((uint16_t)(i + 1u) < totalframe) {
|
||||
this_len = frame_max;
|
||||
frameseq = (uint8_t)(i + 1u); /* 分包从 1 起 */
|
||||
} else {
|
||||
this_len = (uint8_t)(payload_len - data_offset);
|
||||
frameseq = (uint8_t)(i + 1u);
|
||||
}
|
||||
|
||||
/* buf[0]: version[2:0] | datafmt[3:3] | msgtype[7:4] */
|
||||
txbuf[offset + 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[offset + 1] = UTILS_BITFIELD_SET(header->msgid, 3, 0) |
|
||||
UTILS_BITFIELD_SET(header->encrypt, 4, 4) |
|
||||
UTILS_BITFIELD_SET(header->reserve, 7, 5);
|
||||
txbuf[offset + 2] = (uint8_t)totalframe;
|
||||
txbuf[offset + 3] = frameseq;
|
||||
txbuf[offset + 4] = this_len;
|
||||
txbuf[offset + 5] = header->serviceid;
|
||||
proto_bytes_put_le16(&txbuf[offset + 6], payload_len);
|
||||
|
||||
offset = (uint16_t)(offset + BLEPROTO_APPDATA_HEADR_LEN);
|
||||
if (this_len != 0) {
|
||||
memcpy(&txbuf[offset], &payload[data_offset], this_len);
|
||||
offset = (uint16_t)(offset + this_len);
|
||||
data_offset = (uint16_t)(data_offset + this_len);
|
||||
}
|
||||
}
|
||||
|
||||
if (totalframe > 1u) {
|
||||
printf("wrap_raw split %uB -> %u frames, out %uB\n",
|
||||
(unsigned)payload_len, (unsigned)totalframe, (unsigned)offset);
|
||||
}
|
||||
|
||||
return (int)offset;
|
||||
}
|
||||
|
||||
int bleproto_appdata_deserialize(uint8_t * buf,
|
||||
|
||||
Reference in New Issue
Block a user