work in progress
[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 runid;
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 = &[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 }
105
106 int appfwk_run_stop()
107 {
108 }
109
110
111 static struct json_object *mkrunner(const char *appid, const char *runid)
112 {
113         struct json_object *result = json_object_new_object();
114         if (result) {
115                 if(json_add_str(result, "id", appid)
116                 || json_add_str(result, "runid", runid)
117                 || json_add_str(result, "state", NULL)) {
118                         json_object_put(result);
119                         result = NULL;
120                 }
121         }
122         return result;
123 }
124
125 const char *appfwk_start(struct appfwk *af, const char *appid)
126 {
127         struct json_object *appli;
128         struct json_object *runner;
129         char buffer[250];
130
131         /* get the application description */
132         appli = appfwk_get_application(af, appid);
133         if (appli == NULL) {
134                 errno = ENOENT;
135                 return -1;
136         }
137
138         /* prepare the execution */
139         snprintf(buffer, sizeof buffer, "{\"id\":\"%s\",\"runid\":\"%s\"
140 }
141
142 int appfwk_stop(struct appfwk *af, const char *runid)
143 {
144         struct json_object *runner;
145         runner = appfwk_state(af, runid);
146         if (runner == NULL) {
147                 errno = ENOENT;
148                 return -1;
149         }
150         json_object_get(runner);
151         json_object_object_del(af->runners, runid);
152
153
154
155
156
157
158 ..........
159
160
161
162
163
164
165         json_object_put(runner);
166 }
167
168 int appfwk_suspend(struct appfwk *af, const char *runid)
169 {
170 }
171
172 int appfwk_resume(struct appfwk *af, const char *runid)
173 {
174 }
175
176 struct json_object *appfwk_running_list(struct appfwk *af)
177 {
178         return af->runners;
179 }
180
181 struct json_object *appfwk_state(struct appfwk *af, const char *runid)
182 {
183         struct json_object *result;
184         int status = json_object_object_get_ex(af->runners, runid, &result);
185         return status ? result : NULL;
186 }
187
188
189
190
191
192
193
194 #if defined(TESTAPPFWK)
195 #include <stdio.h>
196 int main()
197 {
198 struct appfwk *af = appfwk_create();
199 appfwk_add_root(af,FWK_APP_DIR);
200 appfwk_update_applications(af);
201 printf("array = %s\n", json_object_to_json_string_ext(af->applications.pubarr, 3));
202 printf("direct = %s\n", json_object_to_json_string_ext(af->applications.direct, 3));
203 printf("byapp = %s\n", json_object_to_json_string_ext(af->applications.byapp, 3));
204 return 0;
205 }
206 #endif
207