2021-10-03 10:36:05 +00:00
|
|
|
/**
|
2022-01-05 16:10:18 +00:00
|
|
|
* @file furi_hal_bt.h
|
2021-10-03 10:36:05 +00:00
|
|
|
* BT/BLE HAL API
|
|
|
|
*/
|
|
|
|
|
2020-12-10 14:25:20 +00:00
|
|
|
#pragma once
|
|
|
|
|
2022-10-05 15:15:23 +00:00
|
|
|
#include <furi.h>
|
2020-12-10 14:25:20 +00:00
|
|
|
#include <stdbool.h>
|
2021-10-12 16:41:42 +00:00
|
|
|
#include <gap.h>
|
2023-06-14 00:18:24 +00:00
|
|
|
#include <serial_service.h>
|
2021-11-04 17:26:41 +00:00
|
|
|
#include <ble_glue.h>
|
|
|
|
#include <ble_app.h>
|
|
|
|
|
2023-02-07 16:33:05 +00:00
|
|
|
#include <furi_hal_bt_serial.h>
|
2020-12-10 14:25:20 +00:00
|
|
|
|
2022-01-02 22:36:42 +00:00
|
|
|
#define FURI_HAL_BT_STACK_VERSION_MAJOR (1)
|
2022-06-09 09:07:42 +00:00
|
|
|
#define FURI_HAL_BT_STACK_VERSION_MINOR (12)
|
2022-04-27 15:53:48 +00:00
|
|
|
#define FURI_HAL_BT_C2_START_TIMEOUT 1000
|
2022-01-02 22:36:42 +00:00
|
|
|
|
2020-12-10 14:25:20 +00:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2022-01-02 22:36:42 +00:00
|
|
|
typedef enum {
|
|
|
|
FuriHalBtStackUnknown,
|
|
|
|
FuriHalBtStackLight,
|
2022-06-09 09:07:42 +00:00
|
|
|
FuriHalBtStackFull,
|
2022-01-02 22:36:42 +00:00
|
|
|
} FuriHalBtStack;
|
|
|
|
|
2021-12-08 11:28:01 +00:00
|
|
|
typedef enum {
|
|
|
|
FuriHalBtProfileSerial,
|
|
|
|
FuriHalBtProfileHidKeyboard,
|
|
|
|
|
|
|
|
// Keep last for Profiles number calculation
|
|
|
|
FuriHalBtProfileNumber,
|
|
|
|
} FuriHalBtProfile;
|
|
|
|
|
2021-10-03 10:36:05 +00:00
|
|
|
/** Initialize
|
|
|
|
*/
|
2021-08-08 18:03:25 +00:00
|
|
|
void furi_hal_bt_init();
|
2020-12-10 14:25:20 +00:00
|
|
|
|
2021-11-10 09:53:00 +00:00
|
|
|
/** Lock core2 state transition */
|
|
|
|
void furi_hal_bt_lock_core2();
|
|
|
|
|
|
|
|
/** Lock core2 state transition */
|
|
|
|
void furi_hal_bt_unlock_core2();
|
|
|
|
|
2022-01-02 22:36:42 +00:00
|
|
|
/** Start radio stack
|
|
|
|
*
|
|
|
|
* @return true on successfull radio stack start
|
|
|
|
*/
|
|
|
|
bool furi_hal_bt_start_radio_stack();
|
|
|
|
|
|
|
|
/** Get radio stack type
|
|
|
|
*
|
|
|
|
* @return FuriHalBtStack instance
|
|
|
|
*/
|
|
|
|
FuriHalBtStack furi_hal_bt_get_radio_stack();
|
|
|
|
|
2022-06-09 09:07:42 +00:00
|
|
|
/** Check if radio stack supports BLE GAT/GAP
|
|
|
|
*
|
|
|
|
* @return true if supported
|
|
|
|
*/
|
|
|
|
bool furi_hal_bt_is_ble_gatt_gap_supported();
|
|
|
|
|
|
|
|
/** Check if radio stack supports testing
|
|
|
|
*
|
|
|
|
* @return true if supported
|
|
|
|
*/
|
|
|
|
bool furi_hal_bt_is_testing_supported();
|
|
|
|
|
2021-12-08 11:28:01 +00:00
|
|
|
/** Start BLE app
|
2021-11-04 17:26:41 +00:00
|
|
|
*
|
2021-12-08 11:28:01 +00:00
|
|
|
* @param profile FuriHalBtProfile instance
|
2022-01-02 22:36:42 +00:00
|
|
|
* @param event_cb GapEventCallback instance
|
2021-12-08 11:28:01 +00:00
|
|
|
* @param context pointer to context
|
|
|
|
*
|
|
|
|
* @return true on success
|
|
|
|
*/
|
2022-01-02 22:36:42 +00:00
|
|
|
bool furi_hal_bt_start_app(FuriHalBtProfile profile, GapEventCallback event_cb, void* context);
|
2021-11-04 17:26:41 +00:00
|
|
|
|
[FL-2399, FL-2261] Tickless sleep shenanigans (#1168)
* Disable USART in sleep
* Restore UART state on suspend/resume
* FuriHal: Enable stop mode and add insomnia to I2C and SPI
* Remove IDLE interrupt
* FuriHal: add FPU isr and disable all FPU interrupt, add core2 stop mode configuration on deep sleep
* FuriHal: tie stop mode debug with debug rtc flag
* FuriHal: adjust flash latency on clock switch, tie mcu debug with RTC debug flag
* FuriHal: move resource init to early stage
* Add EXTI pending check, enable debug traps with compile-time flag
* Wrap sleep debug functions in conditional compilation
* Remove erroneous changed
* Do not use CSS, remove it from everywhere
* Enable/disable USB on VBUS connect (prototype)
* FuriHal: add LPMS and DEEPSLEEP magic, workaround state inconsistency between cores
* FuriHal: honor c1 LMPS
* USB mode switch fix
* Applications: add flags and insomnia bypass system
* Correct spelling
* FuriHal: cleanup insomnia usage, reset sleep flags on wakeup, add shutdown api
* FuriHal: extra check on reinit request
* FuriHal: rename gpio_display_rst pin to gpio_display_rst_n
* FuriHal: add debug HAL
* FuriHal: add some magic to core2 reload procedure, fix issue with crash on ble keyboard exit
* FuriHal: cleanup ble glue, add BLE_GLUE_DEBUG flag
* FuriHal: ble reinit API, move os timer to LPTIM1 for deep sleep capability, shutdown that works
* FuriHal: take insomnia while shutdown
* Remove USB switch on/off on VBUS change
* Better tick skew handling
* Improve tick consistency under load
* Add USB_HP dummy IRQ handler
* Move interrupt check closer to sleep
* Clean up includes
* Re-enable Insomnia globally
* FuriHal: enable CSS
* FuriHal: remove questionable core2 clock shenanigans
* FuriHal: use core1 RCC registers in idle timer config
* FuriHal: return back CSS handlers, add lptim isr dispatching
Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
Co-authored-by: nminaylov <nm29719@gmail.com>
2022-04-29 13:29:51 +00:00
|
|
|
/** Reinitialize core2
|
|
|
|
*
|
|
|
|
* Also can be used to prepare core2 for stop modes
|
|
|
|
*/
|
|
|
|
void furi_hal_bt_reinit();
|
|
|
|
|
2021-12-08 11:28:01 +00:00
|
|
|
/** Change BLE app
|
|
|
|
* Restarts 2nd core
|
|
|
|
*
|
|
|
|
* @param profile FuriHalBtProfile instance
|
2022-01-02 22:36:42 +00:00
|
|
|
* @param event_cb GapEventCallback instance
|
2021-12-08 11:28:01 +00:00
|
|
|
* @param context pointer to context
|
|
|
|
*
|
|
|
|
* @return true on success
|
2021-10-12 16:41:42 +00:00
|
|
|
*/
|
2022-01-02 22:36:42 +00:00
|
|
|
bool furi_hal_bt_change_app(FuriHalBtProfile profile, GapEventCallback event_cb, void* context);
|
2021-12-08 11:28:01 +00:00
|
|
|
|
|
|
|
/** Update battery level
|
|
|
|
*
|
|
|
|
* @param battery_level battery level
|
|
|
|
*/
|
|
|
|
void furi_hal_bt_update_battery_level(uint8_t battery_level);
|
2021-09-15 16:58:32 +00:00
|
|
|
|
2022-05-24 13:42:02 +00:00
|
|
|
/** Update battery power state */
|
|
|
|
void furi_hal_bt_update_power_state();
|
|
|
|
|
2022-02-07 13:37:56 +00:00
|
|
|
/** Checks if BLE state is active
|
|
|
|
*
|
|
|
|
* @return true if device is connected or advertising, false otherwise
|
|
|
|
*/
|
|
|
|
bool furi_hal_bt_is_active();
|
|
|
|
|
2021-10-03 10:36:05 +00:00
|
|
|
/** Start advertising
|
|
|
|
*/
|
2021-09-15 16:58:32 +00:00
|
|
|
void furi_hal_bt_start_advertising();
|
|
|
|
|
2021-10-03 10:36:05 +00:00
|
|
|
/** Stop advertising
|
|
|
|
*/
|
2021-09-15 16:58:32 +00:00
|
|
|
void furi_hal_bt_stop_advertising();
|
2021-03-11 09:31:07 +00:00
|
|
|
|
2021-10-03 10:36:05 +00:00
|
|
|
/** Get BT/BLE system component state
|
|
|
|
*
|
2022-10-05 15:15:23 +00:00
|
|
|
* @param[in] buffer FuriString* buffer to write to
|
2021-10-03 10:36:05 +00:00
|
|
|
*/
|
2022-10-05 15:15:23 +00:00
|
|
|
void furi_hal_bt_dump_state(FuriString* buffer);
|
2020-12-10 14:25:20 +00:00
|
|
|
|
2021-10-03 10:36:05 +00:00
|
|
|
/** Get BT/BLE system component state
|
|
|
|
*
|
|
|
|
* @return true if core2 is alive
|
|
|
|
*/
|
2021-08-08 18:03:25 +00:00
|
|
|
bool furi_hal_bt_is_alive();
|
2020-12-10 14:25:20 +00:00
|
|
|
|
2021-11-04 17:26:41 +00:00
|
|
|
/** Get key storage buffer address and size
|
|
|
|
*
|
2021-11-13 02:41:54 +00:00
|
|
|
* @param key_buff_addr pointer to store buffer address
|
|
|
|
* @param key_buff_size pointer to store buffer size
|
2021-11-04 17:26:41 +00:00
|
|
|
*/
|
2021-11-13 02:41:54 +00:00
|
|
|
void furi_hal_bt_get_key_storage_buff(uint8_t** key_buff_addr, uint16_t* key_buff_size);
|
2021-11-04 17:26:41 +00:00
|
|
|
|
|
|
|
/** Get SRAM2 hardware semaphore
|
|
|
|
* @note Must be called before SRAM2 read/write operations
|
|
|
|
*/
|
|
|
|
void furi_hal_bt_nvm_sram_sem_acquire();
|
|
|
|
|
|
|
|
/** Release SRAM2 hardware semaphore
|
|
|
|
* @note Must be called after SRAM2 read/write operations
|
|
|
|
*/
|
|
|
|
void furi_hal_bt_nvm_sram_sem_release();
|
|
|
|
|
2022-01-21 17:32:03 +00:00
|
|
|
/** Clear key storage
|
|
|
|
*
|
|
|
|
* @return true on success
|
|
|
|
*/
|
|
|
|
bool furi_hal_bt_clear_white_list();
|
|
|
|
|
2021-11-04 17:26:41 +00:00
|
|
|
/** Set key storage change callback
|
|
|
|
*
|
|
|
|
* @param callback BleGlueKeyStorageChangedCallback instance
|
|
|
|
* @param context pointer to context
|
|
|
|
*/
|
2022-01-05 16:10:18 +00:00
|
|
|
void furi_hal_bt_set_key_storage_change_callback(
|
|
|
|
BleGlueKeyStorageChangedCallback callback,
|
|
|
|
void* context);
|
2021-11-04 17:26:41 +00:00
|
|
|
|
2021-10-03 10:36:05 +00:00
|
|
|
/** Start ble tone tx at given channel and power
|
|
|
|
*
|
|
|
|
* @param[in] channel The channel
|
|
|
|
* @param[in] power The power
|
|
|
|
*/
|
2021-08-08 18:03:25 +00:00
|
|
|
void furi_hal_bt_start_tone_tx(uint8_t channel, uint8_t power);
|
2021-03-11 09:31:07 +00:00
|
|
|
|
2021-10-03 10:36:05 +00:00
|
|
|
/** Stop ble tone tx
|
|
|
|
*/
|
2021-08-08 18:03:25 +00:00
|
|
|
void furi_hal_bt_stop_tone_tx();
|
2021-03-11 09:31:07 +00:00
|
|
|
|
2021-10-03 10:36:05 +00:00
|
|
|
/** Start sending ble packets at a given frequency and datarate
|
|
|
|
*
|
|
|
|
* @param[in] channel The channel
|
|
|
|
* @param[in] pattern The pattern
|
|
|
|
* @param[in] datarate The datarate
|
|
|
|
*/
|
2021-08-08 18:03:25 +00:00
|
|
|
void furi_hal_bt_start_packet_tx(uint8_t channel, uint8_t pattern, uint8_t datarate);
|
2021-03-11 09:31:07 +00:00
|
|
|
|
2021-10-03 10:36:05 +00:00
|
|
|
/** Stop sending ble packets
|
|
|
|
*
|
|
|
|
* @return sent packet count
|
|
|
|
*/
|
2021-08-08 18:03:25 +00:00
|
|
|
uint16_t furi_hal_bt_stop_packet_test();
|
2021-05-28 22:57:11 +00:00
|
|
|
|
2021-10-03 10:36:05 +00:00
|
|
|
/** Start receiving packets
|
|
|
|
*
|
|
|
|
* @param[in] channel RX channel
|
|
|
|
* @param[in] datarate Datarate
|
|
|
|
*/
|
2021-08-08 18:03:25 +00:00
|
|
|
void furi_hal_bt_start_packet_rx(uint8_t channel, uint8_t datarate);
|
2021-03-11 09:31:07 +00:00
|
|
|
|
2021-10-03 10:36:05 +00:00
|
|
|
/** Set up the RF to listen to a given RF channel
|
|
|
|
*
|
|
|
|
* @param[in] channel RX channel
|
|
|
|
*/
|
2021-08-08 18:03:25 +00:00
|
|
|
void furi_hal_bt_start_rx(uint8_t channel);
|
2021-03-11 09:31:07 +00:00
|
|
|
|
2021-10-03 10:36:05 +00:00
|
|
|
/** Stop RF listenning
|
|
|
|
*/
|
2021-08-08 18:03:25 +00:00
|
|
|
void furi_hal_bt_stop_rx();
|
2021-03-11 09:31:07 +00:00
|
|
|
|
2021-10-03 10:36:05 +00:00
|
|
|
/** Get RSSI
|
|
|
|
*
|
|
|
|
* @return RSSI in dBm
|
|
|
|
*/
|
2021-08-08 18:03:25 +00:00
|
|
|
float furi_hal_bt_get_rssi();
|
2021-05-28 22:57:11 +00:00
|
|
|
|
2021-10-03 10:36:05 +00:00
|
|
|
/** Get number of transmitted packets
|
|
|
|
*
|
|
|
|
* @return packet count
|
|
|
|
*/
|
2021-08-08 18:03:25 +00:00
|
|
|
uint32_t furi_hal_bt_get_transmitted_packets();
|
2021-05-28 22:57:11 +00:00
|
|
|
|
2023-05-12 21:14:22 +00:00
|
|
|
// BadBT stuff
|
|
|
|
/** Modify profile advertisement name and restart bluetooth
|
|
|
|
* @param[in] profile profile type
|
|
|
|
* @param[in] name new adv name
|
|
|
|
*/
|
|
|
|
void furi_hal_bt_set_profile_adv_name(
|
|
|
|
FuriHalBtProfile profile,
|
|
|
|
const char name[FURI_HAL_BT_ADV_NAME_LENGTH]);
|
|
|
|
|
|
|
|
const char* furi_hal_bt_get_profile_adv_name(FuriHalBtProfile profile);
|
|
|
|
|
|
|
|
/** Modify profile mac address and restart bluetooth
|
|
|
|
* @param[in] profile profile type
|
|
|
|
* @param[in] mac new mac address
|
|
|
|
*/
|
|
|
|
void furi_hal_bt_set_profile_mac_addr(
|
|
|
|
FuriHalBtProfile profile,
|
|
|
|
const uint8_t mac_addr[GAP_MAC_ADDR_SIZE]);
|
|
|
|
|
|
|
|
const uint8_t* furi_hal_bt_get_profile_mac_addr(FuriHalBtProfile profile);
|
|
|
|
|
|
|
|
uint32_t furi_hal_bt_get_conn_rssi(uint8_t* rssi);
|
|
|
|
|
|
|
|
void furi_hal_bt_set_profile_pairing_method(FuriHalBtProfile profile, GapPairing pairing_method);
|
|
|
|
|
|
|
|
GapPairing furi_hal_bt_get_profile_pairing_method(FuriHalBtProfile profile);
|
|
|
|
|
|
|
|
bool furi_hal_bt_is_connected(void);
|
|
|
|
|
2022-04-27 15:53:48 +00:00
|
|
|
/** Check & switch C2 to given mode
|
|
|
|
*
|
|
|
|
* @param[in] mode mode to switch into
|
|
|
|
*/
|
|
|
|
bool furi_hal_bt_ensure_c2_mode(BleGlueC2Mode mode);
|
|
|
|
|
2023-05-08 22:31:39 +00:00
|
|
|
typedef struct {
|
|
|
|
uint32_t magic;
|
|
|
|
uint32_t source_pc;
|
|
|
|
uint32_t source_lr;
|
|
|
|
uint32_t source_sp;
|
|
|
|
} FuriHalBtHardfaultInfo;
|
|
|
|
|
|
|
|
/** Get hardfault info
|
|
|
|
*
|
|
|
|
* @return hardfault info. NULL if no hardfault
|
|
|
|
*/
|
|
|
|
const FuriHalBtHardfaultInfo* furi_hal_bt_get_hardfault_info();
|
|
|
|
|
2020-12-10 14:25:20 +00:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|