2 * Copyright (C) 2016 "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-req-itf.h"
36 static struct api_desc *apis_array = NULL;
37 static int apis_count = 0;
44 int afb_apis_add(const char *name, struct afb_api api)
46 struct api_desc *apis;
50 /* check existing or not */
53 fprintf(stderr, "empty api name forbidden\n");
57 /* check previously existing plugin */
58 for (i = 0 ; i < apis_count ; i++) {
59 if (!strcasecmp(apis_array[i].name, name)) {
60 fprintf(stderr, "ERROR: api of name %s already exists\n", name);
65 /* allocates enough memory */
66 apis = realloc(apis_array, ((unsigned)apis_count + 1) * sizeof * apis);
68 fprintf(stderr, "out of memory\n");
73 /* record the plugin */
74 apis = &apis_array[apis_count];
86 void afb_apis_call(struct afb_req req, struct AFB_clientCtx *context, const char *api, size_t lenapi, const char *verb, size_t lenverb)
89 const struct api_desc *a;
92 for (i = 0 ; i < apis_count ; i++, a++) {
93 if (a->namelen == lenapi && !strncasecmp(a->name, api, lenapi)) {
94 req.ctx_closure = &context->contexts[i];
95 a->api.call(a->api.closure, req, verb, lenverb);
99 afb_req_fail(req, "fail", "api not found");