de122b6bdf5434e43158c8de83bf835c31cd6101
[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         NOTICE("API %s added", name);
140
141         return 0;
142
143 error:
144         return -1;
145 }
146
147 /**
148  * Search the 'api'.
149  * @param api the api of the verb
150  * @return the descriptor if found or NULL otherwise
151  */
152 static const struct api_desc *search(const char *api)
153 {
154         int i, c, up, lo;
155         const struct api_desc *a;
156
157         /* dichotomic search of the api */
158         /* initial slice */
159         lo = 0;
160         up = apis_count;
161         for (;;) {
162                 /* check remaining slice */
163                 if (lo >= up) {
164                         /* not found */
165                         return NULL;
166                 }
167                 /* check the mid of the slice */
168                 i = (lo + up) >> 1;
169                 a = &apis_array[i];
170                 c = strcasecmp(a->name, api);
171                 if (c == 0) {
172                         /* found */
173                         return a;
174                 }
175                 /* update the slice */
176                 if (c < 0)
177                         lo = i + 1;
178                 else
179                         up = i;
180         }
181 }
182
183 /**
184  * Starts a service by its 'api' name.
185  * @param api name of the service to start
186  * @param share_session if true start the servic"e in a shared session
187  *                      if false start it in its own session
188  * @param onneed if true start the service if possible, if false the api
189  *               must be a service
190  * @return a positive number on success
191  */
192 int afb_apis_start_service(const char *api, int share_session, int onneed)
193 {
194         int i;
195
196         for (i = 0 ; i < apis_count ; i++) {
197                 if (!strcasecmp(apis_array[i].name, api))
198                         return apis_array[i].api.service_start(apis_array[i].api.closure, share_session, onneed);
199         }
200         ERROR("can't find service %s", api);
201         errno = ENOENT;
202         return -1;
203 }
204
205 /**
206  * Starts all possible services but stops at first error.
207  * @param share_session if true start the servic"e in a shared session
208  *                      if false start it in its own session
209  * @return 0 on success or a negative number when an error is found
210  */
211 int afb_apis_start_all_services(int share_session)
212 {
213         int i, rc;
214
215         for (i = 0 ; i < apis_count ; i++) {
216                 rc = apis_array[i].api.service_start(apis_array[i].api.closure, share_session, 1);
217                 if (rc < 0)
218                         return rc;
219         }
220         return 0;
221 }
222
223 /**
224  * Dispatch the request 'req' with the 'context' to the
225  * method of 'api' and 'verb'.
226  * @param req the request to dispatch
227  * @param context the context of the request
228  * @param api the api of the verb
229  * @param verb the verb within the api
230  */
231 void afb_apis_call(struct afb_xreq *xreq)
232 {
233         const struct api_desc *a;
234
235         /* init hooking the request */
236         // TODO req = afb_hook_req_call(req, context, api, verb);
237
238         /* search the api */
239         a = search(xreq->api);
240         if (!a)
241                 afb_xreq_fail_f(xreq, "unknown-api", "api %s not found", xreq->api);
242         else {
243                 xreq->context.api_key = a->api.closure;
244                 a->api.call(a->api.closure, xreq);
245         }
246 }
247