rpmsg_queue.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. * Copyright (c) 2014, Mentor Graphics Corporation
  3. * Copyright (c) 2015 Xilinx, Inc.
  4. * Copyright (c) 2016 Freescale Semiconductor, Inc.
  5. * Copyright 2016-2022 NXP
  6. * Copyright 2021 ACRIOS Systems s.r.o.
  7. * All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright notice,
  13. * this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright notice,
  15. * this list of conditions and the following disclaimer in the documentation
  16. * and/or other materials provided with the distribution.
  17. * 3. Neither the name of the copyright holder nor the names of its
  18. * contributors may be used to endorse or promote products derived from this
  19. * software without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  22. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  25. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  26. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  27. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  28. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  29. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  30. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  31. * POSSIBILITY OF SUCH DAMAGE.
  32. */
  33. #include "rpmsg_lite.h"
  34. #include "rpmsg_queue.h"
  35. int32_t rpmsg_queue_rx_cb(void *payload, uint32_t payload_len, uint32_t src, void *priv)
  36. {
  37. rpmsg_queue_rx_cb_data_t msg;
  38. RL_ASSERT(priv != RL_NULL);
  39. msg.data = payload;
  40. msg.len = payload_len;
  41. msg.src = src;
  42. /* if message is successfully added into queue then hold rpmsg buffer */
  43. if (0 != env_put_queue(priv, &msg, 0))
  44. {
  45. /* hold the rx buffer */
  46. return RL_HOLD;
  47. }
  48. return RL_RELEASE;
  49. }
  50. #if defined(RL_USE_STATIC_API) && (RL_USE_STATIC_API == 1)
  51. rpmsg_queue_handle rpmsg_queue_create(struct rpmsg_lite_instance *rpmsg_lite_dev,
  52. uint8_t *queue_storage,
  53. rpmsg_static_queue_ctxt *queue_ctxt)
  54. #else
  55. rpmsg_queue_handle rpmsg_queue_create(struct rpmsg_lite_instance *rpmsg_lite_dev)
  56. #endif
  57. {
  58. int32_t status;
  59. void *q = RL_NULL;
  60. if (rpmsg_lite_dev == RL_NULL)
  61. {
  62. return RL_NULL;
  63. }
  64. /* create message queue for channel default endpoint */
  65. #if defined(RL_USE_STATIC_API) && (RL_USE_STATIC_API == 1)
  66. if ((queue_storage == RL_NULL) || (queue_ctxt == RL_NULL))
  67. {
  68. return RL_NULL;
  69. }
  70. status = env_create_queue(&q, 2 * (int32_t)(rpmsg_lite_dev->rvq->vq_nentries),
  71. (int32_t)sizeof(rpmsg_queue_rx_cb_data_t), queue_storage, queue_ctxt);
  72. #else
  73. status = env_create_queue(&q, 2 * (int32_t)(rpmsg_lite_dev->rvq->vq_nentries),
  74. (int32_t)sizeof(rpmsg_queue_rx_cb_data_t));
  75. #endif
  76. if ((status != 0) || (q == RL_NULL))
  77. {
  78. return RL_NULL;
  79. }
  80. return ((rpmsg_queue_handle)q);
  81. }
  82. int32_t rpmsg_queue_destroy(struct rpmsg_lite_instance *rpmsg_lite_dev, rpmsg_queue_handle q)
  83. {
  84. if (rpmsg_lite_dev == RL_NULL)
  85. {
  86. return RL_ERR_PARAM;
  87. }
  88. if (q == RL_NULL)
  89. {
  90. return RL_ERR_PARAM;
  91. }
  92. env_delete_queue((void *)q);
  93. return RL_SUCCESS;
  94. }
  95. int32_t rpmsg_queue_recv(struct rpmsg_lite_instance *rpmsg_lite_dev,
  96. rpmsg_queue_handle q,
  97. uint32_t *src,
  98. char *data,
  99. uint32_t maxlen,
  100. uint32_t *len,
  101. uintptr_t timeout)
  102. {
  103. rpmsg_queue_rx_cb_data_t msg = {0};
  104. int32_t retval = RL_SUCCESS;
  105. if (rpmsg_lite_dev == RL_NULL)
  106. {
  107. return RL_ERR_PARAM;
  108. }
  109. if (q == RL_NULL)
  110. {
  111. return RL_ERR_PARAM;
  112. }
  113. if (data == RL_NULL)
  114. {
  115. return RL_ERR_PARAM;
  116. }
  117. /* Get an element out of the message queue for the selected endpoint */
  118. if (0 != env_get_queue((void *)q, &msg, timeout))
  119. {
  120. if (src != RL_NULL)
  121. {
  122. *src = msg.src;
  123. }
  124. if (len != RL_NULL)
  125. {
  126. *len = msg.len;
  127. }
  128. if (maxlen >= msg.len)
  129. {
  130. env_memcpy(data, msg.data, msg.len);
  131. }
  132. else
  133. {
  134. retval = RL_ERR_BUFF_SIZE;
  135. }
  136. /* Release used buffer. */
  137. return ((RL_SUCCESS == rpmsg_lite_release_rx_buffer(rpmsg_lite_dev, msg.data)) ? retval : RL_ERR_PARAM);
  138. }
  139. else
  140. {
  141. return RL_ERR_NO_BUFF; /* failed */
  142. }
  143. }
  144. int32_t rpmsg_queue_recv_nocopy(struct rpmsg_lite_instance *rpmsg_lite_dev,
  145. rpmsg_queue_handle q,
  146. uint32_t *src,
  147. char **data,
  148. uint32_t *len,
  149. uintptr_t timeout)
  150. {
  151. rpmsg_queue_rx_cb_data_t msg = {0};
  152. if (rpmsg_lite_dev == RL_NULL)
  153. {
  154. return RL_ERR_PARAM;
  155. }
  156. if (data == RL_NULL)
  157. {
  158. return RL_ERR_PARAM;
  159. }
  160. if (q == RL_NULL)
  161. {
  162. return RL_ERR_PARAM;
  163. }
  164. /* Get an element out of the message queue for the selected endpoint */
  165. if (0 != env_get_queue((void *)q, &msg, timeout))
  166. {
  167. if (src != RL_NULL)
  168. {
  169. *src = msg.src;
  170. }
  171. if (len != RL_NULL)
  172. {
  173. *len = msg.len;
  174. }
  175. *data = msg.data;
  176. return RL_SUCCESS; /* success */
  177. }
  178. return RL_ERR_NO_BUFF; /* failed */
  179. }
  180. int32_t rpmsg_queue_nocopy_free(struct rpmsg_lite_instance *rpmsg_lite_dev, void *data)
  181. {
  182. if (rpmsg_lite_dev == RL_NULL)
  183. {
  184. return RL_ERR_PARAM;
  185. }
  186. if (data == RL_NULL)
  187. {
  188. return RL_ERR_PARAM;
  189. }
  190. /* Release used buffer. */
  191. return ((RL_SUCCESS == rpmsg_lite_release_rx_buffer(rpmsg_lite_dev, data)) ? RL_SUCCESS : RL_ERR_PARAM);
  192. }
  193. int32_t rpmsg_queue_get_current_size(rpmsg_queue_handle q)
  194. {
  195. if (q == RL_NULL)
  196. {
  197. return RL_ERR_PARAM;
  198. }
  199. /* Return actual queue size. */
  200. return env_get_current_queue_size((void *)q);
  201. }