2021-10-12 11:48:34 +00:00
|
|
|
#pragma once
|
2021-12-21 12:16:25 +00:00
|
|
|
|
2021-10-12 11:48:34 +00:00
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdint.h>
|
2021-10-26 16:05:28 +00:00
|
|
|
#include <stdbool.h>
|
2021-12-21 12:16:25 +00:00
|
|
|
#include <furi.h>
|
2021-10-12 11:48:34 +00:00
|
|
|
|
2022-09-14 16:11:38 +00:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2021-11-08 19:41:40 +00:00
|
|
|
#define RPC_BUFFER_SIZE (1024)
|
|
|
|
|
2022-07-26 12:21:51 +00:00
|
|
|
#define RECORD_RPC "rpc"
|
|
|
|
|
2021-10-26 16:05:28 +00:00
|
|
|
/** Rpc interface. Used for opening session only. */
|
2021-10-12 11:48:34 +00:00
|
|
|
typedef struct Rpc Rpc;
|
2021-10-26 16:05:28 +00:00
|
|
|
/** Rpc session interface */
|
2021-10-12 11:48:34 +00:00
|
|
|
typedef struct RpcSession RpcSession;
|
|
|
|
|
2021-10-26 16:05:28 +00:00
|
|
|
/** Callback to send to client any data (e.g. response to command) */
|
2021-10-12 11:48:34 +00:00
|
|
|
typedef void (*RpcSendBytesCallback)(void* context, uint8_t* bytes, size_t bytes_len);
|
2021-11-08 19:41:40 +00:00
|
|
|
/** Callback to notify client that buffer is empty */
|
|
|
|
typedef void (*RpcBufferIsEmptyCallback)(void* context);
|
2021-10-26 16:05:28 +00:00
|
|
|
/** Callback to notify transport layer that close_session command
|
|
|
|
* is received. Any other actions lays on transport layer.
|
2022-11-28 16:51:51 +00:00
|
|
|
* No destruction or session close performed. */
|
2021-10-26 16:05:28 +00:00
|
|
|
typedef void (*RpcSessionClosedCallback)(void* context);
|
2022-02-24 12:08:58 +00:00
|
|
|
/** Callback to notify transport layer that session was closed
|
|
|
|
* and all operations were finished */
|
|
|
|
typedef void (*RpcSessionTerminatedCallback)(void* context);
|
2021-10-12 11:48:34 +00:00
|
|
|
|
2023-04-19 08:30:26 +00:00
|
|
|
/** RPC owner */
|
|
|
|
typedef enum {
|
|
|
|
RpcOwnerUnknown = 0,
|
|
|
|
RpcOwnerBle,
|
|
|
|
RpcOwnerUsb,
|
2024-01-16 09:18:56 +00:00
|
|
|
RpcOwnerUart,
|
2023-04-19 08:30:26 +00:00
|
|
|
RpcOwnerCount,
|
|
|
|
} RpcOwner;
|
|
|
|
|
|
|
|
/** Get RPC session owner
|
|
|
|
*
|
|
|
|
* @param session pointer to RpcSession descriptor
|
|
|
|
* @return session owner
|
|
|
|
*/
|
|
|
|
RpcOwner rpc_session_get_owner(RpcSession* session);
|
|
|
|
|
2021-10-26 16:05:28 +00:00
|
|
|
/** 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
|
2023-04-19 08:30:26 +00:00
|
|
|
* @param owner owner of session
|
2021-10-26 16:05:28 +00:00
|
|
|
* @return pointer to RpcSession descriptor, or
|
|
|
|
* NULL if RPC is busy and can't open session now
|
|
|
|
*/
|
2023-04-19 08:30:26 +00:00
|
|
|
RpcSession* rpc_session_open(Rpc* rpc, RpcOwner owner);
|
2021-10-26 16:05:28 +00:00
|
|
|
|
|
|
|
/** 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);
|
|
|
|
|
2021-11-08 19:41:40 +00:00
|
|
|
/** Set callback to notify that buffer is empty
|
|
|
|
*
|
|
|
|
* @param session pointer to RpcSession descriptor
|
|
|
|
* @param callback callback to notify client that buffer is empty (can be NULL)
|
2024-02-16 07:20:45 +00:00
|
|
|
* @param context context to pass to callback
|
2021-11-08 19:41:40 +00:00
|
|
|
*/
|
|
|
|
void rpc_session_set_buffer_is_empty_callback(
|
|
|
|
RpcSession* session,
|
|
|
|
RpcBufferIsEmptyCallback callback);
|
|
|
|
|
2021-10-26 16:05:28 +00:00
|
|
|
/** 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);
|
|
|
|
|
2022-02-24 12:08:58 +00:00
|
|
|
/** 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);
|
|
|
|
|
2021-10-26 16:05:28 +00:00
|
|
|
/** 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
|
|
|
|
*/
|
2024-01-16 09:18:56 +00:00
|
|
|
size_t rpc_session_feed(RpcSession* session, const uint8_t* buffer, size_t size, uint32_t timeout);
|
2021-11-08 19:41:40 +00:00
|
|
|
|
|
|
|
/** Get available size of RPC buffer
|
|
|
|
*
|
|
|
|
* @param session pointer to RpcSession descriptor
|
|
|
|
*
|
|
|
|
* @return bytes available in buffer
|
|
|
|
*/
|
|
|
|
size_t rpc_session_get_available_size(RpcSession* session);
|
2022-09-14 16:11:38 +00:00
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
2024-01-16 09:18:56 +00:00
|
|
|
#endif
|