Adds hooks for service (svc)
[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 AFB_BINDING_PRAGMA_NO_VERBOSE_MACRO
20
21 #include <stdio.h>
22 #include <string.h>
23 #include <dlfcn.h>
24 #include <assert.h>
25
26 #include <json-c/json.h>
27
28 #include <afb/afb-binding-v1.h>
29
30 #include "afb-api.h"
31 #include "afb-api-so-v1.h"
32 #include "afb-apiset.h"
33 #include "afb-svc.h"
34 #include "afb-evt.h"
35 #include "afb-common.h"
36 #include "afb-context.h"
37 #include "afb-api-so.h"
38 #include "afb-xreq.h"
39 #include "afb-ditf.h"
40 #include "verbose.h"
41
42 /*
43  * names of symbols
44  */
45 static const char afb_api_so_v1_register[] = "afbBindingV1Register";
46 static const char afb_api_so_v1_service_init[] = "afbBindingV1ServiceInit";
47 static const char afb_api_so_v1_service_event[] = "afbBindingV1ServiceEvent";
48
49 /*
50  * Description of a binding
51  */
52 struct api_so_v1 {
53         struct afb_binding_v1 *binding; /* descriptor */
54         void *handle;                   /* context of dlopen */
55         struct afb_svc *service;        /* handler for service started */
56         struct afb_binding_interface_v1 interface;
57         struct afb_ditf ditf;           /* daemon interface */
58 };
59
60 static const struct afb_verb_desc_v1 *search(struct api_so_v1 *desc, const char *name)
61 {
62         const struct afb_verb_desc_v1 *verb;
63
64         verb = desc->binding->v1.verbs;
65         while (verb->name && strcasecmp(verb->name, name))
66                 verb++;
67         return verb->name ? verb : NULL;
68 }
69
70 static void call_cb(void *closure, struct afb_xreq *xreq)
71 {
72         const struct afb_verb_desc_v1 *verb;
73         struct api_so_v1 *desc = closure;
74
75         verb = search(desc, xreq->verb);
76         afb_xreq_call_verb_v1(xreq, verb);
77 }
78
79 static int service_start_cb(void *closure, int share_session, int onneed, struct afb_apiset *apiset)
80 {
81         int (*init)(struct afb_service service);
82         void (*onevent)(const char *event, struct json_object *object);
83
84         struct api_so_v1 *desc = closure;
85
86         /* check state */
87         if (desc->service != NULL) {
88                 /* not an error when onneed */
89                 if (onneed != 0)
90                         return 0;
91
92                 /* already started: it is an error */
93                 ERROR("Service %s already started", desc->binding->v1.prefix);
94                 return -1;
95         }
96
97         /* get the initialisation */
98         init = dlsym(desc->handle, afb_api_so_v1_service_init);
99         onevent = dlsym(desc->handle, afb_api_so_v1_service_event);
100         if (init == NULL && onevent == NULL) {
101                 /* not an error when onneed */
102                 if (onneed != 0)
103                         return 0;
104
105                 /* no initialisation method */
106                 ERROR("Binding %s is not a service", desc->binding->v1.prefix);
107                 return -1;
108         }
109
110         /* get the event handler if any */
111         desc->service = afb_svc_create_v1(desc->binding->v1.prefix, apiset, share_session, init, onevent);
112         if (desc->service == NULL) {
113                 /* starting error */
114                 ERROR("Starting service %s failed", desc->binding->v1.prefix);
115                 return -1;
116         }
117
118         return 0;
119 }
120
121 static void update_hooks_cb(void *closure)
122 {
123         struct api_so_v1 *desc = closure;
124         afb_ditf_update_hook(&desc->ditf);
125         if (desc->service)
126                 afb_svc_update_hook(desc->service);
127 }
128
129 static int get_verbosity_cb(void *closure)
130 {
131         struct api_so_v1 *desc = closure;
132         return desc->interface.verbosity;
133 }
134
135 static void set_verbosity_cb(void *closure, int level)
136 {
137         struct api_so_v1 *desc = closure;
138         desc->interface.verbosity = level;
139 }
140
141 struct json_object *describe_cb(void *closure)
142 {
143         struct api_so_v1 *desc = closure;
144         const struct afb_verb_desc_v1 *verb;
145         struct json_object *r, *v, *f, *a;
146
147         r = json_object_new_object();
148         json_object_object_add(r, "version", json_object_new_int(1));
149         json_object_object_add(r, "info", json_object_new_string(desc->binding->v1.info));
150         v = json_object_new_object();
151         json_object_object_add(r, "verbs", v);
152         verb = desc->binding->v1.verbs;
153         while (verb->name) {
154                 f = json_object_new_object();
155                 a = json_object_new_array();
156                 json_object_object_add(f, "name", json_object_new_string(verb->name));
157                 json_object_object_add(f, "info", json_object_new_string(verb->info));
158                 if (verb->session & AFB_SESSION_CLOSE_V1)
159                         json_object_array_add(a, json_object_new_string("session-close"));
160                 if (verb->session & AFB_SESSION_RENEW_V1)
161                         json_object_array_add(a, json_object_new_string("session-renew"));
162                 if (verb->session & AFB_SESSION_CHECK_V1)
163                         json_object_array_add(a, json_object_new_string("session-check"));
164                 if (verb->session & AFB_SESSION_LOA_EQ_V1) {
165                         const char *rel = "?";
166                         char buffer[80];
167                         switch (verb->session & AFB_SESSION_LOA_EQ_V1) {
168                         case AFB_SESSION_LOA_GE_V1: rel = ">="; break;
169                         case AFB_SESSION_LOA_LE_V1: rel = "<="; break;
170                         case AFB_SESSION_LOA_EQ_V1: rel = "=="; break;
171                         }
172                         snprintf(buffer, sizeof buffer, "LOA%s%d", rel, (int)((verb->session >> AFB_SESSION_LOA_SHIFT_V1) & AFB_SESSION_LOA_MASK_V1));
173                         json_object_array_add(a, json_object_new_string(buffer));
174                 }
175                 json_object_object_add(f, "flags", a);
176                 json_object_object_add(v, verb->name, f);
177                 verb++;
178         }
179         return r;
180 }
181
182 static struct afb_api_itf so_v1_api_itf = {
183         .call = call_cb,
184         .service_start = service_start_cb,
185         .update_hooks = update_hooks_cb,
186         .get_verbosity = get_verbosity_cb,
187         .set_verbosity = set_verbosity_cb,
188         .describe = describe_cb
189 };
190
191 int afb_api_so_v1_add(const char *path, void *handle, struct afb_apiset *apiset)
192 {
193         struct api_so_v1 *desc;
194         struct afb_binding_v1 *(*register_function) (const struct afb_binding_interface_v1 *interface);
195         struct afb_api afb_api;
196
197         /* retrieves the register function */
198         register_function = dlsym(handle, afb_api_so_v1_register);
199         if (!register_function)
200                 return 0;
201         INFO("binding [%s] is a valid AFB binding V1", path);
202
203         /* allocates the description */
204         desc = calloc(1, sizeof *desc);
205         if (desc == NULL) {
206                 ERROR("out of memory");
207                 goto error;
208         }
209         desc->handle = handle;
210
211         /* init the interface */
212         afb_ditf_init_v1(&desc->ditf, path, &desc->interface);
213
214         /* init the binding */
215         INFO("binding [%s] calling registering function %s", path, afb_api_so_v1_register);
216         desc->binding = register_function(&desc->interface);
217         if (desc->binding == NULL) {
218                 ERROR("binding [%s] register function failed. continuing...", path);
219                 goto error2;
220         }
221
222         /* check the returned structure */
223         if (desc->binding->type != AFB_BINDING_VERSION_1) {
224                 ERROR("binding [%s] invalid type %d...", path, desc->binding->type);
225                 goto error2;
226         }
227         if (desc->binding->v1.prefix == NULL || *desc->binding->v1.prefix == 0) {
228                 ERROR("binding [%s] bad prefix...", path);
229                 goto error2;
230         }
231         if (!afb_api_is_valid_name(desc->binding->v1.prefix)) {
232                 ERROR("binding [%s] invalid prefix...", path);
233                 goto error2;
234         }
235         if (desc->binding->v1.info == NULL || *desc->binding->v1.info == 0) {
236                 ERROR("binding [%s] bad description...", path);
237                 goto error2;
238         }
239         if (desc->binding->v1.verbs == NULL) {
240                 ERROR("binding [%s] no APIs...", path);
241                 goto error2;
242         }
243
244         /* records the binding */
245         afb_ditf_rename(&desc->ditf, desc->binding->v1.prefix);
246         afb_api.closure = desc;
247         afb_api.itf = &so_v1_api_itf;
248         if (afb_apiset_add(apiset, desc->binding->v1.prefix, afb_api) < 0) {
249                 ERROR("binding [%s] can't be registered...", path);
250                 goto error2;
251         }
252         INFO("binding %s loaded with API prefix %s", path, desc->binding->v1.prefix);
253         return 1;
254
255 error2:
256         free(desc);
257 error:
258         return -1;
259 }
260