in progress (compiles)
[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 #include "appfwk-run.h"
23
24 static struct jbus *jbus;
25 static struct appfwk *appfwk;
26
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\"}";
30
31 static const char *getappid(struct json_object *obj)
32 {
33         return json_object_get_string(obj);
34 }
35
36 static int getrunid(struct json_object *obj)
37 {
38         return json_object_get_int(obj);
39 }
40
41 static void reply(struct jreq *jreq, struct json_object *resp, const char *errstr)
42 {
43         if (resp)
44                 jbus_reply(jreq, resp);
45         else
46                 jbus_replyj(jreq, errstr);
47 }
48
49 static void on_runnables(struct jreq *jreq, struct json_object *obj)
50 {
51         struct json_object *resp = appfwk_application_list(appfwk);
52         jbus_reply(jreq, resp);
53         json_object_put(obj);
54 }
55
56 static void on_detail(struct jreq *jreq, struct json_object *obj)
57 {
58         const char *appid = getappid(obj);
59         struct json_object *resp = appfwk_get_application_public(appfwk, appid);
60         reply(jreq, resp, error_not_found);
61         json_object_put(obj);
62 }
63
64 static void on_start(struct jreq *jreq, struct json_object *obj)
65 {
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);
69         if (runid <= 0) {
70                 
71         jbus_replyj(jreq, runid ? runid : error_not_found);
72         json_object_put(obj);
73 }
74 }
75
76 static void on_stop(struct jreq *jreq, struct json_object *obj)
77 {
78         int runid = getrunid(obj);
79         int status = appfwk_run_stop(runid);
80         jbus_replyj(jreq, status ? error_not_found : "true");
81         json_object_put(obj);
82 }
83
84 static void on_suspend(struct jreq *jreq, struct json_object *obj)
85 {
86         int runid = getrunid(obj);
87         int status = appfwk_run_suspend(runid);
88         jbus_replyj(jreq, status ? error_not_found : "true");
89         json_object_put(obj);
90 }
91
92 static void on_resume(struct jreq *jreq, struct json_object *obj)
93 {
94         int runid = getrunid(obj);
95         int status = appfwk_run_resume(runid);
96         jbus_replyj(jreq, status ? error_not_found : "true");
97         json_object_put(obj);
98 }
99
100 static void on_runners(struct jreq *jreq, struct json_object *obj)
101 {
102         struct json_object *resp = appfwk_run_list();
103         jbus_reply(jreq, resp);
104         json_object_put(resp);
105         json_object_put(obj);
106 }
107
108 static void on_state(struct jreq *jreq, struct json_object *obj)
109 {
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);
115 }
116
117 int main(int ac, char **av)
118 {
119         LOGAUTH("af-usrd");
120
121         /* init framework */
122         appfwk = appfwk_create();
123         if (!appfwk) {
124                 ERROR("appfwk_create failed");
125                 return 1;
126         }
127         if (appfwk_add_root(appfwk, FWK_APP_DIR)) {
128                 ERROR("can't add root %s", FWK_APP_DIR);
129                 return 1;
130         }
131         if (appfwk_update_applications(appfwk)) {
132                 ERROR("appfwk_update_applications failed");
133                 return 1;
134         }
135
136         /* init service */
137         jbus = create_jbus(1, "/org/automotive/linux/framework");
138         if (!jbus) {
139                 ERROR("create_jbus failed");
140                 return 1;
141         }
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, "stop", on_stop)
146         || jbus_add_service(jbus, "suspend", on_suspend)
147         || jbus_add_service(jbus, "resume", on_resume)
148         || jbus_add_service(jbus, "runners", on_runners)
149         || jbus_add_service(jbus, "state", on_state)) {
150                 ERROR("adding services failed");
151                 return 1;
152         }
153
154         /* start and run */
155         if (jbus_start_serving(jbus)) {
156                 ERROR("cant start server");
157                 return 1;
158         }
159         while (!jbus_read_write_dispatch(jbus, -1));
160         return 0;
161 }
162
163
164