| 1 | /*
|
|---|
| 2 | * Copyright (c) 2011 Jiri Svoboda
|
|---|
| 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 loc
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /** @file Categories for location service.
|
|---|
| 33 | */
|
|---|
| 34 |
|
|---|
| 35 | #include <adt/list.h>
|
|---|
| 36 | #include <errno.h>
|
|---|
| 37 | #include <fibril_synch.h>
|
|---|
| 38 | #include <stdlib.h>
|
|---|
| 39 | #include <str.h>
|
|---|
| 40 |
|
|---|
| 41 | #include "category.h"
|
|---|
| 42 | #include "loc.h"
|
|---|
| 43 |
|
|---|
| 44 | /** Initialize category directory. */
|
|---|
| 45 | void categ_dir_init(categ_dir_t *cdir)
|
|---|
| 46 | {
|
|---|
| 47 | fibril_mutex_initialize(&cdir->mutex);
|
|---|
| 48 | list_initialize(&cdir->categories);
|
|---|
| 49 | }
|
|---|
| 50 |
|
|---|
| 51 | /** Add new category to directory. */
|
|---|
| 52 | void categ_dir_add_cat(categ_dir_t *cdir, category_t *cat)
|
|---|
| 53 | {
|
|---|
| 54 | list_append(&cat->cat_list, &cdir->categories);
|
|---|
| 55 | }
|
|---|
| 56 |
|
|---|
| 57 | /** Get list of categories. */
|
|---|
| 58 | int categ_dir_get_categories(categ_dir_t *cdir, category_id_t *id_buf,
|
|---|
| 59 | size_t buf_size, size_t *act_size)
|
|---|
| 60 | {
|
|---|
| 61 | size_t act_cnt;
|
|---|
| 62 | size_t buf_cnt;
|
|---|
| 63 |
|
|---|
| 64 | assert(fibril_mutex_is_locked(&cdir->mutex));
|
|---|
| 65 |
|
|---|
| 66 | buf_cnt = buf_size / sizeof(category_id_t);
|
|---|
| 67 |
|
|---|
| 68 | act_cnt = list_count(&cdir->categories);
|
|---|
| 69 | *act_size = act_cnt * sizeof(category_id_t);
|
|---|
| 70 |
|
|---|
| 71 | if (buf_size % sizeof(category_id_t) != 0)
|
|---|
| 72 | return EINVAL;
|
|---|
| 73 |
|
|---|
| 74 | size_t pos = 0;
|
|---|
| 75 | list_foreach(cdir->categories, item) {
|
|---|
| 76 | category_t *cat =
|
|---|
| 77 | list_get_instance(item, category_t, cat_list);
|
|---|
| 78 |
|
|---|
| 79 | if (pos < buf_cnt)
|
|---|
| 80 | id_buf[pos] = cat->id;
|
|---|
| 81 | pos++;
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 | return EOK;
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|
| 87 |
|
|---|
| 88 | /** Initialize category structure. */
|
|---|
| 89 | static void category_init(category_t *cat, const char *name)
|
|---|
| 90 | {
|
|---|
| 91 | fibril_mutex_initialize(&cat->mutex);
|
|---|
| 92 | cat->name = str_dup(name);
|
|---|
| 93 | cat->id = loc_create_id();
|
|---|
| 94 | link_initialize(&cat->cat_list);
|
|---|
| 95 | list_initialize(&cat->services);
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | /** Allocate new category. */
|
|---|
| 99 | category_t *category_new(const char *name)
|
|---|
| 100 | {
|
|---|
| 101 | category_t *cat;
|
|---|
| 102 |
|
|---|
| 103 | cat = malloc(sizeof(category_t));
|
|---|
| 104 | if (cat == NULL)
|
|---|
| 105 | return NULL;
|
|---|
| 106 |
|
|---|
| 107 | category_init(cat, name);
|
|---|
| 108 | return cat;
|
|---|
| 109 | }
|
|---|
| 110 |
|
|---|
| 111 | /** Add service to category. */
|
|---|
| 112 | int category_add_service(category_t *cat, loc_service_t *svc)
|
|---|
| 113 | {
|
|---|
| 114 | assert(fibril_mutex_is_locked(&cat->mutex));
|
|---|
| 115 |
|
|---|
| 116 | /* Verify that category does not contain this service yet. */
|
|---|
| 117 | list_foreach(cat->services, item) {
|
|---|
| 118 |
|
|---|
| 119 | loc_service_t *csvc = list_get_instance(item, loc_service_t,
|
|---|
| 120 | cat_services);
|
|---|
| 121 | if (csvc == svc) {
|
|---|
| 122 | return EEXIST;
|
|---|
| 123 | }
|
|---|
| 124 | }
|
|---|
| 125 |
|
|---|
| 126 | list_append(&svc->cat_services, &cat->services);
|
|---|
| 127 | return EOK;
|
|---|
| 128 | }
|
|---|
| 129 |
|
|---|
| 130 | /** Get category by ID. */
|
|---|
| 131 | category_t *category_get(categ_dir_t *cdir, catid_t catid)
|
|---|
| 132 | {
|
|---|
| 133 | assert(fibril_mutex_is_locked(&cdir->mutex));
|
|---|
| 134 |
|
|---|
| 135 | list_foreach(cdir->categories, item) {
|
|---|
| 136 | category_t *cat = list_get_instance(item, category_t,
|
|---|
| 137 | cat_list);
|
|---|
| 138 | if (cat->id == catid)
|
|---|
| 139 | return cat;
|
|---|
| 140 | }
|
|---|
| 141 |
|
|---|
| 142 | return NULL;
|
|---|
| 143 | }
|
|---|
| 144 |
|
|---|
| 145 | /** Find category by name. */
|
|---|
| 146 | category_t *category_find_by_name(categ_dir_t *cdir, const char *name)
|
|---|
| 147 | {
|
|---|
| 148 | assert(fibril_mutex_is_locked(&cdir->mutex));
|
|---|
| 149 |
|
|---|
| 150 | list_foreach(cdir->categories, item) {
|
|---|
| 151 | category_t *cat = list_get_instance(item, category_t,
|
|---|
| 152 | cat_list);
|
|---|
| 153 | if (str_cmp(cat->name, name) == 0)
|
|---|
| 154 | return cat;
|
|---|
| 155 | }
|
|---|
| 156 |
|
|---|
| 157 | return NULL;
|
|---|
| 158 | }
|
|---|
| 159 |
|
|---|
| 160 | /** Get list of services in category. */
|
|---|
| 161 | int category_get_services(category_t *cat, service_id_t *id_buf,
|
|---|
| 162 | size_t buf_size, size_t *act_size)
|
|---|
| 163 | {
|
|---|
| 164 | size_t act_cnt;
|
|---|
| 165 | size_t buf_cnt;
|
|---|
| 166 |
|
|---|
| 167 | assert(fibril_mutex_is_locked(&cat->mutex));
|
|---|
| 168 |
|
|---|
| 169 | buf_cnt = buf_size / sizeof(service_id_t);
|
|---|
| 170 |
|
|---|
| 171 | act_cnt = list_count(&cat->services);
|
|---|
| 172 | *act_size = act_cnt * sizeof(service_id_t);
|
|---|
| 173 |
|
|---|
| 174 | if (buf_size % sizeof(service_id_t) != 0)
|
|---|
| 175 | return EINVAL;
|
|---|
| 176 |
|
|---|
| 177 | size_t pos = 0;
|
|---|
| 178 | list_foreach(cat->services, item) {
|
|---|
| 179 | loc_service_t *svc =
|
|---|
| 180 | list_get_instance(item, loc_service_t, cat_services);
|
|---|
| 181 |
|
|---|
| 182 | if (pos < buf_cnt)
|
|---|
| 183 | id_buf[pos] = svc->id;
|
|---|
| 184 | pos++;
|
|---|
| 185 | }
|
|---|
| 186 |
|
|---|
| 187 | return EOK;
|
|---|
| 188 | }
|
|---|
| 189 |
|
|---|
| 190 | /**
|
|---|
| 191 | * @}
|
|---|
| 192 | */
|
|---|