Skip to content

Commit

Permalink
[FL-2705] App RPC Bug Fixes and redesign (#1491)
Browse files Browse the repository at this point in the history
* Rpc: remove callback timer
* Rpc: simplify rpc event callback
* Rpc: migrate to new confirmation schema
* Rpc: migrate to new confirmation schema part2: finalize ibutton and rfid
* Rpc: migrate to new confirmation schema part3: finallize nfc and fix id in load
* Rpc: hardened sequencing check
* Rpc: migrate to new confirmation schema part4: finalize subghz
* iButton: properly handle exit
* Nfc: correct sequence for rpc exit send
* Rpc: fix review issues and nfc exit message
* Rpc: more logging and condition race fix in confirmation
* Rpc: migrate to new confirmation schema part5: finalize infrared
* Rpc: more logging
  • Loading branch information
skotopes authored Aug 2, 2022
1 parent f9386b2 commit f9745b4
Show file tree
Hide file tree
Showing 20 changed files with 378 additions and 270 deletions.
29 changes: 8 additions & 21 deletions applications/ibutton/ibutton.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include "m-string.h"
#include <toolbox/path.h>
#include <flipper_format/flipper_format.h>
#include "rpc/rpc_app.h"
#include <rpc/rpc_app.h>

#define TAG "iButtonApp"

Expand Down Expand Up @@ -58,7 +58,7 @@ static void ibutton_make_app_folder(iButton* ibutton) {
}
}

