2 * Copyright (C) 2015-2019 "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 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-session.h"
37 #include "afb-msg-json.h"
39 #include "afb-apiset.h"
40 #include "afb-api-dbus.h"
41 #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 jobs_acquire_event_manager();
134 sdbus = (system ? systemd_get_system_bus : systemd_get_user_bus)();
148 * create a structure api_dbus connected on either the system
149 * bus if 'system' is not null or on the user bus. The connection
150 * is established for either emiting/receiving on 'path'.
151 * If 'path' is not absolute, it is prefixed with DEFAULT_PATH_PREFIX.
153 static struct api_dbus *make_api_dbus_2(int system, const char *path)
158 /* check the length of the path */
165 /* if the path is absolute, creation now */
167 return make_api_dbus_3(system, path, len);
169 /* compute the path prefixed with DEFAULT_PATH_PREFIX */
170 assert(strlen(DEFAULT_PATH_PREFIX) > 0);
171 assert(DEFAULT_PATH_PREFIX[strlen(DEFAULT_PATH_PREFIX) - 1] == '/');
172 len += strlen(DEFAULT_PATH_PREFIX);
173 ptr = alloca(len + 1);
174 strcpy(stpcpy(ptr, DEFAULT_PATH_PREFIX), path);
176 /* creation for prefixed path */
177 return make_api_dbus_3(system, ptr, len);
181 * create a structure api_dbus connected either emiting/receiving
183 * The path can be prefixed with "system:" or "user:" to select
184 * either the user or the system D-Bus. If none is set then user's
186 * If remaining 'path' is not absolute, it is prefixed with
187 * DEFAULT_PATH_PREFIX.
189 static struct api_dbus *make_api_dbus(const char *path)
194 /* retrieves the prefix "scheme-like" part */
195 ptr = strchr(path, ':');
197 return make_api_dbus_2(0, path);
199 /* check the prefix part */
200 preflen = (size_t)(ptr - path);
201 if (strncmp(path, "system", preflen) == 0)
202 return make_api_dbus_2(1, ptr + 1);
204 if (strncmp(path, "user", preflen) == 0)
205 return make_api_dbus_2(0, ptr + 1);
207 /* TODO: connect to a foreign D-Bus? */
212 static void destroy_api_dbus(struct api_dbus *api)
217 /******************* client part **********************************/
220 * structure for recording query data
223 struct dbus_memo *next; /* the next memo */
224 struct api_dbus *api; /* the dbus api */
225 struct afb_xreq *xreq; /* the request */
226 uint64_t msgid; /* the message identifier */
231 struct dbus_event *next;
232 struct afb_event_x2 *event;
237 /* allocates and init the memorizing data */
238 static struct dbus_memo *api_dbus_client_memo_make(struct api_dbus *api, struct afb_xreq *xreq)
240 struct dbus_memo *memo;
242 memo = malloc(sizeof *memo);
244 afb_xreq_unhooked_addref(xreq);
248 memo->next = api->client.memos;
249 api->client.memos = memo;
254 /* free and release the memorizing data */
255 static void api_dbus_client_memo_destroy(struct dbus_memo *memo)
257 struct dbus_memo **prv;
259 prv = &memo->api->client.memos;
260 while (*prv != NULL) {
268 afb_xreq_unhooked_unref(memo->xreq);
272 /* search a memorized request */
273 static struct dbus_memo *api_dbus_client_memo_search(struct api_dbus *api, uint64_t msgid)
275 struct dbus_memo *memo;
277 memo = api->client.memos;
278 while (memo != NULL && memo->msgid != msgid)
284 /* callback when received answer */
285 static int api_dbus_client_on_reply(sd_bus_message *message, void *userdata, sd_bus_error *ret_error)
288 struct dbus_memo *memo;
289 const char *json, *error, *info;
290 struct json_object *object;
291 enum json_tokener_error jerr;
293 /* retrieve the recorded data */
297 rc = sd_bus_message_read(message, "sss", &json, &error, &info);
299 /* failing to have the answer */
300 afb_xreq_reply(memo->xreq, NULL, "error", "dbus error");
302 /* report the answer */
306 object = json_tokener_parse_verbose(json, &jerr);
307 if (jerr != json_tokener_success)
308 object = json_object_new_string(json);
310 afb_xreq_reply(memo->xreq, object, *error ? error : NULL, *info ? info : NULL);
312 api_dbus_client_memo_destroy(memo);
316 /* on call, propagate it to the dbus service */
317 static void api_dbus_client_call(void *closure, struct afb_xreq *xreq)
319 struct api_dbus *api = closure;
322 struct dbus_memo *memo;
323 struct sd_bus_message *msg;
326 /* create the recording data */
327 memo = api_dbus_client_memo_make(api, xreq);
329 afb_xreq_reply(memo->xreq, NULL, "error", "out of memory");
333 /* creates the message */
335 rc = sd_bus_message_new_method_call(api->sdbus, &msg, api->name, api->path, api->name, xreq->request.called_verb);
339 creds = xreq_on_behalf_cred_export(xreq);
340 rc = sd_bus_message_append(msg, "ssus",
341 afb_xreq_raw(xreq, &size),
342 afb_session_uuid(xreq->context.session),
343 (uint32_t)xreq->context.flags,
349 rc = sd_bus_call_async(api->sdbus, NULL, msg, api_dbus_client_on_reply, memo, (uint64_t)-1);
353 rc = sd_bus_message_get_cookie(msg, &memo->msgid);
358 /* if there was an error report it directly */
360 afb_xreq_reply(memo->xreq, NULL, "error", "dbus error");
361 api_dbus_client_memo_destroy(memo);
363 sd_bus_message_unref(msg);
366 /* receives broadcasted events */
367 static int api_dbus_client_on_broadcast_event(sd_bus_message *m, void *userdata, sd_bus_error *ret_error)
369 struct json_object *object;
370 const char *event, *data;
371 enum json_tokener_error jerr;
372 int rc = sd_bus_message_read(m, "ss", &event, &data);
374 ERROR("unreadable broadcasted event");
376 object = json_tokener_parse_verbose(data, &jerr);
377 if (jerr != json_tokener_success)
378 object = json_object_new_string(data);
379 afb_evt_broadcast(event, object);
384 /* search the eventid */
385 static struct dbus_event *api_dbus_client_event_search(struct api_dbus *api, int id, const char *name)
387 struct dbus_event *ev;
389 ev = api->client.events;
390 while (ev != NULL && (ev->id != id || 0 != strcmp(afb_evt_event_x2_fullname(ev->event), name)))
396 /* adds an eventid */
397 static void api_dbus_client_event_create(struct api_dbus *api, int id, const char *name)
399 struct dbus_event *ev;
401 /* check conflicts */
402 ev = api_dbus_client_event_search(api, id, name);
408 /* no conflict, try to add it */
409 ev = malloc(sizeof *ev);
411 ev->event = afb_evt_event_x2_create(name);
412 if (ev->event == NULL)
417 ev->next = api->client.events;
418 api->client.events = ev;
422 ERROR("can't create event %s, out of memory", name);
425 /* removes an eventid */
426 static void api_dbus_client_event_drop(struct api_dbus *api, int id, const char *name)
428 struct dbus_event *ev, **prv;
430 /* retrieves the event */
431 ev = api_dbus_client_event_search(api, id, name);
433 ERROR("event %s not found", name);
437 /* decrease the reference count */
441 /* unlinks the event */
442 prv = &api->client.events;
447 /* destroys the event */
448 afb_evt_event_x2_unref(ev->event);
453 static void api_dbus_client_event_push(struct api_dbus *api, int id, const char *name, const char *data)
455 struct json_object *object;
456 struct dbus_event *ev;
457 enum json_tokener_error jerr;
459 /* retrieves the event */
460 ev = api_dbus_client_event_search(api, id, name);
462 ERROR("event %s not found", name);
466 /* destroys the event */
467 object = json_tokener_parse_verbose(data, &jerr);
468 if (jerr != json_tokener_success)
469 object = json_object_new_string(data);
470 afb_evt_event_x2_push(ev->event, object);
473 /* subscribes an event */
474 static void api_dbus_client_event_subscribe(struct api_dbus *api, int id, const char *name, uint64_t msgid)
477 struct dbus_event *ev;
478 struct dbus_memo *memo;
480 /* retrieves the event */
481 ev = api_dbus_client_event_search(api, id, name);
483 ERROR("event %s not found", name);
487 /* retrieves the memo */
488 memo = api_dbus_client_memo_search(api, msgid);
490 ERROR("message not found");
494 /* subscribe the request to the event */
495 rc = afb_xreq_subscribe(memo->xreq, ev->event);
497 ERROR("can't subscribe: %m");
500 /* unsubscribes an event */
501 static void api_dbus_client_event_unsubscribe(struct api_dbus *api, int id, const char *name, uint64_t msgid)
504 struct dbus_event *ev;
505 struct dbus_memo *memo;
507 /* retrieves the event */
508 ev = api_dbus_client_event_search(api, id, name);
510 ERROR("event %s not found", name);
514 /* retrieves the memo */
515 memo = api_dbus_client_memo_search(api, msgid);
517 ERROR("message not found");
521 /* unsubscribe the request from the event */
522 rc = afb_xreq_unsubscribe(memo->xreq, ev->event);
524 ERROR("can't unsubscribe: %m");
527 /* receives calls for event */
528 static int api_dbus_client_on_manage_event(sd_bus_message *m, void *userdata, sd_bus_error *ret_error)
530 const char *eventname, *data;
534 struct api_dbus *api;
537 /* check if expected message */
539 if (0 != strcmp(api->name, sd_bus_message_get_interface(m)))
540 return 0; /* not the expected interface */
541 if (0 != strcmp("event", sd_bus_message_get_member(m)))
542 return 0; /* not the expected member */
543 if (sd_bus_message_get_expect_reply(m))
544 return 0; /* not the expected type of message */
546 /* reads the message */
547 rc = sd_bus_message_read(m, "yisst", &order, &eventid, &eventname, &data, &msgid);
549 ERROR("unreadable event");
553 /* what is the order ? */
554 switch ((char)order) {
555 case '+': /* creates the event */
556 api_dbus_client_event_create(api, eventid, eventname);
558 case '-': /* drops the event */
559 api_dbus_client_event_drop(api, eventid, eventname);
561 case '!': /* pushs the event */
562 api_dbus_client_event_push(api, eventid, eventname, data);
564 case 'S': /* subscribe event for a request */
565 api_dbus_client_event_subscribe(api, eventid, eventname, msgid);
567 case 'U': /* unsubscribe event for a request */
568 api_dbus_client_event_unsubscribe(api, eventid, eventname, msgid);
571 /* unexpected order */
572 ERROR("unexpected order '%c' received", (char)order);
578 static struct afb_api_itf dbus_api_itf = {
579 .call = api_dbus_client_call
582 /* adds a afb-dbus-service client api */
583 int afb_api_dbus_add_client(const char *path, struct afb_apiset *declare_set, struct afb_apiset *call_set)
586 struct api_dbus *api;
587 struct afb_api_item afb_api;
590 /* create the dbus client api */
591 api = make_api_dbus(path);
595 /* connect to broadcasted events */
596 rc = asprintf(&match, "type='signal',path='%s',interface='%s',member='broadcast'", api->path, api->name);
599 ERROR("out of memory");
602 rc = sd_bus_add_match(api->sdbus, &api->client.slot_broadcast, match, api_dbus_client_on_broadcast_event, api);
606 ERROR("can't add dbus match %s for %s", api->path, api->name);
610 /* connect to event management */
611 rc = sd_bus_add_object(api->sdbus, &api->client.slot_event, api->path, api_dbus_client_on_manage_event, api);
614 ERROR("can't add dbus object %s for %s", api->path, api->name);
618 /* record it as an API */
619 afb_api.closure = api;
620 afb_api.itf = &dbus_api_itf;
621 afb_api.group = NULL;
622 if (afb_apiset_add(declare_set, api->api, afb_api) < 0)
628 destroy_api_dbus(api);
633 /******************* event structures for server part **********************************/
635 static void afb_api_dbus_server_event_add(void *closure, const char *event, int eventid);
636 static void afb_api_dbus_server_event_remove(void *closure, const char *event, int eventid);
637 static void afb_api_dbus_server_event_push(void *closure, const char *event, int eventid, struct json_object *object);
638 static void afb_api_dbus_server_event_broadcast(void *closure, const char *event, struct json_object *object, const char *uuid);
640 /* the interface for events broadcasting */
641 static const struct afb_evt_itf evt_broadcast_itf = {
642 .broadcast = afb_api_dbus_server_event_broadcast,
645 /* the interface for events pushing */
646 static const struct afb_evt_itf evt_push_itf = {
647 .push = afb_api_dbus_server_event_push,
648 .add = afb_api_dbus_server_event_add,
649 .remove = afb_api_dbus_server_event_remove
652 /******************* origin description part for server *****************************/
656 /* link to next different origin */
659 /* the server dbus-api */
660 struct api_dbus *api;
662 /* count of references */
665 /* credentials of the origin */
666 struct afb_cred *cred;
672 /* get the credentials for the message */
673 static void init_origin_creds(struct origin *origin)
682 rc = sd_bus_get_name_creds(origin->api->sdbus, origin->name,
683 SD_BUS_CREDS_PID|SD_BUS_CREDS_UID|SD_BUS_CREDS_GID|SD_BUS_CREDS_SELINUX_CONTEXT,
688 afb_cred_unref(origin->cred);
689 sd_bus_creds_get_uid(c, &uid);
690 sd_bus_creds_get_gid(c, &gid);
691 sd_bus_creds_get_pid(c, &pid);
692 sd_bus_creds_get_selinux_context(c, &context);
693 origin->cred = afb_cred_create(uid, gid, pid, context);
694 sd_bus_creds_unref(c);
698 static struct origin *afb_api_dbus_server_origin_get(struct api_dbus *api, const char *sender)
700 struct origin *origin;
702 /* searchs for an existing origin */
703 origin = api->server.origins;
704 while (origin != NULL) {
705 if (0 == strcmp(origin->name, sender)) {
709 origin = origin->next;
712 /* not found, create it */
713 origin = malloc(strlen(sender) + 1 + sizeof *origin);
718 origin->refcount = 1;
719 strcpy(origin->name, sender);
720 init_origin_creds(origin);
721 origin->next = api->server.origins;
722 api->server.origins = origin;
727 static void afb_api_dbus_server_origin_unref(struct origin *origin)
729 if (!--origin->refcount) {
732 prv = &origin->api->server.origins;
733 while(*prv != origin)
736 afb_cred_unref(origin->cred);
743 /* link to next different origin */
744 struct origin *origin;
746 /* the listener of events */
747 struct afb_evt_listener *listener;
750 static void afb_api_dbus_server_listener_free(struct listener *listener)
752 afb_evt_listener_unref(listener->listener);
753 afb_api_dbus_server_origin_unref(listener->origin);
757 static struct listener *afb_api_dbus_server_listener_get(struct api_dbus *api, const char *sender, struct afb_session *session)
760 struct listener *listener;
761 struct origin *origin;
764 origin = afb_api_dbus_server_origin_get(api, sender);
768 /* retrieves the stored listener */
769 listener = afb_session_get_cookie(session, origin);
770 if (listener != NULL) {
772 afb_api_dbus_server_origin_unref(origin);
776 /* creates the listener */
777 listener = malloc(sizeof *listener);
778 if (listener == NULL)
781 listener->origin = origin;
782 listener->listener = afb_evt_listener_create(&evt_push_itf, origin);
783 if (listener->listener != NULL) {
784 rc = afb_session_set_cookie(session, origin, listener, (void*)afb_api_dbus_server_listener_free);
787 afb_evt_listener_unref(listener->listener);
791 afb_api_dbus_server_origin_unref(origin);
795 /******************* dbus request part for server *****************/
798 * Structure for a dbus request
801 struct afb_xreq xreq; /**< the xreq of the request */
802 sd_bus_message *message; /**< the incoming request message */
803 const char *request; /**< the readen request as string */
804 struct json_object *json; /**< the readen request as object */
805 struct listener *listener; /**< the listener for events */
808 /* decrement the reference count of the request and free/release it on falling to null */
809 static void dbus_req_destroy(struct afb_xreq *xreq)
811 struct dbus_req *dreq = CONTAINER_OF_XREQ(struct dbus_req, xreq);
813 afb_context_disconnect(&dreq->xreq.context);
814 json_object_put(dreq->json);
815 sd_bus_message_unref(dreq->message);
819 /* get the object of the request */
820 static struct json_object *dbus_req_json(struct afb_xreq *xreq)
822 struct dbus_req *dreq = CONTAINER_OF_XREQ(struct dbus_req, xreq);
827 void dbus_req_raw_reply(struct afb_xreq *xreq, struct json_object *obj, const char *error, const char *info)
829 struct dbus_req *dreq = CONTAINER_OF_XREQ(struct dbus_req, xreq);
832 rc = sd_bus_reply_method_return(dreq->message, "sss",
833 obj ? json_object_to_json_string_ext(obj, JSON_C_TO_STRING_PLAIN|JSON_C_TO_STRING_NOSLASHESCAPE) : "",
837 ERROR("sending the reply failed");
840 static void afb_api_dbus_server_event_send(struct origin *origin, char order, const char *event, int eventid, const char *data, uint64_t msgid);
842 static int dbus_req_subscribe(struct afb_xreq *xreq, struct afb_event_x2 *event)
844 struct dbus_req *dreq = CONTAINER_OF_XREQ(struct dbus_req, xreq);
848 rc = afb_evt_event_x2_add_watch(dreq->listener->listener, event);
849 sd_bus_message_get_cookie(dreq->message, &msgid);
850 afb_api_dbus_server_event_send(dreq->listener->origin, 'S', afb_evt_event_x2_fullname(event), afb_evt_event_x2_id(event), "", msgid);
854 static int dbus_req_unsubscribe(struct afb_xreq *xreq, struct afb_event_x2 *event)
856 struct dbus_req *dreq = CONTAINER_OF_XREQ(struct dbus_req, xreq);
860 sd_bus_message_get_cookie(dreq->message, &msgid);
861 afb_api_dbus_server_event_send(dreq->listener->origin, 'U', afb_evt_event_x2_fullname(event), afb_evt_event_x2_id(event), "", msgid);
862 rc = afb_evt_event_x2_remove_watch(dreq->listener->listener, event);
866 const struct afb_xreq_query_itf afb_api_dbus_xreq_itf = {
867 .json = dbus_req_json,
868 .reply = dbus_req_raw_reply,
869 .unref = dbus_req_destroy,
870 .subscribe = dbus_req_subscribe,
871 .unsubscribe = dbus_req_unsubscribe,
874 /******************* server part **********************************/
876 static void afb_api_dbus_server_event_send(struct origin *origin, char order, const char *event, int eventid, const char *data, uint64_t msgid)
879 struct api_dbus *api;
880 struct sd_bus_message *msg;
885 rc = sd_bus_message_new_method_call(api->sdbus, &msg, origin->name, api->path, api->name, "event");
889 rc = sd_bus_message_append(msg, "yisst", (uint8_t)order, (int32_t)eventid, event, data, msgid);
893 rc = sd_bus_send(api->sdbus, msg, NULL); /* NULL for cookie implies no expected reply */
898 ERROR("error while send event %c%s(%d) to %s", order, event, eventid, origin->name);
900 sd_bus_message_unref(msg);
903 static void afb_api_dbus_server_event_add(void *closure, const char *event, int eventid)
905 afb_api_dbus_server_event_send(closure, '+', event, eventid, "", 0);
908 static void afb_api_dbus_server_event_remove(void *closure, const char *event, int eventid)
910 afb_api_dbus_server_event_send(closure, '-', event, eventid, "", 0);
913 static void afb_api_dbus_server_event_push(void *closure, const char *event, int eventid, struct json_object *object)
915 const char *data = json_object_to_json_string_ext(object, JSON_C_TO_STRING_PLAIN|JSON_C_TO_STRING_NOSLASHESCAPE);
916 afb_api_dbus_server_event_send(closure, '!', event, eventid, data, 0);
917 json_object_put(object);
920 static void afb_api_dbus_server_event_broadcast(void *closure, const char *event, struct json_object *object, const char *uuid)
923 struct api_dbus *api;
926 rc = sd_bus_emit_signal(api->sdbus, api->path, api->name, "broadcast",
927 "ss", event, json_object_to_json_string_ext(object, JSON_C_TO_STRING_PLAIN|JSON_C_TO_STRING_NOSLASHESCAPE));
929 ERROR("error while broadcasting event %s", event);
930 json_object_put(object);
933 /* called when the object for the service is called */
934 static int api_dbus_server_on_object_called(sd_bus_message *message, void *userdata, sd_bus_error *ret_error)
940 struct dbus_req *dreq;
941 struct api_dbus *api = userdata;
943 struct afb_session *session;
944 struct listener *listener;
945 enum json_tokener_error jerr;
947 /* check the interface */
948 if (strcmp(sd_bus_message_get_interface(message), api->name) != 0)
952 method = sd_bus_message_get_member(message);
954 /* create the request */
955 dreq = calloc(1 , sizeof *dreq);
960 rc = sd_bus_message_read(message, "ssus", &dreq->request, &uuid, &flags, &creds);
962 sd_bus_reply_method_errorf(message, SD_BUS_ERROR_INVALID_SIGNATURE, "invalid signature");
966 /* connect to the context */
967 afb_xreq_init(&dreq->xreq, &afb_api_dbus_xreq_itf);
968 if (afb_context_connect(&dreq->xreq.context, uuid, NULL) < 0)
970 session = dreq->xreq.context.session;
972 /* get the listener */
973 listener = afb_api_dbus_server_listener_get(api, sd_bus_message_get_sender(message), session);
974 if (listener == NULL)
977 /* fulfill the request and emit it */
978 dreq->xreq.context.flags = flags;
979 dreq->xreq.cred = afb_cred_mixed_on_behalf_import(listener->origin->cred, uuid, creds && creds[0] ? creds : NULL);
980 dreq->message = sd_bus_message_ref(message);
981 dreq->json = json_tokener_parse_verbose(dreq->request, &jerr);
982 if (jerr != json_tokener_success) {
983 /* lazy error detection of json request. Is it to improve? */
984 dreq->json = json_object_new_string(dreq->request);
986 dreq->listener = listener;
987 dreq->xreq.request.called_api = api->api;
988 dreq->xreq.request.called_verb = method;
989 afb_xreq_process(&dreq->xreq, api->server.apiset);
993 sd_bus_reply_method_errorf(message, SD_BUS_ERROR_NO_MEMORY, "out of memory");
999 /* create the service */
1000 int afb_api_dbus_add_server(const char *path, struct afb_apiset *declare_set, struct afb_apiset *call_set)
1003 struct api_dbus *api;
1005 /* get the dbus api object connected */
1006 api = make_api_dbus(path);
1010 /* request the service object name */
1011 rc = sd_bus_request_name(api->sdbus, api->name, 0);
1014 ERROR("can't register name %s", api->name);
1018 /* connect the service to the dbus object */
1019 rc = sd_bus_add_object(api->sdbus, &api->server.slot_call, api->path, api_dbus_server_on_object_called, api);
1022 ERROR("can't add dbus object %s for %s", api->path, api->name);
1025 INFO("afb service over dbus installed, name %s, path %s", api->name, api->path);
1027 api->server.listener = afb_evt_listener_create(&evt_broadcast_itf, api);
1028 api->server.apiset = afb_apiset_addref(call_set);
1031 sd_bus_release_name(api->sdbus, api->name);
1033 destroy_api_dbus(api);