2 * Copyright (C) 2016, 2017 "IoT.bzh"
3 * Author: José Bollo <jose.bollo@iot.bzh>
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
24 #include <json-c/json.h>
26 #define AFB_BINDING_VERSION 0
27 #include <afb/afb-binding.h>
30 #include "afb-apiset.h"
31 #include "afb-api-dyn.h"
32 #include "afb-common.h"
35 #include "afb-export.h"
37 #include "afb-msg-json.h"
38 #include "afb-session.h"
43 /*************************************************************************
44 * internal types and structures
45 ************************************************************************/
64 struct afb_dynapi dynapi;
69 /* version of the api */
80 struct afb_api_dyn *apidyn;
82 /* session for service */
83 struct afb_session *session;
85 /* apiset for service */
86 struct afb_apiset *apiset;
88 /* event listener for service or NULL */
89 struct afb_evt_listener *listener;
93 int (*v1)(struct afb_service);
95 int (*vdyn)(struct afb_dynapi *dynapi);
100 void (*v12)(const char *event, struct json_object *object);
101 void (*vdyn)(struct afb_dynapi *dynapi, const char *event, struct json_object *object);
106 struct afb_binding_interface_v1 v1;
107 struct afb_binding_data_v2 *v2;
111 /************************************************************************************************************/
113 static inline struct afb_dynapi *to_dynapi(struct afb_export *export)
115 return (struct afb_dynapi*)export;
118 static inline struct afb_export *from_dynapi(struct afb_dynapi *dynapi)
120 return (struct afb_export*)dynapi;
123 /*************************************************************************************************************
124 *************************************************************************************************************
125 *************************************************************************************************************
126 *************************************************************************************************************
128 *************************************************************************************************************
129 *************************************************************************************************************
130 *************************************************************************************************************
131 *************************************************************************************************************/
133 /**********************************************
135 **********************************************/
136 static void vverbose_cb(void *closure, int level, const char *file, int line, const char *function, const char *fmt, va_list args)
139 struct afb_export *export = closure;
141 if (!fmt || vasprintf(&p, fmt, args) < 0)
142 vverbose(level, file, line, function, fmt, args);
144 verbose(level, file, line, function, "[API %s] %s", export->apiname, p);
149 static void old_vverbose_cb(void *closure, int level, const char *file, int line, const char *fmt, va_list args)
151 vverbose_cb(closure, level, file, line, NULL, fmt, args);
154 static struct afb_eventid *eventid_make_cb(void *closure, const char *name)
158 struct afb_export *export = closure;
160 /* check daemon state */
161 if (export->state == Api_State_Pre_Init) {
162 ERROR("[API %s] Bad call to 'afb_daemon_event_make(%s)', must not be in PreInit", export->apiname, name);
167 /* makes the event name */
168 plen = strlen(export->apiname);
170 event = alloca(nlen + plen + 2);
171 memcpy(event, export->apiname, plen);
173 memcpy(event + plen + 1, name, nlen + 1);
175 /* create the event */
176 return afb_evt_eventid_create(event);
179 static struct afb_event event_make_cb(void *closure, const char *name)
181 struct afb_eventid *eventid = eventid_make_cb(closure, name);
182 return (struct afb_event){ .itf = eventid ? eventid->itf : NULL, .closure = eventid };
185 static int event_broadcast_cb(void *closure, const char *name, struct json_object *object)
189 struct afb_export *export = closure;
191 /* check daemon state */
192 if (export->state == Api_State_Pre_Init) {
193 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));
198 /* makes the event name */
199 plen = strlen(export->apiname);
201 event = alloca(nlen + plen + 2);
202 memcpy(event, export->apiname, plen);
204 memcpy(event + plen + 1, name, nlen + 1);
206 /* broadcast the event */
207 return afb_evt_broadcast(event, object);
210 static int rootdir_open_locale_cb(void *closure, const char *filename, int flags, const char *locale)
212 return afb_common_rootdir_open_locale(filename, flags, locale);
215 static int queue_job_cb(void *closure, void (*callback)(int signum, void *arg), void *argument, void *group, int timeout)
217 return jobs_queue(group, timeout, callback, argument);
220 static struct afb_req unstore_req_cb(void *closure, struct afb_stored_req *sreq)
222 return afb_xreq_unstore(sreq);
225 static int require_api_cb(void *closure, const char *name, int initialized)
227 struct afb_export *export = closure;
228 if (export->state != Api_State_Init) {
229 ERROR("[API %s] Bad call to 'afb_daemon_require(%s, %d)', must be in Init", export->apiname, name, initialized);
233 return -!(initialized ? afb_apiset_lookup_started : afb_apiset_lookup)(export->apiset, name, 1);
236 static int rename_api_cb(void *closure, const char *name)
238 struct afb_export *export = closure;
239 if (export->state != Api_State_Pre_Init) {
240 ERROR("[API %s] Bad call to 'afb_daemon_rename(%s)', must be in PreInit", export->apiname, name);
244 if (!afb_api_is_valid_name(name)) {
245 ERROR("[API %s] Can't rename to %s: bad API name", export->apiname, name);
249 NOTICE("[API %s] renamed to [API %s]", export->apiname, name);
250 afb_export_rename(export, name);
254 static int api_new_api_cb(
258 int (*preinit)(void*, struct afb_dynapi *),
259 void *preinit_closure)
261 struct afb_export *export = closure;
262 return afb_api_dyn_add(export->apiset, api, info, preinit, preinit_closure);
265 /**********************************************
267 **********************************************/
268 static void hooked_vverbose_cb(void *closure, int level, const char *file, int line, const char *function, const char *fmt, va_list args)
270 struct afb_export *export = closure;
273 vverbose_cb(closure, level, file, line, function, fmt, args);
274 afb_hook_ditf_vverbose(export, level, file, line, function, fmt, ap);
278 static void hooked_old_vverbose_cb(void *closure, int level, const char *file, int line, const char *fmt, va_list args)
280 hooked_vverbose_cb(closure, level, file, line, NULL, fmt, args);
283 static struct afb_eventid *hooked_eventid_make_cb(void *closure, const char *name)
285 struct afb_export *export = closure;
286 struct afb_eventid *r = eventid_make_cb(closure, name);
287 afb_hook_ditf_event_make(export, name, r);
291 static struct afb_event hooked_event_make_cb(void *closure, const char *name)
293 struct afb_eventid *eventid = hooked_eventid_make_cb(closure, name);
294 return (struct afb_event){ .itf = eventid ? eventid->itf : NULL, .closure = eventid };
297 static int hooked_event_broadcast_cb(void *closure, const char *name, struct json_object *object)
300 struct afb_export *export = closure;
301 json_object_get(object);
302 afb_hook_ditf_event_broadcast_before(export, name, json_object_get(object));
303 r = event_broadcast_cb(closure, name, object);
304 afb_hook_ditf_event_broadcast_after(export, name, object, r);
305 json_object_put(object);
309 static struct sd_event *hooked_get_event_loop(void *closure)
311 struct afb_export *export = closure;
312 struct sd_event *r = afb_common_get_event_loop();
313 return afb_hook_ditf_get_event_loop(export, r);
316 static struct sd_bus *hooked_get_user_bus(void *closure)
318 struct afb_export *export = closure;
319 struct sd_bus *r = afb_common_get_user_bus();
320 return afb_hook_ditf_get_user_bus(export, r);
323 static struct sd_bus *hooked_get_system_bus(void *closure)
325 struct afb_export *export = closure;
326 struct sd_bus *r = afb_common_get_system_bus();
327 return afb_hook_ditf_get_system_bus(export, r);
330 static int hooked_rootdir_get_fd(void *closure)
332 struct afb_export *export = closure;
333 int r = afb_common_rootdir_get_fd();
334 return afb_hook_ditf_rootdir_get_fd(export, r);
337 static int hooked_rootdir_open_locale_cb(void *closure, const char *filename, int flags, const char *locale)
339 struct afb_export *export = closure;
340 int r = rootdir_open_locale_cb(closure, filename, flags, locale);
341 return afb_hook_ditf_rootdir_open_locale(export, filename, flags, locale, r);
344 static int hooked_queue_job_cb(void *closure, void (*callback)(int signum, void *arg), void *argument, void *group, int timeout)
346 struct afb_export *export = closure;
347 int r = queue_job_cb(closure, callback, argument, group, timeout);
348 return afb_hook_ditf_queue_job(export, callback, argument, group, timeout, r);
351 static struct afb_req hooked_unstore_req_cb(void *closure, struct afb_stored_req *sreq)
353 struct afb_export *export = closure;
354 afb_hook_ditf_unstore_req(export, sreq);
355 return unstore_req_cb(closure, sreq);
358 static int hooked_require_api_cb(void *closure, const char *name, int initialized)
361 struct afb_export *export = closure;
362 afb_hook_ditf_require_api(export, name, initialized);
363 result = require_api_cb(closure, name, initialized);
364 return afb_hook_ditf_require_api_result(export, name, initialized, result);
367 static int hooked_rename_api_cb(void *closure, const char *name)
369 struct afb_export *export = closure;
370 const char *oldname = export->apiname;
371 int result = rename_api_cb(closure, name);
372 return afb_hook_ditf_rename_api(export, oldname, name, result);
375 static int hooked_api_new_api_cb(
379 int (*preinit)(void*, struct afb_dynapi *),
380 void *preinit_closure)
383 return api_new_api_cb(closure, api, info, preinit, preinit_closure);
385 /**********************************************
387 **********************************************/
388 static const struct afb_daemon_itf daemon_itf = {
389 .vverbose_v1 = old_vverbose_cb,
390 .vverbose_v2 = vverbose_cb,
391 .event_make = event_make_cb,
392 .event_broadcast = event_broadcast_cb,
393 .get_event_loop = afb_common_get_event_loop,
394 .get_user_bus = afb_common_get_user_bus,
395 .get_system_bus = afb_common_get_system_bus,
396 .rootdir_get_fd = afb_common_rootdir_get_fd,
397 .rootdir_open_locale = rootdir_open_locale_cb,
398 .queue_job = queue_job_cb,
399 .unstore_req = unstore_req_cb,
400 .require_api = require_api_cb,
401 .rename_api = rename_api_cb,
402 .new_api = api_new_api_cb,
405 static const struct afb_daemon_itf hooked_daemon_itf = {
406 .vverbose_v1 = hooked_old_vverbose_cb,
407 .vverbose_v2 = hooked_vverbose_cb,
408 .event_make = hooked_event_make_cb,
409 .event_broadcast = hooked_event_broadcast_cb,
410 .get_event_loop = hooked_get_event_loop,
411 .get_user_bus = hooked_get_user_bus,
412 .get_system_bus = hooked_get_system_bus,
413 .rootdir_get_fd = hooked_rootdir_get_fd,
414 .rootdir_open_locale = hooked_rootdir_open_locale_cb,
415 .queue_job = hooked_queue_job_cb,
416 .unstore_req = hooked_unstore_req_cb,
417 .require_api = hooked_require_api_cb,
418 .rename_api = hooked_rename_api_cb,
419 .new_api = hooked_api_new_api_cb,
422 /*************************************************************************************************************
423 *************************************************************************************************************
424 *************************************************************************************************************
425 *************************************************************************************************************
427 *************************************************************************************************************
428 *************************************************************************************************************
429 *************************************************************************************************************
430 *************************************************************************************************************/
432 /* the common session for services sharing their session */
433 static struct afb_session *common_session;
435 /*************************************************************************************************************
436 *************************************************************************************************************
437 *************************************************************************************************************
438 *************************************************************************************************************
440 *************************************************************************************************************
441 *************************************************************************************************************
442 *************************************************************************************************************
443 *************************************************************************************************************/
446 * Structure for requests initiated by the service
450 struct afb_xreq xreq;
452 struct afb_export *export;
456 void (*callback)(void*, int, struct json_object*);
457 void (*callback_dynapi)(void*, int, struct json_object*, struct afb_dynapi*);
462 struct jobloop *jobloop;
463 struct json_object *result;
468 * destroys the call_req
470 static void callreq_destroy(struct afb_xreq *xreq)
472 struct call_req *callreq = CONTAINER_OF_XREQ(struct call_req, xreq);
474 afb_context_disconnect(&callreq->xreq.context);
475 json_object_put(callreq->xreq.json);
476 afb_cred_unref(callreq->xreq.cred);
480 static void callreq_reply_async(struct afb_xreq *xreq, int status, json_object *obj)
482 struct call_req *callreq = CONTAINER_OF_XREQ(struct call_req, xreq);
483 if (callreq->callback)
484 callreq->callback(callreq->closure, status, obj);
485 json_object_put(obj);
488 static void callreq_reply_async_dynapi(struct afb_xreq *xreq, int status, json_object *obj)
490 struct call_req *callreq = CONTAINER_OF_XREQ(struct call_req, xreq);
491 if (callreq->callback_dynapi)
492 callreq->callback_dynapi(callreq->closure, status, obj, to_dynapi(callreq->export));
493 json_object_put(obj);
496 static void callreq_sync_leave(struct call_req *callreq)
498 struct jobloop *jobloop = callreq->jobloop;
501 callreq->jobloop = NULL;
506 static void callreq_reply_sync(struct afb_xreq *xreq, int status, json_object *obj)
508 struct call_req *callreq = CONTAINER_OF_XREQ(struct call_req, xreq);
509 callreq->status = status;
510 callreq->result = obj;
511 callreq_sync_leave(callreq);
514 static void callreq_sync_enter(int signum, void *closure, struct jobloop *jobloop)
516 struct call_req *callreq = closure;
519 callreq->jobloop = jobloop;
520 afb_xreq_process(&callreq->xreq, callreq->export->apiset);
522 callreq->result = afb_msg_json_internal_error();
523 callreq->status = -1;
524 callreq_sync_leave(callreq);
528 /* interface for requests of services */
529 const struct afb_xreq_query_itf afb_export_xreq_async_itf = {
530 .unref = callreq_destroy,
531 .reply = callreq_reply_async
534 /* interface for requests of services */
535 const struct afb_xreq_query_itf afb_export_xreq_async_dynapi_itf = {
536 .unref = callreq_destroy,
537 .reply = callreq_reply_async_dynapi
540 /* interface for requests of services */
541 const struct afb_xreq_query_itf afb_export_xreq_sync_itf = {
542 .unref = callreq_destroy,
543 .reply = callreq_reply_sync
549 static struct call_req *callreq_create(
550 struct afb_export *export,
553 struct json_object *args,
554 const struct afb_xreq_query_itf *itf)
556 struct call_req *callreq;
557 size_t lenapi, lenverb;
560 /* allocates the request */
561 lenapi = 1 + strlen(api);
562 lenverb = 1 + strlen(verb);
563 callreq = malloc(lenapi + lenverb + sizeof *callreq);
564 if (callreq != NULL) {
565 /* initialises the request */
566 afb_xreq_init(&callreq->xreq, itf);
567 afb_context_init(&callreq->xreq.context, export->session, NULL);
568 callreq->xreq.context.validated = 1;
569 copy = (char*)&callreq[1];
570 memcpy(copy, api, lenapi);
571 callreq->xreq.request.api = copy;
572 copy = ©[lenapi];
573 memcpy(copy, verb, lenverb);
574 callreq->xreq.request.verb = copy;
575 callreq->xreq.listener = export->listener;
576 callreq->xreq.json = args;
577 callreq->export = export;
583 * Initiates a call for the service
585 static void svc_call(
589 struct json_object *args,
590 void (*callback)(void*, int, struct json_object*),
593 struct afb_export *export = closure;
594 struct call_req *callreq;
595 struct json_object *ierr;
597 /* allocates the request */
598 callreq = callreq_create(export, api, verb, args, &afb_export_xreq_async_itf);
599 if (callreq == NULL) {
600 ERROR("out of memory");
601 json_object_put(args);
602 ierr = afb_msg_json_internal_error();
604 callback(cbclosure, -1, ierr);
605 json_object_put(ierr);
609 /* initialises the request */
610 callreq->jobloop = NULL;
611 callreq->callback = callback;
612 callreq->closure = cbclosure;
614 /* terminates and frees ressources if needed */
615 afb_xreq_process(&callreq->xreq, export->apiset);
618 static void svc_call_dynapi(
619 struct afb_dynapi *dynapi,
622 struct json_object *args,
623 void (*callback)(void*, int, struct json_object*, struct afb_dynapi*),
626 struct afb_export *export = from_dynapi(dynapi);
627 struct call_req *callreq;
628 struct json_object *ierr;
630 /* allocates the request */
631 callreq = callreq_create(export, api, verb, args, &afb_export_xreq_async_dynapi_itf);
632 if (callreq == NULL) {
633 ERROR("out of memory");
634 json_object_put(args);
635 ierr = afb_msg_json_internal_error();
637 callback(cbclosure, -1, ierr, to_dynapi(export));
638 json_object_put(ierr);
642 /* initialises the request */
643 callreq->jobloop = NULL;
644 callreq->callback_dynapi = callback;
645 callreq->closure = cbclosure;
647 /* terminates and frees ressources if needed */
648 afb_xreq_process(&callreq->xreq, export->apiset);
651 static int svc_call_sync(
655 struct json_object *args,
656 struct json_object **result)
658 struct afb_export *export = closure;
659 struct call_req *callreq;
660 struct json_object *resu;
663 /* allocates the request */
664 callreq = callreq_create(export, api, verb, args, &afb_export_xreq_sync_itf);
665 if (callreq == NULL) {
666 ERROR("out of memory");
668 json_object_put(args);
669 resu = afb_msg_json_internal_error();
672 /* initialises the request */
673 callreq->jobloop = NULL;
674 callreq->callback = NULL;
675 callreq->result = NULL;
677 afb_xreq_unhooked_addref(&callreq->xreq); /* avoid early callreq destruction */
678 rc = jobs_enter(NULL, 0, callreq_sync_enter, callreq);
680 rc = callreq->status;
681 resu = (rc >= 0 || callreq->result) ? callreq->result : afb_msg_json_internal_error();
682 afb_xreq_unhooked_unref(&callreq->xreq);
687 json_object_put(resu);
693 struct afb_export *export;
695 void (*callback)(void*, int, struct json_object*);
696 void (*callback_dynapi)(void*, int, struct json_object*, struct afb_dynapi*);
701 static void svc_hooked_call_result(void *closure, int status, struct json_object *result)
703 struct hooked_call *hc = closure;
704 afb_hook_svc_call_result(hc->export, status, result);
705 hc->callback(hc->cbclosure, status, result);
709 static void svc_hooked_call_dynapi_result(void *closure, int status, struct json_object *result, struct afb_dynapi *dynapi)
711 struct hooked_call *hc = closure;
712 afb_hook_svc_call_result(hc->export, status, result);
713 hc->callback_dynapi(hc->cbclosure, status, result, dynapi);
717 static void svc_hooked_call(
721 struct json_object *args,
722 void (*callback)(void*, int, struct json_object*),
725 struct afb_export *export = closure;
726 struct hooked_call *hc;
728 if (export->hooksvc & afb_hook_flag_svc_call)
729 afb_hook_svc_call(export, api, verb, args);
731 if (export->hooksvc & afb_hook_flag_svc_call_result) {
732 hc = malloc(sizeof *hc);
734 WARNING("allocation failed");
737 hc->callback = callback;
738 hc->cbclosure = cbclosure;
739 callback = svc_hooked_call_result;
743 svc_call(closure, api, verb, args, callback, cbclosure);
746 static void svc_hooked_call_dynapi(
747 struct afb_dynapi *dynapi,
750 struct json_object *args,
751 void (*callback)(void*, int, struct json_object*, struct afb_dynapi*),
754 struct afb_export *export = from_dynapi(dynapi);
755 struct hooked_call *hc;
757 if (export->hooksvc & afb_hook_flag_svc_call)
758 afb_hook_svc_call(export, api, verb, args);
760 if (export->hooksvc & afb_hook_flag_svc_call_result) {
761 hc = malloc(sizeof *hc);
763 WARNING("allocation failed");
766 hc->callback_dynapi = callback;
767 hc->cbclosure = cbclosure;
768 callback = svc_hooked_call_dynapi_result;
772 svc_call_dynapi(dynapi, api, verb, args, callback, cbclosure);
775 static int svc_hooked_call_sync(
779 struct json_object *args,
780 struct json_object **result)
782 struct afb_export *export = closure;
783 struct json_object *resu;
786 if (export->hooksvc & afb_hook_flag_svc_callsync)
787 afb_hook_svc_callsync(export, api, verb, args);
789 rc = svc_call_sync(closure, api, verb, args, &resu);
791 if (export->hooksvc & afb_hook_flag_svc_callsync_result)
792 afb_hook_svc_callsync_result(export, rc, resu);
797 json_object_put(resu);
802 /* the interface for services */
803 static const struct afb_service_itf service_itf = {
805 .call_sync = svc_call_sync
808 /* the interface for services */
809 static const struct afb_service_itf hooked_service_itf = {
810 .call = svc_hooked_call,
811 .call_sync = svc_hooked_call_sync
814 /*************************************************************************************************************
815 *************************************************************************************************************
816 *************************************************************************************************************
817 *************************************************************************************************************
819 *************************************************************************************************************
820 *************************************************************************************************************
821 *************************************************************************************************************
822 *************************************************************************************************************/
824 static int api_set_verbs_v2_cb(
825 struct afb_dynapi *dynapi,
826 const struct afb_verb_v2 *verbs)
828 struct afb_export *export = from_dynapi(dynapi);
830 if (export->apidyn) {
831 afb_api_dyn_set_verbs_v2(export->apidyn, verbs);
839 static int api_add_verb_cb(
840 struct afb_dynapi *dynapi,
843 void (*callback)(struct afb_request *request),
845 const struct afb_auth *auth,
848 struct afb_export *export = from_dynapi(dynapi);
851 return afb_api_dyn_add_verb(export->apidyn, verb, info, callback, vcbdata, auth, session);
857 static int api_sub_verb_cb(
858 struct afb_dynapi *dynapi,
861 struct afb_export *export = from_dynapi(dynapi);
864 return afb_api_dyn_sub_verb(export->apidyn, verb);
870 static int api_set_on_event_cb(
871 struct afb_dynapi *dynapi,
872 void (*onevent)(struct afb_dynapi *dynapi, const char *event, struct json_object *object))
874 struct afb_export *export = from_dynapi(dynapi);
875 return afb_export_handle_events_vdyn(export, onevent);
878 static int api_set_on_init_cb(
879 struct afb_dynapi *dynapi,
880 int (*oninit)(struct afb_dynapi *dynapi))
882 struct afb_export *export = from_dynapi(dynapi);
884 return afb_export_handle_init_vdyn(export, oninit);
887 static void api_seal_cb(
888 struct afb_dynapi *dynapi)
890 struct afb_export *export = from_dynapi(dynapi);
892 export->apidyn = NULL;
895 static int hooked_api_set_verbs_v2_cb(
896 struct afb_dynapi *dynapi,
897 const struct afb_verb_v2 *verbs)
900 return api_set_verbs_v2_cb(dynapi, verbs);
903 static int hooked_api_add_verb_cb(
904 struct afb_dynapi *dynapi,
907 void (*callback)(struct afb_request *request),
909 const struct afb_auth *auth,
913 return api_add_verb_cb(dynapi, verb, info, callback, vcbdata, auth, session);
916 static int hooked_api_sub_verb_cb(
917 struct afb_dynapi *dynapi,
921 return api_sub_verb_cb(dynapi, verb);
924 static int hooked_api_set_on_event_cb(
925 struct afb_dynapi *dynapi,
926 void (*onevent)(struct afb_dynapi *dynapi, const char *event, struct json_object *object))
929 return api_set_on_event_cb(dynapi, onevent);
932 static int hooked_api_set_on_init_cb(
933 struct afb_dynapi *dynapi,
934 int (*oninit)(struct afb_dynapi *dynapi))
937 return api_set_on_init_cb(dynapi, oninit);
940 static void hooked_api_seal_cb(
941 struct afb_dynapi *dynapi)
947 static const struct afb_dynapi_itf dynapi_itf = {
949 .vverbose = (void*)vverbose_cb,
951 .get_event_loop = afb_common_get_event_loop,
952 .get_user_bus = afb_common_get_user_bus,
953 .get_system_bus = afb_common_get_system_bus,
954 .rootdir_get_fd = afb_common_rootdir_get_fd,
955 .rootdir_open_locale = rootdir_open_locale_cb,
956 .queue_job = queue_job_cb,
958 .require_api = require_api_cb,
959 .rename_api = rename_api_cb,
961 .event_broadcast = event_broadcast_cb,
962 .eventid_make = eventid_make_cb,
964 .call = svc_call_dynapi,
965 .call_sync = svc_call_sync,
967 .api_new_api = api_new_api_cb,
968 .api_set_verbs_v2 = api_set_verbs_v2_cb,
969 .api_add_verb = api_add_verb_cb,
970 .api_sub_verb = api_sub_verb_cb,
971 .api_set_on_event = api_set_on_event_cb,
972 .api_set_on_init = api_set_on_init_cb,
973 .api_seal = api_seal_cb,
976 static const struct afb_dynapi_itf hooked_dynapi_itf = {
978 .vverbose = hooked_vverbose_cb,
980 .get_event_loop = hooked_get_event_loop,
981 .get_user_bus = hooked_get_user_bus,
982 .get_system_bus = hooked_get_system_bus,
983 .rootdir_get_fd = hooked_rootdir_get_fd,
984 .rootdir_open_locale = hooked_rootdir_open_locale_cb,
985 .queue_job = hooked_queue_job_cb,
987 .require_api = hooked_require_api_cb,
988 .rename_api = hooked_rename_api_cb,
990 .event_broadcast = hooked_event_broadcast_cb,
991 .eventid_make = hooked_eventid_make_cb,
993 .call = svc_hooked_call_dynapi,
994 .call_sync = svc_hooked_call_sync,
996 .api_new_api = hooked_api_new_api_cb,
997 .api_set_verbs_v2 = hooked_api_set_verbs_v2_cb,
998 .api_add_verb = hooked_api_add_verb_cb,
999 .api_sub_verb = hooked_api_sub_verb_cb,
1000 .api_set_on_event = hooked_api_set_on_event_cb,
1001 .api_set_on_init = hooked_api_set_on_init_cb,
1002 .api_seal = hooked_api_seal_cb,
1005 /*************************************************************************************************************
1006 *************************************************************************************************************
1007 *************************************************************************************************************
1008 *************************************************************************************************************
1010 *************************************************************************************************************
1011 *************************************************************************************************************
1012 *************************************************************************************************************
1013 *************************************************************************************************************/
1016 * Propagates the event to the service
1018 static void export_on_event_v12(void *closure, const char *event, int eventid, struct json_object *object)
1020 struct afb_export *export = closure;
1022 if (export->hooksvc & afb_hook_flag_svc_on_event_before)
1023 afb_hook_svc_on_event_before(export, event, eventid, object);
1024 export->on_event.v12(event, object);
1025 if (export->hooksvc & afb_hook_flag_svc_on_event_after)
1026 afb_hook_svc_on_event_after(export, event, eventid, object);
1027 json_object_put(object);
1030 /* the interface for events */
1031 static const struct afb_evt_itf evt_v12_itf = {
1032 .broadcast = export_on_event_v12,
1033 .push = export_on_event_v12
1037 * Propagates the event to the service
1039 static void export_on_event_vdyn(void *closure, const char *event, int eventid, struct json_object *object)
1041 struct afb_export *export = closure;
1043 if (export->hooksvc & afb_hook_flag_svc_on_event_before)
1044 afb_hook_svc_on_event_before(export, event, eventid, object);
1045 export->on_event.vdyn(to_dynapi(export), event, object);
1046 if (export->hooksvc & afb_hook_flag_svc_on_event_after)
1047 afb_hook_svc_on_event_after(export, event, eventid, object);
1048 json_object_put(object);
1051 /* the interface for events */
1052 static const struct afb_evt_itf evt_vdyn_itf = {
1053 .broadcast = export_on_event_vdyn,
1054 .push = export_on_event_vdyn
1057 /*************************************************************************************************************
1058 *************************************************************************************************************
1059 *************************************************************************************************************
1060 *************************************************************************************************************
1062 *************************************************************************************************************
1063 *************************************************************************************************************
1064 *************************************************************************************************************
1065 *************************************************************************************************************/
1067 static struct afb_export *create(struct afb_apiset *apiset, const char *apiname, enum afb_api_version version)
1069 struct afb_export *export;
1071 /* session shared with other exports */
1072 if (common_session == NULL) {
1073 common_session = afb_session_create (NULL, 0);
1074 if (common_session == NULL)
1077 export = calloc(1, sizeof *export);
1081 memset(export, 0, sizeof *export);
1082 export->apiname = strdup(apiname);
1083 export->dynapi.apiname = export->apiname;
1084 export->version = version;
1085 export->state = Api_State_Pre_Init;
1086 export->session = afb_session_addref(common_session);
1087 export->apiset = afb_apiset_addref(apiset);
1092 void afb_export_destroy(struct afb_export *export)
1095 if (export->listener != NULL)
1096 afb_evt_listener_unref(export->listener);
1097 afb_session_unref(export->session);
1098 afb_apiset_unref(export->apiset);
1099 free(export->apiname);
1104 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*))
1106 struct afb_export *export = create(apiset, apiname, Api_Version_1);
1108 export->init.v1 = init;
1109 export->on_event.v12 = onevent;
1110 export->export.v1.mode = AFB_MODE_LOCAL;
1111 export->export.v1.daemon.closure = export;
1112 afb_export_verbosity_set(export, verbosity);
1113 afb_export_update_hook(export);
1118 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*))
1120 struct afb_export *export = create(apiset, apiname, Api_Version_2);
1122 export->init.v2 = init;
1123 export->on_event.v12 = onevent;
1124 export->export.v2 = data;
1125 data->daemon.closure = export;
1126 data->service.closure = export;
1127 afb_export_verbosity_set(export, verbosity);
1128 afb_export_update_hook(export);
1133 struct afb_export *afb_export_create_vdyn(struct afb_apiset *apiset, const char *apiname, struct afb_api_dyn *apidyn)
1135 struct afb_export *export = create(apiset, apiname, Api_Version_Dyn);
1137 export->apidyn = apidyn;
1138 afb_export_verbosity_set(export, verbosity);
1139 afb_export_update_hook(export);
1144 void afb_export_rename(struct afb_export *export, const char *apiname)
1146 free(export->apiname);
1147 export->apiname = strdup(apiname);
1148 export->dynapi.apiname = export->apiname;
1149 afb_export_update_hook(export);
1152 const char *afb_export_apiname(const struct afb_export *export)
1154 return export->apiname;
1157 void afb_export_update_hook(struct afb_export *export)
1159 export->hookditf = afb_hook_flags_ditf(export->apiname);
1160 export->hooksvc = afb_hook_flags_svc(export->apiname);
1161 export->dynapi.itf = export->hookditf|export->hooksvc ? &hooked_dynapi_itf : &dynapi_itf;
1163 switch (export->version) {
1165 export->export.v1.daemon.itf = export->hookditf ? &hooked_daemon_itf : &daemon_itf;
1168 export->export.v2->daemon.itf = export->hookditf ? &hooked_daemon_itf : &daemon_itf;
1169 export->export.v2->service.itf = export->hooksvc ? &hooked_service_itf : &service_itf;
1174 struct afb_binding_interface_v1 *afb_export_get_interface_v1(struct afb_export *export)
1176 return export->version == Api_Version_1 ? &export->export.v1 : NULL;
1179 int afb_export_unshare_session(struct afb_export *export)
1181 if (export->session == common_session) {
1182 export->session = afb_session_create (NULL, 0);
1183 if (export->session)
1184 afb_session_unref(common_session);
1186 export->session = common_session;
1193 void afb_export_set_apiset(struct afb_export *export, struct afb_apiset *apiset)
1195 struct afb_apiset *prvset = export->apiset;
1196 export->apiset = afb_apiset_addref(apiset);
1197 afb_apiset_unref(prvset);
1200 struct afb_apiset *afb_export_get_apiset(struct afb_export *export)
1202 return export->apiset;
1205 int afb_export_handle_events_v12(struct afb_export *export, void (*on_event)(const char *event, struct json_object *object))
1208 switch (export->version) {
1209 case Api_Version_1: case Api_Version_2: break;
1211 ERROR("invalid version 12 for API %s", export->apiname);
1216 /* set the event handler */
1218 if (export->listener) {
1219 afb_evt_listener_unref(export->listener);
1220 export->listener = NULL;
1222 export->on_event.v12 = on_event;
1224 export->on_event.v12 = on_event;
1225 if (!export->listener) {
1226 export->listener = afb_evt_listener_create(&evt_v12_itf, export);
1227 if (export->listener == NULL)
1234 int afb_export_handle_events_vdyn(struct afb_export *export, void (*on_event)(struct afb_dynapi *dynapi, const char *event, struct json_object *object))
1237 switch (export->version) {
1238 case Api_Version_Dyn: break;
1240 ERROR("invalid version Dyn for API %s", export->apiname);
1245 /* set the event handler */
1247 if (export->listener) {
1248 afb_evt_listener_unref(export->listener);
1249 export->listener = NULL;
1251 export->on_event.vdyn = on_event;
1253 export->on_event.vdyn = on_event;
1254 if (!export->listener) {
1255 export->listener = afb_evt_listener_create(&evt_vdyn_itf, export);
1256 if (export->listener == NULL)
1263 int afb_export_handle_init_vdyn(struct afb_export *export, int (*oninit)(struct afb_dynapi *dynapi))
1265 if (export->state != Api_State_Pre_Init) {
1266 ERROR("[API %s] Bad call to 'afb_dynapi_on_init', must be in PreInit", export->apiname);
1271 export->init.vdyn = oninit;
1276 * Starts a new service (v1)
1278 struct afb_binding_v1 *afb_export_register_v1(struct afb_export *export, struct afb_binding_v1 *(*regfun)(const struct afb_binding_interface_v1*))
1280 return regfun(&export->export.v1);
1283 int afb_export_preinit_vdyn(struct afb_export *export, int (*preinit)(void*, struct afb_dynapi*), void *closure)
1285 return preinit(closure, to_dynapi(export));
1288 int afb_export_verbosity_get(const struct afb_export *export)
1290 return export->dynapi.verbosity;
1293 void afb_export_verbosity_set(struct afb_export *export, int level)
1295 export->dynapi.verbosity = level;
1296 switch (export->version) {
1297 case Api_Version_1: export->export.v1.verbosity = level; break;
1298 case Api_Version_2: export->export.v2->verbosity = level; break;
1302 /*************************************************************************************************************
1303 *************************************************************************************************************
1304 *************************************************************************************************************
1305 *************************************************************************************************************
1307 *************************************************************************************************************
1308 *************************************************************************************************************
1309 *************************************************************************************************************
1310 *************************************************************************************************************/
1312 int afb_export_start(struct afb_export *export, int share_session, int onneed, struct afb_apiset *apiset)
1317 if (export->state != Api_State_Pre_Init) {
1318 /* not an error when onneed */
1322 /* already started: it is an error */
1323 ERROR("Service of API %s already started", export->apiname);
1327 /* unshare the session if asked */
1328 if (!share_session) {
1329 rc = afb_export_unshare_session(export);
1331 ERROR("Can't unshare the session for %s", export->apiname);
1336 /* set event handling */
1337 switch (export->version) {
1340 rc = afb_export_handle_events_v12(export, export->on_event.v12);
1347 ERROR("Can't set event handler for %s", export->apiname);
1351 /* Starts the service */
1352 if (export->hooksvc & afb_hook_flag_svc_start_before)
1353 afb_hook_svc_start_before(export);
1354 export->state = Api_State_Init;
1355 switch (export->version) {
1357 rc = export->init.v1 ? export->init.v1((struct afb_service){ .itf = &hooked_service_itf, .closure = export }) : 0;
1360 rc = export->init.v2 ? export->init.v2() : 0;
1362 case Api_Version_Dyn:
1363 rc = export->init.vdyn ? export->init.vdyn(to_dynapi(export)) : 0;
1368 export->state = Api_State_Run;
1369 if (export->hooksvc & afb_hook_flag_svc_start_after)
1370 afb_hook_svc_start_after(export, rc);
1372 /* initialisation error */
1373 ERROR("Initialisation of service API %s failed (%d): %m", export->apiname, rc);