[FL-1955] CLI RPC (#781)
- RPC: added CLI command to start session - all input bytes goes into RPC, all RPC output goes into VCP - RPC: added command to close session (actually it only notifies transport layer) - RPC: added recursive rmdir - RPC: hard-coded listing for root directory (any, ext, int) - Fixed CLI leak - Fixed furi_record_delete leak - Unit tests: repaired - Unit tests: corrected output - remove excess, change dots with progress spinner - Unit tests: added leak check - Unit tests: SD mount check before start Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
This commit is contained in:
@@ -1,22 +1,20 @@
|
||||
#include "cmsis_os.h"
|
||||
#include "cmsis_os2.h"
|
||||
#include "flipper.pb.h"
|
||||
#include "furi-hal-delay.h"
|
||||
#include "furi/check.h"
|
||||
#include "furi/log.h"
|
||||
#include <m-string.h>
|
||||
#include "pb.h"
|
||||
#include "pb_decode.h"
|
||||
#include "pb_encode.h"
|
||||
#include "portmacro.h"
|
||||
#include "status.pb.h"
|
||||
#include "storage.pb.h"
|
||||
#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>
|
||||
#include <cli/cli.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <furi.h>
|
||||
#include <stream_buffer.h>
|
||||
#include <m-string.h>
|
||||
#include <m-dict.h>
|
||||
#include "rpc_i.h"
|
||||
|
||||
#define RPC_TAG "RPC"
|
||||
|
||||
@@ -51,10 +49,11 @@ static RpcSystemCallbacks rpc_systems[] = {
|
||||
|
||||
struct RpcSession {
|
||||
RpcSendBytesCallback send_bytes_callback;
|
||||
void* send_bytes_context;
|
||||
osMutexId_t send_bytes_mutex;
|
||||
RpcSessionClosedCallback closed_callback;
|
||||
void* context;
|
||||
osMutexId_t callbacks_mutex;
|
||||
Rpc* rpc;
|
||||
bool terminate_session;
|
||||
bool terminate;
|
||||
void** system_contexts;
|
||||
};
|
||||
|
||||
@@ -70,6 +69,20 @@ struct Rpc {
|
||||
|
||||
static bool content_callback(pb_istream_t* stream, const pb_field_t* field, void** arg);
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
static size_t rpc_sprintf_msg_file(
|
||||
string_t str,
|
||||
const char* prefix,
|
||||
@@ -105,6 +118,31 @@ static size_t rpc_sprintf_msg_file(
|
||||
return cnt;
|
||||
}
|
||||
|
||||
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));
|
||||
string_clean(str);
|
||||
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);
|
||||
}
|
||||
|
||||
void rpc_print_message(const PB_Main* message) {
|
||||
string_t str;
|
||||
string_init(str);
|
||||
@@ -120,6 +158,9 @@ void rpc_print_message(const PB_Main* message) {
|
||||
/* not implemented yet */
|
||||
string_cat_printf(str, "\tNOT_IMPLEMENTED (%d) {\r\n", message->which_content);
|
||||
break;
|
||||
case PB_Main_stop_session_tag:
|
||||
string_cat_printf(str, "\tstop_session {\r\n");
|
||||
break;
|
||||
case PB_Main_app_start_tag: {
|
||||
string_cat_printf(str, "\tapp_start {\r\n");
|
||||
const char* name = message->content.app_start.name;
|
||||
@@ -242,7 +283,7 @@ static Rpc* rpc_alloc(void) {
|
||||
return rpc;
|
||||
}
|
||||
|
||||
RpcSession* rpc_open_session(Rpc* rpc) {
|
||||
RpcSession* rpc_session_open(Rpc* rpc) {
|
||||
furi_assert(rpc);
|
||||
bool result = false;
|
||||
furi_check(osMutexAcquire(rpc->busy_mutex, osWaitForever) == osOK);
|
||||
@@ -256,41 +297,94 @@ RpcSession* rpc_open_session(Rpc* rpc) {
|
||||
|
||||
if(result) {
|
||||
RpcSession* session = &rpc->session;
|
||||
session->send_bytes_mutex = osMutexNew(NULL);
|
||||
session->callbacks_mutex = osMutexNew(NULL);
|
||||
session->rpc = rpc;
|
||||
session->terminate_session = false;
|
||||
session->terminate = false;
|
||||
xStreamBufferReset(rpc->stream);
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
FURI_LOG_D(RPC_TAG, "Session started\r\n");
|
||||
}
|
||||
|
||||
return result ? &rpc->session : NULL; /* support 1 open session for now */
|
||||
}
|
||||
|
||||
void rpc_close_session(RpcSession* session) {
|
||||
void rpc_session_close(RpcSession* session) {
|
||||
furi_assert(session);
|
||||
furi_assert(session->rpc);
|
||||
furi_assert(session->rpc->busy);
|
||||
|
||||
rpc_set_send_bytes_callback(session, NULL, NULL);
|
||||
rpc_session_set_send_bytes_callback(session, NULL);
|
||||
rpc_session_set_close_callback(session, NULL);
|
||||
osEventFlagsSet(session->rpc->events, RPC_EVENT_DISCONNECT);
|
||||
}
|
||||
|
||||
void rpc_set_send_bytes_callback(RpcSession* session, RpcSendBytesCallback callback, void* context) {
|
||||
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);
|
||||
RpcHandlerDict_clean(session->rpc->handlers);
|
||||
|
||||
session->context = NULL;
|
||||
session->closed_callback = NULL;
|
||||
session->send_bytes_callback = NULL;
|
||||
}
|
||||
|
||||
void rpc_session_set_context(RpcSession* session, void* context) {
|
||||
furi_assert(session);
|
||||
furi_assert(session->rpc);
|
||||
furi_assert(session->rpc->busy);
|
||||
|
||||
osMutexAcquire(session->send_bytes_mutex, osWaitForever);
|
||||
session->send_bytes_callback = callback;
|
||||
session->send_bytes_context = context;
|
||||
osMutexRelease(session->send_bytes_mutex);
|
||||
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) {
|
||||
furi_assert(session);
|
||||
furi_assert(session->rpc);
|
||||
furi_assert(session->rpc->busy);
|
||||
|
||||
osMutexAcquire(session->callbacks_mutex, osWaitForever);
|
||||
session->send_bytes_callback = callback;
|
||||
osMutexRelease(session->callbacks_mutex);
|
||||
}
|
||||
|
||||
/* 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.
|
||||
*/
|
||||
size_t
|
||||
rpc_feed_bytes(RpcSession* session, uint8_t* encoded_bytes, size_t size, TickType_t timeout) {
|
||||
rpc_session_feed(RpcSession* session, uint8_t* encoded_bytes, size_t size, TickType_t timeout) {
|
||||
furi_assert(session);
|
||||
Rpc* rpc = session->rpc;
|
||||
furi_assert(rpc->busy);
|
||||
@@ -306,6 +400,8 @@ bool rpc_pb_stream_read(pb_istream_t* istream, pb_byte_t* buf, size_t count) {
|
||||
uint32_t flags = 0;
|
||||
size_t bytes_received = 0;
|
||||
|
||||
furi_assert(istream->bytes_left);
|
||||
|
||||
while(1) {
|
||||
bytes_received +=
|
||||
xStreamBufferReceive(rpc->stream, buf + bytes_received, count - bytes_received, 0);
|
||||
@@ -315,7 +411,9 @@ bool rpc_pb_stream_read(pb_istream_t* istream, pb_byte_t* buf, size_t count) {
|
||||
flags = osEventFlagsWait(rpc->events, RPC_EVENTS_ALL, 0, osWaitForever);
|
||||
if(flags & RPC_EVENT_DISCONNECT) {
|
||||
if(xStreamBufferIsEmpty(rpc->stream)) {
|
||||
rpc->session.terminate_session = true;
|
||||
rpc->session.terminate = true;
|
||||
istream->bytes_left = 0;
|
||||
bytes_received = 0;
|
||||
break;
|
||||
} else {
|
||||
/* Save disconnect flag and continue reading buffer */
|
||||
@@ -325,12 +423,16 @@ bool rpc_pb_stream_read(pb_istream_t* istream, pb_byte_t* buf, size_t count) {
|
||||
}
|
||||
}
|
||||
|
||||
#if DEBUG_PRINT
|
||||
rpc_print_data("INPUT", buf, bytes_received);
|
||||
#endif
|
||||
|
||||
return (count == bytes_received);
|
||||
}
|
||||
|
||||
void rpc_encode_and_send(Rpc* rpc, PB_Main* main_message) {
|
||||
void rpc_send_and_release(Rpc* rpc, PB_Main* message) {
|
||||
furi_assert(rpc);
|
||||
furi_assert(main_message);
|
||||
furi_assert(message);
|
||||
RpcSession* session = &rpc->session;
|
||||
pb_ostream_t ostream = PB_OSTREAM_SIZING;
|
||||
|
||||
@@ -339,47 +441,26 @@ void rpc_encode_and_send(Rpc* rpc, PB_Main* main_message) {
|
||||
rpc_print_message(main_message);
|
||||
#endif
|
||||
|
||||
bool result = pb_encode_ex(&ostream, &PB_Main_msg, main_message, PB_ENCODE_DELIMITED);
|
||||
bool result = pb_encode_ex(&ostream, &PB_Main_msg, message, PB_ENCODE_DELIMITED);
|
||||
furi_check(result && ostream.bytes_written);
|
||||
|
||||
uint8_t* buffer = furi_alloc(ostream.bytes_written);
|
||||
ostream = pb_ostream_from_buffer(buffer, ostream.bytes_written);
|
||||
|
||||
pb_encode_ex(&ostream, &PB_Main_msg, main_message, PB_ENCODE_DELIMITED);
|
||||
pb_encode_ex(&ostream, &PB_Main_msg, message, PB_ENCODE_DELIMITED);
|
||||
|
||||
{
|
||||
#if DEBUG_PRINT
|
||||
string_t str;
|
||||
string_init(str);
|
||||
string_reserve(str, 100 + ostream.bytes_written * 5);
|
||||
rpc_print_data("OUTPUT", buffer, ostream.bytes_written);
|
||||
#endif
|
||||
|
||||
string_cat_printf(str, "\r\nREPONSE DEC(%d): {", ostream.bytes_written);
|
||||
for(int i = 0; i < ostream.bytes_written; ++i) {
|
||||
string_cat_printf(str, "%d, ", buffer[i]);
|
||||
}
|
||||
string_cat_printf(str, "}\r\n");
|
||||
|
||||
printf("%s", string_get_cstr(str));
|
||||
string_clean(str);
|
||||
string_reserve(str, 100 + ostream.bytes_written * 3);
|
||||
|
||||
string_cat_printf(str, "REPONSE HEX(%d): {", ostream.bytes_written);
|
||||
for(int i = 0; i < ostream.bytes_written; ++i) {
|
||||
string_cat_printf(str, "%02X", buffer[i]);
|
||||
}
|
||||
string_cat_printf(str, "}\r\n\r\n");
|
||||
|
||||
printf("%s", string_get_cstr(str));
|
||||
#endif // DEBUG_PRINT
|
||||
|
||||
osMutexAcquire(session->send_bytes_mutex, osWaitForever);
|
||||
if(session->send_bytes_callback) {
|
||||
session->send_bytes_callback(
|
||||
session->send_bytes_context, buffer, ostream.bytes_written);
|
||||
}
|
||||
osMutexRelease(session->send_bytes_mutex);
|
||||
osMutexAcquire(session->callbacks_mutex, osWaitForever);
|
||||
if(session->send_bytes_callback) {
|
||||
session->send_bytes_callback(session->context, buffer, ostream.bytes_written);
|
||||
}
|
||||
osMutexRelease(session->callbacks_mutex);
|
||||
|
||||
free(buffer);
|
||||
pb_release(&PB_Main_msg, message);
|
||||
}
|
||||
|
||||
static bool content_callback(pb_istream_t* stream, const pb_field_t* field, void** arg) {
|
||||
@@ -399,12 +480,17 @@ int32_t rpc_srv(void* p) {
|
||||
Rpc* rpc = rpc_alloc();
|
||||
furi_record_create("rpc", rpc);
|
||||
|
||||
Cli* cli = furi_record_open("cli");
|
||||
|
||||
cli_add_command(
|
||||
cli, "start_rpc_session", CliCommandFlagDefault, rpc_cli_command_start_session, rpc);
|
||||
|
||||
while(1) {
|
||||
pb_istream_t istream = {
|
||||
.callback = rpc_pb_stream_read,
|
||||
.state = rpc,
|
||||
.errmsg = NULL,
|
||||
.bytes_left = 0x7FFFFFFF,
|
||||
.bytes_left = 1024, /* max incoming message size */
|
||||
};
|
||||
|
||||
if(pb_decode_ex(&istream, &PB_Main_msg, rpc->decoded_message, PB_DECODE_DELIMITED)) {
|
||||
@@ -417,35 +503,25 @@ int32_t rpc_srv(void* p) {
|
||||
|
||||
if(handler && handler->message_handler) {
|
||||
handler->message_handler(rpc->decoded_message, handler->context);
|
||||
} else if(!handler) {
|
||||
} else if(!handler && !rpc->session.terminate) {
|
||||
FURI_LOG_E(
|
||||
RPC_TAG,
|
||||
"Unhandled message, tag: %d\r\n",
|
||||
rpc->decoded_message->which_content);
|
||||
RPC_TAG, "Unhandled message, tag: %d", rpc->decoded_message->which_content);
|
||||
}
|
||||
pb_release(&PB_Main_msg, rpc->decoded_message);
|
||||
} else {
|
||||
pb_release(&PB_Main_msg, rpc->decoded_message);
|
||||
RpcSession* session = &rpc->session;
|
||||
if(session->terminate_session) {
|
||||
session->terminate_session = false;
|
||||
osEventFlagsClear(rpc->events, RPC_EVENTS_ALL);
|
||||
FURI_LOG_D(RPC_TAG, "Session terminated\r\n");
|
||||
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->send_bytes_mutex);
|
||||
RpcHandlerDict_clean(rpc->handlers);
|
||||
rpc->busy = false;
|
||||
} else {
|
||||
xStreamBufferReset(rpc->stream);
|
||||
FURI_LOG_E(
|
||||
RPC_TAG, "Decode failed, error: \'%.128s\'\r\n", PB_GET_ERROR(&istream));
|
||||
xStreamBufferReset(rpc->stream);
|
||||
if(!rpc->session.terminate) {
|
||||
FURI_LOG_E(RPC_TAG, "Decode failed, error: \'%.128s\'", PB_GET_ERROR(&istream));
|
||||
}
|
||||
}
|
||||
|
||||
pb_release(&PB_Main_msg, rpc->decoded_message);
|
||||
|
||||
if(rpc->session.terminate) {
|
||||
FURI_LOG_D(RPC_TAG, "Session terminated");
|
||||
osEventFlagsClear(rpc->events, RPC_EVENTS_ALL);
|
||||
rpc_free_session(&rpc->session);
|
||||
rpc->busy = false;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -456,13 +532,13 @@ void rpc_add_handler(Rpc* rpc, pb_size_t message_tag, RpcHandler* handler) {
|
||||
RpcHandlerDict_set_at(rpc->handlers, message_tag, *handler);
|
||||
}
|
||||
|
||||
void rpc_encode_and_send_empty(Rpc* rpc, uint32_t command_id, PB_CommandStatus status) {
|
||||
void rpc_send_and_release_empty(Rpc* rpc, uint32_t command_id, PB_CommandStatus status) {
|
||||
PB_Main message = {
|
||||
.command_id = command_id,
|
||||
.command_status = status,
|
||||
.has_next = false,
|
||||
.which_content = PB_Main_empty_tag,
|
||||
};
|
||||
rpc_encode_and_send(rpc, &message);
|
||||
rpc_send_and_release(rpc, &message);
|
||||
pb_release(&PB_Main_msg, &message);
|
||||
}
|
||||
|
@@ -1,16 +1,79 @@
|
||||
#pragma once
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include "cmsis_os.h"
|
||||
|
||||
/** Rpc interface. Used for opening session only. */
|
||||
typedef struct Rpc Rpc;
|
||||
/** Rpc session interface */
|
||||
typedef struct RpcSession RpcSession;
|
||||
|
||||
/** Callback to send to client any data (e.g. response to command) */
|
||||
typedef void (*RpcSendBytesCallback)(void* context, uint8_t* bytes, size_t bytes_len);
|
||||
/** Callback to notify transport layer that close_session command
|
||||
* is received. Any other actions lays on transport layer.
|
||||
* No destruction or session close preformed. */
|
||||
typedef void (*RpcSessionClosedCallback)(void* context);
|
||||
|
||||
RpcSession* rpc_open_session(Rpc* rpc);
|
||||
void rpc_close_session(RpcSession* session);
|
||||
/* WARN: can't call RPC API within RpcSendBytesCallback */
|
||||
void rpc_set_send_bytes_callback(RpcSession* session, RpcSendBytesCallback callback, void* context);
|
||||
size_t
|
||||
rpc_feed_bytes(RpcSession* session, uint8_t* encoded_bytes, size_t size, TickType_t timeout);
|
||||
/** Open RPC session
|
||||
*
|
||||
* USAGE:
|
||||
* 1) rpc_session_open();
|
||||
* 2) rpc_session_set_context();
|
||||
* 3) rpc_session_set_send_bytes_callback();
|
||||
* 4) rpc_session_set_close_callback();
|
||||
* 5) while(1) {
|
||||
* rpc_session_feed();
|
||||
* }
|
||||
* 6) rpc_session_close();
|
||||
*
|
||||
*
|
||||
* @param rpc instance
|
||||
* @return pointer to RpcSession descriptor, or
|
||||
* NULL if RPC is busy and can't open session now
|
||||
*/
|
||||
RpcSession* rpc_session_open(Rpc* rpc);
|
||||
|
||||
/** Close RPC session
|
||||
* It is guaranteed that no callbacks will be called
|
||||
* as soon as session is closed. So no need in setting
|
||||
* callbacks to NULL after session close.
|
||||
*
|
||||
* @param session pointer to RpcSession descriptor
|
||||
*/
|
||||
void rpc_session_close(RpcSession* session);
|
||||
|
||||
/** Set session context for callbacks to pass
|
||||
*
|
||||
* @param session pointer to RpcSession descriptor
|
||||
* @param context context to pass to callbacks
|
||||
*/
|
||||
void rpc_session_set_context(RpcSession* session, void* context);
|
||||
|
||||
/** Set callback to send bytes to client
|
||||
* WARN: It's forbidden to call RPC API within RpcSendBytesCallback
|
||||
*
|
||||
* @param session pointer to RpcSession descriptor
|
||||
* @param callback callback to send bytes to client (can be NULL)
|
||||
*/
|
||||
void rpc_session_set_send_bytes_callback(RpcSession* session, RpcSendBytesCallback callback);
|
||||
|
||||
/** Set callback to be called when RPC command to close session is received
|
||||
* WARN: It's forbidden to call RPC API within RpcSessionClosedCallback
|
||||
*
|
||||
* @param session pointer to RpcSession descriptor
|
||||
* @param callback callback to inform about RPC close session command (can be NULL)
|
||||
*/
|
||||
void rpc_session_set_close_callback(RpcSession* session, RpcSessionClosedCallback callback);
|
||||
|
||||
/** Give bytes to RPC service to decode them and perform command
|
||||
*
|
||||
* @param session pointer to RpcSession descriptor
|
||||
* @param buffer buffer to provide to RPC service
|
||||
* @param size size of buffer
|
||||
* @param timeout max timeout to wait till all buffer will be consumed
|
||||
*
|
||||
* @return actually consumed bytes
|
||||
*/
|
||||
size_t rpc_session_feed(RpcSession* session, uint8_t* buffer, size_t size, TickType_t timeout);
|
||||
|
@@ -34,7 +34,7 @@ void rpc_system_app_start_process(const PB_Main* request, void* context) {
|
||||
|
||||
furi_record_close("loader");
|
||||
|
||||
rpc_encode_and_send_empty(rpc, request->command_id, result);
|
||||
rpc_send_and_release_empty(rpc, request->command_id, result);
|
||||
}
|
||||
|
||||
void rpc_system_app_lock_status_process(const PB_Main* request, void* context) {
|
||||
@@ -56,7 +56,8 @@ void rpc_system_app_lock_status_process(const PB_Main* request, void* context) {
|
||||
|
||||
furi_record_close("loader");
|
||||
|
||||
rpc_encode_and_send(rpc, &response);
|
||||
rpc_send_and_release(rpc, &response);
|
||||
pb_release(&PB_Main_msg, &response);
|
||||
}
|
||||
|
||||
void* rpc_system_app_alloc(Rpc* rpc) {
|
||||
|
59
applications/rpc/rpc_cli.c
Normal file
59
applications/rpc/rpc_cli.c
Normal file
@@ -0,0 +1,59 @@
|
||||
#include <cli/cli.h>
|
||||
#include <furi.h>
|
||||
#include <rpc/rpc.h>
|
||||
#include <furi-hal-vcp.h>
|
||||
|
||||
typedef struct {
|
||||
Cli* cli;
|
||||
bool session_close_request;
|
||||
} CliRpc;
|
||||
|
||||
#define CLI_READ_BUFFER_SIZE 100
|
||||
|
||||
static void rpc_send_bytes_callback(void* context, uint8_t* bytes, size_t bytes_len) {
|
||||
furi_assert(context);
|
||||
furi_assert(bytes);
|
||||
furi_assert(bytes_len);
|
||||
CliRpc* cli_rpc = context;
|
||||
|
||||
cli_write(cli_rpc->cli, bytes, bytes_len);
|
||||
}
|
||||
|
||||
static void rpc_session_close_callback(void* context) {
|
||||
furi_assert(context);
|
||||
CliRpc* cli_rpc = context;
|
||||
|
||||
cli_rpc->session_close_request = true;
|
||||
}
|
||||
|
||||
void rpc_cli_command_start_session(Cli* cli, string_t args, void* context) {
|
||||
Rpc* rpc = context;
|
||||
|
||||
RpcSession* rpc_session = rpc_session_open(rpc);
|
||||
if(rpc_session == NULL) {
|
||||
printf("Another session is in progress\r\n");
|
||||
return;
|
||||
}
|
||||
|
||||
CliRpc cli_rpc = {.cli = cli, .session_close_request = false};
|
||||
rpc_session_set_context(rpc_session, &cli_rpc);
|
||||
rpc_session_set_send_bytes_callback(rpc_session, rpc_send_bytes_callback);
|
||||
rpc_session_set_close_callback(rpc_session, rpc_session_close_callback);
|
||||
|
||||
uint8_t* buffer = furi_alloc(CLI_READ_BUFFER_SIZE);
|
||||
size_t size_received = 0;
|
||||
|
||||
while(1) {
|
||||
size_received = furi_hal_vcp_rx_with_timeout(buffer, CLI_READ_BUFFER_SIZE, 50);
|
||||
if(!furi_hal_vcp_is_connected() || cli_rpc.session_close_request) {
|
||||
break;
|
||||
}
|
||||
|
||||
if(size_received) {
|
||||
rpc_session_feed(rpc_session, buffer, size_received, 3000);
|
||||
}
|
||||
}
|
||||
|
||||
rpc_session_close(rpc_session);
|
||||
free(buffer);
|
||||
}
|
@@ -1,9 +1,10 @@
|
||||
#pragma once
|
||||
#include "rpc.h"
|
||||
#include "pb.h"
|
||||
#include "pb_decode.h"
|
||||
#include "pb_encode.h"
|
||||
#include "flipper.pb.h"
|
||||
#include <pb.h>
|
||||
#include <pb_decode.h>
|
||||
#include <pb_encode.h>
|
||||
#include <flipper.pb.h>
|
||||
#include <cli/cli.h>
|
||||
|
||||
typedef void* (*RpcSystemAlloc)(Rpc*);
|
||||
typedef void (*RpcSystemFree)(void*);
|
||||
@@ -15,8 +16,8 @@ typedef struct {
|
||||
void* context;
|
||||
} RpcHandler;
|
||||
|
||||
void rpc_encode_and_send(Rpc* rpc, PB_Main* main_message);
|
||||
void rpc_encode_and_send_empty(Rpc* rpc, uint32_t command_id, PB_CommandStatus status);
|
||||
void rpc_send_and_release(Rpc* rpc, PB_Main* main_message);
|
||||
void rpc_send_and_release_empty(Rpc* rpc, uint32_t command_id, PB_CommandStatus status);
|
||||
void rpc_add_handler(Rpc* rpc, pb_size_t message_tag, RpcHandler* handler);
|
||||
|
||||
void* rpc_system_status_alloc(Rpc* rpc);
|
||||
@@ -25,3 +26,4 @@ void rpc_system_storage_free(void* ctx);
|
||||
void* rpc_system_app_alloc(Rpc* rpc);
|
||||
|
||||
void rpc_print_message(const PB_Main* message);
|
||||
void rpc_cli_command_start_session(Cli* cli, string_t args, void* context);
|
||||
|
@@ -9,7 +9,8 @@ void rpc_system_status_ping_process(const PB_Main* msg_request, void* context) {
|
||||
msg_response.command_id = msg_request->command_id;
|
||||
msg_response.which_content = PB_Main_ping_response_tag;
|
||||
|
||||
rpc_encode_and_send(context, &msg_response);
|
||||
rpc_send_and_release(context, &msg_response);
|
||||
pb_release(&PB_Main_msg, &msg_response);
|
||||
}
|
||||
|
||||
void* rpc_system_status_alloc(Rpc* rpc) {
|
||||
|
@@ -35,7 +35,7 @@ static void rpc_system_storage_reset_state(RpcStorageSystem* rpc_storage, bool s
|
||||
|
||||
if(rpc_storage->state != RpcStorageStateIdle) {
|
||||
if(send_error) {
|
||||
rpc_encode_and_send_empty(
|
||||
rpc_send_and_release_empty(
|
||||
rpc_storage->rpc,
|
||||
rpc_storage->current_command_id,
|
||||
PB_CommandStatus_ERROR_CONTINUOUS_COMMAND_INTERRUPTED);
|
||||
@@ -96,6 +96,31 @@ static PB_CommandStatus rpc_system_storage_get_file_error(File* file) {
|
||||
return rpc_system_storage_get_error(storage_file_get_error(file));
|
||||
}
|
||||
|
||||
static void rpc_system_storage_list_root(const PB_Main* request, void* context) {
|
||||
RpcStorageSystem* rpc_storage = context;
|
||||
const char* hard_coded_dirs[] = {"any", "int", "ext"};
|
||||
|
||||
PB_Main response = {
|
||||
.has_next = false,
|
||||
.command_id = request->command_id,
|
||||
.command_status = PB_CommandStatus_OK,
|
||||
.which_content = PB_Main_storage_list_response_tag,
|
||||
};
|
||||
furi_assert(COUNT_OF(hard_coded_dirs) < COUNT_OF(response.content.storage_list_response.file));
|
||||
|
||||
for(int i = 0; i < COUNT_OF(hard_coded_dirs); ++i) {
|
||||
++response.content.storage_list_response.file_count;
|
||||
response.content.storage_list_response.file[i].data = NULL;
|
||||
response.content.storage_list_response.file[i].size = 0;
|
||||
response.content.storage_list_response.file[i].type = PB_Storage_File_FileType_DIR;
|
||||
char* str = furi_alloc(strlen(hard_coded_dirs[i]) + 1);
|
||||
strcpy(str, hard_coded_dirs[i]);
|
||||
response.content.storage_list_response.file[i].name = str;
|
||||
}
|
||||
|
||||
rpc_send_and_release(rpc_storage->rpc, &response);
|
||||
}
|
||||
|
||||
static void rpc_system_storage_list_process(const PB_Main* request, void* context) {
|
||||
furi_assert(request);
|
||||
furi_assert(context);
|
||||
@@ -104,6 +129,11 @@ static void rpc_system_storage_list_process(const PB_Main* request, void* contex
|
||||
RpcStorageSystem* rpc_storage = context;
|
||||
rpc_system_storage_reset_state(rpc_storage, true);
|
||||
|
||||
if(!strcmp(request->content.storage_list_request.path, "/")) {
|
||||
rpc_system_storage_list_root(request, context);
|
||||
return;
|
||||
}
|
||||
|
||||
Storage* fs_api = furi_record_open("storage");
|
||||
File* dir = storage_file_alloc(fs_api);
|
||||
|
||||
@@ -132,8 +162,7 @@ static void rpc_system_storage_list_process(const PB_Main* request, void* contex
|
||||
if(i == COUNT_OF(list->file)) {
|
||||
list->file_count = i;
|
||||
response.has_next = true;
|
||||
rpc_encode_and_send(rpc_storage->rpc, &response);
|
||||
pb_release(&PB_Main_msg, &response);
|
||||
rpc_send_and_release(rpc_storage->rpc, &response);
|
||||
i = 0;
|
||||
}
|
||||
list->file[i].type = (fileinfo.flags & FSF_DIRECTORY) ? PB_Storage_File_FileType_DIR :
|
||||
@@ -150,8 +179,7 @@ static void rpc_system_storage_list_process(const PB_Main* request, void* contex
|
||||
}
|
||||
|
||||
response.has_next = false;
|
||||
rpc_encode_and_send(rpc_storage->rpc, &response);
|
||||
pb_release(&PB_Main_msg, &response);
|
||||
rpc_send_and_release(rpc_storage->rpc, &response);
|
||||
|
||||
storage_dir_close(dir);
|
||||
storage_file_free(dir);
|
||||
@@ -168,9 +196,6 @@ static void rpc_system_storage_read_process(const PB_Main* request, void* contex
|
||||
|
||||
/* use same message memory to send reponse */
|
||||
PB_Main* response = furi_alloc(sizeof(PB_Main));
|
||||
response->command_id = request->command_id;
|
||||
response->which_content = PB_Main_storage_read_response_tag;
|
||||
response->command_status = PB_CommandStatus_OK;
|
||||
const char* path = request->content.storage_read_request.path;
|
||||
Storage* fs_api = furi_record_open("storage");
|
||||
File* file = storage_file_alloc(fs_api);
|
||||
@@ -178,10 +203,13 @@ static void rpc_system_storage_read_process(const PB_Main* request, void* contex
|
||||
|
||||
if(storage_file_open(file, path, FSAM_READ, FSOM_OPEN_EXISTING)) {
|
||||
size_t size_left = storage_file_size(file);
|
||||
response->content.storage_read_response.has_file = true;
|
||||
response->content.storage_read_response.file.data =
|
||||
furi_alloc(PB_BYTES_ARRAY_T_ALLOCSIZE(MIN(size_left, MAX_DATA_SIZE)));
|
||||
do {
|
||||
response->command_id = request->command_id;
|
||||
response->which_content = PB_Main_storage_read_response_tag;
|
||||
response->command_status = PB_CommandStatus_OK;
|
||||
response->content.storage_read_response.has_file = true;
|
||||
response->content.storage_read_response.file.data =
|
||||
furi_alloc(PB_BYTES_ARRAY_T_ALLOCSIZE(MIN(size_left, MAX_DATA_SIZE)));
|
||||
uint8_t* buffer = response->content.storage_read_response.file.data->bytes;
|
||||
uint16_t* read_size_msg = &response->content.storage_read_response.file.data->size;
|
||||
|
||||
@@ -192,21 +220,19 @@ static void rpc_system_storage_read_process(const PB_Main* request, void* contex
|
||||
|
||||
if(result) {
|
||||
response->has_next = (size_left > 0);
|
||||
rpc_encode_and_send(rpc_storage->rpc, response);
|
||||
// no pb_release(...);
|
||||
rpc_send_and_release(rpc_storage->rpc, response);
|
||||
}
|
||||
} while((size_left != 0) && result);
|
||||
|
||||
if(!result) {
|
||||
rpc_encode_and_send_empty(
|
||||
rpc_send_and_release_empty(
|
||||
rpc_storage->rpc, request->command_id, rpc_system_storage_get_file_error(file));
|
||||
}
|
||||
} else {
|
||||
rpc_encode_and_send_empty(
|
||||
rpc_send_and_release_empty(
|
||||
rpc_storage->rpc, request->command_id, rpc_system_storage_get_file_error(file));
|
||||
}
|
||||
|
||||
pb_release(&PB_Main_msg, response);
|
||||
free(response);
|
||||
storage_file_close(file);
|
||||
storage_file_free(file);
|
||||
@@ -245,14 +271,14 @@ static void rpc_system_storage_write_process(const PB_Main* request, void* conte
|
||||
result = (written_size == buffer_size);
|
||||
|
||||
if(result && !request->has_next) {
|
||||
rpc_encode_and_send_empty(
|
||||
rpc_send_and_release_empty(
|
||||
rpc_storage->rpc, rpc_storage->current_command_id, PB_CommandStatus_OK);
|
||||
rpc_system_storage_reset_state(rpc_storage, false);
|
||||
}
|
||||
}
|
||||
|
||||
if(!result) {
|
||||
rpc_encode_and_send_empty(
|
||||
rpc_send_and_release_empty(
|
||||
rpc_storage->rpc,
|
||||
rpc_storage->current_command_id,
|
||||
rpc_system_storage_get_file_error(file));
|
||||
@@ -260,23 +286,57 @@ static void rpc_system_storage_write_process(const PB_Main* request, void* conte
|
||||
}
|
||||
}
|
||||
|
||||
static bool rpc_system_storage_is_dir_is_empty(Storage* fs_api, const char* path) {
|
||||
FileInfo fileinfo;
|
||||
bool is_dir_is_empty = false;
|
||||
FS_Error error = storage_common_stat(fs_api, path, &fileinfo);
|
||||
if((error == FSE_OK) && (fileinfo.flags & FSF_DIRECTORY)) {
|
||||
File* dir = storage_file_alloc(fs_api);
|
||||
if(storage_dir_open(dir, path)) {
|
||||
char* name = furi_alloc(MAX_NAME_LENGTH);
|
||||
is_dir_is_empty = !storage_dir_read(dir, &fileinfo, name, MAX_NAME_LENGTH);
|
||||
free(name);
|
||||
}
|
||||
storage_dir_close(dir);
|
||||
storage_file_free(dir);
|
||||
}
|
||||
|
||||
return is_dir_is_empty;
|
||||
}
|
||||
|
||||
static void rpc_system_storage_delete_process(const PB_Main* request, void* context) {
|
||||
furi_assert(request);
|
||||
furi_assert(request->which_content == PB_Main_storage_delete_request_tag);
|
||||
furi_assert(context);
|
||||
RpcStorageSystem* rpc_storage = context;
|
||||
PB_CommandStatus status;
|
||||
PB_CommandStatus status = PB_CommandStatus_ERROR;
|
||||
rpc_system_storage_reset_state(rpc_storage, true);
|
||||
|
||||
Storage* fs_api = furi_record_open("storage");
|
||||
char* path = request->content.storage_mkdir_request.path;
|
||||
if(path) {
|
||||
FS_Error error = storage_common_remove(fs_api, path);
|
||||
status = rpc_system_storage_get_error(error);
|
||||
} else {
|
||||
|
||||
char* path = request->content.storage_delete_request.path;
|
||||
if(!path) {
|
||||
status = PB_CommandStatus_ERROR_INVALID_PARAMETERS;
|
||||
} else {
|
||||
FS_Error error_remove = storage_common_remove(fs_api, path);
|
||||
// FSE_DENIED is for empty directory, but not only for this
|
||||
// that's why we have to check it
|
||||
if((error_remove == FSE_DENIED) && !rpc_system_storage_is_dir_is_empty(fs_api, path)) {
|
||||
if(request->content.storage_delete_request.recursive) {
|
||||
bool deleted = storage_simply_remove_recursive(fs_api, path);
|
||||
status = deleted ? PB_CommandStatus_OK : PB_CommandStatus_ERROR;
|
||||
} else {
|
||||
status = PB_CommandStatus_ERROR_STORAGE_DIR_NOT_EMPTY;
|
||||
}
|
||||
} else if(error_remove == FSE_NOT_EXIST) {
|
||||
status = PB_CommandStatus_OK;
|
||||
} else {
|
||||
status = rpc_system_storage_get_error(error_remove);
|
||||
}
|
||||
}
|
||||
rpc_encode_and_send_empty(rpc_storage->rpc, request->command_id, status);
|
||||
|
||||
furi_record_close("storage");
|
||||
rpc_send_and_release_empty(rpc_storage->rpc, request->command_id, status);
|
||||
}
|
||||
|
||||
static void rpc_system_storage_mkdir_process(const PB_Main* request, void* context) {
|
||||
@@ -295,7 +355,7 @@ static void rpc_system_storage_mkdir_process(const PB_Main* request, void* conte
|
||||
} else {
|
||||
status = PB_CommandStatus_ERROR_INVALID_PARAMETERS;
|
||||
}
|
||||
rpc_encode_and_send_empty(rpc_storage->rpc, request->command_id, status);
|
||||
rpc_send_and_release_empty(rpc_storage->rpc, request->command_id, status);
|
||||
}
|
||||
|
||||
static void rpc_system_storage_md5sum_process(const PB_Main* request, void* context) {
|
||||
@@ -307,7 +367,7 @@ static void rpc_system_storage_md5sum_process(const PB_Main* request, void* cont
|
||||
|
||||
const char* filename = request->content.storage_md5sum_request.path;
|
||||
if(!filename) {
|
||||
rpc_encode_and_send_empty(
|
||||
rpc_send_and_release_empty(
|
||||
rpc_storage->rpc, request->command_id, PB_CommandStatus_ERROR_INVALID_PARAMETERS);
|
||||
return;
|
||||
}
|
||||
@@ -349,9 +409,9 @@ static void rpc_system_storage_md5sum_process(const PB_Main* request, void* cont
|
||||
free(hash);
|
||||
free(data);
|
||||
storage_file_close(file);
|
||||
rpc_encode_and_send(rpc_storage->rpc, &response);
|
||||
rpc_send_and_release(rpc_storage->rpc, &response);
|
||||
} else {
|
||||
rpc_encode_and_send_empty(
|
||||
rpc_send_and_release_empty(
|
||||
rpc_storage->rpc, request->command_id, rpc_system_storage_get_file_error(file));
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user