fix: 修正 lup_append_checksum total_len 计算错误 (CMD 重复计数)

Bug: 1 + 3 + LEN + 2 把 CMD 计入两次 (Header(3) 和 Value(LEN) 各含一次)
Fix: 5 + LEN = Magic(1)+Addr(1)+LEN(1)+Value(LEN)+Check(2)

影响: 校验字节偏移1位,导致 XOR 写到错误位置,地感MCU 校验失败无响应
同时修正 lup_build_sensor_ack 的 padding 长度和返回值
This commit is contained in:
wangfq
2026-07-02 10:55:37 +08:00
parent e9c24ae736
commit eb7419b3e8

View File

@@ -92,8 +92,10 @@ int lup_verify_checksum(const uint8_t *pkg, uint16_t len)
*/ */
void lup_append_checksum(uint8_t *pkg) void lup_append_checksum(uint8_t *pkg)
{ {
// pkg[0]=7F, pkg[1]=Addr, pkg[2]=LEN, pkg[3]=CMD, ... // Frame: [Magic][Addr][LEN][Value(LEN bytes)][XOR][SUM]
uint8_t total_len = 1 + 3 + pkg[2] + 2; // Magic(1) + Header(3) + Value(LEN) + Check(2) // Total = 1 + 1 + 1 + LEN + 2 = 5 + LEN
// (Addr + LEN_field are NOT inside Value; CMD is the 1st byte of Value)
uint8_t total_len = 5 + pkg[2]; // Magic(1)+Addr(1)+LEN(1)+Value(LEN)+Check(2)
uint16_t check_len = 2 + pkg[2]; // Addr(1)+LEN_field(1)+Value(LEN) uint16_t check_len = 2 + pkg[2]; // Addr(1)+LEN_field(1)+Value(LEN)
pkg[total_len - 2] = lup_calc_xor(pkg + 1, check_len); pkg[total_len - 2] = lup_calc_xor(pkg + 1, check_len);
@@ -248,7 +250,7 @@ uint16_t lup_build_sensor_ack(uint8_t *buf, uint8_t sens_type,
{ {
buf[0] = LUP_MAGIC; buf[0] = LUP_MAGIC;
buf[1] = LUP_ADDR_DEFAULT; buf[1] = LUP_ADDR_DEFAULT;
buf[2] = 0x07; // LEN = 7 (CMD + SensType + Seq + SubAmount) buf[2] = 0x07; // LEN = 7 (CMD + SensType + Seq + SubAmount + 3 padding)
buf[3] = LUP_CMD_SENSOR_REPORT; buf[3] = LUP_CMD_SENSOR_REPORT;
buf[4] = sens_type; buf[4] = sens_type;
buf[5] = seq; buf[5] = seq;
@@ -256,9 +258,8 @@ uint16_t lup_build_sensor_ack(uint8_t *buf, uint8_t sens_type,
buf[7] = 0x00; // padding buf[7] = 0x00; // padding
buf[8] = 0x00; buf[8] = 0x00;
buf[9] = 0x00; buf[9] = 0x00;
buf[10] = 0x00;
lup_append_checksum(buf); lup_append_checksum(buf);
return 13; // Magic(1)+Header(3)+Value(7)+Check(2) return 5 + buf[2]; // = 12: Magic+Addr+LEN+Value(7)+Check(2)
} }
/*=========================================================================== /*===========================================================================