[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 | */
|
---|
[1a0fb3f8] | 50 | #define MODULE_WAIT_TIME ( 10 * 1000 )
|
---|
[21580dd] | 51 |
|
---|
| 52 | int connect_to_service( services_t need ){
|
---|
[1a0fb3f8] | 53 | return connect_to_service_timeout( need, 0 );
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | int connect_to_service_timeout( services_t need, suseconds_t timeout ){
|
---|
[bc9da2a] | 57 | if (timeout <= 0)
|
---|
| 58 | return async_connect_me_to_blocking( PHONE_NS, need, 0, 0);
|
---|
| 59 |
|
---|
[1a0fb3f8] | 60 | while( true ){
|
---|
[bc9da2a] | 61 | int phone;
|
---|
| 62 |
|
---|
| 63 | phone = async_connect_me_to( PHONE_NS, need, 0, 0);
|
---|
| 64 | if( (phone >= 0) || (phone != ENOENT) )
|
---|
[1a0fb3f8] | 65 | return phone;
|
---|
[bc9da2a] | 66 |
|
---|
| 67 | timeout -= MODULE_WAIT_TIME;
|
---|
| 68 | if( timeout <= 0 ) return ETIMEOUT;
|
---|
| 69 |
|
---|
[1a0fb3f8] | 70 | usleep( MODULE_WAIT_TIME );
|
---|
[21580dd] | 71 | }
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | int bind_service( services_t need, ipcarg_t arg1, ipcarg_t arg2, ipcarg_t arg3, async_client_conn_t client_receiver ){
|
---|
[1a0fb3f8] | 75 | return bind_service_timeout( need, arg1, arg2, arg3, client_receiver, 0 );
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | 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 ){
|
---|
[21580dd] | 79 | ERROR_DECLARE;
|
---|
| 80 |
|
---|
| 81 | int phone;
|
---|
| 82 | ipcarg_t phonehash;
|
---|
| 83 |
|
---|
[1a0fb3f8] | 84 | phone = connect_to_service_timeout( need, timeout );
|
---|
[21580dd] | 85 | if( phone >= 0 ){
|
---|
| 86 | if( ERROR_OCCURRED( ipc_connect_to_me( phone, arg1, arg2, arg3, & phonehash ))){
|
---|
[c7315dd] | 87 | ipc_hangup( phone );
|
---|
[21580dd] | 88 | return ERROR_CODE;
|
---|
| 89 | }
|
---|
| 90 | async_new_connection( phonehash, 0, NULL, client_receiver );
|
---|
| 91 | }
|
---|
| 92 | return phone;
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | void answer_call( ipc_callid_t callid, int result, ipc_call_t * answer, int answer_count ){
|
---|
| 96 | if( answer || ( ! answer_count )){
|
---|
| 97 | switch( answer_count ){
|
---|
| 98 | case 0:
|
---|
| 99 | ipc_answer_0( callid, ( ipcarg_t ) result );
|
---|
| 100 | break;
|
---|
| 101 | case 1:
|
---|
| 102 | ipc_answer_1( callid, ( ipcarg_t ) result, IPC_GET_ARG1( * answer ));
|
---|
| 103 | break;
|
---|
| 104 | case 2:
|
---|
| 105 | ipc_answer_2( callid, ( ipcarg_t ) result, IPC_GET_ARG1( * answer ), IPC_GET_ARG2( * answer ));
|
---|
| 106 | break;
|
---|
| 107 | case 3:
|
---|
| 108 | ipc_answer_3( callid, ( ipcarg_t ) result, IPC_GET_ARG1( * answer ), IPC_GET_ARG2( * answer ), IPC_GET_ARG3( * answer ));
|
---|
| 109 | break;
|
---|
| 110 | case 4:
|
---|
| 111 | ipc_answer_4( callid, ( ipcarg_t ) result, IPC_GET_ARG1( * answer ), IPC_GET_ARG2( * answer ), IPC_GET_ARG3( * answer ), IPC_GET_ARG4( * answer ));
|
---|
| 112 | break;
|
---|
| 113 | case 5:
|
---|
| 114 | default:
|
---|
| 115 | 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 ));
|
---|
| 116 | break;
|
---|
| 117 | }
|
---|
| 118 | }
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | void refresh_answer( ipc_call_t * answer, int * answer_count ){
|
---|
| 122 | if( answer_count ){
|
---|
| 123 | * answer_count = 0;
|
---|
| 124 | }
|
---|
| 125 | if( answer ){
|
---|
| 126 | IPC_SET_RETVAL( * answer, 0 );
|
---|
| 127 | // just to be precize
|
---|
| 128 | IPC_SET_METHOD( * answer, 0 );
|
---|
| 129 | IPC_SET_ARG1( * answer, 0 );
|
---|
| 130 | IPC_SET_ARG2( * answer, 0 );
|
---|
| 131 | IPC_SET_ARG3( * answer, 0 );
|
---|
| 132 | IPC_SET_ARG4( * answer, 0 );
|
---|
| 133 | IPC_SET_ARG5( * answer, 0 );
|
---|
| 134 | }
|
---|
| 135 | }
|
---|
| 136 |
|
---|
| 137 | int data_receive( void ** data, size_t * length ){
|
---|
| 138 | ERROR_DECLARE;
|
---|
| 139 |
|
---|
| 140 | ipc_callid_t callid;
|
---|
| 141 |
|
---|
| 142 | if( !( data && length )) return EBADMEM;
|
---|
| 143 | if( ! async_data_write_receive( & callid, length )) return EINVAL;
|
---|
| 144 | * data = malloc( * length );
|
---|
| 145 | if( !( * data )) return ENOMEM;
|
---|
| 146 | if( ERROR_OCCURRED( async_data_write_finalize( callid, * data, * length ))){
|
---|
| 147 | free( data );
|
---|
| 148 | return ERROR_CODE;
|
---|
| 149 | }
|
---|
| 150 | return EOK;
|
---|
| 151 | }
|
---|
| 152 |
|
---|
| 153 | int data_reply( void * data, size_t data_length ){
|
---|
| 154 | size_t length;
|
---|
| 155 | ipc_callid_t callid;
|
---|
| 156 |
|
---|
| 157 | if( ! async_data_read_receive( & callid, & length )){
|
---|
| 158 | return EINVAL;
|
---|
| 159 | }
|
---|
| 160 | if( length < data_length ){
|
---|
| 161 | async_data_read_finalize( callid, data, length );
|
---|
| 162 | return EOVERFLOW;
|
---|
| 163 | }
|
---|
| 164 | return async_data_read_finalize( callid, data, data_length );
|
---|
| 165 | }
|
---|
| 166 |
|
---|
| 167 | /** @}
|
---|
| 168 | */
|
---|