furi_hal_bt.h 4.8 KB

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