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.
28 #include "afb-context.h"
30 #include <afb/afb-req-itf.h>
38 static struct api_desc *apis_array = NULL;
39 static int apis_count = 0;
46 int afb_apis_is_valid_api_name(const char *name)
50 c = (unsigned char)*name;
54 if (c < (unsigned char)'\x80') {
72 c = (unsigned char)*++name;
77 int afb_apis_add(const char *name, struct afb_api api)
79 struct api_desc *apis;
82 /* Checks the api name */
83 if (!afb_apis_is_valid_api_name(name)) {
84 ERROR("invalid api name forbidden (name is '%s')", name);
88 /* check previously existing plugin */
89 for (i = 0 ; i < apis_count ; i++) {
90 if (!strcasecmp(apis_array[i].name, name)) {
91 ERROR("api of name %s already exists", name);
96 /* allocates enough memory */
97 apis = realloc(apis_array, ((unsigned)apis_count + 1) * sizeof * apis);
99 ERROR("out of memory");
104 /* record the plugin */
105 apis = &apis_array[apis_count];
107 apis->namelen = strlen(name);
117 void afb_apis_call_(struct afb_req req, struct afb_context *context, const char *api, const char *verb)
119 afb_apis_call(req, context, api, strlen(api), verb, strlen(verb));
122 void afb_apis_call(struct afb_req req, struct afb_context *context, const char *api, size_t lenapi, const char *verb, size_t lenverb)
125 const struct api_desc *a;
127 req = afb_hook_req_call(req, context, api, lenapi, verb, lenverb);
129 for (i = 0 ; i < apis_count ; i++, a++) {
130 if (a->namelen == lenapi && !strncasecmp(a->name, api, lenapi)) {
131 context->api_index = i;
132 a->api.call(a->api.closure, req, context, verb, lenverb);
136 afb_req_fail(req, "fail", "api not found");
139 int afb_apis_start_service(const char *api, int share_session, int onneed)
143 for (i = 0 ; i < apis_count ; i++) {
144 if (!strcasecmp(apis_array[i].name, api))
145 return apis_array[i].api.service_start(apis_array[i].api.closure, share_session, onneed);
147 ERROR("can't find service %s", api);
151 int afb_apis_start_all_services(int share_session)
155 for (i = 0 ; i < apis_count ; i++) {
156 rc = apis_array[i].api.service_start(apis_array[i].api.closure, share_session, 1);