[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>
|
---|
| 47 |
|
---|
| 48 | /** The time between connect requests in microseconds. */
|
---|
| 49 | #define MODULE_WAIT_TIME (10 * 1000)
|
---|
| 50 |
|
---|
[774e6d1a] | 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.
|
---|
[c7a8442] | 57 | *
|
---|
| 58 | */
|
---|
[774e6d1a] | 59 | void answer_call(ipc_callid_t callid, int result, ipc_call_t *answer,
|
---|
| 60 | size_t count)
|
---|
[c7a8442] | 61 | {
|
---|
[774e6d1a] | 62 | /* Choose the most efficient function */
|
---|
| 63 | if ((answer != NULL) || (count == 0)) {
|
---|
| 64 | switch (count) {
|
---|
[c7a8442] | 65 | case 0:
|
---|
[64d2b10] | 66 | async_answer_0(callid, (sysarg_t) result);
|
---|
[c7a8442] | 67 | break;
|
---|
| 68 | case 1:
|
---|
[64d2b10] | 69 | async_answer_1(callid, (sysarg_t) result,
|
---|
[c7a8442] | 70 | IPC_GET_ARG1(*answer));
|
---|
| 71 | break;
|
---|
| 72 | case 2:
|
---|
[64d2b10] | 73 | async_answer_2(callid, (sysarg_t) result,
|
---|
[c7a8442] | 74 | IPC_GET_ARG1(*answer), IPC_GET_ARG2(*answer));
|
---|
| 75 | break;
|
---|
| 76 | case 3:
|
---|
[64d2b10] | 77 | async_answer_3(callid, (sysarg_t) result,
|
---|
[c7a8442] | 78 | IPC_GET_ARG1(*answer), IPC_GET_ARG2(*answer),
|
---|
| 79 | IPC_GET_ARG3(*answer));
|
---|
| 80 | break;
|
---|
| 81 | case 4:
|
---|
[64d2b10] | 82 | async_answer_4(callid, (sysarg_t) result,
|
---|
[c7a8442] | 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:
|
---|
[64d2b10] | 88 | async_answer_5(callid, (sysarg_t) result,
|
---|
[c7a8442] | 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 | */
|
---|
[96b02eb9] | 110 | int bind_service(services_t need, sysarg_t arg1, sysarg_t arg2, sysarg_t arg3,
|
---|
[c7a8442] | 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 | */
|
---|
[96b02eb9] | 133 | int 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)
|
---|
[c7a8442] | 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 */
|
---|
[64d2b10] | 140 | int rc = async_connect_to_me(phone, arg1, arg2, arg3, client_receiver);
|
---|
[16ac756] | 141 | if (rc != EOK) {
|
---|
[64d2b10] | 142 | async_hangup(phone);
|
---|
[16ac756] | 143 | return rc;
|
---|
[c7a8442] | 144 | }
|
---|
| 145 | }
|
---|
| 146 |
|
---|
| 147 | return phone;
|
---|
| 148 | }
|
---|
| 149 |
|
---|
| 150 | /** Connects to the needed module.
|
---|
| 151 | *
|
---|
| 152 | * @param[in] need The needed module service.
|
---|
[1bfd3d3] | 153 | * @return The phone of the needed service.
|
---|
[c7a8442] | 154 | */
|
---|
| 155 | int 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).
|
---|
[1bfd3d3] | 165 | * @return The phone of the needed service.
|
---|
| 166 | * @return ETIMEOUT if the connection timeouted.
|
---|
[c7a8442] | 167 | */
|
---|
| 168 | int connect_to_service_timeout(services_t need, suseconds_t timeout)
|
---|
| 169 | {
|
---|
| 170 | int phone;
|
---|
| 171 |
|
---|
[45bb1d2] | 172 | /* If no timeout is set */
|
---|
[c7a8442] | 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 |
|
---|
[45bb1d2] | 181 | /* Abort if no time is left */
|
---|
[c7a8442] | 182 | if (timeout <= 0)
|
---|
| 183 | return ETIMEOUT;
|
---|
| 184 |
|
---|
[45bb1d2] | 185 | /* Wait the minimum of the module wait time and the timeout */
|
---|
[c7a8442] | 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.
|
---|
[1bfd3d3] | 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.
|
---|
[c7a8442] | 199 | * Only partial data are transfered.
|
---|
[1bfd3d3] | 200 | * @return Other error codes as defined for the
|
---|
[c7a8442] | 201 | * async_data_read_finalize() function.
|
---|
| 202 | */
|
---|
| 203 | int data_reply(void *data, size_t data_length)
|
---|
| 204 | {
|
---|
| 205 | size_t length;
|
---|
| 206 | ipc_callid_t callid;
|
---|
| 207 |
|
---|
[45bb1d2] | 208 | /* Fetch the request */
|
---|
[c7a8442] | 209 | if (!async_data_read_receive(&callid, &length))
|
---|
| 210 | return EINVAL;
|
---|
| 211 |
|
---|
[45bb1d2] | 212 | /* Check the requested data size */
|
---|
[c7a8442] | 213 | if (length < data_length) {
|
---|
| 214 | async_data_read_finalize(callid, data, length);
|
---|
| 215 | return EOVERFLOW;
|
---|
| 216 | }
|
---|
| 217 |
|
---|
[45bb1d2] | 218 | /* Send the data */
|
---|
[c7a8442] | 219 | return async_data_read_finalize(callid, data, data_length);
|
---|
| 220 | }
|
---|
| 221 |
|
---|
[774e6d1a] | 222 | /** Refresh answer structure and argument count.
|
---|
| 223 | *
|
---|
| 224 | * Erase all arguments.
|
---|
[c7a8442] | 225 | *
|
---|
[774e6d1a] | 226 | * @param[in,out] answer Message processing answer structure.
|
---|
| 227 | * @param[in,out] count Number of answer arguments.
|
---|
[c7a8442] | 228 | *
|
---|
| 229 | */
|
---|
[774e6d1a] | 230 | void refresh_answer(ipc_call_t *answer, size_t *count)
|
---|
[c7a8442] | 231 | {
|
---|
[774e6d1a] | 232 | if (count != NULL)
|
---|
| 233 | *count = 0;
|
---|
| 234 |
|
---|
| 235 | if (answer != NULL) {
|
---|
[c7a8442] | 236 | IPC_SET_RETVAL(*answer, 0);
|
---|
[228e490] | 237 | IPC_SET_IMETHOD(*answer, 0);
|
---|
[c7a8442] | 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 | */
|
---|