| 1 | /*
|
|---|
| 2 | * Copyright (c) 2025 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, fallback_port_id,
|
|---|
| 64 | &fun->service_id);
|
|---|
| 65 |
|
|---|
| 66 | tree_add_loc_function(tree, fun);
|
|---|
| 67 |
|
|---|
| 68 | free(loc_name);
|
|---|
| 69 | free(loc_pathname);
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | errno_t loc_unregister_tree_function(fun_node_t *fun, dev_tree_t *tree)
|
|---|
| 73 | {
|
|---|
| 74 | errno_t rc = loc_service_unregister(devman_srv, fun->service_id);
|
|---|
| 75 | tree_rem_loc_function(tree, fun);
|
|---|
| 76 | return rc;
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | fun_node_t *find_loc_tree_function(dev_tree_t *tree, service_id_t service_id)
|
|---|
| 80 | {
|
|---|
| 81 | fun_node_t *fun = NULL;
|
|---|
| 82 |
|
|---|
| 83 | fibril_rwlock_read_lock(&tree->rwlock);
|
|---|
| 84 | ht_link_t *link = hash_table_find(&tree->loc_functions, &service_id);
|
|---|
| 85 | if (link != NULL) {
|
|---|
| 86 | fun = hash_table_get_inst(link, fun_node_t, loc_fun);
|
|---|
| 87 | fun_add_ref(fun);
|
|---|
| 88 | }
|
|---|
| 89 | fibril_rwlock_read_unlock(&tree->rwlock);
|
|---|
| 90 |
|
|---|
| 91 | return fun;
|
|---|
| 92 | }
|
|---|
| 93 |
|
|---|
| 94 | void tree_add_loc_function(dev_tree_t *tree, fun_node_t *fun)
|
|---|
| 95 | {
|
|---|
| 96 | assert(fibril_rwlock_is_write_locked(&tree->rwlock));
|
|---|
| 97 |
|
|---|
| 98 | hash_table_insert(&tree->loc_functions, &fun->loc_fun);
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| 101 | void tree_rem_loc_function(dev_tree_t *tree, fun_node_t *fun)
|
|---|
| 102 | {
|
|---|
| 103 | assert(fibril_rwlock_is_write_locked(&tree->rwlock));
|
|---|
| 104 |
|
|---|
| 105 | hash_table_remove(&tree->loc_functions, &fun->service_id);
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 | /** @}
|
|---|
| 109 | */
|
|---|