source: mainline/uspace/lib/c/generic/loc.c@ 06599a1

Last change on this file since 06599a1 was 22d990c, checked in by Matthieu Riolo <matthieu.riolo@…>, 5 years ago

Clean up code

Correcting order of includes. Remove unnecessary new lines. Removing
old function declaration. Typos.

  • Property mode set to 100644
File size: 19.8 KB
RevLine 
[15f3c3f]1/*
2 * Copyright (c) 2007 Josef Cejka
[cc574511]3 * Copyright (c) 2011 Jiri Svoboda
[15f3c3f]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#include <str.h>
31#include <ipc/services.h>
32#include <ns.h>
33#include <ipc/loc.h>
34#include <loc.h>
35#include <fibril_synch.h>
36#include <async.h>
37#include <errno.h>
[38d150e]38#include <stdlib.h>
[3e6a98c5]39#include <stdbool.h>
[15f3c3f]40
41static FIBRIL_MUTEX_INITIALIZE(loc_supp_block_mutex);
42static FIBRIL_MUTEX_INITIALIZE(loc_cons_block_mutex);
43
44static FIBRIL_MUTEX_INITIALIZE(loc_supplier_mutex);
45static FIBRIL_MUTEX_INITIALIZE(loc_consumer_mutex);
46
[12f9f0d0]47static FIBRIL_MUTEX_INITIALIZE(loc_callback_mutex);
48static bool loc_callback_created = false;
[a6240a31]49static loc_cat_change_cb_t cat_change_cb = NULL;
[e89a06a]50static void *cat_change_arg = NULL;
[12f9f0d0]51
[15f3c3f]52static async_sess_t *loc_supp_block_sess = NULL;
53static async_sess_t *loc_cons_block_sess = NULL;
54
55static async_sess_t *loc_supplier_sess = NULL;
56static async_sess_t *loc_consumer_sess = NULL;
57
[984a9ba]58static void loc_cb_conn(ipc_call_t *icall, void *arg)
[12f9f0d0]59{
60 while (true) {
61 ipc_call_t call;
[984a9ba]62 async_get_call(&call);
[a35b458]63
[fafb8e5]64 if (!ipc_get_imethod(&call)) {
[889cdb1]65 async_answer_0(&call, EOK);
[12f9f0d0]66 return;
67 }
[a35b458]68
[fafb8e5]69 switch (ipc_get_imethod(&call)) {
[12f9f0d0]70 case LOC_EVENT_CAT_CHANGE:
71 fibril_mutex_lock(&loc_callback_mutex);
[a6240a31]72 loc_cat_change_cb_t cb_fun = cat_change_cb;
[e89a06a]73 void *cb_arg = cat_change_arg;
[12f9f0d0]74 fibril_mutex_unlock(&loc_callback_mutex);
[a35b458]75
[984a9ba]76 async_answer_0(&call, EOK);
[a35b458]77
[a6240a31]78 if (cb_fun != NULL)
[e89a06a]79 (*cb_fun)(cb_arg);
[a35b458]80
[12f9f0d0]81 break;
82 default:
[984a9ba]83 async_answer_0(&call, ENOTSUP);
[12f9f0d0]84 }
85 }
86}
87
[15f3c3f]88static void clone_session(fibril_mutex_t *mtx, async_sess_t *src,
89 async_sess_t **dst)
90{
91 fibril_mutex_lock(mtx);
[a35b458]92
[15f3c3f]93 if ((*dst == NULL) && (src != NULL))
94 *dst = src;
[a35b458]95
[15f3c3f]96 fibril_mutex_unlock(mtx);
97}
98
[a6240a31]99/** Create callback
100 *
101 * Must be called with loc_callback_mutex locked.
102 *
103 * @return EOK on success.
104 *
105 */
[b7fd2a0]106static errno_t loc_callback_create(void)
[12f9f0d0]107{
108 if (!loc_callback_created) {
[a6240a31]109 async_exch_t *exch =
[f9b2cb4c]110 loc_exchange_begin_blocking(INTERFACE_LOC_CONSUMER);
[a35b458]111
[12f9f0d0]112 ipc_call_t answer;
113 aid_t req = async_send_0(exch, LOC_CALLBACK_CREATE, &answer);
[a35b458]114
[f9b2cb4c]115 port_id_t port;
[b7fd2a0]116 errno_t rc = async_create_callback_port(exch, INTERFACE_LOC_CB, 0, 0,
[f9b2cb4c]117 loc_cb_conn, NULL, &port);
[a35b458]118
[12f9f0d0]119 loc_exchange_end(exch);
[a35b458]120
[12f9f0d0]121 if (rc != EOK)
[a6240a31]122 return rc;
[a35b458]123
[b7fd2a0]124 errno_t retval;
[cd66f3c]125 async_wait_for(req, &retval);
126 if (retval != EOK)
127 return retval;
[a35b458]128
[12f9f0d0]129 loc_callback_created = true;
130 }
[a35b458]131
[a6240a31]132 return EOK;
[12f9f0d0]133}
134
[15f3c3f]135/** Start an async exchange on the loc session (blocking).
136 *
137 * @param iface Location service interface to choose
138 *
139 * @return New exchange.
140 *
141 */
[f9b2cb4c]142async_exch_t *loc_exchange_begin_blocking(iface_t iface)
[15f3c3f]143{
144 switch (iface) {
[f9b2cb4c]145 case INTERFACE_LOC_SUPPLIER:
[15f3c3f]146 fibril_mutex_lock(&loc_supp_block_mutex);
[a35b458]147
[15f3c3f]148 while (loc_supp_block_sess == NULL) {
149 clone_session(&loc_supplier_mutex, loc_supplier_sess,
150 &loc_supp_block_sess);
[a35b458]151
[15f3c3f]152 if (loc_supp_block_sess == NULL)
153 loc_supp_block_sess =
[f9b2cb4c]154 service_connect_blocking(SERVICE_LOC,
155 INTERFACE_LOC_SUPPLIER, 0);
[15f3c3f]156 }
[a35b458]157
[15f3c3f]158 fibril_mutex_unlock(&loc_supp_block_mutex);
[a35b458]159
[15f3c3f]160 clone_session(&loc_supplier_mutex, loc_supp_block_sess,
161 &loc_supplier_sess);
[a35b458]162
[15f3c3f]163 return async_exchange_begin(loc_supp_block_sess);
[f9b2cb4c]164 case INTERFACE_LOC_CONSUMER:
[15f3c3f]165 fibril_mutex_lock(&loc_cons_block_mutex);
[a35b458]166
[15f3c3f]167 while (loc_cons_block_sess == NULL) {
168 clone_session(&loc_consumer_mutex, loc_consumer_sess,
169 &loc_cons_block_sess);
[a35b458]170
[15f3c3f]171 if (loc_cons_block_sess == NULL)
172 loc_cons_block_sess =
[f9b2cb4c]173 service_connect_blocking(SERVICE_LOC,
174 INTERFACE_LOC_CONSUMER, 0);
[15f3c3f]175 }
[a35b458]176
[15f3c3f]177 fibril_mutex_unlock(&loc_cons_block_mutex);
[a35b458]178
[15f3c3f]179 clone_session(&loc_consumer_mutex, loc_cons_block_sess,
180 &loc_consumer_sess);
[a35b458]181
[15f3c3f]182 return async_exchange_begin(loc_cons_block_sess);
183 default:
184 return NULL;
185 }
186}
187
188/** Start an async exchange on the loc session.
189 *
190 * @param iface Location service interface to choose
191 *
192 * @return New exchange.
193 *
194 */
[f9b2cb4c]195async_exch_t *loc_exchange_begin(iface_t iface)
[15f3c3f]196{
197 switch (iface) {
[f9b2cb4c]198 case INTERFACE_LOC_SUPPLIER:
[15f3c3f]199 fibril_mutex_lock(&loc_supplier_mutex);
[a35b458]200
[15f3c3f]201 if (loc_supplier_sess == NULL)
202 loc_supplier_sess =
[f9b2cb4c]203 service_connect(SERVICE_LOC,
204 INTERFACE_LOC_SUPPLIER, 0);
[a35b458]205
[15f3c3f]206 fibril_mutex_unlock(&loc_supplier_mutex);
[a35b458]207
[15f3c3f]208 if (loc_supplier_sess == NULL)
209 return NULL;
[a35b458]210
[15f3c3f]211 return async_exchange_begin(loc_supplier_sess);
[f9b2cb4c]212 case INTERFACE_LOC_CONSUMER:
[15f3c3f]213 fibril_mutex_lock(&loc_consumer_mutex);
[a35b458]214
[15f3c3f]215 if (loc_consumer_sess == NULL)
216 loc_consumer_sess =
[f9b2cb4c]217 service_connect(SERVICE_LOC,
218 INTERFACE_LOC_CONSUMER, 0);
[a35b458]219
[15f3c3f]220 fibril_mutex_unlock(&loc_consumer_mutex);
[a35b458]221
[15f3c3f]222 if (loc_consumer_sess == NULL)
223 return NULL;
[a35b458]224
[15f3c3f]225 return async_exchange_begin(loc_consumer_sess);
226 default:
227 return NULL;
228 }
229}
230
231/** Finish an async exchange on the loc session.
232 *
233 * @param exch Exchange to be finished.
234 *
235 */
236void loc_exchange_end(async_exch_t *exch)
237{
238 async_exchange_end(exch);
239}
240
[c477c80]241/** Register new server with loc. */
[b7fd2a0]242errno_t loc_server_register(const char *name)
[15f3c3f]243{
[f9b2cb4c]244 async_exch_t *exch = loc_exchange_begin_blocking(INTERFACE_LOC_SUPPLIER);
[a35b458]245
[15f3c3f]246 ipc_call_t answer;
247 aid_t req = async_send_2(exch, LOC_SERVER_REGISTER, 0, 0, &answer);
[b7fd2a0]248 errno_t retval = async_data_write_start(exch, name, str_size(name));
[a35b458]249
[15f3c3f]250 if (retval != EOK) {
[50b581d]251 async_forget(req);
[0239846]252 loc_exchange_end(exch);
[15f3c3f]253 return retval;
254 }
[a35b458]255
[9b1baac]256 async_connect_to_me(exch, INTERFACE_ANY, 0, 0);
[0239846]257
258 /*
259 * First wait for the answer and then end the exchange. The opposite
260 * order is generally wrong because it may lead to a deadlock under
261 * certain circumstances.
262 */
263 async_wait_for(req, &retval);
[15f3c3f]264 loc_exchange_end(exch);
[a35b458]265
[15f3c3f]266 return retval;
267}
268
[d0dd7b5]269/** Register new service.
[15f3c3f]270 *
[f9b2cb4c]271 * @param fqsn Fully qualified service name
272 * @param[out] sid Service ID of new service
[15f3c3f]273 *
274 */
[b7fd2a0]275errno_t loc_service_register(const char *fqsn, service_id_t *sid)
[15f3c3f]276{
[f9b2cb4c]277 async_exch_t *exch = loc_exchange_begin_blocking(INTERFACE_LOC_SUPPLIER);
[a35b458]278
[15f3c3f]279 ipc_call_t answer;
[f9b2cb4c]280 aid_t req = async_send_0(exch, LOC_SERVICE_REGISTER, &answer);
[b7fd2a0]281 errno_t retval = async_data_write_start(exch, fqsn, str_size(fqsn));
[a35b458]282
[15f3c3f]283 if (retval != EOK) {
[50b581d]284 async_forget(req);
[0239846]285 loc_exchange_end(exch);
[15f3c3f]286 return retval;
287 }
[a35b458]288
[0239846]289 /*
290 * First wait for the answer and then end the exchange. The opposite
291 * order is generally wrong because it may lead to a deadlock under
292 * certain circumstances.
293 */
[15f3c3f]294 async_wait_for(req, &retval);
[0239846]295 loc_exchange_end(exch);
[a35b458]296
[15f3c3f]297 if (retval != EOK) {
[d0dd7b5]298 if (sid != NULL)
299 *sid = -1;
[a35b458]300
[15f3c3f]301 return retval;
302 }
[a35b458]303
[d0dd7b5]304 if (sid != NULL)
[fafb8e5]305 *sid = (service_id_t) ipc_get_arg1(&answer);
[a35b458]306
[15f3c3f]307 return retval;
308}
309
[d0dd7b5]310/** Unregister service.
311 *
312 * @param sid Service ID
313 */
[b7fd2a0]314errno_t loc_service_unregister(service_id_t sid)
[d0dd7b5]315{
[7fa8589]316 async_exch_t *exch = loc_exchange_begin_blocking(INTERFACE_LOC_SUPPLIER);
317 errno_t rc = async_req_1_0(exch, LOC_SERVICE_UNREGISTER, sid);
[d0dd7b5]318 loc_exchange_end(exch);
[7fa8589]319 return rc;
[15f3c3f]320}
321
[b7fd2a0]322errno_t loc_service_get_id(const char *fqdn, service_id_t *handle,
[15f3c3f]323 unsigned int flags)
324{
325 async_exch_t *exch;
[a35b458]326
[22d990c]327 if (flags & IPC_FLAG_BLOCKING)
[f9b2cb4c]328 exch = loc_exchange_begin_blocking(INTERFACE_LOC_CONSUMER);
[15f3c3f]329 else {
[f9b2cb4c]330 exch = loc_exchange_begin(INTERFACE_LOC_CONSUMER);
[15f3c3f]331 if (exch == NULL)
332 return errno;
333 }
[a35b458]334
[15f3c3f]335 ipc_call_t answer;
336 aid_t req = async_send_2(exch, LOC_SERVICE_GET_ID, flags, 0,
337 &answer);
[b7fd2a0]338 errno_t retval = async_data_write_start(exch, fqdn, str_size(fqdn));
[a35b458]339
[15f3c3f]340 loc_exchange_end(exch);
[a35b458]341
[15f3c3f]342 if (retval != EOK) {
[50b581d]343 async_forget(req);
[15f3c3f]344 return retval;
345 }
[a35b458]346
[15f3c3f]347 async_wait_for(req, &retval);
[a35b458]348
[15f3c3f]349 if (retval != EOK) {
350 if (handle != NULL)
351 *handle = (service_id_t) -1;
[a35b458]352
[15f3c3f]353 return retval;
354 }
[a35b458]355
[15f3c3f]356 if (handle != NULL)
[fafb8e5]357 *handle = (service_id_t) ipc_get_arg1(&answer);
[a35b458]358
[15f3c3f]359 return retval;
360}
361
[763e0cd]362/** Get object name.
[cce8a83]363 *
[763e0cd]364 * Provided ID of an object, return its name.
[cce8a83]365 *
[763e0cd]366 * @param method IPC method
367 * @param id Object ID
[cce8a83]368 * @param name Place to store pointer to new string. Caller should
369 * free it using free().
[cde999a]370 * @return EOK on success or an error code
[cce8a83]371 */
[b7fd2a0]372static errno_t loc_get_name_internal(sysarg_t method, sysarg_t id, char **name)
[cce8a83]373{
374 async_exch_t *exch;
375 char name_buf[LOC_NAME_MAXLEN + 1];
[45058baa]376 ipc_call_t dreply;
377 size_t act_size;
[b7fd2a0]378 errno_t dretval;
[a35b458]379
[cce8a83]380 *name = NULL;
[f9b2cb4c]381 exch = loc_exchange_begin_blocking(INTERFACE_LOC_CONSUMER);
[a35b458]382
[cce8a83]383 ipc_call_t answer;
[763e0cd]384 aid_t req = async_send_1(exch, method, id, &answer);
[45058baa]385 aid_t dreq = async_data_read(exch, name_buf, LOC_NAME_MAXLEN,
386 &dreply);
387 async_wait_for(dreq, &dretval);
[a35b458]388
[cce8a83]389 loc_exchange_end(exch);
[a35b458]390
[45058baa]391 if (dretval != EOK) {
[50b581d]392 async_forget(req);
[45058baa]393 return dretval;
[cce8a83]394 }
[a35b458]395
[b7fd2a0]396 errno_t retval;
[cce8a83]397 async_wait_for(req, &retval);
[a35b458]398
[cce8a83]399 if (retval != EOK)
400 return retval;
[a35b458]401
[fafb8e5]402 act_size = ipc_get_arg2(&dreply);
[45058baa]403 assert(act_size <= LOC_NAME_MAXLEN);
404 name_buf[act_size] = '\0';
405
[cce8a83]406 *name = str_dup(name_buf);
407 if (*name == NULL)
408 return ENOMEM;
[a35b458]409
[cce8a83]410 return EOK;
411}
412
[763e0cd]413/** Get category name.
414 *
415 * Provided ID of a service, return its name.
416 *
417 * @param cat_id Category ID
418 * @param name Place to store pointer to new string. Caller should
419 * free it using free().
[cde999a]420 * @return EOK on success or an error code
[763e0cd]421 */
[b7fd2a0]422errno_t loc_category_get_name(category_id_t cat_id, char **name)
[763e0cd]423{
424 return loc_get_name_internal(LOC_CATEGORY_GET_NAME, cat_id, name);
425}
426
427/** Get service name.
428 *
429 * Provided ID of a service, return its name.
430 *
431 * @param svc_id Service ID
432 * @param name Place to store pointer to new string. Caller should
433 * free it using free().
[cde999a]434 * @return EOK on success or an error code
[763e0cd]435 */
[b7fd2a0]436errno_t loc_service_get_name(service_id_t svc_id, char **name)
[763e0cd]437{
438 return loc_get_name_internal(LOC_SERVICE_GET_NAME, svc_id, name);
439}
[cce8a83]440
[a3fcfba]441/** Get service server name.
442 *
443 * Provided ID of a service, return the name of its server.
444 *
445 * @param svc_id Service ID
446 * @param name Place to store pointer to new string. Caller should
447 * free it using free().
[cde999a]448 * @return EOK on success or an error code
[a3fcfba]449 */
[b7fd2a0]450errno_t loc_service_get_server_name(service_id_t svc_id, char **name)
[a3fcfba]451{
452 return loc_get_name_internal(LOC_SERVICE_GET_SERVER_NAME, svc_id, name);
453}
454
[b7fd2a0]455errno_t loc_namespace_get_id(const char *name, service_id_t *handle,
[15f3c3f]456 unsigned int flags)
457{
458 async_exch_t *exch;
[a35b458]459
[15f3c3f]460 if (flags & IPC_FLAG_BLOCKING)
[f9b2cb4c]461 exch = loc_exchange_begin_blocking(INTERFACE_LOC_CONSUMER);
[15f3c3f]462 else {
[f9b2cb4c]463 exch = loc_exchange_begin(INTERFACE_LOC_CONSUMER);
[15f3c3f]464 if (exch == NULL)
465 return errno;
466 }
[a35b458]467
[15f3c3f]468 ipc_call_t answer;
469 aid_t req = async_send_2(exch, LOC_NAMESPACE_GET_ID, flags, 0,
470 &answer);
[b7fd2a0]471 errno_t retval = async_data_write_start(exch, name, str_size(name));
[a35b458]472
[15f3c3f]473 loc_exchange_end(exch);
[a35b458]474
[15f3c3f]475 if (retval != EOK) {
[50b581d]476 async_forget(req);
[15f3c3f]477 return retval;
478 }
[a35b458]479
[15f3c3f]480 async_wait_for(req, &retval);
[a35b458]481
[15f3c3f]482 if (retval != EOK) {
483 if (handle != NULL)
484 *handle = (service_id_t) -1;
[a35b458]485
[15f3c3f]486 return retval;
487 }
[a35b458]488
[15f3c3f]489 if (handle != NULL)
[fafb8e5]490 *handle = (service_id_t) ipc_get_arg1(&answer);
[a35b458]491
[15f3c3f]492 return retval;
493}
494
[cc574511]495/** Get category ID.
496 *
497 * Provided name of a category, return its ID.
498 *
499 * @param name Category name
500 * @param cat_id Place to store ID
501 * @param flags IPC_FLAG_BLOCKING to wait for location service to start
[cde999a]502 * @return EOK on success or an error code
[cc574511]503 */
[b7fd2a0]504errno_t loc_category_get_id(const char *name, category_id_t *cat_id,
[cc574511]505 unsigned int flags)
506{
507 async_exch_t *exch;
[a35b458]508
[cc574511]509 if (flags & IPC_FLAG_BLOCKING)
[f9b2cb4c]510 exch = loc_exchange_begin_blocking(INTERFACE_LOC_CONSUMER);
[cc574511]511 else {
[f9b2cb4c]512 exch = loc_exchange_begin(INTERFACE_LOC_CONSUMER);
[cc574511]513 if (exch == NULL)
514 return errno;
515 }
[a35b458]516
[cc574511]517 ipc_call_t answer;
518 aid_t req = async_send_0(exch, LOC_CATEGORY_GET_ID,
519 &answer);
[b7fd2a0]520 errno_t retval = async_data_write_start(exch, name, str_size(name));
[a35b458]521
[cc574511]522 loc_exchange_end(exch);
[a35b458]523
[cc574511]524 if (retval != EOK) {
[50b581d]525 async_forget(req);
[cc574511]526 return retval;
527 }
[a35b458]528
[cc574511]529 async_wait_for(req, &retval);
[a35b458]530
[cc574511]531 if (retval != EOK) {
532 if (cat_id != NULL)
533 *cat_id = (category_id_t) -1;
[a35b458]534
[cc574511]535 return retval;
536 }
[a35b458]537
[cc574511]538 if (cat_id != NULL)
[fafb8e5]539 *cat_id = (category_id_t) ipc_get_arg1(&answer);
[a35b458]540
[cc574511]541 return retval;
542}
543
[15f3c3f]544loc_object_type_t loc_id_probe(service_id_t handle)
545{
[f9b2cb4c]546 async_exch_t *exch = loc_exchange_begin_blocking(INTERFACE_LOC_CONSUMER);
[a35b458]547
[15f3c3f]548 sysarg_t type;
[b7fd2a0]549 errno_t retval = async_req_1_1(exch, LOC_ID_PROBE, handle, &type);
[a35b458]550
[15f3c3f]551 loc_exchange_end(exch);
[a35b458]552
[15f3c3f]553 if (retval != EOK)
554 return LOC_OBJECT_NONE;
[a35b458]555
[15f3c3f]556 return (loc_object_type_t) type;
557}
558
[f9b2cb4c]559async_sess_t *loc_service_connect(service_id_t handle, iface_t iface,
[15f3c3f]560 unsigned int flags)
561{
562 async_sess_t *sess;
[a35b458]563
[15f3c3f]564 if (flags & IPC_FLAG_BLOCKING)
[f9b2cb4c]565 sess = service_connect_blocking(SERVICE_LOC, iface, handle);
[15f3c3f]566 else
[f9b2cb4c]567 sess = service_connect(SERVICE_LOC, iface, handle);
[a35b458]568
[15f3c3f]569 return sess;
570}
571
[d5c1051]572/**
573 * @return ID of a new NULL device, or -1 if failed.
574 */
[15f3c3f]575int loc_null_create(void)
576{
[f9b2cb4c]577 async_exch_t *exch = loc_exchange_begin_blocking(INTERFACE_LOC_CONSUMER);
[a35b458]578
[15f3c3f]579 sysarg_t null_id;
[b7fd2a0]580 errno_t retval = async_req_0_1(exch, LOC_NULL_CREATE, &null_id);
[a35b458]581
[15f3c3f]582 loc_exchange_end(exch);
[a35b458]583
[15f3c3f]584 if (retval != EOK)
585 return -1;
[a35b458]586
[15f3c3f]587 return (int) null_id;
588}
589
590void loc_null_destroy(int null_id)
591{
[f9b2cb4c]592 async_exch_t *exch = loc_exchange_begin_blocking(INTERFACE_LOC_CONSUMER);
[15f3c3f]593 async_req_1_0(exch, LOC_NULL_DESTROY, (sysarg_t) null_id);
594 loc_exchange_end(exch);
595}
596
597static size_t loc_count_namespaces_internal(async_exch_t *exch)
598{
599 sysarg_t count;
[b7fd2a0]600 errno_t retval = async_req_0_1(exch, LOC_GET_NAMESPACE_COUNT, &count);
[15f3c3f]601 if (retval != EOK)
602 return 0;
[a35b458]603
[15f3c3f]604 return count;
605}
606
[cc574511]607/** Add service to category.
608 *
609 * @param svc_id Service ID
610 * @param cat_id Category ID
[cde999a]611 * @return EOK on success or an error code
[cc574511]612 */
[b7fd2a0]613errno_t loc_service_add_to_cat(service_id_t svc_id, service_id_t cat_id)
[cc574511]614{
615 async_exch_t *exch;
[b7fd2a0]616 errno_t retval;
[a35b458]617
[f9b2cb4c]618 exch = loc_exchange_begin_blocking(INTERFACE_LOC_SUPPLIER);
[cc574511]619 retval = async_req_2_0(exch, LOC_SERVICE_ADD_TO_CAT, svc_id, cat_id);
620 loc_exchange_end(exch);
[a35b458]621
[cc574511]622 return retval;
623}
624
[15f3c3f]625static size_t loc_count_services_internal(async_exch_t *exch,
626 service_id_t ns_handle)
627{
628 sysarg_t count;
[b7fd2a0]629 errno_t retval = async_req_1_1(exch, LOC_GET_SERVICE_COUNT, ns_handle,
[15f3c3f]630 &count);
631 if (retval != EOK)
632 return 0;
[a35b458]633
[15f3c3f]634 return count;
635}
636
637size_t loc_count_namespaces(void)
638{
[f9b2cb4c]639 async_exch_t *exch = loc_exchange_begin_blocking(INTERFACE_LOC_CONSUMER);
[15f3c3f]640 size_t size = loc_count_namespaces_internal(exch);
641 loc_exchange_end(exch);
[a35b458]642
[15f3c3f]643 return size;
644}
645
646size_t loc_count_services(service_id_t ns_handle)
647{
[f9b2cb4c]648 async_exch_t *exch = loc_exchange_begin_blocking(INTERFACE_LOC_CONSUMER);
[15f3c3f]649 size_t size = loc_count_services_internal(exch, ns_handle);
650 loc_exchange_end(exch);
[a35b458]651
[15f3c3f]652 return size;
653}
654
655size_t loc_get_namespaces(loc_sdesc_t **data)
656{
[cc574511]657 /* Loop until read is succesful */
[15f3c3f]658 while (true) {
[f9b2cb4c]659 async_exch_t *exch = loc_exchange_begin_blocking(INTERFACE_LOC_CONSUMER);
[15f3c3f]660 size_t count = loc_count_namespaces_internal(exch);
661 loc_exchange_end(exch);
[a35b458]662
[15f3c3f]663 if (count == 0)
664 return 0;
[a35b458]665
[15f3c3f]666 loc_sdesc_t *devs = (loc_sdesc_t *) calloc(count, sizeof(loc_sdesc_t));
667 if (devs == NULL)
668 return 0;
[a35b458]669
[f9b2cb4c]670 exch = loc_exchange_begin(INTERFACE_LOC_CONSUMER);
[a35b458]671
[15f3c3f]672 ipc_call_t answer;
673 aid_t req = async_send_0(exch, LOC_GET_NAMESPACES, &answer);
[b7fd2a0]674 errno_t rc = async_data_read_start(exch, devs, count * sizeof(loc_sdesc_t));
[a35b458]675
[15f3c3f]676 loc_exchange_end(exch);
[a35b458]677
[15f3c3f]678 if (rc == EOVERFLOW) {
679 /*
680 * Number of namespaces has changed since
681 * the last call of LOC_GET_NAMESPACE_COUNT
682 */
683 free(devs);
684 continue;
685 }
[a35b458]686
[15f3c3f]687 if (rc != EOK) {
[50b581d]688 async_forget(req);
[15f3c3f]689 free(devs);
690 return 0;
691 }
[a35b458]692
[b7fd2a0]693 errno_t retval;
[15f3c3f]694 async_wait_for(req, &retval);
[a35b458]695
[15f3c3f]696 if (retval != EOK)
697 return 0;
[a35b458]698
[15f3c3f]699 *data = devs;
700 return count;
701 }
702}
703
704size_t loc_get_services(service_id_t ns_handle, loc_sdesc_t **data)
705{
[cc574511]706 /* Loop until read is succesful */
[15f3c3f]707 while (true) {
[f9b2cb4c]708 async_exch_t *exch = loc_exchange_begin_blocking(INTERFACE_LOC_CONSUMER);
[15f3c3f]709 size_t count = loc_count_services_internal(exch, ns_handle);
710 loc_exchange_end(exch);
[a35b458]711
[15f3c3f]712 if (count == 0)
713 return 0;
[a35b458]714
[15f3c3f]715 loc_sdesc_t *devs = (loc_sdesc_t *) calloc(count, sizeof(loc_sdesc_t));
716 if (devs == NULL)
717 return 0;
[a35b458]718
[f9b2cb4c]719 exch = loc_exchange_begin(INTERFACE_LOC_CONSUMER);
[a35b458]720
[15f3c3f]721 ipc_call_t answer;
722 aid_t req = async_send_1(exch, LOC_GET_SERVICES, ns_handle, &answer);
[b7fd2a0]723 errno_t rc = async_data_read_start(exch, devs, count * sizeof(loc_sdesc_t));
[a35b458]724
[15f3c3f]725 loc_exchange_end(exch);
[a35b458]726
[15f3c3f]727 if (rc == EOVERFLOW) {
728 /*
729 * Number of services has changed since
730 * the last call of LOC_GET_SERVICE_COUNT
731 */
732 free(devs);
733 continue;
734 }
[a35b458]735
[15f3c3f]736 if (rc != EOK) {
[50b581d]737 async_forget(req);
[15f3c3f]738 free(devs);
739 return 0;
740 }
[a35b458]741
[b7fd2a0]742 errno_t retval;
[15f3c3f]743 async_wait_for(req, &retval);
[a35b458]744
[15f3c3f]745 if (retval != EOK)
746 return 0;
[a35b458]747
[15f3c3f]748 *data = devs;
749 return count;
750 }
751}
[cc574511]752
[b7fd2a0]753static errno_t loc_category_get_ids_once(sysarg_t method, sysarg_t arg1,
[278ac72]754 sysarg_t *id_buf, size_t buf_size, size_t *act_size)
[cc574511]755{
[f9b2cb4c]756 async_exch_t *exch = loc_exchange_begin_blocking(INTERFACE_LOC_CONSUMER);
[cc574511]757
758 ipc_call_t answer;
[278ac72]759 aid_t req = async_send_1(exch, method, arg1, &answer);
[b7fd2a0]760 errno_t rc = async_data_read_start(exch, id_buf, buf_size);
[a35b458]761
[cc574511]762 loc_exchange_end(exch);
[a35b458]763
[cc574511]764 if (rc != EOK) {
[50b581d]765 async_forget(req);
[cc574511]766 return rc;
767 }
[a35b458]768
[b7fd2a0]769 errno_t retval;
[cc574511]770 async_wait_for(req, &retval);
[a35b458]771
[cc574511]772 if (retval != EOK) {
773 return retval;
774 }
[a35b458]775
[fafb8e5]776 *act_size = ipc_get_arg1(&answer);
[cc574511]777 return EOK;
778}
779
[278ac72]780/** Get list of IDs.
[cc574511]781 *
782 * Returns an allocated array of service IDs.
783 *
[278ac72]784 * @param method IPC method
785 * @param arg1 IPC argument 1
[cc574511]786 * @param data Place to store pointer to array of IDs
787 * @param count Place to store number of IDs
[cde999a]788 * @return EOK on success or an error code
[cc574511]789 */
[b7fd2a0]790static errno_t loc_get_ids_internal(sysarg_t method, sysarg_t arg1,
[278ac72]791 sysarg_t **data, size_t *count)
[cc574511]792{
793 *data = NULL;
[88057e3]794 *count = 0;
[a35b458]795
[88057e3]796 size_t act_size = 0;
[b7fd2a0]797 errno_t rc = loc_category_get_ids_once(method, arg1, NULL, 0,
[278ac72]798 &act_size);
[cc574511]799 if (rc != EOK)
800 return rc;
[a35b458]801
[88057e3]802 size_t alloc_size = act_size;
803 service_id_t *ids = malloc(alloc_size);
[cc574511]804 if (ids == NULL)
805 return ENOMEM;
[a35b458]806
[cc574511]807 while (true) {
[278ac72]808 rc = loc_category_get_ids_once(method, arg1, ids, alloc_size,
[cc574511]809 &act_size);
810 if (rc != EOK)
811 return rc;
[a35b458]812
[c1f27f1d]813 if (act_size <= alloc_size)
[cc574511]814 break;
[a35b458]815
[88057e3]816 alloc_size = act_size;
817 ids = realloc(ids, alloc_size);
[cc574511]818 if (ids == NULL)
819 return ENOMEM;
820 }
[a35b458]821
[cc574511]822 *count = act_size / sizeof(category_id_t);
823 *data = ids;
824 return EOK;
825}
[278ac72]826
827/** Get list of services in category.
828 *
829 * Returns an allocated array of service IDs.
830 *
831 * @param cat_id Category ID
832 * @param data Place to store pointer to array of IDs
833 * @param count Place to store number of IDs
[cde999a]834 * @return EOK on success or an error code
[278ac72]835 */
[b7fd2a0]836errno_t loc_category_get_svcs(category_id_t cat_id, service_id_t **data,
[278ac72]837 size_t *count)
838{
839 return loc_get_ids_internal(LOC_CATEGORY_GET_SVCS, cat_id,
840 data, count);
841}
842
843/** Get list of categories.
844 *
845 * Returns an allocated array of category IDs.
846 *
847 * @param data Place to store pointer to array of IDs
848 * @param count Place to store number of IDs
[cde999a]849 * @return EOK on success or an error code
[278ac72]850 */
[b7fd2a0]851errno_t loc_get_categories(category_id_t **data, size_t *count)
[278ac72]852{
853 return loc_get_ids_internal(LOC_GET_CATEGORIES, 0,
854 data, count);
855}
[12f9f0d0]856
[e89a06a]857errno_t loc_register_cat_change_cb(loc_cat_change_cb_t cb_fun, void *cb_arg)
[12f9f0d0]858{
[a6240a31]859 fibril_mutex_lock(&loc_callback_mutex);
[e89a06a]860 if (cat_change_cb != NULL) {
861 fibril_mutex_unlock(&loc_callback_mutex);
862 return EEXIST;
863 }
864
[a6240a31]865 if (loc_callback_create() != EOK) {
866 fibril_mutex_unlock(&loc_callback_mutex);
[12f9f0d0]867 return EIO;
[a6240a31]868 }
[a35b458]869
[12f9f0d0]870 cat_change_cb = cb_fun;
[e89a06a]871 cat_change_arg = cb_arg;
[a6240a31]872 fibril_mutex_unlock(&loc_callback_mutex);
[a35b458]873
[12f9f0d0]874 return EOK;
875}
Note: See TracBrowser for help on using the repository browser.