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_create_event(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.api = copy;
572 copy = ©[lenapi];
573 memcpy(copy, verb, lenverb);
574 callreq->xreq.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),
844 const struct afb_auth *auth,
847 struct afb_export *export = from_dynapi(dynapi);
850 return afb_api_dyn_add_verb(export->apidyn, verb, info, callback, auth, session);
856 static int api_sub_verb_cb(
857 struct afb_dynapi *dynapi,
860 struct afb_export *export = from_dynapi(dynapi);
863 return afb_api_dyn_sub_verb(export->apidyn, verb);
869 static int api_set_on_event_cb(
870 struct afb_dynapi *dynapi,
871 void (*onevent)(struct afb_dynapi *dynapi, const char *event, struct json_object *object))
873 struct afb_export *export = from_dynapi(dynapi);
874 return afb_export_handle_events_vdyn(export, onevent);
877 static int api_set_on_init_cb(
878 struct afb_dynapi *dynapi,
879 int (*oninit)(struct afb_dynapi *dynapi))
881 struct afb_export *export = from_dynapi(dynapi);
883 return afb_export_handle_init_vdyn(export, oninit);
886 static void api_seal_cb(
887 struct afb_dynapi *dynapi)
889 struct afb_export *export = from_dynapi(dynapi);
891 export->apidyn = NULL;
894 static int hooked_api_set_verbs_v2_cb(
895 struct afb_dynapi *dynapi,
896 const struct afb_verb_v2 *verbs)
899 return api_set_verbs_v2_cb(dynapi, verbs);
902 static int hooked_api_add_verb_cb(
903 struct afb_dynapi *dynapi,
906 void (*callback)(struct afb_request *request),
907 const struct afb_auth *auth,
911 return api_add_verb_cb(dynapi, verb, info, callback, auth, session);
914 static int hooked_api_sub_verb_cb(
915 struct afb_dynapi *dynapi,
919 return api_sub_verb_cb(dynapi, verb);
922 static int hooked_api_set_on_event_cb(
923 struct afb_dynapi *dynapi,
924 void (*onevent)(struct afb_dynapi *dynapi, const char *event, struct json_object *object))
927 return api_set_on_event_cb(dynapi, onevent);
930 static int hooked_api_set_on_init_cb(
931 struct afb_dynapi *dynapi,
932 int (*oninit)(struct afb_dynapi *dynapi))
935 return api_set_on_init_cb(dynapi, oninit);
938 static void hooked_api_seal_cb(
939 struct afb_dynapi *dynapi)
945 static const struct afb_dynapi_itf dynapi_itf = {
947 .vverbose = (void*)vverbose_cb,
949 .get_event_loop = afb_common_get_event_loop,
950 .get_user_bus = afb_common_get_user_bus,
951 .get_system_bus = afb_common_get_system_bus,
952 .rootdir_get_fd = afb_common_rootdir_get_fd,
953 .rootdir_open_locale = rootdir_open_locale_cb,
954 .queue_job = queue_job_cb,
956 .require_api = require_api_cb,
957 .rename_api = rename_api_cb,
959 .event_broadcast = event_broadcast_cb,
960 .eventid_make = eventid_make_cb,
962 .call = svc_call_dynapi,
963 .call_sync = svc_call_sync,
965 .api_new_api = api_new_api_cb,
966 .api_set_verbs_v2 = api_set_verbs_v2_cb,
967 .api_add_verb = api_add_verb_cb,
968 .api_sub_verb = api_sub_verb_cb,
969 .api_set_on_event = api_set_on_event_cb,
970 .api_set_on_init = api_set_on_init_cb,
971 .api_seal = api_seal_cb,
974 static const struct afb_dynapi_itf hooked_dynapi_itf = {
976 .vverbose = hooked_vverbose_cb,
978 .get_event_loop = hooked_get_event_loop,
979 .get_user_bus = hooked_get_user_bus,
980 .get_system_bus = hooked_get_system_bus,
981 .rootdir_get_fd = hooked_rootdir_get_fd,
982 .rootdir_open_locale = hooked_rootdir_open_locale_cb,
983 .queue_job = hooked_queue_job_cb,
985 .require_api = hooked_require_api_cb,
986 .rename_api = hooked_rename_api_cb,
988 .event_broadcast = hooked_event_broadcast_cb,
989 .eventid_make = hooked_eventid_make_cb,
991 .call = svc_hooked_call_dynapi,
992 .call_sync = svc_hooked_call_sync,
994 .api_new_api = hooked_api_new_api_cb,
995 .api_set_verbs_v2 = hooked_api_set_verbs_v2_cb,
996 .api_add_verb = hooked_api_add_verb_cb,
997 .api_sub_verb = hooked_api_sub_verb_cb,
998 .api_set_on_event = hooked_api_set_on_event_cb,
999 .api_set_on_init = hooked_api_set_on_init_cb,
1000 .api_seal = hooked_api_seal_cb,
1003 /*************************************************************************************************************
1004 *************************************************************************************************************
1005 *************************************************************************************************************
1006 *************************************************************************************************************
1008 *************************************************************************************************************
1009 *************************************************************************************************************
1010 *************************************************************************************************************
1011 *************************************************************************************************************/
1014 * Propagates the event to the service
1016 static void export_on_event_v12(void *closure, const char *event, int eventid, struct json_object *object)
1018 struct afb_export *export = closure;
1020 if (export->hooksvc & afb_hook_flag_svc_on_event_before)
1021 afb_hook_svc_on_event_before(export, event, eventid, object);
1022 export->on_event.v12(event, object);
1023 if (export->hooksvc & afb_hook_flag_svc_on_event_after)
1024 afb_hook_svc_on_event_after(export, event, eventid, object);
1025 json_object_put(object);
1028 /* the interface for events */
1029 static const struct afb_evt_itf evt_v12_itf = {
1030 .broadcast = export_on_event_v12,
1031 .push = export_on_event_v12
1035 * Propagates the event to the service
1037 static void export_on_event_vdyn(void *closure, const char *event, int eventid, struct json_object *object)
1039 struct afb_export *export = closure;
1041 if (export->hooksvc & afb_hook_flag_svc_on_event_before)
1042 afb_hook_svc_on_event_before(export, event, eventid, object);
1043 export->on_event.vdyn(to_dynapi(export), event, object);
1044 if (export->hooksvc & afb_hook_flag_svc_on_event_after)
1045 afb_hook_svc_on_event_after(export, event, eventid, object);
1046 json_object_put(object);
1049 /* the interface for events */
1050 static const struct afb_evt_itf evt_vdyn_itf = {
1051 .broadcast = export_on_event_vdyn,
1052 .push = export_on_event_vdyn
1055 /*************************************************************************************************************
1056 *************************************************************************************************************
1057 *************************************************************************************************************
1058 *************************************************************************************************************
1060 *************************************************************************************************************
1061 *************************************************************************************************************
1062 *************************************************************************************************************
1063 *************************************************************************************************************/
1065 static struct afb_export *create(struct afb_apiset *apiset, const char *apiname, enum afb_api_version version)
1067 struct afb_export *export;
1069 /* session shared with other exports */
1070 if (common_session == NULL) {
1071 common_session = afb_session_create (NULL, 0);
1072 if (common_session == NULL)
1075 export = calloc(1, sizeof *export);
1079 memset(export, 0, sizeof *export);
1080 export->apiname = strdup(apiname);
1081 export->version = version;
1082 export->state = Api_State_Pre_Init;
1083 export->session = afb_session_addref(common_session);
1084 export->apiset = afb_apiset_addref(apiset);
1089 void afb_export_destroy(struct afb_export *export)
1092 if (export->listener != NULL)
1093 afb_evt_listener_unref(export->listener);
1094 afb_session_unref(export->session);
1095 afb_apiset_unref(export->apiset);
1096 free(export->apiname);
1101 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*))
1103 struct afb_export *export = create(apiset, apiname, Api_Version_1);
1105 export->init.v1 = init;
1106 export->on_event.v12 = onevent;
1107 export->export.v1.mode = AFB_MODE_LOCAL;
1108 export->export.v1.daemon.closure = export;
1109 afb_export_verbosity_set(export, verbosity);
1110 afb_export_update_hook(export);
1115 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*))
1117 struct afb_export *export = create(apiset, apiname, Api_Version_2);
1119 export->init.v2 = init;
1120 export->on_event.v12 = onevent;
1121 export->export.v2 = data;
1122 data->daemon.closure = export;
1123 data->service.closure = export;
1124 afb_export_verbosity_set(export, verbosity);
1125 afb_export_update_hook(export);
1130 struct afb_export *afb_export_create_vdyn(struct afb_apiset *apiset, const char *apiname, struct afb_api_dyn *apidyn)
1132 struct afb_export *export = create(apiset, apiname, Api_Version_Dyn);
1134 export->apidyn = apidyn;
1135 afb_export_verbosity_set(export, verbosity);
1136 afb_export_update_hook(export);
1141 void afb_export_rename(struct afb_export *export, const char *apiname)
1143 free(export->apiname);
1144 export->apiname = strdup(apiname);
1145 afb_export_update_hook(export);
1148 const char *afb_export_apiname(const struct afb_export *export)
1150 return export->apiname;
1153 void afb_export_update_hook(struct afb_export *export)
1155 export->hookditf = afb_hook_flags_ditf(export->apiname);
1156 export->hooksvc = afb_hook_flags_svc(export->apiname);
1157 export->dynapi.itf = export->hookditf|export->hooksvc ? &hooked_dynapi_itf : &dynapi_itf;
1159 switch (export->version) {
1161 export->export.v1.daemon.itf = export->hookditf ? &hooked_daemon_itf : &daemon_itf;
1164 export->export.v2->daemon.itf = export->hookditf ? &hooked_daemon_itf : &daemon_itf;
1165 export->export.v2->service.itf = export->hooksvc ? &hooked_service_itf : &service_itf;
1170 struct afb_binding_interface_v1 *afb_export_get_interface_v1(struct afb_export *export)
1172 return export->version == Api_Version_1 ? &export->export.v1 : NULL;
1175 int afb_export_unshare_session(struct afb_export *export)
1177 if (export->session == common_session) {
1178 export->session = afb_session_create (NULL, 0);
1179 if (export->session)
1180 afb_session_unref(common_session);
1182 export->session = common_session;
1189 void afb_export_set_apiset(struct afb_export *export, struct afb_apiset *apiset)
1191 struct afb_apiset *prvset = export->apiset;
1192 export->apiset = afb_apiset_addref(apiset);
1193 afb_apiset_unref(prvset);
1196 struct afb_apiset *afb_export_get_apiset(struct afb_export *export)
1198 return export->apiset;
1201 int afb_export_handle_events_v12(struct afb_export *export, void (*on_event)(const char *event, struct json_object *object))
1204 switch (export->version) {
1205 case Api_Version_1: case Api_Version_2: break;
1207 ERROR("invalid version 12 for API %s", export->apiname);
1212 /* set the event handler */
1214 if (export->listener) {
1215 afb_evt_listener_unref(export->listener);
1216 export->listener = NULL;
1218 export->on_event.v12 = on_event;
1220 export->on_event.v12 = on_event;
1221 if (!export->listener) {
1222 export->listener = afb_evt_listener_create(&evt_v12_itf, export);
1223 if (export->listener == NULL)
1230 int afb_export_handle_events_vdyn(struct afb_export *export, void (*on_event)(struct afb_dynapi *dynapi, const char *event, struct json_object *object))
1233 switch (export->version) {
1234 case Api_Version_Dyn: break;
1236 ERROR("invalid version Dyn for API %s", export->apiname);
1241 /* set the event handler */
1243 if (export->listener) {
1244 afb_evt_listener_unref(export->listener);
1245 export->listener = NULL;
1247 export->on_event.vdyn = on_event;
1249 export->on_event.vdyn = on_event;
1250 if (!export->listener) {
1251 export->listener = afb_evt_listener_create(&evt_vdyn_itf, export);
1252 if (export->listener == NULL)
1259 int afb_export_handle_init_vdyn(struct afb_export *export, int (*oninit)(struct afb_dynapi *dynapi))
1261 if (export->state != Api_State_Pre_Init) {
1262 ERROR("[API %s] Bad call to 'afb_dynapi_on_init', must be in PreInit", export->apiname);
1267 export->init.vdyn = oninit;
1272 * Starts a new service (v1)
1274 struct afb_binding_v1 *afb_export_register_v1(struct afb_export *export, struct afb_binding_v1 *(*regfun)(const struct afb_binding_interface_v1*))
1276 return regfun(&export->export.v1);
1279 int afb_export_preinit_vdyn(struct afb_export *export, int (*preinit)(void*, struct afb_dynapi*), void *closure)
1281 return preinit(closure, to_dynapi(export));
1284 int afb_export_verbosity_get(const struct afb_export *export)
1286 return export->dynapi.verbosity;
1289 void afb_export_verbosity_set(struct afb_export *export, int level)
1291 export->dynapi.verbosity = level;
1292 switch (export->version) {
1293 case Api_Version_1: export->export.v1.verbosity = level; break;
1294 case Api_Version_2: export->export.v2->verbosity = level; break;
1298 /*************************************************************************************************************
1299 *************************************************************************************************************
1300 *************************************************************************************************************
1301 *************************************************************************************************************
1303 *************************************************************************************************************
1304 *************************************************************************************************************
1305 *************************************************************************************************************
1306 *************************************************************************************************************/
1308 int afb_export_start(struct afb_export *export, int share_session, int onneed, struct afb_apiset *apiset)
1313 if (export->state != Api_State_Pre_Init) {
1314 /* not an error when onneed */
1318 /* already started: it is an error */
1319 ERROR("Service of API %s already started", export->apiname);
1323 /* unshare the session if asked */
1324 if (!share_session) {
1325 rc = afb_export_unshare_session(export);
1327 ERROR("Can't unshare the session for %s", export->apiname);
1332 /* set event handling */
1333 switch (export->version) {
1336 rc = afb_export_handle_events_v12(export, export->on_event.v12);
1343 ERROR("Can't set event handler for %s", export->apiname);
1347 /* Starts the service */
1348 if (export->hooksvc & afb_hook_flag_svc_start_before)
1349 afb_hook_svc_start_before(export);
1350 export->state = Api_State_Init;
1351 switch (export->version) {
1353 rc = export->init.v1 ? export->init.v1((struct afb_service){ .itf = &hooked_service_itf, .closure = export }) : 0;
1356 rc = export->init.v2 ? export->init.v2() : 0;
1358 case Api_Version_Dyn:
1359 rc = export->init.vdyn ? export->init.vdyn(to_dynapi(export)) : 0;
1364 export->state = Api_State_Run;
1365 if (export->hooksvc & afb_hook_flag_svc_start_after)
1366 afb_hook_svc_start_after(export, rc);
1368 /* initialisation error */
1369 ERROR("Initialisation of service API %s failed (%d): %m", export->apiname, rc);