[FL-2589] RPC App control commands (#1350)

* RPC App control commands
* Button release timeout
* SubGhz tx fix

Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
This commit is contained in:
Nikolay Minaylov
2022-07-04 16:09:46 +03:00
committed by GitHub
parent 0e78f38404
commit 4a1695ba1c
32 changed files with 768 additions and 45 deletions

78
applications/nfc/nfc.c Executable file → Normal file
View File

@@ -19,6 +19,77 @@ void nfc_tick_event_callback(void* context) {
scene_manager_handle_tick_event(nfc->scene_manager);
}
void nfc_rpc_exit_callback(Nfc* nfc) {
if(nfc->rpc_state == NfcRpcStateEmulating) {
// Stop worker
nfc_worker_stop(nfc->worker);
} else if(nfc->rpc_state == NfcRpcStateEmulated) {
// Stop worker
nfc_worker_stop(nfc->worker);
// Save data in shadow file
nfc_device_save_shadow(nfc->dev, nfc->dev->dev_name);
}
if(nfc->rpc_ctx) {
rpc_system_app_set_callback(nfc->rpc_ctx, NULL, NULL);
nfc->rpc_ctx = NULL;
}
}
static void nfc_rpc_emulate_callback(NfcWorkerEvent event, void* context) {
UNUSED(event);
Nfc* nfc = context;
nfc->rpc_state = NfcRpcStateEmulated;
}
static bool nfc_rpc_command_callback(RpcAppSystemEvent event, const char* arg, void* context) {
furi_assert(context);
Nfc* nfc = context;
if(!nfc->rpc_ctx) {
return false;
}
bool result = false;
if(event == RpcAppEventSessionClose) {
rpc_system_app_set_callback(nfc->rpc_ctx, NULL, NULL);
nfc->rpc_ctx = NULL;
view_dispatcher_send_custom_event(nfc->view_dispatcher, NfcCustomEventViewExit);
result = true;
} else if(event == RpcAppEventAppExit) {
view_dispatcher_send_custom_event(nfc->view_dispatcher, NfcCustomEventViewExit);
result = true;
} else if(event == RpcAppEventLoadFile) {
if((arg) && (nfc->rpc_state == NfcRpcStateIdle)) {
if(nfc_device_load(nfc->dev, arg, false)) {
if(nfc->dev->format == NfcDeviceSaveFormatMifareUl) {
nfc_worker_start(
nfc->worker,
NfcWorkerStateEmulateMifareUltralight,
&nfc->dev->dev_data,
nfc_rpc_emulate_callback,
nfc);
} else if(nfc->dev->format == NfcDeviceSaveFormatMifareClassic) {
nfc_worker_start(
nfc->worker,
NfcWorkerStateEmulateMifareClassic,
&nfc->dev->dev_data,
nfc_rpc_emulate_callback,
nfc);
} else {
nfc_worker_start(
nfc->worker, NfcWorkerStateEmulate, &nfc->dev->dev_data, NULL, nfc);
}
nfc->rpc_state = NfcRpcStateEmulating;
result = true;
}
}
}
return result;
}
Nfc* nfc_alloc() {
Nfc* nfc = malloc(sizeof(Nfc));
@@ -193,7 +264,12 @@ int32_t nfc_app(void* p) {
// Check argument and run corresponding scene
if((*args != '\0')) {
if(nfc_device_load(nfc->dev, p)) {
uint32_t rpc_ctx = 0;
if(sscanf(p, "RPC %lX", &rpc_ctx) == 1) {
nfc->rpc_ctx = (void*)rpc_ctx;
rpc_system_app_set_callback(nfc->rpc_ctx, nfc_rpc_command_callback, nfc);
scene_manager_next_scene(nfc->scene_manager, NfcSceneRpc);
} else if(nfc_device_load(nfc->dev, p, true)) {
if(nfc->dev->format == NfcDeviceSaveFormatMifareUl) {
scene_manager_next_scene(nfc->scene_manager, NfcSceneEmulateMifareUl);
} else if(nfc->dev->format == NfcDeviceSaveFormatMifareClassic) {

View File

@@ -837,7 +837,7 @@ bool nfc_device_save_shadow(NfcDevice* dev, const char* dev_name) {
return nfc_device_save_file(dev, dev_name, NFC_APP_FOLDER, NFC_APP_SHADOW_EXTENSION, true);
}
static bool nfc_device_load_data(NfcDevice* dev, string_t path) {
static bool nfc_device_load_data(NfcDevice* dev, string_t path, bool show_dialog) {
bool parsed = false;
FlipperFormat* file = flipper_format_file_alloc(dev->storage);
FuriHalNfcDevData* data = &dev->dev_data.nfc_data;
@@ -887,7 +887,7 @@ static bool nfc_device_load_data(NfcDevice* dev, string_t path) {
parsed = true;
} while(false);
if(!parsed) {
if((!parsed) && (show_dialog)) {
if(deprecated_version) {
dialog_message_show_storage_error(dev->dialogs, "File format deprecated");
} else {
@@ -900,13 +900,13 @@ static bool nfc_device_load_data(NfcDevice* dev, string_t path) {
return parsed;
}
bool nfc_device_load(NfcDevice* dev, const char* file_path) {
bool nfc_device_load(NfcDevice* dev, const char* file_path, bool show_dialog) {
furi_assert(dev);
furi_assert(file_path);
// Load device data
string_set_str(dev->load_path, file_path);
bool dev_load = nfc_device_load_data(dev, dev->load_path);
bool dev_load = nfc_device_load_data(dev, dev->load_path, show_dialog);
if(dev_load) {
// Set device name
string_t filename;
@@ -933,7 +933,7 @@ bool nfc_file_select(NfcDevice* dev) {
string_init(filename);
path_extract_filename(dev->load_path, filename, true);
strncpy(dev->dev_name, string_get_cstr(filename), NFC_DEV_NAME_MAX_LEN);
res = nfc_device_load_data(dev, dev->load_path);
res = nfc_device_load_data(dev, dev->load_path, true);
if(res) {
nfc_device_set_name(dev, dev->dev_name);
}
@@ -1017,7 +1017,7 @@ bool nfc_device_restore(NfcDevice* dev, bool use_load_path) {
} else {
string_printf(path, "%s/%s%s", NFC_APP_FOLDER, dev->dev_name, NFC_APP_EXTENSION);
}
if(!nfc_device_load_data(dev, path)) break;
if(!nfc_device_load_data(dev, path, true)) break;
restored = true;
} while(0);

View File

@@ -71,7 +71,7 @@ bool nfc_device_save(NfcDevice* dev, const char* dev_name);
bool nfc_device_save_shadow(NfcDevice* dev, const char* dev_name);
bool nfc_device_load(NfcDevice* dev, const char* file_path);
bool nfc_device_load(NfcDevice* dev, const char* file_path, bool show_dialog);
bool nfc_file_select(NfcDevice* dev);

View File

@@ -29,10 +29,18 @@
#include <nfc/scenes/nfc_scene.h>
#include <nfc/helpers/nfc_custom_event.h>
#include "rpc/rpc_app.h"
#define NFC_SEND_NOTIFICATION_FALSE (0UL)
#define NFC_SEND_NOTIFICATION_TRUE (1UL)
#define NFC_TEXT_STORE_SIZE 128
typedef enum {
NfcRpcStateIdle,
NfcRpcStateEmulating,
NfcRpcStateEmulated,
} NfcRpcState;
// Forward declaration due to circular dependency
typedef struct NfcGenerator NfcGenerator;
@@ -48,6 +56,9 @@ struct Nfc {
char text_store[NFC_TEXT_STORE_SIZE + 1];
string_t text_box_store;
void* rpc_ctx;
NfcRpcState rpc_state;
// Common Views
Submenu* submenu;
DialogEx* dialog_ex;
@@ -85,3 +96,5 @@ void nfc_text_store_clear(Nfc* nfc);
void nfc_blink_start(Nfc* nfc);
void nfc_blink_stop(Nfc* nfc);
void nfc_rpc_exit_callback(Nfc* nfc);

View File

@@ -37,4 +37,5 @@ ADD_SCENE(nfc, read_mifare_classic, ReadMifareClassic)
ADD_SCENE(nfc, emulate_mifare_classic, EmulateMifareClassic)
ADD_SCENE(nfc, mifare_classic_menu, MifareClassicMenu)
ADD_SCENE(nfc, dict_not_found, DictNotFound)
ADD_SCENE(nfc, rpc, Rpc)
ADD_SCENE(nfc, generate_info, GenerateInfo)

View File

@@ -0,0 +1,34 @@
#include "../nfc_i.h"
void nfc_scene_rpc_on_enter(void* context) {
Nfc* nfc = context;
Widget* widget = nfc->widget;
widget_add_text_box_element(
widget, 0, 0, 128, 28, AlignCenter, AlignCenter, "RPC mode", false);
notification_message(nfc->notifications, &sequence_display_backlight_on);
view_dispatcher_switch_to_view(nfc->view_dispatcher, NfcViewWidget);
}
bool nfc_scene_rpc_on_event(void* context, SceneManagerEvent event) {
Nfc* nfc = context;
bool consumed = false;
if(event.type == SceneManagerEventTypeCustom) {
consumed = true;
if(event.event == NfcCustomEventViewExit) {
view_dispatcher_stop(nfc->view_dispatcher);
}
}
return consumed;
}
void nfc_scene_rpc_on_exit(void* context) {
Nfc* nfc = context;
nfc_rpc_exit_callback(nfc);
widget_reset(nfc->widget);
}