api_btstack.h 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105
  1. #ifndef _API_BTSTACK_H
  2. #define _API_BTSTACK_H
  3. //BLE WORK MODE
  4. typedef enum {
  5. MODE_NORMAL = 0, //normal mode
  6. MODE_WIRELESS = 1, //wireless mode, 2.4G
  7. MODE_FCC_TEST = 2, //fcc test mode, use uart for communication
  8. MODE_BQB_RF_BLE = 4, //bqb test mode,use uart for communication
  9. MODE_IODM_TEST = 5, //iodm test mode,use uart for communication
  10. } BT_WORK_MODE;
  11. typedef enum {
  12. FCC_TX_TONE = 1,
  13. FCC_TX_TEST,
  14. FCC_RX_TEST,
  15. } FCC_RF_MODE;
  16. enum {
  17. TXPKT_KICK_DISABLE = 0,
  18. TXPKT_KICK_ENABLE,
  19. TXPKT_KICK_AUTO,
  20. };
  21. /**
  22. * @format 21
  23. * @param handle
  24. * @param status
  25. */
  26. #define BLE_EVT_PROFILE_FIND_DONE 0xA0
  27. /**
  28. * @format 2228
  29. * @param con_handle
  30. * @param start_group_handle
  31. * @param end_group_handle
  32. * @param uuid128
  33. */
  34. #define BLE_EVT_SERVICE_FIND_RESULT 0xA1
  35. /**
  36. * @format 222228
  37. * @param con_handle
  38. * @param start_handle
  39. * @param value_handle
  40. * @param end_handle
  41. * @param properties
  42. * @param uuid128
  43. */
  44. #define BLE_EVT_CHARACTERISTIC_FIND_RESULT 0xA2
  45. /**
  46. * @format 12222
  47. * @param status
  48. * @param handle
  49. * @param interval
  50. * @param latency
  51. * @param timeout
  52. */
  53. #define BLE_EVT_CONNECT_PARAM_UPDATE_DONE 0xB2
  54. /**
  55. * @format 1621
  56. * @param address_type
  57. * @param address
  58. * @param handle
  59. * @param role
  60. */
  61. #define BLE_EVT_CONNECT 0xB3
  62. /**
  63. * @format 21
  64. * @param handle
  65. * @param disconnect_reason
  66. */
  67. #define BLE_EVT_DISCONNECT 0xB4
  68. /**
  69. * @format 22
  70. * @param handle
  71. * @param MTU
  72. */
  73. #define BLE_EVT_MTU_EXCHANGE_DONE 0xB5
  74. /**
  75. * @format 122
  76. * @param status
  77. * @param conn_handle
  78. * @param attribute_handle
  79. */
  80. #define BLE_EVT_INDICATE_DONE 0xB6
  81. /**
  82. * @format 121
  83. * @param status
  84. * @param conn_handle
  85. * @param encryption_enabled
  86. */
  87. #define BLE_EVT_ENCRYPTION_CHANGE 0xC7
  88. /**
  89. * @format 21614
  90. * @param handle
  91. * @param addr_type
  92. * @param address
  93. * @param secure_connection - set to 1 if LE Secure Connection pairing will be used
  94. * @param passkey
  95. */
  96. #define BLE_EVT_PASSKEY_DISPLAY 0xC9
  97. /**
  98. * @format 2161
  99. * @param handle
  100. * @param addr_type
  101. * @param address
  102. * @param secure_connection - set to 1 if LE Secure Connection pairing will be used
  103. */
  104. #define BLE_EVT_PASSKEY_INPUT 0xCB
  105. /**
  106. * @format 216
  107. * @param handle
  108. * @param addr_type
  109. * @param address
  110. */
  111. #define BLE_EVT_IDENTITY_RESOLVING_FAIL 0xCE
  112. /**
  113. * @brief Identify resolving succeeded
  114. *
  115. * @format 216162
  116. * @param handle
  117. * @param addr_type
  118. * @param address
  119. * @param identity_addr_type
  120. * @param identity_address
  121. * @param index
  122. *
  123. */
  124. #define BLE_EVT_IDENTITY_RESOLVING_SUCCEED 0xCF
  125. /**
  126. * @brief Emitted to inform app that pairing has started.
  127. * @format 216
  128. * @param handle
  129. * @param addr_type
  130. * @param address
  131. */
  132. #define BLE_EVT_PAIRING_START 0xD4
  133. /**
  134. * @brief Emitted to inform app that pairing is complete.
  135. * @format 21611
  136. * @param handle
  137. * @param addr_type
  138. * @param address
  139. * @param status
  140. * @param reason
  141. */
  142. #define BLE_EVT_PAIRING_DONE 0xD5
  143. /**
  144. * @brief Emitted to inform app that ltk is missing.
  145. * @format 216
  146. * @param handle
  147. * @param addr_type
  148. * @param address
  149. */
  150. #define BLE_EVT_LTK_MISSING 0xD8
  151. /**
  152. * @format 11611N
  153. * @param advertising_event_type
  154. * @param address_type
  155. * @param address
  156. * @param rssi
  157. * @param data_length
  158. * @param data
  159. */
  160. #define BLE_EVT_ADV_REPORT 0xE2
  161. // ATT_ERR_CODE
  162. #define ATT_ERR_NO_ERR 0x00
  163. #define ATT_ERR_INVALID_HANDLE 0x01
  164. #define ATT_ERR_READ_NOT_PERMITTED 0x02
  165. #define ATT_ERR_WRITE_NOT_PERMITTED 0x03
  166. #define ATT_ERR_INVALID_PDU 0x04
  167. #define ATT_ERR_INSUFFICIENT_AUTHENTICATION 0x05
  168. #define ATT_ERR_REQUEST_NOT_SUPPORTED 0x06
  169. #define ATT_ERR_INVALID_OFFSET 0x07
  170. #define ATT_ERR_INSUFFICIENT_AUTHORIZATION 0x08
  171. #define ATT_ERR_PREPARE_QUEUE_FULL 0x09
  172. #define ATT_ERR_ATTRIBUTE_NOT_FOUND 0x0a
  173. #define ATT_ERR_ATTRIBUTE_NOT_LONG 0x0b
  174. #define ATT_ERR_INSUFFICIENT_ENCRYPTION_KEY_SIZE 0x0c
  175. #define ATT_ERR_INVALID_ATTRIBUTE_VALUE_LENGTH 0x0d
  176. #define ATT_ERR_UNLIKELY_ERROR 0x0e
  177. #define ATT_ERR_INSUFFICIENT_ENCRYPTION 0x0f
  178. #define ATT_ERR_UNSUPPORTED_GROUP_TYPE 0x10
  179. #define ATT_ERR_INSUFFICIENT_RESOURCES 0x11
  180. #define ATT_ERR_VALUE_NOT_ALLOWED 0x13
  181. #define ATT_ERR_IN_WRONG_STATE 0x80
  182. #define ATT_ERR_NOTIFICATION_NOT_SUPPORTED 0x81
  183. #define ATT_ERR_INDICATION_NOT_SUPPORTED 0x82
  184. #define ATT_ERR_INDICATION_IN_PORGRESS 0x83
  185. #define ATT_ERR_TIMEOUT 0x84
  186. #define ATT_ERR_DISCONNECT 0x85
  187. #define ATT_ERR_DATA_MISMATCH 0x86
  188. //Attribute Property
  189. #define ATT_PROPERTY_BROADCAST 0x01
  190. #define ATT_PROPERTY_READ 0x02
  191. #define ATT_PROPERTY_WRITE_WITHOUT_RESPONSE 0x04
  192. #define ATT_PROPERTY_WRITE 0x08
  193. #define ATT_PROPERTY_NOTIFY 0x10
  194. #define ATT_PROPERTY_INDICATE 0x20
  195. #define ATT_PROPERTY_AUTHENTICATED_SIGNED_WRITE 0x40
  196. #define ATT_PROPERTY_EXTENDED_PROPERTIES 0x80
  197. //GATT_CLIENT_CHARACTERISTIC_CONFIGURATION
  198. #define GATT_CCC_NOTIFICATION 0x01
  199. #define GATT_CCC_INDICATION 0x02
  200. //ATT_TRANSACTION_MODE
  201. #define ATT_TRANSACTION_WRITE 0x00
  202. #define ATT_TRANSACTION_PREPARE_WRITE 0x01
  203. #define ATT_TRANSACTION_WRITE_EXECUTE 0x02
  204. #define ATT_TRANSACTION_WRITE_CANCEL 0x03
  205. //SM_AUTHREQ
  206. #define SM_AUTHREQ_NO_BONDING 0x00
  207. #define SM_AUTHREQ_BONDING 0x01
  208. #define SM_AUTHREQ_MITM_PROTECTION 0x04
  209. #define SM_AUTHREQ_SECURE_CONNECTION 0x08
  210. #define SM_AUTHREQ_KEYPRESS 0x10
  211. #define SM_AUTHREQ_CT2 0x20
  212. typedef uint16_t ble_con_handle_t;
  213. typedef uint32_t bt_timer_handle_t;
  214. typedef void (*kick_func_t)(uint16_t con_handle, uint16_t config);
  215. typedef uint16_t (*att_read_callback_t)(uint16_t con_handle, uint16_t attribute_handle, uint16_t offset, uint8_t * buffer, uint16_t buffer_size);
  216. typedef int (*att_write_callback_t)(uint16_t con_handle, uint16_t attribute_handle, uint16_t trans_mode, uint16_t offset, uint8_t *buffer, uint16_t buffer_size);
  217. typedef uint16_t (*att_notify_callback_t)(uint16_t con_handle, uint16_t attribute_handle, uint8_t * buffer, uint16_t buffer_size);
  218. typedef void (*att_event_callback_t) (uint8_t event_type, uint8_t *param, uint16_t size);
  219. typedef void (*ble_init_callback_t)(void);
  220. typedef void (*bb_timer_proc_callback_t)(uint32_t time);
  221. typedef void (*ble_vs_tbox_init_callback_t)(void);
  222. // IO Capability Values
  223. enum {
  224. IO_CAPABILITY_DISPLAY_ONLY = 0,
  225. IO_CAPABILITY_DISPLAY_YES_NO,
  226. IO_CAPABILITY_KEYBOARD_ONLY,
  227. IO_CAPABILITY_NO_INPUT_NO_OUTPUT,
  228. IO_CAPABILITY_KEYBOARD_DISPLAY, // not used by secure simple pairing
  229. };
  230. //ADDRESS TYPE
  231. typedef enum {
  232. GAP_PUBLIC_ADDRESS_TYPE = 0,
  233. GAP_RANDOM_ADDRESS_TYPE_STATIC,
  234. GAP_RANDOM_ADDRESS_NON_RESOLVABLE,
  235. GAP_RANDOM_ADDRESS_RESOLVABLE,
  236. } gap_random_address_type_t;
  237. typedef enum {
  238. BD_ADDR_TYPE_PUBLIC = 0,
  239. BD_ADDR_TYPE_RANDOM = 1,
  240. } bd_addr_type_t;
  241. //ADV_TYPE
  242. enum {
  243. ADV_TYPE_IND = 0,
  244. ADV_TYPE_DIRECT_HI_IND,
  245. ADV_TYPE_SCAN_IND,
  246. ADV_TYPE_NONCONN_IND,
  247. ADV_TYPE_DIRECT_LO_IND,
  248. };
  249. //SCAN STA
  250. typedef enum {
  251. LE_SCAN_IDLE,
  252. LE_START_SCAN,
  253. LE_SCANNING,
  254. LE_STOP_SCAN,
  255. } le_scanning_state_t;
  256. typedef struct {
  257. uint8_t adv_en;
  258. uint8_t adv_data[31];
  259. uint8_t adv_len;
  260. uint8_t scan_rsp[31];
  261. uint8_t scan_rsp_len;
  262. uint16_t adv_int_min; //advertisement interval
  263. uint16_t adv_int_max; //advertisement interval
  264. uint8_t adv_type;
  265. uint8_t direct_address_typ; //only use for direct adv
  266. uint8_t direct_address[6]; //only use for direct adv
  267. uint8_t channel_map;
  268. uint8_t filter_policy;
  269. } adv_param_t;
  270. typedef struct __attribute__((packed)){
  271. uint8_t event_type;
  272. uint8_t addr_type;
  273. uint8_t addr[6];
  274. char rssi;
  275. uint8_t report_len;
  276. const uint8_t * report;
  277. uint8_t offset;
  278. } adv_report_t;
  279. typedef struct {
  280. uint8_t adv_type;
  281. uint8_t data_len;
  282. uint8_t* data;
  283. } adv_struct_t;
  284. typedef struct {
  285. uint8_t scan_type;
  286. uint16_t scan_interval;
  287. uint16_t scan_window;
  288. } scan_param_t;
  289. typedef struct {
  290. ///6-byte array address value
  291. uint8_t addr[6];
  292. }bd_addr_t;
  293. typedef struct {
  294. // Key value MSB -> LSB
  295. uint8_t key[16];
  296. }gap_sec_key_t;
  297. // Do not modify!!
  298. typedef struct {
  299. // Address type of device. 0 public/1 random
  300. uint8_t addr_type;
  301. // BD Address of device
  302. bd_addr_t addr;
  303. }gap_bdaddr_t;
  304. /**
  305. * @brief Identity Resolving Key Information
  306. */
  307. typedef struct {
  308. // Identify Resolving Key
  309. gap_sec_key_t irk;
  310. // Identity broadcast address of device
  311. gap_bdaddr_t addr;
  312. }gapc_irk_t;
  313. /**
  314. * @brief Resolving list information about a device.
  315. */
  316. typedef struct {
  317. // Privacy Mode
  318. uint8_t priv_mode; //PRIV_TYPE_NETWORK = 0x00, Controller accept identity address only
  319. //PRIV_TYPE_DEVICE = 0x01, Controller accept both identity address and resolvable address.
  320. // Device Identity address
  321. gap_bdaddr_t addr;
  322. // Peer device identity resolve key
  323. uint8_t peer_irk[16];
  324. }gap_ral_dev_info_t;
  325. typedef struct {
  326. uint8_t rpa_en;
  327. uint8_t privacy_mode;
  328. uint16_t renew_to;
  329. gap_sec_key_t peer_irk;
  330. gap_bdaddr_t addr;
  331. }rpa_param_t;
  332. typedef struct {
  333. // Number of entries to be added in the list. 0 means that list content has to be cleard.
  334. uint8_t size;
  335. // List of entries to be added in the list.
  336. gap_bdaddr_t addr;
  337. }wlist_param_t;
  338. typedef struct {
  339. uint16_t address_type;
  340. uint16_t real_address_type;
  341. bd_addr_t address;
  342. gap_sec_key_t peer_irk;
  343. } sm_bonding_info_t;
  344. // Read & Write Callbacks for handle range
  345. typedef struct att_service_handler {
  346. void * item;
  347. uint16_t start_handle;
  348. uint16_t end_handle;
  349. att_read_callback_t read_callback;
  350. att_write_callback_t write_callback;
  351. att_event_callback_t event_handler;
  352. } att_service_handler_t;
  353. typedef struct att_client_handler {
  354. void * item;
  355. att_read_callback_t read_callback;
  356. att_notify_callback_t notify_callback;
  357. att_event_callback_t event_handler;
  358. } att_client_handler_t;
  359. typedef struct {
  360. uint16_t start_group_handle;
  361. uint16_t end_group_handle;
  362. uint16_t uuid16;
  363. uint8_t uuid128[16];
  364. } gatt_client_service_t;
  365. typedef struct {
  366. uint16_t start_handle;
  367. uint16_t value_handle;
  368. uint16_t end_handle;
  369. uint16_t properties;
  370. uint16_t uuid16;
  371. uint8_t uuid128[16];
  372. } gatt_client_characteristic_t;
  373. struct txbuf_tag {
  374. uint8_t *ptr;
  375. uint16_t len;
  376. uint16_t att_handle;
  377. uint16_t con_handle;
  378. uint16_t config;
  379. } __attribute__ ((packed)) ;
  380. struct txpkt_tag {
  381. struct txbuf_tag *buf;
  382. uint16_t buf_size;
  383. uint8_t total;
  384. volatile uint8_t cnt;
  385. uint8_t rptr;
  386. volatile uint8_t wptr;
  387. kick_func_t send_kick;
  388. };
  389. typedef struct {
  390. uint8_t rf_mode;
  391. uint8_t tx_power;
  392. uint8_t tx_freq;
  393. uint8_t rx_freq;
  394. uint8_t phy_type;
  395. uint8_t todo;
  396. } fcc_cfg_t;
  397. typedef struct {
  398. uint8_t mem[28];
  399. } bt_timer_mem_t;
  400. extern struct txpkt_tag ble_tx;
  401. //control
  402. void bt_init(void);
  403. int bt_setup(void);
  404. void bt_off(void);
  405. //sleep
  406. bool bt_is_sleep(void);
  407. uint32_t bt_sleep_proc(void);
  408. void bt_enter_sleep(void);
  409. void bt_exit_sleep(void);
  410. uint32_t txpkt_init(struct txpkt_tag *txpkt, void *mem_pool, uint8_t total, uint16_t buf_size);
  411. bool txpkt_is_full(struct txpkt_tag *txpkt);
  412. uint8_t txpkt_available_num(struct txpkt_tag *txpkt);
  413. void txpkt_flush(struct txpkt_tag *txpkt);
  414. /**
  415. * @brief Triggers ble to start notify data, Can be used with ble_notify_for_handle_kick function
  416. * @param txpkt
  417. * @param con_handle
  418. */
  419. void txpkt_server_send_tick(struct txpkt_tag *txpkt, uint16_t con_handle);
  420. /**
  421. * @brief Triggers ble to start write data, Can be used with ble_write_cmd_for_character_kick function(Todo)
  422. * @param txpkt
  423. * @param con_handle
  424. */
  425. void txpkt_client_send_tick(struct txpkt_tag *txpkt, uint16_t con_handle);
  426. /**
  427. * @brief Triggers ble to start sending data
  428. * @param con_handle
  429. */
  430. void ble_send_kick(uint16_t con_handle, uint16_t config);
  431. /**
  432. * @brief ble fcc test mode config
  433. * @param rf_mode: FCC_RF_MODE
  434. * @param tx_power: [0,7]
  435. * @param tx_freq: [0,39],freq = 2402 + tx_freq * 2
  436. * @param tx_freq: [0,39],freq = 2402 + tx_freq * 2
  437. * @param phy_type: 0:1M 1:2M
  438. * @param todo: 1
  439. */
  440. void ble_fcc_cfg_init(fcc_cfg_t *cfg);
  441. /**
  442. * @brief ble advertisement ctrl
  443. * @param none
  444. */
  445. void ble_adv_dis(void);
  446. void ble_adv_en(void);
  447. bool ble_adv_is_en(void);
  448. /**
  449. * @brief set up ble advertisement interval
  450. * @param interval: 0x20 - 0x4000(uint:0.625ms)
  451. * @return return true if ok, invalid parameters will return false
  452. */
  453. bool ble_set_adv_interval(uint16_t interval);
  454. /**
  455. * @brief get ble advertisement interval
  456. * @param interval: 0x20 - 0x4000(uint:0.625ms)
  457. * @return return true if ok, invalid parameters will return false
  458. */
  459. bool ble_get_adv_interval(uint16_t *interval);
  460. /**
  461. * @brief set advertisement data
  462. * @param advertising_data (max 31 octets)
  463. * @param advertising_data_length
  464. * @return return true if ok, invalid parameters will return false
  465. */
  466. bool ble_set_adv_data(const uint8_t *adv_buf, uint32_t size);
  467. /**
  468. * @brief set scan response Data
  469. * @param advertising_data (max 31 octets)
  470. * @param advertising_data_length
  471. * @return return true if ok, invalid parameters will return false
  472. */
  473. bool ble_set_scan_rsp_data(const uint8_t *scan_rsp_buf, uint32_t size);
  474. /**
  475. * @brief initialize advertisement parameter
  476. * @param advertisement parameter
  477. * @return return true if ok, invalid parameters will return false
  478. */
  479. bool ble_set_adv_param(adv_param_t* param);
  480. adv_param_t* ble_get_adv_param(void);
  481. /**
  482. * @brief initialize advertisement parameter
  483. * @param advertisement parameter
  484. * @return return true if ok, invalid parameters will return false
  485. */
  486. bool ble_set_ext_adv_param(adv_param_t* param);
  487. /**
  488. * @brief analyse advertisement report, the results will be store in advertisement struct
  489. * @param report, can be get by BLE_EVT_ADV_REPORT
  490. * @param adv_struct
  491. * @return analysis is complete will return true, else will return false
  492. */
  493. bool ble_adv_report_info_analyse(adv_report_t* report, adv_struct_t* adv_struct);
  494. /**
  495. * @brief disconnect ble connection according to connect handle
  496. * @param con_handle
  497. */
  498. void ble_disconnect(uint16_t con_handle);
  499. /**
  500. * @brief ble master create connect by ble address
  501. * @param addr_ptr
  502. * @param addr type : 0-3(0:public, 1:random, 2:public identity 3:random identity)
  503. * @param interval: 6 - 3200(unit: 1.25ms)
  504. * @param latency
  505. * @param timeout (unit: 10ms)
  506. * @return return true if ok, invalid parameters will return false
  507. */
  508. bool ble_connect_by_addr(uint8_t *addr_ptr, uint8_t addr_type, uint16_t interval, uint16_t conn_latency, uint16_t supervision_timeout);
  509. /**
  510. * @brief update of the connection parameter for a given LE connection
  511. * @param con_handle
  512. * @param interval: 6 - 3200(unit: 1.25ms)
  513. * @param latency
  514. * @param timeout (unit: 10ms)
  515. * @return return true if ok, invalid parameters will return false
  516. */
  517. bool ble_update_conn_param(uint16_t con_handle, uint16_t interval, uint16_t latency, uint16_t timeout);
  518. /**
  519. * @brief update of the connection parameter for a given LE connection
  520. * @param con_handle
  521. * @param interval_min: 6 - 3200(unit: 1.25ms)
  522. * @param interval_max: 6 - 3200(unit: 1.25ms) interval_min <= interval_max
  523. * @param latency
  524. * @param timeout (unit: 10ms)
  525. * @return return true if ok, invalid parameters will return false
  526. */
  527. bool ble_update_conn_param_2(uint16_t con_handle, uint16_t interval_min, uint16_t interval_max, uint16_t latency, uint16_t timeout);
  528. /**
  529. * @brief get of the connection parameter for a given LE connection
  530. * @param con_handle
  531. * @param interval: 6 - 3200(unit: 1.25ms)
  532. * @param latency
  533. * @param timeout (unit: 10ms)
  534. * @return return true if ok, invalid parameters will return false
  535. */
  536. bool ble_get_conn_param(uint16_t con_handle, uint16_t *interval, uint16_t *latency, uint16_t *timeout);
  537. /**
  538. * @brief Disable latency, only valid for the slave role. Even if connection parameters are updated later, latency is disabled also
  539. * @param con_handle
  540. * @return return true if ok, invalid parameters will return false
  541. */
  542. bool ble_disable_latency(uint16_t con_handle);
  543. /**
  544. * @brief Enable latency, only valid for the slave role.
  545. * @param con_handle
  546. * @return return true if ok, invalid parameters will return false
  547. */
  548. bool ble_enable_latency(uint16_t con_handle);
  549. /**
  550. * @brief notify data according to handle
  551. * @param connect handle
  552. * @param attribute handle
  553. * @param buf to be send
  554. * @param len(must be less than att mtu)
  555. * @return return 0 if ok, other refer to ATT_ERR_CODE
  556. */
  557. int ble_notify_for_handle(uint16_t con_handle, uint16_t att_handle, uint8_t* buf, uint16_t len);
  558. /**
  559. * @brief notify data according to handle
  560. * @param connect handle
  561. * @param attribute handle
  562. * @param buf to be send
  563. * @param len(must be less than att mtu)
  564. * @param kick_cfg, The value is as follows:
  565. TXPKT_KICK_ENABLE: the Bluetooth thread is triggered to start sending data,
  566. TXPKT_KICK_DISABLE: the Bluetooth thread will not be triggered to send data immediately, but the data will be cached
  567. TXPKT_KICK_AUTO: Automatically managed by the Btstack
  568. It is suitable for continuous data transmission, and can trigger the Bluetooth thread to send data at the last to reduce the switching of threads
  569. * @return return 0 if ok, other refer to ATT_ERR_CODE
  570. */
  571. int ble_notify_for_handle_kick(uint16_t con_handle, uint16_t att_handle, uint8_t* buf, uint16_t len, uint8_t kick_cfg);
  572. /**
  573. * @brief indicate data according to handle
  574. * @param connect handle
  575. * @param attribute handle
  576. * @param buf to be send
  577. * @param len(must be less than att mtu)
  578. * @return return 0 if ok, other refer to ATT_ERR_CODE
  579. */
  580. int ble_indicate_for_handle(uint16_t con_handle, uint16_t att_handle, uint8_t* buf, uint16_t len);
  581. /**
  582. * @brief Writes the characteristic value using the characteristic's value handle
  583. * @param connect handle
  584. * @param character
  585. * @param buf to be send
  586. * @param len(must be less than att mtu)
  587. * @return return 0 if ok, other refer to ATT_ERR_CODE
  588. */
  589. int ble_write_req_for_character(uint16_t con_handle, gatt_client_characteristic_t* character, uint8_t* buf, uint16_t len);
  590. /**
  591. * @brief Writes the characteristic value using the characteristic's value handle without an acknowledgment that the write was successfully performed.
  592. * @param connect handle
  593. * @param character
  594. * @param buf to be send
  595. * @param len(must be less than att mtu)
  596. * @return return 0 if ok, other refer to ATT_ERR_CODE
  597. */
  598. int ble_write_cmd_for_character(uint16_t con_handle, gatt_client_characteristic_t* character, uint8_t* buf, uint16_t len);
  599. /**
  600. * @brief reads the characteristic value using the characteristic's value handle.
  601. * @param con_handle
  602. * @param character
  603. * @return return 0 if ok, other refer to ATT_ERR_CODE
  604. */
  605. int ble_read_req_for_character(uint16_t con_handle, gatt_client_characteristic_t* character);
  606. /**
  607. * @brief reads the long characteristic value using the characteristic's value handle.
  608. * @param con_handle
  609. * @param character
  610. * @param offset
  611. * @return return 0 if ok, other refer to ATT_ERR_CODE
  612. */
  613. int ble_read_blob_for_character(uint16_t con_handle, gatt_client_characteristic_t* character, uint16_t offset);
  614. /**
  615. * @brief set ble address type
  616. * @param address type
  617. */
  618. void gap_random_address_set_mode(gap_random_address_type_t random_address_type);
  619. /**
  620. * @brief BLE initialize callback function registration,
  621. * @param callback function, which will be called in bluetooth thread
  622. */
  623. void ble_init_callback_register(ble_init_callback_t func);
  624. /**
  625. * @brief BLE test box initialize callback function registration,
  626. * @param callback function, which will be called in bluetooth thread
  627. */
  628. void ble_vs_tbox_init_callback_register(ble_vs_tbox_init_callback_t func);
  629. /**
  630. * @brief Register server packet handle
  631. */
  632. void att_server_register_service_handler(att_service_handler_t * handler);
  633. /**
  634. * @brief Register client packet handle
  635. */
  636. void att_client_register_handler(att_client_handler_t * handler);
  637. /**
  638. * @brief ble sm init
  639. * @param security_req_en(whether to send an sm request when ble connecting)
  640. * @param io_capability
  641. * @param authentication_req_flag
  642. */
  643. void ble_sm_init(uint8_t security_req_en, uint8_t io_capability, uint8_t authentication_req_flag);
  644. /**
  645. * @brief Trigger Security Request
  646. */
  647. void ble_sm_send_security_request(ble_con_handle_t con_handle);
  648. /**
  649. * @brief setup ATT server
  650. * @param db attribute database created by profile tool
  651. */
  652. void att_server_init(uint8_t const * db);
  653. /**
  654. * @brief Set up GATT client.
  655. */
  656. void gatt_client_init(void);
  657. /**
  658. * @brief get att mtu
  659. * @param con_handle
  660. * @return mtu if ok, 0 otherwise
  661. */
  662. uint8_t att_server_get_mtu(uint16_t con_handle, uint16_t * mtu);
  663. /**
  664. * @brief Get att mtu. If status is equal to ATT_ERROR_NO_ERROR, it returns the real value, otherwise the default value ATT_DEFAULT_MTU
  665. * @param con_handle
  666. * @param mtu
  667. * @return return 0 if ok, other refer to ATT_ERR_CODE
  668. */
  669. uint8_t gatt_client_get_mtu(uint16_t con_handle, uint16_t * mtu);
  670. /**
  671. * @brief ble scan ctrl
  672. * @param none
  673. */
  674. void ble_scan_en(void);
  675. void ble_scan_dis(void);
  676. /**
  677. * @brief ble get scan status
  678. * @param none
  679. * @return sta : 0-3(refer to le_scanning_state_t)
  680. */
  681. le_scanning_state_t ble_get_scan_sta(void);
  682. /**
  683. * @brief ble set scan param
  684. * @param type : 0 or 1,0(Passive scan,no scan_rsp),1(Active scan,adv+scan_rsp)
  685. * @param interval: 4 - 16384(unit: 0.625ms)
  686. * @param window : 4 - 16384(less than or equal to interval unit: 0.625ms)
  687. * @return return true if ok, invalid parameters will return false
  688. */
  689. bool ble_set_scan_param(scan_param_t* param);
  690. /**
  691. * @brief Discovers all primary services. For each found service, BLE_EVT_SERVICE_FIND_RESULT will be generated and passed to the registered callback. BLE_EVT_PROFILE_FIND_DONE, marks the end of discovery.
  692. * @param con_handle
  693. * @return return 0 if ok, other refer to ATT_ERR_CODE
  694. */
  695. uint8_t ble_client_discover_primary_services(uint16_t con_handle);
  696. /**
  697. * @brief Discovers a specific primary service given its UUID. For each found service, BLE_EVT_SERVICE_FIND_RESULT will be generated and passed to the registered callback. BLE_EVT_PROFILE_FIND_DONE, marks the end of discovery.
  698. * @param con_handle
  699. * @param uuid16
  700. * @return return 0 if ok, other refer to ATT_ERR_CODE
  701. */
  702. uint8_t ble_client_discover_primary_services_by_uuid16(uint16_t con_handle, uint16_t uuid16);
  703. uint8_t ble_client_discover_primary_services_by_uuid128(uint16_t con_handle, const uint8_t * uuid128);
  704. /**
  705. * @brief Get field service from event BLE_EVT_SERVICE_FIND_RESULT
  706. * @param event packet
  707. * @param Pointer to storage for service
  708. */
  709. void ble_service_query_result_get_service(const uint8_t * event, gatt_client_service_t * service);
  710. /**
  711. * @brief Discovers all characteristics within the specified service. For each found characteristic, an le_characteristics_event_t with type set to BLE_EVT_CHARACTERISTIC_FIND_RESULT will be generated and passed to the registered callback. The gatt_complete_event_t with type set to BLE_EVT_PROFILE_FIND_DONE, marks the end of discovery.
  712. * @param callback
  713. * @param con_handle
  714. * @param service
  715. * @return return 0 if ok, other refer to ATT_ERR_CODE
  716. */
  717. uint8_t ble_client_discover_characteristics_for_service(uint16_t con_handle, gatt_client_service_t * service);
  718. /**
  719. * @brief Get field characteristic from event BLE_EVT_CHARACTERISTIC_FIND_RESULT
  720. * @param event packet
  721. * @param Pointer to storage for characteristic
  722. */
  723. void ble_characteristic_query_result_get_characteristic(const uint8_t * event, gatt_client_characteristic_t * characteristic);
  724. /**
  725. * @brief Discover all characteristics within the specified service, and return those that match the given UUID. For each found characteristic, BLE_EVT_CHARACTERISTIC_FIND_RESULT will be generated and passed to the registered callback. BLE_EVT_PROFILE_FIND_DONE, marks the end of discovery.
  726. * @param con_handle
  727. * @param service
  728. * @param uuid
  729. * @return return 0 if ok, other refer to ATT_ERR_CODE
  730. */
  731. uint8_t ble_client_discover_characteristics_for_service_by_uuid16(uint16_t con_handle, gatt_client_service_t * service, uint16_t uuid16);
  732. uint8_t ble_client_discover_characteristics_for_service_by_uuid128(uint16_t con_handle, gatt_client_service_t * service, const uint8_t * uuid128);
  733. /**
  734. * @brief Writes the client characteristic configuration of the specified characteristic.
  735. * @param con_handle
  736. * @param characteristic
  737. * @param configuration : GATT_CCC_NOTIFICATION, GATT_CCC_INDICATION
  738. * @return return 0 if ok, other refer to ATT_ERR_CODE
  739. */
  740. uint8_t ble_client_write_client_characteristic_configuration(uint16_t con_handle, gatt_client_characteristic_t * characteristic, uint16_t configuration);
  741. /**
  742. * @brief Get specific characteristic struct by its uuid.
  743. * @param ALL characteristics to be matched
  744. * @param The number of characteristics
  745. * @param uuid
  746. * @return return 0 if ok, other refer to ATT_ERR_CODE
  747. */
  748. gatt_client_characteristic_t* ble_get_characteristic_for_uuid16(gatt_client_characteristic_t *characteristic, uint8_t num, uint16_t uuid16);
  749. gatt_client_characteristic_t* ble_get_characteristic_for_uuid128(gatt_client_characteristic_t *characteristic, uint8_t num, uint8_t *uuid128);
  750. /**
  751. * @brief Host renew resolvable private address timeout.
  752. * @param rpa params. mainly timeout in seconds and address resolution enable.
  753. * @return none
  754. */
  755. void ble_set_rslv_renew_to(rpa_param_t *param);
  756. /**
  757. * @brief Host set address resolution enable.
  758. * @param 1 enable
  759. 0 disable.
  760. * @return none
  761. */
  762. void ble_set_rslv_enable(uint8_t enable);
  763. /**
  764. * @brief Host add device to resolvable address list.
  765. * @param privacy mode.
  766. * @param peer device address.
  767. * @param peer device irk.
  768. * @return none
  769. */
  770. void ble_add_dev_to_ral_list(gap_ral_dev_info_t *ral_dev_info);
  771. /**
  772. * @brief Host remove device from resolvable address list.
  773. * @param privacy mode.
  774. * @param peer device address.
  775. * @param peer device irk.
  776. * @return none
  777. */
  778. void ble_rmv_dev_from_ral_list(gap_ral_dev_info_t *ral_dev_info);
  779. /**
  780. * @brief Host add device to filter accept list.
  781. * @param size of wlist.
  782. * @param peer device identity address type.
  783. * @param peer device identity address.
  784. * @return none
  785. */
  786. void ble_add_dev_to_white_list(wlist_param_t *param);
  787. /**
  788. * @brief Host remove device from filter accept list.
  789. * @param peer device identity address type.
  790. * @param peer device identity address.
  791. * @return none
  792. */
  793. void ble_rmv_dev_from_white_list(wlist_param_t *param);
  794. /**
  795. * @brief Host read filter accept list size.
  796. * @return none
  797. */
  798. void ble_rd_dev_white_list(void);
  799. /**
  800. * @brief Host clear filter accept list.
  801. * @return none
  802. */
  803. void ble_clr_dev_white_list(void);
  804. /**
  805. * @brief Used by the user to enter passkey when BLE_EVT_PASSKEY_INPUT event is triggered.
  806. * @return none
  807. */
  808. bool ble_sm_passkey_input(uint16_t con_handle, uint32_t passkey);
  809. /**
  810. * @brief Delete the bonding information of a specified address
  811. * @param address_type
  812. * @param address
  813. * @return return true if ok, invalid parameters will return false
  814. */
  815. bool ble_delete_bonding_for_addr(bd_addr_type_t address_type, bd_addr_t address);
  816. /**
  817. * @brief Delete the bonding information of a specified index
  818. * @param index: [0,N), N can be get form ble_get_bonding_cnt() function
  819. * @return return true if ok, invalid parameters will return false
  820. */
  821. bool ble_delete_bonding_for_index(uint8_t index);
  822. /**
  823. * @brief Delete all bonding information
  824. */
  825. void ble_delete_bonding_all(void);
  826. /**
  827. * @brief Get the number of bonding information
  828. * @return The number of binding information
  829. */
  830. uint8_t ble_get_bonding_cnt(void);
  831. /**
  832. * @brief Gets the bonding information for the specified index
  833. * @param index: [0,N), N can be get form ble_get_bonding_cnt() function
  834. * @return specified bonding information
  835. */
  836. sm_bonding_info_t* ble_get_bonding_info_for_index(uint8_t index);
  837. /**
  838. * @brief Initialize the timer memery pool.
  839. * @note This function and a series of related functions are invoked after bt init.
  840. * @param pool: memery pool head address.
  841. * @param size: byte size of memery pool, one alarm timer need 28Bytes memery.
  842. * @retval None.
  843. */
  844. void bt_alarm_timer_init(void *pool, uint32_t size);
  845. /**
  846. * @brief Extend the timer memery pool, it can be used before and after initialization.
  847. * @param pool: memery pool head address.
  848. * @param size: byte size of memery pool, one alarm timer need 28Bytes memery.
  849. * @retval result code.
  850. */
  851. uint8_t bt_alarm_timer_mem_extend(void *pool, uint32_t size);
  852. /**
  853. * @brief Free the timer memery pool, and all timers in the space are released.
  854. * @param pool: memery pool head address.
  855. * @param size: byte size of memery pool, one alarm timer need 28Bytes memery.
  856. * @retval result code.
  857. */
  858. uint8_t bt_alarm_timer_mem_free(void *pool, uint32_t size);
  859. /**
  860. * @brief Start specified id alarm timer.
  861. * @param handle: alarm timer handle.
  862. * @retval result code.
  863. */
  864. uint8_t bt_alarm_timer_start(uint32_t handle);
  865. /**
  866. * @brief Stop specified id alarm timer.
  867. * @param handle: alarm timer handle.
  868. * @retval result code.
  869. */
  870. uint8_t bt_alarm_timer_stop(uint32_t handle);
  871. /**
  872. * @brief Acquire a alarm timer.
  873. * @param handle: alarm timer handle.
  874. * @param interval: alarm timer trigger interval, unit [1, 0xffffff) ms.
  875. * @param is_repeat: trigger mode.
  876. * @param callback: callback function.
  877. * @retval result code.
  878. */
  879. uint8_t bt_alarm_timer_acquire(uint32_t *handle, uint32_t interval, uint8_t is_repeat, void (*callback)(uint32_t handle));
  880. /**
  881. * @brief Release specified id alarm timer.
  882. * @param handle: alarm timer handle.
  883. * @retval result code.
  884. */
  885. uint8_t bt_alarm_timer_release(uint32_t handle);
  886. /**
  887. * @brief Query specified id alarm timer remain time.
  888. * @param handle: alarm timer handle.
  889. * @param remain: the remain time for specified id alarm timer.
  890. * @retval result code.
  891. */
  892. uint8_t bt_alarm_timer_query_time(uint32_t handle, uint32_t *remain);
  893. /**
  894. * @brief Modify specified id alarm timer interval, if the timer is start, it
  895. * will be re-start after update interval param.
  896. * @param handle: alarm timer handle.
  897. * @param interval: new interval, unit [1, 0xffffff) ms.
  898. * @param is_repeat: new is_repeat mode state.
  899. * @retval result code.
  900. */
  901. uint8_t bt_alarm_timer_modify_interval(uint32_t handle, uint32_t interval, uint8_t is_repeat);
  902. /**
  903. * @brief Get the time that depends on the Bluetooth clock.
  904. * @retval The value of current time, unit: (312.5 us).
  905. */
  906. uint32_t bb_time_hs_get(void);
  907. /**
  908. * @brief Get connection rssi .
  909. * @param conhdl: connect handle.
  910. * @retval -128 ~ 127dBm.
  911. */
  912. int8_t ble_get_conn_rssi(uint16_t conhdl);
  913. /**
  914. * @brief Set device random static address.
  915. * @param device random static address.
  916. * @return none
  917. */
  918. void ble_set_static_addr(uint8_t *addr);
  919. void ble_event_start_hook_register(uint16_t con_handle, void (*callback)(void));
  920. /**
  921. * @brief Config BLE DataLength suggested max length, it should be invoke after stack init and before link created.
  922. * @param rx_octets: range [27, 251] in bytes, DataLengthExchange will refered to.
  923. * @param tx_octets: range [27, 251] in bytes, DataLengthExchange will refered to.
  924. * @retval none
  925. */
  926. void ble_suggested_max_length_write(uint16_t rx_octets, uint16_t tx_octets);
  927. #endif //_API_BTSTACK_H