Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change bt advertise name #530

Draft
wants to merge 10 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions applications/services/bt/bt_service/bt.c
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,13 @@ int32_t bt_srv(void* p) {
FURI_LOG_E(TAG, "BLE App start failed");
} else {
if(bt->bt_settings.enabled) {
if(bt->bt_settings.advertise_type != BtAdvAll) {
const char* advname = "\0";
if(bt->bt_settings.advertise_type == BtAdvName) {
advname = furi_hal_version_get_name_ptr();
}
furi_hal_bt_set_profile_adv_name(FuriHalBtProfileSerial, advname);
}
furi_hal_bt_start_advertising();
}
furi_hal_bt_set_key_storage_change_callback(bt_on_key_storage_change_callback, bt);
Expand Down
7 changes: 7 additions & 0 deletions applications/services/bt/bt_settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,15 @@
extern "C" {
#endif

enum BtAdvType {
BtAdvAll, // Flipper + Name
BtAdvName, // Only Name
BtAdvNum,
};

typedef struct {
bool enabled;
enum BtAdvType advertise_type;
} BtSettings;

bool bt_settings_load(BtSettings* bt_settings);
Expand Down
2 changes: 2 additions & 0 deletions applications/settings/bt_settings_app/bt_settings_app.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ enum BtSettingsCustomEvent {
// Keep first 10 events reserved for button types and indexes
BtSettingsCustomEventReserved = 10,

BtSettingsCustomEventBtEnable,
BtSettingsCustomEventBtDisable,
BtSettingsCustomEventForgetDevices,
BtSettingsCustomEventExitView,
};
Expand Down
Copy link
Contributor

@derskythe derskythe Jul 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Too many const

Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ enum BtSetting {

enum BtSettingIndex {
BtSettingIndexSwitchBt,
BtSettingIndexSwitchAdv,
BtSettingIndexForgetDev,
};

Expand All @@ -17,12 +18,29 @@ const char* const bt_settings_text[BtSettingNum] = {
"ON",
};

static void bt_settings_scene_start_var_list_change_callback(VariableItem* item) {
const char* const bt_advertise_text[BtAdvNum] = {
"All",
"Name",
};

static void bt_settings_scene_start_set_switch_callback(VariableItem* item) {
BtSettingsApp* app = variable_item_get_context(item);
uint8_t index = variable_item_get_current_value_index(item);

variable_item_set_current_value_text(item, bt_settings_text[index]);
view_dispatcher_send_custom_event(app->view_dispatcher, index);
if(index == BtSettingOff) {
view_dispatcher_send_custom_event(app->view_dispatcher, BtSettingsCustomEventBtDisable);
} else {
view_dispatcher_send_custom_event(app->view_dispatcher, BtSettingsCustomEventBtEnable);
}
}

static void bt_settings_scene_start_set_advertise_callback(VariableItem* item) {
BtSettingsApp* app = variable_item_get_context(item);
uint8_t index = variable_item_get_current_value_index(item);
variable_item_set_current_value_text(item, bt_advertise_text[index]);
app->settings.advertise_type = index;
view_dispatcher_send_custom_event(app->view_dispatcher, BtSettingsCustomEventBtEnable);
}

static void bt_settings_scene_start_var_list_enter_callback(void* context, uint32_t index) {
Expand All @@ -44,15 +62,22 @@ void bt_settings_scene_start_on_enter(void* context) {
var_item_list,
"Bluetooth",
BtSettingNum,
bt_settings_scene_start_var_list_change_callback,
bt_settings_scene_start_set_switch_callback,
app);
if(app->settings.enabled) {
variable_item_set_current_value_index(item, BtSettingOn);
variable_item_set_current_value_text(item, bt_settings_text[BtSettingOn]);
} else {
variable_item_set_current_value_index(item, BtSettingOff);
variable_item_set_current_value_text(item, bt_settings_text[BtSettingOff]);
}
variable_item_set_current_value_index(item, app->settings.enabled);
variable_item_set_current_value_text(item, bt_settings_text[app->settings.enabled]);

item = variable_item_list_add(
var_item_list,
"Advertise",
BtAdvNum,
bt_settings_scene_start_set_advertise_callback,
app);
variable_item_set_current_value_index(item, app->settings.advertise_type);
variable_item_set_current_value_text(
item, bt_advertise_text[app->settings.advertise_type]);
variable_item_set_locked(item, !app->settings.enabled, "Enable BT!");

variable_item_list_add(var_item_list, "Forget All Paired Devices", 1, NULL, NULL);
variable_item_list_set_enter_callback(
var_item_list, bt_settings_scene_start_var_list_enter_callback, app);
Expand All @@ -69,13 +94,28 @@ bool bt_settings_scene_start_on_event(void* context, SceneManagerEvent event) {
bool consumed = false;

if(event.type == SceneManagerEventTypeCustom) {
if(event.event == BtSettingOn) {
if(event.event == BtSettingsCustomEventBtEnable) {
if(app->settings.advertise_type == BtAdvAll) {
furi_hal_bt_set_profile_adv_name(
FuriHalBtProfileSerial, furi_hal_version_get_device_name_ptr());
} else {
const char* advname = "\0";
if(app->settings.advertise_type == BtAdvName) {
advname = furi_hal_version_get_name_ptr();
}
furi_hal_bt_set_profile_adv_name(FuriHalBtProfileSerial, advname);
}
furi_hal_bt_stop_advertising();
furi_hal_bt_start_advertising();
app->settings.enabled = true;
scene_manager_previous_scene(app->scene_manager);
scene_manager_next_scene(app->scene_manager, BtSettingsAppSceneStart);
consumed = true;
} else if(event.event == BtSettingOff) {
} else if(event.event == BtSettingsCustomEventBtDisable) {
app->settings.enabled = false;
furi_hal_bt_stop_advertising();
scene_manager_previous_scene(app->scene_manager);
scene_manager_next_scene(app->scene_manager, BtSettingsAppSceneStart);
consumed = true;
} else if(event.event == BtSettingsCustomEventForgetDevices) {
scene_manager_next_scene(app->scene_manager, BtSettingsAppSceneForgetDevConfirm);
Expand Down
15 changes: 6 additions & 9 deletions firmware/targets/f7/furi_hal/furi_hal_bt.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ FuriHalBtProfileConfig profile_config[FuriHalBtProfileNumber] = {
.bonding_mode = true,
.pairing_method = GapPairingPinCodeShow,
.mac_address = FURI_HAL_BT_DEFAULT_MAC_ADDR,
.adv_name = "\0",
.conn_param =
{
.conn_int_min = 0x18, // 30 ms
Expand All @@ -75,7 +74,6 @@ FuriHalBtProfileConfig profile_config[FuriHalBtProfileNumber] = {
.bonding_mode = true,
.pairing_method = GapPairingPinCodeVerifyYesNo,
.mac_address = FURI_HAL_BT_DEFAULT_MAC_ADDR,
.adv_name = "\0",
.conn_param =
{
.conn_int_min = 0x18, // 30 ms
Expand Down Expand Up @@ -233,12 +231,11 @@ bool furi_hal_bt_start_app(FuriHalBtProfile profile, GapEventCallback event_cb,
// Set mac address
memcpy(
config->mac_address, furi_hal_version_get_ble_mac(), sizeof(config->mac_address));
// Set stock advertise name only once on bootup
if(!strnlen(config->adv_name, FURI_HAL_BT_ADV_NAME_LENGTH))
strlcpy(
config->adv_name,
furi_hal_version_get_ble_local_device_name_ptr(),
FURI_HAL_BT_ADV_NAME_LENGTH);
// Set advertise name
strlcpy(
config->adv_name,
furi_hal_version_get_ble_local_device_name_ptr(),
FURI_HAL_BT_ADV_NAME_LENGTH);

config->adv_service_uuid |= furi_hal_version_get_hw_color();
} else if(profile == FuriHalBtProfileHidKeyboard) {
Expand Down Expand Up @@ -501,7 +498,7 @@ void furi_hal_bt_set_profile_adv_name(
furi_assert(profile < FuriHalBtProfileNumber);
furi_assert(name);

if(strlen(name) == 0) {
if(!name || !strlen(name)) {
memset(&(profile_config[profile].config.adv_name[1]), 0, FURI_HAL_BT_ADV_NAME_LENGTH - 1);
} else {
profile_config[profile].config.adv_name[0] = AD_TYPE_COMPLETE_LOCAL_NAME;
Expand Down