afb-export: cleanup of unused async
[src/app-framework-binder.git] / src / afb-export.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
20 #include <stdio.h>
21 #include <string.h>
22 #include <errno.h>
23
24 #include <json-c/json.h>
25
26 #include <afb/afb-binding-v1.h>
27 #include <afb/afb-binding-v2.h>
28
29 #include "afb-api.h"
30 #include "afb-apiset.h"
31 #include "afb-common.h"
32 #include "afb-cred.h"
33 #include "afb-evt.h"
34 #include "afb-export.h"
35 #include "afb-hook.h"
36 #include "afb-msg-json.h"
37 #include "afb-session.h"
38 #include "afb-xreq.h"
39 #include "jobs.h"
40 #include "verbose.h"
41
42
43 /*************************************************************************
44  * internal types and structures
45  ************************************************************************/
46
47 enum afb_api_version
48 {
49         Api_Version_None = 0,
50         Api_Version_1 = 1,
51         Api_Version_2 = 2,
52         Api_Version_3 = 3
53 };
54
55 enum afb_api_state
56 {
57         Api_State_Pre_Init,
58         Api_State_Init,
59         Api_State_Run
60 };
61
62 struct afb_export
63 {
64         /* name of the api */
65         char *apiname;
66
67         /* version of the api */
68         unsigned version: 4;
69
70         /* current state */
71         unsigned state: 4;
72
73         /* hooking flags */
74         int hookditf;
75         int hooksvc;
76
77         /* session for service */
78         struct afb_session *session;
79
80         /* apiset for service */
81         struct afb_apiset *apiset;
82
83         /* event listener for service or NULL */
84         struct afb_evt_listener *listener;
85
86         /* start function */
87         union {
88                 int (*v1)(struct afb_service);
89                 int (*v2)();
90         } init;
91
92         /* event handling */
93         union {
94                 void (*v12)(const char *event, struct json_object *object);
95         } on_event;
96
97         /* exported data */
98         union {
99                 struct afb_binding_interface_v1 v1;
100                 struct afb_binding_data_v2 *v2;
101         } export;
102 };
103
104 /*************************************************************************************************************
105  *************************************************************************************************************
106  *************************************************************************************************************
107  *************************************************************************************************************
108                                            F R O M     D I T F
109  *************************************************************************************************************
110  *************************************************************************************************************
111  *************************************************************************************************************
112  *************************************************************************************************************/
113
114 /**********************************************
115 * normal flow
116 **********************************************/
117 static void vverbose_cb(void *closure, int level, const char *file, int line, const char *function, const char *fmt, va_list args)
118 {
119         char *p;
120         struct afb_export *export = closure;
121
122         if (!fmt || vasprintf(&p, fmt, args) < 0)
123                 vverbose(level, file, line, function, fmt, args);
124         else {
125                 verbose(level, file, line, function, "[API %s] %s", export->apiname, p);
126                 free(p);
127         }
128 }
129
130 static void old_vverbose_cb(void *closure, int level, const char *file, int line, const char *fmt, va_list args)
131 {
132         vverbose_cb(closure, level, file, line, NULL, fmt, args);
133 }
134
135 static struct afb_event event_make_cb(void *closure, const char *name)
136 {
137         size_t plen, nlen;
138         char *event;
139         struct afb_export *export = closure;
140         struct afb_eventid *eventid;
141
142         /* check daemon state */
143         if (export->state == Api_State_Pre_Init) {
144                 ERROR("[API %s] Bad call to 'afb_daemon_event_make(%s)', must not be in PreInit", export->apiname, name);
145                 errno = EINVAL;
146                 return (struct afb_event){ .itf = NULL, .closure = NULL };
147         }
148
149         /* makes the event name */
150         plen = strlen(export->apiname);
151         nlen = strlen(name);
152         event = alloca(nlen + plen + 2);
153         memcpy(event, export->apiname, plen);
154         event[plen] = '/';
155         memcpy(event + plen + 1, name, nlen + 1);
156
157         /* create the event */
158         eventid = afb_evt_create_event(event);
159         return (struct afb_event){ .itf = eventid ? eventid->itf : NULL, .closure = eventid };
160 }
161
162 static int event_broadcast_cb(void *closure, const char *name, struct json_object *object)
163 {
164         size_t plen, nlen;
165         char *event;
166         struct afb_export *export = closure;
167
168         /* check daemon state */
169         if (export->state == Api_State_Pre_Init) {
170                 ERROR("[API %s] Bad call to 'afb_daemon_event_broadcast(%s, %s)', must not be in PreInit", export->apiname, name, json_object_to_json_string(object));
171                 errno = EINVAL;
172                 return 0;
173         }
174
175         /* makes the event name */
176         plen = strlen(export->apiname);
177         nlen = strlen(name);
178         event = alloca(nlen + plen + 2);
179         memcpy(event, export->apiname, plen);
180         event[plen] = '/';
181         memcpy(event + plen + 1, name, nlen + 1);
182
183         /* broadcast the event */
184         return afb_evt_broadcast(event, object);
185 }
186
187 static int rootdir_open_locale_cb(void *closure, const char *filename, int flags, const char *locale)
188 {
189         return afb_common_rootdir_open_locale(filename, flags, locale);
190 }
191
192 static int queue_job_cb(void *closure, void (*callback)(int signum, void *arg), void *argument, void *group, int timeout)
193 {
194         return jobs_queue(group, timeout, callback, argument);
195 }
196
197 static struct afb_req unstore_req_cb(void *closure, struct afb_stored_req *sreq)
198 {
199         return afb_xreq_unstore(sreq);
200 }
201
202 static int require_api_cb(void *closure, const char *name, int initialized)
203 {
204         struct afb_export *export = closure;
205         if (export->state != Api_State_Init) {
206                 ERROR("[API %s] Bad call to 'afb_daemon_require(%s, %d)', must be in Init", export->apiname, name, initialized);
207                 errno = EINVAL;
208                 return -1;
209         }
210         return -!(initialized ? afb_apiset_lookup_started : afb_apiset_lookup)(export->apiset, name, 1);
211 }
212
213 static int rename_api_cb(void *closure, const char *name)
214 {
215         struct afb_export *export = closure;
216         if (export->state != Api_State_Pre_Init) {
217                 ERROR("[API %s] Bad call to 'afb_daemon_rename(%s)', must be in PreInit", export->apiname, name);
218                 errno = EINVAL;
219                 return -1;
220         }
221         if (!afb_api_is_valid_name(name)) {
222                 ERROR("[API %s] Can't rename to %s: bad API name", export->apiname, name);
223                 errno = EINVAL;
224                 return -1;
225         }
226         NOTICE("[API %s] renamed to [API %s]", export->apiname, name);
227         afb_export_rename(export, name);
228         return 0;
229 }
230
231 /**********************************************
232 * hooked flow
233 **********************************************/
234 static void hooked_vverbose_cb(void *closure, int level, const char *file, int line, const char *function, const char *fmt, va_list args)
235 {
236         struct afb_export *export = closure;
237         va_list ap;
238         va_copy(ap, args);
239         vverbose_cb(closure, level, file, line, function, fmt, args);
240         afb_hook_ditf_vverbose(export, level, file, line, function, fmt, ap);
241         va_end(ap);
242 }
243
244 static void hooked_old_vverbose_cb(void *closure, int level, const char *file, int line, const char *fmt, va_list args)
245 {
246         hooked_vverbose_cb(closure, level, file, line, NULL, fmt, args);
247 }
248
249 static struct afb_event hooked_event_make_cb(void *closure, const char *name)
250 {
251         struct afb_export *export = closure;
252         struct afb_event r = event_make_cb(closure, name);
253         afb_hook_ditf_event_make(export, name, r.closure);
254         return r;
255 }
256
257 static int hooked_event_broadcast_cb(void *closure, const char *name, struct json_object *object)
258 {
259         int r;
260         struct afb_export *export = closure;
261         json_object_get(object);
262         afb_hook_ditf_event_broadcast_before(export, name, json_object_get(object));
263         r = event_broadcast_cb(closure, name, object);
264         afb_hook_ditf_event_broadcast_after(export, name, object, r);
265         json_object_put(object);
266         return r;
267 }
268
269 static struct sd_event *hooked_get_event_loop(void *closure)
270 {
271         struct afb_export *export = closure;
272         struct sd_event *r = afb_common_get_event_loop();
273         return afb_hook_ditf_get_event_loop(export, r);
274 }
275
276 static struct sd_bus *hooked_get_user_bus(void *closure)
277 {
278         struct afb_export *export = closure;
279         struct sd_bus *r = afb_common_get_user_bus();
280         return afb_hook_ditf_get_user_bus(export, r);
281 }
282
283 static struct sd_bus *hooked_get_system_bus(void *closure)
284 {
285         struct afb_export *export = closure;
286         struct sd_bus *r = afb_common_get_system_bus();
287         return afb_hook_ditf_get_system_bus(export, r);
288 }
289
290 static int hooked_rootdir_get_fd(void *closure)
291 {
292         struct afb_export *export = closure;
293         int r = afb_common_rootdir_get_fd();
294         return afb_hook_ditf_rootdir_get_fd(export, r);
295 }
296
297 static int hooked_rootdir_open_locale_cb(void *closure, const char *filename, int flags, const char *locale)
298 {
299         struct afb_export *export = closure;
300         int r = rootdir_open_locale_cb(closure, filename, flags, locale);
301         return afb_hook_ditf_rootdir_open_locale(export, filename, flags, locale, r);
302 }
303
304 static int hooked_queue_job_cb(void *closure, void (*callback)(int signum, void *arg), void *argument, void *group, int timeout)
305 {
306         struct afb_export *export = closure;
307         int r = queue_job_cb(closure, callback, argument, group, timeout);
308         return afb_hook_ditf_queue_job(export, callback, argument, group, timeout, r);
309 }
310
311 static struct afb_req hooked_unstore_req_cb(void *closure, struct afb_stored_req *sreq)
312 {
313         struct afb_export *export = closure;
314         afb_hook_ditf_unstore_req(export, sreq);
315         return unstore_req_cb(closure, sreq);
316 }
317
318 static int hooked_require_api_cb(void *closure, const char *name, int initialized)
319 {
320         int result;
321         struct afb_export *export = closure;
322         afb_hook_ditf_require_api(export, name, initialized);
323         result = require_api_cb(closure, name, initialized);
324         return afb_hook_ditf_require_api_result(export, name, initialized, result);
325 }
326
327 static int hooked_rename_api_cb(void *closure, const char *name)
328 {
329         struct afb_export *export = closure;
330         const char *oldname = export->apiname;
331         int result = rename_api_cb(closure, name);
332         return afb_hook_ditf_rename_api(export, oldname, name, result);
333 }
334
335 /**********************************************
336 * vectors
337 **********************************************/
338 static const struct afb_daemon_itf daemon_itf = {
339         .vverbose_v1 = old_vverbose_cb,
340         .vverbose_v2 = vverbose_cb,
341         .event_make = event_make_cb,
342         .event_broadcast = event_broadcast_cb,
343         .get_event_loop = afb_common_get_event_loop,
344         .get_user_bus = afb_common_get_user_bus,
345         .get_system_bus = afb_common_get_system_bus,
346         .rootdir_get_fd = afb_common_rootdir_get_fd,
347         .rootdir_open_locale = rootdir_open_locale_cb,
348         .queue_job = queue_job_cb,
349         .unstore_req = unstore_req_cb,
350         .require_api = require_api_cb,
351         .rename_api = rename_api_cb
352 };
353
354 static const struct afb_daemon_itf hooked_daemon_itf = {
355         .vverbose_v1 = hooked_old_vverbose_cb,
356         .vverbose_v2 = hooked_vverbose_cb,
357         .event_make = hooked_event_make_cb,
358         .event_broadcast = hooked_event_broadcast_cb,
359         .get_event_loop = hooked_get_event_loop,
360         .get_user_bus = hooked_get_user_bus,
361         .get_system_bus = hooked_get_system_bus,
362         .rootdir_get_fd = hooked_rootdir_get_fd,
363         .rootdir_open_locale = hooked_rootdir_open_locale_cb,
364         .queue_job = hooked_queue_job_cb,
365         .unstore_req = hooked_unstore_req_cb,
366         .require_api = hooked_require_api_cb,
367         .rename_api = hooked_rename_api_cb
368 };
369
370
371 /*************************************************************************************************************
372  *************************************************************************************************************
373  *************************************************************************************************************
374  *************************************************************************************************************
375                                            F R O M     S V C
376  *************************************************************************************************************
377  *************************************************************************************************************
378  *************************************************************************************************************
379  *************************************************************************************************************/
380
381 /* the common session for services sharing their session */
382 static struct afb_session *common_session;
383
384 /*************************************************************************************************************
385  *************************************************************************************************************
386  *************************************************************************************************************
387  *************************************************************************************************************
388                                            F R O M     S V C
389  *************************************************************************************************************
390  *************************************************************************************************************
391  *************************************************************************************************************
392  *************************************************************************************************************/
393
394 /*
395  * Structure for requests initiated by the service
396  */
397 struct call_req
398 {
399         struct afb_xreq xreq;
400
401         struct afb_export *export;
402
403         /* the args */
404         void (*callback)(void*, int, struct json_object*);
405         void *closure;
406
407         /* sync */
408         struct jobloop *jobloop;
409         struct json_object *result;
410         int status;
411 };
412
413 /*
414  * destroys the call_req
415  */
416 static void callreq_destroy(struct afb_xreq *xreq)
417 {
418         struct call_req *callreq = CONTAINER_OF_XREQ(struct call_req, xreq);
419
420         afb_context_disconnect(&callreq->xreq.context);
421         json_object_put(callreq->xreq.json);
422         afb_cred_unref(callreq->xreq.cred);
423         free(callreq);
424 }
425
426 static void callreq_reply(struct afb_xreq *xreq, int status, json_object *obj)
427 {
428         struct call_req *callreq = CONTAINER_OF_XREQ(struct call_req, xreq);
429         if (callreq->callback)
430                 callreq->callback(callreq->closure, status, obj);
431         json_object_put(obj);
432 }
433
434 static void callreq_sync_leave(struct call_req *callreq)
435 {
436         struct jobloop *jobloop = callreq->jobloop;
437
438         if (jobloop) {
439                 callreq->jobloop = NULL;
440                 jobs_leave(jobloop);
441         }
442 }
443
444 static void callreq_reply_sync(struct afb_xreq *xreq, int status, json_object *obj)
445 {
446         struct call_req *callreq = CONTAINER_OF_XREQ(struct call_req, xreq);
447         callreq->status = status;
448         callreq->result = obj;
449         callreq_sync_leave(callreq);
450 }
451
452 static void callreq_sync_enter(int signum, void *closure, struct jobloop *jobloop)
453 {
454         struct call_req *callreq = closure;
455
456         if (!signum) {
457                 callreq->jobloop = jobloop;
458                 afb_xreq_process(&callreq->xreq, callreq->export->apiset);
459         } else {
460                 callreq->result = afb_msg_json_internal_error();
461                 callreq->status = -1;
462                 callreq_sync_leave(callreq);
463         }
464 }
465
466 /* interface for requests of services */
467 const struct afb_xreq_query_itf afb_export_xreq_itf = {
468         .unref = callreq_destroy,
469         .reply = callreq_reply
470 };
471
472 /* interface for requests of services */
473 const struct afb_xreq_query_itf afb_export_xreq_sync_itf = {
474         .unref = callreq_destroy,
475         .reply = callreq_reply_sync
476 };
477
478 /*
479  * create an call_req
480  */
481 static struct call_req *callreq_create(struct afb_export *export, const char *api, const char *verb, struct json_object *args, const struct afb_xreq_query_itf *itf)
482 {
483         struct call_req *callreq;
484         size_t lenapi, lenverb;
485         char *copy;
486
487         /* allocates the request */
488         lenapi = 1 + strlen(api);
489         lenverb = 1 + strlen(verb);
490         callreq = malloc(lenapi + lenverb + sizeof *callreq);
491         if (callreq != NULL) {
492                 /* initialises the request */
493                 afb_xreq_init(&callreq->xreq, itf);
494                 afb_context_init(&callreq->xreq.context, export->session, NULL);
495                 callreq->xreq.context.validated = 1;
496                 copy = (char*)&callreq[1];
497                 memcpy(copy, api, lenapi);
498                 callreq->xreq.api = copy;
499                 copy = &copy[lenapi];
500                 memcpy(copy, verb, lenverb);
501                 callreq->xreq.verb = copy;
502                 callreq->xreq.listener = export->listener;
503                 callreq->xreq.json = args;
504                 callreq->export = export;
505         }
506         return callreq;
507 }
508
509 /*
510  * Initiates a call for the service
511  */
512 static void svc_call(void *closure, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*), void *cbclosure)
513 {
514         struct afb_export *export = closure;
515         struct call_req *callreq;
516         struct json_object *ierr;
517
518         /* allocates the request */
519         callreq = callreq_create(export, api, verb, args, &afb_export_xreq_itf);
520         if (callreq == NULL) {
521                 ERROR("out of memory");
522                 json_object_put(args);
523                 ierr = afb_msg_json_internal_error();
524                 if (callback)
525                         callback(cbclosure, -1, ierr);
526                 json_object_put(ierr);
527                 return;
528         }
529
530         /* initialises the request */
531         callreq->jobloop = NULL;
532         callreq->callback = callback;
533         callreq->closure = cbclosure;
534
535         /* terminates and frees ressources if needed */
536         afb_xreq_process(&callreq->xreq, export->apiset);
537 }
538
539 static int svc_call_sync(void *closure, const char *api, const char *verb, struct json_object *args,
540                                 struct json_object **result)
541 {
542         struct afb_export *export = closure;
543         struct call_req *callreq;
544         struct json_object *resu;
545         int rc;
546
547         /* allocates the request */
548         callreq = callreq_create(export, api, verb, args, &afb_export_xreq_sync_itf);
549         if (callreq == NULL) {
550                 ERROR("out of memory");
551                 errno = ENOMEM;
552                 json_object_put(args);
553                 resu = afb_msg_json_internal_error();
554                 rc = -1;
555         } else {
556                 /* initialises the request */
557                 callreq->jobloop = NULL;
558                 callreq->callback = NULL;
559                 callreq->result = NULL;
560                 callreq->status = 0;
561                 afb_xreq_unhooked_addref(&callreq->xreq); /* avoid early callreq destruction */
562                 rc = jobs_enter(NULL, 0, callreq_sync_enter, callreq);
563                 if (rc >= 0)
564                         rc = callreq->status;
565                 resu = (rc >= 0 || callreq->result) ? callreq->result : afb_msg_json_internal_error();
566                 afb_xreq_unhooked_unref(&callreq->xreq);
567         }
568         if (result)
569                 *result = resu;
570         else
571                 json_object_put(resu);
572         return rc;
573 }
574
575 struct hooked_call
576 {
577         struct afb_export *export;
578         void (*callback)(void*, int, struct json_object*);
579         void *cbclosure;
580 };
581
582 static void svc_hooked_call_result(void *closure, int status, struct json_object *result)
583 {
584         struct hooked_call *hc = closure;
585         afb_hook_svc_call_result(hc->export, status, result);
586         hc->callback(hc->cbclosure, status, result);
587         free(hc);
588 }
589
590 static void svc_hooked_call(void *closure, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*), void *cbclosure)
591 {
592         struct afb_export *export = closure;
593         struct hooked_call *hc;
594
595         if (export->hooksvc & afb_hook_flag_svc_call)
596                 afb_hook_svc_call(export, api, verb, args);
597
598         if (export->hooksvc & afb_hook_flag_svc_call_result) {
599                 hc = malloc(sizeof *hc);
600                 if (!hc)
601                         WARNING("allocation failed");
602                 else {
603                         hc->export = export;
604                         hc->callback = callback;
605                         hc->cbclosure = cbclosure;
606                         callback = svc_hooked_call_result;
607                         cbclosure = hc;
608                 }
609         }
610         svc_call(closure, api, verb, args, callback, cbclosure);
611 }
612
613 static int svc_hooked_call_sync(void *closure, const char *api, const char *verb, struct json_object *args,
614                                 struct json_object **result)
615 {
616         struct afb_export *export = closure;
617         struct json_object *resu;
618         int rc;
619
620         if (export->hooksvc & afb_hook_flag_svc_callsync)
621                 afb_hook_svc_callsync(export, api, verb, args);
622
623         rc = svc_call_sync(closure, api, verb, args, &resu);
624
625         if (export->hooksvc & afb_hook_flag_svc_callsync_result)
626                 afb_hook_svc_callsync_result(export, rc, resu);
627
628         if (result)
629                 *result = resu;
630         else
631                 json_object_put(resu);
632
633         return rc;
634 }
635
636 /* the interface for services */
637 static const struct afb_service_itf service_itf = {
638         .call = svc_call,
639         .call_sync = svc_call_sync
640 };
641
642 /* the interface for services */
643 static const struct afb_service_itf hooked_service_itf = {
644         .call = svc_hooked_call,
645         .call_sync = svc_hooked_call_sync
646 };
647
648 /*************************************************************************************************************
649  *************************************************************************************************************
650  *************************************************************************************************************
651  *************************************************************************************************************
652                                            F R O M     S V C
653  *************************************************************************************************************
654  *************************************************************************************************************
655  *************************************************************************************************************
656  *************************************************************************************************************/
657
658 /*
659  * Propagates the event to the service
660  */
661 static void export_on_event_v12(void *closure, const char *event, int eventid, struct json_object *object)
662 {
663         struct afb_export *export = closure;
664
665         if (export->hooksvc & afb_hook_flag_svc_on_event_before)
666                 afb_hook_svc_on_event_before(export, event, eventid, object);
667         export->on_event.v12(event, object);
668         if (export->hooksvc & afb_hook_flag_svc_on_event_after)
669                 afb_hook_svc_on_event_after(export, event, eventid, object);
670         json_object_put(object);
671 }
672
673 /* the interface for events */
674 static const struct afb_evt_itf evt_v12_itf = {
675         .broadcast = export_on_event_v12,
676         .push = export_on_event_v12
677 };
678
679 /*************************************************************************************************************
680  *************************************************************************************************************
681  *************************************************************************************************************
682  *************************************************************************************************************
683                                            M E R G E D
684  *************************************************************************************************************
685  *************************************************************************************************************
686  *************************************************************************************************************
687  *************************************************************************************************************/
688
689 static struct afb_export *create(struct afb_apiset *apiset, const char *apiname, enum afb_api_version version)
690 {
691         struct afb_export *export;
692
693         /* session shared with other exports */
694         if (common_session == NULL) {
695                 common_session = afb_session_create (NULL, 0);
696                 if (common_session == NULL)
697                         return NULL;
698         }
699         export = calloc(1, sizeof *export);
700         if (!export)
701                 errno = ENOMEM;
702         else {
703                 memset(export, 0, sizeof *export);
704                 export->apiname = strdup(apiname);
705                 export->version = version;
706                 export->state = Api_State_Pre_Init;
707                 export->session = afb_session_addref(common_session);
708                 export->apiset = afb_apiset_addref(apiset);
709         }
710         return export;
711 }
712
713 void afb_export_destroy(struct afb_export *export)
714 {
715         if (export) {
716                 if (export->listener != NULL)
717                         afb_evt_listener_unref(export->listener);
718                 afb_session_unref(export->session);
719                 afb_apiset_unref(export->apiset);
720                 free(export->apiname);
721                 free(export);
722         }
723 }
724
725 struct afb_export *afb_export_create_v1(struct afb_apiset *apiset, const char *apiname, int (*init)(struct afb_service), void (*onevent)(const char*, struct json_object*))
726 {
727         struct afb_export *export = create(apiset, apiname, Api_Version_1);
728         if (export) {
729                 export->init.v1 = init;
730                 export->on_event.v12 = onevent;
731                 export->export.v1.verbosity = verbosity;
732                 export->export.v1.mode = AFB_MODE_LOCAL;
733                 export->export.v1.daemon.closure = export;
734                 afb_export_update_hook(export);
735         }
736         return export;
737 }
738
739 struct afb_export *afb_export_create_v2(struct afb_apiset *apiset, const char *apiname, struct afb_binding_data_v2 *data, int (*init)(), void (*onevent)(const char*, struct json_object*))
740 {
741         struct afb_export *export = create(apiset, apiname, Api_Version_2);
742         if (export) {
743                 export->init.v2 = init;
744                 export->on_event.v12 = onevent;
745                 export->export.v2 = data;
746                 data->verbosity = verbosity;
747                 data->daemon.closure = export;
748                 data->service.closure = export;
749                 afb_export_update_hook(export);
750         }
751         return export;
752 }
753
754 void afb_export_rename(struct afb_export *export, const char *apiname)
755 {
756         free(export->apiname);
757         export->apiname = strdup(apiname);
758         afb_export_update_hook(export);
759 }
760
761 const char *afb_export_apiname(const struct afb_export *export)
762 {
763         return export->apiname;
764 }
765
766 void afb_export_update_hook(struct afb_export *export)
767 {
768         export->hookditf = afb_hook_flags_ditf(export->apiname);
769         export->hooksvc = afb_hook_flags_svc(export->apiname);
770         switch (export->version) {
771         case Api_Version_1:
772                 export->export.v1.daemon.itf = export->hookditf ? &hooked_daemon_itf : &daemon_itf;
773                 break;
774         default:
775         case Api_Version_2:
776                 export->export.v2->daemon.itf = export->hookditf ? &hooked_daemon_itf : &daemon_itf;
777                 export->export.v2->service.itf = export->hooksvc ? &hooked_service_itf : &service_itf;
778                 break;
779         }
780 }
781
782 struct afb_binding_interface_v1 *afb_export_get_interface_v1(struct afb_export *export)
783 {
784         return export->version == Api_Version_1 ? &export->export.v1 : NULL;
785 }
786
787 int afb_export_unshare_session(struct afb_export *export)
788 {
789         if (export->session == common_session) {
790                 export->session = afb_session_create (NULL, 0);
791                 if (export->session)
792                         afb_session_unref(common_session);
793                 else {
794                         export->session = common_session;
795                         return -1;
796                 }
797         }
798         return 0;
799 }
800
801 void afb_export_set_apiset(struct afb_export *export, struct afb_apiset *apiset)
802 {
803         struct afb_apiset *prvset = export->apiset;
804         export->apiset = afb_apiset_addref(apiset);
805         afb_apiset_unref(prvset);
806 }
807
808 struct afb_apiset *afb_export_get_apiset(struct afb_export *export)
809 {
810         return export->apiset;
811 }
812
813 /*
814  * Creates a new service
815  */
816 int afb_export_handle_events_v12(struct afb_export *export, void (*on_event)(const char *event, struct json_object *object))
817 {
818         /* check version */
819         switch (export->version) {
820         case Api_Version_1: case Api_Version_2: break;
821         default:
822                 ERROR("invalid version 12 for API %s", export->apiname);
823                 errno = EINVAL;
824                 return -1;
825         }
826
827         /* set the event handler */
828         if (!on_event) {
829                 if (export->listener) {
830                         afb_evt_listener_unref(export->listener);
831                         export->listener = NULL;
832                 }
833                 export->on_event.v12 = on_event;
834         } else {
835                 export->on_event.v12 = on_event;
836                 if (!export->listener) {
837                         export->listener = afb_evt_listener_create(&evt_v12_itf, export);
838                         if (export->listener == NULL)
839                                 return -1;
840                 }
841         }
842         return 0;
843 }
844
845 /*
846  * Starts a new service (v1)
847  */
848 struct afb_binding_v1 *afb_export_register_v1(struct afb_export *export, struct afb_binding_v1 *(*regfun)(const struct afb_binding_interface_v1*))
849 {
850         return regfun(&export->export.v1);
851 }
852
853 int afb_export_verbosity_get(const struct afb_export *export)
854 {
855         switch (export->version) {
856         case Api_Version_1: return export->export.v1.verbosity;
857         case Api_Version_2: return export->export.v2->verbosity;
858         }
859         return verbosity;
860 }
861
862 void afb_export_verbosity_set(struct afb_export *export, int level)
863 {
864         switch (export->version) {
865         case Api_Version_1: export->export.v1.verbosity = level; break;
866         case Api_Version_2: export->export.v2->verbosity = level; break;
867         }
868 }
869
870 /*************************************************************************************************************
871  *************************************************************************************************************
872  *************************************************************************************************************
873  *************************************************************************************************************
874                                            N E W
875  *************************************************************************************************************
876  *************************************************************************************************************
877  *************************************************************************************************************
878  *************************************************************************************************************/
879
880 int afb_export_start(struct afb_export *export, int share_session, int onneed, struct afb_apiset *apiset)
881 {
882         int rc;
883
884         /* check state */
885         if (export->state != Api_State_Pre_Init) {
886                 /* not an error when onneed */
887                 if (onneed != 0)
888                         goto done;
889
890                 /* already started: it is an error */
891                 ERROR("Service of API %s already started", export->apiname);
892                 return -1;
893         }
894
895         /* unshare the session if asked */
896         if (!share_session) {
897                 rc = afb_export_unshare_session(export);
898                 if (rc < 0) {
899                         ERROR("Can't unshare the session for %s", export->apiname);
900                         return -1;
901                 }
902         }
903
904         /* set event handling */
905         switch (export->version) {
906         case Api_Version_1:
907         case Api_Version_2:
908                 rc = afb_export_handle_events_v12(export, export->on_event.v12);
909                 break;
910         default:
911                 rc = 0;
912                 break;
913         }
914         if (rc < 0) {
915                 ERROR("Can't set event handler for %s", export->apiname);
916                 return -1;
917         }
918
919         /* Starts the service */
920         if (export->hooksvc & afb_hook_flag_svc_start_before)
921                 afb_hook_svc_start_before(export);
922         export->state = Api_State_Init;
923         switch (export->version) {
924         case Api_Version_1:
925                 rc = export->init.v1 ? export->init.v1((struct afb_service){ .itf = &hooked_service_itf, .closure = export }) : 0;
926                 break;
927         case Api_Version_2:
928                 rc = export->init.v2 ? export->init.v2() : 0;
929                 break;
930         default:
931                 break;
932         }
933         export->state = Api_State_Run;
934         if (export->hooksvc & afb_hook_flag_svc_start_after)
935                 afb_hook_svc_start_after(export, rc);
936         if (rc < 0) {
937                 /* initialisation error */
938                 ERROR("Initialisation of service API %s failed (%d): %m", export->apiname, rc);
939                 return rc;
940         }
941
942 done:
943         return 0;
944 }
945