furi-hal-bt.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. #include <furi-hal-bt.h>
  2. #include <ble.h>
  3. #include <stm32wbxx.h>
  4. #include <shci.h>
  5. #include <cmsis_os2.h>
  6. #include <furi-hal-version.h>
  7. #include <furi-hal-bt-hid.h>
  8. #include <furi-hal-bt-serial.h>
  9. #include "battery_service.h"
  10. #include <furi.h>
  11. #define TAG "FuriHalBt"
  12. #define FURI_HAL_BT_DEFAULT_MAC_ADDR {0x6c, 0x7a, 0xd8, 0xac, 0x57, 0x72}
  13. osMutexId_t furi_hal_bt_core2_mtx = NULL;
  14. typedef void (*FuriHalBtProfileStart)(void);
  15. typedef void (*FuriHalBtProfileStop)(void);
  16. typedef struct {
  17. FuriHalBtProfileStart start;
  18. FuriHalBtProfileStart stop;
  19. GapConfig config;
  20. uint16_t appearance_char;
  21. uint16_t advertise_service_uuid;
  22. } FuriHalBtProfileConfig;
  23. FuriHalBtProfileConfig profile_config[FuriHalBtProfileNumber] = {
  24. [FuriHalBtProfileSerial] = {
  25. .start = furi_hal_bt_serial_start,
  26. .stop = furi_hal_bt_serial_stop,
  27. .config = {
  28. .adv_service_uuid = 0x3080,
  29. .appearance_char = 0x8600,
  30. .bonding_mode = true,
  31. .pairing_method = GapPairingPinCodeShow,
  32. .mac_address = FURI_HAL_BT_DEFAULT_MAC_ADDR,
  33. },
  34. },
  35. [FuriHalBtProfileHidKeyboard] = {
  36. .start = furi_hal_bt_hid_start,
  37. .stop = furi_hal_bt_hid_stop,
  38. .config = {
  39. .adv_service_uuid = HUMAN_INTERFACE_DEVICE_SERVICE_UUID,
  40. .appearance_char = GAP_APPEARANCE_KEYBOARD,
  41. .bonding_mode = true,
  42. .pairing_method = GapPairingPinCodeVerifyYesNo,
  43. .mac_address = FURI_HAL_BT_DEFAULT_MAC_ADDR,
  44. },
  45. }
  46. };
  47. FuriHalBtProfileConfig* current_profile = NULL;
  48. void furi_hal_bt_init() {
  49. if(!furi_hal_bt_core2_mtx) {
  50. furi_hal_bt_core2_mtx = osMutexNew(NULL);
  51. furi_assert(furi_hal_bt_core2_mtx);
  52. }
  53. // Explicitly tell that we are in charge of CLK48 domain
  54. if(!HAL_HSEM_IsSemTaken(CFG_HW_CLK48_CONFIG_SEMID)) {
  55. HAL_HSEM_FastTake(CFG_HW_CLK48_CONFIG_SEMID);
  56. }
  57. // Start Core2
  58. ble_glue_init();
  59. }
  60. void furi_hal_bt_lock_core2() {
  61. furi_assert(furi_hal_bt_core2_mtx);
  62. furi_check(osMutexAcquire(furi_hal_bt_core2_mtx, osWaitForever) == osOK);
  63. }
  64. void furi_hal_bt_unlock_core2() {
  65. furi_assert(furi_hal_bt_core2_mtx);
  66. furi_check(osMutexRelease(furi_hal_bt_core2_mtx) == osOK);
  67. }
  68. static bool furi_hal_bt_start_core2() {
  69. furi_assert(furi_hal_bt_core2_mtx);
  70. osMutexAcquire(furi_hal_bt_core2_mtx, osWaitForever);
  71. // Explicitly tell that we are in charge of CLK48 domain
  72. if(!HAL_HSEM_IsSemTaken(CFG_HW_CLK48_CONFIG_SEMID)) {
  73. HAL_HSEM_FastTake(CFG_HW_CLK48_CONFIG_SEMID);
  74. }
  75. // Start Core2
  76. bool ret = ble_glue_start();
  77. osMutexRelease(furi_hal_bt_core2_mtx);
  78. return ret;
  79. }
  80. bool furi_hal_bt_start_app(FuriHalBtProfile profile, BleEventCallback event_cb, void* context) {
  81. furi_assert(event_cb);
  82. furi_assert(profile < FuriHalBtProfileNumber);
  83. bool ret = true;
  84. do {
  85. // Start 2nd core
  86. ret = furi_hal_bt_start_core2();
  87. if(!ret) {
  88. ble_app_thread_stop();
  89. FURI_LOG_E(TAG, "Failed to start 2nd core");
  90. break;
  91. }
  92. // Set mac address
  93. memcpy(
  94. profile_config[profile].config.mac_address,
  95. furi_hal_version_get_ble_mac(),
  96. sizeof(profile_config[profile].config.mac_address)
  97. );
  98. // Set advertise name
  99. strlcpy(
  100. profile_config[profile].config.adv_name,
  101. furi_hal_version_get_ble_local_device_name_ptr(),
  102. FURI_HAL_VERSION_DEVICE_NAME_LENGTH
  103. );
  104. // Configure GAP
  105. GapConfig* config = &profile_config[profile].config;
  106. if(profile == FuriHalBtProfileSerial) {
  107. config->adv_service_uuid |= furi_hal_version_get_hw_color();
  108. } else if(profile == FuriHalBtProfileHidKeyboard) {
  109. // Change MAC address for HID profile
  110. config->mac_address[2]++;
  111. // Change name Flipper -> Keynote
  112. const char* clicker_str = "Keynote";
  113. memcpy(&config->adv_name[1], clicker_str, strlen(clicker_str));
  114. }
  115. ret = gap_init(config, event_cb, context);
  116. if(!ret) {
  117. gap_thread_stop();
  118. FURI_LOG_E(TAG, "Failed to init GAP");
  119. break;
  120. }
  121. // Start selected profile services
  122. profile_config[profile].start();
  123. } while(false);
  124. current_profile = &profile_config[profile];
  125. return ret;
  126. }
  127. bool furi_hal_bt_change_app(FuriHalBtProfile profile, BleEventCallback event_cb, void* context) {
  128. furi_assert(event_cb);
  129. furi_assert(profile < FuriHalBtProfileNumber);
  130. bool ret = true;
  131. FURI_LOG_I(TAG, "Stop current profile services");
  132. current_profile->stop();
  133. FURI_LOG_I(TAG, "Disconnect and stop advertising");
  134. furi_hal_bt_stop_advertising();
  135. FURI_LOG_I(TAG, "Shutdow 2nd core");
  136. LL_C2_PWR_SetPowerMode(LL_PWR_MODE_SHUTDOWN);
  137. FURI_LOG_I(TAG, "Stop BLE related RTOS threads");
  138. ble_app_thread_stop();
  139. gap_thread_stop();
  140. FURI_LOG_I(TAG, "Reset SHCI");
  141. SHCI_C2_Reinit();
  142. osDelay(100);
  143. ble_glue_thread_stop();
  144. FURI_LOG_I(TAG, "Start BT initialization");
  145. furi_hal_bt_init();
  146. ret = furi_hal_bt_start_app(profile, event_cb, context);
  147. if(ret) {
  148. current_profile = &profile_config[profile];
  149. }
  150. return ret;
  151. }
  152. void furi_hal_bt_start_advertising() {
  153. if(gap_get_state() == GapStateIdle) {
  154. gap_start_advertising();
  155. }
  156. }
  157. void furi_hal_bt_stop_advertising() {
  158. if(furi_hal_bt_is_active()) {
  159. gap_stop_advertising();
  160. while(furi_hal_bt_is_active()) {
  161. osDelay(1);
  162. }
  163. }
  164. }
  165. void furi_hal_bt_update_battery_level(uint8_t battery_level) {
  166. if(battery_svc_is_started()) {
  167. battery_svc_update_level(battery_level);
  168. }
  169. }
  170. void furi_hal_bt_get_key_storage_buff(uint8_t** key_buff_addr, uint16_t* key_buff_size) {
  171. ble_app_get_key_storage_buff(key_buff_addr, key_buff_size);
  172. }
  173. void furi_hal_bt_set_key_storage_change_callback(BleGlueKeyStorageChangedCallback callback, void* context) {
  174. furi_assert(callback);
  175. ble_glue_set_key_storage_changed_callback(callback, context);
  176. }
  177. void furi_hal_bt_nvm_sram_sem_acquire() {
  178. while(HAL_HSEM_FastTake(CFG_HW_BLE_NVM_SRAM_SEMID) != HAL_OK) {
  179. osDelay(1);
  180. }
  181. }
  182. void furi_hal_bt_nvm_sram_sem_release() {
  183. HAL_HSEM_Release(CFG_HW_BLE_NVM_SRAM_SEMID, 0);
  184. }
  185. void furi_hal_bt_dump_state(string_t buffer) {
  186. if (furi_hal_bt_is_alive()) {
  187. uint8_t HCI_Version;
  188. uint16_t HCI_Revision;
  189. uint8_t LMP_PAL_Version;
  190. uint16_t Manufacturer_Name;
  191. uint16_t LMP_PAL_Subversion;
  192. tBleStatus ret = hci_read_local_version_information(
  193. &HCI_Version, &HCI_Revision, &LMP_PAL_Version, &Manufacturer_Name, &LMP_PAL_Subversion
  194. );
  195. string_cat_printf(buffer,
  196. "Ret: %d, HCI_Version: %d, HCI_Revision: %d, LMP_PAL_Version: %d, Manufacturer_Name: %d, LMP_PAL_Subversion: %d",
  197. ret, HCI_Version, HCI_Revision, LMP_PAL_Version, Manufacturer_Name, LMP_PAL_Subversion
  198. );
  199. } else {
  200. string_cat_printf(buffer, "BLE not ready");
  201. }
  202. }
  203. bool furi_hal_bt_is_alive() {
  204. return ble_glue_is_alive();
  205. }
  206. bool furi_hal_bt_is_active() {
  207. return gap_get_state() > GapStateIdle;
  208. }
  209. void furi_hal_bt_start_tone_tx(uint8_t channel, uint8_t power) {
  210. aci_hal_set_tx_power_level(0, power);
  211. aci_hal_tone_start(channel, 0);
  212. }
  213. void furi_hal_bt_stop_tone_tx() {
  214. aci_hal_tone_stop();
  215. }
  216. void furi_hal_bt_start_packet_tx(uint8_t channel, uint8_t pattern, uint8_t datarate) {
  217. hci_le_enhanced_transmitter_test(channel, 0x25, pattern, datarate);
  218. }
  219. void furi_hal_bt_start_packet_rx(uint8_t channel, uint8_t datarate) {
  220. hci_le_enhanced_receiver_test(channel, datarate, 0);
  221. }
  222. uint16_t furi_hal_bt_stop_packet_test() {
  223. uint16_t num_of_packets = 0;
  224. hci_le_test_end(&num_of_packets);
  225. return num_of_packets;
  226. }
  227. void furi_hal_bt_start_rx(uint8_t channel) {
  228. aci_hal_rx_start(channel);
  229. }
  230. float furi_hal_bt_get_rssi() {
  231. float val;
  232. uint8_t rssi_raw[3];
  233. if (aci_hal_read_raw_rssi(rssi_raw) != BLE_STATUS_SUCCESS) {
  234. return 0.0f;
  235. }
  236. // Some ST magic with rssi
  237. uint8_t agc = rssi_raw[2] & 0xFF;
  238. int rssi = (((int)rssi_raw[1] << 8) & 0xFF00) + (rssi_raw[0] & 0xFF);
  239. if(rssi == 0 || agc > 11) {
  240. val = -127.0;
  241. } else {
  242. val = agc * 6.0f - 127.0f;
  243. while(rssi > 30) {
  244. val += 6.0;
  245. rssi >>=1;
  246. }
  247. val += (417 * rssi + 18080) >> 10;
  248. }
  249. return val;
  250. }
  251. uint32_t furi_hal_bt_get_transmitted_packets() {
  252. uint32_t packets = 0;
  253. aci_hal_le_tx_test_packet_number(&packets);
  254. return packets;
  255. }
  256. void furi_hal_bt_stop_rx() {
  257. aci_hal_rx_stop();
  258. }