furi_hal_bt.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /**
  2. * @file furi_hal_bt.h
  3. * BT/BLE HAL API
  4. */
  5. #pragma once
  6. #include <furi.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 (12)
  15. #define FURI_HAL_BT_C2_START_TIMEOUT 1000
  16. #ifdef __cplusplus
  17. extern "C" {
  18. #endif
  19. typedef enum {
  20. FuriHalBtStackUnknown,
  21. FuriHalBtStackLight,
  22. FuriHalBtStackFull,
  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. /** Check if radio stack supports BLE GAT/GAP
  48. *
  49. * @return true if supported
  50. */
  51. bool furi_hal_bt_is_ble_gatt_gap_supported();
  52. /** Check if radio stack supports testing
  53. *
  54. * @return true if supported
  55. */
  56. bool furi_hal_bt_is_testing_supported();
  57. /** Start BLE app
  58. *
  59. * @param profile FuriHalBtProfile instance
  60. * @param event_cb GapEventCallback instance
  61. * @param context pointer to context
  62. *
  63. * @return true on success
  64. */
  65. bool furi_hal_bt_start_app(FuriHalBtProfile profile, GapEventCallback event_cb, void* context);
  66. /** Reinitialize core2
  67. *
  68. * Also can be used to prepare core2 for stop modes
  69. */
  70. void furi_hal_bt_reinit();
  71. /** Change BLE app
  72. * Restarts 2nd core
  73. *
  74. * @param profile FuriHalBtProfile instance
  75. * @param event_cb GapEventCallback instance
  76. * @param context pointer to context
  77. *
  78. * @return true on success
  79. */
  80. bool furi_hal_bt_change_app(FuriHalBtProfile profile, GapEventCallback event_cb, void* context);
  81. /** Update battery level
  82. *
  83. * @param battery_level battery level
  84. */
  85. void furi_hal_bt_update_battery_level(uint8_t battery_level);
  86. /** Update battery power state */
  87. void furi_hal_bt_update_power_state();
  88. /** Checks if BLE state is active
  89. *
  90. * @return true if device is connected or advertising, false otherwise
  91. */
  92. bool furi_hal_bt_is_active();
  93. /** Start advertising
  94. */
  95. void furi_hal_bt_start_advertising();
  96. /** Stop advertising
  97. */
  98. void furi_hal_bt_stop_advertising();
  99. /** Get BT/BLE system component state
  100. *
  101. * @param[in] buffer FuriString* buffer to write to
  102. */
  103. void furi_hal_bt_dump_state(FuriString* buffer);
  104. /** Get BT/BLE system component state
  105. *
  106. * @return true if core2 is alive
  107. */
  108. bool furi_hal_bt_is_alive();
  109. /** Get key storage buffer address and size
  110. *
  111. * @param key_buff_addr pointer to store buffer address
  112. * @param key_buff_size pointer to store buffer size
  113. */
  114. void furi_hal_bt_get_key_storage_buff(uint8_t** key_buff_addr, uint16_t* key_buff_size);
  115. /** Get SRAM2 hardware semaphore
  116. * @note Must be called before SRAM2 read/write operations
  117. */
  118. void furi_hal_bt_nvm_sram_sem_acquire();
  119. /** Release SRAM2 hardware semaphore
  120. * @note Must be called after SRAM2 read/write operations
  121. */
  122. void furi_hal_bt_nvm_sram_sem_release();
  123. /** Clear key storage
  124. *
  125. * @return true on success
  126. */
  127. bool furi_hal_bt_clear_white_list();
  128. /** Set key storage change callback
  129. *
  130. * @param callback BleGlueKeyStorageChangedCallback instance
  131. * @param context pointer to context
  132. */
  133. void furi_hal_bt_set_key_storage_change_callback(
  134. BleGlueKeyStorageChangedCallback callback,
  135. void* context);
  136. /** Start ble tone tx at given channel and power
  137. *
  138. * @param[in] channel The channel
  139. * @param[in] power The power
  140. */
  141. void furi_hal_bt_start_tone_tx(uint8_t channel, uint8_t power);
  142. /** Stop ble tone tx
  143. */
  144. void furi_hal_bt_stop_tone_tx();
  145. /** Start sending ble packets at a given frequency and datarate
  146. *
  147. * @param[in] channel The channel
  148. * @param[in] pattern The pattern
  149. * @param[in] datarate The datarate
  150. */
  151. void furi_hal_bt_start_packet_tx(uint8_t channel, uint8_t pattern, uint8_t datarate);
  152. /** Stop sending ble packets
  153. *
  154. * @return sent packet count
  155. */
  156. uint16_t furi_hal_bt_stop_packet_test();
  157. /** Start receiving packets
  158. *
  159. * @param[in] channel RX channel
  160. * @param[in] datarate Datarate
  161. */
  162. void furi_hal_bt_start_packet_rx(uint8_t channel, uint8_t datarate);
  163. /** Set up the RF to listen to a given RF channel
  164. *
  165. * @param[in] channel RX channel
  166. */
  167. void furi_hal_bt_start_rx(uint8_t channel);
  168. /** Stop RF listenning
  169. */
  170. void furi_hal_bt_stop_rx();
  171. /** Get RSSI
  172. *
  173. * @return RSSI in dBm
  174. */
  175. float furi_hal_bt_get_rssi();
  176. /** Get number of transmitted packets
  177. *
  178. * @return packet count
  179. */
  180. uint32_t furi_hal_bt_get_transmitted_packets();
  181. /** Check & switch C2 to given mode
  182. *
  183. * @param[in] mode mode to switch into
  184. */
  185. bool furi_hal_bt_ensure_c2_mode(BleGlueC2Mode mode);
  186. #ifdef __cplusplus
  187. }
  188. #endif