furi_hal_bt.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /**
  2. * @file furi_hal_bt.h
  3. * BT/BLE HAL API
  4. */
  5. #pragma once
  6. #include <m-string.h>
  7. #include <stdbool.h>
  8. #include <gap.h>
  9. #include <serial_service.h>
  10. #include <ble_glue.h>
  11. #include <ble_app.h>
  12. #include "furi_hal_bt_serial.h"
  13. #define FURI_HAL_BT_STACK_VERSION_MAJOR (1)
  14. #define FURI_HAL_BT_STACK_VERSION_MINOR (13)
  15. #define FURI_HAL_BT_C2_START_TIMEOUT 1000
  16. #ifdef __cplusplus
  17. extern "C" {
  18. #endif
  19. typedef enum {
  20. FuriHalBtStackUnknown,
  21. FuriHalBtStackHciLayer,
  22. FuriHalBtStackLight,
  23. } FuriHalBtStack;
  24. typedef enum {
  25. FuriHalBtProfileSerial,
  26. FuriHalBtProfileHidKeyboard,
  27. // Keep last for Profiles number calculation
  28. FuriHalBtProfileNumber,
  29. } FuriHalBtProfile;
  30. /** Initialize
  31. */
  32. void furi_hal_bt_init();
  33. /** Lock core2 state transition */
  34. void furi_hal_bt_lock_core2();
  35. /** Lock core2 state transition */
  36. void furi_hal_bt_unlock_core2();
  37. /** Start radio stack
  38. *
  39. * @return true on successfull radio stack start
  40. */
  41. bool furi_hal_bt_start_radio_stack();
  42. /** Get radio stack type
  43. *
  44. * @return FuriHalBtStack instance
  45. */
  46. FuriHalBtStack furi_hal_bt_get_radio_stack();
  47. /** Start BLE app
  48. *
  49. * @param profile FuriHalBtProfile instance
  50. * @param event_cb GapEventCallback instance
  51. * @param context pointer to context
  52. *
  53. * @return true on success
  54. */
  55. bool furi_hal_bt_start_app(FuriHalBtProfile profile, GapEventCallback event_cb, void* context);
  56. /** Reinitialize core2
  57. *
  58. * Also can be used to prepare core2 for stop modes
  59. */
  60. void furi_hal_bt_reinit();
  61. /** Change BLE app
  62. * Restarts 2nd core
  63. *
  64. * @param profile FuriHalBtProfile instance
  65. * @param event_cb GapEventCallback instance
  66. * @param context pointer to context
  67. *
  68. * @return true on success
  69. */
  70. bool furi_hal_bt_change_app(FuriHalBtProfile profile, GapEventCallback event_cb, void* context);
  71. /** Update battery level
  72. *
  73. * @param battery_level battery level
  74. */
  75. void furi_hal_bt_update_battery_level(uint8_t battery_level);
  76. /** Update battery power state */
  77. void furi_hal_bt_update_power_state();
  78. /** Checks if BLE state is active
  79. *
  80. * @return true if device is connected or advertising, false otherwise
  81. */
  82. bool furi_hal_bt_is_active();
  83. /** Start advertising
  84. */
  85. void furi_hal_bt_start_advertising();
  86. /** Stop advertising
  87. */
  88. void furi_hal_bt_stop_advertising();
  89. /** Get BT/BLE system component state
  90. *
  91. * @param[in] buffer string_t buffer to write to
  92. */
  93. void furi_hal_bt_dump_state(string_t buffer);
  94. /** Get BT/BLE system component state
  95. *
  96. * @return true if core2 is alive
  97. */
  98. bool furi_hal_bt_is_alive();
  99. /** Get key storage buffer address and size
  100. *
  101. * @param key_buff_addr pointer to store buffer address
  102. * @param key_buff_size pointer to store buffer size
  103. */
  104. void furi_hal_bt_get_key_storage_buff(uint8_t** key_buff_addr, uint16_t* key_buff_size);
  105. /** Get SRAM2 hardware semaphore
  106. * @note Must be called before SRAM2 read/write operations
  107. */
  108. void furi_hal_bt_nvm_sram_sem_acquire();
  109. /** Release SRAM2 hardware semaphore
  110. * @note Must be called after SRAM2 read/write operations
  111. */
  112. void furi_hal_bt_nvm_sram_sem_release();
  113. /** Clear key storage
  114. *
  115. * @return true on success
  116. */
  117. bool furi_hal_bt_clear_white_list();
  118. /** Set key storage change callback
  119. *
  120. * @param callback BleGlueKeyStorageChangedCallback instance
  121. * @param context pointer to context
  122. */
  123. void furi_hal_bt_set_key_storage_change_callback(
  124. BleGlueKeyStorageChangedCallback callback,
  125. void* context);
  126. /** Start ble tone tx at given channel and power
  127. *
  128. * @param[in] channel The channel
  129. * @param[in] power The power
  130. */
  131. void furi_hal_bt_start_tone_tx(uint8_t channel, uint8_t power);
  132. /** Stop ble tone tx
  133. */
  134. void furi_hal_bt_stop_tone_tx();
  135. /** Start sending ble packets at a given frequency and datarate
  136. *
  137. * @param[in] channel The channel
  138. * @param[in] pattern The pattern
  139. * @param[in] datarate The datarate
  140. */
  141. void furi_hal_bt_start_packet_tx(uint8_t channel, uint8_t pattern, uint8_t datarate);
  142. /** Stop sending ble packets
  143. *
  144. * @return sent packet count
  145. */
  146. uint16_t furi_hal_bt_stop_packet_test();
  147. /** Start receiving packets
  148. *
  149. * @param[in] channel RX channel
  150. * @param[in] datarate Datarate
  151. */
  152. void furi_hal_bt_start_packet_rx(uint8_t channel, uint8_t datarate);
  153. /** Set up the RF to listen to a given RF channel
  154. *
  155. * @param[in] channel RX channel
  156. */
  157. void furi_hal_bt_start_rx(uint8_t channel);
  158. /** Stop RF listenning
  159. */
  160. void furi_hal_bt_stop_rx();
  161. /** Get RSSI
  162. *
  163. * @return RSSI in dBm
  164. */
  165. float furi_hal_bt_get_rssi();
  166. /** Get number of transmitted packets
  167. *
  168. * @return packet count
  169. */
  170. uint32_t furi_hal_bt_get_transmitted_packets();
  171. /** Start MAC addresses scan
  172. * @note Works only with HciLayer 2nd core firmware
  173. *
  174. * @param callback GapScanCallback instance
  175. * @param context pointer to context
  176. */
  177. bool furi_hal_bt_start_scan(GapScanCallback callback, void* context);
  178. /** Stop MAC addresses scan */
  179. void furi_hal_bt_stop_scan();
  180. /** Check & switch C2 to given mode
  181. *
  182. * @param[in] mode mode to switch into
  183. */
  184. bool furi_hal_bt_ensure_c2_mode(BleGlueC2Mode mode);
  185. #ifdef __cplusplus
  186. }
  187. #endif