diff --git a/app/src/ble_data_processer_utils.c b/app/src/ble_data_processer_utils.c index 3817845..8401c3c 100644 --- a/app/src/ble_data_processer_utils.c +++ b/app/src/ble_data_processer_utils.c @@ -5,45 +5,75 @@ static uint8_t _bletxbuf[BLE_MSG_BUF_SIZE]; static uint8_t blereportbuf[BLE_MSG_BUF_SIZE]; +// PACKET_H1 +// PACKET_H2 +// PACKET_TAIL + void bletxbuf_clear() { memset(_bletxbuf, 0, BLE_MSG_BUF_SIZE); } hand_acid_ble_proto_t* bletxbuf_get() { return (hand_acid_ble_proto_t*)_bletxbuf; } +static uint8_t compute_sum(uint8_t* data, size_t len) { + uint8_t sum = 0; + for (size_t i = 0; i < len; i++) { + sum += data[i]; + } + return sum; +} + void send_error_receipt(hand_acid_ble_proto_t* rxpacket, int32_t errorcode) { hand_acid_ble_proto_t* txheader = (hand_acid_ble_proto_t*)_bletxbuf; - uint16_t sendlen = HAND_ACID_BLE_BASE_SIZE + sizeof(txheader->data.errreceipt); - + uint16_t sendlen = HAND_ACID_BLE_BASE_SIZE + sizeof(txheader->data.errreceipt) + 2; + txheader->h1 = PACKET_H1; + txheader->h2 = PACKET_H2; txheader->cmd = rxpacket->cmd; txheader->frame_index = rxpacket->frame_index; txheader->frame_type = kproto_error_receipt; txheader->data.errreceipt.ecode = errorcode; + // 校验 + txheader->packetlen = sendlen; + _bletxbuf[sendlen - 2] = compute_sum(_bletxbuf, sendlen - 2); + _bletxbuf[sendlen - 1] = PACKET_TAIL; zdatachannel_data_send2(_bletxbuf, sendlen); } void send_success_receipt(hand_acid_ble_proto_t* rxpacket) { hand_acid_ble_proto_t* txheader = (hand_acid_ble_proto_t*)_bletxbuf; uint16_t sendlen = HAND_ACID_BLE_BASE_SIZE; - - txheader->cmd = rxpacket->cmd; - txheader->frame_index = rxpacket->frame_index; - txheader->frame_type = kproto_cmd_receipt; + txheader->h1 = PACKET_H1; + txheader->h2 = PACKET_H2; + txheader->cmd = rxpacket->cmd; + txheader->frame_index = rxpacket->frame_index; + txheader->frame_type = kproto_cmd_receipt; if (txheader->cmd == kproto_cmd_read_power) { - sendlen = HAND_ACID_BLE_BASE_SIZE + sizeof(txheader->data.read_power); + sendlen = HAND_ACID_BLE_BASE_SIZE + sizeof(txheader->data.read_power) + 2; } else if (txheader->cmd == kproto_cmd_read_version) { - sendlen = HAND_ACID_BLE_BASE_SIZE + sizeof(txheader->data.read_version); + sendlen = HAND_ACID_BLE_BASE_SIZE + sizeof(txheader->data.read_version) + 2; } + txheader->packetlen = sendlen; + + // 校验 + _bletxbuf[sendlen - 2] = compute_sum(_bletxbuf, sendlen - 2); + _bletxbuf[sendlen - 1] = PACKET_TAIL; zdatachannel_data_send2(_bletxbuf, sendlen); } void send_key_event(uint8_t keyid, uint8_t keyevent) { static uint8_t report_index; - hand_acid_ble_proto_t* reportpacket = (hand_acid_ble_proto_t*)blereportbuf; + hand_acid_ble_proto_t* reportpacket = (hand_acid_ble_proto_t*)blereportbuf; + uint16_t sendlen = HAND_ACID_BLE_BASE_SIZE + sizeof(reportpacket->data.key_event) + 2; + + reportpacket->h1 = PACKET_H1; + reportpacket->h2 = PACKET_H2; reportpacket->frame_type = kproto_report; reportpacket->frame_index = report_index++; reportpacket->cmd = kproto_report_key_event; reportpacket->data.key_event.keyid = keyid; reportpacket->data.key_event.keyevent = keyevent; + reportpacket->packetlen = sendlen; + _bletxbuf[sendlen - 2] = compute_sum(blereportbuf, sendlen - 2); + _bletxbuf[sendlen - 1] = PACKET_TAIL; zdatachannel_data_send2(blereportbuf, // HAND_ACID_BLE_BASE_SIZE + sizeof(reportpacket->data.key_event)); diff --git a/protocol b/protocol index 36ac082..f8920e2 160000 --- a/protocol +++ b/protocol @@ -1 +1 @@ -Subproject commit 36ac082e16852c6b5e3ecc7bb36899286191a0e8 +Subproject commit f8920e28dcff342b07ae6abd243c400d59f8de62