4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
8 http://www.apache.org/licenses/LICENSE-2.0
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
28 #include <sys/types.h>
50 #define ROOT_RUNNERS_COUNT 32
51 #define MAX_RUNNER_COUNT 32767
53 static struct apprun *runners[ROOT_RUNNERS_COUNT];
54 static int runnercount = 0;
55 static int runnerid = 0;
57 static struct apprun *getrunner(int id)
59 struct apprun *result = runners[id & (ROOT_RUNNERS_COUNT - 1)];
60 while (result && result->id != id)
61 result = result->next;
65 static void freerunner(struct apprun *runner)
67 struct apprun **prev = &runners[runner->id & (ROOT_RUNNERS_COUNT - 1)];
69 while(*prev != runner) {
70 prev = &(*prev)->next;
78 static struct apprun *createrunner()
80 struct apprun *result;
82 if (runnercount >= MAX_RUNNER_COUNT)
86 if (runnerid > MAX_RUNNER_COUNT)
88 } while(getrunner(runnerid));
89 result = malloc(sizeof * result);
91 result->id = runnerid;
92 result->state = as_in_progress;
95 result->next = runners[runnerid & (ROOT_RUNNERS_COUNT - 1)];
96 runners[runnerid & (ROOT_RUNNERS_COUNT - 1)] = result;
102 int appfwk_run_start(struct json_object *appli)
107 int appfwk_run_stop(int runid)
112 int appfwk_run_suspend(int runid)
117 int appfwk_run_resume(int runid)
122 struct json_object *appfwk_run_list()
127 struct json_object *appfwk_run_state(int runid)
134 static struct json_object *mkrunner(const char *appid, const char *runid)
136 struct json_object *result = json_object_new_object();
138 if(json_add_str(result, "id", appid)
139 || json_add_str(result, "runid", runid)
140 || json_add_str(result, "state", NULL)) {
141 json_object_put(result);
148 const char *appfwk_start(struct appfwk *af, const char *appid)
150 struct json_object *appli;
151 struct json_object *runner;
154 /* get the application description */
155 appli = appfwk_get_application(af, appid);
161 /* prepare the execution */
162 snprintf(buffer, sizeof buffer, "{\"id\":\"%s\",\"runid\":\"%s\""
165 int appfwk_stop(struct appfwk *af, const char *runid)
167 struct json_object *runner;
168 runner = appfwk_state(af, runid);
169 if (runner == NULL) {
173 json_object_get(runner);
174 json_object_object_del(af->runners, runid);
188 json_object_put(runner);
191 int appfwk_suspend(struct appfwk *af, const char *runid)
195 int appfwk_resume(struct appfwk *af, const char *runid)
199 struct json_object *appfwk_running_list(struct appfwk *af)
204 struct json_object *appfwk_state(struct appfwk *af, const char *runid)
206 struct json_object *result;
207 int status = json_object_object_get_ex(af->runners, runid, &result);
208 return status ? result : NULL;
217 #if defined(TESTAPPFWK)
221 struct appfwk *af = appfwk_create();
222 appfwk_add_root(af,FWK_APP_DIR);
223 appfwk_update_applications(af);
224 printf("array = %s\n", json_object_to_json_string_ext(af->applications.pubarr, 3));
225 printf("direct = %s\n", json_object_to_json_string_ext(af->applications.direct, 3));
226 printf("byapp = %s\n", json_object_to_json_string_ext(af->applications.byapp, 3));
231 static struct json_object *mkrunner(const char *appid, const char *runid)
233 struct json_object *result = json_object_new_object();
235 if(json_add_str(result, "id", appid)
236 || json_add_str(result, "runid", runid)
237 || json_add_str(result, "state", NULL)) {
238 json_object_put(result);
245 const char *appfwk_start(struct appfwk *af, const char *appid)
247 struct json_object *appli;
248 struct json_object *runner;
251 /* get the application description */
252 appli = appfwk_get_application(af, appid);
258 /* prepare the execution */
261 int appfwk_stop(struct appfwk *af, const char *runid)
263 struct json_object *runner;
264 runner = appfwk_state(af, runid);
265 if (runner == NULL) {
269 json_object_get(runner);
270 json_object_object_del(af->runners, runid);
284 json_object_put(runner);
287 int appfwk_suspend(struct appfwk *af, const char *runid)
291 int appfwk_resume(struct appfwk *af, const char *runid)
295 struct json_object *appfwk_running_list(struct appfwk *af)
300 struct json_object *appfwk_state(struct appfwk *af, const char *runid)
302 struct json_object *result;
303 int status = json_object_object_get_ex(af->runners, runid, &result);
304 return status ? result : NULL;