From 26ad6d89b83ec80d83b7c47a85804d92ba977e0e Mon Sep 17 00:00:00 2001 From: MX <10697207+xMasterX@users.noreply.github.com> Date: Wed, 20 Mar 2024 13:19:58 +0300 Subject: [PATCH] upd findmy flipper --- base_pack/find_my_flipper/application.fam | 2 +- base_pack/find_my_flipper/findmy.c | 24 +++---- base_pack/find_my_flipper/findmy.h | 2 - base_pack/find_my_flipper/findmy_i.h | 7 +- base_pack/find_my_flipper/findmy_state.c | 35 +++++++++- base_pack/find_my_flipper/findmy_state.h | 9 +++ .../scenes/findmy_scene_config.c | 14 ++-- .../scenes/findmy_scene_config_import.c | 23 ++++-- .../scenes/findmy_scene_config_packet.c | 11 ++- .../scenes/findmy_scene_config_tagtype.c | 70 +++++++++++++++++++ .../find_my_flipper/scenes/findmy_scenes.h | 1 + base_pack/find_my_flipper/views/findmy_main.c | 16 +++-- base_pack/find_my_flipper/views/findmy_main.h | 1 + 13 files changed, 160 insertions(+), 55 deletions(-) create mode 100644 base_pack/find_my_flipper/scenes/findmy_scene_config_tagtype.c diff --git a/base_pack/find_my_flipper/application.fam b/base_pack/find_my_flipper/application.fam index 2ef49310f01..48133c5a339 100644 --- a/base_pack/find_my_flipper/application.fam +++ b/base_pack/find_my_flipper/application.fam @@ -10,6 +10,6 @@ App( fap_category="Bluetooth", fap_author="@MatthewKuKanich", fap_weburl="https://github.com/MatthewKuKanich/FindMyFlipper", - fap_version="1.0", + fap_version="2.0", fap_description="BLE FindMy Location Beacon", ) diff --git a/base_pack/find_my_flipper/findmy.c b/base_pack/find_my_flipper/findmy.c index f5460e1ce62..7bb10f6b737 100644 --- a/base_pack/find_my_flipper/findmy.c +++ b/base_pack/find_my_flipper/findmy.c @@ -53,7 +53,7 @@ static FindMy* findmy_app_alloc() { findmy_main_update_active(app->findmy_main, furi_hal_bt_extra_beacon_is_active()); findmy_main_update_interval(app->findmy_main, app->state.broadcast_interval); - findmy_main_update_type(app->findmy_main, findmy_data_get_type(app->state.data)); + findmy_main_update_type(app->findmy_main, app->state.tag_type); return app; } @@ -141,21 +141,14 @@ void findmy_toggle_beacon(FindMy* app) { findmy_main_update_active(app->findmy_main, furi_hal_bt_extra_beacon_is_active()); } -FindMyType findmy_data_get_type(uint8_t data[EXTRA_BEACON_MAX_DATA_SIZE]) { - if(data[0] == 0x1E && // Length - data[1] == 0xFF && // Manufacturer Specific Data - data[2] == 0x4C && // Company ID (Apple, Inc.) - data[3] == 0x00 && // ... - data[4] == 0x12 && // Type (FindMy) - data[5] == 0x19 // Length - ) { - return FindMyTypeApple; - } else { - return FindMyTypeSamsung; - } +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_main_update_type(app->findmy_main, type); + FURI_LOG_I("TagType2", "Tag Type: %d", type); } -#if FW_ORIGIN_Official 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++) { @@ -163,5 +156,4 @@ void furi_hal_bt_reverse_mac_addr(uint8_t mac_addr[GAP_MAC_ADDR_SIZE]) { mac_addr[i] = mac_addr[GAP_MAC_ADDR_SIZE - 1 - i]; mac_addr[GAP_MAC_ADDR_SIZE - 1 - i] = tmp; } -} -#endif \ No newline at end of file +} \ No newline at end of file diff --git a/base_pack/find_my_flipper/findmy.h b/base_pack/find_my_flipper/findmy.h index a5135f2d70e..344882ef103 100644 --- a/base_pack/find_my_flipper/findmy.h +++ b/base_pack/find_my_flipper/findmy.h @@ -1,5 +1,3 @@ #pragma once typedef struct FindMy FindMy; - -typedef enum FindMyType FindMyType; \ No newline at end of file diff --git a/base_pack/find_my_flipper/findmy_i.h b/base_pack/find_my_flipper/findmy_i.h index 111c85a4797..82ca48b406c 100644 --- a/base_pack/find_my_flipper/findmy_i.h +++ b/base_pack/find_my_flipper/findmy_i.h @@ -48,12 +48,7 @@ typedef enum { FindMyViewPopup, } FindMyView; -enum FindMyType { - FindMyTypeApple, - FindMyTypeSamsung, -}; - void findmy_change_broadcast_interval(FindMy* app, uint8_t value); void findmy_change_transmit_power(FindMy* app, uint8_t value); +void findmy_set_tag_type(FindMy* app, FindMyType type); void findmy_toggle_beacon(FindMy* app); -FindMyType findmy_data_get_type(uint8_t data[EXTRA_BEACON_MAX_DATA_SIZE]); diff --git a/base_pack/find_my_flipper/findmy_state.c b/base_pack/find_my_flipper/findmy_state.c index 12e8cbed214..5528080b00b 100644 --- a/base_pack/find_my_flipper/findmy_state.c +++ b/base_pack/find_my_flipper/findmy_state.c @@ -29,9 +29,18 @@ bool findmy_state_load(FindMyState* out_state) { if(!flipper_format_read_uint32(file, "transmit_power", &tmp, 1)) break; state.transmit_power = tmp; + if(!flipper_format_read_uint32(file, "tag_type", &tmp, 1)) { + // Support migrating from old config + tmp = FindMyTypeApple; + flipper_format_rewind(file); + } + state.tag_type = tmp; + if(!flipper_format_read_hex(file, "mac", state.mac, sizeof(state.mac))) break; - if(!flipper_format_read_hex(file, "data", state.data, sizeof(state.data))) break; + if(!flipper_format_read_hex( + file, "data", state.data, findmy_state_data_size(state.tag_type))) + break; loaded_from_file = true; } while(0); @@ -45,6 +54,8 @@ bool findmy_state_load(FindMyState* out_state) { state.broadcast_interval = 5; state.transmit_power = 6; + state.tag_type = FindMyTypeApple; + // Set default mac uint8_t default_mac[EXTRA_BEACON_MAC_ADDR_SIZE] = {0x66, 0x55, 0x44, 0x33, 0x22, 0x11}; memcpy(state.mac, default_mac, sizeof(state.mac)); @@ -88,7 +99,8 @@ void findmy_state_apply(FindMyState* state) { furi_check(furi_hal_bt_extra_beacon_set_config(&state->config)); - furi_check(furi_hal_bt_extra_beacon_set_data(state->data, sizeof(state->data))); + 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()); @@ -120,11 +132,28 @@ void findmy_state_save(FindMyState* state) { tmp = state->transmit_power; if(!flipper_format_write_uint32(file, "transmit_power", &tmp, 1)) break; + tmp = state->tag_type; + if(!flipper_format_write_uint32(file, "tag_type", &tmp, 1)) break; + if(!flipper_format_write_hex(file, "mac", state->mac, sizeof(state->mac))) break; - if(!flipper_format_write_hex(file, "data", state->data, sizeof(state->data))) break; + 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); } + +uint8_t findmy_state_data_size(FindMyType type) { + switch(type) { + case FindMyTypeApple: + case FindMyTypeSamsung: + return 31; + case FindMyTypeTile: + return 21; + default: + return 0; + } +} diff --git a/base_pack/find_my_flipper/findmy_state.h b/base_pack/find_my_flipper/findmy_state.h index d11313e588f..4b25ffabb86 100644 --- a/base_pack/find_my_flipper/findmy_state.h +++ b/base_pack/find_my_flipper/findmy_state.h @@ -7,6 +7,12 @@ #define FINDMY_STATE_DIR EXT_PATH("apps_data/findmy") #define FINDMY_STATE_PATH FINDMY_STATE_DIR "/findmy_state.txt" +typedef enum { + FindMyTypeApple, + FindMyTypeSamsung, + FindMyTypeTile, +} FindMyType; + typedef struct { bool beacon_active; uint8_t broadcast_interval; @@ -14,6 +20,7 @@ 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; @@ -26,3 +33,5 @@ void findmy_state_apply(FindMyState* state); void findmy_state_sync_config(FindMyState* state); void findmy_state_save(FindMyState* state); + +uint8_t findmy_state_data_size(FindMyType type); diff --git a/base_pack/find_my_flipper/scenes/findmy_scene_config.c b/base_pack/find_my_flipper/scenes/findmy_scene_config.c index eba1a85a2b9..13c671c8da6 100644 --- a/base_pack/find_my_flipper/scenes/findmy_scene_config.c +++ b/base_pack/find_my_flipper/scenes/findmy_scene_config.c @@ -3,8 +3,7 @@ enum VarItemListIndex { VarItemListIndexBroadcastInterval, VarItemListIndexTransmitPower, - VarItemListIndexImportTagFromFile, - VarItemListIndexRegisterTagManually, + VarItemListIndexRegisterTag, VarItemListIndexAbout, }; @@ -58,9 +57,7 @@ void findmy_scene_config_on_enter(void* context) { snprintf(power_str, sizeof(power_str), "%ddBm", app->state.transmit_power); variable_item_set_current_value_text(item, power_str); - item = variable_item_list_add(var_item_list, "Import Tag From File", 0, NULL, NULL); - - item = variable_item_list_add(var_item_list, "Register Tag Manually", 0, NULL, NULL); + item = variable_item_list_add(var_item_list, "Register Tag", 0, NULL, NULL); item = variable_item_list_add( var_item_list, @@ -86,11 +83,8 @@ bool findmy_scene_config_on_event(void* context, SceneManagerEvent event) { scene_manager_set_scene_state(app->scene_manager, FindMySceneConfig, event.event); consumed = true; switch(event.event) { - case VarItemListIndexImportTagFromFile: - scene_manager_next_scene(app->scene_manager, FindMySceneConfigImport); - break; - case VarItemListIndexRegisterTagManually: - scene_manager_next_scene(app->scene_manager, FindMySceneConfigMac); + case VarItemListIndexRegisterTag: + scene_manager_next_scene(app->scene_manager, FindMySceneConfigTagtype); break; case VarItemListIndexAbout: break; diff --git a/base_pack/find_my_flipper/scenes/findmy_scene_config_import.c b/base_pack/find_my_flipper/scenes/findmy_scene_config_import.c index aedd210668a..c0dd55bab7f 100644 --- a/base_pack/find_my_flipper/scenes/findmy_scene_config_import.c +++ b/base_pack/find_my_flipper/scenes/findmy_scene_config_import.c @@ -3,6 +3,7 @@ enum VarItemListIndex { VarItemListIndexNrfConnect, VarItemListIndexOpenHaystack, + VarItemListIndexRegisterTagManually, }; static const char* parse_nrf_connect(FindMy* app, const char* path) { @@ -42,10 +43,15 @@ static const char* parse_nrf_connect(FindMy* app, const char* path) { furi_string_trim(line); error = "Wrong payload size"; - uint8_t data[EXTRA_BEACON_MAX_DATA_SIZE]; - if(furi_string_size(line) != sizeof(data) * 2) break; + size_t line_size = furi_string_size(line); + uint8_t data_size = findmy_state_data_size(app->state.tag_type); + FURI_LOG_I("ImportPayload", "Line Size: %d", line_size); + FURI_LOG_I("ImportPayload", "Data Size: %d", data_size * 2); + if(line_size != data_size * 2) break; + // Initialize full data to 0's, then fill only first data_size bytes + uint8_t data[EXTRA_BEACON_MAX_DATA_SIZE] = {0}; error = NULL; - for(size_t i = 0; i < sizeof(data); i++) { + for(size_t i = 0; i < data_size; i++) { char a = furi_string_get_char(line, i * 2); char b = furi_string_get_char(line, i * 2 + 1); if((a < 'A' && a > 'F') || (a < '0' && a > '9') || (b < 'A' && b > 'F') || @@ -145,9 +151,13 @@ void findmy_scene_config_import_on_enter(void* context) { VariableItemList* var_item_list = app->var_item_list; VariableItem* item; - item = variable_item_list_add(var_item_list, "nRF Connect .txt", 0, NULL, NULL); + //variable_item_list_set_header(var_item_list, "Choose file type"); - item = variable_item_list_add(var_item_list, "OpenHaystack .keys", 0, NULL, NULL); + item = variable_item_list_add(var_item_list, "nRF Connect (.txt)", 0, NULL, NULL); + + item = variable_item_list_add(var_item_list, "OpenHaystack (.keys)", 0, NULL, NULL); + + item = variable_item_list_add(var_item_list, "Register Tag Manually", 0, NULL, NULL); // This scene acts more like a submenu than a var item list tbh UNUSED(item); @@ -176,6 +186,9 @@ bool findmy_scene_config_import_on_event(void* context, SceneManagerEvent event) case VarItemListIndexOpenHaystack: extension = ".keys"; break; + case VarItemListIndexRegisterTagManually: + scene_manager_next_scene(app->scene_manager, FindMySceneConfigMac); + break; default: break; } diff --git a/base_pack/find_my_flipper/scenes/findmy_scene_config_packet.c b/base_pack/find_my_flipper/scenes/findmy_scene_config_packet.c index 9d77fb1d797..80f23af98a0 100644 --- a/base_pack/find_my_flipper/scenes/findmy_scene_config_packet.c +++ b/base_pack/find_my_flipper/scenes/findmy_scene_config_packet.c @@ -16,7 +16,7 @@ void findmy_scene_config_packet_on_enter(void* context) { byte_input_set_header_text(byte_input, "Enter Bluetooth Payload:"); - memcpy(app->packet_buf, app->state.data, sizeof(app->packet_buf)); + memcpy(app->packet_buf, app->state.data, findmy_state_data_size(app->state.tag_type)); byte_input_set_result_callback( byte_input, @@ -24,7 +24,7 @@ void findmy_scene_config_packet_on_enter(void* context) { NULL, app, app->packet_buf, - sizeof(app->packet_buf)); + findmy_state_data_size(app->state.tag_type)); view_dispatcher_switch_to_view(app->view_dispatcher, FindMyViewByteInput); } @@ -39,11 +39,10 @@ bool findmy_scene_config_packet_on_event(void* context, SceneManagerEvent event) case ByteInputResultOk: scene_manager_search_and_switch_to_previous_scene( app->scene_manager, FindMySceneConfig); - memcpy(app->state.data, app->packet_buf, sizeof(app->state.data)); + 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, sizeof(app->state.data))); - findmy_main_update_type(app->findmy_main, findmy_data_get_type(app->state.data)); + furi_check(furi_hal_bt_extra_beacon_set_data( + app->state.data, findmy_state_data_size(app->state.tag_type))); break; default: break; diff --git a/base_pack/find_my_flipper/scenes/findmy_scene_config_tagtype.c b/base_pack/find_my_flipper/scenes/findmy_scene_config_tagtype.c new file mode 100644 index 00000000000..308935271e3 --- /dev/null +++ b/base_pack/find_my_flipper/scenes/findmy_scene_config_tagtype.c @@ -0,0 +1,70 @@ +#include "../findmy_i.h" + +enum VarItemListIndex { + VarItemListIndexApple, + VarItemListIndexSamsung, + VarItemListIndexTile, +}; + +void findmy_scene_config_tagtype_callback(void* context, uint32_t index) { + furi_assert(context); + FindMy* app = context; + view_dispatcher_send_custom_event(app->view_dispatcher, index); +} + +void findmy_scene_config_tagtype_on_enter(void* context) { + FindMy* app = context; + VariableItemList* var_item_list = app->var_item_list; + VariableItem* item; + + //variable_item_list_set_header(var_item_list, "Choose tag type"); + + item = variable_item_list_add(var_item_list, "Apple AirTag", 0, NULL, NULL); + + item = variable_item_list_add(var_item_list, "Samsung SmartTag", 0, NULL, NULL); + + item = variable_item_list_add(var_item_list, "Tile SmartTag", 0, NULL, NULL); + + UNUSED(item); + + variable_item_list_set_enter_callback( + var_item_list, findmy_scene_config_tagtype_callback, app); + + variable_item_list_set_selected_item( + var_item_list, scene_manager_get_scene_state(app->scene_manager, FindMySceneConfigImport)); + view_dispatcher_switch_to_view(app->view_dispatcher, FindMyViewVarItemList); +} + +bool findmy_scene_config_tagtype_on_event(void* context, SceneManagerEvent event) { + FindMy* app = context; + bool consumed = false; + + if(event.type == SceneManagerEventTypeCustom) { + scene_manager_set_scene_state(app->scene_manager, FindMySceneConfigTagtype, event.event); + consumed = true; + + switch(event.event) { + case VarItemListIndexApple: + findmy_set_tag_type(app, FindMyTypeApple); + break; + case VarItemListIndexSamsung: + findmy_set_tag_type(app, FindMyTypeSamsung); + break; + case VarItemListIndexTile: + findmy_set_tag_type(app, FindMyTypeTile); + break; + default: + break; + } + scene_manager_next_scene(app->scene_manager, FindMySceneConfigImport); + } + + return consumed; +} + +void findmy_scene_config_tagtype_on_exit(void* context) { + FindMy* app = context; + VariableItemList* var_item_list = app->var_item_list; + + variable_item_list_reset(var_item_list); +} \ No newline at end of file diff --git a/base_pack/find_my_flipper/scenes/findmy_scenes.h b/base_pack/find_my_flipper/scenes/findmy_scenes.h index 9a35c519d9a..0680e2c5f9d 100644 --- a/base_pack/find_my_flipper/scenes/findmy_scenes.h +++ b/base_pack/find_my_flipper/scenes/findmy_scenes.h @@ -1,6 +1,7 @@ ADD_SCENE(findmy, main, Main) ADD_SCENE(findmy, config, Config) ADD_SCENE(findmy, config_import, ConfigImport) +ADD_SCENE(findmy, config_tagtype, ConfigTagtype) ADD_SCENE(findmy, config_import_result, ConfigImportResult) ADD_SCENE(findmy, config_mac, ConfigMac) ADD_SCENE(findmy, config_packet, ConfigPacket) diff --git a/base_pack/find_my_flipper/views/findmy_main.c b/base_pack/find_my_flipper/views/findmy_main.c index 9f9f5990553..829088be769 100644 --- a/base_pack/find_my_flipper/views/findmy_main.c +++ b/base_pack/find_my_flipper/views/findmy_main.c @@ -34,18 +34,22 @@ static void findmy_main_draw_callback(Canvas* canvas, void* _model) { snprintf(interval_str, sizeof(interval_str), "Ping Interval: %ds", model->interval); canvas_draw_str(canvas, 4, 62, interval_str); canvas_set_font(canvas, FontPrimary); + const char* network_text = ""; switch(model->type) { case FindMyTypeApple: - canvas_draw_str(canvas, 4, 32, "Apple Network"); - canvas_draw_icon(canvas, 80, 24, &I_Lock_7x8); + network_text = "Apple Network"; break; case FindMyTypeSamsung: - canvas_draw_str(canvas, 4, 32, "Samsung Network"); - canvas_draw_icon(canvas, 97, 24, &I_Lock_7x8); + network_text = "Samsung Network"; + break; + case FindMyTypeTile: + network_text = "Tile Network"; break; default: break; } + canvas_draw_str(canvas, 4, 32, network_text); + canvas_draw_icon(canvas, 6 + canvas_string_width(canvas, network_text), 24, &I_Lock_7x8); canvas_set_font(canvas, FontSecondary); canvas_draw_str(canvas, 100, 61, "Config"); canvas_draw_line(canvas, 100, 51, 127, 51); @@ -103,7 +107,7 @@ FindMyMain* findmy_main_alloc(FindMy* app) { { model->active = app->state.beacon_active; model->interval = app->state.broadcast_interval; - model->type = findmy_data_get_type(app->state.data); + model->type = app->state.tag_type; }, false); view_set_context(findmy_main->view, findmy_main); @@ -147,4 +151,4 @@ void findmy_main_update_type(FindMyMain* findmy_main, FindMyType type) { furi_assert(findmy_main); with_view_model( findmy_main->view, FindMyMainModel * model, { model->type = type; }, true); -} +} \ No newline at end of file diff --git a/base_pack/find_my_flipper/views/findmy_main.h b/base_pack/find_my_flipper/views/findmy_main.h index 92a9170ec25..d73040ee24d 100644 --- a/base_pack/find_my_flipper/views/findmy_main.h +++ b/base_pack/find_my_flipper/views/findmy_main.h @@ -1,6 +1,7 @@ #pragma once #include "../findmy.h" +#include "../findmy_state.h" #include typedef enum {