[cc574511] | 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 |
|
---|
[4122410] | 29 | /** @addtogroup locsrv
|
---|
[cc574511] | 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"
|
---|
[32d96e1] | 42 | #include "locsrv.h"
|
---|
[cc574511] | 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 |
|
---|
[278ac72] | 57 | /** Get list of categories. */
|
---|
[b7fd2a0] | 58 | errno_t categ_dir_get_categories(categ_dir_t *cdir, category_id_t *id_buf,
|
---|
[278ac72] | 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;
|
---|
[feeac0d] | 75 | list_foreach(cdir->categories, cat_list, category_t, cat) {
|
---|
[278ac72] | 76 | if (pos < buf_cnt)
|
---|
| 77 | id_buf[pos] = cat->id;
|
---|
| 78 | pos++;
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | return EOK;
|
---|
| 82 | }
|
---|
| 83 |
|
---|
[cc574511] | 84 | /** Initialize category structure. */
|
---|
| 85 | static void category_init(category_t *cat, const char *name)
|
---|
| 86 | {
|
---|
| 87 | fibril_mutex_initialize(&cat->mutex);
|
---|
| 88 | cat->name = str_dup(name);
|
---|
| 89 | cat->id = loc_create_id();
|
---|
| 90 | link_initialize(&cat->cat_list);
|
---|
[d0dd7b5] | 91 | list_initialize(&cat->svc_memb);
|
---|
[cc574511] | 92 | }
|
---|
| 93 |
|
---|
| 94 | /** Allocate new category. */
|
---|
| 95 | category_t *category_new(const char *name)
|
---|
| 96 | {
|
---|
| 97 | category_t *cat;
|
---|
| 98 |
|
---|
| 99 | cat = malloc(sizeof(category_t));
|
---|
| 100 | if (cat == NULL)
|
---|
| 101 | return NULL;
|
---|
| 102 |
|
---|
| 103 | category_init(cat, name);
|
---|
| 104 | return cat;
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 | /** Add service to category. */
|
---|
[b7fd2a0] | 108 | errno_t category_add_service(category_t *cat, loc_service_t *svc)
|
---|
[cc574511] | 109 | {
|
---|
| 110 | assert(fibril_mutex_is_locked(&cat->mutex));
|
---|
[d0dd7b5] | 111 | assert(fibril_mutex_is_locked(&services_list_mutex));
|
---|
[cc574511] | 112 |
|
---|
| 113 | /* Verify that category does not contain this service yet. */
|
---|
[feeac0d] | 114 | list_foreach(cat->svc_memb, cat_link, svc_categ_t, memb) {
|
---|
[d0dd7b5] | 115 | if (memb->svc == svc) {
|
---|
[cc574511] | 116 | return EEXIST;
|
---|
| 117 | }
|
---|
| 118 | }
|
---|
| 119 |
|
---|
[d0dd7b5] | 120 | svc_categ_t *nmemb = malloc(sizeof(svc_categ_t));
|
---|
| 121 | if (nmemb == NULL)
|
---|
| 122 | return ENOMEM;
|
---|
| 123 |
|
---|
| 124 | nmemb->svc = svc;
|
---|
| 125 | nmemb->cat = cat;
|
---|
| 126 |
|
---|
| 127 | list_append(&nmemb->cat_link, &cat->svc_memb);
|
---|
| 128 | list_append(&nmemb->svc_link, &svc->cat_memb);
|
---|
| 129 |
|
---|
[cc574511] | 130 | return EOK;
|
---|
| 131 | }
|
---|
| 132 |
|
---|
[d0dd7b5] | 133 | /** Remove service from category. */
|
---|
| 134 | void category_remove_service(svc_categ_t *memb)
|
---|
| 135 | {
|
---|
| 136 | assert(fibril_mutex_is_locked(&memb->cat->mutex));
|
---|
| 137 | assert(fibril_mutex_is_locked(&services_list_mutex));
|
---|
| 138 |
|
---|
| 139 | list_remove(&memb->cat_link);
|
---|
| 140 | list_remove(&memb->svc_link);
|
---|
| 141 |
|
---|
| 142 | free(memb);
|
---|
| 143 | }
|
---|
| 144 |
|
---|
[cc574511] | 145 | /** Get category by ID. */
|
---|
| 146 | category_t *category_get(categ_dir_t *cdir, catid_t catid)
|
---|
| 147 | {
|
---|
| 148 | assert(fibril_mutex_is_locked(&cdir->mutex));
|
---|
| 149 |
|
---|
[feeac0d] | 150 | list_foreach(cdir->categories, cat_list, category_t, cat) {
|
---|
[cc574511] | 151 | if (cat->id == catid)
|
---|
| 152 | return cat;
|
---|
| 153 | }
|
---|
| 154 |
|
---|
| 155 | return NULL;
|
---|
| 156 | }
|
---|
| 157 |
|
---|
| 158 | /** Find category by name. */
|
---|
| 159 | category_t *category_find_by_name(categ_dir_t *cdir, const char *name)
|
---|
| 160 | {
|
---|
| 161 | assert(fibril_mutex_is_locked(&cdir->mutex));
|
---|
| 162 |
|
---|
[feeac0d] | 163 | list_foreach(cdir->categories, cat_list, category_t, cat) {
|
---|
[cc574511] | 164 | if (str_cmp(cat->name, name) == 0)
|
---|
| 165 | return cat;
|
---|
| 166 | }
|
---|
| 167 |
|
---|
| 168 | return NULL;
|
---|
| 169 | }
|
---|
| 170 |
|
---|
| 171 | /** Get list of services in category. */
|
---|
[b7fd2a0] | 172 | errno_t category_get_services(category_t *cat, service_id_t *id_buf,
|
---|
[cc574511] | 173 | size_t buf_size, size_t *act_size)
|
---|
| 174 | {
|
---|
| 175 | size_t act_cnt;
|
---|
| 176 | size_t buf_cnt;
|
---|
| 177 |
|
---|
| 178 | assert(fibril_mutex_is_locked(&cat->mutex));
|
---|
| 179 |
|
---|
| 180 | buf_cnt = buf_size / sizeof(service_id_t);
|
---|
| 181 |
|
---|
[d0dd7b5] | 182 | act_cnt = list_count(&cat->svc_memb);
|
---|
[cc574511] | 183 | *act_size = act_cnt * sizeof(service_id_t);
|
---|
| 184 |
|
---|
| 185 | if (buf_size % sizeof(service_id_t) != 0)
|
---|
| 186 | return EINVAL;
|
---|
| 187 |
|
---|
| 188 | size_t pos = 0;
|
---|
[feeac0d] | 189 | list_foreach(cat->svc_memb, cat_link, svc_categ_t, memb) {
|
---|
[cc574511] | 190 | if (pos < buf_cnt)
|
---|
[d0dd7b5] | 191 | id_buf[pos] = memb->svc->id;
|
---|
[cc574511] | 192 | pos++;
|
---|
| 193 | }
|
---|
| 194 |
|
---|
| 195 | return EOK;
|
---|
| 196 | }
|
---|
| 197 |
|
---|
| 198 | /**
|
---|
| 199 | * @}
|
---|
| 200 | */
|
---|