Skip to content

Commit

Permalink
code cleanup, log cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
rdefeo committed Mar 17, 2024
1 parent a938b09 commit 09230aa
Show file tree
Hide file tree
Showing 17 changed files with 116 additions and 150 deletions.
2 changes: 1 addition & 1 deletion actions/action.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include "action_i.h"

void action_tx(void* context, Item* item, FuriString* error) {
FURI_LOG_I(TAG, "action_run: %s : %s", furi_string_get_cstr(item->name), item->ext);
// FURI_LOG_I(TAG, "action_run: %s : %s", furi_string_get_cstr(item->name), item->ext);

if(!strcmp(item->ext, ".sub")) {
action_subghz_tx(context, item->path, error);
Expand Down
8 changes: 4 additions & 4 deletions actions/action_i.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

#define ACTION_SET_ERROR(_msg_fmt, ...) furi_string_printf(error, _msg_fmt, ##__VA_ARGS__)

void action_subghz_tx(void* context, FuriString* action_path, FuriString* error);
void action_rfid_tx(void* context, FuriString* action_path, FuriString* error);
void action_ir_tx(void* context, FuriString* action_path, FuriString* error);
void action_qpl_tx(void* context, FuriString* action_path, FuriString* error);
void action_subghz_tx(void* context, const FuriString* action_path, FuriString* error);
void action_rfid_tx(void* context, const FuriString* action_path, FuriString* error);
void action_ir_tx(void* context, const FuriString* action_path, FuriString* error);
void action_qpl_tx(void* context, const FuriString* action_path, FuriString* error);
2 changes: 1 addition & 1 deletion actions/action_ir.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ InfraredSignal* infrared_signal_alloc() {
return signal;
}

void action_ir_tx(void* context, FuriString* action_path, FuriString* error) {
void action_ir_tx(void* context, const FuriString* action_path, FuriString* error) {
UNUSED(action_path);
UNUSED(error);
UNUSED(context);
Expand Down
26 changes: 17 additions & 9 deletions actions/action_qpl.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,36 @@
#include "quac.h"

/** Open the Playlist file and then transmit each action
*
* Each line of the playlist file is one of:
* <file_path>
* Full SD card path, or relative path to action to be transmitted. Must be
* one of the supported filetypes (.sub, .rfid, [.ir coming soon])
* pause <ms> - NOT IMPLEMENTED
*
* If an .rfid file has a space followed by a number, that will be the
* duration for that RFID transmission. All other .rfid files will use
* the value specified in the Settings
*
* pause <ms>
* Pauses the playback for 'ms' milliseconds.
*
* Blank lines, and comments (start with '#') are ignored. Whitespace is trimmed.
*
* Not yet Implemented:
* - For RFID files, if they have a space followed by a number after their name,
* that number will be the duration of that RFID tx
*/
void action_qpl_tx(void* context, FuriString* action_path, FuriString* error) {
void action_qpl_tx(void* context, const FuriString* action_path, FuriString* error) {
App* app = context;

// Save the current RFID Duration, in case it is changed during playback
uint32_t orig_rfid_duration = app->settings.rfid_duration;

FuriString* buffer;
buffer = furi_string_alloc();

Stream* file = file_stream_alloc(app->storage);
if(file_stream_open(file, furi_string_get_cstr(action_path), FSAM_READ, FSOM_OPEN_EXISTING)) {
while(stream_read_line(file, buffer)) {
furi_string_trim(buffer); // remove '\n\r' line endings, cleanup spaces
FURI_LOG_I(TAG, "line: %s", furi_string_get_cstr(buffer));
// FURI_LOG_I(TAG, "line: %s", furi_string_get_cstr(buffer));

// Skip blank lines
if(furi_string_size(buffer) == 0) {
Expand Down Expand Up @@ -100,7 +106,7 @@ void action_qpl_tx(void* context, FuriString* action_path, FuriString* error) {
// FURI_LOG_I(TAG, "RFID file with duration");
if(sscanf(furi_string_get_cstr(buffer), "%lu", &rfid_duration) == 1) {
FURI_LOG_I(TAG, "RFID duration = %lu", rfid_duration);
// TODO: Need to get the duration to the action_rfid_tx command...
app->settings.rfid_duration = rfid_duration;
}
}

Expand Down Expand Up @@ -128,10 +134,12 @@ void action_qpl_tx(void* context, FuriString* action_path, FuriString* error) {
path_extract_extension(buffer, ext, MAX_EXT_LEN);
if(!strcmp(ext, ".sub")) {
action_subghz_tx(context, buffer, error);
} else if(!strcmp(ext, ".ir")) {
action_ir_tx(context, buffer, error);
} else if(!strcmp(ext, ".rfid")) {
action_rfid_tx(context, buffer, error);
// Reset our default duration back - in case it was changed during playback
app->settings.rfid_duration = orig_rfid_duration;
} else if(!strcmp(ext, ".ir")) {
action_ir_tx(context, buffer, error);
} else if(!strcmp(ext, ".qpl")) {
ACTION_SET_ERROR("Playlist: Can't call playlist from playlist");
} else {
Expand Down
19 changes: 5 additions & 14 deletions actions/action_rfid.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,17 @@
#include "quac.h"

// lifted from flipperzero-firmware/applications/main/lfrfid/lfrfid_cli.c
void action_rfid_tx(void* context, FuriString* action_path, FuriString* error) {
void action_rfid_tx(void* context, const FuriString* action_path, FuriString* error) {
UNUSED(error);

App* app = context;
FuriString* file_name = action_path;
const FuriString* file_name = action_path;

FlipperFormat* fff_data_file = flipper_format_file_alloc(app->storage);
FuriString* temp_str;
temp_str = furi_string_alloc();
uint32_t temp_data32;

FuriString* protocol_name;
FuriString* data_text;
protocol_name = furi_string_alloc();
data_text = furi_string_alloc();

ProtocolDict* dict = protocol_dict_alloc(lfrfid_protocols, LFRFIDProtocolMax);
ProtocolId protocol;
size_t data_size = protocol_dict_get_max_data_size(dict);
Expand All @@ -54,13 +49,13 @@ void action_rfid_tx(void* context, FuriString* action_path, FuriString* error) {
}

// read and check the protocol field
if(!flipper_format_read_string(fff_data_file, "Key type", protocol_name)) {
if(!flipper_format_read_string(fff_data_file, "Key type", temp_str)) {
ACTION_SET_ERROR("RFID: Error reading protocol");
break;
}
protocol = protocol_dict_get_protocol_by_name(dict, furi_string_get_cstr(protocol_name));
protocol = protocol_dict_get_protocol_by_name(dict, furi_string_get_cstr(temp_str));
if(protocol == PROTOCOL_NO) {
ACTION_SET_ERROR("RFID: Unknown protocol: %s", furi_string_get_cstr(protocol_name));
ACTION_SET_ERROR("RFID: Unknown protocol: %s", furi_string_get_cstr(temp_str));
break;
}

Expand Down Expand Up @@ -110,11 +105,7 @@ void action_rfid_tx(void* context, FuriString* action_path, FuriString* error) {
}

furi_string_free(temp_str);
furi_string_free(protocol_name);
furi_string_free(data_text);
free(data);

protocol_dict_free(dict);

flipper_format_free(fff_data_file);
}
14 changes: 7 additions & 7 deletions actions/action_subghz.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ static FuriHalSubGhzPreset action_subghz_get_preset_name(const char* preset_name
}

// Lifted from flipperzero-firmware/applications/main/subghz/subghz_cli.c
void action_subghz_tx(void* context, FuriString* action_path, FuriString* error) {
void action_subghz_tx(void* context, const FuriString* action_path, FuriString* error) {
App* app = context;
FuriString* file_name = action_path;
const char* file_name = furi_string_get_cstr(action_path);
uint32_t repeat = 1; //
// uint32_t device_ind = 0; // 0 - CC1101_INT, 1 - CC1101_EXT

Expand Down Expand Up @@ -76,9 +76,9 @@ void action_subghz_tx(void* context, FuriString* action_path, FuriString* error)
// device_ind = 0;
}

if(!flipper_format_file_open_existing(fff_data_file, furi_string_get_cstr(file_name))) {
FURI_LOG_E(TAG, "Error opening %s", furi_string_get_cstr(file_name));
ACTION_SET_ERROR("SUBGHZ: Error opening %s", furi_string_get_cstr(file_name));
if(!flipper_format_file_open_existing(fff_data_file, file_name)) {
FURI_LOG_E(TAG, "Error opening %s", file_name);
ACTION_SET_ERROR("SUBGHZ: Error opening %s", file_name);
break;
}

Expand Down Expand Up @@ -169,7 +169,7 @@ void action_subghz_tx(void* context, FuriString* action_path, FuriString* error)
if(!strcmp(furi_string_get_cstr(temp_str), "RAW")) {
FURI_LOG_I(TAG, "Protocol = RAW");
subghz_protocol_raw_gen_fff_data(
fff_data_raw, furi_string_get_cstr(file_name), subghz_devices_get_name(device));
fff_data_raw, file_name, subghz_devices_get_name(device));
transmitter =
subghz_transmitter_alloc_init(environment, furi_string_get_cstr(temp_str));
if(transmitter == NULL) {
Expand Down Expand Up @@ -219,7 +219,7 @@ void action_subghz_tx(void* context, FuriString* action_path, FuriString* error)
FURI_LOG_I(
TAG,
"Listening at %s. Frequency=%lu, Protocol=%s",
furi_string_get_cstr(file_name),
file_name,
frequency,
furi_string_get_cstr(temp_str));
do {
Expand Down
16 changes: 0 additions & 16 deletions flipper.h

This file was deleted.

55 changes: 29 additions & 26 deletions item.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,26 @@

ARRAY_DEF(FileArray, FuriString*, FURI_STRING_OPLIST);

ItemsView* item_get_items_view_from_path(void* context, FuriString* input_path) {
ItemsView* item_get_items_view_from_path(void* context, const FuriString* input_path) {
App* app = context;

// Handle the app start condition
FuriString* in_path;
if(input_path == NULL) {
input_path = furi_string_alloc_set_str(QUAC_DATA_PATH);
in_path = furi_string_alloc_set_str(QUAC_DATA_PATH);
} else {
in_path = furi_string_alloc_set(input_path);
}
const char* cpath = furi_string_get_cstr(input_path);
const char* cpath = furi_string_get_cstr(in_path);

FURI_LOG_I(TAG, "Getting items from: %s", cpath);
FURI_LOG_I(TAG, "Reading items from path: %s", cpath);
ItemsView* iview = malloc(sizeof(ItemsView));
iview->path = furi_string_alloc_set(input_path);
iview->path = furi_string_alloc_set(in_path);

iview->name = furi_string_alloc();
if(app->depth == 0) {
FURI_LOG_I(TAG, "Depth is ZERO!");
furi_string_set_str(iview->name, QUAC_NAME);
} else {
FURI_LOG_I(TAG, "Depth is %d", app->depth);
path_extract_basename(cpath, iview->name);
item_prettify_name(iview->name);
}
Expand All @@ -43,17 +44,17 @@ ItemsView* item_get_items_view_from_path(void* context, FuriString* input_path)
FuriString* filename_tmp;
filename_tmp = furi_string_alloc();

// FURI_LOG_I(TAG, "About to walk the dir");
// Walk the directory and store all file names in sorted order
if(dir_walk_open(dir_walk, cpath)) {
while(dir_walk_read(dir_walk, path, NULL) == DirWalkOK) {
FURI_LOG_I(TAG, "> dir_walk: %s", furi_string_get_cstr(path));
// FURI_LOG_I(TAG, "> dir_walk: %s", furi_string_get_cstr(path));
const char* cpath = furi_string_get_cstr(path);

// Skip "hidden" files
path_extract_filename(path, filename_tmp, false);
char first_char = furi_string_get_char(filename_tmp, 0);
if(first_char == '.') {
FURI_LOG_I(TAG, ">> skipping hidden file: %s", furi_string_get_cstr(filename_tmp));
// FURI_LOG_I(TAG, ">> skipping hidden file: %s", furi_string_get_cstr(filename_tmp));
continue;
}

Expand All @@ -78,6 +79,7 @@ ItemsView* item_get_items_view_from_path(void* context, FuriString* input_path)
furi_string_free(filename_tmp);
furi_string_free(path);

// Generate our Item list
FileArray_it_t iter;
ItemArray_init(iview->items);
for(FileArray_it(iter, flist); !FileArray_end_p(iter); FileArray_next(iter)) {
Expand All @@ -89,20 +91,7 @@ ItemsView* item_get_items_view_from_path(void* context, FuriString* input_path)
// Action files have extensions, so item->ext starts with '.'
item->ext[0] = 0;
path_extract_extension(path, item->ext, MAX_EXT_LEN);
// FURI_LOG_I(TAG, ". EXT = %s", item->ext);
if(item->ext[0] == '.') {
// TODO: hack alert - make a helper fn here, or something
if(item->ext[1] == 's')
item->type = Item_SubGhz;
else if(item->ext[1] == 'r')
item->type = Item_RFID;
else if(item->ext[1] == 'q')
item->type = Item_Playlist;
else if(item->ext[1] == 'i')
item->type = Item_IR;
} else {
item->type = Item_Group;
}
item->type = item_get_item_type_from_extension(item->ext);

item->name = furi_string_alloc();
path_extract_filename_no_ext(found_path, item->name);
Expand All @@ -114,14 +103,14 @@ ItemsView* item_get_items_view_from_path(void* context, FuriString* input_path)
// FURI_LOG_I(TAG, "Path: %s", furi_string_get_cstr(item->path));
}

furi_string_free(in_path);
FileArray_clear(flist);
dir_walk_free(dir_walk);

return iview;
}

void item_items_view_free(ItemsView* items_view) {
FURI_LOG_I(TAG, "item_items_view_free - begin");
furi_string_free(items_view->name);
furi_string_free(items_view->path);
ItemArray_it_t iter;
Expand All @@ -131,7 +120,6 @@ void item_items_view_free(ItemsView* items_view) {
}
ItemArray_clear(items_view->items);
free(items_view);
FURI_LOG_I(TAG, "item_items_view_free - end");
}

void item_prettify_name(FuriString* name) {
Expand All @@ -148,4 +136,19 @@ void item_prettify_name(FuriString* name) {
}
furi_string_replace_str(name, "_", " ", 0);
// FURI_LOG_I(TAG, "... %s", furi_string_get_cstr(name));
}

ItemType item_get_item_type_from_extension(const char* ext) {
ItemType type = Item_Group;

if(!strcmp(ext, ".sub")) {
type = Item_SubGhz;
} else if(!strcmp(ext, ".rfid")) {
type = Item_RFID;
} else if(!strcmp(ext, ".ir")) {
type = Item_IR;
} else if(!strcmp(ext, ".qpl")) {
type = Item_Playlist;
}
return type;
}
12 changes: 9 additions & 3 deletions item.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ typedef enum {
Item_IR,
Item_Playlist,
Item_Group,
Item_Settings
Item_Settings,
Item_count
} ItemType;

typedef struct Item {
Expand All @@ -41,7 +42,7 @@ typedef struct ItemsView {
* @param path FuriString*
* @return ItemsView*
*/
ItemsView* item_get_items_view_from_path(void* context, FuriString* path);
ItemsView* item_get_items_view_from_path(void* context, const FuriString* path);

/** Free ItemsView
* @param items_view
Expand All @@ -52,4 +53,9 @@ void item_items_view_free(ItemsView* items_view);
* as well as replace all '_' with ' '.
* @param name FuriString*
*/
void item_prettify_name(FuriString* name);
void item_prettify_name(FuriString* name);

/** Return the ItemType enum for the given extension
* @param ext File extension
*/
ItemType item_get_item_type_from_extension(const char* ext);
Loading

0 comments on commit 09230aa

Please sign in to comment.