afm-binding: Simplify helpers
[src/app-framework-main.git] / src / afm-binding.c
index f4bba8a..7e99b20 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");
@@ -41,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";
@@ -68,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 = {
@@ -179,6 +180,9 @@ static void do_reloads()
 /* common bad request reply */
 static void bad_request(afb_req_t req)
 {
+       INFO("bad request verb %s: %s",
+               afb_req_get_called_verb(req),
+               json_object_to_json_string(afb_req_json(req)));
        afb_req_fail(req, _bad_request_, NULL);
 }
 
@@ -226,16 +230,27 @@ 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
- * request 'req' for the 'method'.
+ * request 'req'.
  *
  * 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(afb_req_t req, const char *method, const char **appid)
+static int onappid(afb_req_t req, const char **appid)
 {
        struct json_object *json;
 
@@ -246,26 +261,24 @@ static int onappid(afb_req_t req, const char *method, const char **appid)
        if (!wrap_json_unpack(json, "s", appid)
         || !wrap_json_unpack(json, "{ss}", _id_, appid)) {
                /* found */
-               INFO("method %s called for %s", method, *appid);
+               INFO("method %s called for %s", afb_req_get_called_verb(req), *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'.
+ * request 'req'.
  *
  * Returns 1 in case of success.
  * 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(afb_req_t req, const char *method, int *runid)
+static int onrunid(afb_req_t req, int *runid)
 {
        struct json_object *json;
        const char *appid;
@@ -276,19 +289,19 @@ static int onrunid(afb_req_t req, const char *method, int *runid)
        /* 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);
+               INFO("method %s called for %d", afb_req_get_called_verb(req), *runid);
                return 1;
        }
 
        /* get the appid if any */
-       if (!onappid(req, method, &appid))
+       if (!onappid(req, &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,
+               INFO("method %s can't get runid for %s: %m", afb_req_get_called_verb(req),
                                                        appid);
                if (errno == ESRCH)
                        not_running(req);
@@ -298,7 +311,7 @@ static int onrunid(afb_req_t req, const char *method, int *runid)
        }
 
        /* found */
-       INFO("method %s called for %s -> %d", method, appid, *runid);
+       INFO("method %s called for %s -> %d", afb_req_get_called_verb(req), appid, *runid);
        return 1;
 }
 
@@ -328,14 +341,18 @@ static void reply_status(afb_req_t req, int status)
  */
 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);
 }
 
@@ -349,7 +366,7 @@ static void detail(afb_req_t req)
        struct json_object *resp;
 
        /* scan the request */
-       if (!onappid(req, _detail_, &appid))
+       if (!onappid(req, &appid))
                return;
 
        /* get the language */
@@ -373,7 +390,7 @@ static void start(afb_req_t req)
        int runid;
 
        /* scan the request */
-       if (!onappid(req, _start_, &appid))
+       if (!onappid(req, &appid))
                return;
 
        /* get the application */
@@ -410,7 +427,7 @@ static void once(afb_req_t req)
        int runid;
 
        /* scan the request */
-       if (!onappid(req, _once_, &appid))
+       if (!onappid(req, &appid))
                return;
 
        /* get the application */
@@ -438,7 +455,7 @@ static void once(afb_req_t req)
 static void pause(afb_req_t req)
 {
        int runid, status;
-       if (onrunid(req, "pause", &runid)) {
+       if (onrunid(req, &runid)) {
                status = afm_urun_pause(runid, afb_req_get_uid(req));
                reply_status(req, status);
        }
@@ -450,7 +467,7 @@ static void pause(afb_req_t req)
 static void resume(afb_req_t req)
 {
        int runid, status;
-       if (onrunid(req, "resume", &runid)) {
+       if (onrunid(req, &runid)) {
                status = afm_urun_resume(runid, afb_req_get_uid(req));
                reply_status(req, status);
        }
@@ -462,7 +479,7 @@ static void resume(afb_req_t req)
 static void terminate(afb_req_t req)
 {
        int runid, status;
-       if (onrunid(req, "terminate", &runid)) {
+       if (onrunid(req, &runid)) {
                status = afm_urun_terminate(runid, afb_req_get_uid(req));
                reply_status(req, status);
        }
@@ -473,8 +490,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);
 }
 
@@ -485,7 +504,7 @@ static void state(afb_req_t req)
 {
        int runid;
        struct json_object *resp;
-       if (onrunid(req, "state", &runid)) {
+       if (onrunid(req, &runid)) {
                resp = afm_urun_state(afudb, runid, afb_req_get_uid(req));
                reply(req, resp);
        }
@@ -585,7 +604,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;