| 1 | /*
 | 
|---|
| 2 |  * Copyright (c) 2010 Lenka Trochtova
 | 
|---|
| 3 |  * All rights reserved.
 | 
|---|
| 4 |  *
 | 
|---|
| 5 |  * Redistribution and use in source and binary forms, with or without
 | 
|---|
| 6 |  * modification, are permitted provided that the following conditions
 | 
|---|
| 7 |  * are met:
 | 
|---|
| 8 |  *
 | 
|---|
| 9 |  * - Redistributions of source code must retain the above copyright
 | 
|---|
| 10 |  *   notice, this list of conditions and the following disclaimer.
 | 
|---|
| 11 |  * - Redistributions in binary form must reproduce the above copyright
 | 
|---|
| 12 |  *   notice, this list of conditions and the following disclaimer in the
 | 
|---|
| 13 |  *   documentation and/or other materials provided with the distribution.
 | 
|---|
| 14 |  * - The name of the author may not be used to endorse or promote products
 | 
|---|
| 15 |  *   derived from this software without specific prior written permission.
 | 
|---|
| 16 |  *
 | 
|---|
| 17 |  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 | 
|---|
| 18 |  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 | 
|---|
| 19 |  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 | 
|---|
| 20 |  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 | 
|---|
| 21 |  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 | 
|---|
| 22 |  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 | 
|---|
| 23 |  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 | 
|---|
| 24 |  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 | 
|---|
| 25 |  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 | 
|---|
| 26 |  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 | 
|---|
| 27 |  */
 | 
|---|
| 28 | 
 | 
|---|
| 29 | /** @addtogroup devman
 | 
|---|
| 30 |  * @{
 | 
|---|
| 31 |  */
 | 
|---|
| 32 | 
 | 
|---|
| 33 | #include <loc.h>
 | 
|---|
| 34 | #include <stdio.h>
 | 
|---|
| 35 | 
 | 
|---|
| 36 | #include "devman.h"
 | 
|---|
| 37 | #include "fun.h"
 | 
|---|
| 38 | #include "loc.h"
 | 
|---|
| 39 | 
 | 
|---|
| 40 | /** Create loc path and name for the function. */
 | 
|---|
| 41 | void loc_register_tree_function(fun_node_t *fun, dev_tree_t *tree)
 | 
|---|
| 42 | {
 | 
|---|
| 43 |         char *loc_pathname = NULL;
 | 
|---|
| 44 |         char *loc_name = NULL;
 | 
|---|
| 45 | 
 | 
|---|
| 46 |         assert(fibril_rwlock_is_locked(&tree->rwlock));
 | 
|---|
| 47 | 
 | 
|---|
| 48 |         asprintf(&loc_name, "%s", fun->pathname);
 | 
|---|
| 49 |         if (loc_name == NULL)
 | 
|---|
| 50 |                 return;
 | 
|---|
| 51 | 
 | 
|---|
| 52 |         replace_char(loc_name, '/', LOC_SEPARATOR);
 | 
|---|
| 53 | 
 | 
|---|
| 54 |         asprintf(&loc_pathname, "%s/%s", LOC_DEVICE_NAMESPACE,
 | 
|---|
| 55 |             loc_name);
 | 
|---|
| 56 |         if (loc_pathname == NULL) {
 | 
|---|
| 57 |                 free(loc_name);
 | 
|---|
| 58 |                 return;
 | 
|---|
| 59 |         }
 | 
|---|
| 60 | 
 | 
|---|
| 61 |         loc_service_register(loc_pathname, &fun->service_id);
 | 
|---|
| 62 | 
 | 
|---|
| 63 |         tree_add_loc_function(tree, fun);
 | 
|---|
| 64 | 
 | 
|---|
| 65 |         free(loc_name);
 | 
|---|
| 66 |         free(loc_pathname);
 | 
|---|
| 67 | }
 | 
|---|
| 68 | 
 | 
|---|
| 69 | errno_t loc_unregister_tree_function(fun_node_t *fun, dev_tree_t *tree)
 | 
|---|
| 70 | {
 | 
|---|
| 71 |         errno_t rc = loc_service_unregister(fun->service_id);
 | 
|---|
| 72 |         tree_rem_loc_function(tree, fun);
 | 
|---|
| 73 |         return rc;
 | 
|---|
| 74 | }
 | 
|---|
| 75 | 
 | 
|---|
| 76 | fun_node_t *find_loc_tree_function(dev_tree_t *tree, service_id_t service_id)
 | 
|---|
| 77 | {
 | 
|---|
| 78 |         fun_node_t *fun = NULL;
 | 
|---|
| 79 | 
 | 
|---|
| 80 |         fibril_rwlock_read_lock(&tree->rwlock);
 | 
|---|
| 81 |         ht_link_t *link = hash_table_find(&tree->loc_functions, &service_id);
 | 
|---|
| 82 |         if (link != NULL) {
 | 
|---|
| 83 |                 fun = hash_table_get_inst(link, fun_node_t, loc_fun);
 | 
|---|
| 84 |                 fun_add_ref(fun);
 | 
|---|
| 85 |         }
 | 
|---|
| 86 |         fibril_rwlock_read_unlock(&tree->rwlock);
 | 
|---|
| 87 | 
 | 
|---|
| 88 |         return fun;
 | 
|---|
| 89 | }
 | 
|---|
| 90 | 
 | 
|---|
| 91 | void tree_add_loc_function(dev_tree_t *tree, fun_node_t *fun)
 | 
|---|
| 92 | {
 | 
|---|
| 93 |         assert(fibril_rwlock_is_write_locked(&tree->rwlock));
 | 
|---|
| 94 | 
 | 
|---|
| 95 |         hash_table_insert(&tree->loc_functions, &fun->loc_fun);
 | 
|---|
| 96 | }
 | 
|---|
| 97 | 
 | 
|---|
| 98 | void tree_rem_loc_function(dev_tree_t *tree, fun_node_t *fun)
 | 
|---|
| 99 | {
 | 
|---|
| 100 |         assert(fibril_rwlock_is_write_locked(&tree->rwlock));
 | 
|---|
| 101 | 
 | 
|---|
| 102 |         hash_table_remove(&tree->loc_functions, &fun->service_id);
 | 
|---|
| 103 | }
 | 
|---|
| 104 | 
 | 
|---|
| 105 | /** @}
 | 
|---|
| 106 |  */
 | 
|---|