rpc.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. /** RPC owner */
  27. typedef enum {
  28. RpcOwnerUnknown = 0,
  29. RpcOwnerBle,
  30. RpcOwnerUsb,
  31. RpcOwnerCount,
  32. } RpcOwner;
  33. /** Get RPC session owner
  34. *
  35. * @param session pointer to RpcSession descriptor
  36. * @return session owner
  37. */
  38. RpcOwner rpc_session_get_owner(RpcSession* session);
  39. /** Open RPC session
  40. *
  41. * USAGE:
  42. * 1) rpc_session_open();
  43. * 2) rpc_session_set_context();
  44. * 3) rpc_session_set_send_bytes_callback();
  45. * 4) rpc_session_set_close_callback();
  46. * 5) while(1) {
  47. * rpc_session_feed();
  48. * }
  49. * 6) rpc_session_close();
  50. *
  51. *
  52. * @param rpc instance
  53. * @param owner owner of session
  54. * @return pointer to RpcSession descriptor, or
  55. * NULL if RPC is busy and can't open session now
  56. */
  57. RpcSession* rpc_session_open(Rpc* rpc, RpcOwner owner);
  58. /** Close RPC session
  59. * It is guaranteed that no callbacks will be called
  60. * as soon as session is closed. So no need in setting
  61. * callbacks to NULL after session close.
  62. *
  63. * @param session pointer to RpcSession descriptor
  64. */
  65. void rpc_session_close(RpcSession* session);
  66. /** Set session context for callbacks to pass
  67. *
  68. * @param session pointer to RpcSession descriptor
  69. * @param context context to pass to callbacks
  70. */
  71. void rpc_session_set_context(RpcSession* session, void* context);
  72. /** Set callback to send bytes to client
  73. * WARN: It's forbidden to call RPC API within RpcSendBytesCallback
  74. *
  75. * @param session pointer to RpcSession descriptor
  76. * @param callback callback to send bytes to client (can be NULL)
  77. */
  78. void rpc_session_set_send_bytes_callback(RpcSession* session, RpcSendBytesCallback callback);
  79. /** Set callback to notify that buffer is empty
  80. *
  81. * @param session pointer to RpcSession descriptor
  82. * @param callback callback to notify client that buffer is empty (can be NULL)
  83. */
  84. void rpc_session_set_buffer_is_empty_callback(
  85. RpcSession* session,
  86. RpcBufferIsEmptyCallback callback);
  87. /** Set callback to be called when RPC command to close session is received
  88. * WARN: It's forbidden to call RPC API within RpcSessionClosedCallback
  89. *
  90. * @param session pointer to RpcSession descriptor
  91. * @param callback callback to inform about RPC close session command (can be NULL)
  92. */
  93. void rpc_session_set_close_callback(RpcSession* session, RpcSessionClosedCallback callback);
  94. /** Set callback to be called when RPC session is closed
  95. *
  96. * @param session pointer to RpcSession descriptor
  97. * @param callback callback to inform about RPC session state
  98. */
  99. void rpc_session_set_terminated_callback(
  100. RpcSession* session,
  101. RpcSessionTerminatedCallback callback);
  102. /** Give bytes to RPC service to decode them and perform command
  103. *
  104. * @param session pointer to RpcSession descriptor
  105. * @param buffer buffer to provide to RPC service
  106. * @param size size of buffer
  107. * @param timeout max timeout to wait till all buffer will be consumed
  108. *
  109. * @return actually consumed bytes
  110. */
  111. size_t rpc_session_feed(RpcSession* session, uint8_t* buffer, size_t size, TickType_t timeout);
  112. /** Get available size of RPC buffer
  113. *
  114. * @param session pointer to RpcSession descriptor
  115. *
  116. * @return bytes available in buffer
  117. */
  118. size_t rpc_session_get_available_size(RpcSession* session);
  119. #ifdef __cplusplus
  120. }
  121. #endif