Rev 966 | Rev 1006 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
954 | palkovsky | 1 | /* |
2 | * Copyright (C) 2006 Ondrej Palkovsky |
||
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 | #include <ipc.h> |
||
30 | #include <libc.h> |
||
999 | palkovsky | 31 | #include <malloc.h> |
32 | #include <errno.h> |
||
33 | #include <list.h> |
||
34 | #include <stdio.h> |
||
35 | #include <unistd.h> |
||
954 | palkovsky | 36 | |
999 | palkovsky | 37 | /** Structure used for keeping track of sent async msgs |
38 | * and queing unsent msgs |
||
39 | * |
||
40 | */ |
||
41 | typedef struct { |
||
42 | link_t list; |
||
43 | |||
44 | ipc_async_callback_t callback; |
||
45 | void *private; |
||
46 | union { |
||
47 | ipc_callid_t callid; |
||
48 | struct { |
||
49 | int phoneid; |
||
50 | ipc_data_t data; |
||
51 | } msg; |
||
52 | }u; |
||
53 | } async_call_t; |
||
54 | |||
55 | LIST_INITIALIZE(dispatched_calls); |
||
56 | LIST_INITIALIZE(queued_calls); |
||
57 | |||
58 | int ipc_call_sync(int phoneid, ipcarg_t method, ipcarg_t arg1, |
||
59 | ipcarg_t *result) |
||
954 | palkovsky | 60 | { |
966 | palkovsky | 61 | ipc_data_t resdata; |
62 | int callres; |
||
63 | |||
999 | palkovsky | 64 | callres = __SYSCALL4(SYS_IPC_CALL_SYNC_FAST, phoneid, method, arg1, |
966 | palkovsky | 65 | (sysarg_t)&resdata); |
66 | if (callres) |
||
67 | return callres; |
||
68 | if (result) |
||
69 | *result = IPC_GET_ARG1(resdata); |
||
70 | return IPC_GET_RETVAL(resdata); |
||
954 | palkovsky | 71 | } |
72 | |||
999 | palkovsky | 73 | int ipc_call_sync_3(int phoneid, ipcarg_t method, ipcarg_t arg1, |
74 | ipcarg_t arg2, ipcarg_t arg3, |
||
75 | ipcarg_t *result1, ipcarg_t *result2, ipcarg_t *result3) |
||
954 | palkovsky | 76 | { |
966 | palkovsky | 77 | ipc_data_t data; |
78 | int callres; |
||
79 | |||
80 | IPC_SET_METHOD(data, method); |
||
81 | IPC_SET_ARG1(data, arg1); |
||
82 | IPC_SET_ARG2(data, arg2); |
||
83 | IPC_SET_ARG3(data, arg3); |
||
84 | |||
999 | palkovsky | 85 | callres = __SYSCALL2(SYS_IPC_CALL_SYNC, phoneid, (sysarg_t)&data); |
966 | palkovsky | 86 | if (callres) |
87 | return callres; |
||
88 | |||
89 | if (result1) |
||
90 | *result1 = IPC_GET_ARG1(data); |
||
91 | if (result2) |
||
92 | *result2 = IPC_GET_ARG2(data); |
||
93 | if (result3) |
||
94 | *result3 = IPC_GET_ARG3(data); |
||
95 | return IPC_GET_RETVAL(data); |
||
96 | } |
||
97 | |||
999 | palkovsky | 98 | /** Syscall to send asynchronous message */ |
99 | static ipc_callid_t _ipc_call_async(int phoneid, ipc_data_t *data) |
||
100 | { |
||
101 | return __SYSCALL2(SYS_IPC_CALL_ASYNC, phoneid, (sysarg_t)data); |
||
102 | } |
||
103 | |||
966 | palkovsky | 104 | /** Send asynchronous message |
105 | * |
||
106 | * - if fatal error, call callback handler with proper error code |
||
107 | * - if message cannot be temporarily sent, add to queue |
||
108 | */ |
||
999 | palkovsky | 109 | void ipc_call_async_2(int phoneid, ipcarg_t method, ipcarg_t arg1, |
110 | ipcarg_t arg2, void *private, |
||
966 | palkovsky | 111 | ipc_async_callback_t callback) |
112 | { |
||
999 | palkovsky | 113 | async_call_t *call; |
966 | palkovsky | 114 | ipc_callid_t callid; |
115 | |||
999 | palkovsky | 116 | call = malloc(sizeof(*call)); |
117 | if (!call) { |
||
118 | callback(private, ENOMEM, NULL); |
||
119 | } |
||
120 | |||
121 | callid = __SYSCALL4(SYS_IPC_CALL_ASYNC_FAST, phoneid, method, arg1, arg2); |
||
966 | palkovsky | 122 | if (callid == IPC_CALLRET_FATAL) { |
123 | /* Call asynchronous handler with error code */ |
||
999 | palkovsky | 124 | IPC_SET_RETVAL(call->u.msg.data, ENOENT); |
125 | callback(private, ENOENT, NULL); |
||
126 | free(call); |
||
966 | palkovsky | 127 | return; |
128 | } |
||
999 | palkovsky | 129 | |
130 | call->callback = callback; |
||
131 | call->private = private; |
||
132 | |||
966 | palkovsky | 133 | if (callid == IPC_CALLRET_TEMPORARY) { |
134 | /* Add asynchronous call to queue of non-dispatched async calls */ |
||
999 | palkovsky | 135 | call->u.msg.phoneid = phoneid; |
136 | IPC_SET_METHOD(call->u.msg.data, method); |
||
137 | IPC_SET_ARG1(call->u.msg.data, arg1); |
||
138 | IPC_SET_ARG2(call->u.msg.data, arg2); |
||
139 | |||
140 | list_append(&call->list, &queued_calls); |
||
966 | palkovsky | 141 | return; |
142 | } |
||
999 | palkovsky | 143 | call->u.callid = callid; |
144 | /* Add call to list of dispatched calls */ |
||
145 | list_append(&call->list, &dispatched_calls); |
||
954 | palkovsky | 146 | } |
147 | |||
966 | palkovsky | 148 | |
149 | /** Send answer to a received call */ |
||
999 | palkovsky | 150 | void ipc_answer(ipc_callid_t callid, ipcarg_t retval, ipcarg_t arg1, |
151 | ipcarg_t arg2) |
||
954 | palkovsky | 152 | { |
966 | palkovsky | 153 | __SYSCALL4(SYS_IPC_ANSWER, callid, retval, arg1, arg2); |
954 | palkovsky | 154 | } |
155 | |||
966 | palkovsky | 156 | |
954 | palkovsky | 157 | /** Call syscall function sys_ipc_wait_for_call */ |
158 | static inline ipc_callid_t _ipc_wait_for_call(ipc_data_t *data, int flags) |
||
159 | { |
||
160 | return __SYSCALL2(SYS_IPC_WAIT, (sysarg_t)data, flags); |
||
161 | } |
||
162 | |||
999 | palkovsky | 163 | /** Try to dispatch queed calls from async queue */ |
164 | static void try_dispatch_queued_calls(void) |
||
165 | { |
||
166 | async_call_t *call; |
||
167 | ipc_callid_t callid; |
||
168 | |||
169 | while (!list_empty(&queued_calls)) { |
||
170 | call = list_get_instance(queued_calls.next, async_call_t, |
||
171 | list); |
||
172 | |||
173 | callid = _ipc_call_async(call->u.msg.phoneid, |
||
174 | &call->u.msg.data); |
||
175 | if (callid == IPC_CALLRET_TEMPORARY) |
||
176 | break; |
||
177 | list_remove(&call->list); |
||
178 | if (callid == IPC_CALLRET_FATAL) { |
||
179 | call->callback(call->private, ENOENT, NULL); |
||
180 | free(call); |
||
181 | } else { |
||
182 | call->u.callid = callid; |
||
183 | list_append(&call->list, &dispatched_calls); |
||
184 | } |
||
185 | } |
||
186 | } |
||
187 | |||
188 | /** Handle received answer |
||
189 | * |
||
190 | * TODO: Make it use hash table |
||
191 | * |
||
192 | * @param callid Callid (with first bit set) of the answered call |
||
193 | */ |
||
194 | static void handle_answer(ipc_callid_t callid, ipc_data_t *data) |
||
195 | { |
||
196 | link_t *item; |
||
197 | async_call_t *call; |
||
198 | |||
199 | callid &= ~IPC_CALLID_ANSWERED; |
||
200 | |||
201 | for (item = dispatched_calls.next; item != &dispatched_calls; |
||
202 | item = item->next) { |
||
203 | call = list_get_instance(item, async_call_t, list); |
||
204 | if (call->u.callid == callid) { |
||
205 | list_remove(&call->list); |
||
206 | call->callback(call->private, |
||
207 | IPC_GET_RETVAL(*data), |
||
208 | data); |
||
209 | return; |
||
210 | } |
||
211 | } |
||
212 | printf("Received unidentified answer: %P!!!\n", callid); |
||
213 | } |
||
214 | |||
215 | |||
954 | palkovsky | 216 | /** Wait for IPC call and return |
217 | * |
||
218 | * - dispatch ASYNC reoutines in the background |
||
999 | palkovsky | 219 | * @param data Space where the message is stored |
220 | * @return Callid or 0 if nothing available and started with |
||
221 | * IPC_WAIT_NONBLOCKING |
||
954 | palkovsky | 222 | */ |
223 | int ipc_wait_for_call(ipc_data_t *data, int flags) |
||
224 | { |
||
225 | ipc_callid_t callid; |
||
226 | |||
966 | palkovsky | 227 | do { |
999 | palkovsky | 228 | try_dispatch_queued_calls(); |
229 | |||
966 | palkovsky | 230 | callid = _ipc_wait_for_call(data, flags); |
999 | palkovsky | 231 | /* Handle received answers */ |
232 | if (callid & IPC_CALLID_ANSWERED) |
||
233 | handle_answer(callid, data); |
||
966 | palkovsky | 234 | } while (callid & IPC_CALLID_ANSWERED); |
999 | palkovsky | 235 | |
954 | palkovsky | 236 | return callid; |
237 | } |