/** * @file app_ble_proto.h * @brief BLE frame protocol frontend (over the CLS write/notify * characteristics): frame reassembly, CLI request/response transport * and OTA frame dispatch. * * Frame format (little endian): * [0] SOF = 0xCA * [1] TYPE * [2] SEQ * [3:4] LEN (payload length, LE) * [5..] PAYLOAD * [end-2..end] CRC16-CCITT (init 0xFFFF, poly 0x1021), LE, over TYPE..PAYLOAD * * All functions run in the BLE schedule task context only. */ #ifndef __APP_BLE_PROTO_H__ #define __APP_BLE_PROTO_H__ #ifdef __cplusplus extern "C" { #endif #include /* Frame types */ #define BLE_FRAME_CLI_REQ 0x01 /* phone -> device: one command line */ #define BLE_FRAME_CLI_RSP 0x02 /* device -> phone: response text chunk */ #define BLE_FRAME_CLI_RSP_END 0x03 /* device -> phone: {status u8} */ #define BLE_FRAME_OTA_BEGIN 0x10 /* {total_size u32, image_crc32 u32, version u32} */ #define BLE_FRAME_OTA_DATA 0x11 /* {offset u32 + data}, strictly sequential */ #define BLE_FRAME_OTA_END 0x12 /* {crc32 u32} */ #define BLE_FRAME_OTA_RSP 0x1F /* device -> phone: {cmd_echo u8, status u8, offset u32} */ /* OTA_RSP status codes */ #define BLE_OTA_ST_OK 0 #define BLE_OTA_ST_BAD_FRAME 1 #define BLE_OTA_ST_BAD_STATE 2 /* bad state / bad offset sequence */ #define BLE_OTA_ST_SIZE_TOO_BIG 3 #define BLE_OTA_ST_CRC_FAIL 4 #define BLE_OTA_ST_FLASH_FAIL 5 /** * @brief Reset the protocol state machine and pending buffer. */ void app_ble_proto_init(void); /** * @brief Feed a received BLE write chunk into the frame reassembler. * Registered as the app_ble downlink callback (BLE task context). */ void app_ble_proto_rx_bytes(const uint8_t* data, uint16_t len); /** * @brief Retry flushing pending TX frames. Called every BleTask loop round. */ void app_ble_proto_poll(void); /** * @brief BLE disconnect hook: abort OTA session, drop pending TX, reset RX. */ void app_ble_proto_on_disconnect(void); /** * @brief Build and send (or queue) one frame. Used by app_ota.c for OTA_RSP. * @return 0 if sent or queued, -1 if dropped (not connected / pending full). */ int app_ble_proto_send_frame(uint8_t type, uint8_t seq, const uint8_t* payload, uint16_t len); #ifdef __cplusplus } #endif #endif /* __APP_BLE_PROTO_H__ */