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