furi_hal_bt.c 12 KB

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