2021-09-02 02:22:40 +03:00
|
|
|
#include "bt_i.h"
|
2021-09-10 00:11:32 +03:00
|
|
|
#include "battery_service.h"
|
2021-09-02 02:22:40 +03:00
|
|
|
|
|
|
|
#define BT_SERVICE_TAG "BT"
|
|
|
|
|
|
|
|
static void bt_draw_statusbar_callback(Canvas* canvas, void* context) {
|
|
|
|
canvas_draw_icon(canvas, 0, 0, &I_Bluetooth_5x8);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ViewPort* bt_statusbar_view_port_alloc() {
|
|
|
|
ViewPort* statusbar_view_port = view_port_alloc();
|
|
|
|
view_port_set_width(statusbar_view_port, 5);
|
|
|
|
view_port_draw_callback_set(statusbar_view_port, bt_draw_statusbar_callback, NULL);
|
|
|
|
view_port_enabled_set(statusbar_view_port, false);
|
|
|
|
return statusbar_view_port;
|
|
|
|
}
|
|
|
|
|
2021-09-13 14:25:37 +03:00
|
|
|
static void bt_pin_code_show_event_handler(Bt* bt, uint32_t pin) {
|
|
|
|
furi_assert(bt);
|
|
|
|
string_t pin_str;
|
|
|
|
string_init_printf(pin_str, "%06d", pin);
|
|
|
|
dialog_message_set_text(
|
|
|
|
bt->dialog_message, string_get_cstr(pin_str), 64, 32, AlignCenter, AlignCenter);
|
|
|
|
dialog_message_set_buttons(bt->dialog_message, "Back", NULL, NULL);
|
|
|
|
dialog_message_show(bt->dialogs, bt->dialog_message);
|
|
|
|
string_clear(pin_str);
|
|
|
|
}
|
|
|
|
|
2021-09-02 02:22:40 +03:00
|
|
|
Bt* bt_alloc() {
|
|
|
|
Bt* bt = furi_alloc(sizeof(Bt));
|
|
|
|
// 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
|
|
|
|
bt->statusbar_view_port = bt_statusbar_view_port_alloc();
|
|
|
|
// Gui
|
|
|
|
bt->gui = furi_record_open("gui");
|
|
|
|
gui_add_view_port(bt->gui, bt->statusbar_view_port, GuiLayerStatusBarLeft);
|
|
|
|
|
2021-09-13 14:25:37 +03:00
|
|
|
// Dialogs
|
|
|
|
bt->dialogs = furi_record_open("dialogs");
|
|
|
|
bt->dialog_message = dialog_message_alloc();
|
2021-09-02 02:22:40 +03:00
|
|
|
|
2021-09-13 14:25:37 +03:00
|
|
|
return bt;
|
2021-09-10 00:11:32 +03:00
|
|
|
}
|
|
|
|
|
2021-09-02 02:22:40 +03:00
|
|
|
int32_t bt_srv() {
|
|
|
|
Bt* bt = bt_alloc();
|
|
|
|
furi_record_create("bt", bt);
|
|
|
|
|
2021-09-02 12:02:34 +03:00
|
|
|
if(!furi_hal_bt_wait_startup()) {
|
|
|
|
FURI_LOG_E(BT_SERVICE_TAG, "Core2 startup failed");
|
|
|
|
} else {
|
|
|
|
view_port_enabled_set(bt->statusbar_view_port, true);
|
2021-09-15 19:58:32 +03:00
|
|
|
if(furi_hal_bt_init_app()) {
|
|
|
|
FURI_LOG_I(BT_SERVICE_TAG, "BLE stack started");
|
|
|
|
if(bt->bt_settings.enabled) {
|
|
|
|
furi_hal_bt_start_advertising();
|
|
|
|
FURI_LOG_I(BT_SERVICE_TAG, "Start advertising");
|
2021-09-02 02:22:40 +03:00
|
|
|
}
|
2021-09-15 19:58:32 +03:00
|
|
|
} else {
|
|
|
|
FURI_LOG_E(BT_SERVICE_TAG, "BT App start failed");
|
2021-09-02 02:22:40 +03:00
|
|
|
}
|
|
|
|
}
|
2021-09-15 19:58:32 +03:00
|
|
|
// Update statusbar
|
2021-09-16 19:12:07 +03:00
|
|
|
view_port_enabled_set(bt->statusbar_view_port, furi_hal_bt_is_active());
|
2021-09-02 02:22:40 +03:00
|
|
|
|
|
|
|
BtMessage message;
|
|
|
|
while(1) {
|
|
|
|
furi_check(osMessageQueueGet(bt->message_queue, &message, NULL, osWaitForever) == osOK);
|
|
|
|
if(message.type == BtMessageTypeUpdateStatusbar) {
|
|
|
|
// Update statusbar
|
2021-09-16 19:12:07 +03:00
|
|
|
view_port_enabled_set(bt->statusbar_view_port, furi_hal_bt_is_active());
|
2021-09-10 00:11:32 +03:00
|
|
|
} else if(message.type == BtMessageTypeUpdateBatteryLevel) {
|
2021-09-13 14:25:37 +03:00
|
|
|
// Update battery level
|
2021-09-16 19:12:07 +03:00
|
|
|
if(furi_hal_bt_is_active()) {
|
2021-09-10 00:11:32 +03:00
|
|
|
battery_svc_update_level(message.data.battery_level);
|
|
|
|
}
|
2021-09-13 14:25:37 +03:00
|
|
|
} else if(message.type == BtMessageTypePinCodeShow) {
|
|
|
|
// Display PIN code
|
|
|
|
bt_pin_code_show_event_handler(bt, message.data.pin_code);
|
2021-09-02 02:22:40 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|