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 |
|
---|
39 | #include <async.h>
|
---|
40 | #include <ipc/ipc.h>
|
---|
41 | #include <ipc/services.h>
|
---|
42 |
|
---|
43 | #include <net/device.h>
|
---|
44 | #include <adt/measured_strings.h>
|
---|
45 | #include <net/packet.h>
|
---|
46 |
|
---|
47 | /** Notify the module about the device state change.
|
---|
48 | *
|
---|
49 | * @param[in] phone The service module phone.
|
---|
50 | * @param[in] message The service specific message.
|
---|
51 | * @param[in] device_id The device identifier.
|
---|
52 | * @param[in] state The new device state.
|
---|
53 | * @param[in] target The target module service.
|
---|
54 | * @return EOK on success.
|
---|
55 | *
|
---|
56 | */
|
---|
57 | int
|
---|
58 | generic_device_state_msg_remote(int phone, int message, device_id_t device_id,
|
---|
59 | int state, services_t target)
|
---|
60 | {
|
---|
61 | async_msg_3(phone, (ipcarg_t) message, (ipcarg_t) device_id,
|
---|
62 | (ipcarg_t) state, target);
|
---|
63 |
|
---|
64 | return EOK;
|
---|
65 | }
|
---|
66 |
|
---|
67 | /** Notify a module about the device.
|
---|
68 | *
|
---|
69 | * @param[in] phone The service module phone.
|
---|
70 | * @param[in] message The service specific message.
|
---|
71 | * @param[in] device_id The device identifier.
|
---|
72 | * @param[in] arg2 The second argument of the message.
|
---|
73 | * @param[in] service The device module service.
|
---|
74 | * @return EOK on success.
|
---|
75 | * @return Other error codes as defined for the specific service
|
---|
76 | * message.
|
---|
77 | *
|
---|
78 | */
|
---|
79 | int
|
---|
80 | generic_device_req_remote(int phone, int message, device_id_t device_id,
|
---|
81 | int arg2, services_t service)
|
---|
82 | {
|
---|
83 | return (int) async_req_3_0(phone, (ipcarg_t) message,
|
---|
84 | (ipcarg_t) device_id, (ipcarg_t) arg2, (ipcarg_t) service);
|
---|
85 | }
|
---|
86 |
|
---|
87 | /** Returns the address.
|
---|
88 | *
|
---|
89 | * @param[in] phone The service module phone.
|
---|
90 | * @param[in] message The service specific message.
|
---|
91 | * @param[in] device_id The device identifier.
|
---|
92 | * @param[out] address The desired address.
|
---|
93 | * @param[out] data The address data container.
|
---|
94 | * @returns EOK on success.
|
---|
95 | * @returns EBADMEM if the address parameter and/or the data
|
---|
96 | * parameter is NULL.
|
---|
97 | * @returns Other error codes as defined for the specific service
|
---|
98 | * message.
|
---|
99 | */
|
---|
100 | int
|
---|
101 | generic_get_addr_req(int phone, int message, device_id_t device_id,
|
---|
102 | measured_string_ref *address, char ** data)
|
---|
103 | {
|
---|
104 | aid_t message_id;
|
---|
105 | ipcarg_t result;
|
---|
106 | int string;
|
---|
107 |
|
---|
108 | if (!address || !data)
|
---|
109 | return EBADMEM;
|
---|
110 |
|
---|
111 | // request the address
|
---|
112 | message_id = async_send_1(phone, (ipcarg_t) message,
|
---|
113 | (ipcarg_t) device_id, NULL);
|
---|
114 | string = measured_strings_return(phone, address, data, 1);
|
---|
115 | async_wait_for(message_id, &result);
|
---|
116 |
|
---|
117 | // if not successful
|
---|
118 | if ((string == EOK) && (result != EOK)) {
|
---|
119 | // clear the data
|
---|
120 | free(*address);
|
---|
121 | free(*data);
|
---|
122 | }
|
---|
123 |
|
---|
124 | return (int) result;
|
---|
125 | }
|
---|
126 |
|
---|
127 | /** Return the device packet dimension for sending.
|
---|
128 | *
|
---|
129 | * @param[in] phone The service module phone.
|
---|
130 | * @param[in] message The service specific message.
|
---|
131 | * @param[in] device_id The device identifier.
|
---|
132 | * @param[out] packet_dimension The packet dimension.
|
---|
133 | * @return EOK on success.
|
---|
134 | * @return EBADMEM if the packet_dimension parameter is NULL.
|
---|
135 | * @return Other error codes as defined for the specific service
|
---|
136 | * message.
|
---|
137 | */
|
---|
138 | int
|
---|
139 | generic_packet_size_req_remote(int phone, int message, device_id_t device_id,
|
---|
140 | packet_dimension_t *packet_dimension)
|
---|
141 | {
|
---|
142 | if (!packet_dimension)
|
---|
143 | return EBADMEM;
|
---|
144 |
|
---|
145 | ipcarg_t addr_len;
|
---|
146 | ipcarg_t prefix;
|
---|
147 | ipcarg_t content;
|
---|
148 | ipcarg_t suffix;
|
---|
149 |
|
---|
150 | ipcarg_t result = async_req_1_4(phone, (ipcarg_t) message,
|
---|
151 | (ipcarg_t) device_id, &addr_len, &prefix, &content, &suffix);
|
---|
152 |
|
---|
153 | packet_dimension->prefix = (size_t) prefix;
|
---|
154 | packet_dimension->content = (size_t) content;
|
---|
155 | packet_dimension->suffix = (size_t) suffix;
|
---|
156 | packet_dimension->addr_len = (size_t) addr_len;
|
---|
157 |
|
---|
158 | return (int) result;
|
---|
159 | }
|
---|
160 |
|
---|
161 | /** Pass the packet queue to the module.
|
---|
162 | *
|
---|
163 | * @param[in] phone The service module phone.
|
---|
164 | * @param[in] message The service specific message.
|
---|
165 | * @param[in] device_id The device identifier.
|
---|
166 | * @param[in] packet_id The received packet or the received packet queue
|
---|
167 | * identifier.
|
---|
168 | * @param[in] target The target module service.
|
---|
169 | * @param[in] error The error module service.
|
---|
170 | * @return EOK on success.
|
---|
171 | */
|
---|
172 | int
|
---|
173 | generic_received_msg_remote(int phone, int message, device_id_t device_id,
|
---|
174 | packet_id_t packet_id, services_t target, services_t error)
|
---|
175 | {
|
---|
176 | if (error) {
|
---|
177 | async_msg_4(phone, (ipcarg_t) message, (ipcarg_t) device_id,
|
---|
178 | (ipcarg_t) packet_id, (ipcarg_t) target, (ipcarg_t) error);
|
---|
179 | } else {
|
---|
180 | async_msg_3(phone, (ipcarg_t) message, (ipcarg_t) device_id,
|
---|
181 | (ipcarg_t) packet_id, (ipcarg_t) target);
|
---|
182 | }
|
---|
183 |
|
---|
184 | return EOK;
|
---|
185 | }
|
---|
186 |
|
---|
187 | /** Send the packet queue.
|
---|
188 | *
|
---|
189 | * @param[in] phone The service module phone.
|
---|
190 | * @param[in] message The service specific message.
|
---|
191 | * @param[in] device_id The device identifier.
|
---|
192 | * @param[in] packet_id The packet or the packet queue identifier.
|
---|
193 | * @param[in] sender The sending module service.
|
---|
194 | * @param[in] error The error module service.
|
---|
195 | * @return EOK on success.
|
---|
196 | *
|
---|
197 | */
|
---|
198 | int
|
---|
199 | generic_send_msg_remote(int phone, int message, device_id_t device_id,
|
---|
200 | packet_id_t packet_id, services_t sender, services_t error)
|
---|
201 | {
|
---|
202 | if (error) {
|
---|
203 | async_msg_4(phone, (ipcarg_t) message, (ipcarg_t) device_id,
|
---|
204 | (ipcarg_t) packet_id, (ipcarg_t) sender, (ipcarg_t) error);
|
---|
205 | } else {
|
---|
206 | async_msg_3(phone, (ipcarg_t) message, (ipcarg_t) device_id,
|
---|
207 | (ipcarg_t) packet_id, (ipcarg_t) sender);
|
---|
208 | }
|
---|
209 |
|
---|
210 | return EOK;
|
---|
211 | }
|
---|
212 |
|
---|
213 | /** Translates the given strings.
|
---|
214 | *
|
---|
215 | * Allocates and returns the needed memory block as the data parameter.
|
---|
216 | *
|
---|
217 | * @param[in] phone The service module phone.
|
---|
218 | * @param[in] message The service specific message.
|
---|
219 | * @param[in] device_id The device identifier.
|
---|
220 | * @param[in] service The module service.
|
---|
221 | * @param[in] configuration The key strings.
|
---|
222 | * @param[in] count The number of configuration keys.
|
---|
223 | * @param[out] translation The translated values.
|
---|
224 | * @param[out] data The translation data container.
|
---|
225 | * @returns EOK on success.
|
---|
226 | * @returns EINVAL if the configuration parameter is NULL.
|
---|
227 | * @returns EINVAL if the count parameter is zero.
|
---|
228 | * @returns EBADMEM if the translation or the data parameters are
|
---|
229 | * NULL.
|
---|
230 | * @returns Other error codes as defined for the specific service
|
---|
231 | * message.
|
---|
232 | */
|
---|
233 | int
|
---|
234 | generic_translate_req(int phone, int message, device_id_t device_id,
|
---|
235 | services_t service, measured_string_ref configuration, size_t count,
|
---|
236 | measured_string_ref *translation, char **data)
|
---|
237 | {
|
---|
238 | aid_t message_id;
|
---|
239 | ipcarg_t result;
|
---|
240 | int string;
|
---|
241 |
|
---|
242 | if (!configuration || (count == 0))
|
---|
243 | return EINVAL;
|
---|
244 | if (!translation || !data)
|
---|
245 | return EBADMEM;
|
---|
246 |
|
---|
247 | // request the translation
|
---|
248 | message_id = async_send_3(phone, (ipcarg_t) message,
|
---|
249 | (ipcarg_t) device_id, (ipcarg_t) count, (ipcarg_t) service, NULL);
|
---|
250 | measured_strings_send(phone, configuration, count);
|
---|
251 | string = measured_strings_return(phone, translation, data, count);
|
---|
252 | async_wait_for(message_id, &result);
|
---|
253 |
|
---|
254 | // if not successful
|
---|
255 | if ((string == EOK) && (result != EOK)) {
|
---|
256 | // clear the data
|
---|
257 | free(*translation);
|
---|
258 | free(*data);
|
---|
259 | }
|
---|
260 |
|
---|
261 | return (int) result;
|
---|
262 | }
|
---|
263 |
|
---|
264 | /** @}
|
---|
265 | */
|
---|