bt_common.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. // binary representation
  2. // attribute size in bytes (16), flags(16), handle (16), uuid (16/128), value(...)
  3. #ifndef _BT_COMMON_H
  4. #define _BT_COMMON_H
  5. #include <stdint.h>
  6. #include "app_config.h"
  7. #ifdef APP_PRIVATE_PROFILE_CFG
  8. #include "lib_profile_cfg.h"
  9. #else
  10. #include "bt_profile_cfg.h"
  11. #endif
  12. #define SYNC_TIMEOUT_MS(x) {(x / 10) & 0xff, (x / 10) >> 8}
  13. #define ALIGN_2BYTE(size) (((size)+1)&0xfffffffe)
  14. #define BYTE_LEN(x...) sizeof((u8 []) {x})
  15. #define MS_TO_SLOT(x) (x * 8 / 5)
  16. #define UINT24_TO_BYTE(x) \
  17. { \
  18. x, \
  19. x >> 8, \
  20. x >> 16 \
  21. }
  22. enum {
  23. ST_BIT_INQUIRY = 0,
  24. ST_BIT_PAGE_SCAN,
  25. ST_BIT_BLE_ADV,
  26. ST_BIT_SPP_CONN,
  27. ST_BIT_BLE_CONN,
  28. ST_BIT_WEIXIN_CONN,
  29. };
  30. enum {
  31. BLE_PRIV_MSG_PAIR_CONFIRM = 0xF0,
  32. BLE_PRIV_PAIR_ENCRYPTION_CHANGE,
  33. };//ble_state_e
  34. // #define SYS_BT_EVENT_FORM_COMMON (('C' << 24) | ('M' << 16) | ('M' << 8) | '\0')
  35. enum {
  36. COMMON_EVENT_EDR_REMOTE_TYPE = 1,
  37. COMMON_EVENT_BLE_REMOTE_TYPE,
  38. COMMON_EVENT_SHUTDOWN_ENABLE,
  39. COMMON_EVENT_SHUTDOWN_DISABLE,
  40. COMMON_EVENT_MODE_DETECT,
  41. };
  42. // LE Controller Commands
  43. struct __ext_adv_report_event {
  44. u8 Subevent_Code;
  45. u8 Num_Reports;
  46. u16 Event_Type;
  47. u8 Address_Type;
  48. u8 Address[6];
  49. u8 Primary_PHY;
  50. u8 Secondary_PHY;
  51. u8 Advertising_SID;
  52. u8 Tx_Power;
  53. u8 RSSI;
  54. u16 Periodic_Advertising_Interval;
  55. u8 Direct_Address_Type;
  56. u8 Direct_Address[6];
  57. u8 Data_Length;
  58. u8 Data[0];
  59. } _GNU_PACKED_;
  60. struct __periodic_adv_report_event {
  61. u8 Subevent_Code;
  62. u16 Sync_Handle;
  63. u8 Tx_Power;
  64. u8 RSSI;
  65. u8 Unused;
  66. u8 Data_Status;
  67. u8 Data_Length;
  68. u8 Data[0];
  69. } _GNU_PACKED_ ;
  70. struct __periodic_creat_sync {
  71. u8 Filter_Policy;
  72. u8 Advertising_SID;
  73. u8 Advertising_Address_Type;
  74. u8 Advertiser_Address[6];
  75. u8 Skip[2];
  76. u8 Sync_Timeout[2];
  77. u8 Unused;
  78. } _GNU_PACKED_;
  79. struct __ext_scan_param {
  80. u8 Own_Address_Type;
  81. u8 Scanning_Filter_Policy;
  82. u8 Scanning_PHYs;
  83. u8 Scan_Type;
  84. u16 Scan_Interval;
  85. u16 Scan_Window;
  86. } _GNU_PACKED_;
  87. struct __ext_scan_enable {
  88. u8 Enable;
  89. u8 Filter_Duplicates;
  90. u16 Duration;
  91. u16 Period;
  92. } _GNU_PACKED_;
  93. struct ext_advertising_param {
  94. u8 Advertising_Handle;
  95. u16 Advertising_Event_Properties;
  96. u8 Primary_Advertising_Interval_Min[3];
  97. u8 Primary_Advertising_Interval_Max[3];
  98. u8 Primary_Advertising_Channel_Map;
  99. u8 Own_Address_Type;
  100. u8 Peer_Address_Type;
  101. u8 Peer_Address[6];
  102. u8 Advertising_Filter_Policy;
  103. u8 Advertising_Tx_Power;
  104. u8 Primary_Advertising_PHY;
  105. u8 Secondary_Advertising_Max_Skip;
  106. u8 Secondary_Advertising_PHY;
  107. u8 Advertising_SID;
  108. u8 Scan_Request_Notification_Enable;
  109. } _GNU_PACKED_;
  110. struct ext_advertising_data {
  111. u8 Advertising_Handle;
  112. u8 Operation;
  113. u8 Fragment_Preference;
  114. u8 Advertising_Data_Length;
  115. u8 Advertising_Data[31];
  116. } _GNU_PACKED_;
  117. struct ext_advertising_enable {
  118. u8 Enable;
  119. u8 Number_of_Sets;
  120. u8 Advertising_Handle;
  121. u16 Duration;
  122. u8 Max_Extended_Advertising_Events;
  123. } _GNU_PACKED_;
  124. struct periodic_advertising_param {
  125. u8 Advertising_Handle;
  126. u16 Periodic_Advertising_Interval_Min;
  127. u16 Periodic_Advertising_Interval_Max;
  128. u16 Periodic_Advertising_Properties;
  129. } _GNU_PACKED_;
  130. struct periodic_advertising_data {
  131. u8 Advertising_Handle;
  132. u8 Operation;
  133. u8 Advertising_Data_Length;
  134. u8 Advertising_Data[31];
  135. } _GNU_PACKED_;
  136. struct periodic_advertising_enable {
  137. u8 Enable;
  138. u8 Advertising_Handle;
  139. } _GNU_PACKED_;
  140. extern const int config_le_hci_connection_num;//支持同时连接个数
  141. extern const int config_le_sm_support_enable; //是否支持加密配对
  142. extern const int config_le_gatt_server_num; //支持server角色个数
  143. extern const int config_le_gatt_client_num; //支持client角色个数
  144. extern uint16_t little_endian_read_16(const uint8_t *buffer, int pos);
  145. extern uint32_t little_endian_read_24(const uint8_t *buffer, int pos);
  146. extern uint32_t little_endian_read_32(const uint8_t *buffer, int pos);
  147. extern void swapX(const uint8_t *src, uint8_t *dst, int len);
  148. extern void little_endian_store_16(uint8_t *buffer, uint16_t pos, uint16_t value);
  149. extern void little_endian_store_32(uint8_t *buffer, uint16_t pos, uint32_t value);
  150. extern void big_endian_store_16(uint8_t *buffer, uint16_t pos, uint16_t value);
  151. extern void big_endian_store_32(uint8_t *buffer, uint16_t pos, uint32_t value);
  152. //common api
  153. extern void bt_ble_init(void);
  154. extern void bt_ble_exit(void);
  155. extern void bt_ble_adv_enable(u8 enable);
  156. extern void ble_app_disconnect(void);
  157. extern void ble_module_enable(u8 en);
  158. extern const u8 *bt_get_mac_addr();
  159. extern void lib_make_ble_address(u8 *ble_address, u8 *edr_address);
  160. extern void user_spp_data_handler(u8 packet_type, u16 ch, u8 *packet, u16 size);
  161. extern void bt_get_vm_mac_addr(u8 *addr);
  162. extern void set_remote_test_flag(u8 own_remote_test);
  163. extern void bredr_power_get(void);
  164. extern void bredr_power_put(void);
  165. extern void radio_set_eninv(int v);
  166. extern void transport_spp_init(void);
  167. extern const char *bt_get_local_name();
  168. extern void wdt_clear(void);
  169. extern u8 ble_update_get_ready_jump_flag(void);
  170. extern void reset_PK_cb_register(void (*reset_pk)(u32 *));
  171. extern void reset_PK_cb_register_ext(void (*reset_pk)(u32 *, u16));
  172. extern void att_server_flow_enable(u8 enable);
  173. extern void le_device_db_init(void);
  174. extern bool get_remote_test_flag();
  175. extern void transport_spp_disconnect(void);
  176. extern int transport_spp_flow_enable(u8 en);
  177. extern void transport_spp_flow_cfg(void);
  178. void att_set_conn_handle(u16 conn_handle, u8 handle_type);
  179. void at_send_rx_cid_data(u8 cid, u8 *packet, u16 size);
  180. void at_send_conn_result(u8 cid, u8 is_sucess);
  181. void at_send_disconnect(u8 cid);
  182. void at_send_connected(u8 cid);
  183. void at_send_string(u8 *str);
  184. void at_respond_send_err(u32 err_id);
  185. int le_att_server_send_data(u8 cid, u8 *packet, u16 size);
  186. int le_att_server_send_ctrl(u8 cid, u8 *packet, u16 size);
  187. int le_att_client_send_data(u8 cid, u8 *packet, u16 size);
  188. int le_at_client_creat_connection(u8 *conn_addr, u8 addr_type);
  189. void black_list_check(u8 sta, u8 *peer_addr);
  190. //errid
  191. enum {
  192. ERR_AT_CMD = 1,
  193. //add here
  194. };
  195. //初始化配置蓝牙发射功率最大值范围,解析详见 btcontroller_modules.h
  196. void bt_max_pwr_set(u8 pwr, u8 pg_pwr, u8 iq_pwr, u8 ble_pwr);
  197. #endif