Introduce apiset for grouping apis
[src/app-framework-binder.git] / src / afb-api-so-v2.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-ditf.h"
31 #include "afb-evt.h"
32 #include "afb-common.h"
33 #include "afb-context.h"
34 #include "afb-api-so.h"
35 #include "afb-xreq.h"
36 #include "verbose.h"
37
38 /*
39  * names of symbols
40  */
41 static const char afb_api_so_v2_descriptor[] = "afbBindingV2";
42
43 /*
44  * Description of a binding
45  */
46 struct api_so_v2 {
47         struct afb_binding_v2 *binding; /* descriptor */
48         void *handle;                   /* context of dlopen */
49         struct afb_svc *service;        /* handler for service started */
50         struct afb_ditf ditf;           /* daemon interface */
51 };
52
53 static const struct afb_verb_v2 *search(struct api_so_v2 *desc, const char *verb)
54 {
55         const struct afb_verb_v2 *result;
56
57         result = desc->binding->verbs;
58         while (result->verb && strcasecmp(result->verb, verb))
59                 result++;
60         return result->verb ? result : NULL;
61 }
62
63 static void call_cb(void *closure, struct afb_xreq *xreq)
64 {
65         struct api_so_v2 *desc = closure;
66         const struct afb_verb_v2 *verb;
67
68         verb = search(desc, xreq->verb);
69         if (!verb)
70                 afb_xreq_fail_f(xreq, "unknown-verb", "verb %s unknown within api %s", xreq->verb, desc->binding->api);
71         else
72                 afb_xreq_so_call(xreq, verb->session, verb->callback);
73 }
74
75 static int service_start_cb(void *closure, int share_session, int onneed, struct afb_apiset *apiset)
76 {
77         int (*start)(const struct afb_binding_interface *interface, struct afb_service service);
78         void (*onevent)(const char *event, struct json_object *object);
79
80         struct api_so_v2 *desc = closure;
81
82         /* check state */
83         if (desc->service != NULL) {
84                 /* not an error when onneed */
85                 if (onneed != 0)
86                         return 0;
87
88                 /* already started: it is an error */
89                 ERROR("Service %s already started", desc->binding->api);
90                 return -1;
91         }
92
93         /* get the initialisation */
94         start = desc->binding->start;
95         if (start == NULL) {
96                 /* not an error when onneed */
97                 if (onneed != 0)
98                         return 0;
99
100                 /* no initialisation method */
101                 ERROR("Binding %s is not a service", desc->binding->api);
102                 return -1;
103         }
104
105         /* get the event handler if any */
106         onevent = desc->binding->onevent;
107         desc->service = afb_svc_create_v2(apiset, share_session, onevent, start, &desc->ditf.interface);
108         if (desc->service == NULL) {
109                 /* starting error */
110                 ERROR("Starting service %s failed", desc->binding->api);
111                 return -1;
112         }
113
114         return 0;
115 }
116
117 static void update_hooks_cb(void *closure)
118 {
119         struct api_so_v2 *desc = closure;
120         afb_ditf_update_hook(&desc->ditf);
121 }
122
123 static int get_verbosity_cb(void *closure)
124 {
125         struct api_so_v2 *desc = closure;
126         return desc->ditf.interface.verbosity;
127 }
128
129 static void set_verbosity_cb(void *closure, int level)
130 {
131         struct api_so_v2 *desc = closure;
132         desc->ditf.interface.verbosity = level;
133 }
134
135 static struct afb_api_itf so_v2_api_itf = {
136         .call = call_cb,
137         .service_start = service_start_cb,
138         .update_hooks = update_hooks_cb,
139         .get_verbosity = get_verbosity_cb,
140         .set_verbosity = set_verbosity_cb
141 };
142
143 int afb_api_so_v2_add(const char *path, void *handle, struct afb_apiset *apiset)
144 {
145         int rc;
146         struct api_so_v2 *desc;
147         struct afb_binding_v2 *binding;
148         struct afb_api afb_api;
149
150         /* retrieves the register function */
151         binding = dlsym(handle, afb_api_so_v2_descriptor);
152         if (!binding)
153                 return 0;
154
155         INFO("binding [%s] looks like an AFB binding V2", path);
156
157         /* basic checks */
158         if (binding->api == NULL || *binding->api == 0) {
159                 ERROR("binding [%s] bad api name...", path);
160                 goto error;
161         }
162         if (!afb_api_is_valid_name(binding->api)) {
163                 ERROR("binding [%s] invalid api name...", path);
164                 goto error;
165         }
166         if (binding->specification == NULL || *binding->specification == 0) {
167                 ERROR("binding [%s] bad specification...", path);
168                 goto error;
169         }
170         if (binding->verbs == NULL) {
171                 ERROR("binding [%s] no verbs...", path);
172                 goto error;
173         }
174
175         /* allocates the description */
176         desc = calloc(1, sizeof *desc);
177         if (desc == NULL) {
178                 ERROR("out of memory");
179                 goto error;
180         }
181         desc->binding = binding;
182         desc->handle = handle;
183
184         /* init the interface */
185         afb_ditf_init(&desc->ditf, binding->api);
186
187         /* for log purpose, a fake binding is needed here */
188
189         /* init the binding */
190         if (binding->init) {
191                 NOTICE("binding %s [%s] calling init function", binding->api, path);
192                 rc = binding->init(&desc->ditf.interface);
193                 if (rc < 0) {
194                         ERROR("binding %s [%s] initialisation function failed...", binding->api, path);
195                         goto error2;
196                 }
197         }
198
199         /* records the binding */
200         afb_api.closure = desc;
201         afb_api.itf = &so_v2_api_itf;
202         if (afb_apiset_add(apiset, binding->api, 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, binding->api);
207         return 1;
208
209 error2:
210         free(desc);
211 error:
212         return -1;
213 }
214