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];
147 * @param api the api of the verb
148 * @return the descriptor if found or NULL otherwise
150 static const struct api_desc *search(const char *api)
153 const struct api_desc *a;
155 /* dichotomic search of the api */
160 /* check remaining slice */
165 /* check the mid of the slice */
168 c = strcasecmp(a->name, api);
173 /* update the slice */
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
189 void afb_apis_call(struct afb_req req, struct afb_context *context, const char *api, const char *verb)
191 const struct api_desc *a;
193 /* init hooking the request */
194 req = afb_hook_req_call(req, context, api, verb);
199 afb_req_fail(req, "fail", "api not found");
201 context->api_key = a->api.closure;
202 a->api.call(a->api.closure, req, context, verb);
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
213 * @return a positive number on success
215 int afb_apis_start_service(const char *api, int share_session, int onneed)
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);
223 ERROR("can't find service %s", api);
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
234 int afb_apis_start_all_services(int share_session)
238 for (i = 0 ; i < apis_count ; i++) {
239 rc = apis_array[i].api.service_start(apis_array[i].api.closure, share_session, 1);
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
254 void afb_apis_xcall(struct afb_xreq *xreq)
256 const struct api_desc *a;
258 /* init hooking the request */
259 // TODO req = afb_hook_req_call(req, context, api, verb);
262 a = search(xreq->api);
264 afb_xreq_fail_f(xreq, "unknown-api", "api %s not found", xreq->api);
266 xreq->context.api_key = a->api.closure;
267 a->api.xcall(a->api.closure, xreq);