rpc.h 3.9 KB

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