2021-10-13 15:39:37 +00:00
|
|
|
#include "flipper.pb.h"
|
2022-07-20 10:56:33 +00:00
|
|
|
#include <core/record.h>
|
2021-10-13 15:39:37 +00:00
|
|
|
#include "rpc_i.h"
|
|
|
|
#include <furi.h>
|
|
|
|
#include <loader/loader.h>
|
2022-07-04 13:09:46 +00:00
|
|
|
#include "rpc_app.h"
|
2021-10-13 15:39:37 +00:00
|
|
|
|
2022-04-14 16:41:15 +00:00
|
|
|
#define TAG "RpcSystemApp"
|
2022-07-04 13:09:46 +00:00
|
|
|
#define APP_BUTTON_TIMEOUT 1000
|
|
|
|
|
|
|
|
struct RpcAppSystem {
|
|
|
|
RpcSession* session;
|
|
|
|
RpcAppSystemCallback app_callback;
|
|
|
|
void* app_context;
|
2022-07-20 10:56:33 +00:00
|
|
|
FuriTimer* timer;
|
2022-07-04 13:09:46 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static void rpc_system_app_timer_callback(void* context) {
|
|
|
|
furi_assert(context);
|
|
|
|
RpcAppSystem* rpc_app = context;
|
|
|
|
|
|
|
|
if(rpc_app->app_callback) {
|
|
|
|
rpc_app->app_callback(RpcAppEventButtonRelease, NULL, rpc_app->app_context);
|
|
|
|
}
|
|
|
|
}
|
2022-04-14 16:41:15 +00:00
|
|
|
|
2022-01-12 13:12:40 +00:00
|
|
|
static void rpc_system_app_start_process(const PB_Main* request, void* context) {
|
2021-10-13 15:39:37 +00:00
|
|
|
furi_assert(request);
|
2022-07-04 13:09:46 +00:00
|
|
|
furi_assert(context);
|
|
|
|
|
2021-11-01 16:26:37 +00:00
|
|
|
furi_assert(request->which_content == PB_Main_app_start_request_tag);
|
2022-07-04 13:09:46 +00:00
|
|
|
RpcAppSystem* rpc_app = context;
|
|
|
|
RpcSession* session = rpc_app->session;
|
2022-03-16 08:52:11 +00:00
|
|
|
furi_assert(session);
|
2022-07-04 13:09:46 +00:00
|
|
|
char args_temp[16];
|
2022-03-16 08:52:11 +00:00
|
|
|
|
2022-04-14 16:41:15 +00:00
|
|
|
FURI_LOG_D(TAG, "Start");
|
|
|
|
|
2021-10-13 15:39:37 +00:00
|
|
|
PB_CommandStatus result = PB_CommandStatus_ERROR_APP_CANT_START;
|
|
|
|
|
|
|
|
Loader* loader = furi_record_open("loader");
|
2021-11-01 16:26:37 +00:00
|
|
|
const char* app_name = request->content.app_start_request.name;
|
2021-10-13 15:39:37 +00:00
|
|
|
if(app_name) {
|
2021-11-01 16:26:37 +00:00
|
|
|
const char* app_args = request->content.app_start_request.args;
|
2022-07-04 13:09:46 +00:00
|
|
|
if(strcmp(app_args, "RPC") == 0) {
|
|
|
|
// If app is being started in RPC mode - pass RPC context via args string
|
|
|
|
snprintf(args_temp, 16, "RPC %08lX", (uint32_t)rpc_app);
|
|
|
|
app_args = args_temp;
|
|
|
|
}
|
2021-10-13 15:39:37 +00:00
|
|
|
LoaderStatus status = loader_start(loader, app_name, app_args);
|
|
|
|
if(status == LoaderStatusErrorAppStarted) {
|
|
|
|
result = PB_CommandStatus_ERROR_APP_SYSTEM_LOCKED;
|
|
|
|
} else if(status == LoaderStatusErrorInternal) {
|
|
|
|
result = PB_CommandStatus_ERROR_APP_CANT_START;
|
|
|
|
} else if(status == LoaderStatusErrorUnknownApp) {
|
|
|
|
result = PB_CommandStatus_ERROR_INVALID_PARAMETERS;
|
|
|
|
} else if(status == LoaderStatusOk) {
|
|
|
|
result = PB_CommandStatus_OK;
|
|
|
|
} else {
|
|
|
|
furi_assert(0);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
result = PB_CommandStatus_ERROR_INVALID_PARAMETERS;
|
|
|
|
}
|
|
|
|
|
|
|
|
furi_record_close("loader");
|
|
|
|
|
2022-03-16 08:52:11 +00:00
|
|
|
rpc_send_and_release_empty(session, request->command_id, result);
|
2021-10-13 15:39:37 +00:00
|
|
|
}
|
|
|
|
|
2022-01-12 13:12:40 +00:00
|
|
|
static void rpc_system_app_lock_status_process(const PB_Main* request, void* context) {
|
2021-10-13 15:39:37 +00:00
|
|
|
furi_assert(request);
|
2022-07-04 13:09:46 +00:00
|
|
|
furi_assert(context);
|
|
|
|
|
2021-10-13 15:39:37 +00:00
|
|
|
furi_assert(request->which_content == PB_Main_app_lock_status_request_tag);
|
2022-07-04 13:09:46 +00:00
|
|
|
RpcAppSystem* rpc_app = context;
|
|
|
|
RpcSession* session = rpc_app->session;
|
2022-03-16 08:52:11 +00:00
|
|
|
furi_assert(session);
|
2021-10-13 15:39:37 +00:00
|
|
|
|
2022-04-14 16:41:15 +00:00
|
|
|
FURI_LOG_D(TAG, "LockStatus");
|
|
|
|
|
2021-10-13 15:39:37 +00:00
|
|
|
Loader* loader = furi_record_open("loader");
|
|
|
|
|
|
|
|
PB_Main response = {
|
|
|
|
.has_next = false,
|
|
|
|
.command_status = PB_CommandStatus_OK,
|
|
|
|
.command_id = request->command_id,
|
|
|
|
.which_content = PB_Main_app_lock_status_response_tag,
|
|
|
|
};
|
|
|
|
|
|
|
|
response.content.app_lock_status_response.locked = loader_is_locked(loader);
|
|
|
|
|
|
|
|
furi_record_close("loader");
|
|
|
|
|
2022-03-16 08:52:11 +00:00
|
|
|
rpc_send_and_release(session, &response);
|
2021-10-26 16:05:28 +00:00
|
|
|
pb_release(&PB_Main_msg, &response);
|
2021-10-13 15:39:37 +00:00
|
|
|
}
|
|
|
|
|
2022-07-04 13:09:46 +00:00
|
|
|
static void rpc_system_app_exit(const PB_Main* request, void* context) {
|
|
|
|
furi_assert(request);
|
|
|
|
furi_assert(context);
|
|
|
|
|
|
|
|
furi_assert(request->which_content == PB_Main_app_exit_request_tag);
|
|
|
|
RpcAppSystem* rpc_app = context;
|
|
|
|
RpcSession* session = rpc_app->session;
|
|
|
|
furi_assert(session);
|
|
|
|
|
|
|
|
PB_CommandStatus status;
|
|
|
|
|
|
|
|
if(rpc_app->app_callback) {
|
|
|
|
if(rpc_app->app_callback(RpcAppEventAppExit, NULL, rpc_app->app_context)) {
|
|
|
|
status = PB_CommandStatus_OK;
|
2022-07-20 10:56:33 +00:00
|
|
|
furi_timer_stop(rpc_app->timer);
|
2022-07-04 13:09:46 +00:00
|
|
|
} else {
|
|
|
|
status = PB_CommandStatus_ERROR_APP_CMD_ERROR;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
status = PB_CommandStatus_ERROR_APP_NOT_RUNNING;
|
|
|
|
}
|
|
|
|
|
|
|
|
rpc_send_and_release_empty(session, request->command_id, status);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void rpc_system_app_load_file(const PB_Main* request, void* context) {
|
|
|
|
furi_assert(request);
|
|
|
|
furi_assert(context);
|
|
|
|
|
|
|
|
furi_assert(request->which_content == PB_Main_app_load_file_request_tag);
|
|
|
|
RpcAppSystem* rpc_app = context;
|
|
|
|
RpcSession* session = rpc_app->session;
|
|
|
|
furi_assert(session);
|
|
|
|
|
|
|
|
PB_CommandStatus status;
|
|
|
|
if(rpc_app->app_callback) {
|
|
|
|
const char* file_path = request->content.app_load_file_request.path;
|
|
|
|
if(rpc_app->app_callback(RpcAppEventLoadFile, file_path, rpc_app->app_context)) {
|
|
|
|
status = PB_CommandStatus_OK;
|
|
|
|
} else {
|
|
|
|
status = PB_CommandStatus_ERROR_APP_CMD_ERROR;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
status = PB_CommandStatus_ERROR_APP_NOT_RUNNING;
|
|
|
|
}
|
|
|
|
|
|
|
|
rpc_send_and_release_empty(session, request->command_id, status);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void rpc_system_app_button_press(const PB_Main* request, void* context) {
|
|
|
|
furi_assert(request);
|
|
|
|
furi_assert(context);
|
|
|
|
|
|
|
|
furi_assert(request->which_content == PB_Main_app_button_press_request_tag);
|
|
|
|
RpcAppSystem* rpc_app = context;
|
|
|
|
RpcSession* session = rpc_app->session;
|
|
|
|
furi_assert(session);
|
|
|
|
|
|
|
|
PB_CommandStatus status;
|
|
|
|
if(rpc_app->app_callback) {
|
|
|
|
const char* args = request->content.app_button_press_request.args;
|
|
|
|
if(rpc_app->app_callback(RpcAppEventButtonPress, args, rpc_app->app_context)) {
|
|
|
|
status = PB_CommandStatus_OK;
|
2022-07-20 10:56:33 +00:00
|
|
|
furi_timer_start(rpc_app->timer, APP_BUTTON_TIMEOUT);
|
2022-07-04 13:09:46 +00:00
|
|
|
} else {
|
|
|
|
status = PB_CommandStatus_ERROR_APP_CMD_ERROR;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
status = PB_CommandStatus_ERROR_APP_NOT_RUNNING;
|
|
|
|
}
|
|
|
|
|
|
|
|
rpc_send_and_release_empty(session, request->command_id, status);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void rpc_system_app_button_release(const PB_Main* request, void* context) {
|
|
|
|
furi_assert(request);
|
|
|
|
furi_assert(request->which_content == PB_Main_app_button_release_request_tag);
|
|
|
|
furi_assert(context);
|
|
|
|
|
|
|
|
RpcAppSystem* rpc_app = context;
|
|
|
|
RpcSession* session = rpc_app->session;
|
|
|
|
furi_assert(session);
|
|
|
|
|
|
|
|
PB_CommandStatus status;
|
|
|
|
if(rpc_app->app_callback) {
|
|
|
|
if(rpc_app->app_callback(RpcAppEventButtonRelease, NULL, rpc_app->app_context)) {
|
|
|
|
status = PB_CommandStatus_OK;
|
2022-07-20 10:56:33 +00:00
|
|
|
furi_timer_stop(rpc_app->timer);
|
2022-07-04 13:09:46 +00:00
|
|
|
} else {
|
|
|
|
status = PB_CommandStatus_ERROR_APP_CMD_ERROR;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
status = PB_CommandStatus_ERROR_APP_NOT_RUNNING;
|
|
|
|
}
|
|
|
|
|
|
|
|
rpc_send_and_release_empty(session, request->command_id, status);
|
|
|
|
}
|
|
|
|
|
|
|
|
void rpc_system_app_set_callback(RpcAppSystem* rpc_app, RpcAppSystemCallback callback, void* ctx) {
|
|
|
|
furi_assert(rpc_app);
|
|
|
|
|
|
|
|
rpc_app->app_callback = callback;
|
|
|
|
rpc_app->app_context = ctx;
|
|
|
|
}
|
|
|
|
|
2022-03-16 08:52:11 +00:00
|
|
|
void* rpc_system_app_alloc(RpcSession* session) {
|
|
|
|
furi_assert(session);
|
2021-10-13 15:39:37 +00:00
|
|
|
|
2022-07-04 13:09:46 +00:00
|
|
|
RpcAppSystem* rpc_app = malloc(sizeof(RpcAppSystem));
|
|
|
|
rpc_app->session = session;
|
|
|
|
|
2022-07-20 10:56:33 +00:00
|
|
|
rpc_app->timer = furi_timer_alloc(rpc_system_app_timer_callback, FuriTimerTypeOnce, rpc_app);
|
2022-07-04 13:09:46 +00:00
|
|
|
|
2021-10-13 15:39:37 +00:00
|
|
|
RpcHandler rpc_handler = {
|
|
|
|
.message_handler = NULL,
|
|
|
|
.decode_submessage = NULL,
|
2022-07-04 13:09:46 +00:00
|
|
|
.context = rpc_app,
|
2021-10-13 15:39:37 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
rpc_handler.message_handler = rpc_system_app_start_process;
|
2022-03-16 08:52:11 +00:00
|
|
|
rpc_add_handler(session, PB_Main_app_start_request_tag, &rpc_handler);
|
2021-10-13 15:39:37 +00:00
|
|
|
|
|
|
|
rpc_handler.message_handler = rpc_system_app_lock_status_process;
|
2022-03-16 08:52:11 +00:00
|
|
|
rpc_add_handler(session, PB_Main_app_lock_status_request_tag, &rpc_handler);
|
2021-10-13 15:39:37 +00:00
|
|
|
|
2022-07-04 13:09:46 +00:00
|
|
|
rpc_handler.message_handler = rpc_system_app_exit;
|
|
|
|
rpc_add_handler(session, PB_Main_app_exit_request_tag, &rpc_handler);
|
|
|
|
|
|
|
|
rpc_handler.message_handler = rpc_system_app_load_file;
|
|
|
|
rpc_add_handler(session, PB_Main_app_load_file_request_tag, &rpc_handler);
|
|
|
|
|
|
|
|
rpc_handler.message_handler = rpc_system_app_button_press;
|
|
|
|
rpc_add_handler(session, PB_Main_app_button_press_request_tag, &rpc_handler);
|
|
|
|
|
|
|
|
rpc_handler.message_handler = rpc_system_app_button_release;
|
|
|
|
rpc_add_handler(session, PB_Main_app_button_release_request_tag, &rpc_handler);
|
|
|
|
|
|
|
|
return rpc_app;
|
|
|
|
}
|
|
|
|
|
|
|
|
void rpc_system_app_free(void* context) {
|
|
|
|
RpcAppSystem* rpc_app = context;
|
|
|
|
RpcSession* session = rpc_app->session;
|
|
|
|
furi_assert(session);
|
|
|
|
|
2022-07-20 10:56:33 +00:00
|
|
|
furi_timer_free(rpc_app->timer);
|
2022-07-04 13:09:46 +00:00
|
|
|
|
|
|
|
if(rpc_app->app_callback) {
|
|
|
|
rpc_app->app_callback(RpcAppEventSessionClose, NULL, rpc_app->app_context);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(rpc_app);
|
2021-10-13 15:39:37 +00:00
|
|
|
}
|