mirror of
https://github.com/Next-Flip/Momentum-Firmware.git
synced 2026-04-24 03:29:57 -07:00
FindMy: Fix battery level edge cases (eg. at boot) and cleanup
- Generate config struct only when necessary - Centralize last few things to use state values - Uniform state apply and save process - Simplify battery level handling
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
#include "findmy_i.h"
|
||||
#include <furi_hal_power.h>
|
||||
|
||||
static bool findmy_custom_event_callback(void* context, uint32_t event) {
|
||||
furi_assert(context);
|
||||
@@ -52,7 +51,7 @@ static FindMy* findmy_app_alloc() {
|
||||
findmy_state_load(&app->state);
|
||||
findmy_state_apply(&app->state);
|
||||
|
||||
findmy_main_update_active(app->findmy_main, furi_hal_bt_extra_beacon_is_active());
|
||||
findmy_main_update_active(app->findmy_main, app->state.beacon_active);
|
||||
findmy_main_update_interval(app->findmy_main, app->state.broadcast_interval);
|
||||
findmy_main_toggle_mac(app->findmy_main, app->state.show_mac);
|
||||
findmy_main_update_mac(app->findmy_main, app->state.mac);
|
||||
@@ -104,7 +103,7 @@ void findmy_change_broadcast_interval(FindMy* app, uint8_t value) {
|
||||
}
|
||||
app->state.broadcast_interval = value;
|
||||
findmy_main_update_interval(app->findmy_main, app->state.broadcast_interval);
|
||||
findmy_state_save_and_apply(app, &app->state);
|
||||
findmy_state_save_and_apply(&app->state);
|
||||
}
|
||||
|
||||
void findmy_change_transmit_power(FindMy* app, uint8_t value) {
|
||||
@@ -112,57 +111,27 @@ void findmy_change_transmit_power(FindMy* app, uint8_t value) {
|
||||
return;
|
||||
}
|
||||
app->state.transmit_power = value;
|
||||
findmy_state_save_and_apply(app, &app->state);
|
||||
findmy_state_save_and_apply(&app->state);
|
||||
}
|
||||
|
||||
void findmy_toggle_show_mac(FindMy* app, bool show_mac) {
|
||||
app->state.show_mac = show_mac;
|
||||
findmy_main_toggle_mac(app->findmy_main, app->state.show_mac);
|
||||
findmy_state_save_and_apply(app, &app->state);
|
||||
findmy_state_save_and_apply(&app->state);
|
||||
}
|
||||
|
||||
void findmy_toggle_beacon(FindMy* app) {
|
||||
app->state.beacon_active = !app->state.beacon_active;
|
||||
findmy_state_save_and_apply(app, &app->state);
|
||||
findmy_main_update_active(app->findmy_main, furi_hal_bt_extra_beacon_is_active());
|
||||
findmy_state_save_and_apply(&app->state);
|
||||
findmy_main_update_active(app->findmy_main, app->state.beacon_active);
|
||||
}
|
||||
|
||||
void findmy_set_tag_type(FindMy* app, FindMyType type) {
|
||||
app->state.tag_type = type;
|
||||
findmy_state_save_and_apply(app, &app->state);
|
||||
findmy_state_save_and_apply(&app->state);
|
||||
findmy_main_update_type(app->findmy_main, type);
|
||||
}
|
||||
|
||||
void findmy_state_save_and_apply(FindMy* app, FindMyState* state) {
|
||||
uint32_t battery_capacity = furi_hal_power_get_battery_full_capacity();
|
||||
uint32_t battery_remaining = furi_hal_power_get_battery_remaining_capacity();
|
||||
uint16_t battery_percent = (battery_remaining * 100) / battery_capacity;
|
||||
uint8_t battery_level;
|
||||
|
||||
if(battery_percent > 80) {
|
||||
battery_level = BATTERY_FULL;
|
||||
} else if(battery_percent > 50) {
|
||||
battery_level = BATTERY_MEDIUM;
|
||||
} else if(battery_percent > 20) {
|
||||
battery_level = BATTERY_LOW;
|
||||
} else {
|
||||
battery_level = BATTERY_CRITICAL;
|
||||
}
|
||||
app->state.battery_level = battery_level;
|
||||
|
||||
if(furi_hal_bt_extra_beacon_is_active()) {
|
||||
furi_check(furi_hal_bt_extra_beacon_stop());
|
||||
}
|
||||
furi_check(
|
||||
furi_hal_bt_extra_beacon_set_data(state->data, findmy_state_data_size(state->tag_type)));
|
||||
findmy_state_sync_config(state);
|
||||
findmy_state_save(state);
|
||||
furi_check(furi_hal_bt_extra_beacon_set_config(&state->config));
|
||||
if(state->beacon_active) {
|
||||
furi_check(furi_hal_bt_extra_beacon_start());
|
||||
}
|
||||
}
|
||||
|
||||
void furi_hal_bt_reverse_mac_addr(uint8_t mac_addr[GAP_MAC_ADDR_SIZE]) {
|
||||
uint8_t tmp;
|
||||
for(size_t i = 0; i < GAP_MAC_ADDR_SIZE / 2; i++) {
|
||||
|
||||
@@ -54,4 +54,3 @@ void findmy_change_transmit_power(FindMy* app, uint8_t value);
|
||||
void findmy_toggle_show_mac(FindMy* app, bool show_mac);
|
||||
void findmy_set_tag_type(FindMy* app, FindMyType type);
|
||||
void findmy_toggle_beacon(FindMy* app);
|
||||
void findmy_state_save_and_apply(FindMy* app, FindMyState* state);
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
#include <string.h>
|
||||
#include <stddef.h>
|
||||
#include <furi_hal_bt.h>
|
||||
#include <furi_hal_power.h>
|
||||
#include <flipper_format/flipper_format.h>
|
||||
|
||||
bool findmy_state_load(FindMyState* out_state) {
|
||||
@@ -82,13 +83,6 @@ bool findmy_state_load(FindMyState* out_state) {
|
||||
*data++ = 0x00; // Hint (0x00)
|
||||
}
|
||||
|
||||
// Sync values to config
|
||||
findmy_state_sync_config(&state);
|
||||
|
||||
// Set constants
|
||||
state.config.adv_channel_map = GapAdvChannelMapAll;
|
||||
state.config.address_type = GapAddressTypePublic;
|
||||
|
||||
// Copy to caller state before popping stack
|
||||
memcpy(out_state, &state, sizeof(state));
|
||||
|
||||
@@ -96,53 +90,58 @@ bool findmy_state_load(FindMyState* out_state) {
|
||||
return state.beacon_active;
|
||||
}
|
||||
|
||||
static void findmy_state_update_payload_battery(FindMyState* state) {
|
||||
// Update the battery level in the payload
|
||||
if(state->tag_type == FindMyTypeApple) {
|
||||
uint32_t battery_capacity = furi_hal_power_get_battery_full_capacity();
|
||||
uint32_t battery_remaining = furi_hal_power_get_battery_remaining_capacity();
|
||||
uint8_t battery_percent = (battery_remaining * 100) / battery_capacity;
|
||||
uint8_t battery_level;
|
||||
|
||||
if(battery_percent > 80) {
|
||||
battery_level = BATTERY_FULL;
|
||||
} else if(battery_percent > 50) {
|
||||
battery_level = BATTERY_MEDIUM;
|
||||
} else if(battery_percent > 20) {
|
||||
battery_level = BATTERY_LOW;
|
||||
} else {
|
||||
battery_level = BATTERY_CRITICAL;
|
||||
}
|
||||
state->data[6] = battery_level;
|
||||
}
|
||||
}
|
||||
|
||||
void findmy_state_apply(FindMyState* state) {
|
||||
// This function applies initial state to the beacon (loaded values)
|
||||
// This function applies configured state to the beacon (loaded values)
|
||||
|
||||
// Stop beacon before configuring
|
||||
if(furi_hal_bt_extra_beacon_is_active()) {
|
||||
furi_check(furi_hal_bt_extra_beacon_stop());
|
||||
}
|
||||
furi_check(furi_hal_bt_extra_beacon_set_config(&state->config));
|
||||
findmy_update_payload_battery(state->data, state->battery_level, state->tag_type);
|
||||
|
||||
// Make config struct from configured parameters and set it
|
||||
GapExtraBeaconConfig config = {
|
||||
.min_adv_interval_ms = state->broadcast_interval * 1000, // Converting s to ms
|
||||
.max_adv_interval_ms = (state->broadcast_interval * 1000) + 150,
|
||||
.adv_channel_map = GapAdvChannelMapAll,
|
||||
.adv_power_level = GapAdvPowerLevel_0dBm + state->transmit_power,
|
||||
.address_type = GapAddressTypePublic,
|
||||
};
|
||||
memcpy(config.address, state->mac, sizeof(config.address));
|
||||
furi_check(furi_hal_bt_extra_beacon_set_config(&config));
|
||||
|
||||
// Update data payload with battery level and set it
|
||||
findmy_state_update_payload_battery(state);
|
||||
furi_check(
|
||||
furi_hal_bt_extra_beacon_set_data(state->data, findmy_state_data_size(state->tag_type)));
|
||||
|
||||
// Start beacon if configured
|
||||
if(state->beacon_active) {
|
||||
furi_check(furi_hal_bt_extra_beacon_start());
|
||||
}
|
||||
}
|
||||
|
||||
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));
|
||||
findmy_update_payload_battery(state->data, state->battery_level, state->tag_type);
|
||||
}
|
||||
|
||||
void findmy_update_payload_battery(uint8_t* data, uint8_t battery_level, FindMyType type) {
|
||||
// Update the battery level in the payload
|
||||
if(type == FindMyTypeApple) {
|
||||
switch(battery_level) {
|
||||
case BATTERY_FULL:
|
||||
data[6] = BATTERY_FULL;
|
||||
break;
|
||||
case BATTERY_MEDIUM:
|
||||
data[6] = BATTERY_MEDIUM;
|
||||
break;
|
||||
case BATTERY_LOW:
|
||||
data[6] = BATTERY_LOW;
|
||||
break;
|
||||
case BATTERY_CRITICAL:
|
||||
data[6] = BATTERY_CRITICAL;
|
||||
break;
|
||||
default:
|
||||
FURI_LOG_E("update_bat", "Invalid battery level: %d", battery_level);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void findmy_state_save(FindMyState* state) {
|
||||
static void findmy_state_save(FindMyState* state) {
|
||||
Storage* storage = furi_record_open(RECORD_STORAGE);
|
||||
storage_simply_mkdir(storage, FINDMY_STATE_DIR);
|
||||
FlipperFormat* file = flipper_format_file_alloc(storage);
|
||||
@@ -161,13 +160,11 @@ void findmy_state_save(FindMyState* state) {
|
||||
if(!flipper_format_write_uint32(file, "transmit_power", &tmp, 1)) break;
|
||||
|
||||
tmp = state->tag_type;
|
||||
FURI_LOG_E("tag_type at save", "%ld", tmp);
|
||||
if(!flipper_format_write_uint32(file, "tag_type", &tmp, 1)) break;
|
||||
|
||||
if(!flipper_format_write_bool(file, "show_mac", &state->show_mac, 1)) break;
|
||||
|
||||
if(!flipper_format_write_hex(file, "mac", state->mac, sizeof(state->mac))) break;
|
||||
findmy_update_payload_battery(state->data, state->battery_level, state->tag_type);
|
||||
if(!flipper_format_write_hex(
|
||||
file, "data", state->data, findmy_state_data_size(state->tag_type)))
|
||||
break;
|
||||
@@ -176,6 +173,11 @@ void findmy_state_save(FindMyState* state) {
|
||||
furi_record_close(RECORD_STORAGE);
|
||||
}
|
||||
|
||||
void findmy_state_save_and_apply(FindMyState* state) {
|
||||
findmy_state_apply(state);
|
||||
findmy_state_save(state);
|
||||
}
|
||||
|
||||
uint8_t findmy_state_data_size(FindMyType type) {
|
||||
switch(type) {
|
||||
case FindMyTypeApple:
|
||||
|
||||
@@ -26,21 +26,12 @@ typedef struct {
|
||||
uint8_t mac[EXTRA_BEACON_MAC_ADDR_SIZE];
|
||||
uint8_t data[EXTRA_BEACON_MAX_DATA_SIZE];
|
||||
FindMyType tag_type;
|
||||
|
||||
// Generated from the other state values
|
||||
GapExtraBeaconConfig config;
|
||||
|
||||
uint8_t battery_level;
|
||||
} FindMyState;
|
||||
|
||||
bool findmy_state_load(FindMyState* out_state);
|
||||
|
||||
void findmy_state_apply(FindMyState* state);
|
||||
|
||||
void findmy_state_sync_config(FindMyState* state);
|
||||
|
||||
void findmy_state_save(FindMyState* state);
|
||||
|
||||
void findmy_update_payload_battery(uint8_t* data, uint8_t battery_level, FindMyType type);
|
||||
void findmy_state_save_and_apply(FindMyState* state);
|
||||
|
||||
uint8_t findmy_state_data_size(FindMyType type);
|
||||
|
||||
@@ -64,8 +64,7 @@ static const char* parse_nrf_connect(FindMy* app, const char* path) {
|
||||
|
||||
memcpy(app->state.mac, mac, sizeof(app->state.mac));
|
||||
memcpy(app->state.data, data, sizeof(app->state.data));
|
||||
findmy_state_sync_config(&app->state);
|
||||
findmy_state_save(&app->state);
|
||||
findmy_state_save_and_apply(&app->state);
|
||||
|
||||
error = NULL;
|
||||
|
||||
@@ -126,8 +125,7 @@ static const char* parse_open_haystack(FindMy* app, const char* path) {
|
||||
memcpy(app->state.data, advertisement_template, sizeof(app->state.data));
|
||||
memcpy(&app->state.data[7], &public_key[6], decoded_len - 6);
|
||||
app->state.data[29] = public_key[0] >> 6;
|
||||
findmy_state_sync_config(&app->state);
|
||||
findmy_state_save(&app->state);
|
||||
findmy_state_save_and_apply(&app->state);
|
||||
|
||||
free(public_key);
|
||||
error = NULL;
|
||||
|
||||
@@ -29,7 +29,7 @@ void findmy_scene_config_import_result_on_enter(void* context) {
|
||||
popup_set_timeout(popup, 1500);
|
||||
popup_set_context(popup, app);
|
||||
popup_set_callback(popup, findmy_scene_config_import_result_callback);
|
||||
findmy_main_update_active(app->findmy_main, furi_hal_bt_extra_beacon_is_active());
|
||||
findmy_main_update_active(app->findmy_main, app->state.beacon_active);
|
||||
findmy_main_update_mac(app->findmy_main, app->state.mac);
|
||||
findmy_main_update_type(app->findmy_main, app->state.tag_type);
|
||||
view_dispatcher_switch_to_view(app->view_dispatcher, FindMyViewPopup);
|
||||
|
||||
@@ -40,15 +40,7 @@ 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);
|
||||
if(furi_hal_bt_extra_beacon_is_active()) {
|
||||
furi_check(furi_hal_bt_extra_beacon_stop());
|
||||
}
|
||||
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());
|
||||
}
|
||||
findmy_state_save_and_apply(&app->state);
|
||||
findmy_main_update_mac(app->findmy_main, app->state.mac);
|
||||
scene_manager_next_scene(app->scene_manager, FindMySceneConfigPacket);
|
||||
break;
|
||||
|
||||
@@ -40,9 +40,7 @@ bool findmy_scene_config_packet_on_event(void* context, SceneManagerEvent event)
|
||||
scene_manager_search_and_switch_to_previous_scene(
|
||||
app->scene_manager, FindMySceneConfig);
|
||||
memcpy(app->state.data, app->packet_buf, findmy_state_data_size(app->state.tag_type));
|
||||
findmy_state_save(&app->state);
|
||||
furi_check(furi_hal_bt_extra_beacon_set_data(
|
||||
app->state.data, findmy_state_data_size(app->state.tag_type)));
|
||||
findmy_state_save_and_apply(&app->state);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
|
||||
@@ -25,10 +25,7 @@ bool findmy_scene_main_on_event(void* context, SceneManagerEvent event) {
|
||||
break;
|
||||
case FindMyMainEventBackground:
|
||||
app->state.beacon_active = true;
|
||||
findmy_state_save(&app->state);
|
||||
if(!furi_hal_bt_extra_beacon_is_active()) {
|
||||
furi_check(furi_hal_bt_extra_beacon_start());
|
||||
}
|
||||
findmy_state_save_and_apply(&app->state);
|
||||
view_dispatcher_stop(app->view_dispatcher);
|
||||
break;
|
||||
case FindMyMainEventConfig:
|
||||
@@ -42,10 +39,7 @@ bool findmy_scene_main_on_event(void* context, SceneManagerEvent event) {
|
||||
break;
|
||||
case FindMyMainEventQuit:
|
||||
app->state.beacon_active = false;
|
||||
findmy_state_save(&app->state);
|
||||
if(furi_hal_bt_extra_beacon_is_active()) {
|
||||
furi_check(furi_hal_bt_extra_beacon_stop());
|
||||
}
|
||||
findmy_state_save_and_apply(&app->state);
|
||||
break;
|
||||
default:
|
||||
consumed = false;
|
||||
|
||||
Reference in New Issue
Block a user