unleashed-firmware/applications/external/flipfrid/scene/flipfrid_scene_load_file.c

193 lines
7.5 KiB
C
Raw Normal View History

2022-09-01 15:03:18 +00:00
#include "flipfrid_scene_load_file.h"
2022-09-04 21:32:58 +00:00
#include "flipfrid_scene_entrypoint.h"
2022-09-01 15:03:18 +00:00
#define LFRFID_APP_EXTENSION ".rfid"
2022-09-05 11:05:43 +00:00
#define LFRFID_APP_PATH_FOLDER "/ext/lfrfid"
2022-09-01 15:03:18 +00:00
bool flipfrid_load(FlipFridState* context, const char* file_path) {
bool result = false;
Storage* storage = furi_record_open(RECORD_STORAGE);
FlipperFormat* fff_data_file = flipper_format_file_alloc(storage);
2022-10-05 18:27:13 +00:00
FuriString* temp_str;
temp_str = furi_string_alloc();
2022-09-01 15:03:18 +00:00
do {
if(!flipper_format_file_open_existing(fff_data_file, file_path)) {
FURI_LOG_E(TAG, "Error open file %s", file_path);
2022-10-05 18:27:13 +00:00
furi_string_reset(context->notification_msg);
furi_string_set(context->notification_msg, "Error open file");
2022-09-01 15:03:18 +00:00
break;
}
// FileType
if(!flipper_format_read_string(fff_data_file, "Filetype", temp_str)) {
FURI_LOG_E(TAG, "Missing or incorrect Filetype");
2022-10-05 18:27:13 +00:00
furi_string_reset(context->notification_msg);
furi_string_set(context->notification_msg, "Missing or incorrect Filetypes");
2022-09-01 15:03:18 +00:00
break;
} else {
2022-10-05 18:27:13 +00:00
FURI_LOG_I(TAG, "Filetype: %s", furi_string_get_cstr(temp_str));
2022-09-01 15:03:18 +00:00
}
// Key type
if(!flipper_format_read_string(fff_data_file, "Key type", temp_str)) {
FURI_LOG_E(TAG, "Missing or incorrect Key type");
2022-10-05 18:27:13 +00:00
furi_string_reset(context->notification_msg);
furi_string_set(context->notification_msg, "Missing or incorrect Key type");
2022-09-01 15:03:18 +00:00
break;
} else {
2022-10-05 18:27:13 +00:00
FURI_LOG_I(TAG, "Key type: %s", furi_string_get_cstr(temp_str));
if(context->proto == EM4100) {
2022-10-05 18:27:13 +00:00
if(strcmp(furi_string_get_cstr(temp_str), "EM4100") != 0) {
FURI_LOG_E(TAG, "Unsupported Key type");
2022-10-05 18:27:13 +00:00
furi_string_reset(context->notification_msg);
furi_string_set(context->notification_msg, "Unsupported Key type");
break;
}
} else if(context->proto == PAC) {
2022-10-05 18:27:13 +00:00
if(strcmp(furi_string_get_cstr(temp_str), "PAC/Stanley") != 0) {
FURI_LOG_E(TAG, "Unsupported Key type");
2022-10-05 18:27:13 +00:00
furi_string_reset(context->notification_msg);
furi_string_set(context->notification_msg, "Unsupported Key type");
break;
}
} else if(context->proto == H10301) {
2022-10-05 18:27:13 +00:00
if(strcmp(furi_string_get_cstr(temp_str), "H10301") != 0) {
FURI_LOG_E(TAG, "Unsupported Key type");
2022-10-05 18:27:13 +00:00
furi_string_reset(context->notification_msg);
furi_string_set(context->notification_msg, "Unsupported Key type");
break;
}
} else {
2022-10-05 18:27:13 +00:00
if(strcmp(furi_string_get_cstr(temp_str), "HIDProx") != 0) {
FURI_LOG_E(TAG, "Unsupported Key type");
2022-10-05 18:27:13 +00:00
furi_string_reset(context->notification_msg);
furi_string_set(context->notification_msg, "Unsupported Key type");
break;
}
2022-09-01 15:03:18 +00:00
}
}
// Data
if(!flipper_format_read_string(fff_data_file, "Data", context->data_str)) {
FURI_LOG_E(TAG, "Missing or incorrect Data");
2022-10-05 18:27:13 +00:00
furi_string_reset(context->notification_msg);
furi_string_set(context->notification_msg, "Missing or incorrect Key");
2022-09-01 15:03:18 +00:00
break;
} else {
2022-10-05 18:27:13 +00:00
FURI_LOG_I(TAG, "Key: %s", furi_string_get_cstr(context->data_str));
2022-09-01 15:03:18 +00:00
if(context->proto == EM4100) {
2022-10-05 18:27:13 +00:00
if(furi_string_size(context->data_str) != 14) {
FURI_LOG_E(TAG, "Incorrect Key length");
2022-10-05 18:27:13 +00:00
furi_string_reset(context->notification_msg);
furi_string_set(context->notification_msg, "Incorrect Key length");
break;
}
} else if(context->proto == PAC) {
2022-10-05 18:27:13 +00:00
if(furi_string_size(context->data_str) != 11) {
FURI_LOG_E(TAG, "Incorrect Key length");
2022-10-05 18:27:13 +00:00
furi_string_reset(context->notification_msg);
furi_string_set(context->notification_msg, "Incorrect Key length");
break;
}
} else if(context->proto == H10301) {
2022-10-05 18:27:13 +00:00
if(furi_string_size(context->data_str) != 8) {
FURI_LOG_E(TAG, "Incorrect Key length");
2022-10-05 18:27:13 +00:00
furi_string_reset(context->notification_msg);
furi_string_set(context->notification_msg, "Incorrect Key length");
break;
}
} else {
2022-10-05 18:27:13 +00:00
if(furi_string_size(context->data_str) != 17) {
FURI_LOG_E(TAG, "Incorrect Key length");
2022-10-05 18:27:13 +00:00
furi_string_reset(context->notification_msg);
furi_string_set(context->notification_msg, "Incorrect Key length");
break;
}
2022-09-01 15:03:18 +00:00
}
2022-09-01 15:03:18 +00:00
// String to uint8_t
for(uint8_t i = 0; i < 6; i++) {
2022-09-01 15:03:18 +00:00
char temp_str2[3];
2022-10-05 18:27:13 +00:00
temp_str2[0] = furi_string_get_cstr(context->data_str)[i * 3];
temp_str2[1] = furi_string_get_cstr(context->data_str)[i * 3 + 1];
2022-09-01 15:03:18 +00:00
temp_str2[2] = '\0';
context->data[i] = (uint8_t)strtol(temp_str2, NULL, 16);
}
}
result = true;
} while(0);
2022-10-05 18:27:13 +00:00
furi_string_free(temp_str);
2022-09-01 15:03:18 +00:00
flipper_format_free(fff_data_file);
if(result) {
FURI_LOG_I(TAG, "Loaded successfully");
2022-10-05 18:27:13 +00:00
furi_string_reset(context->notification_msg);
furi_string_set(context->notification_msg, "Source loaded.");
2022-09-01 15:03:18 +00:00
}
return result;
}
void flipfrid_scene_load_file_on_enter(FlipFridState* context) {
if(flipfrid_load_protocol_from_file(context)) {
context->current_scene = SceneSelectField;
} else {
2022-09-04 21:32:58 +00:00
flipfrid_scene_entrypoint_on_enter(context);
2022-09-01 15:03:18 +00:00
context->current_scene = SceneEntryPoint;
}
}
void flipfrid_scene_load_file_on_exit(FlipFridState* context) {
UNUSED(context);
}
void flipfrid_scene_load_file_on_tick(FlipFridState* context) {
UNUSED(context);
}
void flipfrid_scene_load_file_on_event(FlipFridEvent event, FlipFridState* context) {
if(event.evt_type == EventTypeKey) {
if(event.input_type == InputTypeShort) {
switch(event.key) {
case InputKeyDown:
case InputKeyUp:
case InputKeyLeft:
case InputKeyRight:
case InputKeyOk:
case InputKeyBack:
context->current_scene = SceneEntryPoint;
break;
2022-11-02 20:07:00 +00:00
default:
break;
2022-09-01 15:03:18 +00:00
}
}
}
}
void flipfrid_scene_load_file_on_draw(Canvas* canvas, FlipFridState* context) {
UNUSED(context);
UNUSED(canvas);
}
bool flipfrid_load_protocol_from_file(FlipFridState* context) {
2022-10-05 18:27:13 +00:00
FuriString* user_file_path;
user_file_path = furi_string_alloc();
furi_string_set(user_file_path, LFRFID_APP_PATH_FOLDER);
2022-09-01 15:03:18 +00:00
2022-09-14 18:04:04 +00:00
DialogsFileBrowserOptions browser_options;
dialog_file_browser_set_basic_options(&browser_options, LFRFID_APP_EXTENSION, &I_125_10px);
2022-12-08 07:10:38 +00:00
browser_options.base_path = LFRFID_APP_PATH_FOLDER;
2022-09-14 18:04:04 +00:00
2022-09-01 15:03:18 +00:00
// Input events and views are managed by file_select
bool res = dialog_file_browser_show(
2022-09-14 18:04:04 +00:00
context->dialogs, user_file_path, user_file_path, &browser_options);
2022-09-01 15:03:18 +00:00
if(res) {
2022-10-05 18:27:13 +00:00
res = flipfrid_load(context, furi_string_get_cstr(user_file_path));
2022-09-01 15:03:18 +00:00
}
2022-10-05 18:27:13 +00:00
furi_string_free(user_file_path);
2022-09-01 15:03:18 +00:00
return res;
}