Improve use of systemd's states
[src/app-framework-main.git] / src / afm-binding.c
index dd59be9..1771520 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2015-2018 "IoT.bzh"
+ * Copyright (C) 2015-2020 "IoT.bzh"
  * Author José Bollo <jose.bollo@iot.bzh>
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
@@ -21,6 +21,7 @@
 #include <string.h>
 #include <assert.h>
 #include <signal.h>
+#include <errno.h>
 
 #include <json-c/json.h>
 
@@ -40,6 +41,7 @@
  * constant strings
  */
 static const char _added_[]     = "added";
+static const char _all_[]       = "all";
 static const char _a_l_c_[]     = "application-list-changed";
 static const char _bad_request_[] = "bad-request";
 static const char _cannot_start_[] = "cannot-start";
@@ -48,6 +50,7 @@ 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 _not_running_[] = "not-running";
 static const char _once_[]      = "once";
 static const char _pause_[]     = "pause";
 static const char _resume_[]    = "resume";
@@ -66,43 +69,43 @@ static const char _update_[]    = "update";
 static const struct afb_auth
        auth_perm_widget = {
                .type = afb_auth_Permission,
-               .text = "urn:AGL:permission:afm:system:widget"
+               .text = FWK_PREFIX"permission:afm:system:widget"
        },
        auth_perm_widget_install = {
                .type = afb_auth_Permission,
-               .text = "urn:AGL:permission:afm:system:widget:install"
+               .text = FWK_PREFIX"permission:afm:system:widget:install"
        },
        auth_perm_widget_uninstall = {
                .type = afb_auth_Permission,
-               .text = "urn:AGL:permission:afm:system:widget:uninstall"
+               .text = FWK_PREFIX"permission:afm:system:widget:uninstall"
        },
        auth_perm_widget_preinstall = {
                .type = afb_auth_Permission,
-               .text = "urn:AGL:permission:afm:system:widget:preinstall"
+               .text = FWK_PREFIX"permission:afm:system:widget:preinstall"
        },
        auth_perm_widget_detail = {
                .type = afb_auth_Permission,
-               .text = "urn:AGL:permission:afm:system:widget:detail"
+               .text = FWK_PREFIX"permission:afm:system:widget:detail"
        },
        auth_perm_widget_start = {
                .type = afb_auth_Permission,
-               .text = "urn:AGL:permission:afm:system:widget:start"
+               .text = FWK_PREFIX"permission:afm:system:widget:start"
        },
        auth_perm_widget_view_all = {
                .type = afb_auth_Permission,
-               .text = "urn:AGL:permission:afm:system:widget:view-all"
+               .text = FWK_PREFIX"permission:afm:system:widget:view-all"
        },
        auth_perm_runner = {
                .type = afb_auth_Permission,
-               .text = "urn:AGL:permission:afm:system:runner"
+               .text = FWK_PREFIX"permission:afm:system:runner"
        },
        auth_perm_runner_state = {
                .type = afb_auth_Permission,
-               .text = "urn:AGL:permission:afm:system:runner:state"
+               .text = FWK_PREFIX"permission:afm:system:runner:state"
        },
        auth_perm_runner_kill = {
                .type = afb_auth_Permission,
-               .text = "urn:AGL:permission:afm:system:runner:kill"
+               .text = FWK_PREFIX"permission:afm:system:runner:kill"
        },
 
        auth_install = {
@@ -186,6 +189,12 @@ static void not_found(afb_req_t req)
        afb_req_fail(req, _not_found_, NULL);
 }
 
+/* common not running reply */
+static void not_running(afb_req_t req)
+{
+       afb_req_fail(req, _not_running_, NULL);
+}
+
 /* common can't start reply */
 static void cant_start(afb_req_t req)
 {
@@ -218,6 +227,18 @@ static const char *get_lang(afb_req_t req)
        return lang;
 }
 
+/*
+ * Retrieve whether all is required from 'req'.
+ */
+static int get_all(afb_req_t req)
+{
+       struct json_object *val;
+
+       /* get the optional language */
+       return json_object_object_get_ex(afb_req_json(req), _all_, &val)
+               && json_object_get_boolean(val);
+}
+
 
 /*
  * retrieves the 'appid' in parameters received with the
@@ -236,7 +257,7 @@ static int onappid(afb_req_t req, const char *method, const char **appid)
 
        /* get the appid if any */
        if (!wrap_json_unpack(json, "s", appid)
-        || !wrap_json_unpack(json, "{si}", _id_, appid)) {
+        || !wrap_json_unpack(json, "{ss}", _id_, appid)) {
                /* found */
                INFO("method %s called for %s", method, *appid);
                return 1;
@@ -282,7 +303,10 @@ static int onrunid(afb_req_t req, const char *method, int *runid)
                /* nothing appropriate */
                INFO("method %s can't get runid for %s: %m", method,
                                                        appid);
-               not_found(req);
+               if (errno == ESRCH)
+                       not_running(req);
+               else
+                       not_found(req);
                return 0;
        }
 
