Service instanciation
[src/app-framework-binder.git] / src / afb-api-dbus.c
index 818f8d1..3d9da07 100644 (file)
@@ -1,5 +1,5 @@
 /* 
- * Copyright (C) 2015 "IoT.bzh"
+ * Copyright (C) 2015, 2016 "IoT.bzh"
  * Author José Bollo <jose.bollo@iot.bzh>
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
@@ -16,6 +16,7 @@
  */
 
 #define _GNU_SOURCE
+#define NO_PLUGIN_VERBOSE_MACRO
 
 #include <stdlib.h>
 #include <string.h>
 #include <errno.h>
 
 #include <systemd/sd-bus.h>
-#include <json.h>
+#include <json-c/json.h>
 
-#include "afb-plugin.h"
-#include "afb-req-itf.h"
+#include <afb/afb-plugin.h>
+#include <afb/afb-req-itf.h>
 
 #include "afb-common.h"
 
 #include "session.h"
+#include "afb-msg-json.h"
 #include "afb-apis.h"
 #include "afb-api-so.h"
 #include "afb-context.h"
+#include "afb-evt.h"
+#include "afb-subcall.h"
 #include "verbose.h"
 
 static const char DEFAULT_PATH_PREFIX[] = "/org/agl/afb/api/";
@@ -47,9 +51,11 @@ static const char DEFAULT_PATH_PREFIX[] = "/org/agl/afb/api/";
 struct api_dbus
 {
        struct sd_bus *sdbus;   /* the bus */
+       struct sd_bus_slot *slot; /* the slot */
        char *path;             /* path of the object for the API */
        char *name;             /* name/interface of the object */
        char *api;              /* api name of the interface */
+       struct afb_evt_listener *listener;
 };
 
 #define RETOK   1
@@ -90,6 +96,10 @@ static struct api_dbus *make_api_dbus_3(int system, const char *path, size_t pat
                goto error2;
        }
        api->api++;
+       if (!afb_apis_is_valid_api_name(api->api)) {
+               errno = EINVAL;
+               goto error2;
+       }
 
        /* the name/interface is copied after the path */
        api->name = &api->path[pathlen + 1];
@@ -286,20 +296,65 @@ static void api_dbus_client_call(struct api_dbus *api, struct afb_req req, struc
        }
 }
 
