furi_hal_bt.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. #include <furi_hal_bt.h>
  2. #include <ble/ble.h>
  3. #include <interface/patterns/ble_thread/shci/shci.h>
  4. #include <stm32wbxx.h>
  5. #include <furi_hal_version.h>
  6. #include <furi_hal_bt_hid.h>
  7. #include <furi_hal_bt_serial.h>
  8. #include "battery_service.h"
  9. #include <furi.h>
  10. #define TAG "FuriHalBt"
  11. #define FURI_HAL_BT_DEFAULT_MAC_ADDR \
  12. { 0x6c, 0x7a, 0xd8, 0xac, 0x57, 0x72 }
  13. /* Time, in ms, to wait for mode transition before crashing */
  14. #define C2_MODE_SWITCH_TIMEOUT 10000
  15. FuriMutex* furi_hal_bt_core2_mtx = NULL;
  16. static FuriHalBtStack furi_hal_bt_stack = FuriHalBtStackUnknown;
  17. typedef void (*FuriHalBtProfileStart)(void);
  18. typedef void (*FuriHalBtProfileStop)(void);
  19. typedef struct {
  20. FuriHalBtProfileStart start;
  21. FuriHalBtProfileStart stop;
  22. GapConfig config;
  23. uint16_t appearance_char;
  24. uint16_t advertise_service_uuid;
  25. } FuriHalBtProfileConfig;
  26. FuriHalBtProfileConfig profile_config[FuriHalBtProfileNumber] = {
  27. [FuriHalBtProfileSerial] =
  28. {
  29. .start = furi_hal_bt_serial_start,
  30. .stop = furi_hal_bt_serial_stop,
  31. .config =
  32. {
  33. .adv_service_uuid = 0x3080,
  34. .appearance_char = 0x8600,
  35. .bonding_mode = true,
  36. .pairing_method = GapPairingPinCodeShow,
  37. .mac_address = FURI_HAL_BT_DEFAULT_MAC_ADDR,
  38. .conn_param =
  39. {
  40. .conn_int_min = 0x18, // 30 ms
  41. .conn_int_max = 0x24, // 45 ms
  42. .slave_latency = 0,
  43. .supervisor_timeout = 0,
  44. },
  45. },
  46. },
  47. [FuriHalBtProfileHidKeyboard] =
  48. {
  49. .start = furi_hal_bt_hid_start,
  50. .stop = furi_hal_bt_hid_stop,
  51. .config =
  52. {
  53. .adv_service_uuid = HUMAN_INTERFACE_DEVICE_SERVICE_UUID,
  54. .appearance_char = GAP_APPEARANCE_KEYBOARD,
  55. .bonding_mode = true,
  56. .pairing_method = GapPairingPinCodeVerifyYesNo,
  57. .mac_address = FURI_HAL_BT_DEFAULT_MAC_ADDR,
  58. .conn_param =
  59. {
  60. .conn_int_min = 0x18, // 30 ms
  61. .conn_int_max = 0x24, // 45 ms
  62. .slave_latency = 0,
  63. .supervisor_timeout = 0,
  64. },
  65. },
  66. },
  67. };
  68. FuriHalBtProfileConfig* current_profile = NULL;
  69. void furi_hal_bt_init() {
  70. if(!furi_hal_bt_core2_mtx) {
  71. furi_hal_bt_core2_mtx = furi_mutex_alloc(FuriMutexTypeNormal);
  72. furi_assert(furi_hal_bt_core2_mtx);
  73. }
  74. // Explicitly tell that we are in charge of CLK48 domain
  75. if(!LL_HSEM_IsSemaphoreLocked(HSEM, CFG_HW_CLK48_CONFIG_SEMID)) {
  76. furi_check(LL_HSEM_1StepLock(HSEM, CFG_HW_CLK48_CONFIG_SEMID) == 0);
  77. }
  78. // Start Core2
  79. ble_glue_init();
  80. }
  81. void furi_hal_bt_lock_core2() {
  82. furi_assert(furi_hal_bt_core2_mtx);
  83. furi_check(furi_mutex_acquire(furi_hal_bt_core2_mtx, FuriWaitForever) == FuriStatusOk);
  84. }
  85. void furi_hal_bt_unlock_core2() {
  86. furi_assert(furi_hal_bt_core2_mtx);
  87. furi_check(furi_mutex_release(furi_hal_bt_core2_mtx) == FuriStatusOk);
  88. }
  89. static bool furi_hal_bt_radio_stack_is_supported(const BleGlueC2Info* info) {
  90. bool supported = false;
  91. if(info->StackType == INFO_STACK_TYPE_BLE_LIGHT) {
  92. if(info->VersionMajor >= FURI_HAL_BT_STACK_VERSION_MAJOR &&
  93. info->VersionMinor >= FURI_HAL_BT_STACK_VERSION_MINOR) {
  94. furi_hal_bt_stack = FuriHalBtStackLight;
  95. supported = true;
  96. }
  97. } else if(info->StackType == INFO_STACK_TYPE_BLE_FULL) {
  98. if(info->VersionMajor >= FURI_HAL_BT_STACK_VERSION_MAJOR &&
  99. info->VersionMinor >= FURI_HAL_BT_STACK_VERSION_MINOR) {
  100. furi_hal_bt_stack = FuriHalBtStackFull;
  101. supported = true;
  102. }
  103. } else {
  104. furi_hal_bt_stack = FuriHalBtStackUnknown;
  105. }
  106. return supported;
  107. }
  108. bool furi_hal_bt_start_radio_stack() {
  109. bool res = false;
  110. furi_assert(furi_hal_bt_core2_mtx);
  111. furi_mutex_acquire(furi_hal_bt_core2_mtx, FuriWaitForever);
  112. // Explicitly tell that we are in charge of CLK48 domain
  113. if(!LL_HSEM_IsSemaphoreLocked(HSEM, CFG_HW_CLK48_CONFIG_SEMID)) {
  114. furi_check(LL_HSEM_1StepLock(HSEM, CFG_HW_CLK48_CONFIG_SEMID) == 0);
  115. }
  116. do {
  117. // Wait until C2 is started or timeout
  118. if(!ble_glue_wait_for_c2_start(FURI_HAL_BT_C2_START_TIMEOUT)) {
  119. FURI_LOG_E(TAG, "Core2 start failed");
  120. ble_glue_thread_stop();
  121. break;
  122. }
  123. // If C2 is running, start radio stack fw
  124. if(!furi_hal_bt_ensure_c2_mode(BleGlueC2ModeStack)) {
  125. break;
  126. }
  127. // Check whether we support radio stack
  128. const BleGlueC2Info* c2_info = ble_glue_get_c2_info();
  129. if(!furi_hal_bt_radio_stack_is_supported(c2_info)) {
  130. FURI_LOG_E(TAG, "Unsupported radio stack");
  131. // Don't stop SHCI for crypto enclave support
  132. break;
  133. }
  134. // Starting radio stack
  135. if(!ble_glue_start()) {
  136. FURI_LOG_E(TAG, "Failed to start radio stack");
  137. ble_glue_thread_stop();
  138. ble_app_thread_stop();
  139. break;
  140. }
  141. res = true;
  142. } while(false);
  143. furi_mutex_release(furi_hal_bt_core2_mtx);
  144. return res;
  145. }
  146. FuriHalBtStack furi_hal_bt_get_radio_stack() {
  147. return furi_hal_bt_stack;
  148. }
  149. bool furi_hal_bt_is_ble_gatt_gap_supported() {
  150. if(furi_hal_bt_stack == FuriHalBtStackLight || furi_hal_bt_stack == FuriHalBtStackFull) {
  151. return true;
  152. } else {
  153. return false;
  154. }
  155. }
  156. bool furi_hal_bt_is_testing_supported() {
  157. if(furi_hal_bt_stack == FuriHalBtStackFull) {
  158. return true;
  159. } else {
  160. return false;
  161. }
  162. }
  163. bool furi_hal_bt_start_app(FuriHalBtProfile profile, GapEventCallback event_cb, void* context) {
  164. furi_assert(event_cb);
  165. furi_assert(profile < FuriHalBtProfileNumber);
  166. bool ret = false;
  167. do {
  168. if(!ble_glue_is_radio_stack_ready()) {
  169. FURI_LOG_E(TAG, "Can't start BLE App - radio stack did not start");
  170. break;
  171. }
  172. if(!furi_hal_bt_is_ble_gatt_gap_supported()) {
  173. FURI_LOG_E(TAG, "Can't start Ble App - unsupported radio stack");
  174. break;
  175. }
  176. // Set mac address
  177. memcpy(
  178. profile_config[profile].config.mac_address,
  179. furi_hal_version_get_ble_mac(),
  180. sizeof(profile_config[profile].config.mac_address));
  181. // Set advertise name
  182. strlcpy(
  183. profile_config[profile].config.adv_name,
  184. furi_hal_version_get_ble_local_device_name_ptr(),
  185. FURI_HAL_VERSION_DEVICE_NAME_LENGTH);
  186. // Configure GAP
  187. GapConfig* config = &profile_config[profile].config;
  188. if(profile == FuriHalBtProfileSerial) {
  189. config->adv_service_uuid |= furi_hal_version_get_hw_color();
  190. } else if(profile == FuriHalBtProfileHidKeyboard) {
  191. // Change MAC address for HID profile
  192. config->mac_address[2]++;
  193. // Change name Flipper -> Control
  194. const char* clicker_str = "Control";
  195. memcpy(&config->adv_name[1], clicker_str, strlen(clicker_str));
  196. }
  197. if(!gap_init(config, event_cb, context)) {
  198. gap_thread_stop();
  199. FURI_LOG_E(TAG, "Failed to init GAP");
  200. break;
  201. }
  202. // Start selected profile services
  203. if(furi_hal_bt_is_ble_gatt_gap_supported()) {
  204. profile_config[profile].start();
  205. }
  206. ret = true;
  207. } while(false);
  208. current_profile = &profile_config[profile];
  209. return ret;
  210. }
  211. void furi_hal_bt_reinit() {
  212. FURI_LOG_I(TAG, "Disconnect and stop advertising");
  213. furi_hal_bt_stop_advertising();
  214. FURI_LOG_I(TAG, "Stop current profile services");
  215. current_profile->stop();
  216. // Magic happens here
  217. hci_reset();
  218. FURI_LOG_I(TAG, "Stop BLE related RTOS threads");
  219. ble_app_thread_stop();
  220. gap_thread_stop();
  221. FURI_LOG_I(TAG, "Reset SHCI");
  222. furi_check(ble_glue_reinit_c2());
  223. furi_delay_ms(100);
  224. ble_glue_thread_stop();
  225. FURI_LOG_I(TAG, "Start BT initialization");
  226. furi_hal_bt_init();
  227. furi_hal_bt_start_radio_stack();
  228. }
  229. bool furi_hal_bt_change_app(FuriHalBtProfile profile, GapEventCallback event_cb, void* context) {
  230. furi_assert(event_cb);
  231. furi_assert(profile < FuriHalBtProfileNumber);
  232. bool ret = true;
  233. furi_hal_bt_reinit();
  234. ret = furi_hal_bt_start_app(profile, event_cb, context);
  235. if(ret) {
  236. current_profile = &profile_config[profile];
  237. }
  238. return ret;
  239. }
  240. bool furi_hal_bt_is_active() {
  241. return gap_get_state() > GapStateIdle;
  242. }
  243. void furi_hal_bt_start_advertising() {
  244. if(gap_get_state() == GapStateIdle) {
  245. gap_start_advertising();
  246. }
  247. }
  248. void furi_hal_bt_stop_advertising() {
  249. if(furi_hal_bt_is_active()) {
  250. gap_stop_advertising();
  251. while(furi_hal_bt_is_active()) {
  252. furi_delay_tick(1);
  253. }
  254. }
  255. }
  256. void furi_hal_bt_update_battery_level(uint8_t battery_level) {
  257. if(battery_svc_is_started()) {
  258. battery_svc_update_level(battery_level);
  259. }
  260. }
  261. void furi_hal_bt_update_power_state() {
  262. if(battery_svc_is_started()) {
  263. battery_svc_update_power_state();
  264. }
  265. }
  266. void furi_hal_bt_get_key_storage_buff(uint8_t** key_buff_addr, uint16_t* key_buff_size) {
  267. ble_app_get_key_storage_buff(key_buff_addr, key_buff_size);
  268. }
  269. void furi_hal_bt_set_key_storage_change_callback(
  270. BleGlueKeyStorageChangedCallback callback,
  271. void* context) {
  272. furi_assert(callback);
  273. ble_glue_set_key_storage_changed_callback(callback, context);
  274. }
  275. void furi_hal_bt_nvm_sram_sem_acquire() {
  276. while(LL_HSEM_1StepLock(HSEM, CFG_HW_BLE_NVM_SRAM_SEMID)) {
  277. furi_thread_yield();
  278. }
  279. }
  280. void furi_hal_bt_nvm_sram_sem_release() {
  281. LL_HSEM_ReleaseLock(HSEM, CFG_HW_BLE_NVM_SRAM_SEMID, 0);
  282. }
  283. bool furi_hal_bt_clear_white_list() {
  284. furi_hal_bt_nvm_sram_sem_acquire();
  285. tBleStatus status = aci_gap_clear_security_db();
  286. if(status) {
  287. FURI_LOG_E(TAG, "Clear while list failed with status %d", status);
  288. }
  289. furi_hal_bt_nvm_sram_sem_release();
  290. return status != BLE_STATUS_SUCCESS;
  291. }
  292. void furi_hal_bt_dump_state(FuriString* buffer) {
  293. if(furi_hal_bt_is_alive()) {
  294. uint8_t HCI_Version;
  295. uint16_t HCI_Revision;
  296. uint8_t LMP_PAL_Version;
  297. uint16_t Manufacturer_Name;
  298. uint16_t LMP_PAL_Subversion;
  299. tBleStatus ret = hci_read_local_version_information(
  300. &HCI_Version, &HCI_Revision, &LMP_PAL_Version, &Manufacturer_Name, &LMP_PAL_Subversion);
  301. furi_string_cat_printf(
  302. buffer,
  303. "Ret: %d, HCI_Version: %d, HCI_Revision: %d, LMP_PAL_Version: %d, Manufacturer_Name: %d, LMP_PAL_Subversion: %d",
  304. ret,
  305. HCI_Version,
  306. HCI_Revision,
  307. LMP_PAL_Version,
  308. Manufacturer_Name,
  309. LMP_PAL_Subversion);
  310. } else {
  311. furi_string_cat_printf(buffer, "BLE not ready");
  312. }
  313. }
  314. bool furi_hal_bt_is_alive() {
  315. return ble_glue_is_alive();
  316. }
  317. void furi_hal_bt_start_tone_tx(uint8_t channel, uint8_t power) {
  318. aci_hal_set_tx_power_level(0, power);
  319. aci_hal_tone_start(channel, 0);
  320. }
  321. void furi_hal_bt_stop_tone_tx() {
  322. aci_hal_tone_stop();
  323. }
  324. void furi_hal_bt_start_packet_tx(uint8_t channel, uint8_t pattern, uint8_t datarate) {
  325. hci_le_enhanced_transmitter_test(channel, 0x25, pattern, datarate);
  326. }
  327. void furi_hal_bt_start_packet_rx(uint8_t channel, uint8_t datarate) {
  328. hci_le_enhanced_receiver_test(channel, datarate, 0);
  329. }
  330. uint16_t furi_hal_bt_stop_packet_test() {
  331. uint16_t num_of_packets = 0;
  332. hci_le_test_end(&num_of_packets);
  333. return num_of_packets;
  334. }
  335. void furi_hal_bt_start_rx(uint8_t channel) {
  336. aci_hal_rx_start(channel);
  337. }
  338. float furi_hal_bt_get_rssi() {
  339. float val;
  340. uint8_t rssi_raw[3];
  341. if(aci_hal_read_raw_rssi(rssi_raw) != BLE_STATUS_SUCCESS) {
  342. return 0.0f;
  343. }
  344. // Some ST magic with rssi
  345. uint8_t agc = rssi_raw[2] & 0xFF;
  346. int rssi = (((int)rssi_raw[1] << 8) & 0xFF00) + (rssi_raw[0] & 0xFF);
  347. if(rssi == 0 || agc > 11) {
  348. val = -127.0;
  349. } else {
  350. val = agc * 6.0f - 127.0f;
  351. while(rssi > 30) {
  352. val += 6.0;
  353. rssi >>= 1;
  354. }
  355. val += (float)((417 * rssi + 18080) >> 10);
  356. }
  357. return val;
  358. }
  359. uint32_t furi_hal_bt_get_transmitted_packets() {
  360. uint32_t packets = 0;
  361. aci_hal_le_tx_test_packet_number(&packets);
  362. return packets;
  363. }
  364. void furi_hal_bt_stop_rx() {
  365. aci_hal_rx_stop();
  366. }
  367. bool furi_hal_bt_ensure_c2_mode(BleGlueC2Mode mode) {
  368. BleGlueCommandResult fw_start_res = ble_glue_force_c2_mode(mode);
  369. if(fw_start_res == BleGlueCommandResultOK) {
  370. return true;
  371. } else if(fw_start_res == BleGlueCommandResultRestartPending) {
  372. // Do nothing and wait for system reset
  373. furi_delay_ms(C2_MODE_SWITCH_TIMEOUT);
  374. furi_crash("Waiting for FUS->radio stack transition");
  375. return true;
  376. }
  377. FURI_LOG_E(TAG, "Failed to switch C2 mode: %d", fw_start_res);
  378. return false;
  379. }