From 2a105e42668e9c065127a61d1e22bc95e5c80f46 Mon Sep 17 00:00:00 2001 From: MatthewKuKanich Date: Fri, 8 Mar 2024 13:37:55 -0500 Subject: [PATCH 01/19] fix placeholders and beacon stop on exit --- applications/system/findmy/findmy.c | 34 +++++++++++++++---- .../system/findmy/scenes/findmy_scene_main.c | 1 + 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/applications/system/findmy/findmy.c b/applications/system/findmy/findmy.c index 2722f32ab..47b9f2936 100644 --- a/applications/system/findmy/findmy.c +++ b/applications/system/findmy/findmy.c @@ -83,7 +83,7 @@ static void findmy_start(FindMy* app) { app->config.adv_power_level = GapAdvPowerLevel_0dBm + app->transmit_power; app->config.address_type = GapAddressTypePublic; - uint8_t mac[EXTRA_BEACON_MAC_ADDR_SIZE] = {0x4D, 0x61, 0x74, 0x4B, 0x75, 0x4B}; + uint8_t mac[EXTRA_BEACON_MAC_ADDR_SIZE] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66}; furi_hal_bt_reverse_mac_addr(mac); memcpy(&app->config.address, mac, sizeof(app->config.address)); furi_check(furi_hal_bt_extra_beacon_set_config(&app->config)); @@ -92,15 +92,37 @@ static void findmy_start(FindMy* app) { uint8_t* it = data; // For Apple AirTags - *it++ = 0x1E; // Length + *it++ = 0x1E, // Length *it++ = 0xFF; // Manufacturer Specific Data *it++ = 0x4C; // Company ID (Apple, Inc.) *it++ = 0x00; // State *it++ = 0x12; // Data - Public Key without the MAC address - *it++ = 0x81; // ... - *it++ = 0xB9; // ... - *it++ = 0x02; // First 2 bits are the version, the rest is the battery level - *it++ = 0x7E; // Hint (0x00) + *it++ = 0x19; + *it++ = 0x00; + *it++ = 0x00; + *it++ = 0x00; + *it++ = 0x00; + *it++ = 0x00; + *it++ = 0x00; + *it++ = 0x00; + *it++ = 0x00; + *it++ = 0x00; + *it++ = 0x00; + *it++ = 0x00; + *it++ = 0x00; + *it++ = 0x00; + *it++ = 0x00; + *it++ = 0x00; + *it++ = 0x00; + *it++ = 0x00; + *it++ = 0x00; + *it++ = 0x00; + *it++ = 0x00; + *it++ = 0x00; + *it++ = 0x00; + *it++ = 0x00; + *it++ = 0x00; // First 2 bits are the version, the rest is the battery level + *it++ = 0x00; // Hint (0x00) furi_check(furi_hal_bt_extra_beacon_set_data(data, it - data)); } diff --git a/applications/system/findmy/scenes/findmy_scene_main.c b/applications/system/findmy/scenes/findmy_scene_main.c index 5738370cf..923dfafd6 100644 --- a/applications/system/findmy/scenes/findmy_scene_main.c +++ b/applications/system/findmy/scenes/findmy_scene_main.c @@ -51,4 +51,5 @@ bool findmy_scene_main_on_event(void* context, SceneManagerEvent event) { void findmy_scene_main_on_exit(void* context) { FindMy* app = context; UNUSED(app); + furi_hal_bt_extra_beacon_stop(); } \ No newline at end of file From aa15ee6fbc737d4fdab5f5482bda5dd8e3ca78dc Mon Sep 17 00:00:00 2001 From: Willy-JL <49810075+Willy-JL@users.noreply.github.com> Date: Fri, 8 Mar 2024 18:55:29 +0000 Subject: [PATCH 02/19] FindMy: Disable beacon correctly at exit, not in config --- applications/system/findmy/scenes/findmy_scene_main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/applications/system/findmy/scenes/findmy_scene_main.c b/applications/system/findmy/scenes/findmy_scene_main.c index 923dfafd6..40e22d942 100644 --- a/applications/system/findmy/scenes/findmy_scene_main.c +++ b/applications/system/findmy/scenes/findmy_scene_main.c @@ -38,6 +38,7 @@ bool findmy_scene_main_on_event(void* context, SceneManagerEvent event) { findmy_change_broadcast_interval(app, app->broadcast_interval - 1); break; case FindMyMainEventQuit: + furi_hal_bt_extra_beacon_stop(); break; default: consumed = false; @@ -51,5 +52,4 @@ bool findmy_scene_main_on_event(void* context, SceneManagerEvent event) { void findmy_scene_main_on_exit(void* context) { FindMy* app = context; UNUSED(app); - furi_hal_bt_extra_beacon_stop(); } \ No newline at end of file From 1941da7fd62b7e57bfef1b057f44e0a64b8ca6cc Mon Sep 17 00:00:00 2001 From: Willy-JL <49810075+Willy-JL@users.noreply.github.com> Date: Fri, 8 Mar 2024 18:59:13 +0000 Subject: [PATCH 03/19] Format + cleanup --- applications/system/findmy/findmy.c | 2 +- .../findmy/scenes/findmy_scene_config.c | 7 +++++- .../findmy/scenes/findmy_scene_config_mac.c | 1 - .../scenes/findmy_scene_config_packet.c | 5 +++-- .../system/findmy/views/findmy_main.c | 22 ++++++++++--------- 5 files changed, 22 insertions(+), 15 deletions(-) diff --git a/applications/system/findmy/findmy.c b/applications/system/findmy/findmy.c index 47b9f2936..6ccade2bf 100644 --- a/applications/system/findmy/findmy.c +++ b/applications/system/findmy/findmy.c @@ -92,7 +92,7 @@ static void findmy_start(FindMy* app) { uint8_t* it = data; // For Apple AirTags - *it++ = 0x1E, // Length + *it++ = 0x1E; // Length *it++ = 0xFF; // Manufacturer Specific Data *it++ = 0x4C; // Company ID (Apple, Inc.) *it++ = 0x00; // State diff --git a/applications/system/findmy/scenes/findmy_scene_config.c b/applications/system/findmy/scenes/findmy_scene_config.c index 8f16d7b19..bf961a5f7 100644 --- a/applications/system/findmy/scenes/findmy_scene_config.c +++ b/applications/system/findmy/scenes/findmy_scene_config.c @@ -58,7 +58,12 @@ void findmy_scene_config_on_enter(void* context) { variable_item_set_current_value_text(item, transmit_power_s); item = variable_item_list_add(var_item_list, "Register Tag", 0, NULL, NULL); - item = variable_item_list_add(var_item_list, "Matthew KuKanich, Thanks to Chapoly1305, WillyJL, OpenHaystack, Testers", 1, NULL, NULL); + item = variable_item_list_add( + var_item_list, + "Matthew KuKanich, Thanks to Chapoly1305, WillyJL, OpenHaystack, Testers", + 1, + NULL, + NULL); variable_item_set_current_value_text(item, "Credits"); variable_item_list_set_enter_callback(var_item_list, findmy_scene_config_callback, app); diff --git a/applications/system/findmy/scenes/findmy_scene_config_mac.c b/applications/system/findmy/scenes/findmy_scene_config_mac.c index c86e3dc1e..0a5ed1392 100644 --- a/applications/system/findmy/scenes/findmy_scene_config_mac.c +++ b/applications/system/findmy/scenes/findmy_scene_config_mac.c @@ -56,5 +56,4 @@ void findmy_scene_config_mac_on_exit(void* context) { byte_input_set_result_callback(app->byte_input, NULL, NULL, NULL, NULL, 0); byte_input_set_header_text(app->byte_input, ""); - } \ No newline at end of file diff --git a/applications/system/findmy/scenes/findmy_scene_config_packet.c b/applications/system/findmy/scenes/findmy_scene_config_packet.c index 4dddf6c02..f431445d5 100644 --- a/applications/system/findmy/scenes/findmy_scene_config_packet.c +++ b/applications/system/findmy/scenes/findmy_scene_config_packet.c @@ -40,8 +40,9 @@ 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); - furi_check(furi_hal_bt_extra_beacon_set_data(app->packet_buf, sizeof(app->packet_buf))); - if (app->packet_buf[0] == 0x1E && app->packet_buf[3] == 0x00) { + furi_check( + furi_hal_bt_extra_beacon_set_data(app->packet_buf, sizeof(app->packet_buf))); + if(app->packet_buf[0] == 0x1E && app->packet_buf[3] == 0x00) { app->apple = true; // Checks payload data for Apple identifier } else { app->apple = false; diff --git a/applications/system/findmy/views/findmy_main.c b/applications/system/findmy/views/findmy_main.c index f7b248546..553a8d87e 100644 --- a/applications/system/findmy/views/findmy_main.c +++ b/applications/system/findmy/views/findmy_main.c @@ -34,7 +34,7 @@ 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); - if(model->apple){ + if(model->apple) { canvas_draw_str(canvas, 4, 32, "Apple Network"); canvas_draw_icon(canvas, 80, 24, &I_Lock_7x8); } else { @@ -56,30 +56,32 @@ static bool findmy_main_input_callback(InputEvent* event, void* context) { if(event->type == InputTypePress) { consumed = true; - // FIXME: finish implementing handlers in scene side + FindMyMainEvent event; + switch(event->key) { case InputKeyBack: - findmy_main->callback(FindMyMainEventQuit, findmy_main->context); - // furi_hal_bt_extra_beacon_stop(); + event = FindMyMainEventQuit; break; case InputKeyOk: - findmy_main->callback(FindMyMainEventToggle, findmy_main->context); + event = FindMyMainEventToggle; break; case InputKeyLeft: - findmy_main->callback(FindMyMainEventBackground, findmy_main->context); + event = FindMyMainEventBackground; break; case InputKeyRight: - findmy_main->callback(FindMyMainEventConfig, findmy_main->context); + event = FindMyMainEventConfig; break; case InputKeyUp: - findmy_main->callback(FindMyMainEventIntervalUp, findmy_main->context); + event = FindMyMainEventIntervalUp; break; case InputKeyDown: - findmy_main->callback(FindMyMainEventIntervalDown, findmy_main->context); + event = FindMyMainEventIntervalDown; break; default: - break; + return consumed; } + + findmy_main->callback(event, findmy_main->context); } return consumed; From a8d18fa02aa1f188589fde8221bc6ef4a1c94f0b Mon Sep 17 00:00:00 2001 From: Willy-JL <49810075+Willy-JL@users.noreply.github.com> Date: Fri, 8 Mar 2024 19:04:47 +0000 Subject: [PATCH 04/19] Cleanup default packet template and fix comments --- applications/system/findmy/findmy.c | 34 +++++++---------------------- 1 file changed, 8 insertions(+), 26 deletions(-) diff --git a/applications/system/findmy/findmy.c b/applications/system/findmy/findmy.c index 6ccade2bf..ed7d472f0 100644 --- a/applications/system/findmy/findmy.c +++ b/applications/system/findmy/findmy.c @@ -95,32 +95,14 @@ static void findmy_start(FindMy* app) { *it++ = 0x1E; // Length *it++ = 0xFF; // Manufacturer Specific Data *it++ = 0x4C; // Company ID (Apple, Inc.) - *it++ = 0x00; // State - *it++ = 0x12; // Data - Public Key without the MAC address - *it++ = 0x19; - *it++ = 0x00; - *it++ = 0x00; - *it++ = 0x00; - *it++ = 0x00; - *it++ = 0x00; - *it++ = 0x00; - *it++ = 0x00; - *it++ = 0x00; - *it++ = 0x00; - *it++ = 0x00; - *it++ = 0x00; - *it++ = 0x00; - *it++ = 0x00; - *it++ = 0x00; - *it++ = 0x00; - *it++ = 0x00; - *it++ = 0x00; - *it++ = 0x00; - *it++ = 0x00; - *it++ = 0x00; - *it++ = 0x00; - *it++ = 0x00; - *it++ = 0x00; + *it++ = 0x00; // ... + *it++ = 0x12; // Type (FindMy) + *it++ = 0x19; // Length + *it++ = 0x00; // Status + // Placeholder Empty Public Key without the MAC address + for(size_t i = 0; i < 22; ++i) { + *it++ = 0x00; + } *it++ = 0x00; // First 2 bits are the version, the rest is the battery level *it++ = 0x00; // Hint (0x00) From 09af215e75a47b998e4704f02260974f5d082bec Mon Sep 17 00:00:00 2001 From: Willy-JL <49810075+Willy-JL@users.noreply.github.com> Date: Fri, 8 Mar 2024 19:10:08 +0000 Subject: [PATCH 05/19] Oops --- applications/system/findmy/views/findmy_main.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/applications/system/findmy/views/findmy_main.c b/applications/system/findmy/views/findmy_main.c index 553a8d87e..6ac55d261 100644 --- a/applications/system/findmy/views/findmy_main.c +++ b/applications/system/findmy/views/findmy_main.c @@ -56,32 +56,32 @@ static bool findmy_main_input_callback(InputEvent* event, void* context) { if(event->type == InputTypePress) { consumed = true; - FindMyMainEvent event; + FindMyMainEvent cb_event; switch(event->key) { case InputKeyBack: - event = FindMyMainEventQuit; + cb_event = FindMyMainEventQuit; break; case InputKeyOk: - event = FindMyMainEventToggle; + cb_event = FindMyMainEventToggle; break; case InputKeyLeft: - event = FindMyMainEventBackground; + cb_event = FindMyMainEventBackground; break; case InputKeyRight: - event = FindMyMainEventConfig; + cb_event = FindMyMainEventConfig; break; case InputKeyUp: - event = FindMyMainEventIntervalUp; + cb_event = FindMyMainEventIntervalUp; break; case InputKeyDown: - event = FindMyMainEventIntervalDown; + cb_event = FindMyMainEventIntervalDown; break; default: return consumed; } - findmy_main->callback(event, findmy_main->context); + findmy_main->callback(cb_event, findmy_main->context); } return consumed; From 7c48c6164aeb242145cdfff385305e57bd3ec17f Mon Sep 17 00:00:00 2001 From: Willy-JL <49810075+Willy-JL@users.noreply.github.com> Date: Fri, 8 Mar 2024 20:53:00 +0000 Subject: [PATCH 06/19] FindMy: Refactors and improvements - state handled reparately, decoupled from app - better apple/samsung/unknown parsing and handling - improve error handling - add url to manifest + up stack size just in case --- applications/system/findmy/application.fam | 4 +- applications/system/findmy/findmy.c | 100 +++++++----------- applications/system/findmy/findmy.h | 2 + applications/system/findmy/findmy_i.h | 13 ++- applications/system/findmy/findmy_state.c | 61 +++++++++++ applications/system/findmy/findmy_state.h | 17 +++ .../findmy/scenes/findmy_scene_config.c | 24 ++--- .../findmy/scenes/findmy_scene_config_mac.c | 13 ++- .../scenes/findmy_scene_config_packet.c | 13 +-- .../system/findmy/scenes/findmy_scene_main.c | 12 ++- .../system/findmy/views/findmy_main.c | 21 ++-- .../system/findmy/views/findmy_main.h | 2 +- 12 files changed, 176 insertions(+), 106 deletions(-) create mode 100644 applications/system/findmy/findmy_state.c create mode 100644 applications/system/findmy/findmy_state.h diff --git a/applications/system/findmy/application.fam b/applications/system/findmy/application.fam index 0b6006923..9a7b2df94 100644 --- a/applications/system/findmy/application.fam +++ b/applications/system/findmy/application.fam @@ -4,11 +4,11 @@ App( apptype=FlipperAppType.EXTERNAL, entry_point="findmy_main", requires=["gui"], - stack_size=1 * 1024, - order=35, + stack_size=2 * 1024, fap_icon="location_icon.png", fap_category="Bluetooth", fap_author="@MatthewKuKanich", + fap_weburl="https://github.com/MatthewKuKanich/FindMyFlipper", fap_version="1.0", fap_description="BLE FindMy Location Beacon", ) diff --git a/applications/system/findmy/findmy.c b/applications/system/findmy/findmy.c index ed7d472f0..10019347f 100644 --- a/applications/system/findmy/findmy.c +++ b/applications/system/findmy/findmy.c @@ -43,13 +43,12 @@ static FindMy* findmy_app_alloc() { view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen); - app->beacon_active = false; - findmy_main_update_active(app->findmy_main, app->beacon_active); - app->broadcast_interval = 5; - findmy_main_update_interval(app->findmy_main, app->broadcast_interval); - app->transmit_power = 6; - app->apple = true; - findmy_main_update_apple(app->findmy_main, app->apple); + 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_interval(app->findmy_main, app->state.broadcast_interval); + findmy_main_update_type(app->findmy_main, findmy_data_get_type(app->state.data)); return app; } @@ -74,47 +73,10 @@ static void findmy_app_free(FindMy* app) { free(app); } -static void findmy_start(FindMy* app) { - furi_hal_bt_extra_beacon_stop(); // Stop any running beacon - - app->config.min_adv_interval_ms = app->broadcast_interval * 1000; // Converting s to ms - app->config.max_adv_interval_ms = (app->broadcast_interval * 1000) + 150; - app->config.adv_channel_map = GapAdvChannelMapAll; - app->config.adv_power_level = GapAdvPowerLevel_0dBm + app->transmit_power; - app->config.address_type = GapAddressTypePublic; - - uint8_t mac[EXTRA_BEACON_MAC_ADDR_SIZE] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66}; - furi_hal_bt_reverse_mac_addr(mac); - memcpy(&app->config.address, mac, sizeof(app->config.address)); - furi_check(furi_hal_bt_extra_beacon_set_config(&app->config)); - - uint8_t data[EXTRA_BEACON_MAX_DATA_SIZE]; - uint8_t* it = data; - - // For Apple AirTags - *it++ = 0x1E; // Length - *it++ = 0xFF; // Manufacturer Specific Data - *it++ = 0x4C; // Company ID (Apple, Inc.) - *it++ = 0x00; // ... - *it++ = 0x12; // Type (FindMy) - *it++ = 0x19; // Length - *it++ = 0x00; // Status - // Placeholder Empty Public Key without the MAC address - for(size_t i = 0; i < 22; ++i) { - *it++ = 0x00; - } - *it++ = 0x00; // First 2 bits are the version, the rest is the battery level - *it++ = 0x00; // Hint (0x00) - - furi_check(furi_hal_bt_extra_beacon_set_data(data, it - data)); -} - int32_t findmy_main(void* p) { UNUSED(p); FindMy* app = findmy_app_alloc(); - findmy_start(app); - scene_manager_next_scene(app->scene_manager, FindMySceneMain); view_dispatcher_run(app->view_dispatcher); @@ -127,16 +89,16 @@ void findmy_change_broadcast_interval(FindMy* app, uint8_t value) { if(value > 10 || value < 1) { return; } - app->broadcast_interval = value; - findmy_main_update_interval(app->findmy_main, app->broadcast_interval); - if(app->beacon_active) { + app->state.broadcast_interval = value; + 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->config.min_adv_interval_ms = app->broadcast_interval * 1000; - app->config.max_adv_interval_ms = app->config.min_adv_interval_ms + 150; - furi_check(furi_hal_bt_extra_beacon_set_config(&app->config)); - if(app->beacon_active) { + 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()); } } @@ -145,24 +107,38 @@ void findmy_change_transmit_power(FindMy* app, uint8_t value) { if(value > 6) { return; } - app->transmit_power = value; - if(app->beacon_active) { + app->state.transmit_power = value; + if(furi_hal_bt_extra_beacon_is_active()) { furi_check(furi_hal_bt_extra_beacon_stop()); } - app->config.adv_power_level = GapAdvPowerLevel_0dBm + app->transmit_power; - furi_check(furi_hal_bt_extra_beacon_set_config(&app->config)); - if(app->beacon_active) { + 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()); } } void findmy_toggle_beacon(FindMy* app) { - app->beacon_active = !app->beacon_active; - findmy_main_update_active(app->findmy_main, app->beacon_active); - findmy_main_update_apple(app->findmy_main, app->apple); - if(app->beacon_active) { - furi_hal_bt_extra_beacon_start(); + app->state.beacon_active = !app->state.beacon_active; + 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_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 { - furi_hal_bt_extra_beacon_stop(); + return FindMyTypeSamsung; } } \ No newline at end of file diff --git a/applications/system/findmy/findmy.h b/applications/system/findmy/findmy.h index 344882ef1..a5135f2d7 100644 --- a/applications/system/findmy/findmy.h +++ b/applications/system/findmy/findmy.h @@ -1,3 +1,5 @@ #pragma once typedef struct FindMy FindMy; + +typedef enum FindMyType FindMyType; \ No newline at end of file diff --git a/applications/system/findmy/findmy_i.h b/applications/system/findmy/findmy_i.h index fd09e5324..c5991b5f4 100644 --- a/applications/system/findmy/findmy_i.h +++ b/applications/system/findmy/findmy_i.h @@ -1,6 +1,7 @@ #pragma once #include "findmy.h" +#include "findmy_state.h" #include #include #include @@ -24,11 +25,7 @@ struct FindMy { uint8_t mac_buf[EXTRA_BEACON_MAC_ADDR_SIZE]; uint8_t packet_buf[EXTRA_BEACON_MAX_DATA_SIZE]; - GapExtraBeaconConfig config; - bool apple; - bool beacon_active; - uint8_t broadcast_interval; - uint8_t transmit_power; + FindMyState state; }; typedef enum { @@ -37,6 +34,12 @@ typedef enum { FindMyViewVarItemList, } 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_toggle_beacon(FindMy* app); +FindMyType findmy_data_get_type(uint8_t data[EXTRA_BEACON_MAX_DATA_SIZE]); diff --git a/applications/system/findmy/findmy_state.c b/applications/system/findmy/findmy_state.c new file mode 100644 index 000000000..34e9c80b2 --- /dev/null +++ b/applications/system/findmy/findmy_state.c @@ -0,0 +1,61 @@ +#include "findmy_state.h" + +#include +#include +#include + +bool findmy_state_load(FindMyState* out_state) { + FindMyState state; + + // Set default values + state.beacon_active = false; + state.broadcast_interval = 5; + state.transmit_power = 6; + 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_channel_map = GapAdvChannelMapAll; + state.config.adv_power_level = GapAdvPowerLevel_0dBm + state.transmit_power; + state.config.address_type = GapAddressTypePublic; + + // 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)); + memcpy(state.config.address, default_mac, sizeof(state.config.address)); + + // Set default empty AirTag data + uint8_t* data = state.data; + *data++ = 0x1E; // Length + *data++ = 0xFF; // Manufacturer Specific Data + *data++ = 0x4C; // Company ID (Apple, Inc.) + *data++ = 0x00; // ... + *data++ = 0x12; // Type (FindMy) + *data++ = 0x19; // Length + *data++ = 0x00; // Status + // 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; // Hint (0x00) + + // Copy to caller state before popping stack + memcpy(out_state, &state, sizeof(state)); + + // Return if active, can be used to start after loading in an if statement + return state.beacon_active; +} + +void findmy_state_apply(FindMyState* state) { + // Stop any running beacon + 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)); + + furi_check(furi_hal_bt_extra_beacon_set_data(state->data, sizeof(state->data))); + + if(state->beacon_active) { + furi_check(furi_hal_bt_extra_beacon_start()); + } +} diff --git a/applications/system/findmy/findmy_state.h b/applications/system/findmy/findmy_state.h new file mode 100644 index 000000000..48a82fd2e --- /dev/null +++ b/applications/system/findmy/findmy_state.h @@ -0,0 +1,17 @@ +#pragma once + +#include + +typedef struct { + uint8_t mac[EXTRA_BEACON_MAC_ADDR_SIZE]; + uint8_t data[EXTRA_BEACON_MAX_DATA_SIZE]; + GapExtraBeaconConfig config; + + bool beacon_active; + uint8_t broadcast_interval; + uint8_t transmit_power; +} FindMyState; + +bool findmy_state_load(FindMyState* out_state); + +void findmy_state_apply(FindMyState* state); diff --git a/applications/system/findmy/scenes/findmy_scene_config.c b/applications/system/findmy/scenes/findmy_scene_config.c index bf961a5f7..415bda945 100644 --- a/applications/system/findmy/scenes/findmy_scene_config.c +++ b/applications/system/findmy/scenes/findmy_scene_config.c @@ -12,9 +12,9 @@ void findmy_scene_config_broadcast_interval_changed(VariableItem* item) { uint8_t index = variable_item_get_current_value_index(item); findmy_change_broadcast_interval(app, index + 1); char str[5]; - snprintf(str, sizeof(str), "%ds", app->broadcast_interval); + snprintf(str, sizeof(str), "%ds", app->state.broadcast_interval); variable_item_set_current_value_text(item, str); - variable_item_set_current_value_index(item, app->broadcast_interval - 1); + variable_item_set_current_value_index(item, app->state.broadcast_interval - 1); } void findmy_scene_config_transmit_power_changed(VariableItem* item) { @@ -22,9 +22,9 @@ void findmy_scene_config_transmit_power_changed(VariableItem* item) { uint8_t index = variable_item_get_current_value_index(item); findmy_change_transmit_power(app, index); char str[7]; - snprintf(str, sizeof(str), "%ddBm", app->transmit_power); + snprintf(str, sizeof(str), "%ddBm", app->state.transmit_power); variable_item_set_current_value_text(item, str); - variable_item_set_current_value_index(item, app->transmit_power); + variable_item_set_current_value_index(item, app->state.transmit_power); } void findmy_scene_config_callback(void* context, uint32_t index) { @@ -45,17 +45,17 @@ void findmy_scene_config_on_enter(void* context) { findmy_scene_config_broadcast_interval_changed, app); // Broadcast Interval is 1-10, so use 0-9 and offset indexes by 1 - variable_item_set_current_value_index(item, app->broadcast_interval - 1); - char broadcast_interval_s[5]; - snprintf(broadcast_interval_s, sizeof(broadcast_interval_s), "%ds", app->broadcast_interval); - variable_item_set_current_value_text(item, broadcast_interval_s); + variable_item_set_current_value_index(item, app->state.broadcast_interval - 1); + char interval_str[5]; + snprintf(interval_str, sizeof(interval_str), "%ds", app->state.broadcast_interval); + variable_item_set_current_value_text(item, interval_str); item = variable_item_list_add( var_item_list, "Transmit Power", 7, findmy_scene_config_transmit_power_changed, app); - variable_item_set_current_value_index(item, app->transmit_power); - char transmit_power_s[7]; - snprintf(transmit_power_s, sizeof(transmit_power_s), "%ddBm", app->transmit_power); - variable_item_set_current_value_text(item, transmit_power_s); + variable_item_set_current_value_index(item, app->state.transmit_power); + char power_str[7]; + 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, "Register Tag", 0, NULL, NULL); item = variable_item_list_add( diff --git a/applications/system/findmy/scenes/findmy_scene_config_mac.c b/applications/system/findmy/scenes/findmy_scene_config_mac.c index 0a5ed1392..b42291a7f 100644 --- a/applications/system/findmy/scenes/findmy_scene_config_mac.c +++ b/applications/system/findmy/scenes/findmy_scene_config_mac.c @@ -16,7 +16,7 @@ void findmy_scene_config_mac_on_enter(void* context) { byte_input_set_header_text(byte_input, "Enter Bluetooth MAC:"); - memcpy(app->mac_buf, &app->config.address, sizeof(app->mac_buf)); + memcpy(app->mac_buf, app->state.mac, sizeof(app->mac_buf)); furi_hal_bt_reverse_mac_addr(app->mac_buf); byte_input_set_result_callback( @@ -39,8 +39,15 @@ bool findmy_scene_config_mac_on_event(void* context, SceneManagerEvent event) { switch(event.event) { case ByteInputResultOk: furi_hal_bt_reverse_mac_addr(app->mac_buf); - memcpy(&app->config.address, app->mac_buf, sizeof(app->config.address)); - furi_hal_bt_extra_beacon_set_config(&app->config); + memcpy(&app->state.mac, app->mac_buf, sizeof(app->state.mac)); + 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()); + } + 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()); + } scene_manager_next_scene(app->scene_manager, FindMySceneConfigPacket); break; default: diff --git a/applications/system/findmy/scenes/findmy_scene_config_packet.c b/applications/system/findmy/scenes/findmy_scene_config_packet.c index f431445d5..121bf58b5 100644 --- a/applications/system/findmy/scenes/findmy_scene_config_packet.c +++ b/applications/system/findmy/scenes/findmy_scene_config_packet.c @@ -16,8 +16,7 @@ void findmy_scene_config_packet_on_enter(void* context) { byte_input_set_header_text(byte_input, "Enter Bluetooth Payload:"); - memset(app->packet_buf, 0, sizeof(app->packet_buf)); - furi_hal_bt_extra_beacon_get_data(app->packet_buf); + memcpy(app->packet_buf, app->state.data, sizeof(app->packet_buf)); byte_input_set_result_callback( byte_input, @@ -40,14 +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)); furi_check( - furi_hal_bt_extra_beacon_set_data(app->packet_buf, sizeof(app->packet_buf))); - if(app->packet_buf[0] == 0x1E && app->packet_buf[3] == 0x00) { - app->apple = true; // Checks payload data for Apple identifier - } else { - app->apple = false; - } - findmy_main_update_apple(app->findmy_main, app->apple); + 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)); break; default: break; diff --git a/applications/system/findmy/scenes/findmy_scene_main.c b/applications/system/findmy/scenes/findmy_scene_main.c index 40e22d942..63932fb5c 100644 --- a/applications/system/findmy/scenes/findmy_scene_main.c +++ b/applications/system/findmy/scenes/findmy_scene_main.c @@ -25,20 +25,24 @@ bool findmy_scene_main_on_event(void* context, SceneManagerEvent event) { findmy_toggle_beacon(app); break; case FindMyMainEventBackground: - furi_hal_bt_extra_beacon_start(); + if(!furi_hal_bt_extra_beacon_is_active()) { + furi_check(furi_hal_bt_extra_beacon_start()); + } view_dispatcher_stop(app->view_dispatcher); break; case FindMyMainEventConfig: scene_manager_next_scene(app->scene_manager, FindMySceneConfig); break; case FindMyMainEventIntervalUp: - findmy_change_broadcast_interval(app, app->broadcast_interval + 1); + findmy_change_broadcast_interval(app, app->state.broadcast_interval + 1); break; case FindMyMainEventIntervalDown: - findmy_change_broadcast_interval(app, app->broadcast_interval - 1); + findmy_change_broadcast_interval(app, app->state.broadcast_interval - 1); break; case FindMyMainEventQuit: - furi_hal_bt_extra_beacon_stop(); + if(furi_hal_bt_extra_beacon_is_active()) { + furi_check(furi_hal_bt_extra_beacon_stop()); + } break; default: consumed = false; diff --git a/applications/system/findmy/views/findmy_main.c b/applications/system/findmy/views/findmy_main.c index 6ac55d261..b39adc2bf 100644 --- a/applications/system/findmy/views/findmy_main.c +++ b/applications/system/findmy/views/findmy_main.c @@ -9,8 +9,8 @@ struct FindMyMain { typedef struct { bool active; - bool apple; uint8_t interval; + FindMyType type; } FindMyMainModel; static void findmy_main_draw_callback(Canvas* canvas, void* _model) { @@ -34,12 +34,17 @@ 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); - if(model->apple) { + switch(model->type) { + case FindMyTypeApple: canvas_draw_str(canvas, 4, 32, "Apple Network"); canvas_draw_icon(canvas, 80, 24, &I_Lock_7x8); - } else { + break; + case FindMyTypeSamsung: canvas_draw_str(canvas, 4, 32, "Samsung Network"); canvas_draw_icon(canvas, 97, 24, &I_Lock_7x8); + break; + default: + break; } canvas_set_font(canvas, FontSecondary); canvas_draw_str(canvas, 100, 61, "Config"); @@ -96,9 +101,9 @@ FindMyMain* findmy_main_alloc(FindMy* app) { findmy_main->view, FindMyMainModel * model, { - model->active = app->beacon_active; - model->apple = app->apple; - model->interval = app->broadcast_interval; + model->active = app->state.beacon_active; + model->interval = app->state.broadcast_interval; + model->type = findmy_data_get_type(app->state.data); }, false); view_set_context(findmy_main->view, findmy_main); @@ -138,8 +143,8 @@ void findmy_main_update_interval(FindMyMain* findmy_main, uint8_t interval) { findmy_main->view, FindMyMainModel * model, { model->interval = interval; }, true); } -void findmy_main_update_apple(FindMyMain* findmy_main, bool apple) { +void findmy_main_update_type(FindMyMain* findmy_main, FindMyType type) { furi_assert(findmy_main); with_view_model( - findmy_main->view, FindMyMainModel * model, { model->apple = apple; }, true); + findmy_main->view, FindMyMainModel * model, { model->type = type; }, true); } \ No newline at end of file diff --git a/applications/system/findmy/views/findmy_main.h b/applications/system/findmy/views/findmy_main.h index 977f574d7..92a9170ec 100644 --- a/applications/system/findmy/views/findmy_main.h +++ b/applications/system/findmy/views/findmy_main.h @@ -26,4 +26,4 @@ void findmy_main_set_callback(FindMyMain* findmy_main, FindMyMainCallback callba // To redraw when info changes void findmy_main_update_active(FindMyMain* findmy_main, bool active); void findmy_main_update_interval(FindMyMain* findmy_main, uint8_t interval); -void findmy_main_update_apple(FindMyMain* findmy_main, bool apple); \ No newline at end of file +void findmy_main_update_type(FindMyMain* findmy_main, FindMyType type); \ No newline at end of file From 5d45d6abb8d8b598d226636605309df6d38c4c87 Mon Sep 17 00:00:00 2001 From: Willy-JL <49810075+Willy-JL@users.noreply.github.com> Date: Fri, 8 Mar 2024 21:35:59 +0000 Subject: [PATCH 07/19] FindMy: Save and load state from file --- applications/system/findmy/findmy.c | 3 + applications/system/findmy/findmy_state.c | 113 ++++++++++++++---- applications/system/findmy/findmy_state.h | 17 ++- .../findmy/scenes/findmy_scene_config_mac.c | 1 + .../scenes/findmy_scene_config_packet.c | 1 + .../system/findmy/scenes/findmy_scene_main.c | 4 + 6 files changed, 110 insertions(+), 29 deletions(-) diff --git a/applications/system/findmy/findmy.c b/applications/system/findmy/findmy.c index 10019347f..f9dc292ca 100644 --- a/applications/system/findmy/findmy.c +++ b/applications/system/findmy/findmy.c @@ -90,6 +90,7 @@ void findmy_change_broadcast_interval(FindMy* app, uint8_t value) { return; } app->state.broadcast_interval = value; + 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 @@ -108,6 +109,7 @@ void findmy_change_transmit_power(FindMy* app, uint8_t value) { return; } app->state.transmit_power = value; + findmy_state_save(&app->state); if(furi_hal_bt_extra_beacon_is_active()) { furi_check(furi_hal_bt_extra_beacon_stop()); } @@ -120,6 +122,7 @@ void findmy_change_transmit_power(FindMy* app, uint8_t value) { 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()); } diff --git a/applications/system/findmy/findmy_state.c b/applications/system/findmy/findmy_state.c index 34e9c80b2..a8d15d75e 100644 --- a/applications/system/findmy/findmy_state.c +++ b/applications/system/findmy/findmy_state.c @@ -3,40 +3,76 @@ #include #include #include +#include bool findmy_state_load(FindMyState* out_state) { FindMyState state; - // Set default values - state.beacon_active = false; - state.broadcast_interval = 5; - state.transmit_power = 6; + // Try to load from file + bool loaded_from_file = false; + Storage* storage = furi_record_open(RECORD_STORAGE); + if(storage_file_exists(storage, FINDMY_STATE_PATH)) { + FlipperFormat* file = flipper_format_file_alloc(storage); + do { + uint32_t tmp; + FuriString* str = furi_string_alloc(); + if(!flipper_format_file_open_existing(file, FINDMY_STATE_PATH)) break; + if(!flipper_format_read_header(file, str, &tmp)) break; + if(furi_string_cmp_str(str, FINDMY_STATE_HEADER)) break; + if(tmp != FINDMY_STATE_VER) break; + + if(!flipper_format_read_bool(file, "beacon_active", &state.beacon_active, 1)) break; + + if(!flipper_format_read_uint32(file, "broadcast_interval", &tmp, 1)) break; + state.broadcast_interval = tmp; + + if(!flipper_format_read_uint32(file, "transmit_power", &tmp, 1)) break; + state.transmit_power = 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; + + loaded_from_file = true; + } while(0); + flipper_format_free(file); + } + furi_record_close(RECORD_STORAGE); + + // Otherwise set default values + if(!loaded_from_file) { + state.beacon_active = false; + state.broadcast_interval = 5; + state.transmit_power = 6; + + // 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)); + + // Set default empty AirTag data + uint8_t* data = state.data; + *data++ = 0x1E; // Length + *data++ = 0xFF; // Manufacturer Specific Data + *data++ = 0x4C; // Company ID (Apple, Inc.) + *data++ = 0x00; // ... + *data++ = 0x12; // Type (FindMy) + *data++ = 0x19; // Length + *data++ = 0x00; // Status + // 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; // Hint (0x00) + } + + // 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; state.config.adv_channel_map = GapAdvChannelMapAll; state.config.adv_power_level = GapAdvPowerLevel_0dBm + state.transmit_power; state.config.address_type = GapAddressTypePublic; - - // 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)); - memcpy(state.config.address, default_mac, sizeof(state.config.address)); - - // Set default empty AirTag data - uint8_t* data = state.data; - *data++ = 0x1E; // Length - *data++ = 0xFF; // Manufacturer Specific Data - *data++ = 0x4C; // Company ID (Apple, Inc.) - *data++ = 0x00; // ... - *data++ = 0x12; // Type (FindMy) - *data++ = 0x19; // Length - *data++ = 0x00; // Status - // 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; // Hint (0x00) + memcpy(state.config.address, state.mac, sizeof(state.config.address)); // Copy to caller state before popping stack memcpy(out_state, &state, sizeof(state)); @@ -59,3 +95,30 @@ void findmy_state_apply(FindMyState* state) { furi_check(furi_hal_bt_extra_beacon_start()); } } + +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); + + do { + uint32_t tmp; + if(!flipper_format_file_open_always(file, FINDMY_STATE_PATH)) break; + if(!flipper_format_write_header_cstr(file, FINDMY_STATE_HEADER, FINDMY_STATE_VER)) break; + + if(!flipper_format_write_bool(file, "beacon_active", &state->beacon_active, 1)) break; + + tmp = state->broadcast_interval; + if(!flipper_format_write_uint32(file, "broadcast_interval", &tmp, 1)) break; + + tmp = state->transmit_power; + if(!flipper_format_write_uint32(file, "transmit_power", &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; + } while(0); + + flipper_format_free(file); + furi_record_close(RECORD_STORAGE); +} diff --git a/applications/system/findmy/findmy_state.h b/applications/system/findmy/findmy_state.h index 48a82fd2e..284f2dfbc 100644 --- a/applications/system/findmy/findmy_state.h +++ b/applications/system/findmy/findmy_state.h @@ -2,16 +2,25 @@ #include -typedef struct { - uint8_t mac[EXTRA_BEACON_MAC_ADDR_SIZE]; - uint8_t data[EXTRA_BEACON_MAX_DATA_SIZE]; - GapExtraBeaconConfig config; +#define FINDMY_STATE_HEADER "FindMy Flipper State" +#define FINDMY_STATE_VER 1 +#define FINDMY_STATE_DIR EXT_PATH("apps_data/findmy") +#define FINDMY_STATE_PATH FINDMY_STATE_DIR "/findmy_state.txt" +typedef struct { bool beacon_active; uint8_t broadcast_interval; uint8_t transmit_power; + + uint8_t mac[EXTRA_BEACON_MAC_ADDR_SIZE]; + uint8_t data[EXTRA_BEACON_MAX_DATA_SIZE]; + + // Generated from the other state values + GapExtraBeaconConfig config; } FindMyState; bool findmy_state_load(FindMyState* out_state); void findmy_state_apply(FindMyState* state); + +void findmy_state_save(FindMyState* state); diff --git a/applications/system/findmy/scenes/findmy_scene_config_mac.c b/applications/system/findmy/scenes/findmy_scene_config_mac.c index b42291a7f..2dc67f79d 100644 --- a/applications/system/findmy/scenes/findmy_scene_config_mac.c +++ b/applications/system/findmy/scenes/findmy_scene_config_mac.c @@ -40,6 +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_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()); diff --git a/applications/system/findmy/scenes/findmy_scene_config_packet.c b/applications/system/findmy/scenes/findmy_scene_config_packet.c index 121bf58b5..9d77fb1d7 100644 --- a/applications/system/findmy/scenes/findmy_scene_config_packet.c +++ b/applications/system/findmy/scenes/findmy_scene_config_packet.c @@ -40,6 +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, sizeof(app->state.data)); + 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)); diff --git a/applications/system/findmy/scenes/findmy_scene_main.c b/applications/system/findmy/scenes/findmy_scene_main.c index 63932fb5c..e70b59fc5 100644 --- a/applications/system/findmy/scenes/findmy_scene_main.c +++ b/applications/system/findmy/scenes/findmy_scene_main.c @@ -25,6 +25,8 @@ bool findmy_scene_main_on_event(void* context, SceneManagerEvent event) { findmy_toggle_beacon(app); 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()); } @@ -40,6 +42,8 @@ bool findmy_scene_main_on_event(void* context, SceneManagerEvent event) { findmy_change_broadcast_interval(app, app->state.broadcast_interval - 1); 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()); } From 61b35e07ff9d112423c98e72cf1ad41a6d8f5f95 Mon Sep 17 00:00:00 2001 From: Willy-JL <49810075+Willy-JL@users.noreply.github.com> Date: Fri, 8 Mar 2024 21:41:57 +0000 Subject: [PATCH 08/19] FindMy: Simpler beacon state sync --- applications/system/findmy/findmy.c | 5 ++--- applications/system/findmy/findmy_state.c | 14 ++++++++++---- .../system/findmy/scenes/findmy_scene_config_mac.c | 2 +- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/applications/system/findmy/findmy.c b/applications/system/findmy/findmy.c index f9dc292ca..921818103 100644 --- a/applications/system/findmy/findmy.c +++ b/applications/system/findmy/findmy.c @@ -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()); @@ -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()); diff --git a/applications/system/findmy/findmy_state.c b/applications/system/findmy/findmy_state.c index a8d15d75e..69c90389e 100644 --- a/applications/system/findmy/findmy_state.c +++ b/applications/system/findmy/findmy_state.c @@ -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)); @@ -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); diff --git a/applications/system/findmy/scenes/findmy_scene_config_mac.c b/applications/system/findmy/scenes/findmy_scene_config_mac.c index 2dc67f79d..1b72d1927 100644 --- a/applications/system/findmy/scenes/findmy_scene_config_mac.c +++ b/applications/system/findmy/scenes/findmy_scene_config_mac.c @@ -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()); } From f4ef9e50fbb81db602d539bc88720894b87e9b86 Mon Sep 17 00:00:00 2001 From: Willy-JL <49810075+Willy-JL@users.noreply.github.com> Date: Fri, 8 Mar 2024 22:35:24 +0000 Subject: [PATCH 09/19] FindMy: Fix build --- applications/system/findmy/findmy_state.c | 8 ++++---- applications/system/findmy/findmy_state.h | 2 ++ 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/applications/system/findmy/findmy_state.c b/applications/system/findmy/findmy_state.c index 69c90389e..12e8cbed2 100644 --- a/applications/system/findmy/findmy_state.c +++ b/applications/system/findmy/findmy_state.c @@ -96,10 +96,10 @@ 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)); + 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) { diff --git a/applications/system/findmy/findmy_state.h b/applications/system/findmy/findmy_state.h index 284f2dfbc..d11313e58 100644 --- a/applications/system/findmy/findmy_state.h +++ b/applications/system/findmy/findmy_state.h @@ -23,4 +23,6 @@ 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); From edf013eacda870b7004120b08c48b327cf507144 Mon Sep 17 00:00:00 2001 From: Matthew <113921492+MatthewKuKanich@users.noreply.github.com> Date: Fri, 8 Mar 2024 21:39:22 -0500 Subject: [PATCH 10/19] Update findmy.c - ignore changable byte --- applications/system/findmy/findmy.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/applications/system/findmy/findmy.c b/applications/system/findmy/findmy.c index 921818103..78303257a 100644 --- a/applications/system/findmy/findmy.c +++ b/applications/system/findmy/findmy.c @@ -135,7 +135,7 @@ 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[3] == 0x00 && // The state of the tag can vary, if we can find all known states this could work. I think its safe to ignore for now data[4] == 0x12 && // Type (FindMy) data[5] == 0x19 // Length ) { @@ -143,4 +143,4 @@ FindMyType findmy_data_get_type(uint8_t data[EXTRA_BEACON_MAX_DATA_SIZE]) { } else { return FindMyTypeSamsung; } -} \ No newline at end of file +} From d23f64d693ea22d50379d89689353cf998aff980 Mon Sep 17 00:00:00 2001 From: Willy-JL <49810075+Willy-JL@users.noreply.github.com> Date: Sat, 9 Mar 2024 04:19:50 +0000 Subject: [PATCH 11/19] FindMy: Add generate keys script --- applications/system/findmy/generate_keys.py | 84 +++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 applications/system/findmy/generate_keys.py diff --git a/applications/system/findmy/generate_keys.py b/applications/system/findmy/generate_keys.py new file mode 100644 index 000000000..59740f766 --- /dev/null +++ b/applications/system/findmy/generate_keys.py @@ -0,0 +1,84 @@ +import base64 +import os +from cryptography.hazmat.primitives.asymmetric import ec +from cryptography.hazmat.backends import default_backend +from cryptography.hazmat.primitives import hashes + +def advertisement_template(): + adv = "" + adv += "1e" # length (30) + adv += "ff" # manufacturer specific data + adv += "4c00" # company ID (Apple) + adv += "1219" # offline finding type and length + adv += "00" # state + for _ in range(22): + adv += "00" + adv += "00" # first two bits of key[0] + adv += "00" # hint + return bytearray.fromhex(adv) + +def convert_key_to_hex(private_key, public_key): + private_key_hex = private_key.private_numbers().private_value.to_bytes(28, byteorder='big').hex() + public_key_hex = public_key.public_numbers().x.to_bytes(28, byteorder='big').hex() + return private_key_hex, public_key_hex + +def generate_mac_and_payload(public_key): + public_key_bytes = public_key.public_numbers().x.to_bytes(28, byteorder='big') + mac = public_key_bytes[:6].hex() + payload = advertisement_template() + payload[7:29] = public_key_bytes[6:22] + return mac, payload.hex() + +def main(): + nkeys = int(input('Enter the number of keys to generate: ')) + prefix = input('Enter a name for the keyfiles (optional, press enter to skip): ') + print() + + if not os.path.exists('keys'): + os.makedirs('keys') + + for i in range(nkeys): + while True: + private_key = ec.generate_private_key(ec.SECP224R1(), default_backend()) + public_key = private_key.public_key() + + private_key_bytes = private_key.private_numbers().private_value.to_bytes(28, byteorder='big') + public_key_bytes = public_key.public_numbers().x.to_bytes(28, byteorder='big') + + private_key_b64 = base64.b64encode(private_key_bytes).decode("ascii") + public_key_b64 = base64.b64encode(public_key_bytes).decode("ascii") + + private_key_hex, public_key_hex = convert_key_to_hex(private_key, public_key) + mac, payload = generate_mac_and_payload(public_key) + + public_key_hash = hashes.Hash(hashes.SHA256()) + public_key_hash.update(public_key_bytes) + s256_b64 = base64.b64encode(public_key_hash.finalize()).decode("ascii") + + if '/' not in s256_b64[:7]: + fname = f"{prefix}_{s256_b64[:7]}.keys" if prefix else f"{s256_b64[:7]}.keys" + + print(f'{i + 1})') + print('Private key (Base64):', private_key_b64) + print('Public key (Base64):', public_key_b64) + print('Hashed adv key (Base64):', s256_b64) + print('---------------------------------------------------------------------------------') + print('Private key (Hex):', private_key_hex) + print('Public key (Hex):', public_key_hex) + print('---------------------------------------------------------------------------------') + print('MAC:', mac) + print('Payload:', payload) + print() + print('Place the .keys file onto your Flipper or input the MAC and Payload manually.') + + with open(f"keys/{fname}", 'w') as f: + f.write(f'Private key: {private_key_b64}\n') + f.write(f'Public key: {public_key_b64}\n') + f.write(f'Hashed adv key: {s256_b64}\n') + f.write(f'Private key (Hex): {private_key_hex}\n') + f.write(f'Public key (Hex): {public_key_hex}\n') + f.write(f'MAC: {mac}\n') + f.write(f'Payload: {payload}\n') + break + +main() \ No newline at end of file From 448033c8022dee407c3564650dbf0ed08bf7a4ce Mon Sep 17 00:00:00 2001 From: Willy-JL <49810075+Willy-JL@users.noreply.github.com> Date: Sat, 9 Mar 2024 04:20:04 +0000 Subject: [PATCH 12/19] FindMy: Fix key generation script --- applications/system/findmy/generate_keys.py | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/applications/system/findmy/generate_keys.py b/applications/system/findmy/generate_keys.py index 59740f766..505905104 100644 --- a/applications/system/findmy/generate_keys.py +++ b/applications/system/findmy/generate_keys.py @@ -23,11 +23,16 @@ def convert_key_to_hex(private_key, public_key): return private_key_hex, public_key_hex def generate_mac_and_payload(public_key): - public_key_bytes = public_key.public_numbers().x.to_bytes(28, byteorder='big') - mac = public_key_bytes[:6].hex() - payload = advertisement_template() - payload[7:29] = public_key_bytes[6:22] - return mac, payload.hex() + key = public_key.public_numbers().x.to_bytes(28, byteorder='big') + + addr = bytearray(key[:6]) + addr[0] |= 0b11000000 + + adv = advertisement_template() + adv[7:29] = key[6:28] + adv[29] = key[0] >> 6 + + return addr.hex(), adv.hex() def main(): nkeys = int(input('Enter the number of keys to generate: ')) @@ -57,14 +62,14 @@ def main(): if '/' not in s256_b64[:7]: fname = f"{prefix}_{s256_b64[:7]}.keys" if prefix else f"{s256_b64[:7]}.keys" - + print(f'{i + 1})') print('Private key (Base64):', private_key_b64) print('Public key (Base64):', public_key_b64) print('Hashed adv key (Base64):', s256_b64) print('---------------------------------------------------------------------------------') print('Private key (Hex):', private_key_hex) - print('Public key (Hex):', public_key_hex) + print('Public key (Hex):', public_key_hex) print('---------------------------------------------------------------------------------') print('MAC:', mac) print('Payload:', payload) From cd837576ea0e66954b9f3bf0953ffdc304332f35 Mon Sep 17 00:00:00 2001 From: Willy-JL <49810075+Willy-JL@users.noreply.github.com> Date: Sat, 9 Mar 2024 05:04:09 +0000 Subject: [PATCH 13/19] FindMy: Add OpenHaystack import from .keys file --- applications/system/findmy/application.fam | 1 + applications/system/findmy/findmy.c | 10 ++ applications/system/findmy/findmy_i.h | 11 ++ applications/system/findmy/helpers/base64.c | 141 +++++++++++++++++ applications/system/findmy/helpers/base64.h | 21 +++ .../system/findmy/icons/text_10px.png | Bin 0 -> 158 bytes .../findmy/scenes/findmy_scene_config.c | 13 +- .../scenes/findmy_scene_config_import.c | 145 ++++++++++++++++++ .../findmy_scene_config_import_result.c | 58 +++++++ .../system/findmy/scenes/findmy_scenes.h | 2 + 10 files changed, 399 insertions(+), 3 deletions(-) create mode 100644 applications/system/findmy/helpers/base64.c create mode 100644 applications/system/findmy/helpers/base64.h create mode 100644 applications/system/findmy/icons/text_10px.png create mode 100644 applications/system/findmy/scenes/findmy_scene_config_import.c create mode 100644 applications/system/findmy/scenes/findmy_scene_config_import_result.c diff --git a/applications/system/findmy/application.fam b/applications/system/findmy/application.fam index 9a7b2df94..2ef49310f 100644 --- a/applications/system/findmy/application.fam +++ b/applications/system/findmy/application.fam @@ -6,6 +6,7 @@ App( requires=["gui"], stack_size=2 * 1024, fap_icon="location_icon.png", + fap_icon_assets="icons", fap_category="Bluetooth", fap_author="@MatthewKuKanich", fap_weburl="https://github.com/MatthewKuKanich/FindMyFlipper", diff --git a/applications/system/findmy/findmy.c b/applications/system/findmy/findmy.c index 921818103..b51343ce1 100644 --- a/applications/system/findmy/findmy.c +++ b/applications/system/findmy/findmy.c @@ -16,6 +16,8 @@ static FindMy* findmy_app_alloc() { FindMy* app = malloc(sizeof(FindMy)); app->gui = furi_record_open(RECORD_GUI); + app->storage = furi_record_open(RECORD_STORAGE); + app->dialogs = furi_record_open(RECORD_DIALOGS); app->view_dispatcher = view_dispatcher_alloc(); view_dispatcher_enable_queue(app->view_dispatcher); @@ -41,6 +43,9 @@ static FindMy* findmy_app_alloc() { FindMyViewVarItemList, variable_item_list_get_view(app->var_item_list)); + app->popup = popup_alloc(); + view_dispatcher_add_view(app->view_dispatcher, FindMyViewPopup, popup_get_view(app->popup)); + view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen); findmy_state_load(&app->state); @@ -56,6 +61,9 @@ static FindMy* findmy_app_alloc() { static void findmy_app_free(FindMy* app) { furi_assert(app); + view_dispatcher_remove_view(app->view_dispatcher, FindMyViewPopup); + popup_free(app->popup); + view_dispatcher_remove_view(app->view_dispatcher, FindMyViewVarItemList); variable_item_list_free(app->var_item_list); @@ -68,6 +76,8 @@ static void findmy_app_free(FindMy* app) { view_dispatcher_free(app->view_dispatcher); scene_manager_free(app->scene_manager); + furi_record_close(RECORD_DIALOGS); + furi_record_close(RECORD_STORAGE); furi_record_close(RECORD_GUI); free(app); diff --git a/applications/system/findmy/findmy_i.h b/applications/system/findmy/findmy_i.h index c5991b5f4..c7ff965ef 100644 --- a/applications/system/findmy/findmy_i.h +++ b/applications/system/findmy/findmy_i.h @@ -5,22 +5,32 @@ #include #include #include +#include "findmy_icons.h" +#include +#include #include +#include +#include #include #include #include "views/findmy_main.h" #include #include +#include #include "scenes/findmy_scene.h" +#include "helpers/base64.h" struct FindMy { Gui* gui; + Storage* storage; + DialogsApp* dialogs; SceneManager* scene_manager; ViewDispatcher* view_dispatcher; FindMyMain* findmy_main; ByteInput* byte_input; VariableItemList* var_item_list; + Popup* popup; uint8_t mac_buf[EXTRA_BEACON_MAC_ADDR_SIZE]; uint8_t packet_buf[EXTRA_BEACON_MAX_DATA_SIZE]; @@ -32,6 +42,7 @@ typedef enum { FindMyViewMain, FindMyViewByteInput, FindMyViewVarItemList, + FindMyViewPopup, } FindMyView; enum FindMyType { diff --git a/applications/system/findmy/helpers/base64.c b/applications/system/findmy/helpers/base64.c new file mode 100644 index 000000000..f1fb71870 --- /dev/null +++ b/applications/system/findmy/helpers/base64.c @@ -0,0 +1,141 @@ +/* + * Base64 encoding/decoding (RFC1341) + * Copyright (c) 2005-2011, Jouni Malinen + * + * This software may be distributed under the terms of the BSD license. + * See README for more details. + */ +// https://web.mit.edu/freebsd/head/contrib/wpa/src/utils/base64.c + +#include "base64.h" + +#define os_malloc malloc +#define os_free free +#define os_memset memset + +static const unsigned char base64_table[65] = + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + +/** + * base64_encode - Base64 encode + * @src: Data to be encoded + * @len: Length of the data to be encoded + * @out_len: Pointer to output length variable, or %NULL if not used + * Returns: Allocated buffer of out_len bytes of encoded data, + * or %NULL on failure + * + * Caller is responsible for freeing the returned buffer. Returned buffer is + * nul terminated to make it easier to use as a C string. The nul terminator is + * not included in out_len. + */ +unsigned char* base64_encode(const unsigned char* src, size_t len, size_t* out_len) { + unsigned char *out, *pos; + const unsigned char *end, *in; + size_t olen; + int line_len; + + olen = len * 4 / 3 + 4; /* 3-byte blocks to 4-byte */ + olen += olen / 72; /* line feeds */ + olen++; /* nul termination */ + if(olen < len) return NULL; /* integer overflow */ + out = os_malloc(olen); + if(out == NULL) return NULL; + + end = src + len; + in = src; + pos = out; + line_len = 0; + while(end - in >= 3) { + *pos++ = base64_table[in[0] >> 2]; + *pos++ = base64_table[((in[0] & 0x03) << 4) | (in[1] >> 4)]; + *pos++ = base64_table[((in[1] & 0x0f) << 2) | (in[2] >> 6)]; + *pos++ = base64_table[in[2] & 0x3f]; + in += 3; + line_len += 4; + if(line_len >= 72) { + *pos++ = '\n'; + line_len = 0; + } + } + + if(end - in) { + *pos++ = base64_table[in[0] >> 2]; + if(end - in == 1) { + *pos++ = base64_table[(in[0] & 0x03) << 4]; + *pos++ = '='; + } else { + *pos++ = base64_table[((in[0] & 0x03) << 4) | (in[1] >> 4)]; + *pos++ = base64_table[(in[1] & 0x0f) << 2]; + } + *pos++ = '='; + line_len += 4; + } + + if(line_len) *pos++ = '\n'; + + *pos = '\0'; + if(out_len) *out_len = pos - out; + return out; +} + +/** + * base64_decode - Base64 decode + * @src: Data to be decoded + * @len: Length of the data to be decoded + * @out_len: Pointer to output length variable + * Returns: Allocated buffer of out_len bytes of decoded data, + * or %NULL on failure + * + * Caller is responsible for freeing the returned buffer. + */ +unsigned char* base64_decode(const unsigned char* src, size_t len, size_t* out_len) { + unsigned char dtable[256], *out, *pos, block[4], tmp; + size_t i, count, olen; + int pad = 0; + + os_memset(dtable, 0x80, 256); + for(i = 0; i < sizeof(base64_table) - 1; i++) dtable[base64_table[i]] = (unsigned char)i; + dtable['='] = 0; + + count = 0; + for(i = 0; i < len; i++) { + if(dtable[src[i]] != 0x80) count++; + } + + if(count == 0 || count % 4) return NULL; + + olen = count / 4 * 3; + pos = out = os_malloc(olen); + if(out == NULL) return NULL; + + count = 0; + for(i = 0; i < len; i++) { + tmp = dtable[src[i]]; + if(tmp == 0x80) continue; + + if(src[i] == '=') pad++; + block[count] = tmp; + count++; + if(count == 4) { + *pos++ = (block[0] << 2) | (block[1] >> 4); + *pos++ = (block[1] << 4) | (block[2] >> 2); + *pos++ = (block[2] << 6) | block[3]; + count = 0; + if(pad) { + if(pad == 1) + pos--; + else if(pad == 2) + pos -= 2; + else { + /* Invalid padding */ + os_free(out); + return NULL; + } + break; + } + } + } + + *out_len = pos - out; + return out; +} diff --git a/applications/system/findmy/helpers/base64.h b/applications/system/findmy/helpers/base64.h new file mode 100644 index 000000000..333f82dc1 --- /dev/null +++ b/applications/system/findmy/helpers/base64.h @@ -0,0 +1,21 @@ +/* + * Base64 encoding/decoding (RFC1341) + * Copyright (c) 2005, Jouni Malinen + * + * This software may be distributed under the terms of the BSD license. + * See README for more details. + */ +// https://web.mit.edu/freebsd/head/contrib/wpa/src/utils/base64.h + +#ifndef BASE64_H +#define BASE64_H + +#include +#include +#include +#include + +unsigned char* base64_encode(const unsigned char* src, size_t len, size_t* out_len); +unsigned char* base64_decode(const unsigned char* src, size_t len, size_t* out_len); + +#endif /* BASE64_H */ \ No newline at end of file diff --git a/applications/system/findmy/icons/text_10px.png b/applications/system/findmy/icons/text_10px.png new file mode 100644 index 0000000000000000000000000000000000000000..8e8a6183dd50535729dc9c9b4f220a12dd4c600f GIT binary patch literal 158 zcmeAS@N?(olHy`uVBq!ia0vp^AT}2VGmzZ%#=aj&u?6^qxB}__|Nk$&IsYz@rRC}3 z7*a7OIiZ2U&CSi=;0cBn1vTatM&Z;3u7g(^G9`qQn09G2aWeNXaKC0S=Q~tg57Z@F z;u=vBoS#-wo>-L1;E+?AmspUPnOCA;ke9BToS%}K{MA`f4ycg9)78&qol`;+00Iau A9smFU literal 0 HcmV?d00001 diff --git a/applications/system/findmy/scenes/findmy_scene_config.c b/applications/system/findmy/scenes/findmy_scene_config.c index 415bda945..eba1a85a2 100644 --- a/applications/system/findmy/scenes/findmy_scene_config.c +++ b/applications/system/findmy/scenes/findmy_scene_config.c @@ -3,7 +3,8 @@ enum VarItemListIndex { VarItemListIndexBroadcastInterval, VarItemListIndexTransmitPower, - VarItemListIndexRegisterTag, + VarItemListIndexImportTagFromFile, + VarItemListIndexRegisterTagManually, VarItemListIndexAbout, }; @@ -57,7 +58,10 @@ 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, "Register Tag", 0, NULL, NULL); + 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, "Matthew KuKanich, Thanks to Chapoly1305, WillyJL, OpenHaystack, Testers", @@ -82,7 +86,10 @@ 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 VarItemListIndexRegisterTag: + case VarItemListIndexImportTagFromFile: + scene_manager_next_scene(app->scene_manager, FindMySceneConfigImport); + break; + case VarItemListIndexRegisterTagManually: scene_manager_next_scene(app->scene_manager, FindMySceneConfigMac); break; case VarItemListIndexAbout: diff --git a/applications/system/findmy/scenes/findmy_scene_config_import.c b/applications/system/findmy/scenes/findmy_scene_config_import.c new file mode 100644 index 000000000..8dc9a07e7 --- /dev/null +++ b/applications/system/findmy/scenes/findmy_scene_config_import.c @@ -0,0 +1,145 @@ +#include "../findmy_i.h" + +enum VarItemListIndex { + VarItemListIndexOpenHaystack, +}; + +static const char* parse_open_haystack(FindMy* app, const char* path) { + const char* error = NULL; + + Stream* stream = file_stream_alloc(app->storage); + FuriString* line = furi_string_alloc(); + do { + error = "Can't open file"; + if(!file_stream_open(stream, path, FSAM_READ, FSOM_OPEN_EXISTING)) break; + + error = "Wrong file format"; + while(stream_read_line(stream, line)) { + if(furi_string_start_with(line, "Public key: ") || + furi_string_start_with(line, "Advertisement key: ")) { + error = NULL; + break; + } + } + if(error) break; + + furi_string_right(line, furi_string_search_char(line, ':') + 2); + furi_string_trim(line); + + error = "Base64 failed"; + size_t decoded_len; + uint8_t* public_key = base64_decode( + (uint8_t*)furi_string_get_cstr(line), furi_string_size(line), &decoded_len); + if(decoded_len != 28) { + free(public_key); + break; + } + + memcpy(app->state.mac, public_key, sizeof(app->state.mac)); + app->state.mac[0] |= 0b11000000; + furi_hal_bt_reverse_mac_addr(app->state.mac); + + uint8_t advertisement_template[EXTRA_BEACON_MAX_DATA_SIZE] = { + 0x1e, // length (30) + 0xff, // manufacturer specific data + 0x4c, 0x00, // company ID (Apple) + 0x12, 0x19, // offline finding type and length + 0x00, //state + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, // first two bits of key[0] + 0x00, // hint + }; + 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); + + free(public_key); + error = NULL; + + } while(false); + furi_string_free(line); + file_stream_close(stream); + stream_free(stream); + + return error; +} + +void findmy_scene_config_import_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_import_on_enter(void* context) { + FindMy* app = context; + VariableItemList* var_item_list = app->var_item_list; + VariableItem* item; + + item = variable_item_list_add(var_item_list, "OpenHaystack .keys", 0, NULL, NULL); + + // This scene acts more like a submenu than a var item list tbh + UNUSED(item); + + variable_item_list_set_enter_callback(var_item_list, findmy_scene_config_import_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_import_on_event(void* context, SceneManagerEvent event) { + FindMy* app = context; + bool consumed = false; + + if(event.type == SceneManagerEventTypeCustom) { + scene_manager_set_scene_state(app->scene_manager, FindMySceneConfigImport, event.event); + consumed = true; + + const char* extension = NULL; + switch(event.event) { + case VarItemListIndexOpenHaystack: + extension = ".keys"; + break; + default: + break; + } + if(!extension) { + return consumed; + } + + const DialogsFileBrowserOptions browser_options = { + .extension = extension, + .icon = &I_text_10px, + .base_path = FINDMY_STATE_DIR, + }; + storage_simply_mkdir(app->storage, browser_options.base_path); + FuriString* path = furi_string_alloc_set_str(browser_options.base_path); + if(dialog_file_browser_show(app->dialogs, path, path, &browser_options)) { + // The parse functions return the error text, or NULL for success + // Used in result to show success or error message + const char* error = NULL; + switch(event.event) { + case VarItemListIndexOpenHaystack: + error = parse_open_haystack(app, furi_string_get_cstr(path)); + break; + } + scene_manager_set_scene_state( + app->scene_manager, FindMySceneConfigImportResult, (uint32_t)error); + scene_manager_next_scene(app->scene_manager, FindMySceneConfigImportResult); + } + furi_string_free(path); + } + + return consumed; +} + +void findmy_scene_config_import_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/applications/system/findmy/scenes/findmy_scene_config_import_result.c b/applications/system/findmy/scenes/findmy_scene_config_import_result.c new file mode 100644 index 000000000..62056e78a --- /dev/null +++ b/applications/system/findmy/scenes/findmy_scene_config_import_result.c @@ -0,0 +1,58 @@ +#include "../findmy_i.h" + +enum PopupEvent { + PopupEventExit, +}; + +static void findmy_scene_config_import_result_callback(void* context) { + FindMy* app = context; + + view_dispatcher_send_custom_event(app->view_dispatcher, PopupEventExit); +} + +void findmy_scene_config_import_result_on_enter(void* context) { + FindMy* app = context; + Popup* popup = app->popup; + + const char* error = (const char*)scene_manager_get_scene_state( + app->scene_manager, FindMySceneConfigImportResult); + if(error) { + popup_set_icon(popup, 83, 22, &I_WarningDolphinFlip_45x42); + popup_set_header(popup, "Error!", 13, 22, AlignLeft, AlignBottom); + popup_set_text(popup, error, 6, 26, AlignLeft, AlignTop); + popup_disable_timeout(popup); + } else { + popup_set_icon(popup, 36, 5, &I_DolphinDone_80x58); + popup_set_header(popup, "Imported!", 13, 22, AlignLeft, AlignBottom); + popup_enable_timeout(popup); + } + popup_set_timeout(popup, 1500); + popup_set_context(popup, app); + popup_set_callback(popup, findmy_scene_config_import_result_callback); + + view_dispatcher_switch_to_view(app->view_dispatcher, FindMyViewPopup); +} + +bool findmy_scene_config_import_result_on_event(void* context, SceneManagerEvent event) { + FindMy* app = context; + bool consumed = false; + + if(event.type == SceneManagerEventTypeCustom) { + consumed = true; + switch(event.event) { + case PopupEventExit: + scene_manager_search_and_switch_to_previous_scene( + app->scene_manager, FindMySceneConfig); + break; + default: + break; + } + } + + return consumed; +} + +void findmy_scene_config_import_result_on_exit(void* context) { + FindMy* app = context; + popup_reset(app->popup); +} diff --git a/applications/system/findmy/scenes/findmy_scenes.h b/applications/system/findmy/scenes/findmy_scenes.h index 683c52c5b..9a35c519d 100644 --- a/applications/system/findmy/scenes/findmy_scenes.h +++ b/applications/system/findmy/scenes/findmy_scenes.h @@ -1,4 +1,6 @@ ADD_SCENE(findmy, main, Main) ADD_SCENE(findmy, config, Config) +ADD_SCENE(findmy, config_import, ConfigImport) +ADD_SCENE(findmy, config_import_result, ConfigImportResult) ADD_SCENE(findmy, config_mac, ConfigMac) ADD_SCENE(findmy, config_packet, ConfigPacket) From fca5da1c424958d727ee223f866e5150719e18bf Mon Sep 17 00:00:00 2001 From: Willy-JL <49810075+Willy-JL@users.noreply.github.com> Date: Sat, 9 Mar 2024 05:04:37 +0000 Subject: [PATCH 14/19] Revert "Update findmy.c - ignore changable byte" This reverts commit edf013eacda870b7004120b08c48b327cf507144. --- applications/system/findmy/findmy.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/applications/system/findmy/findmy.c b/applications/system/findmy/findmy.c index 80244e642..b51343ce1 100644 --- a/applications/system/findmy/findmy.c +++ b/applications/system/findmy/findmy.c @@ -145,7 +145,7 @@ 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 && // The state of the tag can vary, if we can find all known states this could work. I think its safe to ignore for now + data[3] == 0x00 && // ... data[4] == 0x12 && // Type (FindMy) data[5] == 0x19 // Length ) { @@ -153,4 +153,4 @@ FindMyType findmy_data_get_type(uint8_t data[EXTRA_BEACON_MAX_DATA_SIZE]) { } else { return FindMyTypeSamsung; } -} +} \ No newline at end of file From 74c6440a27dbf877bdb1280e936c61dee0fa0c83 Mon Sep 17 00:00:00 2001 From: Willy-JL <49810075+Willy-JL@users.noreply.github.com> Date: Sat, 9 Mar 2024 05:09:48 +0000 Subject: [PATCH 15/19] Format --- applications/system/findmy/generate_keys.py | 87 +++++++++++++-------- 1 file changed, 55 insertions(+), 32 deletions(-) diff --git a/applications/system/findmy/generate_keys.py b/applications/system/findmy/generate_keys.py index 505905104..83ca274fd 100644 --- a/applications/system/findmy/generate_keys.py +++ b/applications/system/findmy/generate_keys.py @@ -4,6 +4,7 @@ from cryptography.hazmat.primitives.asymmetric import ec from cryptography.hazmat.backends import default_backend from cryptography.hazmat.primitives import hashes + def advertisement_template(): adv = "" adv += "1e" # length (30) @@ -17,13 +18,17 @@ def advertisement_template(): adv += "00" # hint return bytearray.fromhex(adv) + def convert_key_to_hex(private_key, public_key): - private_key_hex = private_key.private_numbers().private_value.to_bytes(28, byteorder='big').hex() - public_key_hex = public_key.public_numbers().x.to_bytes(28, byteorder='big').hex() + private_key_hex = ( + private_key.private_numbers().private_value.to_bytes(28, byteorder="big").hex() + ) + public_key_hex = public_key.public_numbers().x.to_bytes(28, byteorder="big").hex() return private_key_hex, public_key_hex + def generate_mac_and_payload(public_key): - key = public_key.public_numbers().x.to_bytes(28, byteorder='big') + key = public_key.public_numbers().x.to_bytes(28, byteorder="big") addr = bytearray(key[:6]) addr[0] |= 0b11000000 @@ -34,56 +39,74 @@ def generate_mac_and_payload(public_key): return addr.hex(), adv.hex() + def main(): - nkeys = int(input('Enter the number of keys to generate: ')) - prefix = input('Enter a name for the keyfiles (optional, press enter to skip): ') + nkeys = int(input("Enter the number of keys to generate: ")) + prefix = input("Enter a name for the keyfiles (optional, press enter to skip): ") print() - if not os.path.exists('keys'): - os.makedirs('keys') + if not os.path.exists("keys"): + os.makedirs("keys") for i in range(nkeys): while True: private_key = ec.generate_private_key(ec.SECP224R1(), default_backend()) public_key = private_key.public_key() - private_key_bytes = private_key.private_numbers().private_value.to_bytes(28, byteorder='big') - public_key_bytes = public_key.public_numbers().x.to_bytes(28, byteorder='big') + private_key_bytes = private_key.private_numbers().private_value.to_bytes( + 28, byteorder="big" + ) + public_key_bytes = public_key.public_numbers().x.to_bytes( + 28, byteorder="big" + ) private_key_b64 = base64.b64encode(private_key_bytes).decode("ascii") public_key_b64 = base64.b64encode(public_key_bytes).decode("ascii") - private_key_hex, public_key_hex = convert_key_to_hex(private_key, public_key) + private_key_hex, public_key_hex = convert_key_to_hex( + private_key, public_key + ) mac, payload = generate_mac_and_payload(public_key) public_key_hash = hashes.Hash(hashes.SHA256()) public_key_hash.update(public_key_bytes) s256_b64 = base64.b64encode(public_key_hash.finalize()).decode("ascii") - if '/' not in s256_b64[:7]: - fname = f"{prefix}_{s256_b64[:7]}.keys" if prefix else f"{s256_b64[:7]}.keys" + if "/" not in s256_b64[:7]: + fname = ( + f"{prefix}_{s256_b64[:7]}.keys" + if prefix + else f"{s256_b64[:7]}.keys" + ) - print(f'{i + 1})') - print('Private key (Base64):', private_key_b64) - print('Public key (Base64):', public_key_b64) - print('Hashed adv key (Base64):', s256_b64) - print('---------------------------------------------------------------------------------') - print('Private key (Hex):', private_key_hex) - print('Public key (Hex):', public_key_hex) - print('---------------------------------------------------------------------------------') - print('MAC:', mac) - print('Payload:', payload) + print(f"{i + 1})") + print("Private key (Base64):", private_key_b64) + print("Public key (Base64):", public_key_b64) + print("Hashed adv key (Base64):", s256_b64) + print( + "---------------------------------------------------------------------------------" + ) + print("Private key (Hex):", private_key_hex) + print("Public key (Hex):", public_key_hex) + print( + "---------------------------------------------------------------------------------" + ) + print("MAC:", mac) + print("Payload:", payload) print() - print('Place the .keys file onto your Flipper or input the MAC and Payload manually.') + print( + "Place the .keys file onto your Flipper or input the MAC and Payload manually." + ) - with open(f"keys/{fname}", 'w') as f: - f.write(f'Private key: {private_key_b64}\n') - f.write(f'Public key: {public_key_b64}\n') - f.write(f'Hashed adv key: {s256_b64}\n') - f.write(f'Private key (Hex): {private_key_hex}\n') - f.write(f'Public key (Hex): {public_key_hex}\n') - f.write(f'MAC: {mac}\n') - f.write(f'Payload: {payload}\n') + with open(f"keys/{fname}", "w") as f: + f.write(f"Private key: {private_key_b64}\n") + f.write(f"Public key: {public_key_b64}\n") + f.write(f"Hashed adv key: {s256_b64}\n") + f.write(f"Private key (Hex): {private_key_hex}\n") + f.write(f"Public key (Hex): {public_key_hex}\n") + f.write(f"MAC: {mac}\n") + f.write(f"Payload: {payload}\n") break -main() \ No newline at end of file + +main() From 0b814717403c3e1920cd98173cb9cb511142fb0b Mon Sep 17 00:00:00 2001 From: MatthewKuKanich Date: Sat, 9 Mar 2024 00:23:17 -0500 Subject: [PATCH 16/19] Replace Terminal menu with MNTM menu --- applications/services/gui/modules/menu.c | 103 +++++++++++++---------- lib/momentum/momentum.h | 2 +- 2 files changed, 60 insertions(+), 45 deletions(-) diff --git a/applications/services/gui/modules/menu.c b/applications/services/gui/modules/menu.c index 4fac392d8..7025b85a2 100644 --- a/applications/services/gui/modules/menu.c +++ b/applications/services/gui/modules/menu.c @@ -348,47 +348,68 @@ static void menu_draw_callback(Canvas* canvas, void* _model) { break; } - case MenuStyleTerminal: { - // Draw a border around the screen + case MenuStyleMNTM: { + // Reset canvas and set background + canvas_reset(canvas); + canvas_set_color(canvas, ColorBlack); + canvas_clear(canvas); + canvas_set_bitmap_mode(canvas, true); + canvas_set_font(canvas, FontPrimary); + canvas_draw_str(canvas, 5, 13, "Momentum"); + canvas_draw_icon(canvas, 62, 4, &I_Release_arrow_18x15); + canvas_draw_line(canvas, 5, 15, 59, 15); + canvas_draw_line(canvas, 7, 17, 61, 17); + canvas_draw_line(canvas, 10, 19, 63, 19); + char title[20]; + snprintf(title, sizeof(title), "%s", furi_hal_version_get_name_ptr()); + canvas_draw_str(canvas, 5, 34, title); + DateTime curr_dt; + furi_hal_rtc_get_datetime(&curr_dt); + int hour = curr_dt.hour; + int min = curr_dt.minute; + if(hour > 12) { + hour -= 12; + } + if(hour == 0) { + hour = 12; + } + canvas_set_font(canvas, FontSecondary); + char clk[20]; + snprintf(clk, sizeof(clk), "%02u:%02u", hour, min); + canvas_draw_str(canvas, 5, 46, clk); + + // Draw the selected menu item + MenuItem* item = MenuItemArray_get(model->items, position); + FuriString* name = furi_string_alloc(); + menu_short_name(item, name); + canvas_set_font(canvas, FontSecondary); + canvas_set_color(canvas, ColorBlack); + elements_bold_rounded_frame(canvas, 42, 23, 35, 33); + menu_centered_icon(canvas, item, 43, 24, 35, 32); canvas_draw_frame(canvas, 0, 0, 128, 64); - // current dir on the title bar - canvas_set_font(canvas, FontSecondary); - char title[20]; - snprintf(title, sizeof(title), "%s@fz: ~/Home", furi_hal_version_get_name_ptr()); - canvas_draw_str(canvas, 20, 10, title); + int startY = 15; + int itemHeight = 10; + int itemMaxVisible = 5; + size_t startItem = model->vertical_offset; + size_t endItem = startItem + itemMaxVisible; + endItem = (endItem > MenuItemArray_size(model->items)) ? + MenuItemArray_size(model->items) : + endItem; + size_t scroll_counter = menu_scroll_counter(model, item); - canvas_draw_str(canvas, 118, 9, "x"); // "X" button on the top-right corner - canvas_draw_frame(canvas, 116, 2, 8, 9); - canvas_draw_frame(canvas, 0, 0, 128, 13); - - // Display the user's name line at the bottom - canvas_set_font(canvas, FontBatteryPercent); - char prefix[15]; - snprintf(prefix, sizeof(prefix), "%s@fz:~$", furi_hal_version_get_name_ptr()); - canvas_draw_str(canvas, 2, 56, prefix); - - size_t name_start_x = 2 + (strlen(prefix) - 1) * 6; - - for(size_t i = 0; i < 4 && (position + i) < items_count; i++) { - item = MenuItemArray_get(model->items, position + i); + for(size_t i = startItem; i < endItem; i++) { + MenuItem* item = MenuItemArray_get(model->items, i); + FuriString* name = furi_string_alloc(); menu_short_name(item, name); - - size_t scroll_counter = menu_scroll_counter(model, item); - if(i == 0) { - // Display selected item to the right of the $ symbol - // May want to reduce spacing - elements_scrollable_text_line( - canvas, name_start_x, 56, 60, name, scroll_counter, false); - } else { - // Display the previous items above the user's name line - canvas_draw_str(canvas, 2, 56 - i * 12, item->label); - } + int yPos = startY + ((i - startItem) * itemHeight); + elements_scrollable_text_line(canvas, 83, yPos, 62, name, scroll_counter, false); + furi_string_free(name); } - + furi_string_free(name); + canvas_commit(canvas); break; } - default: break; } @@ -614,16 +635,13 @@ static void menu_process_up(Menu* menu) { switch(momentum_settings.menu_style) { case MenuStyleList: - case MenuStyleTerminal: + case MenuStyleMNTM: if(position > 0) { position--; - if(vertical_offset && vertical_offset == position) { - vertical_offset--; - } } else { position = count - 1; - vertical_offset = count - 8; } + vertical_offset = position; break; case MenuStyleWii: if(position % 2 || (position == count - 1 && count % 2)) { @@ -665,16 +683,13 @@ static void menu_process_down(Menu* menu) { switch(momentum_settings.menu_style) { case MenuStyleList: - case MenuStyleTerminal: + case MenuStyleMNTM: if(position < count - 1) { position++; - if(vertical_offset < count - 8 && vertical_offset == position - 7) { - vertical_offset++; - } } else { position = 0; - vertical_offset = 0; } + vertical_offset = position; break; case MenuStyleWii: if(position % 2 || (position == count - 1 && count % 2)) { diff --git a/lib/momentum/momentum.h b/lib/momentum/momentum.h index 6a8c82b5b..9b3974fd8 100644 --- a/lib/momentum/momentum.h +++ b/lib/momentum/momentum.h @@ -34,7 +34,7 @@ typedef enum { MenuStyleVertical, MenuStyleC64, MenuStyleCompact, - MenuStyleTerminal, + MenuStyleMNTM, MenuStyleCount, } MenuStyle; From 0baa796e335b313bb9124b527065bf37c474f23c Mon Sep 17 00:00:00 2001 From: Willy-JL <49810075+Willy-JL@users.noreply.github.com> Date: Sat, 9 Mar 2024 05:51:22 +0000 Subject: [PATCH 17/19] FindMy: Add import from nRF Connect .txt file --- applications/system/findmy/findmy_i.h | 1 + .../scenes/findmy_scene_config_import.c | 75 +++++++++++++++++++ 2 files changed, 76 insertions(+) diff --git a/applications/system/findmy/findmy_i.h b/applications/system/findmy/findmy_i.h index c7ff965ef..e008d30b3 100644 --- a/applications/system/findmy/findmy_i.h +++ b/applications/system/findmy/findmy_i.h @@ -8,6 +8,7 @@ #include "findmy_icons.h" #include #include +#include #include #include #include diff --git a/applications/system/findmy/scenes/findmy_scene_config_import.c b/applications/system/findmy/scenes/findmy_scene_config_import.c index 8dc9a07e7..aedd21066 100644 --- a/applications/system/findmy/scenes/findmy_scene_config_import.c +++ b/applications/system/findmy/scenes/findmy_scene_config_import.c @@ -1,9 +1,76 @@ #include "../findmy_i.h" enum VarItemListIndex { + VarItemListIndexNrfConnect, VarItemListIndexOpenHaystack, }; +static const char* parse_nrf_connect(FindMy* app, const char* path) { + const char* error = NULL; + + Stream* stream = file_stream_alloc(app->storage); + FuriString* line = furi_string_alloc(); + do { + // XX-XX-XX-XX-XX-XX_YYYY-MM-DD HH_MM_SS.txt + error = "Filename must\nhave MAC\naddress"; + uint8_t mac[EXTRA_BEACON_MAC_ADDR_SIZE]; + path_extract_filename_no_ext(path, line); + if(furi_string_size(line) < sizeof(mac) * 3 - 1) break; + error = NULL; + for(size_t i = 0; i < sizeof(mac); i++) { + char a = furi_string_get_char(line, i * 3); + char b = furi_string_get_char(line, i * 3 + 1); + if((a < 'A' && a > 'F') || (a < '0' && a > '9') || (b < 'A' && b > 'F') || + (b < '0' && b > '9') || !hex_char_to_uint8(a, b, &mac[i])) { + error = "Filename must\nhave MAC\naddress"; + break; + } + } + if(error) break; + furi_hal_bt_reverse_mac_addr(mac); + + error = "Can't open file"; + if(!file_stream_open(stream, path, FSAM_READ, FSOM_OPEN_EXISTING)) break; + + // YYYY-MM-DD HH:MM:SS.ms, XX dBm, 0xXXXXX + error = "Wrong file format"; + if(!stream_read_line(stream, line)) break; + const char* marker = " dBm, 0x"; + size_t pos = furi_string_search(line, marker); + if(pos == FURI_STRING_FAILURE) break; + furi_string_right(line, pos + strlen(marker)); + 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; + error = NULL; + for(size_t i = 0; i < sizeof(data); 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') || + (b < '0' && b > '9') || !hex_char_to_uint8(a, b, &data[i])) { + error = "Invalid payload"; + break; + } + } + if(error) break; + + 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); + + error = NULL; + + } while(false); + furi_string_free(line); + file_stream_close(stream); + stream_free(stream); + + return error; +} + static const char* parse_open_haystack(FindMy* app, const char* path) { const char* error = NULL; @@ -78,6 +145,8 @@ 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); + item = variable_item_list_add(var_item_list, "OpenHaystack .keys", 0, NULL, NULL); // This scene acts more like a submenu than a var item list tbh @@ -101,6 +170,9 @@ bool findmy_scene_config_import_on_event(void* context, SceneManagerEvent event) const char* extension = NULL; switch(event.event) { + case VarItemListIndexNrfConnect: + extension = ".txt"; + break; case VarItemListIndexOpenHaystack: extension = ".keys"; break; @@ -123,6 +195,9 @@ bool findmy_scene_config_import_on_event(void* context, SceneManagerEvent event) // Used in result to show success or error message const char* error = NULL; switch(event.event) { + case VarItemListIndexNrfConnect: + error = parse_nrf_connect(app, furi_string_get_cstr(path)); + break; case VarItemListIndexOpenHaystack: error = parse_open_haystack(app, furi_string_get_cstr(path)); break; From b253bede42138efb1e149c6ab1b2d03832983180 Mon Sep 17 00:00:00 2001 From: Willy-JL <49810075+Willy-JL@users.noreply.github.com> Date: Sat, 9 Mar 2024 05:51:48 +0000 Subject: [PATCH 18/19] FindMy: Fix imported text position --- .../system/findmy/scenes/findmy_scene_config_import_result.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/applications/system/findmy/scenes/findmy_scene_config_import_result.c b/applications/system/findmy/scenes/findmy_scene_config_import_result.c index 62056e78a..f15f55c06 100644 --- a/applications/system/findmy/scenes/findmy_scene_config_import_result.c +++ b/applications/system/findmy/scenes/findmy_scene_config_import_result.c @@ -23,7 +23,7 @@ void findmy_scene_config_import_result_on_enter(void* context) { popup_disable_timeout(popup); } else { popup_set_icon(popup, 36, 5, &I_DolphinDone_80x58); - popup_set_header(popup, "Imported!", 13, 22, AlignLeft, AlignBottom); + popup_set_header(popup, "Imported!", 7, 14, AlignLeft, AlignBottom); popup_enable_timeout(popup); } popup_set_timeout(popup, 1500); From 52d3fe9b388da9673f02c27d66b8a25cdcb86f0b Mon Sep 17 00:00:00 2001 From: Willy-JL <49810075+Willy-JL@users.noreply.github.com> Date: Sat, 9 Mar 2024 05:54:52 +0000 Subject: [PATCH 19/19] FindMy: Add docs from app repo Co-authored-by: MatthewKuKanich --- applications/system/findmy/README.md | 74 ++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 applications/system/findmy/README.md diff --git a/applications/system/findmy/README.md b/applications/system/findmy/README.md new file mode 100644 index 000000000..e697aeb7a --- /dev/null +++ b/applications/system/findmy/README.md @@ -0,0 +1,74 @@ +# FindMy Flipper - FindMy SmartTag Emulator + +This app extends the functionality of the FlipperZero's bluetooth capabilities, enabling it to act as an Apple AirTag or Samsung SmartTag, or even both simultaneously. It utilizes the FlipperZero's BLE beacon to broadcast a SmartTag signal to be picked up by the FindMy Network. I made this to serve as a versatile tool for tracking purposes, offering the ability to clone existing tags, generate OpenHaystack key pairs for integration with Apple's FindMy network, and tune the device's beacon broadcast settings. + +## Features + +1. Tag Emulation: Clone your existing Apple AirTag or Samsung SmartTag to the FlipperZero, or generate a key pair for use with the FindMy network without owning an actual AirTag. +2. Customization: Users can adjust the interval between beacon broadcasts and modify the transmit power to suit their needs, optimizing for both visibility and battery life. +3. Efficient Background Operation: The app is optimized to run in the background, ensuring that your FlipperZero can still be tracked with minimal battery usage and without stopping normal use. + +## Usage Guide + +### Step 1: Installation +- **Option A:** Use the released/precompiled firmware appropriate (FAP) for your device. +- **Option B:** Build the firmware yourself using `fbt/ufbt`. +- Both Installation options require you to be running a dev build of firmware. When release gets access to the extra BLE beacon this will change, thank you! +### Step 2: Obtaining SmartTag Data + +#### Option A: Open Haystack Method +1. **Generate a Tag:** Download the `generate_keys.py` file and execute it in your terminal. (You will need cryptography ```python3 -m pip install cryptography```) +2. **Follow Prompts:** During execution, you'll be prompted for inputs. By the end, you'll obtain a **Private Key**, **Public Key**, **Payload**, and **MAC Address**. + - **Private Key** is necessary to receive location reports from Apple. + - **MAC Address** should be registered in the FlipperZero app: + 1. Open the app and navigate to the config menu. + 2. Choose "register tag" and enter the MAC Address when prompted. + 3. A payload dialog will appear next. Enter your **Payload** here. + 4. Click save. +3. **Configuration Completion:** With this setup, your device is ready for Open Haystack. Proceed with the specific steps for Open Haystack or MaclessHaystack based on your setup. + - Don't Own a Mac: https://github.com/dchristl/macless-haystack + - Own a Mac: https://github.com/seemoo-lab/openhaystack + +#### Option B: Cloning Existing Tag +1. **Pair a Tag:** First, pair an AirTag or Samsung SmartTag with your device. +2. **Enter 'Lost' Mode:** Keep the tag away from the device it's registered to for approximately 15 minutes. +3. **Download nrfConnect:** Install nrfConnect from the Apple App Store or Google Play Store. +4. **Filter and Scan:** + - Open the app, click on filters, and exclude all except for the brand of your tag (Apple/Samsung). + - Adjust the RSSI to the lowest setting (-40 dBm). + - Initiate a scan. Wait for your SmartTag to appear as a "FindMy" device. +5. **Capture Data:** Click **Raw** or **View Raw** to capture your **payload** and note your tag's **MAC Address**. Immediately remove the tag's battery to prevent key/MAC rotation. +6. **Enter Data in FlipperZero App:** Input the captured **payload** and **MAC Address** into the FlipperZero app. + +### Step 3: Configuration +- Upon launching the app, choose whether to clone an AirTag or SmartTag, generate a new Open Haystack key pair, or adjust broadcast settings. + +### Step 4: Tracking +- Once the app is configured, your FlipperZero can be tracked using the relevant platform's tracking service (FindMy app for Apple devices, SmartThings for Samsung devices, and respective web browsers). + + +Customization + +- Beacon Interval: Adjust how frequently your FlipperZero broadcasts its presence. +- Transmit Power: Increase or decrease the signal strength to balance between tracking range and battery life. + +Background Use + +The app is designed to have a negligible impact on battery life, even when running in the background. This allows for continuous tracking without the need for frequent recharging. + +Compatibility + +- Apple devices for AirTag tracking via the FindMy network. +- Any device that supports Samsung SmartTag tracking, including web browsers (previously FindMyMobile). + +Thanks + +- Huge thanks to all the people that contributed to the OpenHaystack project, supporting projects, and guides on the subject. This wouldn't be a thing without any of you! + +Legal and Privacy + +This app is intended for personal and educational use. Users are responsible for complying with local privacy laws and regulations regarding tracking devices. The cloning and emulation of tracking tags should be done responsibly and with respect to the ownership of the original devices. + +Disclaimer + +This project is not affiliated with Apple Inc. or Samsung. All product names, logos, and brands are property of their respective owners. Use this app responsibly and ethically.