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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since a07a454 was 9934f7d, checked in by Jiri Svoboda <jiri@…>, 14 years ago

Add extra argument to async connection handlers that can be used for passing
information from async_connect_to_me() to the handler.

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