2021-10-03 13:36:05 +03:00
|
|
|
/**
|
2022-01-05 19:10:18 +03:00
|
|
|
* @file furi_hal_bt.h
|
2021-10-03 13:36:05 +03:00
|
|
|
* BT/BLE HAL API
|
|
|
|
*/
|
|
|
|
|
2020-12-10 17:25:20 +03:00
|
|
|
#pragma once
|
|
|
|
|
2022-10-05 18:15:23 +03:00
|
|
|
#include <furi.h>
|
2020-12-10 17:25:20 +03:00
|
|
|
#include <stdbool.h>
|
2021-10-12 19:41:42 +03:00
|
|
|
#include <gap.h>
|
2024-02-16 10:20:45 +03:00
|
|
|
#include <extra_beacon.h>
|
|
|
|
#include <furi_ble/profile_interface.h>
|
2021-11-04 20:26:41 +03:00
|
|
|
#include <ble_glue.h>
|
|
|
|
#include <ble_app.h>
|
2024-02-16 10:20:45 +03:00
|
|
|
#include <stdint.h>
|
2020-12-10 17:25:20 +03:00
|
|
|
|
2022-01-03 01:36:42 +03:00
|
|
|
#define FURI_HAL_BT_STACK_VERSION_MAJOR (1)
|
2022-06-09 12:07:42 +03:00
|
|
|
#define FURI_HAL_BT_STACK_VERSION_MINOR (12)
|
2024-02-16 10:20:45 +03:00
|
|
|
#define FURI_HAL_BT_C2_START_TIMEOUT (1000)
|
2022-01-03 01:36:42 +03:00
|
|
|
|
2020-12-10 17:25:20 +03:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2022-01-03 01:36:42 +03:00
|
|
|
typedef enum {
|
|
|
|
FuriHalBtStackUnknown,
|
|
|
|
FuriHalBtStackLight,
|
2022-06-09 12:07:42 +03:00
|
|
|
FuriHalBtStackFull,
|
2022-01-03 01:36:42 +03:00
|
|
|
} FuriHalBtStack;
|
|
|
|
|
2021-10-03 13:36:05 +03:00
|
|
|
/** Initialize
|
|
|
|
*/
|
2024-03-25 13:53:32 +03:00
|
|
|
void furi_hal_bt_init(void);
|
2020-12-10 17:25:20 +03:00
|
|
|
|
2021-11-10 12:53:00 +03:00
|
|
|
/** Lock core2 state transition */
|
2024-03-25 13:53:32 +03:00
|
|
|
void furi_hal_bt_lock_core2(void);
|
2021-11-10 12:53:00 +03:00
|
|
|
|
|
|
|
/** Lock core2 state transition */
|
2024-03-25 13:53:32 +03:00
|
|
|
void furi_hal_bt_unlock_core2(void);
|
2021-11-10 12:53:00 +03:00
|
|
|
|
2022-01-03 01:36:42 +03:00
|
|
|
/** Start radio stack
|
|
|
|
*
|
|
|
|
* @return true on successfull radio stack start
|
|
|
|
*/
|
2024-03-25 13:53:32 +03:00
|
|
|
bool furi_hal_bt_start_radio_stack(void);
|
2022-01-03 01:36:42 +03:00
|
|
|
|
|
|
|
/** Get radio stack type
|
|
|
|
*
|
|
|
|
* @return FuriHalBtStack instance
|
|
|
|
*/
|
2024-03-25 13:53:32 +03:00
|
|
|
FuriHalBtStack furi_hal_bt_get_radio_stack(void);
|
2022-01-03 01:36:42 +03:00
|
|
|
|
2022-06-09 12:07:42 +03:00
|
|
|
/** Check if radio stack supports BLE GAT/GAP
|
|
|
|
*
|
|
|
|
* @return true if supported
|
|
|
|
*/
|
2024-03-25 13:53:32 +03:00
|
|
|
bool furi_hal_bt_is_gatt_gap_supported(void);
|
2022-06-09 12:07:42 +03:00
|
|
|
|
|
|
|
/** Check if radio stack supports testing
|
|
|
|
*
|
|
|
|
* @return true if supported
|
|
|
|
*/
|
2024-03-25 13:53:32 +03:00
|
|
|
bool furi_hal_bt_is_testing_supported(void);
|
2022-06-09 12:07:42 +03:00
|
|
|
|
2024-02-16 10:20:45 +03:00
|
|
|
/** Check if particular instance of profile belongs to given type
|
2021-11-04 20:26:41 +03:00
|
|
|
*
|
2024-02-16 10:20:45 +03:00
|
|
|
* @param profile FuriHalBtProfile instance. If NULL, uses current profile
|
|
|
|
* @param profile_template basic profile template to check against
|
2021-12-08 14:28:01 +03:00
|
|
|
*
|
|
|
|
* @return true on success
|
|
|
|
*/
|
2024-02-16 10:20:45 +03:00
|
|
|
bool furi_hal_bt_check_profile_type(
|
|
|
|
FuriHalBleProfileBase* profile,
|
|
|
|
const FuriHalBleProfileTemplate* profile_template);
|
|
|
|
|
|
|
|
/** Start BLE app
|
|
|
|
*
|
|
|
|
* @param profile_template FuriHalBleProfileTemplate instance
|
|
|
|
* @param params Parameters to pass to the profile. Can be NULL
|
|
|
|
* @param event_cb GapEventCallback instance
|
|
|
|
* @param context pointer to context
|
|
|
|
*
|
|
|
|
* @return instance of profile, NULL on failure
|
|
|
|
*/
|
|
|
|
FURI_WARN_UNUSED FuriHalBleProfileBase* furi_hal_bt_start_app(
|
|
|
|
const FuriHalBleProfileTemplate* profile_template,
|
|
|
|
FuriHalBleProfileParams params,
|
|
|
|
GapEventCallback event_cb,
|
|
|
|
void* context);
|
2021-11-04 20:26:41 +03: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 16:29:51 +03:00
|
|
|
/** Reinitialize core2
|
|
|
|
*
|
|
|
|
* Also can be used to prepare core2 for stop modes
|
|
|
|
*/
|
2024-03-25 13:53:32 +03:00
|
|
|
void furi_hal_bt_reinit(void);
|
[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 16:29:51 +03:00
|
|
|
|
2021-12-08 14:28:01 +03:00
|
|
|
/** Change BLE app
|
|
|
|
* Restarts 2nd core
|
|
|
|
*
|
2024-03-10 23:35:44 +03:00
|
|
|
* @param profile_template FuriHalBleProfileTemplate instance
|
|
|
|
* @param profile_params Parameters to pass to the profile. Can be NULL
|
|
|
|
* @param event_cb GapEventCallback instance
|
|
|
|
* @param context pointer to context
|
2021-12-08 14:28:01 +03:00
|
|
|
*
|
2024-03-10 23:35:44 +03:00
|
|
|
* @return instance of profile, NULL on failure
|
2021-10-12 19:41:42 +03:00
|
|
|
*/
|
2024-02-16 10:20:45 +03:00
|
|
|
FURI_WARN_UNUSED FuriHalBleProfileBase* furi_hal_bt_change_app(
|
|
|
|
const FuriHalBleProfileTemplate* profile_template,
|
|
|
|
FuriHalBleProfileParams profile_params,
|
|
|
|
GapEventCallback event_cb,
|
|
|
|
void* context);
|
2021-12-08 14:28:01 +03:00
|
|
|
|
|
|
|
/** Update battery level
|
|
|
|
*
|
|
|
|
* @param battery_level battery level
|
|
|
|
*/
|
|
|
|
void furi_hal_bt_update_battery_level(uint8_t battery_level);
|
2021-09-15 19:58:32 +03:00
|
|
|
|
2022-05-24 16:42:02 +03:00
|
|
|
/** Update battery power state */
|
2024-02-16 10:20:45 +03:00
|
|
|
void furi_hal_bt_update_power_state(bool charging);
|
2022-05-24 16:42:02 +03:00
|
|
|
|
2022-02-07 16:37:56 +03:00
|
|
|
/** Checks if BLE state is active
|
|
|
|
*
|
|
|
|
* @return true if device is connected or advertising, false otherwise
|
|
|
|
*/
|
2024-03-25 13:53:32 +03:00
|
|
|
bool furi_hal_bt_is_active(void);
|
2022-02-07 16:37:56 +03:00
|
|
|
|
2021-10-03 13:36:05 +03:00
|
|
|
/** Start advertising
|
|
|
|
*/
|
2024-03-25 13:53:32 +03:00
|
|
|
void furi_hal_bt_start_advertising(void);
|
2021-09-15 19:58:32 +03:00
|
|
|
|
2021-10-03 13:36:05 +03:00
|
|
|
/** Stop advertising
|
|
|
|
*/
|
2024-03-25 13:53:32 +03:00
|
|
|
void furi_hal_bt_stop_advertising(void);
|
2021-03-11 12:31:07 +03:00
|
|
|
|
2021-10-03 13:36:05 +03:00
|
|
|
/** Get BT/BLE system component state
|
|
|
|
*
|
2022-10-05 18:15:23 +03:00
|
|
|
* @param[in] buffer FuriString* buffer to write to
|
2021-10-03 13:36:05 +03:00
|
|
|
*/
|
2022-10-05 18:15:23 +03:00
|
|
|
void furi_hal_bt_dump_state(FuriString* buffer);
|
2020-12-10 17:25:20 +03:00
|
|
|
|
2021-10-03 13:36:05 +03:00
|
|
|
/** Get BT/BLE system component state
|
|
|
|
*
|
|
|
|
* @return true if core2 is alive
|
|
|
|
*/
|
2024-03-25 13:53:32 +03:00
|
|
|
bool furi_hal_bt_is_alive(void);
|
2020-12-10 17:25:20 +03:00
|
|
|
|
2021-11-04 20:26:41 +03:00
|
|
|
/** Get key storage buffer address and size
|
|
|
|
*
|
2021-11-13 05:41:54 +03:00
|
|
|
* @param key_buff_addr pointer to store buffer address
|
|
|
|
* @param key_buff_size pointer to store buffer size
|
2021-11-04 20:26:41 +03:00
|
|
|
*/
|
2021-11-13 05:41:54 +03:00
|
|
|
void furi_hal_bt_get_key_storage_buff(uint8_t** key_buff_addr, uint16_t* key_buff_size);
|
2021-11-04 20:26:41 +03:00
|
|
|
|
|
|
|
/** Get SRAM2 hardware semaphore
|
|
|
|
* @note Must be called before SRAM2 read/write operations
|
|
|
|
*/
|
2024-03-25 13:53:32 +03:00
|
|
|
void furi_hal_bt_nvm_sram_sem_acquire(void);
|
2021-11-04 20:26:41 +03:00
|
|
|
|
|
|
|
/** Release SRAM2 hardware semaphore
|
|
|
|
* @note Must be called after SRAM2 read/write operations
|
|
|
|
*/
|
2024-03-25 13:53:32 +03:00
|
|
|
void furi_hal_bt_nvm_sram_sem_release(void);
|
2021-11-04 20:26:41 +03:00
|
|
|
|
2022-01-21 20:32:03 +03:00
|
|
|
/** Clear key storage
|
|
|
|
*
|
|
|
|
* @return true on success
|
|
|
|
*/
|
2024-03-25 13:53:32 +03:00
|
|
|
bool furi_hal_bt_clear_white_list(void);
|
2022-01-21 20:32:03 +03:00
|
|
|
|
2021-11-04 20:26:41 +03:00
|
|
|
/** Set key storage change callback
|
|
|
|
*
|
|
|
|
* @param callback BleGlueKeyStorageChangedCallback instance
|
|
|
|
* @param context pointer to context
|
|
|
|
*/
|
2022-01-05 19:10:18 +03:00
|
|
|
void furi_hal_bt_set_key_storage_change_callback(
|
|
|
|
BleGlueKeyStorageChangedCallback callback,
|
|
|
|
void* context);
|
2021-11-04 20:26:41 +03:00
|
|
|
|
2021-10-03 13:36:05 +03:00
|
|
|
/** Start ble tone tx at given channel and power
|
|
|
|
*
|
|
|
|
* @param[in] channel The channel
|
|
|
|
* @param[in] power The power
|
|
|
|
*/
|
2021-08-08 21:03:25 +03:00
|
|
|
void furi_hal_bt_start_tone_tx(uint8_t channel, uint8_t power);
|
2021-03-11 12:31:07 +03:00
|
|
|
|
2021-10-03 13:36:05 +03:00
|
|
|
/** Stop ble tone tx
|
|
|
|
*/
|
2024-03-25 13:53:32 +03:00
|
|
|
void furi_hal_bt_stop_tone_tx(void);
|
2021-03-11 12:31:07 +03:00
|
|
|
|
2021-10-03 13:36:05 +03: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 21:03:25 +03:00
|
|
|
void furi_hal_bt_start_packet_tx(uint8_t channel, uint8_t pattern, uint8_t datarate);
|
2021-03-11 12:31:07 +03:00
|
|
|
|
2021-10-03 13:36:05 +03:00
|
|
|
/** Stop sending ble packets
|
|
|
|
*
|
|
|
|
* @return sent packet count
|
|
|
|
*/
|
2024-03-25 13:53:32 +03:00
|
|
|
uint16_t furi_hal_bt_stop_packet_test(void);
|
2021-05-29 01:57:11 +03:00
|
|
|
|
2021-10-03 13:36:05 +03:00
|
|
|
/** Start receiving packets
|
|
|
|
*
|
|
|
|
* @param[in] channel RX channel
|
|
|
|
* @param[in] datarate Datarate
|
|
|
|
*/
|
2021-08-08 21:03:25 +03:00
|
|
|
void furi_hal_bt_start_packet_rx(uint8_t channel, uint8_t datarate);
|
2021-03-11 12:31:07 +03:00
|
|
|
|
2021-10-03 13:36:05 +03:00
|
|
|
/** Set up the RF to listen to a given RF channel
|
|
|
|
*
|
|
|
|
* @param[in] channel RX channel
|
|
|
|
*/
|
2021-08-08 21:03:25 +03:00
|
|
|
void furi_hal_bt_start_rx(uint8_t channel);
|
2021-03-11 12:31:07 +03:00
|
|
|
|
2021-10-03 13:36:05 +03:00
|
|
|
/** Stop RF listenning
|
|
|
|
*/
|
2024-03-25 13:53:32 +03:00
|
|
|
void furi_hal_bt_stop_rx(void);
|
2021-03-11 12:31:07 +03:00
|
|
|
|
2021-10-03 13:36:05 +03:00
|
|
|
/** Get RSSI
|
|
|
|
*
|
|
|
|
* @return RSSI in dBm
|
|
|
|
*/
|
2024-03-25 13:53:32 +03:00
|
|
|
float furi_hal_bt_get_rssi(void);
|
2021-05-29 01:57:11 +03:00
|
|
|
|
2021-10-03 13:36:05 +03:00
|
|
|
/** Get number of transmitted packets
|
|
|
|
*
|
|
|
|
* @return packet count
|
|
|
|
*/
|
2024-03-25 13:53:32 +03:00
|
|
|
uint32_t furi_hal_bt_get_transmitted_packets(void);
|
2021-05-29 01:57:11 +03:00
|
|
|
|
2023-07-05 14:41:28 +03:00
|
|
|
/** Reverse a MAC address byte order in-place
|
|
|
|
* @param[in] mac mac address to reverse
|
|
|
|
*/
|
|
|
|
void furi_hal_bt_reverse_mac_addr(uint8_t mac_addr[GAP_MAC_ADDR_SIZE]);
|
|
|
|
|
2023-05-13 00:14:22 +03:00
|
|
|
uint32_t furi_hal_bt_get_conn_rssi(uint8_t* rssi);
|
|
|
|
|
|
|
|
bool furi_hal_bt_is_connected(void);
|
|
|
|
|
2022-04-27 18:53:48 +03:00
|
|
|
/** Check & switch C2 to given mode
|
|
|
|
*
|
|
|
|
* @param[in] mode mode to switch into
|
|
|
|
*/
|
|
|
|
bool furi_hal_bt_ensure_c2_mode(BleGlueC2Mode mode);
|
|
|
|
|
2024-02-16 10:20:45 +03:00
|
|
|
/**
|
|
|
|
* Extra BLE beacon API
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** Set extra beacon data. Can be called in any state
|
|
|
|
*
|
|
|
|
* @param[in] data data to set
|
|
|
|
* @param[in] len data length. Must be <= EXTRA_BEACON_MAX_DATA_SIZE
|
|
|
|
*
|
|
|
|
* @return true on success
|
|
|
|
*/
|
|
|
|
bool furi_hal_bt_extra_beacon_set_data(const uint8_t* data, uint8_t len);
|
|
|
|
|
|
|
|
/** Get last configured extra beacon data
|
|
|
|
*
|
|
|
|
* @param data data buffer to write to. Must be at least EXTRA_BEACON_MAX_DATA_SIZE bytes long
|
|
|
|
*
|
|
|
|
* @return valid data length
|
|
|
|
*/
|
|
|
|
uint8_t furi_hal_bt_extra_beacon_get_data(uint8_t* data);
|
|
|
|
|
|
|
|
/** Configure extra beacon.
|
|
|
|
*
|
|
|
|
* @param[in] config extra beacon config: interval, power, address, etc.
|
|
|
|
*
|
|
|
|
* @return true on success
|
|
|
|
*/
|
|
|
|
bool furi_hal_bt_extra_beacon_set_config(const GapExtraBeaconConfig* config);
|
|
|
|
|
|
|
|
/** Start extra beacon.
|
|
|
|
* Beacon must configured with furi_hal_bt_extra_beacon_set_config()
|
|
|
|
* and in stopped state before calling this function.
|
|
|
|
*
|
|
|
|
* @return true on success
|
|
|
|
*/
|
2024-03-25 13:53:32 +03:00
|
|
|
bool furi_hal_bt_extra_beacon_start(void);
|
2024-02-16 10:20:45 +03:00
|
|
|
|
|
|
|
/** Stop extra beacon
|
|
|
|
*
|
|
|
|
* @return true on success
|
|
|
|
*/
|
2024-03-25 13:53:32 +03:00
|
|
|
bool furi_hal_bt_extra_beacon_stop(void);
|
2024-02-16 10:20:45 +03:00
|
|
|
|
|
|
|
/** Check if extra beacon is active.
|
|
|
|
*
|
|
|
|
* @return extra beacon state
|
|
|
|
*/
|
2024-03-25 13:53:32 +03:00
|
|
|
bool furi_hal_bt_extra_beacon_is_active(void);
|
2023-05-09 01:31:39 +03:00
|
|
|
|
2024-02-16 10:20:45 +03:00
|
|
|
/** Get last configured extra beacon config
|
2023-05-09 01:31:39 +03:00
|
|
|
*
|
2024-02-16 10:20:45 +03:00
|
|
|
* @return extra beacon config. NULL if beacon had never been configured.
|
2023-05-09 01:31:39 +03:00
|
|
|
*/
|
2024-03-25 13:53:32 +03:00
|
|
|
const GapExtraBeaconConfig* furi_hal_bt_extra_beacon_get_config(void);
|
2023-05-09 01:31:39 +03:00
|
|
|
|
2020-12-10 17:25:20 +03:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|