1 | /*
|
---|
2 | * Copyright (c) 2023 Jiri Svoboda
|
---|
3 | * Copyright (c) 2010 Lenka Trochtova
|
---|
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 devman
|
---|
31 | * @{
|
---|
32 | */
|
---|
33 |
|
---|
34 | #include <loc.h>
|
---|
35 | #include <stdio.h>
|
---|
36 |
|
---|
37 | #include "devman.h"
|
---|
38 | #include "fun.h"
|
---|
39 | #include "loc.h"
|
---|
40 | #include "main.h"
|
---|
41 |
|
---|
42 | /** Create loc path and name for the function. */
|
---|
43 | void loc_register_tree_function(fun_node_t *fun, dev_tree_t *tree)
|
---|
44 | {
|
---|
45 | char *loc_pathname = NULL;
|
---|
46 | char *loc_name = NULL;
|
---|
47 |
|
---|
48 | assert(fibril_rwlock_is_locked(&tree->rwlock));
|
---|
49 |
|
---|
50 | asprintf(&loc_name, "%s", fun->pathname);
|
---|
51 | if (loc_name == NULL)
|
---|
52 | return;
|
---|
53 |
|
---|
54 | replace_char(loc_name, '/', LOC_SEPARATOR);
|
---|
55 |
|
---|
56 | asprintf(&loc_pathname, "%s/%s", LOC_DEVICE_NAMESPACE,
|
---|
57 | loc_name);
|
---|
58 | if (loc_pathname == NULL) {
|
---|
59 | free(loc_name);
|
---|
60 | return;
|
---|
61 | }
|
---|
62 |
|
---|
63 | loc_service_register(devman_srv, loc_pathname, &fun->service_id);
|
---|
64 |
|
---|
65 | tree_add_loc_function(tree, fun);
|
---|
66 |
|
---|
67 | free(loc_name);
|
---|
68 | free(loc_pathname);
|
---|
69 | }
|
---|
70 |
|
---|
71 | errno_t loc_unregister_tree_function(fun_node_t *fun, dev_tree_t *tree)
|
---|
72 | {
|
---|
73 | errno_t rc = loc_service_unregister(devman_srv, fun->service_id);
|
---|
74 | tree_rem_loc_function(tree, fun);
|
---|
75 | return rc;
|
---|
76 | }
|
---|
77 |
|
---|
78 | fun_node_t *find_loc_tree_function(dev_tree_t *tree, service_id_t service_id)
|
---|
79 | {
|
---|
80 | fun_node_t *fun = NULL;
|
---|
81 |
|
---|
82 | fibril_rwlock_read_lock(&tree->rwlock);
|
---|
83 | ht_link_t *link = hash_table_find(&tree->loc_functions, &service_id);
|
---|
84 | if (link != NULL) {
|
---|
85 | fun = hash_table_get_inst(link, fun_node_t, loc_fun);
|
---|
86 | fun_add_ref(fun);
|
---|
87 | }
|
---|
88 | fibril_rwlock_read_unlock(&tree->rwlock);
|
---|
89 |
|
---|
90 | return fun;
|
---|
91 | }
|
---|
92 |
|
---|
93 | void tree_add_loc_function(dev_tree_t *tree, fun_node_t *fun)
|
---|
94 | {
|
---|
95 | assert(fibril_rwlock_is_write_locked(&tree->rwlock));
|
---|
96 |
|
---|
97 | hash_table_insert(&tree->loc_functions, &fun->loc_fun);
|
---|
98 | }
|
---|
99 |
|
---|
100 | void tree_rem_loc_function(dev_tree_t *tree, fun_node_t *fun)
|
---|
101 | {
|
---|
102 | assert(fibril_rwlock_is_write_locked(&tree->rwlock));
|
---|
103 |
|
---|
104 | hash_table_remove(&tree->loc_functions, &fun->service_id);
|
---|
105 | }
|
---|
106 |
|
---|
107 | /** @}
|
---|
108 | */
|
---|