1563e44bb1326c5b101453fe0090469828cbbd16
[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 <stdio.h>
18
19 #include <json.h>
20
21 #include "verbose.h"
22 #include "utils-jbus.h"
23 #include "appfwk.h"
24 #include "appfwk-run.h"
25
26 static struct jbus *jbus;
27 static struct appfwk *appfwk;
28
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\"}";
33
34 static const char *getappid(struct json_object *obj)
35 {
36         return json_type_string == json_object_get_type(obj) ? json_object_get_string(obj) : NULL;
37 }
38
39 static int getrunid(struct json_object *obj)
40 {
41         return json_type_int == json_object_get_type(obj) ? json_object_get_int(obj) : 0;
42 }
43
44 static void reply(struct jreq *jreq, struct json_object *resp, const char *errstr)
45 {
46         if (resp)
47                 jbus_reply(jreq, resp);
48         else
49                 jbus_replyj(jreq, errstr);
50 }
51
52 static void on_runnables(struct jreq *jreq, struct json_object *obj)
53 {
54         struct json_object *resp = appfwk_application_list(appfwk);
55         jbus_reply(jreq, resp);
56         json_object_put(obj);
57 }
58
59 static void on_detail(struct jreq *jreq, struct json_object *obj)
60 {
61         const char *appid = getappid(obj);
62         struct json_object *resp = appfwk_get_application_public(appfwk, appid);
63         reply(jreq, resp, error_not_found);
64         json_object_put(obj);
65 }
66
67 static void on_start(struct jreq *jreq, struct json_object *obj)
68 {
69         const char *appid;
70         struct json_object *appli;
71         int runid;
72         char runidstr[20];
73
74         appid = getappid(obj);
75         if (appid == NULL)
76                 jbus_replyj(jreq, error_bad_request);
77         else {
78                 appli = appfwk_get_application(appfwk, appid);
79                 if (appli == NULL)
80                         jbus_replyj(jreq, error_not_found);
81                 else {
82                         runid = appfwk_run_start(appli);
83                         if (runid <= 0)
84                                 jbus_replyj(jreq, error_cant_start);
85                         else {
86                                 snprintf(runidstr, sizeof runidstr, "%d", runid);
87                                 runidstr[sizeof runidstr - 1] = 0;
88                                 jbus_replyj(jreq, runidstr);
89                         }
90                 }
91         }
92         json_object_put(obj);
93 }
94
95 static void on_stop(struct jreq *jreq, struct json_object *obj)
96 {
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);
101 }
102
103 static void on_continue(struct jreq *jreq, struct json_object *obj)
104 {
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);
109 }
110
111 static void on_terminate(struct jreq *jreq, struct json_object *obj)
112 {
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);
117 }
118
119 static void on_runners(struct jreq *jreq, struct json_object *obj)
120 {
121         struct json_object *resp = appfwk_run_list();
122         jbus_reply(jreq, resp);
123         json_object_put(resp);
124         json_object_put(obj);
125 }
126
127 static void on_state(struct jreq *jreq, struct json_object *obj)
128 {
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);
134 }
135
136 static int daemonize()
137 {
138         int rc = fork();
139         if (rc < 0)
140                 return rc;
141         if (rc)
142                 _exit(0);
143         return 0;
144 }
145
146 int main(int ac, char **av)
147 {
148         LOGAUTH("af-usrd");
149
150         /* init framework */
151         appfwk = appfwk_create();
152         if (!appfwk) {
153                 ERROR("appfwk_create failed");
154                 return 1;
155         }
156         if (appfwk_add_root(appfwk, FWK_APP_DIR)) {
157                 ERROR("can't add root %s", FWK_APP_DIR);
158                 return 1;
159         }
160         if (appfwk_update_applications(appfwk)) {
161                 ERROR("appfwk_update_applications failed");
162                 return 1;
163         }
164
165         /* init service */
166         jbus = create_jbus(1, "/org/automotive/linux/framework");
167         if (!jbus) {
168                 ERROR("create_jbus failed");
169                 return 1;
170         }
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");
180                 return 1;
181         }
182
183         /* start and run */
184         if (jbus_start_serving(jbus)) {
185                 ERROR("cant start server");
186                 return 1;
187         }
188         while (!jbus_read_write_dispatch(jbus, -1));
189         return 0;
190 }
191
192
193