static bool ibutton_load_key_data(iButton* ibutton, string_t key_path, bool show_dialog) {
bool ibutton_load_key_data(iButton* ibutton, string_t key_path, bool show_dialog) {
FlipperFormat* file = flipper_format_file_alloc(ibutton->storage);
bool result = false;
string_t data;
Expand Down Expand Up @@ -99,33 +99,20 @@ static bool ibutton_load_key_data(iButton* ibutton, string_t key_path, bool show
return result;
}

static bool ibutton_rpc_command_callback(RpcAppSystemEvent event, const char* arg, void* context) {
static void ibutton_rpc_command_callback(RpcAppSystemEvent event, void* context) {
furi_assert(context);
iButton* ibutton = context;

bool result = false;

if(event == RpcAppEventSessionClose) {
rpc_system_app_set_callback(ibutton->rpc_ctx, NULL, NULL);
ibutton->rpc_ctx = NULL;
view_dispatcher_send_custom_event(ibutton->view_dispatcher, iButtonCustomEventRpcExit);
result = true;
view_dispatcher_send_custom_event(
ibutton->view_dispatcher, iButtonCustomEventRpcSessionClose);
} else if(event == RpcAppEventAppExit) {
view_dispatcher_send_custom_event(ibutton->view_dispatcher, iButtonCustomEventRpcExit);
result = true;
} else if(event == RpcAppEventLoadFile) {
if(arg) {
string_set_str(ibutton->file_path, arg);
if(ibutton_load_key_data(ibutton, ibutton->file_path, false)) {
ibutton_worker_emulate_start(ibutton->key_worker, ibutton->key);
view_dispatcher_send_custom_event(
ibutton->view_dispatcher, iButtonCustomEventRpcLoad);
result = true;
}
}
view_dispatcher_send_custom_event(ibutton->view_dispatcher, iButtonCustomEventRpcLoad);
} else {
rpc_system_app_confirm(ibutton->rpc_ctx, event, false);
}

return result;
}

bool ibutton_custom_event_callback(void* context, uint32_t event) {
Expand Down
1 change: 1 addition & 0 deletions applications/ibutton/ibutton_custom_event.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ enum iButtonCustomEvent {

iButtonCustomEventRpcLoad,
iButtonCustomEventRpcExit,
iButtonCustomEventRpcSessionClose,
};
1 change: 1 addition & 0 deletions applications/ibutton/ibutton_i.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ typedef enum {
} iButtonNotificationMessage;

bool ibutton_file_select(iButton* ibutton);
bool ibutton_load_key_data(iButton* ibutton, string_t key_path, bool show_dialog);
bool ibutton_save_key(iButton* ibutton, const char* key_name);
bool ibutton_delete_key(iButton* ibutton);
void ibutton_text_store_set(iButton* ibutton, const char* text, ...);
Expand Down
44 changes: 31 additions & 13 deletions applications/ibutton/scenes/ibutton_scene_rpc.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "../ibutton_i.h"
#include <toolbox/path.h>
#include <rpc/rpc_app.h>

void ibutton_scene_rpc_on_enter(void* context) {
iButton* ibutton = context;
Expand All @@ -26,23 +27,40 @@ bool ibutton_scene_rpc_on_event(void* context, SceneManagerEvent event) {
if(event.type == SceneManagerEventTypeCustom) {
consumed = true;
if(event.event == iButtonCustomEventRpcLoad) {
string_t key_name;
string_init(key_name);
if(string_end_with_str_p(ibutton->file_path, IBUTTON_APP_EXTENSION)) {
path_extract_filename(ibutton->file_path, key_name, true);
}
const char* arg = rpc_system_app_get_data(ibutton->rpc_ctx);
bool result = false;
if(arg) {
string_set_str(ibutton->file_path, arg);
if(ibutton_load_key_data(ibutton, ibutton->file_path, false)) {
ibutton_worker_emulate_start(ibutton->key_worker, ibutton->key);
string_t key_name;
string_init(key_name);
if(string_end_with_str_p(ibutton->file_path, IBUTTON_APP_EXTENSION)) {
path_extract_filename(ibutton->file_path, key_name, true);
}

if(!string_empty_p(key_name)) {
ibutton_text_store_set(ibutton, "emulating\n%s", string_get_cstr(key_name));
} else {
ibutton_text_store_set(ibutton, "emulating");
}
popup_set_text(popup, ibutton->text_store, 82, 32, AlignCenter, AlignTop);
if(!string_empty_p(key_name)) {
ibutton_text_store_set(
ibutton, "emulating\n%s", string_get_cstr(key_name));
} else {
ibutton_text_store_set(ibutton, "emulating");
}
popup_set_text(popup, ibutton->text_store, 82, 32, AlignCenter, AlignTop);

ibutton_notification_message(ibutton, iButtonNotificationMessageEmulateStart);
ibutton_notification_message(ibutton, iButtonNotificationMessageEmulateStart);

string_clear(key_name);
string_clear(key_name);
result = true;
}
}
rpc_system_app_confirm(ibutton->rpc_ctx, RpcAppEventLoadFile, result);
} else if(event.event == iButtonCustomEventRpcExit) {
rpc_system_app_confirm(ibutton->rpc_ctx, RpcAppEventAppExit, true);
ibutton_notification_message(ibutton, iButtonNotificationMessageBlinkStop);
view_dispatcher_stop(ibutton->view_dispatcher);
} else if(event.event == iButtonCustomEventRpcSessionClose) {
rpc_system_app_set_callback(ibutton->rpc_ctx, NULL, NULL);
ibutton->rpc_ctx = NULL;
ibutton_notification_message(ibutton, iButtonNotificationMessageBlinkStop);
view_dispatcher_stop(ibutton->view_dispatcher);
}
Expand Down
47 changes: 12 additions & 35 deletions applications/infrared/infrared.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,52 +36,29 @@ static void infrared_tick_event_callback(void* context) {
scene_manager_handle_tick_event(infrared->scene_manager);
}

static bool
infrared_rpc_command_callback(RpcAppSystemEvent event, const char* arg, void* context) {
static void infrared_rpc_command_callback(RpcAppSystemEvent event, void* context) {
furi_assert(context);
Infrared* infrared = context;

if(!infrared->rpc_ctx) {
return false;
}

bool result = false;
furi_assert(infrared->rpc_ctx);

if(event == RpcAppEventSessionClose) {
rpc_system_app_set_callback(infrared->rpc_ctx, NULL, NULL);
infrared->rpc_ctx = NULL;
view_dispatcher_send_custom_event(
infrared->view_dispatcher, InfraredCustomEventTypeBackPressed);
result = true;
infrared->view_dispatcher, InfraredCustomEventTypeRpcSessionClose);
} else if(event == RpcAppEventAppExit) {
view_dispatcher_send_custom_event(
infrared->view_dispatcher, InfraredCustomEventTypeBackPressed);
result = true;
infrared->view_dispatcher, InfraredCustomEventTypeRpcExit);
} else if(event == RpcAppEventLoadFile) {
if(arg) {
string_set_str(infrared->file_path, arg);
result = infrared_remote_load(infrared->remote, infrared->file_path);
infrared_worker_tx_set_get_signal_callback(
infrared->worker, infrared_worker_tx_get_signal_steady_callback, infrared);
infrared_worker_tx_set_signal_sent_callback(
infrared->worker, infrared_signal_sent_callback, infrared);
view_dispatcher_send_custom_event(
infrared->view_dispatcher, InfraredCustomEventTypeRpcLoaded);
}
view_dispatcher_send_custom_event(
infrared->view_dispatcher, InfraredCustomEventTypeRpcLoad);
} else if(event == RpcAppEventButtonPress) {
if(arg) {
size_t button_index = 0;
if(infrared_remote_find_button_by_name(infrared->remote, arg, &button_index)) {
infrared_tx_start_button_index(infrared, button_index);
result = true;
}
}
view_dispatcher_send_custom_event(
infrared->view_dispatcher, InfraredCustomEventTypeRpcButtonPress);
} else if(event == RpcAppEventButtonRelease) {
infrared_tx_stop(infrared);
result = true;
view_dispatcher_send_custom_event(
infrared->view_dispatcher, InfraredCustomEventTypeRpcButtonRelease);
} else {
rpc_system_app_confirm(infrared->rpc_ctx, event, false);
}

return result;
}

static void infrared_find_vacant_remote_name(string_t name, const char* path) {
Expand Down
7 changes: 6 additions & 1 deletion applications/infrared/infrared_custom_event.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ enum InfraredCustomEventType {
InfraredCustomEventTypePopupClosed,
InfraredCustomEventTypeButtonSelected,
InfraredCustomEventTypeBackPressed,
InfraredCustomEventTypeRpcLoaded,

InfraredCustomEventTypeRpcLoad,
InfraredCustomEventTypeRpcExit,
InfraredCustomEventTypeRpcButtonPress,
InfraredCustomEventTypeRpcButtonRelease,
InfraredCustomEventTypeRpcSessionClose,
};

#pragma pack(push, 1)
Expand Down
35 changes: 34 additions & 1 deletion applications/infrared/scenes/infrared_scene_rpc.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,45 @@ bool infrared_scene_rpc_on_event(void* context, SceneManagerEvent event) {
view_dispatcher_stop(infrared->view_dispatcher);
} else if(event.event == InfraredCustomEventTypePopupClosed) {
view_dispatcher_stop(infrared->view_dispatcher);
} else if(event.event == InfraredCustomEventTypeRpcLoaded) {
} else if(event.event == InfraredCustomEventTypeRpcLoad) {
bool result = false;
const char* arg = rpc_system_app_get_data(infrared->rpc_ctx);
if(arg) {
string_set_str(infrared->file_path, arg);
result = infrared_remote_load(infrared->remote, infrared->file_path);
infrared_worker_tx_set_get_signal_callback(
infrared->worker, infrared_worker_tx_get_signal_steady_callback, infrared);
infrared_worker_tx_set_signal_sent_callback(
infrared->worker, infrared_signal_sent_callback, infrared);
}
const char* remote_name = infrared_remote_get_name(infrared->remote);

infrared_text_store_set(infrared, 0, "loaded\n%s", remote_name);
popup_set_text(
infrared->popup, infrared->text_store[0], 82, 32, AlignCenter, AlignTop);

rpc_system_app_confirm(infrared->rpc_ctx, RpcAppEventLoadFile, result);
} else if(event.event == InfraredCustomEventTypeRpcButtonPress) {
bool result = false;
const char* arg = rpc_system_app_get_data(infrared->rpc_ctx);
if(arg) {
size_t button_index = 0;
if(infrared_remote_find_button_by_name(infrared->remote, arg, &button_index)) {
infrared_tx_start_button_index(infrared, button_index);
result = true;
}
}
rpc_system_app_confirm(infrared->rpc_ctx, RpcAppEventButtonRelease, result);
} else if(event.event == InfraredCustomEventTypeRpcButtonRelease) {
infrared_tx_stop(infrared);
rpc_system_app_confirm(infrared->rpc_ctx, RpcAppEventButtonRelease, true);
} else if(event.event == InfraredCustomEventTypeRpcExit) {
view_dispatcher_stop(infrared->view_dispatcher);
rpc_system_app_confirm(infrared->rpc_ctx, RpcAppEventAppExit, true);
} else if(event.event == InfraredCustomEventTypeRpcSessionClose) {
rpc_system_app_set_callback(infrared->rpc_ctx, NULL, NULL);
infrared->rpc_ctx = NULL;
view_dispatcher_stop(infrared->view_dispatcher);
}
}
return consumed;
Expand Down
35 changes: 11 additions & 24 deletions applications/lfrfid/lfrfid_app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
#include <toolbox/path.h>
#include <flipper_format/flipper_format.h>

#include "rpc/rpc_app.h"
#include <rpc/rpc_app.h>

const char* LfRfidApp::app_folder = ANY_PATH("lfrfid");
const char* LfRfidApp::app_extension = ".rfid";
Expand All @@ -48,38 +48,25 @@ LfRfidApp::~LfRfidApp() {
}
}

static bool rpc_command_callback(RpcAppSystemEvent event, const char* arg, void* context) {
static void rpc_command_callback(RpcAppSystemEvent rpc_event, void* context) {
furi_assert(context);
LfRfidApp* app = static_cast<LfRfidApp*>(context);

bool result = false;

if(event == RpcAppEventSessionClose) {
rpc_system_app_set_callback(app->rpc_ctx, NULL, NULL);
app->rpc_ctx = NULL;
if(rpc_event == RpcAppEventSessionClose) {
LfRfidApp::Event event;
event.type = LfRfidApp::EventType::Exit;
event.type = LfRfidApp::EventType::RpcSessionClose;
app->view_controller.send_event(&event);
result = true;
} else if(event == RpcAppEventAppExit) {
} else if(rpc_event == RpcAppEventAppExit) {
LfRfidApp::Event event;
event.type = LfRfidApp::EventType::Exit;
app->view_controller.send_event(&event);
result = true;
} else if(event == RpcAppEventLoadFile) {
if(arg) {
string_set_str(app->file_path, arg);
if(app->load_key_data(app->file_path, &(app->worker.key), false)) {
LfRfidApp::Event event;
event.type = LfRfidApp::EventType::EmulateStart;
app->view_controller.send_event(&event);
app->worker.start_emulate();
result = true;
}
}
} else if(rpc_event == RpcAppEventLoadFile) {
LfRfidApp::Event event;
event.type = LfRfidApp::EventType::RpcLoadFile;
app->view_controller.send_event(&event);
} else {
rpc_system_app_confirm(app->rpc_ctx, rpc_event, false);
}

return result;
}

void LfRfidApp::run(void* _args) {
Expand Down
2 changes: 2 additions & 0 deletions applications/lfrfid/lfrfid_app.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ class LfRfidApp {
Retry,
Exit,
EmulateStart,
RpcLoadFile,
RpcSessionClose,
};

enum class SceneType : uint8_t {
Expand Down
26 changes: 26 additions & 0 deletions applications/lfrfid/scene/lfrfid_app_scene_rpc.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "lfrfid_app_scene_rpc.h"
#include <core/common_defines.h>
#include <dolphin/dolphin.h>
#include <rpc/rpc_app.h>

static const NotificationSequence sequence_blink_start_magenta = {
&message_blink_start_10,
Expand Down Expand Up @@ -36,6 +37,16 @@ bool LfRfidAppSceneRpc::on_event(LfRfidApp* app, LfRfidApp::Event* event) {
LfRfidApp::Event view_event;
view_event.type = LfRfidApp::EventType::Back;
app->view_controller.send_event(&view_event);
rpc_system_app_confirm(app->rpc_ctx, RpcAppEventAppExit, true);
} else if(event->type == LfRfidApp::EventType::RpcSessionClose) {
// Detach RPC
rpc_system_app_set_callback(app->rpc_ctx, NULL, NULL);
app->rpc_ctx = NULL;

consumed = true;
LfRfidApp::Event view_event;
view_event.type = LfRfidApp::EventType::Back;
app->view_controller.send_event(&view_event);
} else if(event->type == LfRfidApp::EventType::EmulateStart) {
auto popup = app->view_controller.get<PopupVM>();
consumed = true;
Expand All @@ -45,7 +56,22 @@ bool LfRfidAppSceneRpc::on_event(LfRfidApp* app, LfRfidApp::Event* event) {
popup->set_text(app->text_store.text, 89, 43, AlignCenter, AlignTop);

notification_message(app->notification, &sequence_blink_start_magenta);
} else if(event->type == LfRfidApp::EventType::RpcLoadFile) {
const char* arg = rpc_system_app_get_data(app->rpc_ctx);
bool result = false;
if(arg) {
string_set_str(app->file_path, arg);
if(app->load_key_data(app->file_path, &(app->worker.key), false)) {
LfRfidApp::Event event;
event.type = LfRfidApp::EventType::EmulateStart;
app->view_controller.send_event(&event);
app->worker.start_emulate();
result = true;
}
}
rpc_system_app_confirm(app->rpc_ctx, RpcAppEventLoadFile, result);
}

return consumed;
}

Expand Down
1 change: 1 addition & 0 deletions applications/nfc/helpers/nfc_custom_event.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ enum NfcCustomEvent {
NfcCustomEventDictAttackDone,
NfcCustomEventDictAttackSkip,
NfcCustomEventRpcLoad,
NfcCustomEventRpcSessionClose,
};
Loading

0 comments on commit f9745b4

Please sign in to comment.