4 author: José Bollo <jose.bollo@iot.bzh>
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
10 http://www.apache.org/licenses/LICENSE-2.0
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
27 #include "utils-jbus.h"
31 static const char appname[] = "afm-user-daemon";
36 "usage: %s [-q] [-v] [-r rootdir]... [-a appdir]...\n"
38 " -a appdir adds an application directory\n"
39 " -r rootdir adds a root directory of applications\n"
40 " -d run as a daemon\n"
48 static struct option options[] = {
49 { "root", required_argument, NULL, 'r' },
50 { "application", required_argument, NULL, 'a' },
51 { "daemon", no_argument, NULL, 'd' },
52 { "quiet", no_argument, NULL, 'q' },
53 { "verbose", no_argument, NULL, 'v' },
54 { "help", no_argument, NULL, 'h' },
58 static struct jbus *jbus;
59 static struct af_db *afdb;
61 const char error_nothing[] = "[]";
62 const char error_bad_request[] = "\"bad request\"";
63 const char error_not_found[] = "\"not found\"";
64 const char error_cant_start[] = "\"can't start\"";
66 static const char *getappid(struct json_object *obj)
68 return json_type_string == json_object_get_type(obj) ? json_object_get_string(obj) : NULL;
71 static int getrunid(struct json_object *obj)
73 return json_type_int == json_object_get_type(obj) ? json_object_get_int(obj) : 0;
76 static void reply(struct jreq *jreq, struct json_object *resp, const char *errstr)
79 jbus_reply_j(jreq, resp);
81 jbus_reply_error_s(jreq, errstr);
84 static void reply_status(struct jreq *jreq, int status)
87 jbus_reply_error_s(jreq, error_not_found);
89 jbus_reply_s(jreq, "true");
92 static void on_runnables(struct jreq *jreq, struct json_object *obj)
94 struct json_object *resp = af_db_application_list(afdb);
95 jbus_reply_j(jreq, resp);
99 static void on_detail(struct jreq *jreq, struct json_object *obj)
101 const char *appid = getappid(obj);
102 struct json_object *resp = af_db_get_application_public(afdb, appid);
103 reply(jreq, resp, error_not_found);
104 json_object_put(obj);
107 static void on_start(struct jreq *jreq, struct json_object *obj)
110 struct json_object *appli;
114 appid = getappid(obj);
116 jbus_reply_error_s(jreq, error_bad_request);
118 appli = af_db_get_application(afdb, appid);
120 jbus_reply_error_s(jreq, error_not_found);
122 runid = af_run_start(appli);
124 jbus_reply_error_s(jreq, error_cant_start);
126 snprintf(runidstr, sizeof runidstr, "%d", runid);
127 runidstr[sizeof runidstr - 1] = 0;
128 jbus_reply_s(jreq, runidstr);
132 json_object_put(obj);
135 static void on_stop(struct jreq *jreq, struct json_object *obj)
137 int runid = getrunid(obj);
138 int status = af_run_stop(runid);
139 reply_status(jreq, status);
140 json_object_put(obj);
143 static void on_continue(struct jreq *jreq, struct json_object *obj)
145 int runid = getrunid(obj);
146 int status = af_run_continue(runid);
147 reply_status(jreq, status);
148 json_object_put(obj);
151 static void on_terminate(struct jreq *jreq, struct json_object *obj)
153 int runid = getrunid(obj);
154 int status = af_run_terminate(runid);
155 reply_status(jreq, status);
156 json_object_put(obj);
159 static void on_runners(struct jreq *jreq, struct json_object *obj)
161 struct json_object *resp = af_run_list();
162 jbus_reply_j(jreq, resp);
163 json_object_put(resp);
164 json_object_put(obj);
167 static void on_state(struct jreq *jreq, struct json_object *obj)
169 int runid = getrunid(obj);
170 struct json_object *resp = af_run_state(runid);
171 reply(jreq, resp, error_not_found);
172 json_object_put(resp);
173 json_object_put(obj);
176 static int daemonize()
186 int main(int ac, char **av)
192 /* first interpretation of arguments */
193 while ((i = getopt_long(ac, av, "hdqvr:a:", options, NULL)) >= 0) {
213 ERROR("missing argument value");
216 ERROR("unrecognized option");
221 /* init random generator */
222 srandom((unsigned int)time(NULL));
226 ERROR("af_run_init failed");
231 afdb = af_db_create();
233 ERROR("af_create failed");
236 if (af_db_add_root(afdb, FWK_APP_DIR)) {
237 ERROR("can't add root %s", FWK_APP_DIR);
241 /* second interpretation of arguments */
243 while ((i = getopt_long(ac, av, "hdqvr:a:", options, NULL)) >= 0) {
246 if (af_db_add_root(afdb, optarg)) {
247 ERROR("can't add root %s", optarg);
252 if (af_db_add_application(afdb, optarg)) {
253 ERROR("can't add application %s", optarg);
260 /* update the database */
261 if (af_db_update_applications(afdb)) {
262 ERROR("af_update_applications failed");
266 if (daemon && daemonize()) {
267 ERROR("daemonization failed");
272 jbus = create_jbus(1, "/org/AGL/afmMain");
274 ERROR("create_jbus failed");
277 if(jbus_add_service_j(jbus, "runnables", on_runnables)
278 || jbus_add_service_j(jbus, "detail", on_detail)
279 || jbus_add_service_j(jbus, "start", on_start)
280 || jbus_add_service_j(jbus, "terminate", on_terminate)
281 || jbus_add_service_j(jbus, "stop", on_stop)
282 || jbus_add_service_j(jbus, "continue", on_continue)
283 || jbus_add_service_j(jbus, "runners", on_runners)
284 || jbus_add_service_j(jbus, "state", on_state)) {
285 ERROR("adding services failed");
290 if (jbus_start_serving(jbus)) {
291 ERROR("cant start server");
294 while (!jbus_read_write_dispatch(jbus, -1));