7 changed files with 256 additions and 159 deletions
-
66ble_through/ble_cmd_app.h
-
71ble_through/ble_cmd_public.h
-
16ble_through/ble_proto.h
-
50ble_through/ble_proto_packet.h
-
34ble_through/ble_proto_utils.c
-
18ble_through/ble_proto_utils.h
-
160protocol.h
@ -0,0 +1,66 @@ |
|||||
|
#pragma once |
||||
|
#include <stdbool.h> |
||||
|
#include <stdint.h> |
||||
|
|
||||
|
#ifdef __cplusplus |
||||
|
extern "C" { |
||||
|
#endif |
||||
|
|
||||
|
/** |
||||
|
* @brief |
||||
|
* |
||||
|
* cmd: |
||||
|
* 000->049 app_cmd |
||||
|
* 050->099 app_event_report |
||||
|
* |
||||
|
* 100->149 public_cmd |
||||
|
* 150->199 public_event_report |
||||
|
* |
||||
|
* |
||||
|
*/ |
||||
|
|
||||
|
typedef enum { |
||||
|
kble_app_proto_cmd_sync_state = 1, // set_state |
||||
|
kble_app_proto_cmd_trigger_pump_start_event = 3, // |
||||
|
kble_app_proto_cmd_trigger_pump_stop_event = 4, // |
||||
|
kble_app_proto_cmd_trigger_reflux_start_event = 5, // |
||||
|
kble_app_proto_cmd_trigger_reflux_stop_event = 6, // |
||||
|
kble_app_proto_cmd_trigger_prepare_start_event = 7, // |
||||
|
kble_app_proto_cmd_trigger_prepare_stop_event = 8, // |
||||
|
kble_app_proto_cmd_read_version = 9, // |
||||
|
|
||||
|
kble_app_proto_report_reflux_butt_short_press_event = 50, // key_event |
||||
|
kble_app_proto_report_reflux_butt_long_press_event = 51, // key_event |
||||
|
kble_app_proto_report_action_butt_short_press_event = 52, // key_event |
||||
|
kble_app_proto_report_action_butt_long_press_event = 53, // key_event |
||||
|
kble_app_proto_report_client_heart = 54, // |
||||
|
|
||||
|
} ble_app_proto_cmd_t; |
||||
|
/*********************************************************************************************************************** |
||||
|
* ENUM * |
||||
|
***********************************************************************************************************************/ |
||||
|
|
||||
|
typedef enum { |
||||
|
khand_acid_m_unset_mode = 0, // unset_mode |
||||
|
khand_acid_m_jog_mode = 1, // jog_mode |
||||
|
khand_acid_m_continuous_mode = 2, // continuous_mode |
||||
|
} hand_acid_mode_t; |
||||
|
|
||||
|
typedef struct { |
||||
|
uint8_t mode; |
||||
|
} ble_app_proto_cmd_sync_state_t; |
||||
|
|
||||
|
typedef struct { |
||||
|
int32_t firmware_version; |
||||
|
int32_t blestack_version; |
||||
|
int32_t bootloader_version; |
||||
|
int32_t hardware_version; |
||||
|
} ble_app_proto_cmd_read_version_t; |
||||
|
|
||||
|
typedef struct { |
||||
|
uint8_t placeholder; |
||||
|
} ble_app_proto_report_client_heart_t; |
||||
|
|
||||
|
#ifdef __cplusplus |
||||
|
} |
||||
|
#endif |
@ -0,0 +1,71 @@ |
|||||
|
#pragma once |
||||
|
#include <stdbool.h> |
||||
|
#include <stdint.h> |
||||
|
|
||||
|
#ifdef __cplusplus |
||||
|
extern "C" { |
||||
|
#endif |
||||
|
|
||||
|
/** |
||||
|
* @brief |
||||
|
* |
||||
|
* cmd: |
||||
|
* 000->049 app_cmd |
||||
|
* 050->099 app_event_report |
||||
|
* |
||||
|
* 100->149 public_cmd |
||||
|
* 150->199 public_event_report |
||||
|
* |
||||
|
* |
||||
|
*/ |
||||
|
|
||||
|
typedef enum { |
||||
|
|
||||
|
kproto_cmd_ble_master_reset = 100, // process by ble master |
||||
|
kproto_cmd_ble_master_read_version = 101, // process by ble master ble_read_version_t |
||||
|
kproto_cmd_ble_master_start_scan = 102, // process by ble master |
||||
|
kproto_cmd_ble_master_stop_scan = 103, // process by ble master |
||||
|
kproto_cmd_ble_master_enter_dfu = 104, // process by ble master |
||||
|
kproto_cmd_ble_master_clear_reset_flag = 105, // process by ble master |
||||
|
|
||||
|
kproto_report_ble_master_connected_event = 150, // generator from ble master ble_connected_event_t |
||||
|
kproto_report_ble_master_scan_result = 151, // generator from ble master ble_scan_result_t |
||||
|
kproto_report_ble_master_power_on = 152, // generator from ble master ble_scan_result_t |
||||
|
kproto_report_ble_master_heart = 153, // generator from ble master ble_scan_result_t |
||||
|
|
||||
|
} ble_proto_public_cmd_t; |
||||
|
/*********************************************************************************************************************** |
||||
|
* ENUM * |
||||
|
***********************************************************************************************************************/ |
||||
|
typedef struct { |
||||
|
uint8_t auto_connect; |
||||
|
uint8_t auto_connect_client_name[20]; |
||||
|
} ble_master_start_scan_t; |
||||
|
|
||||
|
typedef struct { |
||||
|
uint8_t connected; |
||||
|
char blename[20]; |
||||
|
char bleid[20]; |
||||
|
} ble_master_connected_event_t; |
||||
|
|
||||
|
typedef struct { |
||||
|
char blename[20]; |
||||
|
char bleid[20]; |
||||
|
uint8_t rssi; |
||||
|
} ble_master_scan_result_t; |
||||
|
|
||||
|
typedef struct { |
||||
|
int32_t firmware_version; |
||||
|
int32_t blestack_version; |
||||
|
int32_t bootloader_version; |
||||
|
int32_t hardware_version; |
||||
|
} ble_master_read_version_t; |
||||
|
|
||||
|
typedef struct { |
||||
|
uint8_t reset_flag; |
||||
|
uint8_t connected_flag; |
||||
|
} ble_master_heart_t; |
||||
|
|
||||
|
#ifdef __cplusplus |
||||
|
} |
||||
|
#endif |
@ -0,0 +1,16 @@ |
|||||
|
#pragma once |
||||
|
#include <stdbool.h> |
||||
|
#include <stdint.h> |
||||
|
// |
||||
|
|
||||
|
#ifdef __cplusplus |
||||
|
extern "C" { |
||||
|
#include "ble_cmd_app.h" |
||||
|
// |
||||
|
#include "ble_cmd_public.h" |
||||
|
// |
||||
|
#include "ble_proto_packet.h" |
||||
|
// |
||||
|
#include "ble_proto_utils.h" |
||||
|
} |
||||
|
#endif |
@ -0,0 +1,50 @@ |
|||||
|
#pragma once |
||||
|
#pragma once |
||||
|
#include <stdbool.h> |
||||
|
#include <stdint.h> |
||||
|
// |
||||
|
#include "ble_cmd_app.h" |
||||
|
#include "ble_cmd_public.h" |
||||
|
|
||||
|
#ifdef __cplusplus |
||||
|
extern "C" { |
||||
|
#endif |
||||
|
|
||||
|
#define PACKET_H1 (0xAA) |
||||
|
#define PACKET_H2 (0xBB) |
||||
|
#define PACKET_TAIL (0xCC) |
||||
|
typedef enum { |
||||
|
kproto_cmd = 1, |
||||
|
kproto_cmd_receipt = 2, |
||||
|
kproto_report = 3, |
||||
|
kproto_error_receipt = 4, |
||||
|
} hand_acid_ble_proto_packet_type_t; |
||||
|
|
||||
|
/*********************************************************************************************************************** |
||||
|
* ENUM * |
||||
|
***********************************************************************************************************************/ |
||||
|
|
||||
|
#pragma pack(1) |
||||
|
typedef struct { |
||||
|
uint8_t h1; |
||||
|
uint8_t h2; |
||||
|
uint8_t packetlen; |
||||
|
uint8_t frame_type; |
||||
|
uint8_t frame_index; |
||||
|
uint8_t cmd; |
||||
|
union { |
||||
|
ble_master_start_scan_t ble_master_start_scan; |
||||
|
ble_master_connected_event_t ble_master_connected_event; |
||||
|
ble_master_scan_result_t ble_master_scan_result; |
||||
|
ble_master_read_version_t ble_master_read_version; |
||||
|
ble_master_heart_t ble_master_heart; |
||||
|
} d; |
||||
|
} ble_through_proto_t; |
||||
|
|
||||
|
#pragma pack() |
||||
|
|
||||
|
#define BLE_PACKET_BASE_SIZE() (sizeof(ble_through_proto_t) - sizeof(((ble_through_proto_t*)(0))->d) + 2) |
||||
|
|
||||
|
#ifdef __cplusplus |
||||
|
} |
||||
|
#endif |
@ -0,0 +1,34 @@ |
|||||
|
#include "ble_proto_packet.h" |
||||
|
|
||||
|
bool ble_through_proto_check_packet(ble_through_proto_t *packet) { |
||||
|
uint8_t *pu8 = (uint8_t *)packet; |
||||
|
|
||||
|
if (packet->packetlen < (BLE_PACKET_BASE_SIZE())) { |
||||
|
return false; |
||||
|
} |
||||
|
if (packet->h1 != PACKET_H1 || packet->h2 != PACKET_H2) { |
||||
|
return false; |
||||
|
} |
||||
|
if (pu8[packet->packetlen - 1] != PACKET_TAIL) { |
||||
|
return false; |
||||
|
} |
||||
|
|
||||
|
uint8_t checksum = 0; |
||||
|
for (int i = 0; i < packet->packetlen - 2; i++) { |
||||
|
checksum += pu8[i]; |
||||
|
} |
||||
|
if (checksum != pu8[packet->packetlen - 2]) { |
||||
|
return false; |
||||
|
} |
||||
|
|
||||
|
return true; |
||||
|
} |
||||
|
|
||||
|
uint8_t ble_through_proto_sum(ble_through_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; |
||||
|
} |
@ -0,0 +1,18 @@ |
|||||
|
#pragma once |
||||
|
|
||||
|
#include "ble_proto_packet.h" |
||||
|
|
||||
|
bool ble_through_proto_check_packet(ble_through_proto_t *packet); |
||||
|
uint8_t ble_through_proto_sum(ble_through_proto_t *packet); |
||||
|
|
||||
|
#define BLE_THROUGH_PACKET_SET_TAIL(packet) \ |
||||
|
{ \ |
||||
|
uint8_t *__data = (uint8_t *)packet; \ |
||||
|
__data[packet->packetlen - 1] = PACKET_TAIL; \ |
||||
|
} |
||||
|
|
||||
|
#define BLE_THROUGH_PACKET_SET_CHECKSUM(packet) \ |
||||
|
{ \ |
||||
|
uint8_t *__data = (uint8_t *)packet; \ |
||||
|
__data[packet->packetlen - 2] = hand_acid_ble_proto_sum(packet); \ |
||||
|
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue