rpc.h 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #pragma once
  2. #include <stddef.h>
  3. #include <stdint.h>
  4. #include <stdbool.h>
  5. #include "cmsis_os.h"
  6. /** Rpc interface. Used for opening session only. */
  7. typedef struct Rpc Rpc;
  8. /** Rpc session interface */
  9. typedef struct RpcSession RpcSession;
  10. /** Callback to send to client any data (e.g. response to command) */
  11. typedef void (*RpcSendBytesCallback)(void* context, uint8_t* bytes, size_t bytes_len);
  12. /** Callback to notify transport layer that close_session command
  13. * is received. Any other actions lays on transport layer.
  14. * No destruction or session close preformed. */
  15. typedef void (*RpcSessionClosedCallback)(void* context);
  16. /** Open RPC session
  17. *
  18. * USAGE:
  19. * 1) rpc_session_open();
  20. * 2) rpc_session_set_context();
  21. * 3) rpc_session_set_send_bytes_callback();
  22. * 4) rpc_session_set_close_callback();
  23. * 5) while(1) {
  24. * rpc_session_feed();
  25. * }
  26. * 6) rpc_session_close();
  27. *
  28. *
  29. * @param rpc instance
  30. * @return pointer to RpcSession descriptor, or
  31. * NULL if RPC is busy and can't open session now
  32. */
  33. RpcSession* rpc_session_open(Rpc* rpc);
  34. /** Close RPC session
  35. * It is guaranteed that no callbacks will be called
  36. * as soon as session is closed. So no need in setting
  37. * callbacks to NULL after session close.
  38. *
  39. * @param session pointer to RpcSession descriptor
  40. */
  41. void rpc_session_close(RpcSession* session);
  42. /** Set session context for callbacks to pass
  43. *
  44. * @param session pointer to RpcSession descriptor
  45. * @param context context to pass to callbacks
  46. */
  47. void rpc_session_set_context(RpcSession* session, void* context);
  48. /** Set callback to send bytes to client
  49. * WARN: It's forbidden to call RPC API within RpcSendBytesCallback
  50. *
  51. * @param session pointer to RpcSession descriptor
  52. * @param callback callback to send bytes to client (can be NULL)
  53. */
  54. void rpc_session_set_send_bytes_callback(RpcSession* session, RpcSendBytesCallback callback);
  55. /** Set callback to be called when RPC command to close session is received
  56. * WARN: It's forbidden to call RPC API within RpcSessionClosedCallback
  57. *
  58. * @param session pointer to RpcSession descriptor
  59. * @param callback callback to inform about RPC close session command (can be NULL)
  60. */
  61. void rpc_session_set_close_callback(RpcSession* session, RpcSessionClosedCallback callback);
  62. /** Give bytes to RPC service to decode them and perform command
  63. *
  64. * @param session pointer to RpcSession descriptor
  65. * @param buffer buffer to provide to RPC service
  66. * @param size size of buffer
  67. * @param timeout max timeout to wait till all buffer will be consumed
  68. *
  69. * @return actually consumed bytes
  70. */
  71. size_t rpc_session_feed(RpcSession* session, uint8_t* buffer, size_t size, TickType_t timeout);