| 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 | #include <ns.h>
|
|---|
| 48 |
|
|---|
| 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.
|
|---|
| 55 | *
|
|---|
| 56 | */
|
|---|
| 57 | void answer_call(ipc_callid_t callid, int result, ipc_call_t *answer,
|
|---|
| 58 | size_t count)
|
|---|
| 59 | {
|
|---|
| 60 | /* Choose the most efficient function */
|
|---|
| 61 | if ((answer != NULL) || (count == 0)) {
|
|---|
| 62 | switch (count) {
|
|---|
| 63 | case 0:
|
|---|
| 64 | async_answer_0(callid, (sysarg_t) result);
|
|---|
| 65 | break;
|
|---|
| 66 | case 1:
|
|---|
| 67 | async_answer_1(callid, (sysarg_t) result,
|
|---|
| 68 | IPC_GET_ARG1(*answer));
|
|---|
| 69 | break;
|
|---|
| 70 | case 2:
|
|---|
| 71 | async_answer_2(callid, (sysarg_t) result,
|
|---|
| 72 | IPC_GET_ARG1(*answer), IPC_GET_ARG2(*answer));
|
|---|
| 73 | break;
|
|---|
| 74 | case 3:
|
|---|
| 75 | async_answer_3(callid, (sysarg_t) result,
|
|---|
| 76 | IPC_GET_ARG1(*answer), IPC_GET_ARG2(*answer),
|
|---|
| 77 | IPC_GET_ARG3(*answer));
|
|---|
| 78 | break;
|
|---|
| 79 | case 4:
|
|---|
| 80 | async_answer_4(callid, (sysarg_t) result,
|
|---|
| 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:
|
|---|
| 86 | async_answer_5(callid, (sysarg_t) result,
|
|---|
| 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 |
|
|---|
| 95 | /** Create bidirectional connection with the needed module service and register
|
|---|
| 96 | * the message receiver.
|
|---|
| 97 | *
|
|---|
| 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.
|
|---|
| 107 | *
|
|---|
| 108 | */
|
|---|
| 109 | async_sess_t *bind_service(services_t need, sysarg_t arg1, sysarg_t arg2,
|
|---|
| 110 | sysarg_t arg3, async_client_conn_t client_receiver)
|
|---|
| 111 | {
|
|---|
| 112 | /* Connect to the needed service */
|
|---|
| 113 | async_sess_t *sess = connect_to_service(need);
|
|---|
| 114 | if (sess != NULL) {
|
|---|
| 115 | /* Request the bidirectional connection */
|
|---|
| 116 | async_exch_t *exch = async_exchange_begin(sess);
|
|---|
| 117 | int rc = async_connect_to_me(exch, arg1, arg2, arg3,
|
|---|
| 118 | client_receiver, NULL);
|
|---|
| 119 | async_exchange_end(exch);
|
|---|
| 120 |
|
|---|
| 121 | if (rc != EOK) {
|
|---|
| 122 | async_hangup(sess);
|
|---|
| 123 | errno = rc;
|
|---|
| 124 | return NULL;
|
|---|
| 125 | }
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 | return sess;
|
|---|
| 129 | }
|
|---|
| 130 |
|
|---|
| 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.
|
|---|
| 137 | *
|
|---|
| 138 | */
|
|---|
| 139 | async_sess_t *connect_to_service(services_t need)
|
|---|
| 140 | {
|
|---|
| 141 | return service_connect_blocking(EXCHANGE_SERIALIZE, need, 0, 0);
|
|---|
| 142 | }
|
|---|
| 143 |
|
|---|
| 144 | /** Reply the data to the other party.
|
|---|
| 145 | *
|
|---|
| 146 | * @param[in] data The data buffer to be sent.
|
|---|
| 147 | * @param[in] data_length The buffer length.
|
|---|
| 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 | *
|
|---|
| 156 | */
|
|---|
| 157 | int data_reply(void *data, size_t data_length)
|
|---|
| 158 | {
|
|---|
| 159 | size_t length;
|
|---|
| 160 | ipc_callid_t callid;
|
|---|
| 161 |
|
|---|
| 162 | /* Fetch the request */
|
|---|
| 163 | if (!async_data_read_receive(&callid, &length))
|
|---|
| 164 | return EINVAL;
|
|---|
| 165 |
|
|---|
| 166 | /* Check the requested data size */
|
|---|
| 167 | if (length < data_length) {
|
|---|
| 168 | async_data_read_finalize(callid, data, length);
|
|---|
| 169 | return EOVERFLOW;
|
|---|
| 170 | }
|
|---|
| 171 |
|
|---|
| 172 | /* Send the data */
|
|---|
| 173 | return async_data_read_finalize(callid, data, data_length);
|
|---|
| 174 | }
|
|---|
| 175 |
|
|---|
| 176 | /** Refresh answer structure and argument count.
|
|---|
| 177 | *
|
|---|
| 178 | * Erase all arguments.
|
|---|
| 179 | *
|
|---|
| 180 | * @param[in,out] answer Message processing answer structure.
|
|---|
| 181 | * @param[in,out] count Number of answer arguments.
|
|---|
| 182 | *
|
|---|
| 183 | */
|
|---|
| 184 | void refresh_answer(ipc_call_t *answer, size_t *count)
|
|---|
| 185 | {
|
|---|
| 186 | if (count != NULL)
|
|---|
| 187 | *count = 0;
|
|---|
| 188 |
|
|---|
| 189 | if (answer != NULL) {
|
|---|
| 190 | IPC_SET_RETVAL(*answer, 0);
|
|---|
| 191 | IPC_SET_IMETHOD(*answer, 0);
|
|---|
| 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 | */
|
|---|