Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Stock Functions to Achieve Directory and Flipper Format functions #10

Merged
merged 2 commits into from
Aug 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 11 additions & 13 deletions examples/Tag_1.t5577
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
Filetype: Flipper T5577 Raw File
Version: 1.0
Modulation: ASK/MC
Version: 2
Modulation: FSK2a
RF Clock: 64
Number of User Blocks: 8

Raw Data:
Block 0: 00148100
Block 1: 1A2B3C4D
Block 2: 5678ABCD
Block 3: 12345678
Block 4: 87654321
Block 5: 12341234
Block 6: ABCDEFAB
Block 7: 78901234

Raw Data:
Block 0: 00 14 71 00
Block 1: 11 12 13 14
Block 2: 22 33 44 55
Block 3: 1A 2B 3C 4D
Block 4: 56 78 AB CD
Block 5: 12 34 12 34
Block 6: AB CD EF AB
Block 7: 12 34 56 78
149 changes: 48 additions & 101 deletions t5577_writer.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <t5577_config.h>
#include <t5577_writer.h>
#include <dolphin/dolphin.h>
#include <flipper_format.h>

#define TAG "T5577 Writer"
#define MAX_REPEAT_WRITING_FRAMES 10
Expand Down Expand Up @@ -259,24 +260,6 @@ static void t5577_writer_edit_block_slc_change(VariableItem* item) {
furi_string_free(buffer);
}

void ensure_dir_exists(Storage* storage) {
// If apps_data directory doesn't exist, create it.
if(!storage_dir_exists(storage, T5577_WRITER_APPS_DATA_FOLDER)) {
FURI_LOG_I(TAG, "Creating directory: %s", T5577_WRITER_APPS_DATA_FOLDER);
storage_simply_mkdir(storage, T5577_WRITER_APPS_DATA_FOLDER);
} else {
FURI_LOG_I(TAG, "Directory exists: %s", T5577_WRITER_APPS_DATA_FOLDER);
}

// If t5577_writer directory doesn't exist, create it.
if(!storage_dir_exists(storage, T5577_WRITER_FILE_FOLDER)) {
FURI_LOG_I(TAG, "Creating directory: %s", T5577_WRITER_FILE_FOLDER);
storage_simply_mkdir(storage, T5577_WRITER_FILE_FOLDER);
} else {
FURI_LOG_I(TAG, "Directory exists: %s", T5577_WRITER_FILE_FOLDER);
}
}

