[21580dd] | 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 net
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 |
|
---|
| 33 | /** @file
|
---|
| 34 | * Generic module functions implementation.
|
---|
| 35 | */
|
---|
| 36 |
|
---|
| 37 | #include <async.h>
|
---|
| 38 | #include <malloc.h>
|
---|
| 39 |
|
---|
| 40 | #include <ipc/ipc.h>
|
---|
| 41 | #include <ipc/services.h>
|
---|
| 42 |
|
---|
[1a0fb3f8] | 43 | #include <sys/time.h>
|
---|
| 44 |
|
---|
[21580dd] | 45 | #include "err.h"
|
---|
| 46 | #include "modules.h"
|
---|
| 47 |
|
---|
[1a0fb3f8] | 48 | /** The time between connect requests in microseconds.
|
---|
[21580dd] | 49 | */
|
---|
[aadf01e] | 50 | #define MODULE_WAIT_TIME (10 * 1000)
|
---|
[21580dd] | 51 |
|
---|
[aadf01e] | 52 | void answer_call(ipc_callid_t callid, int result, ipc_call_t * answer, int answer_count){
|
---|
[a64c64d] | 53 | // choose the most efficient answer function
|
---|
[aadf01e] | 54 | if(answer || (! answer_count)){
|
---|
| 55 | switch(answer_count){
|
---|
[21580dd] | 56 | case 0:
|
---|
[aadf01e] | 57 | ipc_answer_0(callid, (ipcarg_t) result);
|
---|
[21580dd] | 58 | break;
|
---|
| 59 | case 1:
|
---|
[aadf01e] | 60 | ipc_answer_1(callid, (ipcarg_t) result, IPC_GET_ARG1(*answer));
|
---|
[21580dd] | 61 | break;
|
---|
| 62 | case 2:
|
---|
[aadf01e] | 63 | ipc_answer_2(callid, (ipcarg_t) result, IPC_GET_ARG1(*answer), IPC_GET_ARG2(*answer));
|
---|
[21580dd] | 64 | break;
|
---|
| 65 | case 3:
|
---|
[aadf01e] | 66 | ipc_answer_3(callid, (ipcarg_t) result, IPC_GET_ARG1(*answer), IPC_GET_ARG2(*answer), IPC_GET_ARG3(*answer));
|
---|
[21580dd] | 67 | break;
|
---|
| 68 | case 4:
|
---|
[aadf01e] | 69 | ipc_answer_4(callid, (ipcarg_t) result, IPC_GET_ARG1(*answer), IPC_GET_ARG2(*answer), IPC_GET_ARG3(*answer), IPC_GET_ARG4(*answer));
|
---|
[21580dd] | 70 | break;
|
---|
| 71 | case 5:
|
---|
| 72 | default:
|
---|
[aadf01e] | 73 | ipc_answer_5(callid, (ipcarg_t) result, IPC_GET_ARG1(*answer), IPC_GET_ARG2(*answer), IPC_GET_ARG3(*answer), IPC_GET_ARG4(*answer), IPC_GET_ARG5(*answer));
|
---|
[21580dd] | 74 | break;
|
---|
| 75 | }
|
---|
| 76 | }
|
---|
| 77 | }
|
---|
| 78 |
|
---|
[aadf01e] | 79 | int bind_service(services_t need, ipcarg_t arg1, ipcarg_t arg2, ipcarg_t arg3, async_client_conn_t client_receiver){
|
---|
| 80 | return bind_service_timeout(need, arg1, arg2, arg3, client_receiver, 0);
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | int bind_service_timeout(services_t need, ipcarg_t arg1, ipcarg_t arg2, ipcarg_t arg3, async_client_conn_t client_receiver, suseconds_t timeout){
|
---|
| 84 | ERROR_DECLARE;
|
---|
| 85 |
|
---|
| 86 | int phone;
|
---|
| 87 | ipcarg_t phonehash;
|
---|
| 88 |
|
---|
[a64c64d] | 89 | // connect to the needed service
|
---|
[aadf01e] | 90 | phone = connect_to_service_timeout(need, timeout);
|
---|
[a64c64d] | 91 | // if connected
|
---|
[aadf01e] | 92 | if(phone >= 0){
|
---|
[a64c64d] | 93 | // request the bidirectional connection
|
---|
[aadf01e] | 94 | if(ERROR_OCCURRED(ipc_connect_to_me(phone, arg1, arg2, arg3, &phonehash))){
|
---|
| 95 | ipc_hangup(phone);
|
---|
| 96 | return ERROR_CODE;
|
---|
| 97 | }
|
---|
| 98 | async_new_connection(phonehash, 0, NULL, client_receiver);
|
---|
[21580dd] | 99 | }
|
---|
[aadf01e] | 100 | return phone;
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | int connect_to_service(services_t need){
|
---|
| 104 | return connect_to_service_timeout(need, 0);
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 | int connect_to_service_timeout(services_t need, suseconds_t timeout){
|
---|
[a64c64d] | 108 | int phone;
|
---|
| 109 |
|
---|
| 110 | // if no timeout is set
|
---|
| 111 | if (timeout <= 0){
|
---|
[aadf01e] | 112 | return async_connect_me_to_blocking(PHONE_NS, need, 0, 0);
|
---|
[a64c64d] | 113 | }
|
---|
[aadf01e] | 114 |
|
---|
[a64c64d] | 115 | while(true){
|
---|
[aadf01e] | 116 | phone = async_connect_me_to(PHONE_NS, need, 0, 0);
|
---|
[a64c64d] | 117 | if((phone >= 0) || (phone != ENOENT)){
|
---|
[aadf01e] | 118 | return phone;
|
---|
[a64c64d] | 119 | }
|
---|
| 120 |
|
---|
| 121 | // end if no time is left
|
---|
[aadf01e] | 122 | if(timeout <= 0){
|
---|
| 123 | return ETIMEOUT;
|
---|
| 124 | }
|
---|
| 125 |
|
---|
[a64c64d] | 126 | // wait the minimum of the module wait time and the timeout
|
---|
| 127 | usleep((timeout <= MODULE_WAIT_TIME) ? timeout : MODULE_WAIT_TIME);
|
---|
| 128 | timeout -= MODULE_WAIT_TIME;
|
---|
[21580dd] | 129 | }
|
---|
| 130 | }
|
---|
| 131 |
|
---|
[aadf01e] | 132 | int data_receive(void ** data, size_t * length){
|
---|
[21580dd] | 133 | ERROR_DECLARE;
|
---|
| 134 |
|
---|
[aadf01e] | 135 | ipc_callid_t callid;
|
---|
[21580dd] | 136 |
|
---|
[aadf01e] | 137 | if(!(data && length)){
|
---|
| 138 | return EBADMEM;
|
---|
| 139 | }
|
---|
[a64c64d] | 140 |
|
---|
| 141 | // fetch the request
|
---|
[aadf01e] | 142 | if(! async_data_write_receive(&callid, length)){
|
---|
| 143 | return EINVAL;
|
---|
| 144 | }
|
---|
[a64c64d] | 145 |
|
---|
| 146 | // allocate the buffer
|
---|
[aadf01e] | 147 | *data = malloc(*length);
|
---|
| 148 | if(!(*data)){
|
---|
| 149 | return ENOMEM;
|
---|
| 150 | }
|
---|
[a64c64d] | 151 |
|
---|
| 152 | // fetch the data
|
---|
[aadf01e] | 153 | if(ERROR_OCCURRED(async_data_write_finalize(callid, * data, * length))){
|
---|
| 154 | free(data);
|
---|
[21580dd] | 155 | return ERROR_CODE;
|
---|
| 156 | }
|
---|
| 157 | return EOK;
|
---|
| 158 | }
|
---|
| 159 |
|
---|
[aadf01e] | 160 | int data_reply(void * data, size_t data_length){
|
---|
| 161 | size_t length;
|
---|
| 162 | ipc_callid_t callid;
|
---|
[21580dd] | 163 |
|
---|
[a64c64d] | 164 | // fetch the request
|
---|
[aadf01e] | 165 | if(! async_data_read_receive(&callid, &length)){
|
---|
[21580dd] | 166 | return EINVAL;
|
---|
| 167 | }
|
---|
[a64c64d] | 168 |
|
---|
| 169 | // check the requested data size
|
---|
[aadf01e] | 170 | if(length < data_length){
|
---|
| 171 | async_data_read_finalize(callid, data, length);
|
---|
[21580dd] | 172 | return EOVERFLOW;
|
---|
| 173 | }
|
---|
[a64c64d] | 174 |
|
---|
| 175 | // send the data
|
---|
[aadf01e] | 176 | return async_data_read_finalize(callid, data, data_length);
|
---|
| 177 | }
|
---|
| 178 |
|
---|
| 179 | void refresh_answer(ipc_call_t * answer, int * answer_count){
|
---|
[a64c64d] | 180 |
|
---|
[aadf01e] | 181 | if(answer_count){
|
---|
| 182 | *answer_count = 0;
|
---|
| 183 | }
|
---|
[a64c64d] | 184 |
|
---|
[aadf01e] | 185 | if(answer){
|
---|
| 186 | IPC_SET_RETVAL(*answer, 0);
|
---|
| 187 | // just to be precize
|
---|
| 188 | IPC_SET_METHOD(*answer, 0);
|
---|
| 189 | IPC_SET_ARG1(*answer, 0);
|
---|
| 190 | IPC_SET_ARG2(*answer, 0);
|
---|
| 191 | IPC_SET_ARG3(*answer, 0);
|
---|
| 192 | IPC_SET_ARG4(*answer, 0);
|
---|
| 193 | IPC_SET_ARG5(*answer, 0);
|
---|
| 194 | }
|
---|
[21580dd] | 195 | }
|
---|
| 196 |
|
---|
| 197 | /** @}
|
---|
| 198 | */
|
---|