furi_hal_bt.c 13 KB

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