provides developper files
[src/app-framework-binder.git] / plugins / afm-main-plugin / afm-main-plugin.c
index 553cffc..705efcb 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2015 "IoT.bzh"
+ * Copyright (C) 2015, 2016 "IoT.bzh"
  * Author "Fulup Ar Foll"
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
 #include <stdio.h>
 #include <string.h>
 #include <assert.h>
-#include <json.h>
+#include <json-c/json.h>
 
-#include "afb-plugin.h"
+#include <afb/afb-plugin.h>
 
-#include "utils-sbus.h"
 #include "utils-jbus.h"
 
 static const char _added_[]     = "added";
@@ -51,6 +50,23 @@ static struct afb_evmgr evmgr;
 
 static struct jbus *jbus;
 
+struct memo
+{
+       struct afb_req request;
+       const char *method;
+};
+
+static struct memo *make_memo(struct afb_req request, const char *method)
+{
+       struct memo *memo = malloc(sizeof *memo);
+       if (memo != NULL) {
+               memo->request = request;
+               memo->method = method;
+               afb_req_addref(request);
+       }
+       return memo;
+}
+
 static void application_list_changed(const char *data, void *closure)
 {
        afb_evmgr_push(evmgr, "application-list-changed", NULL);
@@ -78,49 +94,72 @@ static struct json_object *embed(const char *tag, struct json_object *obj)
        return result;
 }
 
-static void embed_call_void(struct afb_req request, const char *method)
+static void embed_call_void_callback(int status, struct json_object *obj, struct memo *memo)
 {
-       struct json_object *obj = jbus_call_sj_sync(jbus, method, "true");
        if (interface->verbosity)
-               fprintf(stderr, "(afm-main-plugin) %s(true) -> %s\n", method,
+               fprintf(stderr, "(afm-main-plugin) %s(true) -> %s\n", memo->method,
                        obj ? json_object_to_json_string(obj) : "NULL");
        if (obj == NULL) {
-               afb_req_fail(request, "failed", "framework daemon failure");
-               return;
+               afb_req_fail(memo->request, "failed", "framework daemon failure");
+       } else {
+               obj = json_object_get(obj);
+               obj = embed(memo->method, obj);
+               if (obj == NULL) {
+                       afb_req_fail(memo->request, "failed", "framework daemon failure");
+               } else {
+                       afb_req_success(memo->request, obj, NULL);
+               }
        }
-       obj = json_object_get(obj);
-       obj = embed(method, obj);
+       afb_req_unref(memo->request);
+       free(memo);
+}
+
+static void embed_call_void(struct afb_req request, const char *method)
+{
+       struct memo *memo = make_memo(request, method);
+       if (memo == NULL)
+               afb_req_fail(request, "failed", "out of memory");
+       else if (jbus_call_sj(jbus, method, "true", (void*)embed_call_void_callback, memo) < 0) {
+               afb_req_fail(request, "failed", "dbus failure");
+               free(memo);
+       }
+}
+
+static void call_appid_callback(int status, struct json_object *obj, struct memo *memo)
+{
+       if (interface->verbosity)
+               fprintf(stderr, "(afm-main-plugin) %s -> %s\n", memo->method, 
+                       obj ? json_object_to_json_string(obj) : "NULL");
        if (obj == NULL) {
-               afb_req_fail(request, "failed", "framework daemon failure");
-               return;
+               afb_req_fail(memo->request, "failed", "framework daemon failure");
+       } else {
+               obj = json_object_get(obj);
+               afb_req_success(memo->request, obj, NULL);
        }
-       afb_req_success(request, obj, NULL);
+       afb_req_unref(memo->request);
+       free(memo);
 }
 
 static void call_appid(struct afb_req request, const char *method)
 {
-       struct json_object *obj;
+       struct memo *memo;
        char *sid;
        const char *id = afb_req_value(request, _id_);
        if (id == NULL) {
                afb_req_fail(request, "bad-request", "missing 'id'");
                return;
        }
-       if (asprintf(&sid, "\"%s\"", id) <= 0) {
+       memo = make_memo(request, method);
+       if (asprintf(&sid, "\"%s\"", id) <= 0 || memo == NULL) {
                afb_req_fail(request, "server-error", "out of memory");
+               free(memo);
                return;
        }
-       obj = jbus_call_sj_sync(jbus, method, sid);
-       if (interface->verbosity)
-               fprintf(stderr, "(afm-main-plugin) %s(%s) -> %s\n", method, sid,
-                       obj ? json_object_to_json_string(obj) : "NULL");
-       free(sid);
-       if (obj == NULL) {
-               afb_req_fail(request, "failed", "framework daemon failure");
-               return;
+       if (jbus_call_sj(jbus, method, sid, (void*)call_appid_callback, memo) < 0) {
+               afb_req_fail(request, "failed", "dbus failure");
+               free(memo);
        }
-       obj = json_object_get(obj);
-       afb_req_success(request, obj, NULL);
+       free(sid);
 }
 
 static void call_runid(struct afb_req request, const char *method)
@@ -143,7 +182,6 @@ static void call_runid(struct afb_req request, const char *method)
        afb_req_success(request, obj, NULL);
 }
 
-
 /************************** entries ******************************/
 
 static void runnables(struct afb_req request)
@@ -250,7 +288,6 @@ static void install(struct afb_req request)
 
        obj = jbus_call_sj_sync(jbus, _install_, query);
        if (interface->verbosity)
-;
                fprintf(stderr, "(afm-main-plugin) install(%s) -> %s\n", query,
                        obj ? json_object_to_json_string(obj) : "NULL");
        free(query);
@@ -296,39 +333,23 @@ static const struct AFB_plugin plug_desc = {
        .apis = plug_apis
 };
 
-static struct sbus_itf sbusitf;
-
 const struct AFB_plugin *pluginRegister(const struct AFB_interface *itf)
 {
        int rc;
-       struct afb_pollmgr pollmgr;
-       struct sbus *sbus;
+       struct sd_bus *sbus;
 
        /* records the interface */
        assert (interface == NULL);
        interface = itf;
        evmgr = afb_daemon_get_evmgr(itf->daemon);
 
-       /* creates the sbus for session */
-       pollmgr = afb_daemon_get_pollmgr(itf->daemon);
-       sbusitf.wait = pollmgr.itf->wait;
-       sbusitf.open = pollmgr.itf->open;
-       sbusitf.on_readable = pollmgr.itf->on_readable;
-       sbusitf.on_writable = pollmgr.itf->on_writable;
-       sbusitf.on_hangup = pollmgr.itf->on_hangup;
-       sbusitf.close = pollmgr.itf->close;
-       sbus = sbus_session(&sbusitf, pollmgr.closure);
-       if (sbus == NULL) {
-               fprintf(stderr, "ERROR: %s:%d: can't connect to DBUS session\n", __FILE__, __LINE__);
-               return NULL;
-       }
-
        /* creates the jbus for accessing afm-user-daemon */
+       sbus = afb_daemon_get_user_bus(itf->daemon);
+       if (sbus == NULL)
+               return NULL;
        jbus = create_jbus(sbus, "/org/AGL/afm/user");
-        if (jbus == NULL) {
-               sbus_unref(sbus);
+        if (jbus == NULL)
                return NULL;
-       }
 
        /* records the signal handler */
        rc = jbus_on_signal_s(jbus, _changed_, application_list_changed, NULL);