static const char* tag_name_entry_text = "Enter name";
static const char* tag_name_default_value = "Tag_1";
static void t5577_writer_file_saver(void* context) {
Expand All @@ -292,40 +275,45 @@ static void t5577_writer_file_saver(void* context) {
T5577WriterModel * model,
{ furi_string_set(model->tag_name_str, app->temp_buffer); },
redraw);
FuriString* buffer = furi_string_alloc();
FuriString* file_path = furi_string_alloc();
furi_string_printf(
file_path,
"%s/%s%s",
T5577_WRITER_FILE_FOLDER,
STORAGE_APP_DATA_PATH_PREFIX,
furi_string_get_cstr(model->tag_name_str),
T5577_WRITER_FILE_EXTENSION);

Storage* storage = furi_record_open(RECORD_STORAGE);
ensure_dir_exists(storage);
File* data_file = storage_file_alloc(storage);
if(storage_file_open(
data_file, furi_string_get_cstr(file_path), FSAM_WRITE, FSOM_OPEN_ALWAYS)) {
furi_string_printf(buffer, "Filetype: Flipper T5577 Raw File\n");
storage_file_write(data_file, furi_string_get_cstr(buffer), furi_string_size(buffer));
furi_string_printf(buffer, "Version: 1.0\n");
storage_file_write(data_file, furi_string_get_cstr(buffer), furi_string_size(buffer));
furi_string_printf(buffer, "Modulation: %s\n", model->modulation.modulation_name);
storage_file_write(data_file, furi_string_get_cstr(buffer), furi_string_size(buffer));
furi_string_printf(buffer, "RF Clock: %u\n", model->rf_clock.rf_clock_num);
storage_file_write(data_file, furi_string_get_cstr(buffer), furi_string_size(buffer));
furi_string_printf(buffer, "Number of User Blocks: %u\n", model->user_block_num);
storage_file_write(data_file, furi_string_get_cstr(buffer), furi_string_size(buffer));
furi_string_printf(buffer, "\nRaw Data:\n");
storage_simply_mkdir(storage, STORAGE_APP_DATA_PATH_PREFIX);
FuriString* buffer = furi_string_alloc();
FlipperFormat* format = flipper_format_file_alloc(storage);
do {
const uint32_t version = 2;
const uint32_t clock_buffer = (uint32_t)model->rf_clock.rf_clock_num;
const uint32_t block_num_buffer = (uint32_t)model->user_block_num;
if(!flipper_format_file_open_always(format, furi_string_get_cstr(file_path))) break;
if(!flipper_format_write_header_cstr(format, "Flipper T5577 Raw File", version)) break;
if(!flipper_format_write_string_cstr(
format, "Modulation", model->modulation.modulation_name))
break;
if(!flipper_format_write_uint32(format, "RF Clock", &clock_buffer, 1)) break;
if(!flipper_format_write_uint32(format, "Number of User Blocks", &block_num_buffer, 1))
break;
if(!flipper_format_write_string_cstr(format, "Raw Data", "")) break; // raw data begins
for(int i = 0; i < LFRFID_T5577_BLOCK_COUNT; i++) {
furi_string_cat_printf(buffer, "Block %u: %08lX\n", i, model->content[i]);
furi_string_printf(buffer, "Block %u", i);
uint8_t byte_array_buffer[app->bytes_count];
uint32_to_byte_buffer(model->content[i], byte_array_buffer);
if(!flipper_format_write_hex(
format, furi_string_get_cstr(buffer), byte_array_buffer, app->bytes_count))
break;
}
furi_string_push_back(buffer, '\n');
storage_file_write(data_file, furi_string_get_cstr(buffer), furi_string_size(buffer));
storage_file_close(data_file);
view_dispatcher_switch_to_view(
app->view_dispatcher, T5577WriterViewSubmenu); // maybe add a pop up later
}
// signal that the file was written successfully
} while(0);
flipper_format_free(format);

view_dispatcher_switch_to_view(
app->view_dispatcher, T5577WriterViewSubmenu); // maybe add a pop up later
}

void t5577_writer_update_config_from_load(void* context) {
Expand All @@ -350,21 +338,6 @@ void t5577_writer_update_config_from_load(void* context) {
}

static const char* edit_block_data_config_label = "Block Data";
void uint32_to_byte_buffer(uint32_t block_data, uint8_t byte_buffer[4]) {
byte_buffer[0] = (block_data >> 24) & 0xFF;
byte_buffer[1] = (block_data >> 16) & 0xFF;
byte_buffer[2] = (block_data >> 8) & 0xFF;
byte_buffer[3] = block_data & 0xFF;
}

uint32_t byte_buffer_to_uint32(uint8_t byte_buffer[4]) {
uint32_t block_data = 0;
block_data |= ((uint32_t)byte_buffer[0] << 24);
block_data |= ((uint32_t)byte_buffer[1] << 16);
block_data |= ((uint32_t)byte_buffer[2] << 8);
block_data |= ((uint32_t)byte_buffer[3]);
return block_data;
}

static void t5577_writer_content_byte_input_confirmed(void* context) {
T5577WriterApp* app = (T5577WriterApp*)context;
Expand Down Expand Up @@ -469,55 +442,29 @@ void t5577_writer_view_load_callback(void* context) {
T5577WriterModel* model = view_get_model(app->view_write);
DialogsFileBrowserOptions browser_options;
Storage* storage = furi_record_open(RECORD_STORAGE);
ensure_dir_exists(storage);
File* data_file = storage_file_alloc(storage);
storage_simply_mkdir(storage, STORAGE_APP_DATA_PATH_PREFIX);
dialog_file_browser_set_basic_options(&browser_options, T5577_WRITER_FILE_EXTENSION, &I_icon);
browser_options.base_path = T5577_WRITER_FILE_FOLDER;
browser_options.base_path = STORAGE_APP_DATA_PATH_PREFIX;
furi_string_set(app->file_path, browser_options.base_path);
FuriString* buffer = furi_string_alloc();
if(dialog_file_browser_show(app->dialogs, app->file_path, app->file_path, &browser_options)) {
if(storage_file_open(
data_file, furi_string_get_cstr(app->file_path), FSAM_READ, FSOM_OPEN_EXISTING)) {
while(!storage_file_eof(data_file)) { // fill buffer with every line because ch == '\n'
char ch;
furi_string_reset(buffer);
while(storage_file_read(data_file, &ch, 1) && !storage_file_eof(data_file)) {
furi_string_push_back(buffer, ch);
if(ch == '\n') {
break;
}
}
if(furi_string_start_with(buffer, "Block ")) {
uint32_t row_data_buffer = 0;
char row_data_char_buffer[] = "00000000";
uint32_t row_num_buffer = 0;
char row_num_char_buffer[] = "0";
int length = furi_string_size(buffer);
char ch;
int i = 0;
while(i < length) {
if(furi_string_get_char(buffer, i) == ':') {
row_num_char_buffer[0] = furi_string_get_char(buffer, i - 1);
//the number before ":" is block num
i += 2; // skip a space
for(size_t j = 0; j < sizeof(row_data_char_buffer); j++) {
ch = furi_string_get_char(buffer, i);
row_data_char_buffer[j] = ch;
i++;
}
break;
}
i++;
}
sscanf(row_num_char_buffer, "%lx", &row_num_buffer);
sscanf(row_data_char_buffer, "%lx", &row_data_buffer);
model->content[row_num_buffer] = row_data_buffer;
}
FlipperFormat* format = flipper_format_file_alloc(storage);
do {
if(!flipper_format_file_open_existing(format, furi_string_get_cstr(app->file_path)))
break;
uint8_t byte_array_buffer[app->bytes_count];
for(int i = 0; i < LFRFID_T5577_BLOCK_COUNT; i++) {
furi_string_printf(buffer, "Block %u", i);
if(!flipper_format_read_hex(
format, furi_string_get_cstr(buffer), byte_array_buffer, app->bytes_count))
break;
model->content[i] = byte_buffer_to_uint32(
byte_array_buffer); // we only extract the raw data. configs are then updated from block 0
}
storage_file_close(data_file);
t5577_writer_update_config_from_load(app);
}
storage_file_free(data_file);
// signal that the file was read successfully
} while(0);
flipper_format_free(format);
t5577_writer_update_config_from_load(app);
furi_record_close(RECORD_STORAGE);
}
view_dispatcher_switch_to_view(app->view_dispatcher, T5577WriterViewSubmenu);
Expand Down
20 changes: 17 additions & 3 deletions t5577_writer.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
#define T5577_WRITER_APPS_DATA_FOLDER EXT_PATH("apps_data")
#define T5577_WRITER_FILE_FOLDER EXT_PATH("apps_data/t5577_writer")
#define T5577_WRITER_FILE_EXTENSION ".t5577"
#define T5577_WRITER_FILE_EXTENSION ".t5577"

void uint32_to_byte_buffer(uint32_t block_data, uint8_t byte_buffer[4]) {
byte_buffer[0] = (block_data >> 24) & 0xFF;
byte_buffer[1] = (block_data >> 16) & 0xFF;
byte_buffer[2] = (block_data >> 8) & 0xFF;
byte_buffer[3] = block_data & 0xFF;
}

uint32_t byte_buffer_to_uint32(uint8_t byte_buffer[4]) {
uint32_t block_data = 0;
block_data |= ((uint32_t)byte_buffer[0] << 24);
block_data |= ((uint32_t)byte_buffer[1] << 16);
block_data |= ((uint32_t)byte_buffer[2] << 8);
block_data |= ((uint32_t)byte_buffer[3]);
return block_data;
}
Loading