|
|
|
|
@@ -1,10 +1,13 @@
|
|
|
|
|
#include "bt_i.h"
|
|
|
|
|
#include "bt_keys_storage.h"
|
|
|
|
|
|
|
|
|
|
#include <core/check.h>
|
|
|
|
|
#include <furi_hal_bt.h>
|
|
|
|
|
#include <services/battery_service.h>
|
|
|
|
|
#include <notification/notification_messages.h>
|
|
|
|
|
#include <gui/elements.h>
|
|
|
|
|
#include <assets_icons.h>
|
|
|
|
|
#include <profiles/serial_profile.h>
|
|
|
|
|
|
|
|
|
|
#define TAG "BtSrv"
|
|
|
|
|
|
|
|
|
|
@@ -12,14 +15,21 @@
|
|
|
|
|
#define BT_RPC_EVENT_DISCONNECTED (1UL << 1)
|
|
|
|
|
#define BT_RPC_EVENT_ALL (BT_RPC_EVENT_BUFF_SENT | BT_RPC_EVENT_DISCONNECTED)
|
|
|
|
|
|
|
|
|
|
#define ICON_SPACER 2
|
|
|
|
|
|
|
|
|
|
static void bt_draw_statusbar_callback(Canvas* canvas, void* context) {
|
|
|
|
|
furi_assert(context);
|
|
|
|
|
|
|
|
|
|
Bt* bt = context;
|
|
|
|
|
uint8_t draw_offset = 0;
|
|
|
|
|
if(bt->beacon_active) {
|
|
|
|
|
canvas_draw_icon(canvas, 0, 0, &I_BLE_beacon_7x8);
|
|
|
|
|
draw_offset += icon_get_width(&I_BLE_beacon_7x8) + ICON_SPACER;
|
|
|
|
|
}
|
|
|
|
|
if(bt->status == BtStatusAdvertising) {
|
|
|
|
|
canvas_draw_icon(canvas, 0, 0, &I_Bluetooth_Idle_5x8);
|
|
|
|
|
canvas_draw_icon(canvas, draw_offset, 0, &I_Bluetooth_Idle_5x8);
|
|
|
|
|
} else if(bt->status == BtStatusConnected) {
|
|
|
|
|
canvas_draw_icon(canvas, 0, 0, &I_Bluetooth_Connected_16x8);
|
|
|
|
|
canvas_draw_icon(canvas, draw_offset, 0, &I_Bluetooth_Connected_16x8);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -60,8 +70,12 @@ static ViewPort* bt_pin_code_view_port_alloc(Bt* bt) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void bt_pin_code_show(Bt* bt, uint32_t pin_code) {
|
|
|
|
|
furi_assert(bt);
|
|
|
|
|
bt->pin_code = pin_code;
|
|
|
|
|
if(!bt->pin_code_view_port) {
|
|
|
|
|
// Pin code view port
|
|
|
|
|
bt->pin_code_view_port = bt_pin_code_view_port_alloc(bt);
|
|
|
|
|
gui_add_view_port(bt->gui, bt->pin_code_view_port, GuiLayerFullscreen);
|
|
|
|
|
}
|
|
|
|
|
notification_message(bt->notification, &sequence_display_backlight_on);
|
|
|
|
|
if(bt->suppress_pin_screen) return;
|
|
|
|
|
|
|
|
|
|
@@ -71,7 +85,7 @@ static void bt_pin_code_show(Bt* bt, uint32_t pin_code) {
|
|
|
|
|
|
|
|
|
|
static void bt_pin_code_hide(Bt* bt) {
|
|
|
|
|
bt->pin_code = 0;
|
|
|
|
|
if(view_port_is_enabled(bt->pin_code_view_port)) {
|
|
|
|
|
if(bt->pin_code_view_port && view_port_is_enabled(bt->pin_code_view_port)) {
|
|
|
|
|
view_port_enabled_set(bt->pin_code_view_port, false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
@@ -83,6 +97,9 @@ static bool bt_pin_code_verify_event_handler(Bt* bt, uint32_t pin) {
|
|
|
|
|
if(bt->suppress_pin_screen) return true;
|
|
|
|
|
|
|
|
|
|
FuriString* pin_str;
|
|
|
|
|
if(!bt->dialog_message) {
|
|
|
|
|
bt->dialog_message = dialog_message_alloc();
|
|
|
|
|
}
|
|
|
|
|
dialog_message_set_icon(bt->dialog_message, &I_BLE_Pairing_128x64, 0, 0);
|
|
|
|
|
pin_str = furi_string_alloc_printf("Verify code\n%06lu", pin);
|
|
|
|
|
dialog_message_set_text(
|
|
|
|
|
@@ -100,25 +117,32 @@ static void bt_battery_level_changed_callback(const void* _event, void* context)
|
|
|
|
|
Bt* bt = context;
|
|
|
|
|
BtMessage message = {};
|
|
|
|
|
const PowerEvent* event = _event;
|
|
|
|
|
if(event->type == PowerEventTypeBatteryLevelChanged) {
|
|
|
|
|
bool is_charging = false;
|
|
|
|
|
switch(event->type) {
|
|
|
|
|
case PowerEventTypeBatteryLevelChanged:
|
|
|
|
|
message.type = BtMessageTypeUpdateBatteryLevel;
|
|
|
|
|
message.data.battery_level = event->data.battery_level;
|
|
|
|
|
furi_check(
|
|
|
|
|
furi_message_queue_put(bt->message_queue, &message, FuriWaitForever) == FuriStatusOk);
|
|
|
|
|
} else if(
|
|
|
|
|
event->type == PowerEventTypeStartCharging || event->type == PowerEventTypeFullyCharged ||
|
|
|
|
|
event->type == PowerEventTypeStopCharging) {
|
|
|
|
|
break;
|
|
|
|
|
case PowerEventTypeStartCharging:
|
|
|
|
|
is_charging = true;
|
|
|
|
|
/* fallthrough */
|
|
|
|
|
case PowerEventTypeFullyCharged:
|
|
|
|
|
case PowerEventTypeStopCharging:
|
|
|
|
|
message.type = BtMessageTypeUpdatePowerState;
|
|
|
|
|
message.data.power_state_charging = is_charging;
|
|
|
|
|
furi_check(
|
|
|
|
|
furi_message_queue_put(bt->message_queue, &message, FuriWaitForever) == FuriStatusOk);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Bt* bt_alloc() {
|
|
|
|
|
Bt* bt = malloc(sizeof(Bt));
|
|
|
|
|
// Init default maximum packet size
|
|
|
|
|
bt->max_packet_size = FURI_HAL_BT_SERIAL_PACKET_SIZE_MAX;
|
|
|
|
|
bt->profile = BtProfileSerial;
|
|
|
|
|
bt->max_packet_size = BLE_PROFILE_SERIAL_PACKET_SIZE_MAX;
|
|
|
|
|
bt->current_profile = NULL;
|
|
|
|
|
// Load settings
|
|
|
|
|
bt_settings_load(&bt->bt_settings);
|
|
|
|
|
// Keys storage
|
|
|
|
|
@@ -128,18 +152,14 @@ Bt* bt_alloc() {
|
|
|
|
|
|
|
|
|
|
// Setup statusbar view port
|
|
|
|
|
bt->statusbar_view_port = bt_statusbar_view_port_alloc(bt);
|
|
|
|
|
// Pin code view port
|
|
|
|
|
bt->pin_code_view_port = bt_pin_code_view_port_alloc(bt);
|
|
|
|
|
// Notification
|
|
|
|
|
bt->notification = furi_record_open(RECORD_NOTIFICATION);
|
|
|
|
|
// Gui
|
|
|
|
|
bt->gui = furi_record_open(RECORD_GUI);
|
|
|
|
|
gui_add_view_port(bt->gui, bt->statusbar_view_port, GuiLayerStatusBarLeft);
|
|
|
|
|
gui_add_view_port(bt->gui, bt->pin_code_view_port, GuiLayerFullscreen);
|
|
|
|
|
|
|
|
|
|
// Dialogs
|
|
|
|
|
bt->dialogs = furi_record_open(RECORD_DIALOGS);
|
|
|
|
|
bt->dialog_message = dialog_message_alloc();
|
|
|
|
|
|
|
|
|
|
// Power
|
|
|
|
|
bt->power = furi_record_open(RECORD_POWER);
|
|
|
|
|
@@ -176,7 +196,11 @@ static uint16_t bt_serial_event_callback(SerialServiceEvent event, void* context
|
|
|
|
|
furi_event_flag_set(bt->rpc_event, BT_RPC_EVENT_BUFF_SENT);
|
|
|
|
|
} else if(event.event == SerialServiceEventTypesBleResetRequest) {
|
|
|
|
|
FURI_LOG_I(TAG, "BLE restart request received");
|
|
|
|
|
BtMessage message = {.type = BtMessageTypeSetProfile, .data.profile = BtProfileSerial};
|
|
|
|
|
BtMessage message = {
|
|
|
|
|
.type = BtMessageTypeSetProfile,
|
|
|
|
|
.data.profile.params = NULL,
|
|
|
|
|
.data.profile.template = ble_profile_serial,
|
|
|
|
|
};
|
|
|
|
|
furi_check(
|
|
|
|
|
furi_message_queue_put(bt->message_queue, &message, FuriWaitForever) == FuriStatusOk);
|
|
|
|
|
}
|
|
|
|
|
@@ -197,10 +221,10 @@ static void bt_rpc_send_bytes_callback(void* context, uint8_t* bytes, size_t byt
|
|
|
|
|
while(bytes_sent < bytes_len) {
|
|
|
|
|
size_t bytes_remain = bytes_len - bytes_sent;
|
|
|
|
|
if(bytes_remain > bt->max_packet_size) {
|
|
|
|
|
furi_hal_bt_serial_tx(&bytes[bytes_sent], bt->max_packet_size);
|
|
|
|
|
ble_profile_serial_tx(bt->current_profile, &bytes[bytes_sent], bt->max_packet_size);
|
|
|
|
|
bytes_sent += bt->max_packet_size;
|
|
|
|
|
} else {
|
|
|
|
|
furi_hal_bt_serial_tx(&bytes[bytes_sent], bytes_remain);
|
|
|
|
|
ble_profile_serial_tx(bt->current_profile, &bytes[bytes_sent], bytes_remain);
|
|
|
|
|
bytes_sent += bytes_remain;
|
|
|
|
|
}
|
|
|
|
|
// We want BT_RPC_EVENT_DISCONNECTED to stick, so don't clear
|
|
|
|
|
@@ -215,49 +239,55 @@ static void bt_rpc_send_bytes_callback(void* context, uint8_t* bytes, size_t byt
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void bt_serial_buffer_is_empty_callback(void* context) {
|
|
|
|
|
furi_assert(context);
|
|
|
|
|
Bt* bt = context;
|
|
|
|
|
furi_check(furi_hal_bt_check_profile_type(bt->current_profile, ble_profile_serial));
|
|
|
|
|
ble_profile_serial_notify_buffer_is_empty(bt->current_profile);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Called from GAP thread
|
|
|
|
|
static bool bt_on_gap_event_callback(GapEvent event, void* context) {
|
|
|
|
|
furi_assert(context);
|
|
|
|
|
Bt* bt = context;
|
|
|
|
|
bool ret = false;
|
|
|
|
|
bt->pin = 0;
|
|
|
|
|
bool do_update_status = false;
|
|
|
|
|
bool current_profile_is_serial =
|
|
|
|
|
furi_hal_bt_check_profile_type(bt->current_profile, ble_profile_serial);
|
|
|
|
|
|
|
|
|
|
if(event.type == GapEventTypeConnected) {
|
|
|
|
|
// Update status bar
|
|
|
|
|
bt->status = BtStatusConnected;
|
|
|
|
|
BtMessage message = {.type = BtMessageTypeUpdateStatus};
|
|
|
|
|
furi_check(
|
|
|
|
|
furi_message_queue_put(bt->message_queue, &message, FuriWaitForever) == FuriStatusOk);
|
|
|
|
|
do_update_status = true;
|
|
|
|
|
bt_open_rpc_connection(bt);
|
|
|
|
|
// Update battery level
|
|
|
|
|
PowerInfo info;
|
|
|
|
|
power_get_info(bt->power, &info);
|
|
|
|
|
BtMessage message = {.type = BtMessageTypeUpdateStatus};
|
|
|
|
|
message.type = BtMessageTypeUpdateBatteryLevel;
|
|
|
|
|
message.data.battery_level = info.charge;
|
|
|
|
|
furi_check(
|
|
|
|
|
furi_message_queue_put(bt->message_queue, &message, FuriWaitForever) == FuriStatusOk);
|
|
|
|
|
ret = true;
|
|
|
|
|
} else if(event.type == GapEventTypeDisconnected) {
|
|
|
|
|
if(bt->profile == BtProfileSerial && bt->rpc_session) {
|
|
|
|
|
if(current_profile_is_serial && bt->rpc_session) {
|
|
|
|
|
FURI_LOG_I(TAG, "Close RPC connection");
|
|
|
|
|
furi_hal_bt_serial_set_rpc_status(FuriHalBtSerialRpcStatusNotActive);
|
|
|
|
|
ble_profile_serial_set_rpc_active(
|
|
|
|
|
bt->current_profile, FuriHalBtSerialRpcStatusNotActive);
|
|
|
|
|
furi_event_flag_set(bt->rpc_event, BT_RPC_EVENT_DISCONNECTED);
|
|
|
|
|
rpc_session_close(bt->rpc_session);
|
|
|
|
|
furi_hal_bt_serial_set_event_callback(0, NULL, NULL);
|
|
|
|
|
ble_profile_serial_set_event_callback(bt->current_profile, 0, NULL, NULL);
|
|
|
|
|
bt->rpc_session = NULL;
|
|
|
|
|
}
|
|
|
|
|
ret = true;
|
|
|
|
|
} else if(event.type == GapEventTypeStartAdvertising) {
|
|
|
|
|
bt->status = BtStatusAdvertising;
|
|
|
|
|
BtMessage message = {.type = BtMessageTypeUpdateStatus};
|
|
|
|
|
furi_check(
|
|
|
|
|
furi_message_queue_put(bt->message_queue, &message, FuriWaitForever) == FuriStatusOk);
|
|
|
|
|
do_update_status = true;
|
|
|
|
|
ret = true;
|
|
|
|
|
} else if(event.type == GapEventTypeStopAdvertising) {
|
|
|
|
|
bt->status = BtStatusOff;
|
|
|
|
|
BtMessage message = {.type = BtMessageTypeUpdateStatus};
|
|
|
|
|
furi_check(
|
|
|
|
|
furi_message_queue_put(bt->message_queue, &message, FuriWaitForever) == FuriStatusOk);
|
|
|
|
|
do_update_status = true;
|
|
|
|
|
ret = true;
|
|
|
|
|
} else if(event.type == GapEventTypePinCodeShow) {
|
|
|
|
|
bt->pin = event.data.pin_code;
|
|
|
|
|
@@ -272,6 +302,20 @@ static bool bt_on_gap_event_callback(GapEvent event, void* context) {
|
|
|
|
|
} else if(event.type == GapEventTypeUpdateMTU) {
|
|
|
|
|
bt->max_packet_size = event.data.max_packet_size;
|
|
|
|
|
ret = true;
|
|
|
|
|
} else if(event.type == GapEventTypeBeaconStart) {
|
|
|
|
|
bt->beacon_active = true;
|
|
|
|
|
do_update_status = true;
|
|
|
|
|
ret = true;
|
|
|
|
|
} else if(event.type == GapEventTypeBeaconStop) {
|
|
|
|
|
bt->beacon_active = false;
|
|
|
|
|
do_update_status = true;
|
|
|
|
|
ret = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(do_update_status) {
|
|
|
|
|
BtMessage message = {.type = BtMessageTypeUpdateStatus};
|
|
|
|
|
furi_check(
|
|
|
|
|
furi_message_queue_put(bt->message_queue, &message, FuriWaitForever) == FuriStatusOk);
|
|
|
|
|
}
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
@@ -288,11 +332,18 @@ static void bt_on_key_storage_change_callback(uint8_t* addr, uint16_t size, void
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void bt_statusbar_update(Bt* bt) {
|
|
|
|
|
uint8_t active_icon_width = 0;
|
|
|
|
|
if(bt->beacon_active) {
|
|
|
|
|
active_icon_width = icon_get_width(&I_BLE_beacon_7x8) + ICON_SPACER;
|
|
|
|
|
}
|
|
|
|
|
if(bt->status == BtStatusAdvertising) {
|
|
|
|
|
view_port_set_width(bt->statusbar_view_port, icon_get_width(&I_Bluetooth_Idle_5x8));
|
|
|
|
|
view_port_enabled_set(bt->statusbar_view_port, true);
|
|
|
|
|
active_icon_width += icon_get_width(&I_Bluetooth_Idle_5x8);
|
|
|
|
|
} else if(bt->status == BtStatusConnected) {
|
|
|
|
|
view_port_set_width(bt->statusbar_view_port, icon_get_width(&I_Bluetooth_Connected_16x8));
|
|
|
|
|
active_icon_width += icon_get_width(&I_Bluetooth_Connected_16x8);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(active_icon_width > 0) {
|
|
|
|
|
view_port_set_width(bt->statusbar_view_port, active_icon_width);
|
|
|
|
|
view_port_enabled_set(bt->statusbar_view_port, true);
|
|
|
|
|
} else {
|
|
|
|
|
view_port_enabled_set(bt->statusbar_view_port, false);
|
|
|
|
|
@@ -300,6 +351,9 @@ static void bt_statusbar_update(Bt* bt) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void bt_show_warning(Bt* bt, const char* text) {
|
|
|
|
|
if(!bt->dialog_message) {
|
|
|
|
|
bt->dialog_message = dialog_message_alloc();
|
|
|
|
|
}
|
|
|
|
|
dialog_message_set_text(bt->dialog_message, text, 64, 28, AlignCenter, AlignCenter);
|
|
|
|
|
dialog_message_set_buttons(bt->dialog_message, "Quit", NULL, NULL);
|
|
|
|
|
dialog_message_show(bt->dialogs, bt->dialog_message);
|
|
|
|
|
@@ -309,18 +363,19 @@ void bt_open_rpc_connection(Bt* bt) {
|
|
|
|
|
if(!bt->rpc_session && bt->status == BtStatusConnected) {
|
|
|
|
|
// Clear BT_RPC_EVENT_DISCONNECTED because it might be set from previous session
|
|
|
|
|
furi_event_flag_clear(bt->rpc_event, BT_RPC_EVENT_DISCONNECTED);
|
|
|
|
|
if(bt->profile == BtProfileSerial) {
|
|
|
|
|
if(furi_hal_bt_check_profile_type(bt->current_profile, ble_profile_serial)) {
|
|
|
|
|
// Open RPC session
|
|
|
|
|
bt->rpc_session = rpc_session_open(bt->rpc, RpcOwnerBle);
|
|
|
|
|
if(bt->rpc_session) {
|
|
|
|
|
FURI_LOG_I(TAG, "Open RPC connection");
|
|
|
|
|
rpc_session_set_send_bytes_callback(bt->rpc_session, bt_rpc_send_bytes_callback);
|
|
|
|
|
rpc_session_set_buffer_is_empty_callback(
|
|
|
|
|
bt->rpc_session, furi_hal_bt_serial_notify_buffer_is_empty);
|
|
|
|
|
bt->rpc_session, bt_serial_buffer_is_empty_callback);
|
|
|
|
|
rpc_session_set_context(bt->rpc_session, bt);
|
|
|
|
|
furi_hal_bt_serial_set_event_callback(
|
|
|
|
|
RPC_BUFFER_SIZE, bt_serial_event_callback, bt);
|
|
|
|
|
furi_hal_bt_serial_set_rpc_status(FuriHalBtSerialRpcStatusActive);
|
|
|
|
|
ble_profile_serial_set_event_callback(
|
|
|
|
|
bt->current_profile, RPC_BUFFER_SIZE, bt_serial_event_callback, bt);
|
|
|
|
|
ble_profile_serial_set_rpc_active(
|
|
|
|
|
bt->current_profile, FuriHalBtSerialRpcStatusActive);
|
|
|
|
|
} else {
|
|
|
|
|
FURI_LOG_W(TAG, "RPC is busy, failed to open new session");
|
|
|
|
|
}
|
|
|
|
|
@@ -329,49 +384,51 @@ void bt_open_rpc_connection(Bt* bt) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void bt_close_rpc_connection(Bt* bt) {
|
|
|
|
|
if(bt->profile == BtProfileSerial && bt->rpc_session) {
|
|
|
|
|
if(furi_hal_bt_check_profile_type(bt->current_profile, ble_profile_serial) &&
|
|
|
|
|
bt->rpc_session) {
|
|
|
|
|
FURI_LOG_I(TAG, "Close RPC connection");
|
|
|
|
|
furi_event_flag_set(bt->rpc_event, BT_RPC_EVENT_DISCONNECTED);
|
|
|
|
|
rpc_session_close(bt->rpc_session);
|
|
|
|
|
furi_hal_bt_serial_set_event_callback(0, NULL, NULL);
|
|
|
|
|
ble_profile_serial_set_event_callback(bt->current_profile, 0, NULL, NULL);
|
|
|
|
|
bt->rpc_session = NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void bt_change_profile(Bt* bt, BtMessage* message) {
|
|
|
|
|
if(furi_hal_bt_is_ble_gatt_gap_supported()) {
|
|
|
|
|
if(furi_hal_bt_is_gatt_gap_supported()) {
|
|
|
|
|
bt_close_rpc_connection(bt);
|
|
|
|
|
|
|
|
|
|
FuriHalBtProfile furi_profile;
|
|
|
|
|
if(message->data.profile == BtProfileHidKeyboard) {
|
|
|
|
|
furi_profile = FuriHalBtProfileHidKeyboard;
|
|
|
|
|
} else {
|
|
|
|
|
furi_profile = FuriHalBtProfileSerial;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bt_keys_storage_load(bt->keys_storage);
|
|
|
|
|
|
|
|
|
|
if(furi_hal_bt_change_app(furi_profile, bt_on_gap_event_callback, bt)) {
|
|
|
|
|
bt->current_profile = furi_hal_bt_change_app(
|
|
|
|
|
message->data.profile.template,
|
|
|
|
|
message->data.profile.params,
|
|
|
|
|
bt_on_gap_event_callback,
|
|
|
|
|
bt);
|
|
|
|
|
if(bt->current_profile) {
|
|
|
|
|
FURI_LOG_I(TAG, "Bt App started");
|
|
|
|
|
if(bt->bt_settings.enabled) {
|
|
|
|
|
furi_hal_bt_start_advertising();
|
|
|
|
|
}
|
|
|
|
|
furi_hal_bt_set_key_storage_change_callback(bt_on_key_storage_change_callback, bt);
|
|
|
|
|
bt->profile = message->data.profile;
|
|
|
|
|
if(message->result) {
|
|
|
|
|
*message->result = true;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
FURI_LOG_E(TAG, "Failed to start Bt App");
|
|
|
|
|
if(message->result) {
|
|
|
|
|
*message->result = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if(message->profile_instance) {
|
|
|
|
|
*message->profile_instance = bt->current_profile;
|
|
|
|
|
}
|
|
|
|
|
if(message->result) {
|
|
|
|
|
*message->result = bt->current_profile != NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
bt_show_warning(bt, "Radio stack doesn't support this app");
|
|
|
|
|
if(message->result) {
|
|
|
|
|
*message->result = false;
|
|
|
|
|
}
|
|
|
|
|
if(message->profile_instance) {
|
|
|
|
|
*message->profile_instance = NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if(message->lock) api_lock_unlock(message->lock);
|
|
|
|
|
}
|
|
|
|
|
@@ -382,52 +439,6 @@ static void bt_close_connection(Bt* bt, BtMessage* message) {
|
|
|
|
|
if(message->lock) api_lock_unlock(message->lock);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline FuriHalBtProfile get_hal_bt_profile(BtProfile profile) {
|
|
|
|
|
if(profile == BtProfileHidKeyboard) {
|
|
|
|
|
return FuriHalBtProfileHidKeyboard;
|
|
|
|
|
} else {
|
|
|
|
|
return FuriHalBtProfileSerial;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void bt_restart(Bt* bt) {
|
|
|
|
|
furi_hal_bt_change_app(get_hal_bt_profile(bt->profile), bt_on_gap_event_callback, bt);
|
|
|
|
|
furi_hal_bt_start_advertising();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void bt_set_profile_adv_name(Bt* bt, const char* fmt, ...) {
|
|
|
|
|
furi_assert(bt);
|
|
|
|
|
furi_assert(fmt);
|
|
|
|
|
|
|
|
|
|
char name[FURI_HAL_BT_ADV_NAME_LENGTH];
|
|
|
|
|
va_list args;
|
|
|
|
|
va_start(args, fmt);
|
|
|
|
|
vsnprintf(name, sizeof(name), fmt, args);
|
|
|
|
|
va_end(args);
|
|
|
|
|
furi_hal_bt_set_profile_adv_name(get_hal_bt_profile(bt->profile), name);
|
|
|
|
|
|
|
|
|
|
bt_restart(bt);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const char* bt_get_profile_adv_name(Bt* bt) {
|
|
|
|
|
furi_assert(bt);
|
|
|
|
|
return furi_hal_bt_get_profile_adv_name(get_hal_bt_profile(bt->profile));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void bt_set_profile_mac_address(Bt* bt, const uint8_t mac[6]) {
|
|
|
|
|
furi_assert(bt);
|
|
|
|
|
furi_assert(mac);
|
|
|
|
|
|
|
|
|
|
furi_hal_bt_set_profile_mac_addr(get_hal_bt_profile(bt->profile), mac);
|
|
|
|
|
|
|
|
|
|
bt_restart(bt);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const uint8_t* bt_get_profile_mac_address(Bt* bt) {
|
|
|
|
|
furi_assert(bt);
|
|
|
|
|
return furi_hal_bt_get_profile_mac_addr(get_hal_bt_profile(bt->profile));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool bt_remote_rssi(Bt* bt, uint8_t* rssi) {
|
|
|
|
|
furi_assert(bt);
|
|
|
|
|
|
|
|
|
|
@@ -441,27 +452,6 @@ bool bt_remote_rssi(Bt* bt, uint8_t* rssi) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void bt_set_profile_pairing_method(Bt* bt, GapPairing pairing_method) {
|
|
|
|
|
furi_assert(bt);
|
|
|
|
|
furi_hal_bt_set_profile_pairing_method(get_hal_bt_profile(bt->profile), pairing_method);
|
|
|
|
|
bt_restart(bt);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
GapPairing bt_get_profile_pairing_method(Bt* bt) {
|
|
|
|
|
furi_assert(bt);
|
|
|
|
|
return furi_hal_bt_get_profile_pairing_method(get_hal_bt_profile(bt->profile));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void bt_disable_peer_key_update(Bt* bt) {
|
|
|
|
|
UNUSED(bt);
|
|
|
|
|
furi_hal_bt_set_key_storage_change_callback(NULL, NULL);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void bt_enable_peer_key_update(Bt* bt) {
|
|
|
|
|
furi_assert(bt);
|
|
|
|
|
furi_hal_bt_set_key_storage_change_callback(bt_on_key_storage_change_callback, bt);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int32_t bt_srv(void* p) {
|
|
|
|
|
UNUSED(p);
|
|
|
|
|
Bt* bt = bt_alloc();
|
|
|
|
|
@@ -483,8 +473,10 @@ int32_t bt_srv(void* p) {
|
|
|
|
|
FURI_LOG_E(TAG, "Radio stack start failed");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(furi_hal_bt_is_ble_gatt_gap_supported()) {
|
|
|
|
|
if(!furi_hal_bt_start_app(FuriHalBtProfileSerial, bt_on_gap_event_callback, bt)) {
|
|
|
|
|
if(furi_hal_bt_is_gatt_gap_supported()) {
|
|
|
|
|
bt->current_profile =
|
|
|
|
|
furi_hal_bt_start_app(ble_profile_serial, NULL, bt_on_gap_event_callback, bt);
|
|
|
|
|
if(!bt->current_profile) {
|
|
|
|
|
FURI_LOG_E(TAG, "BLE App start failed");
|
|
|
|
|
} else {
|
|
|
|
|
if(bt->bt_settings.enabled) {
|
|
|
|
|
@@ -514,7 +506,7 @@ int32_t bt_srv(void* p) {
|
|
|
|
|
// Update battery level
|
|
|
|
|
furi_hal_bt_update_battery_level(message.data.battery_level);
|
|
|
|
|
} else if(message.type == BtMessageTypeUpdatePowerState) {
|
|
|
|
|
furi_hal_bt_update_power_state();
|
|
|
|
|
furi_hal_bt_update_power_state(message.data.power_state_charging);
|
|
|
|
|
} else if(message.type == BtMessageTypePinCodeShow) {
|
|
|
|
|
// Display PIN code
|
|
|
|
|
bt_pin_code_show(bt, message.data.pin_code);
|
|
|
|
|
|