afm-run: improve error diagnostic
[src/app-framework-main.git] / src / afm-run.c
index 537ba62..089aa77 100644 (file)
@@ -1,5 +1,5 @@
 /*
- Copyright 2015, 2016 IoT.bzh
+ Copyright 2015, 2016, 2017 IoT.bzh
 
  author: José Bollo <jose.bollo@iot.bzh>
 
@@ -247,6 +247,25 @@ static struct apprun *getrunner(int runid)
        return result;
 }
 
+/*
+ * Get first runner of 'appli' (NULL if not found)
+ */
+static struct apprun *getrunner_appli(json_object *appli)
+{
+       int i;
+       struct apprun *result;
+
+       for (i = 0 ; i < ROOT_RUNNERS_COUNT ; i++) {
+               result = runners_by_pgid[i];
+               while (result != NULL) {
+                       if (result->appli == appli)
+                               return result;
+                       result = result->next_by_pgid;
+               }
+       }
+       return NULL;
+}
+
 /*
  * Free an existing 'runner'
  */
@@ -449,6 +468,7 @@ static int fill_launch_desc(struct json_object *appli,
        || !j_read_string_at(pub, "name", &desc->name)
        || !j_read_integer_at(pub, "width", &desc->width)
        || !j_read_integer_at(pub, "height", &desc->height)) {
+               ERROR("bad internal description of the application to launch: %s", json_object_get_string(appli));
                errno = EINVAL;
                return -1;
        }
@@ -592,6 +612,23 @@ int afm_run_start(struct json_object *appli, enum afm_launch_mode mode,
        return rc;
 }
 
+/*
+ * Returns the runid of a previously started application 'appli'
+ * or if none is running, starts the application described by 'appli'
+ * in local mode.
+ *
+ * A reference to 'appli' is kept during the live of the
+ * runner. This is made using json_object_get. Thus be aware
+ * that further modifications to 'appli' might create errors.
+ *
+ * Returns the runid in case of success or -1 in case of error
+ */
+int afm_run_once(struct json_object *appli)
+{
+       struct apprun *runner = getrunner_appli(appli);
+       return runner && is_alive(runner) ? runner->runid : afm_run_start(appli, mode_local, NULL);
+}
+
 /*
  * Terminates the runner of 'runid'
  *