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.
20 #include "utils-jbus.h"
22 #include "appfwk-run.h"
24 static struct jbus *jbus;
25 static struct appfwk *appfwk;
27 const char error_nothing[] = "[]";
28 const char error_bad_request[] = "{\"status\":\"error: bad request\"}";
29 const char error_not_found[] = "{\"status\":\"error: not found\"}";
31 static const char *getappid(struct json_object *obj)
33 return json_object_get_string(obj);
36 static int getrunid(struct json_object *obj)
38 return json_object_get_int(obj);
41 static void reply(struct jreq *jreq, struct json_object *resp, const char *errstr)
44 jbus_reply(jreq, resp);
46 jbus_replyj(jreq, errstr);
49 static void on_runnables(struct jreq *jreq, struct json_object *obj)
51 struct json_object *resp = appfwk_application_list(appfwk);
52 jbus_reply(jreq, resp);
56 static void on_detail(struct jreq *jreq, struct json_object *obj)
58 const char *appid = getappid(obj);
59 struct json_object *resp = appfwk_get_application_public(appfwk, appid);
60 reply(jreq, resp, error_not_found);
64 static void on_start(struct jreq *jreq, struct json_object *obj)
66 const char *appid = getappid(obj);
67 struct json_object *appli = appid ? appfwk_get_application_public(appfwk, appid) : NULL;
68 int runid = appfwk_run_start(appli);
71 jbus_replyj(jreq, runid ? runid : error_not_found);
76 static void on_terminate(struct jreq *jreq, struct json_object *obj)
78 int runid = getrunid(obj);
79 int status = appfwk_run_terminate(runid);
80 jbus_replyj(jreq, status ? error_not_found : "true");
84 static void on_stop(struct jreq *jreq, struct json_object *obj)
86 int runid = getrunid(obj);
87 int status = appfwk_run_stop(runid);
88 jbus_replyj(jreq, status ? error_not_found : "true");
92 static void on_continue(struct jreq *jreq, struct json_object *obj)
94 int runid = getrunid(obj);
95 int status = appfwk_run_continue(runid);
96 jbus_replyj(jreq, status ? error_not_found : "true");
100 static void on_runners(struct jreq *jreq, struct json_object *obj)
102 struct json_object *resp = appfwk_run_list();
103 jbus_reply(jreq, resp);
104 json_object_put(resp);
105 json_object_put(obj);
108 static void on_state(struct jreq *jreq, struct json_object *obj)
110 int runid = getrunid(obj);
111 struct json_object *resp = appfwk_run_state(runid);
112 reply(jreq, resp, error_not_found);
113 json_object_put(obj);
114 json_object_put(resp);
117 int main(int ac, char **av)
122 appfwk = appfwk_create();
124 ERROR("appfwk_create failed");
127 if (appfwk_add_root(appfwk, FWK_APP_DIR)) {
128 ERROR("can't add root %s", FWK_APP_DIR);
131 if (appfwk_update_applications(appfwk)) {
132 ERROR("appfwk_update_applications failed");
137 jbus = create_jbus(1, "/org/automotive/linux/framework");
139 ERROR("create_jbus failed");
142 if(jbus_add_service(jbus, "runnables", on_runnables)
143 || jbus_add_service(jbus, "detail", on_detail)
144 || jbus_add_service(jbus, "start", on_start)
145 || jbus_add_service(jbus, "terminate", on_terminate)
146 || jbus_add_service(jbus, "stop", on_stop)
147 || jbus_add_service(jbus, "continue", on_continue)
148 || jbus_add_service(jbus, "runners", on_runners)
149 || jbus_add_service(jbus, "state", on_state)) {
150 ERROR("adding services failed");
155 if (jbus_start_serving(jbus)) {
156 ERROR("cant start server");
159 while (!jbus_read_write_dispatch(jbus, -1));