add findmy to system apps [ci skip]

autoload by Willy-JL
This commit is contained in:
MX
2025-02-13 20:12:18 +03:00
parent 85cba9fd8d
commit 95483fb56f
33 changed files with 1725 additions and 1 deletions

View File

@@ -0,0 +1,31 @@
#include "findmy_scene.h"
#include "findmy.h"
// Generate scene on_enter handlers array
#define ADD_SCENE(prefix, name, id) prefix##_scene_##name##_on_enter,
void (*const findmy_on_enter_handlers[])(void*) = {
#include "findmy_scenes.h"
};
#undef ADD_SCENE
// Generate scene on_event handlers array
#define ADD_SCENE(prefix, name, id) prefix##_scene_##name##_on_event,
bool (*const findmy_on_event_handlers[])(void* context, SceneManagerEvent event) = {
#include "findmy_scenes.h"
};
#undef ADD_SCENE
// Generate scene on_exit handlers array
#define ADD_SCENE(prefix, name, id) prefix##_scene_##name##_on_exit,
void (*const findmy_on_exit_handlers[])(void* context) = {
#include "findmy_scenes.h"
};
#undef ADD_SCENE
// Initialize scene handlers configuration structure
const SceneManagerHandlers findmy_scene_handlers = {
.on_enter_handlers = findmy_on_enter_handlers,
.on_event_handlers = findmy_on_event_handlers,
.on_exit_handlers = findmy_on_exit_handlers,
.scene_num = FindMySceneNum,
};

View File

@@ -0,0 +1,30 @@
#pragma once
#include <gui/scene_manager.h>
#include "findmy.h"
// Generate scene id and total number
#define ADD_SCENE(prefix, name, id) FindMyScene##id,
typedef enum {
#include "findmy_scenes.h"
FindMySceneNum,
} FindMyScene;
#undef ADD_SCENE
extern const SceneManagerHandlers findmy_scene_handlers;
// Generate scene on_enter handlers declaration
#define ADD_SCENE(prefix, name, id) void prefix##_scene_##name##_on_enter(void*);
#include "findmy_scenes.h"
#undef ADD_SCENE
// Generate scene on_event handlers declaration
#define ADD_SCENE(prefix, name, id) \
bool prefix##_scene_##name##_on_event(void* context, SceneManagerEvent event);
#include "findmy_scenes.h"
#undef ADD_SCENE
// Generate scene on_exit handlers declaration
#define ADD_SCENE(prefix, name, id) void prefix##_scene_##name##_on_exit(void* context);
#include "findmy_scenes.h"
#undef ADD_SCENE

View File

@@ -0,0 +1,117 @@
#include "../findmy_i.h"
enum VarItemListIndex {
VarItemListIndexBroadcastInterval,
VarItemListIndexTransmitPower,
VarItemListIndexRegisterTag,
VarItemListIndexShowMac,
VarItemListIndexAbout,
};
void findmy_scene_config_broadcast_interval_changed(VariableItem* item) {
FindMy* app = variable_item_get_context(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->state.broadcast_interval);
variable_item_set_current_value_text(item, str);
variable_item_set_current_value_index(item, app->state.broadcast_interval - 1);
}
void findmy_scene_config_transmit_power_changed(VariableItem* item) {
FindMy* app = variable_item_get_context(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->state.transmit_power);
variable_item_set_current_value_text(item, str);
variable_item_set_current_value_index(item, app->state.transmit_power);
}
void findmy_scene_config_show_mac(VariableItem* item) {
FindMy* app = variable_item_get_context(item);
uint8_t index = variable_item_get_current_value_index(item);
findmy_toggle_show_mac(app, index);
variable_item_set_current_value_text(item, app->state.show_mac ? "Yes" : "No");
variable_item_set_current_value_index(item, app->state.show_mac);
}
void findmy_scene_config_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_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,
"Broadcast Interval",
10,
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->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->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(var_item_list, "Show MAC", 2, findmy_scene_config_show_mac, app);
variable_item_set_current_value_index(item, app->state.show_mac);
variable_item_set_current_value_text(item, app->state.show_mac ? "Yes" : "No");
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);
variable_item_list_set_selected_item(
var_item_list, scene_manager_get_scene_state(app->scene_manager, FindMySceneConfig));
view_dispatcher_switch_to_view(app->view_dispatcher, FindMyViewVarItemList);
}
bool findmy_scene_config_on_event(void* context, SceneManagerEvent event) {
FindMy* app = context;
bool consumed = false;
if(event.type == SceneManagerEventTypeCustom) {
scene_manager_set_scene_state(app->scene_manager, FindMySceneConfig, event.event);
consumed = true;
switch(event.event) {
case VarItemListIndexRegisterTag:
scene_manager_next_scene(app->scene_manager, FindMySceneConfigTagtype);
break;
case VarItemListIndexAbout:
break;
default:
break;
}
}
return consumed;
}
void findmy_scene_config_on_exit(void* context) {
FindMy* app = context;
VariableItemList* var_item_list = app->var_item_list;
variable_item_list_reset(var_item_list);
}