@@ -295,21 +319,21 @@ static int onrunid(afb_req_t req, const char *method, int *runid)
  * Sends the reply 'resp' to the request 'req' if 'resp' is not NULLzero.
  * Otherwise, when 'resp' is NULL replies the error string 'errstr'.
  */
-static void reply(afb_req_t req, struct json_object *resp, const char *errstr)
+static void reply(afb_req_t req, struct json_object *resp)
 {
-       if (!resp)
-               afb_req_fail(req, errstr, NULL);
+       if (resp)
+               afb_req_reply(req, resp, NULL, NULL);
        else
-               afb_req_success(req, resp, NULL);
+               afb_req_reply(req, NULL, "failed", strerror(errno));
 }
 
 /*
  * Sends the reply "true" to the request 'req' if 'status' is zero.
  * Otherwise, when 'status' is not zero replies the error string 'errstr'.
  */
-static void reply_status(afb_req_t req, int status, const char *errstr)
+static void reply_status(afb_req_t req, int status)
 {
-       reply(req, status ? NULL : json_object_get(json_true), errstr);
+       reply(req, status ? NULL : json_object_get(json_true));
 }
 
 /*
@@ -317,14 +341,18 @@ static void reply_status(afb_req_t req, int status, const char *errstr)
  */
 static void runnables(afb_req_t req)
 {
+       int all;
        const char *lang;
        struct json_object *resp;
 
        /* get the language */
        lang = get_lang(req);
 
+       /* get the all */
+       all = get_all(req);
+
        /* get the details */
-       resp = afm_udb_applications_public(afudb, afb_req_get_uid(req), lang);
+       resp = afm_udb_applications_public(afudb, all, afb_req_get_uid(req), lang);
        afb_req_success(req, resp, NULL);
 }
 
@@ -374,7 +402,7 @@ static void start(afb_req_t req)
 
        /* launch the application */
        runid = afm_urun_start(appli, afb_req_get_uid(req));
-       if (runid <= 0) {
+       if (runid < 0) {
                cant_start(req);
                return;
        }
@@ -384,7 +412,8 @@ static void start(afb_req_t req)
 #if 0
        wrap_json_pack(&resp, "{si}", _runid_, runid);
 #else
-       wrap_json_pack(&resp, "i", runid);
+       if (runid)
+               wrap_json_pack(&resp, "i", runid);
 #endif
        afb_req_success(req, resp, NULL);
 }
@@ -411,13 +440,13 @@ static void once(afb_req_t req)
 
        /* launch the application */
        runid = afm_urun_once(appli, afb_req_get_uid(req));
-       if (runid <= 0) {
+       if (runid < 0) {
                cant_start(req);
                return;
        }
 
        /* returns the state */
-       resp = afm_urun_state(afudb, runid, afb_req_get_uid(req));
+       resp = runid ? afm_urun_state(afudb, runid, afb_req_get_uid(req)) : NULL;
        afb_req_success(req, resp, NULL);
 }
 
@@ -429,7 +458,7 @@ static void pause(afb_req_t req)
        int runid, status;
        if (onrunid(req, "pause", &runid)) {
                status = afm_urun_pause(runid, afb_req_get_uid(req));
-               reply_status(req, status, _not_found_);
+               reply_status(req, status);
        }
 }
 
@@ -441,7 +470,7 @@ static void resume(afb_req_t req)
        int runid, status;
        if (onrunid(req, "resume", &runid)) {
                status = afm_urun_resume(runid, afb_req_get_uid(req));
-               reply_status(req, status, _not_found_);
+               reply_status(req, status);
        }
 }
 
@@ -453,7 +482,7 @@ static void terminate(afb_req_t req)
        int runid, status;
        if (onrunid(req, "terminate", &runid)) {
                status = afm_urun_terminate(runid, afb_req_get_uid(req));
-               reply_status(req, status, _not_found_);
+               reply_status(req, status);
        }
 }
 
@@ -462,8 +491,10 @@ static void terminate(afb_req_t req)
  */
 static void runners(afb_req_t req)
 {
+       int all;
        struct json_object *resp;
-       resp = afm_urun_list(afudb, afb_req_get_uid(req));
+       all = get_all(req);
+       resp = afm_urun_list(afudb, all, afb_req_get_uid(req));
        afb_req_success(req, resp, NULL);
 }
 
@@ -476,7 +507,7 @@ static void state(afb_req_t req)
        struct json_object *resp;
        if (onrunid(req, "state", &runid)) {
                resp = afm_urun_state(afudb, runid, afb_req_get_uid(req));
-               reply(req, resp, _not_found_);
+               reply(req, resp);
        }
 }
 
@@ -574,7 +605,7 @@ static int init(afb_api_t api)
        json_true = json_object_new_boolean(1);
 
        /* init database */
-       afudb = afm_udb_create(1, 0, "afm-appli-");
+       afudb = afm_udb_create(1, 0, "afm-");
        if (!afudb) {
                ERROR("afm_udb_create failed");
                return -1;