furi_hal_bt.c 11 KB

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