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.
25 #include <systemd/sd-bus.h>
26 #include <json-c/json.h>
28 #include <afb/afb-plugin.h>
29 #include <afb/afb-req-itf.h>
31 #include "afb-common.h"
35 #include "afb-api-so.h"
36 #include "afb-context.h"
39 static const char DEFAULT_PATH_PREFIX[] = "/org/agl/afb/api/";
42 * The path given are of the form
43 * system:/org/agl/afb/api/...
45 * user:/org/agl/afb/api/...
49 struct sd_bus *sdbus; /* the bus */
50 struct sd_bus_slot *slot; /* the slot */
51 char *path; /* path of the object for the API */
52 char *name; /* name/interface of the object */
53 char *api; /* api name of the interface */
60 /******************* common part **********************************/
63 * create a structure api_dbus connected on either the system
64 * bus if 'system' is not null or on the user bus. The connection
65 * is established for either emiting/receiving on 'path' being of length
68 static struct api_dbus *make_api_dbus_3(int system, const char *path, size_t pathlen)
74 /* allocates the structure */
75 api = calloc(1, sizeof *api + 1 + pathlen + pathlen);
81 /* init the structure's strings */
83 /* path is copied after the struct */
84 api->path = (void*)(api+1);
85 strcpy(api->path, path);
87 /* api name is at the end of the path */
88 api->api = strrchr(api->path, '/');
89 if (api->api == NULL) {
95 /* the name/interface is copied after the path */
96 api->name = &api->path[pathlen + 1];
97 strcpy(api->name, &path[1]);
98 ptr = strchr(api->name, '/');
101 ptr = strchr(ptr, '/');
105 sdbus = (system ? afb_common_get_system_bus : afb_common_get_user_bus)();
119 * create a structure api_dbus connected on either the system
120 * bus if 'system' is not null or on the user bus. The connection
121 * is established for either emiting/receiving on 'path'.
122 * If 'path' is not absolute, it is prefixed with DEFAULT_PATH_PREFIX.
124 static struct api_dbus *make_api_dbus_2(int system, const char *path)
129 /* check the length of the path */
136 /* if the path is absolute, creation now */
138 return make_api_dbus_3(system, path, len);
140 /* compute the path prefixed with DEFAULT_PATH_PREFIX */
141 assert(strlen(DEFAULT_PATH_PREFIX) > 0);
142 assert(DEFAULT_PATH_PREFIX[strlen(DEFAULT_PATH_PREFIX) - 1] == '/');
143 len += strlen(DEFAULT_PATH_PREFIX);
144 ptr = alloca(len + 1);
145 strcpy(stpcpy(ptr, DEFAULT_PATH_PREFIX), path);
147 /* creation for prefixed path */
148 return make_api_dbus_3(system, ptr, len);
152 * create a structure api_dbus connected either emiting/receiving
154 * The path can be prefixed with "system:" or "user:" to select
155 * either the user or the system D-Bus. If none is set then user's
157 * If remaining 'path' is not absolute, it is prefixed with
158 * DEFAULT_PATH_PREFIX.
160 static struct api_dbus *make_api_dbus(const char *path)
165 /* retrieves the prefix "scheme-like" part */
166 ptr = strchr(path, ':');
168 return make_api_dbus_2(0, path);
170 /* check the prefix part */
171 preflen = (size_t)(ptr - path);
172 if (strncmp(path, "system", preflen) == 0)
173 return make_api_dbus_2(1, ptr + 1);
175 if (strncmp(path, "user", preflen) == 0)
176 return make_api_dbus_2(0, ptr + 1);
178 /* TODO: connect to a foreign D-Bus? */
183 static void destroy_api_dbus(struct api_dbus *api)
188 /******************* client part **********************************/
191 * structure for recording query data
194 struct afb_req req; /* the request handle */
195 struct afb_context *context; /* the context of the query */
198 /* allocates and init the memorizing data */
199 static struct dbus_memo *api_dbus_client_make_memo(struct afb_req req, struct afb_context *context)
201 struct dbus_memo *memo;
203 memo = malloc(sizeof *memo);
207 memo->context = context;
212 /* free and release the memorizing data */
213 static void api_dbus_client_free_memo(struct dbus_memo *memo)
215 afb_req_unref(memo->req);
219 /* callback when received answer */
220 static int api_dbus_client_on_reply(sd_bus_message *message, void *userdata, sd_bus_error *ret_error)
223 struct dbus_memo *memo;
224 const char *first, *second;
228 /* retrieve the recorded data */
232 rc = sd_bus_message_read(message, "yssu", &type, &first, &second, &flags);
234 /* failing to have the answer */
235 afb_req_fail(memo->req, "error", "dbus error");
237 /* report the answer */
238 memo->context->flags = (unsigned)flags;
241 afb_req_success(memo->req, json_tokener_parse(first), second);
244 afb_req_fail(memo->req, first, second);
247 afb_req_send(memo->req, first, strlen(first));
250 afb_req_fail(memo->req, "error", "dbus link broken");
254 api_dbus_client_free_memo(memo);
258 /* on call, propagate it to the dbus service */
259 static void api_dbus_client_call(struct api_dbus *api, struct afb_req req, struct afb_context *context, const char *verb, size_t lenverb)
263 char *method = strndupa(verb, lenverb);
264 struct dbus_memo *memo;
266 /* create the recording data */
267 memo = api_dbus_client_make_memo(req, context);
269 afb_req_fail(req, "error", "out of memory");
274 rc = sd_bus_call_method_async(api->sdbus, NULL,
275 api->name, api->path, api->name, method,
276 api_dbus_client_on_reply, memo,
278 afb_req_raw(req, &size),
279 ctxClientGetUuid(context->session),
280 (uint32_t)context->flags);
282 /* if there was an error report it directly */
285 afb_req_fail(req, "error", "dbus error");
286 api_dbus_client_free_memo(memo);
290 /* receives events */
291 static int api_dbus_client_on_event(sd_bus_message *m, void *userdata, sd_bus_error *ret_error)
293 struct json_object *object;
294 const char *event, *data;
295 int rc = sd_bus_message_read(m, "ss", &event, &data);
297 ERROR("unreadable event");
299 object = json_tokener_parse(data);
300 ctxClientEventSend(NULL, event, object);
301 json_object_put(object);
306 /* adds a afb-dbus-service client api */
307 int afb_api_dbus_add_client(const char *path)
310 struct api_dbus *api;
311 struct afb_api afb_api;
314 /* create the dbus client api */
315 api = make_api_dbus(path);
319 /* connect to events */
320 rc = asprintf(&match, "type='signal',path='%s',interface='%s',member='event'", api->path, api->name);
323 ERROR("out of memory");
326 rc = sd_bus_add_match(api->sdbus, &api->slot, match, api_dbus_client_on_event, api);
330 ERROR("can't add dbus object %s for %s", api->path, api->name);
334 /* record it as an API */
335 afb_api.closure = api;
336 afb_api.call = (void*)api_dbus_client_call;
337 if (afb_apis_add(api->api, afb_api) < 0)
343 destroy_api_dbus(api);
348 /******************* dbus request part for server *****************/
351 * structure for a dbus request
354 struct afb_context context; /* the context, should be THE FIRST */
355 sd_bus_message *message; /* the incoming request message */
356 const char *request; /* the readen request as string */
357 struct json_object *json; /* the readen request as object */
358 int refcount; /* reference count of the request */
361 /* increment the reference count of the request */
362 static void dbus_req_addref(struct dbus_req *dreq)
367 /* decrement the reference count of the request and free/release it on falling to null */
368 static void dbus_req_unref(struct dbus_req *dreq)
370 if (dreq == NULL || --dreq->refcount)
373 afb_context_disconnect(&dreq->context);
374 json_object_put(dreq->json);
375 sd_bus_message_unref(dreq->message);
379 /* get the object of the request */
380 static struct json_object *dbus_req_json(struct dbus_req *dreq)
382 if (dreq->json == NULL) {
383 dreq->json = json_tokener_parse(dreq->request);
384 if (dreq->json == NULL) {
385 /* lazy error detection of json request. Is it to improve? */
386 dreq->json = json_object_new_string(dreq->request);
392 /* get the argument of the request of 'name' */
393 static struct afb_arg dbus_req_get(struct dbus_req *dreq, const char *name)
396 struct json_object *value, *root;
398 root = dbus_req_json(dreq);
399 if (root != NULL && json_object_object_get_ex(root, name, &value)) {
401 arg.value = json_object_get_string(value);
410 static void dbus_req_reply(struct dbus_req *dreq, uint8_t type, const char *first, const char *second)
413 rc = sd_bus_reply_method_return(dreq->message,
414 "yssu", type, first, second, (uint32_t)dreq->context.flags);
416 ERROR("sending the reply failed");
419 static void dbus_req_success(struct dbus_req *dreq, struct json_object *obj, const char *info)
421 dbus_req_reply(dreq, RETOK, json_object_to_json_string(obj), info);
424 static void dbus_req_fail(struct dbus_req *dreq, const char *status, const char *info)
426 dbus_req_reply(dreq, RETERR, status, info);
429 static const char *dbus_req_raw(struct dbus_req *dreq, size_t *size)
432 *size = strlen(dreq->request);
433 return dreq->request;
436 static void dbus_req_send(struct dbus_req *dreq, const char *buffer, size_t size)
438 /* TODO: how to put sized buffer as strings? things aren't clear here!!! */
439 dbus_req_reply(dreq, RETRAW, buffer, "");
442 struct afb_req_itf dbus_req_itf = {
443 .json = (void*)dbus_req_json,
444 .get = (void*)dbus_req_get,
445 .success = (void*)dbus_req_success,
446 .fail = (void*)dbus_req_fail,
447 .raw = (void*)dbus_req_raw,
448 .send = (void*)dbus_req_send,
449 .context_get = (void*)afb_context_get,
450 .context_set = (void*)afb_context_set,
451 .addref = (void*)dbus_req_addref,
452 .unref = (void*)dbus_req_unref
455 /******************* server part **********************************/
457 /* called when the object for the service is called */
458 static int api_dbus_server_on_object_called(sd_bus_message *message, void *userdata, sd_bus_error *ret_error)
463 struct dbus_req *dreq;
464 struct api_dbus *api = userdata;
468 /* check the interface */
469 if (strcmp(sd_bus_message_get_interface(message), api->name) != 0)
473 method = sd_bus_message_get_member(message);
475 /* create the request */
476 dreq = calloc(1 , sizeof *dreq);
478 sd_bus_reply_method_errorf(message, SD_BUS_ERROR_NO_MEMORY, "out of memory");
483 rc = sd_bus_message_read(message, "ssu", &dreq->request, &uuid, &flags);
485 sd_bus_reply_method_errorf(message, SD_BUS_ERROR_INVALID_SIGNATURE, "invalid signature");
490 /* connect to the context */
491 if (afb_context_connect(&dreq->context, uuid, NULL) < 0) {
492 sd_bus_reply_method_errorf(message, SD_BUS_ERROR_NO_MEMORY, "out of memory");
497 /* fulfill the request and emit it */
498 dreq->context.flags = flags;
499 dreq->message = sd_bus_message_ref(message);
502 areq.itf = &dbus_req_itf;
504 afb_apis_call_(areq, &dreq->context, api->api, method);
505 dbus_req_unref(dreq);
509 static void afb_api_dbus_server_send_event(struct api_dbus *api, const char *event, struct json_object *object)
513 rc = sd_bus_emit_signal(api->sdbus, api->path, api->name,
514 "event", "ss", event, json_object_to_json_string(object));
516 ERROR("error while emiting event %s", event);
517 json_object_put(object);
520 static int afb_api_dbus_server_expects_event(struct api_dbus *api, const char *event)
522 size_t len = strlen(api->api);
523 if (strncasecmp(event, api->api, len) != 0)
525 return event[len] == '.';
528 static struct afb_event_listener_itf evitf = {
529 .send = (void*)afb_api_dbus_server_send_event,
530 .expects = (void*)afb_api_dbus_server_expects_event
533 /* create the service */
534 int afb_api_dbus_add_server(const char *path)
537 struct api_dbus *api;
539 /* get the dbus api object connected */
540 api = make_api_dbus(path);
544 /* request the service object name */
545 rc = sd_bus_request_name(api->sdbus, api->name, 0);
548 ERROR("can't register name %s", api->name);
552 /* connect the service to the dbus object */
553 rc = sd_bus_add_object(api->sdbus, &api->slot, api->path, api_dbus_server_on_object_called, api);
556 ERROR("can't add dbus object %s for %s", api->path, api->name);
559 INFO("afb service over dbus installed, name %s, path %s", api->name, api->path);
561 ctxClientEventListenerAdd(NULL, (struct afb_event_listener){ .itf = &evitf, .closure = api });
565 sd_bus_release_name(api->sdbus, api->name);
567 destroy_api_dbus(api);