feat(gml670x4): 所有协议帧添加 CRC-8/MAXIM 校验

使用 Zephyr crc8() 对帧头之后的载荷计算 CRC,上行发送前填充、
下行接收时校验不通过则丢弃并告警。同步更新协议文档。

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-05-09 14:38:28 +08:00
parent 0d2fd30b3a
commit dc525ea4e7
6 changed files with 61 additions and 29 deletions
+15
View File
@@ -78,6 +78,11 @@ static void nus_received_cb(struct bt_conn *conn, const uint8_t *data, uint16_t
switch (data[2]) {
case PROTO_TYPE_RESISTANCE:
if (len >= sizeof(struct resistance_frame_t)) {
uint8_t crc = crc8(data + 2, sizeof(struct resistance_frame_t) - 3, 0x31, 0x00, true);
if (data[sizeof(struct resistance_frame_t) - 1] != crc) {
LOG_WRN("Resistance frame CRC mismatch");
break;
}
union resistance_pkt_t pkt;
memcpy(pkt.bytes, data, sizeof(struct resistance_frame_t));
resistance_K = pkt.frame.K;
@@ -90,6 +95,11 @@ static void nus_received_cb(struct bt_conn *conn, const uint8_t *data, uint16_t
break;
case PROTO_TYPE_SPOTTER:
if (len >= sizeof(struct spotter_frame_t)) {
uint8_t crc = crc8(data + 2, sizeof(struct spotter_frame_t) - 3, 0x31, 0x00, true);
if (data[sizeof(struct spotter_frame_t) - 1] != crc) {
LOG_WRN("Spotter frame CRC mismatch");
break;
}
union spotter_pkt_t pkt;
memcpy(pkt.bytes, data, sizeof(struct spotter_frame_t));
spotter_threshold = pkt.frame.threshold;
@@ -99,6 +109,11 @@ static void nus_received_cb(struct bt_conn *conn, const uint8_t *data, uint16_t
break;
case PROTO_TYPE_HEARTBEAT:
if (len >= sizeof(struct heartbeat_frame_t)) {
uint8_t crc = crc8(data + 2, sizeof(struct heartbeat_frame_t) - 3, 0x31, 0x00, true);
if (data[sizeof(struct heartbeat_frame_t) - 1] != crc) {
LOG_WRN("Heartbeat frame CRC mismatch");
break;
}
last_heartbeat_ms = k_uptime_get();
LOG_DBG("RX Heartbeat");
}
+2 -3
View File
@@ -23,9 +23,8 @@ int main(void) {
ble_transport_adv_start();
if (sensor_init()) return 0;
while (1) {
k_sleep(K_SECONDS(1));
}
k_sleep(K_FOREVER);
return 0;
}
+1
View File
@@ -163,6 +163,7 @@ static void sensor_thread_fn(void *p1, void *p2, void *p3) {
pkt.frame.cop_x = cop_x;
pkt.frame.cop_y = cop_y;
pkt.frame.force = force;
pkt.frame.crc = crc8(pkt.bytes + 2, sizeof(pkt.bytes) - 3, 0x31, 0x00, true);
/* 发送 */
ble_transport_send(pkt.bytes, sizeof(pkt.bytes));