refactoring context handling
[src/app-framework-binder.git] / src / afb-apis.c
1 /*
2  * Copyright (C) 2016 "IoT.bzh"
3  * Author "Fulup Ar Foll"
4  * Author José Bollo <jose.bollo@iot.bzh>
5  *
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
9  *
10  *   http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  */
18
19 #define _GNU_SOURCE
20
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24
25 #include "session.h"
26 #include "verbose.h"
27 #include "afb-apis.h"
28 #include "afb-req-itf.h"
29
30 struct api_desc {
31         struct afb_api api;
32         const char *name;
33         size_t namelen;
34 };
35
36 static struct api_desc *apis_array = NULL;
37 static int apis_count = 0;
38
39 int afb_apis_count()
40 {
41         return apis_count;
42 }
43
44 int afb_apis_add(const char *name, struct afb_api api)
45 {
46         struct api_desc *apis;
47         size_t len;
48         int i;
49
50         /* check existing or not */
51         len = strlen(name);
52         if (len == 0) {
53                 fprintf(stderr, "empty api name forbidden\n");
54                 goto error;
55         }
56
57         /* check previously existing plugin */
58         for (i = 0 ; i < apis_count ; i++) {
59                 if (!strcasecmp(apis_array[i].name, name)) {
60                         fprintf(stderr, "ERROR: api of name %s already exists\n", name);
61                         goto error;
62                 }
63         }
64
65         /* allocates enough memory */
66         apis = realloc(apis_array, ((unsigned)apis_count + 1) * sizeof * apis);
67         if (apis == NULL) {
68                 fprintf(stderr, "out of memory\n");
69                 goto error;
70         }
71         apis_array = apis;
72
73         /* record the plugin */
74         apis = &apis_array[apis_count];
75         apis->api = api;
76         apis->namelen = len;
77         apis->name = name;
78         apis_count++;
79
80         return 0;
81
82 error:
83         return -1;
84 }
85
86 void afb_apis_call(struct afb_req req, struct AFB_clientCtx *context, const char *api, size_t lenapi, const char *verb, size_t lenverb)
87 {
88         int i;
89         const struct api_desc *a;
90
91         a = apis_array;
92         for (i = 0 ; i < apis_count ; i++, a++) {
93                 if (a->namelen == lenapi && !strncasecmp(a->name, api, lenapi)) {
94                         req.ctx_closure = &context->contexts[i];
95                         a->api.call(a->api.closure, req, verb, lenverb);
96                         return;
97                 }
98         }
99         afb_req_fail(req, "fail", "api not found");
100 }
101