source: mainline/uspace/srv/loc/loc.c@ 16dc887

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 16dc887 was cc574511, checked in by Jiri Svoboda <jiri@…>, 14 years ago

Basic category support in location service. Devman adds device services
to categories. Switch over input server device discovery from using
/dev/class to loc categories.

  • Property mode set to 100644
File size: 27.8 KB
Line 
1/*
2 * Copyright (c) 2007 Josef Cejka
3 * Copyright (c) 2011 Jiri Svoboda
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/**
31 * @defgroup loc Location Service.
32 * @brief HelenOS location service.
33 * @{
34 */
35
36/** @file
37 */
38
39#include <ipc/services.h>
40#include <ns.h>
41#include <async.h>
42#include <stdio.h>
43#include <errno.h>
44#include <bool.h>
45#include <fibril_synch.h>
46#include <stdlib.h>
47#include <str.h>
48#include <ipc/loc.h>
49#include <assert.h>
50
51#include "category.h"
52#include "loc.h"
53
54#define NAME "loc"
55#define NULL_SERVICES 256
56
57LIST_INITIALIZE(services_list);
58LIST_INITIALIZE(namespaces_list);
59LIST_INITIALIZE(servers_list);
60
61/* Locking order:
62 * servers_list_mutex
63 * services_list_mutex
64 * (loc_server_t *)->services_mutex
65 * create_id_mutex
66 **/
67
68static FIBRIL_MUTEX_INITIALIZE(services_list_mutex);
69static FIBRIL_CONDVAR_INITIALIZE(services_list_cv);
70static FIBRIL_MUTEX_INITIALIZE(servers_list_mutex);
71static FIBRIL_MUTEX_INITIALIZE(create_id_mutex);
72static FIBRIL_MUTEX_INITIALIZE(null_services_mutex);
73
74static service_id_t last_id = 0;
75static loc_service_t *null_services[NULL_SERVICES];
76
77/*
78 * Dummy list for null services. This is necessary so that null services can
79 * be used just as any other services, e.g. in loc_service_unregister_core().
80 */
81static LIST_INITIALIZE(dummy_null_services);
82
83/** Service directory ogranized by categories (yellow pages) */
84static categ_dir_t cdir;
85
86service_id_t loc_create_id(void)
87{
88 /* TODO: allow reusing old ids after their unregistration
89 * and implement some version of LRU algorithm, avoid overflow
90 */
91
92 fibril_mutex_lock(&create_id_mutex);
93 last_id++;
94 fibril_mutex_unlock(&create_id_mutex);
95
96 return last_id;
97}
98
99/** Convert fully qualified service name to namespace and service name.
100 *
101 * A fully qualified service name can be either a plain service name
102 * (then the namespace is considered to be an empty string) or consist
103 * of two components separated by a slash. No more than one slash
104 * is allowed.
105 *
106 */
107static bool loc_fqsn_split(const char *fqsn, char **ns_name, char **name)
108{
109 size_t cnt = 0;
110 size_t slash_offset = 0;
111 size_t slash_after = 0;
112
113 size_t offset = 0;
114 size_t offset_prev = 0;
115 wchar_t c;
116
117 while ((c = str_decode(fqsn, &offset, STR_NO_LIMIT)) != 0) {
118 if (c == '/') {
119 cnt++;
120 slash_offset = offset_prev;
121 slash_after = offset;
122 }
123 offset_prev = offset;
124 }
125
126 /* More than one slash */
127 if (cnt > 1)
128 return false;
129
130 /* No slash -> namespace is empty */
131 if (cnt == 0) {
132 *ns_name = str_dup("");
133 if (*ns_name == NULL)
134 return false;
135
136 *name = str_dup(fqsn);
137 if (*name == NULL) {
138 free(*ns_name);
139 return false;
140 }
141
142 if (str_cmp(*name, "") == 0) {
143 free(*name);
144 free(*ns_name);
145 return false;
146 }
147
148 return true;
149 }
150
151 /* Exactly one slash */
152 *ns_name = str_ndup(fqsn, slash_offset);
153 if (*ns_name == NULL)
154 return false;
155
156 *name = str_dup(fqsn + slash_after);
157 if (*name == NULL) {
158 free(*ns_name);
159 return false;
160 }
161
162 if (str_cmp(*name, "") == 0) {
163 free(*name);
164 free(*ns_name);
165 return false;
166 }
167
168 return true;
169}
170
171/** Find namespace with given name. */
172static loc_namespace_t *loc_namespace_find_name(const char *name)
173{
174 assert(fibril_mutex_is_locked(&services_list_mutex));
175
176 list_foreach(namespaces_list, item) {
177 loc_namespace_t *namespace =
178 list_get_instance(item, loc_namespace_t, namespaces);
179 if (str_cmp(namespace->name, name) == 0)
180 return namespace;
181 }
182
183 return NULL;
184}
185
186/** Find namespace with given ID.
187 *
188 * @todo: use hash table
189 *
190 */
191static loc_namespace_t *loc_namespace_find_id(service_id_t id)
192{
193 assert(fibril_mutex_is_locked(&services_list_mutex));
194
195 list_foreach(namespaces_list, item) {
196 loc_namespace_t *namespace =
197 list_get_instance(item, loc_namespace_t, namespaces);
198 if (namespace->id == id)
199 return namespace;
200 }
201
202 return NULL;
203}
204
205/** Find service with given name. */
206static loc_service_t *loc_service_find_name(const char *ns_name,
207 const char *name)
208{
209 assert(fibril_mutex_is_locked(&services_list_mutex));
210
211 list_foreach(services_list, item) {
212 loc_service_t *service =
213 list_get_instance(item, loc_service_t, services);
214 if ((str_cmp(service->namespace->name, ns_name) == 0)
215 && (str_cmp(service->name, name) == 0))
216 return service;
217 }
218
219 return NULL;
220}
221
222/** Find service with given ID.
223 *
224 * @todo: use hash table
225 *
226 */
227static loc_service_t *loc_service_find_id(service_id_t id)
228{
229 assert(fibril_mutex_is_locked(&services_list_mutex));
230
231 list_foreach(services_list, item) {
232 loc_service_t *service =
233 list_get_instance(item, loc_service_t, services);
234 if (service->id == id)
235 return service;
236 }
237
238 return NULL;
239}
240
241/** Create a namespace (if not already present). */
242static loc_namespace_t *loc_namespace_create(const char *ns_name)
243{
244 loc_namespace_t *namespace;
245
246 assert(fibril_mutex_is_locked(&services_list_mutex));
247
248 namespace = loc_namespace_find_name(ns_name);
249 if (namespace != NULL)
250 return namespace;
251
252 namespace = (loc_namespace_t *) malloc(sizeof(loc_namespace_t));
253 if (namespace == NULL)
254 return NULL;
255
256 namespace->name = str_dup(ns_name);
257 if (namespace->name == NULL) {
258 free(namespace);
259 return NULL;
260 }
261
262 namespace->id = loc_create_id();
263 namespace->refcnt = 0;
264
265 /*
266 * Insert new namespace into list of registered namespaces
267 */
268 list_append(&(namespace->namespaces), &namespaces_list);
269
270 return namespace;
271}
272
273/** Destroy a namespace (if it is no longer needed). */
274static void loc_namespace_destroy(loc_namespace_t *namespace)
275{
276 assert(fibril_mutex_is_locked(&services_list_mutex));
277
278 if (namespace->refcnt == 0) {
279 list_remove(&(namespace->namespaces));
280
281 free(namespace->name);
282 free(namespace);
283 }
284}
285
286/** Increase namespace reference count by including service. */
287static void loc_namespace_addref(loc_namespace_t *namespace,
288 loc_service_t *service)
289{
290 assert(fibril_mutex_is_locked(&services_list_mutex));
291
292 service->namespace = namespace;
293 namespace->refcnt++;
294}
295
296/** Decrease namespace reference count. */
297static void loc_namespace_delref(loc_namespace_t *namespace)
298{
299 assert(fibril_mutex_is_locked(&services_list_mutex));
300
301 namespace->refcnt--;
302 loc_namespace_destroy(namespace);
303}
304
305/** Unregister service and free it. */
306static void loc_service_unregister_core(loc_service_t *service)
307{
308 assert(fibril_mutex_is_locked(&services_list_mutex));
309
310 loc_namespace_delref(service->namespace);
311 list_remove(&(service->services));
312 list_remove(&(service->server_services));
313
314 free(service->name);
315 free(service);
316}
317
318/**
319 * Read info about new server and add it into linked list of registered
320 * servers.
321 */
322static loc_server_t *loc_server_register(void)
323{
324 ipc_call_t icall;
325 ipc_callid_t iid = async_get_call(&icall);
326
327 if (IPC_GET_IMETHOD(icall) != LOC_SERVER_REGISTER) {
328 async_answer_0(iid, EREFUSED);
329 return NULL;
330 }
331
332 loc_server_t *server =
333 (loc_server_t *) malloc(sizeof(loc_server_t));
334 if (server == NULL) {
335 async_answer_0(iid, ENOMEM);
336 return NULL;
337 }
338
339 /*
340 * Get server name
341 */
342 int rc = async_data_write_accept((void **) &server->name, true, 0,
343 LOC_NAME_MAXLEN, 0, NULL);
344 if (rc != EOK) {
345 free(server);
346 async_answer_0(iid, rc);
347 return NULL;
348 }
349
350 /*
351 * Create connection to the server
352 */
353 server->sess = async_callback_receive(EXCHANGE_SERIALIZE);
354 if (!server->sess) {
355 free(server->name);
356 free(server);
357 async_answer_0(iid, ENOTSUP);
358 return NULL;
359 }
360
361 /*
362 * Initialize mutex for list of services
363 * supplied by this server
364 */
365 fibril_mutex_initialize(&server->services_mutex);
366
367 /*
368 * Initialize list of supplied services
369 */
370 list_initialize(&server->services);
371
372 link_initialize(&server->servers);
373
374 fibril_mutex_lock(&servers_list_mutex);
375
376 /* TODO:
377 * Check that no server with name equal to
378 * server->name is registered
379 */
380
381 /*
382 * Insert new server into list of registered servers
383 */
384 list_append(&(server->servers), &servers_list);
385 fibril_mutex_unlock(&servers_list_mutex);
386
387 async_answer_0(iid, EOK);
388
389 return server;
390}
391
392/**
393 * Unregister server, unregister all its services and free server
394 * structure.
395 *
396 */
397static int loc_server_unregister(loc_server_t *server)
398{
399 if (server == NULL)
400 return EEXISTS;
401
402 fibril_mutex_lock(&servers_list_mutex);
403
404 if (server->sess)
405 async_hangup(server->sess);
406
407 /* Remove it from list of servers */
408 list_remove(&(server->servers));
409
410 /* Unregister all its services */
411 fibril_mutex_lock(&services_list_mutex);
412 fibril_mutex_lock(&server->services_mutex);
413
414 while (!list_empty(&server->services)) {
415 loc_service_t *service = list_get_instance(
416 list_first(&server->services), loc_service_t,
417 server_services);
418 loc_service_unregister_core(service);
419 }
420
421 fibril_mutex_unlock(&server->services_mutex);
422 fibril_mutex_unlock(&services_list_mutex);
423 fibril_mutex_unlock(&servers_list_mutex);
424
425 /* Free name and server */
426 if (server->name != NULL)
427 free(server->name);
428
429 free(server);
430
431 return EOK;
432}
433
434/** Register service
435 *
436 */
437static void loc_service_register(ipc_callid_t iid, ipc_call_t *icall,
438 loc_server_t *server)
439{
440 if (server == NULL) {
441 async_answer_0(iid, EREFUSED);
442 return;
443 }
444
445 /* Create new service entry */
446 loc_service_t *service =
447 (loc_service_t *) malloc(sizeof(loc_service_t));
448 if (service == NULL) {
449 async_answer_0(iid, ENOMEM);
450 return;
451 }
452
453 /* Set the interface, if any. */
454 service->forward_interface = IPC_GET_ARG1(*icall);
455
456 /* Get fqsn */
457 char *fqsn;
458 int rc = async_data_write_accept((void **) &fqsn, true, 0,
459 LOC_NAME_MAXLEN, 0, NULL);
460 if (rc != EOK) {
461 free(service);
462 async_answer_0(iid, rc);
463 return;
464 }
465
466 char *ns_name;
467 if (!loc_fqsn_split(fqsn, &ns_name, &service->name)) {
468 free(fqsn);
469 free(service);
470 async_answer_0(iid, EINVAL);
471 return;
472 }
473
474 free(fqsn);
475
476 fibril_mutex_lock(&services_list_mutex);
477
478 loc_namespace_t *namespace = loc_namespace_create(ns_name);
479 free(ns_name);
480 if (namespace == NULL) {
481 fibril_mutex_unlock(&services_list_mutex);
482 free(service->name);
483 free(service);
484 async_answer_0(iid, ENOMEM);
485 return;
486 }
487
488 link_initialize(&service->services);
489 link_initialize(&service->server_services);
490
491 /* Check that service is not already registered */
492 if (loc_service_find_name(namespace->name, service->name) != NULL) {
493 printf("%s: Service '%s/%s' already registered\n", NAME,
494 namespace->name, service->name);
495 loc_namespace_destroy(namespace);
496 fibril_mutex_unlock(&services_list_mutex);
497 free(service->name);
498 free(service);
499 async_answer_0(iid, EEXISTS);
500 return;
501 }
502
503 /* Get unique service ID */
504 service->id = loc_create_id();
505
506 loc_namespace_addref(namespace, service);
507 service->server = server;
508
509 /* Insert service into list of all services */
510 list_append(&service->services, &services_list);
511
512 /* Insert service into list of services supplied by one server */
513 fibril_mutex_lock(&service->server->services_mutex);
514
515 list_append(&service->server_services, &service->server->services);
516
517 fibril_mutex_unlock(&service->server->services_mutex);
518 fibril_condvar_broadcast(&services_list_cv);
519 fibril_mutex_unlock(&services_list_mutex);
520
521 async_answer_1(iid, EOK, service->id);
522}
523
524/**
525 *
526 */
527static int loc_service_unregister(ipc_callid_t iid, ipc_call_t *icall,
528 loc_server_t *server)
529{
530 /* TODO */
531 return EOK;
532}
533
534/** Connect client to the service.
535 *
536 * Find server supplying requested service and forward
537 * the message to it.
538 *
539 */
540static void loc_forward(ipc_callid_t callid, ipc_call_t *call)
541{
542 fibril_mutex_lock(&services_list_mutex);
543
544 /*
545 * Get ID from request
546 */
547 service_id_t id = IPC_GET_ARG2(*call);
548 loc_service_t *svc = loc_service_find_id(id);
549
550 if ((svc == NULL) || (svc->server == NULL) || (!svc->server->sess)) {
551 fibril_mutex_unlock(&services_list_mutex);
552 async_answer_0(callid, ENOENT);
553 return;
554 }
555
556 async_exch_t *exch = async_exchange_begin(svc->server->sess);
557
558 if (svc->forward_interface == 0)
559 async_forward_fast(callid, exch, svc->id, 0, 0, IPC_FF_NONE);
560 else
561 async_forward_fast(callid, exch, svc->forward_interface,
562 svc->id, 0, IPC_FF_NONE);
563
564 async_exchange_end(exch);
565
566 fibril_mutex_unlock(&services_list_mutex);
567}
568
569/** Find ID for service identified by name.
570 *
571 * In answer will be send EOK and service ID in arg1 or a error
572 * code from errno.h.
573 *
574 */
575static void loc_service_get_id(ipc_callid_t iid, ipc_call_t *icall)
576{
577 char *fqsn;
578
579 /* Get fqsn */
580 int rc = async_data_write_accept((void **) &fqsn, true, 0,
581 LOC_NAME_MAXLEN, 0, NULL);
582 if (rc != EOK) {
583 async_answer_0(iid, rc);
584 return;
585 }
586
587 char *ns_name;
588 char *name;
589 if (!loc_fqsn_split(fqsn, &ns_name, &name)) {
590 free(fqsn);
591 async_answer_0(iid, EINVAL);
592 return;
593 }
594
595 free(fqsn);
596
597 fibril_mutex_lock(&services_list_mutex);
598 const loc_service_t *svc;
599
600recheck:
601
602 /*
603 * Find service name in the list of known services.
604 */
605 svc = loc_service_find_name(ns_name, name);
606
607 /*
608 * Device was not found.
609 */
610 if (svc == NULL) {
611 if (IPC_GET_ARG1(*icall) & IPC_FLAG_BLOCKING) {
612 /* Blocking lookup */
613 fibril_condvar_wait(&services_list_cv,
614 &services_list_mutex);
615 goto recheck;
616 }
617
618 async_answer_0(iid, ENOENT);
619 free(ns_name);
620 free(name);
621 fibril_mutex_unlock(&services_list_mutex);
622 return;
623 }
624
625 async_answer_1(iid, EOK, svc->id);
626
627 fibril_mutex_unlock(&services_list_mutex);
628 free(ns_name);
629 free(name);
630}
631
632/** Find ID for namespace identified by name.
633 *
634 * In answer will be send EOK and service ID in arg1 or a error
635 * code from errno.h.
636 *
637 */
638static void loc_namespace_get_id(ipc_callid_t iid, ipc_call_t *icall)
639{
640 char *name;
641
642 /* Get service name */
643 int rc = async_data_write_accept((void **) &name, true, 0,
644 LOC_NAME_MAXLEN, 0, NULL);
645 if (rc != EOK) {
646 async_answer_0(iid, rc);
647 return;
648 }
649
650 fibril_mutex_lock(&services_list_mutex);
651 const loc_namespace_t *namespace;
652
653recheck:
654
655 /*
656 * Find namespace name in the list of known namespaces.
657 */
658 namespace = loc_namespace_find_name(name);
659
660 /*
661 * Namespace was not found.
662 */
663 if (namespace == NULL) {
664 if (IPC_GET_ARG1(*icall) & IPC_FLAG_BLOCKING) {
665 /* Blocking lookup */
666 fibril_condvar_wait(&services_list_cv,
667 &services_list_mutex);
668 goto recheck;
669 }
670
671 async_answer_0(iid, ENOENT);
672 free(name);
673 fibril_mutex_unlock(&services_list_mutex);
674 return;
675 }
676
677 async_answer_1(iid, EOK, namespace->id);
678
679 fibril_mutex_unlock(&services_list_mutex);
680 free(name);
681}
682
683/** Find ID for category specified by name.
684 *
685 * On success, answer will contain EOK int retval and service ID in arg1.
686 * On failure, error code will be sent in retval.
687 *
688 */
689static void loc_category_get_id(ipc_callid_t iid, ipc_call_t *icall)
690{
691 char *name;
692 category_t *cat;
693
694 /* Get service name */
695 int rc = async_data_write_accept((void **) &name, true, 0,
696 LOC_NAME_MAXLEN, 0, NULL);
697 if (rc != EOK) {
698 async_answer_0(iid, rc);
699 return;
700 }
701
702 fibril_mutex_lock(&cdir.mutex);
703
704 cat = category_find_by_name(&cdir, name);
705 if (cat == NULL) {
706 /* Category not found */
707 async_answer_0(iid, ENOENT);
708 goto cleanup;
709 }
710
711 async_answer_1(iid, EOK, cat->id);
712cleanup:
713 fibril_mutex_unlock(&cdir.mutex);
714 free(name);
715}
716
717static void loc_id_probe(ipc_callid_t iid, ipc_call_t *icall)
718{
719 fibril_mutex_lock(&services_list_mutex);
720
721 loc_namespace_t *namespace =
722 loc_namespace_find_id(IPC_GET_ARG1(*icall));
723 if (namespace == NULL) {
724 loc_service_t *svc =
725 loc_service_find_id(IPC_GET_ARG1(*icall));
726 if (svc == NULL)
727 async_answer_1(iid, EOK, LOC_OBJECT_NONE);
728 else
729 async_answer_1(iid, EOK, LOC_OBJECT_SERVICE);
730 } else
731 async_answer_1(iid, EOK, LOC_OBJECT_NAMESPACE);
732
733 fibril_mutex_unlock(&services_list_mutex);
734}
735
736static void loc_get_namespace_count(ipc_callid_t iid, ipc_call_t *icall)
737{
738 fibril_mutex_lock(&services_list_mutex);
739 async_answer_1(iid, EOK, list_count(&namespaces_list));
740 fibril_mutex_unlock(&services_list_mutex);
741}
742
743static void loc_get_service_count(ipc_callid_t iid, ipc_call_t *icall)
744{
745 fibril_mutex_lock(&services_list_mutex);
746
747 loc_namespace_t *namespace =
748 loc_namespace_find_id(IPC_GET_ARG1(*icall));
749 if (namespace == NULL)
750 async_answer_0(iid, EEXISTS);
751 else
752 async_answer_1(iid, EOK, namespace->refcnt);
753
754 fibril_mutex_unlock(&services_list_mutex);
755}
756
757static void loc_get_namespaces(ipc_callid_t iid, ipc_call_t *icall)
758{
759 ipc_callid_t callid;
760 size_t size;
761 if (!async_data_read_receive(&callid, &size)) {
762 async_answer_0(callid, EREFUSED);
763 async_answer_0(iid, EREFUSED);
764 return;
765 }
766
767 if ((size % sizeof(loc_sdesc_t)) != 0) {
768 async_answer_0(callid, EINVAL);
769 async_answer_0(iid, EINVAL);
770 return;
771 }
772
773 fibril_mutex_lock(&services_list_mutex);
774
775 size_t count = size / sizeof(loc_sdesc_t);
776 if (count != list_count(&namespaces_list)) {
777 fibril_mutex_unlock(&services_list_mutex);
778 async_answer_0(callid, EOVERFLOW);
779 async_answer_0(iid, EOVERFLOW);
780 return;
781 }
782
783 loc_sdesc_t *desc = (loc_sdesc_t *) malloc(size);
784 if (desc == NULL) {
785 fibril_mutex_unlock(&services_list_mutex);
786 async_answer_0(callid, ENOMEM);
787 async_answer_0(iid, ENOMEM);
788 return;
789 }
790
791 size_t pos = 0;
792 list_foreach(namespaces_list, item) {
793 loc_namespace_t *namespace =
794 list_get_instance(item, loc_namespace_t, namespaces);
795
796 desc[pos].id = namespace->id;
797 str_cpy(desc[pos].name, LOC_NAME_MAXLEN, namespace->name);
798 pos++;
799 }
800
801 sysarg_t retval = async_data_read_finalize(callid, desc, size);
802
803 free(desc);
804 fibril_mutex_unlock(&services_list_mutex);
805
806 async_answer_0(iid, retval);
807}
808
809static void loc_get_services(ipc_callid_t iid, ipc_call_t *icall)
810{
811 /* FIXME: Use faster algorithm which can make better use
812 of namespaces */
813
814 ipc_callid_t callid;
815 size_t size;
816 if (!async_data_read_receive(&callid, &size)) {
817 async_answer_0(callid, EREFUSED);
818 async_answer_0(iid, EREFUSED);
819 return;
820 }
821
822 if ((size % sizeof(loc_sdesc_t)) != 0) {
823 async_answer_0(callid, EINVAL);
824 async_answer_0(iid, EINVAL);
825 return;
826 }
827
828 fibril_mutex_lock(&services_list_mutex);
829
830 loc_namespace_t *namespace =
831 loc_namespace_find_id(IPC_GET_ARG1(*icall));
832 if (namespace == NULL) {
833 fibril_mutex_unlock(&services_list_mutex);
834 async_answer_0(callid, ENOENT);
835 async_answer_0(iid, ENOENT);
836 return;
837 }
838
839 size_t count = size / sizeof(loc_sdesc_t);
840 if (count != namespace->refcnt) {
841 fibril_mutex_unlock(&services_list_mutex);
842 async_answer_0(callid, EOVERFLOW);
843 async_answer_0(iid, EOVERFLOW);
844 return;
845 }
846
847 loc_sdesc_t *desc = (loc_sdesc_t *) malloc(size);
848 if (desc == NULL) {
849 fibril_mutex_unlock(&services_list_mutex);
850 async_answer_0(callid, ENOMEM);
851 async_answer_0(iid, EREFUSED);
852 return;
853 }
854
855 size_t pos = 0;
856 list_foreach(services_list, item) {
857 loc_service_t *service =
858 list_get_instance(item, loc_service_t, services);
859
860 if (service->namespace == namespace) {
861 desc[pos].id = service->id;
862 str_cpy(desc[pos].name, LOC_NAME_MAXLEN, service->name);
863 pos++;
864 }
865 }
866
867 sysarg_t retval = async_data_read_finalize(callid, desc, size);
868
869 free(desc);
870 fibril_mutex_unlock(&services_list_mutex);
871
872 async_answer_0(iid, retval);
873}
874
875static void loc_category_get_svcs(ipc_callid_t iid, ipc_call_t *icall)
876{
877 ipc_callid_t callid;
878 size_t size;
879 size_t act_size;
880 int rc;
881
882 if (!async_data_read_receive(&callid, &size)) {
883 async_answer_0(callid, EREFUSED);
884 async_answer_0(iid, EREFUSED);
885 return;
886 }
887
888 fibril_mutex_lock(&cdir.mutex);
889
890 category_t *cat = category_get(&cdir, IPC_GET_ARG1(*icall));
891 if (cat == NULL) {
892 fibril_mutex_unlock(&cdir.mutex);
893 async_answer_0(callid, ENOENT);
894 async_answer_0(iid, ENOENT);
895 return;
896 }
897
898 category_id_t *id_buf = (category_id_t *) malloc(size);
899 if (id_buf == NULL) {
900 fibril_mutex_unlock(&cdir.mutex);
901 async_answer_0(callid, ENOMEM);
902 async_answer_0(iid, ENOMEM);
903 return;
904 }
905
906 fibril_mutex_lock(&cat->mutex);
907
908 rc = category_get_services(cat, id_buf, size, &act_size);
909 if (rc != EOK) {
910 fibril_mutex_unlock(&cat->mutex);
911 fibril_mutex_unlock(&cdir.mutex);
912 async_answer_0(callid, rc);
913 async_answer_0(iid, rc);
914 return;
915 }
916
917 fibril_mutex_unlock(&cat->mutex);
918 fibril_mutex_unlock(&cdir.mutex);
919
920 sysarg_t retval = async_data_read_finalize(callid, id_buf, size);
921 free(id_buf);
922
923 async_answer_1(iid, retval, act_size);
924}
925
926
927static void loc_null_create(ipc_callid_t iid, ipc_call_t *icall)
928{
929 fibril_mutex_lock(&null_services_mutex);
930
931 unsigned int i;
932 bool fnd = false;
933
934 for (i = 0; i < NULL_SERVICES; i++) {
935 if (null_services[i] == NULL) {
936 fnd = true;
937 break;
938 }
939 }
940
941 if (!fnd) {
942 fibril_mutex_unlock(&null_services_mutex);
943 async_answer_0(iid, ENOMEM);
944 return;
945 }
946
947 char null[LOC_NAME_MAXLEN];
948 snprintf(null, LOC_NAME_MAXLEN, "%u", i);
949
950 char *dev_name = str_dup(null);
951 if (dev_name == NULL) {
952 fibril_mutex_unlock(&null_services_mutex);
953 async_answer_0(iid, ENOMEM);
954 return;
955 }
956
957 loc_service_t *service =
958 (loc_service_t *) malloc(sizeof(loc_service_t));
959 if (service == NULL) {
960 fibril_mutex_unlock(&null_services_mutex);
961 async_answer_0(iid, ENOMEM);
962 return;
963 }
964
965 fibril_mutex_lock(&services_list_mutex);
966
967 loc_namespace_t *namespace = loc_namespace_create("null");
968 if (namespace == NULL) {
969 fibril_mutex_lock(&services_list_mutex);
970 fibril_mutex_unlock(&null_services_mutex);
971 async_answer_0(iid, ENOMEM);
972 return;
973 }
974
975 link_initialize(&service->services);
976 link_initialize(&service->server_services);
977
978 /* Get unique service ID */
979 service->id = loc_create_id();
980 service->server = NULL;
981
982 loc_namespace_addref(namespace, service);
983 service->name = dev_name;
984
985 /*
986 * Insert service into list of all services and into null services array.
987 * Insert service into a dummy list of null server's services so that it
988 * can be safely removed later.
989 */
990 list_append(&service->services, &services_list);
991 list_append(&service->server_services, &dummy_null_services);
992 null_services[i] = service;
993
994 fibril_mutex_unlock(&services_list_mutex);
995 fibril_mutex_unlock(&null_services_mutex);
996
997 async_answer_1(iid, EOK, (sysarg_t) i);
998}
999
1000static void loc_null_destroy(ipc_callid_t iid, ipc_call_t *icall)
1001{
1002 sysarg_t i = IPC_GET_ARG1(*icall);
1003 if (i >= NULL_SERVICES) {
1004 async_answer_0(iid, ELIMIT);
1005 return;
1006 }
1007
1008 fibril_mutex_lock(&null_services_mutex);
1009
1010 if (null_services[i] == NULL) {
1011 fibril_mutex_unlock(&null_services_mutex);
1012 async_answer_0(iid, ENOENT);
1013 return;
1014 }
1015
1016 fibril_mutex_lock(&services_list_mutex);
1017 loc_service_unregister_core(null_services[i]);
1018 fibril_mutex_unlock(&services_list_mutex);
1019
1020 null_services[i] = NULL;
1021
1022 fibril_mutex_unlock(&null_services_mutex);
1023 async_answer_0(iid, EOK);
1024}
1025
1026static void loc_service_add_to_cat(ipc_callid_t iid, ipc_call_t *icall)
1027{
1028 category_t *cat;
1029 loc_service_t *svc;
1030 catid_t cat_id;
1031 service_id_t svc_id;
1032 sysarg_t retval;
1033
1034 svc_id = IPC_GET_ARG1(*icall);
1035 cat_id = IPC_GET_ARG2(*icall);
1036
1037 fibril_mutex_lock(&services_list_mutex);
1038 fibril_mutex_lock(&cdir.mutex);
1039
1040 cat = category_get(&cdir, cat_id);
1041 svc = loc_service_find_id(svc_id);
1042
1043 fibril_mutex_lock(&cat->mutex);
1044 retval = category_add_service(cat, svc);
1045
1046 fibril_mutex_unlock(&cat->mutex);
1047 fibril_mutex_unlock(&cdir.mutex);
1048 fibril_mutex_unlock(&services_list_mutex);
1049
1050 async_answer_0(iid, retval);
1051}
1052
1053
1054/** Initialize location service.
1055 *
1056 *
1057 */
1058static bool loc_init(void)
1059{
1060 unsigned int i;
1061 category_t *cat;
1062
1063 for (i = 0; i < NULL_SERVICES; i++)
1064 null_services[i] = NULL;
1065
1066 categ_dir_init(&cdir);
1067
1068 cat = category_new("bd");
1069 categ_dir_add_cat(&cdir, cat);
1070
1071 cat = category_new("keyboard");
1072 categ_dir_add_cat(&cdir, cat);
1073
1074 cat = category_new("mouse");
1075 categ_dir_add_cat(&cdir, cat);
1076
1077 cat = category_new("serial");
1078 categ_dir_add_cat(&cdir, cat);
1079
1080 return true;
1081}
1082
1083/** Handle connection on supplier port.
1084 *
1085 */
1086static void loc_connection_supplier(ipc_callid_t iid, ipc_call_t *icall)
1087{
1088 /* Accept connection */
1089 async_answer_0(iid, EOK);
1090
1091 loc_server_t *server = loc_server_register();
1092 if (server == NULL)
1093 return;
1094
1095 while (true) {
1096 ipc_call_t call;
1097 ipc_callid_t callid = async_get_call(&call);
1098
1099 if (!IPC_GET_IMETHOD(call))
1100 break;
1101
1102 switch (IPC_GET_IMETHOD(call)) {
1103 case LOC_SERVER_UNREGISTER:
1104 if (server == NULL)
1105 async_answer_0(callid, ENOENT);
1106 else
1107 async_answer_0(callid, EOK);
1108 break;
1109 case LOC_SERVICE_ADD_TO_CAT:
1110 /* Add service to category */
1111 loc_service_add_to_cat(callid, &call);
1112 break;
1113 case LOC_SERVICE_REGISTER:
1114 /* Register one service */
1115 loc_service_register(callid, &call, server);
1116 break;
1117 case LOC_SERVICE_UNREGISTER:
1118 /* Remove one service */
1119 loc_service_unregister(callid, &call, server);
1120 break;
1121 case LOC_SERVICE_GET_ID:
1122 loc_service_get_id(callid, &call);
1123 break;
1124 case LOC_NAMESPACE_GET_ID:
1125 loc_namespace_get_id(callid, &call);
1126 break;
1127 default:
1128 async_answer_0(callid, ENOENT);
1129 }
1130 }
1131
1132 if (server != NULL) {
1133 /*
1134 * Unregister the server and all its services.
1135 */
1136 loc_server_unregister(server);
1137 server = NULL;
1138 }
1139}
1140
1141/** Handle connection on consumer port.
1142 *
1143 */
1144static void loc_connection_consumer(ipc_callid_t iid, ipc_call_t *icall)
1145{
1146 /* Accept connection */
1147 async_answer_0(iid, EOK);
1148
1149 while (true) {
1150 ipc_call_t call;
1151 ipc_callid_t callid = async_get_call(&call);
1152
1153 if (!IPC_GET_IMETHOD(call))
1154 break;
1155
1156 switch (IPC_GET_IMETHOD(call)) {
1157 case LOC_SERVICE_GET_ID:
1158 loc_service_get_id(callid, &call);
1159 break;
1160 case LOC_NAMESPACE_GET_ID:
1161 loc_namespace_get_id(callid, &call);
1162 break;
1163 case LOC_CATEGORY_GET_ID:
1164 loc_category_get_id(callid, &call);
1165 break;
1166 case LOC_CATEGORY_GET_SVCS:
1167 loc_category_get_svcs(callid, &call);
1168 break;
1169 case LOC_ID_PROBE:
1170 loc_id_probe(callid, &call);
1171 break;
1172 case LOC_NULL_CREATE:
1173 loc_null_create(callid, &call);
1174 break;
1175 case LOC_NULL_DESTROY:
1176 loc_null_destroy(callid, &call);
1177 break;
1178 case LOC_GET_NAMESPACE_COUNT:
1179 loc_get_namespace_count(callid, &call);
1180 break;
1181 case LOC_GET_SERVICE_COUNT:
1182 loc_get_service_count(callid, &call);
1183 break;
1184 case LOC_GET_NAMESPACES:
1185 loc_get_namespaces(callid, &call);
1186 break;
1187 case LOC_GET_SERVICES:
1188 loc_get_services(callid, &call);
1189 break;
1190 default:
1191 async_answer_0(callid, ENOENT);
1192 }
1193 }
1194}
1195
1196/** Function for handling connections to location service
1197 *
1198 */
1199static void loc_connection(ipc_callid_t iid, ipc_call_t *icall, void *arg)
1200{
1201 /* Select interface */
1202 switch ((sysarg_t) (IPC_GET_ARG1(*icall))) {
1203 case LOC_PORT_SUPPLIER:
1204 loc_connection_supplier(iid, icall);
1205 break;
1206 case LOC_PORT_CONSUMER:
1207 loc_connection_consumer(iid, icall);
1208 break;
1209 case LOC_CONNECT_TO_SERVICE:
1210 /* Connect client to selected service */
1211 loc_forward(iid, icall);
1212 break;
1213 default:
1214 /* No such interface */
1215 async_answer_0(iid, ENOENT);
1216 }
1217}
1218
1219/**
1220 *
1221 */
1222int main(int argc, char *argv[])
1223{
1224 printf("%s: HelenOS Location Service\n", NAME);
1225
1226 if (!loc_init()) {
1227 printf("%s: Error while initializing service\n", NAME);
1228 return -1;
1229 }
1230
1231 /* Set a handler of incomming connections */
1232 async_set_client_connection(loc_connection);
1233
1234 /* Register location service at naming service */
1235 if (service_register(SERVICE_LOC) != EOK)
1236 return -1;
1237
1238 printf("%s: Accepting connections\n", NAME);
1239 async_manager();
1240
1241 /* Never reached */
1242 return 0;
1243}
1244
1245/**
1246 * @}
1247 */
Note: See TracBrowser for help on using the repository browser.