Introduce apiset for grouping apis
[src/app-framework-binder.git] / src / afb-api-so-v1.c
1 /*
2  * Copyright (C) 2016, 2017 "IoT.bzh"
3  * Author José Bollo <jose.bollo@iot.bzh>
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *   http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #define _GNU_SOURCE
19 #define NO_BINDING_VERBOSE_MACRO
20
21 #include <string.h>
22 #include <dlfcn.h>
23 #include <assert.h>
24
25 #include <afb/afb-binding.h>
26
27 #include "afb-api.h"
28 #include "afb-apiset.h"
29 #include "afb-svc.h"
30 #include "afb-evt.h"
31 #include "afb-common.h"
32 #include "afb-context.h"
33 #include "afb-api-so.h"
34 #include "afb-xreq.h"
35 #include "afb-ditf.h"
36 #include "verbose.h"
37
38 /*
39  * names of symbols
40  */
41 static const char afb_api_so_v1_register[] = "afbBindingV1Register";
42 static const char afb_api_so_v1_service_init[] = "afbBindingV1ServiceInit";
43 static const char afb_api_so_v1_service_event[] = "afbBindingV1ServiceEvent";
44
45 /*
46  * Description of a binding
47  */
48 struct api_so_v1 {
49         struct afb_binding *binding;    /* descriptor */
50         void *handle;                   /* context of dlopen */
51         struct afb_svc *service;        /* handler for service started */
52         struct afb_ditf ditf;           /* daemon interface */
53 };
54
55 static const struct afb_verb_desc_v1 *search(struct api_so_v1 *desc, const char *name)
56 {
57         const struct afb_verb_desc_v1 *verb;
58
59         verb = desc->binding->v1.verbs;
60         while (verb->name && strcasecmp(verb->name, name))
61                 verb++;
62         return verb->name ? verb : NULL;
63 }
64
65 static void call_cb(void *closure, struct afb_xreq *xreq)
66 {
67         const struct afb_verb_desc_v1 *verb;
68         struct api_so_v1 *desc = closure;
69
70         verb = search(desc, xreq->verb);
71         if (!verb)
72                 afb_xreq_fail_f(xreq, "unknown-verb", "verb %s unknown within api %s", xreq->verb, desc->binding->v1.prefix);
73         else
74                 afb_xreq_so_call(xreq, verb->session, verb->callback);
75 }
76
77 static int service_start_cb(void *closure, int share_session, int onneed, struct afb_apiset *apiset)
78 {
79         int (*init)(struct afb_service service);
80         void (*onevent)(const char *event, struct json_object *object);
81
82         struct api_so_v1 *desc = closure;
83
84         /* check state */
85         if (desc->service != NULL) {
86                 /* not an error when onneed */
87                 if (onneed != 0)
88                         return 0;
89
90                 /* already started: it is an error */
91                 ERROR("Service %s already started", desc->binding->v1.prefix);
92                 return -1;
93         }
94
95         /* get the initialisation */
96         init = dlsym(desc->handle, afb_api_so_v1_service_init);
97         if (init == NULL) {
98                 /* not an error when onneed */
99                 if (onneed != 0)
100                         return 0;
101
102                 /* no initialisation method */
103                 ERROR("Binding %s is not a service", desc->binding->v1.prefix);
104                 return -1;
105         }
106
107         /* get the event handler if any */
108         onevent = dlsym(desc->handle, afb_api_so_v1_service_event);
109         desc->service = afb_svc_create(apiset, share_session, init, onevent);
110         if (desc->service == NULL) {
111                 /* starting error */
112                 ERROR("Starting service %s failed", desc->binding->v1.prefix);
113                 return -1;
114         }
115
116         return 0;
117 }
118
119 static void update_hooks_cb(void *closure)
120 {
121         struct api_so_v1 *desc = closure;
122         afb_ditf_update_hook(&desc->ditf);
123 }
124
125 static int get_verbosity_cb(void *closure)
126 {
127         struct api_so_v1 *desc = closure;
128         return desc->ditf.interface.verbosity;
129 }
130
131 static void set_verbosity_cb(void *closure, int level)
132 {
133         struct api_so_v1 *desc = closure;
134         desc->ditf.interface.verbosity = level;
135 }
136
137 static struct afb_api_itf so_v1_api_itf = {
138         .call = call_cb,
139         .service_start = service_start_cb,
140         .update_hooks = update_hooks_cb,
141         .get_verbosity = get_verbosity_cb,
142         .set_verbosity = set_verbosity_cb
143 };
144
145 int afb_api_so_v1_add(const char *path, void *handle, struct afb_apiset *apiset)
146 {
147         struct api_so_v1 *desc;
148         struct afb_binding *(*register_function) (const struct afb_binding_interface *interface);
149         struct afb_api afb_api;
150
151         /* retrieves the register function */
152         register_function = dlsym(handle, afb_api_so_v1_register);
153         if (!register_function)
154                 return 0;
155         INFO("binding [%s] is a valid AFB binding V1", path);
156
157         /* allocates the description */
158         desc = calloc(1, sizeof *desc);
159         if (desc == NULL) {
160                 ERROR("out of memory");
161                 goto error;
162         }
163         desc->handle = handle;
164
165         /* init the interface */
166         afb_ditf_init(&desc->ditf, path);
167
168         /* init the binding */
169         NOTICE("binding [%s] calling registering function %s", path, afb_api_so_v1_register);
170         desc->binding = register_function(&desc->ditf.interface);
171         if (desc->binding == NULL) {
172                 ERROR("binding [%s] register function failed. continuing...", path);
173                 goto error2;
174         }
175
176         /* check the returned structure */
177         if (desc->binding->type != AFB_BINDING_VERSION_1) {
178                 ERROR("binding [%s] invalid type %d...", path, desc->binding->type);
179                 goto error2;
180         }
181         if (desc->binding->v1.prefix == NULL || *desc->binding->v1.prefix == 0) {
182                 ERROR("binding [%s] bad prefix...", path);
183                 goto error2;
184         }
185         if (!afb_api_is_valid_name(desc->binding->v1.prefix)) {
186                 ERROR("binding [%s] invalid prefix...", path);
187                 goto error2;
188         }
189         if (desc->binding->v1.info == NULL || *desc->binding->v1.info == 0) {
190                 ERROR("binding [%s] bad description...", path);
191                 goto error2;
192         }
193         if (desc->binding->v1.verbs == NULL) {
194                 ERROR("binding [%s] no APIs...", path);
195                 goto error2;
196         }
197
198         /* records the binding */
199         afb_ditf_rename(&desc->ditf, desc->binding->v1.prefix);
200         afb_api.closure = desc;
201         afb_api.itf = &so_v1_api_itf;
202         if (afb_apiset_add(apiset, desc->binding->v1.prefix, afb_api) < 0) {
203                 ERROR("binding [%s] can't be registered...", path);
204                 goto error2;
205         }
206         NOTICE("binding %s loaded with API prefix %s", path, desc->binding->v1.prefix);
207         return 1;
208
209 error2:
210         free(desc);
211 error:
212         return -1;
213 }
214