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

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

networking stack: convert to the new async framework

  • Property mode set to 100644
File size: 5.7 KB
RevLine 
[c7a8442]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
[774e6d1a]34 * Generic module functions implementation.
[c7a8442]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>
[16ac756]43#include <errno.h>
[c7a8442]44#include <sys/time.h>
45#include <ipc/services.h>
46#include <net/modules.h>
[79ae36dd]47#include <ns.h>
[c7a8442]48
[774e6d1a]49/** Answer a call.
50 *
51 * @param[in] callid Call identifier.
52 * @param[in] result Message processing result.
53 * @param[in] answer Message processing answer.
54 * @param[in] count Number of answer parameters.
[c7a8442]55 *
56 */
[774e6d1a]57void answer_call(ipc_callid_t callid, int result, ipc_call_t *answer,
58 size_t count)
[c7a8442]59{
[774e6d1a]60 /* Choose the most efficient function */
61 if ((answer != NULL) || (count == 0)) {
62 switch (count) {
[c7a8442]63 case 0:
[64d2b10]64 async_answer_0(callid, (sysarg_t) result);
[c7a8442]65 break;
66 case 1:
[64d2b10]67 async_answer_1(callid, (sysarg_t) result,
[c7a8442]68 IPC_GET_ARG1(*answer));
69 break;
70 case 2:
[64d2b10]71 async_answer_2(callid, (sysarg_t) result,
[c7a8442]72 IPC_GET_ARG1(*answer), IPC_GET_ARG2(*answer));
73 break;
74 case 3:
[64d2b10]75 async_answer_3(callid, (sysarg_t) result,
[c7a8442]76 IPC_GET_ARG1(*answer), IPC_GET_ARG2(*answer),
77 IPC_GET_ARG3(*answer));
78 break;
79 case 4:
[64d2b10]80 async_answer_4(callid, (sysarg_t) result,
[c7a8442]81 IPC_GET_ARG1(*answer), IPC_GET_ARG2(*answer),
82 IPC_GET_ARG3(*answer), IPC_GET_ARG4(*answer));
83 break;
84 case 5:
85 default:
[64d2b10]86 async_answer_5(callid, (sysarg_t) result,
[c7a8442]87 IPC_GET_ARG1(*answer), IPC_GET_ARG2(*answer),
88 IPC_GET_ARG3(*answer), IPC_GET_ARG4(*answer),
89 IPC_GET_ARG5(*answer));
90 break;
91 }
92 }
93}
94
[6b82009]95/** Create bidirectional connection with the needed module service and register
[c7a8442]96 * the message receiver.
97 *
[6b82009]98 * @param[in] need Needed module service.
99 * @param[in] arg1 First parameter.
100 * @param[in] arg2 Second parameter.
101 * @param[in] arg3 Third parameter.
102 * @param[in] client_receiver Message receiver.
103 *
104 * @return Session to the needed service.
105 * @return Other error codes as defined for the async_connect_to_me()
106 * function.
[c7a8442]107 *
108 */
[6b82009]109async_sess_t *bind_service(services_t need, sysarg_t arg1, sysarg_t arg2,
110 sysarg_t arg3, async_client_conn_t client_receiver)
[c7a8442]111{
112 /* Connect to the needed service */
[6b82009]113 async_sess_t *sess = connect_to_service(need);
114 if (sess != NULL) {
[c7a8442]115 /* Request the bidirectional connection */
[6b82009]116 async_exch_t *exch = async_exchange_begin(sess);
117 int rc = async_connect_to_me(exch, arg1, arg2, arg3,
[9934f7d]118 client_receiver, NULL);
[6b82009]119 async_exchange_end(exch);
120
[16ac756]121 if (rc != EOK) {
[6b82009]122 async_hangup(sess);
123 errno = rc;
124 return NULL;
[c7a8442]125 }
126 }
127
[6b82009]128 return sess;
[c7a8442]129}
130
[6b82009]131/** Connect to the needed module.
132 *
133 * @param[in] need Needed module service.
134 *
135 * @return Session to the needed service.
136 * @return NULL if the connection timeouted.
[c7a8442]137 *
138 */
[6b82009]139async_sess_t *connect_to_service(services_t need)
[c7a8442]140{
[6b82009]141 return service_connect_blocking(EXCHANGE_SERIALIZE, need, 0, 0);
[c7a8442]142}
143
[6b82009]144/** Reply the data to the other party.
[c7a8442]145 *
[6b82009]146 * @param[in] data The data buffer to be sent.
[c7a8442]147 * @param[in] data_length The buffer length.
[6b82009]148 *
149 * @return EOK on success.
150 * @return EINVAL if the client does not expect the data.
151 * @return EOVERFLOW if the client does not expect all the data.
152 * Only partial data are transfered.
153 * @return Other error codes as defined for the
154 * async_data_read_finalize() function.
155 *
[c7a8442]156 */
157int data_reply(void *data, size_t data_length)
158{
159 size_t length;
160 ipc_callid_t callid;
[6b82009]161
[45bb1d2]162 /* Fetch the request */
[c7a8442]163 if (!async_data_read_receive(&callid, &length))
164 return EINVAL;
[6b82009]165
[45bb1d2]166 /* Check the requested data size */
[c7a8442]167 if (length < data_length) {
168 async_data_read_finalize(callid, data, length);
169 return EOVERFLOW;
170 }
[6b82009]171
[45bb1d2]172 /* Send the data */
[c7a8442]173 return async_data_read_finalize(callid, data, data_length);
174}
175
[774e6d1a]176/** Refresh answer structure and argument count.
177 *
178 * Erase all arguments.
[c7a8442]179 *
[774e6d1a]180 * @param[in,out] answer Message processing answer structure.
181 * @param[in,out] count Number of answer arguments.
[c7a8442]182 *
183 */
[774e6d1a]184void refresh_answer(ipc_call_t *answer, size_t *count)
[c7a8442]185{
[774e6d1a]186 if (count != NULL)
187 *count = 0;
188
189 if (answer != NULL) {
[c7a8442]190 IPC_SET_RETVAL(*answer, 0);
[228e490]191 IPC_SET_IMETHOD(*answer, 0);
[c7a8442]192 IPC_SET_ARG1(*answer, 0);
193 IPC_SET_ARG2(*answer, 0);
194 IPC_SET_ARG3(*answer, 0);
195 IPC_SET_ARG4(*answer, 0);
196 IPC_SET_ARG5(*answer, 0);
197 }
198}
199
200/** @}
201 */
Note: See TracBrowser for help on using the repository browser.