2 * Copyright (C) 2015, 2016, 2017 IoT.bzh
3 * Author "Fulup Ar Foll"
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 #define _GNU_SOURCE /* See feature_test_macros(7) */
23 #include <json-c/json.h>
25 #define AFB_BINDING_VERSION 2
26 #include <afb/afb-binding.h>
29 #include "utils-systemd.h"
34 #include "wgtpkg-install.h"
35 #include "wgtpkg-uninstall.h"
36 #include "wrap-json.h"
38 static const char _added_[] = "added";
39 static const char _a_l_c_[] = "application-list-changed";
40 static const char _detail_[] = "detail";
41 static const char _id_[] = "id";
42 static const char _install_[] = "install";
43 static const char _not_found_[] = "not-found";
44 static const char _once_[] = "once";
45 static const char _pause_[] = "pause";
46 static const char _resume_[] = "resume";
47 static const char _runid_[] = "runid";
48 static const char _runnables_[] = "runnables";
49 static const char _runners_[] = "runners";
50 static const char _start_[] = "start";
51 static const char _state_[] = "state";
52 static const char _terminate_[] = "terminate";
53 static const char _uninstall_[] = "uninstall";
55 static const char *rootdir = FWK_APP_DIR;
56 static struct afb_event applist_changed_event;
57 static struct afm_udb *afudb;
58 static struct json_object *json_true;
60 static void do_reloads()
62 /* enforce daemon reload */
63 systemd_daemon_reload(0);
64 systemd_unit_restart_name(0, "sockets.target");
67 static void bad_request(struct afb_req req)
69 afb_req_fail(req, "bad-request", NULL);
72 static void not_found(struct afb_req req)
74 afb_req_fail(req, _not_found_, NULL);
77 static void cant_start(struct afb_req req)
79 afb_req_fail(req, "cannot-start", NULL);
83 * Broadcast the event "application-list-changed".
84 * This event is sent was the event "changed" is received from dbus.
86 static void application_list_changed(const char *operation, const char *data)
88 struct json_object *e = NULL;
89 wrap_json_pack(&e, "{ss ss}", "operation", operation, "data", data);
90 afb_event_broadcast(applist_changed_event, e);
94 * retrieves the 'runid' in parameters received with the
95 * request 'req' for the 'method'.
97 * Returns 1 in case of success.
98 * Otherwise, if the 'runid' can't be retrived, an error stating
99 * the bad request is replied for 'req' and 0 is returned.
101 static int onrunid(struct afb_req req, const char *method, int *runid)
103 struct json_object *json;
105 json = afb_req_json(req);
106 if (wrap_json_unpack(json, "s", runid)
107 || wrap_json_unpack(json, "{ss}", "runid", runid)) {
108 INFO("bad request method %s: %s", method,
109 json_object_to_json_string(json));
114 INFO("method %s called for %d", method, *runid);
119 * Sends the reply 'resp' to the request 'req' if 'resp' is not NULLzero.
120 * Otherwise, when 'resp' is NULL replies the error string 'errstr'.
122 static void reply(struct afb_req req, struct json_object *resp, const char *errstr)
125 afb_req_fail(req, errstr, NULL);
127 afb_req_success(req, resp, NULL);
131 * Sends the reply "true" to the request 'req' if 'status' is zero.
132 * Otherwise, when 'status' is not zero replies the error string 'errstr'.
134 static void reply_status(struct afb_req req, int status, const char *errstr)
136 reply(req, status ? NULL : json_object_get(json_true), errstr);
140 * On query "runnables"
142 static void runnables(struct afb_req req)
144 struct json_object *resp;
145 INFO("method runnables called");
146 resp = afm_udb_applications_public(afudb);
147 afb_req_success(req, resp, NULL);
153 static void detail(struct afb_req req)
156 struct json_object *resp, *json;
158 /* scan the request */
159 json = afb_req_json(req);
160 if (wrap_json_unpack(json, "s", &appid)
161 || wrap_json_unpack(json, "{ss}", _id_, &appid)) {
166 /* wants details for appid */
167 INFO("method detail called for %s", appid);
168 resp = afm_udb_get_application_public(afudb, appid);
170 afb_req_success(req, resp, NULL);
178 static void start(struct afb_req req)
181 struct json_object *appli, *resp, *json;
184 /* scan the request */
185 json = afb_req_json(req);
186 if (wrap_json_unpack(json, "s", &appid)
187 || wrap_json_unpack(json, "{ss}", _id_, &appid)) {
192 /* get the application */
193 INFO("method start called for %s", appid);
194 appli = afm_udb_get_application_private(afudb, appid);
200 /* launch the application */
201 runid = afm_urun_start(appli);
209 wrap_json_pack(&resp, "{si}", _runid_, runid);
210 afb_req_success(req, resp, NULL);
216 static void once(struct afb_req req)
219 struct json_object *appli, *resp, *json;
222 /* scan the request */
223 json = afb_req_json(req);
224 if (wrap_json_unpack(json, "s", &appid)
225 || wrap_json_unpack(json, "{ss}", _id_, &appid)) {
230 /* get the application */
231 INFO("method once called for %s", appid);
232 appli = afm_udb_get_application_private(afudb, appid);
238 /* launch the application */
239 runid = afm_urun_once(appli);
245 /* returns the state */
246 resp = afm_urun_state(afudb, runid);
247 afb_req_success(req, resp, NULL);
253 static void pause(struct afb_req req)
256 if (onrunid(req, "pause", &runid)) {
257 status = afm_urun_pause(runid);
258 reply_status(req, status, _not_found_);
263 * On query "resume" from 'smsg' with parameters of 'obj'.
265 static void resume(struct afb_req req)
268 if (onrunid(req, "resume", &runid)) {
269 status = afm_urun_resume(runid);
270 reply_status(req, status, _not_found_);
275 * On query "terminate"
277 static void terminate(struct afb_req req)
280 if (onrunid(req, "terminate", &runid)) {
281 status = afm_urun_terminate(runid);
282 reply_status(req, status, _not_found_);
289 static void runners(struct afb_req req)
291 struct json_object *resp;
292 INFO("method runners called");
293 resp = afm_urun_list(afudb);
294 afb_req_success(req, resp, NULL);
300 static void state(struct afb_req req)
303 struct json_object *resp;
304 if (onrunid(req, "state", &runid)) {
305 resp = afm_urun_state(afudb, runid);
306 reply(req, resp, _not_found_);
355 static void install(struct afb_req req)
361 struct wgt_info *ifo;
362 struct json_object *json;
363 struct json_object *resp;
365 /* default settings */
370 /* scan the request */
371 json = afb_req_json(req);
372 if (wrap_json_unpack(json, "s", &wgtfile)
373 || wrap_json_unpack(json, "{ss s?s s?b s?b}",
377 "reload", &reload)) {
378 return bad_request(req);
381 /* install the widget */
382 ifo = install_widget(wgtfile, root, force);
384 afb_req_fail_f(req, "failed", "installation failed: %m");
386 /* reload if needed */
390 /* build the response */
391 wrap_json_pack(&resp, "{ss}", _added_, wgt_info_desc(ifo)->idaver);
392 afb_req_success(req, resp, NULL);
393 application_list_changed(_install_, wgt_info_desc(ifo)->idaver);
400 static void uninstall(struct afb_req req)
404 struct json_object *json;
407 /* default settings */
410 /* scan the request */
411 json = afb_req_json(req);
412 if (wrap_json_unpack(json, "s", &idaver)
413 || wrap_json_unpack(json, "{ss s?s}",
416 return bad_request(req);
419 /* install the widget */
420 rc = uninstall_widget(idaver, root);
422 afb_req_fail_f(req, "failed", "uninstallation failed: %m");
424 afb_req_success(req, NULL, NULL);
425 application_list_changed(_uninstall_, idaver);
432 afudb = afm_udb_create(1, 1, "afm-appli-");
434 ERROR("afm_udb_create failed");
438 /* set the systemd's buses */
439 systemd_set_bus(0, afb_daemon_get_system_bus());
440 systemd_set_bus(1, afb_daemon_get_user_bus());
443 json_true = json_object_new_boolean(1);
445 /* create the event */
446 applist_changed_event = afb_daemon_make_event(_a_l_c_);
447 return -!afb_event_is_valid(applist_changed_event);
450 static const struct afb_auth
452 .type = afb_auth_Permission,
453 .text = "urn:AGL:permission:afm:system:widget:install"
456 .type = afb_auth_Permission,
457 .text = "urn:AGL:permission:afm:system:widget:uninstall"
461 static const struct afb_verb_v2 verbs[] =
463 {_runnables_, runnables, NULL, "Get list of runnable applications", AFB_SESSION_CHECK_V2 },
464 {_detail_ , detail, NULL, "Get the details for one application", AFB_SESSION_CHECK_V2 },
465 {_start_ , start, NULL, "Start an application", AFB_SESSION_CHECK_V2 },
466 {_once_ , once, NULL, "Start once an application", AFB_SESSION_CHECK_V2 },
467 {_terminate_, terminate, NULL, "Terminate a running application", AFB_SESSION_CHECK_V2 },
468 {_pause_ , pause, NULL, "Pause a running application", AFB_SESSION_CHECK_V2 },
469 {_resume_ , resume, NULL, "Resume a paused application", AFB_SESSION_CHECK_V2 },
470 {_runners_ , runners, NULL, "Get the list of running applications", AFB_SESSION_CHECK_V2 },
471 {_state_ , state, NULL, "Get the state of a running application", AFB_SESSION_CHECK_V2 },
472 {_install_ , install, NULL, "Install an application using a widget file", AFB_SESSION_CHECK_V2 },
473 {_uninstall_, uninstall, NULL, "Uninstall an application", AFB_SESSION_CHECK_V2 },
474 { NULL, NULL, NULL, NULL, 0 }
477 const struct afb_binding_v2 afbBindingV2 = {
479 .specification = NULL,
480 .info = "Application Framework Master Service",
485 .noconcurrency = 1 /* relies on binder for serialisation of requests */