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>
|
2024-02-16 07:20:45 +00:00
|
|
|
#include <extra_beacon.h>
|
|
|
|
#include <furi_ble/profile_interface.h>
|
2021-11-04 17:26:41 +00:00
|
|
|
#include <ble_glue.h>
|
|
|
|
#include <ble_app.h>
|
2024-02-16 07:20:45 +00:00
|
|
|
#include <stdint.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)
|
2024-02-16 07:20:45 +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-10-03 10:36:05 +00:00
|
|
|
/** Initialize
|
|
|
|
*/
|
2024-03-19 14:43:52 +00:00
|
|
|
void furi_hal_bt_init(void);
|
2020-12-10 14:25:20 +00:00
|
|
|
|
2021-11-10 09:53:00 +00:00
|
|
|
/** Lock core2 state transition */
|
2024-03-19 14:43:52 +00:00
|
|
|
void furi_hal_bt_lock_core2(void);
|
2021-11-10 09:53:00 +00:00
|
|
|
|
|
|
|
/** Lock core2 state transition */
|
2024-03-19 14:43:52 +00:00
|
|
|
void furi_hal_bt_unlock_core2(void);
|
2021-11-10 09:53:00 +00:00
|
|
|
|
2022-01-02 22:36:42 +00:00
|
|
|
/** Start radio stack
|
|
|
|
*
|
|
|
|
* @return true on successfull radio stack start
|
|
|
|
*/
|
2024-03-19 14:43:52 +00:00
|
|
|
bool furi_hal_bt_start_radio_stack(void);
|
2022-01-02 22:36:42 +00:00
|
|
|
|
|
|
|
/** Get radio stack type
|
|
|
|
*
|
|
|
|
* @return FuriHalBtStack instance
|
|
|
|
*/
|
2024-03-19 14:43:52 +00:00
|
|
|
FuriHalBtStack furi_hal_bt_get_radio_stack(void);
|
2022-01-02 22:36:42 +00:00
|
|
|
|
2022-06-09 09:07:42 +00:00
|
|
|
/** Check if radio stack supports BLE GAT/GAP
|
|
|
|
*
|
|
|
|
* @return true if supported
|
|
|
|
*/
|
2024-03-19 14:43:52 +00:00
|
|
|
bool furi_hal_bt_is_gatt_gap_supported(void);
|
2022-06-09 09:07:42 +00:00
|
|
|
|
|
|
|
/** Check if radio stack supports testing
|
|
|
|
*
|
|
|
|
* @return true if supported
|
|
|
|
*/
|
2024-03-19 14:43:52 +00:00
|
|
|
bool furi_hal_bt_is_testing_supported(void);
|
2022-06-09 09:07:42 +00:00
|
|
|
|
2024-02-16 07:20:45 +00:00
|
|
|
/** Check if particular instance of profile belongs to given type
|
2021-11-04 17:26:41 +00:00
|
|
|
*
|
2024-02-16 07:20:45 +00:00
|
|
|
* @param profile FuriHalBtProfile instance. If NULL, uses current profile
|
|
|
|
* @param profile_template basic profile template to check against
|
2021-12-08 11:28:01 +00:00
|
|
|
*
|
|
|
|
* @return true on success
|
|
|
|
*/
|
2024-02-16 07:20:45 +00: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 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
|
|
|
|
*/
|
2024-03-19 14:43:52 +00: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 13:29:51 +00:00
|
|
|
|
2021-12-08 11:28:01 +00:00
|
|
|
/** Change BLE app
|
|
|
|
* Restarts 2nd core
|
|
|
|
*
|
2024-03-06 06:25:21 +00: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 11:28:01 +00:00
|
|
|
*
|
2024-03-06 06:25:21 +00:00
|
|
|
* @return instance of profile, NULL on failure
|
2021-10-12 16:41:42 +00:00
|
|
|
*/
|
2024-02-16 07:20:45 +00: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 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 */
|
2024-02-16 07:20:45 +00:00
|
|
|
void furi_hal_bt_update_power_state(bool charging);
|
2022-05-24 13:42:02 +00:00
|
|
|
|
2022-02-07 13:37:56 +00:00
|
|
|
/** Checks if BLE state is active
|
|
|
|
*
|
|
|
|
* @return true if device is connected or advertising, false otherwise
|
|
|
|
*/
|
2024-03-19 14:43:52 +00:00
|
|
|
bool furi_hal_bt_is_active(void);
|
2022-02-07 13:37:56 +00:00
|
|
|
|
2021-10-03 10:36:05 +00:00
|
|
|
/** Start advertising
|
|
|
|
*/
|
2024-03-19 14:43:52 +00:00
|
|
|
void furi_hal_bt_start_advertising(void);
|
2021-09-15 16:58:32 +00:00
|
|
|
|
2021-10-03 10:36:05 +00:00
|
|
|
/** Stop advertising
|
|
|
|
*/
|
2024-03-19 14:43:52 +00:00
|
|
|
void furi_hal_bt_stop_advertising(void);
|
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
|
|
|
|
*/
|
2024-03-19 14:43:52 +00:00
|
|
|
bool furi_hal_bt_is_alive(void);
|
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
|
|
|
|
*/
|
2024-03-19 14:43:52 +00:00
|
|
|
void furi_hal_bt_nvm_sram_sem_acquire(void);
|
2021-11-04 17:26:41 +00:00
|
|
|
|
|
|
|
/** Release SRAM2 hardware semaphore
|
|
|
|
* @note Must be called after SRAM2 read/write operations
|
|
|
|
*/
|
2024-03-19 14:43:52 +00:00
|
|
|
void furi_hal_bt_nvm_sram_sem_release(void);
|
2021-11-04 17:26:41 +00:00
|
|
|
|
2022-01-21 17:32:03 +00:00
|
|
|
/** Clear key storage
|
|
|
|
*
|
|
|
|
* @return true on success
|
|
|
|
*/
|
2024-03-19 14:43:52 +00:00
|
|
|
bool furi_hal_bt_clear_white_list(void);
|
2022-01-21 17:32:03 +00:00
|
|
|
|
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
|
|
|
|
*/
|
2024-03-19 14:43:52 +00:00
|
|
|
void furi_hal_bt_stop_tone_tx(void);
|
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
|
|
|
|
*/
|
2024-03-19 14:43:52 +00:00
|
|
|
uint16_t furi_hal_bt_stop_packet_test(void);
|
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
|
|
|
|
*/
|
2024-03-19 14:43:52 +00:00
|
|
|
void furi_hal_bt_stop_rx(void);
|
2021-03-11 09:31:07 +00:00
|
|
|
|
2021-10-03 10:36:05 +00:00
|
|
|
/** Get RSSI
|
|
|
|
*
|
|
|
|
* @return RSSI in dBm
|
|
|
|
*/
|
2024-03-19 14:43:52 +00:00
|
|
|
float furi_hal_bt_get_rssi(void);
|
2021-05-28 22:57:11 +00:00
|
|
|
|
2021-10-03 10:36:05 +00:00
|
|
|
/** Get number of transmitted packets
|
|
|
|
*
|
|
|
|
* @return packet count
|
|
|
|
*/
|
2024-03-19 14:43:52 +00:00
|
|
|
uint32_t furi_hal_bt_get_transmitted_packets(void);
|
2021-05-28 22:57:11 +00:00
|
|
|
|
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);
|
|
|
|
|
2024-02-16 07:20:45 +00: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-19 14:43:52 +00:00
|
|
|
bool furi_hal_bt_extra_beacon_start(void);
|
2024-02-16 07:20:45 +00:00
|
|
|
|
|
|
|
/** Stop extra beacon
|
|
|
|
*
|
|
|
|
* @return true on success
|
|
|
|
*/
|
2024-03-19 14:43:52 +00:00
|
|
|
bool furi_hal_bt_extra_beacon_stop(void);
|
2024-02-16 07:20:45 +00:00
|
|
|
|
|
|
|
/** Check if extra beacon is active.
|
|
|
|
*
|
|
|
|
* @return extra beacon state
|
|
|
|
*/
|
2024-03-19 14:43:52 +00:00
|
|
|
bool furi_hal_bt_extra_beacon_is_active(void);
|
2023-05-08 22:31:39 +00:00
|
|
|
|
2024-02-16 07:20:45 +00:00
|
|
|
/** Get last configured extra beacon config
|
2023-05-08 22:31:39 +00:00
|
|
|
*
|
2024-02-16 07:20:45 +00:00
|
|
|
* @return extra beacon config. NULL if beacon had never been configured.
|
2023-05-08 22:31:39 +00:00
|
|
|
*/
|
2024-03-19 14:43:52 +00:00
|
|
|
const GapExtraBeaconConfig* furi_hal_bt_extra_beacon_get_config(void);
|
2023-05-08 22:31:39 +00:00
|
|
|
|
2020-12-10 14:25:20 +00:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|