in progress (compiles)
[src/app-framework-main.git] / src / appfwk-run.c
1 /*
2  Copyright 2015 IoT.bzh
3
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
7
8      http://www.apache.org/licenses/LICENSE-2.0
9
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.
15 */
16
17 #include <unistd.h>
18
19
20
21
22 #include <stdlib.h>
23 #include <assert.h>
24 #include <string.h>
25 #include <errno.h>
26 #include <dirent.h>
27 #include <fcntl.h>
28 #include <sys/types.h>
29
30 #include <json.h>
31
32 #include <wgt-info.h>
33
34
35 enum appstate {
36         as_in_progress,
37         as_running,
38         as_paused,
39         as_stopping
40 };
41
42 struct apprun {
43         struct apprun *next;
44         int id;
45         enum appstate state;
46         pid_t backend;
47         pid_t frontend;
48 };
49
50 #define ROOT_RUNNERS_COUNT  32
51 #define MAX_RUNNER_COUNT    32767
52
53 static struct apprun *runners[ROOT_RUNNERS_COUNT];
54 static int runnercount = 0;
55 static int runnerid = 0;
56
57 static struct apprun *getrunner(int id)
58 {
59         struct apprun *result = runners[id & (ROOT_RUNNERS_COUNT - 1)];
60         while (result && result->id != id)
61                 result = result->next;
62         return result;
63 }
64
65 static void freerunner(struct apprun *runner)
66 {
67         struct apprun **prev = &runners[runner->id & (ROOT_RUNNERS_COUNT - 1)];
68         assert(*prev);
69         while(*prev != runner) {
70                 prev = &(*prev)->next;
71                 assert(*prev);
72         }
73         *prev = runner->next;
74         free(runner);
75         runnercount--;
76 }
77
78 static struct apprun *createrunner()
79 {
80         struct apprun *result;
81
82         if (runnercount >= MAX_RUNNER_COUNT)
83                 return NULL;
84         do {
85                 runnerid++;
86                 if (runnerid > MAX_RUNNER_COUNT)
87                         runnerid = 1;
88         } while(getrunner(runnerid));
89         result = malloc(sizeof * result);
90         if (result) {
91                 result->id = runnerid;
92                 result->state = as_in_progress;
93                 result->backend = 0;
94                 result->frontend = 0;
95                 result->next = runners[runnerid & (ROOT_RUNNERS_COUNT - 1)];
96                 runners[runnerid & (ROOT_RUNNERS_COUNT - 1)] = result;
97                 runnercount++;
98         }
99         return result;
100 }
101
102 int appfwk_run_start(struct json_object *appli)
103 {
104         return -1;
105 }
106
107 int appfwk_run_stop(int runid)
108 {
109         return -1;
110 }
111
112 int appfwk_run_suspend(int runid)
113 {
114         return -1;
115 }
116
117 int appfwk_run_resume(int runid)
118 {
119         return -1;
120 }
121
122 struct json_object *appfwk_run_list()
123 {
124         return NULL;
125 }
126
127 struct json_object *appfwk_run_state(int runid)
128 {
129         return NULL;
130 }
131
132 #if 0
133
134 static struct json_object *mkrunner(const char *appid, const char *runid)
135 {
136         struct json_object *result = json_object_new_object();
137         if (result) {
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);
142                         result = NULL;
143                 }
144         }
145         return result;
146 }
147
148 const char *appfwk_start(struct appfwk *af, const char *appid)
149 {
150         struct json_object *appli;
151         struct json_object *runner;
152         char buffer[250];
153
154         /* get the application description */
155         appli = appfwk_get_application(af, appid);
156         if (appli == NULL) {
157                 errno = ENOENT;
158                 return -1;
159         }
160
161         /* prepare the execution */
162         snprintf(buffer, sizeof buffer, "{\"id\":\"%s\",\"runid\":\"%s\""
163 }
164
165 int appfwk_stop(struct appfwk *af, const char *runid)
166 {
167         struct json_object *runner;
168         runner = appfwk_state(af, runid);
169         if (runner == NULL) {
170                 errno = ENOENT;
171                 return -1;
172         }
173         json_object_get(runner);
174         json_object_object_del(af->runners, runid);
175
176
177
178
179
180
181 ..........
182
183
184
185
186
187
188         json_object_put(runner);
189 }
190
191 int appfwk_suspend(struct appfwk *af, const char *runid)
192 {
193 }
194
195 int appfwk_resume(struct appfwk *af, const char *runid)
196 {
197 }
198
199 struct json_object *appfwk_running_list(struct appfwk *af)
200 {
201         return af->runners;
202 }
203
204 struct json_object *appfwk_state(struct appfwk *af, const char *runid)
205 {
206         struct json_object *result;
207         int status = json_object_object_get_ex(af->runners, runid, &result);
208         return status ? result : NULL;
209 }
210
211
212
213
214
215
216
217 #if defined(TESTAPPFWK)
218 #include <stdio.h>
219 int main()
220 {
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));
227 return 0;
228 }
229 #endif
230
231 static struct json_object *mkrunner(const char *appid, const char *runid)
232 {
233         struct json_object *result = json_object_new_object();
234         if (result) {
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);
239                         result = NULL;
240                 }
241         }
242         return result;
243 }
244
245 const char *appfwk_start(struct appfwk *af, const char *appid)
246 {
247         struct json_object *appli;
248         struct json_object *runner;
249         char buffer[250];
250
251         /* get the application description */
252         appli = appfwk_get_application(af, appid);
253         if (appli == NULL) {
254                 errno = ENOENT;
255                 return -1;
256         }
257
258         /* prepare the execution */
259 }
260
261 int appfwk_stop(struct appfwk *af, const char *runid)
262 {
263         struct json_object *runner;
264         runner = appfwk_state(af, runid);
265         if (runner == NULL) {
266                 errno = ENOENT;
267                 return -1;
268         }
269         json_object_get(runner);
270         json_object_object_del(af->runners, runid);
271
272
273
274
275
276
277 ..........
278
279
280
281
282
283
284         json_object_put(runner);
285 }
286
287 int appfwk_suspend(struct appfwk *af, const char *runid)
288 {
289 }
290
291 int appfwk_resume(struct appfwk *af, const char *runid)
292 {
293 }
294
295 struct json_object *appfwk_running_list(struct appfwk *af)
296 {
297         return af->runners;
298 }
299
300 struct json_object *appfwk_state(struct appfwk *af, const char *runid)
301 {
302         struct json_object *result;
303         int status = json_object_object_get_ex(af->runners, runid, &result);
304         return status ? result : NULL;
305 }
306
307
308
309 #endif