rpc.h 3.9 KB

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