X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;ds=sidebyside;f=plugins%2Fafm-main-plugin%2Fafm-main-plugin.c;h=60db5b40bdff67f7aa0f95bf6caf370b20071a37;hb=ed54e8410771ee5f13c76f968086c6aa098a750d;hp=d86e92634363b23f7e9bf7ab1f64ce953c96c0c2;hpb=fb230eee946673ed5ebe9659d623c2a06d0a80ce;p=src%2Fapp-framework-binder.git
diff --git a/plugins/afm-main-plugin/afm-main-plugin.c b/plugins/afm-main-plugin/afm-main-plugin.c
index d86e9263..60db5b40 100644
--- a/plugins/afm-main-plugin/afm-main-plugin.c
+++ b/plugins/afm-main-plugin/afm-main-plugin.c
@@ -2,32 +2,33 @@
* Copyright (C) 2015 "IoT.bzh"
* Author "Fulup Ar Foll"
*
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
*
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
+ * http://www.apache.org/licenses/LICENSE-2.0
*
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see .
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
*/
#define _GNU_SOURCE /* See feature_test_macros(7) */
#include
#include
-#include
+#include
+#include
#include "afb-plugin.h"
-#include "afb-req-itf.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";
@@ -45,9 +46,32 @@ static const char _uninstall_[] = "uninstall";
static const char _uri_[] = "uri";
static const struct AFB_interface *interface;
+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);
+}
+
static struct json_object *embed(const char *tag, struct json_object *obj)
{
struct json_object *result;
@@ -70,51 +94,78 @@ 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, obj ? json_object_to_json_string(obj) : "NULL");
+ 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 (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_argument(request, _id_);
+ 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)
{
struct json_object *obj;
- const char *id = afb_req_argument(request, _runid_);
+ const char *id = afb_req_value(request, _runid_);
if (id == NULL) {
afb_req_fail(request, "bad-request", "missing 'runid'");
return;
@@ -127,10 +178,10 @@ static void call_runid(struct afb_req request, const char *method)
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)
@@ -151,13 +202,13 @@ static void start(struct afb_req request)
int rc;
/* get the id */
- id = afb_req_argument(request, _id_);
+ id = afb_req_value(request, _id_);
if (id == NULL) {
afb_req_fail(request, "bad-request", "missing 'id'");
return;
}
/* get the mode */
- mode = afb_req_argument(request, _mode_);
+ mode = afb_req_value(request, _mode_);
if (mode == NULL || !strcmp(mode, _auto_)) {
mode = interface->mode == AFB_MODE_REMOTE ? _remote_ : _local_;
}
@@ -172,10 +223,12 @@ static void start(struct afb_req request)
/* 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");
+ 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;
@@ -214,15 +267,15 @@ static void state(struct afb_req request)
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;
/* get the argument */
arg = afb_req_get(request, "widget");
- filename = arg.value;
- if (filename == NULL || !arg.is_file) {
+ filename = arg.path;
+ if (filename == NULL) {
afb_req_fail(request, "bad-request", "missing 'widget' file");
return;
}
@@ -235,7 +288,8 @@ 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");
+ fprintf(stderr, "(afm-main-plugin) install(%s) -> %s\n", query,
+ obj ? json_object_to_json_string(obj) : "NULL");
free(query);
/* check status */
@@ -245,6 +299,9 @@ static void install(struct afb_req request)
}
/* 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);
}
@@ -278,12 +335,29 @@ static const struct AFB_plugin plug_desc = {
const struct AFB_plugin *pluginRegister(const struct AFB_interface *itf)
{
+ int rc;
+ struct sd_bus *sbus;
+
+ /* records the interface */
+ assert (interface == NULL);
interface = itf;
+ evmgr = afb_daemon_get_evmgr(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;
+
+ /* 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_session("/org/AGL/afm/user");
- if (jbus)
- return &plug_desc;
- fprintf(stderr, "ERROR: %s:%d: can't connect to DBUS session\n", __FILE__, __LINE__);
- return NULL;
+ return &plug_desc;
}