f7e039fe6b28637a51cb3867119c761870869360
[src/app-framework-main.git] / src / af-usrd.c
1 /*
2  Copyright 2015 IoT.bzh
3
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
7
8      http://www.apache.org/licenses/LICENSE-2.0
9
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.
15 */
16
17 #include <json.h>
18
19 #include "verbose.h"
20 #include "utils-jbus.h"
21 #include "appfwk.h"
22
23 static struct jbus *jbus;
24 static struct appfwk *appfwk;
25
26 const char error_nothing[] = "[]";
27 const char error_not_found[] = "{\"status\":\"error: not found\"}";
28
29 static const char *getappid(struct json_object *obj)
30 {
31         return json_object_get_string(obj);
32 }
33
34 static const char *getrunid(struct json_object *obj)
35 {
36         return json_object_get_string(obj);
37 }
38
39 static void reply(struct jreq *jreq, struct json_object *resp, const char *errstr)
40 {
41         if (obj)
42                 jbus_reply(jreq, resp);
43         else
44                 jbus_replyj(jreq, errstr);
45 }
46
47 static void on_runnables(struct jreq *jreq, struct json_object *obj)
48 {
49         struct json_object *resp = appfwk_application_list(appfwk);
50         jbus_reply(jreq, resp);
51         json_object_put(obj);
52 }
53
54 static void on_detail(struct jreq *jreq, struct json_object *obj)
55 {
56         const char *appid = getappid(obj);
57         struct json_object *resp = appfwk_get_application_public(appfwk, appid);
58         reply(jreq, resp, error_not_found);
59         json_object_put(obj);
60 }
61
62 static void on_start(struct jreq *jreq, struct json_object *obj)
63 {
64         const char *appid = getappid(obj);
65         const char *runid = appfwk_start(appfwk, appid);
66         jbus_replyj(jreq, runid ? runid : error_not_found);
67         json_object_put(obj);
68 }
69
70 static void on_stop(struct jreq *jreq, struct json_object *obj)
71 {
72         const char *runid = getrunid(obj);
73         int status = appfwk_stop(appfwk, runid);
74         jbus_replyj(jreq, status ? error_not_found : "true");
75         json_object_put(obj);
76 }
77
78 static void on_suspend(struct jreq *jreq, struct json_object *obj)
79 {
80         const char *runid = getrunid(obj);
81         int status = appfwk_suspend(appfwk, runid);
82         jbus_replyj(jreq, status ? error_not_found : "true");
83         json_object_put(obj);
84 }
85
86 static void on_resume(struct jreq *jreq, struct json_object *obj)
87 {
88         const char *runid = getrunid(obj);
89         int status = appfwk_resume(appfwk, runid);
90         jbus_replyj(jreq, status ? error_not_found : "true");
91         json_object_put(obj);
92 }
93
94 static void on_runners(struct jreq *jreq, struct json_object *obj)
95 {
96         struct json_object *resp = appfwk_running_list(appfwk);
97         jbus_reply(jreq, resp);
98         json_object_put(obj);
99 }
100
101 static void on_state(struct jreq *jreq, struct json_object *obj)
102 {
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);
107 }
108
109 int main(int ac, char **av)
110 {
111         LOGAUTH("af-usrd");
112
113         /* init framework */
114         appfwk = appfwk_create();
115         if (!appfwk) {
116                 ERROR("appfwk_create failed");
117                 return 1;
118         }
119         if (appfwk_add_root(appfwk, FWK_APP_DIR)) {
120                 ERROR("can't add root %s", FWK_APP_DIR);
121                 return 1;
122         }
123         if (appfwk_update_applications(appfwk)) {
124                 ERROR("appfwk_update_applications failed");
125                 return 1;
126         }
127
128         /* init service */
129         jbus = create_jbus(1, "/org/automotive/linux/framework");
130         if (!jbus) {
131                 ERROR("create_jbus failed");
132                 return 1;
133         }
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");
143                 return 1;
144         }
145
146         /* start and run */
147         if (jbus_start_serving(jbus)) {
148                 ERROR("cant start server");
149                 return 1;
150         }
151         while (!jbus_read_write_dispatch(jbus, -1));
152         return 0;
153 }
154
155
156