1 | /*
|
---|
2 | * Copyright (c) 2009 Lukas Mejdrech
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup libnet
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 |
|
---|
33 | /** @file
|
---|
34 | * Generic communication interfaces for networking.
|
---|
35 | */
|
---|
36 |
|
---|
37 | #include <generic.h>
|
---|
38 | #include <async.h>
|
---|
39 | #include <ipc/services.h>
|
---|
40 | #include <net/device.h>
|
---|
41 | #include <adt/measured_strings.h>
|
---|
42 | #include <net/packet.h>
|
---|
43 |
|
---|
44 | /** Notify the module about the device state change.
|
---|
45 | *
|
---|
46 | * @param[in] sess Service module session.
|
---|
47 | * @param[in] message Service specific message.
|
---|
48 | * @param[in] device_id Device identifier.
|
---|
49 | * @param[in] state New device state.
|
---|
50 | * @param[in] target Target module service.
|
---|
51 | *
|
---|
52 | * @return EOK on success.
|
---|
53 | *
|
---|
54 | */
|
---|
55 | int generic_device_state_msg_remote(async_sess_t *sess, sysarg_t message,
|
---|
56 | nic_device_id_t device_id, sysarg_t state, services_t target)
|
---|
57 | {
|
---|
58 | async_exch_t *exch = async_exchange_begin(sess);
|
---|
59 | async_msg_3(exch, message, (sysarg_t) device_id, state, target);
|
---|
60 | async_exchange_end(exch);
|
---|
61 |
|
---|
62 | return EOK;
|
---|
63 | }
|
---|
64 |
|
---|
65 | /** Notify a module about the device.
|
---|
66 | *
|
---|
67 | * @param[in] sess Service module session.
|
---|
68 | * @param[in] message Service specific message.
|
---|
69 | * @param[in] device_id Device identifier.
|
---|
70 | * @param[in] service Device module service.
|
---|
71 | *
|
---|
72 | * @return EOK on success.
|
---|
73 | * @return Other error codes as defined for the specific service
|
---|
74 | * message.
|
---|
75 | *
|
---|
76 | */
|
---|
77 | int generic_device_req_remote(async_sess_t *sess, sysarg_t message,
|
---|
78 | nic_device_id_t device_id, services_t service)
|
---|
79 | {
|
---|
80 | async_exch_t *exch = async_exchange_begin(sess);
|
---|
81 | int rc = async_req_3_0(exch, message, (sysarg_t) device_id, 0,
|
---|
82 | (sysarg_t) service);
|
---|
83 | async_exchange_end(exch);
|
---|
84 |
|
---|
85 | return rc;
|
---|
86 | }
|
---|
87 |
|
---|
88 | /** Returns the address.
|
---|
89 | *
|
---|
90 | * @param[in] sess Service module session.
|
---|
91 | * @param[in] message Service specific message.
|
---|
92 | * @param[in] device_id Device identifier.
|
---|
93 | * @param[out] address Desired address.
|
---|
94 | * @param[out] data Address data container.
|
---|
95 | *
|
---|
96 | * @return EOK on success.
|
---|
97 | * @return EBADMEM if the address parameter and/or the data
|
---|
98 | * parameter is NULL.
|
---|
99 | * @return Other error codes as defined for the specific service
|
---|
100 | * message.
|
---|
101 | *
|
---|
102 | */
|
---|
103 | int generic_get_addr_req(async_sess_t *sess, sysarg_t message,
|
---|
104 | nic_device_id_t device_id, uint8_t *address, size_t max_len)
|
---|
105 | {
|
---|
106 | aid_t aid;
|
---|
107 | ipc_call_t result;
|
---|
108 |
|
---|
109 | assert(address != NULL);
|
---|
110 |
|
---|
111 | /* Request the address */
|
---|
112 | async_exch_t *exch = async_exchange_begin(sess);
|
---|
113 | aid = async_send_1(exch, message, (sysarg_t) device_id, &result);
|
---|
114 |
|
---|
115 | sysarg_t ipcrc;
|
---|
116 | int rc = async_data_read_start(exch, address, max_len);
|
---|
117 | async_exchange_end(exch);
|
---|
118 |
|
---|
119 | if (rc != EOK) {
|
---|
120 | async_wait_for(aid, &ipcrc);
|
---|
121 | return rc;
|
---|
122 | }
|
---|
123 |
|
---|
124 | async_wait_for(aid, &ipcrc);
|
---|
125 | if (ipcrc == EOK) {
|
---|
126 | return IPC_GET_ARG1(result);
|
---|
127 | } else {
|
---|
128 | return (int) ipcrc;
|
---|
129 | }
|
---|
130 | }
|
---|
131 |
|
---|
132 | /** Return the device packet dimension for sending.
|
---|
133 | *
|
---|
134 | * @param[in] sess Service module session.
|
---|
135 | * @param[in] message Service specific message.
|
---|
136 | * @param[in] device_id Device identifier.
|
---|
137 | * @param[out] packet_dimension Packet dimension.
|
---|
138 | *
|
---|
139 | * @return EOK on success.
|
---|
140 | * @return EBADMEM if the packet_dimension parameter is NULL.
|
---|
141 | * @return Other error codes as defined for the specific service
|
---|
142 | * message.
|
---|
143 | *
|
---|
144 | */
|
---|
145 | int generic_packet_size_req_remote(async_sess_t *sess, sysarg_t message,
|
---|
146 | nic_device_id_t device_id, packet_dimension_t *packet_dimension)
|
---|
147 | {
|
---|
148 | if (!packet_dimension)
|
---|
149 | return EBADMEM;
|
---|
150 |
|
---|
151 | sysarg_t addr_len;
|
---|
152 | sysarg_t prefix;
|
---|
153 | sysarg_t content;
|
---|
154 | sysarg_t suffix;
|
---|
155 |
|
---|
156 | async_exch_t *exch = async_exchange_begin(sess);
|
---|
157 | sysarg_t result = async_req_1_4(exch, message, (sysarg_t) device_id,
|
---|
158 | &addr_len, &prefix, &content, &suffix);
|
---|
159 | async_exchange_end(exch);
|
---|
160 |
|
---|
161 | packet_dimension->prefix = (size_t) prefix;
|
---|
162 | packet_dimension->content = (size_t) content;
|
---|
163 | packet_dimension->suffix = (size_t) suffix;
|
---|
164 | packet_dimension->addr_len = (size_t) addr_len;
|
---|
165 |
|
---|
166 | return (int) result;
|
---|
167 | }
|
---|
168 |
|
---|
169 | /** Pass the packet queue to the module.
|
---|
170 | *
|
---|
171 | * @param[in] sess Service module session.
|
---|
172 | * @param[in] message Service specific message.
|
---|
173 | * @param[in] device_id Device identifier.
|
---|
174 | * @param[in] packet_id Received packet or the received packet queue
|
---|
175 | * identifier.
|
---|
176 | * @param[in] target Target module service.
|
---|
177 | * @param[in] error Error module service.
|
---|
178 | *
|
---|
179 | * @return EOK on success.
|
---|
180 | *
|
---|
181 | */
|
---|
182 | int generic_received_msg_remote(async_sess_t *sess, sysarg_t message,
|
---|
183 | nic_device_id_t device_id, packet_id_t packet_id, services_t target,
|
---|
184 | services_t error)
|
---|
185 | {
|
---|
186 | async_exch_t *exch = async_exchange_begin(sess);
|
---|
187 |
|
---|
188 | if (error) {
|
---|
189 | async_msg_4(exch, message, (sysarg_t) device_id,
|
---|
190 | (sysarg_t) packet_id, (sysarg_t) target, (sysarg_t) error);
|
---|
191 | } else {
|
---|
192 | async_msg_3(exch, message, (sysarg_t) device_id,
|
---|
193 | (sysarg_t) packet_id, (sysarg_t) target);
|
---|
194 | }
|
---|
195 |
|
---|
196 | async_exchange_end(exch);
|
---|
197 |
|
---|
198 | return EOK;
|
---|
199 | }
|
---|
200 |
|
---|
201 | /** Send the packet queue.
|
---|
202 | *
|
---|
203 | * @param[in] sess Service module session.
|
---|
204 | * @param[in] message Service specific message.
|
---|
205 | * @param[in] device_id Device identifier.
|
---|
206 | * @param[in] packet_id Packet or the packet queue identifier.
|
---|
207 | * @param[in] sender Sending module service.
|
---|
208 | * @param[in] error Error module service.
|
---|
209 | *
|
---|
210 | * @return EOK on success.
|
---|
211 | *
|
---|
212 | */
|
---|
213 | int generic_send_msg_remote(async_sess_t *sess, sysarg_t message,
|
---|
214 | nic_device_id_t device_id, packet_id_t packet_id, services_t sender,
|
---|
215 | services_t error)
|
---|
216 | {
|
---|
217 | async_exch_t *exch = async_exchange_begin(sess);
|
---|
218 |
|
---|
219 | if (error) {
|
---|
220 | async_msg_4(exch, message, (sysarg_t) device_id,
|
---|
221 | (sysarg_t) packet_id, (sysarg_t) sender, (sysarg_t) error);
|
---|
222 | } else {
|
---|
223 | async_msg_3(exch, message, (sysarg_t) device_id,
|
---|
224 | (sysarg_t) packet_id, (sysarg_t) sender);
|
---|
225 | }
|
---|
226 |
|
---|
227 | async_exchange_end(exch);
|
---|
228 |
|
---|
229 | return EOK;
|
---|
230 | }
|
---|
231 |
|
---|
232 | /** Translates the given strings.
|
---|
233 | *
|
---|
234 | * Allocates and returns the needed memory block as the data parameter.
|
---|
235 | *
|
---|
236 | * @param[in] sess Service module session.
|
---|
237 | * @param[in] message Service specific message.
|
---|
238 | * @param[in] device_id Device identifier.
|
---|
239 | * @param[in] service Module service.
|
---|
240 | * @param[in] configuration Key strings.
|
---|
241 | * @param[in] count Number of configuration keys.
|
---|
242 | * @param[out] translation Translated values.
|
---|
243 | * @param[out] data Translation data container.
|
---|
244 | *
|
---|
245 | * @return EOK on success.
|
---|
246 | * @return EINVAL if the configuration parameter is NULL.
|
---|
247 | * @return EINVAL if the count parameter is zero.
|
---|
248 | * @return EBADMEM if the translation or the data parameters are
|
---|
249 | * NULL.
|
---|
250 | * @return Other error codes as defined for the specific service
|
---|
251 | * message.
|
---|
252 | *
|
---|
253 | */
|
---|
254 | int generic_translate_req(async_sess_t *sess, sysarg_t message,
|
---|
255 | nic_device_id_t device_id, services_t service,
|
---|
256 | measured_string_t *configuration, size_t count,
|
---|
257 | measured_string_t **translation, uint8_t **data)
|
---|
258 | {
|
---|
259 | if ((!configuration) || (count == 0))
|
---|
260 | return EINVAL;
|
---|
261 |
|
---|
262 | if ((!translation) || (!data))
|
---|
263 | return EBADMEM;
|
---|
264 |
|
---|
265 | /* Request the translation */
|
---|
266 | async_exch_t *exch = async_exchange_begin(sess);
|
---|
267 | aid_t message_id = async_send_3(exch, message, (sysarg_t) device_id,
|
---|
268 | (sysarg_t) count, (sysarg_t) service, NULL);
|
---|
269 | measured_strings_send(exch, configuration, count);
|
---|
270 | int rc = measured_strings_return(exch, translation, data, count);
|
---|
271 | async_exchange_end(exch);
|
---|
272 |
|
---|
273 | sysarg_t result;
|
---|
274 | async_wait_for(message_id, &result);
|
---|
275 |
|
---|
276 | /* If not successful */
|
---|
277 | if ((rc == EOK) && (result != EOK)) {
|
---|
278 | /* Clear the data */
|
---|
279 | free(*translation);
|
---|
280 | free(*data);
|
---|
281 | }
|
---|
282 |
|
---|
283 | return (int) result;
|
---|
284 | }
|
---|
285 |
|
---|
286 | /** @}
|
---|
287 | */
|
---|