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