source: mainline/uspace/srv/net/modules.c@ 9f2ea28

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 9f2ea28 was aadf01e, checked in by Lukas Mejdrech <lukasmejdrech@…>, 15 years ago

Coding style (no functional change)

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