source: mainline/uspace/srv/locsrv/category.c@ 69d25e2

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 69d25e2 was feeac0d, checked in by Jiri Svoboda <jiri@…>, 12 years ago

Simplify use of list_foreach.

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