2f61fe9547bb945ad2eca933228cb78561fa0900
[src/app-framework-binder.git] / src / afb-apis.c
1 /*
2  * Copyright (C) 2016, 2017 "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 #include <errno.h>
25
26 #include "afb-session.h"
27 #include "verbose.h"
28 #include "afb-apis.h"
29 #include "afb-context.h"
30 #include "afb-hook.h"
31 #include "afb-xreq.h"
32 #include "jobs.h"
33
34 #include <afb/afb-req-itf.h>
35
36 /**
37  * Internal description of an api
38  */
39 struct api_desc {
40         const char *name;       /**< name of the api */
41         struct afb_api api;     /**< handler of the api */
42 };
43
44 static struct api_desc *apis_array = NULL;
45 static int apis_count = 0;
46 static int apis_timeout = 15;
47
48 /**
49  * Set the API timeout
50  * @param to the timeout in seconds
51  */
52 void afb_apis_set_timeout(int to)
53 {
54         apis_timeout = to;
55 }
56
57 /**
58  * Checks wether 'name' is a valid API name.
59  * @return 1 if valid, 0 otherwise
60  */
61 int afb_apis_is_valid_api_name(const char *name)
62 {
63         unsigned char c;
64
65         c = (unsigned char)*name;
66         if (c == 0)
67                 /* empty names aren't valid */
68                 return 0;
69
70         do {
71                 if (c < (unsigned char)'\x80') {
72                         switch(c) {
73                         default:
74                                 if (c > ' ')
75                                         break;
76                         case '"':
77                         case '#':
78                         case '%':
79                         case '&':
80                         case '\'':
81                         case '/':
82                         case '?':
83                         case '`':
84                         case '\\':
85                         case '\x7f':
86                                 return 0;
87                         }
88                 }
89                 c = (unsigned char)*++name;
90         } while(c != 0);
91         return 1;
92 }
93
94 /**
95  * Adds the api of 'name' described by 'api'.
96  * @param name the name of the api to add (have to survive, not copied!)
97  * @param api the api
98  * @returns 0 in case of success or -1 in case
99  * of error with errno set:
100  *   - EINVAL if name isn't valid
101  *   - EEXIST if name already registered
102  *   - ENOMEM when out of memory
103  */
104 int afb_apis_add(const char *name, struct afb_api api)
105 {
106         struct api_desc *apis;
107         int i, c;
108
109         /* Checks the api name */
110         if (!afb_apis_is_valid_api_name(name)) {
111                 ERROR("invalid api name forbidden (name is '%s')", name);
112                 errno = EINVAL;
113                 goto error;
114         }
115
116         /* check previously existing plugin */
117         for (i = 0 ; i < apis_count ; i++) {
118                 c = strcasecmp(apis_array[i].name, name);
119                 if (c == 0) {
120                         ERROR("api of name %s already exists", name);
121                         errno = EEXIST;
122                         goto error;
123                 }
124                 if (c > 0)
125                         break;
126         }
127
128         /* allocates enough memory */
129         apis = realloc(apis_array, ((unsigned)apis_count + 1) * sizeof * apis);
130         if (apis == NULL) {
131                 ERROR("out of memory");
132                 errno = ENOMEM;
133                 goto error;
134         }
135         apis_array = apis;
136
137         /* copy higher part of the array */
138         c = apis_count;
139         while (c > i) {
140                 apis_array[c] = apis_array[c - 1];
141                 c--;
142         }
143
144         /* record the plugin */
145         apis = &apis_array[i];
146         apis->api = api;
147         apis->name = name;
148         apis_count++;
149
150         NOTICE("API %s added", name);
151
152         return 0;
153
154 error:
155         return -1;
156 }
157
158 /**
159  * Search the 'api'.
160  * @param api the api of the verb
161  * @return the descriptor if found or NULL otherwise
162  */
163 static const struct api_desc *search(const char *api)
164 {
165         int i, c, up, lo;
166         const struct api_desc *a;
167
168         /* dichotomic search of the api */
169         /* initial slice */
170         lo = 0;
171         up = apis_count;
172         for (;;) {
173                 /* check remaining slice */
174                 if (lo >= up) {
175                         /* not found */
176                         return NULL;
177                 }
178                 /* check the mid of the slice */
179                 i = (lo + up) >> 1;
180                 a = &apis_array[i];
181                 c = strcasecmp(a->name, api);
182                 if (c == 0) {
183                         /* found */
184                         return a;
185                 }
186                 /* update the slice */
187                 if (c < 0)
188                         lo = i + 1;
189                 else
190                         up = i;
191         }
192 }
193
194 /**
195  * Starts a service by its 'api' name.
196  * @param api name of the service to start
197  * @param share_session if true start the servic"e in a shared session
198  *                      if false start it in its own session
199  * @param onneed if true start the service if possible, if false the api
200  *               must be a service
201  * @return a positive number on success
202  */
203 int afb_apis_start_service(const char *api, int share_session, int onneed)
204 {
205         int i;
206
207         for (i = 0 ; i < apis_count ; i++) {
208                 if (!strcasecmp(apis_array[i].name, api))
209                         return apis_array[i].api.service_start(apis_array[i].api.closure, share_session, onneed);
210         }
211         ERROR("can't find service %s", api);
212         errno = ENOENT;
213         return -1;
214 }
215
216 /**
217  * Starts all possible services but stops at first error.
218  * @param share_session if true start the servic"e in a shared session
219  *                      if false start it in its own session
220  * @return 0 on success or a negative number when an error is found
221  */
222 int afb_apis_start_all_services(int share_session)
223 {
224         int i, rc;
225
226         for (i = 0 ; i < apis_count ; i++) {
227                 rc = apis_array[i].api.service_start(apis_array[i].api.closure, share_session, 1);
228                 if (rc < 0)
229                         return rc;
230         }
231         return 0;
232 }
233
234
235
236
237
238
239 static void do_call_async(int signum, void *arg)
240 {
241         struct afb_xreq *xreq = arg;
242         const struct api_desc *a;
243
244         if (signum != 0)
245                 afb_xreq_fail_f(xreq, "aborted", "signal %s(%d) caught", strsignal(signum), signum);
246         else {
247                 /* search the api */
248                 a = search(xreq->api);
249                 if (!a)
250                         afb_xreq_fail_f(xreq, "unknown-api", "api %s not found", xreq->api);
251                 else {
252                         xreq->context.api_key = a->api.closure;
253                         a->api.call(a->api.closure, xreq);
254                 }
255         }
256         afb_xreq_unref(xreq);
257 }
258
259 /**
260  * Dispatch the request 'req' with the 'context' to the
261  * method of 'api' and 'verb'.
262  * @param req the request to dispatch
263  * @param context the context of the request
264  * @param api the api of the verb
265  * @param verb the verb within the api
266  */
267 void afb_apis_call(struct afb_xreq *xreq)
268 {
269         int rc;
270
271         /* init hooking the request */
272         // TODO req = afb_hook_req_call(req, context, api, verb);
273
274         afb_xreq_addref(xreq);
275         rc = jobs_queue(NULL, apis_timeout, do_call_async, xreq);
276         if (rc < 0) {
277                 /* TODO: allows or not to proccess it directly as when no threading? (see above) */
278                 ERROR("can't process job with threads: %m");
279                 afb_xreq_fail_f(xreq, "cancelled", "not able to create a job for the task");
280                 afb_xreq_unref(xreq);
281         }
282 }
283