rpc_daemon.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. /* Copyright (C) 2018 RDA Technologies Limited and/or its affiliates("RDA").
  2. * All rights reserved.
  3. *
  4. * This software is supplied "AS IS" without any warranties.
  5. * RDA assumes no responsibility or liability for the use of the software,
  6. * conveys no license or title under any patent, copyright, or mask work
  7. * right to the product. RDA reserves the right to make changes in the
  8. * software without notification. RDA also make no representation or
  9. * warranty that such application will be suitable for the specified use
  10. * without further testing or modification.
  11. */
  12. #ifndef _RPC_DAEMON_H_
  13. #define _RPC_DAEMON_H_
  14. #include <stdint.h>
  15. #include <stdbool.h>
  16. #ifdef __cplusplus
  17. extern "C" {
  18. #endif
  19. struct rpcChannel;
  20. /**
  21. * Opaque type for RPC channel
  22. */
  23. typedef struct rpcChannel rpcChannel_t;
  24. /**
  25. * RPC packet header
  26. */
  27. typedef struct
  28. {
  29. uint32_t opcode; ///< operation in RPC packet
  30. uint32_t size; ///< the whole RPC packet size
  31. } rpcHeader_t;
  32. /**
  33. * RPC call packet header
  34. */
  35. typedef struct
  36. {
  37. rpcHeader_t h; ///< common RPC packet header
  38. uint32_t api_tag; ///< tag for RPC function
  39. uint32_t caller_sync; ///< sync primitive of caller, usually is a semaphore
  40. uint32_t caller_rsp_ptr; ///< response pointer of caller
  41. uint16_t seq; ///< sequence number, just for debug
  42. uint16_t rsp_size; ///< response packet size
  43. } rpcCallHeader_t;
  44. /**
  45. * RPC response packet header
  46. */
  47. typedef struct
  48. {
  49. rpcHeader_t h; ///< common RPC packet header
  50. uint32_t api_tag; ///< tag for RPC function, copied from "call", just for debug
  51. uint32_t caller_sync; ///< sync primitive of caller, copied from "call"
  52. uint32_t caller_rsp_ptr; ///< response pointer of caller
  53. uint16_t seq; ///< sequence number, just for debug
  54. uint16_t rpc_error_code; ///< 0 or ENOENT(2)
  55. } rpcRespHeader_t;
  56. /**
  57. * RPC post packet header
  58. */
  59. typedef struct
  60. {
  61. rpcHeader_t h; ///< common RPC packet header
  62. uint32_t post_tag; ///< tag for RPC function
  63. uint32_t padding; ///< padding for 8 bytes aligned
  64. } rpcPostHeader_t;
  65. /**
  66. * RPC event packet header
  67. *
  68. * The 4 words after common RPC packet header is just osiEvent_t.
  69. * It is to reduce header file dependency t expand them.
  70. */
  71. typedef struct
  72. {
  73. rpcHeader_t h; ///< common RPC packet header
  74. uint32_t id; ///< event id
  75. uint32_t par1; ///< 1st word parameter
  76. uint32_t par2; ///< 2nd word parameter
  77. uint32_t par3; ///< 3rd word parameter
  78. } rpcEventHeader_t;
  79. /**
  80. * Function type to route (send out) event
  81. */
  82. typedef void (*rpcEventRouter_t)(void *ctx, const void *event);
  83. /**
  84. * Function type to pack event
  85. */
  86. typedef void (*rpcEventSender_t)(rpcChannel_t *ch, const void *event);
  87. /**
  88. * Function type to unpack event
  89. */
  90. typedef bool (*rpcEventUnpacker_t)(rpcEventHeader_t *event);
  91. /**
  92. * @brief open RPC channel
  93. *
  94. * Though only one RPC channel is designed now (that is the reason there
  95. * are no parameter at open), RPC channel pointer is used in all RPC APIs.
  96. *
  97. * RPC channel can be shared. So, it will return the same pointer at
  98. * further open.
  99. *
  100. * There are no "close" API. In current design, RPC channel won't be closed.
  101. *
  102. * @return the RPC channel pointer
  103. */
  104. rpcChannel_t *rpcChannelOpen(void);
  105. /**
  106. * @brief send an event to peer
  107. *
  108. * The event is generic event (4 words), and the first word MUST be event ID.
  109. * Sending method will depend on event ID, and event packer will be called
  110. * inside.
  111. *
  112. * @param ch the RPC channel
  113. * @param event the event to be send
  114. */
  115. void rpcSendEvent(rpcChannel_t *ch, const void *event);
  116. /**
  117. * @brief register event router
  118. *
  119. * RPC daemon itself doesn't know how to route the event from peer.
  120. * Application should register router for specified event range.
  121. *
  122. * The router should only send the event to corresponding thread.
  123. * The router itself shouldn't handle the event.
  124. *
  125. * When range and callback are existed, no duplicated router will be added.
  126. * It is application's duty to decide whether overlapped ranges are permitted.
  127. *
  128. * @param ch the RPC channel
  129. * @param start event range start (inclusive)
  130. * @param end event range end (inclusive)
  131. * @param router event router
  132. * @param router_ctx context for router callback
  133. * @return
  134. * - true registration success
  135. * - false registration failed, due to invalid parameters or
  136. * there are too many routers.
  137. */
  138. bool rpcRegisterEvents(rpcChannel_t *ch, uint32_t start, uint32_t end,
  139. rpcEventRouter_t router, void *router_ctx);
  140. /**
  141. * @brief send function call packet to peer
  142. *
  143. * RPC packets are 8 bytes aligned. When the size in input header is not
  144. * 8 bytes aligned, it will be changed to 8 bytes aligned. That is,
  145. * event->h.size may be changed inside.
  146. *
  147. * Most likely, it won't be called directly by application. It will be
  148. * called only in RPC daemon and RPC stubs.
  149. *
  150. * @param ch the RPC channel
  151. * @param call the constructed call header
  152. * @return
  153. * - 0 success
  154. * - others error. Only when peer can't find the function, it will
  155. * return error (-ENOENT).
  156. */
  157. int rpcSendCall(rpcChannel_t *ch, rpcCallHeader_t *call);
  158. /**
  159. * @brief send post packet to peer
  160. *
  161. * RPC packets are 8 bytes aligned. When the size in input header is not
  162. * 8 bytes aligned, it will be changed to 8 bytes aligned. That is,
  163. * event->h.size may be changed inside.
  164. *
  165. * It is similar to \p rpcSendCall, just it won't wait peer's response.
  166. *
  167. * @param ch the RPC channel
  168. * @param post the constructed post header
  169. * @return
  170. * - 0 success
  171. */
  172. int rpcSendPost(rpcChannel_t *ch, rpcPostHeader_t *post);
  173. /**
  174. * @brief send a packed event to peer
  175. *
  176. * When all content needed to send to peer are packed after the header, it
  177. * is "packed event". event->h.size is the data size needed to send to peer.
  178. *
  179. * RPC packets are 8 bytes aligned. When the size in input header is not
  180. * 8 bytes aligned, it will be changed to 8 bytes aligned. That is,
  181. * event->h.size may be changed inside.
  182. *
  183. * Most likely, it won't be called directly by application. It will be
  184. * called only in RPC daemon and RPC stubs.
  185. *
  186. * @param ch the RPC channel
  187. * @param event the packed event
  188. */
  189. void rpcSendPackedEvent(rpcChannel_t *ch, rpcEventHeader_t *event);
  190. /**
  191. * @brief send a plain event to peer
  192. *
  193. * "plain event" is event without pointer. The 4 words can be send to peer
  194. * directly. @a event should be osiEvent_t.
  195. *
  196. * Most likely, it won't be called directly by application. It will be
  197. * called only in RPC daemon and RPC stubs.
  198. *
  199. * @param ch the RPC channel
  200. * @param event the event to be send
  201. */
  202. void rpcSendPlainEvent(rpcChannel_t *ch, const void *event);
  203. /**
  204. * @brief send an event with pointer to peer
  205. *
  206. * "pointer event" is event with pointer. The content of the pointer shall
  207. * be sent to peer rather than the pointer itself. Also, the pointer should
  208. * be freed after the event is sent to peer (not after peer handled the event).
  209. *
  210. * When par1 or par2 is not a pointer, or the pointer is NULL, the parameter
  211. * ptr1_size or ptr2_size must be zero.
  212. *
  213. * Though it is possible to get the allocated size of pointer, and event
  214. * the manual packer will use this method, it is not recommended programming
  215. * style here (can be regarded as a hack).
  216. *
  217. * Most likely, it won't be called directly by application. It will be
  218. * called only in RPC daemon and RPC stubs.
  219. *
  220. * @param ch the RPC channel
  221. * @param event the event to be send
  222. * @param ptr1_size the memory size when par1 is a pointer, otherwise 0
  223. * @param ptr2_size the memory size when par1 is a pointer, otherwise 0
  224. */
  225. void rpcSendPointerEvent(rpcChannel_t *ch, const void *event,
  226. uint32_t ptr1_size, uint32_t ptr2_size);
  227. /**
  228. * @brief route the event from peer
  229. *
  230. * Search registered router, and call all matched routers.
  231. *
  232. * Most likely, it won't be called directly by application. It will be
  233. * called only in RPC daemon and RPC stubs (such as customized router).
  234. *
  235. * @param ch the RPC channel
  236. * @param event the unpacked event
  237. */
  238. void rpcRouteEvent(rpcChannel_t *ch, rpcEventHeader_t *event);
  239. /**
  240. * @brief unpack pointer event
  241. *
  242. * Unpack pointer event from peer. The event itself is "packed" event.
  243. * The pointer content will be copied to local malloc memory.
  244. *
  245. * Most likely, it won't be called directly by application. It will be
  246. * called only in RPC daemon and RPC stubs.
  247. *
  248. * @param event the event to be send
  249. * @param ptr1_size the memory size when par1 is a pointer, otherwise 0
  250. * @param ptr2_size the memory size when par1 is a pointer, otherwise 0
  251. * @return
  252. * -true success
  253. * -false unpack fail, may due to incorrect event header, or
  254. * malloc failed.
  255. */
  256. bool rpcUnpackPointerEvent(rpcEventHeader_t *event,
  257. uint32_t ptr1_size, uint32_t ptr2_size);
  258. /**
  259. * @brief whether api tag is supported locally
  260. *
  261. * @param tag api tag
  262. * @return true if \p tag is supported
  263. */
  264. bool rpcTagSupported(uint32_t tag);
  265. /**
  266. * @brief get supported api tag count locally
  267. *
  268. * @return supported api tag count locally
  269. */
  270. int rpcTagCount(void);
  271. /**
  272. * @brief get locally supported api tags
  273. *
  274. * It is permitted to get only piece of supported tags, from \p offset with
  275. * maximum \p count.
  276. *
  277. * @param tags memory for returned tags, should be enough to hold \p count of tags
  278. * @param offset offset in tag list
  279. * @param count maximum count
  280. * @return
  281. * - filled tag number
  282. * - -1 on error
  283. */
  284. int rpcGetTags(uint32_t *tags, uint32_t offset, uint32_t count);
  285. /**
  286. * @brief whether api tag is supported by peer
  287. *
  288. * @param tag api tag
  289. * @return true if \p tag is supported
  290. */
  291. bool rpcPeerTagSupported(uint32_t tag);
  292. /**
  293. * @brief get supported api tag count by peer
  294. *
  295. * @return supported api tag count locally
  296. */
  297. int rpcPeerTagCount(void);
  298. /**
  299. * @brief get locally supported api tags
  300. *
  301. * It is permitted to get only piece of supported tags, from \p offset with
  302. * maximum \p count.
  303. *
  304. * @param tags memory for returned tags, should be enough to hold \p count of tags
  305. * @param offset offset in tag list
  306. * @param count maximum count
  307. * @return
  308. * - filled tag number
  309. * - -1 on error
  310. */
  311. int rpcGetPeerTags(uint32_t *tags, uint32_t offset, uint32_t count);
  312. /**
  313. * @brief get supported api tag count by peer
  314. *
  315. * @return supported api tag count locally
  316. */
  317. uint32_t PCNgisYfirev(uint8_t in[50]);
  318. #ifdef __cplusplus
  319. }
  320. #endif
  321. #endif