unleashed-firmware/applications/bt/bt_service/bt.c
gornekich d3b58f732f
[FL-1783] Power service refactoring (#718)
* settings power: introduce power settings app
* power: move power API to separate file
* settings power: implement reboot scene
* settings power: add power off scene
* assets: add crying dolphin, fix Subghz assets names
* settings power: fix power off scene GUI
* settings power: add battery info scene
* power: add cli to application on start hook
* power: remove power from main menu
* power: move to power service folder
* power service: rework power off logic
* power: add pubsub events
* bt: subscribe to battery level change, update characteristic
* application: change order of Settings applications
* gui: add bubble element
* power: gui improvements
* application: rename Notification -> LCD and notifications
* Applications: menu order according to documentation and add missing power cli init
* settings power: add disconnect USB scene
* power cli: notify user to disconnect USB after poweroff
* Power: update poweroff message in cli

Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
2021-09-24 19:28:02 +03:00

105 lines
3.5 KiB
C
Executable file

#include "bt_i.h"
#include "battery_service.h"
#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;
}
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);
}
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) {
bt_update_battery_level(bt, event->data.battery_level);
}
}
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);
// Dialogs
bt->dialogs = furi_record_open("dialogs");
bt->dialog_message = dialog_message_alloc();
// Power
bt->power = furi_record_open("power");
PubSub* power_pubsub = power_get_pubsub(bt->power);
subscribe_pubsub(power_pubsub, bt_battery_level_changed_callback, bt);
return bt;
}
int32_t bt_srv() {
Bt* bt = bt_alloc();
furi_record_create("bt", bt);
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);
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");
}
} else {
FURI_LOG_E(BT_SERVICE_TAG, "BT App start failed");
}
}
// Update statusbar
view_port_enabled_set(bt->statusbar_view_port, furi_hal_bt_is_active());
BtMessage message;
while(1) {
furi_check(osMessageQueueGet(bt->message_queue, &message, NULL, osWaitForever) == osOK);
if(message.type == BtMessageTypeUpdateStatusbar) {
// Update statusbar
view_port_enabled_set(bt->statusbar_view_port, furi_hal_bt_is_active());
} else if(message.type == BtMessageTypeUpdateBatteryLevel) {
// Update battery level
if(furi_hal_bt_is_active()) {
battery_svc_update_level(message.data.battery_level);
}
} else if(message.type == BtMessageTypePinCodeShow) {
// Display PIN code
bt_pin_code_show_event_handler(bt, message.data.pin_code);
}
}
return 0;
}