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/";
46 * The path given are of the form
47 * system:/org/agl/afb/api/...
49 * user:/org/agl/afb/api/...
53 struct sd_bus *sdbus; /* the bus */
54 struct sd_bus_slot *slot; /* the slot */
55 char *path; /* path of the object for the API */
56 char *name; /* name/interface of the object */
57 char *api; /* api name of the interface */
58 struct afb_evt_listener *listener;
65 /******************* common part **********************************/
68 * create a structure api_dbus connected on either the system
69 * bus if 'system' is not null or on the user bus. The connection
70 * is established for either emiting/receiving on 'path' being of length
73 static struct api_dbus *make_api_dbus_3(int system, const char *path, size_t pathlen)
79 /* allocates the structure */
80 api = calloc(1, sizeof *api + 1 + pathlen + pathlen);
86 /* init the structure's strings */
88 /* path is copied after the struct */
89 api->path = (void*)(api+1);
90 strcpy(api->path, path);
92 /* api name is at the end of the path */
93 api->api = strrchr(api->path, '/');
94 if (api->api == NULL) {
99 if (!afb_apis_is_valid_api_name(api->api)) {
104 /* the name/interface is copied after the path */
105 api->name = &api->path[pathlen + 1];
106 strcpy(api->name, &path[1]);
107 ptr = strchr(api->name, '/');
110 ptr = strchr(ptr, '/');
114 sdbus = (system ? afb_common_get_system_bus : afb_common_get_user_bus)();
128 * create a structure api_dbus connected on either the system
129 * bus if 'system' is not null or on the user bus. The connection
130 * is established for either emiting/receiving on 'path'.
131 * If 'path' is not absolute, it is prefixed with DEFAULT_PATH_PREFIX.
133 static struct api_dbus *make_api_dbus_2(int system, const char *path)
138 /* check the length of the path */
145 /* if the path is absolute, creation now */
147 return make_api_dbus_3(system, path, len);
149 /* compute the path prefixed with DEFAULT_PATH_PREFIX */
150 assert(strlen(DEFAULT_PATH_PREFIX) > 0);
151 assert(DEFAULT_PATH_PREFIX[strlen(DEFAULT_PATH_PREFIX) - 1] == '/');
152 len += strlen(DEFAULT_PATH_PREFIX);
153 ptr = alloca(len + 1);
154 strcpy(stpcpy(ptr, DEFAULT_PATH_PREFIX), path);
156 /* creation for prefixed path */
157 return make_api_dbus_3(system, ptr, len);
161 * create a structure api_dbus connected either emiting/receiving
163 * The path can be prefixed with "system:" or "user:" to select
164 * either the user or the system D-Bus. If none is set then user's
166 * If remaining 'path' is not absolute, it is prefixed with
167 * DEFAULT_PATH_PREFIX.
169 static struct api_dbus *make_api_dbus(const char *path)
174 /* retrieves the prefix "scheme-like" part */
175 ptr = strchr(path, ':');
177 return make_api_dbus_2(0, path);
179 /* check the prefix part */
180 preflen = (size_t)(ptr - path);
181 if (strncmp(path, "system", preflen) == 0)
182 return make_api_dbus_2(1, ptr + 1);
184 if (strncmp(path, "user", preflen) == 0)
185 return make_api_dbus_2(0, ptr + 1);
187 /* TODO: connect to a foreign D-Bus? */
192 static void destroy_api_dbus(struct api_dbus *api)
197 /******************* client part **********************************/
200 * structure for recording query data
203 struct afb_req req; /* the request handle */
204 struct afb_context *context; /* the context of the query */
207 /* allocates and init the memorizing data */
208 static struct dbus_memo *api_dbus_client_make_memo(struct afb_req req, struct afb_context *context)
210 struct dbus_memo *memo;
212 memo = malloc(sizeof *memo);
216 memo->context = context;
221 /* free and release the memorizing data */
222 static void api_dbus_client_free_memo(struct dbus_memo *memo)
224 afb_req_unref(memo->req);
228 /* callback when received answer */
229 static int api_dbus_client_on_reply(sd_bus_message *message, void *userdata, sd_bus_error *ret_error)
232 struct dbus_memo *memo;
233 const char *first, *second;
237 /* retrieve the recorded data */
241 rc = sd_bus_message_read(message, "yssu", &type, &first, &second, &flags);
243 /* failing to have the answer */
244 afb_req_fail(memo->req, "error", "dbus error");
246 /* report the answer */
247 memo->context->flags = (unsigned)flags;
250 afb_req_success(memo->req, json_tokener_parse(first), second);
253 afb_req_fail(memo->req, first, second);
256 afb_req_send(memo->req, first, strlen(first));
259 afb_req_fail(memo->req, "error", "dbus link broken");
263 api_dbus_client_free_memo(memo);
267 /* on call, propagate it to the dbus service */
268 static void api_dbus_client_call(struct api_dbus *api, struct afb_req req, struct afb_context *context, const char *verb, size_t lenverb)
272 char *method = strndupa(verb, lenverb);
273 struct dbus_memo *memo;
275 /* create the recording data */
276 memo = api_dbus_client_make_memo(req, context);
278 afb_req_fail(req, "error", "out of memory");
283 rc = sd_bus_call_method_async(api->sdbus, NULL,
284 api->name, api->path, api->name, method,
285 api_dbus_client_on_reply, memo,
287 afb_req_raw(req, &size),
288 ctxClientGetUuid(context->session),
289 (uint32_t)context->flags);
291 /* if there was an error report it directly */
294 afb_req_fail(req, "error", "dbus error");
295 api_dbus_client_free_memo(memo);
299 /* receives events */
300 static int api_dbus_client_on_event(sd_bus_message *m, void *userdata, sd_bus_error *ret_error)
302 struct json_object *object;
303 const char *event, *data;
304 int rc = sd_bus_message_read(m, "ss", &event, &data);
306 ERROR("unreadable event");
308 object = json_tokener_parse(data);
309 afb_evt_broadcast(event, object);
310 json_object_put(object);
315 /* adds a afb-dbus-service client api */
316 int afb_api_dbus_add_client(const char *path)
319 struct api_dbus *api;
320 struct afb_api afb_api;
323 /* create the dbus client api */
324 api = make_api_dbus(path);
328 /* connect to events */
329 rc = asprintf(&match, "type='signal',path='%s',interface='%s',member='event'", api->path, api->name);
332 ERROR("out of memory");
335 rc = sd_bus_add_match(api->sdbus, &api->slot, match, api_dbus_client_on_event, api);
339 ERROR("can't add dbus object %s for %s", api->path, api->name);
343 /* record it as an API */
344 afb_api.closure = api;
345 afb_api.call = (void*)api_dbus_client_call;
346 if (afb_apis_add(api->api, afb_api) < 0)
352 destroy_api_dbus(api);
357 /******************* dbus request part for server *****************/
360 * structure for a dbus request
363 struct afb_context context; /* the context, should be THE FIRST */
364 sd_bus_message *message; /* the incoming request message */
365 const char *request; /* the readen request as string */
366 struct json_object *json; /* the readen request as object */
367 int refcount; /* reference count of the request */
370 /* increment the reference count of the request */
371 static void dbus_req_addref(struct dbus_req *dreq)
376 /* decrement the reference count of the request and free/release it on falling to null */
377 static void dbus_req_unref(struct dbus_req *dreq)
379 if (dreq == NULL || --dreq->refcount)
382 afb_context_disconnect(&dreq->context);
383 json_object_put(dreq->json);
384 sd_bus_message_unref(dreq->message);
388 /* get the object of the request */
389 static struct json_object *dbus_req_json(struct dbus_req *dreq)
391 if (dreq->json == NULL) {
392 dreq->json = json_tokener_parse(dreq->request);
393 if (dreq->json == NULL) {
394 /* lazy error detection of json request. Is it to improve? */
395 dreq->json = json_object_new_string(dreq->request);
401 /* get the argument of the request of 'name' */
402 static struct afb_arg dbus_req_get(struct dbus_req *dreq, const char *name)
404 return afb_msg_json_get_arg(dbus_req_json(dreq), name);
407 static void dbus_req_reply(struct dbus_req *dreq, uint8_t type, const char *first, const char *second)
410 rc = sd_bus_reply_method_return(dreq->message,
411 "yssu", type, first, second, (uint32_t)dreq->context.flags);
413 ERROR("sending the reply failed");
416 static void dbus_req_success(struct dbus_req *dreq, struct json_object *obj, const char *info)
418 dbus_req_reply(dreq, RETOK, json_object_to_json_string_ext(obj, JSON_C_TO_STRING_PLAIN), info);
421 static void dbus_req_fail(struct dbus_req *dreq, const char *status, const char *info)
423 dbus_req_reply(dreq, RETERR, status, info);
426 static const char *dbus_req_raw(struct dbus_req *dreq, size_t *size)
429 *size = strlen(dreq->request);
430 return dreq->request;
433 static void dbus_req_send(struct dbus_req *dreq, const char *buffer, size_t size)
435 /* TODO: how to put sized buffer as strings? things aren't clear here!!! */
436 dbus_req_reply(dreq, RETRAW, buffer, "");
439 static int dbus_req_subscribe(struct dbus_req *dreq, struct afb_event event)
444 static int dbus_req_unsubscribe(struct dbus_req *dreq, struct afb_event event)
449 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);
451 const struct afb_req_itf afb_api_dbus_req_itf = {
452 .json = (void*)dbus_req_json,
453 .get = (void*)dbus_req_get,
454 .success = (void*)dbus_req_success,
455 .fail = (void*)dbus_req_fail,
456 .raw = (void*)dbus_req_raw,
457 .send = (void*)dbus_req_send,
458 .context_get = (void*)afb_context_get,
459 .context_set = (void*)afb_context_set,
460 .addref = (void*)dbus_req_addref,
461 .unref = (void*)dbus_req_unref,
462 .session_close = (void*)afb_context_close,
463 .session_set_LOA = (void*)afb_context_change_loa,
464 .subscribe = (void*)dbus_req_subscribe,
465 .unsubscribe = (void*)dbus_req_unsubscribe,
466 .subcall = (void*)dbus_req_subcall
469 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)
471 afb_subcall(&dreq->context, api, verb, args, callback, closure, (struct afb_req){ .itf = &afb_api_dbus_req_itf, .closure = dreq });
474 /******************* server part **********************************/
476 /* called when the object for the service is called */
477 static int api_dbus_server_on_object_called(sd_bus_message *message, void *userdata, sd_bus_error *ret_error)
482 struct dbus_req *dreq;
483 struct api_dbus *api = userdata;
487 /* check the interface */
488 if (strcmp(sd_bus_message_get_interface(message), api->name) != 0)
492 method = sd_bus_message_get_member(message);
494 /* create the request */
495 dreq = calloc(1 , sizeof *dreq);
497 sd_bus_reply_method_errorf(message, SD_BUS_ERROR_NO_MEMORY, "out of memory");
502 rc = sd_bus_message_read(message, "ssu", &dreq->request, &uuid, &flags);
504 sd_bus_reply_method_errorf(message, SD_BUS_ERROR_INVALID_SIGNATURE, "invalid signature");
509 /* connect to the context */
510 if (afb_context_connect(&dreq->context, uuid, NULL) < 0) {
511 sd_bus_reply_method_errorf(message, SD_BUS_ERROR_NO_MEMORY, "out of memory");
516 /* fulfill the request and emit it */
517 dreq->context.flags = flags;
518 dreq->message = sd_bus_message_ref(message);
521 areq.itf = &afb_api_dbus_req_itf;
523 afb_apis_call_(areq, &dreq->context, api->api, method);
524 dbus_req_unref(dreq);
528 static void afb_api_dbus_server_send_event(struct api_dbus *api, const char *event, struct json_object *object)
532 rc = sd_bus_emit_signal(api->sdbus, api->path, api->name,
533 "event", "ss", event, json_object_to_json_string_ext(object, JSON_C_TO_STRING_PLAIN));
535 ERROR("error while emiting event %s", event);
536 json_object_put(object);
539 /* create the service */
540 int afb_api_dbus_add_server(const char *path)
543 struct api_dbus *api;
545 /* get the dbus api object connected */
546 api = make_api_dbus(path);
550 /* request the service object name */
551 rc = sd_bus_request_name(api->sdbus, api->name, 0);
554 ERROR("can't register name %s", api->name);
558 /* connect the service to the dbus object */
559 rc = sd_bus_add_object(api->sdbus, &api->slot, api->path, api_dbus_server_on_object_called, api);
562 ERROR("can't add dbus object %s for %s", api->path, api->name);
565 INFO("afb service over dbus installed, name %s, path %s", api->name, api->path);
567 api->listener = afb_evt_listener_create((void*)afb_api_dbus_server_send_event, api);
571 sd_bus_release_name(api->sdbus, api->name);
573 destroy_api_dbus(api);