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.
25 #include "utils-jbus.h"
29 static struct jbus *jbus;
30 static struct af_db *afdb;
32 const char error_nothing[] = "[]";
33 const char error_bad_request[] = "{\"status\":\"error: bad request\"}";
34 const char error_not_found[] = "{\"status\":\"error: not found\"}";
35 const char error_cant_start[] = "{\"status\":\"error: can't start\"}";
37 static const char *getappid(struct json_object *obj)
39 return json_type_string == json_object_get_type(obj) ? json_object_get_string(obj) : NULL;
42 static int getrunid(struct json_object *obj)
44 return json_type_int == json_object_get_type(obj) ? json_object_get_int(obj) : 0;
47 static void reply(struct jreq *jreq, struct json_object *resp, const char *errstr)
50 jbus_reply(jreq, resp);
52 jbus_replyj(jreq, errstr);
55 static void on_runnables(struct jreq *jreq, struct json_object *obj)
57 struct json_object *resp = af_db_application_list(afdb);
58 jbus_reply(jreq, resp);
62 static void on_detail(struct jreq *jreq, struct json_object *obj)
64 const char *appid = getappid(obj);
65 struct json_object *resp = af_db_get_application_public(afdb, appid);
66 reply(jreq, resp, error_not_found);
70 static void on_start(struct jreq *jreq, struct json_object *obj)
73 struct json_object *appli;
77 appid = getappid(obj);
79 jbus_replyj(jreq, error_bad_request);
81 appli = af_db_get_application(afdb, appid);
83 jbus_replyj(jreq, error_not_found);
85 runid = af_run_start(appli);
87 jbus_replyj(jreq, error_cant_start);
89 snprintf(runidstr, sizeof runidstr, "%d", runid);
90 runidstr[sizeof runidstr - 1] = 0;
91 jbus_replyj(jreq, runidstr);
98 static void on_stop(struct jreq *jreq, struct json_object *obj)
100 int runid = getrunid(obj);
101 int status = af_run_stop(runid);
102 jbus_replyj(jreq, status ? error_not_found : "true");
103 json_object_put(obj);
106 static void on_continue(struct jreq *jreq, struct json_object *obj)
108 int runid = getrunid(obj);
109 int status = af_run_continue(runid);
110 jbus_replyj(jreq, status ? error_not_found : "true");
111 json_object_put(obj);
114 static void on_terminate(struct jreq *jreq, struct json_object *obj)
116 int runid = getrunid(obj);
117 int status = af_run_terminate(runid);
118 jbus_replyj(jreq, status ? error_not_found : "true");
119 json_object_put(obj);
122 static void on_runners(struct jreq *jreq, struct json_object *obj)
124 struct json_object *resp = af_run_list();
125 jbus_reply(jreq, resp);
126 json_object_put(resp);
127 json_object_put(obj);
130 static void on_state(struct jreq *jreq, struct json_object *obj)
132 int runid = getrunid(obj);
133 struct json_object *resp = af_run_state(runid);
134 reply(jreq, resp, error_not_found);
135 json_object_put(resp);
136 json_object_put(obj);
139 static int daemonize()
149 int main(int ac, char **av)
151 LOGAUTH("afdb-usrd");
153 /* init random generator */
154 srandom((unsigned int)time(NULL));
158 ERROR("af_run_init failed");
163 afdb = af_db_create();
165 ERROR("af_create failed");
168 if (af_db_add_root(afdb, FWK_APP_DIR)) {
169 ERROR("can't add root %s", FWK_APP_DIR);
172 if (af_db_update_applications(afdb)) {
173 ERROR("af_update_applications failed");
178 jbus = create_jbus(1, "/org/AGL/framework");
180 ERROR("create_jbus failed");
183 if(jbus_add_service(jbus, "runnables", on_runnables)
184 || jbus_add_service(jbus, "detail", on_detail)
185 || jbus_add_service(jbus, "start", on_start)
186 || jbus_add_service(jbus, "terminate", on_terminate)
187 || jbus_add_service(jbus, "stop", on_stop)
188 || jbus_add_service(jbus, "continue", on_continue)
189 || jbus_add_service(jbus, "runners", on_runners)
190 || jbus_add_service(jbus, "state", on_state)) {
191 ERROR("adding services failed");
196 if (jbus_start_serving(jbus)) {
197 ERROR("cant start server");
200 while (!jbus_read_write_dispatch(jbus, -1));