/*
- * 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");
#define _GNU_SOURCE /* See feature_test_macros(7) */
#include <stdio.h>
#include <string.h>
-#include <json.h>
+#include <assert.h>
+#include <json-c/json.h>
-#include "afb-plugin.h"
-#include "afb-req-itf.h"
-#include "afb-poll-itf.h"
+#include <afb/afb-plugin.h>
-#include "utils-sbus.h"
#include "utils-jbus.h"
+static const char _added_[] = "added";
static const char _auto_[] = "auto";
static const char _continue_[] = "continue";
+static const char _changed_[] = "changed";
static const char _detail_[] = "detail";
static const char _id_[] = "id";
static const char _install_[] = "install";
static const char _uninstall_[] = "uninstall";
static const char _uri_[] = "uri";
-static const struct AFB_interface *interface;
+static const struct AFB_interface *afb_interface;
+static struct afb_event_sender event_sender;
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_event_sender_push(event_sender, "application-list-changed", NULL);
+}
+
static struct json_object *embed(const char *tag, struct json_object *obj)
{
struct json_object *result;
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, obj ? json_object_to_json_string(obj) : "NULL");
+ if (afb_interface->verbosity)
+ 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);
+ }
+ }
+ 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);
}
- obj = embed(method, obj);
+}
+
+static void call_appid_callback(int status, struct json_object *obj, struct memo *memo)
+{
+ if (afb_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);
}
- afb_req_success(request, obj, NULL);
+ free(sid);
}
static void call_runid(struct afb_req request, const char *method)
return;
}
obj = jbus_call_sj_sync(jbus, method, id);
- if (interface->verbosity)
+ if (afb_interface->verbosity)
fprintf(stderr, "(afm-main-plugin) %s(%s) -> %s\n", method, id,
obj ? json_object_to_json_string(obj) : "NULL");
if (obj == NULL) {
afb_req_fail(request, "failed", "framework daemon failure");
return;
}
+ obj = json_object_get(obj);
afb_req_success(request, obj, NULL);
}
-
/************************** entries ******************************/
static void runnables(struct afb_req request)
/* get the mode */
mode = afb_req_value(request, _mode_);
if (mode == NULL || !strcmp(mode, _auto_)) {
- mode = interface->mode == AFB_MODE_REMOTE ? _remote_ : _local_;
+ mode = afb_interface->mode == AFB_MODE_REMOTE ? _remote_ : _local_;
}
/* create the query */
/* calls the service */
obj = jbus_call_sj_sync(jbus, _start_, query);
- if (interface->verbosity)
- fprintf(stderr, "(afm-main-plugin) start(%s) -> %s\n", query, obj ? json_object_to_json_string(obj) : "NULL");
+ if (afb_interface->verbosity)
+ fprintf(stderr, "(afm-main-plugin) start(%s) -> %s\n", query,
+ obj ? json_object_to_json_string(obj) : "NULL");
free(query);
/* check status */
+ obj = json_object_get(obj);
if (obj == NULL) {
afb_req_fail(request, "failed", "framework daemon failure");
return;
static void install(struct afb_req request)
{
- struct json_object *obj;
+ struct json_object *obj, *added;
char *query;
const char *filename;
struct afb_arg arg;
}
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");
+ if (afb_interface->verbosity)
+ fprintf(stderr, "(afm-main-plugin) install(%s) -> %s\n", query,
+ obj ? json_object_to_json_string(obj) : "NULL");
free(query);
/* check status */
}
/* embed if needed */
+ if (json_object_object_get_ex(obj, _added_, &added))
+ obj = added;
+ obj = json_object_get(obj);
obj = embed(_id_, obj);
afb_req_success(request, obj, NULL);
}
call_appid(request, _uninstall_);
}
-static const struct AFB_restapi plug_apis[] =
+static const struct AFB_verb_desc_v1 verbs[] =
{
{_runnables_, AFB_SESSION_CHECK, runnables, "Get list of runnable applications"},
{_detail_ , AFB_SESSION_CHECK, detail, "Get the details for one application"},
};
static const struct AFB_plugin plug_desc = {
- .type = AFB_PLUGIN_JSON,
- .info = "Application Framework Master Service",
- .prefix = "afm-main",
- .apis = plug_apis
+ .type = AFB_PLUGIN_VERSION_1,
+ .v1 = {
+ .info = "Application Framework Master Service",
+ .prefix = "afm-main",
+ .verbs = verbs
+ }
};
-static struct sbus_itf sbusitf;
-
-const struct AFB_plugin *pluginRegister(const struct AFB_interface *itf)
+const struct AFB_plugin *pluginAfbV1Register(const struct AFB_interface *itf)
{
- struct sbus *sbus;
+ int rc;
+ struct sd_bus *sbus;
- if (interface != NULL)
+ /* records the interface */
+ assert (afb_interface == NULL);
+ afb_interface = itf;
+ event_sender = afb_daemon_get_event_sender(itf->daemon);
+
+ /* 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)
return NULL;
- interface = itf;
- sbusitf.wait = itf->pollitf->wait;
- sbusitf.open = itf->pollitf->open;
- sbusitf.on_readable = itf->pollitf->on_readable;
- sbusitf.on_writable = itf->pollitf->on_writable;
- sbusitf.on_hangup = itf->pollitf->on_hangup;
- sbusitf.close = itf->pollitf->close;
-
- sbus = sbus_session(&sbusitf, itf->pollclosure);
- if (sbus == NULL) {
- fprintf(stderr, "ERROR: %s:%d: can't connect to DBUS session\n", __FILE__, __LINE__);
+ /* records the signal handler */
+ rc = jbus_on_signal_s(jbus, _changed_, application_list_changed, NULL);
+ if (rc < 0) {
+ jbus_unref(jbus);
return NULL;
}
- jbus = create_jbus(sbus, "/org/AGL/afm/user");
- if (jbus)
- return &plug_desc;
- sbus_unref(sbus);
- return NULL;
+ return &plug_desc;
}