/*
Copyright 2015 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.
You may obtain a copy of the License at
limitations under the License.
*/
+#include <unistd.h>
+#include <stdio.h>
+#include <time.h>
+
#include <json.h>
#include "verbose.h"
#include "utils-jbus.h"
-#include "appfwk.h"
+#include "af-db.h"
+#include "af-run.h"
static struct jbus *jbus;
-static struct appfwk *appfwk;
+static struct af_db *afdb;
const char error_nothing[] = "[]";
+const char error_bad_request[] = "{\"status\":\"error: bad request\"}";
const char error_not_found[] = "{\"status\":\"error: not found\"}";
+const char error_cant_start[] = "{\"status\":\"error: can't start\"}";
static const char *getappid(struct json_object *obj)
{
- return json_object_get_string(obj);
+ return json_type_string == json_object_get_type(obj) ? json_object_get_string(obj) : NULL;
}
-static const char *getrunid(struct json_object *obj)
+static int getrunid(struct json_object *obj)
{
- return json_object_get_string(obj);
+ return json_type_int == json_object_get_type(obj) ? json_object_get_int(obj) : 0;
}
static void reply(struct jreq *jreq, struct json_object *resp, const char *errstr)
{
- if (obj)
+ if (resp)
jbus_reply(jreq, resp);
else
jbus_replyj(jreq, errstr);
static void on_runnables(struct jreq *jreq, struct json_object *obj)
{
- struct json_object *resp = appfwk_application_list(appfwk);
+ struct json_object *resp = af_db_application_list(afdb);
jbus_reply(jreq, resp);
json_object_put(obj);
}
static void on_detail(struct jreq *jreq, struct json_object *obj)
{
const char *appid = getappid(obj);
- struct json_object *resp = appfwk_get_application_public(appfwk, appid);
+ struct json_object *resp = af_db_get_application_public(afdb, appid);
reply(jreq, resp, error_not_found);
json_object_put(obj);
}
static void on_start(struct jreq *jreq, struct json_object *obj)
{
- const char *appid = getappid(obj);
- const char *runid = appfwk_start(appfwk, appid);
- jbus_replyj(jreq, runid ? runid : error_not_found);
+ const char *appid;
+ struct json_object *appli;
+ int runid;
+ char runidstr[20];
+
+ appid = getappid(obj);
+ if (appid == NULL)
+ jbus_replyj(jreq, error_bad_request);
+ else {
+ appli = af_db_get_application(afdb, appid);
+ if (appli == NULL)
+ jbus_replyj(jreq, error_not_found);
+ else {
+ runid = af_run_start(appli);
+ if (runid <= 0)
+ jbus_replyj(jreq, error_cant_start);
+ else {
+ snprintf(runidstr, sizeof runidstr, "%d", runid);
+ runidstr[sizeof runidstr - 1] = 0;
+ jbus_replyj(jreq, runidstr);
+ }
+ }
+ }
json_object_put(obj);
}
static void on_stop(struct jreq *jreq, struct json_object *obj)
{
- const char *runid = getrunid(obj);
- int status = appfwk_stop(appfwk, runid);
+ int runid = getrunid(obj);
+ int status = af_run_stop(runid);
jbus_replyj(jreq, status ? error_not_found : "true");
json_object_put(obj);
}
-static void on_suspend(struct jreq *jreq, struct json_object *obj)
+static void on_continue(struct jreq *jreq, struct json_object *obj)
{
- const char *runid = getrunid(obj);
- int status = appfwk_suspend(appfwk, runid);
+ int runid = getrunid(obj);
+ int status = af_run_continue(runid);
jbus_replyj(jreq, status ? error_not_found : "true");
json_object_put(obj);
}
-static void on_resume(struct jreq *jreq, struct json_object *obj)
+static void on_terminate(struct jreq *jreq, struct json_object *obj)
{
- const char *runid = getrunid(obj);
- int status = appfwk_resume(appfwk, runid);
+ int runid = getrunid(obj);
+ int status = af_run_terminate(runid);
jbus_replyj(jreq, status ? error_not_found : "true");
json_object_put(obj);
}
static void on_runners(struct jreq *jreq, struct json_object *obj)
{
- struct json_object *resp = appfwk_running_list(appfwk);
+ struct json_object *resp = af_run_list();
jbus_reply(jreq, resp);
+ json_object_put(resp);
json_object_put(obj);
}
static void on_state(struct jreq *jreq, struct json_object *obj)
{
- const char *runid = getrunid(obj);
- int status = appfwk_state(appfwk, runid);
- jbus_replyj(jreq, status ? error_not_found : "true");
+ int runid = getrunid(obj);
+ struct json_object *resp = af_run_state(runid);
+ reply(jreq, resp, error_not_found);
+ json_object_put(resp);
json_object_put(obj);
}
+static int daemonize()
+{
+ int rc = fork();
+ if (rc < 0)
+ return rc;
+ if (rc)
+ _exit(0);
+ return 0;
+}
+
int main(int ac, char **av)
{
- LOGAUTH("af-usrd");
+ LOGAUTH("afdb-usrd");
+
+ /* init random generator */
+ srandom((unsigned int)time(NULL));
+
+ /* init runners */
+ if (af_run_init()) {
+ ERROR("af_run_init failed");
+ return 1;
+ }
/* init framework */
- appfwk = appfwk_create();
- if (!appfwk) {
- ERROR("appfwk_create failed");
+ afdb = af_db_create();
+ if (!afdb) {
+ ERROR("af_create failed");
return 1;
}
- if (appfwk_add_root(appfwk, FWK_APP_DIR)) {
+ if (af_db_add_root(afdb, FWK_APP_DIR)) {
ERROR("can't add root %s", FWK_APP_DIR);
return 1;
}
- if (appfwk_update_applications(appfwk)) {
- ERROR("appfwk_update_applications failed");
+ if (af_db_update_applications(afdb)) {
+ ERROR("af_update_applications failed");
return 1;
}
/* init service */
- jbus = create_jbus(1, "/org/automotive/linux/framework");
+ jbus = create_jbus(1, "/org/AGL/framework");
if (!jbus) {
ERROR("create_jbus failed");
return 1;
}
if(jbus_add_service(jbus, "runnables", on_runnables)
|| jbus_add_service(jbus, "detail", on_detail)
- || jbus_add_service(jbus, "start", on_run)
+ || jbus_add_service(jbus, "start", on_start)
+ || jbus_add_service(jbus, "terminate", on_terminate)
|| jbus_add_service(jbus, "stop", on_stop)
- || jbus_add_service(jbus, "suspend", on_suspend)
- || jbus_add_service(jbus, "resume", on_resume)
+ || jbus_add_service(jbus, "continue", on_continue)
|| jbus_add_service(jbus, "runners", on_runners)
|| jbus_add_service(jbus, "state", on_state)) {
ERROR("adding services failed");