This commit is contained in:
Willy-JL
2024-02-16 11:27:05 +00:00
91 changed files with 3578 additions and 2228 deletions

View File

@@ -0,0 +1,11 @@
App(
appid="example_ble_beacon",
name="Example: BLE Beacon",
apptype=FlipperAppType.EXTERNAL,
entry_point="ble_beacon_app",
requires=["gui"],
stack_size=1 * 1024,
fap_icon="example_ble_beacon_10px.png",
fap_category="Examples",
fap_icon_assets="images",
)

View File

@@ -0,0 +1,149 @@
#include "ble_beacon_app.h"
#include <extra_beacon.h>
#include <furi_hal_version.h>
#include <string.h>
#define TAG "ble_beacon_app"
static bool ble_beacon_app_custom_event_callback(void* context, uint32_t event) {
furi_assert(context);
BleBeaconApp* app = context;
return scene_manager_handle_custom_event(app->scene_manager, event);
}
static bool ble_beacon_app_back_event_callback(void* context) {
furi_assert(context);
BleBeaconApp* app = context;
return scene_manager_handle_back_event(app->scene_manager);
}
static void ble_beacon_app_tick_event_callback(void* context) {
furi_assert(context);
BleBeaconApp* app = context;
scene_manager_handle_tick_event(app->scene_manager);
}
static void ble_beacon_app_restore_beacon_state(BleBeaconApp* app) {
// Restore beacon data from service
GapExtraBeaconConfig* local_config = &app->beacon_config;
const GapExtraBeaconConfig* config = furi_hal_bt_extra_beacon_get_config();
if(config) {
// We have a config, copy it
memcpy(local_config, config, sizeof(app->beacon_config));
} else {
// No config, set up default values - they will stay until overriden or device is reset
local_config->min_adv_interval_ms = 50;
local_config->max_adv_interval_ms = 150;
local_config->adv_channel_map = GapAdvChannelMapAll;
local_config->adv_power_level = GapAdvPowerLevel_0dBm;
local_config->address_type = GapAddressTypePublic;
memcpy(
local_config->address, furi_hal_version_get_ble_mac(), sizeof(local_config->address));
// Modify MAC address to make it different from the one used by the main app
local_config->address[0] ^= 0xFF;
local_config->address[3] ^= 0xFF;
furi_check(furi_hal_bt_extra_beacon_set_config(local_config));
}
// Get beacon state
app->is_beacon_active = furi_hal_bt_extra_beacon_is_active();
// Restore last beacon data
app->beacon_data_len = furi_hal_bt_extra_beacon_get_data(app->beacon_data);
}
static BleBeaconApp* ble_beacon_app_alloc() {
BleBeaconApp* app = malloc(sizeof(BleBeaconApp));
app->gui = furi_record_open(RECORD_GUI);
app->scene_manager = scene_manager_alloc(&ble_beacon_app_scene_handlers, app);
app->view_dispatcher = view_dispatcher_alloc();
app->status_string = furi_string_alloc();
view_dispatcher_set_event_callback_context(app->view_dispatcher, app);
view_dispatcher_set_custom_event_callback(
app->view_dispatcher, ble_beacon_app_custom_event_callback);
view_dispatcher_set_navigation_event_callback(
app->view_dispatcher, ble_beacon_app_back_event_callback);
view_dispatcher_set_tick_event_callback(
app->view_dispatcher, ble_beacon_app_tick_event_callback, 100);
view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen);
view_dispatcher_enable_queue(app->view_dispatcher);
app->submenu = submenu_alloc();
view_dispatcher_add_view(
app->view_dispatcher, BleBeaconAppViewSubmenu, submenu_get_view(app->submenu));
app->dialog_ex = dialog_ex_alloc();
view_dispatcher_add_view(
app->view_dispatcher, BleBeaconAppViewDialog, dialog_ex_get_view(app->dialog_ex));
app->byte_input = byte_input_alloc();
view_dispatcher_add_view(
app->view_dispatcher, BleBeaconAppViewByteInput, byte_input_get_view(app->byte_input));
ble_beacon_app_restore_beacon_state(app);
return app;
}
static void ble_beacon_app_free(BleBeaconApp* app) {
view_dispatcher_remove_view(app->view_dispatcher, BleBeaconAppViewByteInput);
view_dispatcher_remove_view(app->view_dispatcher, BleBeaconAppViewSubmenu);
view_dispatcher_remove_view(app->view_dispatcher, BleBeaconAppViewDialog);
free(app->byte_input);
free(app->submenu);
free(app->dialog_ex);
free(app->scene_manager);
free(app->view_dispatcher);
free(app->status_string);
furi_record_close(RECORD_NOTIFICATION);
furi_record_close(RECORD_GUI);
app->gui = NULL;
free(app);
}
int32_t ble_beacon_app(void* args) {
UNUSED(args);
BleBeaconApp* app = ble_beacon_app_alloc();
scene_manager_next_scene(app->scene_manager, BleBeaconAppSceneRunBeacon);
view_dispatcher_run(app->view_dispatcher);
ble_beacon_app_free(app);
return 0;
}
void ble_beacon_app_update_state(BleBeaconApp* app) {
furi_hal_bt_extra_beacon_stop();
furi_check(furi_hal_bt_extra_beacon_set_config(&app->beacon_config));
app->beacon_data_len = 0;
while((app->beacon_data[app->beacon_data_len] != 0) &&
(app->beacon_data_len < sizeof(app->beacon_data))) {
app->beacon_data_len++;
}
FURI_LOG_I(TAG, "beacon_data_len: %d", app->beacon_data_len);
furi_check(furi_hal_bt_extra_beacon_set_data(app->beacon_data, app->beacon_data_len));
if(app->is_beacon_active) {
furi_check(furi_hal_bt_extra_beacon_start());
}
}

View File

@@ -0,0 +1,50 @@
#pragma once
#include "extra_beacon.h"
#include <furi.h>
#include <gui/gui.h>
#include <gui/view.h>
#include <gui/scene_manager.h>
#include <gui/view_dispatcher.h>
#include <gui/modules/widget.h>
#include <gui/modules/submenu.h>
#include <gui/modules/byte_input.h>
#include <gui/modules/dialog_ex.h>
#include <rpc/rpc_app.h>
#include <notification/notification_messages.h>
#include <furi_hal_bt.h>
#include "scenes/scenes.h"
#include <stdint.h>
typedef struct {
Gui* gui;
SceneManager* scene_manager;
ViewDispatcher* view_dispatcher;
Submenu* submenu;
ByteInput* byte_input;
DialogEx* dialog_ex;
FuriString* status_string;
GapExtraBeaconConfig beacon_config;
uint8_t beacon_data[EXTRA_BEACON_MAX_DATA_SIZE];
uint8_t beacon_data_len;
bool is_beacon_active;
} BleBeaconApp;
typedef enum {
BleBeaconAppViewSubmenu,
BleBeaconAppViewByteInput,
BleBeaconAppViewDialog,
} BleBeaconAppView;
typedef enum {
BleBeaconAppCustomEventDataEditResult = 100,
} BleBeaconAppCustomEvent;
void ble_beacon_app_update_state(BleBeaconApp* app);

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.7 KiB

