[007e6efa] | 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 |
|
---|
[79ae36dd] | 35 | #include <ns.h>
|
---|
[007e6efa] | 36 | #include <ipc/ns.h>
|
---|
[79ae36dd] | 37 | #include <async.h>
|
---|
| 38 | #include <macros.h>
|
---|
[b1bd89ea] | 39 | #include <errno.h>
|
---|
[79ae36dd] | 40 | #include "private/ns.h"
|
---|
[007e6efa] | 41 |
|
---|
[7b616e2] | 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 |
|
---|
[9b1baac] | 48 | errno_t service_register(service_t service, iface_t iface,
|
---|
| 49 | async_port_handler_t handler, void *data)
|
---|
[007e6efa] | 50 | {
|
---|
[01900b6] | 51 | errno_t rc;
|
---|
| 52 | async_sess_t *sess = ns_session_get(&rc);
|
---|
[9b1baac] | 53 | if (sess == NULL)
|
---|
[01900b6] | 54 | return rc;
|
---|
[9b1baac] | 55 |
|
---|
| 56 | port_id_t port;
|
---|
[01900b6] | 57 | rc = async_create_port(iface, handler, data, &port);
|
---|
[9b1baac] | 58 | if (rc != EOK)
|
---|
| 59 | return rc;
|
---|
| 60 |
|
---|
| 61 | async_exch_t *exch = async_exchange_begin(sess);
|
---|
| 62 |
|
---|
[7b616e2] | 63 | ipc_call_t answer;
|
---|
[9b1baac] | 64 | aid_t req = async_send_2(exch, NS_REGISTER, service, iface, &answer);
|
---|
| 65 | rc = async_connect_to_me(exch, iface, service, 0);
|
---|
| 66 |
|
---|
| 67 | async_exchange_end(exch);
|
---|
| 68 |
|
---|
| 69 | if (rc != EOK) {
|
---|
| 70 | async_forget(req);
|
---|
| 71 | return rc;
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | errno_t retval;
|
---|
| 75 | async_wait_for(req, &retval);
|
---|
| 76 | return rc;
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | errno_t service_register_broker(service_t service, async_port_handler_t handler,
|
---|
| 80 | void *data)
|
---|
| 81 | {
|
---|
| 82 | async_set_fallback_port_handler(handler, data);
|
---|
[a35b458] | 83 |
|
---|
[01900b6] | 84 | errno_t rc;
|
---|
| 85 | async_sess_t *sess = ns_session_get(&rc);
|
---|
[7b616e2] | 86 | if (sess == NULL)
|
---|
[01900b6] | 87 | return rc;
|
---|
[a35b458] | 88 |
|
---|
[7b616e2] | 89 | async_exch_t *exch = async_exchange_begin(sess);
|
---|
[9b1baac] | 90 |
|
---|
| 91 | ipc_call_t answer;
|
---|
| 92 | aid_t req = async_send_1(exch, NS_REGISTER_BROKER, service, &answer);
|
---|
[01900b6] | 93 | rc = async_connect_to_me(exch, INTERFACE_ANY, service, 0);
|
---|
[a35b458] | 94 |
|
---|
[79ae36dd] | 95 | async_exchange_end(exch);
|
---|
[a35b458] | 96 |
|
---|
[7b616e2] | 97 | if (rc != EOK) {
|
---|
| 98 | async_forget(req);
|
---|
| 99 | return rc;
|
---|
| 100 | }
|
---|
[a35b458] | 101 |
|
---|
[9b1baac] | 102 | errno_t retval;
|
---|
[7b616e2] | 103 | async_wait_for(req, &retval);
|
---|
[79ae36dd] | 104 | return rc;
|
---|
| 105 | }
|
---|
| 106 |
|
---|
[01900b6] | 107 | /** Connect to a singleton service.
|
---|
| 108 | *
|
---|
| 109 | * @param service Singleton service ID.
|
---|
| 110 | * @param iface Interface to connect to.
|
---|
| 111 | * @param arg3 Custom connection argument.
|
---|
| 112 | * @param rc Placeholder for return code. Unused if NULL.
|
---|
| 113 | *
|
---|
| 114 | * @return New session on success or NULL on error.
|
---|
| 115 | *
|
---|
| 116 | */
|
---|
| 117 | async_sess_t *service_connect(service_t service, iface_t iface, sysarg_t arg3,
|
---|
| 118 | errno_t *rc)
|
---|
[0dd16778] | 119 | {
|
---|
[01900b6] | 120 | async_sess_t *sess = ns_session_get(rc);
|
---|
[7b616e2] | 121 | if (sess == NULL)
|
---|
| 122 | return NULL;
|
---|
[a35b458] | 123 |
|
---|
[7b616e2] | 124 | async_exch_t *exch = async_exchange_begin(sess);
|
---|
| 125 | if (exch == NULL)
|
---|
[0dd16778] | 126 | return NULL;
|
---|
[a35b458] | 127 |
|
---|
[7b616e2] | 128 | async_sess_t *csess =
|
---|
[01900b6] | 129 | async_connect_me_to(exch, iface, service, arg3, rc);
|
---|
[0dd16778] | 130 | async_exchange_end(exch);
|
---|
[a35b458] | 131 |
|
---|
[7b616e2] | 132 | if (csess == NULL)
|
---|
[0dd16778] | 133 | return NULL;
|
---|
[a35b458] | 134 |
|
---|
[0dd16778] | 135 | /*
|
---|
| 136 | * FIXME Ugly hack to work around limitation of implementing
|
---|
| 137 | * parallel exchanges using multiple connections. Shift out
|
---|
| 138 | * first argument for non-initial connections.
|
---|
| 139 | */
|
---|
[7b616e2] | 140 | async_sess_args_set(csess, iface, arg3, 0);
|
---|
[a35b458] | 141 |
|
---|
[7b616e2] | 142 | return csess;
|
---|
[0dd16778] | 143 | }
|
---|
| 144 |
|
---|
[01900b6] | 145 | /** Wait and connect to a singleton service.
|
---|
| 146 | *
|
---|
| 147 | * @param service Singleton service ID.
|
---|
| 148 | * @param iface Interface to connect to.
|
---|
| 149 | * @param arg3 Custom connection argument.
|
---|
| 150 | * @param rc Placeholder for return code. Unused if NULL.
|
---|
| 151 | *
|
---|
| 152 | * @return New session on success or NULL on error.
|
---|
| 153 | *
|
---|
| 154 | */
|
---|
[f9b2cb4c] | 155 | async_sess_t *service_connect_blocking(service_t service, iface_t iface,
|
---|
[01900b6] | 156 | sysarg_t arg3, errno_t *rc)
|
---|
[566992e1] | 157 | {
|
---|
[01900b6] | 158 | async_sess_t *sess = ns_session_get(rc);
|
---|
[7b616e2] | 159 | if (sess == NULL)
|
---|
| 160 | return NULL;
|
---|
[a35b458] | 161 |
|
---|
[7b616e2] | 162 | async_exch_t *exch = async_exchange_begin(sess);
|
---|
| 163 | async_sess_t *csess =
|
---|
[01900b6] | 164 | async_connect_me_to_blocking(exch, iface, service, arg3, rc);
|
---|
[566992e1] | 165 | async_exchange_end(exch);
|
---|
[a35b458] | 166 |
|
---|
[7b616e2] | 167 | if (csess == NULL)
|
---|
[566992e1] | 168 | return NULL;
|
---|
[a35b458] | 169 |
|
---|
[566992e1] | 170 | /*
|
---|
| 171 | * FIXME Ugly hack to work around limitation of implementing
|
---|
| 172 | * parallel exchanges using multiple connections. Shift out
|
---|
| 173 | * first argument for non-initial connections.
|
---|
| 174 | */
|
---|
[7b616e2] | 175 | async_sess_args_set(csess, iface, arg3, 0);
|
---|
[a35b458] | 176 |
|
---|
[7b616e2] | 177 | return csess;
|
---|
[566992e1] | 178 | }
|
---|
| 179 |
|
---|
[b7fd2a0] | 180 | errno_t ns_ping(void)
|
---|
[007e6efa] | 181 | {
|
---|
[01900b6] | 182 | errno_t rc;
|
---|
| 183 | async_sess_t *sess = ns_session_get(&rc);
|
---|
[7b616e2] | 184 | if (sess == NULL)
|
---|
[01900b6] | 185 | return rc;
|
---|
[a35b458] | 186 |
|
---|
[7b616e2] | 187 | async_exch_t *exch = async_exchange_begin(sess);
|
---|
[01900b6] | 188 | rc = async_req_0_0(exch, NS_PING);
|
---|
[79ae36dd] | 189 | async_exchange_end(exch);
|
---|
[a35b458] | 190 |
|
---|
[79ae36dd] | 191 | return rc;
|
---|
[007e6efa] | 192 | }
|
---|
| 193 |
|
---|
[b7fd2a0] | 194 | errno_t ns_intro(task_id_t id)
|
---|
[007e6efa] | 195 | {
|
---|
[01900b6] | 196 | errno_t rc;
|
---|
| 197 | async_sess_t *sess = ns_session_get(&rc);
|
---|
[7b616e2] | 198 | if (sess == NULL)
|
---|
| 199 | return EIO;
|
---|
[a35b458] | 200 |
|
---|
[01900b6] | 201 | async_exch_t *exch = async_exchange_begin(sess);
|
---|
| 202 | rc = async_req_2_0(exch, NS_ID_INTRO, LOWER32(id), UPPER32(id));
|
---|
[79ae36dd] | 203 | async_exchange_end(exch);
|
---|
[a35b458] | 204 |
|
---|
[79ae36dd] | 205 | return rc;
|
---|
[007e6efa] | 206 | }
|
---|
| 207 |
|
---|
[01900b6] | 208 | async_sess_t *ns_session_get(errno_t *rc)
|
---|
[7b616e2] | 209 | {
|
---|
| 210 | async_exch_t *exch;
|
---|
[a35b458] | 211 |
|
---|
[7b616e2] | 212 | if (sess_ns == NULL) {
|
---|
[9f272d9] | 213 | exch = async_exchange_begin(&session_ns);
|
---|
[01900b6] | 214 | sess_ns = async_connect_me_to(exch, 0, 0, 0, rc);
|
---|
[7b616e2] | 215 | async_exchange_end(exch);
|
---|
| 216 | if (sess_ns == NULL)
|
---|
| 217 | return NULL;
|
---|
| 218 | }
|
---|
[a35b458] | 219 |
|
---|
[7b616e2] | 220 | return sess_ns;
|
---|
| 221 | }
|
---|
| 222 |
|
---|
[007e6efa] | 223 | /** @}
|
---|
| 224 | */
|
---|