[FL-1792] RPC service (#698)
* Lib: added nanopb * Hal rfid: fixed confused arguments * Lib: update makefile, include nanopb * Lib: remove nanopb * Lib: add nanopb as submodule * Assets: remove protobuf * Assets: add protobuf message definitions as submodule * WIP: [FL-1792] Add Protobuf RPC * WIP: RPC add ping * Add Ping * Fix Ping, Add (WIP) storage * Update submodule * ble-glue: add ptotobuf to ble * WIP: Add storage list test * revert applications.mk * Add Storage List command * ble-glue: fix fast updating rx charachteristic * ble serial: split long ble packets * Add Storage Read/Write/Mkdir/Delete * Disable tests * Rename Element -> File * Add md5sum, fix test leak * Regenerate Protobuf * Fix review comments * ble-glue: sync f7 target Co-authored-by: Albert Kharisov <albert@flipperdevices.com> Co-authored-by: gornekich <n.gorbadey@gmail.com> Co-authored-by: あく <alleteam@gmail.com>
This commit is contained in:
438
applications/rpc/rpc.c
Normal file
438
applications/rpc/rpc.c
Normal file
@@ -0,0 +1,438 @@
|
||||
#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 "pb.h"
|
||||
#include "pb_decode.h"
|
||||
#include "pb_encode.h"
|
||||
#include "portmacro.h"
|
||||
#include "status.pb.h"
|
||||
#include "storage.pb.h"
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <furi.h>
|
||||
#include <stream_buffer.h>
|
||||
#include <m-dict.h>
|
||||
#include "rpc_i.h"
|
||||
|
||||
#define RPC_TAG "RPC"
|
||||
|
||||
#define RPC_EVENT_NEW_DATA (1 << 0)
|
||||
#define RPC_EVENT_DISCONNECT (1 << 1)
|
||||
#define RPC_EVENTS_ALL (RPC_EVENT_DISCONNECT | RPC_EVENT_NEW_DATA)
|
||||
|
||||
#define DEBUG_PRINT 0
|
||||
|
||||
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,
|
||||
},
|
||||
};
|
||||
|
||||
struct RpcSession {
|
||||
RpcSendBytesCallback send_bytes_callback;
|
||||
void* send_bytes_context;
|
||||
osMutexId_t send_bytes_mutex;
|
||||
Rpc* rpc;
|
||||
bool terminate_session;
|
||||
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);
|
||||
|
||||
static size_t rpc_sprint_msg_file(
|
||||
char* str,
|
||||
size_t str_size,
|
||||
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) {
|
||||
cnt += snprintf(
|
||||
str + cnt,
|
||||
str_size - cnt,
|
||||
"%s[%c] size: %5ld",
|
||||
prefix,
|
||||
msg_file->type == PB_Storage_File_FileType_DIR ? 'd' : 'f',
|
||||
msg_file->size);
|
||||
|
||||
if(msg_file->name) {
|
||||
cnt += snprintf(str + cnt, str_size - cnt, " \'%s\'", msg_file->name);
|
||||
}
|
||||
|
||||
if(msg_file->data && msg_file->data->size) {
|
||||
cnt += snprintf(
|
||||
str + cnt,
|
||||
str_size - cnt,
|
||||
" (%d):\'%.*s%s\'",
|
||||
msg_file->data->size,
|
||||
MIN(msg_file->data->size, 30),
|
||||
msg_file->data->bytes,
|
||||
msg_file->data->size > 30 ? "..." : "");
|
||||
}
|
||||
|
||||
cnt += snprintf(str + cnt, str_size - cnt, "\r\n");
|
||||
}
|
||||
|
||||
return cnt;
|
||||
}
|
||||
|
||||
#define ADD_STR(s, c, ...) snprintf(s + c, sizeof(s) - c, ##__VA_ARGS__);
|
||||
|
||||
#define ADD_STR_ELEMENT(s, c, ...) rpc_sprint_msg_file(s + c, sizeof(s) - c, ##__VA_ARGS__);
|
||||
|
||||
void rpc_print_message(const PB_Main* message) {
|
||||
char str[500];
|
||||
size_t cnt = 0;
|
||||
|
||||
cnt += snprintf(
|
||||
str + cnt,
|
||||
sizeof(str) - cnt,
|
||||
"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 */
|
||||
cnt += ADD_STR(str, cnt, "\tNOT_IMPLEMENTED (%d) {\r\n", message->which_content);
|
||||
break;
|
||||
case PB_Main_storage_md5sum_request_tag: {
|
||||
cnt += ADD_STR(str, cnt, "\tmd5sum_request {\r\n");
|
||||
const char* path = message->content.storage_md5sum_request.path;
|
||||
if(path) {
|
||||
cnt += ADD_STR(str, cnt, "\t\tpath: %s\r\n", path);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case PB_Main_storage_md5sum_response_tag: {
|
||||
cnt += ADD_STR(str, cnt, "\tmd5sum_response {\r\n");
|
||||
const char* path = message->content.storage_md5sum_response.md5sum;
|
||||
if(path) {
|
||||
cnt += ADD_STR(str, cnt, "\t\tmd5sum: %s\r\n", path);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case PB_Main_ping_request_tag:
|
||||
cnt += ADD_STR(str, cnt, "\tping_request {\r\n");
|
||||
break;
|
||||
case PB_Main_ping_response_tag:
|
||||
cnt += ADD_STR(str, cnt, "\tping_response {\r\n");
|
||||
break;
|
||||
case PB_Main_storage_mkdir_request_tag:
|
||||
cnt += ADD_STR(str, cnt, "\tmkdir {\r\n");
|
||||
break;
|
||||
case PB_Main_storage_delete_request_tag: {
|
||||
cnt += ADD_STR(str, cnt, "\tdelete {\r\n");
|
||||
const char* path = message->content.storage_delete_request.path;
|
||||
if(path) {
|
||||
cnt += ADD_STR(str, cnt, "\t\tpath: %s\r\n", path);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case PB_Main_empty_tag:
|
||||
cnt += ADD_STR(str, cnt, "\tempty {\r\n");
|
||||
break;
|
||||
case PB_Main_storage_list_request_tag: {
|
||||
cnt += ADD_STR(str, cnt, "\tlist_request {\r\n");
|
||||
const char* path = message->content.storage_list_request.path;
|
||||
if(path) {
|
||||
cnt += ADD_STR(str, cnt, "\t\tpath: %s\r\n", path);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case PB_Main_storage_read_request_tag: {
|
||||
cnt += ADD_STR(str, cnt, "\tread_request {\r\n");
|
||||
const char* path = message->content.storage_read_request.path;
|
||||
if(path) {
|
||||
cnt += ADD_STR(str, cnt, "\t\tpath: %s\r\n", path);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case PB_Main_storage_write_request_tag: {
|
||||
cnt += ADD_STR(str, cnt, "\twrite_request {\r\n");
|
||||
const char* path = message->content.storage_write_request.path;
|
||||
if(path) {
|
||||
cnt += ADD_STR(str, cnt, "\t\tpath: %s\r\n", path);
|
||||
}
|
||||
if(message->content.storage_write_request.has_file) {
|
||||
const PB_Storage_File* msg_file = &message->content.storage_write_request.file;
|
||||
cnt += ADD_STR_ELEMENT(str, cnt, "\t\t\t", msg_file, 1);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case PB_Main_storage_read_response_tag:
|
||||
cnt += ADD_STR(str, cnt, "\tread_response {\r\n");
|
||||
if(message->content.storage_read_response.has_file) {
|
||||
const PB_Storage_File* msg_file = &message->content.storage_read_response.file;
|
||||
cnt += ADD_STR_ELEMENT(str, cnt, "\t\t\t", msg_file, 1);
|
||||
}
|
||||
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;
|
||||
cnt += ADD_STR(str, cnt, "\tlist_response {\r\n");
|
||||
cnt += ADD_STR_ELEMENT(str, cnt, "\t\t", msg_file, msg_file_count);
|
||||
}
|
||||
}
|
||||
cnt += ADD_STR(str, cnt, "\t}\r\n}\r\n");
|
||||
printf("%s", str);
|
||||
}
|
||||
|
||||
static Rpc* rpc_alloc(void) {
|
||||
Rpc* rpc = furi_alloc(sizeof(Rpc));
|
||||
rpc->busy_mutex = osMutexNew(NULL);
|
||||
rpc->busy = false;
|
||||
rpc->events = osEventFlagsNew(NULL);
|
||||
rpc->stream = xStreamBufferCreate(256, 1);
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
RpcSession* rpc_open_session(Rpc* rpc) {
|
||||
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;
|
||||
session->send_bytes_mutex = osMutexNew(NULL);
|
||||
session->rpc = rpc;
|
||||
session->terminate_session = false;
|
||||
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);
|
||||
}
|
||||
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) {
|
||||
furi_assert(session);
|
||||
furi_assert(session->rpc);
|
||||
furi_assert(session->rpc->busy);
|
||||
|
||||
osMutexDelete(session->send_bytes_mutex);
|
||||
rpc_set_send_bytes_callback(session, NULL, NULL);
|
||||
osEventFlagsSet(session->rpc->events, RPC_EVENT_DISCONNECT);
|
||||
}
|
||||
|
||||
void rpc_set_send_bytes_callback(RpcSession* session, RpcSendBytesCallback callback, 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);
|
||||
}
|
||||
|
||||
size_t
|
||||
rpc_feed_bytes(RpcSession* session, uint8_t* encoded_bytes, size_t size, TickType_t timeout) {
|
||||
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;
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
while(1) {
|
||||
bytes_received +=
|
||||
xStreamBufferReceive(rpc->stream, buf + bytes_received, count - bytes_received, 0);
|
||||
if(count == bytes_received) {
|
||||
break;
|
||||
} else {
|
||||
flags = osEventFlagsWait(rpc->events, RPC_EVENTS_ALL, 0, osWaitForever);
|
||||
if(flags & RPC_EVENT_DISCONNECT) {
|
||||
if(xStreamBufferIsEmpty(rpc->stream)) {
|
||||
rpc->session.terminate_session = true;
|
||||
break;
|
||||
} else {
|
||||
/* Save disconnect flag and continue reading buffer */
|
||||
osEventFlagsSet(rpc->events, RPC_EVENT_DISCONNECT);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return (count == bytes_received);
|
||||
}
|
||||
|
||||
void rpc_encode_and_send(Rpc* rpc, PB_Main* main_message) {
|
||||
furi_assert(rpc);
|
||||
furi_assert(main_message);
|
||||
RpcSession* session = &rpc->session;
|
||||
pb_ostream_t ostream = PB_OSTREAM_SIZING;
|
||||
|
||||
#if DEBUG_PRINT
|
||||
FURI_LOG_I(RPC_TAG, "OUTPUT:");
|
||||
rpc_print_message(main_message);
|
||||
#endif
|
||||
|
||||
bool result = pb_encode_ex(&ostream, &PB_Main_msg, main_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);
|
||||
|
||||
{
|
||||
osMutexAcquire(session->send_bytes_mutex, osWaitForever);
|
||||
|
||||
#if DEBUG_PRINT
|
||||
printf("\r\nREPONSE DEC(%d): {", ostream.bytes_written);
|
||||
for(int i = 0; i < ostream.bytes_written; ++i) {
|
||||
printf("%d, ", buffer[i]);
|
||||
}
|
||||
printf("}\r\n");
|
||||
|
||||
printf("REPONSE HEX(%d): {", ostream.bytes_written);
|
||||
for(int i = 0; i < ostream.bytes_written; ++i) {
|
||||
printf("%02X", buffer[i]);
|
||||
}
|
||||
printf("}\r\n\r\n");
|
||||
#endif // DEBUG_PRINT
|
||||
|
||||
if(session->send_bytes_callback) {
|
||||
session->send_bytes_callback(
|
||||
session->send_bytes_context, buffer, ostream.bytes_written);
|
||||
}
|
||||
osMutexRelease(session->send_bytes_mutex);
|
||||
}
|
||||
free(buffer);
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
while(1) {
|
||||
pb_istream_t istream = {
|
||||
.callback = rpc_pb_stream_read,
|
||||
.state = rpc,
|
||||
.errmsg = NULL,
|
||||
.bytes_left = 0x7FFFFFFF,
|
||||
};
|
||||
|
||||
if(pb_decode_ex(&istream, &PB_Main_msg, rpc->decoded_message, PB_DECODE_DELIMITED)) {
|
||||
#if DEBUG_PRINT
|
||||
FURI_LOG_I(RPC_TAG, "INPUT:");
|
||||
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);
|
||||
} else if(!handler) {
|
||||
FURI_LOG_E(
|
||||
RPC_TAG,
|
||||
"Unhandled message, tag: %d\r\n",
|
||||
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);
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
||||
void rpc_encode_and_send_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);
|
||||
pb_release(&PB_Main_msg, &message);
|
||||
}
|
16
applications/rpc/rpc.h
Normal file
16
applications/rpc/rpc.h
Normal file
@@ -0,0 +1,16 @@
|
||||
#pragma once
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include "cmsis_os.h"
|
||||
|
||||
typedef struct Rpc Rpc;
|
||||
typedef struct RpcSession RpcSession;
|
||||
|
||||
typedef void (*RpcSendBytesCallback)(void* context, uint8_t* bytes, size_t bytes_len);
|
||||
|
||||
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);
|
25
applications/rpc/rpc_i.h
Normal file
25
applications/rpc/rpc_i.h
Normal file
@@ -0,0 +1,25 @@
|
||||
#pragma once
|
||||
#include "rpc.h"
|
||||
#include "pb.h"
|
||||
#include "pb_decode.h"
|
||||
#include "pb_encode.h"
|
||||
#include "flipper.pb.h"
|
||||
|
||||
typedef void* (*RpcSystemAlloc)(Rpc*);
|
||||
typedef void (*RpcSystemFree)(void*);
|
||||
typedef void (*PBMessageHandler)(const PB_Main* msg_request, void* context);
|
||||
|
||||
typedef struct {
|
||||
bool (*decode_submessage)(pb_istream_t* stream, const pb_field_t* field, void** arg);
|
||||
PBMessageHandler message_handler;
|
||||
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_add_handler(Rpc* rpc, pb_size_t message_tag, RpcHandler* handler);
|
||||
|
||||
void* rpc_system_status_alloc(Rpc* rpc);
|
||||
void* rpc_system_storage_alloc(Rpc* rpc);
|
||||
void rpc_system_storage_free(void* ctx);
|
||||
void rpc_print_message(const PB_Main* message);
|
25
applications/rpc/rpc_status.c
Normal file
25
applications/rpc/rpc_status.c
Normal file
@@ -0,0 +1,25 @@
|
||||
#include "flipper.pb.h"
|
||||
#include "rpc_i.h"
|
||||
#include "status.pb.h"
|
||||
|
||||
void rpc_system_status_ping_process(const PB_Main* msg_request, void* context) {
|
||||
PB_Main msg_response = PB_Main_init_default;
|
||||
msg_response.has_next = false;
|
||||
msg_response.command_status = PB_CommandStatus_OK;
|
||||
msg_response.command_id = msg_request->command_id;
|
||||
msg_response.which_content = PB_Main_ping_response_tag;
|
||||
|
||||
rpc_encode_and_send(context, &msg_response);
|
||||
}
|
||||
|
||||
void* rpc_system_status_alloc(Rpc* rpc) {
|
||||
RpcHandler rpc_handler = {
|
||||
.message_handler = rpc_system_status_ping_process,
|
||||
.decode_submessage = NULL,
|
||||
.context = rpc,
|
||||
};
|
||||
|
||||
rpc_add_handler(rpc, PB_Main_ping_request_tag, &rpc_handler);
|
||||
|
||||
return NULL;
|
||||
}
|
401
applications/rpc/rpc_storage.c
Normal file
401
applications/rpc/rpc_storage.c
Normal file
@@ -0,0 +1,401 @@
|
||||
#include "flipper.pb.h"
|
||||
#include "furi/common_defines.h"
|
||||
#include "furi/memmgr.h"
|
||||
#include "furi/record.h"
|
||||
#include "pb_decode.h"
|
||||
#include "rpc/rpc.h"
|
||||
#include "rpc_i.h"
|
||||
#include "storage.pb.h"
|
||||
#include "storage/filesystem-api-defines.h"
|
||||
#include "storage/storage.h"
|
||||
#include <stdint.h>
|
||||
#include <lib/toolbox/md5.h>
|
||||
|
||||
#define RPC_TAG "RPC_STORAGE"
|
||||
#define MAX_NAME_LENGTH 255
|
||||
#define MAX_DATA_SIZE 512
|
||||
|
||||
typedef enum {
|
||||
RpcStorageStateIdle = 0,
|
||||
RpcStorageStateWriting,
|
||||
} RpcStorageState;
|
||||
|
||||
typedef struct {
|
||||
Rpc* rpc;
|
||||
Storage* api;
|
||||
File* file;
|
||||
RpcStorageState state;
|
||||
uint32_t current_command_id;
|
||||
} RpcStorageSystem;
|
||||
|
||||
void rpc_print_message(const PB_Main* message);
|
||||
|
||||
static void rpc_system_storage_reset_state(RpcStorageSystem* rpc_storage, bool send_error) {
|
||||
furi_assert(rpc_storage);
|
||||
|
||||
if(rpc_storage->state != RpcStorageStateIdle) {
|
||||
if(send_error) {
|
||||
rpc_encode_and_send_empty(
|
||||
rpc_storage->rpc,
|
||||
rpc_storage->current_command_id,
|
||||
PB_CommandStatus_ERROR_CONTINUOUS_COMMAND_INTERRUPTED);
|
||||
}
|
||||
|
||||
if(rpc_storage->state == RpcStorageStateWriting) {
|
||||
storage_file_close(rpc_storage->file);
|
||||
storage_file_free(rpc_storage->file);
|
||||
furi_record_close("storage");
|
||||
}
|
||||
|
||||
rpc_storage->state = RpcStorageStateIdle;
|
||||
}
|
||||
}
|
||||
|
||||
static PB_CommandStatus rpc_system_storage_get_error(FS_Error fs_error) {
|
||||
PB_CommandStatus pb_error;
|
||||
switch(fs_error) {
|
||||
case FSE_OK:
|
||||
pb_error = PB_CommandStatus_OK;
|
||||
break;
|
||||
case FSE_INVALID_NAME:
|
||||
pb_error = PB_CommandStatus_ERROR_STORAGE_INVALID_NAME;
|
||||
break;
|
||||
case FSE_INVALID_PARAMETER:
|
||||
pb_error = PB_CommandStatus_ERROR_STORAGE_INVALID_PARAMETER;
|
||||
break;
|
||||
case FSE_INTERNAL:
|
||||
pb_error = PB_CommandStatus_ERROR_STORAGE_INTERNAL;
|
||||
break;
|
||||
case FSE_ALREADY_OPEN:
|
||||
pb_error = PB_CommandStatus_ERROR_STORAGE_ALREADY_OPEN;
|
||||
break;
|
||||
case FSE_DENIED:
|
||||
pb_error = PB_CommandStatus_ERROR_STORAGE_DENIED;
|
||||
break;
|
||||
case FSE_EXIST:
|
||||
pb_error = PB_CommandStatus_ERROR_STORAGE_EXIST;
|
||||
break;
|
||||
case FSE_NOT_EXIST:
|
||||
pb_error = PB_CommandStatus_ERROR_STORAGE_NOT_EXIST;
|
||||
break;
|
||||
case FSE_NOT_READY:
|
||||
pb_error = PB_CommandStatus_ERROR_STORAGE_NOT_READY;
|
||||
break;
|
||||
case FSE_NOT_IMPLEMENTED:
|
||||
pb_error = PB_CommandStatus_ERROR_STORAGE_NOT_IMPLEMENTED;
|
||||
break;
|
||||
default:
|
||||
pb_error = PB_CommandStatus_ERROR;
|
||||
break;
|
||||
}
|
||||
|
||||
return pb_error;
|
||||
}
|
||||
|
||||
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_process(const PB_Main* request, void* context) {
|
||||
furi_assert(request);
|
||||
furi_assert(context);
|
||||
furi_assert(request->which_content == PB_Main_storage_list_request_tag);
|
||||
|
||||
RpcStorageSystem* rpc_storage = context;
|
||||
rpc_system_storage_reset_state(rpc_storage, true);
|
||||
|
||||
Storage* fs_api = furi_record_open("storage");
|
||||
File* dir = storage_file_alloc(fs_api);
|
||||
|
||||
PB_Main response = {
|
||||
.command_id = request->command_id,
|
||||
.has_next = false,
|
||||
.which_content = PB_Main_storage_list_request_tag,
|
||||
.command_status = PB_CommandStatus_OK,
|
||||
};
|
||||
PB_Storage_ListResponse* list = &response.content.storage_list_response;
|
||||
response.which_content = PB_Main_storage_list_response_tag;
|
||||
|
||||
bool finish = false;
|
||||
int i = 0;
|
||||
|
||||
if(!storage_dir_open(dir, request->content.storage_list_request.path)) {
|
||||
response.command_status = rpc_system_storage_get_file_error(dir);
|
||||
response.which_content = PB_Main_empty_tag;
|
||||
finish = true;
|
||||
}
|
||||
|
||||
while(!finish) {
|
||||
FileInfo fileinfo;
|
||||
char* name = furi_alloc(MAX_NAME_LENGTH + 1);
|
||||
if(storage_dir_read(dir, &fileinfo, name, MAX_NAME_LENGTH)) {
|
||||
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);
|
||||
i = 0;
|
||||
}
|
||||
list->file[i].type = (fileinfo.flags & FSF_DIRECTORY) ? PB_Storage_File_FileType_DIR :
|
||||
PB_Storage_File_FileType_FILE;
|
||||
list->file[i].size = fileinfo.size;
|
||||
list->file[i].data = NULL;
|
||||
list->file[i].name = name;
|
||||
++i;
|
||||
} else {
|
||||
list->file_count = i;
|
||||
finish = true;
|
||||
free(name);
|
||||
}
|
||||
}
|
||||
|
||||
response.has_next = false;
|
||||
rpc_encode_and_send(rpc_storage->rpc, &response);
|
||||
pb_release(&PB_Main_msg, &response);
|
||||
|
||||
storage_dir_close(dir);
|
||||
storage_file_free(dir);
|
||||
|
||||
furi_record_close("storage");
|
||||
}
|
||||
|
||||
static void rpc_system_storage_read_process(const PB_Main* request, void* context) {
|
||||
furi_assert(request);
|
||||
furi_assert(request->which_content == PB_Main_storage_read_request_tag);
|
||||
|
||||
RpcStorageSystem* rpc_storage = context;
|
||||
rpc_system_storage_reset_state(rpc_storage, true);
|
||||
|
||||
/* 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);
|
||||
bool result = false;
|
||||
|
||||
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 {
|
||||
uint8_t* buffer = response->content.storage_read_response.file.data->bytes;
|
||||
uint16_t* read_size_msg = &response->content.storage_read_response.file.data->size;
|
||||
|
||||
size_t read_size = MIN(size_left, MAX_DATA_SIZE);
|
||||
*read_size_msg = storage_file_read(file, buffer, read_size);
|
||||
size_left -= read_size;
|
||||
result = (*read_size_msg == read_size);
|
||||
|
||||
if(result) {
|
||||
response->has_next = (size_left > 0);
|
||||
rpc_encode_and_send(rpc_storage->rpc, response);
|
||||
// no pb_release(...);
|
||||
}
|
||||
} while((size_left != 0) && result);
|
||||
|
||||
if(!result) {
|
||||
rpc_encode_and_send_empty(
|
||||
rpc_storage->rpc, request->command_id, rpc_system_storage_get_file_error(file));
|
||||
}
|
||||
} else {
|
||||
rpc_encode_and_send_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);
|
||||
|
||||
furi_record_close("storage");
|
||||
}
|
||||
|
||||
static void rpc_system_storage_write_process(const PB_Main* request, void* context) {
|
||||
furi_assert(request);
|
||||
furi_assert(request->which_content == PB_Main_storage_write_request_tag);
|
||||
|
||||
RpcStorageSystem* rpc_storage = context;
|
||||
bool result = true;
|
||||
|
||||
if((request->command_id != rpc_storage->current_command_id) &&
|
||||
(rpc_storage->state == RpcStorageStateWriting)) {
|
||||
rpc_system_storage_reset_state(rpc_storage, true);
|
||||
}
|
||||
|
||||
if(rpc_storage->state != RpcStorageStateWriting) {
|
||||
rpc_storage->api = furi_record_open("storage");
|
||||
rpc_storage->file = storage_file_alloc(rpc_storage->api);
|
||||
rpc_storage->current_command_id = request->command_id;
|
||||
rpc_storage->state = RpcStorageStateWriting;
|
||||
const char* path = request->content.storage_write_request.path;
|
||||
result = storage_file_open(rpc_storage->file, path, FSAM_WRITE, FSOM_CREATE_ALWAYS);
|
||||
}
|
||||
|
||||
File* file = rpc_storage->file;
|
||||
|
||||
if(result) {
|
||||
uint8_t* buffer = request->content.storage_write_request.file.data->bytes;
|
||||
size_t buffer_size = request->content.storage_write_request.file.data->size;
|
||||
|
||||
uint16_t written_size = storage_file_write(file, buffer, buffer_size);
|
||||
result = (written_size == buffer_size);
|
||||
|
||||
if(result && !request->has_next) {
|
||||
rpc_encode_and_send_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_storage->rpc,
|
||||
rpc_storage->current_command_id,
|
||||
rpc_system_storage_get_file_error(file));
|
||||
rpc_system_storage_reset_state(rpc_storage, false);
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
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 {
|
||||
status = PB_CommandStatus_ERROR_INVALID_PARAMETERS;
|
||||
}
|
||||
rpc_encode_and_send_empty(rpc_storage->rpc, request->command_id, status);
|
||||
}
|
||||
|
||||
static void rpc_system_storage_mkdir_process(const PB_Main* request, void* context) {
|
||||
furi_assert(request);
|
||||
furi_assert(request->which_content == PB_Main_storage_mkdir_request_tag);
|
||||
furi_assert(context);
|
||||
RpcStorageSystem* rpc_storage = context;
|
||||
PB_CommandStatus status;
|
||||
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_mkdir(fs_api, path);
|
||||
status = rpc_system_storage_get_error(error);
|
||||
} else {
|
||||
status = PB_CommandStatus_ERROR_INVALID_PARAMETERS;
|
||||
}
|
||||
rpc_encode_and_send_empty(rpc_storage->rpc, request->command_id, status);
|
||||
}
|
||||
|
||||
static void rpc_system_storage_md5sum_process(const PB_Main* request, void* context) {
|
||||
furi_assert(request);
|
||||
furi_assert(request->which_content == PB_Main_storage_md5sum_request_tag);
|
||||
furi_assert(context);
|
||||
RpcStorageSystem* rpc_storage = context;
|
||||
rpc_system_storage_reset_state(rpc_storage, true);
|
||||
|
||||
const char* filename = request->content.storage_md5sum_request.path;
|
||||
if(!filename) {
|
||||
rpc_encode_and_send_empty(
|
||||
rpc_storage->rpc, request->command_id, PB_CommandStatus_ERROR_INVALID_PARAMETERS);
|
||||
return;
|
||||
}
|
||||
|
||||
Storage* fs_api = furi_record_open("storage");
|
||||
File* file = storage_file_alloc(fs_api);
|
||||
|
||||
if(storage_file_open(file, filename, FSAM_READ, FSOM_OPEN_EXISTING)) {
|
||||
const uint16_t read_size = 512;
|
||||
const uint8_t hash_size = 16;
|
||||
uint8_t* data = malloc(read_size);
|
||||
uint8_t* hash = malloc(sizeof(uint8_t) * hash_size);
|
||||
md5_context* md5_ctx = malloc(sizeof(md5_context));
|
||||
|
||||
md5_starts(md5_ctx);
|
||||
while(true) {
|
||||
uint16_t readed_size = storage_file_read(file, data, read_size);
|
||||
if(readed_size == 0) break;
|
||||
md5_update(md5_ctx, data, readed_size);
|
||||
}
|
||||
md5_finish(md5_ctx, hash);
|
||||
free(md5_ctx);
|
||||
|
||||
PB_Main response = {
|
||||
.command_id = request->command_id,
|
||||
.command_status = PB_CommandStatus_OK,
|
||||
.which_content = PB_Main_storage_md5sum_response_tag,
|
||||
.has_next = false,
|
||||
};
|
||||
|
||||
char* md5sum = response.content.storage_md5sum_response.md5sum;
|
||||
size_t md5sum_size = sizeof(response.content.storage_md5sum_response.md5sum);
|
||||
furi_assert(hash_size <= ((md5sum_size - 1) / 2));
|
||||
for(uint8_t i = 0; i < hash_size; i++) {
|
||||
md5sum += sprintf(md5sum, "%02x", hash[i]);
|
||||
}
|
||||
|
||||
free(hash);
|
||||
free(data);
|
||||
storage_file_close(file);
|
||||
rpc_encode_and_send(rpc_storage->rpc, &response);
|
||||
} else {
|
||||
rpc_encode_and_send_empty(
|
||||
rpc_storage->rpc, request->command_id, rpc_system_storage_get_file_error(file));
|
||||
}
|
||||
|
||||
storage_file_free(file);
|
||||
|
||||
furi_record_close("storage");
|
||||
}
|
||||
|
||||
void* rpc_system_storage_alloc(Rpc* rpc) {
|
||||
furi_assert(rpc);
|
||||
|
||||
RpcStorageSystem* rpc_storage = furi_alloc(sizeof(RpcStorageSystem));
|
||||
rpc_storage->api = furi_record_open("storage");
|
||||
rpc_storage->rpc = rpc;
|
||||
rpc_storage->state = RpcStorageStateIdle;
|
||||
|
||||
RpcHandler rpc_handler = {
|
||||
.message_handler = NULL,
|
||||
.decode_submessage = NULL,
|
||||
.context = rpc_storage,
|
||||
};
|
||||
|
||||
rpc_handler.message_handler = rpc_system_storage_list_process;
|
||||
rpc_add_handler(rpc, PB_Main_storage_list_request_tag, &rpc_handler);
|
||||
|
||||
rpc_handler.message_handler = rpc_system_storage_read_process;
|
||||
rpc_add_handler(rpc, PB_Main_storage_read_request_tag, &rpc_handler);
|
||||
|
||||
rpc_handler.message_handler = rpc_system_storage_write_process;
|
||||
rpc_add_handler(rpc, PB_Main_storage_write_request_tag, &rpc_handler);
|
||||
|
||||
rpc_handler.message_handler = rpc_system_storage_delete_process;
|
||||
rpc_add_handler(rpc, PB_Main_storage_delete_request_tag, &rpc_handler);
|
||||
|
||||
rpc_handler.message_handler = rpc_system_storage_mkdir_process;
|
||||
rpc_add_handler(rpc, PB_Main_storage_mkdir_request_tag, &rpc_handler);
|
||||
|
||||
rpc_handler.message_handler = rpc_system_storage_md5sum_process;
|
||||
rpc_add_handler(rpc, PB_Main_storage_md5sum_request_tag, &rpc_handler);
|
||||
|
||||
return rpc_storage;
|
||||
}
|
||||
|
||||
void rpc_system_storage_free(void* ctx) {
|
||||
RpcStorageSystem* rpc_storage = ctx;
|
||||
rpc_system_storage_reset_state(rpc_storage, false);
|
||||
free(rpc_storage);
|
||||
}
|
Reference in New Issue
Block a user