2 * Copyright (C) 2015, 2016 "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-plugin.h>
30 #include <afb/afb-req-itf.h>
32 #include "afb-common.h"
35 #include "afb-msg-json.h"
37 #include "afb-api-so.h"
38 #include "afb-context.h"
40 #include "afb-subcall.h"
43 static const char DEFAULT_PATH_PREFIX[] = "/org/agl/afb/api/";
50 * The path given are of the form
51 * system:/org/agl/afb/api/...
53 * user:/org/agl/afb/api/...
57 struct sd_bus *sdbus; /* the bus */
58 char *path; /* path of the object for the API */
59 char *name; /* name/interface of the object */
60 char *api; /* api name of the interface */
63 struct sd_bus_slot *slot_broadcast;
64 struct sd_bus_slot *slot_event;
65 struct dbus_event *events;
66 struct dbus_memo *memos;
69 struct sd_bus_slot *slot_call;
70 struct afb_evt_listener *listener; /* listener for broadcasted events */
71 struct destination *destinations;
80 /******************* common part **********************************/
83 * create a structure api_dbus connected on either the system
84 * bus if 'system' is not null or on the user bus. The connection
85 * is established for either emiting/receiving on 'path' being of length
88 static struct api_dbus *make_api_dbus_3(int system, const char *path, size_t pathlen)
94 /* allocates the structure */
95 api = calloc(1, sizeof *api + 1 + pathlen + pathlen);
101 /* init the structure's strings */
103 /* path is copied after the struct */
104 api->path = (void*)(api+1);
105 strcpy(api->path, path);
107 /* api name is at the end of the path */
108 api->api = strrchr(api->path, '/');
109 if (api->api == NULL) {
114 if (!afb_apis_is_valid_api_name(api->api)) {
119 /* the name/interface is copied after the path */
120 api->name = &api->path[pathlen + 1];
121 strcpy(api->name, &path[1]);
122 ptr = strchr(api->name, '/');
125 ptr = strchr(ptr, '/');
129 sdbus = (system ? afb_common_get_system_bus : afb_common_get_user_bus)();
143 * create a structure api_dbus connected on either the system
144 * bus if 'system' is not null or on the user bus. The connection
145 * is established for either emiting/receiving on 'path'.
146 * If 'path' is not absolute, it is prefixed with DEFAULT_PATH_PREFIX.
148 static struct api_dbus *make_api_dbus_2(int system, const char *path)
153 /* check the length of the path */
160 /* if the path is absolute, creation now */
162 return make_api_dbus_3(system, path, len);
164 /* compute the path prefixed with DEFAULT_PATH_PREFIX */
165 assert(strlen(DEFAULT_PATH_PREFIX) > 0);
166 assert(DEFAULT_PATH_PREFIX[strlen(DEFAULT_PATH_PREFIX) - 1] == '/');
167 len += strlen(DEFAULT_PATH_PREFIX);
168 ptr = alloca(len + 1);
169 strcpy(stpcpy(ptr, DEFAULT_PATH_PREFIX), path);
171 /* creation for prefixed path */
172 return make_api_dbus_3(system, ptr, len);
176 * create a structure api_dbus connected either emiting/receiving
178 * The path can be prefixed with "system:" or "user:" to select
179 * either the user or the system D-Bus. If none is set then user's
181 * If remaining 'path' is not absolute, it is prefixed with
182 * DEFAULT_PATH_PREFIX.
184 static struct api_dbus *make_api_dbus(const char *path)
189 /* retrieves the prefix "scheme-like" part */
190 ptr = strchr(path, ':');
192 return make_api_dbus_2(0, path);
194 /* check the prefix part */
195 preflen = (size_t)(ptr - path);
196 if (strncmp(path, "system", preflen) == 0)
197 return make_api_dbus_2(1, ptr + 1);
199 if (strncmp(path, "user", preflen) == 0)
200 return make_api_dbus_2(0, ptr + 1);
202 /* TODO: connect to a foreign D-Bus? */
207 static void destroy_api_dbus(struct api_dbus *api)
212 /******************* client part **********************************/
215 * structure for recording query data
218 struct dbus_memo *next; /* the next memo */
219 struct api_dbus *api; /* the dbus api */
220 struct afb_req req; /* the request handle */
221 struct afb_context *context; /* the context of the query */
222 uint64_t msgid; /* the message identifier */
227 struct dbus_event *next;
228 struct afb_event event;
233 /* allocates and init the memorizing data */
234 static struct dbus_memo *api_dbus_client_memo_make(struct api_dbus *api, struct afb_req req, struct afb_context *context)
236 struct dbus_memo *memo;
238 memo = malloc(sizeof *memo);
242 memo->context = context;
245 memo->next = api->client.memos;
246 api->client.memos = memo;
251 /* free and release the memorizing data */
252 static void api_dbus_client_memo_destroy(struct dbus_memo *memo)
254 struct dbus_memo **prv;
256 prv = &memo->api->client.memos;
257 while (*prv != NULL) {
265 afb_req_unref(memo->req);
269 /* search a memorized request */
270 static struct dbus_memo *api_dbus_client_memo_search(struct api_dbus *api, uint64_t msgid)
272 struct dbus_memo *memo;
274 memo = api->client.memos;
275 while (memo != NULL && memo->msgid != msgid)
281 /* callback when received answer */
282 static int api_dbus_client_on_reply(sd_bus_message *message, void *userdata, sd_bus_error *ret_error)
285 struct dbus_memo *memo;
286 const char *first, *second;
290 /* retrieve the recorded data */
294 rc = sd_bus_message_read(message, "yssu", &type, &first, &second, &flags);
296 /* failing to have the answer */
297 afb_req_fail(memo->req, "error", "dbus error");
299 /* report the answer */
300 memo->context->flags = (unsigned)flags;
303 afb_req_success(memo->req, json_tokener_parse(first), *second ? second : NULL);
306 afb_req_fail(memo->req, first, *second ? second : NULL);
309 afb_req_send(memo->req, first, strlen(first));
312 afb_req_fail(memo->req, "error", "dbus link broken");
316 api_dbus_client_memo_destroy(memo);
320 /* on call, propagate it to the dbus service */
321 static void api_dbus_client_call(struct api_dbus *api, struct afb_req req, struct afb_context *context, const char *verb, size_t lenverb)
325 char *method = strndupa(verb, lenverb);
326 struct dbus_memo *memo;
327 struct sd_bus_message *msg;
329 /* create the recording data */
330 memo = api_dbus_client_memo_make(api, req, context);
332 afb_req_fail(req, "error", "out of memory");
336 /* creates the message */
338 rc = sd_bus_message_new_method_call(api->sdbus, &msg, api->name, api->path, api->name, method);
342 rc = sd_bus_message_append(msg, "ssu",
343 afb_req_raw(req, &size),
344 ctxClientGetUuid(context->session),
345 (uint32_t)context->flags);
350 rc = sd_bus_call_async(api->sdbus, NULL, msg, api_dbus_client_on_reply, memo, (uint64_t)-1);
354 rc = sd_bus_message_get_cookie(msg, &memo->msgid);
359 /* if there was an error report it directly */
361 afb_req_fail(req, "error", "dbus error");
362 api_dbus_client_memo_destroy(memo);
364 sd_bus_message_unref(msg);
367 static int api_dbus_service_start(struct api_dbus *api, int share_session, int onneed)
369 /* not an error when onneed */
373 /* already started: it is an error */
374 ERROR("The Dbus binding %s is not a startable service", api->name);
378 /* receives broadcasted events */
379 static int api_dbus_client_on_broadcast_event(sd_bus_message *m, void *userdata, sd_bus_error *ret_error)
381 struct json_object *object;
382 const char *event, *data;
383 int rc = sd_bus_message_read(m, "ss", &event, &data);
385 ERROR("unreadable broadcasted event");
387 object = json_tokener_parse(data);
388 afb_evt_broadcast(event, object);
393 /* search the event */
394 static struct dbus_event *api_dbus_client_event_search(struct api_dbus *api, int id, const char *name)
396 struct dbus_event *ev;
398 ev = api->client.events;
399 while (ev != NULL && (ev->id != id || 0 != strcmp(afb_evt_event_name(ev->event), name)))
406 static void api_dbus_client_event_create(struct api_dbus *api, int id, const char *name)
408 struct dbus_event *ev;
410 /* check conflicts */
411 ev = api_dbus_client_event_search(api, id, name);
417 /* no conflict, try to add it */
418 ev = malloc(sizeof *ev);
420 ev->event = afb_evt_create_event(name);
421 if (ev->event.closure == NULL)
426 ev->next = api->client.events;
427 api->client.events = ev;
431 ERROR("can't create event %s, out of memory", name);
434 /* removes an event */
435 static void api_dbus_client_event_drop(struct api_dbus *api, int id, const char *name)
437 struct dbus_event *ev, **prv;
439 /* retrieves the event */
440 ev = api_dbus_client_event_search(api, id, name);
442 ERROR("event %s not found", name);
446 /* decrease the reference count */
450 /* unlinks the event */
451 prv = &api->client.events;
456 /* destroys the event */
457 afb_event_drop(ev->event);
462 static void api_dbus_client_event_push(struct api_dbus *api, int id, const char *name, const char *data)
464 struct json_object *object;
465 struct dbus_event *ev;
467 /* retrieves the event */
468 ev = api_dbus_client_event_search(api, id, name);
470 ERROR("event %s not found", name);
474 /* destroys the event */
475 object = json_tokener_parse(data);
476 afb_event_push(ev->event, object);
479 /* subscribes an event */
480 static void api_dbus_client_event_subscribe(struct api_dbus *api, int id, const char *name, uint64_t msgid)
483 struct dbus_event *ev;
484 struct dbus_memo *memo;
486 /* retrieves the event */
487 ev = api_dbus_client_event_search(api, id, name);
489 ERROR("event %s not found", name);
493 /* retrieves the memo */
494 memo = api_dbus_client_memo_search(api, msgid);
496 ERROR("message not found");
500 /* subscribe the request to the event */
501 rc = afb_req_subscribe(memo->req, ev->event);
503 ERROR("can't subscribe: %m");
506 /* unsubscribes an event */
507 static void api_dbus_client_event_unsubscribe(struct api_dbus *api, int id, const char *name, uint64_t msgid)
510 struct dbus_event *ev;
511 struct dbus_memo *memo;
513 /* retrieves the event */
514 ev = api_dbus_client_event_search(api, id, name);
516 ERROR("event %s not found", name);
520 /* retrieves the memo */
521 memo = api_dbus_client_memo_search(api, msgid);
523 ERROR("message not found");
527 /* unsubscribe the request from the event */
528 rc = afb_req_unsubscribe(memo->req, ev->event);
530 ERROR("can't unsubscribe: %m");
533 /* receives calls for event */
534 static int api_dbus_client_on_manage_event(sd_bus_message *m, void *userdata, sd_bus_error *ret_error)
536 const char *eventname, *data;
540 struct api_dbus *api;
543 /* check if expected message */
545 if (0 != strcmp(api->name, sd_bus_message_get_interface(m)))
546 return 0; /* not the expected interface */
547 if (0 != strcmp("event", sd_bus_message_get_member(m)))
548 return 0; /* not the expected member */
549 if (sd_bus_message_get_expect_reply(m))
550 return 0; /* not the expected type of message */
552 /* reads the message */
553 rc = sd_bus_message_read(m, "yisst", &order, &eventid, &eventname, &data, &msgid);
555 ERROR("unreadable event");
559 /* what is the order ? */
560 switch ((char)order) {
561 case '+': /* creates the event */
562 api_dbus_client_event_create(api, eventid, eventname);
564 case '-': /* drops the event */
565 api_dbus_client_event_drop(api, eventid, eventname);
567 case '!': /* pushs the event */
568 api_dbus_client_event_push(api, eventid, eventname, data);
570 case 'S': /* subscribe event for a request */
571 api_dbus_client_event_subscribe(api, eventid, eventname, msgid);
573 case 'U': /* unsubscribe event for a request */
574 api_dbus_client_event_unsubscribe(api, eventid, eventname, msgid);
577 /* unexpected order */
578 ERROR("unexpected order '%c' received", (char)order);
584 /* adds a afb-dbus-service client api */
585 int afb_api_dbus_add_client(const char *path)
588 struct api_dbus *api;
589 struct afb_api afb_api;
592 /* create the dbus client api */
593 api = make_api_dbus(path);
597 /* connect to broadcasted events */
598 rc = asprintf(&match, "type='signal',path='%s',interface='%s',member='broadcast'", api->path, api->name);
601 ERROR("out of memory");
604 rc = sd_bus_add_match(api->sdbus, &api->client.slot_broadcast, match, api_dbus_client_on_broadcast_event, api);
608 ERROR("can't add dbus match %s for %s", api->path, api->name);
612 /* connect to event management */
613 rc = sd_bus_add_object(api->sdbus, &api->client.slot_event, api->path, api_dbus_client_on_manage_event, api);
616 ERROR("can't add dbus object %s for %s", api->path, api->name);
620 /* record it as an API */
621 afb_api.closure = api;
622 afb_api.call = (void*)api_dbus_client_call;
623 afb_api.service_start = (void*)api_dbus_service_start;
624 if (afb_apis_add(api->api, afb_api) < 0)
630 destroy_api_dbus(api);
635 /******************* event structures for server part **********************************/
637 static void afb_api_dbus_server_event_add(void *closure, const char *event, int eventid);
638 static void afb_api_dbus_server_event_remove(void *closure, const char *event, int eventid);
639 static void afb_api_dbus_server_event_push(void *closure, const char *event, int eventid, struct json_object *object);
640 static void afb_api_dbus_server_event_broadcast(void *closure, const char *event, int eventid, struct json_object *object);
642 /* the interface for events broadcasting */
643 static const struct afb_evt_itf evt_broadcast_itf = {
644 .broadcast = afb_api_dbus_server_event_broadcast,
647 /* the interface for events pushing */
648 static const struct afb_evt_itf evt_push_itf = {
649 .push = afb_api_dbus_server_event_push,
650 .add = afb_api_dbus_server_event_add,
651 .remove = afb_api_dbus_server_event_remove
654 /******************* destination description part for server *****************************/
658 /* link to next different destination */
659 struct destination *next;
661 /* the server dbus-api */
662 struct api_dbus *api;
664 /* count of references */
667 /* the destination */
671 static struct destination *afb_api_dbus_server_destination_get(struct api_dbus *api, const char *sender)
673 struct destination *destination;
675 /* searchs for an existing destination */
676 destination = api->server.destinations;
677 while (destination != NULL) {
678 if (0 == strcmp(destination->name, sender)) {
679 destination->refcount++;
682 destination = destination->next;
685 /* not found, create it */
686 destination = malloc(strlen(sender) + sizeof *destination);
687 if (destination == NULL)
690 destination->api = api;
691 destination->refcount = 1;
692 strcpy(destination->name, sender);
693 destination->next = api->server.destinations;
694 api->server.destinations = destination;
699 static void afb_api_dbus_server_destination_unref(struct destination *destination)
701 if (!--destination->refcount) {
702 struct destination **prv;
704 prv = &destination->api->server.destinations;
705 while(*prv != destination)
707 *prv = destination->next;
714 /* link to next different destination */
715 struct destination *destination;
717 /* the listener of events */
718 struct afb_evt_listener *listener;
721 static void afb_api_dbus_server_listener_free(struct listener *listener)
723 afb_evt_listener_unref(listener->listener);
724 afb_api_dbus_server_destination_unref(listener->destination);
728 static struct listener *afb_api_dbus_server_listerner_get(struct api_dbus *api, const char *sender, struct AFB_clientCtx *session)
731 struct listener *listener;
732 struct destination *destination;
734 /* get the destination */
735 destination = afb_api_dbus_server_destination_get(api, sender);
736 if (destination == NULL)
739 /* retrieves the stored listener */
740 listener = ctxClientCookieGet(session, destination);
741 if (listener != NULL) {
743 afb_api_dbus_server_destination_unref(destination);
747 /* creates the listener */
748 listener = malloc(sizeof *listener);
749 if (listener == NULL)
752 listener->destination = destination;
753 listener->listener = afb_evt_listener_create(&evt_push_itf, destination);
754 if (listener->listener != NULL) {
755 rc = ctxClientCookieSet(session, destination, listener, (void*)afb_api_dbus_server_listener_free);
758 afb_evt_listener_unref(listener->listener);
762 afb_api_dbus_server_destination_unref(destination);
766 /******************* dbus request part for server *****************/
769 * structure for a dbus request
772 struct afb_context context; /* the context, should be THE FIRST */
773 sd_bus_message *message; /* the incoming request message */
774 const char *request; /* the readen request as string */
775 struct json_object *json; /* the readen request as object */
776 struct listener *listener; /* the listener for events */
777 int refcount; /* reference count of the request */
780 /* increment the reference count of the request */
781 static void dbus_req_addref(struct dbus_req *dreq)
786 /* decrement the reference count of the request and free/release it on falling to null */
787 static void dbus_req_unref(struct dbus_req *dreq)
789 if (dreq == NULL || --dreq->refcount)
792 afb_context_disconnect(&dreq->context);
793 json_object_put(dreq->json);
794 sd_bus_message_unref(dreq->message);
798 /* get the object of the request */
799 static struct json_object *dbus_req_json(struct dbus_req *dreq)
801 if (dreq->json == NULL) {
802 dreq->json = json_tokener_parse(dreq->request);
803 if (dreq->json == NULL) {
804 /* lazy error detection of json request. Is it to improve? */
805 dreq->json = json_object_new_string(dreq->request);
811 /* get the argument of the request of 'name' */
812 static struct afb_arg dbus_req_get(struct dbus_req *dreq, const char *name)
814 return afb_msg_json_get_arg(dbus_req_json(dreq), name);
817 static void dbus_req_reply(struct dbus_req *dreq, uint8_t type, const char *first, const char *second)
820 rc = sd_bus_reply_method_return(dreq->message,
821 "yssu", type, first ? : "", second ? : "", (uint32_t)dreq->context.flags);
823 ERROR("sending the reply failed");
826 static void dbus_req_success(struct dbus_req *dreq, struct json_object *obj, const char *info)
828 dbus_req_reply(dreq, RETOK, json_object_to_json_string_ext(obj, JSON_C_TO_STRING_PLAIN), info);
831 static void dbus_req_fail(struct dbus_req *dreq, const char *status, const char *info)
833 dbus_req_reply(dreq, RETERR, status, info);
836 static const char *dbus_req_raw(struct dbus_req *dreq, size_t *size)
839 *size = strlen(dreq->request);
840 return dreq->request;
843 static void dbus_req_send(struct dbus_req *dreq, const char *buffer, size_t size)
845 /* TODO: how to put sized buffer as strings? things aren't clear here!!! */
846 dbus_req_reply(dreq, RETRAW, buffer, "");
849 static void afb_api_dbus_server_event_send(struct destination *destination, char order, const char *event, int eventid, const char *data, uint64_t msgid);
851 static int dbus_req_subscribe(struct dbus_req *dreq, struct afb_event event)
856 rc = afb_evt_add_watch(dreq->listener->listener, event);
857 sd_bus_message_get_cookie(dreq->message, &msgid);
858 afb_api_dbus_server_event_send(dreq->listener->destination, 'S', afb_evt_event_name(event), afb_evt_event_id(event), "", msgid);
862 static int dbus_req_unsubscribe(struct dbus_req *dreq, struct afb_event event)
867 sd_bus_message_get_cookie(dreq->message, &msgid);
868 afb_api_dbus_server_event_send(dreq->listener->destination, 'U', afb_evt_event_name(event), afb_evt_event_id(event), "", msgid);
869 rc = afb_evt_remove_watch(dreq->listener->listener, event);
873 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);
875 const struct afb_req_itf afb_api_dbus_req_itf = {
876 .json = (void*)dbus_req_json,
877 .get = (void*)dbus_req_get,
878 .success = (void*)dbus_req_success,
879 .fail = (void*)dbus_req_fail,
880 .raw = (void*)dbus_req_raw,
881 .send = (void*)dbus_req_send,
882 .context_get = (void*)afb_context_get,
883 .context_set = (void*)afb_context_set,
884 .addref = (void*)dbus_req_addref,
885 .unref = (void*)dbus_req_unref,
886 .session_close = (void*)afb_context_close,
887 .session_set_LOA = (void*)afb_context_change_loa,
888 .subscribe = (void*)dbus_req_subscribe,
889 .unsubscribe = (void*)dbus_req_unsubscribe,
890 .subcall = (void*)dbus_req_subcall
893 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)
895 afb_subcall(&dreq->context, api, verb, args, callback, closure, (struct afb_req){ .itf = &afb_api_dbus_req_itf, .closure = dreq });
898 /******************* server part **********************************/
900 static void afb_api_dbus_server_event_send(struct destination *destination, char order, const char *event, int eventid, const char *data, uint64_t msgid)
903 struct api_dbus *api;
904 struct sd_bus_message *msg;
906 api = destination->api;
909 rc = sd_bus_message_new_method_call(api->sdbus, &msg, destination->name, api->path, api->name, "event");
913 rc = sd_bus_message_append(msg, "yisst", (uint8_t)order, (int32_t)eventid, event, data, msgid);
917 rc = sd_bus_send(api->sdbus, msg, NULL); /* NULL for cookie implies no expected reply */
922 ERROR("error while send event %c%s(%d) to %s", order, event, eventid, destination->name);
924 sd_bus_message_unref(msg);
927 static void afb_api_dbus_server_event_add(void *closure, const char *event, int eventid)
929 afb_api_dbus_server_event_send(closure, '+', event, eventid, "", 0);
932 static void afb_api_dbus_server_event_remove(void *closure, const char *event, int eventid)
934 afb_api_dbus_server_event_send(closure, '-', event, eventid, "", 0);
937 static void afb_api_dbus_server_event_push(void *closure, const char *event, int eventid, struct json_object *object)
939 const char *data = json_object_to_json_string_ext(object, JSON_C_TO_STRING_PLAIN);
940 afb_api_dbus_server_event_send(closure, '!', event, eventid, data, 0);
943 static void afb_api_dbus_server_event_broadcast(void *closure, const char *event, int eventid, struct json_object *object)
946 struct api_dbus *api;
949 rc = sd_bus_emit_signal(api->sdbus, api->path, api->name, "broadcast",
950 "ss", event, json_object_to_json_string_ext(object, JSON_C_TO_STRING_PLAIN));
952 ERROR("error while broadcasting event %s", event);
953 json_object_put(object);
956 /* called when the object for the service is called */
957 static int api_dbus_server_on_object_called(sd_bus_message *message, void *userdata, sd_bus_error *ret_error)
962 struct dbus_req *dreq;
963 struct api_dbus *api = userdata;
966 struct AFB_clientCtx *session;
967 struct listener *listener;
969 /* check the interface */
970 if (strcmp(sd_bus_message_get_interface(message), api->name) != 0)
974 method = sd_bus_message_get_member(message);
976 /* create the request */
977 dreq = calloc(1 , sizeof *dreq);
982 rc = sd_bus_message_read(message, "ssu", &dreq->request, &uuid, &flags);
984 sd_bus_reply_method_errorf(message, SD_BUS_ERROR_INVALID_SIGNATURE, "invalid signature");
988 /* connect to the context */
989 if (afb_context_connect(&dreq->context, uuid, NULL) < 0)
991 session = dreq->context.session;
993 /* get the listener */
994 listener = afb_api_dbus_server_listerner_get(api, sd_bus_message_get_sender(message), session);
995 if (listener == NULL)
998 /* fulfill the request and emit it */
999 dreq->context.flags = flags;
1000 dreq->message = sd_bus_message_ref(message);
1002 dreq->listener = listener;
1004 areq.itf = &afb_api_dbus_req_itf;
1005 areq.closure = dreq;
1006 afb_apis_call_(areq, &dreq->context, api->api, method);
1007 dbus_req_unref(dreq);
1011 sd_bus_reply_method_errorf(message, SD_BUS_ERROR_NO_MEMORY, "out of memory");
1017 /* create the service */
1018 int afb_api_dbus_add_server(const char *path)
1021 struct api_dbus *api;
1023 /* get the dbus api object connected */
1024 api = make_api_dbus(path);
1028 /* request the service object name */
1029 rc = sd_bus_request_name(api->sdbus, api->name, 0);
1032 ERROR("can't register name %s", api->name);
1036 /* connect the service to the dbus object */
1037 rc = sd_bus_add_object(api->sdbus, &api->server.slot_call, api->path, api_dbus_server_on_object_called, api);
1040 ERROR("can't add dbus object %s for %s", api->path, api->name);
1043 INFO("afb service over dbus installed, name %s, path %s", api->name, api->path);
1045 api->server.listener = afb_evt_listener_create(&evt_broadcast_itf, api);
1048 sd_bus_release_name(api->sdbus, api->name);
1050 destroy_api_dbus(api);