rpmsg_ns.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. * Copyright (c) 2014, Mentor Graphics Corporation
  3. * Copyright (c) 2015 Xilinx, Inc.
  4. * Copyright (c) 2016 Freescale Semiconductor, Inc.
  5. * Copyright 2016-2019 NXP
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright notice,
  12. * this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright notice,
  14. * this list of conditions and the following disclaimer in the documentation
  15. * and/or other materials provided with the distribution.
  16. * 3. Neither the name of the copyright holder nor the names of its
  17. * contributors may be used to endorse or promote products derived from this
  18. * software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  24. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  25. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  26. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  27. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  30. * POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. #include "rpmsg_lite.h"
  33. #include "rpmsg_ns.h"
  34. #include <stdint.h>
  35. #define RL_NS_NAME_SIZE (32)
  36. /*!
  37. * struct rpmsg_ns_msg - dynamic name service announcement message
  38. * @name: name of remote service that is published
  39. * @addr: address of remote service that is published
  40. * @flags: indicates whether service is created or destroyed
  41. *
  42. * This message is sent across to publish a new service, or announce
  43. * about its removal. When we receive these messages, an appropriate
  44. * rpmsg channel (i.e device) is created/destroyed. In turn, the ->probe()
  45. * or ->remove() handler of the appropriate rpmsg driver will be invoked
  46. * (if/as-soon-as one is registered).
  47. */
  48. RL_PACKED_BEGIN
  49. struct rpmsg_ns_msg
  50. {
  51. char name[RL_NS_NAME_SIZE];
  52. uint32_t addr;
  53. uint32_t flags;
  54. } RL_PACKED_END;
  55. /*!
  56. * @brief
  57. * Nameservice callback, called in interrupt context
  58. *
  59. * @param payload Pointer to the buffer containing received data
  60. * @param payload_len Size of data received, in bytes
  61. * @param src Pointer to address of the endpoint from which data is received
  62. * @param priv Private data provided during endpoint creation
  63. *
  64. * @return RL_RELEASE, message is always freed
  65. *
  66. */
  67. static int32_t rpmsg_ns_rx_cb(void *payload, uint32_t payload_len, uint32_t src, void *priv)
  68. {
  69. struct rpmsg_ns_msg *ns_msg_ptr = payload;
  70. struct rpmsg_ns_callback_data *cb_ctxt = priv;
  71. RL_ASSERT(priv != RL_NULL);
  72. RL_ASSERT(cb_ctxt->cb != RL_NULL);
  73. /* Drop likely bad messages received at nameservice address */
  74. if (payload_len == sizeof(struct rpmsg_ns_msg))
  75. {
  76. cb_ctxt->cb(ns_msg_ptr->addr, ns_msg_ptr->name, ns_msg_ptr->flags, cb_ctxt->user_data);
  77. }
  78. return RL_RELEASE;
  79. }
  80. #if defined(RL_USE_STATIC_API) && (RL_USE_STATIC_API == 1)
  81. rpmsg_ns_handle rpmsg_ns_bind(struct rpmsg_lite_instance *rpmsg_lite_dev,
  82. rpmsg_ns_new_ept_cb app_cb,
  83. void *user_data,
  84. rpmsg_ns_static_context *ns_ept_ctxt)
  85. #else
  86. rpmsg_ns_handle rpmsg_ns_bind(struct rpmsg_lite_instance *rpmsg_lite_dev, rpmsg_ns_new_ept_cb app_cb, void *user_data)
  87. #endif /* RL_USE_STATIC_API */
  88. {
  89. struct rpmsg_ns_context *ns_ctxt;
  90. if (app_cb == RL_NULL)
  91. {
  92. return RL_NULL;
  93. }
  94. #if defined(RL_USE_STATIC_API) && (RL_USE_STATIC_API == 1)
  95. if (ns_ept_ctxt == RL_NULL)
  96. {
  97. return RL_NULL;
  98. }
  99. ns_ctxt = &ns_ept_ctxt->ns_ctxt;
  100. /* Set-up the nameservice callback context */
  101. ns_ept_ctxt->cb_ctxt.user_data = user_data;
  102. ns_ept_ctxt->cb_ctxt.cb = app_cb;
  103. ns_ctxt->cb_ctxt = &ns_ept_ctxt->cb_ctxt;
  104. ns_ctxt->ept = rpmsg_lite_create_ept(rpmsg_lite_dev, RL_NS_EPT_ADDR, rpmsg_ns_rx_cb, (void *)ns_ctxt->cb_ctxt,
  105. &ns_ept_ctxt->ept_ctxt);
  106. #else
  107. {
  108. struct rpmsg_ns_callback_data *cb_ctxt;
  109. cb_ctxt = env_allocate_memory(sizeof(struct rpmsg_ns_callback_data));
  110. if (cb_ctxt == RL_NULL)
  111. {
  112. return RL_NULL;
  113. }
  114. ns_ctxt = env_allocate_memory(sizeof(struct rpmsg_ns_context));
  115. if (ns_ctxt == RL_NULL)
  116. {
  117. env_free_memory(cb_ctxt);
  118. return RL_NULL;
  119. }
  120. /* Set-up the nameservice callback context */
  121. cb_ctxt->user_data = user_data;
  122. cb_ctxt->cb = app_cb;
  123. ns_ctxt->cb_ctxt = cb_ctxt;
  124. ns_ctxt->ept = rpmsg_lite_create_ept(rpmsg_lite_dev, RL_NS_EPT_ADDR, rpmsg_ns_rx_cb, (void *)ns_ctxt->cb_ctxt);
  125. }
  126. #endif /* RL_USE_STATIC_API */
  127. return (rpmsg_ns_handle)ns_ctxt;
  128. }
  129. int32_t rpmsg_ns_unbind(struct rpmsg_lite_instance *rpmsg_lite_dev, rpmsg_ns_handle handle)
  130. {
  131. struct rpmsg_ns_context *ns_ctxt = (struct rpmsg_ns_context *)handle;
  132. #if defined(RL_USE_STATIC_API) && (RL_USE_STATIC_API == 1)
  133. return rpmsg_lite_destroy_ept(rpmsg_lite_dev, ns_ctxt->ept);
  134. #else
  135. {
  136. int32_t retval;
  137. retval = rpmsg_lite_destroy_ept(rpmsg_lite_dev, ns_ctxt->ept);
  138. env_free_memory(ns_ctxt->cb_ctxt);
  139. env_free_memory(ns_ctxt);
  140. return retval;
  141. }
  142. #endif
  143. }
  144. int32_t rpmsg_ns_announce(struct rpmsg_lite_instance *rpmsg_lite_dev,
  145. struct rpmsg_lite_endpoint *new_ept,
  146. const char *ept_name,
  147. uint32_t flags)
  148. {
  149. struct rpmsg_ns_msg ns_msg;
  150. if (ept_name == RL_NULL)
  151. {
  152. return RL_ERR_PARAM;
  153. }
  154. if (new_ept == RL_NULL)
  155. {
  156. return RL_ERR_PARAM;
  157. }
  158. env_strncpy(ns_msg.name, ept_name, RL_NS_NAME_SIZE);
  159. ns_msg.flags = flags;
  160. ns_msg.addr = new_ept->addr;
  161. return rpmsg_lite_send(rpmsg_lite_dev, new_ept, RL_NS_EPT_ADDR, (char *)&ns_msg, sizeof(struct rpmsg_ns_msg),
  162. RL_BLOCK);
  163. }