Skip to content

Commit

Permalink
[FL-3895] Broken file interaction fixes (flipperdevices#3852)
Browse files Browse the repository at this point in the history
* Show error screen if corrupted filed has been loaded
* Added rpc error codes and error processing to NFC
* Made iButton scene on_enter handler clear to prevent showing scene before file is loaded
* Added rpc error codes and error processing to iButton
* Made lfRfid scene on_enter handler clear to prevent showing scene before file is loaded
* Added rpc error codes and error processing to lfRfid
* Made SubGHz scene on_enter handler clear to prevent showing scene before file is loaded. Also moved file_name_tmp formatting logic to a separate function
* Now function returns loading status and starts rx only if load succeeded
* Added show error logic on tx_button start
* Introduced rpc error codes for infrared
* Adjusted rpc scene logic to show scene only when
loading is fine
* Added new  list of rpc errors which are common within several applications
* Removed same enums from apps
* Same rpc error in different apps replaced with common value from rpc error code list
* SubGHz error codes now start from RpcAppSystemErrorCodesReserved value
* Infrared error codes now start from RpcAppSystemErrorCodesReserved value
* Removed unused enum
* Now all rpc error codes are more generalized and can be used among all apps without any specific enums
* Removed specific error codes, now rpc error codes are used instead
* RPC: no plurals in enums

Co-authored-by: あく <[email protected]>
  • Loading branch information
2 people authored and ofabel committed Sep 26, 2024
1 parent 316df21 commit 25e3d12
Show file tree
Hide file tree
Showing 13 changed files with 106 additions and 88 deletions.
24 changes: 12 additions & 12 deletions applications/main/ibutton/scenes/ibutton_scene_rpc.c
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
#include "../ibutton_i.h"

void ibutton_scene_rpc_on_enter(void* context) {
iButton* ibutton = context;
UNUSED(context);
}

static void ibutton_rpc_start_emulation(iButton* ibutton) {
Popup* popup = ibutton->popup;

popup_set_header(popup, "iButton", 82, 28, AlignCenter, AlignBottom);
popup_set_text(popup, "RPC mode", 82, 32, AlignCenter, AlignTop);

popup_set_text(popup, ibutton->key_name, 82, 32, AlignCenter, AlignTop);
popup_set_icon(popup, 2, 14, &I_iButtonKey_49x44);

view_dispatcher_switch_to_view(ibutton->view_dispatcher, iButtonViewPopup);

ibutton_worker_emulate_start(ibutton->worker, ibutton->key);

ibutton_notification_message(ibutton, iButtonNotificationMessageEmulateStart);
notification_message(ibutton->notifications, &sequence_display_backlight_on);
}

bool ibutton_scene_rpc_on_event(void* context, SceneManagerEvent event) {
iButton* ibutton = context;
Popup* popup = ibutton->popup;

bool consumed = false;

Expand All @@ -27,17 +31,13 @@ bool ibutton_scene_rpc_on_event(void* context, SceneManagerEvent event) {
bool result = false;

if(ibutton_load_key(ibutton, false)) {
popup_set_text(popup, ibutton->key_name, 82, 32, AlignCenter, AlignTop);
view_dispatcher_switch_to_view(ibutton->view_dispatcher, iButtonViewPopup);

ibutton_notification_message(ibutton, iButtonNotificationMessageEmulateStart);
ibutton_worker_emulate_start(ibutton->worker, ibutton->key);

ibutton_rpc_start_emulation(ibutton);
result = true;
} else {
rpc_system_app_set_error_code(ibutton->rpc, RpcAppSystemErrorCodeParseFile);
rpc_system_app_set_error_text(ibutton->rpc, "Cannot load key file");
}

rpc_system_app_confirm(ibutton->rpc, result);

} else if(event.event == iButtonCustomEventRpcExit) {
rpc_system_app_confirm(ibutton->rpc, true);
scene_manager_stop(ibutton->scene_manager);
Expand Down
12 changes: 5 additions & 7 deletions applications/main/infrared/infrared_app.c
Original file line number Diff line number Diff line change
Expand Up @@ -382,17 +382,15 @@ void infrared_tx_start(InfraredApp* infrared) {
infrared->app_state.is_transmitting = true;
}

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

if(infrared_remote_load_signal(infrared->remote, infrared->current_signal, button_index)) {
bool result =
infrared_remote_load_signal(infrared->remote, infrared->current_signal, button_index);
if(result) {
infrared_tx_start(infrared);
} else {
infrared_show_error_message(
infrared,
"Failed to load\n\"%s\"",
infrared_remote_get_signal_name(infrared->remote, button_index));
}
return result;
}

void infrared_tx_stop(InfraredApp* infrared) {
Expand Down
2 changes: 1 addition & 1 deletion applications/main/infrared/infrared_app_i.h
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ void infrared_tx_start(InfraredApp* infrared);
* @param[in] button_index index of the signal to be loaded.
* @returns true if the signal could be loaded, false otherwise.
*/
void infrared_tx_start_button_index(InfraredApp* infrared, size_t button_index);
bool infrared_tx_start_button_index(InfraredApp* infrared, size_t button_index);

/**
* @brief Stop transmission of the currently loaded signal.
Expand Down
7 changes: 6 additions & 1 deletion applications/main/infrared/scenes/infrared_scene_remote.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,12 @@ bool infrared_scene_remote_on_event(void* context, SceneManagerEvent event) {

if(custom_type == InfraredCustomEventTypeTransmitStarted) {
furi_assert(button_index >= 0);
infrared_tx_start_button_index(infrared, button_index);
if(!infrared_tx_start_button_index(infrared, button_index)) {
infrared_show_error_message(
infrared,
"Failed to load\n\"%s\"",
infrared_remote_get_signal_name(infrared->remote, button_index));
}
consumed = true;
} else if(custom_type == InfraredCustomEventTypeTransmitStopped) {
infrared_tx_stop(infrared);
Expand Down
45 changes: 28 additions & 17 deletions applications/main/infrared/scenes/infrared_scene_rpc.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,24 @@ static int32_t infrared_scene_rpc_task_callback(void* context) {

void infrared_scene_rpc_on_enter(void* context) {
InfraredApp* infrared = context;
scene_manager_set_scene_state(infrared->scene_manager, InfraredSceneRpc, InfraredRpcStateIdle);
}

static void infrared_scene_rpc_show(InfraredApp* infrared) {
Popup* popup = infrared->popup;

popup_set_header(popup, "Infrared", 89, 42, AlignCenter, AlignBottom);
popup_set_text(popup, "RPC mode", 89, 44, AlignCenter, AlignTop);
popup_set_text(popup, infrared->text_store[0], 89, 44, AlignCenter, AlignTop);

popup_set_icon(popup, 0, 12, &I_RFIDDolphinSend_97x61);

popup_set_context(popup, context);
popup_set_context(popup, infrared);
popup_set_callback(popup, infrared_popup_closed_callback);

view_dispatcher_switch_to_view(infrared->view_dispatcher, InfraredViewPopup);
scene_manager_set_scene_state(infrared->scene_manager, InfraredSceneRpc, InfraredRpcStateIdle);
scene_manager_set_scene_state(
infrared->scene_manager, InfraredSceneRpc, InfraredRpcStateSending);
notification_message(infrared->notifications, &sequence_display_backlight_on);
}

Expand All @@ -52,24 +58,20 @@ bool infrared_scene_rpc_on_event(void* context, SceneManagerEvent event) {

} else if(event.event == InfraredCustomEventTypeTaskFinished) {
const bool task_success = infrared_blocking_task_finalize(infrared);

if(task_success) {
const char* remote_name = infrared_remote_get_name(infrared->remote);
infrared_text_store_set(infrared, 0, "loaded\n%s", remote_name);
scene_manager_set_scene_state(
infrared->scene_manager, InfraredSceneRpc, InfraredRpcStateLoaded);

} else {
infrared_text_store_set(
infrared, 0, "failed to load\n%s", furi_string_get_cstr(infrared->file_path));
}
FuriString* str = furi_string_alloc();
furi_string_printf(
str, "Failed to load\n%s", furi_string_get_cstr(infrared->file_path));

popup_set_text(
infrared->popup, infrared->text_store[0], 89, 44, AlignCenter, AlignTop);
view_dispatcher_switch_to_view(infrared->view_dispatcher, InfraredViewPopup);
rpc_system_app_set_error_code(infrared->rpc_ctx, RpcAppSystemErrorCodeParseFile);
rpc_system_app_set_error_text(infrared->rpc_ctx, furi_string_get_cstr(str));

furi_string_free(str);
}
rpc_system_app_confirm(infrared->rpc_ctx, task_success);

} else if(
event.event == InfraredCustomEventTypeRpcButtonPressName ||
event.event == InfraredCustomEventTypeRpcButtonPressIndex) {
Expand All @@ -88,10 +90,19 @@ bool infrared_scene_rpc_on_event(void* context, SceneManagerEvent event) {
TAG, "Sending signal with index \"%ld\"", app_state->current_button_index);
}
if(infrared->app_state.current_button_index != InfraredButtonIndexNone) {
infrared_tx_start_button_index(infrared, app_state->current_button_index);
scene_manager_set_scene_state(
infrared->scene_manager, InfraredSceneRpc, InfraredRpcStateSending);
result = true;
if(infrared_tx_start_button_index(infrared, app_state->current_button_index)) {
const char* remote_name = infrared_remote_get_name(infrared->remote);
infrared_text_store_set(infrared, 0, "emulating\n%s", remote_name);

infrared_scene_rpc_show(infrared);
result = true;
} else {
rpc_system_app_set_error_code(
infrared->rpc_ctx, RpcAppSystemErrorCodeInternalParse);
rpc_system_app_set_error_text(
infrared->rpc_ctx, "Cannot load button data");
result = false;
}
}
}
rpc_system_app_confirm(infrared->rpc_ctx, result);
Expand Down
30 changes: 16 additions & 14 deletions applications/main/lfrfid/scenes/lfrfid_scene_rpc.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,30 @@

void lfrfid_scene_rpc_on_enter(void* context) {
LfRfid* app = context;
app->rpc_state = LfRfidRpcStateIdle;
}

static void lfrfid_rpc_start_emulation(LfRfid* app) {
Popup* popup = app->popup;

lfrfid_text_store_set(app, "emulating\n%s", furi_string_get_cstr(app->file_name));

popup_set_header(popup, "LF RFID", 89, 42, AlignCenter, AlignBottom);
popup_set_text(popup, "RPC mode", 89, 44, AlignCenter, AlignTop);
popup_set_text(popup, app->text_store, 89, 44, AlignCenter, AlignTop);
popup_set_icon(popup, 0, 12, &I_RFIDDolphinSend_97x61);

view_dispatcher_switch_to_view(app->view_dispatcher, LfRfidViewPopup);

notification_message(app->notifications, &sequence_display_backlight_on);
lfrfid_worker_start_thread(app->lfworker);
lfrfid_worker_emulate_start(app->lfworker, (LFRFIDProtocol)app->protocol_id);

app->rpc_state = LfRfidRpcStateIdle;
notification_message(app->notifications, &sequence_display_backlight_on);
notification_message(app->notifications, &sequence_blink_start_magenta);
app->rpc_state = LfRfidRpcStateEmulating;
}

bool lfrfid_scene_rpc_on_event(void* context, SceneManagerEvent event) {
LfRfid* app = context;
Popup* popup = app->popup;
UNUSED(event);
bool consumed = false;

if(event.type == SceneManagerEventTypeCustom) {
Expand All @@ -34,16 +41,11 @@ bool lfrfid_scene_rpc_on_event(void* context, SceneManagerEvent event) {
bool result = false;
if(app->rpc_state == LfRfidRpcStateIdle) {
if(lfrfid_load_key_data(app, app->file_path, false)) {
lfrfid_worker_start_thread(app->lfworker);
lfrfid_worker_emulate_start(app->lfworker, (LFRFIDProtocol)app->protocol_id);
app->rpc_state = LfRfidRpcStateEmulating;

lfrfid_text_store_set(
app, "emulating\n%s", furi_string_get_cstr(app->file_name));
popup_set_text(popup, app->text_store, 89, 44, AlignCenter, AlignTop);

notification_message(app->notifications, &sequence_blink_start_magenta);
lfrfid_rpc_start_emulation(app);
result = true;
} else {
rpc_system_app_set_error_code(app->rpc_ctx, RpcAppSystemErrorCodeParseFile);
rpc_system_app_set_error_text(app->rpc_ctx, "Cannot load key file");
}
}
rpc_system_app_confirm(app->rpc_ctx, result);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,10 @@ static bool nfc_protocol_support_scene_rpc_on_event(NfcApp* instance, SceneManag
if(nfc_load_file(instance, instance->file_path, false)) {
nfc_protocol_support_scene_rpc_setup_ui_and_emulate(instance);
success = true;
} else {
rpc_system_app_set_error_code(
instance->rpc_ctx, RpcAppSystemErrorCodeParseFile);
rpc_system_app_set_error_text(instance->rpc_ctx, "Cannot load key file");
}
}
rpc_system_app_confirm(instance->rpc_ctx, success);
Expand Down
2 changes: 1 addition & 1 deletion applications/main/nfc/nfc_app.c
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ int32_t nfc_app(void* p) {
nfc->view_dispatcher, nfc->gui, ViewDispatcherTypeFullscreen);

furi_string_set(nfc->file_path, args);
if(nfc_load_file(nfc, nfc->file_path, false)) {
if(nfc_load_file(nfc, nfc->file_path, true)) {
nfc_show_initial_scene_for_device(nfc);
} else {
view_dispatcher_stop(nfc->view_dispatcher);
Expand Down
14 changes: 0 additions & 14 deletions applications/main/subghz/helpers/subghz_error_type.h

This file was deleted.

41 changes: 21 additions & 20 deletions applications/main/subghz/scenes/subghz_scene_rpc.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,33 @@ typedef enum {

void subghz_scene_rpc_on_enter(void* context) {
SubGhz* subghz = context;
scene_manager_set_scene_state(subghz->scene_manager, SubGhzSceneRpc, SubGhzRpcStateIdle);
}

static void subghz_format_file_name_tmp(SubGhz* subghz) {
FuriString* file_name;
file_name = furi_string_alloc();
path_extract_filename(subghz->file_path, file_name, true);
snprintf(
subghz->file_name_tmp, SUBGHZ_MAX_LEN_NAME, "loaded\n%s", furi_string_get_cstr(file_name));
furi_string_free(file_name);
}

static void subghz_scene_rpc_emulation_show(SubGhz* subghz) {
Popup* popup = subghz->popup;

subghz_format_file_name_tmp(subghz);
popup_set_header(popup, "Sub-GHz", 89, 42, AlignCenter, AlignBottom);
popup_set_text(popup, "RPC mode", 89, 44, AlignCenter, AlignTop);

popup_set_icon(popup, 0, 12, &I_RFIDDolphinSend_97x61);
popup_set_text(popup, subghz->file_name_tmp, 89, 44, AlignCenter, AlignTop);

view_dispatcher_switch_to_view(subghz->view_dispatcher, SubGhzViewIdPopup);

scene_manager_set_scene_state(subghz->scene_manager, SubGhzSceneRpc, SubGhzRpcStateIdle);

notification_message(subghz->notifications, &sequence_display_backlight_on);
}

bool subghz_scene_rpc_on_event(void* context, SceneManagerEvent event) {
SubGhz* subghz = context;
Popup* popup = subghz->popup;
bool consumed = false;
SubGhzRpcState state = scene_manager_get_scene_state(subghz->scene_manager, SubGhzSceneRpc);

Expand All @@ -43,13 +53,15 @@ bool subghz_scene_rpc_on_event(void* context, SceneManagerEvent event) {
switch(
subghz_txrx_tx_start(subghz->txrx, subghz_txrx_get_fff_data(subghz->txrx))) {
case SubGhzTxRxStartTxStateErrorOnlyRx:
rpc_system_app_set_error_code(subghz->rpc_ctx, SubGhzErrorTypeOnlyRX);
rpc_system_app_set_error_code(
subghz->rpc_ctx, RpcAppSystemErrorCodeRegionLock);
rpc_system_app_set_error_text(
subghz->rpc_ctx,
"Transmission on this frequency is restricted in your region");
break;
case SubGhzTxRxStartTxStateErrorParserOthers:
rpc_system_app_set_error_code(subghz->rpc_ctx, SubGhzErrorTypeParserOthers);
rpc_system_app_set_error_code(
subghz->rpc_ctx, RpcAppSystemErrorCodeInternalParse);
rpc_system_app_set_error_text(
subghz->rpc_ctx, "Error in protocol parameters description");
break;
Expand Down Expand Up @@ -77,23 +89,12 @@ bool subghz_scene_rpc_on_event(void* context, SceneManagerEvent event) {
bool result = false;
if(state == SubGhzRpcStateIdle) {
if(subghz_key_load(subghz, furi_string_get_cstr(subghz->file_path), false)) {
subghz_scene_rpc_emulation_show(subghz);
scene_manager_set_scene_state(
subghz->scene_manager, SubGhzSceneRpc, SubGhzRpcStateLoaded);
result = true;
FuriString* file_name;
file_name = furi_string_alloc();
path_extract_filename(subghz->file_path, file_name, true);

snprintf(
subghz->file_name_tmp,
SUBGHZ_MAX_LEN_NAME,
"loaded\n%s",
furi_string_get_cstr(file_name));
popup_set_text(popup, subghz->file_name_tmp, 89, 44, AlignCenter, AlignTop);

furi_string_free(file_name);
} else {
rpc_system_app_set_error_code(subghz->rpc_ctx, SubGhzErrorTypeParseFile);
rpc_system_app_set_error_code(subghz->rpc_ctx, RpcAppSystemErrorCodeParseFile);
rpc_system_app_set_error_text(subghz->rpc_ctx, "Cannot parse file");
}
}
Expand Down
1 change: 0 additions & 1 deletion applications/main/subghz/subghz_i.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#pragma once

#include "helpers/subghz_types.h"
#include "helpers/subghz_error_type.h"
#include <lib/subghz/types.h>
#include "subghz.h"
#include "views/receiver.h"
Expand Down
1 change: 1 addition & 0 deletions applications/services/rpc/rpc_app.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#pragma once

#include "rpc.h"
#include "rpc_app_error_codes.h"

#ifdef __cplusplus
extern "C" {
Expand Down
11 changes: 11 additions & 0 deletions applications/services/rpc/rpc_app_error_codes.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once

/**
* @brief Enumeration of possible error codes for application which can be started through rpc
*/
typedef enum {
RpcAppSystemErrorCodeNone, /** There are no errors */
RpcAppSystemErrorCodeParseFile, /** File parsing error, or wrong file structure, or missing required parameters. more accurate data can be obtained through the debug port */
RpcAppSystemErrorCodeRegionLock, /** Requested function is blocked by regional settings */
RpcAppSystemErrorCodeInternalParse, /** Error in protocol parameters description, or some data in opened file are unsupported */
} RpcAppSystemErrorCode;

0 comments on commit 25e3d12

Please sign in to comment.