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
33 #include "rpmsg_lite.h"
34 #include "rpmsg_ns.h"
35 #include <stdint.h>
36
37 #define RL_NS_NAME_SIZE (32)
38
39 /*!
40 * struct rpmsg_ns_msg - dynamic name service announcement message
41 * @name: name of remote service that is published
42 * @addr: address of remote service that is published
43 * @flags: indicates whether service is created or destroyed
44 *
45 * This message is sent across to publish a new service, or announce
46 * about its removal. When we receive these messages, an appropriate
47 * rpmsg channel (i.e device) is created/destroyed. In turn, the ->probe()
48 * or ->remove() handler of the appropriate rpmsg driver will be invoked
49 * (if/as-soon-as one is registered).
50 */
51 RL_PACKED_BEGIN
52 struct rpmsg_ns_msg
53 {
54 char name[RL_NS_NAME_SIZE];
55 uint32_t addr;
56 uint32_t flags;
57 } RL_PACKED_END;
58
59 /*!
60 * @brief
61 * Nameservice callback, called in interrupt context
62 *
63 * @param payload Pointer to the buffer containing received data
64 * @param payload_len Size of data received, in bytes
65 * @param src Pointer to address of the endpoint from which data is received
66 * @param priv Private data provided during endpoint creation
67 *
68 * @return RL_RELEASE, message is always freed
69 *
70 */
rpmsg_ns_rx_cb(void * payload,uint32_t payload_len,uint32_t src,void * priv)71 static int32_t rpmsg_ns_rx_cb(void *payload, uint32_t payload_len, uint32_t src, void *priv)
72 {
73 struct rpmsg_ns_msg *ns_msg_ptr = payload;
74 struct rpmsg_ns_callback_data *cb_ctxt = priv;
75 RL_ASSERT(priv != RL_NULL);
76 RL_ASSERT(cb_ctxt->cb != RL_NULL);
77
78 /* Drop likely bad messages received at nameservice address */
79 if (payload_len == sizeof(struct rpmsg_ns_msg))
80 {
81 cb_ctxt->cb(ns_msg_ptr->addr, ns_msg_ptr->name, ns_msg_ptr->flags, cb_ctxt->user_data);
82 }
83
84 return RL_RELEASE;
85 }
86
87 #if defined(RL_USE_STATIC_API) && (RL_USE_STATIC_API == 1)
rpmsg_ns_bind(struct rpmsg_lite_instance * rpmsg_lite_dev,rpmsg_ns_new_ept_cb app_cb,void * user_data,rpmsg_ns_static_context * ns_ept_ctxt)88 rpmsg_ns_handle rpmsg_ns_bind(struct rpmsg_lite_instance *rpmsg_lite_dev,
89 rpmsg_ns_new_ept_cb app_cb,
90 void *user_data,
91 rpmsg_ns_static_context *ns_ept_ctxt)
92 #else
93 rpmsg_ns_handle rpmsg_ns_bind(struct rpmsg_lite_instance *rpmsg_lite_dev, rpmsg_ns_new_ept_cb app_cb, void *user_data)
94 #endif /* RL_USE_STATIC_API */
95 {
96 struct rpmsg_ns_context *ns_ctxt;
97
98 if (app_cb == RL_NULL)
99 {
100 return RL_NULL;
101 }
102
103 #if defined(RL_USE_STATIC_API) && (RL_USE_STATIC_API == 1)
104 if (ns_ept_ctxt == RL_NULL)
105 {
106 return RL_NULL;
107 }
108
109 ns_ctxt = &ns_ept_ctxt->ns_ctxt;
110
111 /* Set-up the nameservice callback context */
112 ns_ept_ctxt->cb_ctxt.user_data = user_data;
113 ns_ept_ctxt->cb_ctxt.cb = app_cb;
114
115 ns_ctxt->cb_ctxt = &ns_ept_ctxt->cb_ctxt;
116
117 ns_ctxt->ept = rpmsg_lite_create_ept(rpmsg_lite_dev, RL_NS_EPT_ADDR, rpmsg_ns_rx_cb, (void *)ns_ctxt->cb_ctxt,
118 &ns_ept_ctxt->ept_ctxt);
119 #else
120 {
121 struct rpmsg_ns_callback_data *cb_ctxt;
122
123 cb_ctxt = env_allocate_memory(sizeof(struct rpmsg_ns_callback_data));
124 if (cb_ctxt == RL_NULL)
125 {
126 return RL_NULL;
127 }
128 ns_ctxt = env_allocate_memory(sizeof(struct rpmsg_ns_context));
129 if (ns_ctxt == RL_NULL)
130 {
131 env_free_memory(cb_ctxt);
132 return RL_NULL;
133 }
134
135 /* Set-up the nameservice callback context */
136 cb_ctxt->user_data = user_data;
137 cb_ctxt->cb = app_cb;
138
139 ns_ctxt->cb_ctxt = cb_ctxt;
140
141 ns_ctxt->ept = rpmsg_lite_create_ept(rpmsg_lite_dev, RL_NS_EPT_ADDR, rpmsg_ns_rx_cb, (void *)ns_ctxt->cb_ctxt);
142 }
143 #endif /* RL_USE_STATIC_API */
144
145 return (rpmsg_ns_handle)ns_ctxt;
146 }
147
rpmsg_ns_unbind(struct rpmsg_lite_instance * rpmsg_lite_dev,rpmsg_ns_handle handle)148 int32_t rpmsg_ns_unbind(struct rpmsg_lite_instance *rpmsg_lite_dev, rpmsg_ns_handle handle)
149 {
150 struct rpmsg_ns_context *ns_ctxt = (struct rpmsg_ns_context *)handle;
151
152 #if defined(RL_USE_STATIC_API) && (RL_USE_STATIC_API == 1)
153 return rpmsg_lite_destroy_ept(rpmsg_lite_dev, ns_ctxt->ept);
154 #else
155 {
156 int32_t retval;
157
158 retval = rpmsg_lite_destroy_ept(rpmsg_lite_dev, ns_ctxt->ept);
159 env_free_memory(ns_ctxt->cb_ctxt);
160 env_free_memory(ns_ctxt);
161 return retval;
162 }
163 #endif
164 }
165
rpmsg_ns_announce(struct rpmsg_lite_instance * rpmsg_lite_dev,struct rpmsg_lite_endpoint * new_ept,const char * ept_name,uint32_t flags)166 int32_t rpmsg_ns_announce(struct rpmsg_lite_instance *rpmsg_lite_dev,
167 struct rpmsg_lite_endpoint *new_ept,
168 const char *ept_name,
169 uint32_t flags)
170 {
171 struct rpmsg_ns_msg ns_msg;
172
173 if (ept_name == RL_NULL)
174 {
175 return RL_ERR_PARAM;
176 }
177
178 if (new_ept == RL_NULL)
179 {
180 return RL_ERR_PARAM;
181 }
182
183 env_strncpy(ns_msg.name, ept_name, RL_NS_NAME_SIZE);
184 ns_msg.flags = flags;
185 ns_msg.addr = new_ept->addr;
186
187 return rpmsg_lite_send(rpmsg_lite_dev, new_ept, RL_NS_EPT_ADDR, (char *)&ns_msg, sizeof(struct rpmsg_ns_msg),
188 RL_BLOCK);
189 }
190