evt: handles broadcasting and tracking
[src/app-framework-binder.git] / src / afb-api-dbus.c
index eda7985..054755a 100644 (file)
 #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/";
@@ -294,6 +296,17 @@ 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)
 {
@@ -341,6 +354,7 @@ int afb_api_dbus_add_client(const char *path)
        /* 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;
 
@@ -399,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)
@@ -446,6 +448,18 @@ static void dbus_req_send(struct dbus_req *dreq, const char *buffer, size_t size
        dbus_req_reply(dreq, RETRAW, buffer, "");
 }
 
+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,
@@ -456,9 +470,19 @@ const struct afb_req_itf afb_api_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 */
@@ -513,7 +537,7 @@ static int api_dbus_server_on_object_called(sd_bus_message *message, void *userd
        return 1;
 }
 
-static void afb_api_dbus_server_send_event(struct api_dbus *api, const char *event, struct json_object *object)
+static void afb_api_dbus_server_send_event(struct api_dbus *api, const char *event, int eventid, struct json_object *object)
 {
        int rc;
 
@@ -524,6 +548,12 @@ static void afb_api_dbus_server_send_event(struct api_dbus *api, const char *eve
        json_object_put(object);
 }
 
+/* the interface for events */
+static const struct afb_evt_itf evt_itf = {
+       .broadcast = (void*)afb_api_dbus_server_send_event,
+       .push = (void*)afb_api_dbus_server_send_event
+};
+
 /* create the service */
 int afb_api_dbus_add_server(const char *path)
 {
@@ -552,7 +582,7 @@ 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);
+       api->listener = afb_evt_listener_create(&evt_itf, api);
 
        return 0;
 error3: