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"
23 static struct jbus *jbus;
24 static struct appfwk *appfwk;
26 const char error_nothing[] = "[]";
27 const char error_not_found[] = "{\"status\":\"error: not found\"}";
29 static const char *getappid(struct json_object *obj)
31 return json_object_get_string(obj);
34 static const char *getrunid(struct json_object *obj)
36 return json_object_get_string(obj);
39 static void reply(struct jreq *jreq, struct json_object *resp, const char *errstr)
42 jbus_reply(jreq, resp);
44 jbus_replyj(jreq, errstr);
47 static void on_runnables(struct jreq *jreq, struct json_object *obj)
49 struct json_object *resp = appfwk_application_list(appfwk);
50 jbus_reply(jreq, resp);
54 static void on_detail(struct jreq *jreq, struct json_object *obj)
56 const char *appid = getappid(obj);
57 struct json_object *resp = appfwk_get_application_public(appfwk, appid);
58 reply(jreq, resp, error_not_found);
62 static void on_start(struct jreq *jreq, struct json_object *obj)
64 const char *appid = getappid(obj);
65 const char *runid = appfwk_start(appfwk, appid);
66 jbus_replyj(jreq, runid ? runid : error_not_found);
70 static void on_stop(struct jreq *jreq, struct json_object *obj)
72 const char *runid = getrunid(obj);
73 int status = appfwk_stop(appfwk, runid);
74 jbus_replyj(jreq, status ? error_not_found : "true");
78 static void on_suspend(struct jreq *jreq, struct json_object *obj)
80 const char *runid = getrunid(obj);
81 int status = appfwk_suspend(appfwk, runid);
82 jbus_replyj(jreq, status ? error_not_found : "true");
86 static void on_resume(struct jreq *jreq, struct json_object *obj)
88 const char *runid = getrunid(obj);
89 int status = appfwk_resume(appfwk, runid);
90 jbus_replyj(jreq, status ? error_not_found : "true");
94 static void on_runners(struct jreq *jreq, struct json_object *obj)
96 struct json_object *resp = appfwk_running_list(appfwk);
97 jbus_reply(jreq, resp);
101 static void on_state(struct jreq *jreq, struct json_object *obj)
103 const char *runid = getrunid(obj);
104 int status = appfwk_state(appfwk, runid);
105 jbus_replyj(jreq, status ? error_not_found : "true");
106 json_object_put(obj);
109 int main(int ac, char **av)
114 appfwk = appfwk_create();
116 ERROR("appfwk_create failed");
119 if (appfwk_add_root(appfwk, FWK_APP_DIR)) {
120 ERROR("can't add root %s", FWK_APP_DIR);
123 if (appfwk_update_applications(appfwk)) {
124 ERROR("appfwk_update_applications failed");
129 jbus = create_jbus(1, "/org/automotive/linux/framework");
131 ERROR("create_jbus failed");
134 if(jbus_add_service(jbus, "runnables", on_runnables)
135 || jbus_add_service(jbus, "detail", on_detail)
136 || jbus_add_service(jbus, "start", on_run)
137 || jbus_add_service(jbus, "stop", on_stop)
138 || jbus_add_service(jbus, "suspend", on_suspend)
139 || jbus_add_service(jbus, "resume", on_resume)
140 || jbus_add_service(jbus, "runners", on_runners)
141 || jbus_add_service(jbus, "state", on_state)) {
142 ERROR("adding services failed");
147 if (jbus_start_serving(jbus)) {
148 ERROR("cant start server");
151 while (!jbus_read_write_dispatch(jbus, -1));