source: mainline/uspace/lib/c/generic/net/modules.c@ 64d2b10

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 64d2b10 was 64d2b10, checked in by Martin Decky <martin@…>, 14 years ago

libc: do not intermix low-level IPC methods with async framework methods

  • Property mode set to 100644
File size: 7.2 KB
Line 
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 libc
30 * @{
31 */
32
33/** @file
34 * Generic module functions implementation.
35 *
36 * @todo MAKE IT POSSIBLE TO REMOVE THIS FILE VIA EITHER REPLACING PART OF ITS
37 * FUNCTIONALITY OR VIA INTEGRATING ITS FUNCTIONALITY MORE TIGHTLY WITH THE REST
38 * OF THE SYSTEM.
39 */
40
41#include <async.h>
42#include <malloc.h>
43#include <errno.h>
44#include <sys/time.h>
45#include <ipc/services.h>
46#include <net/modules.h>
47
48/** The time between connect requests in microseconds. */
49#define MODULE_WAIT_TIME (10 * 1000)
50
51/** Answer a call.
52 *
53 * @param[in] callid Call identifier.
54 * @param[in] result Message processing result.
55 * @param[in] answer Message processing answer.
56 * @param[in] count Number of answer parameters.
57 *
58 */
59void answer_call(ipc_callid_t callid, int result, ipc_call_t *answer,
60 size_t count)
61{
62 /* Choose the most efficient function */
63 if ((answer != NULL) || (count == 0)) {
64 switch (count) {
65 case 0:
66 async_answer_0(callid, (sysarg_t) result);
67 break;
68 case 1:
69 async_answer_1(callid, (sysarg_t) result,
70 IPC_GET_ARG1(*answer));
71 break;
72 case 2:
73 async_answer_2(callid, (sysarg_t) result,
74 IPC_GET_ARG1(*answer), IPC_GET_ARG2(*answer));
75 break;
76 case 3:
77 async_answer_3(callid, (sysarg_t) result,
78 IPC_GET_ARG1(*answer), IPC_GET_ARG2(*answer),
79 IPC_GET_ARG3(*answer));
80 break;
81 case 4:
82 async_answer_4(callid, (sysarg_t) result,
83 IPC_GET_ARG1(*answer), IPC_GET_ARG2(*answer),
84 IPC_GET_ARG3(*answer), IPC_GET_ARG4(*answer));
85 break;
86 case 5:
87 default:
88 async_answer_5(callid, (sysarg_t) result,
89 IPC_GET_ARG1(*answer), IPC_GET_ARG2(*answer),
90 IPC_GET_ARG3(*answer), IPC_GET_ARG4(*answer),
91 IPC_GET_ARG5(*answer));
92 break;
93 }
94 }
95}
96
97/** Create bidirectional connection with the needed module service and registers
98 * the message receiver.
99 *
100 * @param[in] need The needed module service.
101 * @param[in] arg1 The first parameter.
102 * @param[in] arg2 The second parameter.
103 * @param[in] arg3 The third parameter.
104 * @param[in] client_receiver The message receiver.
105 *
106 * @return The phone of the needed service.
107 * @return Other error codes as defined for the ipc_connect_to_me()
108 * function.
109 */
110int bind_service(services_t need, sysarg_t arg1, sysarg_t arg2, sysarg_t arg3,
111 async_client_conn_t client_receiver)
112{
113 return bind_service_timeout(need, arg1, arg2, arg3, client_receiver, 0);
114}
115
116/** Create bidirectional connection with the needed module service and registers
117 * the message receiver.
118 *
119 * @param[in] need The needed module service.
120 * @param[in] arg1 The first parameter.
121 * @param[in] arg2 The second parameter.
122 * @param[in] arg3 The third parameter.
123 * @param[in] client_receiver The message receiver.
124 * @param[in] timeout The connection timeout in microseconds. No timeout if
125 * set to zero (0).
126 *
127 * @return The phone of the needed service.
128 * @return ETIMEOUT if the connection timeouted.
129 * @return Other error codes as defined for the ipc_connect_to_me()
130 * function.
131 *
132 */
133int bind_service_timeout(services_t need, sysarg_t arg1, sysarg_t arg2,
134 sysarg_t arg3, async_client_conn_t client_receiver, suseconds_t timeout)
135{
136 /* Connect to the needed service */
137 int phone = connect_to_service_timeout(need, timeout);
138 if (phone >= 0) {
139 /* Request the bidirectional connection */
140 int rc = async_connect_to_me(phone, arg1, arg2, arg3, client_receiver);
141 if (rc != EOK) {
142 async_hangup(phone);
143 return rc;
144 }
145 }
146
147 return phone;
148}
149
150/** Connects to the needed module.
151 *
152 * @param[in] need The needed module service.
153 * @return The phone of the needed service.
154 */
155int connect_to_service(services_t need)
156{
157 return connect_to_service_timeout(need, 0);
158}
159
160/** Connects to the needed module.
161 *
162 * @param[in] need The needed module service.
163 * @param[in] timeout The connection timeout in microseconds. No timeout if
164 * set to zero (0).
165 * @return The phone of the needed service.
166 * @return ETIMEOUT if the connection timeouted.
167 */
168int connect_to_service_timeout(services_t need, suseconds_t timeout)
169{
170 int phone;
171
172 /* If no timeout is set */
173 if (timeout <= 0)
174 return async_connect_me_to_blocking(PHONE_NS, need, 0, 0);
175
176 while (true) {
177 phone = async_connect_me_to(PHONE_NS, need, 0, 0);
178 if ((phone >= 0) || (phone != ENOENT))
179 return phone;
180
181 /* Abort if no time is left */
182 if (timeout <= 0)
183 return ETIMEOUT;
184
185 /* Wait the minimum of the module wait time and the timeout */
186 usleep((timeout <= MODULE_WAIT_TIME) ?
187 timeout : MODULE_WAIT_TIME);
188 timeout -= MODULE_WAIT_TIME;
189 }
190}
191
192/** Replies the data to the other party.
193 *
194 * @param[in] data The data buffer to be sent.
195 * @param[in] data_length The buffer length.
196 * @return EOK on success.
197 * @return EINVAL if the client does not expect the data.
198 * @return EOVERFLOW if the client does not expect all the data.
199 * Only partial data are transfered.
200 * @return Other error codes as defined for the
201 * async_data_read_finalize() function.
202 */
203int data_reply(void *data, size_t data_length)
204{
205 size_t length;
206 ipc_callid_t callid;
207
208 /* Fetch the request */
209 if (!async_data_read_receive(&callid, &length))
210 return EINVAL;
211
212 /* Check the requested data size */
213 if (length < data_length) {
214 async_data_read_finalize(callid, data, length);
215 return EOVERFLOW;
216 }
217
218 /* Send the data */
219 return async_data_read_finalize(callid, data, data_length);
220}
221
222/** Refresh answer structure and argument count.
223 *
224 * Erase all arguments.
225 *
226 * @param[in,out] answer Message processing answer structure.
227 * @param[in,out] count Number of answer arguments.
228 *
229 */
230void refresh_answer(ipc_call_t *answer, size_t *count)
231{
232 if (count != NULL)
233 *count = 0;
234
235 if (answer != NULL) {
236 IPC_SET_RETVAL(*answer, 0);
237 IPC_SET_IMETHOD(*answer, 0);
238 IPC_SET_ARG1(*answer, 0);
239 IPC_SET_ARG2(*answer, 0);
240 IPC_SET_ARG3(*answer, 0);
241 IPC_SET_ARG4(*answer, 0);
242 IPC_SET_ARG5(*answer, 0);
243 }
244}
245
246/** @}
247 */
Note: See TracBrowser for help on using the repository browser.