api_wireless.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. #ifndef _API_WIRELESS_H
  2. #define _API_WIRELESS_H
  3. /**
  4. * @format 216
  5. * @param con_handle
  6. * @param address_type
  7. * @param address
  8. */
  9. #define WIRELESS_EVENT_CONNECTED 0x10
  10. /**
  11. * @format 21
  12. * @param con_handle
  13. * @param disc_reason
  14. */
  15. #define WIRELESS_EVENT_DISCONNECTED 0x11
  16. /**
  17. * @format 11611N
  18. * @param advertising_event_type
  19. * @param address_type
  20. * @param address
  21. * @param rssi
  22. * @param data_length
  23. * @param data
  24. */
  25. #define WIRELESS_EVENT_ADV_REPORT 0x12
  26. /**
  27. * @format 1
  28. * @param reason
  29. * @var 0x04: init(page) state timeout.
  30. * @var 0x0c: connection command reject.
  31. * @var 0x0d: resource limit, bandwidth no enough for new link.
  32. */
  33. #define WIRELESS_EVENT_INIT_TERMINATED 0x14
  34. typedef void (* wireless_event_callback_t)(uint8_t event_type, uint8_t *packet, uint8_t size);
  35. typedef void (* wireless_receive_callback_t)(uint16_t con_handle, uint8_t *packet, uint8_t size);
  36. typedef struct PACKED {
  37. uint8_t length;
  38. uint8_t type;
  39. uint8_t data[];
  40. } wireless_adv_slice_t;
  41. typedef struct PACKED {
  42. uint8_t event_type;
  43. uint8_t addr_type;
  44. uint8_t addr[6];
  45. int8_t rssi;
  46. uint8_t report_len;
  47. uint8_t report[];
  48. } wireless_adv_report_t;
  49. typedef struct PACKED {
  50. uint8_t reason;
  51. } wireless_init_terminated_t;
  52. typedef struct {
  53. uint16_t intv;
  54. uint8_t adv_type;
  55. uint8_t peer_addr_type;
  56. uint8_t peer_addr[6];
  57. uint8_t data_len;
  58. uint8_t data_buf[31];
  59. } wireless_adv_param_t;
  60. typedef struct {
  61. uint8_t conn_nb_support;
  62. uint8_t conn_phy_forced;
  63. } wireless_setup_param_t;
  64. /**
  65. * @brief Wireless initialize event callback function registration.
  66. * @param handler: callback function which will be trigger when any event happen.
  67. * @retval None.
  68. */
  69. void wireless_event_handler_register(wireless_event_callback_t handler);
  70. /**
  71. * @brief Wireless initialize packet data receive callback function registration.
  72. * @param handler: callback function which will be trigger when new packet data received.
  73. * @retval None.
  74. */
  75. void wireless_receive_handler_register(wireless_receive_callback_t handler);
  76. /**
  77. * @brief Send data according to connection handle.
  78. * @param con_handle: connection handle.
  79. * @param buf: data buffer need to xfer.
  80. * @param size: length of data need to xfer in buf.
  81. * @retval Return 0 if OK, other is wrong.
  82. */
  83. int wireless_send_for_con(uint16_t con_handle, uint8_t *buf, uint16_t size);
  84. /**
  85. * @brief Release rxbuf and prepare for the next reception.
  86. * @param buffer: rxbuf, must be generated by the `wireless_receive_handler_register` callback func.
  87. * @retval Return 0 if OK, other is wrong.
  88. */
  89. int wireless_rxbuf_release(uint8_t *buffer);
  90. /**
  91. * @brief Wireless master create a connection by address.
  92. * interval, latency and timeout need to meet the follwing relationships:
  93. * (timeout * 10) > [(1 + latency) * interval * 1.25 * 2]
  94. * @param addr: peer device address.
  95. * @param addr_type: peer device address type.
  96. * @param interval: 2 - 3200 (unit: 1.25ms).
  97. * 4 - 3200 (unit: 1.25ms, more connections).
  98. * @param latency: 0 - 499 (unit: intv)
  99. * @param timeout: 10 - 3200 (unit: 10ms)
  100. * @param init_timeout: 1 - 0xfffffffe (unit: 1ms), and -1L means no timeout. timeout ind see `WIRELESS_EVENT_INIT_TERMINATED`
  101. * @retval Return 0 if OK, other is wrong.
  102. */
  103. int wireless_connect_by_addr(uint8_t *addr, uint8_t addr_type, uint16_t interval, uint16_t latency, uint16_t timeout, uint32_t init_timeout);
  104. /**
  105. * @brief Wireless set scan param.
  106. * @param type: scan type, select the following:
  107. * @arg 0: passive scanning.
  108. * @arg 1: active scanning.
  109. * @param interval: 4 - 16384 (unit: 0.625ms)
  110. * @param window: 4 - 16384 (unit: 0.625ms), should <= interval.
  111. * @retval Return 0 if OK, other is wrong.
  112. */
  113. int wireless_scan_param_set(uint8_t type, uint16_t interval, uint16_t window);
  114. /**
  115. * @brief Wireless scan control function.
  116. * @param enable: true will start scan if allow, or stop.
  117. * @retval Return 0 if OK, other is wrong.
  118. */
  119. int wireless_scan_ctrl(bool enable);
  120. /**
  121. * @brief Wireless scan state check, async exec by le msg.
  122. * @retval Return true if scan active, else is stopped.
  123. */
  124. bool wireless_scan_state_get(void);
  125. /**
  126. * @brief Wireless set advertisement param.
  127. * @param wireless_adv_param: adv param, see struct "wireless_adv_param_t".
  128. * @retval Return 0 if OK, other is wrong.
  129. */
  130. int wireless_adv_param_set(wireless_adv_param_t wireless_adv_param);
  131. /**
  132. * @brief Wireless adv control function.
  133. * @param enable: true will start adv if allow, or stop.
  134. * @retval Return 0 if OK, other is wrong.
  135. */
  136. int wireless_adv_ctrl(bool enable);
  137. /**
  138. * @brief Wireless adv state check, async exec by le msg.
  139. * @retval Return true if ADV active, else is stopped.
  140. */
  141. bool wireless_adv_state_get(void);
  142. /**
  143. * @brief Wireless config if prefem a latency.
  144. * @param con_handle: connection handle.
  145. * @param applied: true will allow wireless perform latency, or disallow.
  146. * @retval Return 0 if OK, other is wrong.
  147. */
  148. int wireless_latency_applied(uint16_t con_handle, bool applied);
  149. /**
  150. * @brief disconnect connection according to connect handle
  151. * @param con_handle
  152. */
  153. int wireless_disconnect(uint16_t con_handle);
  154. /**
  155. * @brief Update connection parameter according to connection handle.
  156. * @param con_handle: connection handle.
  157. * @param intv_min: 2 - 3200(unit: 1.25ms)
  158. * 4 - 3200(unit: 1.25ms, more connection)
  159. * @param intv_max: 2 - 3200(unit: 1.25ms) interval_min <= interval_max
  160. * 4 - 3200(unit: 1.25ms, more connection)
  161. * @param lat: 0 - 499 (unit: intv)
  162. * @param sup_to: 10 - 3200 (unit: 10ms)
  163. * @retval Return 0 if OK, other is wrong.
  164. */
  165. int wireless_conn_update(uint16_t con_handle, uint16_t intv_min, uint16_t intv_max, uint16_t lat, uint16_t sup_to);
  166. /**
  167. * @brief Query connection parameter according to connction handle.
  168. * @param con_handle: connection handle.
  169. * @param intv: return conn interval value.
  170. * @param lat: return conn latency value.
  171. * @param sup_to: return conn supervise timeout value.
  172. * @retval Return 0 if OK, other is wrong.
  173. */
  174. int wireless_conn_query(uint16_t con_handle, uint16_t *intv, uint16_t *lat, uint16_t *sup_to);
  175. /**
  176. * @brief Query maximum transmission unit according to connection handle.
  177. * @param con_handle: connection handle.
  178. * @param mtu[out]: return mtu value.
  179. * @retval Return 0 if OK, other is wrong.
  180. */
  181. int wireless_mtu_get(uint16_t con_handle, uint8_t *mtu);
  182. /**
  183. * @brief Register a function that will be invoke before each active frame start.
  184. * @param con_handle: connection handle.
  185. * @param callback: callback function when before frame start. It would be voke
  186. * @retval Return 0 if OK, other is wrong.
  187. */
  188. int wireless_frame_start_hook_register(uint16_t con_handle, void (*callback)(void));
  189. /**
  190. * @brief Initialize wireless module.
  191. * @param param: parameter configuration for wireless core setup.
  192. * @retval None.
  193. */
  194. void wireless_setup(wireless_setup_param_t const *param);
  195. /**
  196. * @brief De-initialize wireless module.
  197. * @retval None.
  198. */
  199. void wireless_off(void);
  200. /**
  201. * @brief ble fcc test mode config
  202. * @param rf_mode: FCC_RF_MODE
  203. * @param tx_power: [0,7]
  204. * @param tx_freq: [0,39],freq = 2402 + tx_freq * 2
  205. * @param tx_freq: [0,39],freq = 2402 + tx_freq * 2
  206. * @param phy_type: 0:1M 1:2M
  207. * @param todo: 1
  208. */
  209. int fcc_test_ctrl(fcc_cfg_t *cfg);
  210. /**
  211. * @brief Initialize the timer memery pool.
  212. * @note This function and a series of related functions are invoked after bt init.
  213. * @param pool: memery pool head address.
  214. * @param size: byte size of memery pool, one alarm timer need 28Bytes memery.
  215. * @retval None.
  216. */
  217. void bt_alarm_timer_init(void *pool, uint32_t size);
  218. /**
  219. * @brief Extend the timer memery pool, it can be used before and after initialization.
  220. * @param pool: memery pool head address.
  221. * @param size: byte size of memery pool, one alarm timer need 28Bytes memery.
  222. * @retval result code.
  223. */
  224. uint8_t bt_alarm_timer_mem_extend(void *pool, uint32_t size);
  225. /**
  226. * @brief Free the timer memery pool, and all timers in the space are released.
  227. * @param pool: memery pool head address.
  228. * @param size: byte size of memery pool, one alarm timer need 28Bytes memery.
  229. * @retval result code.
  230. */
  231. uint8_t bt_alarm_timer_mem_free(void *pool, uint32_t size);
  232. /**
  233. * @brief Start specified id alarm timer.
  234. * @param handle: alarm timer handle.
  235. * @retval result code.
  236. */
  237. uint8_t bt_alarm_timer_start(uint32_t handle);
  238. /**
  239. * @brief Stop specified id alarm timer.
  240. * @param handle: alarm timer handle.
  241. * @retval result code.
  242. */
  243. uint8_t bt_alarm_timer_stop(uint32_t handle);
  244. /**
  245. * @brief Acquire a alarm timer.
  246. * @param handle: alarm timer handle.
  247. * @param interval: alarm timer trigger interval, unit [1, 0xffffff) ms.
  248. * @param is_repeat: trigger mode.
  249. * @param callback: callback function.
  250. * @retval result code.
  251. */
  252. uint8_t bt_alarm_timer_acquire(uint32_t *handle, uint32_t interval, uint8_t is_repeat, void (*callback)(uint32_t handle));
  253. /**
  254. * @brief Release specified id alarm timer.
  255. * @param handle: alarm timer handle.
  256. * @retval result code.
  257. */
  258. uint8_t bt_alarm_timer_release(uint32_t handle);
  259. /**
  260. * @brief Query specified id alarm timer remain time.
  261. * @param handle: alarm timer handle.
  262. * @param remain: the remain time for specified id alarm timer.
  263. * @retval result code.
  264. */
  265. uint8_t bt_alarm_timer_query_time(uint32_t handle, uint32_t *remain);
  266. /**
  267. * @brief Modify specified id alarm timer interval, if the timer is start, it
  268. * will be re-start after update interval param.
  269. * @param handle: alarm timer handle.
  270. * @param interval: new interval, unit [1, 0xffffff) ms.
  271. * @param is_repeat: new is_repeat mode state.
  272. * @retval result code.
  273. */
  274. uint8_t bt_alarm_timer_modify_interval(uint32_t handle, uint32_t interval, uint8_t is_repeat);
  275. /* LPWR API */
  276. void bt_enter_sleep(void);
  277. bool bt_is_sleep(void);
  278. uint32_t bt_sleep_proc(void);
  279. void bt_exit_sleep(void);
  280. #endif // _API_WIRELESS_H