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"
36 #include "afb-api-so.h"
37 #include "afb-context.h"
40 static const char DEFAULT_PATH_PREFIX[] = "/org/agl/afb/api/";
43 * The path given are of the form
44 * system:/org/agl/afb/api/...
46 * user:/org/agl/afb/api/...
50 struct sd_bus *sdbus; /* the bus */
51 struct sd_bus_slot *slot; /* the slot */
52 char *path; /* path of the object for the API */
53 char *name; /* name/interface of the object */
54 char *api; /* api name of the interface */
61 /******************* common part **********************************/
64 * create a structure api_dbus connected on either the system
65 * bus if 'system' is not null or on the user bus. The connection
66 * is established for either emiting/receiving on 'path' being of length
69 static struct api_dbus *make_api_dbus_3(int system, const char *path, size_t pathlen)
75 /* allocates the structure */
76 api = calloc(1, sizeof *api + 1 + pathlen + pathlen);
82 /* init the structure's strings */
84 /* path is copied after the struct */
85 api->path = (void*)(api+1);
86 strcpy(api->path, path);
88 /* api name is at the end of the path */
89 api->api = strrchr(api->path, '/');
90 if (api->api == NULL) {
95 if (!afb_apis_is_valid_api_name(api->api)) {
100 /* the name/interface is copied after the path */
101 api->name = &api->path[pathlen + 1];
102 strcpy(api->name, &path[1]);
103 ptr = strchr(api->name, '/');
106 ptr = strchr(ptr, '/');
110 sdbus = (system ? afb_common_get_system_bus : afb_common_get_user_bus)();
124 * create a structure api_dbus connected on either the system
125 * bus if 'system' is not null or on the user bus. The connection
126 * is established for either emiting/receiving on 'path'.
127 * If 'path' is not absolute, it is prefixed with DEFAULT_PATH_PREFIX.
129 static struct api_dbus *make_api_dbus_2(int system, const char *path)
134 /* check the length of the path */
141 /* if the path is absolute, creation now */
143 return make_api_dbus_3(system, path, len);
145 /* compute the path prefixed with DEFAULT_PATH_PREFIX */
146 assert(strlen(DEFAULT_PATH_PREFIX) > 0);
147 assert(DEFAULT_PATH_PREFIX[strlen(DEFAULT_PATH_PREFIX) - 1] == '/');
148 len += strlen(DEFAULT_PATH_PREFIX);
149 ptr = alloca(len + 1);
150 strcpy(stpcpy(ptr, DEFAULT_PATH_PREFIX), path);
152 /* creation for prefixed path */
153 return make_api_dbus_3(system, ptr, len);
157 * create a structure api_dbus connected either emiting/receiving
159 * The path can be prefixed with "system:" or "user:" to select
160 * either the user or the system D-Bus. If none is set then user's
162 * If remaining 'path' is not absolute, it is prefixed with
163 * DEFAULT_PATH_PREFIX.
165 static struct api_dbus *make_api_dbus(const char *path)
170 /* retrieves the prefix "scheme-like" part */
171 ptr = strchr(path, ':');
173 return make_api_dbus_2(0, path);
175 /* check the prefix part */
176 preflen = (size_t)(ptr - path);
177 if (strncmp(path, "system", preflen) == 0)
178 return make_api_dbus_2(1, ptr + 1);
180 if (strncmp(path, "user", preflen) == 0)
181 return make_api_dbus_2(0, ptr + 1);
183 /* TODO: connect to a foreign D-Bus? */
188 static void destroy_api_dbus(struct api_dbus *api)
193 /******************* client part **********************************/
196 * structure for recording query data
199 struct afb_req req; /* the request handle */
200 struct afb_context *context; /* the context of the query */
203 /* allocates and init the memorizing data */
204 static struct dbus_memo *api_dbus_client_make_memo(struct afb_req req, struct afb_context *context)
206 struct dbus_memo *memo;
208 memo = malloc(sizeof *memo);
212 memo->context = context;
217 /* free and release the memorizing data */
218 static void api_dbus_client_free_memo(struct dbus_memo *memo)
220 afb_req_unref(memo->req);
224 /* callback when received answer */
225 static int api_dbus_client_on_reply(sd_bus_message *message, void *userdata, sd_bus_error *ret_error)
228 struct dbus_memo *memo;
229 const char *first, *second;
233 /* retrieve the recorded data */
237 rc = sd_bus_message_read(message, "yssu", &type, &first, &second, &flags);
239 /* failing to have the answer */
240 afb_req_fail(memo->req, "error", "dbus error");
242 /* report the answer */
243 memo->context->flags = (unsigned)flags;
246 afb_req_success(memo->req, json_tokener_parse(first), second);
249 afb_req_fail(memo->req, first, second);
252 afb_req_send(memo->req, first, strlen(first));
255 afb_req_fail(memo->req, "error", "dbus link broken");
259 api_dbus_client_free_memo(memo);
263 /* on call, propagate it to the dbus service */
264 static void api_dbus_client_call(struct api_dbus *api, struct afb_req req, struct afb_context *context, const char *verb, size_t lenverb)
268 char *method = strndupa(verb, lenverb);
269 struct dbus_memo *memo;
271 /* create the recording data */
272 memo = api_dbus_client_make_memo(req, context);
274 afb_req_fail(req, "error", "out of memory");
279 rc = sd_bus_call_method_async(api->sdbus, NULL,
280 api->name, api->path, api->name, method,
281 api_dbus_client_on_reply, memo,
283 afb_req_raw(req, &size),
284 ctxClientGetUuid(context->session),
285 (uint32_t)context->flags);
287 /* if there was an error report it directly */
290 afb_req_fail(req, "error", "dbus error");
291 api_dbus_client_free_memo(memo);
295 /* receives events */
296 static int api_dbus_client_on_event(sd_bus_message *m, void *userdata, sd_bus_error *ret_error)
298 struct json_object *object;
299 const char *event, *data;
300 int rc = sd_bus_message_read(m, "ss", &event, &data);
302 ERROR("unreadable event");
304 object = json_tokener_parse(data);
305 ctxClientEventSend(NULL, event, object);
306 json_object_put(object);
311 /* adds a afb-dbus-service client api */
312 int afb_api_dbus_add_client(const char *path)
315 struct api_dbus *api;
316 struct afb_api afb_api;
319 /* create the dbus client api */
320 api = make_api_dbus(path);
324 /* connect to events */
325 rc = asprintf(&match, "type='signal',path='%s',interface='%s',member='event'", api->path, api->name);
328 ERROR("out of memory");
331 rc = sd_bus_add_match(api->sdbus, &api->slot, match, api_dbus_client_on_event, api);
335 ERROR("can't add dbus object %s for %s", api->path, api->name);
339 /* record it as an API */
340 afb_api.closure = api;
341 afb_api.call = (void*)api_dbus_client_call;
342 if (afb_apis_add(api->api, afb_api) < 0)
348 destroy_api_dbus(api);
353 /******************* dbus request part for server *****************/
356 * structure for a dbus request
359 struct afb_context context; /* the context, should be THE FIRST */
360 sd_bus_message *message; /* the incoming request message */
361 const char *request; /* the readen request as string */
362 struct json_object *json; /* the readen request as object */
363 int refcount; /* reference count of the request */
366 /* increment the reference count of the request */
367 static void dbus_req_addref(struct dbus_req *dreq)
372 /* decrement the reference count of the request and free/release it on falling to null */
373 static void dbus_req_unref(struct dbus_req *dreq)
375 if (dreq == NULL || --dreq->refcount)
378 afb_context_disconnect(&dreq->context);
379 json_object_put(dreq->json);
380 sd_bus_message_unref(dreq->message);
384 /* get the object of the request */
385 static struct json_object *dbus_req_json(struct dbus_req *dreq)
387 if (dreq->json == NULL) {
388 dreq->json = json_tokener_parse(dreq->request);
389 if (dreq->json == NULL) {
390 /* lazy error detection of json request. Is it to improve? */
391 dreq->json = json_object_new_string(dreq->request);
397 /* get the argument of the request of 'name' */
398 static struct afb_arg dbus_req_get(struct dbus_req *dreq, const char *name)
401 struct json_object *value, *root;
403 root = dbus_req_json(dreq);
404 if (root != NULL && json_object_object_get_ex(root, name, &value)) {
406 arg.value = json_object_get_string(value);
415 static void dbus_req_reply(struct dbus_req *dreq, uint8_t type, const char *first, const char *second)
418 rc = sd_bus_reply_method_return(dreq->message,
419 "yssu", type, first, second, (uint32_t)dreq->context.flags);
421 ERROR("sending the reply failed");
424 static void dbus_req_success(struct dbus_req *dreq, struct json_object *obj, const char *info)
426 dbus_req_reply(dreq, RETOK, json_object_to_json_string(obj), info);
429 static void dbus_req_fail(struct dbus_req *dreq, const char *status, const char *info)
431 dbus_req_reply(dreq, RETERR, status, info);
434 static const char *dbus_req_raw(struct dbus_req *dreq, size_t *size)
437 *size = strlen(dreq->request);
438 return dreq->request;
441 static void dbus_req_send(struct dbus_req *dreq, const char *buffer, size_t size)
443 /* TODO: how to put sized buffer as strings? things aren't clear here!!! */
444 dbus_req_reply(dreq, RETRAW, buffer, "");
447 struct afb_req_itf dbus_req_itf = {
448 .json = (void*)dbus_req_json,
449 .get = (void*)dbus_req_get,
450 .success = (void*)dbus_req_success,
451 .fail = (void*)dbus_req_fail,
452 .raw = (void*)dbus_req_raw,
453 .send = (void*)dbus_req_send,
454 .context_get = (void*)afb_context_get,
455 .context_set = (void*)afb_context_set,
456 .addref = (void*)dbus_req_addref,
457 .unref = (void*)dbus_req_unref
460 /******************* server part **********************************/
462 /* called when the object for the service is called */
463 static int api_dbus_server_on_object_called(sd_bus_message *message, void *userdata, sd_bus_error *ret_error)
468 struct dbus_req *dreq;
469 struct api_dbus *api = userdata;
473 /* check the interface */
474 if (strcmp(sd_bus_message_get_interface(message), api->name) != 0)
478 method = sd_bus_message_get_member(message);
480 /* create the request */
481 dreq = calloc(1 , sizeof *dreq);
483 sd_bus_reply_method_errorf(message, SD_BUS_ERROR_NO_MEMORY, "out of memory");
488 rc = sd_bus_message_read(message, "ssu", &dreq->request, &uuid, &flags);
490 sd_bus_reply_method_errorf(message, SD_BUS_ERROR_INVALID_SIGNATURE, "invalid signature");
495 /* connect to the context */
496 if (afb_context_connect(&dreq->context, uuid, NULL) < 0) {
497 sd_bus_reply_method_errorf(message, SD_BUS_ERROR_NO_MEMORY, "out of memory");
502 /* fulfill the request and emit it */
503 dreq->context.flags = flags;
504 dreq->message = sd_bus_message_ref(message);
507 areq.itf = &dbus_req_itf;
509 afb_apis_call_(areq, &dreq->context, api->api, method);
510 dbus_req_unref(dreq);
514 static void afb_api_dbus_server_send_event(struct api_dbus *api, const char *event, struct json_object *object)
518 rc = sd_bus_emit_signal(api->sdbus, api->path, api->name,
519 "event", "ss", event, json_object_to_json_string(object));
521 ERROR("error while emiting event %s", event);
522 json_object_put(object);
525 static int afb_api_dbus_server_expects_event(struct api_dbus *api, const char *event)
527 size_t len = strlen(api->api);
528 if (strncasecmp(event, api->api, len) != 0)
530 return event[len] == '.';
533 static struct afb_event_listener_itf evitf = {
534 .send = (void*)afb_api_dbus_server_send_event,
535 .expects = (void*)afb_api_dbus_server_expects_event
538 /* create the service */
539 int afb_api_dbus_add_server(const char *path)
542 struct api_dbus *api;
544 /* get the dbus api object connected */
545 api = make_api_dbus(path);
549 /* request the service object name */
550 rc = sd_bus_request_name(api->sdbus, api->name, 0);
553 ERROR("can't register name %s", api->name);
557 /* connect the service to the dbus object */
558 rc = sd_bus_add_object(api->sdbus, &api->slot, api->path, api_dbus_server_on_object_called, api);
561 ERROR("can't add dbus object %s for %s", api->path, api->name);
564 INFO("afb service over dbus installed, name %s, path %s", api->name, api->path);
566 ctxClientEventListenerAdd(NULL, (struct afb_event_listener){ .itf = &evitf, .closure = api });
570 sd_bus_release_name(api->sdbus, api->name);
572 destroy_api_dbus(api);