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"
31 #include <afb/afb-req-itf.h>
38 static struct api_desc *apis_array = NULL;
39 static int apis_count = 0;
42 * Returns the current count of APIs
50 * Checks wether 'name' is a valid API name.
51 * @return 1 if valid, 0 otherwise
53 int afb_apis_is_valid_api_name(const char *name)
57 c = (unsigned char)*name;
59 /* empty names aren't valid */
63 if (c < (unsigned char)'\x80') {
81 c = (unsigned char)*++name;
87 * Adds the api of 'name' described by 'api'.
88 * @param name the name of the api to add
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
96 int afb_apis_add(const char *name, struct afb_api api)
98 struct api_desc *apis;
101 /* Checks the api name */
102 if (!afb_apis_is_valid_api_name(name)) {
103 ERROR("invalid api name forbidden (name is '%s')", name);
108 /* check previously existing plugin */
109 for (i = 0 ; i < apis_count ; i++) {
110 if (!strcasecmp(apis_array[i].name, name)) {
111 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 /* record the plugin */
127 apis = &apis_array[apis_count];
138 void afb_apis_call(struct afb_req req, struct afb_context *context, const char *api, const char *verb)
141 const struct api_desc *a;
143 req = afb_hook_req_call(req, context, api, verb);
145 for (i = 0 ; i < apis_count ; i++, a++) {
146 if (!strcasecmp(a->name, api)) {
147 context->api_index = i;
148 a->api.call(a->api.closure, req, context, verb);
152 afb_req_fail(req, "fail", "api not found");
155 int afb_apis_start_service(const char *api, int share_session, int onneed)
159 for (i = 0 ; i < apis_count ; i++) {
160 if (!strcasecmp(apis_array[i].name, api))
161 return apis_array[i].api.service_start(apis_array[i].api.closure, share_session, onneed);
163 ERROR("can't find service %s", api);
167 int afb_apis_start_all_services(int share_session)
171 for (i = 0 ; i < apis_count ; i++) {
172 rc = apis_array[i].api.service_start(apis_array[i].api.closure, share_session, 1);