rpc.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #pragma once
  2. #include <stddef.h>
  3. #include <stdint.h>
  4. #include <stdbool.h>
  5. #include <furi.h>
  6. #ifdef __cplusplus
  7. extern "C" {
  8. #endif
  9. #define RPC_BUFFER_SIZE (1024)
  10. #define RECORD_RPC "rpc"
  11. /** Rpc interface. Used for opening session only. */
  12. typedef struct Rpc Rpc;
  13. /** Rpc session interface */
  14. typedef struct RpcSession RpcSession;
  15. /** Callback to send to client any data (e.g. response to command) */
  16. typedef void (*RpcSendBytesCallback)(void* context, uint8_t* bytes, size_t bytes_len);
  17. /** Callback to notify client that buffer is empty */
  18. typedef void (*RpcBufferIsEmptyCallback)(void* context);
  19. /** Callback to notify transport layer that close_session command
  20. * is received. Any other actions lays on transport layer.
  21. * No destruction or session close performed. */
  22. typedef void (*RpcSessionClosedCallback)(void* context);
  23. /** Callback to notify transport layer that session was closed
  24. * and all operations were finished */
  25. typedef void (*RpcSessionTerminatedCallback)(void* context);
  26. /** Open RPC session
  27. *
  28. * USAGE:
  29. * 1) rpc_session_open();
  30. * 2) rpc_session_set_context();
  31. * 3) rpc_session_set_send_bytes_callback();
  32. * 4) rpc_session_set_close_callback();
  33. * 5) while(1) {
  34. * rpc_session_feed();
  35. * }
  36. * 6) rpc_session_close();
  37. *
  38. *
  39. * @param rpc instance
  40. * @return pointer to RpcSession descriptor, or
  41. * NULL if RPC is busy and can't open session now
  42. */
  43. RpcSession* rpc_session_open(Rpc* rpc);
  44. /** Close RPC session
  45. * It is guaranteed that no callbacks will be called
  46. * as soon as session is closed. So no need in setting
  47. * callbacks to NULL after session close.
  48. *
  49. * @param session pointer to RpcSession descriptor
  50. */
  51. void rpc_session_close(RpcSession* session);
  52. /** Set session context for callbacks to pass
  53. *
  54. * @param session pointer to RpcSession descriptor
  55. * @param context context to pass to callbacks
  56. */
  57. void rpc_session_set_context(RpcSession* session, void* context);
  58. /** Set callback to send bytes to client
  59. * WARN: It's forbidden to call RPC API within RpcSendBytesCallback
  60. *
  61. * @param session pointer to RpcSession descriptor
  62. * @param callback callback to send bytes to client (can be NULL)
  63. */
  64. void rpc_session_set_send_bytes_callback(RpcSession* session, RpcSendBytesCallback callback);
  65. /** Set callback to notify that buffer is empty
  66. *
  67. * @param session pointer to RpcSession descriptor
  68. * @param callback callback to notify client that buffer is empty (can be NULL)
  69. */
  70. void rpc_session_set_buffer_is_empty_callback(
  71. RpcSession* session,
  72. RpcBufferIsEmptyCallback callback);
  73. /** Set callback to be called when RPC command to close session is received
  74. * WARN: It's forbidden to call RPC API within RpcSessionClosedCallback
  75. *
  76. * @param session pointer to RpcSession descriptor
  77. * @param callback callback to inform about RPC close session command (can be NULL)
  78. */
  79. void rpc_session_set_close_callback(RpcSession* session, RpcSessionClosedCallback callback);
  80. /** Set callback to be called when RPC session is closed
  81. *
  82. * @param session pointer to RpcSession descriptor
  83. * @param callback callback to inform about RPC session state
  84. */
  85. void rpc_session_set_terminated_callback(
  86. RpcSession* session,
  87. RpcSessionTerminatedCallback callback);
  88. /** Give bytes to RPC service to decode them and perform command
  89. *
  90. * @param session pointer to RpcSession descriptor
  91. * @param buffer buffer to provide to RPC service
  92. * @param size size of buffer
  93. * @param timeout max timeout to wait till all buffer will be consumed
  94. *
  95. * @return actually consumed bytes
  96. */
  97. size_t rpc_session_feed(RpcSession* session, uint8_t* buffer, size_t size, TickType_t timeout);
  98. /** Get available size of RPC buffer
  99. *
  100. * @param session pointer to RpcSession descriptor
  101. *
  102. * @return bytes available in buffer
  103. */
  104. size_t rpc_session_get_available_size(RpcSession* session);
  105. #ifdef __cplusplus
  106. }
  107. #endif