2 * Copyright (C) 2015-2018 "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.
18 #if defined(WITH_DBUS_TRANSPARENCY)
28 #include <systemd/sd-bus.h>
29 #include <json-c/json.h>
30 #if !defined(JSON_C_TO_STRING_NOSLASHESCAPE)
31 #define JSON_C_TO_STRING_NOSLASHESCAPE 0
34 #include <afb/afb-event-x2.h>
36 #include "afb-systemd.h"
38 #include "afb-session.h"
39 #include "afb-msg-json.h"
41 #include "afb-apiset.h"
42 #include "afb-api-dbus.h"
43 #include "afb-context.h"
50 static const char DEFAULT_PATH_PREFIX[] = "/org/agl/afb/api/";
57 * The path given are of the form
58 * system:/org/agl/afb/api/...
60 * user:/org/agl/afb/api/...
64 struct sd_bus *sdbus; /* the bus */
65 char *path; /* path of the object for the API */
66 char *name; /* name/interface of the object */
67 char *api; /* api name of the interface */
70 struct sd_bus_slot *slot_broadcast;
71 struct sd_bus_slot *slot_event;
72 struct dbus_event *events;
73 struct dbus_memo *memos;
76 struct sd_bus_slot *slot_call;
77 struct afb_evt_listener *listener; /* listener for broadcasted events */
78 struct origin *origins;
79 struct afb_apiset *apiset;
84 /******************* common part **********************************/
87 * create a structure api_dbus connected on either the system
88 * bus if 'system' is not null or on the user bus. The connection
89 * is established for either emiting/receiving on 'path' being of length
92 static struct api_dbus *make_api_dbus_3(int system, const char *path, size_t pathlen)
98 /* allocates the structure */
99 api = calloc(1, sizeof *api + 1 + pathlen + pathlen);
105 /* init the structure's strings */
107 /* path is copied after the struct */
108 api->path = (void*)(api+1);
109 strcpy(api->path, path);
111 /* api name is at the end of the path */
112 api->api = strrchr(api->path, '/');
113 if (api->api == NULL) {
118 if (!afb_api_is_valid_name(api->api)) {
123 /* the name/interface is copied after the path */
124 api->name = &api->path[pathlen + 1];
125 strcpy(api->name, &path[1]);
126 ptr = strchr(api->name, '/');
129 ptr = strchr(ptr, '/');
133 sdbus = (system ? afb_systemd_get_system_bus : afb_systemd_get_user_bus)();
147 * create a structure api_dbus connected on either the system
148 * bus if 'system' is not null or on the user bus. The connection
149 * is established for either emiting/receiving on 'path'.
150 * If 'path' is not absolute, it is prefixed with DEFAULT_PATH_PREFIX.
152 static struct api_dbus *make_api_dbus_2(int system, const char *path)
157 /* check the length of the path */
164 /* if the path is absolute, creation now */
166 return make_api_dbus_3(system, path, len);
168 /* compute the path prefixed with DEFAULT_PATH_PREFIX */
169 assert(strlen(DEFAULT_PATH_PREFIX) > 0);
170 assert(DEFAULT_PATH_PREFIX[strlen(DEFAULT_PATH_PREFIX) - 1] == '/');
171 len += strlen(DEFAULT_PATH_PREFIX);
172 ptr = alloca(len + 1);
173 strcpy(stpcpy(ptr, DEFAULT_PATH_PREFIX), path);
175 /* creation for prefixed path */
176 return make_api_dbus_3(system, ptr, len);
180 * create a structure api_dbus connected either emiting/receiving
182 * The path can be prefixed with "system:" or "user:" to select
183 * either the user or the system D-Bus. If none is set then user's
185 * If remaining 'path' is not absolute, it is prefixed with
186 * DEFAULT_PATH_PREFIX.
188 static struct api_dbus *make_api_dbus(const char *path)
193 /* retrieves the prefix "scheme-like" part */
194 ptr = strchr(path, ':');
196 return make_api_dbus_2(0, path);
198 /* check the prefix part */
199 preflen = (size_t)(ptr - path);
200 if (strncmp(path, "system", preflen) == 0)
201 return make_api_dbus_2(1, ptr + 1);
203 if (strncmp(path, "user", preflen) == 0)
204 return make_api_dbus_2(0, ptr + 1);
206 /* TODO: connect to a foreign D-Bus? */
211 static void destroy_api_dbus(struct api_dbus *api)
216 /******************* client part **********************************/
219 * structure for recording query data
222 struct dbus_memo *next; /* the next memo */
223 struct api_dbus *api; /* the dbus api */
224 struct afb_xreq *xreq; /* the request */
225 uint64_t msgid; /* the message identifier */
230 struct dbus_event *next;
231 struct afb_event_x2 *event;
236 /* allocates and init the memorizing data */
237 static struct dbus_memo *api_dbus_client_memo_make(struct api_dbus *api, struct afb_xreq *xreq)
239 struct dbus_memo *memo;
241 memo = malloc(sizeof *memo);
243 afb_xreq_unhooked_addref(xreq);
247 memo->next = api->client.memos;
248 api->client.memos = memo;
253 /* free and release the memorizing data */
254 static void api_dbus_client_memo_destroy(struct dbus_memo *memo)
256 struct dbus_memo **prv;
258 prv = &memo->api->client.memos;
259 while (*prv != NULL) {
267 afb_xreq_unhooked_unref(memo->xreq);
271 /* search a memorized request */
272 static struct dbus_memo *api_dbus_client_memo_search(struct api_dbus *api, uint64_t msgid)
274 struct dbus_memo *memo;
276 memo = api->client.memos;
277 while (memo != NULL && memo->msgid != msgid)
283 /* callback when received answer */
284 static int api_dbus_client_on_reply(sd_bus_message *message, void *userdata, sd_bus_error *ret_error)
287 struct dbus_memo *memo;
288 const char *json, *error, *info;
289 struct json_object *object;
290 enum json_tokener_error jerr;
292 /* retrieve the recorded data */
296 rc = sd_bus_message_read(message, "sss", &json, &error, &info);
298 /* failing to have the answer */
299 afb_xreq_reply(memo->xreq, NULL, "error", "dbus error");
301 /* report the answer */
305 object = json_tokener_parse_verbose(json, &jerr);
306 if (jerr != json_tokener_success)
307 object = json_object_new_string(json);
309 afb_xreq_reply(memo->xreq, object, *error ? error : NULL, *info ? info : NULL);
311 api_dbus_client_memo_destroy(memo);
315 /* on call, propagate it to the dbus service */
316 static void api_dbus_client_call(void *closure, struct afb_xreq *xreq)
318 struct api_dbus *api = closure;
321 struct dbus_memo *memo;
322 struct sd_bus_message *msg;
325 /* create the recording data */
326 memo = api_dbus_client_memo_make(api, xreq);
328 afb_xreq_reply(memo->xreq, NULL, "error", "out of memory");
332 /* creates the message */
334 rc = sd_bus_message_new_method_call(api->sdbus, &msg, api->name, api->path, api->name, xreq->request.called_verb);
338 creds = xreq_on_behalf_cred_export(xreq);
339 rc = sd_bus_message_append(msg, "ssus",
340 afb_xreq_raw(xreq, &size),
341 afb_session_uuid(xreq->context.session),
342 (uint32_t)xreq->context.flags,
348 rc = sd_bus_call_async(api->sdbus, NULL, msg, api_dbus_client_on_reply, memo, (uint64_t)-1);
352 rc = sd_bus_message_get_cookie(msg, &memo->msgid);
357 /* if there was an error report it directly */
359 afb_xreq_reply(memo->xreq, NULL, "error", "dbus error");
360 api_dbus_client_memo_destroy(memo);
362 sd_bus_message_unref(msg);
365 /* receives broadcasted events */
366 static int api_dbus_client_on_broadcast_event(sd_bus_message *m, void *userdata, sd_bus_error *ret_error)
368 struct json_object *object;
369 const char *event, *data;
370 enum json_tokener_error jerr;
371 int rc = sd_bus_message_read(m, "ss", &event, &data);
373 ERROR("unreadable broadcasted event");
375 object = json_tokener_parse_verbose(data, &jerr);
376 if (jerr != json_tokener_success)
377 object = json_object_new_string(data);
378 afb_evt_broadcast(event, object);
383 /* search the eventid */
384 static struct dbus_event *api_dbus_client_event_search(struct api_dbus *api, int id, const char *name)
386 struct dbus_event *ev;
388 ev = api->client.events;
389 while (ev != NULL && (ev->id != id || 0 != strcmp(afb_evt_event_x2_fullname(ev->event), name)))
395 /* adds an eventid */
396 static void api_dbus_client_event_create(struct api_dbus *api, int id, const char *name)
398 struct dbus_event *ev;
400 /* check conflicts */
401 ev = api_dbus_client_event_search(api, id, name);
407 /* no conflict, try to add it */
408 ev = malloc(sizeof *ev);
410 ev->event = afb_evt_event_x2_create(name);
411 if (ev->event == NULL)
416 ev->next = api->client.events;
417 api->client.events = ev;
421 ERROR("can't create event %s, out of memory", name);
424 /* removes an eventid */
425 static void api_dbus_client_event_drop(struct api_dbus *api, int id, const char *name)
427 struct dbus_event *ev, **prv;
429 /* retrieves the event */
430 ev = api_dbus_client_event_search(api, id, name);
432 ERROR("event %s not found", name);
436 /* decrease the reference count */
440 /* unlinks the event */
441 prv = &api->client.events;
446 /* destroys the event */
447 afb_evt_event_x2_unref(ev->event);
452 static void api_dbus_client_event_push(struct api_dbus *api, int id, const char *name, const char *data)
454 struct json_object *object;
455 struct dbus_event *ev;
456 enum json_tokener_error jerr;
458 /* retrieves the event */
459 ev = api_dbus_client_event_search(api, id, name);
461 ERROR("event %s not found", name);
465 /* destroys the event */
466 object = json_tokener_parse_verbose(data, &jerr);
467 if (jerr != json_tokener_success)
468 object = json_object_new_string(data);
469 afb_evt_event_x2_push(ev->event, object);
472 /* subscribes an event */
473 static void api_dbus_client_event_subscribe(struct api_dbus *api, int id, const char *name, uint64_t msgid)
476 struct dbus_event *ev;
477 struct dbus_memo *memo;
479 /* retrieves the event */
480 ev = api_dbus_client_event_search(api, id, name);
482 ERROR("event %s not found", name);
486 /* retrieves the memo */
487 memo = api_dbus_client_memo_search(api, msgid);
489 ERROR("message not found");
493 /* subscribe the request to the event */
494 rc = afb_xreq_subscribe(memo->xreq, ev->event);
496 ERROR("can't subscribe: %m");
499 /* unsubscribes an event */
500 static void api_dbus_client_event_unsubscribe(struct api_dbus *api, int id, const char *name, uint64_t msgid)
503 struct dbus_event *ev;
504 struct dbus_memo *memo;
506 /* retrieves the event */
507 ev = api_dbus_client_event_search(api, id, name);
509 ERROR("event %s not found", name);
513 /* retrieves the memo */
514 memo = api_dbus_client_memo_search(api, msgid);
516 ERROR("message not found");
520 /* unsubscribe the request from the event */
521 rc = afb_xreq_unsubscribe(memo->xreq, ev->event);
523 ERROR("can't unsubscribe: %m");
526 /* receives calls for event */
527 static int api_dbus_client_on_manage_event(sd_bus_message *m, void *userdata, sd_bus_error *ret_error)
529 const char *eventname, *data;
533 struct api_dbus *api;
536 /* check if expected message */
538 if (0 != strcmp(api->name, sd_bus_message_get_interface(m)))
539 return 0; /* not the expected interface */
540 if (0 != strcmp("event", sd_bus_message_get_member(m)))
541 return 0; /* not the expected member */
542 if (sd_bus_message_get_expect_reply(m))
543 return 0; /* not the expected type of message */
545 /* reads the message */
546 rc = sd_bus_message_read(m, "yisst", &order, &eventid, &eventname, &data, &msgid);
548 ERROR("unreadable event");
552 /* what is the order ? */
553 switch ((char)order) {
554 case '+': /* creates the event */
555 api_dbus_client_event_create(api, eventid, eventname);
557 case '-': /* drops the event */
558 api_dbus_client_event_drop(api, eventid, eventname);
560 case '!': /* pushs the event */
561 api_dbus_client_event_push(api, eventid, eventname, data);
563 case 'S': /* subscribe event for a request */
564 api_dbus_client_event_subscribe(api, eventid, eventname, msgid);
566 case 'U': /* unsubscribe event for a request */
567 api_dbus_client_event_unsubscribe(api, eventid, eventname, msgid);
570 /* unexpected order */
571 ERROR("unexpected order '%c' received", (char)order);
577 static struct afb_api_itf dbus_api_itf = {
578 .call = api_dbus_client_call
581 /* adds a afb-dbus-service client api */
582 int afb_api_dbus_add_client(const char *path, struct afb_apiset *declare_set, struct afb_apiset *call_set)
585 struct api_dbus *api;
586 struct afb_api_item afb_api;
589 /* create the dbus client api */
590 api = make_api_dbus(path);
594 /* connect to broadcasted events */
595 rc = asprintf(&match, "type='signal',path='%s',interface='%s',member='broadcast'", api->path, api->name);
598 ERROR("out of memory");
601 rc = sd_bus_add_match(api->sdbus, &api->client.slot_broadcast, match, api_dbus_client_on_broadcast_event, api);
605 ERROR("can't add dbus match %s for %s", api->path, api->name);
609 /* connect to event management */
610 rc = sd_bus_add_object(api->sdbus, &api->client.slot_event, api->path, api_dbus_client_on_manage_event, api);
613 ERROR("can't add dbus object %s for %s", api->path, api->name);
617 /* record it as an API */
618 afb_api.closure = api;
619 afb_api.itf = &dbus_api_itf;
620 afb_api.group = NULL;
621 if (afb_apiset_add(declare_set, api->api, afb_api) < 0)
627 destroy_api_dbus(api);
632 /******************* event structures for server part **********************************/
634 static void afb_api_dbus_server_event_add(void *closure, const char *event, int eventid);
635 static void afb_api_dbus_server_event_remove(void *closure, const char *event, int eventid);
636 static void afb_api_dbus_server_event_push(void *closure, const char *event, int eventid, struct json_object *object);
637 static void afb_api_dbus_server_event_broadcast(void *closure, const char *event, int eventid, struct json_object *object);
639 /* the interface for events broadcasting */
640 static const struct afb_evt_itf evt_broadcast_itf = {
641 .broadcast = afb_api_dbus_server_event_broadcast,
644 /* the interface for events pushing */
645 static const struct afb_evt_itf evt_push_itf = {
646 .push = afb_api_dbus_server_event_push,
647 .add = afb_api_dbus_server_event_add,
648 .remove = afb_api_dbus_server_event_remove
651 /******************* origin description part for server *****************************/
655 /* link to next different origin */
658 /* the server dbus-api */
659 struct api_dbus *api;
661 /* count of references */
664 /* credentials of the origin */
665 struct afb_cred *cred;
671 /* get the credentials for the message */
672 static void init_origin_creds(struct origin *origin)
681 rc = sd_bus_get_name_creds(origin->api->sdbus, origin->name,
682 SD_BUS_CREDS_PID|SD_BUS_CREDS_UID|SD_BUS_CREDS_GID|SD_BUS_CREDS_SELINUX_CONTEXT,
687 afb_cred_unref(origin->cred);
688 sd_bus_creds_get_uid(c, &uid);
689 sd_bus_creds_get_gid(c, &gid);
690 sd_bus_creds_get_pid(c, &pid);
691 sd_bus_creds_get_selinux_context(c, &context);
692 origin->cred = afb_cred_create(uid, gid, pid, context);
693 sd_bus_creds_unref(c);
697 static struct origin *afb_api_dbus_server_origin_get(struct api_dbus *api, const char *sender)
699 struct origin *origin;
701 /* searchs for an existing origin */
702 origin = api->server.origins;
703 while (origin != NULL) {
704 if (0 == strcmp(origin->name, sender)) {
708 origin = origin->next;
711 /* not found, create it */
712 origin = malloc(strlen(sender) + sizeof *origin);
717 origin->refcount = 1;
718 strcpy(origin->name, sender);
719 init_origin_creds(origin);
720 origin->next = api->server.origins;
721 api->server.origins = origin;
726 static void afb_api_dbus_server_origin_unref(struct origin *origin)
728 if (!--origin->refcount) {
731 prv = &origin->api->server.origins;
732 while(*prv != origin)
735 afb_cred_unref(origin->cred);
742 /* link to next different origin */
743 struct origin *origin;
745 /* the listener of events */
746 struct afb_evt_listener *listener;
749 static void afb_api_dbus_server_listener_free(struct listener *listener)
751 afb_evt_listener_unref(listener->listener);
752 afb_api_dbus_server_origin_unref(listener->origin);
756 static struct listener *afb_api_dbus_server_listener_get(struct api_dbus *api, const char *sender, struct afb_session *session)
759 struct listener *listener;
760 struct origin *origin;
763 origin = afb_api_dbus_server_origin_get(api, sender);
767 /* retrieves the stored listener */
768 listener = afb_session_get_cookie(session, origin);
769 if (listener != NULL) {
771 afb_api_dbus_server_origin_unref(origin);
775 /* creates the listener */
776 listener = malloc(sizeof *listener);
777 if (listener == NULL)
780 listener->origin = origin;
781 listener->listener = afb_evt_listener_create(&evt_push_itf, origin);
782 if (listener->listener != NULL) {
783 rc = afb_session_set_cookie(session, origin, listener, (void*)afb_api_dbus_server_listener_free);
786 afb_evt_listener_unref(listener->listener);
790 afb_api_dbus_server_origin_unref(origin);
794 /******************* dbus request part for server *****************/
797 * Structure for a dbus request
800 struct afb_xreq xreq; /**< the xreq of the request */
801 sd_bus_message *message; /**< the incoming request message */
802 const char *request; /**< the readen request as string */
803 struct json_object *json; /**< the readen request as object */
804 struct listener *listener; /**< the listener for events */
807 /* decrement the reference count of the request and free/release it on falling to null */
808 static void dbus_req_destroy(struct afb_xreq *xreq)
810 struct dbus_req *dreq = CONTAINER_OF_XREQ(struct dbus_req, xreq);
812 afb_context_disconnect(&dreq->xreq.context);
813 json_object_put(dreq->json);
814 sd_bus_message_unref(dreq->message);
818 /* get the object of the request */
819 static struct json_object *dbus_req_json(struct afb_xreq *xreq)
821 struct dbus_req *dreq = CONTAINER_OF_XREQ(struct dbus_req, xreq);
826 void dbus_req_raw_reply(struct afb_xreq *xreq, struct json_object *obj, const char *error, const char *info)
828 struct dbus_req *dreq = CONTAINER_OF_XREQ(struct dbus_req, xreq);
831 rc = sd_bus_reply_method_return(dreq->message, "sss",
832 obj ? json_object_to_json_string_ext(obj, JSON_C_TO_STRING_PLAIN|JSON_C_TO_STRING_NOSLASHESCAPE) : "",
836 ERROR("sending the reply failed");
839 static void afb_api_dbus_server_event_send(struct origin *origin, char order, const char *event, int eventid, const char *data, uint64_t msgid);
841 static int dbus_req_subscribe(struct afb_xreq *xreq, struct afb_event_x2 *event)
843 struct dbus_req *dreq = CONTAINER_OF_XREQ(struct dbus_req, xreq);
847 rc = afb_evt_event_x2_add_watch(dreq->listener->listener, event);
848 sd_bus_message_get_cookie(dreq->message, &msgid);
849 afb_api_dbus_server_event_send(dreq->listener->origin, 'S', afb_evt_event_x2_fullname(event), afb_evt_event_x2_id(event), "", msgid);
853 static int dbus_req_unsubscribe(struct afb_xreq *xreq, struct afb_event_x2 *event)
855 struct dbus_req *dreq = CONTAINER_OF_XREQ(struct dbus_req, xreq);
859 sd_bus_message_get_cookie(dreq->message, &msgid);
860 afb_api_dbus_server_event_send(dreq->listener->origin, 'U', afb_evt_event_x2_fullname(event), afb_evt_event_x2_id(event), "", msgid);
861 rc = afb_evt_event_x2_remove_watch(dreq->listener->listener, event);
865 const struct afb_xreq_query_itf afb_api_dbus_xreq_itf = {
866 .json = dbus_req_json,
867 .reply = dbus_req_raw_reply,
868 .unref = dbus_req_destroy,
869 .subscribe = dbus_req_subscribe,
870 .unsubscribe = dbus_req_unsubscribe,
873 /******************* server part **********************************/
875 static void afb_api_dbus_server_event_send(struct origin *origin, char order, const char *event, int eventid, const char *data, uint64_t msgid)
878 struct api_dbus *api;
879 struct sd_bus_message *msg;
884 rc = sd_bus_message_new_method_call(api->sdbus, &msg, origin->name, api->path, api->name, "event");
888 rc = sd_bus_message_append(msg, "yisst", (uint8_t)order, (int32_t)eventid, event, data, msgid);
892 rc = sd_bus_send(api->sdbus, msg, NULL); /* NULL for cookie implies no expected reply */
897 ERROR("error while send event %c%s(%d) to %s", order, event, eventid, origin->name);
899 sd_bus_message_unref(msg);
902 static void afb_api_dbus_server_event_add(void *closure, const char *event, int eventid)
904 afb_api_dbus_server_event_send(closure, '+', event, eventid, "", 0);
907 static void afb_api_dbus_server_event_remove(void *closure, const char *event, int eventid)
909 afb_api_dbus_server_event_send(closure, '-', event, eventid, "", 0);
912 static void afb_api_dbus_server_event_push(void *closure, const char *event, int eventid, struct json_object *object)
914 const char *data = json_object_to_json_string_ext(object, JSON_C_TO_STRING_PLAIN|JSON_C_TO_STRING_NOSLASHESCAPE);
915 afb_api_dbus_server_event_send(closure, '!', event, eventid, data, 0);
916 json_object_put(object);
919 static void afb_api_dbus_server_event_broadcast(void *closure, const char *event, int eventid, struct json_object *object)
922 struct api_dbus *api;
925 rc = sd_bus_emit_signal(api->sdbus, api->path, api->name, "broadcast",
926 "ss", event, json_object_to_json_string_ext(object, JSON_C_TO_STRING_PLAIN|JSON_C_TO_STRING_NOSLASHESCAPE));
928 ERROR("error while broadcasting event %s", event);
929 json_object_put(object);
932 /* called when the object for the service is called */
933 static int api_dbus_server_on_object_called(sd_bus_message *message, void *userdata, sd_bus_error *ret_error)
939 struct dbus_req *dreq;
940 struct api_dbus *api = userdata;
942 struct afb_session *session;
943 struct listener *listener;
944 enum json_tokener_error jerr;
946 /* check the interface */
947 if (strcmp(sd_bus_message_get_interface(message), api->name) != 0)
951 method = sd_bus_message_get_member(message);
953 /* create the request */
954 dreq = calloc(1 , sizeof *dreq);
959 rc = sd_bus_message_read(message, "ssus", &dreq->request, &uuid, &flags, &creds);
961 sd_bus_reply_method_errorf(message, SD_BUS_ERROR_INVALID_SIGNATURE, "invalid signature");
965 /* connect to the context */
966 afb_xreq_init(&dreq->xreq, &afb_api_dbus_xreq_itf);
967 if (afb_context_connect(&dreq->xreq.context, uuid, NULL) < 0)
969 session = dreq->xreq.context.session;
971 /* get the listener */
972 listener = afb_api_dbus_server_listener_get(api, sd_bus_message_get_sender(message), session);
973 if (listener == NULL)
976 /* fulfill the request and emit it */
977 dreq->xreq.context.flags = flags;
978 dreq->xreq.cred = afb_cred_mixed_on_behalf_import(listener->origin->cred, uuid, creds && creds[0] ? creds : NULL);
979 dreq->message = sd_bus_message_ref(message);
980 dreq->json = json_tokener_parse_verbose(dreq->request, &jerr);
981 if (jerr != json_tokener_success) {
982 /* lazy error detection of json request. Is it to improve? */
983 dreq->json = json_object_new_string(dreq->request);
985 dreq->listener = listener;
986 dreq->xreq.request.called_api = api->api;
987 dreq->xreq.request.called_verb = method;
988 afb_xreq_process(&dreq->xreq, api->server.apiset);
992 sd_bus_reply_method_errorf(message, SD_BUS_ERROR_NO_MEMORY, "out of memory");
998 /* create the service */
999 int afb_api_dbus_add_server(const char *path, struct afb_apiset *declare_set, struct afb_apiset *call_set)
1002 struct api_dbus *api;
1004 /* get the dbus api object connected */
1005 api = make_api_dbus(path);
1009 /* request the service object name */
1010 rc = sd_bus_request_name(api->sdbus, api->name, 0);
1013 ERROR("can't register name %s", api->name);
1017 /* connect the service to the dbus object */
1018 rc = sd_bus_add_object(api->sdbus, &api->server.slot_call, api->path, api_dbus_server_on_object_called, api);
1021 ERROR("can't add dbus object %s for %s", api->path, api->name);
1024 INFO("afb service over dbus installed, name %s, path %s", api->name, api->path);
1026 api->server.listener = afb_evt_listener_create(&evt_broadcast_itf, api);
1027 api->server.apiset = afb_apiset_addref(call_set);
1030 sd_bus_release_name(api->sdbus, api->name);
1032 destroy_api_dbus(api);