561b2752bf20cbeb5d1131c81f7569bb5552f5ed
[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  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  * 
19  * Contain all generic part to handle REST/API
20  * 
21  *  https://www.gnu.org/software/libmicrohttpd/tutorial.html [search 'largepost.c']
22  */
23
24 #define _GNU_SOURCE
25
26 #include <stdio.h>
27 #include <assert.h>
28 #include <string.h>
29 #include <dirent.h>
30 #include <dlfcn.h>
31 #include <unistd.h>
32 #include <limits.h>
33 #include <sys/types.h>
34 #include <sys/stat.h>
35 #include <signal.h>
36 #include <time.h>
37 #include <sys/syscall.h>
38 #include <setjmp.h>
39
40 #include "afb-plugin.h"
41 #include "afb-req-itf.h"
42 #include "afb-poll-itf.h"
43
44 #include "session.h"
45 #include "afb-apis.h"
46 #include "verbose.h"
47 #include "utils-upoll.h"
48
49 struct api_desc {
50         struct afb_api api;
51         const char *name;
52         size_t namelen;
53 };
54
55 static struct api_desc *apis_array = NULL;
56 static int apis_count = 0;
57
58 int afb_apis_count()
59 {
60         return apis_count;
61 }
62
63 void afb_apis_free_context(int apiidx, void *context)
64 {
65         const struct afb_api *api;
66         api = &apis_array[apiidx].api;
67         api->free_context(api->closure, context);
68 }
69
70 int afb_apis_add(const char *name, struct afb_api api)
71 {
72         struct api_desc *apis;
73         size_t len;
74         int i;
75
76         /* check existing or not */
77         len = strlen(name);
78         if (len == 0) {
79                 fprintf(stderr, "empty api name forbidden\n");
80                 goto error;
81         }
82
83         /* check previously existing plugin */
84         for (i = 0 ; i < apis_count ; i++) {
85                 if (!strcasecmp(apis_array[i].name, name)) {
86                         fprintf(stderr, "ERROR: api of name %s already exists\n", name);
87                         goto error;
88                 }
89         }
90
91         /* allocates enough memory */
92         apis = realloc(apis_array, ((unsigned)apis_count + 1) * sizeof * apis);
93         if (apis == NULL) {
94                 fprintf(stderr, "out of memory\n");
95                 goto error;
96         }
97         apis_array = apis;
98
99         /* record the plugin */
100         apis = &apis_array[apis_count];
101         apis->api = api;
102         apis->namelen = len;
103         apis->name = name;
104         apis_count++;
105
106         return 0;
107
108 error:
109         return -1;
110 }
111
112 void afb_apis_call(struct afb_req req, struct AFB_clientCtx *context, const char *api, size_t lenapi, const char *verb, size_t lenverb)
113 {
114         int i;
115         const struct api_desc *a;
116
117         a = apis_array;
118         for (i = 0 ; i < apis_count ; i++, a++) {
119                 if (a->namelen == lenapi && !strncasecmp(a->name, api, lenapi)) {
120                         req.context = &context->contexts[i];
121                         a->api.call(a->api.closure, req, verb, lenverb);
122                         return;
123                 }
124         }
125         afb_req_fail(req, "fail", "api not found");
126 }
127