0af3b42cd4a36c0a54d4e71fe7ee842f395d92ab
[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 void afb_apis_free_context(int apiidx, void *context)
45 {
46         const struct afb_api *api;
47         api = &apis_array[apiidx].api;
48         api->free_context(api->closure, context);
49 }
50
51 int afb_apis_add(const char *name, struct afb_api api)
52 {
53         struct api_desc *apis;
54         size_t len;
55         int i;
56
57         /* check existing or not */
58         len = strlen(name);
59         if (len == 0) {
60                 fprintf(stderr, "empty api name forbidden\n");
61                 goto error;
62         }
63
64         /* check previously existing plugin */
65         for (i = 0 ; i < apis_count ; i++) {
66                 if (!strcasecmp(apis_array[i].name, name)) {
67                         fprintf(stderr, "ERROR: api of name %s already exists\n", name);
68                         goto error;
69                 }
70         }
71
72         /* allocates enough memory */
73         apis = realloc(apis_array, ((unsigned)apis_count + 1) * sizeof * apis);
74         if (apis == NULL) {
75                 fprintf(stderr, "out of memory\n");
76                 goto error;
77         }
78         apis_array = apis;
79
80         /* record the plugin */
81         apis = &apis_array[apis_count];
82         apis->api = api;
83         apis->namelen = len;
84         apis->name = name;
85         apis_count++;
86
87         return 0;
88
89 error:
90         return -1;
91 }
92
93 void afb_apis_call(struct afb_req req, struct AFB_clientCtx *context, const char *api, size_t lenapi, const char *verb, size_t lenverb)
94 {
95         int i;
96         const struct api_desc *a;
97
98         a = apis_array;
99         for (i = 0 ; i < apis_count ; i++, a++) {
100                 if (a->namelen == lenapi && !strncasecmp(a->name, api, lenapi)) {
101                         req.context = &context->contexts[i];
102                         a->api.call(a->api.closure, req, verb, lenverb);
103                         return;
104                 }
105         }
106         afb_req_fail(req, "fail", "api not found");
107 }
108