2021-10-26 16:05:28 +00:00
|
|
|
#include "rpc_i.h"
|
2021-12-21 12:16:25 +00:00
|
|
|
|
2021-10-26 16:05:28 +00:00
|
|
|
#include <pb.h>
|
|
|
|
#include <pb_decode.h>
|
|
|
|
#include <pb_encode.h>
|
2021-12-21 12:16:25 +00:00
|
|
|
|
2021-10-26 16:05:28 +00:00
|
|
|
#include <storage.pb.h>
|
|
|
|
#include <flipper.pb.h>
|
|
|
|
#include <portmacro.h>
|
2021-12-21 12:16:25 +00:00
|
|
|
|
2021-10-26 16:05:28 +00:00
|
|
|
#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 <m-dict.h>
|
|
|
|
|
2023-09-01 04:54:12 +00:00
|
|
|
#include <bt/bt_service/bt.h>
|
|
|
|
|
2021-11-12 13:04:35 +00:00
|
|
|
#define TAG "RpcSrv"
|
2021-10-12 11:48:34 +00:00
|
|
|
|
2022-03-16 08:52:11 +00:00
|
|
|
typedef enum {
|
|
|
|
RpcEvtNewData = (1 << 0),
|
|
|
|
RpcEvtDisconnect = (1 << 1),
|
|
|
|
} RpcEvtFlags;
|
|
|
|
|
|
|
|
#define RPC_ALL_EVENTS (RpcEvtNewData | RpcEvtDisconnect)
|
2021-10-12 11:48:34 +00:00
|
|
|
|
|
|
|
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[] = {
|
|
|
|
{
|
2021-12-07 13:47:20 +00:00
|
|
|
.alloc = rpc_system_system_alloc,
|
2021-10-12 11:48:34 +00:00
|
|
|
.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,
|
2022-07-04 13:09:46 +00:00
|
|
|
.free = rpc_system_app_free,
|
2021-10-13 15:39:37 +00:00
|
|
|
},
|
2021-11-01 16:26:37 +00:00
|
|
|
{
|
|
|
|
.alloc = rpc_system_gui_alloc,
|
|
|
|
.free = rpc_system_gui_free,
|
|
|
|
},
|
2022-07-04 15:32:06 +00:00
|
|
|
{
|
|
|
|
.alloc = rpc_system_gpio_alloc,
|
|
|
|
.free = NULL,
|
2022-11-29 09:08:08 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
.alloc = rpc_system_property_alloc,
|
|
|
|
.free = NULL,
|
|
|
|
},
|
2023-05-11 09:50:17 +00:00
|
|
|
{
|
|
|
|
.alloc = rpc_desktop_alloc,
|
|
|
|
.free = rpc_desktop_free,
|
|
|
|
},
|
2022-11-29 09:08:08 +00:00
|
|
|
};
|
2021-10-12 11:48:34 +00:00
|
|
|
|
|
|
|
struct RpcSession {
|
2022-03-16 08:52:11 +00:00
|
|
|
Rpc* rpc;
|
|
|
|
|
|
|
|
FuriThread* thread;
|
|
|
|
|
|
|
|
RpcHandlerDict_t handlers;
|
2022-10-07 12:27:11 +00:00
|
|
|
FuriStreamBuffer* stream;
|
2022-03-16 08:52:11 +00:00
|
|
|
PB_Main* decoded_message;
|
|
|
|
bool terminate;
|
|
|
|
void** system_contexts;
|
|
|
|
bool decode_error;
|
|
|
|
|
2022-07-20 10:56:33 +00:00
|
|
|
FuriMutex* callbacks_mutex;
|
2021-10-12 11:48:34 +00:00
|
|
|
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;
|
2022-02-24 12:08:58 +00:00
|
|
|
RpcSessionTerminatedCallback terminated_callback;
|
2023-04-19 08:30:26 +00:00
|
|
|
RpcOwner owner;
|
2021-10-26 16:05:28 +00:00
|
|
|
void* context;
|
2021-10-12 11:48:34 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct Rpc {
|
2022-07-20 10:56:33 +00:00
|
|
|
FuriMutex* busy_mutex;
|
2021-10-12 11:48:34 +00:00
|
|
|
};
|
|
|
|
|
2023-04-19 08:30:26 +00:00
|
|
|
RpcOwner rpc_session_get_owner(RpcSession* session) {
|
|
|
|
furi_assert(session);
|
|
|
|
return session->owner;
|
|
|
|
}
|
|
|
|
|
2022-03-16 08:52:11 +00:00
|
|
|
static void rpc_close_session_process(const PB_Main* request, void* context) {
|
|
|
|
furi_assert(request);
|
2022-08-22 16:36:45 +00:00
|
|
|
furi_assert(context);
|
2021-10-26 16:05:28 +00:00
|
|
|
|
2022-03-16 08:52:11 +00:00
|
|
|
RpcSession* session = (RpcSession*)context;
|
2021-10-26 16:05:28 +00:00
|
|
|
|
2022-03-16 08:52:11 +00:00
|
|
|
rpc_send_and_release_empty(session, request->command_id, PB_CommandStatus_OK);
|
2022-07-20 10:56:33 +00:00
|
|
|
furi_mutex_acquire(session->callbacks_mutex, FuriWaitForever);
|
2022-03-16 08:52:11 +00:00
|
|
|
if(session->closed_callback) {
|
|
|
|
session->closed_callback(session->context);
|
2022-01-14 09:58:26 +00:00
|
|
|
} else {
|
2022-03-16 08:52:11 +00:00
|
|
|
FURI_LOG_W(TAG, "Session stop isn't processed by transport layer");
|
2021-10-26 16:05:28 +00:00
|
|
|
}
|
2022-07-20 10:56:33 +00:00
|
|
|
furi_mutex_release(session->callbacks_mutex);
|
2021-10-26 16:05:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void rpc_session_set_context(RpcSession* session, void* context) {
|
|
|
|
furi_assert(session);
|
|
|
|
|
2022-07-20 10:56:33 +00:00
|
|
|
furi_mutex_acquire(session->callbacks_mutex, FuriWaitForever);
|
2021-10-26 16:05:28 +00:00
|
|
|
session->context = context;
|
2022-07-20 10:56:33 +00:00
|
|
|
furi_mutex_release(session->callbacks_mutex);
|
2021-10-26 16:05:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void rpc_session_set_close_callback(RpcSession* session, RpcSessionClosedCallback callback) {
|
|
|
|
furi_assert(session);
|
|
|
|
|
2022-07-20 10:56:33 +00:00
|
|
|
furi_mutex_acquire(session->callbacks_mutex, FuriWaitForever);
|
2021-10-26 16:05:28 +00:00
|
|
|
session->closed_callback = callback;
|
2022-07-20 10:56:33 +00:00
|
|
|
furi_mutex_release(session->callbacks_mutex);
|
2021-10-26 16:05:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void rpc_session_set_send_bytes_callback(RpcSession* session, RpcSendBytesCallback callback) {
|
2021-10-12 11:48:34 +00:00
|
|
|
furi_assert(session);
|
|
|
|
|
2022-07-20 10:56:33 +00:00
|
|
|
furi_mutex_acquire(session->callbacks_mutex, FuriWaitForever);
|
2021-10-12 11:48:34 +00:00
|
|
|
session->send_bytes_callback = callback;
|
2022-07-20 10:56:33 +00:00
|
|
|
furi_mutex_release(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);
|
|
|
|
|
2022-07-20 10:56:33 +00:00
|
|
|
furi_mutex_acquire(session->callbacks_mutex, FuriWaitForever);
|
2021-11-08 19:41:40 +00:00
|
|
|
session->buffer_is_empty_callback = callback;
|
2022-07-20 10:56:33 +00:00
|
|
|
furi_mutex_release(session->callbacks_mutex);
|
2021-11-08 19:41:40 +00:00
|
|
|
}
|
|
|
|
|
2022-02-24 12:08:58 +00:00
|
|
|
void rpc_session_set_terminated_callback(
|
|
|
|
RpcSession* session,
|
|
|
|
RpcSessionTerminatedCallback callback) {
|
|
|
|
furi_assert(session);
|
|
|
|
|
2022-07-20 10:56:33 +00:00
|
|
|
furi_mutex_acquire(session->callbacks_mutex, FuriWaitForever);
|
2022-02-24 12:08:58 +00:00
|
|
|
session->terminated_callback = callback;
|
2022-07-20 10:56:33 +00:00
|
|
|
furi_mutex_release(session->callbacks_mutex);
|
2022-02-24 12:08:58 +00:00
|
|
|
}
|
|
|
|
|
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);
|
2022-08-22 16:36:45 +00:00
|
|
|
furi_assert(encoded_bytes);
|
2022-10-24 10:50:34 +00:00
|
|
|
|
|
|
|
if(!size) return 0;
|
2022-08-22 16:36:45 +00:00
|
|
|
|
2022-10-07 12:27:11 +00:00
|
|
|
size_t bytes_sent = furi_stream_buffer_send(session->stream, encoded_bytes, size, timeout);
|
2021-10-12 11:48:34 +00:00
|
|
|
|
2022-06-20 14:54:48 +00:00
|
|
|
furi_thread_flags_set(furi_thread_get_id(session->thread), RpcEvtNewData);
|
2021-10-12 11:48:34 +00:00
|
|
|
|
|
|
|
return bytes_sent;
|
|
|
|
}
|
|
|
|
|
2021-11-08 19:41:40 +00:00
|
|
|
size_t rpc_session_get_available_size(RpcSession* session) {
|
|
|
|
furi_assert(session);
|
2022-10-07 12:27:11 +00:00
|
|
|
return furi_stream_buffer_spaces_available(session->stream);
|
2021-11-08 19:41:40 +00:00
|
|
|
}
|
|
|
|
|
2021-10-12 11:48:34 +00:00
|
|
|
bool rpc_pb_stream_read(pb_istream_t* istream, pb_byte_t* buf, size_t count) {
|
2022-08-22 16:36:45 +00:00
|
|
|
furi_assert(istream);
|
|
|
|
furi_assert(buf);
|
2022-03-16 08:52:11 +00:00
|
|
|
RpcSession* session = istream->state;
|
|
|
|
furi_assert(session);
|
|
|
|
furi_assert(istream->bytes_left);
|
|
|
|
|
2021-10-12 11:48:34 +00:00
|
|
|
uint32_t flags = 0;
|
|
|
|
size_t bytes_received = 0;
|
|
|
|
|
|
|
|
while(1) {
|
2022-10-07 12:27:11 +00:00
|
|
|
bytes_received += furi_stream_buffer_receive(
|
|
|
|
session->stream, buf + bytes_received, count - bytes_received, 0);
|
|
|
|
if(furi_stream_buffer_is_empty(session->stream)) {
|
2022-03-16 08:52:11 +00:00
|
|
|
if(session->buffer_is_empty_callback) {
|
|
|
|
session->buffer_is_empty_callback(session->context);
|
2021-11-08 19:41:40 +00:00
|
|
|
}
|
|
|
|
}
|
2022-03-16 08:52:11 +00:00
|
|
|
if(session->decode_error) {
|
2022-01-14 09:58:26 +00:00
|
|
|
/* never go out till RPC_EVENT_DISCONNECT come */
|
|
|
|
bytes_received = 0;
|
|
|
|
}
|
2021-10-12 11:48:34 +00:00
|
|
|
if(count == bytes_received) {
|
|
|
|
break;
|
|
|
|
} else {
|
2022-07-20 10:56:33 +00:00
|
|
|
flags = furi_thread_flags_wait(RPC_ALL_EVENTS, FuriFlagWaitAny, FuriWaitForever);
|
2022-03-16 08:52:11 +00:00
|
|
|
if(flags & RpcEvtDisconnect) {
|
2022-10-07 12:27:11 +00:00
|
|
|
if(furi_stream_buffer_is_empty(session->stream)) {
|
2022-03-16 08:52:11 +00:00
|
|
|
session->terminate = true;
|
2021-10-26 16:05:28 +00:00
|
|
|
istream->bytes_left = 0;
|
|
|
|
bytes_received = 0;
|
2021-10-12 11:48:34 +00:00
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
/* Save disconnect flag and continue reading buffer */
|
2022-06-20 14:54:48 +00:00
|
|
|
furi_thread_flags_set(furi_thread_get_id(session->thread), RpcEvtDisconnect);
|
2021-10-12 11:48:34 +00:00
|
|
|
}
|
2022-03-16 08:52:11 +00:00
|
|
|
} else if(flags & RpcEvtNewData) {
|
|
|
|
// Just wake thread up
|
2021-10-12 11:48:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-12 13:04:35 +00:00
|
|
|
#if SRV_RPC_DEBUG
|
2022-08-22 16:36:45 +00:00
|
|
|
rpc_debug_print_data("INPUT", buf, bytes_received);
|
2021-10-26 16:05:28 +00:00
|
|
|
#endif
|
|
|
|
|
2021-10-12 11:48:34 +00:00
|
|
|
return (count == bytes_received);
|
|
|
|
}
|
|
|
|
|
2022-08-22 16:36:45 +00:00
|
|
|
static bool rpc_pb_content_callback(pb_istream_t* stream, const pb_field_t* field, void** arg) {
|
2021-10-12 11:48:34 +00:00
|
|
|
furi_assert(stream);
|
2022-03-16 08:52:11 +00:00
|
|
|
RpcSession* session = stream->state;
|
|
|
|
furi_assert(session);
|
2022-08-22 16:36:45 +00:00
|
|
|
furi_assert(field);
|
2021-10-12 11:48:34 +00:00
|
|
|
|
2022-03-16 08:52:11 +00:00
|
|
|
RpcHandler* handler = RpcHandlerDict_get(session->handlers, field->tag);
|
2021-10-12 11:48:34 +00:00
|
|
|
|
|
|
|
if(handler && handler->decode_submessage) {
|
|
|
|
handler->decode_submessage(stream, field, arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-03-16 08:52:11 +00:00
|
|
|
static int32_t rpc_session_worker(void* context) {
|
|
|
|
furi_assert(context);
|
|
|
|
RpcSession* session = (RpcSession*)context;
|
|
|
|
Rpc* rpc = session->rpc;
|
2021-10-26 16:05:28 +00:00
|
|
|
|
2022-03-16 08:52:11 +00:00
|
|
|
FURI_LOG_D(TAG, "Session started");
|
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,
|
2022-03-16 08:52:11 +00:00
|
|
|
.state = session,
|
2021-10-12 11:48:34 +00:00
|
|
|
.errmsg = NULL,
|
2023-03-31 07:15:15 +00:00
|
|
|
.bytes_left = SIZE_MAX,
|
2021-10-12 11:48:34 +00:00
|
|
|
};
|
|
|
|
|
2022-01-14 09:58:26 +00:00
|
|
|
bool message_decode_failed = false;
|
|
|
|
|
2022-03-16 08:52:11 +00:00
|
|
|
if(pb_decode_ex(&istream, &PB_Main_msg, session->decoded_message, PB_DECODE_DELIMITED)) {
|
2021-11-12 13:04:35 +00:00
|
|
|
#if SRV_RPC_DEBUG
|
|
|
|
FURI_LOG_I(TAG, "INPUT:");
|
2022-08-22 16:36:45 +00:00
|
|
|
rpc_debug_print_message(session->decoded_message);
|
2021-10-12 11:48:34 +00:00
|
|
|
#endif
|
|
|
|
RpcHandler* handler =
|
2022-03-16 08:52:11 +00:00
|
|
|
RpcHandlerDict_get(session->handlers, session->decoded_message->which_content);
|
2021-10-12 11:48:34 +00:00
|
|
|
|
|
|
|
if(handler && handler->message_handler) {
|
2022-07-20 10:56:33 +00:00
|
|
|
furi_check(furi_mutex_acquire(rpc->busy_mutex, FuriWaitForever) == FuriStatusOk);
|
2022-03-16 08:52:11 +00:00
|
|
|
handler->message_handler(session->decoded_message, handler->context);
|
2022-07-20 10:56:33 +00:00
|
|
|
furi_check(furi_mutex_release(rpc->busy_mutex) == FuriStatusOk);
|
2022-03-16 08:52:11 +00:00
|
|
|
} else if(session->decoded_message->which_content == 0) {
|
2022-01-14 09:58:26 +00:00
|
|
|
/* Receiving zeroes means message is 0-length, which
|
|
|
|
* is valid for proto3: all fields are filled with default values.
|
|
|
|
* 0 - is default value for which_content field.
|
|
|
|
* Mark it as decode error, because there is no content message
|
|
|
|
* in Main message with tag 0.
|
|
|
|
*/
|
|
|
|
message_decode_failed = true;
|
2022-03-16 08:52:11 +00:00
|
|
|
} else if(!handler && !session->terminate) {
|
2022-01-14 09:58:26 +00:00
|
|
|
FURI_LOG_E(
|
|
|
|
TAG,
|
|
|
|
"Message(%d) decoded, but not implemented",
|
2022-03-16 08:52:11 +00:00
|
|
|
session->decoded_message->which_content);
|
2022-01-14 09:58:26 +00:00
|
|
|
rpc_send_and_release_empty(
|
2022-03-16 08:52:11 +00:00
|
|
|
session,
|
|
|
|
session->decoded_message->command_id,
|
|
|
|
PB_CommandStatus_ERROR_NOT_IMPLEMENTED);
|
2021-10-12 11:48:34 +00:00
|
|
|
}
|
|
|
|
} else {
|
2022-01-14 09:58:26 +00:00
|
|
|
message_decode_failed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(message_decode_failed) {
|
2022-10-07 12:27:11 +00:00
|
|
|
furi_stream_buffer_reset(session->stream);
|
2022-03-16 08:52:11 +00:00
|
|
|
if(!session->terminate) {
|
2022-01-14 09:58:26 +00:00
|
|
|
/* Protobuf can't determine start and end of message.
|
|
|
|
* Handle this by adding varint at beginning
|
|
|
|
* of a message (PB_ENCODE_DELIMITED). But decoding fail
|
|
|
|
* means we can't be sure next bytes are varint for next
|
|
|
|
* message, so the only way to close session.
|
|
|
|
* RPC itself can't make decision to close session. It has
|
|
|
|
* to notify:
|
|
|
|
* 1) down layer (transport)
|
|
|
|
* 2) other side (companion app)
|
|
|
|
* Who are responsible to handle RPC session lifecycle.
|
|
|
|
* Companion receives 2 messages: ERROR_DECODE and session_closed.
|
|
|
|
*/
|
2021-11-12 13:04:35 +00:00
|
|
|
FURI_LOG_E(TAG, "Decode failed, error: \'%.128s\'", PB_GET_ERROR(&istream));
|
2022-03-16 08:52:11 +00:00
|
|
|
session->decode_error = true;
|
|
|
|
rpc_send_and_release_empty(session, 0, PB_CommandStatus_ERROR_DECODE);
|
2022-07-20 10:56:33 +00:00
|
|
|
furi_mutex_acquire(session->callbacks_mutex, FuriWaitForever);
|
2022-03-16 08:52:11 +00:00
|
|
|
if(session->closed_callback) {
|
|
|
|
session->closed_callback(session->context);
|
2022-01-14 09:58:26 +00:00
|
|
|
}
|
2022-07-20 10:56:33 +00:00
|
|
|
furi_mutex_release(session->callbacks_mutex);
|
2023-09-01 04:54:12 +00:00
|
|
|
|
|
|
|
if(session->owner == RpcOwnerBle) {
|
|
|
|
// Disconnect BLE session
|
|
|
|
FURI_LOG_E("RPC", "BLE session closed due to a decode error");
|
|
|
|
Bt* bt = furi_record_open(RECORD_BT);
|
|
|
|
bt_set_profile(bt, BtProfileSerial);
|
|
|
|
furi_record_close(RECORD_BT);
|
|
|
|
FURI_LOG_E("RPC", "Finished disconnecting the BLE session");
|
|
|
|
}
|
2021-10-12 11:48:34 +00:00
|
|
|
}
|
|
|
|
}
|
2021-10-26 16:05:28 +00:00
|
|
|
|
2022-03-16 08:52:11 +00:00
|
|
|
pb_release(&PB_Main_msg, session->decoded_message);
|
2021-10-26 16:05:28 +00:00
|
|
|
|
2022-03-16 08:52:11 +00:00
|
|
|
if(session->terminate) {
|
2021-11-12 13:04:35 +00:00
|
|
|
FURI_LOG_D(TAG, "Session terminated");
|
2022-03-16 08:52:11 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-05-05 12:40:55 +00:00
|
|
|
static void rpc_session_thread_pending_callback(void* context, uint32_t arg) {
|
|
|
|
UNUSED(arg);
|
2022-03-16 08:52:11 +00:00
|
|
|
RpcSession* session = (RpcSession*)context;
|
|
|
|
|
2023-05-05 12:40:55 +00:00
|
|
|
for(size_t i = 0; i < COUNT_OF(rpc_systems); ++i) {
|
|
|
|
if(rpc_systems[i].free) {
|
|
|
|
(rpc_systems[i].free)(session->system_contexts[i]);
|
2022-03-16 08:52:11 +00:00
|
|
|
}
|
2023-05-05 12:40:55 +00:00
|
|
|
}
|
|
|
|
free(session->system_contexts);
|
|
|
|
free(session->decoded_message);
|
|
|
|
RpcHandlerDict_clear(session->handlers);
|
|
|
|
furi_stream_buffer_free(session->stream);
|
2022-03-16 08:52:11 +00:00
|
|
|
|
2023-05-05 12:40:55 +00:00
|
|
|
furi_mutex_acquire(session->callbacks_mutex, FuriWaitForever);
|
|
|
|
if(session->terminated_callback) {
|
|
|
|
session->terminated_callback(session->context);
|
|
|
|
}
|
|
|
|
furi_mutex_release(session->callbacks_mutex);
|
|
|
|
|
|
|
|
furi_mutex_free(session->callbacks_mutex);
|
|
|
|
furi_thread_join(session->thread);
|
|
|
|
furi_thread_free(session->thread);
|
|
|
|
free(session);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void rpc_session_thread_state_callback(FuriThreadState thread_state, void* context) {
|
|
|
|
if(thread_state == FuriThreadStateStopped) {
|
|
|
|
furi_timer_pending_callback(rpc_session_thread_pending_callback, context, 0);
|
2022-03-16 08:52:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-19 08:30:26 +00:00
|
|
|
RpcSession* rpc_session_open(Rpc* rpc, RpcOwner owner) {
|
2022-03-16 08:52:11 +00:00
|
|
|
furi_assert(rpc);
|
|
|
|
|
|
|
|
RpcSession* session = malloc(sizeof(RpcSession));
|
2022-07-20 10:56:33 +00:00
|
|
|
session->callbacks_mutex = furi_mutex_alloc(FuriMutexTypeNormal);
|
2022-10-07 12:27:11 +00:00
|
|
|
session->stream = furi_stream_buffer_alloc(RPC_BUFFER_SIZE, 1);
|
2022-03-16 08:52:11 +00:00
|
|
|
session->rpc = rpc;
|
|
|
|
session->terminate = false;
|
|
|
|
session->decode_error = false;
|
2023-04-19 08:30:26 +00:00
|
|
|
session->owner = owner;
|
2022-03-16 08:52:11 +00:00
|
|
|
RpcHandlerDict_init(session->handlers);
|
|
|
|
|
|
|
|
session->decoded_message = malloc(sizeof(PB_Main));
|
2022-08-22 16:36:45 +00:00
|
|
|
session->decoded_message->cb_content.funcs.decode = rpc_pb_content_callback;
|
2022-03-16 08:52:11 +00:00
|
|
|
session->decoded_message->cb_content.arg = session;
|
|
|
|
|
|
|
|
session->system_contexts = malloc(COUNT_OF(rpc_systems) * sizeof(void*));
|
2022-05-06 13:37:10 +00:00
|
|
|
for(size_t i = 0; i < COUNT_OF(rpc_systems); ++i) {
|
2022-03-16 08:52:11 +00:00
|
|
|
session->system_contexts[i] = rpc_systems[i].alloc(session);
|
2021-10-12 11:48:34 +00:00
|
|
|
}
|
2022-03-16 08:52:11 +00:00
|
|
|
|
|
|
|
RpcHandler rpc_handler = {
|
|
|
|
.message_handler = rpc_close_session_process,
|
|
|
|
.decode_submessage = NULL,
|
|
|
|
.context = session,
|
|
|
|
};
|
|
|
|
rpc_add_handler(session, PB_Main_stop_session_tag, &rpc_handler);
|
|
|
|
|
2022-11-23 12:49:17 +00:00
|
|
|
session->thread = furi_thread_alloc_ex("RpcSessionWorker", 3072, rpc_session_worker, session);
|
2022-03-16 08:52:11 +00:00
|
|
|
|
|
|
|
furi_thread_set_state_context(session->thread, session);
|
2023-05-05 12:40:55 +00:00
|
|
|
furi_thread_set_state_callback(session->thread, rpc_session_thread_state_callback);
|
2022-03-16 08:52:11 +00:00
|
|
|
|
|
|
|
furi_thread_start(session->thread);
|
|
|
|
|
|
|
|
return session;
|
|
|
|
}
|
|
|
|
|
|
|
|
void rpc_session_close(RpcSession* session) {
|
|
|
|
furi_assert(session);
|
|
|
|
furi_assert(session->rpc);
|
|
|
|
|
|
|
|
rpc_session_set_send_bytes_callback(session, NULL);
|
|
|
|
rpc_session_set_close_callback(session, NULL);
|
|
|
|
rpc_session_set_buffer_is_empty_callback(session, NULL);
|
2022-06-20 14:54:48 +00:00
|
|
|
furi_thread_flags_set(furi_thread_get_id(session->thread), RpcEvtDisconnect);
|
2022-03-16 08:52:11 +00:00
|
|
|
}
|
|
|
|
|
2022-08-27 04:25:47 +00:00
|
|
|
void rpc_on_system_start(void* p) {
|
2022-05-06 13:37:10 +00:00
|
|
|
UNUSED(p);
|
2022-03-16 08:52:11 +00:00
|
|
|
Rpc* rpc = malloc(sizeof(Rpc));
|
|
|
|
|
2022-07-20 10:56:33 +00:00
|
|
|
rpc->busy_mutex = furi_mutex_alloc(FuriMutexTypeNormal);
|
2022-03-16 08:52:11 +00:00
|
|
|
|
2022-07-26 12:21:51 +00:00
|
|
|
Cli* cli = furi_record_open(RECORD_CLI);
|
2022-03-16 08:52:11 +00:00
|
|
|
cli_add_command(
|
|
|
|
cli, "start_rpc_session", CliCommandFlagParallelSafe, rpc_cli_command_start_session, rpc);
|
|
|
|
|
2022-07-26 12:21:51 +00:00
|
|
|
furi_record_create(RECORD_RPC, rpc);
|
2021-10-12 11:48:34 +00:00
|
|
|
}
|
|
|
|
|
2022-03-16 08:52:11 +00:00
|
|
|
void rpc_add_handler(RpcSession* session, pb_size_t message_tag, RpcHandler* handler) {
|
|
|
|
furi_assert(RpcHandlerDict_get(session->handlers, message_tag) == NULL);
|
|
|
|
|
|
|
|
RpcHandlerDict_set_at(session->handlers, message_tag, *handler);
|
|
|
|
}
|
|
|
|
|
2022-03-17 09:44:54 +00:00
|
|
|
void rpc_send(RpcSession* session, PB_Main* message) {
|
2022-03-16 08:52:11 +00:00
|
|
|
furi_assert(session);
|
|
|
|
furi_assert(message);
|
2022-03-17 09:44:54 +00:00
|
|
|
|
2022-03-16 08:52:11 +00:00
|
|
|
pb_ostream_t ostream = PB_OSTREAM_SIZING;
|
|
|
|
|
|
|
|
#if SRV_RPC_DEBUG
|
|
|
|
FURI_LOG_I(TAG, "OUTPUT:");
|
2022-08-22 16:36:45 +00:00
|
|
|
rpc_debug_print_message(message);
|
2022-03-16 08:52:11 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
bool result = pb_encode_ex(&ostream, &PB_Main_msg, message, PB_ENCODE_DELIMITED);
|
|
|
|
furi_check(result && ostream.bytes_written);
|
|
|
|
|
|
|
|
uint8_t* buffer = malloc(ostream.bytes_written);
|
|
|
|
ostream = pb_ostream_from_buffer(buffer, ostream.bytes_written);
|
|
|
|
|
|
|
|
pb_encode_ex(&ostream, &PB_Main_msg, message, PB_ENCODE_DELIMITED);
|
2021-10-12 11:48:34 +00:00
|
|
|
|
2022-03-16 08:52:11 +00:00
|
|
|
#if SRV_RPC_DEBUG
|
2022-08-22 16:36:45 +00:00
|
|
|
rpc_debug_print_data("OUTPUT", buffer, ostream.bytes_written);
|
2022-03-16 08:52:11 +00:00
|
|
|
#endif
|
|
|
|
|
2022-07-20 10:56:33 +00:00
|
|
|
furi_mutex_acquire(session->callbacks_mutex, FuriWaitForever);
|
2022-03-16 08:52:11 +00:00
|
|
|
if(session->send_bytes_callback) {
|
|
|
|
session->send_bytes_callback(session->context, buffer, ostream.bytes_written);
|
|
|
|
}
|
2022-07-20 10:56:33 +00:00
|
|
|
furi_mutex_release(session->callbacks_mutex);
|
2022-03-16 08:52:11 +00:00
|
|
|
|
|
|
|
free(buffer);
|
2022-03-17 09:44:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void rpc_send_and_release(RpcSession* session, PB_Main* message) {
|
|
|
|
rpc_send(session, message);
|
2022-03-16 08:52:11 +00:00
|
|
|
pb_release(&PB_Main_msg, message);
|
2021-10-12 11:48:34 +00:00
|
|
|
}
|
|
|
|
|
2022-03-16 08:52:11 +00:00
|
|
|
void rpc_send_and_release_empty(RpcSession* session, 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,
|
|
|
|
};
|
2022-03-16 08:52:11 +00:00
|
|
|
rpc_send_and_release(session, &message);
|
2021-10-12 11:48:34 +00:00
|
|
|
pb_release(&PB_Main_msg, &message);
|
|
|
|
}
|