| [007e6efa] | 1 | /*
|
|---|
| [ca48672] | 2 | * Copyright (c) 2025 Jiri Svoboda
|
|---|
| [007e6efa] | 3 | * Copyright (c) 2011 Martin Decky
|
|---|
| 4 | * All rights reserved.
|
|---|
| 5 | *
|
|---|
| 6 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 7 | * modification, are permitted provided that the following conditions
|
|---|
| 8 | * are met:
|
|---|
| 9 | *
|
|---|
| 10 | * - Redistributions of source code must retain the above copyright
|
|---|
| 11 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 12 | * - Redistributions in binary form must reproduce the above copyright
|
|---|
| 13 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 14 | * documentation and/or other materials provided with the distribution.
|
|---|
| 15 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 16 | * derived from this software without specific prior written permission.
|
|---|
| 17 | *
|
|---|
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 28 | */
|
|---|
| 29 |
|
|---|
| 30 | /** @addtogroup libc
|
|---|
| 31 | * @{
|
|---|
| 32 | */
|
|---|
| 33 | /** @file
|
|---|
| 34 | */
|
|---|
| 35 |
|
|---|
| [79ae36dd] | 36 | #include <ns.h>
|
|---|
| [007e6efa] | 37 | #include <ipc/ns.h>
|
|---|
| [79ae36dd] | 38 | #include <async.h>
|
|---|
| 39 | #include <macros.h>
|
|---|
| [b1bd89ea] | 40 | #include <errno.h>
|
|---|
| [79ae36dd] | 41 | #include "private/ns.h"
|
|---|
| [007e6efa] | 42 |
|
|---|
| [7b616e2] | 43 | /*
|
|---|
| 44 | * XXX ns does not know about session_ns, so we create an extra session for
|
|---|
| 45 | * actual communicaton
|
|---|
| 46 | */
|
|---|
| 47 | static async_sess_t *sess_ns = NULL;
|
|---|
| 48 |
|
|---|
| [9b1baac] | 49 | errno_t service_register(service_t service, iface_t iface,
|
|---|
| 50 | async_port_handler_t handler, void *data)
|
|---|
| [007e6efa] | 51 | {
|
|---|
| [01900b6] | 52 | errno_t rc;
|
|---|
| 53 | async_sess_t *sess = ns_session_get(&rc);
|
|---|
| [9b1baac] | 54 | if (sess == NULL)
|
|---|
| [01900b6] | 55 | return rc;
|
|---|
| [9b1baac] | 56 |
|
|---|
| 57 | port_id_t port;
|
|---|
| [01900b6] | 58 | rc = async_create_port(iface, handler, data, &port);
|
|---|
| [9b1baac] | 59 | if (rc != EOK)
|
|---|
| 60 | return rc;
|
|---|
| 61 |
|
|---|
| 62 | async_exch_t *exch = async_exchange_begin(sess);
|
|---|
| 63 |
|
|---|
| [7b616e2] | 64 | ipc_call_t answer;
|
|---|
| [9b1baac] | 65 | aid_t req = async_send_2(exch, NS_REGISTER, service, iface, &answer);
|
|---|
| 66 | rc = async_connect_to_me(exch, iface, service, 0);
|
|---|
| 67 |
|
|---|
| 68 | async_exchange_end(exch);
|
|---|
| 69 |
|
|---|
| 70 | if (rc != EOK) {
|
|---|
| [ca48672] | 71 | async_port_destroy(port);
|
|---|
| [9b1baac] | 72 | async_forget(req);
|
|---|
| 73 | return rc;
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| 76 | errno_t retval;
|
|---|
| 77 | async_wait_for(req, &retval);
|
|---|
| [ca48672] | 78 |
|
|---|
| 79 | if (rc != EOK)
|
|---|
| 80 | async_port_destroy(port);
|
|---|
| [9b1baac] | 81 | return rc;
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 | errno_t service_register_broker(service_t service, async_port_handler_t handler,
|
|---|
| 85 | void *data)
|
|---|
| 86 | {
|
|---|
| 87 | async_set_fallback_port_handler(handler, data);
|
|---|
| [a35b458] | 88 |
|
|---|
| [01900b6] | 89 | errno_t rc;
|
|---|
| 90 | async_sess_t *sess = ns_session_get(&rc);
|
|---|
| [7b616e2] | 91 | if (sess == NULL)
|
|---|
| [01900b6] | 92 | return rc;
|
|---|
| [a35b458] | 93 |
|
|---|
| [7b616e2] | 94 | async_exch_t *exch = async_exchange_begin(sess);
|
|---|
| [9b1baac] | 95 |
|
|---|
| 96 | ipc_call_t answer;
|
|---|
| 97 | aid_t req = async_send_1(exch, NS_REGISTER_BROKER, service, &answer);
|
|---|
| [01900b6] | 98 | rc = async_connect_to_me(exch, INTERFACE_ANY, service, 0);
|
|---|
| [a35b458] | 99 |
|
|---|
| [79ae36dd] | 100 | async_exchange_end(exch);
|
|---|
| [a35b458] | 101 |
|
|---|
| [7b616e2] | 102 | if (rc != EOK) {
|
|---|
| 103 | async_forget(req);
|
|---|
| 104 | return rc;
|
|---|
| 105 | }
|
|---|
| [a35b458] | 106 |
|
|---|
| [9b1baac] | 107 | errno_t retval;
|
|---|
| [7b616e2] | 108 | async_wait_for(req, &retval);
|
|---|
| [79ae36dd] | 109 | return rc;
|
|---|
| 110 | }
|
|---|
| 111 |
|
|---|
| [01900b6] | 112 | /** Connect to a singleton service.
|
|---|
| 113 | *
|
|---|
| 114 | * @param service Singleton service ID.
|
|---|
| 115 | * @param iface Interface to connect to.
|
|---|
| 116 | * @param arg3 Custom connection argument.
|
|---|
| 117 | * @param rc Placeholder for return code. Unused if NULL.
|
|---|
| 118 | *
|
|---|
| 119 | * @return New session on success or NULL on error.
|
|---|
| 120 | *
|
|---|
| 121 | */
|
|---|
| 122 | async_sess_t *service_connect(service_t service, iface_t iface, sysarg_t arg3,
|
|---|
| 123 | errno_t *rc)
|
|---|
| [0dd16778] | 124 | {
|
|---|
| [01900b6] | 125 | async_sess_t *sess = ns_session_get(rc);
|
|---|
| [7b616e2] | 126 | if (sess == NULL)
|
|---|
| 127 | return NULL;
|
|---|
| [a35b458] | 128 |
|
|---|
| [7b616e2] | 129 | async_exch_t *exch = async_exchange_begin(sess);
|
|---|
| 130 | if (exch == NULL)
|
|---|
| [0dd16778] | 131 | return NULL;
|
|---|
| [a35b458] | 132 |
|
|---|
| [7b616e2] | 133 | async_sess_t *csess =
|
|---|
| [01900b6] | 134 | async_connect_me_to(exch, iface, service, arg3, rc);
|
|---|
| [0dd16778] | 135 | async_exchange_end(exch);
|
|---|
| [a35b458] | 136 |
|
|---|
| [7b616e2] | 137 | if (csess == NULL)
|
|---|
| [0dd16778] | 138 | return NULL;
|
|---|
| [a35b458] | 139 |
|
|---|
| [0dd16778] | 140 | /*
|
|---|
| 141 | * FIXME Ugly hack to work around limitation of implementing
|
|---|
| 142 | * parallel exchanges using multiple connections. Shift out
|
|---|
| 143 | * first argument for non-initial connections.
|
|---|
| 144 | */
|
|---|
| [7b616e2] | 145 | async_sess_args_set(csess, iface, arg3, 0);
|
|---|
| [a35b458] | 146 |
|
|---|
| [7b616e2] | 147 | return csess;
|
|---|
| [0dd16778] | 148 | }
|
|---|
| 149 |
|
|---|
| [01900b6] | 150 | /** Wait and connect to a singleton service.
|
|---|
| 151 | *
|
|---|
| 152 | * @param service Singleton service ID.
|
|---|
| 153 | * @param iface Interface to connect to.
|
|---|
| 154 | * @param arg3 Custom connection argument.
|
|---|
| 155 | * @param rc Placeholder for return code. Unused if NULL.
|
|---|
| 156 | *
|
|---|
| 157 | * @return New session on success or NULL on error.
|
|---|
| 158 | *
|
|---|
| 159 | */
|
|---|
| [f9b2cb4c] | 160 | async_sess_t *service_connect_blocking(service_t service, iface_t iface,
|
|---|
| [01900b6] | 161 | sysarg_t arg3, errno_t *rc)
|
|---|
| [566992e1] | 162 | {
|
|---|
| [01900b6] | 163 | async_sess_t *sess = ns_session_get(rc);
|
|---|
| [7b616e2] | 164 | if (sess == NULL)
|
|---|
| 165 | return NULL;
|
|---|
| [a35b458] | 166 |
|
|---|
| [7b616e2] | 167 | async_exch_t *exch = async_exchange_begin(sess);
|
|---|
| 168 | async_sess_t *csess =
|
|---|
| [01900b6] | 169 | async_connect_me_to_blocking(exch, iface, service, arg3, rc);
|
|---|
| [566992e1] | 170 | async_exchange_end(exch);
|
|---|
| [a35b458] | 171 |
|
|---|
| [7b616e2] | 172 | if (csess == NULL)
|
|---|
| [566992e1] | 173 | return NULL;
|
|---|
| [a35b458] | 174 |
|
|---|
| [566992e1] | 175 | /*
|
|---|
| 176 | * FIXME Ugly hack to work around limitation of implementing
|
|---|
| 177 | * parallel exchanges using multiple connections. Shift out
|
|---|
| 178 | * first argument for non-initial connections.
|
|---|
| 179 | */
|
|---|
| [7b616e2] | 180 | async_sess_args_set(csess, iface, arg3, 0);
|
|---|
| [a35b458] | 181 |
|
|---|
| [7b616e2] | 182 | return csess;
|
|---|
| [566992e1] | 183 | }
|
|---|
| 184 |
|
|---|
| [b7fd2a0] | 185 | errno_t ns_ping(void)
|
|---|
| [007e6efa] | 186 | {
|
|---|
| [01900b6] | 187 | errno_t rc;
|
|---|
| 188 | async_sess_t *sess = ns_session_get(&rc);
|
|---|
| [7b616e2] | 189 | if (sess == NULL)
|
|---|
| [01900b6] | 190 | return rc;
|
|---|
| [a35b458] | 191 |
|
|---|
| [7b616e2] | 192 | async_exch_t *exch = async_exchange_begin(sess);
|
|---|
| [01900b6] | 193 | rc = async_req_0_0(exch, NS_PING);
|
|---|
| [79ae36dd] | 194 | async_exchange_end(exch);
|
|---|
| [a35b458] | 195 |
|
|---|
| [79ae36dd] | 196 | return rc;
|
|---|
| [007e6efa] | 197 | }
|
|---|
| 198 |
|
|---|
| [b7fd2a0] | 199 | errno_t ns_intro(task_id_t id)
|
|---|
| [007e6efa] | 200 | {
|
|---|
| [01900b6] | 201 | errno_t rc;
|
|---|
| 202 | async_sess_t *sess = ns_session_get(&rc);
|
|---|
| [7b616e2] | 203 | if (sess == NULL)
|
|---|
| 204 | return EIO;
|
|---|
| [a35b458] | 205 |
|
|---|
| [01900b6] | 206 | async_exch_t *exch = async_exchange_begin(sess);
|
|---|
| 207 | rc = async_req_2_0(exch, NS_ID_INTRO, LOWER32(id), UPPER32(id));
|
|---|
| [79ae36dd] | 208 | async_exchange_end(exch);
|
|---|
| [a35b458] | 209 |
|
|---|
| [79ae36dd] | 210 | return rc;
|
|---|
| [007e6efa] | 211 | }
|
|---|
| 212 |
|
|---|
| [01900b6] | 213 | async_sess_t *ns_session_get(errno_t *rc)
|
|---|
| [7b616e2] | 214 | {
|
|---|
| 215 | async_exch_t *exch;
|
|---|
| [a35b458] | 216 |
|
|---|
| [7b616e2] | 217 | if (sess_ns == NULL) {
|
|---|
| [9f272d9] | 218 | exch = async_exchange_begin(&session_ns);
|
|---|
| [01900b6] | 219 | sess_ns = async_connect_me_to(exch, 0, 0, 0, rc);
|
|---|
| [7b616e2] | 220 | async_exchange_end(exch);
|
|---|
| 221 | if (sess_ns == NULL)
|
|---|
| 222 | return NULL;
|
|---|
| 223 | }
|
|---|
| [a35b458] | 224 |
|
|---|
| [7b616e2] | 225 | return sess_ns;
|
|---|
| 226 | }
|
|---|
| 227 |
|
|---|
| [007e6efa] | 228 | /** @}
|
|---|
| 229 | */
|
|---|