/*
- * Copyright (C) 2015, 2016, 2017 IoT.bzh
- * Author "Fulup Ar Foll"
+ * Copyright (C) 2015-2018 "IoT.bzh"
+ * Author José Bollo <jose.bollo@iot.bzh>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
#include "verbose.h"
#include "utils-systemd.h"
-#include "afm.h"
#include "afm-udb.h"
#include "afm-urun.h"
#include "wgt-info.h"
#include "wgtpkg-uninstall.h"
#include "wrap-json.h"
+/*
+ * constant strings
+ */
static const char _added_[] = "added";
static const char _a_l_c_[] = "application-list-changed";
+static const char _bad_request_[] = "bad-request";
+static const char _cannot_start_[] = "cannot-start";
static const char _detail_[] = "detail";
static const char _id_[] = "id";
static const char _install_[] = "install";
+static const char _lang_[] = "lang";
static const char _not_found_[] = "not-found";
static const char _once_[] = "once";
static const char _pause_[] = "pause";
static const char _terminate_[] = "terminate";
static const char _uninstall_[] = "uninstall";
+/*
+ * the permissions
+ */
+static const struct afb_auth
+ auth_install = {
+ .type = afb_auth_Permission,
+ .text = "urn:AGL:permission:afm:system:widget:install"
+ },
+ auth_uninstall = {
+ .type = afb_auth_Permission,
+ .text = "urn:AGL:permission:afm:system:widget:uninstall"
+ },
+ auth_preinstall = {
+ .type = afb_auth_Permission,
+ .text = "urn:AGL:permission:afm:system:widget:preinstall"
+ },
+ auth_detail = {
+ .type = afb_auth_Permission,
+ .text = "urn:AGL:permission:afm:system:widget:detail"
+ },
+ auth_start = {
+ .type = afb_auth_Permission,
+ .text = "urn:AGL:permission:afm:system:widget:start"
+ },
+ auth_view_all = {
+ .type = afb_auth_Permission,
+ .text = "urn:AGL:permission:afm:system:widget:view-all"
+ },
+ auth_state = {
+ .type = afb_auth_Permission,
+ .text = "urn:AGL:permission:afm:system:runner:state"
+ },
+ auth_kill = {
+ .type = afb_auth_Permission,
+ .text = "urn:AGL:permission:afm:system:runner:kill"
+ }
+;
+
+/*
+ * default root
+ */
static const char *rootdir = FWK_APP_DIR;
-static struct afb_event applist_changed_event;
+
+/*
+ * the internal application database
+ */
static struct afm_udb *afudb;
+
+/*
+ * the event signalling that application list changed
+ */
+static struct afb_event applist_changed_event;
+
+/*
+ * the preallocated true json_object
+ */
static struct json_object *json_true;
+/* enforce daemon reload */
static void do_reloads()
{
- /* enforce daemon reload */
systemd_daemon_reload(0);
systemd_unit_restart_name(0, "sockets.target");
}
+/* common bad request reply */
static void bad_request(struct afb_req req)
{
- afb_req_fail(req, "bad-request", NULL);
+ afb_req_fail(req, _bad_request_, NULL);
}
+/* common not found reply */
static void not_found(struct afb_req req)
{
afb_req_fail(req, _not_found_, NULL);
}
+/* common can't start reply */
static void cant_start(struct afb_req req)
{
- afb_req_fail(req, "cannot-start", NULL);
+ afb_req_fail(req, _cannot_start_, NULL);
}
/*
afb_event_broadcast(applist_changed_event, e);
}
+/*
+ * Retrieve the required language from 'req'.
+ */
+static const char *get_lang(struct afb_req req)
+{
+ const char *lang;
+
+ /* get the optional language */
+ lang = afb_req_value(req, _lang_);
+
+ /* TODO use the req to get the lang of the session (if any) */
+
+ return lang;
+}
+
+
+/*
+ * retrieves the 'appid' in parameters received with the
+ * request 'req' for the 'method'.
+ *
+ * Returns 1 in case of success.
+ * Otherwise, if the 'appid' can't be retrieved, an error stating
+ * the bad request is replied for 'req' and 0 is returned.
+ */
+static int onappid(struct afb_req req, const char *method, const char **appid)
+{
+ struct json_object *json;
+
+ /* get the paramaters of the request */
+ json = afb_req_json(req);
+
+ /* get the appid if any */
+ if (!wrap_json_unpack(json, "s", appid)
+ || !wrap_json_unpack(json, "{si}", _id_, appid)) {
+ /* found */
+ INFO("method %s called for %s", method, *appid);
+ return 1;
+ }
+
+ /* nothing appropriate */
+ INFO("bad request method %s: %s", method,
+ json_object_to_json_string(json));
+ bad_request(req);
+ return 0;
+}
+
/*
* retrieves the 'runid' in parameters received with the
* request 'req' for the 'method'.
*
* Returns 1 in case of success.
- * Otherwise, if the 'runid' can't be retrived, an error stating
+ * Otherwise, if the 'runid' can't be retrieved, an error stating
* the bad request is replied for 'req' and 0 is returned.
*/
static int onrunid(struct afb_req req, const char *method, int *runid)
{
struct json_object *json;
+ const char *appid;
+ /* get the paramaters of the request */
json = afb_req_json(req);
- if (wrap_json_unpack(json, "s", runid)
- && wrap_json_unpack(json, "{ss}", "runid", runid)) {
- INFO("bad request method %s: %s", method,
- json_object_to_json_string(json));
- bad_request(req);
+
+ /* get the runid if any */
+ if (!wrap_json_unpack(json, "i", runid)
+ || !wrap_json_unpack(json, "{si}", _runid_, runid)) {
+ INFO("method %s called for %d", method, *runid);
+ return 1;
+ }
+
+ /* get the appid if any */
+ if (!onappid(req, method, &appid))
+ return 0;
+
+ /* search the runid of the appid */
+ *runid = afm_urun_search_runid(afudb, appid, afb_req_get_uid(req));
+ if (*runid < 0) {
+ /* nothing appropriate */
+ INFO("method %s can't get runid for %s: %m", method,
+ appid);
+ not_found(req);
return 0;
}
- INFO("method %s called for %d", method, *runid);
+ /* found */
+ INFO("method %s called for %s -> %d", method, appid, *runid);
return 1;
}
*/
static void runnables(struct afb_req req)
{
+ const char *lang;
struct json_object *resp;
- INFO("method runnables called");
- resp = afm_udb_applications_public(afudb, afb_req_get_uid(req));
+
+ /* get the language */
+ lang = get_lang(req);
+
+ /* get the details */
+ resp = afm_udb_applications_public(afudb, afb_req_get_uid(req), lang);
afb_req_success(req, resp, NULL);
}
*/
static void detail(struct afb_req req)
{
+ const char *lang;
const char *appid;
- struct json_object *resp, *json;
+ struct json_object *resp;
/* scan the request */
- json = afb_req_json(req);
- if (wrap_json_unpack(json, "s", &appid)
- && wrap_json_unpack(json, "{ss}", _id_, &appid)) {
- bad_request(req);
+ if (!onappid(req, _detail_, &appid))
return;
- }
+
+ /* get the language */
+ lang = get_lang(req);
/* wants details for appid */
- INFO("method detail called for %s", appid);
- resp = afm_udb_get_application_public(afudb, appid, afb_req_get_uid(req));
+ resp = afm_udb_get_application_public(afudb, appid, afb_req_get_uid(req), lang);
if (resp)
afb_req_success(req, resp, NULL);
else
static void start(struct afb_req req)
{
const char *appid;
- struct json_object *appli, *resp, *json;
+ struct json_object *appli, *resp;
int runid;
/* scan the request */
- json = afb_req_json(req);
- if (wrap_json_unpack(json, "s", &appid)
- && wrap_json_unpack(json, "{ss}", _id_, &appid)) {
- bad_request(req);
+ if (!onappid(req, _start_, &appid))
return;
- }
/* get the application */
- INFO("method start called for %s", appid);
appli = afm_udb_get_application_private(afudb, appid, afb_req_get_uid(req));
if (appli == NULL) {
not_found(req);
/* returns */
resp = NULL;
+#if 0
wrap_json_pack(&resp, "{si}", _runid_, runid);
+#else
+ wrap_json_pack(&resp, "i", runid);
+#endif
afb_req_success(req, resp, NULL);
}
static void once(struct afb_req req)
{
const char *appid;
- struct json_object *appli, *resp, *json;
+ struct json_object *appli, *resp;
int runid;
/* scan the request */
- json = afb_req_json(req);
- if (wrap_json_unpack(json, "s", &appid)
- && wrap_json_unpack(json, "{ss}", _id_, &appid)) {
- bad_request(req);
+ if (!onappid(req, _once_, &appid))
return;
- }
/* get the application */
- INFO("method once called for %s", appid);
appli = afm_udb_get_application_private(afudb, appid, afb_req_get_uid(req));
if (appli == NULL) {
not_found(req);
static void runners(struct afb_req req)
{
struct json_object *resp;
- INFO("method runners called");
resp = afm_urun_list(afudb, afb_req_get_uid(req));
afb_req_success(req, resp, NULL);
}
}
}
+/*
+ * On querying installation of widget(s)
+ */
static void install(struct afb_req req)
{
const char *wgtfile;
}
}
+/*
+ * On querying uninstallation of widget(s)
+ */
static void uninstall(struct afb_req req)
{
const char *idaver;
return -!afb_event_is_valid(applist_changed_event);
}
-static const struct afb_auth
- auth_install = {
- .type = afb_auth_Permission,
- .text = "urn:AGL:permission:afm:system:widget:install"
- },
- auth_uninstall = {
- .type = afb_auth_Permission,
- .text = "urn:AGL:permission:afm:system:widget:uninstall"
- }
-;
-
static const struct afb_verb_v2 verbs[] =
{
- {_runnables_, runnables, NULL, "Get list of runnable applications", AFB_SESSION_CHECK_V2 },
- {_detail_ , detail, NULL, "Get the details for one application", AFB_SESSION_CHECK_V2 },
- {_start_ , start, NULL, "Start an application", AFB_SESSION_CHECK_V2 },
- {_once_ , once, NULL, "Start once an application", AFB_SESSION_CHECK_V2 },
- {_terminate_, terminate, NULL, "Terminate a running application", AFB_SESSION_CHECK_V2 },
- {_pause_ , pause, NULL, "Pause a running application", AFB_SESSION_CHECK_V2 },
- {_resume_ , resume, NULL, "Resume a paused application", AFB_SESSION_CHECK_V2 },
- {_runners_ , runners, NULL, "Get the list of running applications", AFB_SESSION_CHECK_V2 },
- {_state_ , state, NULL, "Get the state of a running application", AFB_SESSION_CHECK_V2 },
- {_install_ , install, NULL, "Install an application using a widget file", AFB_SESSION_CHECK_V2 },
- {_uninstall_, uninstall, NULL, "Uninstall an application", AFB_SESSION_CHECK_V2 },
+ {_runnables_, runnables, &auth_detail, "Get list of runnable applications", AFB_SESSION_CHECK_V2 },
+ {_detail_ , detail, &auth_detail, "Get the details for one application", AFB_SESSION_CHECK_V2 },
+ {_start_ , start, &auth_start, "Start an application", AFB_SESSION_CHECK_V2 },
+ {_once_ , once, &auth_start, "Start once an application", AFB_SESSION_CHECK_V2 },
+ {_terminate_, terminate, &auth_kill, "Terminate a running application", AFB_SESSION_CHECK_V2 },
+ {_pause_ , pause, &auth_kill, "Pause a running application", AFB_SESSION_CHECK_V2 },
+ {_resume_ , resume, &auth_kill, "Resume a paused application", AFB_SESSION_CHECK_V2 },
+ {_runners_ , runners, &auth_state, "Get the list of running applications", AFB_SESSION_CHECK_V2 },
+ {_state_ , state, &auth_state, "Get the state of a running application", AFB_SESSION_CHECK_V2 },
+ {_install_ , install, &auth_install, "Install an application using a widget file", AFB_SESSION_CHECK_V2 },
+ {_uninstall_, uninstall, &auth_uninstall, "Uninstall an application", AFB_SESSION_CHECK_V2 },
{ NULL, NULL, NULL, NULL, 0 }
};