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
|
|
|
|
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) {
|
|
|
|
canvas_draw_icon(canvas, 0, 0, &I_Bluetooth_5x8);
|
|
|
|
} else if(bt->status == BtStatusConnected) {
|
|
|
|
canvas_draw_icon(canvas, 0, 0, &I_BT_Pair_9x8);
|
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
2021-09-13 11:25:37 +00:00
|
|
|
static void bt_pin_code_show_event_handler(Bt* bt, uint32_t pin) {
|
|
|
|
furi_assert(bt);
|
|
|
|
string_t pin_str;
|
2021-10-21 16:27:58 +00:00
|
|
|
dialog_message_set_icon(bt->dialog_message, &I_BLE_Pairing_128x64, 0, 0);
|
|
|
|
string_init_printf(pin_str, "Pairing code\n%06d", pin);
|
2021-09-13 11:25:37 +00:00
|
|
|
dialog_message_set_text(
|
2021-10-21 16:27:58 +00:00
|
|
|
bt->dialog_message, string_get_cstr(pin_str), 64, 4, AlignCenter, AlignTop);
|
|
|
|
dialog_message_set_buttons(bt->dialog_message, "Quit", NULL, NULL);
|
2021-09-13 11:25:37 +00:00
|
|
|
dialog_message_show(bt->dialogs, bt->dialog_message);
|
|
|
|
string_clear(pin_str);
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
const PowerEvent* event = _event;
|
|
|
|
if(event->type == PowerEventTypeBatteryLevelChanged) {
|
2021-10-12 16:41:42 +00:00
|
|
|
BtMessage message = {
|
|
|
|
.type = BtMessageTypeUpdateBatteryLevel,
|
|
|
|
.data.battery_level = event->data.battery_level};
|
|
|
|
furi_check(osMessageQueuePut(bt->message_queue, &message, 0, osWaitForever) == osOK);
|
2021-09-24 16:28:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-01 23:22:40 +00:00
|
|
|
Bt* bt_alloc() {
|
|
|
|
Bt* bt = furi_alloc(sizeof(Bt));
|
2021-11-21 14:47:54 +00:00
|
|
|
// Init default maximum packet size
|
|
|
|
bt->max_packet_size = FURI_HAL_BT_PACKET_SIZE_MAX;
|
2021-09-01 23:22:40 +00:00
|
|
|
// Load settings
|
|
|
|
if(!bt_settings_load(&bt->bt_settings)) {
|
|
|
|
bt_settings_save(&bt->bt_settings);
|
|
|
|
}
|
|
|
|
// Alloc queue
|
|
|
|
bt->message_queue = osMessageQueueNew(8, sizeof(BtMessage), NULL);
|
|
|
|
|
|
|
|
// Setup statusbar view port
|
2021-10-21 16:27:58 +00:00
|
|
|
bt->statusbar_view_port = bt_statusbar_view_port_alloc(bt);
|
2021-09-01 23:22:40 +00:00
|
|
|
// Gui
|
|
|
|
bt->gui = furi_record_open("gui");
|
|
|
|
gui_add_view_port(bt->gui, bt->statusbar_view_port, GuiLayerStatusBarLeft);
|
|
|
|
|
2021-09-13 11:25:37 +00:00
|
|
|
// Dialogs
|
|
|
|
bt->dialogs = furi_record_open("dialogs");
|
|
|
|
bt->dialog_message = dialog_message_alloc();
|
2021-09-01 23:22:40 +00:00
|
|
|
|
2021-09-24 16:28:02 +00:00
|
|
|
// Power
|
|
|
|
bt->power = furi_record_open("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
|
|
|
|
bt->rpc = furi_record_open("rpc");
|
2021-11-17 18:15:58 +00:00
|
|
|
bt->rpc_event = osEventFlagsNew(NULL);
|
2021-10-12 16:41:42 +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-11-08 19:41:40 +00:00
|
|
|
static uint16_t bt_on_data_received_callback(uint8_t* data, uint16_t size, void* context) {
|
2021-10-12 16:41:42 +00:00
|
|
|
furi_assert(context);
|
|
|
|
Bt* bt = context;
|
|
|
|
|
2021-10-26 16:05:28 +00:00
|
|
|
size_t bytes_processed = rpc_session_feed(bt->rpc_session, data, size, 1000);
|
2021-10-12 16:41:42 +00:00
|
|
|
if(bytes_processed != size) {
|
2021-11-12 13:04:35 +00:00
|
|
|
FURI_LOG_E(TAG, "Only %d of %d bytes processed by RPC", bytes_processed, size);
|
2021-10-12 16:41:42 +00:00
|
|
|
}
|
2021-11-08 19:41:40 +00:00
|
|
|
return rpc_session_get_available_size(bt->rpc_session);
|
2021-10-12 16:41:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Called from GAP thread from Serial service
|
|
|
|
static void bt_on_data_sent_callback(void* context) {
|
|
|
|
furi_assert(context);
|
|
|
|
Bt* bt = context;
|
|
|
|
|
2021-11-17 18:15:58 +00:00
|
|
|
osEventFlagsSet(bt->rpc_event, BT_RPC_EVENT_BUFF_SENT);
|
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;
|
|
|
|
|
2021-11-17 18:15:58 +00:00
|
|
|
osEventFlagsClear(bt->rpc_event, BT_RPC_EVENT_ALL);
|
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-11-21 14:47:54 +00:00
|
|
|
if(bytes_remain > bt->max_packet_size) {
|
|
|
|
furi_hal_bt_tx(&bytes[bytes_sent], bt->max_packet_size);
|
|
|
|
bytes_sent += bt->max_packet_size;
|
2021-10-12 16:41:42 +00:00
|
|
|
} else {
|
|
|
|
furi_hal_bt_tx(&bytes[bytes_sent], bytes_remain);
|
|
|
|
bytes_sent += bytes_remain;
|
|
|
|
}
|
2021-11-17 18:15:58 +00:00
|
|
|
uint32_t event_flag =
|
|
|
|
osEventFlagsWait(bt->rpc_event, BT_RPC_EVENT_ALL, osFlagsWaitAny, osWaitForever);
|
|
|
|
if(event_flag & BT_RPC_EVENT_DISCONNECTED) {
|
|
|
|
break;
|
|
|
|
}
|
2021-10-12 16:41:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-08 19:41:40 +00:00
|
|
|
static void bt_rpc_buffer_is_empty_callback(void* context) {
|
|
|
|
furi_assert(context);
|
|
|
|
furi_hal_bt_notify_buffer_is_empty();
|
|
|
|
}
|
|
|
|
|
2021-10-12 16:41:42 +00:00
|
|
|
// Called from GAP thread
|
|
|
|
static void bt_on_gap_event_callback(BleEvent event, void* context) {
|
|
|
|
furi_assert(context);
|
|
|
|
Bt* bt = context;
|
|
|
|
|
|
|
|
if(event.type == BleEventTypeConnected) {
|
2021-10-21 16:27:58 +00:00
|
|
|
// Update status bar
|
|
|
|
bt->status = BtStatusConnected;
|
|
|
|
BtMessage message = {.type = BtMessageTypeUpdateStatusbar};
|
|
|
|
furi_check(osMessageQueuePut(bt->message_queue, &message, 0, osWaitForever) == osOK);
|
|
|
|
// Open RPC session
|
2021-11-12 13:04:35 +00:00
|
|
|
FURI_LOG_I(TAG, "Open RPC connection");
|
2021-10-26 16:05:28 +00:00
|
|
|
bt->rpc_session = rpc_session_open(bt->rpc);
|
|
|
|
rpc_session_set_send_bytes_callback(bt->rpc_session, bt_rpc_send_bytes_callback);
|
2021-11-08 19:41:40 +00:00
|
|
|
rpc_session_set_buffer_is_empty_callback(bt->rpc_session, bt_rpc_buffer_is_empty_callback);
|
2021-10-26 16:05:28 +00:00
|
|
|
rpc_session_set_context(bt->rpc_session, bt);
|
2021-10-12 16:41:42 +00:00
|
|
|
furi_hal_bt_set_data_event_callbacks(
|
2021-11-08 19:41:40 +00:00
|
|
|
RPC_BUFFER_SIZE, bt_on_data_received_callback, bt_on_data_sent_callback, bt);
|
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;
|
2021-10-12 16:41:42 +00:00
|
|
|
furi_check(osMessageQueuePut(bt->message_queue, &message, 0, osWaitForever) == osOK);
|
|
|
|
} else if(event.type == BleEventTypeDisconnected) {
|
|
|
|
if(bt->rpc_session) {
|
2021-11-17 18:15:58 +00:00
|
|
|
FURI_LOG_I(TAG, "Close RPC connection");
|
|
|
|
osEventFlagsSet(bt->rpc_event, BT_RPC_EVENT_DISCONNECTED);
|
2021-10-26 16:05:28 +00:00
|
|
|
rpc_session_close(bt->rpc_session);
|
2021-11-17 18:15:58 +00:00
|
|
|
furi_hal_bt_set_data_event_callbacks(0, NULL, NULL, NULL);
|
2021-10-12 16:41:42 +00:00
|
|
|
bt->rpc_session = NULL;
|
|
|
|
}
|
2021-10-21 16:27:58 +00:00
|
|
|
} else if(event.type == BleEventTypeStartAdvertising) {
|
|
|
|
bt->status = BtStatusAdvertising;
|
|
|
|
BtMessage message = {.type = BtMessageTypeUpdateStatusbar};
|
|
|
|
furi_check(osMessageQueuePut(bt->message_queue, &message, 0, osWaitForever) == osOK);
|
|
|
|
} else if(event.type == BleEventTypeStopAdvertising) {
|
|
|
|
bt->status = BtStatusOff;
|
2021-10-12 16:41:42 +00:00
|
|
|
BtMessage message = {.type = BtMessageTypeUpdateStatusbar};
|
|
|
|
furi_check(osMessageQueuePut(bt->message_queue, &message, 0, osWaitForever) == osOK);
|
|
|
|
} else if(event.type == BleEventTypePinCodeShow) {
|
|
|
|
BtMessage message = {
|
|
|
|
.type = BtMessageTypePinCodeShow, .data.pin_code = event.data.pin_code};
|
|
|
|
furi_check(osMessageQueuePut(bt->message_queue, &message, 0, osWaitForever) == osOK);
|
2021-11-21 14:47:54 +00:00
|
|
|
} else if(event.type == BleEventTypeUpdateMTU) {
|
|
|
|
bt->max_packet_size = event.data.max_packet_size;
|
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;
|
2021-11-12 13:04:35 +00:00
|
|
|
FURI_LOG_I(TAG, "Changed addr start: %08lX, size changed: %d", addr, size);
|
2021-11-04 17:26:41 +00:00
|
|
|
BtMessage message = {.type = BtMessageTypeKeysStorageUpdated};
|
|
|
|
furi_check(osMessageQueuePut(bt->message_queue, &message, 0, osWaitForever) == osOK);
|
|
|
|
}
|
|
|
|
|
2021-10-21 16:27:58 +00:00
|
|
|
static void bt_statusbar_update(Bt* bt) {
|
|
|
|
if(bt->status == BtStatusAdvertising) {
|
|
|
|
view_port_set_width(bt->statusbar_view_port, icon_get_width(&I_Bluetooth_5x8));
|
|
|
|
view_port_enabled_set(bt->statusbar_view_port, true);
|
|
|
|
} else if(bt->status == BtStatusConnected) {
|
|
|
|
view_port_set_width(bt->statusbar_view_port, icon_get_width(&I_BT_Pair_9x8));
|
|
|
|
view_port_enabled_set(bt->statusbar_view_port, true);
|
|
|
|
} else {
|
|
|
|
view_port_enabled_set(bt->statusbar_view_port, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-01 23:22:40 +00:00
|
|
|
int32_t bt_srv() {
|
|
|
|
Bt* bt = bt_alloc();
|
|
|
|
furi_record_create("bt", bt);
|
|
|
|
|
2021-11-04 17:26:41 +00:00
|
|
|
// Read keys
|
|
|
|
if(!bt_load_key_storage(bt)) {
|
2021-11-12 13:04:35 +00:00
|
|
|
FURI_LOG_W(TAG, "Failed to load saved bonding keys");
|
2021-11-04 17:26:41 +00:00
|
|
|
}
|
|
|
|
// Start 2nd core
|
|
|
|
if(!furi_hal_bt_start_core2()) {
|
2021-11-12 13:04:35 +00:00
|
|
|
FURI_LOG_E(TAG, "Core2 startup failed");
|
2021-09-02 09:02:34 +00:00
|
|
|
} else {
|
|
|
|
view_port_enabled_set(bt->statusbar_view_port, true);
|
2021-10-12 16:41:42 +00:00
|
|
|
if(furi_hal_bt_init_app(bt_on_gap_event_callback, bt)) {
|
2021-11-12 13:04:35 +00:00
|
|
|
FURI_LOG_I(TAG, "BLE stack started");
|
2021-09-15 16:58:32 +00:00
|
|
|
if(bt->bt_settings.enabled) {
|
|
|
|
furi_hal_bt_start_advertising();
|
2021-09-01 23:22:40 +00:00
|
|
|
}
|
2021-09-15 16:58:32 +00:00
|
|
|
} else {
|
2021-11-12 13:04:35 +00:00
|
|
|
FURI_LOG_E(TAG, "BT App start failed");
|
2021-09-01 23:22:40 +00:00
|
|
|
}
|
|
|
|
}
|
2021-11-04 17:26:41 +00:00
|
|
|
furi_hal_bt_set_key_storage_change_callback(bt_on_key_storage_change_callback, bt);
|
|
|
|
|
2021-09-15 16:58:32 +00:00
|
|
|
// Update statusbar
|
2021-10-21 16:27:58 +00:00
|
|
|
bt_statusbar_update(bt);
|
2021-09-01 23:22:40 +00:00
|
|
|
|
|
|
|
BtMessage message;
|
|
|
|
while(1) {
|
|
|
|
furi_check(osMessageQueueGet(bt->message_queue, &message, NULL, osWaitForever) == osOK);
|
|
|
|
if(message.type == BtMessageTypeUpdateStatusbar) {
|
|
|
|
// Update statusbar
|
2021-10-21 16:27:58 +00:00
|
|
|
bt_statusbar_update(bt);
|
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-09-16 16:12:07 +00:00
|
|
|
if(furi_hal_bt_is_active()) {
|
2021-09-09 21:11:32 +00:00
|
|
|
battery_svc_update_level(message.data.battery_level);
|
|
|
|
}
|
2021-09-13 11:25:37 +00:00
|
|
|
} else if(message.type == BtMessageTypePinCodeShow) {
|
|
|
|
// Display PIN code
|
|
|
|
bt_pin_code_show_event_handler(bt, message.data.pin_code);
|
2021-11-04 17:26:41 +00:00
|
|
|
} else if(message.type == BtMessageTypeKeysStorageUpdated) {
|
|
|
|
bt_save_key_storage(bt);
|
2021-09-01 23:22:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|