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)
156 struct afb_export *export = closure;
158 /* check daemon state */
159 if (export->state == Api_State_Pre_Init) {
160 ERROR("[API %s] Bad call to 'afb_daemon_event_make(%s)', must not be in PreInit", export->apiname, name);
165 /* create the event */
166 return afb_evt_eventid_create2(export->apiname, name);
169 static struct afb_event event_make_cb(void *closure, const char *name)
171 struct afb_eventid *eventid = eventid_make_cb(closure, name);
172 return afb_evt_event_from_evtid(afb_evt_eventid_to_evtid(eventid));
175 static int event_broadcast_cb(void *closure, const char *name, struct json_object *object)
179 struct afb_export *export = closure;
181 /* check daemon state */
182 if (export->state == Api_State_Pre_Init) {
183 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));
188 /* makes the event name */
189 plen = strlen(export->apiname);
191 event = alloca(nlen + plen + 2);
192 memcpy(event, export->apiname, plen);
194 memcpy(event + plen + 1, name, nlen + 1);
196 /* broadcast the event */
197 return afb_evt_broadcast(event, object);
200 static int rootdir_open_locale_cb(void *closure, const char *filename, int flags, const char *locale)
202 return afb_common_rootdir_open_locale(filename, flags, locale);
205 static int queue_job_cb(void *closure, void (*callback)(int signum, void *arg), void *argument, void *group, int timeout)
207 return jobs_queue(group, timeout, callback, argument);
210 static struct afb_req unstore_req_cb(void *closure, struct afb_stored_req *sreq)
212 return afb_xreq_unstore(sreq);
215 static int require_api_cb(void *closure, const char *name, int initialized)
217 struct afb_export *export = closure;
218 if (export->state != Api_State_Init) {
219 ERROR("[API %s] Bad call to 'afb_daemon_require(%s, %d)', must be in Init", export->apiname, name, initialized);
223 return -!(initialized ? afb_apiset_lookup_started : afb_apiset_lookup)(export->apiset, name, 1);
226 static int rename_api_cb(void *closure, const char *name)
228 struct afb_export *export = closure;
229 if (export->state != Api_State_Pre_Init) {
230 ERROR("[API %s] Bad call to 'afb_daemon_rename(%s)', must be in PreInit", export->apiname, name);
234 if (!afb_api_is_valid_name(name, 1)) {
235 ERROR("[API %s] Can't rename to %s: bad API name", export->apiname, name);
239 NOTICE("[API %s] renamed to [API %s]", export->apiname, name);
240 afb_export_rename(export, name);
244 static int api_new_api_cb(
249 int (*preinit)(void*, struct afb_dynapi *),
250 void *preinit_closure)
252 struct afb_export *export = closure;
253 return afb_api_dyn_add(export->apiset, api, info, noconcurrency, preinit, preinit_closure);
256 /**********************************************
258 **********************************************/
259 static void hooked_vverbose_cb(void *closure, int level, const char *file, int line, const char *function, const char *fmt, va_list args)
261 struct afb_export *export = closure;
264 vverbose_cb(closure, level, file, line, function, fmt, args);
265 afb_hook_ditf_vverbose(export, level, file, line, function, fmt, ap);
269 static void hooked_old_vverbose_cb(void *closure, int level, const char *file, int line, const char *fmt, va_list args)
271 hooked_vverbose_cb(closure, level, file, line, NULL, fmt, args);
274 static struct afb_eventid *hooked_eventid_make_cb(void *closure, const char *name)
276 struct afb_export *export = closure;
277 struct afb_eventid *r = eventid_make_cb(closure, name);
278 afb_hook_ditf_event_make(export, name, r);
282 static struct afb_event hooked_event_make_cb(void *closure, const char *name)
284 struct afb_eventid *eventid = hooked_eventid_make_cb(closure, name);
285 return (struct afb_event){ .itf = eventid ? eventid->itf : NULL, .closure = eventid };
288 static int hooked_event_broadcast_cb(void *closure, const char *name, struct json_object *object)
291 struct afb_export *export = closure;
292 json_object_get(object);
293 afb_hook_ditf_event_broadcast_before(export, name, json_object_get(object));
294 r = event_broadcast_cb(closure, name, object);
295 afb_hook_ditf_event_broadcast_after(export, name, object, r);
296 json_object_put(object);
300 static struct sd_event *hooked_get_event_loop(void *closure)
302 struct afb_export *export = closure;
303 struct sd_event *r = afb_common_get_event_loop();
304 return afb_hook_ditf_get_event_loop(export, r);
307 static struct sd_bus *hooked_get_user_bus(void *closure)
309 struct afb_export *export = closure;
310 struct sd_bus *r = afb_common_get_user_bus();
311 return afb_hook_ditf_get_user_bus(export, r);
314 static struct sd_bus *hooked_get_system_bus(void *closure)
316 struct afb_export *export = closure;
317 struct sd_bus *r = afb_common_get_system_bus();
318 return afb_hook_ditf_get_system_bus(export, r);
321 static int hooked_rootdir_get_fd(void *closure)
323 struct afb_export *export = closure;
324 int r = afb_common_rootdir_get_fd();
325 return afb_hook_ditf_rootdir_get_fd(export, r);
328 static int hooked_rootdir_open_locale_cb(void *closure, const char *filename, int flags, const char *locale)
330 struct afb_export *export = closure;
331 int r = rootdir_open_locale_cb(closure, filename, flags, locale);
332 return afb_hook_ditf_rootdir_open_locale(export, filename, flags, locale, r);
335 static int hooked_queue_job_cb(void *closure, void (*callback)(int signum, void *arg), void *argument, void *group, int timeout)
337 struct afb_export *export = closure;
338 int r = queue_job_cb(closure, callback, argument, group, timeout);
339 return afb_hook_ditf_queue_job(export, callback, argument, group, timeout, r);
342 static struct afb_req hooked_unstore_req_cb(void *closure, struct afb_stored_req *sreq)
344 struct afb_export *export = closure;
345 afb_hook_ditf_unstore_req(export, sreq);
346 return unstore_req_cb(closure, sreq);
349 static int hooked_require_api_cb(void *closure, const char *name, int initialized)
352 struct afb_export *export = closure;
353 afb_hook_ditf_require_api(export, name, initialized);
354 result = require_api_cb(closure, name, initialized);
355 return afb_hook_ditf_require_api_result(export, name, initialized, result);
358 static int hooked_rename_api_cb(void *closure, const char *name)
360 struct afb_export *export = closure;
361 const char *oldname = export->apiname;
362 int result = rename_api_cb(closure, name);
363 return afb_hook_ditf_rename_api(export, oldname, name, result);
366 static int hooked_api_new_api_cb(
371 int (*preinit)(void*, struct afb_dynapi *),
372 void *preinit_closure)
375 return api_new_api_cb(closure, api, info, noconcurrency, preinit, preinit_closure);
377 /**********************************************
379 **********************************************/
380 static const struct afb_daemon_itf daemon_itf = {
381 .vverbose_v1 = old_vverbose_cb,
382 .vverbose_v2 = vverbose_cb,
383 .event_make = event_make_cb,
384 .event_broadcast = event_broadcast_cb,
385 .get_event_loop = afb_common_get_event_loop,
386 .get_user_bus = afb_common_get_user_bus,
387 .get_system_bus = afb_common_get_system_bus,
388 .rootdir_get_fd = afb_common_rootdir_get_fd,
389 .rootdir_open_locale = rootdir_open_locale_cb,
390 .queue_job = queue_job_cb,
391 .unstore_req = unstore_req_cb,
392 .require_api = require_api_cb,
393 .rename_api = rename_api_cb,
394 .new_api = api_new_api_cb,
397 static const struct afb_daemon_itf hooked_daemon_itf = {
398 .vverbose_v1 = hooked_old_vverbose_cb,
399 .vverbose_v2 = hooked_vverbose_cb,
400 .event_make = hooked_event_make_cb,
401 .event_broadcast = hooked_event_broadcast_cb,
402 .get_event_loop = hooked_get_event_loop,
403 .get_user_bus = hooked_get_user_bus,
404 .get_system_bus = hooked_get_system_bus,
405 .rootdir_get_fd = hooked_rootdir_get_fd,
406 .rootdir_open_locale = hooked_rootdir_open_locale_cb,
407 .queue_job = hooked_queue_job_cb,
408 .unstore_req = hooked_unstore_req_cb,
409 .require_api = hooked_require_api_cb,
410 .rename_api = hooked_rename_api_cb,
411 .new_api = hooked_api_new_api_cb,
414 /*************************************************************************************************************
415 *************************************************************************************************************
416 *************************************************************************************************************
417 *************************************************************************************************************
419 *************************************************************************************************************
420 *************************************************************************************************************
421 *************************************************************************************************************
422 *************************************************************************************************************/
424 /* the common session for services sharing their session */
425 static struct afb_session *common_session;
427 /*************************************************************************************************************
428 *************************************************************************************************************
429 *************************************************************************************************************
430 *************************************************************************************************************
432 *************************************************************************************************************
433 *************************************************************************************************************
434 *************************************************************************************************************
435 *************************************************************************************************************/
438 * Structure for requests initiated by the service
442 struct afb_xreq xreq;
444 struct afb_export *export;
448 void (*callback)(void*, int, struct json_object*);
449 void (*callback_dynapi)(void*, int, struct json_object*, struct afb_dynapi*);
454 struct jobloop *jobloop;
455 struct json_object *result;
460 * destroys the call_req
462 static void callreq_destroy(struct afb_xreq *xreq)
464 struct call_req *callreq = CONTAINER_OF_XREQ(struct call_req, xreq);
466 afb_context_disconnect(&callreq->xreq.context);
467 json_object_put(callreq->xreq.json);
468 afb_cred_unref(callreq->xreq.cred);
472 static void callreq_reply_async(struct afb_xreq *xreq, int status, json_object *obj)
474 struct call_req *callreq = CONTAINER_OF_XREQ(struct call_req, xreq);
475 if (callreq->callback)
476 callreq->callback(callreq->closure, status, obj);
477 json_object_put(obj);
480 static void callreq_reply_async_dynapi(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_dynapi)
484 callreq->callback_dynapi(callreq->closure, status, obj, to_dynapi(callreq->export));
485 json_object_put(obj);
488 static void callreq_sync_leave(struct call_req *callreq)
490 struct jobloop *jobloop = callreq->jobloop;
493 callreq->jobloop = NULL;
498 static void callreq_reply_sync(struct afb_xreq *xreq, int status, json_object *obj)
500 struct call_req *callreq = CONTAINER_OF_XREQ(struct call_req, xreq);
501 callreq->status = status;
502 callreq->result = obj;
503 callreq_sync_leave(callreq);
506 static void callreq_sync_enter(int signum, void *closure, struct jobloop *jobloop)
508 struct call_req *callreq = closure;
511 callreq->jobloop = jobloop;
512 afb_xreq_process(&callreq->xreq, callreq->export->apiset);
514 callreq->result = afb_msg_json_internal_error();
515 callreq->status = -1;
516 callreq_sync_leave(callreq);
520 /* interface for requests of services */
521 const struct afb_xreq_query_itf afb_export_xreq_async_itf = {
522 .unref = callreq_destroy,
523 .reply = callreq_reply_async
526 /* interface for requests of services */
527 const struct afb_xreq_query_itf afb_export_xreq_async_dynapi_itf = {
528 .unref = callreq_destroy,
529 .reply = callreq_reply_async_dynapi
532 /* interface for requests of services */
533 const struct afb_xreq_query_itf afb_export_xreq_sync_itf = {
534 .unref = callreq_destroy,
535 .reply = callreq_reply_sync
541 static struct call_req *callreq_create(
542 struct afb_export *export,
545 struct json_object *args,
546 const struct afb_xreq_query_itf *itf)
548 struct call_req *callreq;
549 size_t lenapi, lenverb;
552 /* allocates the request */
553 lenapi = 1 + strlen(api);
554 lenverb = 1 + strlen(verb);
555 callreq = malloc(lenapi + lenverb + sizeof *callreq);
556 if (callreq != NULL) {
557 /* initialises the request */
558 afb_xreq_init(&callreq->xreq, itf);
559 afb_context_init(&callreq->xreq.context, export->session, NULL);
560 callreq->xreq.context.validated = 1;
561 copy = (char*)&callreq[1];
562 memcpy(copy, api, lenapi);
563 callreq->xreq.request.api = copy;
564 copy = ©[lenapi];
565 memcpy(copy, verb, lenverb);
566 callreq->xreq.request.verb = copy;
567 callreq->xreq.listener = export->listener;
568 callreq->xreq.json = args;
569 callreq->export = export;
575 * Initiates a call for the service
577 static void svc_call(
581 struct json_object *args,
582 void (*callback)(void*, int, struct json_object*),
585 struct afb_export *export = closure;
586 struct call_req *callreq;
587 struct json_object *ierr;
589 /* allocates the request */
590 callreq = callreq_create(export, api, verb, args, &afb_export_xreq_async_itf);
591 if (callreq == NULL) {
592 ERROR("out of memory");
593 json_object_put(args);
594 ierr = afb_msg_json_internal_error();
596 callback(cbclosure, -1, ierr);
597 json_object_put(ierr);
601 /* initialises the request */
602 callreq->jobloop = NULL;
603 callreq->callback = callback;
604 callreq->closure = cbclosure;
606 /* terminates and frees ressources if needed */
607 afb_xreq_process(&callreq->xreq, export->apiset);
610 static void svc_call_dynapi(
611 struct afb_dynapi *dynapi,
614 struct json_object *args,
615 void (*callback)(void*, int, struct json_object*, struct afb_dynapi*),
618 struct afb_export *export = from_dynapi(dynapi);
619 struct call_req *callreq;
620 struct json_object *ierr;
622 /* allocates the request */
623 callreq = callreq_create(export, api, verb, args, &afb_export_xreq_async_dynapi_itf);
624 if (callreq == NULL) {
625 ERROR("out of memory");
626 json_object_put(args);
627 ierr = afb_msg_json_internal_error();
629 callback(cbclosure, -1, ierr, to_dynapi(export));
630 json_object_put(ierr);
634 /* initialises the request */
635 callreq->jobloop = NULL;
636 callreq->callback_dynapi = callback;
637 callreq->closure = cbclosure;
639 /* terminates and frees ressources if needed */
640 afb_xreq_process(&callreq->xreq, export->apiset);
643 static int svc_call_sync(
647 struct json_object *args,
648 struct json_object **result)
650 struct afb_export *export = closure;
651 struct call_req *callreq;
652 struct json_object *resu;
655 /* allocates the request */
656 callreq = callreq_create(export, api, verb, args, &afb_export_xreq_sync_itf);
657 if (callreq == NULL) {
658 ERROR("out of memory");
660 json_object_put(args);
661 resu = afb_msg_json_internal_error();
664 /* initialises the request */
665 callreq->jobloop = NULL;
666 callreq->callback = NULL;
667 callreq->result = NULL;
669 afb_xreq_unhooked_addref(&callreq->xreq); /* avoid early callreq destruction */
670 rc = jobs_enter(NULL, 0, callreq_sync_enter, callreq);
672 rc = callreq->status;
673 resu = (rc >= 0 || callreq->result) ? callreq->result : afb_msg_json_internal_error();
674 afb_xreq_unhooked_unref(&callreq->xreq);
679 json_object_put(resu);
685 struct afb_export *export;
687 void (*callback)(void*, int, struct json_object*);
688 void (*callback_dynapi)(void*, int, struct json_object*, struct afb_dynapi*);
693 static void svc_hooked_call_result(void *closure, int status, struct json_object *result)
695 struct hooked_call *hc = closure;
696 afb_hook_svc_call_result(hc->export, status, result);
697 hc->callback(hc->cbclosure, status, result);
701 static void svc_hooked_call_dynapi_result(void *closure, int status, struct json_object *result, struct afb_dynapi *dynapi)
703 struct hooked_call *hc = closure;
704 afb_hook_svc_call_result(hc->export, status, result);
705 hc->callback_dynapi(hc->cbclosure, status, result, dynapi);
709 static void svc_hooked_call(
713 struct json_object *args,
714 void (*callback)(void*, int, struct json_object*),
717 struct afb_export *export = closure;
718 struct hooked_call *hc;
720 if (export->hooksvc & afb_hook_flag_svc_call)
721 afb_hook_svc_call(export, api, verb, args);
723 if (export->hooksvc & afb_hook_flag_svc_call_result) {
724 hc = malloc(sizeof *hc);
726 WARNING("allocation failed");
729 hc->callback = callback;
730 hc->cbclosure = cbclosure;
731 callback = svc_hooked_call_result;
735 svc_call(closure, api, verb, args, callback, cbclosure);
738 static void svc_hooked_call_dynapi(
739 struct afb_dynapi *dynapi,
742 struct json_object *args,
743 void (*callback)(void*, int, struct json_object*, struct afb_dynapi*),
746 struct afb_export *export = from_dynapi(dynapi);
747 struct hooked_call *hc;
749 if (export->hooksvc & afb_hook_flag_svc_call)
750 afb_hook_svc_call(export, api, verb, args);
752 if (export->hooksvc & afb_hook_flag_svc_call_result) {
753 hc = malloc(sizeof *hc);
755 WARNING("allocation failed");
758 hc->callback_dynapi = callback;
759 hc->cbclosure = cbclosure;
760 callback = svc_hooked_call_dynapi_result;
764 svc_call_dynapi(dynapi, api, verb, args, callback, cbclosure);
767 static int svc_hooked_call_sync(
771 struct json_object *args,
772 struct json_object **result)
774 struct afb_export *export = closure;
775 struct json_object *resu;
778 if (export->hooksvc & afb_hook_flag_svc_callsync)
779 afb_hook_svc_callsync(export, api, verb, args);
781 rc = svc_call_sync(closure, api, verb, args, &resu);
783 if (export->hooksvc & afb_hook_flag_svc_callsync_result)
784 afb_hook_svc_callsync_result(export, rc, resu);
789 json_object_put(resu);
794 /* the interface for services */
795 static const struct afb_service_itf service_itf = {
797 .call_sync = svc_call_sync
800 /* the interface for services */
801 static const struct afb_service_itf hooked_service_itf = {
802 .call = svc_hooked_call,
803 .call_sync = svc_hooked_call_sync
806 /*************************************************************************************************************
807 *************************************************************************************************************
808 *************************************************************************************************************
809 *************************************************************************************************************
811 *************************************************************************************************************
812 *************************************************************************************************************
813 *************************************************************************************************************
814 *************************************************************************************************************/
816 static int api_set_verbs_v2_cb(
817 struct afb_dynapi *dynapi,
818 const struct afb_verb_v2 *verbs)
820 struct afb_export *export = from_dynapi(dynapi);
822 if (export->apidyn) {
823 afb_api_dyn_set_verbs_v2(export->apidyn, verbs);
831 static int api_add_verb_cb(
832 struct afb_dynapi *dynapi,
835 void (*callback)(struct afb_request *request),
837 const struct afb_auth *auth,
840 struct afb_export *export = from_dynapi(dynapi);
843 return afb_api_dyn_add_verb(export->apidyn, verb, info, callback, vcbdata, auth, session);
849 static int api_sub_verb_cb(
850 struct afb_dynapi *dynapi,
853 struct afb_export *export = from_dynapi(dynapi);
856 return afb_api_dyn_sub_verb(export->apidyn, verb);
862 static int api_set_on_event_cb(
863 struct afb_dynapi *dynapi,
864 void (*onevent)(struct afb_dynapi *dynapi, const char *event, struct json_object *object))
866 struct afb_export *export = from_dynapi(dynapi);
867 return afb_export_handle_events_vdyn(export, onevent);
870 static int api_set_on_init_cb(
871 struct afb_dynapi *dynapi,
872 int (*oninit)(struct afb_dynapi *dynapi))
874 struct afb_export *export = from_dynapi(dynapi);
876 return afb_export_handle_init_vdyn(export, oninit);
879 static void api_seal_cb(
880 struct afb_dynapi *dynapi)
882 struct afb_export *export = from_dynapi(dynapi);
884 export->apidyn = NULL;
887 static int hooked_api_set_verbs_v2_cb(
888 struct afb_dynapi *dynapi,
889 const struct afb_verb_v2 *verbs)
892 return api_set_verbs_v2_cb(dynapi, verbs);
895 static int hooked_api_add_verb_cb(
896 struct afb_dynapi *dynapi,
899 void (*callback)(struct afb_request *request),
901 const struct afb_auth *auth,
905 return api_add_verb_cb(dynapi, verb, info, callback, vcbdata, auth, session);
908 static int hooked_api_sub_verb_cb(
909 struct afb_dynapi *dynapi,
913 return api_sub_verb_cb(dynapi, verb);
916 static int hooked_api_set_on_event_cb(
917 struct afb_dynapi *dynapi,
918 void (*onevent)(struct afb_dynapi *dynapi, const char *event, struct json_object *object))
921 return api_set_on_event_cb(dynapi, onevent);
924 static int hooked_api_set_on_init_cb(
925 struct afb_dynapi *dynapi,
926 int (*oninit)(struct afb_dynapi *dynapi))
929 return api_set_on_init_cb(dynapi, oninit);
932 static void hooked_api_seal_cb(
933 struct afb_dynapi *dynapi)
939 static const struct afb_dynapi_itf dynapi_itf = {
941 .vverbose = (void*)vverbose_cb,
943 .get_event_loop = afb_common_get_event_loop,
944 .get_user_bus = afb_common_get_user_bus,
945 .get_system_bus = afb_common_get_system_bus,
946 .rootdir_get_fd = afb_common_rootdir_get_fd,
947 .rootdir_open_locale = rootdir_open_locale_cb,
948 .queue_job = queue_job_cb,
950 .require_api = require_api_cb,
951 .rename_api = rename_api_cb,
953 .event_broadcast = event_broadcast_cb,
954 .eventid_make = eventid_make_cb,
956 .call = svc_call_dynapi,
957 .call_sync = svc_call_sync,
959 .api_new_api = api_new_api_cb,
960 .api_set_verbs_v2 = api_set_verbs_v2_cb,
961 .api_add_verb = api_add_verb_cb,
962 .api_sub_verb = api_sub_verb_cb,
963 .api_set_on_event = api_set_on_event_cb,
964 .api_set_on_init = api_set_on_init_cb,
965 .api_seal = api_seal_cb,
968 static const struct afb_dynapi_itf hooked_dynapi_itf = {
970 .vverbose = hooked_vverbose_cb,
972 .get_event_loop = hooked_get_event_loop,
973 .get_user_bus = hooked_get_user_bus,
974 .get_system_bus = hooked_get_system_bus,
975 .rootdir_get_fd = hooked_rootdir_get_fd,
976 .rootdir_open_locale = hooked_rootdir_open_locale_cb,
977 .queue_job = hooked_queue_job_cb,
979 .require_api = hooked_require_api_cb,
980 .rename_api = hooked_rename_api_cb,
982 .event_broadcast = hooked_event_broadcast_cb,
983 .eventid_make = hooked_eventid_make_cb,
985 .call = svc_hooked_call_dynapi,
986 .call_sync = svc_hooked_call_sync,
988 .api_new_api = hooked_api_new_api_cb,
989 .api_set_verbs_v2 = hooked_api_set_verbs_v2_cb,
990 .api_add_verb = hooked_api_add_verb_cb,
991 .api_sub_verb = hooked_api_sub_verb_cb,
992 .api_set_on_event = hooked_api_set_on_event_cb,
993 .api_set_on_init = hooked_api_set_on_init_cb,
994 .api_seal = hooked_api_seal_cb,
997 /*************************************************************************************************************
998 *************************************************************************************************************
999 *************************************************************************************************************
1000 *************************************************************************************************************
1002 *************************************************************************************************************
1003 *************************************************************************************************************
1004 *************************************************************************************************************
1005 *************************************************************************************************************/
1008 * Propagates the event to the service
1010 static void export_on_event_v12(void *closure, const char *event, int eventid, struct json_object *object)
1012 struct afb_export *export = closure;
1014 if (export->hooksvc & afb_hook_flag_svc_on_event_before)
1015 afb_hook_svc_on_event_before(export, event, eventid, object);
1016 export->on_event.v12(event, object);
1017 if (export->hooksvc & afb_hook_flag_svc_on_event_after)
1018 afb_hook_svc_on_event_after(export, event, eventid, object);
1019 json_object_put(object);
1022 /* the interface for events */
1023 static const struct afb_evt_itf evt_v12_itf = {
1024 .broadcast = export_on_event_v12,
1025 .push = export_on_event_v12
1029 * Propagates the event to the service
1031 static void export_on_event_vdyn(void *closure, const char *event, int eventid, struct json_object *object)
1033 struct afb_export *export = closure;
1035 if (export->hooksvc & afb_hook_flag_svc_on_event_before)
1036 afb_hook_svc_on_event_before(export, event, eventid, object);
1037 export->on_event.vdyn(to_dynapi(export), event, object);
1038 if (export->hooksvc & afb_hook_flag_svc_on_event_after)
1039 afb_hook_svc_on_event_after(export, event, eventid, object);
1040 json_object_put(object);
1043 /* the interface for events */
1044 static const struct afb_evt_itf evt_vdyn_itf = {
1045 .broadcast = export_on_event_vdyn,
1046 .push = export_on_event_vdyn
1049 /*************************************************************************************************************
1050 *************************************************************************************************************
1051 *************************************************************************************************************
1052 *************************************************************************************************************
1054 *************************************************************************************************************
1055 *************************************************************************************************************
1056 *************************************************************************************************************
1057 *************************************************************************************************************/
1059 static struct afb_export *create(struct afb_apiset *apiset, const char *apiname, enum afb_api_version version)
1061 struct afb_export *export;
1063 /* session shared with other exports */
1064 if (common_session == NULL) {
1065 common_session = afb_session_create (0);
1066 if (common_session == NULL)
1069 export = calloc(1, sizeof *export);
1073 memset(export, 0, sizeof *export);
1074 export->apiname = strdup(apiname);
1075 export->dynapi.apiname = export->apiname;
1076 export->version = version;
1077 export->state = Api_State_Pre_Init;
1078 export->session = afb_session_addref(common_session);
1079 export->apiset = afb_apiset_addref(apiset);
1084 void afb_export_destroy(struct afb_export *export)
1087 if (export->listener != NULL)
1088 afb_evt_listener_unref(export->listener);
1089 afb_session_unref(export->session);
1090 afb_apiset_unref(export->apiset);
1091 free(export->apiname);
1096 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*))
1098 struct afb_export *export = create(apiset, apiname, Api_Version_1);
1100 export->init.v1 = init;
1101 export->on_event.v12 = onevent;
1102 export->export.v1.mode = AFB_MODE_LOCAL;
1103 export->export.v1.daemon.closure = export;
1104 afb_export_verbosity_set(export, verbosity);
1105 afb_export_update_hook(export);
1110 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*))
1112 struct afb_export *export = create(apiset, apiname, Api_Version_2);
1114 export->init.v2 = init;
1115 export->on_event.v12 = onevent;
1116 export->export.v2 = data;
1117 data->daemon.closure = export;
1118 data->service.closure = export;
1119 afb_export_verbosity_set(export, verbosity);
1120 afb_export_update_hook(export);
1125 struct afb_export *afb_export_create_vdyn(struct afb_apiset *apiset, const char *apiname, struct afb_api_dyn *apidyn)
1127 struct afb_export *export = create(apiset, apiname, Api_Version_Dyn);
1129 export->apidyn = apidyn;
1130 afb_export_verbosity_set(export, verbosity);
1131 afb_export_update_hook(export);
1136 void afb_export_rename(struct afb_export *export, const char *apiname)
1138 free(export->apiname);
1139 export->apiname = strdup(apiname);
1140 export->dynapi.apiname = export->apiname;
1141 afb_export_update_hook(export);
1144 const char *afb_export_apiname(const struct afb_export *export)
1146 return export->apiname;
1149 void afb_export_update_hook(struct afb_export *export)
1151 export->hookditf = afb_hook_flags_ditf(export->apiname);
1152 export->hooksvc = afb_hook_flags_svc(export->apiname);
1153 export->dynapi.itf = export->hookditf|export->hooksvc ? &hooked_dynapi_itf : &dynapi_itf;
1155 switch (export->version) {
1157 export->export.v1.daemon.itf = export->hookditf ? &hooked_daemon_itf : &daemon_itf;
1160 export->export.v2->daemon.itf = export->hookditf ? &hooked_daemon_itf : &daemon_itf;
1161 export->export.v2->service.itf = export->hooksvc ? &hooked_service_itf : &service_itf;
1166 struct afb_binding_interface_v1 *afb_export_get_interface_v1(struct afb_export *export)
1168 return export->version == Api_Version_1 ? &export->export.v1 : NULL;
1171 int afb_export_unshare_session(struct afb_export *export)
1173 if (export->session == common_session) {
1174 export->session = afb_session_create (0);
1175 if (export->session)
1176 afb_session_unref(common_session);
1178 export->session = common_session;
1185 void afb_export_set_apiset(struct afb_export *export, struct afb_apiset *apiset)
1187 struct afb_apiset *prvset = export->apiset;
1188 export->apiset = afb_apiset_addref(apiset);
1189 afb_apiset_unref(prvset);
1192 struct afb_apiset *afb_export_get_apiset(struct afb_export *export)
1194 return export->apiset;
1197 int afb_export_handle_events_v12(struct afb_export *export, void (*on_event)(const char *event, struct json_object *object))
1200 switch (export->version) {
1201 case Api_Version_1: case Api_Version_2: break;
1203 ERROR("invalid version 12 for API %s", export->apiname);
1208 /* set the event handler */
1210 if (export->listener) {
1211 afb_evt_listener_unref(export->listener);
1212 export->listener = NULL;
1214 export->on_event.v12 = on_event;
1216 export->on_event.v12 = on_event;
1217 if (!export->listener) {
1218 export->listener = afb_evt_listener_create(&evt_v12_itf, export);
1219 if (export->listener == NULL)
1226 int afb_export_handle_events_vdyn(struct afb_export *export, void (*on_event)(struct afb_dynapi *dynapi, const char *event, struct json_object *object))
1229 switch (export->version) {
1230 case Api_Version_Dyn: break;
1232 ERROR("invalid version Dyn for API %s", export->apiname);
1237 /* set the event handler */
1239 if (export->listener) {
1240 afb_evt_listener_unref(export->listener);
1241 export->listener = NULL;
1243 export->on_event.vdyn = on_event;
1245 export->on_event.vdyn = on_event;
1246 if (!export->listener) {
1247 export->listener = afb_evt_listener_create(&evt_vdyn_itf, export);
1248 if (export->listener == NULL)
1255 int afb_export_handle_init_vdyn(struct afb_export *export, int (*oninit)(struct afb_dynapi *dynapi))
1257 if (export->state != Api_State_Pre_Init) {
1258 ERROR("[API %s] Bad call to 'afb_dynapi_on_init', must be in PreInit", export->apiname);
1263 export->init.vdyn = oninit;
1268 * Starts a new service (v1)
1270 struct afb_binding_v1 *afb_export_register_v1(struct afb_export *export, struct afb_binding_v1 *(*regfun)(const struct afb_binding_interface_v1*))
1272 return regfun(&export->export.v1);
1275 int afb_export_preinit_vdyn(struct afb_export *export, int (*preinit)(void*, struct afb_dynapi*), void *closure)
1277 return preinit(closure, to_dynapi(export));
1280 int afb_export_verbosity_get(const struct afb_export *export)
1282 return export->dynapi.verbosity;
1285 void afb_export_verbosity_set(struct afb_export *export, int level)
1287 export->dynapi.verbosity = level;
1288 switch (export->version) {
1289 case Api_Version_1: export->export.v1.verbosity = level; break;
1290 case Api_Version_2: export->export.v2->verbosity = level; break;
1294 /*************************************************************************************************************
1295 *************************************************************************************************************
1296 *************************************************************************************************************
1297 *************************************************************************************************************
1299 *************************************************************************************************************
1300 *************************************************************************************************************
1301 *************************************************************************************************************
1302 *************************************************************************************************************/
1304 int afb_export_start(struct afb_export *export, int share_session, int onneed, struct afb_apiset *apiset)
1309 if (export->state != Api_State_Pre_Init) {
1310 /* not an error when onneed */
1314 /* already started: it is an error */
1315 ERROR("Service of API %s already started", export->apiname);
1319 /* unshare the session if asked */
1320 if (!share_session) {
1321 rc = afb_export_unshare_session(export);
1323 ERROR("Can't unshare the session for %s", export->apiname);
1328 /* set event handling */
1329 switch (export->version) {
1332 rc = afb_export_handle_events_v12(export, export->on_event.v12);
1339 ERROR("Can't set event handler for %s", export->apiname);
1343 /* Starts the service */
1344 if (export->hooksvc & afb_hook_flag_svc_start_before)
1345 afb_hook_svc_start_before(export);
1346 export->state = Api_State_Init;
1347 switch (export->version) {
1349 rc = export->init.v1 ? export->init.v1((struct afb_service){ .itf = &hooked_service_itf, .closure = export }) : 0;
1352 rc = export->init.v2 ? export->init.v2() : 0;
1354 case Api_Version_Dyn:
1355 rc = export->init.vdyn ? export->init.vdyn(to_dynapi(export)) : 0;
1360 export->state = Api_State_Run;
1361 if (export->hooksvc & afb_hook_flag_svc_start_after)
1362 afb_hook_svc_start_after(export, rc);
1364 /* initialisation error */
1365 ERROR("Initialisation of service API %s failed (%d): %m", export->apiname, rc);