furi_hal_bt.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. /** Checks if BLE state is active
  77. *
  78. * @return true if device is connected or advertising, false otherwise
  79. */
  80. bool furi_hal_bt_is_active();
  81. /** Start advertising
  82. */
  83. void furi_hal_bt_start_advertising();
  84. /** Stop advertising
  85. */
  86. void furi_hal_bt_stop_advertising();
  87. /** Get BT/BLE system component state
  88. *
  89. * @param[in] buffer string_t buffer to write to
  90. */
  91. void furi_hal_bt_dump_state(string_t buffer);
  92. /** Get BT/BLE system component state
  93. *
  94. * @return true if core2 is alive
  95. */
  96. bool furi_hal_bt_is_alive();
  97. /** Get key storage buffer address and size
  98. *
  99. * @param key_buff_addr pointer to store buffer address
  100. * @param key_buff_size pointer to store buffer size
  101. */
  102. void furi_hal_bt_get_key_storage_buff(uint8_t** key_buff_addr, uint16_t* key_buff_size);
  103. /** Get SRAM2 hardware semaphore
  104. * @note Must be called before SRAM2 read/write operations
  105. */
  106. void furi_hal_bt_nvm_sram_sem_acquire();
  107. /** Release SRAM2 hardware semaphore
  108. * @note Must be called after SRAM2 read/write operations
  109. */
  110. void furi_hal_bt_nvm_sram_sem_release();
  111. /** Clear key storage
  112. *
  113. * @return true on success
  114. */
  115. bool furi_hal_bt_clear_white_list();
  116. /** Set key storage change callback
  117. *
  118. * @param callback BleGlueKeyStorageChangedCallback instance
  119. * @param context pointer to context
  120. */
  121. void furi_hal_bt_set_key_storage_change_callback(
  122. BleGlueKeyStorageChangedCallback callback,
  123. void* context);
  124. /** Start ble tone tx at given channel and power
  125. *
  126. * @param[in] channel The channel
  127. * @param[in] power The power
  128. */
  129. void furi_hal_bt_start_tone_tx(uint8_t channel, uint8_t power);
  130. /** Stop ble tone tx
  131. */
  132. void furi_hal_bt_stop_tone_tx();
  133. /** Start sending ble packets at a given frequency and datarate
  134. *
  135. * @param[in] channel The channel
  136. * @param[in] pattern The pattern
  137. * @param[in] datarate The datarate
  138. */
  139. void furi_hal_bt_start_packet_tx(uint8_t channel, uint8_t pattern, uint8_t datarate);
  140. /** Stop sending ble packets
  141. *
  142. * @return sent packet count
  143. */
  144. uint16_t furi_hal_bt_stop_packet_test();
  145. /** Start receiving packets
  146. *
  147. * @param[in] channel RX channel
  148. * @param[in] datarate Datarate
  149. */
  150. void furi_hal_bt_start_packet_rx(uint8_t channel, uint8_t datarate);
  151. /** Set up the RF to listen to a given RF channel
  152. *
  153. * @param[in] channel RX channel
  154. */
  155. void furi_hal_bt_start_rx(uint8_t channel);
  156. /** Stop RF listenning
  157. */
  158. void furi_hal_bt_stop_rx();
  159. /** Get RSSI
  160. *
  161. * @return RSSI in dBm
  162. */
  163. float furi_hal_bt_get_rssi();
  164. /** Get number of transmitted packets
  165. *
  166. * @return packet count
  167. */
  168. uint32_t furi_hal_bt_get_transmitted_packets();
  169. /** Start MAC addresses scan
  170. * @note Works only with HciLayer 2nd core firmware
  171. *
  172. * @param callback GapScanCallback instance
  173. * @param context pointer to context
  174. */
  175. bool furi_hal_bt_start_scan(GapScanCallback callback, void* context);
  176. /** Stop MAC addresses scan */
  177. void furi_hal_bt_stop_scan();
  178. /** Check & switch C2 to given mode
  179. *
  180. * @param[in] mode mode to switch into
  181. */
  182. bool furi_hal_bt_ensure_c2_mode(BleGlueC2Mode mode);
  183. #ifdef __cplusplus
  184. }
  185. #endif