2 * Copyright (C) 2016, 2017 "IoT.bzh"
3 * Author "Fulup Ar Foll"
4 * Author José Bollo <jose.bollo@iot.bzh>
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
26 #include "afb-session.h"
29 #include "afb-apiset.h"
30 #include "afb-context.h"
34 #include <afb/afb-req-itf.h>
36 #define INCR 8 /* CAUTION: must be a power of 2 */
39 * Internal description of an api
44 const char *name; /**< name of the api */
45 struct afb_api api; /**< handler of the api */
49 * Data structure for apiset
53 struct api_desc *apis; /**< description of apis */
54 struct afb_apiset *subset; /**< subset if any */
55 int count; /**< count of apis in the set */
56 int timeout; /**< the timeout in second for the apiset */
57 int refcount; /**< reference count for freeing resources */
58 char name[1]; /**< name of the apiset */
62 * Search the api of 'name'.
63 * @param set the api set
64 * @param name the api name to search
65 * @return the descriptor if found or NULL otherwise
67 static struct api_desc *search(struct afb_apiset *set, const char *name)
72 /* dichotomic search of the api */
77 /* check remaining slice */
82 /* check the mid of the slice */
85 c = strcasecmp(a->name, name);
90 /* update the slice */
99 * Increases the count of references to the apiset and return its address
100 * @param set the set whose reference count is to be increased
101 * @return the given apiset
103 struct afb_apiset *afb_apiset_addref(struct afb_apiset *set)
106 __atomic_add_fetch(&set->refcount, 1, __ATOMIC_RELAXED);
111 * Decreases the count of references to the apiset and frees its
112 * resources when no more references exists.
113 * @param set the set to unrefrence
115 void afb_apiset_unref(struct afb_apiset *set)
117 if (set && !__atomic_sub_fetch(&set->refcount, 1, __ATOMIC_RELAXED)) {
118 afb_apiset_unref(set->subset);
126 * @param name the name of the apiset
127 * @param timeout the default timeout in seconds for the apiset
128 * @return the created apiset or NULL in case of error
130 struct afb_apiset *afb_apiset_create(const char *name, int timeout)
132 struct afb_apiset *set;
134 set = malloc((name ? strlen(name) : 0) + sizeof *set);
136 set->apis = malloc(INCR * sizeof *set->apis);
138 set->timeout = timeout;
142 strcpy(set->name, name);
150 * the name of the apiset
151 * @param set the api set
152 * @return the name of the set
154 const char *afb_apiset_name(struct afb_apiset *set)
160 * Get the API timeout of the set
161 * @param set the api set
162 * @return the timeout in seconds
164 int afb_apiset_timeout_get(struct afb_apiset *set)
170 * Set the API timeout of the set
171 * @param set the api set
172 * @param to the timeout in seconds
174 void afb_apiset_timeout_set(struct afb_apiset *set, int to)
180 * Get the subset of the set
181 * @param set the api set
182 * @return the subset of set
184 struct afb_apiset *afb_apiset_subset_get(struct afb_apiset *set)
190 * Set the subset of the set
191 * @param set the api set
192 * @param subset the subset to set
194 void afb_apiset_subset_set(struct afb_apiset *set, struct afb_apiset *subset)
196 struct afb_apiset *tmp;
198 /* avoid infinite loop */
202 set->subset = afb_apiset_addref(subset);
203 afb_apiset_unref(tmp);
207 * Adds the api of 'name' described by 'api'.
208 * @param set the api set
209 * @param name the name of the api to add (have to survive, not copied!)
211 * @returns 0 in case of success or -1 in case
212 * of error with errno set:
213 * - EEXIST if name already registered
214 * - ENOMEM when out of memory
216 int afb_apiset_add(struct afb_apiset *set, const char *name, struct afb_api api)
218 struct api_desc *apis;
221 /* check previously existing plugin */
222 for (i = 0 ; i < set->count ; i++) {
223 c = strcasecmp(set->apis[i].name, name);
225 ERROR("api of name %s already exists", name);
233 /* allocates enough memory */
234 c = (set->count + INCR) & ~(INCR - 1);
235 apis = realloc(set->apis, ((unsigned)c) * sizeof * apis);
237 ERROR("out of memory");
243 /* copy higher part of the array */
246 memmove(apis + 1, apis, ((unsigned)(set->count - i)) * sizeof *apis);
248 /* record the plugin */
254 NOTICE("API %s added", name);
263 * Delete from the 'set' the api of 'name'.
264 * @param set the set to be changed
265 * @param name the name of the API to remove
266 * @return 0 in case of success or -1 in case where the API doesn't exist.
268 int afb_apiset_del(struct afb_apiset *set, const char *name)
273 for (i = 0 ; i < set->count ; i++) {
274 c = strcasecmp(set->apis[i].name, name);
277 while(i < set->count) {
278 set->apis[i] = set->apis[i + 1];
291 * Get from the 'set' the API of 'name' in 'api'
292 * @param set the set of API
293 * @param name the name of the API to get
294 * @param api the structure where to store data about the API of name
295 * @return 0 in case of success or -1 in case of error
297 int afb_apiset_lookup(struct afb_apiset *set, const char *name, struct afb_api *api)
299 const struct api_desc *i;
301 i = search(set, name);
312 * Get from the 'set' the API of 'name' in 'api' with fallback to subset or default api
313 * @param set the set of API
314 * @param name the name of the API to get
315 * @param api the structure where to store data about the API of name
316 * @return 0 in case of success or -1 in case of error
318 static struct api_desc *get_api(struct afb_apiset *set, const char *name)
320 struct api_desc *i = search(set, name);
321 return i || !set->subset ? i : get_api(set->subset, name);
325 * Get from the 'set' the API of 'name' in 'api' with fallback to subset or default api
326 * @param set the set of API
327 * @param name the name of the API to get
328 * @param api the structure where to store data about the API of name
329 * @return 0 in case of success or -1 in case of error
331 int afb_apiset_get(struct afb_apiset *set, const char *name, struct afb_api *api)
333 const struct api_desc *i;
335 i = get_api(set, name);
345 * Starts the service 'api'.
347 * @param share_session if true start the servic"e in a shared session
348 * if false start it in its own session
349 * @param onneed if true start the service if possible, if false the api
351 * @return a positive number on success
353 static int start_api(struct afb_apiset *set, struct api_desc *api, int share_session, int onneed)
357 if (api->status == 0)
359 else if (api->status > 0) {
364 NOTICE("API %s starting...", api->name);
365 if (api->api.itf->service_start) {
367 rc = api->api.itf->service_start(api->api.closure, share_session, onneed, set);
369 api->status = errno ?: ECANCELED;
370 ERROR("The api %s failed to start (%d)", api->name, rc);
373 } else if (!onneed) {
374 /* already started: it is an error */
375 ERROR("The api %s is not a startable service", api->name);
376 api->status = EINVAL;
379 NOTICE("API %s started", api->name);
385 * Get from the 'set' the API of 'name' in 'api' with fallback to subset or default api
386 * @param set the set of API
387 * @param name the name of the API to get
388 * @param api the structure where to store data about the API of name
389 * @return 0 in case of success or -1 in case of error
391 int afb_apiset_get_started(struct afb_apiset *set, const char *name, struct afb_api *api)
395 i = get_api(set, name);
401 return i->status ? start_api(set, i, 1, 1) : 0;
405 * Starts a service by its 'api' name.
406 * @param set the api set
407 * @param name name of the service to start
408 * @param share_session if true start the servic"e in a shared session
409 * if false start it in its own session
410 * @param onneed if true start the service if possible, if false the api
412 * @return a positive number on success
414 int afb_apiset_start_service(struct afb_apiset *set, const char *name, int share_session, int onneed)
418 a = search(set, name);
420 ERROR("can't find service %s", name);
425 return start_api(set, a, share_session, onneed);
429 * Starts all possible services but stops at first error.
430 * @param set the api set
431 * @param share_session if true start the servic"e in a shared session
432 * if false start it in its own session
433 * @return 0 on success or a negative number when an error is found
435 int afb_apiset_start_all_services(struct afb_apiset *set, int share_session)
438 struct api_desc *i, *e;
441 e = &set->apis[set->count];
443 rc = start_api(set, i, share_session, 1);
449 return set->subset ? afb_apiset_start_all_services(set->subset, share_session) : 0;
453 * Ask to update the hook flags of the 'api'
454 * @param set the api set
455 * @param name the api to update (NULL updates all)
457 void afb_apiset_update_hooks(struct afb_apiset *set, const char *name)
459 const struct api_desc *i, *e;
463 e = &set->apis[set->count];
465 i = search(set, name);
469 if (i->api.itf->update_hooks)
470 i->api.itf->update_hooks(i->api.closure);
476 * Set the verbosity level of the 'api'
477 * @param set the api set
478 * @param name the api to set (NULL set all)
480 void afb_apiset_set_verbosity(struct afb_apiset *set, const char *name, int level)
482 const struct api_desc *i, *e;
486 e = &set->apis[set->count];
488 i = search(set, name);
492 if (i->api.itf->set_verbosity)
493 i->api.itf->set_verbosity(i->api.closure, level);
499 * Set the verbosity level of the 'api'
500 * @param set the api set
501 * @param name the api to set (NULL set all)
503 int afb_apiset_get_verbosity(struct afb_apiset *set, const char *name)
505 const struct api_desc *i;
507 i = name ? search(set, name) : NULL;
512 if (!i->api.itf->get_verbosity)
515 return i->api.itf->get_verbosity(i->api.closure);
519 * Get the list of api names
520 * @param set the api set
521 * @return a NULL terminated array of api names. Must be freed.
523 const char **afb_apiset_get_names(struct afb_apiset *set)
530 size = set->count * (1 + sizeof(*names)) + sizeof(*names);
531 for (i = 0 ; i < set->count ; i++)
532 size += strlen(set->apis[i].name);
534 names = malloc(size);
538 dest = (void*)&names[set->count+1];
539 for (i = 0 ; i < set->count ; i++) {
541 dest = stpcpy(dest, set->apis[i].name) + 1;
549 * Enumerate the api names to a callback.
550 * @param set the api set
551 * @param callback the function to call for each name
552 * @param closure the closure for the callback
554 void afb_apiset_enum(struct afb_apiset *set, void (*callback)(struct afb_apiset *set, const char *name, void *closure), void *closure)
558 for (i = 0 ; i < set->count ; i++)
559 callback(set, set->apis[i].name, closure);