[FL-2242] RPC: Wait for session termination in unit tests (#1005)
* rpc: session termination callback * grammar fixes Co-authored-by: あく <alleteam@gmail.com>
This commit is contained in:
@@ -54,6 +54,7 @@ struct RpcSession {
|
||||
RpcSendBytesCallback send_bytes_callback;
|
||||
RpcBufferIsEmptyCallback buffer_is_empty_callback;
|
||||
RpcSessionClosedCallback closed_callback;
|
||||
RpcSessionTerminatedCallback terminated_callback;
|
||||
void* context;
|
||||
osMutexId_t callbacks_mutex;
|
||||
Rpc* rpc;
|
||||
@@ -429,6 +430,7 @@ static void rpc_free_session(RpcSession* session) {
|
||||
session->closed_callback = NULL;
|
||||
session->send_bytes_callback = NULL;
|
||||
session->buffer_is_empty_callback = NULL;
|
||||
session->terminated_callback = NULL;
|
||||
}
|
||||
|
||||
void rpc_session_set_context(RpcSession* session, void* context) {
|
||||
@@ -472,6 +474,18 @@ void rpc_session_set_buffer_is_empty_callback(
|
||||
osMutexRelease(session->callbacks_mutex);
|
||||
}
|
||||
|
||||
void rpc_session_set_terminated_callback(
|
||||
RpcSession* session,
|
||||
RpcSessionTerminatedCallback callback) {
|
||||
furi_assert(session);
|
||||
furi_assert(session->rpc);
|
||||
furi_assert(session->rpc->busy);
|
||||
|
||||
osMutexAcquire(session->callbacks_mutex, osWaitForever);
|
||||
session->terminated_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
|
||||
@@ -665,6 +679,11 @@ int32_t rpc_srv(void* p) {
|
||||
|
||||
if(rpc->session.terminate) {
|
||||
FURI_LOG_D(TAG, "Session terminated");
|
||||
osMutexAcquire(rpc->session.callbacks_mutex, osWaitForever);
|
||||
if(rpc->session.terminated_callback) {
|
||||
rpc->session.terminated_callback(rpc->session.context);
|
||||
}
|
||||
osMutexRelease(rpc->session.callbacks_mutex);
|
||||
osEventFlagsClear(rpc->events, RPC_EVENTS_ALL);
|
||||
rpc_free_session(&rpc->session);
|
||||
rpc->busy = false;
|
||||
|
12
applications/rpc/rpc.h
Executable file → Normal file
12
applications/rpc/rpc.h
Executable file → Normal file
@@ -21,6 +21,9 @@ typedef void (*RpcBufferIsEmptyCallback)(void* context);
|
||||
* is received. Any other actions lays on transport layer.
|
||||
* No destruction or session close preformed. */
|
||||
typedef void (*RpcSessionClosedCallback)(void* context);
|
||||
/** Callback to notify transport layer that session was closed
|
||||
* and all operations were finished */
|
||||
typedef void (*RpcSessionTerminatedCallback)(void* context);
|
||||
|
||||
/** Open RPC session
|
||||
*
|
||||
@@ -82,6 +85,15 @@ void rpc_session_set_buffer_is_empty_callback(
|
||||
*/
|
||||
void rpc_session_set_close_callback(RpcSession* session, RpcSessionClosedCallback callback);
|
||||
|
||||
/** Set callback to be called when RPC session is closed
|
||||
*
|
||||
* @param session pointer to RpcSession descriptor
|
||||
* @param callback callback to inform about RPC session state
|
||||
*/
|
||||
void rpc_session_set_terminated_callback(
|
||||
RpcSession* session,
|
||||
RpcSessionTerminatedCallback callback);
|
||||
|
||||
/** Give bytes to RPC service to decode them and perform command
|
||||
*
|
||||
* @param session pointer to RpcSession descriptor
|
||||
|
Reference in New Issue
Block a user