+static int api_dbus_service_start(struct api_dbus *api, int share_session, int onneed)
+{
+       /* not an error when onneed */
+       if (onneed != 0)
+               return 0;
+
+       /* already started: it is an error */
+       ERROR("The Dbus binding %s is not a startable service", api->name);
+       return -1;
+}
+
+/* receives events */
+static int api_dbus_client_on_event(sd_bus_message *m, void *userdata, sd_bus_error *ret_error)
+{
+       struct json_object *object;
+       const char *event, *data;
+       int rc = sd_bus_message_read(m, "ss", &event, &data);
+       if (rc < 0)
+               ERROR("unreadable event");
+       else {
+               object = json_tokener_parse(data);
+               afb_evt_broadcast(event, object);
+               json_object_put(object);
+       }
+       return 1;
+}
+
 /* adds a afb-dbus-service client api */
 int afb_api_dbus_add_client(const char *path)
 {
+       int rc;
        struct api_dbus *api;
        struct afb_api afb_api;
+       char *match;
 
        /* create the dbus client api */
        api = make_api_dbus(path);
        if (api == NULL)
                goto error;
 
+       /* connect to events */
+       rc = asprintf(&match, "type='signal',path='%s',interface='%s',member='event'", api->path, api->name);
+       if (rc < 0) {
+               errno = ENOMEM;
+               ERROR("out of memory");
+               goto error;
+       }
+       rc = sd_bus_add_match(api->sdbus, &api->slot, match, api_dbus_client_on_event, api);
+       free(match);
+       if (rc < 0) {
+               errno = -rc;
+               ERROR("can't add dbus object %s for %s", api->path, api->name);
+               goto error;
+       }
+
        /* record it as an API */
        afb_api.closure = api;
        afb_api.call = (void*)api_dbus_client_call;
+       afb_api.service_start = (void*)api_dbus_service_start;
        if (afb_apis_add(api->api, afb_api) < 0)
                goto error2;
 
@@ -358,19 +413,7 @@ static struct json_object *dbus_req_json(struct dbus_req *dreq)
 /* get the argument of the request of 'name' */
 static struct afb_arg dbus_req_get(struct dbus_req *dreq, const char *name)
 {
-       struct afb_arg arg;
-       struct json_object *value, *root;
-
-       root = dbus_req_json(dreq);
-       if (root != NULL && json_object_object_get_ex(root, name, &value)) {
-               arg.name = name;
-               arg.value = json_object_get_string(value);
-       } else {
-               arg.name = NULL;
-               arg.value = NULL;
-       }
-       arg.path = NULL;
-       return arg;
+       return afb_msg_json_get_arg(dbus_req_json(dreq), name);
 }
 
 static void dbus_req_reply(struct dbus_req *dreq, uint8_t type, const char *first, const char *second)
@@ -378,11 +421,13 @@ static void dbus_req_reply(struct dbus_req *dreq, uint8_t type, const char *firs
        int rc;
        rc = sd_bus_reply_method_return(dreq->message,
                        "yssu", type, first, second, (uint32_t)dreq->context.flags);
+       if (rc < 0)
+               ERROR("sending the reply failed");
 }
 
 static void dbus_req_success(struct dbus_req *dreq, struct json_object *obj, const char *info)
 {
-       dbus_req_reply(dreq, RETOK, json_object_to_json_string(obj), info);
+       dbus_req_reply(dreq, RETOK, json_object_to_json_string_ext(obj, JSON_C_TO_STRING_PLAIN), info);
 }
 
 static void dbus_req_fail(struct dbus_req *dreq, const char *status, const char *info)
@@ -403,7 +448,19 @@ static void dbus_req_send(struct dbus_req *dreq, const char *buffer, size_t size
        dbus_req_reply(dreq, RETRAW, buffer, "");
 }
 
-struct afb_req_itf dbus_req_itf = {
+static int dbus_req_subscribe(struct dbus_req *dreq, struct afb_event event)
+{
+       return -1;
+}
+
+static int dbus_req_unsubscribe(struct dbus_req *dreq, struct afb_event event)
+{
+       return -1;
+}
+
+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);
+
+const struct afb_req_itf afb_api_dbus_req_itf = {
        .json = (void*)dbus_req_json,
        .get = (void*)dbus_req_get,
        .success = (void*)dbus_req_success,
@@ -413,9 +470,19 @@ struct afb_req_itf dbus_req_itf = {
        .context_get = (void*)afb_context_get,
        .context_set = (void*)afb_context_set,
        .addref = (void*)dbus_req_addref,
-       .unref = (void*)dbus_req_unref
+       .unref = (void*)dbus_req_unref,
+       .session_close = (void*)afb_context_close,
+       .session_set_LOA = (void*)afb_context_change_loa,
+       .subscribe = (void*)dbus_req_subscribe,
+       .unsubscribe = (void*)dbus_req_unsubscribe,
+       .subcall = (void*)dbus_req_subcall
 };
 
+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)
+{
+       afb_subcall(&dreq->context, api, verb, args, callback, closure, (struct afb_req){ .itf = &afb_api_dbus_req_itf, .closure = dreq });
+}
+
 /******************* server part **********************************/
 
 /* called when the object for the service is called */
@@ -463,19 +530,29 @@ static int api_dbus_server_on_object_called(sd_bus_message *message, void *userd
        dreq->message = sd_bus_message_ref(message);
        dreq->json = NULL;
        dreq->refcount = 1;
-       areq.itf = &dbus_req_itf;
+       areq.itf = &afb_api_dbus_req_itf;
        areq.closure = dreq;
        afb_apis_call_(areq, &dreq->context, api->api, method);
        dbus_req_unref(dreq);
        return 1;
 }
 
+static void afb_api_dbus_server_send_event(struct api_dbus *api, const char *event, struct json_object *object)
+{
+       int rc;
+
+       rc = sd_bus_emit_signal(api->sdbus, api->path, api->name,
+                       "event", "ss", event, json_object_to_json_string_ext(object, JSON_C_TO_STRING_PLAIN));
+       if (rc < 0)
+               ERROR("error while emiting event %s", event);
+       json_object_put(object);
+}
+
 /* create the service */
 int afb_api_dbus_add_server(const char *path)
 {
        int rc;
        struct api_dbus *api;
-       sd_bus_slot *slot;
 
        /* get the dbus api object connected */
        api = make_api_dbus(path);
@@ -491,7 +568,7 @@ int afb_api_dbus_add_server(const char *path)
        }
 
        /* connect the service to the dbus object */
-       rc = sd_bus_add_object(api->sdbus, &slot, api->path, api_dbus_server_on_object_called, api);
+       rc = sd_bus_add_object(api->sdbus, &api->slot, api->path, api_dbus_server_on_object_called, api);
        if (rc < 0) {
                errno = -rc;
                ERROR("can't add dbus object %s for %s", api->path, api->name);
@@ -499,6 +576,8 @@ int afb_api_dbus_add_server(const char *path)
        }
        INFO("afb service over dbus installed, name %s, path %s", api->name, api->path);
 
+       api->listener = afb_evt_listener_create((void*)afb_api_dbus_server_send_event, api);
+
        return 0;
 error3:
        sd_bus_release_name(api->sdbus, api->name);