Make daemon interface common
[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-apis.h"
28 #include "afb-svc.h"
29 #include "afb-evt.h"
30 #include "afb-common.h"
31 #include "afb-context.h"
32 #include "afb-api-so.h"
33 #include "afb-xreq.h"
34 #include "afb-ditf.h"
35 #include "verbose.h"
36
37 /*
38  * names of symbols
39  */
40 static const char afb_api_so_v1_register[] = "afbBindingV1Register";
41 static const char afb_api_so_v1_service_init[] = "afbBindingV1ServiceInit";
42 static const char afb_api_so_v1_service_event[] = "afbBindingV1ServiceEvent";
43
44 /*
45  * Description of a binding
46  */
47 struct api_so_v1 {
48         struct afb_binding *binding;    /* descriptor */
49         void *handle;                   /* context of dlopen */
50         struct afb_svc *service;        /* handler for service started */
51         struct afb_ditf ditf;           /* daemon interface */
52 };
53
54 static const struct afb_verb_desc_v1 *search(struct api_so_v1 *desc, const char *name)
55 {
56         const struct afb_verb_desc_v1 *verb;
57
58         verb = desc->binding->v1.verbs;
59         while (verb->name && strcasecmp(verb->name, name))
60                 verb++;
61         return verb->name ? verb : NULL;
62 }
63
64 static void call_cb(void *closure, struct afb_xreq *xreq)
65 {
66         const struct afb_verb_desc_v1 *verb;
67         struct api_so_v1 *desc = closure;
68
69         verb = search(desc, xreq->verb);
70         if (!verb)
71                 afb_xreq_fail_f(xreq, "unknown-verb", "verb %s unknown within api %s", xreq->verb, desc->binding->v1.prefix);
72         else
73                 afb_xreq_call(xreq, verb->session, verb->callback);
74 }
75
76 static int service_start_cb(void *closure, int share_session, int onneed)
77 {
78         int (*init)(struct afb_service service);
79         void (*onevent)(const char *event, struct json_object *object);
80
81         struct api_so_v1 *desc = closure;
82
83         /* check state */
84         if (desc->service != NULL) {
85                 /* not an error when onneed */
86                 if (onneed != 0)
87                         return 0;
88
89                 /* already started: it is an error */
90                 ERROR("Service %s already started", desc->binding->v1.prefix);
91                 return -1;
92         }
93
94         /* get the initialisation */
95         init = dlsym(desc->handle, afb_api_so_v1_service_init);
96         if (init == NULL) {
97                 /* not an error when onneed */
98                 if (onneed != 0)
99                         return 0;
100
101                 /* no initialisation method */
102                 ERROR("Binding %s is not a service", desc->binding->v1.prefix);
103                 return -1;
104         }
105
106         /* get the event handler if any */
107         onevent = dlsym(desc->handle, afb_api_so_v1_service_event);
108         desc->service = afb_svc_create(share_session, init, onevent);
109         if (desc->service == NULL) {
110                 /* starting error */
111                 ERROR("Starting service %s failed", desc->binding->v1.prefix);
112                 return -1;
113         }
114
115         return 0;
116 }
117
118 int afb_api_so_v1_add(const char *path, void *handle)
119 {
120         struct api_so_v1 *desc;
121         struct afb_binding *(*register_function) (const struct afb_binding_interface *interface);
122
123         /* retrieves the register function */
124         register_function = dlsym(handle, afb_api_so_v1_register);
125         if (!register_function)
126                 return 0;
127         INFO("binding [%s] is a valid AFB binding V1", path);
128
129         /* allocates the description */
130         desc = calloc(1, sizeof *desc);
131         if (desc == NULL) {
132                 ERROR("out of memory");
133                 goto error;
134         }
135         desc->handle = handle;
136
137         /* init the interface */
138         afb_ditf_init(&desc->ditf, path);
139
140         /* init the binding */
141         NOTICE("binding [%s] calling registering function %s", path, afb_api_so_v1_register);
142         desc->binding = register_function(&desc->ditf.interface);
143         if (desc->binding == NULL) {
144                 ERROR("binding [%s] register function failed. continuing...", path);
145                 goto error2;
146         }
147
148         /* check the returned structure */
149         if (desc->binding->type != AFB_BINDING_VERSION_1) {
150                 ERROR("binding [%s] invalid type %d...", path, desc->binding->type);
151                 goto error2;
152         }
153         if (desc->binding->v1.prefix == NULL || *desc->binding->v1.prefix == 0) {
154                 ERROR("binding [%s] bad prefix...", path);
155                 goto error2;
156         }
157         if (!afb_apis_is_valid_api_name(desc->binding->v1.prefix)) {
158                 ERROR("binding [%s] invalid prefix...", path);
159                 goto error2;
160         }
161         if (desc->binding->v1.info == NULL || *desc->binding->v1.info == 0) {
162                 ERROR("binding [%s] bad description...", path);
163                 goto error2;
164         }
165         if (desc->binding->v1.verbs == NULL) {
166                 ERROR("binding [%s] no APIs...", path);
167                 goto error2;
168         }
169
170         /* records the binding */
171         afb_ditf_rename(&desc->ditf, desc->binding->v1.prefix);
172         if (afb_apis_add(desc->binding->v1.prefix, (struct afb_api){
173                         .closure = desc,
174                         .call = call_cb,
175                         .service_start = service_start_cb }) < 0) {
176                 ERROR("binding [%s] can't be registered...", path);
177                 goto error2;
178         }
179         NOTICE("binding %s loaded with API prefix %s", path, desc->binding->v1.prefix);
180         return 1;
181
182 error2:
183         free(desc);
184 error:
185         return -1;
186 }
187