Browse Source

update

master
zhaohe 1 year ago
parent
commit
b70c1c213d
  1. 66
      ble_through/ble_cmd_app.h
  2. 71
      ble_through/ble_cmd_public.h
  3. 16
      ble_through/ble_proto.h
  4. 50
      ble_through/ble_proto_packet.h
  5. 34
      ble_through/ble_proto_utils.c
  6. 18
      ble_through/ble_proto_utils.h
  7. 160
      protocol.h

66
ble_through/ble_cmd_app.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

71
ble_through/ble_cmd_public.h

@ -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

16
ble_through/ble_proto.h

@ -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

50
ble_through/ble_proto_packet.h

@ -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

34
ble_through/ble_proto_utils.c

@ -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;
}

18
ble_through/ble_proto_utils.h

@ -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); \
}

160
protocol.h

@ -2,163 +2,5 @@
#include <stdbool.h>
#include <stdint.h>
#include "ble_through/ble_proto.h"
#include "errorcode.h"
#ifdef __cplusplus
extern "C" {
#endif
#define HAND_ACID_BLE_BASE_SIZE 6 //
#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;
typedef enum {
kproto_cmd_set_state = 1, // set_state
kproto_cmd_reset = 2, //
kproto_cmd_setlong_key_delayms = 3, //
kproto_cmd_read_power = 4, //
kproto_cmd_read_version = 5, //
kproto_cmd_ble_master_start_scan = 50, // process by ble master ble_master_start_scan
kproto_cmd_ble_master_enter_dfu = 51, // process by ble master ble_master_enter_dfu
kproto_cmd_ble_master_reset = 52, // process by ble master
kproto_cmd_ble_master_read_version = 53, // process by ble master
kproto_report_key_event = 101, // key_event
kproto_report_connected_event = 102, // generater by ble master and slave
kproto_report_heart = 104, //
} hand_acid_ble_proto_cmd_t;
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 enum {
khand_acid_pump_is_idle = 0, //
khand_acid_pump_is_working = 1, //
} hand_pump_working_state_t;
typedef enum {
kkey_short_press_event = 0, //
kkey_long_press_event = 1, //
} key_event_t;
typedef enum {
kkey_id_mode_and_action_butt,
kkey_id_liquid_reflux_butt,
} key_id_t;
#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 {
struct {
uint8_t hand_acid_mode; // hand_acid_mode_t
uint8_t hand_pump_working_state; // hand_pump_working_state_t
} set_state;
//
struct {
uint8_t scanid[20]; // scan id
} ble_master_start_scan;
struct {
uint8_t keyid; // key_id_t
uint8_t keyevent; // key_event_t
} key_event;
struct {
uint8_t connected;
uint8_t from_ble_master;
} connected_event;
struct {
uint8_t powerlevel;
} heart;
struct {
uint32_t longkeydelay;
} setlong_key_delayms;
struct {
int32_t ecode;
} errreceipt;
struct {
int32_t power;
} read_power;
struct {
int32_t firmware_version;
int32_t blestack_version;
int32_t bootloader_version;
int32_t hardware_version;
} read_version;
} data;
} hand_acid_ble_proto_t;
static inline bool hand_acid_ble_proto_check_packet(hand_acid_ble_proto_t *packet) {
uint8_t *pu8 = (uint8_t *)packet;
if (packet->packetlen < (HAND_ACID_BLE_BASE_SIZE + 2)) {
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;
}
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
Loading…
Cancel
Save