View File

@@ -0,0 +1,4 @@
ADD_SCENE(ble_beacon_app, menu, Menu)
ADD_SCENE(ble_beacon_app, input_mac_addr, InputMacAddress)
ADD_SCENE(ble_beacon_app, input_beacon_data, InputBeaconData)
ADD_SCENE(ble_beacon_app, run_beacon, RunBeacon)

View File

@@ -0,0 +1,44 @@
#include "../ble_beacon_app.h"
static void ble_beacon_app_scene_add_type_byte_input_callback(void* context) {
BleBeaconApp* ble_beacon = context;
view_dispatcher_send_custom_event(
ble_beacon->view_dispatcher, BleBeaconAppCustomEventDataEditResult);
}
void ble_beacon_app_scene_input_beacon_data_on_enter(void* context) {
BleBeaconApp* ble_beacon = context;
byte_input_set_header_text(ble_beacon->byte_input, "Enter beacon data");
byte_input_set_result_callback(
ble_beacon->byte_input,
ble_beacon_app_scene_add_type_byte_input_callback,
NULL,
context,
ble_beacon->beacon_data,
sizeof(ble_beacon->beacon_data));
view_dispatcher_switch_to_view(ble_beacon->view_dispatcher, BleBeaconAppViewByteInput);
}
bool ble_beacon_app_scene_input_beacon_data_on_event(void* context, SceneManagerEvent event) {
BleBeaconApp* ble_beacon = context;
SceneManager* scene_manager = ble_beacon->scene_manager;
if(event.type == SceneManagerEventTypeCustom) {
if(event.event == BleBeaconAppCustomEventDataEditResult) {
ble_beacon_app_update_state(ble_beacon);
scene_manager_previous_scene(scene_manager);
return true;
}
}
return false;
}
void ble_beacon_app_scene_input_beacon_data_on_exit(void* context) {
BleBeaconApp* ble_beacon = context;
byte_input_set_result_callback(ble_beacon->byte_input, NULL, NULL, NULL, NULL, 0);
byte_input_set_header_text(ble_beacon->byte_input, NULL);
}

View File

@@ -0,0 +1,44 @@
#include "../ble_beacon_app.h"
static void ble_beacon_app_scene_add_type_byte_input_callback(void* context) {
BleBeaconApp* ble_beacon = context;
view_dispatcher_send_custom_event(
ble_beacon->view_dispatcher, BleBeaconAppCustomEventDataEditResult);
}
void ble_beacon_app_scene_input_mac_addr_on_enter(void* context) {
BleBeaconApp* ble_beacon = context;
byte_input_set_header_text(ble_beacon->byte_input, "Enter MAC (reversed)");
byte_input_set_result_callback(
ble_beacon->byte_input,
ble_beacon_app_scene_add_type_byte_input_callback,
NULL,
context,
ble_beacon->beacon_config.address,
sizeof(ble_beacon->beacon_config.address));
view_dispatcher_switch_to_view(ble_beacon->view_dispatcher, BleBeaconAppViewByteInput);
}
bool ble_beacon_app_scene_input_mac_addr_on_event(void* context, SceneManagerEvent event) {
BleBeaconApp* ble_beacon = context;
SceneManager* scene_manager = ble_beacon->scene_manager;
if(event.type == SceneManagerEventTypeCustom) {
if(event.event == BleBeaconAppCustomEventDataEditResult) {
ble_beacon_app_update_state(ble_beacon);
scene_manager_previous_scene(scene_manager);
return true;
}
}
return false;
}
void ble_beacon_app_scene_input_mac_addr_on_exit(void* context) {
BleBeaconApp* ble_beacon = context;
byte_input_set_result_callback(ble_beacon->byte_input, NULL, NULL, NULL, NULL, 0);
byte_input_set_header_text(ble_beacon->byte_input, NULL);
}

View File

@@ -0,0 +1,56 @@
#include "../ble_beacon_app.h"
enum SubmenuIndex {
SubmenuIndexSetMac,
SubmenuIndexSetData,
};
static void ble_beacon_app_scene_menu_submenu_callback(void* context, uint32_t index) {
BleBeaconApp* ble_beacon = context;
view_dispatcher_send_custom_event(ble_beacon->view_dispatcher, index);
}
void ble_beacon_app_scene_menu_on_enter(void* context) {
BleBeaconApp* ble_beacon = context;
Submenu* submenu = ble_beacon->submenu;
submenu_add_item(
submenu,
"Set MAC",
SubmenuIndexSetMac,
ble_beacon_app_scene_menu_submenu_callback,
ble_beacon);
submenu_add_item(
submenu,
"Set Data",
SubmenuIndexSetData,
ble_beacon_app_scene_menu_submenu_callback,
ble_beacon);
view_dispatcher_switch_to_view(ble_beacon->view_dispatcher, BleBeaconAppViewSubmenu);
}
bool ble_beacon_app_scene_menu_on_event(void* context, SceneManagerEvent event) {
BleBeaconApp* ble_beacon = context;
SceneManager* scene_manager = ble_beacon->scene_manager;
bool consumed = false;
if(event.type == SceneManagerEventTypeCustom) {
const uint32_t submenu_index = event.event;
if(submenu_index == SubmenuIndexSetMac) {
scene_manager_next_scene(scene_manager, BleBeaconAppSceneInputMacAddress);
consumed = true;
} else if(submenu_index == SubmenuIndexSetData) {
scene_manager_next_scene(scene_manager, BleBeaconAppSceneInputBeaconData);
consumed = true;
}
}
return consumed;
}
void ble_beacon_app_scene_menu_on_exit(void* context) {
BleBeaconApp* ble_beacon = context;
submenu_reset(ble_beacon->submenu);
}

View File

