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.
22 #include "utils-jbus.h"
24 #include "appfwk-run.h"
26 static struct jbus *jbus;
27 static struct appfwk *appfwk;
29 const char error_nothing[] = "[]";
30 const char error_bad_request[] = "{\"status\":\"error: bad request\"}";
31 const char error_not_found[] = "{\"status\":\"error: not found\"}";
32 const char error_cant_start[] = "{\"status\":\"error: can't start\"}";
34 static const char *getappid(struct json_object *obj)
36 return json_type_string == json_object_get_type(obj) ? json_object_get_string(obj) : NULL;
39 static int getrunid(struct json_object *obj)
41 return json_type_int == json_object_get_type(obj) ? json_object_get_int(obj) : 0;
44 static void reply(struct jreq *jreq, struct json_object *resp, const char *errstr)
47 jbus_reply(jreq, resp);
49 jbus_replyj(jreq, errstr);
52 static void on_runnables(struct jreq *jreq, struct json_object *obj)
54 struct json_object *resp = appfwk_application_list(appfwk);
55 jbus_reply(jreq, resp);
59 static void on_detail(struct jreq *jreq, struct json_object *obj)
61 const char *appid = getappid(obj);
62 struct json_object *resp = appfwk_get_application_public(appfwk, appid);
63 reply(jreq, resp, error_not_found);
67 static void on_start(struct jreq *jreq, struct json_object *obj)
70 struct json_object *appli;
74 appid = getappid(obj);
76 jbus_replyj(jreq, error_bad_request);
78 appli = appfwk_get_application(appfwk, appid);
80 jbus_replyj(jreq, error_not_found);
82 runid = appfwk_run_start(appli);
84 jbus_replyj(jreq, error_cant_start);
86 snprintf(runidstr, sizeof runidstr, "%d", runid);
87 runidstr[sizeof runidstr - 1] = 0;
88 jbus_replyj(jreq, runidstr);
95 static void on_stop(struct jreq *jreq, struct json_object *obj)
97 int runid = getrunid(obj);
98 int status = appfwk_run_stop(runid);
99 jbus_replyj(jreq, status ? error_not_found : "true");
100 json_object_put(obj);
103 static void on_continue(struct jreq *jreq, struct json_object *obj)
105 int runid = getrunid(obj);
106 int status = appfwk_run_continue(runid);
107 jbus_replyj(jreq, status ? error_not_found : "true");
108 json_object_put(obj);
111 static void on_terminate(struct jreq *jreq, struct json_object *obj)
113 int runid = getrunid(obj);
114 int status = appfwk_run_terminate(runid);
115 jbus_replyj(jreq, status ? error_not_found : "true");
116 json_object_put(obj);
119 static void on_runners(struct jreq *jreq, struct json_object *obj)
121 struct json_object *resp = appfwk_run_list();
122 jbus_reply(jreq, resp);
123 json_object_put(resp);
124 json_object_put(obj);
127 static void on_state(struct jreq *jreq, struct json_object *obj)
129 int runid = getrunid(obj);
130 struct json_object *resp = appfwk_run_state(runid);
131 reply(jreq, resp, error_not_found);
132 json_object_put(resp);
133 json_object_put(obj);
136 static int daemonize()
146 int main(int ac, char **av)
151 appfwk = appfwk_create();
153 ERROR("appfwk_create failed");
156 if (appfwk_add_root(appfwk, FWK_APP_DIR)) {
157 ERROR("can't add root %s", FWK_APP_DIR);
160 if (appfwk_update_applications(appfwk)) {
161 ERROR("appfwk_update_applications failed");
166 jbus = create_jbus(1, "/org/automotive/linux/framework");
168 ERROR("create_jbus failed");
171 if(jbus_add_service(jbus, "runnables", on_runnables)
172 || jbus_add_service(jbus, "detail", on_detail)
173 || jbus_add_service(jbus, "start", on_start)
174 || jbus_add_service(jbus, "terminate", on_terminate)
175 || jbus_add_service(jbus, "stop", on_stop)
176 || jbus_add_service(jbus, "continue", on_continue)
177 || jbus_add_service(jbus, "runners", on_runners)
178 || jbus_add_service(jbus, "state", on_state)) {
179 ERROR("adding services failed");
184 if (jbus_start_serving(jbus)) {
185 ERROR("cant start server");
188 while (!jbus_read_write_dispatch(jbus, -1));