2021-09-01 23:22:40 +00:00
|
|
|
#include "bt_i.h"
|
2021-09-09 21:11:32 +00:00
|
|
|
#include "battery_service.h"
|
2021-11-04 17:26:41 +00:00
|
|
|
#include "bt_keys_storage.h"
|
2021-09-01 23:22:40 +00:00
|
|
|
|
2022-01-21 17:32:03 +00:00
|
|
|
#include <notification/notification_messages.h>
|
|
|
|
#include <gui/elements.h>
|
2022-10-28 14:08:50 +00:00
|
|
|
#include <assets_icons.h>
|
2021-12-15 17:39:06 +00:00
|
|
|
|
2021-11-12 13:04:35 +00:00
|
|
|
#define TAG "BtSrv"
|
2021-09-01 23:22:40 +00:00
|
|
|
|
2021-11-17 18:15:58 +00:00
|
|
|
#define BT_RPC_EVENT_BUFF_SENT (1UL << 0)
|
|
|
|
#define BT_RPC_EVENT_DISCONNECTED (1UL << 1)
|
|
|
|
#define BT_RPC_EVENT_ALL (BT_RPC_EVENT_BUFF_SENT | BT_RPC_EVENT_DISCONNECTED)
|
|
|
|
|
2021-09-01 23:22:40 +00:00
|
|
|
static void bt_draw_statusbar_callback(Canvas* canvas, void* context) {
|
2021-10-21 16:27:58 +00:00
|
|
|
furi_assert(context);
|
|
|
|
|
|
|
|
Bt* bt = context;
|
|
|
|
if(bt->status == BtStatusAdvertising) {
|
2022-02-09 11:57:44 +00:00
|
|
|
canvas_draw_icon(canvas, 0, 0, &I_Bluetooth_Idle_5x8);
|
2021-10-21 16:27:58 +00:00
|
|
|
} else if(bt->status == BtStatusConnected) {
|
2022-02-09 11:57:44 +00:00
|
|
|
canvas_draw_icon(canvas, 0, 0, &I_Bluetooth_Connected_16x8);
|
2021-10-21 16:27:58 +00:00
|
|
|
}
|
2021-09-01 23:22:40 +00:00
|
|
|
}
|
|
|
|
|
2021-10-21 16:27:58 +00:00
|
|
|
static ViewPort* bt_statusbar_view_port_alloc(Bt* bt) {
|
2021-09-01 23:22:40 +00:00
|
|
|
ViewPort* statusbar_view_port = view_port_alloc();
|
|
|
|
view_port_set_width(statusbar_view_port, 5);
|
2021-10-21 16:27:58 +00:00
|
|
|
view_port_draw_callback_set(statusbar_view_port, bt_draw_statusbar_callback, bt);
|
2021-09-01 23:22:40 +00:00
|
|
|
view_port_enabled_set(statusbar_view_port, false);
|
|
|
|
return statusbar_view_port;
|
|
|
|
}
|
|
|
|
|
2022-01-21 17:32:03 +00:00
|
|
|
static void bt_pin_code_view_port_draw_callback(Canvas* canvas, void* context) {
|
|
|
|
furi_assert(context);
|
|
|
|
Bt* bt = context;
|
|
|
|
char pin_code_info[24];
|
|
|
|
canvas_draw_icon(canvas, 0, 0, &I_BLE_Pairing_128x64);
|
2022-12-26 12:13:30 +00:00
|
|
|
snprintf(pin_code_info, sizeof(pin_code_info), "Pairing code\n%06lu", bt->pin_code);
|
2022-01-21 17:32:03 +00:00
|
|
|
elements_multiline_text_aligned(canvas, 64, 4, AlignCenter, AlignTop, pin_code_info);
|
|
|
|
elements_button_left(canvas, "Quit");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void bt_pin_code_view_port_input_callback(InputEvent* event, void* context) {
|
|
|
|
furi_assert(context);
|
|
|
|
Bt* bt = context;
|
|
|
|
if(event->type == InputTypeShort) {
|
|
|
|
if(event->key == InputKeyLeft || event->key == InputKeyBack) {
|
|
|
|
view_port_enabled_set(bt->pin_code_view_port, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static ViewPort* bt_pin_code_view_port_alloc(Bt* bt) {
|
|
|
|
ViewPort* view_port = view_port_alloc();
|
|
|
|
view_port_draw_callback_set(view_port, bt_pin_code_view_port_draw_callback, bt);
|
|
|
|
view_port_input_callback_set(view_port, bt_pin_code_view_port_input_callback, bt);
|
|
|
|
view_port_enabled_set(view_port, false);
|
|
|
|
return view_port;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void bt_pin_code_show(Bt* bt, uint32_t pin_code) {
|
|
|
|
bt->pin_code = pin_code;
|
2022-04-29 14:21:12 +00:00
|
|
|
notification_message(bt->notification, &sequence_display_backlight_on);
|
2022-01-21 17:32:03 +00:00
|
|
|
gui_view_port_send_to_front(bt->gui, bt->pin_code_view_port);
|
|
|
|
view_port_enabled_set(bt->pin_code_view_port, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void bt_pin_code_hide(Bt* bt) {
|
|
|
|
bt->pin_code = 0;
|
|
|
|
if(view_port_is_enabled(bt->pin_code_view_port)) {
|
|
|
|
view_port_enabled_set(bt->pin_code_view_port, false);
|
|
|
|
}
|
2021-09-13 11:25:37 +00:00
|
|
|
}
|
|
|
|
|
2021-12-08 11:28:01 +00:00
|
|
|
static bool bt_pin_code_verify_event_handler(Bt* bt, uint32_t pin) {
|
|
|
|
furi_assert(bt);
|
2022-04-29 14:21:12 +00:00
|
|
|
notification_message(bt->notification, &sequence_display_backlight_on);
|
2022-10-05 15:15:23 +00:00
|
|
|
FuriString* pin_str;
|
2021-12-08 11:28:01 +00:00
|
|
|
dialog_message_set_icon(bt->dialog_message, &I_BLE_Pairing_128x64, 0, 0);
|
2022-12-26 12:13:30 +00:00
|
|
|
pin_str = furi_string_alloc_printf("Verify code\n%06lu", pin);
|
2021-12-08 11:28:01 +00:00
|
|
|
dialog_message_set_text(
|
2022-10-05 15:15:23 +00:00
|
|
|
bt->dialog_message, furi_string_get_cstr(pin_str), 64, 4, AlignCenter, AlignTop);
|
2022-05-19 16:07:45 +00:00
|
|
|
dialog_message_set_buttons(bt->dialog_message, "Cancel", "OK", NULL);
|
2021-12-08 11:28:01 +00:00
|
|
|
DialogMessageButton button = dialog_message_show(bt->dialogs, bt->dialog_message);
|
2022-10-05 15:15:23 +00:00
|
|
|
furi_string_free(pin_str);
|
2021-12-08 11:28:01 +00:00
|
|
|
return button == DialogMessageButtonCenter;
|
|
|
|
}
|
|
|
|
|
2021-09-24 16:28:02 +00:00
|
|
|
static void bt_battery_level_changed_callback(const void* _event, void* context) {
|
|
|
|
furi_assert(_event);
|
|
|
|
furi_assert(context);
|
|
|
|
|
|
|
|
Bt* bt = context;
|
2022-05-24 13:42:02 +00:00
|
|
|
BtMessage message = {};
|
2021-09-24 16:28:02 +00:00
|
|
|
const PowerEvent* event = _event;
|
|
|
|
if(event->type == PowerEventTypeBatteryLevelChanged) {
|
2022-05-24 13:42:02 +00:00
|
|
|
message.type = BtMessageTypeUpdateBatteryLevel;
|
|
|
|
message.data.battery_level = event->data.battery_level;
|
2022-07-20 10:56:33 +00:00
|
|
|
furi_check(
|
|
|
|
furi_message_queue_put(bt->message_queue, &message, FuriWaitForever) == FuriStatusOk);
|
2022-05-24 13:42:02 +00:00
|
|
|
} else if(
|
|
|
|
event->type == PowerEventTypeStartCharging || event->type == PowerEventTypeFullyCharged ||
|
|
|
|
event->type == PowerEventTypeStopCharging) {
|
|
|
|
message.type = BtMessageTypeUpdatePowerState;
|
2022-07-20 10:56:33 +00:00
|
|
|
furi_check(
|
|
|
|
furi_message_queue_put(bt->message_queue, &message, FuriWaitForever) == FuriStatusOk);
|
2021-09-24 16:28:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-01 23:22:40 +00:00
|
|
|
Bt* bt_alloc() {
|
2022-02-18 19:53:46 +00:00
|
|
|
Bt* bt = malloc(sizeof(Bt));
|
2021-11-21 14:47:54 +00:00
|
|
|
// Init default maximum packet size
|
2021-12-08 11:28:01 +00:00
|
|
|
bt->max_packet_size = FURI_HAL_BT_SERIAL_PACKET_SIZE_MAX;
|
|
|
|
bt->profile = BtProfileSerial;
|
2021-09-01 23:22:40 +00:00
|
|
|
// Load settings
|
|
|
|
if(!bt_settings_load(&bt->bt_settings)) {
|
|
|
|
bt_settings_save(&bt->bt_settings);
|
|
|
|
}
|
2022-12-20 12:32:24 +00:00
|
|
|
// Keys storage
|
|
|
|
bt->keys_storage = bt_keys_storage_alloc(BT_KEYS_STORAGE_PATH);
|
2021-09-01 23:22:40 +00:00
|
|
|
// Alloc queue
|
2022-07-20 10:56:33 +00:00
|
|
|
bt->message_queue = furi_message_queue_alloc(8, sizeof(BtMessage));
|
2021-09-01 23:22:40 +00:00
|
|
|
|
|
|
|
// Setup statusbar view port
|
2021-10-21 16:27:58 +00:00
|
|
|
bt->statusbar_view_port = bt_statusbar_view_port_alloc(bt);
|
2022-01-21 17:32:03 +00:00
|
|
|
// Pin code view port
|
|
|
|
bt->pin_code_view_port = bt_pin_code_view_port_alloc(bt);
|
2021-12-15 17:39:06 +00:00
|
|
|
// Notification
|
2022-07-26 12:21:51 +00:00
|
|
|
bt->notification = furi_record_open(RECORD_NOTIFICATION);
|
2021-09-01 23:22:40 +00:00
|
|
|
// Gui
|
2022-07-26 12:21:51 +00:00
|
|
|
bt->gui = furi_record_open(RECORD_GUI);
|
2021-09-01 23:22:40 +00:00
|
|
|
gui_add_view_port(bt->gui, bt->statusbar_view_port, GuiLayerStatusBarLeft);
|
2022-01-21 17:32:03 +00:00
|
|
|
gui_add_view_port(bt->gui, bt->pin_code_view_port, GuiLayerFullscreen);
|
2021-09-01 23:22:40 +00:00
|
|
|
|
2021-09-13 11:25:37 +00:00
|
|
|
// Dialogs
|
2022-07-26 12:21:51 +00:00
|
|
|
bt->dialogs = furi_record_open(RECORD_DIALOGS);
|
2021-09-13 11:25:37 +00:00
|
|
|
bt->dialog_message = dialog_message_alloc();
|
2021-09-01 23:22:40 +00:00
|
|
|
|
2021-09-24 16:28:02 +00:00
|
|
|
// Power
|
2022-07-26 12:21:51 +00:00
|
|
|
bt->power = furi_record_open(RECORD_POWER);
|
2021-11-01 20:35:54 +00:00
|
|
|
FuriPubSub* power_pubsub = power_get_pubsub(bt->power);
|
|
|
|
furi_pubsub_subscribe(power_pubsub, bt_battery_level_changed_callback, bt);
|
2021-09-24 16:28:02 +00:00
|
|
|
|
2021-10-12 16:41:42 +00:00
|
|
|
// RPC
|
2022-07-26 12:21:51 +00:00
|
|
|
bt->rpc = furi_record_open(RECORD_RPC);
|
2022-07-20 10:56:33 +00:00
|
|
|
bt->rpc_event = furi_event_flag_alloc();
|
2021-10-12 16:41:42 +00:00
|
|
|
|
2021-12-08 11:28:01 +00:00
|
|
|
// API evnent
|
2022-07-20 10:56:33 +00:00
|
|
|
bt->api_event = furi_event_flag_alloc();
|
2021-12-08 11:28:01 +00:00
|
|
|
|
2021-09-13 11:25:37 +00:00
|
|
|
return bt;
|
2021-09-09 21:11:32 +00:00
|
|
|
}
|
|
|
|
|
2021-10-12 16:41:42 +00:00
|
|
|
// Called from GAP thread from Serial service
|
2021-12-08 11:28:01 +00:00
|
|
|
static uint16_t bt_serial_event_callback(SerialServiceEvent event, void* context) {
|
2021-10-12 16:41:42 +00:00
|
|
|
furi_assert(context);
|
|
|
|
Bt* bt = context;
|
2021-12-08 11:28:01 +00:00
|
|
|
uint16_t ret = 0;
|
2021-10-12 16:41:42 +00:00
|
|
|
|
2021-12-08 11:28:01 +00:00
|
|
|
if(event.event == SerialServiceEventTypeDataReceived) {
|
|
|
|
size_t bytes_processed =
|
|
|
|
rpc_session_feed(bt->rpc_session, event.data.buffer, event.data.size, 1000);
|
|
|
|
if(bytes_processed != event.data.size) {
|
|
|
|
FURI_LOG_E(
|
2022-12-26 12:13:30 +00:00
|
|
|
TAG, "Only %zu of %u bytes processed by RPC", bytes_processed, event.data.size);
|
2021-12-08 11:28:01 +00:00
|
|
|
}
|
|
|
|
ret = rpc_session_get_available_size(bt->rpc_session);
|
|
|
|
} else if(event.event == SerialServiceEventTypeDataSent) {
|
2022-07-20 10:56:33 +00:00
|
|
|
furi_event_flag_set(bt->rpc_event, BT_RPC_EVENT_BUFF_SENT);
|
2022-10-14 17:21:23 +00:00
|
|
|
} else if(event.event == SerialServiceEventTypesBleResetRequest) {
|
|
|
|
FURI_LOG_I(TAG, "BLE restart request received");
|
|
|
|
BtMessage message = {.type = BtMessageTypeSetProfile, .data.profile = BtProfileSerial};
|
|
|
|
furi_check(
|
|
|
|
furi_message_queue_put(bt->message_queue, &message, FuriWaitForever) == FuriStatusOk);
|
2021-10-12 16:41:42 +00:00
|
|
|
}
|
2021-12-08 11:28:01 +00:00
|
|
|
return ret;
|
2021-10-12 16:41:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Called from RPC thread
|
|
|
|
static void bt_rpc_send_bytes_callback(void* context, uint8_t* bytes, size_t bytes_len) {
|
|
|
|
furi_assert(context);
|
|
|
|
Bt* bt = context;
|
|
|
|
|
2022-07-20 10:56:33 +00:00
|
|
|
if(furi_event_flag_get(bt->rpc_event) & BT_RPC_EVENT_DISCONNECTED) {
|
2022-05-23 21:43:39 +00:00
|
|
|
// Early stop from sending if we're already disconnected
|
|
|
|
return;
|
|
|
|
}
|
2022-07-20 10:56:33 +00:00
|
|
|
furi_event_flag_clear(bt->rpc_event, BT_RPC_EVENT_ALL & (~BT_RPC_EVENT_DISCONNECTED));
|
2021-10-12 16:41:42 +00:00
|
|
|
size_t bytes_sent = 0;
|
|
|
|
while(bytes_sent < bytes_len) {
|
|
|
|
size_t bytes_remain = bytes_len - bytes_sent;
|
2021-12-08 13:59:40 +00:00
|
|
|
if(bytes_remain > bt->max_packet_size) {
|
|
|
|
furi_hal_bt_serial_tx(&bytes[bytes_sent], bt->max_packet_size);
|
|
|
|
bytes_sent += bt->max_packet_size;
|
2021-10-12 16:41:42 +00:00
|
|
|
} else {
|
2021-12-08 11:28:01 +00:00
|
|
|
furi_hal_bt_serial_tx(&bytes[bytes_sent], bytes_remain);
|
2021-10-12 16:41:42 +00:00
|
|
|
bytes_sent += bytes_remain;
|
|
|
|
}
|
2022-05-23 21:43:39 +00:00
|
|
|
// We want BT_RPC_EVENT_DISCONNECTED to stick, so don't clear
|
2022-07-20 10:56:33 +00:00
|
|
|
uint32_t event_flag = furi_event_flag_wait(
|
|
|
|
bt->rpc_event, BT_RPC_EVENT_ALL, FuriFlagWaitAny | FuriFlagNoClear, FuriWaitForever);
|
2021-11-17 18:15:58 +00:00
|
|
|
if(event_flag & BT_RPC_EVENT_DISCONNECTED) {
|
|
|
|
break;
|
2022-05-23 21:43:39 +00:00
|
|
|
} else {
|
|
|
|
// If we didn't get BT_RPC_EVENT_DISCONNECTED, then clear everything else
|
2022-07-20 10:56:33 +00:00
|
|
|
furi_event_flag_clear(bt->rpc_event, BT_RPC_EVENT_ALL & (~BT_RPC_EVENT_DISCONNECTED));
|
2021-11-17 18:15:58 +00:00
|
|
|
}
|
2021-10-12 16:41:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Called from GAP thread
|
2022-01-02 22:36:42 +00:00
|
|
|
static bool bt_on_gap_event_callback(GapEvent event, void* context) {
|
2021-10-12 16:41:42 +00:00
|
|
|
furi_assert(context);
|
|
|
|
Bt* bt = context;
|
2021-12-08 11:28:01 +00:00
|
|
|
bool ret = false;
|
2021-10-12 16:41:42 +00:00
|
|
|
|
2022-01-02 22:36:42 +00:00
|
|
|
if(event.type == GapEventTypeConnected) {
|
2021-10-21 16:27:58 +00:00
|
|
|
// Update status bar
|
|
|
|
bt->status = BtStatusConnected;
|
2022-01-21 17:32:03 +00:00
|
|
|
BtMessage message = {.type = BtMessageTypeUpdateStatus};
|
2022-07-20 10:56:33 +00:00
|
|
|
furi_check(
|
|
|
|
furi_message_queue_put(bt->message_queue, &message, FuriWaitForever) == FuriStatusOk);
|
2022-05-23 21:43:39 +00:00
|
|
|
// Clear BT_RPC_EVENT_DISCONNECTED because it might be set from previous session
|
2022-07-20 10:56:33 +00:00
|
|
|
furi_event_flag_clear(bt->rpc_event, BT_RPC_EVENT_DISCONNECTED);
|
2021-12-08 11:28:01 +00:00
|
|
|
if(bt->profile == BtProfileSerial) {
|
|
|
|
// Open RPC session
|
2023-04-19 08:30:26 +00:00
|
|
|
bt->rpc_session = rpc_session_open(bt->rpc, RpcOwnerBle);
|
2022-02-09 12:03:27 +00:00
|
|
|
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);
|
|
|
|
rpc_session_set_context(bt->rpc_session, bt);
|
|
|
|
furi_hal_bt_serial_set_event_callback(
|
|
|
|
RPC_BUFFER_SIZE, bt_serial_event_callback, bt);
|
2022-10-14 17:21:23 +00:00
|
|
|
furi_hal_bt_serial_set_rpc_status(FuriHalBtSerialRpcStatusActive);
|
2022-02-09 12:03:27 +00:00
|
|
|
} else {
|
|
|
|
FURI_LOG_W(TAG, "RPC is busy, failed to open new session");
|
|
|
|
}
|
2021-12-08 11:28:01 +00:00
|
|
|
}
|
2021-10-12 16:41:42 +00:00
|
|
|
// Update battery level
|
|
|
|
PowerInfo info;
|
|
|
|
power_get_info(bt->power, &info);
|
2021-10-21 16:27:58 +00:00
|
|
|
message.type = BtMessageTypeUpdateBatteryLevel;
|
|
|
|
message.data.battery_level = info.charge;
|
2022-07-20 10:56:33 +00:00
|
|
|
furi_check(
|
|
|
|
furi_message_queue_put(bt->message_queue, &message, FuriWaitForever) == FuriStatusOk);
|
2021-12-08 11:28:01 +00:00
|
|
|
ret = true;
|
2022-01-02 22:36:42 +00:00
|
|
|
} else if(event.type == GapEventTypeDisconnected) {
|
2021-12-08 11:28:01 +00:00
|
|
|
if(bt->profile == BtProfileSerial && bt->rpc_session) {
|
2021-11-17 18:15:58 +00:00
|
|
|
FURI_LOG_I(TAG, "Close RPC connection");
|
2022-10-14 17:21:23 +00:00
|
|
|
furi_hal_bt_serial_set_rpc_status(FuriHalBtSerialRpcStatusNotActive);
|
2022-07-20 10:56:33 +00:00
|
|
|
furi_event_flag_set(bt->rpc_event, BT_RPC_EVENT_DISCONNECTED);
|
2021-10-26 16:05:28 +00:00
|
|
|
rpc_session_close(bt->rpc_session);
|
2021-12-08 11:28:01 +00:00
|
|
|
furi_hal_bt_serial_set_event_callback(0, NULL, NULL);
|
2021-10-12 16:41:42 +00:00
|
|
|
bt->rpc_session = NULL;
|
|
|
|
}
|
2021-12-08 11:28:01 +00:00
|
|
|
ret = true;
|
2022-01-02 22:36:42 +00:00
|
|
|
} else if(event.type == GapEventTypeStartAdvertising) {
|
2021-10-21 16:27:58 +00:00
|
|
|
bt->status = BtStatusAdvertising;
|
2022-01-21 17:32:03 +00:00
|
|
|
BtMessage message = {.type = BtMessageTypeUpdateStatus};
|
2022-07-20 10:56:33 +00:00
|
|
|
furi_check(
|
|
|
|
furi_message_queue_put(bt->message_queue, &message, FuriWaitForever) == FuriStatusOk);
|
2021-12-08 11:28:01 +00:00
|
|
|
ret = true;
|
2022-01-02 22:36:42 +00:00
|
|
|
} else if(event.type == GapEventTypeStopAdvertising) {
|
2021-10-21 16:27:58 +00:00
|
|
|
bt->status = BtStatusOff;
|
2022-01-21 17:32:03 +00:00
|
|
|
BtMessage message = {.type = BtMessageTypeUpdateStatus};
|
2022-07-20 10:56:33 +00:00
|
|
|
furi_check(
|
|
|
|
furi_message_queue_put(bt->message_queue, &message, FuriWaitForever) == FuriStatusOk);
|
2021-12-08 11:28:01 +00:00
|
|
|
ret = true;
|
2022-01-02 22:36:42 +00:00
|
|
|
} else if(event.type == GapEventTypePinCodeShow) {
|
2021-10-12 16:41:42 +00:00
|
|
|
BtMessage message = {
|
|
|
|
.type = BtMessageTypePinCodeShow, .data.pin_code = event.data.pin_code};
|
2022-07-20 10:56:33 +00:00
|
|
|
furi_check(
|
|
|
|
furi_message_queue_put(bt->message_queue, &message, FuriWaitForever) == FuriStatusOk);
|
2021-12-08 11:28:01 +00:00
|
|
|
ret = true;
|
2022-01-02 22:36:42 +00:00
|
|
|
} else if(event.type == GapEventTypePinCodeVerify) {
|
2021-12-08 11:28:01 +00:00
|
|
|
ret = bt_pin_code_verify_event_handler(bt, event.data.pin_code);
|
2022-01-02 22:36:42 +00:00
|
|
|
} else if(event.type == GapEventTypeUpdateMTU) {
|
2021-11-21 14:47:54 +00:00
|
|
|
bt->max_packet_size = event.data.max_packet_size;
|
2021-12-08 11:28:01 +00:00
|
|
|
ret = true;
|
2021-10-12 16:41:42 +00:00
|
|
|
}
|
2021-12-08 11:28:01 +00:00
|
|
|
return ret;
|
2021-10-12 16:41:42 +00:00
|
|
|
}
|
|
|
|
|
2021-11-04 17:26:41 +00:00
|
|
|
static void bt_on_key_storage_change_callback(uint8_t* addr, uint16_t size, void* context) {
|
|
|
|
furi_assert(context);
|
|
|
|
Bt* bt = context;
|
2022-12-20 12:32:24 +00:00
|
|
|
BtMessage message = {
|
|
|
|
.type = BtMessageTypeKeysStorageUpdated,
|
|
|
|
.data.key_storage_data.start_address = addr,
|
|
|
|
.data.key_storage_data.size = size};
|
2022-07-20 10:56:33 +00:00
|
|
|
furi_check(
|
|
|
|
furi_message_queue_put(bt->message_queue, &message, FuriWaitForever) == FuriStatusOk);
|
2021-11-04 17:26:41 +00:00
|
|
|
}
|
|
|
|
|
2021-10-21 16:27:58 +00:00
|
|
|
static void bt_statusbar_update(Bt* bt) {
|
|
|
|
if(bt->status == BtStatusAdvertising) {
|
2022-02-09 11:57:44 +00:00
|
|
|
view_port_set_width(bt->statusbar_view_port, icon_get_width(&I_Bluetooth_Idle_5x8));
|
2021-10-21 16:27:58 +00:00
|
|
|
view_port_enabled_set(bt->statusbar_view_port, true);
|
|
|
|
} else if(bt->status == BtStatusConnected) {
|
2022-02-09 11:57:44 +00:00
|
|
|
view_port_set_width(bt->statusbar_view_port, icon_get_width(&I_Bluetooth_Connected_16x8));
|
2021-10-21 16:27:58 +00:00
|
|
|
view_port_enabled_set(bt->statusbar_view_port, true);
|
|
|
|
} else {
|
|
|
|
view_port_enabled_set(bt->statusbar_view_port, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-02 22:36:42 +00:00
|
|
|
static void bt_show_warning(Bt* bt, const char* text) {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2022-06-09 09:07:42 +00:00
|
|
|
static void bt_close_rpc_connection(Bt* bt) {
|
|
|
|
if(bt->profile == BtProfileSerial && bt->rpc_session) {
|
|
|
|
FURI_LOG_I(TAG, "Close RPC connection");
|
2022-07-20 10:56:33 +00:00
|
|
|
furi_event_flag_set(bt->rpc_event, BT_RPC_EVENT_DISCONNECTED);
|
2022-06-09 09:07:42 +00:00
|
|
|
rpc_session_close(bt->rpc_session);
|
|
|
|
furi_hal_bt_serial_set_event_callback(0, NULL, NULL);
|
|
|
|
bt->rpc_session = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-08 11:28:01 +00:00
|
|
|
static void bt_change_profile(Bt* bt, BtMessage* message) {
|
2022-06-09 09:07:42 +00:00
|
|
|
if(furi_hal_bt_is_ble_gatt_gap_supported()) {
|
2022-01-02 22:36:42 +00:00
|
|
|
bt_settings_load(&bt->bt_settings);
|
2022-06-09 09:07:42 +00:00
|
|
|
bt_close_rpc_connection(bt);
|
2021-12-08 11:28:01 +00:00
|
|
|
|
2022-01-02 22:36:42 +00:00
|
|
|
FuriHalBtProfile furi_profile;
|
|
|
|
if(message->data.profile == BtProfileHidKeyboard) {
|
|
|
|
furi_profile = FuriHalBtProfileHidKeyboard;
|
|
|
|
} else {
|
|
|
|
furi_profile = FuriHalBtProfileSerial;
|
|
|
|
}
|
2021-12-08 11:28:01 +00:00
|
|
|
|
2022-12-20 12:32:24 +00:00
|
|
|
bt_keys_storage_load(bt->keys_storage);
|
|
|
|
|
2022-01-02 22:36:42 +00:00
|
|
|
if(furi_hal_bt_change_app(furi_profile, bt_on_gap_event_callback, bt)) {
|
|
|
|
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;
|
2022-10-14 17:21:23 +00:00
|
|
|
if(message->result) {
|
|
|
|
*message->result = true;
|
|
|
|
}
|
2022-01-02 22:36:42 +00:00
|
|
|
} else {
|
|
|
|
FURI_LOG_E(TAG, "Failed to start Bt App");
|
2022-10-14 17:21:23 +00:00
|
|
|
if(message->result) {
|
|
|
|
*message->result = false;
|
|
|
|
}
|
2021-12-08 11:28:01 +00:00
|
|
|
}
|
|
|
|
} else {
|
2022-01-02 22:36:42 +00:00
|
|
|
bt_show_warning(bt, "Radio stack doesn't support this app");
|
2022-10-14 17:21:23 +00:00
|
|
|
if(message->result) {
|
|
|
|
*message->result = false;
|
|
|
|
}
|
2021-12-08 11:28:01 +00:00
|
|
|
}
|
2022-07-20 10:56:33 +00:00
|
|
|
furi_event_flag_set(bt->api_event, BT_API_UNLOCK_EVENT);
|
2021-12-08 11:28:01 +00:00
|
|
|
}
|
|
|
|
|
2022-06-09 09:07:42 +00:00
|
|
|
static void bt_close_connection(Bt* bt) {
|
|
|
|
bt_close_rpc_connection(bt);
|
2022-12-20 12:32:24 +00:00
|
|
|
furi_hal_bt_stop_advertising();
|
2022-07-20 10:56:33 +00:00
|
|
|
furi_event_flag_set(bt->api_event, BT_API_UNLOCK_EVENT);
|
2022-06-09 09:07:42 +00:00
|
|
|
}
|
|
|
|
|
2022-08-03 15:47:10 +00:00
|
|
|
int32_t bt_srv(void* p) {
|
|
|
|
UNUSED(p);
|
2021-09-01 23:22:40 +00:00
|
|
|
Bt* bt = bt_alloc();
|
|
|
|
|
2022-04-27 15:53:48 +00:00
|
|
|
if(furi_hal_rtc_get_boot_mode() != FuriHalRtcBootModeNormal) {
|
2023-02-26 15:15:26 +00:00
|
|
|
FURI_LOG_W(TAG, "Skipping start in special boot mode");
|
2022-04-27 15:53:48 +00:00
|
|
|
ble_glue_wait_for_c2_start(FURI_HAL_BT_C2_START_TIMEOUT);
|
2022-07-26 12:21:51 +00:00
|
|
|
furi_record_create(RECORD_BT, bt);
|
2022-04-27 15:53:48 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-12-20 12:32:24 +00:00
|
|
|
// Load keys
|
|
|
|
if(!bt_keys_storage_load(bt->keys_storage)) {
|
2021-12-08 11:28:01 +00:00
|
|
|
FURI_LOG_W(TAG, "Failed to load bonding keys");
|
2021-11-04 17:26:41 +00:00
|
|
|
}
|
2021-12-08 11:28:01 +00:00
|
|
|
|
2022-01-02 22:36:42 +00:00
|
|
|
// Start radio stack
|
|
|
|
if(!furi_hal_bt_start_radio_stack()) {
|
|
|
|
FURI_LOG_E(TAG, "Radio stack start failed");
|
|
|
|
}
|
|
|
|
|
2022-06-09 09:07:42 +00:00
|
|
|
if(furi_hal_bt_is_ble_gatt_gap_supported()) {
|
2022-01-02 22:36:42 +00:00
|
|
|
if(!furi_hal_bt_start_app(FuriHalBtProfileSerial, bt_on_gap_event_callback, bt)) {
|
|
|
|
FURI_LOG_E(TAG, "BLE App start failed");
|
|
|
|
} else {
|
|
|
|
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);
|
2021-09-01 23:22:40 +00:00
|
|
|
}
|
2022-06-09 09:07:42 +00:00
|
|
|
} else {
|
|
|
|
bt_show_warning(bt, "Unsupported radio stack");
|
|
|
|
bt->status = BtStatusUnavailable;
|
2021-09-01 23:22:40 +00:00
|
|
|
}
|
2021-11-04 17:26:41 +00:00
|
|
|
|
2022-07-26 12:21:51 +00:00
|
|
|
furi_record_create(RECORD_BT, bt);
|
2021-09-01 23:22:40 +00:00
|
|
|
|
|
|
|
BtMessage message;
|
|
|
|
while(1) {
|
2022-07-20 10:56:33 +00:00
|
|
|
furi_check(
|
|
|
|
furi_message_queue_get(bt->message_queue, &message, FuriWaitForever) == FuriStatusOk);
|
2022-01-21 17:32:03 +00:00
|
|
|
if(message.type == BtMessageTypeUpdateStatus) {
|
|
|
|
// Update view ports
|
2021-10-21 16:27:58 +00:00
|
|
|
bt_statusbar_update(bt);
|
2022-01-21 17:32:03 +00:00
|
|
|
bt_pin_code_hide(bt);
|
2021-12-15 17:39:06 +00:00
|
|
|
if(bt->status_changed_cb) {
|
|
|
|
bt->status_changed_cb(bt->status, bt->status_changed_ctx);
|
|
|
|
}
|
2021-09-09 21:11:32 +00:00
|
|
|
} else if(message.type == BtMessageTypeUpdateBatteryLevel) {
|
2021-09-13 11:25:37 +00:00
|
|
|
// Update battery level
|
2021-12-08 11:28:01 +00:00
|
|
|
furi_hal_bt_update_battery_level(message.data.battery_level);
|
2022-05-24 13:42:02 +00:00
|
|
|
} else if(message.type == BtMessageTypeUpdatePowerState) {
|
|
|
|
furi_hal_bt_update_power_state();
|
2021-09-13 11:25:37 +00:00
|
|
|
} else if(message.type == BtMessageTypePinCodeShow) {
|
|
|
|
// Display PIN code
|
2022-01-21 17:32:03 +00:00
|
|
|
bt_pin_code_show(bt, message.data.pin_code);
|
2021-11-04 17:26:41 +00:00
|
|
|
} else if(message.type == BtMessageTypeKeysStorageUpdated) {
|
2022-12-20 12:32:24 +00:00
|
|
|
bt_keys_storage_update(
|
|
|
|
bt->keys_storage,
|
|
|
|
message.data.key_storage_data.start_address,
|
|
|
|
message.data.key_storage_data.size);
|
2021-12-08 11:28:01 +00:00
|
|
|
} else if(message.type == BtMessageTypeSetProfile) {
|
|
|
|
bt_change_profile(bt, &message);
|
2022-06-09 09:07:42 +00:00
|
|
|
} else if(message.type == BtMessageTypeDisconnect) {
|
|
|
|
bt_close_connection(bt);
|
2022-01-21 17:32:03 +00:00
|
|
|
} else if(message.type == BtMessageTypeForgetBondedDevices) {
|
2022-12-20 12:32:24 +00:00
|
|
|
bt_keys_storage_delete(bt->keys_storage);
|
2021-09-01 23:22:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|