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 <sys/types.h>
32 #include <sys/syscall.h>
35 #include "afb-plugin.h"
36 #include "afb-req-itf.h"
37 #include "afb-poll-itf.h"
42 #include "utils-upoll.h"
50 static struct api_desc *apis_array = NULL;
51 static int apis_count = 0;
58 void afb_apis_free_context(int apiidx, void *context)
60 const struct afb_api *api;
61 api = &apis_array[apiidx].api;
62 api->free_context(api->closure, context);
65 int afb_apis_add(const char *name, struct afb_api api)
67 struct api_desc *apis;
71 /* check existing or not */
74 fprintf(stderr, "empty api name forbidden\n");
78 /* check previously existing plugin */
79 for (i = 0 ; i < apis_count ; i++) {
80 if (!strcasecmp(apis_array[i].name, name)) {
81 fprintf(stderr, "ERROR: api of name %s already exists\n", name);
86 /* allocates enough memory */
87 apis = realloc(apis_array, ((unsigned)apis_count + 1) * sizeof * apis);
89 fprintf(stderr, "out of memory\n");
94 /* record the plugin */
95 apis = &apis_array[apis_count];
107 void afb_apis_call(struct afb_req req, struct AFB_clientCtx *context, const char *api, size_t lenapi, const char *verb, size_t lenverb)
110 const struct api_desc *a;
113 for (i = 0 ; i < apis_count ; i++, a++) {
114 if (a->namelen == lenapi && !strncasecmp(a->name, api, lenapi)) {
115 req.context = &context->contexts[i];
116 a->api.call(a->api.closure, req, verb, lenverb);
120 afb_req_fail(req, "fail", "api not found");