View File

@@ -0,0 +1,231 @@
#include "../findmy_i.h"
enum VarItemListIndex {
VarItemListIndexNrfConnect,
VarItemListIndexOpenHaystack,
VarItemListIndexRegisterTagManually,
};
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;
findmy_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";
size_t line_size = furi_string_size(line);
uint8_t data_size = findmy_state_data_size(app->state.tag_type);
FURI_LOG_I("ImportPayload", "Line Size: %d", line_size);
FURI_LOG_I("ImportPayload", "Data Size: %d", data_size * 2);
if(line_size != data_size * 2) break;
// Initialize full data to 0's, then fill only first data_size bytes
uint8_t data[EXTRA_BEACON_MAX_DATA_SIZE] = {0};
error = NULL;
for(size_t i = 0; i < data_size; i++) {
char a = furi_string_get_char(line, i * 2);
char b = furi_string_get_char(line, i * 2 + 1);
if((a < 'A' && a > 'F') || (a < '0' && a > '9') || (b < 'A' && b > 'F') ||
(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_save_and_apply(&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;
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;
findmy_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_save_and_apply(&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;
//variable_item_list_set_header(var_item_list, "Choose file type");
item = variable_item_list_add(var_item_list, "nRF Connect (.txt)", 0, NULL, NULL);
item = variable_item_list_add(var_item_list, "OpenHaystack (.keys)", 0, NULL, NULL);
item = variable_item_list_add(var_item_list, "Register Tag Manually", 0, NULL, NULL);
// This scene acts more like a submenu than a var item list tbh
UNUSED(item);
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 VarItemListIndexNrfConnect:
extension = ".txt";
break;
case VarItemListIndexOpenHaystack:
extension = ".keys";
break;
case VarItemListIndexRegisterTagManually:
scene_manager_next_scene(app->scene_manager, FindMySceneConfigMac);
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 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;
}
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);
}

View File

@@ -0,0 +1,60 @@
#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!", 7, 14, 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);
findmy_main_update_active(app->findmy_main, app->state.beacon_active);
findmy_main_update_mac(app->findmy_main, app->state.mac);
findmy_main_update_type(app->findmy_main, app->state.tag_type);
view_dispatcher_switch_to_view(app->view_dispatcher, FindMyViewPopup);
}
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);
}

View File

@@ -0,0 +1,60 @@
#include "../findmy_i.h"
enum ByteInputResult {
ByteInputResultOk,
};
static void findmy_scene_config_mac_callback(void* context) {
FindMy* app = context;
view_dispatcher_send_custom_event(app->view_dispatcher, ByteInputResultOk);
}
void findmy_scene_config_mac_on_enter(void* context) {
FindMy* app = context;
ByteInput* byte_input = app->byte_input;
byte_input_set_header_text(byte_input, "Enter Bluetooth MAC:");
memcpy(app->mac_buf, app->state.mac, sizeof(app->mac_buf));
findmy_reverse_mac_addr(app->mac_buf);
byte_input_set_result_callback(
byte_input,
findmy_scene_config_mac_callback,
NULL,
app,
app->mac_buf,
sizeof(app->mac_buf));
view_dispatcher_switch_to_view(app->view_dispatcher, FindMyViewByteInput);
}
bool findmy_scene_config_mac_on_event(void* context, SceneManagerEvent event) {
FindMy* app = context;
bool consumed = false;
if(event.type == SceneManagerEventTypeCustom) {
consumed = true;
switch(event.event) {
case ByteInputResultOk:
findmy_reverse_mac_addr(app->mac_buf);
memcpy(&app->state.mac, app->mac_buf, sizeof(app->state.mac));
findmy_state_save_and_apply(&app->state);
findmy_main_update_mac(app->findmy_main, app->state.mac);
scene_manager_next_scene(app->scene_manager, FindMySceneConfigPacket);
break;
default:
break;
}
}
return consumed;
}
void findmy_scene_config_mac_on_exit(void* context) {
FindMy* app = context;
byte_input_set_result_callback(app->byte_input, NULL, NULL, NULL, NULL, 0);
byte_input_set_header_text(app->byte_input, "");
}

View File

@@ -0,0 +1,58 @@
#include "../findmy_i.h"
enum ByteInputResult {
ByteInputResultOk,
};
static void findmy_scene_config_packet_callback(void* context) {
FindMy* app = context;
view_dispatcher_send_custom_event(app->view_dispatcher, ByteInputResultOk);
}
void findmy_scene_config_packet_on_enter(void* context) {
FindMy* app = context;
ByteInput* byte_input = app->byte_input;
byte_input_set_header_text(byte_input, "Enter Bluetooth Payload:");
memcpy(app->packet_buf, app->state.data, findmy_state_data_size(app->state.tag_type));
byte_input_set_result_callback(
byte_input,
findmy_scene_config_packet_callback,
NULL,
app,
app->packet_buf,
findmy_state_data_size(app->state.tag_type));
view_dispatcher_switch_to_view(app->view_dispatcher, FindMyViewByteInput);
}
bool findmy_scene_config_packet_on_event(void* context, SceneManagerEvent event) {
FindMy* app = context;
bool consumed = false;
if(event.type == SceneManagerEventTypeCustom) {
consumed = true;
switch(event.event) {
case ByteInputResultOk:
scene_manager_search_and_switch_to_previous_scene(
app->scene_manager, FindMySceneConfig);
memcpy(app->state.data, app->packet_buf, findmy_state_data_size(app->state.tag_type));
findmy_state_save_and_apply(&app->state);
break;
default:
break;
}
}
return consumed;
}
void findmy_scene_config_packet_on_exit(void* context) {
FindMy* app = context;
byte_input_set_result_callback(app->byte_input, NULL, NULL, NULL, NULL, 0);
byte_input_set_header_text(app->byte_input, "");
}

