2021-10-26 16:05:28 +00:00
|
|
|
#include "rpc_i.h"
|
|
|
|
#include <pb.h>
|
|
|
|
#include <pb_decode.h>
|
|
|
|
#include <pb_encode.h>
|
|
|
|
#include <status.pb.h>
|
|
|
|
#include <storage.pb.h>
|
|
|
|
#include <flipper.pb.h>
|
|
|
|
#include <cmsis_os.h>
|
|
|
|
#include <cmsis_os2.h>
|
|
|
|
#include <portmacro.h>
|
|
|
|
#include <furi.h>
|
2021-11-12 13:04:35 +00:00
|
|
|
|
2021-10-26 16:05:28 +00:00
|
|
|
#include <cli/cli.h>
|
2021-10-12 11:48:34 +00:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stream_buffer.h>
|
2021-10-26 16:05:28 +00:00
|
|
|
#include <m-string.h>
|
2021-10-12 11:48:34 +00:00
|
|
|
#include <m-dict.h>
|
|
|
|
|
2021-11-12 13:04:35 +00:00
|
|
|
#define TAG "RpcSrv"
|
2021-10-12 11:48:34 +00:00
|
|
|
|
|
|
|
#define RPC_EVENT_NEW_DATA (1 << 0)
|
|
|
|
#define RPC_EVENT_DISCONNECT (1 << 1)
|
|
|
|
#define RPC_EVENTS_ALL (RPC_EVENT_DISCONNECT | RPC_EVENT_NEW_DATA)
|
|
|
|
|
|
|
|
DICT_DEF2(RpcHandlerDict, pb_size_t, M_DEFAULT_OPLIST, RpcHandler, M_POD_OPLIST)
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
RpcSystemAlloc alloc;
|
|
|
|
RpcSystemFree free;
|
|
|
|
void* context;
|
|
|
|
} RpcSystemCallbacks;
|
|
|
|
|
|
|
|
static RpcSystemCallbacks rpc_systems[] = {
|
|
|
|
{
|
|
|
|
.alloc = rpc_system_status_alloc,
|
|
|
|
.free = NULL,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.alloc = rpc_system_storage_alloc,
|
|
|
|
.free = rpc_system_storage_free,
|
|
|
|
},
|
2021-10-13 15:39:37 +00:00
|
|
|
{
|
|
|
|
.alloc = rpc_system_app_alloc,
|
|
|
|
.free = NULL,
|
|
|
|
},
|
2021-11-01 16:26:37 +00:00
|
|
|
{
|
|
|
|
.alloc = rpc_system_gui_alloc,
|
|
|
|
.free = rpc_system_gui_free,
|
|
|
|
},
|
2021-10-12 11:48:34 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct RpcSession {
|
|
|
|
RpcSendBytesCallback send_bytes_callback;
|
2021-11-08 19:41:40 +00:00
|
|
|
RpcBufferIsEmptyCallback buffer_is_empty_callback;
|
2021-10-26 16:05:28 +00:00
|
|
|
RpcSessionClosedCallback closed_callback;
|
|
|
|
void* context;
|
|
|
|
osMutexId_t callbacks_mutex;
|
2021-10-12 11:48:34 +00:00
|
|
|
Rpc* rpc;
|
2021-10-26 16:05:28 +00:00
|
|
|
bool terminate;
|
2021-10-12 11:48:34 +00:00
|
|
|
void** system_contexts;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Rpc {
|
|
|
|
bool busy;
|
|
|
|
osMutexId_t busy_mutex;
|
|
|
|
RpcSession session;
|
|
|
|
osEventFlagsId_t events;
|
|
|
|
StreamBufferHandle_t stream;
|
|
|
|
RpcHandlerDict_t handlers;
|
|
|
|
PB_Main* decoded_message;
|
|
|
|
};
|
|
|
|
|
|
|
|
static bool content_callback(pb_istream_t* stream, const pb_field_t* field, void** arg);
|
|
|
|
|
2021-10-26 16:05:28 +00:00
|
|
|
static void rpc_close_session_process(const PB_Main* msg_request, void* context) {
|
|
|
|
furi_assert(msg_request);
|
|
|
|
furi_assert(context);
|
|
|
|
|
|
|
|
Rpc* rpc = context;
|
|
|
|
rpc_send_and_release_empty(rpc, msg_request->command_id, PB_CommandStatus_OK);
|
|
|
|
|
|
|
|
osMutexAcquire(rpc->session.callbacks_mutex, osWaitForever);
|
|
|
|
if(rpc->session.closed_callback) {
|
|
|
|
rpc->session.closed_callback(rpc->session.context);
|
|
|
|
}
|
|
|
|
osMutexRelease(rpc->session.callbacks_mutex);
|
|
|
|
}
|
|
|
|
|
2021-10-13 15:39:37 +00:00
|
|
|
static size_t rpc_sprintf_msg_file(
|
|
|
|
string_t str,
|
2021-10-12 11:48:34 +00:00
|
|
|
const char* prefix,
|
|
|
|
const PB_Storage_File* msg_file,
|
|
|
|
size_t msg_files_size) {
|
|
|
|
size_t cnt = 0;
|
|
|
|
|
|
|
|
for(int i = 0; i < msg_files_size; ++i, ++msg_file) {
|
2021-10-13 15:39:37 +00:00
|
|
|
string_cat_printf(
|
|
|
|
str,
|
2021-10-12 11:48:34 +00:00
|
|
|
"%s[%c] size: %5ld",
|
|
|
|
prefix,
|
|
|
|
msg_file->type == PB_Storage_File_FileType_DIR ? 'd' : 'f',
|
|
|
|
msg_file->size);
|
|
|
|
|
|
|
|
if(msg_file->name) {
|
2021-10-13 15:39:37 +00:00
|
|
|
string_cat_printf(str, " \'%s\'", msg_file->name);
|
2021-10-12 11:48:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(msg_file->data && msg_file->data->size) {
|
2021-10-13 15:39:37 +00:00
|
|
|
string_cat_printf(
|
|
|
|
str,
|
2021-10-12 11:48:34 +00:00
|
|
|
" (%d):\'%.*s%s\'",
|
|
|
|
msg_file->data->size,
|
|
|
|
MIN(msg_file->data->size, 30),
|
|
|
|
msg_file->data->bytes,
|
|
|
|
msg_file->data->size > 30 ? "..." : "");
|
|
|
|
}
|
|
|
|
|
2021-10-13 15:39:37 +00:00
|
|
|
string_cat_printf(str, "\r\n");
|
2021-10-12 11:48:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return cnt;
|
|
|
|
}
|
|
|
|
|
2021-10-26 16:05:28 +00:00
|
|
|
void rpc_print_data(const char* prefix, uint8_t* buffer, size_t size) {
|
|
|
|
string_t str;
|
|
|
|
string_init(str);
|
|
|
|
string_reserve(str, 100 + size * 5);
|
|
|
|
|
|
|
|
string_cat_printf(str, "\r\n%s DEC(%d): {", prefix, size);
|
|
|
|
for(int i = 0; i < size; ++i) {
|
|
|
|
string_cat_printf(str, "%d, ", buffer[i]);
|
|
|
|
}
|
|
|
|
string_cat_printf(str, "}\r\n");
|
|
|
|
|
|
|
|
printf("%s", string_get_cstr(str));
|
2021-11-15 17:09:40 +00:00
|
|
|
string_reset(str);
|
2021-10-26 16:05:28 +00:00
|
|
|
string_reserve(str, 100 + size * 3);
|
|
|
|
|
|
|
|
string_cat_printf(str, "%s HEX(%d): {", prefix, size);
|
|
|
|
for(int i = 0; i < size; ++i) {
|
|
|
|
string_cat_printf(str, "%02X", buffer[i]);
|
|
|
|
}
|
|
|
|
string_cat_printf(str, "}\r\n\r\n");
|
|
|
|
|
|
|
|
printf("%s", string_get_cstr(str));
|
|
|
|
string_clear(str);
|
|
|
|
}
|
|
|
|
|
2021-10-12 11:48:34 +00:00
|
|
|
void rpc_print_message(const PB_Main* message) {
|
2021-10-13 15:39:37 +00:00
|
|
|
string_t str;
|
|
|
|
string_init(str);
|
2021-10-12 11:48:34 +00:00
|
|
|
|
2021-10-13 15:39:37 +00:00
|
|
|
string_cat_printf(
|
|
|
|
str,
|
2021-10-12 11:48:34 +00:00
|
|
|
"PB_Main: {\r\n\tresult: %d cmd_id: %ld (%s)\r\n",
|
|
|
|
message->command_status,
|
|
|
|
message->command_id,
|
|
|
|
message->has_next ? "has_next" : "last");
|
|
|
|
switch(message->which_content) {
|
|
|
|
default:
|
|
|
|
/* not implemented yet */
|
2021-10-13 15:39:37 +00:00
|
|
|
string_cat_printf(str, "\tNOT_IMPLEMENTED (%d) {\r\n", message->which_content);
|
|
|
|
break;
|
2021-10-26 16:05:28 +00:00
|
|
|
case PB_Main_stop_session_tag:
|
|
|
|
string_cat_printf(str, "\tstop_session {\r\n");
|
|
|
|
break;
|
2021-11-01 16:26:37 +00:00
|
|
|
case PB_Main_app_start_request_tag: {
|
2021-10-13 15:39:37 +00:00
|
|
|
string_cat_printf(str, "\tapp_start {\r\n");
|
2021-11-01 16:26:37 +00:00
|
|
|
const char* name = message->content.app_start_request.name;
|
|
|
|
const char* args = message->content.app_start_request.args;
|
2021-10-13 15:39:37 +00:00
|
|
|
if(name) {
|
|
|
|
string_cat_printf(str, "\t\tname: %s\r\n", name);
|
|
|
|
}
|
|
|
|
if(args) {
|
|
|
|
string_cat_printf(str, "\t\targs: %s\r\n", args);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PB_Main_app_lock_status_request_tag: {
|
|
|
|
string_cat_printf(str, "\tapp_lock_status_request {\r\n");
|
2021-10-12 11:48:34 +00:00
|
|
|
break;
|
2021-10-13 15:39:37 +00:00
|
|
|
}
|
|
|
|
case PB_Main_app_lock_status_response_tag: {
|
|
|
|
string_cat_printf(str, "\tapp_lock_status_response {\r\n");
|
|
|
|
bool lock_status = message->content.app_lock_status_response.locked;
|
|
|
|
string_cat_printf(str, "\t\tlocked: %s\r\n", lock_status ? "true" : "false");
|
|
|
|
break;
|
|
|
|
}
|
2021-10-12 11:48:34 +00:00
|
|
|
case PB_Main_storage_md5sum_request_tag: {
|
2021-10-13 15:39:37 +00:00
|
|
|
string_cat_printf(str, "\tmd5sum_request {\r\n");
|
2021-10-12 11:48:34 +00:00
|
|
|
const char* path = message->content.storage_md5sum_request.path;
|
|
|
|
if(path) {
|
2021-10-13 15:39:37 +00:00
|
|
|
string_cat_printf(str, "\t\tpath: %s\r\n", path);
|
2021-10-12 11:48:34 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PB_Main_storage_md5sum_response_tag: {
|
2021-10-13 15:39:37 +00:00
|
|
|
string_cat_printf(str, "\tmd5sum_response {\r\n");
|
2021-10-12 11:48:34 +00:00
|
|
|
const char* path = message->content.storage_md5sum_response.md5sum;
|
|
|
|
if(path) {
|
2021-10-13 15:39:37 +00:00
|
|
|
string_cat_printf(str, "\t\tmd5sum: %s\r\n", path);
|
2021-10-12 11:48:34 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PB_Main_ping_request_tag:
|
2021-10-13 15:39:37 +00:00
|
|
|
string_cat_printf(str, "\tping_request {\r\n");
|
2021-10-12 11:48:34 +00:00
|
|
|
break;
|
|
|
|
case PB_Main_ping_response_tag:
|
2021-10-13 15:39:37 +00:00
|
|
|
string_cat_printf(str, "\tping_response {\r\n");
|
2021-10-12 11:48:34 +00:00
|
|
|
break;
|
|
|
|
case PB_Main_storage_mkdir_request_tag:
|
2021-10-13 15:39:37 +00:00
|
|
|
string_cat_printf(str, "\tmkdir {\r\n");
|
2021-10-12 11:48:34 +00:00
|
|
|
break;
|
|
|
|
case PB_Main_storage_delete_request_tag: {
|
2021-10-13 15:39:37 +00:00
|
|
|
string_cat_printf(str, "\tdelete {\r\n");
|
2021-10-12 11:48:34 +00:00
|
|
|
const char* path = message->content.storage_delete_request.path;
|
|
|
|
if(path) {
|
2021-10-13 15:39:37 +00:00
|
|
|
string_cat_printf(str, "\t\tpath: %s\r\n", path);
|
2021-10-12 11:48:34 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PB_Main_empty_tag:
|
2021-10-13 15:39:37 +00:00
|
|
|
string_cat_printf(str, "\tempty {\r\n");
|
2021-10-12 11:48:34 +00:00
|
|
|
break;
|
|
|
|
case PB_Main_storage_list_request_tag: {
|
2021-10-13 15:39:37 +00:00
|
|
|
string_cat_printf(str, "\tlist_request {\r\n");
|
2021-10-12 11:48:34 +00:00
|
|
|
const char* path = message->content.storage_list_request.path;
|
|
|
|
if(path) {
|
2021-10-13 15:39:37 +00:00
|
|
|
string_cat_printf(str, "\t\tpath: %s\r\n", path);
|
2021-10-12 11:48:34 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PB_Main_storage_read_request_tag: {
|
2021-10-13 15:39:37 +00:00
|
|
|
string_cat_printf(str, "\tread_request {\r\n");
|
2021-10-12 11:48:34 +00:00
|
|
|
const char* path = message->content.storage_read_request.path;
|
|
|
|
if(path) {
|
2021-10-13 15:39:37 +00:00
|
|
|
string_cat_printf(str, "\t\tpath: %s\r\n", path);
|
2021-10-12 11:48:34 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PB_Main_storage_write_request_tag: {
|
2021-10-13 15:39:37 +00:00
|
|
|
string_cat_printf(str, "\twrite_request {\r\n");
|
2021-10-12 11:48:34 +00:00
|
|
|
const char* path = message->content.storage_write_request.path;
|
|
|
|
if(path) {
|
2021-10-13 15:39:37 +00:00
|
|
|
string_cat_printf(str, "\t\tpath: %s\r\n", path);
|
2021-10-12 11:48:34 +00:00
|
|
|
}
|
|
|
|
if(message->content.storage_write_request.has_file) {
|
|
|
|
const PB_Storage_File* msg_file = &message->content.storage_write_request.file;
|
2021-10-13 15:39:37 +00:00
|
|
|
rpc_sprintf_msg_file(str, "\t\t\t", msg_file, 1);
|
2021-10-12 11:48:34 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PB_Main_storage_read_response_tag:
|
2021-10-13 15:39:37 +00:00
|
|
|
string_cat_printf(str, "\tread_response {\r\n");
|
2021-10-12 11:48:34 +00:00
|
|
|
if(message->content.storage_read_response.has_file) {
|
|
|
|
const PB_Storage_File* msg_file = &message->content.storage_read_response.file;
|
2021-10-13 15:39:37 +00:00
|
|
|
rpc_sprintf_msg_file(str, "\t\t\t", msg_file, 1);
|
2021-10-12 11:48:34 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case PB_Main_storage_list_response_tag: {
|
|
|
|
const PB_Storage_File* msg_file = message->content.storage_list_response.file;
|
|
|
|
size_t msg_file_count = message->content.storage_list_response.file_count;
|
2021-10-13 15:39:37 +00:00
|
|
|
string_cat_printf(str, "\tlist_response {\r\n");
|
|
|
|
rpc_sprintf_msg_file(str, "\t\t", msg_file, msg_file_count);
|
2021-11-12 13:04:35 +00:00
|
|
|
break;
|
2021-10-12 11:48:34 +00:00
|
|
|
}
|
2021-11-01 16:26:37 +00:00
|
|
|
case PB_Main_gui_start_screen_stream_request_tag:
|
|
|
|
string_cat_printf(str, "\tstart_screen_stream {\r\n");
|
|
|
|
break;
|
|
|
|
case PB_Main_gui_stop_screen_stream_request_tag:
|
|
|
|
string_cat_printf(str, "\tstop_screen_stream {\r\n");
|
|
|
|
break;
|
2021-11-12 13:04:35 +00:00
|
|
|
case PB_Main_gui_screen_frame_tag:
|
|
|
|
string_cat_printf(str, "\tscreen_frame {\r\n");
|
2021-11-01 16:26:37 +00:00
|
|
|
break;
|
|
|
|
case PB_Main_gui_send_input_event_request_tag:
|
|
|
|
string_cat_printf(str, "\tsend_input_event {\r\n");
|
|
|
|
string_cat_printf(
|
|
|
|
str, "\t\tkey: %d\r\n", message->content.gui_send_input_event_request.key);
|
|
|
|
string_cat_printf(
|
|
|
|
str, "\t\type: %d\r\n", message->content.gui_send_input_event_request.type);
|
|
|
|
break;
|
2021-11-12 13:04:35 +00:00
|
|
|
case PB_Main_gui_start_virtual_display_request_tag:
|
|
|
|
string_cat_printf(str, "\tstart_virtual_display {\r\n");
|
|
|
|
break;
|
|
|
|
case PB_Main_gui_stop_virtual_display_request_tag:
|
|
|
|
string_cat_printf(str, "\tstop_virtual_display {\r\n");
|
|
|
|
break;
|
2021-10-12 11:48:34 +00:00
|
|
|
}
|
2021-10-13 15:39:37 +00:00
|
|
|
string_cat_printf(str, "\t}\r\n}\r\n");
|
|
|
|
printf("%s", string_get_cstr(str));
|
|
|
|
|
|
|
|
string_clear(str);
|
2021-10-12 11:48:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static Rpc* rpc_alloc(void) {
|
|
|
|
Rpc* rpc = furi_alloc(sizeof(Rpc));
|
|
|
|
rpc->busy_mutex = osMutexNew(NULL);
|
|
|
|
rpc->busy = false;
|
|
|
|
rpc->events = osEventFlagsNew(NULL);
|
2021-11-08 19:41:40 +00:00
|
|
|
rpc->stream = xStreamBufferCreate(RPC_BUFFER_SIZE, 1);
|
2021-10-12 11:48:34 +00:00
|
|
|
|
|
|
|
rpc->decoded_message = furi_alloc(sizeof(PB_Main));
|
|
|
|
rpc->decoded_message->cb_content.funcs.decode = content_callback;
|
|
|
|
rpc->decoded_message->cb_content.arg = rpc;
|
|
|
|
|
|
|
|
RpcHandlerDict_init(rpc->handlers);
|
|
|
|
|
|
|
|
return rpc;
|
|
|
|
}
|
|
|
|
|
2021-10-26 16:05:28 +00:00
|
|
|
RpcSession* rpc_session_open(Rpc* rpc) {
|
2021-10-12 11:48:34 +00:00
|
|
|
furi_assert(rpc);
|
|
|
|
bool result = false;
|
|
|
|
furi_check(osMutexAcquire(rpc->busy_mutex, osWaitForever) == osOK);
|
|
|
|
if(rpc->busy) {
|
|
|
|
result = false;
|
|
|
|
} else {
|
|
|
|
rpc->busy = true;
|
|
|
|
result = true;
|
|
|
|
}
|
|
|
|
furi_check(osMutexRelease(rpc->busy_mutex) == osOK);
|
|
|
|
|
|
|
|
if(result) {
|
|
|
|
RpcSession* session = &rpc->session;
|
2021-10-26 16:05:28 +00:00
|
|
|
session->callbacks_mutex = osMutexNew(NULL);
|
2021-10-12 11:48:34 +00:00
|
|
|
session->rpc = rpc;
|
2021-10-26 16:05:28 +00:00
|
|
|
session->terminate = false;
|
|
|
|
xStreamBufferReset(rpc->stream);
|
|
|
|
|
2021-10-12 11:48:34 +00:00
|
|
|
session->system_contexts = furi_alloc(COUNT_OF(rpc_systems) * sizeof(void*));
|
|
|
|
for(int i = 0; i < COUNT_OF(rpc_systems); ++i) {
|
|
|
|
session->system_contexts[i] = rpc_systems[i].alloc(rpc);
|
|
|
|
}
|
2021-10-26 16:05:28 +00:00
|
|
|
|
|
|
|
RpcHandler rpc_handler = {
|
|
|
|
.message_handler = rpc_close_session_process,
|
|
|
|
.decode_submessage = NULL,
|
|
|
|
.context = rpc,
|
|
|
|
};
|
|
|
|
rpc_add_handler(rpc, PB_Main_stop_session_tag, &rpc_handler);
|
|
|
|
|
2021-11-12 13:04:35 +00:00
|
|
|
FURI_LOG_D(TAG, "Session started\r\n");
|
2021-10-12 11:48:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result ? &rpc->session : NULL; /* support 1 open session for now */
|
|
|
|
}
|
|
|
|
|
2021-10-26 16:05:28 +00:00
|
|
|
void rpc_session_close(RpcSession* session) {
|
2021-10-12 11:48:34 +00:00
|
|
|
furi_assert(session);
|
|
|
|
furi_assert(session->rpc);
|
|
|
|
furi_assert(session->rpc->busy);
|
|
|
|
|
2021-10-26 16:05:28 +00:00
|
|
|
rpc_session_set_send_bytes_callback(session, NULL);
|
|
|
|
rpc_session_set_close_callback(session, NULL);
|
2021-10-12 11:48:34 +00:00
|
|
|
osEventFlagsSet(session->rpc->events, RPC_EVENT_DISCONNECT);
|
|
|
|
}
|
|
|
|
|
2021-10-26 16:05:28 +00:00
|
|
|
static void rpc_free_session(RpcSession* session) {
|
|
|
|
furi_assert(session);
|
|
|
|
|
|
|
|
for(int i = 0; i < COUNT_OF(rpc_systems); ++i) {
|
|
|
|
if(rpc_systems[i].free) {
|
|
|
|
rpc_systems[i].free(session->system_contexts[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free(session->system_contexts);
|
|
|
|
osMutexDelete(session->callbacks_mutex);
|
2021-11-15 17:09:40 +00:00
|
|
|
RpcHandlerDict_reset(session->rpc->handlers);
|
2021-10-26 16:05:28 +00:00
|
|
|
|
|
|
|
session->context = NULL;
|
|
|
|
session->closed_callback = NULL;
|
|
|
|
session->send_bytes_callback = NULL;
|
2021-11-08 19:41:40 +00:00
|
|
|
session->buffer_is_empty_callback = NULL;
|
2021-10-26 16:05:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void rpc_session_set_context(RpcSession* session, void* context) {
|
|
|
|
furi_assert(session);
|
|
|
|
furi_assert(session->rpc);
|
|
|
|
furi_assert(session->rpc->busy);
|
|
|
|
|
|
|
|
osMutexAcquire(session->callbacks_mutex, osWaitForever);
|
|
|
|
session->context = context;
|
|
|
|
osMutexRelease(session->callbacks_mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
void rpc_session_set_close_callback(RpcSession* session, RpcSessionClosedCallback callback) {
|
|
|
|
furi_assert(session);
|
|
|
|
furi_assert(session->rpc);
|
|
|
|
furi_assert(session->rpc->busy);
|
|
|
|
|
|
|
|
osMutexAcquire(session->callbacks_mutex, osWaitForever);
|
|
|
|
session->closed_callback = callback;
|
|
|
|
osMutexRelease(session->callbacks_mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
void rpc_session_set_send_bytes_callback(RpcSession* session, RpcSendBytesCallback callback) {
|
2021-10-12 11:48:34 +00:00
|
|
|
furi_assert(session);
|
|
|
|
furi_assert(session->rpc);
|
|
|
|
furi_assert(session->rpc->busy);
|
|
|
|
|
2021-10-26 16:05:28 +00:00
|
|
|
osMutexAcquire(session->callbacks_mutex, osWaitForever);
|
2021-10-12 11:48:34 +00:00
|
|
|
session->send_bytes_callback = callback;
|
2021-10-26 16:05:28 +00:00
|
|
|
osMutexRelease(session->callbacks_mutex);
|
2021-10-12 11:48:34 +00:00
|
|
|
}
|
|
|
|
|
2021-11-08 19:41:40 +00:00
|
|
|
void rpc_session_set_buffer_is_empty_callback(
|
|
|
|
RpcSession* session,
|
|
|
|
RpcBufferIsEmptyCallback callback) {
|
|
|
|
furi_assert(session);
|
|
|
|
furi_assert(callback);
|
|
|
|
furi_assert(session->rpc->busy);
|
|
|
|
|
|
|
|
osMutexAcquire(session->callbacks_mutex, osWaitForever);
|
|
|
|
session->buffer_is_empty_callback = callback;
|
|
|
|
osMutexRelease(session->callbacks_mutex);
|
|
|
|
}
|
|
|
|
|
2021-10-26 16:05:28 +00:00
|
|
|
/* Doesn't forbid using rpc_feed_bytes() after session close - it's safe.
|
|
|
|
* Because any bytes received in buffer will be flushed before next session.
|
|
|
|
* If bytes get into stream buffer before it's get epmtified and this
|
|
|
|
* command is gets processed - it's safe either. But case of it is quite
|
|
|
|
* odd: client sends close request and sends command after.
|
|
|
|
*/
|
2021-10-12 11:48:34 +00:00
|
|
|
size_t
|
2021-10-26 16:05:28 +00:00
|
|
|
rpc_session_feed(RpcSession* session, uint8_t* encoded_bytes, size_t size, TickType_t timeout) {
|
2021-10-12 11:48:34 +00:00
|
|
|
furi_assert(session);
|
|
|
|
Rpc* rpc = session->rpc;
|
|
|
|
furi_assert(rpc->busy);
|
|
|
|
|
|
|
|
size_t bytes_sent = xStreamBufferSend(rpc->stream, encoded_bytes, size, timeout);
|
|
|
|
osEventFlagsSet(rpc->events, RPC_EVENT_NEW_DATA);
|
|
|
|
|
|
|
|
return bytes_sent;
|
|
|
|
}
|
|
|
|
|
2021-11-08 19:41:40 +00:00
|
|
|
size_t rpc_session_get_available_size(RpcSession* session) {
|
|
|
|
furi_assert(session);
|
|
|
|
Rpc* rpc = session->rpc;
|
|
|
|
return xStreamBufferSpacesAvailable(rpc->stream);
|
|
|
|
}
|
|
|
|
|
2021-10-12 11:48:34 +00:00
|
|
|
bool rpc_pb_stream_read(pb_istream_t* istream, pb_byte_t* buf, size_t count) {
|
|
|
|
Rpc* rpc = istream->state;
|
|
|
|
uint32_t flags = 0;
|
|
|
|
size_t bytes_received = 0;
|
|
|
|
|
2021-10-26 16:05:28 +00:00
|
|
|
furi_assert(istream->bytes_left);
|
|
|
|
|
2021-10-12 11:48:34 +00:00
|
|
|
while(1) {
|
|
|
|
bytes_received +=
|
|
|
|
xStreamBufferReceive(rpc->stream, buf + bytes_received, count - bytes_received, 0);
|
2021-11-08 19:41:40 +00:00
|
|
|
if(xStreamBufferIsEmpty(rpc->stream)) {
|
|
|
|
if(rpc->session.buffer_is_empty_callback) {
|
|
|
|
rpc->session.buffer_is_empty_callback(rpc->session.context);
|
|
|
|
}
|
|
|
|
}
|
2021-10-12 11:48:34 +00:00
|
|
|
if(count == bytes_received) {
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
flags = osEventFlagsWait(rpc->events, RPC_EVENTS_ALL, 0, osWaitForever);
|
|
|
|
if(flags & RPC_EVENT_DISCONNECT) {
|
|
|
|
if(xStreamBufferIsEmpty(rpc->stream)) {
|
2021-10-26 16:05:28 +00:00
|
|
|
rpc->session.terminate = true;
|
|
|
|
istream->bytes_left = 0;
|
|
|
|
bytes_received = 0;
|
2021-10-12 11:48:34 +00:00
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
/* Save disconnect flag and continue reading buffer */
|
|
|
|
osEventFlagsSet(rpc->events, RPC_EVENT_DISCONNECT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-12 13:04:35 +00:00
|
|
|
#if SRV_RPC_DEBUG
|
2021-10-26 16:05:28 +00:00
|
|
|
rpc_print_data("INPUT", buf, bytes_received);
|
|
|
|
#endif
|
|
|
|
|
2021-10-12 11:48:34 +00:00
|
|
|
return (count == bytes_received);
|
|
|
|
}
|
|
|
|
|
2021-10-26 16:05:28 +00:00
|
|
|
void rpc_send_and_release(Rpc* rpc, PB_Main* message) {
|
2021-10-12 11:48:34 +00:00
|
|
|
furi_assert(rpc);
|
2021-10-26 16:05:28 +00:00
|
|
|
furi_assert(message);
|
2021-10-12 11:48:34 +00:00
|
|
|
RpcSession* session = &rpc->session;
|
|
|
|
pb_ostream_t ostream = PB_OSTREAM_SIZING;
|
|
|
|
|
2021-11-12 13:04:35 +00:00
|
|
|
#if SRV_RPC_DEBUG
|
|
|
|
FURI_LOG_I(TAG, "OUTPUT:");
|
2021-10-27 15:20:08 +00:00
|
|
|
rpc_print_message(message);
|
2021-10-12 11:48:34 +00:00
|
|
|
#endif
|
|
|
|
|
2021-10-26 16:05:28 +00:00
|
|
|
bool result = pb_encode_ex(&ostream, &PB_Main_msg, message, PB_ENCODE_DELIMITED);
|
2021-10-12 11:48:34 +00:00
|
|
|
furi_check(result && ostream.bytes_written);
|
|
|
|
|
|
|
|
uint8_t* buffer = furi_alloc(ostream.bytes_written);
|
|
|
|
ostream = pb_ostream_from_buffer(buffer, ostream.bytes_written);
|
|
|
|
|
2021-10-26 16:05:28 +00:00
|
|
|
pb_encode_ex(&ostream, &PB_Main_msg, message, PB_ENCODE_DELIMITED);
|
2021-10-12 11:48:34 +00:00
|
|
|
|
2021-11-12 13:04:35 +00:00
|
|
|
#if SRV_RPC_DEBUG
|
2021-10-26 16:05:28 +00:00
|
|
|
rpc_print_data("OUTPUT", buffer, ostream.bytes_written);
|
|
|
|
#endif
|
2021-10-12 11:48:34 +00:00
|
|
|
|
2021-10-26 16:05:28 +00:00
|
|
|
osMutexAcquire(session->callbacks_mutex, osWaitForever);
|
|
|
|
if(session->send_bytes_callback) {
|
|
|
|
session->send_bytes_callback(session->context, buffer, ostream.bytes_written);
|
2021-10-12 11:48:34 +00:00
|
|
|
}
|
2021-10-26 16:05:28 +00:00
|
|
|
osMutexRelease(session->callbacks_mutex);
|
|
|
|
|
2021-10-12 11:48:34 +00:00
|
|
|
free(buffer);
|
2021-10-26 16:05:28 +00:00
|
|
|
pb_release(&PB_Main_msg, message);
|
2021-10-12 11:48:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool content_callback(pb_istream_t* stream, const pb_field_t* field, void** arg) {
|
|
|
|
furi_assert(stream);
|
|
|
|
Rpc* rpc = *arg;
|
|
|
|
|
|
|
|
RpcHandler* handler = RpcHandlerDict_get(rpc->handlers, field->tag);
|
|
|
|
|
|
|
|
if(handler && handler->decode_submessage) {
|
|
|
|
handler->decode_submessage(stream, field, arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t rpc_srv(void* p) {
|
|
|
|
Rpc* rpc = rpc_alloc();
|
|
|
|
furi_record_create("rpc", rpc);
|
|
|
|
|
2021-10-26 16:05:28 +00:00
|
|
|
Cli* cli = furi_record_open("cli");
|
|
|
|
|
|
|
|
cli_add_command(
|
2021-10-27 15:20:08 +00:00
|
|
|
cli, "start_rpc_session", CliCommandFlagParallelSafe, rpc_cli_command_start_session, rpc);
|
2021-10-26 16:05:28 +00:00
|
|
|
|
2021-10-12 11:48:34 +00:00
|
|
|
while(1) {
|
|
|
|
pb_istream_t istream = {
|
|
|
|
.callback = rpc_pb_stream_read,
|
|
|
|
.state = rpc,
|
|
|
|
.errmsg = NULL,
|
2021-11-12 13:04:35 +00:00
|
|
|
.bytes_left = RPC_MAX_MESSAGE_SIZE, /* max incoming message size */
|
2021-10-12 11:48:34 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if(pb_decode_ex(&istream, &PB_Main_msg, rpc->decoded_message, PB_DECODE_DELIMITED)) {
|
2021-11-12 13:04:35 +00:00
|
|
|
#if SRV_RPC_DEBUG
|
|
|
|
FURI_LOG_I(TAG, "INPUT:");
|
2021-10-12 11:48:34 +00:00
|
|
|
rpc_print_message(rpc->decoded_message);
|
|
|
|
#endif
|
|
|
|
RpcHandler* handler =
|
|
|
|
RpcHandlerDict_get(rpc->handlers, rpc->decoded_message->which_content);
|
|
|
|
|
|
|
|
if(handler && handler->message_handler) {
|
|
|
|
handler->message_handler(rpc->decoded_message, handler->context);
|
2021-10-26 16:05:28 +00:00
|
|
|
} else if(!handler && !rpc->session.terminate) {
|
2021-11-12 13:04:35 +00:00
|
|
|
FURI_LOG_E(TAG, "Unhandled message, tag: %d", rpc->decoded_message->which_content);
|
2021-10-12 11:48:34 +00:00
|
|
|
}
|
|
|
|
} else {
|
2021-10-26 16:05:28 +00:00
|
|
|
xStreamBufferReset(rpc->stream);
|
|
|
|
if(!rpc->session.terminate) {
|
2021-11-12 13:04:35 +00:00
|
|
|
FURI_LOG_E(TAG, "Decode failed, error: \'%.128s\'", PB_GET_ERROR(&istream));
|
2021-10-12 11:48:34 +00:00
|
|
|
}
|
|
|
|
}
|
2021-10-26 16:05:28 +00:00
|
|
|
|
|
|
|
pb_release(&PB_Main_msg, rpc->decoded_message);
|
|
|
|
|
|
|
|
if(rpc->session.terminate) {
|
2021-11-12 13:04:35 +00:00
|
|
|
FURI_LOG_D(TAG, "Session terminated");
|
2021-10-26 16:05:28 +00:00
|
|
|
osEventFlagsClear(rpc->events, RPC_EVENTS_ALL);
|
|
|
|
rpc_free_session(&rpc->session);
|
|
|
|
rpc->busy = false;
|
|
|
|
}
|
2021-10-12 11:48:34 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void rpc_add_handler(Rpc* rpc, pb_size_t message_tag, RpcHandler* handler) {
|
|
|
|
furi_assert(RpcHandlerDict_get(rpc->handlers, message_tag) == NULL);
|
|
|
|
|
|
|
|
RpcHandlerDict_set_at(rpc->handlers, message_tag, *handler);
|
|
|
|
}
|
|
|
|
|
2021-10-26 16:05:28 +00:00
|
|
|
void rpc_send_and_release_empty(Rpc* rpc, uint32_t command_id, PB_CommandStatus status) {
|
2021-10-12 11:48:34 +00:00
|
|
|
PB_Main message = {
|
|
|
|
.command_id = command_id,
|
|
|
|
.command_status = status,
|
|
|
|
.has_next = false,
|
|
|
|
.which_content = PB_Main_empty_tag,
|
|
|
|
};
|
2021-10-26 16:05:28 +00:00
|
|
|
rpc_send_and_release(rpc, &message);
|
2021-10-12 11:48:34 +00:00
|
|
|
pb_release(&PB_Main_msg, &message);
|
|
|
|
}
|