[FL-2627] Flipper applications: SDK, build and debug system (#1387)
* Added support for running applications from SD card (FAPs - Flipper Application Packages) * Added plugin_dist target for fbt to build FAPs * All apps of type FlipperAppType.EXTERNAL and FlipperAppType.PLUGIN are built as FAPs by default * Updated VSCode configuration for new fbt features - re-deploy stock configuration to use them * Added debugging support for FAPs with fbt debug & VSCode * Added public firmware API with automated versioning Co-authored-by: hedger <hedger@users.noreply.github.com> Co-authored-by: SG <who.just.the.doctor@gmail.com> Co-authored-by: あく <alleteam@gmail.com>
This commit is contained in:
25
applications/services/bt/application.fam
Normal file
25
applications/services/bt/application.fam
Normal file
@@ -0,0 +1,25 @@
|
||||
App(
|
||||
appid="bt",
|
||||
name="BtSrv",
|
||||
apptype=FlipperAppType.SERVICE,
|
||||
entry_point="bt_srv",
|
||||
cdefines=["SRV_BT"],
|
||||
requires=[
|
||||
"cli",
|
||||
"dialogs",
|
||||
],
|
||||
provides=[
|
||||
"bt_start",
|
||||
"bt_settings",
|
||||
],
|
||||
stack_size=1 * 1024,
|
||||
order=20,
|
||||
sdk_headers=["bt_service/bt.h"],
|
||||
)
|
||||
|
||||
App(
|
||||
appid="bt_start",
|
||||
apptype=FlipperAppType.STARTUP,
|
||||
entry_point="bt_on_system_start",
|
||||
order=70,
|
||||
)
|
237
applications/services/bt/bt_cli.c
Normal file
237
applications/services/bt/bt_cli.c
Normal file
@@ -0,0 +1,237 @@
|
||||
#include <furi.h>
|
||||
#include <furi_hal.h>
|
||||
#include <cli/cli.h>
|
||||
#include <lib/toolbox/args.h>
|
||||
|
||||
#include <ble/ble.h>
|
||||
#include "bt_settings.h"
|
||||
#include "bt_service/bt.h"
|
||||
|
||||
static void bt_cli_command_hci_info(Cli* cli, string_t args, void* context) {
|
||||
UNUSED(cli);
|
||||
UNUSED(args);
|
||||
UNUSED(context);
|
||||
string_t buffer;
|
||||
string_init(buffer);
|
||||
furi_hal_bt_dump_state(buffer);
|
||||
printf("%s", string_get_cstr(buffer));
|
||||
string_clear(buffer);
|
||||
}
|
||||
|
||||
static void bt_cli_command_carrier_tx(Cli* cli, string_t args, void* context) {
|
||||
UNUSED(context);
|
||||
int channel = 0;
|
||||
int power = 0;
|
||||
|
||||
do {
|
||||
if(!args_read_int_and_trim(args, &channel) && (channel < 0 || channel > 39)) {
|
||||
printf("Incorrect or missing channel, expected int 0-39");
|
||||
break;
|
||||
}
|
||||
if(!args_read_int_and_trim(args, &power) && (power < 0 || power > 6)) {
|
||||
printf("Incorrect or missing power, expected int 0-6");
|
||||
break;
|
||||
}
|
||||
|
||||
Bt* bt = furi_record_open(RECORD_BT);
|
||||
bt_disconnect(bt);
|
||||
furi_hal_bt_reinit();
|
||||
printf("Transmitting carrier at %d channel at %d dB power\r\n", channel, power);
|
||||
printf("Press CTRL+C to stop\r\n");
|
||||
furi_hal_bt_start_tone_tx(channel, 0x19 + power);
|
||||
|
||||
while(!cli_cmd_interrupt_received(cli)) {
|
||||
furi_delay_ms(250);
|
||||
}
|
||||
furi_hal_bt_stop_tone_tx();
|
||||
|
||||
bt_set_profile(bt, BtProfileSerial);
|
||||
furi_record_close(RECORD_BT);
|
||||
} while(false);
|
||||
}
|
||||
|
||||
static void bt_cli_command_carrier_rx(Cli* cli, string_t args, void* context) {
|
||||
UNUSED(context);
|
||||
int channel = 0;
|
||||
|
||||
do {
|
||||
if(!args_read_int_and_trim(args, &channel) && (channel < 0 || channel > 39)) {
|
||||
printf("Incorrect or missing channel, expected int 0-39");
|
||||
break;
|
||||
}
|
||||
|
||||
Bt* bt = furi_record_open(RECORD_BT);
|
||||
bt_disconnect(bt);
|
||||
furi_hal_bt_reinit();
|
||||
printf("Receiving carrier at %d channel\r\n", channel);
|
||||
printf("Press CTRL+C to stop\r\n");
|
||||
|
||||
furi_hal_bt_start_packet_rx(channel, 1);
|
||||
|
||||
while(!cli_cmd_interrupt_received(cli)) {
|
||||
furi_delay_ms(250);
|
||||
printf("RSSI: %6.1f dB\r", (double)furi_hal_bt_get_rssi());
|
||||
fflush(stdout);
|
||||
}
|
||||
|
||||
furi_hal_bt_stop_packet_test();
|
||||
|
||||
bt_set_profile(bt, BtProfileSerial);
|
||||
furi_record_close(RECORD_BT);
|
||||
} while(false);
|
||||
}
|
||||
|
||||
static void bt_cli_command_packet_tx(Cli* cli, string_t args, void* context) {
|
||||
UNUSED(context);
|
||||
int channel = 0;
|
||||
int pattern = 0;
|
||||
int datarate = 1;
|
||||
|
||||
do {
|
||||
if(!args_read_int_and_trim(args, &channel) && (channel < 0 || channel > 39)) {
|
||||
printf("Incorrect or missing channel, expected int 0-39");
|
||||
break;
|
||||
}
|
||||
if(!args_read_int_and_trim(args, &pattern) && (pattern < 0 || pattern > 5)) {
|
||||
printf("Incorrect or missing pattern, expected int 0-5 \r\n");
|
||||
printf("0 - Pseudo-Random bit sequence 9\r\n");
|
||||
printf("1 - Pattern of alternating bits '11110000'\r\n");
|
||||
printf("2 - Pattern of alternating bits '10101010'\r\n");
|
||||
printf("3 - Pseudo-Random bit sequence 15\r\n");
|
||||
printf("4 - Pattern of All '1' bits\r\n");
|
||||
printf("5 - Pattern of All '0' bits\r\n");
|
||||
break;
|
||||
}
|
||||
if(!args_read_int_and_trim(args, &datarate) && (datarate < 1 || datarate > 2)) {
|
||||
printf("Incorrect or missing datarate, expected int 1-2");
|
||||
break;
|
||||
}
|
||||
|
||||
Bt* bt = furi_record_open(RECORD_BT);
|
||||
bt_disconnect(bt);
|
||||
furi_hal_bt_reinit();
|
||||
printf(
|
||||
"Transmitting %d pattern packet at %d channel at %d M datarate\r\n",
|
||||
pattern,
|
||||
channel,
|
||||
datarate);
|
||||
printf("Press CTRL+C to stop\r\n");
|
||||
furi_hal_bt_start_packet_tx(channel, pattern, datarate);
|
||||
|
||||
while(!cli_cmd_interrupt_received(cli)) {
|
||||
furi_delay_ms(250);
|
||||
}
|
||||
furi_hal_bt_stop_packet_test();
|
||||
printf("Transmitted %lu packets", furi_hal_bt_get_transmitted_packets());
|
||||
|
||||
bt_set_profile(bt, BtProfileSerial);
|
||||
furi_record_close(RECORD_BT);
|
||||
} while(false);
|
||||
}
|
||||
|
||||
static void bt_cli_command_packet_rx(Cli* cli, string_t args, void* context) {
|
||||
UNUSED(context);
|
||||
int channel = 0;
|
||||
int datarate = 1;
|
||||
|
||||
do {
|
||||
if(!args_read_int_and_trim(args, &channel) && (channel < 0 || channel > 39)) {
|
||||
printf("Incorrect or missing channel, expected int 0-39");
|
||||
break;
|
||||
}
|
||||
if(!args_read_int_and_trim(args, &datarate) && (datarate < 1 || datarate > 2)) {
|
||||
printf("Incorrect or missing datarate, expected int 1-2");
|
||||
break;
|
||||
}
|
||||
|
||||
Bt* bt = furi_record_open(RECORD_BT);
|
||||
bt_disconnect(bt);
|
||||
furi_hal_bt_reinit();
|
||||
printf("Receiving packets at %d channel at %d M datarate\r\n", channel, datarate);
|
||||
printf("Press CTRL+C to stop\r\n");
|
||||
furi_hal_bt_start_packet_rx(channel, datarate);
|
||||
|
||||
while(!cli_cmd_interrupt_received(cli)) {
|
||||
furi_delay_ms(250);
|
||||
printf("RSSI: %03.1f dB\r", (double)furi_hal_bt_get_rssi());
|
||||
fflush(stdout);
|
||||
}
|
||||
uint16_t packets_received = furi_hal_bt_stop_packet_test();
|
||||
printf("Received %hu packets", packets_received);
|
||||
|
||||
bt_set_profile(bt, BtProfileSerial);
|
||||
furi_record_close(RECORD_BT);
|
||||
} while(false);
|
||||
}
|
||||
|
||||
static void bt_cli_print_usage() {
|
||||
printf("Usage:\r\n");
|
||||
printf("bt <cmd> <args>\r\n");
|
||||
printf("Cmd list:\r\n");
|
||||
printf("\thci_info\t - HCI info\r\n");
|
||||
if(furi_hal_rtc_is_flag_set(FuriHalRtcFlagDebug) && furi_hal_bt_is_testing_supported()) {
|
||||
printf("\ttx_carrier <channel:0-39> <power:0-6>\t - start tx carrier test\r\n");
|
||||
printf("\trx_carrier <channel:0-39>\t - start rx carrier test\r\n");
|
||||
printf(
|
||||
"\ttx_packet <channel:0-39> <pattern:0-5> <datarate:1-2>\t - start tx packet test\r\n");
|
||||
printf("\trx_packet <channel:0-39> <datarate:1-2>\t - start rx packer test\r\n");
|
||||
}
|
||||
}
|
||||
|
||||
static void bt_cli(Cli* cli, string_t args, void* context) {
|
||||
UNUSED(context);
|
||||
furi_record_open(RECORD_BT);
|
||||
|
||||
string_t cmd;
|
||||
string_init(cmd);
|
||||
BtSettings bt_settings;
|
||||
bt_settings_load(&bt_settings);
|
||||
|
||||
do {
|
||||
if(!args_read_string_and_trim(args, cmd)) {
|
||||
bt_cli_print_usage();
|
||||
break;
|
||||
}
|
||||
if(string_cmp_str(cmd, "hci_info") == 0) {
|
||||
bt_cli_command_hci_info(cli, args, NULL);
|
||||
break;
|
||||
}
|
||||
if(furi_hal_rtc_is_flag_set(FuriHalRtcFlagDebug) && furi_hal_bt_is_testing_supported()) {
|
||||
if(string_cmp_str(cmd, "tx_carrier") == 0) {
|
||||
bt_cli_command_carrier_tx(cli, args, NULL);
|
||||
break;
|
||||
}
|
||||
if(string_cmp_str(cmd, "rx_carrier") == 0) {
|
||||
bt_cli_command_carrier_rx(cli, args, NULL);
|
||||
break;
|
||||
}
|
||||
if(string_cmp_str(cmd, "tx_packet") == 0) {
|
||||
bt_cli_command_packet_tx(cli, args, NULL);
|
||||
break;
|
||||
}
|
||||
if(string_cmp_str(cmd, "rx_packet") == 0) {
|
||||
bt_cli_command_packet_rx(cli, args, NULL);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
bt_cli_print_usage();
|
||||
} while(false);
|
||||
|
||||
if(bt_settings.enabled) {
|
||||
furi_hal_bt_start_advertising();
|
||||
}
|
||||
|
||||
string_clear(cmd);
|
||||
furi_record_close(RECORD_BT);
|
||||
}
|
||||
|
||||
void bt_on_system_start() {
|
||||
#ifdef SRV_CLI
|
||||
Cli* cli = furi_record_open(RECORD_CLI);
|
||||
cli_add_command(cli, RECORD_BT, CliCommandFlagDefault, bt_cli, NULL);
|
||||
furi_record_close(RECORD_CLI);
|
||||
#else
|
||||
UNUSED(bt_cli);
|
||||
#endif
|
||||
}
|
417
applications/services/bt/bt_service/bt.c
Normal file
417
applications/services/bt/bt_service/bt.c
Normal file
@@ -0,0 +1,417 @@
|
||||
#include "bt_i.h"
|
||||
#include "battery_service.h"
|
||||
#include "bt_keys_storage.h"
|
||||
|
||||
#include <notification/notification_messages.h>
|
||||
#include <gui/elements.h>
|
||||
|
||||
#define TAG "BtSrv"
|
||||
|
||||
#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)
|
||||
|
||||
static void bt_draw_statusbar_callback(Canvas* canvas, void* context) {
|
||||
furi_assert(context);
|
||||
|
||||
Bt* bt = context;
|
||||
if(bt->status == BtStatusAdvertising) {
|
||||
canvas_draw_icon(canvas, 0, 0, &I_Bluetooth_Idle_5x8);
|
||||
} else if(bt->status == BtStatusConnected) {
|
||||
canvas_draw_icon(canvas, 0, 0, &I_Bluetooth_Connected_16x8);
|
||||
}
|
||||
}
|
||||
|
||||
static ViewPort* bt_statusbar_view_port_alloc(Bt* bt) {
|
||||
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, bt);
|
||||
view_port_enabled_set(statusbar_view_port, false);
|
||||
return statusbar_view_port;
|
||||
}
|
||||
|
||||
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);
|
||||
snprintf(pin_code_info, sizeof(pin_code_info), "Pairing code\n%06ld", bt->pin_code);
|
||||
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;
|
||||
notification_message(bt->notification, &sequence_display_backlight_on);
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
static bool bt_pin_code_verify_event_handler(Bt* bt, uint32_t pin) {
|
||||
furi_assert(bt);
|
||||
notification_message(bt->notification, &sequence_display_backlight_on);
|
||||
string_t pin_str;
|
||||
dialog_message_set_icon(bt->dialog_message, &I_BLE_Pairing_128x64, 0, 0);
|
||||
string_init_printf(pin_str, "Verify code\n%06d", pin);
|
||||
dialog_message_set_text(
|
||||
bt->dialog_message, string_get_cstr(pin_str), 64, 4, AlignCenter, AlignTop);
|
||||
dialog_message_set_buttons(bt->dialog_message, "Cancel", "OK", NULL);
|
||||
DialogMessageButton button = dialog_message_show(bt->dialogs, bt->dialog_message);
|
||||
string_clear(pin_str);
|
||||
return button == DialogMessageButtonCenter;
|
||||
}
|
||||
|
||||
static void bt_battery_level_changed_callback(const void* _event, void* context) {
|
||||
furi_assert(_event);
|
||||
furi_assert(context);
|
||||
|
||||
Bt* bt = context;
|
||||
BtMessage message = {};
|
||||
const PowerEvent* event = _event;
|
||||
if(event->type == 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) {
|
||||
message.type = BtMessageTypeUpdatePowerState;
|
||||
furi_check(
|
||||
furi_message_queue_put(bt->message_queue, &message, FuriWaitForever) == FuriStatusOk);
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
// Load settings
|
||||
if(!bt_settings_load(&bt->bt_settings)) {
|
||||
bt_settings_save(&bt->bt_settings);
|
||||
}
|
||||
// Alloc queue
|
||||
bt->message_queue = furi_message_queue_alloc(8, sizeof(BtMessage));
|
||||
|
||||
// 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);
|
||||
FuriPubSub* power_pubsub = power_get_pubsub(bt->power);
|
||||
furi_pubsub_subscribe(power_pubsub, bt_battery_level_changed_callback, bt);
|
||||
|
||||
// RPC
|
||||
bt->rpc = furi_record_open(RECORD_RPC);
|
||||
bt->rpc_event = furi_event_flag_alloc();
|
||||
|
||||
// API evnent
|
||||
bt->api_event = furi_event_flag_alloc();
|
||||
|
||||
return bt;
|
||||
}
|
||||
|
||||
// Called from GAP thread from Serial service
|
||||
static uint16_t bt_serial_event_callback(SerialServiceEvent event, void* context) {
|
||||
furi_assert(context);
|
||||
Bt* bt = context;
|
||||
uint16_t ret = 0;
|
||||
|
||||
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(
|
||||
TAG, "Only %d of %d bytes processed by RPC", bytes_processed, event.data.size);
|
||||
}
|
||||
ret = rpc_session_get_available_size(bt->rpc_session);
|
||||
} else if(event.event == SerialServiceEventTypeDataSent) {
|
||||
furi_event_flag_set(bt->rpc_event, BT_RPC_EVENT_BUFF_SENT);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
// 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;
|
||||
|
||||
if(furi_event_flag_get(bt->rpc_event) & BT_RPC_EVENT_DISCONNECTED) {
|
||||
// Early stop from sending if we're already disconnected
|
||||
return;
|
||||
}
|
||||
furi_event_flag_clear(bt->rpc_event, BT_RPC_EVENT_ALL & (~BT_RPC_EVENT_DISCONNECTED));
|
||||
size_t bytes_sent = 0;
|
||||
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);
|
||||
bytes_sent += bt->max_packet_size;
|
||||
} else {
|
||||
furi_hal_bt_serial_tx(&bytes[bytes_sent], bytes_remain);
|
||||
bytes_sent += bytes_remain;
|
||||
}
|
||||
// We want BT_RPC_EVENT_DISCONNECTED to stick, so don't clear
|
||||
uint32_t event_flag = furi_event_flag_wait(
|
||||
bt->rpc_event, BT_RPC_EVENT_ALL, FuriFlagWaitAny | FuriFlagNoClear, FuriWaitForever);
|
||||
if(event_flag & BT_RPC_EVENT_DISCONNECTED) {
|
||||
break;
|
||||
} else {
|
||||
// If we didn't get BT_RPC_EVENT_DISCONNECTED, then clear everything else
|
||||
furi_event_flag_clear(bt->rpc_event, BT_RPC_EVENT_ALL & (~BT_RPC_EVENT_DISCONNECTED));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Called from GAP thread
|
||||
static bool bt_on_gap_event_callback(GapEvent event, void* context) {
|
||||
furi_assert(context);
|
||||
Bt* bt = context;
|
||||
bool ret = false;
|
||||
|
||||
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);
|
||||
// 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) {
|
||||
// Open RPC session
|
||||
bt->rpc_session = rpc_session_open(bt->rpc);
|
||||
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);
|
||||
} else {
|
||||
FURI_LOG_W(TAG, "RPC is busy, failed to open new session");
|
||||
}
|
||||
}
|
||||
// Update battery level
|
||||
PowerInfo info;
|
||||
power_get_info(bt->power, &info);
|
||||
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) {
|
||||
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);
|
||||
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);
|
||||
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);
|
||||
ret = true;
|
||||
} else if(event.type == GapEventTypePinCodeShow) {
|
||||
BtMessage message = {
|
||||
.type = BtMessageTypePinCodeShow, .data.pin_code = event.data.pin_code};
|
||||
furi_check(
|
||||
furi_message_queue_put(bt->message_queue, &message, FuriWaitForever) == FuriStatusOk);
|
||||
ret = true;
|
||||
} else if(event.type == GapEventTypePinCodeVerify) {
|
||||
ret = bt_pin_code_verify_event_handler(bt, event.data.pin_code);
|
||||
} else if(event.type == GapEventTypeUpdateMTU) {
|
||||
bt->max_packet_size = event.data.max_packet_size;
|
||||
ret = true;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
static void bt_on_key_storage_change_callback(uint8_t* addr, uint16_t size, void* context) {
|
||||
furi_assert(context);
|
||||
Bt* bt = context;
|
||||
FURI_LOG_I(TAG, "Changed addr start: %08lX, size changed: %d", addr, size);
|
||||
BtMessage message = {.type = BtMessageTypeKeysStorageUpdated};
|
||||
furi_check(
|
||||
furi_message_queue_put(bt->message_queue, &message, FuriWaitForever) == FuriStatusOk);
|
||||
}
|
||||
|
||||
static void bt_statusbar_update(Bt* bt) {
|
||||
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);
|
||||
} else if(bt->status == BtStatusConnected) {
|
||||
view_port_set_width(bt->statusbar_view_port, icon_get_width(&I_Bluetooth_Connected_16x8));
|
||||
view_port_enabled_set(bt->statusbar_view_port, true);
|
||||
} else {
|
||||
view_port_enabled_set(bt->statusbar_view_port, false);
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
static void bt_close_rpc_connection(Bt* bt) {
|
||||
if(bt->profile == BtProfileSerial && 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);
|
||||
bt->rpc_session = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
static void bt_change_profile(Bt* bt, BtMessage* message) {
|
||||
if(furi_hal_bt_is_ble_gatt_gap_supported()) {
|
||||
bt_settings_load(&bt->bt_settings);
|
||||
bt_close_rpc_connection(bt);
|
||||
|
||||
FuriHalBtProfile furi_profile;
|
||||
if(message->data.profile == BtProfileHidKeyboard) {
|
||||
furi_profile = FuriHalBtProfileHidKeyboard;
|
||||
} else {
|
||||
furi_profile = FuriHalBtProfileSerial;
|
||||
}
|
||||
|
||||
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;
|
||||
*message->result = true;
|
||||
} else {
|
||||
FURI_LOG_E(TAG, "Failed to start Bt App");
|
||||
*message->result = false;
|
||||
}
|
||||
} else {
|
||||
bt_show_warning(bt, "Radio stack doesn't support this app");
|
||||
*message->result = false;
|
||||
}
|
||||
furi_event_flag_set(bt->api_event, BT_API_UNLOCK_EVENT);
|
||||
}
|
||||
|
||||
static void bt_close_connection(Bt* bt) {
|
||||
bt_close_rpc_connection(bt);
|
||||
furi_event_flag_set(bt->api_event, BT_API_UNLOCK_EVENT);
|
||||
}
|
||||
|
||||
int32_t bt_srv(void* p) {
|
||||
UNUSED(p);
|
||||
Bt* bt = bt_alloc();
|
||||
|
||||
if(furi_hal_rtc_get_boot_mode() != FuriHalRtcBootModeNormal) {
|
||||
FURI_LOG_W(TAG, "Skipped BT init: device in special startup mode");
|
||||
ble_glue_wait_for_c2_start(FURI_HAL_BT_C2_START_TIMEOUT);
|
||||
furi_record_create(RECORD_BT, bt);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Read keys
|
||||
if(!bt_keys_storage_load(bt)) {
|
||||
FURI_LOG_W(TAG, "Failed to load bonding keys");
|
||||
}
|
||||
|
||||
// Start radio stack
|
||||
if(!furi_hal_bt_start_radio_stack()) {
|
||||
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)) {
|
||||
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);
|
||||
}
|
||||
} else {
|
||||
bt_show_warning(bt, "Unsupported radio stack");
|
||||
bt->status = BtStatusUnavailable;
|
||||
}
|
||||
|
||||
furi_record_create(RECORD_BT, bt);
|
||||
|
||||
BtMessage message;
|
||||
while(1) {
|
||||
furi_check(
|
||||
furi_message_queue_get(bt->message_queue, &message, FuriWaitForever) == FuriStatusOk);
|
||||
if(message.type == BtMessageTypeUpdateStatus) {
|
||||
// Update view ports
|
||||
bt_statusbar_update(bt);
|
||||
bt_pin_code_hide(bt);
|
||||
if(bt->status_changed_cb) {
|
||||
bt->status_changed_cb(bt->status, bt->status_changed_ctx);
|
||||
}
|
||||
} else if(message.type == BtMessageTypeUpdateBatteryLevel) {
|
||||
// Update battery level
|
||||
furi_hal_bt_update_battery_level(message.data.battery_level);
|
||||
} else if(message.type == BtMessageTypeUpdatePowerState) {
|
||||
furi_hal_bt_update_power_state();
|
||||
} else if(message.type == BtMessageTypePinCodeShow) {
|
||||
// Display PIN code
|
||||
bt_pin_code_show(bt, message.data.pin_code);
|
||||
} else if(message.type == BtMessageTypeKeysStorageUpdated) {
|
||||
bt_keys_storage_save(bt);
|
||||
} else if(message.type == BtMessageTypeSetProfile) {
|
||||
bt_change_profile(bt, &message);
|
||||
} else if(message.type == BtMessageTypeDisconnect) {
|
||||
bt_close_connection(bt);
|
||||
} else if(message.type == BtMessageTypeForgetBondedDevices) {
|
||||
bt_keys_storage_delete(bt);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
61
applications/services/bt/bt_service/bt.h
Normal file
61
applications/services/bt/bt_service/bt.h
Normal file
@@ -0,0 +1,61 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define RECORD_BT "bt"
|
||||
|
||||
typedef struct Bt Bt;
|
||||
|
||||
typedef enum {
|
||||
BtStatusUnavailable,
|
||||
BtStatusOff,
|
||||
BtStatusAdvertising,
|
||||
BtStatusConnected,
|
||||
} BtStatus;
|
||||
|
||||
typedef enum {
|
||||
BtProfileSerial,
|
||||
BtProfileHidKeyboard,
|
||||
} BtProfile;
|
||||
|
||||
typedef void (*BtStatusChangedCallback)(BtStatus status, void* context);
|
||||
|
||||
/** Change BLE Profile
|
||||
* @note Call of this function leads to 2nd core restart
|
||||
*
|
||||
* @param bt Bt instance
|
||||
* @param profile BtProfile
|
||||
*
|
||||
* @return true on success
|
||||
*/
|
||||
bool bt_set_profile(Bt* bt, BtProfile profile);
|
||||
|
||||
/** Disconnect from Central
|
||||
*
|
||||
* @param bt Bt instance
|
||||
*/
|
||||
void bt_disconnect(Bt* bt);
|
||||
|
||||
/** Set callback for Bluetooth status change notification
|
||||
*
|
||||
* @param bt Bt instance
|
||||
* @param callback BtStatusChangedCallback instance
|
||||
* @param context pointer to context
|
||||
*/
|
||||
void bt_set_status_changed_callback(Bt* bt, BtStatusChangedCallback callback, void* context);
|
||||
|
||||
/** Forget bonded devices
|
||||
* @note Leads to wipe ble key storage and deleting bt.keys
|
||||
*
|
||||
* @param bt Bt instance
|
||||
*/
|
||||
void bt_forget_bonded_devices(Bt* bt);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
41
applications/services/bt/bt_service/bt_api.c
Normal file
41
applications/services/bt/bt_service/bt_api.c
Normal file
@@ -0,0 +1,41 @@
|
||||
#include "bt_i.h"
|
||||
|
||||
bool bt_set_profile(Bt* bt, BtProfile profile) {
|
||||
furi_assert(bt);
|
||||
|
||||
// Send message
|
||||
bool result = false;
|
||||
BtMessage message = {
|
||||
.type = BtMessageTypeSetProfile, .data.profile = profile, .result = &result};
|
||||
furi_check(
|
||||
furi_message_queue_put(bt->message_queue, &message, FuriWaitForever) == FuriStatusOk);
|
||||
// Wait for unlock
|
||||
furi_event_flag_wait(bt->api_event, BT_API_UNLOCK_EVENT, FuriFlagWaitAny, FuriWaitForever);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void bt_disconnect(Bt* bt) {
|
||||
furi_assert(bt);
|
||||
|
||||
// Send message
|
||||
BtMessage message = {.type = BtMessageTypeDisconnect};
|
||||
furi_check(
|
||||
furi_message_queue_put(bt->message_queue, &message, FuriWaitForever) == FuriStatusOk);
|
||||
// Wait for unlock
|
||||
furi_event_flag_wait(bt->api_event, BT_API_UNLOCK_EVENT, FuriFlagWaitAny, FuriWaitForever);
|
||||
}
|
||||
|
||||
void bt_set_status_changed_callback(Bt* bt, BtStatusChangedCallback callback, void* context) {
|
||||
furi_assert(bt);
|
||||
|
||||
bt->status_changed_cb = callback;
|
||||
bt->status_changed_ctx = context;
|
||||
}
|
||||
|
||||
void bt_forget_bonded_devices(Bt* bt) {
|
||||
furi_assert(bt);
|
||||
BtMessage message = {.type = BtMessageTypeForgetBondedDevices};
|
||||
furi_check(
|
||||
furi_message_queue_put(bt->message_queue, &message, FuriWaitForever) == FuriStatusOk);
|
||||
}
|
66
applications/services/bt/bt_service/bt_i.h
Normal file
66
applications/services/bt/bt_service/bt_i.h
Normal file
@@ -0,0 +1,66 @@
|
||||
#pragma once
|
||||
|
||||
#include "bt.h"
|
||||
|
||||
#include <furi.h>
|
||||
#include <furi_hal.h>
|
||||
|
||||
#include <gui/gui.h>
|
||||
#include <gui/view_port.h>
|
||||
#include <gui/view.h>
|
||||
|
||||
#include <dialogs/dialogs.h>
|
||||
#include <power/power_service/power.h>
|
||||
#include <rpc/rpc.h>
|
||||
#include <notification/notification.h>
|
||||
|
||||
#include <bt/bt_settings.h>
|
||||
|
||||
#define BT_API_UNLOCK_EVENT (1UL << 0)
|
||||
|
||||
typedef enum {
|
||||
BtMessageTypeUpdateStatus,
|
||||
BtMessageTypeUpdateBatteryLevel,
|
||||
BtMessageTypeUpdatePowerState,
|
||||
BtMessageTypePinCodeShow,
|
||||
BtMessageTypeKeysStorageUpdated,
|
||||
BtMessageTypeSetProfile,
|
||||
BtMessageTypeDisconnect,
|
||||
BtMessageTypeForgetBondedDevices,
|
||||
} BtMessageType;
|
||||
|
||||
typedef union {
|
||||
uint32_t pin_code;
|
||||
uint8_t battery_level;
|
||||
BtProfile profile;
|
||||
} BtMessageData;
|
||||
|
||||
typedef struct {
|
||||
BtMessageType type;
|
||||
BtMessageData data;
|
||||
bool* result;
|
||||
} BtMessage;
|
||||
|
||||
struct Bt {
|
||||
uint8_t* bt_keys_addr_start;
|
||||
uint16_t bt_keys_size;
|
||||
uint16_t max_packet_size;
|
||||
BtSettings bt_settings;
|
||||
BtStatus status;
|
||||
BtProfile profile;
|
||||
FuriMessageQueue* message_queue;
|
||||
NotificationApp* notification;
|
||||
Gui* gui;
|
||||
ViewPort* statusbar_view_port;
|
||||
ViewPort* pin_code_view_port;
|
||||
uint32_t pin_code;
|
||||
DialogsApp* dialogs;
|
||||
DialogMessage* dialog_message;
|
||||
Power* power;
|
||||
Rpc* rpc;
|
||||
RpcSession* rpc_session;
|
||||
FuriEventFlag* rpc_event;
|
||||
FuriEventFlag* api_event;
|
||||
BtStatusChangedCallback status_changed_cb;
|
||||
void* status_changed_ctx;
|
||||
};
|
3
applications/services/bt/bt_service/bt_keys_filename.h
Normal file
3
applications/services/bt/bt_service/bt_keys_filename.h
Normal file
@@ -0,0 +1,3 @@
|
||||
#pragma once
|
||||
|
||||
#define BT_KEYS_STORAGE_FILE_NAME ".bt.keys"
|
57
applications/services/bt/bt_service/bt_keys_storage.c
Normal file
57
applications/services/bt/bt_service/bt_keys_storage.c
Normal file
@@ -0,0 +1,57 @@
|
||||
#include "bt_keys_storage.h"
|
||||
|
||||
#include <furi.h>
|
||||
#include <lib/toolbox/saved_struct.h>
|
||||
#include <storage/storage.h>
|
||||
|
||||
#define BT_KEYS_STORAGE_PATH INT_PATH(BT_KEYS_STORAGE_FILE_NAME)
|
||||
#define BT_KEYS_STORAGE_VERSION (0)
|
||||
#define BT_KEYS_STORAGE_MAGIC (0x18)
|
||||
|
||||
bool bt_keys_storage_load(Bt* bt) {
|
||||
furi_assert(bt);
|
||||
bool file_loaded = false;
|
||||
|
||||
furi_hal_bt_get_key_storage_buff(&bt->bt_keys_addr_start, &bt->bt_keys_size);
|
||||
furi_hal_bt_nvm_sram_sem_acquire();
|
||||
file_loaded = saved_struct_load(
|
||||
BT_KEYS_STORAGE_PATH,
|
||||
bt->bt_keys_addr_start,
|
||||
bt->bt_keys_size,
|
||||
BT_KEYS_STORAGE_MAGIC,
|
||||
BT_KEYS_STORAGE_VERSION);
|
||||
furi_hal_bt_nvm_sram_sem_release();
|
||||
|
||||
return file_loaded;
|
||||
}
|
||||
|
||||
bool bt_keys_storage_save(Bt* bt) {
|
||||
furi_assert(bt);
|
||||
furi_assert(bt->bt_keys_addr_start);
|
||||
bool file_saved = false;
|
||||
|
||||
furi_hal_bt_nvm_sram_sem_acquire();
|
||||
file_saved = saved_struct_save(
|
||||
BT_KEYS_STORAGE_PATH,
|
||||
bt->bt_keys_addr_start,
|
||||
bt->bt_keys_size,
|
||||
BT_KEYS_STORAGE_MAGIC,
|
||||
BT_KEYS_STORAGE_VERSION);
|
||||
furi_hal_bt_nvm_sram_sem_release();
|
||||
|
||||
return file_saved;
|
||||
}
|
||||
|
||||
bool bt_keys_storage_delete(Bt* bt) {
|
||||
furi_assert(bt);
|
||||
bool delete_succeed = false;
|
||||
bool bt_is_active = furi_hal_bt_is_active();
|
||||
|
||||
furi_hal_bt_stop_advertising();
|
||||
delete_succeed = furi_hal_bt_clear_white_list();
|
||||
if(bt_is_active) {
|
||||
furi_hal_bt_start_advertising();
|
||||
}
|
||||
|
||||
return delete_succeed;
|
||||
}
|
10
applications/services/bt/bt_service/bt_keys_storage.h
Normal file
10
applications/services/bt/bt_service/bt_keys_storage.h
Normal file
@@ -0,0 +1,10 @@
|
||||
#pragma once
|
||||
|
||||
#include "bt_i.h"
|
||||
#include "bt_keys_filename.h"
|
||||
|
||||
bool bt_keys_storage_load(Bt* bt);
|
||||
|
||||
bool bt_keys_storage_save(Bt* bt);
|
||||
|
||||
bool bt_keys_storage_delete(Bt* bt);
|
23
applications/services/bt/bt_settings.c
Normal file
23
applications/services/bt/bt_settings.c
Normal file
@@ -0,0 +1,23 @@
|
||||
#include "bt_settings.h"
|
||||
|
||||
#include <furi.h>
|
||||
#include <lib/toolbox/saved_struct.h>
|
||||
#include <storage/storage.h>
|
||||
|
||||
#define BT_SETTINGS_PATH INT_PATH(BT_SETTINGS_FILE_NAME)
|
||||
#define BT_SETTINGS_VERSION (0)
|
||||
#define BT_SETTINGS_MAGIC (0x19)
|
||||
|
||||
bool bt_settings_load(BtSettings* bt_settings) {
|
||||
furi_assert(bt_settings);
|
||||
|
||||
return saved_struct_load(
|
||||
BT_SETTINGS_PATH, bt_settings, sizeof(BtSettings), BT_SETTINGS_MAGIC, BT_SETTINGS_VERSION);
|
||||
}
|
||||
|
||||
bool bt_settings_save(BtSettings* bt_settings) {
|
||||
furi_assert(bt_settings);
|
||||
|
||||
return saved_struct_save(
|
||||
BT_SETTINGS_PATH, bt_settings, sizeof(BtSettings), BT_SETTINGS_MAGIC, BT_SETTINGS_VERSION);
|
||||
}
|
14
applications/services/bt/bt_settings.h
Normal file
14
applications/services/bt/bt_settings.h
Normal file
@@ -0,0 +1,14 @@
|
||||
#pragma once
|
||||
|
||||
#include "bt_settings_filename.h"
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
typedef struct {
|
||||
bool enabled;
|
||||
} BtSettings;
|
||||
|
||||
bool bt_settings_load(BtSettings* bt_settings);
|
||||
|
||||
bool bt_settings_save(BtSettings* bt_settings);
|
3
applications/services/bt/bt_settings_filename.h
Normal file
3
applications/services/bt/bt_settings_filename.h
Normal file
@@ -0,0 +1,3 @@
|
||||
#pragma once
|
||||
|
||||
#define BT_SETTINGS_FILE_NAME ".bt.settings"
|
Reference in New Issue
Block a user