2 * Copyright (C) 2015, 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.
19 #define NO_PLUGIN_VERBOSE_MACRO
26 #include <systemd/sd-bus.h>
27 #include <json-c/json.h>
29 #include <afb/afb-req-itf.h>
31 #include "afb-common.h"
33 #include "afb-session.h"
34 #include "afb-msg-json.h"
36 #include "afb-api-so.h"
37 #include "afb-context.h"
39 #include "afb-subcall.h"
42 static const char DEFAULT_PATH_PREFIX[] = "/org/agl/afb/api/";
49 * The path given are of the form
50 * system:/org/agl/afb/api/...
52 * user:/org/agl/afb/api/...
56 struct sd_bus *sdbus; /* the bus */
57 char *path; /* path of the object for the API */
58 char *name; /* name/interface of the object */
59 char *api; /* api name of the interface */
62 struct sd_bus_slot *slot_broadcast;
63 struct sd_bus_slot *slot_event;
64 struct dbus_event *events;
65 struct dbus_memo *memos;
68 struct sd_bus_slot *slot_call;
69 struct afb_evt_listener *listener; /* listener for broadcasted events */
70 struct destination *destinations;
79 /******************* common part **********************************/
82 * create a structure api_dbus connected on either the system
83 * bus if 'system' is not null or on the user bus. The connection
84 * is established for either emiting/receiving on 'path' being of length
87 static struct api_dbus *make_api_dbus_3(int system, const char *path, size_t pathlen)
93 /* allocates the structure */
94 api = calloc(1, sizeof *api + 1 + pathlen + pathlen);
100 /* init the structure's strings */
102 /* path is copied after the struct */
103 api->path = (void*)(api+1);
104 strcpy(api->path, path);
106 /* api name is at the end of the path */
107 api->api = strrchr(api->path, '/');
108 if (api->api == NULL) {
113 if (!afb_apis_is_valid_api_name(api->api)) {
118 /* the name/interface is copied after the path */
119 api->name = &api->path[pathlen + 1];
120 strcpy(api->name, &path[1]);
121 ptr = strchr(api->name, '/');
124 ptr = strchr(ptr, '/');
128 sdbus = (system ? afb_common_get_system_bus : afb_common_get_user_bus)();
142 * create a structure api_dbus connected on either the system
143 * bus if 'system' is not null or on the user bus. The connection
144 * is established for either emiting/receiving on 'path'.
145 * If 'path' is not absolute, it is prefixed with DEFAULT_PATH_PREFIX.
147 static struct api_dbus *make_api_dbus_2(int system, const char *path)
152 /* check the length of the path */
159 /* if the path is absolute, creation now */
161 return make_api_dbus_3(system, path, len);
163 /* compute the path prefixed with DEFAULT_PATH_PREFIX */
164 assert(strlen(DEFAULT_PATH_PREFIX) > 0);
165 assert(DEFAULT_PATH_PREFIX[strlen(DEFAULT_PATH_PREFIX) - 1] == '/');
166 len += strlen(DEFAULT_PATH_PREFIX);
167 ptr = alloca(len + 1);
168 strcpy(stpcpy(ptr, DEFAULT_PATH_PREFIX), path);
170 /* creation for prefixed path */
171 return make_api_dbus_3(system, ptr, len);
175 * create a structure api_dbus connected either emiting/receiving
177 * The path can be prefixed with "system:" or "user:" to select
178 * either the user or the system D-Bus. If none is set then user's
180 * If remaining 'path' is not absolute, it is prefixed with
181 * DEFAULT_PATH_PREFIX.
183 static struct api_dbus *make_api_dbus(const char *path)
188 /* retrieves the prefix "scheme-like" part */
189 ptr = strchr(path, ':');
191 return make_api_dbus_2(0, path);
193 /* check the prefix part */
194 preflen = (size_t)(ptr - path);
195 if (strncmp(path, "system", preflen) == 0)
196 return make_api_dbus_2(1, ptr + 1);
198 if (strncmp(path, "user", preflen) == 0)
199 return make_api_dbus_2(0, ptr + 1);
201 /* TODO: connect to a foreign D-Bus? */
206 static void destroy_api_dbus(struct api_dbus *api)
211 /******************* client part **********************************/
214 * structure for recording query data
217 struct dbus_memo *next; /* the next memo */
218 struct api_dbus *api; /* the dbus api */
219 struct afb_req req; /* the request handle */
220 struct afb_context *context; /* the context of the query */
221 uint64_t msgid; /* the message identifier */
226 struct dbus_event *next;
227 struct afb_event event;
232 /* allocates and init the memorizing data */
233 static struct dbus_memo *api_dbus_client_memo_make(struct api_dbus *api, struct afb_req req, struct afb_context *context)
235 struct dbus_memo *memo;
237 memo = malloc(sizeof *memo);
241 memo->context = context;
244 memo->next = api->client.memos;
245 api->client.memos = memo;
250 /* free and release the memorizing data */
251 static void api_dbus_client_memo_destroy(struct dbus_memo *memo)
253 struct dbus_memo **prv;
255 prv = &memo->api->client.memos;
256 while (*prv != NULL) {
264 afb_req_unref(memo->req);
268 /* search a memorized request */
269 static struct dbus_memo *api_dbus_client_memo_search(struct api_dbus *api, uint64_t msgid)
271 struct dbus_memo *memo;
273 memo = api->client.memos;
274 while (memo != NULL && memo->msgid != msgid)
280 /* callback when received answer */
281 static int api_dbus_client_on_reply(sd_bus_message *message, void *userdata, sd_bus_error *ret_error)
284 struct dbus_memo *memo;
285 const char *first, *second;
289 /* retrieve the recorded data */
293 rc = sd_bus_message_read(message, "yssu", &type, &first, &second, &flags);
295 /* failing to have the answer */
296 afb_req_fail(memo->req, "error", "dbus error");
298 /* report the answer */
299 memo->context->flags = (unsigned)flags;
302 afb_req_success(memo->req, json_tokener_parse(first), *second ? second : NULL);
305 afb_req_fail(memo->req, first, *second ? second : NULL);
308 afb_req_send(memo->req, first, strlen(first));
311 afb_req_fail(memo->req, "error", "dbus link broken");
315 api_dbus_client_memo_destroy(memo);
319 /* on call, propagate it to the dbus service */
320 static void api_dbus_client_call(void *closure, struct afb_req req, struct afb_context *context, const char *verb)
322 struct api_dbus *api = closure;
325 struct dbus_memo *memo;
326 struct sd_bus_message *msg;
328 /* create the recording data */
329 memo = api_dbus_client_memo_make(api, req, context);
331 afb_req_fail(req, "error", "out of memory");
335 /* creates the message */
337 rc = sd_bus_message_new_method_call(api->sdbus, &msg, api->name, api->path, api->name, verb);
341 rc = sd_bus_message_append(msg, "ssu",
342 afb_req_raw(req, &size),
343 afb_session_uuid(context->session),
344 (uint32_t)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_req_fail(req, "error", "dbus error");
361 api_dbus_client_memo_destroy(memo);
363 sd_bus_message_unref(msg);
366 static int api_dbus_service_start(void *closure, int share_session, int onneed)
368 struct api_dbus *api = closure;
370 /* not an error when onneed */
374 /* already started: it is an error */
375 ERROR("The Dbus binding %s is not a startable service", api->name);
379 /* receives broadcasted events */
380 static int api_dbus_client_on_broadcast_event(sd_bus_message *m, void *userdata, sd_bus_error *ret_error)
382 struct json_object *object;
383 const char *event, *data;
384 int rc = sd_bus_message_read(m, "ss", &event, &data);
386 ERROR("unreadable broadcasted event");
388 object = json_tokener_parse(data);
389 afb_evt_broadcast(event, object);
394 /* search the event */
395 static struct dbus_event *api_dbus_client_event_search(struct api_dbus *api, int id, const char *name)
397 struct dbus_event *ev;
399 ev = api->client.events;
400 while (ev != NULL && (ev->id != id || 0 != strcmp(afb_evt_event_name(ev->event), name)))
407 static void api_dbus_client_event_create(struct api_dbus *api, int id, const char *name)
409 struct dbus_event *ev;
411 /* check conflicts */
412 ev = api_dbus_client_event_search(api, id, name);
418 /* no conflict, try to add it */
419 ev = malloc(sizeof *ev);
421 ev->event = afb_evt_create_event(name);
422 if (ev->event.closure == NULL)
427 ev->next = api->client.events;
428 api->client.events = ev;
432 ERROR("can't create event %s, out of memory", name);
435 /* removes an event */
436 static void api_dbus_client_event_drop(struct api_dbus *api, int id, const char *name)
438 struct dbus_event *ev, **prv;
440 /* retrieves the event */
441 ev = api_dbus_client_event_search(api, id, name);
443 ERROR("event %s not found", name);
447 /* decrease the reference count */
451 /* unlinks the event */
452 prv = &api->client.events;
457 /* destroys the event */
458 afb_event_drop(ev->event);
463 static void api_dbus_client_event_push(struct api_dbus *api, int id, const char *name, const char *data)
465 struct json_object *object;
466 struct dbus_event *ev;
468 /* retrieves the event */
469 ev = api_dbus_client_event_search(api, id, name);
471 ERROR("event %s not found", name);
475 /* destroys the event */
476 object = json_tokener_parse(data);
477 afb_event_push(ev->event, object);
480 /* subscribes an event */
481 static void api_dbus_client_event_subscribe(struct api_dbus *api, int id, const char *name, uint64_t msgid)
484 struct dbus_event *ev;
485 struct dbus_memo *memo;
487 /* retrieves the event */
488 ev = api_dbus_client_event_search(api, id, name);
490 ERROR("event %s not found", name);
494 /* retrieves the memo */
495 memo = api_dbus_client_memo_search(api, msgid);
497 ERROR("message not found");
501 /* subscribe the request to the event */
502 rc = afb_req_subscribe(memo->req, ev->event);
504 ERROR("can't subscribe: %m");
507 /* unsubscribes an event */
508 static void api_dbus_client_event_unsubscribe(struct api_dbus *api, int id, const char *name, uint64_t msgid)
511 struct dbus_event *ev;
512 struct dbus_memo *memo;
514 /* retrieves the event */
515 ev = api_dbus_client_event_search(api, id, name);
517 ERROR("event %s not found", name);
521 /* retrieves the memo */
522 memo = api_dbus_client_memo_search(api, msgid);
524 ERROR("message not found");
528 /* unsubscribe the request from the event */
529 rc = afb_req_unsubscribe(memo->req, ev->event);
531 ERROR("can't unsubscribe: %m");
534 /* receives calls for event */
535 static int api_dbus_client_on_manage_event(sd_bus_message *m, void *userdata, sd_bus_error *ret_error)
537 const char *eventname, *data;
541 struct api_dbus *api;
544 /* check if expected message */
546 if (0 != strcmp(api->name, sd_bus_message_get_interface(m)))
547 return 0; /* not the expected interface */
548 if (0 != strcmp("event", sd_bus_message_get_member(m)))
549 return 0; /* not the expected member */
550 if (sd_bus_message_get_expect_reply(m))
551 return 0; /* not the expected type of message */
553 /* reads the message */
554 rc = sd_bus_message_read(m, "yisst", &order, &eventid, &eventname, &data, &msgid);
556 ERROR("unreadable event");
560 /* what is the order ? */
561 switch ((char)order) {
562 case '+': /* creates the event */
563 api_dbus_client_event_create(api, eventid, eventname);
565 case '-': /* drops the event */
566 api_dbus_client_event_drop(api, eventid, eventname);
568 case '!': /* pushs the event */
569 api_dbus_client_event_push(api, eventid, eventname, data);
571 case 'S': /* subscribe event for a request */
572 api_dbus_client_event_subscribe(api, eventid, eventname, msgid);
574 case 'U': /* unsubscribe event for a request */
575 api_dbus_client_event_unsubscribe(api, eventid, eventname, msgid);
578 /* unexpected order */
579 ERROR("unexpected order '%c' received", (char)order);
585 /* adds a afb-dbus-service client api */
586 int afb_api_dbus_add_client(const char *path)
589 struct api_dbus *api;
590 struct afb_api afb_api;
593 /* create the dbus client api */
594 api = make_api_dbus(path);
598 /* connect to broadcasted events */
599 rc = asprintf(&match, "type='signal',path='%s',interface='%s',member='broadcast'", api->path, api->name);
602 ERROR("out of memory");
605 rc = sd_bus_add_match(api->sdbus, &api->client.slot_broadcast, match, api_dbus_client_on_broadcast_event, api);
609 ERROR("can't add dbus match %s for %s", api->path, api->name);
613 /* connect to event management */
614 rc = sd_bus_add_object(api->sdbus, &api->client.slot_event, api->path, api_dbus_client_on_manage_event, api);
617 ERROR("can't add dbus object %s for %s", api->path, api->name);
621 /* record it as an API */
622 afb_api.closure = api;
623 afb_api.call = api_dbus_client_call;
624 afb_api.service_start = api_dbus_service_start;
625 if (afb_apis_add(api->api, afb_api) < 0)
631 destroy_api_dbus(api);
636 /******************* event structures for server part **********************************/
638 static void afb_api_dbus_server_event_add(void *closure, const char *event, int eventid);
639 static void afb_api_dbus_server_event_remove(void *closure, const char *event, int eventid);
640 static void afb_api_dbus_server_event_push(void *closure, const char *event, int eventid, struct json_object *object);
641 static void afb_api_dbus_server_event_broadcast(void *closure, const char *event, int eventid, struct json_object *object);
643 /* the interface for events broadcasting */
644 static const struct afb_evt_itf evt_broadcast_itf = {
645 .broadcast = afb_api_dbus_server_event_broadcast,
648 /* the interface for events pushing */
649 static const struct afb_evt_itf evt_push_itf = {
650 .push = afb_api_dbus_server_event_push,
651 .add = afb_api_dbus_server_event_add,
652 .remove = afb_api_dbus_server_event_remove
655 /******************* destination description part for server *****************************/
659 /* link to next different destination */
660 struct destination *next;
662 /* the server dbus-api */
663 struct api_dbus *api;
665 /* count of references */
668 /* the destination */
672 static struct destination *afb_api_dbus_server_destination_get(struct api_dbus *api, const char *sender)
674 struct destination *destination;
676 /* searchs for an existing destination */
677 destination = api->server.destinations;
678 while (destination != NULL) {
679 if (0 == strcmp(destination->name, sender)) {
680 destination->refcount++;
683 destination = destination->next;
686 /* not found, create it */
687 destination = malloc(strlen(sender) + sizeof *destination);
688 if (destination == NULL)
691 destination->api = api;
692 destination->refcount = 1;
693 strcpy(destination->name, sender);
694 destination->next = api->server.destinations;
695 api->server.destinations = destination;
700 static void afb_api_dbus_server_destination_unref(struct destination *destination)
702 if (!--destination->refcount) {
703 struct destination **prv;
705 prv = &destination->api->server.destinations;
706 while(*prv != destination)
708 *prv = destination->next;
715 /* link to next different destination */
716 struct destination *destination;
718 /* the listener of events */
719 struct afb_evt_listener *listener;
722 static void afb_api_dbus_server_listener_free(struct listener *listener)
724 afb_evt_listener_unref(listener->listener);
725 afb_api_dbus_server_destination_unref(listener->destination);
729 static struct listener *afb_api_dbus_server_listerner_get(struct api_dbus *api, const char *sender, struct afb_session *session)
732 struct listener *listener;
733 struct destination *destination;
735 /* get the destination */
736 destination = afb_api_dbus_server_destination_get(api, sender);
737 if (destination == NULL)
740 /* retrieves the stored listener */
741 listener = afb_session_get_cookie(session, destination);
742 if (listener != NULL) {
744 afb_api_dbus_server_destination_unref(destination);
748 /* creates the listener */
749 listener = malloc(sizeof *listener);
750 if (listener == NULL)
753 listener->destination = destination;
754 listener->listener = afb_evt_listener_create(&evt_push_itf, destination);
755 if (listener->listener != NULL) {
756 rc = afb_session_set_cookie(session, destination, listener, (void*)afb_api_dbus_server_listener_free);
759 afb_evt_listener_unref(listener->listener);
763 afb_api_dbus_server_destination_unref(destination);
767 /******************* dbus request part for server *****************/
770 * structure for a dbus request
773 struct afb_context context; /* the context, should be THE FIRST */
774 sd_bus_message *message; /* the incoming request message */
775 const char *request; /* the readen request as string */
776 struct json_object *json; /* the readen request as object */
777 struct listener *listener; /* the listener for events */
778 int refcount; /* reference count of the request */
781 /* increment the reference count of the request */
782 static void dbus_req_addref(struct dbus_req *dreq)
787 /* decrement the reference count of the request and free/release it on falling to null */
788 static void dbus_req_unref(struct dbus_req *dreq)
790 if (dreq == NULL || --dreq->refcount)
793 afb_context_disconnect(&dreq->context);
794 json_object_put(dreq->json);
795 sd_bus_message_unref(dreq->message);
799 /* get the object of the request */
800 static struct json_object *dbus_req_json(struct dbus_req *dreq)
802 if (dreq->json == NULL) {
803 dreq->json = json_tokener_parse(dreq->request);
804 if (dreq->json == NULL && strcmp(dreq->request, "null")) {
805 /* lazy error detection of json request. Is it to improve? */
806 dreq->json = json_object_new_string(dreq->request);
812 /* get the argument of the request of 'name' */
813 static struct afb_arg dbus_req_get(struct dbus_req *dreq, const char *name)
815 return afb_msg_json_get_arg(dbus_req_json(dreq), name);
818 static void dbus_req_reply(struct dbus_req *dreq, uint8_t type, const char *first, const char *second)
821 rc = sd_bus_reply_method_return(dreq->message,
822 "yssu", type, first ? : "", second ? : "", (uint32_t)dreq->context.flags);
824 ERROR("sending the reply failed");
827 static void dbus_req_success(struct dbus_req *dreq, struct json_object *obj, const char *info)
829 dbus_req_reply(dreq, RETOK, json_object_to_json_string_ext(obj, JSON_C_TO_STRING_PLAIN), info);
832 static void dbus_req_fail(struct dbus_req *dreq, const char *status, const char *info)
834 dbus_req_reply(dreq, RETERR, status, info);
837 static const char *dbus_req_raw(struct dbus_req *dreq, size_t *size)
840 *size = strlen(dreq->request);
841 return dreq->request;
844 static void dbus_req_send(struct dbus_req *dreq, const char *buffer, size_t size)
846 /* TODO: how to put sized buffer as strings? things aren't clear here!!! */
847 dbus_req_reply(dreq, RETRAW, buffer, "");
850 static void afb_api_dbus_server_event_send(struct destination *destination, char order, const char *event, int eventid, const char *data, uint64_t msgid);
852 static int dbus_req_subscribe(struct dbus_req *dreq, struct afb_event event)
857 rc = afb_evt_add_watch(dreq->listener->listener, event);
858 sd_bus_message_get_cookie(dreq->message, &msgid);
859 afb_api_dbus_server_event_send(dreq->listener->destination, 'S', afb_evt_event_name(event), afb_evt_event_id(event), "", msgid);
863 static int dbus_req_unsubscribe(struct dbus_req *dreq, struct afb_event event)
868 sd_bus_message_get_cookie(dreq->message, &msgid);
869 afb_api_dbus_server_event_send(dreq->listener->destination, 'U', afb_evt_event_name(event), afb_evt_event_id(event), "", msgid);
870 rc = afb_evt_remove_watch(dreq->listener->listener, event);
874 static void dbus_req_subcall(struct dbus_req *dreq, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*), void *closure);
876 const struct afb_req_itf afb_api_dbus_req_itf = {
877 .json = (void*)dbus_req_json,
878 .get = (void*)dbus_req_get,
879 .success = (void*)dbus_req_success,
880 .fail = (void*)dbus_req_fail,
881 .raw = (void*)dbus_req_raw,
882 .send = (void*)dbus_req_send,
883 .context_get = (void*)afb_context_get,
884 .context_set = (void*)afb_context_set,
885 .addref = (void*)dbus_req_addref,
886 .unref = (void*)dbus_req_unref,
887 .session_close = (void*)afb_context_close,
888 .session_set_LOA = (void*)afb_context_change_loa,
889 .subscribe = (void*)dbus_req_subscribe,
890 .unsubscribe = (void*)dbus_req_unsubscribe,
891 .subcall = (void*)dbus_req_subcall
894 static void dbus_req_subcall(struct dbus_req *dreq, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*), void *closure)
896 afb_subcall(&dreq->context, api, verb, args, callback, closure, (struct afb_req){ .itf = &afb_api_dbus_req_itf, .closure = dreq });
899 /******************* server part **********************************/
901 static void afb_api_dbus_server_event_send(struct destination *destination, char order, const char *event, int eventid, const char *data, uint64_t msgid)
904 struct api_dbus *api;
905 struct sd_bus_message *msg;
907 api = destination->api;
910 rc = sd_bus_message_new_method_call(api->sdbus, &msg, destination->name, api->path, api->name, "event");
914 rc = sd_bus_message_append(msg, "yisst", (uint8_t)order, (int32_t)eventid, event, data, msgid);
918 rc = sd_bus_send(api->sdbus, msg, NULL); /* NULL for cookie implies no expected reply */
923 ERROR("error while send event %c%s(%d) to %s", order, event, eventid, destination->name);
925 sd_bus_message_unref(msg);
928 static void afb_api_dbus_server_event_add(void *closure, const char *event, int eventid)
930 afb_api_dbus_server_event_send(closure, '+', event, eventid, "", 0);
933 static void afb_api_dbus_server_event_remove(void *closure, const char *event, int eventid)
935 afb_api_dbus_server_event_send(closure, '-', event, eventid, "", 0);
938 static void afb_api_dbus_server_event_push(void *closure, const char *event, int eventid, struct json_object *object)
940 const char *data = json_object_to_json_string_ext(object, JSON_C_TO_STRING_PLAIN);
941 afb_api_dbus_server_event_send(closure, '!', event, eventid, data, 0);
942 json_object_put(object);
945 static void afb_api_dbus_server_event_broadcast(void *closure, const char *event, int eventid, struct json_object *object)
948 struct api_dbus *api;
951 rc = sd_bus_emit_signal(api->sdbus, api->path, api->name, "broadcast",
952 "ss", event, json_object_to_json_string_ext(object, JSON_C_TO_STRING_PLAIN));
954 ERROR("error while broadcasting event %s", event);
955 json_object_put(object);
958 /* called when the object for the service is called */
959 static int api_dbus_server_on_object_called(sd_bus_message *message, void *userdata, sd_bus_error *ret_error)
964 struct dbus_req *dreq;
965 struct api_dbus *api = userdata;
968 struct afb_session *session;
969 struct listener *listener;
971 /* check the interface */
972 if (strcmp(sd_bus_message_get_interface(message), api->name) != 0)
976 method = sd_bus_message_get_member(message);
978 /* create the request */
979 dreq = calloc(1 , sizeof *dreq);
984 rc = sd_bus_message_read(message, "ssu", &dreq->request, &uuid, &flags);
986 sd_bus_reply_method_errorf(message, SD_BUS_ERROR_INVALID_SIGNATURE, "invalid signature");
990 /* connect to the context */
991 if (afb_context_connect(&dreq->context, uuid, NULL) < 0)
993 session = dreq->context.session;
995 /* get the listener */
996 listener = afb_api_dbus_server_listerner_get(api, sd_bus_message_get_sender(message), session);
997 if (listener == NULL)
1000 /* fulfill the request and emit it */
1001 dreq->context.flags = flags;
1002 dreq->message = sd_bus_message_ref(message);
1004 dreq->listener = listener;
1006 areq.itf = &afb_api_dbus_req_itf;
1007 areq.closure = dreq;
1008 afb_apis_call(areq, &dreq->context, api->api, method);
1009 dbus_req_unref(dreq);
1013 sd_bus_reply_method_errorf(message, SD_BUS_ERROR_NO_MEMORY, "out of memory");
1019 /* create the service */
1020 int afb_api_dbus_add_server(const char *path)
1023 struct api_dbus *api;
1025 /* get the dbus api object connected */
1026 api = make_api_dbus(path);
1030 /* request the service object name */
1031 rc = sd_bus_request_name(api->sdbus, api->name, 0);
1034 ERROR("can't register name %s", api->name);
1038 /* connect the service to the dbus object */
1039 rc = sd_bus_add_object(api->sdbus, &api->server.slot_call, api->path, api_dbus_server_on_object_called, api);
1042 ERROR("can't add dbus object %s for %s", api->path, api->name);
1045 INFO("afb service over dbus installed, name %s, path %s", api->name, api->path);
1047 api->server.listener = afb_evt_listener_create(&evt_broadcast_itf, api);
1050 sd_bus_release_name(api->sdbus, api->name);
1052 destroy_api_dbus(api);