diff --git a/protocol.h b/protocol.h index baa9232..e8598d5 100644 --- a/protocol.h +++ b/protocol.h @@ -112,8 +112,6 @@ typedef struct { } data; } hand_acid_ble_proto_t; -#pragma pack() - static inline bool hand_acid_ble_proto_check_packet(hand_acid_ble_proto_t *packet) { uint8_t *pu8 = (uint8_t *)packet; @@ -138,6 +136,29 @@ static inline bool hand_acid_ble_proto_check_packet(hand_acid_ble_proto_t *packe return true; } +static inline uint8_t hand_acid_ble_proto_sum(hand_acid_ble_proto_t *packet) { + uint8_t *_packet = (uint8_t *)packet; + uint8_t sum = 0; + for (int i = 0; i < packet->packetlen - 2; i++) { + sum += _packet[i]; + } + return sum; +} + +#define PACKET_SET_TAIL(packet) \ + { \ + uint8_t *__data = (uint8_t *)packet; \ + __data[packet->packetlen - 1] = PACKET_TAIL; \ + } + +#define PACKET_SET_CHECKSUM(packet) \ + { \ + uint8_t *__data = (uint8_t *)packet; \ + __data[packet->packetlen - 2] = hand_acid_ble_proto_sum(packet); \ + } + +#pragma pack() + #ifdef __cplusplus } #endif