bsal_app_api.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. /*
  2. * Copyright (c) 2006-2020, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2020-05-28 Supperthomas the first version
  9. */
  10. #ifndef __BSAL_APP_API_H__
  11. #define __BSAL_APP_API_H__
  12. #ifdef __cplusplus
  13. extern "C" {
  14. #endif
  15. #include <stdint.h>
  16. #include <stdbool.h>
  17. #include "bsal_common.h"
  18. //0. CALLED BY STACK
  19. /**
  20. * BSAL connect status callback
  21. *
  22. * @param stack_ptr the point of the stack object
  23. * @param conn_handle the connect handle of link
  24. * @param conn_status the connection status
  25. * @Note callback the connect status called by stack
  26. */
  27. int bsal_gap_connect_status(void *stack_ptr, uint16_t conn_handle, uint8_t connect_status, uint16_t disconnect_status);
  28. //====================================ENTER MAIN==============================
  29. //=================================APP API CALLED BY APP=======================
  30. /**
  31. * BSAL find the stack ptr
  32. *
  33. * @param stack_ptr the point of the stack object
  34. * @param stack_name the name of the stack
  35. * @Note find the object ptr of the stack by the name
  36. * called by APP
  37. */
  38. void *bsal_find_stack_ptr(char *stack_name);
  39. /**
  40. * BSAL init the stack
  41. *
  42. * @param stack_ptr the point of the stack object
  43. * @param callback the function to deal with the event
  44. * @Note find the object ptr of the stack by the name
  45. * called by APP
  46. */
  47. int bsal_stack_init(void *stack_ptr, void *callback);
  48. /**
  49. * BSAL start register the profile table
  50. *
  51. * @param stack_ptr the point of the stack object
  52. * @param num the all number of the profile table
  53. * @param p_fun_cb the function of the profile to callback
  54. * @Note start to register the profile with the num
  55. */
  56. void bsal_stack_le_srv_begin(void *stack_ptr, uint8_t num, void *p_fun_cb);
  57. /**
  58. * BSAL end register the profile table
  59. *
  60. * @param stack_ptr the point of the stack object
  61. * @Note the end to register the profile with the num
  62. */
  63. void bsal_stack_le_srv_end(void *stack_ptr);
  64. /**
  65. * BSAL start the stack
  66. *
  67. * @param stack_ptr the point of the stack object
  68. * @Note to start the stack
  69. */
  70. void bsal_stack_startup(void *stack_ptr);
  71. //2. SERVICE CALLBACK API APP NEED KNOWN
  72. typedef enum
  73. {
  74. BSAL_CALLBACK_TYPE_READ_CHAR_VALUE = 1, /**< client read event */
  75. BSAL_CALLBACK_TYPE_WRITE_CHAR_VALUE = 2, /**< client write event */
  76. BSAL_CALLBACK_TYPE_INDIFICATION_NOTIFICATION = 3, /**< CCCD update event */
  77. BSAL_CALLBACK_TYPE_HANDLE_TABLE = 4, /**< handle event */
  78. } T_BSAL_SRV_CB_TYPE;
  79. struct bsal_callbak_data
  80. {
  81. void *stack_ptr;
  82. uint8_t conn_id;
  83. T_BSAL_SRV_CB_TYPE msg_type;
  84. uint16_t start_handle;
  85. uint32_t off_handle;
  86. uint16_t length;
  87. uint8_t *data;
  88. uint32_t value; /**< CCCD save the temp value */
  89. bsal_uuid_any_t srv_uuid;
  90. };
  91. typedef struct bsal_callbak_data bsal_callbak_data_t;
  92. //3.ADV OPERATE
  93. struct le_adv_ltv
  94. {
  95. uint8_t length;
  96. uint8_t type;
  97. uint8_t *value;
  98. };
  99. typedef struct le_adv_ltv le_adv_ltv_t;
  100. /**
  101. * BSAL ADD FLAG LTV TO adv_data.
  102. *
  103. * @param adv_data the array of adv_data
  104. * @param flag the flag value of adv_type
  105. * @Note the function is used for add flag
  106. */
  107. void bsal_le_adv_data_add_flag(uint8_t *adv_data , uint8_t flag);
  108. /**
  109. * BSAL ADD name
  110. *
  111. * @param adv_data the array of adv_data
  112. * @param name_length the length of name
  113. * @note the function is used for add fla g
  114. */
  115. void bsal_adv_data_add_name(uint8_t *adv_data, uint8_t name_length, char *name);
  116. /**
  117. * BSAL ADD ONE UUID
  118. *
  119. * @param adv_data the array of adv_data
  120. * @param uuid the uuid valule of service
  121. * @note the function is used for add UUID
  122. */
  123. void bsal_adv_data_add_uuid16(uint8_t *adv_data, uint16_t uuid);
  124. /**
  125. * BSAL add LTV TO adv_data.
  126. *
  127. * @param adv_data the array of adv_data
  128. * @param ltv the struct of lengthTypeValue
  129. * @param timeout the maximum response time
  130. * @Note the function is used in combine the adv_data of ltv
  131. */
  132. void bsal_le_adv_data_add_ltv(uint8_t *adv_data, le_adv_ltv_t *ltv);
  133. /**
  134. * BSAL Set the adv data to stack.
  135. *
  136. * @param stack_ptr the object of the stack
  137. * @param adv_data the array of adv_data
  138. * @Note the function is used for add the data to stack
  139. */
  140. void bsal_set_le_adv_data_user(void *stack_ptr, uint8_t *adv_data);
  141. /**
  142. * BSAL Set the scan rsp data to stack.
  143. *
  144. * @param stack_ptr the object of the stack
  145. * @param scan_rsp_data the array of scan_rsp_data
  146. * @Note the function is used for add the scan response data to stack
  147. */
  148. void bsal_set_le_scan_rsp_data_user(void *stack_ptr, uint8_t *scan_rsp_data);
  149. /** @defgroup BSAL_SET_LE_PARAM_TYPE
  150. * @{
  151. */
  152. #define BSAL_SET_LE_ADV_PARAM BIT(0)
  153. #define BSAL_SET_LE_SCAN_RSP_DATA BIT(1)
  154. #define BSAL_SET_LE_ADV_DATA BIT(2)
  155. /**
  156. * @}
  157. */
  158. struct bsal_adv_param
  159. {
  160. uint8_t set_param_flag; //!< set the param bit see #BSAL_SET_LE_PARAM_TYPE
  161. uint16_t adv_int_min; //!< set adv interval min
  162. uint16_t adv_int_max; //!< set adv interval max
  163. uint8_t adv_type; //!< set adv type
  164. uint8_t own_address_type; //!< own address type
  165. uint8_t direct_address_type; //!< set direct bt mac address type
  166. uint8_t direct_address[6]; //!< set the direct mac address
  167. uint8_t channel_map; //!< set the channel map see @ref BTCHANNEL_MAP
  168. uint8_t filter_policy; //!< the filter policy see @ref ADV_PARAM_FILTER_POLICY
  169. uint8_t scan_rsp_data[BSAL_MAX_ADV_SCAN_DATA_LENGTH]; //the scan rsp data
  170. uint8_t adv_data[BSAL_MAX_ADV_SCAN_DATA_LENGTH]; //the adv data
  171. };
  172. typedef struct bsal_adv_param bsal_adv_param_t;
  173. /**
  174. * BSAL set le advertising param.
  175. *
  176. * @param stack_ptr the object of the stack
  177. * @param adv_param the point of the adv_param
  178. * @Note the function is used for set the adv param
  179. */
  180. void bsal_set_le_adv_parm(void *stack_ptr, bsal_adv_param_t *adv_param);
  181. /**
  182. * BSAL start adv
  183. *
  184. * @param stack_ptr the object of the stack
  185. * @Note the function is used for start tx the adv
  186. */
  187. int bsal_stack_start_adv(void *stack_ptr);
  188. /**
  189. * BSAL stop adv
  190. *
  191. * @param stack_ptr the object of the stack
  192. * @Note the function is used for stop tx the adv
  193. */
  194. int bsal_stack_stop_adv(void *stack_ptr);
  195. //BOND
  196. //self define
  197. typedef enum
  198. {
  199. BSAL_NO_INPUT,
  200. BSAL_INPUT_YES_NO,
  201. BSAL_INPUT_KEYBOARD,
  202. } T_BSAL_GAP_INPUT_CAP;
  203. typedef enum
  204. {
  205. BSAL_NO_OUTPUT,
  206. BSAL_YES_OUTPUT,
  207. } T_BSAL_GAP_OUTPUT_CAP;
  208. //bond
  209. struct bsal_bond_param
  210. {
  211. uint8_t LE_BR_EDR_FLAG; // LE is 1 BR/EDR is 0
  212. bool bonding_flag; //is bond
  213. uint8_t oob_flag; //not support;
  214. uint8_t auth_type; //AuthReq flag of device
  215. uint8_t io_capability; //io_capability
  216. //useless
  217. uint8_t force_auth_type;
  218. bool fixed_key_flag;
  219. uint32_t fixed_key;
  220. bool sm_req_enable;
  221. };
  222. typedef struct bsal_bond_param bsal_bond_param_t;
  223. /**
  224. * BSAL set the bond type
  225. *
  226. * @param stack_ptr the object of the stack see
  227. * @param is_bond the bool about is needed bond
  228. * @param input the ability of input see #T_BSAL_GAP_INPUT_CAP
  229. * @param output the ability of output see #T_BSAL_GAP_OUTPUT_CAP
  230. * @param bond_type the bond type of the #BSAL_GAP_AUTH_TYPE
  231. * @Note the function is used for stop tx the adv
  232. */
  233. uint16_t bsal_set_device_le_bond_type(void *stack_ptr, bool is_bond, uint8_t input, uint8_t output, uint8_t bond_type, bool oob_enable);
  234. /**
  235. * BSAL set the bond param
  236. *
  237. * @param stack_ptr the object of the stack see
  238. * @param bond_param the param of the bond
  239. * @Note the function is used for le bond param directly
  240. */
  241. uint16_t bsal_set_device_le_bond_param(void *stack_ptr, bsal_bond_param_t *bond_param);
  242. struct bsal_gaps_param
  243. {
  244. uint8_t name[BSAL_MAX_NAME_LENGTH]; //!< The GAP's SHOW NAME MAX LENGTH IS 40
  245. uint16_t apperance; //!< The GAP's apperance
  246. };
  247. typedef struct bsal_gaps_param bsal_gaps_param_t;
  248. /**
  249. * BSAL set gap param set the param of gap
  250. *
  251. * @param stack_ptr the object of the stack
  252. * @param gaps_param the struct of the gaps_param see #bsal_gaps_param_t
  253. * @Note the function is set the gap param
  254. */
  255. void bsal_set_gap_param(void *stack_ptr, bsal_gaps_param_t *gaps_param);
  256. /**
  257. * BSAL set set the device name
  258. *
  259. * @param stack_ptr the object of the stack
  260. * @param length the length of the name
  261. * @param name the buffer of the name
  262. * @Note the function is set the device name
  263. */
  264. int bsal_set_device_name(void *stack_ptr, uint8_t length, uint8_t *name);
  265. /**
  266. * BSAL set get the device name
  267. *
  268. * @param stack_ptr the object of the stack
  269. * @Note the function is get the device name
  270. */
  271. uint8_t *bsal_get_device_name(void *stack_ptr);
  272. //========================================PROFILE API==CALLED BY SERVICE===========================
  273. //2. SERVICE API
  274. typedef BSAL_STATUS(*P_SRV_GENERAL_CB)(void *p_para);
  275. /**
  276. * BSAL register the profile table
  277. *
  278. * @param stack_ptr the object of the stack
  279. * @param srv_def the table of the service
  280. * @param callback the callback function of the profile
  281. * @Note the function is used for register the servcie table
  282. * called by the profile
  283. */
  284. int bsal_stack_le_srv_reg_func(void *stack_ptr, void *srv_def, P_SRV_GENERAL_CB *callback);
  285. //3. SERVICE SEND DATA
  286. /**
  287. * BSAL send the notify data
  288. *
  289. * @param stack_ptr the object of the stack
  290. * @param conn_id the connect id of the link(like conn_handle)
  291. * @param service_id the identify of the profile
  292. * @param offset_handle the offset handle of the att in the profile
  293. * @param value_length the data length of the data
  294. * @param value the buff of the data
  295. * @Note called by the profile to send the notify
  296. */
  297. int bsal_srv_send_notify_data(void *stack_ptr, uint16_t conn_id, uint16_t service_id, uint16_t offset_handle, uint16_t value_length, uint8_t *value);
  298. /**
  299. * BSAL send the indicate data
  300. *
  301. * @param stack_ptr the object of the stack
  302. * @param conn_id the connect id of the link(like conn_handle)
  303. * @param service_id the identify of the profile
  304. * @param offset_handle the offset handle of the att in the profile
  305. * @param value_length the data length of the data
  306. * @param value the buff of the data
  307. * @Note called by the profile to send the indicate
  308. */
  309. int bsal_srv_send_ind_data(void *stack_ptr, uint16_t conn_id, uint16_t service_id, uint16_t offset_handle, uint16_t value_length, uint8_t *value);
  310. /**
  311. * BSAL get the start handle
  312. *
  313. * @param stack_ptr the object of the stack
  314. * @param uuid the profile main service uuid
  315. * @Note called by the profile to get the start handle
  316. */
  317. uint16_t bsal_srv_get_start_handle(void *stack_ptr, bsal_uuid_any_t uuid);
  318. /**
  319. * BSAL get the start handle by value handle
  320. *
  321. * @param stack_ptr the object of the stack
  322. * @param handle the u_handle of the device
  323. * @Note called by the profile to get the start handle
  324. */
  325. uint16_t bsal_profile_get_start_handle(void *stack_ptr, uint16_t value_handle);
  326. /**
  327. * BSAL WRITE ATT
  328. *
  329. * @param p_db the database of data
  330. * @param u_handle the unique handle of mcu
  331. * @note the function is used for read attribute
  332. */
  333. int bsal_srv_write_data(void *stack_ptr, uint16_t start_handle, uint16_t offset_handle, uint16_t value_length, uint8_t *value);
  334. /**
  335. * BSAL READ ATT
  336. *
  337. * @param p_db the database of data
  338. * @param u_handle the unique handle of mcu
  339. * @note the function is used for read attribute
  340. */
  341. int bsal_srv_read_data(void *stack_ptr, uint16_t start_handle, uint16_t offset_handle, uint16_t *value_length, uint8_t **value);
  342. /**
  343. * BSAL CREATE ATT
  344. *
  345. * @param p_db the database of data
  346. * @param u_handle the unique handle of mcu
  347. * @note the function is used for read attribute
  348. */
  349. int bsal_srv_create_att(void *stack_ptr, uint16_t start_handle, uint16_t offset_handle, uint16_t value_length);
  350. /**
  351. * BSAL WRITE ATT
  352. *
  353. * @param p_db the database of data
  354. * @param u_handle the unique handle of mcu
  355. * @note the function is used for read attribute
  356. */
  357. int bsal_srv_write_data_by_handle(void *stack_ptr, uint16_t u_handle, uint16_t value_length, uint8_t *value);
  358. /**
  359. * BSAL READ ATT
  360. *
  361. * @param p_db the database of data
  362. * @param u_handle the unique handle of mcu
  363. * @note the function is used for read attribute
  364. */
  365. int bsal_srv_read_data_by_handle(void *stack_ptr, uint16_t u_handle, uint16_t *value_length, uint8_t **value);
  366. /**
  367. * BSAL CREATE ATT
  368. *
  369. * @param p_db the database of data
  370. * @param u_handle the unique handle of mcu
  371. * @note the function is used for read attribute
  372. */
  373. int bsal_srv_create_att_by_handle(void *stack_ptr, uint16_t u_handle, uint16_t value_length);
  374. //=========================================STACK API ==CALLED BY STACK===========
  375. /**
  376. * BSAL READ ATT EVENT
  377. *
  378. * @param stack_ptr point of the stack
  379. * @param conn_handle conn id of the link
  380. * @param start_handle the start handle of the profile
  381. * @param offset_handle the att value of the handle
  382. * @param length the length of the value
  383. * @param[out] the buffer address of the value
  384. * @note read the callback event, called by the stack
  385. */
  386. void bsal_gatt_read_callback_event(void *stack_ptr, uint16_t conn_handle, uint16_t start_handle, uint16_t offset_handle, uint16_t *length , uint8_t **value);
  387. /**
  388. * @brief BSAL write ATT EVENT
  389. *
  390. * @param stack_ptr point of the stack
  391. * @param conn_handle conn id of the link
  392. * @param start_handle the start handle of the profile
  393. * @param offset_handle the att value of the handle
  394. * @param length the length of the value
  395. * @param value the data buffer address
  396. * @note write the callback event, called by the stack
  397. */
  398. void bsal_gatt_write_callback_event(void *stack_ptr, uint16_t conn_handle, uint16_t start_handle, uint16_t offset_handle, uint16_t length , uint8_t *value);
  399. /**
  400. * @brief BSAL CCCD ATT EVENT
  401. *
  402. * @param stack_ptr point of the stack
  403. * @param conn_handle conn id of the link
  404. * @param start_handle the start handle of the profile
  405. * @param offset_handle the att offset handle
  406. * @param length the length of the value
  407. * @param value the cccd_bit of the device
  408. * @note cccd the callback event, called by the stack
  409. */
  410. void bsal_gatt_cccd_callback_event(void *stack_ptr, uint16_t conn_handle, uint16_t start_handle, uint16_t offset_handle, uint16_t value);
  411. /**
  412. * BSAL READ ATT EVENT
  413. *
  414. * @param stack_ptr point of the stack
  415. * @param conn_handle conn id of the link
  416. * @param u_handle the unique handle of the device
  417. * @param length the length of the value
  418. * @param[out] the buffer address of the value
  419. * @note read the callback event, called by the stack
  420. */
  421. void bsal_gatt_read_callback_event_by_handle(void *stack_ptr, uint16_t conn_handle, uint16_t u_handle, uint16_t *length , uint8_t **value);
  422. /**
  423. * @brief BSAL write ATT EVENT
  424. *
  425. * @param stack_ptr point of the stack
  426. * @param conn_handle conn id of the link
  427. * @param u_handle the unique handle of the device
  428. * @param length the length of the value
  429. * @param value the data buffer address
  430. * @note write the callback event, called by the stack
  431. */
  432. void bsal_gatt_write_callback_event_by_handle(void *stack_ptr, uint16_t conn_handle, uint16_t u_handle, uint16_t length , uint8_t *value);
  433. /**
  434. * @brief BSAL CCCD ATT EVENT
  435. *
  436. * @param stack_ptr point of the stack
  437. * @param conn_handle conn id of the link
  438. * @param u_handle the unique handle of the device
  439. * @param value the CCCD value of the device
  440. * @note cccd the callback event, called by the stack
  441. */
  442. void bsal_gatt_cccd_callback_event_by_handle(void *stack_ptr, uint16_t conn_handle, uint16_t u_handle , uint16_t value);
  443. #ifdef __cplusplus
  444. }
  445. #endif
  446. #endif