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>
|
2023-06-14 03:18:24 +03:00
|
|
|
#include <serial_service.h>
|
2021-11-04 20:26:41 +03:00
|
|
|
#include <ble_glue.h>
|
|
|
|
#include <ble_app.h>
|
|
|
|
|
2023-02-07 19:33:05 +03:00
|
|
|
#include <furi_hal_bt_serial.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)
|
2022-04-27 18:53:48 +03:00
|
|
|
#define FURI_HAL_BT_C2_START_TIMEOUT 1000
|
2022-01-03 01:36:42 +03:00
|
|
|
|
2023-07-05 14:41:28 +03:00
|
|
|
#define FURI_HAL_BT_EMPTY_MAC_ADDR \
|
|
|
|
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
|
|
|
|
|
|
|
|
#define FURI_HAL_BT_DEFAULT_MAC_ADDR \
|
|
|
|
{ 0x6c, 0x7a, 0xd8, 0xac, 0x57, 0x72 }
|
|
|
|
|
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-12-08 14:28:01 +03:00
|
|
|
typedef enum {
|
|
|
|
FuriHalBtProfileSerial,
|
|
|
|
FuriHalBtProfileHidKeyboard,
|
|
|
|
|
|
|
|
// Keep last for Profiles number calculation
|
|
|
|
FuriHalBtProfileNumber,
|
|
|
|
} FuriHalBtProfile;
|
|
|
|
|
2021-10-03 13:36:05 +03:00
|
|
|
/** Initialize
|
|
|
|
*/
|
2021-08-08 21:03:25 +03:00
|
|
|
void furi_hal_bt_init();
|
2020-12-10 17:25:20 +03:00
|
|
|
|
2021-11-10 12:53:00 +03:00
|
|
|
/** Lock core2 state transition */
|
|
|
|
void furi_hal_bt_lock_core2();
|
|
|
|
|
|
|
|
/** Lock core2 state transition */
|
|
|
|
void furi_hal_bt_unlock_core2();
|
|
|
|
|
2022-01-03 01:36:42 +03: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 12:07:42 +03: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 14:28:01 +03:00
|
|
|
/** Start BLE app
|
2021-11-04 20:26:41 +03:00
|
|
|
*
|
2021-12-08 14:28:01 +03:00
|
|
|
* @param profile FuriHalBtProfile instance
|
2022-01-03 01:36:42 +03:00
|
|
|
* @param event_cb GapEventCallback instance
|
2021-12-08 14:28:01 +03:00
|
|
|
* @param context pointer to context
|
|
|
|
*
|
|
|
|
* @return true on success
|
|
|
|
*/
|
2022-01-03 01:36:42 +03:00
|
|
|
bool furi_hal_bt_start_app(FuriHalBtProfile profile, 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
|
|
|
|
*/
|
|
|
|
void furi_hal_bt_reinit();
|
|
|
|
|
2021-12-08 14:28:01 +03:00
|
|
|
/** Change BLE app
|
|
|
|
* Restarts 2nd core
|
|
|
|
*
|
|
|
|
* @param profile FuriHalBtProfile instance
|
2022-01-03 01:36:42 +03:00
|
|
|
* @param event_cb GapEventCallback instance
|
2021-12-08 14:28:01 +03:00
|
|
|
* @param context pointer to context
|
|
|
|
*
|
|
|
|
* @return true on success
|
2021-10-12 19:41:42 +03:00
|
|
|
*/
|
2022-01-03 01:36:42 +03:00
|
|
|
bool furi_hal_bt_change_app(FuriHalBtProfile profile, 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 */
|
|
|
|
void furi_hal_bt_update_power_state();
|
|
|
|
|
2022-02-07 16:37:56 +03: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 13:36:05 +03:00
|
|
|
/** Start advertising
|
|
|
|
*/
|
2021-09-15 19:58:32 +03:00
|
|
|
void furi_hal_bt_start_advertising();
|
|
|
|
|
2021-10-03 13:36:05 +03:00
|
|
|
/** Stop advertising
|
|
|
|
*/
|
2021-09-15 19:58:32 +03:00
|
|
|
void furi_hal_bt_stop_advertising();
|
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
|
|
|
|
*/
|
2021-08-08 21:03:25 +03:00
|
|
|
bool furi_hal_bt_is_alive();
|
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
|
|
|
|
*/
|
|
|
|
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 20:32:03 +03:00
|
|
|
/** Clear key storage
|
|
|
|
*
|
|
|
|
* @return true on success
|
|
|
|
*/
|
|
|
|
bool furi_hal_bt_clear_white_list();
|
|
|
|
|
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
|
|
|
|
*/
|
2021-08-08 21:03:25 +03:00
|
|
|
void furi_hal_bt_stop_tone_tx();
|
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
|
|
|
|
*/
|
2021-08-08 21:03:25 +03:00
|
|
|
uint16_t furi_hal_bt_stop_packet_test();
|
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
|
|
|
|
*/
|
2021-08-08 21:03:25 +03:00
|
|
|
void furi_hal_bt_stop_rx();
|
2021-03-11 12:31:07 +03:00
|
|
|
|
2021-10-03 13:36:05 +03:00
|
|
|
/** Get RSSI
|
|
|
|
*
|
|
|
|
* @return RSSI in dBm
|
|
|
|
*/
|
2021-08-08 21:03:25 +03:00
|
|
|
float furi_hal_bt_get_rssi();
|
2021-05-29 01:57:11 +03:00
|
|
|
|
2021-10-03 13:36:05 +03:00
|
|
|
/** Get number of transmitted packets
|
|
|
|
*
|
|
|
|
* @return packet count
|
|
|
|
*/
|
2021-08-08 21:03:25 +03:00
|
|
|
uint32_t furi_hal_bt_get_transmitted_packets();
|
2021-05-29 01:57:11 +03:00
|
|
|
|
2023-07-05 14:41:28 +03:00
|
|
|
// BadBT Stuff
|
|
|
|
/** 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
|
|
|
/** 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);
|
|
|
|
|
2023-09-05 16:22:14 +03:00
|
|
|
// API for BLE Beacon plugin
|
2023-09-07 13:45:37 +03:00
|
|
|
/** Set custom advertisement packet data
|
|
|
|
* @param[in] adv_data pointer to advertisement data
|
|
|
|
* @param[in] adv_len length of advertisement data
|
|
|
|
*
|
|
|
|
* @return true on success
|
|
|
|
*/
|
|
|
|
bool furi_hal_bt_custom_adv_set(const uint8_t* adv_data, size_t adv_len);
|
|
|
|
|
|
|
|
/** Start custom advertisement beacon
|
|
|
|
* @param[in] min_interval minimum advertisement interval (20 - 10240 ms)
|
|
|
|
* @param[in] max_interval maximum advertisement interval (20 - 10240 ms)
|
|
|
|
* @param[in] mac_type type of mac address (0x00 public, 0x01 static random)
|
|
|
|
* @param[in] mac_addr pointer to mac address
|
|
|
|
* @param[in] power_amp_level amplifier level (output dBm) (0x00 - 0x1F)
|
|
|
|
*
|
|
|
|
* @return true on success
|
|
|
|
*/
|
|
|
|
bool furi_hal_bt_custom_adv_start(
|
|
|
|
uint16_t min_interval,
|
|
|
|
uint16_t max_interval,
|
|
|
|
uint8_t mac_type,
|
|
|
|
const uint8_t mac_addr[GAP_MAC_ADDR_SIZE],
|
|
|
|
uint8_t power_amp_level);
|
|
|
|
|
|
|
|
/** Stop custom advertisement beacon
|
|
|
|
*
|
|
|
|
* @return true on success
|
|
|
|
*/
|
|
|
|
bool furi_hal_bt_custom_adv_stop();
|
2023-09-05 16:22:14 +03:00
|
|
|
|
2023-05-13 00:14:22 +03:00
|
|
|
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 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);
|
|
|
|
|
2023-05-09 01:31:39 +03: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 17:25:20 +03:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|