2 * Copyright (C) 2016, 2017 "IoT.bzh"
3 * Author "Fulup Ar Foll"
4 * Author José Bollo <jose.bollo@iot.bzh>
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
26 #include "afb-session.h"
29 #include "afb-context.h"
33 #include <afb/afb-req-itf.h>
36 * Internal description of an api
39 const char *name; /**< name of the api */
40 struct afb_api api; /**< handler of the api */
43 static struct api_desc *apis_array = NULL;
44 static int apis_count = 0;
47 * Checks wether 'name' is a valid API name.
48 * @return 1 if valid, 0 otherwise
50 int afb_apis_is_valid_api_name(const char *name)
54 c = (unsigned char)*name;
56 /* empty names aren't valid */
60 if (c < (unsigned char)'\x80') {
78 c = (unsigned char)*++name;
84 * Adds the api of 'name' described by 'api'.
85 * @param name the name of the api to add (have to survive, not copied!)
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
93 int afb_apis_add(const char *name, struct afb_api api)
95 struct api_desc *apis;
98 /* Checks the api name */
99 if (!afb_apis_is_valid_api_name(name)) {
100 ERROR("invalid api name forbidden (name is '%s')", name);
105 /* check previously existing plugin */
106 for (i = 0 ; i < apis_count ; i++) {
107 c = strcasecmp(apis_array[i].name, name);
109 ERROR("api of name %s already exists", name);
117 /* allocates enough memory */
118 apis = realloc(apis_array, ((unsigned)apis_count + 1) * sizeof * apis);
120 ERROR("out of memory");
126 /* copy higher part of the array */
129 apis_array[c] = apis_array[c - 1];
133 /* record the plugin */
134 apis = &apis_array[i];
139 NOTICE("API %s added", name);
149 * @param api the api of the verb
150 * @return the descriptor if found or NULL otherwise
152 static const struct api_desc *search(const char *api)
155 const struct api_desc *a;
157 /* dichotomic search of the api */
162 /* check remaining slice */
167 /* check the mid of the slice */
170 c = strcasecmp(a->name, api);
175 /* update the slice */
184 * Dispatch the request 'req' with the 'context' to the
185 * method of 'api' and 'verb'.
186 * @param req the request to dispatch
187 * @param context the context of the request
188 * @param api the api of the verb
189 * @param verb the verb within the api
191 void afb_apis_call(struct afb_req req, struct afb_context *context, const char *api, const char *verb)
193 const struct api_desc *a;
195 /* init hooking the request */
196 req = afb_hook_req_call(req, context, api, verb);
201 afb_req_fail(req, "fail", "api not found");
203 context->api_key = a->api.closure;
204 a->api.call(a->api.closure, req, context, verb);
209 * Starts a service by its 'api' name.
210 * @param api name of the service to start
211 * @param share_session if true start the servic"e in a shared session
212 * if false start it in its own session
213 * @param onneed if true start the service if possible, if false the api
215 * @return a positive number on success
217 int afb_apis_start_service(const char *api, int share_session, int onneed)
221 for (i = 0 ; i < apis_count ; i++) {
222 if (!strcasecmp(apis_array[i].name, api))
223 return apis_array[i].api.service_start(apis_array[i].api.closure, share_session, onneed);
225 ERROR("can't find service %s", api);
231 * Starts all possible services but stops at first error.
232 * @param share_session if true start the servic"e in a shared session
233 * if false start it in its own session
234 * @return 0 on success or a negative number when an error is found
236 int afb_apis_start_all_services(int share_session)
240 for (i = 0 ; i < apis_count ; i++) {
241 rc = apis_array[i].api.service_start(apis_array[i].api.closure, share_session, 1);
249 * Dispatch the request 'req' with the 'context' to the
250 * method of 'api' and 'verb'.
251 * @param req the request to dispatch
252 * @param context the context of the request
253 * @param api the api of the verb
254 * @param verb the verb within the api
256 void afb_apis_xcall(struct afb_xreq *xreq)
258 const struct api_desc *a;
260 /* init hooking the request */
261 // TODO req = afb_hook_req_call(req, context, api, verb);
264 a = search(xreq->api);
266 afb_xreq_fail_f(xreq, "unknown-api", "api %s not found", xreq->api);
268 xreq->context.api_key = a->api.closure;
269 a->api.xcall(a->api.closure, xreq);