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