1cc0648a089e9f920fa625358907c5711c607379
[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 <stdio.h>
22 #include <assert.h>
23 #include <string.h>
24 #include <dirent.h>
25 #include <dlfcn.h>
26 #include <unistd.h>
27 #include <limits.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <signal.h>
31 #include <time.h>
32 #include <sys/syscall.h>
33 #include <setjmp.h>
34
35 #include "afb-plugin.h"
36 #include "afb-req-itf.h"
37 #include "afb-poll-itf.h"
38
39 #include "session.h"
40 #include "afb-apis.h"
41 #include "verbose.h"
42 #include "utils-upoll.h"
43
44 struct api_desc {
45         struct afb_api api;
46         const char *name;
47         size_t namelen;
48 };
49
50 static struct api_desc *apis_array = NULL;
51 static int apis_count = 0;
52
53 int afb_apis_count()
54 {
55         return apis_count;
56 }
57
58 void afb_apis_free_context(int apiidx, void *context)
59 {
60         const struct afb_api *api;
61         api = &apis_array[apiidx].api;
62         api->free_context(api->closure, context);
63 }
64
65 int afb_apis_add(const char *name, struct afb_api api)
66 {
67         struct api_desc *apis;
68         size_t len;
69         int i;
70
71         /* check existing or not */
72         len = strlen(name);
73         if (len == 0) {
74                 fprintf(stderr, "empty api name forbidden\n");
75                 goto error;
76         }
77
78         /* check previously existing plugin */
79         for (i = 0 ; i < apis_count ; i++) {
80                 if (!strcasecmp(apis_array[i].name, name)) {
81                         fprintf(stderr, "ERROR: api of name %s already exists\n", name);
82                         goto error;
83                 }
84         }
85
86         /* allocates enough memory */
87         apis = realloc(apis_array, ((unsigned)apis_count + 1) * sizeof * apis);
88         if (apis == NULL) {
89                 fprintf(stderr, "out of memory\n");
90                 goto error;
91         }
92         apis_array = apis;
93
94         /* record the plugin */
95         apis = &apis_array[apis_count];
96         apis->api = api;
97         apis->namelen = len;
98         apis->name = name;
99         apis_count++;
100
101         return 0;
102
103 error:
104         return -1;
105 }
106
107 void afb_apis_call(struct afb_req req, struct AFB_clientCtx *context, const char *api, size_t lenapi, const char *verb, size_t lenverb)
108 {
109         int i;
110         const struct api_desc *a;
111
112         a = apis_array;
113         for (i = 0 ; i < apis_count ; i++, a++) {
114                 if (a->namelen == lenapi && !strncasecmp(a->name, api, lenapi)) {
115                         req.context = &context->contexts[i];
116                         a->api.call(a->api.closure, req, verb, lenverb);
117                         return;
118                 }
119         }
120         afb_req_fail(req, "fail", "api not found");
121 }
122