4 author: José Bollo <jose.bollo@iot.bzh>
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
10 http://www.apache.org/licenses/LICENSE-2.0
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
26 #include "utils-jbus.h"
30 static struct jbus *jbus;
31 static struct af_db *afdb;
33 const char error_nothing[] = "[]";
34 const char error_bad_request[] = "bad request";
35 const char error_not_found[] = "not found";
36 const char error_cant_start[] = "can't start";
38 static const char *getappid(struct json_object *obj)
40 return json_type_string == json_object_get_type(obj) ? json_object_get_string(obj) : NULL;
43 static int getrunid(struct json_object *obj)
45 return json_type_int == json_object_get_type(obj) ? json_object_get_int(obj) : 0;
48 static void reply(struct jreq *jreq, struct json_object *resp, const char *errstr)
51 jbus_reply_j(jreq, resp);
53 jbus_reply_error_s(jreq, errstr);
56 static void reply_status(struct jreq *jreq, int status)
59 jbus_reply_error_s(jreq, error_not_found);
61 jbus_reply_s(jreq, "true");
64 static void on_runnables(struct jreq *jreq, struct json_object *obj)
66 struct json_object *resp = af_db_application_list(afdb);
67 jbus_reply_j(jreq, resp);
71 static void on_detail(struct jreq *jreq, struct json_object *obj)
73 const char *appid = getappid(obj);
74 struct json_object *resp = af_db_get_application_public(afdb, appid);
75 reply(jreq, resp, error_not_found);
79 static void on_start(struct jreq *jreq, struct json_object *obj)
82 struct json_object *appli;
86 appid = getappid(obj);
88 jbus_reply_error_s(jreq, error_bad_request);
90 appli = af_db_get_application(afdb, appid);
92 jbus_reply_error_s(jreq, error_not_found);
94 runid = af_run_start(appli);
96 jbus_reply_error_s(jreq, error_cant_start);
98 snprintf(runidstr, sizeof runidstr, "%d", runid);
99 runidstr[sizeof runidstr - 1] = 0;
100 jbus_reply_s(jreq, runidstr);
104 json_object_put(obj);
107 static void on_stop(struct jreq *jreq, struct json_object *obj)
109 int runid = getrunid(obj);
110 int status = af_run_stop(runid);
111 reply_status(jreq, status);
112 json_object_put(obj);
115 static void on_continue(struct jreq *jreq, struct json_object *obj)
117 int runid = getrunid(obj);
118 int status = af_run_continue(runid);
119 reply_status(jreq, status);
120 json_object_put(obj);
123 static void on_terminate(struct jreq *jreq, struct json_object *obj)
125 int runid = getrunid(obj);
126 int status = af_run_terminate(runid);
127 reply_status(jreq, status);
128 json_object_put(obj);
131 static void on_runners(struct jreq *jreq, struct json_object *obj)
133 struct json_object *resp = af_run_list();
134 jbus_reply_j(jreq, resp);
135 json_object_put(resp);
136 json_object_put(obj);
139 static void on_state(struct jreq *jreq, struct json_object *obj)
141 int runid = getrunid(obj);
142 struct json_object *resp = af_run_state(runid);
143 reply(jreq, resp, error_not_found);
144 json_object_put(resp);
145 json_object_put(obj);
148 static int daemonize()
158 int main(int ac, char **av)
160 LOGAUTH("afm-main-daemon");
162 /* init random generator */
163 srandom((unsigned int)time(NULL));
167 ERROR("af_run_init failed");
172 afdb = af_db_create();
174 ERROR("af_create failed");
177 if (af_db_add_root(afdb, FWK_APP_DIR)) {
178 ERROR("can't add root %s", FWK_APP_DIR);
181 if (af_db_update_applications(afdb)) {
182 ERROR("af_update_applications failed");
187 jbus = create_jbus(1, "/org/AGL/afmMain");
189 ERROR("create_jbus failed");
192 if(jbus_add_service_j(jbus, "runnables", on_runnables)
193 || jbus_add_service_j(jbus, "detail", on_detail)
194 || jbus_add_service_j(jbus, "start", on_start)
195 || jbus_add_service_j(jbus, "terminate", on_terminate)
196 || jbus_add_service_j(jbus, "stop", on_stop)
197 || jbus_add_service_j(jbus, "continue", on_continue)
198 || jbus_add_service_j(jbus, "runners", on_runners)
199 || jbus_add_service_j(jbus, "state", on_state)) {
200 ERROR("adding services failed");
205 if (jbus_start_serving(jbus)) {
206 ERROR("cant start server");
209 while (!jbus_read_write_dispatch(jbus, -1));