Skip to content

Commit

Permalink
FindMy: Simpler beacon state sync
Browse files Browse the repository at this point in the history
  • Loading branch information
Willy-JL committed Mar 8, 2024
1 parent 5d45d6a commit 61b35e0
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
5 changes: 2 additions & 3 deletions applications/system/findmy/findmy.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,13 @@ void findmy_change_broadcast_interval(FindMy* app, uint8_t value) {
return;
}
app->state.broadcast_interval = value;
findmy_state_sync_config(&app->state);
findmy_state_save(&app->state);
findmy_main_update_interval(app->findmy_main, app->state.broadcast_interval);
if(furi_hal_bt_extra_beacon_is_active()) {
// Always check if beacon is active before changing config
furi_check(furi_hal_bt_extra_beacon_stop());
}
app->state.config.min_adv_interval_ms = app->state.broadcast_interval * 1000;
app->state.config.max_adv_interval_ms = app->state.config.min_adv_interval_ms + 150;
furi_check(furi_hal_bt_extra_beacon_set_config(&app->state.config));
if(app->state.beacon_active) {
furi_check(furi_hal_bt_extra_beacon_start());
Expand All @@ -109,11 +108,11 @@ void findmy_change_transmit_power(FindMy* app, uint8_t value) {
return;
}
app->state.transmit_power = value;
findmy_state_sync_config(&app->state);
findmy_state_save(&app->state);
if(furi_hal_bt_extra_beacon_is_active()) {
furi_check(furi_hal_bt_extra_beacon_stop());
}
app->state.config.adv_power_level = GapAdvPowerLevel_0dBm + app->state.transmit_power;
furi_check(furi_hal_bt_extra_beacon_set_config(&app->state.config));
if(app->state.beacon_active) {
furi_check(furi_hal_bt_extra_beacon_start());
Expand Down
14 changes: 10 additions & 4 deletions applications/system/findmy/findmy_state.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,11 @@ bool findmy_state_load(FindMyState* out_state) {
}

// Sync values to config
state.config.min_adv_interval_ms = state.broadcast_interval * 1000; // Converting s to ms
state.config.max_adv_interval_ms = (state.broadcast_interval * 1000) + 150;
findmy_state_sync_config(&state);

// Set constants
state.config.adv_channel_map = GapAdvChannelMapAll;
state.config.adv_power_level = GapAdvPowerLevel_0dBm + state.transmit_power;
state.config.address_type = GapAddressTypePublic;
memcpy(state.config.address, state.mac, sizeof(state.config.address));

// Copy to caller state before popping stack
memcpy(out_state, &state, sizeof(state));
Expand All @@ -96,6 +95,13 @@ void findmy_state_apply(FindMyState* state) {
}
}

void findmy_state_sync_config(FindMyState* state) {
state.config.min_adv_interval_ms = state.broadcast_interval * 1000; // Converting s to ms
state.config.max_adv_interval_ms = (state.broadcast_interval * 1000) + 150;
state.config.adv_power_level = GapAdvPowerLevel_0dBm + state.transmit_power;
memcpy(state.config.address, state.mac, sizeof(state.config.address));
}

void findmy_state_save(FindMyState* state) {
Storage* storage = furi_record_open(RECORD_STORAGE);
storage_simply_mkdir(storage, FINDMY_STATE_DIR);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ bool findmy_scene_config_mac_on_event(void* context, SceneManagerEvent event) {
case ByteInputResultOk:
furi_hal_bt_reverse_mac_addr(app->mac_buf);
memcpy(&app->state.mac, app->mac_buf, sizeof(app->state.mac));
findmy_state_sync_config(&app->state);
findmy_state_save(&app->state);
memcpy(&app->state.config.address, app->mac_buf, sizeof(app->state.config.address));
if(furi_hal_bt_extra_beacon_is_active()) {
furi_check(furi_hal_bt_extra_beacon_stop());
}
Expand Down

0 comments on commit 61b35e0

Please sign in to comment.