furi_hal_bt.c 12 KB

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