1 | /*
|
---|
2 | * Copyright (c) 2011 Martin Decky
|
---|
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 libc
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /** @file
|
---|
33 | */
|
---|
34 |
|
---|
35 | #include <ns.h>
|
---|
36 | #include <ipc/ns.h>
|
---|
37 | #include <async.h>
|
---|
38 | #include <macros.h>
|
---|
39 | #include <errno.h>
|
---|
40 | #include "private/ns.h"
|
---|
41 |
|
---|
42 | /*
|
---|
43 | * XXX ns does not know about session_ns, so we create an extra session for
|
---|
44 | * actual communicaton
|
---|
45 | */
|
---|
46 | static async_sess_t *sess_ns = NULL;
|
---|
47 |
|
---|
48 | errno_t service_register(service_t service, iface_t iface,
|
---|
49 | async_port_handler_t handler, void *data)
|
---|
50 | {
|
---|
51 | async_sess_t *sess = ns_session_get();
|
---|
52 | if (sess == NULL)
|
---|
53 | return EIO;
|
---|
54 |
|
---|
55 | port_id_t port;
|
---|
56 | errno_t rc = async_create_port(iface, handler, data, &port);
|
---|
57 | if (rc != EOK)
|
---|
58 | return rc;
|
---|
59 |
|
---|
60 | async_exch_t *exch = async_exchange_begin(sess);
|
---|
61 |
|
---|
62 | ipc_call_t answer;
|
---|
63 | aid_t req = async_send_2(exch, NS_REGISTER, service, iface, &answer);
|
---|
64 | rc = async_connect_to_me(exch, iface, service, 0);
|
---|
65 |
|
---|
66 | async_exchange_end(exch);
|
---|
67 |
|
---|
68 | if (rc != EOK) {
|
---|
69 | async_forget(req);
|
---|
70 | return rc;
|
---|
71 | }
|
---|
72 |
|
---|
73 | errno_t retval;
|
---|
74 | async_wait_for(req, &retval);
|
---|
75 | return rc;
|
---|
76 | }
|
---|
77 |
|
---|
78 | errno_t service_register_broker(service_t service, async_port_handler_t handler,
|
---|
79 | void *data)
|
---|
80 | {
|
---|
81 | async_set_fallback_port_handler(handler, data);
|
---|
82 |
|
---|
83 | async_sess_t *sess = ns_session_get();
|
---|
84 | if (sess == NULL)
|
---|
85 | return EIO;
|
---|
86 |
|
---|
87 | async_exch_t *exch = async_exchange_begin(sess);
|
---|
88 |
|
---|
89 | ipc_call_t answer;
|
---|
90 | aid_t req = async_send_1(exch, NS_REGISTER_BROKER, service, &answer);
|
---|
91 | errno_t rc = async_connect_to_me(exch, INTERFACE_ANY, service, 0);
|
---|
92 |
|
---|
93 | async_exchange_end(exch);
|
---|
94 |
|
---|
95 | if (rc != EOK) {
|
---|
96 | async_forget(req);
|
---|
97 | return rc;
|
---|
98 | }
|
---|
99 |
|
---|
100 | errno_t retval;
|
---|
101 | async_wait_for(req, &retval);
|
---|
102 | return rc;
|
---|
103 | }
|
---|
104 |
|
---|
105 | async_sess_t *service_connect(service_t service, iface_t iface, sysarg_t arg3)
|
---|
106 | {
|
---|
107 | async_sess_t *sess = ns_session_get();
|
---|
108 | if (sess == NULL)
|
---|
109 | return NULL;
|
---|
110 |
|
---|
111 | async_exch_t *exch = async_exchange_begin(sess);
|
---|
112 | if (exch == NULL)
|
---|
113 | return NULL;
|
---|
114 |
|
---|
115 | async_sess_t *csess =
|
---|
116 | async_connect_me_to(exch, iface, service, arg3);
|
---|
117 | async_exchange_end(exch);
|
---|
118 |
|
---|
119 | if (csess == NULL)
|
---|
120 | return NULL;
|
---|
121 |
|
---|
122 | /*
|
---|
123 | * FIXME Ugly hack to work around limitation of implementing
|
---|
124 | * parallel exchanges using multiple connections. Shift out
|
---|
125 | * first argument for non-initial connections.
|
---|
126 | */
|
---|
127 | async_sess_args_set(csess, iface, arg3, 0);
|
---|
128 |
|
---|
129 | return csess;
|
---|
130 | }
|
---|
131 |
|
---|
132 | async_sess_t *service_connect_blocking(service_t service, iface_t iface,
|
---|
133 | sysarg_t arg3)
|
---|
134 | {
|
---|
135 | async_sess_t *sess = ns_session_get();
|
---|
136 | if (sess == NULL)
|
---|
137 | return NULL;
|
---|
138 |
|
---|
139 | async_exch_t *exch = async_exchange_begin(sess);
|
---|
140 | async_sess_t *csess =
|
---|
141 | async_connect_me_to_blocking(exch, iface, service, arg3);
|
---|
142 | async_exchange_end(exch);
|
---|
143 |
|
---|
144 | if (csess == NULL)
|
---|
145 | return NULL;
|
---|
146 |
|
---|
147 | /*
|
---|
148 | * FIXME Ugly hack to work around limitation of implementing
|
---|
149 | * parallel exchanges using multiple connections. Shift out
|
---|
150 | * first argument for non-initial connections.
|
---|
151 | */
|
---|
152 | async_sess_args_set(csess, iface, arg3, 0);
|
---|
153 |
|
---|
154 | return csess;
|
---|
155 | }
|
---|
156 |
|
---|
157 | errno_t ns_ping(void)
|
---|
158 | {
|
---|
159 | async_sess_t *sess = ns_session_get();
|
---|
160 | if (sess == NULL)
|
---|
161 | return EIO;
|
---|
162 |
|
---|
163 | async_exch_t *exch = async_exchange_begin(sess);
|
---|
164 | errno_t rc = async_req_0_0(exch, NS_PING);
|
---|
165 | async_exchange_end(exch);
|
---|
166 |
|
---|
167 | return rc;
|
---|
168 | }
|
---|
169 |
|
---|
170 | errno_t ns_intro(task_id_t id)
|
---|
171 | {
|
---|
172 | async_exch_t *exch;
|
---|
173 | async_sess_t *sess = ns_session_get();
|
---|
174 | if (sess == NULL)
|
---|
175 | return EIO;
|
---|
176 |
|
---|
177 | exch = async_exchange_begin(sess);
|
---|
178 | errno_t rc = async_req_2_0(exch, NS_ID_INTRO, LOWER32(id), UPPER32(id));
|
---|
179 | async_exchange_end(exch);
|
---|
180 |
|
---|
181 | return rc;
|
---|
182 | }
|
---|
183 |
|
---|
184 | async_sess_t *ns_session_get(void)
|
---|
185 | {
|
---|
186 | async_exch_t *exch;
|
---|
187 |
|
---|
188 | if (sess_ns == NULL) {
|
---|
189 | exch = async_exchange_begin(&session_ns);
|
---|
190 | sess_ns = async_connect_me_to(exch, 0, 0, 0);
|
---|
191 | async_exchange_end(exch);
|
---|
192 | if (sess_ns == NULL)
|
---|
193 | return NULL;
|
---|
194 | }
|
---|
195 |
|
---|
196 | return sess_ns;
|
---|
197 | }
|
---|
198 |
|
---|
199 | /** @}
|
---|
200 | */
|
---|