Implement dichotomic search of APIs
[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/afb-req-itf.h>
32
33 struct api_desc {
34         const char *name;
35         struct afb_api api;
36 };
37
38 static struct api_desc *apis_array = NULL;
39 static int apis_count = 0;
40
41 /**
42  * Returns the current count of APIs
43  */
44 int afb_apis_count()
45 {
46         return apis_count;
47 }
48
49 /**
50  * Checks wether 'name' is a valid API name.
51  * @return 1 if valid, 0 otherwise
52  */
53 int afb_apis_is_valid_api_name(const char *name)
54 {
55         unsigned char c;
56
57         c = (unsigned char)*name;
58         if (c == 0)
59                 /* empty names aren't valid */
60                 return 0;
61
62         do {
63                 if (c < (unsigned char)'\x80') {
64                         switch(c) {
65                         default:
66                                 if (c > ' ')
67                                         break;
68                         case '"':
69                         case '#':
70                         case '%':
71                         case '&':
72                         case '\'':
73                         case '/':
74                         case '?':
75                         case '`':
76                         case '\\':
77                         case '\x7f':
78                                 return 0;
79                         }
80                 }
81                 c = (unsigned char)*++name;
82         } while(c != 0);
83         return 1;
84 }
85
86 /**
87  * Adds the api of 'name' described by 'api'.
88  * @param name the name of the api to add (have to survive, not copied!)
89  * @param api the api
90  * @returns 0 in case of success or -1 in case
91  * of error with errno set:
92  *   - EINVAL if name isn't valid
93  *   - EEXIST if name already registered
94  *   - ENOMEM when out of memory
95  */
96 int afb_apis_add(const char *name, struct afb_api api)
97 {
98         struct api_desc *apis;
99         int i, c;
100
101         /* Checks the api name */
102         if (!afb_apis_is_valid_api_name(name)) {
103                 ERROR("invalid api name forbidden (name is '%s')", name);
104                 errno = EINVAL;
105                 goto error;
106         }
107
108         /* check previously existing plugin */
109         for (i = 0 ; i < apis_count ; i++) {
110                 c = strcasecmp(apis_array[i].name, name);
111                 if (c == 0) {
112                         ERROR("api of name %s already exists", name);
113                         errno = EEXIST;
114                         goto error;
115                 }
116                 if (c > 0)
117                         break;
118         }
119
120         /* allocates enough memory */
121         apis = realloc(apis_array, ((unsigned)apis_count + 1) * sizeof * apis);
122         if (apis == NULL) {
123                 ERROR("out of memory");
124                 errno = ENOMEM;
125                 goto error;
126         }
127         apis_array = apis;
128
129         /* copy higher part of the array */
130         c = apis_count;
131         while (c > i) {
132                 apis_array[c] = apis_array[c - 1];
133                 c--;
134         }
135
136         /* record the plugin */
137         apis = &apis_array[i];
138         apis->api = api;
139         apis->name = name;
140         apis_count++;
141
142         return 0;
143
144 error:
145         return -1;
146 }
147
148 /**
149  * Dispatch the request 'req' with the 'context' to the
150  * method of 'api' and 'verb'.
151  * @param req the request to dispatch
152  * @param context the context of the request
153  * @param api the api of the verb
154  * @param verb the verb within the api
155  */
156 void afb_apis_call(struct afb_req req, struct afb_context *context, const char *api, const char *verb)
157 {
158         int i, c, up, lo;
159         const struct api_desc *a;
160
161         /* init hooking the request */
162         req = afb_hook_req_call(req, context, api, verb);
163
164         /* dichotomic search of the api */
165         /* initial slice */
166         lo = 0;
167         up = apis_count;
168         for (;;) {
169                 /* check remaining slice */
170                 if (lo >= up) {
171                         /* empty ?! */
172                         afb_req_fail(req, "fail", "api not found");
173                         break;
174                 }
175                 /* check the mid of the slice */
176                 i = (lo + up) >> 1;
177                 a = &apis_array[i];
178                 c = strcasecmp(a->name, api);
179                 if (c == 0) {
180                         /* api found */
181                         context->api_index = i;
182                         a->api.call(a->api.closure, req, context, verb);
183                         break;
184                 }
185                 /* update the slice */
186                 if (c < 0)
187                         lo = i + 1;
188                 else
189                         up = i;
190         }
191 }
192
193 int afb_apis_start_service(const char *api, int share_session, int onneed)
194 {
195         int i;
196
197         for (i = 0 ; i < apis_count ; i++) {
198                 if (!strcasecmp(apis_array[i].name, api))
199                         return apis_array[i].api.service_start(apis_array[i].api.closure, share_session, onneed);
200         }
201         ERROR("can't find service %s", api);
202         return -1;
203 }
204
205 int afb_apis_start_all_services(int share_session)
206 {
207         int i, rc;
208
209         for (i = 0 ; i < apis_count ; i++) {
210                 rc = apis_array[i].api.service_start(apis_array[i].api.closure, share_session, 1);
211                 if (rc < 0)
212                         return rc;
213         }
214         return 0;
215 }
216