@@ -0,0 +1,79 @@
#include "../ble_beacon_app.h"
#include <example_ble_beacon_icons.h>
static void
ble_beacon_app_scene_run_beacon_confirm_dialog_callback(DialogExResult result, void* context) {
BleBeaconApp* ble_beacon = context;
view_dispatcher_send_custom_event(ble_beacon->view_dispatcher, result);
}
static void update_status_text(BleBeaconApp* ble_beacon) {
DialogEx* dialog_ex = ble_beacon->dialog_ex;
dialog_ex_set_header(dialog_ex, "BLE Beacon Demo", 64, 0, AlignCenter, AlignTop);
FuriString* status = ble_beacon->status_string;
furi_string_reset(status);
furi_string_cat_str(status, "Status: ");
if(ble_beacon->is_beacon_active) {
furi_string_cat_str(status, "Running\n");
} else {
furi_string_cat_str(status, "Stopped\n");
}
// Output MAC in reverse order
for(int i = sizeof(ble_beacon->beacon_config.address) - 1; i >= 0; i--) {
furi_string_cat_printf(status, "%02X", ble_beacon->beacon_config.address[i]);
if(i > 0) {
furi_string_cat_str(status, ":");
}
}
furi_string_cat_printf(status, "\nData length: %d", ble_beacon->beacon_data_len);
dialog_ex_set_text(dialog_ex, furi_string_get_cstr(status), 0, 29, AlignLeft, AlignCenter);
dialog_ex_set_icon(dialog_ex, 93, 20, &I_lighthouse_35x44);
dialog_ex_set_left_button_text(dialog_ex, "Config");
dialog_ex_set_center_button_text(dialog_ex, ble_beacon->is_beacon_active ? "Stop" : "Start");
dialog_ex_set_result_callback(
dialog_ex, ble_beacon_app_scene_run_beacon_confirm_dialog_callback);
dialog_ex_set_context(dialog_ex, ble_beacon);
}
void ble_beacon_app_scene_run_beacon_on_enter(void* context) {
BleBeaconApp* ble_beacon = context;
update_status_text(ble_beacon);
view_dispatcher_switch_to_view(ble_beacon->view_dispatcher, BleBeaconAppViewDialog);
}
bool ble_beacon_app_scene_run_beacon_on_event(void* context, SceneManagerEvent event) {
BleBeaconApp* ble_beacon = context;
SceneManager* scene_manager = ble_beacon->scene_manager;
if(event.type == SceneManagerEventTypeCustom) {
if(event.event == DialogExResultLeft) {
scene_manager_next_scene(scene_manager, BleBeaconAppSceneMenu);
return true;
} else if(event.event == DialogExResultCenter) {
ble_beacon->is_beacon_active = !ble_beacon->is_beacon_active;
ble_beacon_app_update_state(ble_beacon);
update_status_text(ble_beacon);
return true;
}
}
return false;
}
void ble_beacon_app_scene_run_beacon_on_exit(void* context) {
BleBeaconApp* ble_beacon = context;
UNUSED(ble_beacon);
}

View File

@@ -0,0 +1,30 @@
#include "scenes.h"
// Generate scene on_enter handlers array
#define ADD_SCENE(prefix, name, id) prefix##_scene_##name##_on_enter,
void (*const ble_beacon_app_on_enter_handlers[])(void*) = {
#include "scene_config.h"
};
#undef ADD_SCENE
// Generate scene on_event handlers array
#define ADD_SCENE(prefix, name, id) prefix##_scene_##name##_on_event,
bool (*const ble_beacon_app_on_event_handlers[])(void* context, SceneManagerEvent event) = {
#include "scene_config.h"
};
#undef ADD_SCENE
// Generate scene on_exit handlers array
#define ADD_SCENE(prefix, name, id) prefix##_scene_##name##_on_exit,
void (*const ble_beacon_app_on_exit_handlers[])(void* context) = {
#include "scene_config.h"
};
#undef ADD_SCENE
// Initialize scene handlers configuration structure
const SceneManagerHandlers ble_beacon_app_scene_handlers = {
.on_enter_handlers = ble_beacon_app_on_enter_handlers,
.on_event_handlers = ble_beacon_app_on_event_handlers,
.on_exit_handlers = ble_beacon_app_on_exit_handlers,
.scene_num = BleBeaconAppSceneNum,
};

View File

@@ -0,0 +1,29 @@
#pragma once
#include <gui/scene_manager.h>
// Generate scene id and total number
#define ADD_SCENE(prefix, name, id) BleBeaconAppScene##id,
typedef enum {
#include "scene_config.h"
BleBeaconAppSceneNum,
} BleBeaconAppScene;
#undef ADD_SCENE
extern const SceneManagerHandlers ble_beacon_app_scene_handlers;
// Generate scene on_enter handlers declaration
#define ADD_SCENE(prefix, name, id) void prefix##_scene_##name##_on_enter(void*);
#include "scene_config.h"
#undef ADD_SCENE
// Generate scene on_event handlers declaration
#define ADD_SCENE(prefix, name, id) \
bool prefix##_scene_##name##_on_event(void* context, SceneManagerEvent event);
#include "scene_config.h"
#undef ADD_SCENE
// Generate scene on_exit handlers declaration
#define ADD_SCENE(prefix, name, id) void prefix##_scene_##name##_on_exit(void* context);
#include "scene_config.h"
#undef ADD_SCENE

View File

@@ -1,9 +1,9 @@
App(
appid="example_custom_font",
name="Example: custom font",
apptype=FlipperAppType.DEBUG,
apptype=FlipperAppType.EXTERNAL,
entry_point="example_custom_font_main",
requires=["gui"],
stack_size=1 * 1024,
fap_category="Debug",
fap_category="Examples",
)

View File

@@ -1,5 +1,7 @@
#pragma once
#include "archive_files.h"
typedef enum {
ArchiveAppTypeU2f,
ArchiveAppTypeSearch,

View File

@@ -61,7 +61,7 @@ static const char* opal_usages[14] = {
};
// Opal file 0x7 structure. Assumes a little-endian CPU.
typedef struct __attribute__((__packed__)) {
typedef struct FURI_PACKED {
uint32_t serial : 32;
uint8_t check_digit : 4;
bool blocked : 1;

View File

@@ -6,6 +6,7 @@
#include <ble/ble.h>
#include "bt_settings.h"
#include "bt_service/bt.h"
#include <profiles/serial_profile.h>
static void bt_cli_command_hci_info(Cli* cli, FuriString* args, void* context) {
UNUSED(cli);
@@ -45,7 +46,7 @@ static void bt_cli_command_carrier_tx(Cli* cli, FuriString* args, void* context)
}
furi_hal_bt_stop_tone_tx();
bt_set_profile(bt, BtProfileSerial);
bt_profile_restore_default(bt);
furi_record_close(RECORD_BT);
} while(false);
}
@@ -76,7 +77,7 @@ static void bt_cli_command_carrier_rx(Cli* cli, FuriString* args, void* context)
furi_hal_bt_stop_packet_test();
bt_set_profile(bt, BtProfileSerial);
bt_profile_restore_default(bt);
furi_record_close(RECORD_BT);
} while(false);
}
@@ -124,7 +125,7 @@ static void bt_cli_command_packet_tx(Cli* cli, FuriString* args, void* context)
furi_hal_bt_stop_packet_test();
printf("Transmitted %lu packets", furi_hal_bt_get_transmitted_packets());
bt_set_profile(bt, BtProfileSerial);
bt_profile_restore_default(bt);
furi_record_close(RECORD_BT);
} while(false);
}
@@ -159,7 +160,7 @@ static void bt_cli_command_packet_rx(Cli* cli, FuriString* args, void* context)
uint16_t packets_received = furi_hal_bt_stop_packet_test();
printf("Received %hu packets", packets_received);
bt_set_profile(bt, BtProfileSerial);
bt_profile_restore_default(bt);
furi_record_close(RECORD_BT);
} while(false);
}

View File

