From 4341a567a79dee8b3dae7a35f05bd9dbe7ce84c1 Mon Sep 17 00:00:00 2001 From: MX <10697207+xMasterX@users.noreply.github.com> Date: Tue, 23 Apr 2024 13:02:35 +0300 Subject: [PATCH] upd findmyflipper --- base_pack/find_my_flipper/findmy.c | 67 ++++++++++--------- base_pack/find_my_flipper/findmy_i.h | 1 + base_pack/find_my_flipper/findmy_state.c | 36 ++++++++-- base_pack/find_my_flipper/findmy_state.h | 9 +++ .../findmy_scene_config_import_result.c | 4 +- 5 files changed, 78 insertions(+), 39 deletions(-) diff --git a/base_pack/find_my_flipper/findmy.c b/base_pack/find_my_flipper/findmy.c index 1cafcf8cbdc..26381351dce 100644 --- a/base_pack/find_my_flipper/findmy.c +++ b/base_pack/find_my_flipper/findmy.c @@ -1,4 +1,5 @@ #include "findmy_i.h" +#include static bool findmy_custom_event_callback(void* context, uint32_t event) { furi_assert(context); @@ -102,17 +103,8 @@ 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()); - } - 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, &app->state); } void findmy_change_transmit_power(FindMy* app, uint8_t value) { @@ -120,42 +112,55 @@ 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()); - } - 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, &app->state); } void findmy_toggle_show_mac(FindMy* app, bool show_mac) { app->state.show_mac = show_mac; - findmy_state_sync_config(&app->state); - findmy_state_save(&app->state); findmy_main_toggle_mac(app->findmy_main, app->state.show_mac); + findmy_state_save_and_apply(app, &app->state); } void findmy_toggle_beacon(FindMy* app) { app->state.beacon_active = !app->state.beacon_active; - findmy_state_save(&app->state); - if(furi_hal_bt_extra_beacon_is_active()) { - furi_check(furi_hal_bt_extra_beacon_stop()); - } - if(app->state.beacon_active) { - furi_check(furi_hal_bt_extra_beacon_start()); - } + findmy_state_save_and_apply(app, &app->state); findmy_main_update_active(app->findmy_main, furi_hal_bt_extra_beacon_is_active()); } void findmy_set_tag_type(FindMy* app, FindMyType type) { app->state.tag_type = type; - findmy_state_sync_config(&app->state); - findmy_state_save(&app->state); + findmy_state_save_and_apply(app, &app->state); findmy_main_update_type(app->findmy_main, type); - FURI_LOG_I("TagType2", "Tag Type: %d", 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]) { diff --git a/base_pack/find_my_flipper/findmy_i.h b/base_pack/find_my_flipper/findmy_i.h index 450bf0dd610..c1469943ce7 100644 --- a/base_pack/find_my_flipper/findmy_i.h +++ b/base_pack/find_my_flipper/findmy_i.h @@ -53,3 +53,4 @@ 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); diff --git a/base_pack/find_my_flipper/findmy_state.c b/base_pack/find_my_flipper/findmy_state.c index ae81eab8393..8a60a312e51 100644 --- a/base_pack/find_my_flipper/findmy_state.c +++ b/base_pack/find_my_flipper/findmy_state.c @@ -74,12 +74,12 @@ bool findmy_state_load(FindMyState* out_state) { *data++ = 0x00; // ... *data++ = 0x12; // Type (FindMy) *data++ = 0x19; // Length - *data++ = 0x00; // Status + *data++ = 0x00; // Battery Status set to Full // Placeholder Empty Public Key without the MAC address for(size_t i = 0; i < 22; ++i) { *data++ = 0x00; } - *data++ = 0x00; // First 2 bits are the version, the rest is the battery level + *data++ = 0x00; // First 2 bits are the version *data++ = 0x00; // Hint (0x00) } @@ -98,16 +98,15 @@ bool findmy_state_load(FindMyState* out_state) { } void findmy_state_apply(FindMyState* state) { - // Stop any running beacon + // This function applies initial state to the beacon (loaded values) 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); furi_check( furi_hal_bt_extra_beacon_set_data(state->data, findmy_state_data_size(state->tag_type))); - if(state->beacon_active) { furi_check(furi_hal_bt_extra_beacon_start()); } @@ -118,6 +117,30 @@ void findmy_state_sync_config(FindMyState* state) { 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) { @@ -144,12 +167,11 @@ void findmy_state_save(FindMyState* state) { 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; } while(0); - flipper_format_free(file); furi_record_close(RECORD_STORAGE); } diff --git a/base_pack/find_my_flipper/findmy_state.h b/base_pack/find_my_flipper/findmy_state.h index 813dfee4147..75753135ce0 100644 --- a/base_pack/find_my_flipper/findmy_state.h +++ b/base_pack/find_my_flipper/findmy_state.h @@ -7,6 +7,11 @@ #define FINDMY_STATE_DIR EXT_PATH("apps_data/findmy") #define FINDMY_STATE_PATH FINDMY_STATE_DIR "/findmy_state.txt" +#define BATTERY_FULL 0x00 +#define BATTERY_MEDIUM 0x50 +#define BATTERY_LOW 0xA0 +#define BATTERY_CRITICAL 0xF0 + typedef enum { FindMyTypeApple, FindMyTypeSamsung, @@ -24,6 +29,8 @@ typedef struct { // Generated from the other state values GapExtraBeaconConfig config; + + uint8_t battery_level; } FindMyState; bool findmy_state_load(FindMyState* out_state); @@ -34,4 +41,6 @@ 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); + uint8_t findmy_state_data_size(FindMyType type); diff --git a/base_pack/find_my_flipper/scenes/findmy_scene_config_import_result.c b/base_pack/find_my_flipper/scenes/findmy_scene_config_import_result.c index f15f55c0693..c6cd956655c 100644 --- a/base_pack/find_my_flipper/scenes/findmy_scene_config_import_result.c +++ b/base_pack/find_my_flipper/scenes/findmy_scene_config_import_result.c @@ -29,7 +29,9 @@ 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_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); }