source: mainline/uspace/lib/c/generic/ns.c@ 514d561

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 514d561 was 9b1baac, checked in by Martin Decky <martin@…>, 7 years ago

ns: register service interfaces individually

Each service interface is now registered individually with the naming
service. This adds a degree of type safety, potentially allows the
individual interfaces to be implemented by independent tasks and moves
the code slightly closer to the full-fledged ports design.

Broker services (e.g. the location service) can still register a
fallback port for receiving connections to all interface types
explicitly using service_register_broker().

  • Property mode set to 100644
File size: 4.9 KB
Line 
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 */
46static async_sess_t *sess_ns = NULL;
47
48errno_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
78errno_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
105async_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
132async_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
158errno_t ns_ping(void)
159{
160 async_sess_t *sess = ns_session_get();
161 if (sess == NULL)
162 return EIO;
163
164 async_exch_t *exch = async_exchange_begin(sess);
165 errno_t rc = async_req_0_0(exch, NS_PING);
166 async_exchange_end(exch);
167
168 return rc;
169}
170
171errno_t ns_intro(task_id_t id)
172{
173 async_exch_t *exch;
174 async_sess_t *sess = ns_session_get();
175 if (sess == NULL)
176 return EIO;
177
178 exch = async_exchange_begin(sess);
179 errno_t rc = async_req_2_0(exch, NS_ID_INTRO, LOWER32(id), UPPER32(id));
180 async_exchange_end(exch);
181
182 return rc;
183}
184
185async_sess_t *ns_session_get(void)
186{
187 async_exch_t *exch;
188
189 if (sess_ns == NULL) {
190 exch = async_exchange_begin(&session_ns);
191 sess_ns = async_connect_me_to(exch, 0, 0, 0);
192 async_exchange_end(exch);
193 if (sess_ns == NULL)
194 return NULL;
195 }
196
197 return sess_ns;
198}
199
200/** @}
201 */
Note: See TracBrowser for help on using the repository browser.