@@ -1,10 +1,13 @@
#include "bt_i.h"
#include "bt_keys_storage.h"
#include <core/check.h>
#include <furi_hal_bt.h>
#include <services/battery_service.h>
#include <notification/notification_messages.h>
#include <gui/elements.h>
#include <assets_icons.h>
#include <profiles/serial_profile.h>
#define TAG "BtSrv"
@@ -12,14 +15,21 @@
#define BT_RPC_EVENT_DISCONNECTED (1UL << 1)
#define BT_RPC_EVENT_ALL (BT_RPC_EVENT_BUFF_SENT | BT_RPC_EVENT_DISCONNECTED)
#define ICON_SPACER 2
static void bt_draw_statusbar_callback(Canvas* canvas, void* context) {
furi_assert(context);
Bt* bt = context;
uint8_t draw_offset = 0;
if(bt->beacon_active) {
canvas_draw_icon(canvas, 0, 0, &I_BLE_beacon_7x8);
draw_offset += icon_get_width(&I_BLE_beacon_7x8) + ICON_SPACER;
}
if(bt->status == BtStatusAdvertising) {
canvas_draw_icon(canvas, 0, 0, &I_Bluetooth_Idle_5x8);
canvas_draw_icon(canvas, draw_offset, 0, &I_Bluetooth_Idle_5x8);
} else if(bt->status == BtStatusConnected) {
canvas_draw_icon(canvas, 0, 0, &I_Bluetooth_Connected_16x8);
canvas_draw_icon(canvas, draw_offset, 0, &I_Bluetooth_Connected_16x8);
}
}
@@ -60,8 +70,12 @@ static ViewPort* bt_pin_code_view_port_alloc(Bt* bt) {
}
static void bt_pin_code_show(Bt* bt, uint32_t pin_code) {
furi_assert(bt);
bt->pin_code = pin_code;
if(!bt->pin_code_view_port) {
// Pin code view port
bt->pin_code_view_port = bt_pin_code_view_port_alloc(bt);
gui_add_view_port(bt->gui, bt->pin_code_view_port, GuiLayerFullscreen);
}
notification_message(bt->notification, &sequence_display_backlight_on);
if(bt->suppress_pin_screen) return;
@@ -71,7 +85,7 @@ static void bt_pin_code_show(Bt* bt, uint32_t pin_code) {
static void bt_pin_code_hide(Bt* bt) {
bt->pin_code = 0;
if(view_port_is_enabled(bt->pin_code_view_port)) {
if(bt->pin_code_view_port && view_port_is_enabled(bt->pin_code_view_port)) {
view_port_enabled_set(bt->pin_code_view_port, false);
}
}
@@ -83,6 +97,9 @@ static bool bt_pin_code_verify_event_handler(Bt* bt, uint32_t pin) {
if(bt->suppress_pin_screen) return true;
FuriString* pin_str;
if(!bt->dialog_message) {
bt->dialog_message = dialog_message_alloc();
}
dialog_message_set_icon(bt->dialog_message, &I_BLE_Pairing_128x64, 0, 0);
pin_str = furi_string_alloc_printf("Verify code\n%06lu", pin);
dialog_message_set_text(
@@ -100,25 +117,32 @@ static void bt_battery_level_changed_callback(const void* _event, void* context)
Bt* bt = context;
BtMessage message = {};
const PowerEvent* event = _event;
if(event->type == PowerEventTypeBatteryLevelChanged) {
bool is_charging = false;
switch(event->type) {
case 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) {
break;
case PowerEventTypeStartCharging:
is_charging = true;
/* fallthrough */
case PowerEventTypeFullyCharged:
case PowerEventTypeStopCharging:
message.type = BtMessageTypeUpdatePowerState;
message.data.power_state_charging = is_charging;
furi_check(
furi_message_queue_put(bt->message_queue, &message, FuriWaitForever) == FuriStatusOk);
break;
}
}
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;
bt->max_packet_size = BLE_PROFILE_SERIAL_PACKET_SIZE_MAX;
bt->current_profile = NULL;
// Load settings
bt_settings_load(&bt->bt_settings);
// Keys storage
@@ -128,18 +152,14 @@ Bt* bt_alloc() {
// 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);
@@ -176,7 +196,11 @@ static uint16_t bt_serial_event_callback(SerialServiceEvent event, void* context
furi_event_flag_set(bt->rpc_event, BT_RPC_EVENT_BUFF_SENT);
} else if(event.event == SerialServiceEventTypesBleResetRequest) {
FURI_LOG_I(TAG, "BLE restart request received");
BtMessage message = {.type = BtMessageTypeSetProfile, .data.profile = BtProfileSerial};
BtMessage message = {
.type = BtMessageTypeSetProfile,
.data.profile.params = NULL,
.data.profile.template = ble_profile_serial,
};
furi_check(
furi_message_queue_put(bt->message_queue, &message, FuriWaitForever) == FuriStatusOk);
}
@@ -197,10 +221,10 @@ static void bt_rpc_send_bytes_callback(void* context, uint8_t* bytes, size_t byt
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);
ble_profile_serial_tx(bt->current_profile, &bytes[bytes_sent], bt->max_packet_size);
bytes_sent += bt->max_packet_size;
} else {
furi_hal_bt_serial_tx(&bytes[bytes_sent], bytes_remain);
ble_profile_serial_tx(bt->current_profile, &bytes[bytes_sent], bytes_remain);
bytes_sent += bytes_remain;
}
// We want BT_RPC_EVENT_DISCONNECTED to stick, so don't clear
@@ -215,49 +239,55 @@ static void bt_rpc_send_bytes_callback(void* context, uint8_t* bytes, size_t byt
}
}
static void bt_serial_buffer_is_empty_callback(void* context) {
furi_assert(context);
Bt* bt = context;
furi_check(furi_hal_bt_check_profile_type(bt->current_profile, ble_profile_serial));
ble_profile_serial_notify_buffer_is_empty(bt->current_profile);
}
// Called from GAP thread
static bool bt_on_gap_event_callback(GapEvent event, void* context) {
furi_assert(context);
Bt* bt = context;
bool ret = false;
bt->pin = 0;
bool do_update_status = false;
bool current_profile_is_serial =
furi_hal_bt_check_profile_type(bt->current_profile, ble_profile_serial);
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);
do_update_status = true;
bt_open_rpc_connection(bt);
// Update battery level
PowerInfo info;
power_get_info(bt->power, &info);
BtMessage message = {.type = BtMessageTypeUpdateStatus};
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) {
if(current_profile_is_serial && bt->rpc_session) {
FURI_LOG_I(TAG, "Close RPC connection");
furi_hal_bt_serial_set_rpc_status(FuriHalBtSerialRpcStatusNotActive);
ble_profile_serial_set_rpc_active(
bt->current_profile, FuriHalBtSerialRpcStatusNotActive);
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);
ble_profile_serial_set_event_callback(bt->current_profile, 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);
do_update_status = true;
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);
do_update_status = true;
ret = true;
} else if(event.type == GapEventTypePinCodeShow) {
bt->pin = event.data.pin_code;
@@ -272,6 +302,20 @@ static bool bt_on_gap_event_callback(GapEvent event, void* context) {
} else if(event.type == GapEventTypeUpdateMTU) {
bt->max_packet_size = event.data.max_packet_size;
ret = true;
} else if(event.type == GapEventTypeBeaconStart) {
bt->beacon_active = true;
do_update_status = true;
ret = true;
} else if(event.type == GapEventTypeBeaconStop) {
bt->beacon_active = false;
do_update_status = true;
ret = true;
}
if(do_update_status) {
BtMessage message = {.type = BtMessageTypeUpdateStatus};
furi_check(
furi_message_queue_put(bt->message_queue, &message, FuriWaitForever) == FuriStatusOk);
}
return ret;
}
@@ -288,11 +332,18 @@ static void bt_on_key_storage_change_callback(uint8_t* addr, uint16_t size, void
}
static void bt_statusbar_update(Bt* bt) {
uint8_t active_icon_width = 0;
if(bt->beacon_active) {
active_icon_width = icon_get_width(&I_BLE_beacon_7x8) + ICON_SPACER;
}
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);
active_icon_width += icon_get_width(&I_Bluetooth_Idle_5x8);
} else if(bt->status == BtStatusConnected) {
view_port_set_width(bt->statusbar_view_port, icon_get_width(&I_Bluetooth_Connected_16x8));
active_icon_width += icon_get_width(&I_Bluetooth_Connected_16x8);
}
if(active_icon_width > 0) {
view_port_set_width(bt->statusbar_view_port, active_icon_width);
view_port_enabled_set(bt->statusbar_view_port, true);
} else {
view_port_enabled_set(bt->statusbar_view_port, false);
@@ -300,6 +351,9 @@ static void bt_statusbar_update(Bt* bt) {
}
static void bt_show_warning(Bt* bt, const char* text) {
if(!bt->dialog_message) {
bt->dialog_message = dialog_message_alloc();
}
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);
@@ -309,18 +363,19 @@ void bt_open_rpc_connection(Bt* bt) {
if(!bt->rpc_session && bt->status == BtStatusConnected) {
// 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) {
if(furi_hal_bt_check_profile_type(bt->current_profile, ble_profile_serial)) {
// Open RPC session
bt->rpc_session = rpc_session_open(bt->rpc, RpcOwnerBle);
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);
bt->rpc_session, bt_serial_buffer_is_empty_callback);
rpc_session_set_context(bt->rpc_session, bt);
furi_hal_bt_serial_set_event_callback(
RPC_BUFFER_SIZE, bt_serial_event_callback, bt);
furi_hal_bt_serial_set_rpc_status(FuriHalBtSerialRpcStatusActive);
ble_profile_serial_set_event_callback(
bt->current_profile, RPC_BUFFER_SIZE, bt_serial_event_callback, bt);
ble_profile_serial_set_rpc_active(
bt->current_profile, FuriHalBtSerialRpcStatusActive);
} else {
FURI_LOG_W(TAG, "RPC is busy, failed to open new session");
}
@@ -329,49 +384,51 @@ void bt_open_rpc_connection(Bt* bt) {
}
void bt_close_rpc_connection(Bt* bt) {
if(bt->profile == BtProfileSerial && bt->rpc_session) {
if(furi_hal_bt_check_profile_type(bt->current_profile, ble_profile_serial) &&
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);
ble_profile_serial_set_event_callback(bt->current_profile, 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()) {
if(furi_hal_bt_is_gatt_gap_supported()) {
bt_close_rpc_connection(bt);
FuriHalBtProfile furi_profile;
if(message->data.profile == BtProfileHidKeyboard) {
furi_profile = FuriHalBtProfileHidKeyboard;
} else {
furi_profile = FuriHalBtProfileSerial;
}
bt_keys_storage_load(bt->keys_storage);
if(furi_hal_bt_change_app(furi_profile, bt_on_gap_event_callback, bt)) {
bt->current_profile = furi_hal_bt_change_app(
message->data.profile.template,
message->data.profile.params,
bt_on_gap_event_callback,
bt);
if(bt->current_profile) {
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;
if(message->result) {
*message->result = true;
}
} else {
FURI_LOG_E(TAG, "Failed to start Bt App");
if(message->result) {
*message->result = false;
}
}
if(message->profile_instance) {
*message->profile_instance = bt->current_profile;
}
if(message->result) {
*message->result = bt->current_profile != NULL;
}
} else {
bt_show_warning(bt, "Radio stack doesn't support this app");
if(message->result) {
*message->result = false;
}
if(message->profile_instance) {
*message->profile_instance = NULL;
}
}
if(message->lock) api_lock_unlock(message->lock);
}
@@ -382,52 +439,6 @@ static void bt_close_connection(Bt* bt, BtMessage* message) {
if(message->lock) api_lock_unlock(message->lock);
}
static inline FuriHalBtProfile get_hal_bt_profile(BtProfile profile) {
if(profile == BtProfileHidKeyboard) {
return FuriHalBtProfileHidKeyboard;
} else {
return FuriHalBtProfileSerial;
}
}
void bt_restart(Bt* bt) {
furi_hal_bt_change_app(get_hal_bt_profile(bt->profile), bt_on_gap_event_callback, bt);
furi_hal_bt_start_advertising();
}
void bt_set_profile_adv_name(Bt* bt, const char* fmt, ...) {
furi_assert(bt);
furi_assert(fmt);
char name[FURI_HAL_BT_ADV_NAME_LENGTH];
va_list args;
va_start(args, fmt);
vsnprintf(name, sizeof(name), fmt, args);
va_end(args);
furi_hal_bt_set_profile_adv_name(get_hal_bt_profile(bt->profile), name);
bt_restart(bt);
}
const char* bt_get_profile_adv_name(Bt* bt) {
furi_assert(bt);
return furi_hal_bt_get_profile_adv_name(get_hal_bt_profile(bt->profile));
}
void bt_set_profile_mac_address(Bt* bt, const uint8_t mac[6]) {
furi_assert(bt);
furi_assert(mac);
furi_hal_bt_set_profile_mac_addr(get_hal_bt_profile(bt->profile), mac);
bt_restart(bt);
}
const uint8_t* bt_get_profile_mac_address(Bt* bt) {
furi_assert(bt);
return furi_hal_bt_get_profile_mac_addr(get_hal_bt_profile(bt->profile));
}
bool bt_remote_rssi(Bt* bt, uint8_t* rssi) {
furi_assert(bt);
@@ -441,27 +452,6 @@ bool bt_remote_rssi(Bt* bt, uint8_t* rssi) {
return true;
}
void bt_set_profile_pairing_method(Bt* bt, GapPairing pairing_method) {
furi_assert(bt);
furi_hal_bt_set_profile_pairing_method(get_hal_bt_profile(bt->profile), pairing_method);
bt_restart(bt);
}
GapPairing bt_get_profile_pairing_method(Bt* bt) {
furi_assert(bt);
return furi_hal_bt_get_profile_pairing_method(get_hal_bt_profile(bt->profile));
}
void bt_disable_peer_key_update(Bt* bt) {
UNUSED(bt);
furi_hal_bt_set_key_storage_change_callback(NULL, NULL);
}
void bt_enable_peer_key_update(Bt* bt) {
furi_assert(bt);
furi_hal_bt_set_key_storage_change_callback(bt_on_key_storage_change_callback, bt);
}
int32_t bt_srv(void* p) {
UNUSED(p);
Bt* bt = bt_alloc();
@@ -483,8 +473,10 @@ int32_t bt_srv(void* p) {
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)) {
if(furi_hal_bt_is_gatt_gap_supported()) {
bt->current_profile =
furi_hal_bt_start_app(ble_profile_serial, NULL, bt_on_gap_event_callback, bt);
if(!bt->current_profile) {
FURI_LOG_E(TAG, "BLE App start failed");
} else {
if(bt->bt_settings.enabled) {
@@ -514,7 +506,7 @@ int32_t bt_srv(void* p) {
// Update battery level
furi_hal_bt_update_battery_level(message.data.battery_level);
} else if(message.type == BtMessageTypeUpdatePowerState) {
furi_hal_bt_update_power_state();
furi_hal_bt_update_power_state(message.data.power_state_charging);
} else if(message.type == BtMessageTypePinCodeShow) {
// Display PIN code
bt_pin_code_show(bt, message.data.pin_code);

View File

@@ -2,7 +2,8 @@
#include <stdint.h>
#include <stdbool.h>
#include <furi_hal_bt.h>
#include <furi_ble/profile_interface.h>
#include <core/common_defines.h>
#ifdef __cplusplus
extern "C" {
@@ -19,11 +20,6 @@ typedef enum {
BtStatusConnected,
} BtStatus;
typedef enum {
BtProfileSerial,
BtProfileHidKeyboard,
} BtProfile;
typedef struct {
uint8_t rssi;
uint32_t since;
@@ -34,12 +30,25 @@ 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
* @param bt Bt instance
* @param profile_template Profile template to change to
* @param params Profile parameters. Can be NULL
*
* @return true on success
*/
bool bt_set_profile(Bt* bt, BtProfile profile);
FURI_WARN_UNUSED FuriHalBleProfileBase* bt_profile_start(
Bt* bt,
const FuriHalBleProfileTemplate* profile_template,
FuriHalBleProfileParams params);
/** Stop current BLE Profile and restore default profile
* @note Call of this function leads to 2nd core restart
*
* @param bt Bt instance
*
* @return true on success
*/
bool bt_profile_restore_default(Bt* bt);
/** Disconnect from Central
*
@@ -75,30 +84,8 @@ void bt_keys_storage_set_storage_path(Bt* bt, const char* keys_storage_path);
*/
void bt_keys_storage_set_default_path(Bt* bt);
void bt_set_profile_adv_name(Bt* bt, const char* fmt, ...);
const char* bt_get_profile_adv_name(Bt* bt);
void bt_set_profile_mac_address(Bt* bt, const uint8_t mac[6]);
const uint8_t* bt_get_profile_mac_address(Bt* bt);
bool bt_remote_rssi(Bt* bt, uint8_t* rssi);
void bt_set_profile_pairing_method(Bt* bt, GapPairing pairing_method);
GapPairing bt_get_profile_pairing_method(Bt* bt);
/** Stop saving new peer key to flash (in .bt.keys file)
*
*/
void bt_disable_peer_key_update(Bt* bt);
/** Enable saving peer key to internal flash (enable by default)
*
* @note This function should be called if bt_disable_peer_key_update was called before
*/
void bt_enable_peer_key_update(Bt* bt);
/**
*
* (Probably bad) way of opening the RPC connection, everywhereTM

View File

@@ -1,21 +1,34 @@
#include "bt_i.h"
#include <profiles/serial_profile.h>
bool bt_set_profile(Bt* bt, BtProfile profile) {
FuriHalBleProfileBase* bt_profile_start(
Bt* bt,
const FuriHalBleProfileTemplate* profile_template,
FuriHalBleProfileParams params) {
furi_assert(bt);
// Send message
bool result = false;
FuriHalBleProfileBase* profile_instance = NULL;
BtMessage message = {
.lock = api_lock_alloc_locked(),
.type = BtMessageTypeSetProfile,
.data.profile = profile,
.result = &result};
.profile_instance = &profile_instance,
.data.profile.params = params,
.data.profile.template = profile_template,
};
furi_check(
furi_message_queue_put(bt->message_queue, &message, FuriWaitForever) == FuriStatusOk);
// Wait for unlock
api_lock_wait_unlock_and_free(message.lock);
return result;
bt->current_profile = profile_instance;
return profile_instance;
}
bool bt_profile_restore_default(Bt* bt) {
bt->current_profile = bt_profile_start(bt, ble_profile_serial, NULL);
return bt->current_profile != NULL;
}
void bt_disconnect(Bt* bt) {

View File

@@ -41,7 +41,12 @@ typedef struct {
typedef union {
uint32_t pin_code;
uint8_t battery_level;
BtProfile profile;
bool power_state_charging;
struct {
const FuriHalBleProfileTemplate* template;
FuriHalBleProfileParams params;
} profile;
FuriHalBleProfileParams profile_params;
BtKeyStorageUpdateData key_storage_data;
} BtMessageData;
@@ -50,6 +55,7 @@ typedef struct {
BtMessageType type;
BtMessageData data;
bool* result;
FuriHalBleProfileBase** profile_instance;
} BtMessage;
struct Bt {
@@ -59,7 +65,8 @@ struct Bt {
BtSettings bt_settings;
BtKeysStorage* keys_storage;
BtStatus status;
BtProfile profile;
bool beacon_active;
FuriHalBleProfileBase* current_profile;
FuriMessageQueue* message_queue;
NotificationApp* notification;
Gui* gui;

View File

@@ -1,6 +1,5 @@
#include "cli_command_gpio.h"
#include "core/string.h"
#include <furi.h>
#include <furi_hal.h>
#include <lib/toolbox/args.h>

View File

@@ -1,6 +1,7 @@
#include "cli_commands.h"
#include "cli_command_gpio.h"
#include <core/thread.h>
#include <furi_hal.h>
#include <furi_hal_info.h>
#include <task_control_block.h>
@@ -398,27 +399,30 @@ void cli_command_ps(Cli* cli, FuriString* args, void* context) {
const uint8_t threads_num_max = 32;
FuriThreadId threads_ids[threads_num_max];
uint8_t thread_num = furi_thread_enumerate(threads_ids, threads_num_max);
uint32_t thread_num = furi_thread_enumerate(threads_ids, threads_num_max);
printf(
"%-20s %-20s %-14s %-8s %-8s %s\r\n",
"%-17s %-20s %-5s %-13s %-6s %-8s %s\r\n",
"AppID",
"Name",
"Prio",
"Stack start",
"Heap",
"Stack",
"Stack min free");
for(uint8_t i = 0; i < thread_num; i++) {
TaskControlBlock* tcb = (TaskControlBlock*)threads_ids[i];
size_t thread_heap = memmgr_heap_get_thread_memory(threads_ids[i]);
printf(
"%-20s %-20s 0x%-12lx %-8zu %-8lu %-8lu\r\n",
"%-17s %-20s %-5d 0x%-11lx %-6zu %-8lu %-8lu\r\n",
furi_thread_get_appid(threads_ids[i]),
furi_thread_get_name(threads_ids[i]),
furi_thread_get_priority(threads_ids[i]),
(uint32_t)tcb->pxStack,
memmgr_heap_get_thread_memory(threads_ids[i]),
thread_heap == MEMMGR_HEAP_UNKNOWN ? 0u : thread_heap,
(uint32_t)(tcb->pxEndOfStack - tcb->pxStack + 1) * sizeof(StackType_t),
furi_thread_get_stack_space(threads_ids[i]));
}
printf("\r\nTotal: %d", thread_num);
printf("\r\nTotal: %lu", thread_num);
}
void cli_command_free(Cli* cli, FuriString* args, void* context) {

View File

@@ -4,10 +4,12 @@
*/
#pragma once
#include "../widget.h"
#include "widget_element.h"
#include <furi.h>
#include <gui/view.h>
#include <input/input.h>
#include "widget_element.h"
#ifdef __cplusplus
extern "C" {

View File

@@ -1,5 +1,4 @@
#include "loader.h"
#include "core/core_defines.h"
#include "loader_i.h"
#include <applications.h>
#include <storage/storage.h>

View File

@@ -1,3 +1,4 @@
#include "profiles/serial_profile.h"
#include "rpc_i.h"
#include <pb.h>
@@ -334,7 +335,7 @@ static int32_t rpc_session_worker(void* context) {
// Disconnect BLE session
FURI_LOG_E("RPC", "BLE session closed due to a decode error");
Bt* bt = furi_record_open(RECORD_BT);
bt_set_profile(bt, BtProfileSerial);
bt_profile_restore_default(bt);
furi_record_close(RECORD_BT);
FURI_LOG_E("RPC", "Finished disconnecting the BLE session");
}

View File

@@ -94,6 +94,7 @@ void rpc_session_set_send_bytes_callback(RpcSession* session, RpcSendBytesCallba
*
* @param session pointer to RpcSession descriptor
* @param callback callback to notify client that buffer is empty (can be NULL)
* @param context context to pass to callback
*/
void rpc_session_set_buffer_is_empty_callback(
RpcSession* session,

View File

@@ -39,7 +39,7 @@ void bt_settings_scene_start_on_enter(void* context) {
VariableItemList* var_item_list = app->var_item_list;
VariableItem* item;
if(furi_hal_bt_is_ble_gatt_gap_supported()) {
if(furi_hal_bt_is_gatt_gap_supported()) {
item = variable_item_list_add(
var_item_list,
"Bluetooth",

View File

@@ -4,6 +4,10 @@ App(
apptype=FlipperAppType.EXTERNAL,
entry_point="hid_usb_app",
stack_size=1 * 1024,
sources=["*.c", "!transport_ble.c"],
cdefines=["HID_TRANSPORT_USB"],
fap_description="Use Flipper as a HID remote control over USB",
fap_version="1.0",
fap_category="USB",
fap_icon="hid_usb_10px.png",
fap_icon_assets="assets",
@@ -17,6 +21,11 @@ App(
apptype=FlipperAppType.EXTERNAL,
entry_point="hid_ble_app",
stack_size=1 * 1024,
sources=["*.c", "!transport_usb.c"],
cdefines=["HID_TRANSPORT_BLE"],
fap_libs=["ble_profile"],
fap_description="Use Flipper as a HID remote control over Bluetooth",
fap_version="1.0",
fap_category="Bluetooth",
fap_icon="hid_ble_10px.png",
fap_icon_assets="assets",

View File

@@ -1,4 +1,6 @@
#include "hid.h"
#include <extra_profiles/hid_profile.h>
#include <profiles/serial_profile.h>
#include "views.h"
#include <notification/notification_messages.h>
#include <dolphin/dolphin.h>
@@ -20,6 +22,19 @@ enum HidDebugSubmenuIndex {
HidSubmenuIndexPushToTalk,
};
static void bt_hid_remove_pairing(Bt* bt) {
bt_disconnect(bt);
// Wait 2nd core to update nvm storage
furi_delay_ms(200);
furi_hal_bt_stop_advertising();
bt_forget_bonded_devices(bt);
furi_hal_bt_start_advertising();
}
static void hid_submenu_callback(void* context, uint32_t index) {
furi_assert(context);
Hid* app = context;
@@ -61,6 +76,8 @@ static void hid_submenu_callback(void* context, uint32_t index) {
} else if(index == HidSubmenuIndexPushToTalk) {
app->view_id = HidViewPushToTalkMenu;
view_dispatcher_switch_to_view(app->view_dispatcher, HidViewPushToTalkMenu);
} else if(index == HidSubmenuIndexRemovePairing) {
bt_hid_remove_pairing(app->bt);
}
}
@@ -68,13 +85,13 @@ static void bt_hid_connection_status_changed_callback(BtStatus status, void* con
furi_assert(context);
Hid* hid = context;
bool connected = (status == BtStatusConnected);
if(hid->transport == HidTransportBle) {
if(connected) {
notification_internal_message(hid->notifications, &sequence_set_blue_255);
} else {
notification_internal_message(hid->notifications, &sequence_reset_blue);
}
#ifdef HID_TRANSPORT_BLE
if(connected) {
notification_internal_message(hid->notifications, &sequence_set_blue_255);
} else {
notification_internal_message(hid->notifications, &sequence_reset_blue);
}
#endif
hid_keynote_set_connected_status(hid->hid_keynote, connected);
hid_keyboard_set_connected_status(hid->hid_keyboard, connected);
hid_numpad_set_connected_status(hid->hid_numpad, connected);
@@ -103,9 +120,8 @@ static uint32_t hid_ptt_menu_view(void* context) {
return HidViewPushToTalkMenu;
}
Hid* hid_alloc(HidTransport transport) {
Hid* hid_alloc() {
Hid* app = malloc(sizeof(Hid));
app->transport = transport;
// Gui
app->gui = furi_record_open(RECORD_GUI);
@@ -146,14 +162,12 @@ Hid* hid_alloc(HidTransport transport) {
app->device_type_submenu, "Movie", HidSubmenuIndexMovie, hid_submenu_callback, app);
submenu_add_item(
app->device_type_submenu, "Mouse", HidSubmenuIndexMouse, hid_submenu_callback, app);
if(app->transport == HidTransportBle) {
submenu_add_item(
app->device_type_submenu,
"TikTok / YT Shorts",
HidSubmenuIndexTikShorts,
hid_submenu_callback,
app);
}
submenu_add_item(
app->device_type_submenu,
"TikTok / YT Shorts",
HidSubmenuIndexTikShorts,
hid_submenu_callback,
app);
submenu_add_item(
app->device_type_submenu,
"Mouse Clicker",
@@ -172,6 +186,14 @@ Hid* hid_alloc(HidTransport transport) {
HidSubmenuIndexPushToTalk,
hid_submenu_callback,
app);
#ifdef HID_TRANSPORT_BLE
submenu_add_item(
app->device_type_submenu,
"Remove Pairing",
HidSubmenuIndexRemovePairing,
hid_submenu_callback,
app);
#endif
view_set_previous_callback(submenu_get_view(app->device_type_submenu), hid_exit);
view_dispatcher_add_view(
app->view_dispatcher, HidViewSubmenu, submenu_get_view(app->device_type_submenu));
@@ -265,10 +287,9 @@ void hid_free(Hid* app) {
furi_assert(app);
// Reset notification
if(app->transport == HidTransportBle) {
notification_internal_message(app->notifications, &sequence_reset_blue);
}
#ifdef HID_TRANSPORT_BLE
notification_internal_message(app->notifications, &sequence_reset_blue);
#endif
// Free views
view_dispatcher_remove_view(app->view_dispatcher, HidViewSubmenu);
submenu_free(app->device_type_submenu);
@@ -434,7 +455,7 @@ void hid_hal_mouse_release_all(Hid* instance) {
int32_t hid_usb_app(void* p) {
UNUSED(p);
Hid* app = hid_alloc(HidTransportUsb);
Hid* app = hid_alloc();
app = hid_app_alloc_view(app);
FuriHalUsbInterface* usb_mode_prev = furi_hal_usb_get_config();
furi_hal_usb_unlock();
@@ -455,7 +476,7 @@ int32_t hid_usb_app(void* p) {
int32_t hid_ble_app(void* p) {
UNUSED(p);
Hid* app = hid_alloc(HidTransportBle);
Hid* app = hid_alloc();
app = hid_app_alloc_view(app);
bt_disconnect(app->bt);
@@ -475,7 +496,9 @@ int32_t hid_ble_app(void* p) {
furi_record_close(RECORD_STORAGE);
furi_check(bt_set_profile(app->bt, BtProfileHidKeyboard));
app->ble_hid_profile = bt_profile_start(app->bt, ble_profile_hid, NULL);
furi_check(app->ble_hid_profile);
furi_hal_bt_start_advertising();
bt_set_status_changed_callback(app->bt, bt_hid_connection_status_changed_callback, app);
@@ -493,7 +516,7 @@ int32_t hid_ble_app(void* p) {
bt_keys_storage_set_default_path(app->bt);
furi_check(bt_set_profile(app->bt, BtProfileSerial));
furi_check(bt_profile_restore_default(app->bt));
hid_free(app);

View File

@@ -2,10 +2,11 @@
#include <furi.h>
#include <furi_hal_bt.h>
#include <furi_hal_bt_hid.h>
#include <furi_hal_usb.h>
#include <furi_hal_usb_hid.h>
#include <extra_profiles/hid_profile.h>
#include <bt/bt_service/bt.h>
#include <gui/gui.h>
#include <gui/view.h>
@@ -40,6 +41,7 @@ typedef enum {
typedef struct Hid Hid;
struct Hid {
FuriHalBleProfileBase* ble_hid_profile;
Bt* bt;
Gui* gui;
NotificationApp* notifications;

View File

@@ -0,0 +1,60 @@
#include "hid.h"
#ifndef HID_TRANSPORT_BLE
#error "HID_TRANSPORT_BLE must be defined"
#endif
void hid_hal_keyboard_press(Hid* instance, uint16_t event) {
furi_assert(instance);
ble_profile_hid_kb_press(instance->ble_hid_profile, event);
}
void hid_hal_keyboard_release(Hid* instance, uint16_t event) {
furi_assert(instance);
ble_profile_hid_kb_release(instance->ble_hid_profile, event);
}
void hid_hal_keyboard_release_all(Hid* instance) {
furi_assert(instance);
ble_profile_hid_kb_release_all(instance->ble_hid_profile);
}
void hid_hal_consumer_key_press(Hid* instance, uint16_t event) {
furi_assert(instance);
ble_profile_hid_consumer_key_press(instance->ble_hid_profile, event);
}
void hid_hal_consumer_key_release(Hid* instance, uint16_t event) {
furi_assert(instance);
ble_profile_hid_consumer_key_release(instance->ble_hid_profile, event);
}
void hid_hal_consumer_key_release_all(Hid* instance) {
furi_assert(instance);
ble_profile_hid_consumer_key_release_all(instance->ble_hid_profile);
}
void hid_hal_mouse_move(Hid* instance, int8_t dx, int8_t dy) {
furi_assert(instance);
ble_profile_hid_mouse_move(instance->ble_hid_profile, dx, dy);
}
void hid_hal_mouse_scroll(Hid* instance, int8_t delta) {
furi_assert(instance);
ble_profile_hid_mouse_scroll(instance->ble_hid_profile, delta);
}
void hid_hal_mouse_press(Hid* instance, uint16_t event) {
furi_assert(instance);
ble_profile_hid_mouse_press(instance->ble_hid_profile, event);
}
void hid_hal_mouse_release(Hid* instance, uint16_t event) {
furi_assert(instance);
ble_profile_hid_mouse_release(instance->ble_hid_profile, event);
}
void hid_hal_mouse_release_all(Hid* instance) {
furi_assert(instance);
ble_profile_hid_mouse_release_all(instance->ble_hid_profile);
}

View File

@@ -0,0 +1,61 @@
#include "hid.h"
#ifndef HID_TRANSPORT_USB
#error "HID_TRANSPORT_USB must be defined"
#endif
void hid_hal_keyboard_press(Hid* instance, uint16_t event) {
furi_assert(instance);
furi_hal_hid_kb_press(event);
}
void hid_hal_keyboard_release(Hid* instance, uint16_t event) {
furi_assert(instance);
furi_hal_hid_kb_release(event);
}
void hid_hal_keyboard_release_all(Hid* instance) {
furi_assert(instance);
furi_hal_hid_kb_release_all();
}
void hid_hal_consumer_key_press(Hid* instance, uint16_t event) {
furi_assert(instance);
furi_hal_hid_consumer_key_press(event);
}
void hid_hal_consumer_key_release(Hid* instance, uint16_t event) {
furi_assert(instance);
furi_hal_hid_consumer_key_release(event);
}
void hid_hal_consumer_key_release_all(Hid* instance) {
furi_assert(instance);
furi_hal_hid_kb_release_all();
}
void hid_hal_mouse_move(Hid* instance, int8_t dx, int8_t dy) {
furi_assert(instance);
furi_hal_hid_mouse_move(dx, dy);
}
void hid_hal_mouse_scroll(Hid* instance, int8_t delta) {
furi_assert(instance);
furi_hal_hid_mouse_scroll(delta);
}
void hid_hal_mouse_press(Hid* instance, uint16_t event) {
furi_assert(instance);
furi_hal_hid_mouse_press(event);
}
void hid_hal_mouse_release(Hid* instance, uint16_t event) {
furi_assert(instance);
furi_hal_hid_mouse_release(event);
}
void hid_hal_mouse_release_all(Hid* instance) {
furi_assert(instance);
furi_hal_hid_mouse_release(HID_MOUSE_BTN_LEFT);
furi_hal_hid_mouse_release(HID_MOUSE_BTN_RIGHT);
}

View File

@@ -1,7 +1,7 @@
#include "hid_media.h"
#include <furi.h>
#include <furi_hal_bt_hid.h>
#include <furi_hal_usb_hid.h>
#include <extra_profiles/hid_profile.h>
#include <gui/elements.h>
#include "../hid.h"