[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 |
|
---|
| 46 | #include <ipc/ipc.h>
|
---|
| 47 | #include <ipc/services.h>
|
---|
| 48 |
|
---|
| 49 | #include <net/modules.h>
|
---|
| 50 |
|
---|
| 51 | /** The time between connect requests in microseconds. */
|
---|
| 52 | #define MODULE_WAIT_TIME (10 * 1000)
|
---|
| 53 |
|
---|
[774e6d1a] | 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.
|
---|
[c7a8442] | 60 | *
|
---|
| 61 | */
|
---|
[774e6d1a] | 62 | void answer_call(ipc_callid_t callid, int result, ipc_call_t *answer,
|
---|
| 63 | size_t count)
|
---|
[c7a8442] | 64 | {
|
---|
[774e6d1a] | 65 | /* Choose the most efficient function */
|
---|
| 66 | if ((answer != NULL) || (count == 0)) {
|
---|
| 67 | switch (count) {
|
---|
[c7a8442] | 68 | case 0:
|
---|
[96b02eb9] | 69 | ipc_answer_0(callid, (sysarg_t) result);
|
---|
[c7a8442] | 70 | break;
|
---|
| 71 | case 1:
|
---|
[96b02eb9] | 72 | ipc_answer_1(callid, (sysarg_t) result,
|
---|
[c7a8442] | 73 | IPC_GET_ARG1(*answer));
|
---|
| 74 | break;
|
---|
| 75 | case 2:
|
---|
[96b02eb9] | 76 | ipc_answer_2(callid, (sysarg_t) result,
|
---|
[c7a8442] | 77 | IPC_GET_ARG1(*answer), IPC_GET_ARG2(*answer));
|
---|
| 78 | break;
|
---|
| 79 | case 3:
|
---|
[96b02eb9] | 80 | ipc_answer_3(callid, (sysarg_t) result,
|
---|
[c7a8442] | 81 | IPC_GET_ARG1(*answer), IPC_GET_ARG2(*answer),
|
---|
| 82 | IPC_GET_ARG3(*answer));
|
---|
| 83 | break;
|
---|
| 84 | case 4:
|
---|
[96b02eb9] | 85 | ipc_answer_4(callid, (sysarg_t) result,
|
---|
[c7a8442] | 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:
|
---|
[96b02eb9] | 91 | ipc_answer_5(callid, (sysarg_t) result,
|
---|
[c7a8442] | 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 | */
|
---|
[96b02eb9] | 113 | int bind_service(services_t need, sysarg_t arg1, sysarg_t arg2, sysarg_t arg3,
|
---|
[c7a8442] | 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 | */
|
---|
[96b02eb9] | 136 | int 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)
|
---|
[c7a8442] | 138 | {
|
---|
[16ac756] | 139 | int rc;
|
---|
[c7a8442] | 140 |
|
---|
| 141 | /* Connect to the needed service */
|
---|
| 142 | int phone = connect_to_service_timeout(need, timeout);
|
---|
| 143 | if (phone >= 0) {
|
---|
| 144 | /* Request the bidirectional connection */
|
---|
[96b02eb9] | 145 | sysarg_t phonehash;
|
---|
[16ac756] | 146 |
|
---|
[124c061] | 147 | rc = ipc_connect_to_me(phone, arg1, arg2, arg3, NULL,
|
---|
| 148 | &phonehash);
|
---|
[16ac756] | 149 | if (rc != EOK) {
|
---|
[c7a8442] | 150 | ipc_hangup(phone);
|
---|
[16ac756] | 151 | return rc;
|
---|
[c7a8442] | 152 | }
|
---|
| 153 | async_new_connection(phonehash, 0, NULL, client_receiver);
|
---|
| 154 | }
|
---|
| 155 |
|
---|
| 156 | return phone;
|
---|
| 157 | }
|
---|
| 158 |
|
---|
| 159 | /** Connects to the needed module.
|
---|
| 160 | *
|
---|
| 161 | * @param[in] need The needed module service.
|
---|
[1bfd3d3] | 162 | * @return The phone of the needed service.
|
---|
[c7a8442] | 163 | */
|
---|
| 164 | int connect_to_service(services_t need)
|
---|
| 165 | {
|
---|
| 166 | return connect_to_service_timeout(need, 0);
|
---|
| 167 | }
|
---|
| 168 |
|
---|
| 169 | /** Connects to the needed module.
|
---|
| 170 | *
|
---|
| 171 | * @param[in] need The needed module service.
|
---|
| 172 | * @param[in] timeout The connection timeout in microseconds. No timeout if
|
---|
| 173 | * set to zero (0).
|
---|
[1bfd3d3] | 174 | * @return The phone of the needed service.
|
---|
| 175 | * @return ETIMEOUT if the connection timeouted.
|
---|
[c7a8442] | 176 | */
|
---|
| 177 | int connect_to_service_timeout(services_t need, suseconds_t timeout)
|
---|
| 178 | {
|
---|
| 179 | int phone;
|
---|
| 180 |
|
---|
[45bb1d2] | 181 | /* If no timeout is set */
|
---|
[c7a8442] | 182 | if (timeout <= 0)
|
---|
| 183 | return async_connect_me_to_blocking(PHONE_NS, need, 0, 0);
|
---|
| 184 |
|
---|
| 185 | while (true) {
|
---|
| 186 | phone = async_connect_me_to(PHONE_NS, need, 0, 0);
|
---|
| 187 | if ((phone >= 0) || (phone != ENOENT))
|
---|
| 188 | return phone;
|
---|
| 189 |
|
---|
[45bb1d2] | 190 | /* Abort if no time is left */
|
---|
[c7a8442] | 191 | if (timeout <= 0)
|
---|
| 192 | return ETIMEOUT;
|
---|
| 193 |
|
---|
[45bb1d2] | 194 | /* Wait the minimum of the module wait time and the timeout */
|
---|
[c7a8442] | 195 | usleep((timeout <= MODULE_WAIT_TIME) ?
|
---|
| 196 | timeout : MODULE_WAIT_TIME);
|
---|
| 197 | timeout -= MODULE_WAIT_TIME;
|
---|
| 198 | }
|
---|
| 199 | }
|
---|
| 200 |
|
---|
| 201 | /** Replies the data to the other party.
|
---|
| 202 | *
|
---|
| 203 | * @param[in] data The data buffer to be sent.
|
---|
| 204 | * @param[in] data_length The buffer length.
|
---|
[1bfd3d3] | 205 | * @return EOK on success.
|
---|
| 206 | * @return EINVAL if the client does not expect the data.
|
---|
| 207 | * @return EOVERFLOW if the client does not expect all the data.
|
---|
[c7a8442] | 208 | * Only partial data are transfered.
|
---|
[1bfd3d3] | 209 | * @return Other error codes as defined for the
|
---|
[c7a8442] | 210 | * async_data_read_finalize() function.
|
---|
| 211 | */
|
---|
| 212 | int data_reply(void *data, size_t data_length)
|
---|
| 213 | {
|
---|
| 214 | size_t length;
|
---|
| 215 | ipc_callid_t callid;
|
---|
| 216 |
|
---|
[45bb1d2] | 217 | /* Fetch the request */
|
---|
[c7a8442] | 218 | if (!async_data_read_receive(&callid, &length))
|
---|
| 219 | return EINVAL;
|
---|
| 220 |
|
---|
[45bb1d2] | 221 | /* Check the requested data size */
|
---|
[c7a8442] | 222 | if (length < data_length) {
|
---|
| 223 | async_data_read_finalize(callid, data, length);
|
---|
| 224 | return EOVERFLOW;
|
---|
| 225 | }
|
---|
| 226 |
|
---|
[45bb1d2] | 227 | /* Send the data */
|
---|
[c7a8442] | 228 | return async_data_read_finalize(callid, data, data_length);
|
---|
| 229 | }
|
---|
| 230 |
|
---|
[774e6d1a] | 231 | /** Refresh answer structure and argument count.
|
---|
| 232 | *
|
---|
| 233 | * Erase all arguments.
|
---|
[c7a8442] | 234 | *
|
---|
[774e6d1a] | 235 | * @param[in,out] answer Message processing answer structure.
|
---|
| 236 | * @param[in,out] count Number of answer arguments.
|
---|
[c7a8442] | 237 | *
|
---|
| 238 | */
|
---|
[774e6d1a] | 239 | void refresh_answer(ipc_call_t *answer, size_t *count)
|
---|
[c7a8442] | 240 | {
|
---|
[774e6d1a] | 241 | if (count != NULL)
|
---|
| 242 | *count = 0;
|
---|
| 243 |
|
---|
| 244 | if (answer != NULL) {
|
---|
[c7a8442] | 245 | IPC_SET_RETVAL(*answer, 0);
|
---|
[228e490] | 246 | IPC_SET_IMETHOD(*answer, 0);
|
---|
[c7a8442] | 247 | IPC_SET_ARG1(*answer, 0);
|
---|
| 248 | IPC_SET_ARG2(*answer, 0);
|
---|
| 249 | IPC_SET_ARG3(*answer, 0);
|
---|
| 250 | IPC_SET_ARG4(*answer, 0);
|
---|
| 251 | IPC_SET_ARG5(*answer, 0);
|
---|
| 252 | }
|
---|
| 253 | }
|
---|
| 254 |
|
---|
| 255 | /** @}
|
---|
| 256 | */
|
---|