Skip to content

Commit

Permalink
Merge remote-tracking branch 'OFW/dev' into dev [ci skip]
Browse files Browse the repository at this point in the history
  • Loading branch information
xMasterX committed Sep 6, 2024
2 parents ad1c904 + e0654fe commit 4098ee9
Show file tree
Hide file tree
Showing 18 changed files with 538 additions and 258 deletions.
48 changes: 30 additions & 18 deletions applications/main/infrared/infrared_app.c
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ static void infrared_free(InfraredApp* infrared) {
free(infrared);
}

bool infrared_add_remote_with_button(
InfraredErrorCode infrared_add_remote_with_button(
const InfraredApp* infrared,
const char* button_name,
const InfraredSignal* signal) {
Expand All @@ -301,21 +301,23 @@ bool infrared_add_remote_with_button(
furi_string_cat_printf(
new_path, "/%s%s", furi_string_get_cstr(new_name), INFRARED_APP_EXTENSION);

bool success = false;
InfraredErrorCode error = InfraredErrorCodeNone;

do {
if(!infrared_remote_create(remote, furi_string_get_cstr(new_path))) break;
if(!infrared_remote_append_signal(remote, signal, button_name)) break;
success = true;
error = infrared_remote_create(remote, furi_string_get_cstr(new_path));
if(INFRARED_ERROR_PRESENT(error)) break;

error = infrared_remote_append_signal(remote, signal, button_name);
} while(false);

furi_string_free(new_name);
furi_string_free(new_path);

return success;
return error;
}

bool infrared_rename_current_remote(const InfraredApp* infrared, const char* new_name) {
InfraredErrorCode
infrared_rename_current_remote(const InfraredApp* infrared, const char* new_name) {
InfraredRemote* remote = infrared->remote;
const char* old_path = infrared_remote_get_path(remote);

Expand All @@ -335,12 +337,13 @@ bool infrared_rename_current_remote(const InfraredApp* infrared, const char* new
path_append(new_path_fstr, furi_string_get_cstr(new_name_fstr));
furi_string_cat(new_path_fstr, INFRARED_APP_EXTENSION);

const bool success = infrared_remote_rename(remote, furi_string_get_cstr(new_path_fstr));
const InfraredErrorCode error =
infrared_remote_rename(remote, furi_string_get_cstr(new_path_fstr));

furi_string_free(new_name_fstr);
furi_string_free(new_path_fstr);

return success;
return error;
}

void infrared_tx_start(InfraredApp* infrared) {
Expand Down Expand Up @@ -373,15 +376,16 @@ void infrared_tx_start(InfraredApp* infrared) {
infrared->app_state.is_transmitting = true;
}

bool infrared_tx_start_button_index(InfraredApp* infrared, size_t button_index) {
InfraredErrorCode infrared_tx_start_button_index(InfraredApp* infrared, size_t button_index) {
furi_assert(button_index < infrared_remote_get_signal_count(infrared->remote));

bool result =
InfraredErrorCode error =
infrared_remote_load_signal(infrared->remote, infrared->current_signal, button_index);
if(result) {

if(!INFRARED_ERROR_PRESENT(error)) {
infrared_tx_start(infrared);
}
return result;
return error;
}

void infrared_tx_stop(InfraredApp* infrared) {
Expand All @@ -404,7 +408,7 @@ void infrared_blocking_task_start(InfraredApp* infrared, FuriThreadCallback call
furi_thread_start(infrared->task_thread);
}

bool infrared_blocking_task_finalize(InfraredApp* infrared) {
InfraredErrorCode infrared_blocking_task_finalize(InfraredApp* infrared) {
furi_thread_join(infrared->task_thread);
return furi_thread_get_return_code(infrared->task_thread);
}
Expand Down Expand Up @@ -554,10 +558,18 @@ int32_t infrared_app(void* p) {
is_rpc_mode = true;
} else {
const char* file_path = (const char*)p;
is_remote_loaded = infrared_remote_load(infrared->remote, file_path);

if(!is_remote_loaded) {
infrared_show_error_message(infrared, "Failed to load\n\"%s\"", file_path);
InfraredErrorCode error = infrared_remote_load(infrared->remote, file_path);

if(!INFRARED_ERROR_PRESENT(error)) {
is_remote_loaded = true;
} else {
is_remote_loaded = false;
bool wrong_file_type = INFRARED_ERROR_CHECK(error, InfraredErrorCodeWrongFileType);
const char* format = wrong_file_type ?
"Library file\n\"%s\" can't be openned as a remote" :
"Failed to load\n\"%s\"";

infrared_show_error_message(infrared, format, file_path);
return -1;
}

Expand Down
17 changes: 9 additions & 8 deletions applications/main/infrared/infrared_app_i.h
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,9 @@ typedef enum {
* @param[in] infrared pointer to the application instance.
* @param[in] name pointer to a zero-terminated string containing the signal name.
* @param[in] signal pointer to the signal to be added.
* @return true if the remote was successfully created, false otherwise.
* @return InfraredErrorCodeNone if the remote was successfully created, otherwise error code.
*/
bool infrared_add_remote_with_button(
InfraredErrorCode infrared_add_remote_with_button(
const InfraredApp* infrared,
const char* name,
const InfraredSignal* signal);
Expand All @@ -186,9 +186,10 @@ bool infrared_add_remote_with_button(
*
* @param[in] infrared pointer to the application instance.
* @param[in] new_name pointer to a zero-terminated string containing the new remote name.
* @return true if the remote was successfully renamed, false otherwise.
* @return InfraredErrorCodeNone if the remote was successfully renamed, otherwise error code.
*/
bool infrared_rename_current_remote(const InfraredApp* infrared, const char* new_name);
InfraredErrorCode
infrared_rename_current_remote(const InfraredApp* infrared, const char* new_name);

/**
* @brief Begin transmission of the currently loaded signal.
Expand All @@ -206,9 +207,9 @@ void infrared_tx_start(InfraredApp* infrared);
*
* @param[in,out] infrared pointer to the application instance.
* @param[in] button_index index of the signal to be loaded.
* @returns true if the signal could be loaded, false otherwise.
* @returns InfraredErrorCodeNone if the signal could be loaded, otherwise error code.
*/
bool infrared_tx_start_button_index(InfraredApp* infrared, size_t button_index);
InfraredErrorCode infrared_tx_start_button_index(InfraredApp* infrared, size_t button_index);

/**
* @brief Stop transmission of the currently loaded signal.
Expand Down Expand Up @@ -236,9 +237,9 @@ void infrared_blocking_task_start(InfraredApp* infrared, FuriThreadCallback call
* (e.g. to display the results), the caller code MUST set it explicitly.
*
* @param[in,out] infrared pointer to the application instance.
* @return true if the blocking task finished successfully, false otherwise.
* @return InfraredErrorCodeNone if the blocking task finished successfully, otherwise error code.
*/
bool infrared_blocking_task_finalize(InfraredApp* infrared);
InfraredErrorCode infrared_blocking_task_finalize(InfraredApp* infrared);

/**
* @brief Set the internal text store with formatted text.
Expand Down
27 changes: 15 additions & 12 deletions applications/main/infrared/infrared_brute_force.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,23 +50,26 @@ void infrared_brute_force_set_db_filename(InfraredBruteForce* brute_force, const
brute_force->db_filename = db_filename;
}

bool infrared_brute_force_calculate_messages(InfraredBruteForce* brute_force) {
InfraredErrorCode infrared_brute_force_calculate_messages(InfraredBruteForce* brute_force) {
furi_assert(!brute_force->is_started);
furi_assert(brute_force->db_filename);
bool success = false;
InfraredErrorCode error = InfraredErrorCodeNone;

Storage* storage = furi_record_open(RECORD_STORAGE);
FlipperFormat* ff = flipper_format_buffered_file_alloc(storage);
FuriString* signal_name = furi_string_alloc();
InfraredSignal* signal = infrared_signal_alloc();

do {
if(!flipper_format_buffered_file_open_existing(ff, brute_force->db_filename)) break;
if(!flipper_format_buffered_file_open_existing(ff, brute_force->db_filename)) {
error = InfraredErrorCodeFileOperationFailed;
break;
}

bool signals_valid = false;
while(infrared_signal_read_name(ff, signal_name)) {
signals_valid = infrared_signal_read_body(signal, ff) &&
infrared_signal_is_valid(signal);
while(infrared_signal_read_name(ff, signal_name) == InfraredErrorCodeNone) {
error = infrared_signal_read_body(signal, ff);
signals_valid = (!INFRARED_ERROR_PRESENT(error)) && infrared_signal_is_valid(signal);
if(!signals_valid) break;

InfraredBruteForceRecord* record =
Expand All @@ -75,17 +78,15 @@ bool infrared_brute_force_calculate_messages(InfraredBruteForce* brute_force) {
++(record->count);
}
}

if(!signals_valid) break;
success = true;
} while(false);

infrared_signal_free(signal);
furi_string_free(signal_name);

flipper_format_free(ff);
furi_record_close(RECORD_STORAGE);
return success;
return error;
}

bool infrared_brute_force_start(
Expand Down Expand Up @@ -139,10 +140,12 @@ void infrared_brute_force_stop(InfraredBruteForce* brute_force) {

bool infrared_brute_force_send_next(InfraredBruteForce* brute_force) {
furi_assert(brute_force->is_started);

const bool success = infrared_signal_search_by_name_and_read(
brute_force->current_signal,
brute_force->ff,
furi_string_get_cstr(brute_force->current_record_name));
brute_force->current_signal,
brute_force->ff,
furi_string_get_cstr(brute_force->current_record_name)) ==
InfraredErrorCodeNone;
if(success) {
infrared_signal_transmit(brute_force->current_signal);
}
Expand Down
5 changes: 3 additions & 2 deletions applications/main/infrared/infrared_brute_force.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include <stdint.h>
#include <stdbool.h>
#include "infrared_error_code.h"

/**
* @brief InfraredBruteForce opaque type declaration.
Expand Down Expand Up @@ -45,9 +46,9 @@ void infrared_brute_force_set_db_filename(InfraredBruteForce* brute_force, const
* a infrared_brute_force_set_db_filename() call.
*
* @param[in,out] brute_force pointer to the instance to be updated.
* @returns true on success, false otherwise.
* @returns InfraredErrorCodeNone on success, otherwise error code.
*/
bool infrared_brute_force_calculate_messages(InfraredBruteForce* brute_force);
InfraredErrorCode infrared_brute_force_calculate_messages(InfraredBruteForce* brute_force);

/**
* @brief Start transmitting signals from a category stored in an InfraredBruteForce's instance dictionary.
Expand Down
16 changes: 11 additions & 5 deletions applications/main/infrared/infrared_cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -232,9 +232,15 @@ static void infrared_cli_start_ir_tx(Cli* cli, FuriString* args) {

static bool
infrared_cli_save_signal(InfraredSignal* signal, FlipperFormat* file, const char* name) {
bool ret = infrared_signal_save(signal, file, name);
if(!ret) {
printf("Failed to save signal: \"%s\"\r\n", name);
bool ret = true;
InfraredErrorCode error = infrared_signal_save(signal, file, name);
if(INFRARED_ERROR_PRESENT(error)) {
printf(
"Failed to save signal: \"%s\" code: 0x%X index: 0x%02X\r\n",
name,
INFRARED_ERROR_GET_CODE(error),
INFRARED_ERROR_GET_INDEX(error));
ret = false;
}
return ret;
}
Expand Down Expand Up @@ -296,7 +302,7 @@ static bool infrared_cli_decode_file(FlipperFormat* input_file, FlipperFormat* o
FuriString* tmp;
tmp = furi_string_alloc();

while(infrared_signal_read(signal, input_file, tmp)) {
while(infrared_signal_read(signal, input_file, tmp) == InfraredErrorCodeNone) {
ret = false;
if(!infrared_signal_is_valid(signal)) {
printf("Invalid signal\r\n");
Expand Down Expand Up @@ -464,7 +470,7 @@ static void
printf("Missing signal name.\r\n");
break;
}
if(!infrared_brute_force_calculate_messages(brute_force)) {
if(infrared_brute_force_calculate_messages(brute_force) != InfraredErrorCodeNone) {
printf("Invalid remote name.\r\n");
break;
}
Expand Down
45 changes: 45 additions & 0 deletions applications/main/infrared/infrared_error_code.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#pragma once

typedef enum {
InfraredErrorCodeNone = 0,
InfraredErrorCodeFileOperationFailed = 0x800000,
InfraredErrorCodeWrongFileType = 0x80000100,
InfraredErrorCodeWrongFileVersion = 0x80000200,

//Common signal errors
InfraredErrorCodeSignalTypeUnknown = 0x80000300,
InfraredErrorCodeSignalNameNotFound = 0x80000400,
InfraredErrorCodeSignalUnableToReadType = 0x80000500,
InfraredErrorCodeSignalUnableToWriteType = 0x80000600,

//Raw signal errors
InfraredErrorCodeSignalRawUnableToReadFrequency = 0x80000700,
InfraredErrorCodeSignalRawUnableToReadDutyCycle = 0x80000800,
InfraredErrorCodeSignalRawUnableToReadTimingsSize = 0x80000900,
InfraredErrorCodeSignalRawUnableToReadTooLongData = 0x80000A00,
InfraredErrorCodeSignalRawUnableToReadData = 0x80000B00,

InfraredErrorCodeSignalRawUnableToWriteFrequency = 0x80000C00,
InfraredErrorCodeSignalRawUnableToWriteDutyCycle = 0x80000D00,
InfraredErrorCodeSignalRawUnableToWriteData = 0x80000E00,

//Message signal errors
InfraredErrorCodeSignalMessageUnableToReadProtocol = 0x80000F00,
InfraredErrorCodeSignalMessageUnableToReadAddress = 0x80001000,
InfraredErrorCodeSignalMessageUnableToReadCommand = 0x80001100,
InfraredErrorCodeSignalMessageIsInvalid = 0x80001200,

InfraredErrorCodeSignalMessageUnableToWriteProtocol = 0x80001300,
InfraredErrorCodeSignalMessageUnableToWriteAddress = 0x80001400,
InfraredErrorCodeSignalMessageUnableToWriteCommand = 0x80001500,
} InfraredErrorCode;

#define INFRARED_ERROR_CODE_MASK (0xFFFFFF00)
#define INFRARED_ERROR_INDEX_MASK (0x000000FF)

#define INFRARED_ERROR_GET_CODE(error) (error & INFRARED_ERROR_CODE_MASK)
#define INFRARED_ERROR_GET_INDEX(error) (error & INFRARED_ERROR_INDEX_MASK)
#define INFRARED_ERROR_SET_INDEX(code, index) (code |= (index & INFRARED_ERROR_INDEX_MASK))

#define INFRARED_ERROR_PRESENT(error) (INFRARED_ERROR_GET_CODE(error) != InfraredErrorCodeNone)
#define INFRARED_ERROR_CHECK(error, test_code) (INFRARED_ERROR_GET_CODE(error) == test_code)
Loading

0 comments on commit 4098ee9

Please sign in to comment.