rpc.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #pragma once
  2. #include <stddef.h>
  3. #include <stdint.h>
  4. #include <stdbool.h>
  5. #include "cmsis_os.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. /** Open RPC session
  21. *
  22. * USAGE:
  23. * 1) rpc_session_open();
  24. * 2) rpc_session_set_context();
  25. * 3) rpc_session_set_send_bytes_callback();
  26. * 4) rpc_session_set_close_callback();
  27. * 5) while(1) {
  28. * rpc_session_feed();
  29. * }
  30. * 6) rpc_session_close();
  31. *
  32. *
  33. * @param rpc instance
  34. * @return pointer to RpcSession descriptor, or
  35. * NULL if RPC is busy and can't open session now
  36. */
  37. RpcSession* rpc_session_open(Rpc* rpc);
  38. /** Close RPC session
  39. * It is guaranteed that no callbacks will be called
  40. * as soon as session is closed. So no need in setting
  41. * callbacks to NULL after session close.
  42. *
  43. * @param session pointer to RpcSession descriptor
  44. */
  45. void rpc_session_close(RpcSession* session);
  46. /** Set session context for callbacks to pass
  47. *
  48. * @param session pointer to RpcSession descriptor
  49. * @param context context to pass to callbacks
  50. */
  51. void rpc_session_set_context(RpcSession* session, void* context);
  52. /** Set callback to send bytes to client
  53. * WARN: It's forbidden to call RPC API within RpcSendBytesCallback
  54. *
  55. * @param session pointer to RpcSession descriptor
  56. * @param callback callback to send bytes to client (can be NULL)
  57. */
  58. void rpc_session_set_send_bytes_callback(RpcSession* session, RpcSendBytesCallback callback);
  59. /** Set callback to notify that buffer is empty
  60. *
  61. * @param session pointer to RpcSession descriptor
  62. * @param callback callback to notify client that buffer is empty (can be NULL)
  63. */
  64. void rpc_session_set_buffer_is_empty_callback(
  65. RpcSession* session,
  66. RpcBufferIsEmptyCallback callback);
  67. /** Set callback to be called when RPC command to close session is received
  68. * WARN: It's forbidden to call RPC API within RpcSessionClosedCallback
  69. *
  70. * @param session pointer to RpcSession descriptor
  71. * @param callback callback to inform about RPC close session command (can be NULL)
  72. */
  73. void rpc_session_set_close_callback(RpcSession* session, RpcSessionClosedCallback callback);
  74. /** Give bytes to RPC service to decode them and perform command
  75. *
  76. * @param session pointer to RpcSession descriptor
  77. * @param buffer buffer to provide to RPC service
  78. * @param size size of buffer
  79. * @param timeout max timeout to wait till all buffer will be consumed
  80. *
  81. * @return actually consumed bytes
  82. */
  83. size_t rpc_session_feed(RpcSession* session, uint8_t* buffer, size_t size, TickType_t timeout);
  84. /** Get available size of RPC buffer
  85. *
  86. * @param session pointer to RpcSession descriptor
  87. *
  88. * @return bytes available in buffer
  89. */
  90. size_t rpc_session_get_available_size(RpcSession* session);