View File

@@ -0,0 +1,70 @@
#include "../findmy_i.h"
enum VarItemListIndex {
VarItemListIndexApple,
VarItemListIndexSamsung,
VarItemListIndexTile,
};
void findmy_scene_config_tagtype_callback(void* context, uint32_t index) {
furi_assert(context);
FindMy* app = context;
view_dispatcher_send_custom_event(app->view_dispatcher, index);
}
void findmy_scene_config_tagtype_on_enter(void* context) {
FindMy* app = context;
VariableItemList* var_item_list = app->var_item_list;
VariableItem* item;
//variable_item_list_set_header(var_item_list, "Choose tag type");
item = variable_item_list_add(var_item_list, "Apple AirTag", 0, NULL, NULL);
item = variable_item_list_add(var_item_list, "Samsung SmartTag", 0, NULL, NULL);
item = variable_item_list_add(var_item_list, "Tile SmartTag", 0, NULL, NULL);
UNUSED(item);
variable_item_list_set_enter_callback(
var_item_list, findmy_scene_config_tagtype_callback, app);
variable_item_list_set_selected_item(
var_item_list, scene_manager_get_scene_state(app->scene_manager, FindMySceneConfigImport));
view_dispatcher_switch_to_view(app->view_dispatcher, FindMyViewVarItemList);
}
bool findmy_scene_config_tagtype_on_event(void* context, SceneManagerEvent event) {
FindMy* app = context;
bool consumed = false;
if(event.type == SceneManagerEventTypeCustom) {
scene_manager_set_scene_state(app->scene_manager, FindMySceneConfigTagtype, event.event);
consumed = true;
switch(event.event) {
case VarItemListIndexApple:
findmy_set_tag_type(app, FindMyTypeApple);
break;
case VarItemListIndexSamsung:
findmy_set_tag_type(app, FindMyTypeSamsung);
break;
case VarItemListIndexTile:
findmy_set_tag_type(app, FindMyTypeTile);
break;
default:
break;
}
scene_manager_next_scene(app->scene_manager, FindMySceneConfigImport);
}
return consumed;
}
void findmy_scene_config_tagtype_on_exit(void* context) {
FindMy* app = context;
VariableItemList* var_item_list = app->var_item_list;
variable_item_list_reset(var_item_list);
}

View File

@@ -0,0 +1,56 @@
#include "../findmy_i.h"
void findmy_scene_main_callback(FindMyMainEvent event, void* context) {
furi_assert(context);
FindMy* app = context;
view_dispatcher_send_custom_event(app->view_dispatcher, event);
}
void findmy_scene_main_on_enter(void* context) {
FindMy* app = context;
findmy_main_set_callback(app->findmy_main, findmy_scene_main_callback, app);
view_dispatcher_switch_to_view(app->view_dispatcher, FindMyViewMain);
}
bool findmy_scene_main_on_event(void* context, SceneManagerEvent event) {
FindMy* app = context;
bool consumed = false;
if(event.type == SceneManagerEventTypeCustom) {
consumed = true;
switch(event.event) {
case FindMyMainEventToggle:
findmy_toggle_beacon(app);
break;
case FindMyMainEventBackground:
app->state.beacon_active = true;
findmy_state_save_and_apply(&app->state);
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->state.broadcast_interval + 1);
break;
case FindMyMainEventIntervalDown:
findmy_change_broadcast_interval(app, app->state.broadcast_interval - 1);
break;
case FindMyMainEventQuit:
app->state.beacon_active = false;
findmy_state_save_and_apply(&app->state);
break;
default:
consumed = false;
break;
}
}
return consumed;
}
void findmy_scene_main_on_exit(void* context) {
FindMy* app = context;
UNUSED(app);
}

View File

@@ -0,0 +1,7 @@
ADD_SCENE(findmy, main, Main)
ADD_SCENE(findmy, config, Config)
ADD_SCENE(findmy, config_import, ConfigImport)
ADD_SCENE(findmy, config_tagtype, ConfigTagtype)
ADD_SCENE(findmy, config_import_result, ConfigImportResult)
ADD_SCENE(findmy, config_mac, ConfigMac)
ADD_SCENE(findmy, config_packet, ConfigPacket)