2 * Copyright (C) 2015 "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 "afb-plugin.h"
24 #include "afb-req-itf.h"
25 #include "afb-poll-itf.h"
27 #include "utils-sbus.h"
28 #include "utils-jbus.h"
30 static const char _auto_[] = "auto";
31 static const char _continue_[] = "continue";
32 static const char _detail_[] = "detail";
33 static const char _id_[] = "id";
34 static const char _install_[] = "install";
35 static const char _local_[] = "local";
36 static const char _mode_[] = "mode";
37 static const char _remote_[] = "remote";
38 static const char _runid_[] = "runid";
39 static const char _runnables_[] = "runnables";
40 static const char _runners_[] = "runners";
41 static const char _start_[] = "start";
42 static const char _state_[] = "state";
43 static const char _stop_[] = "stop";
44 static const char _terminate_[] = "terminate";
45 static const char _uninstall_[] = "uninstall";
46 static const char _uri_[] = "uri";
48 static const struct AFB_interface *interface;
50 static struct jbus *jbus;
52 static struct json_object *embed(const char *tag, struct json_object *obj)
54 struct json_object *result;
61 result = json_object_new_object();
67 /* TODO why is json-c not returning a status? */
68 json_object_object_add(result, tag, obj);
74 static void embed_call_void(struct afb_req request, const char *method)
76 struct json_object *obj = jbus_call_sj_sync(jbus, method, "true");
77 if (interface->verbosity)
78 fprintf(stderr, "(afm-main-plugin) %s(true) -> %s\n", method,
79 obj ? json_object_to_json_string(obj) : "NULL");
81 afb_req_fail(request, "failed", "framework daemon failure");
84 obj = embed(method, obj);
86 afb_req_fail(request, "failed", "framework daemon failure");
89 afb_req_success(request, obj, NULL);
92 static void call_appid(struct afb_req request, const char *method)
94 struct json_object *obj;
96 const char *id = afb_req_value(request, _id_);
98 afb_req_fail(request, "bad-request", "missing 'id'");
101 if (asprintf(&sid, "\"%s\"", id) <= 0) {
102 afb_req_fail(request, "server-error", "out of memory");
105 obj = jbus_call_sj_sync(jbus, method, sid);
106 if (interface->verbosity)
107 fprintf(stderr, "(afm-main-plugin) %s(%s) -> %s\n", method, sid,
108 obj ? json_object_to_json_string(obj) : "NULL");
111 afb_req_fail(request, "failed", "framework daemon failure");
114 afb_req_success(request, obj, NULL);
117 static void call_runid(struct afb_req request, const char *method)
119 struct json_object *obj;
120 const char *id = afb_req_value(request, _runid_);
122 afb_req_fail(request, "bad-request", "missing 'runid'");
125 obj = jbus_call_sj_sync(jbus, method, id);
126 if (interface->verbosity)
127 fprintf(stderr, "(afm-main-plugin) %s(%s) -> %s\n", method, id,
128 obj ? json_object_to_json_string(obj) : "NULL");
130 afb_req_fail(request, "failed", "framework daemon failure");
133 afb_req_success(request, obj, NULL);
137 /************************** entries ******************************/
139 static void runnables(struct afb_req request)
141 embed_call_void(request, _runnables_);
144 static void detail(struct afb_req request)
146 call_appid(request, _detail_);
149 static void start(struct afb_req request)
151 struct json_object *obj;
152 const char *id, *mode;
157 id = afb_req_value(request, _id_);
159 afb_req_fail(request, "bad-request", "missing 'id'");
163 mode = afb_req_value(request, _mode_);
164 if (mode == NULL || !strcmp(mode, _auto_)) {
165 mode = interface->mode == AFB_MODE_REMOTE ? _remote_ : _local_;
168 /* create the query */
169 rc = asprintf(&query, "{\"id\":\"%s\",\"mode\":\"%s\"}", id, mode);
171 afb_req_fail(request, "server-error", "out of memory");
175 /* calls the service */
176 obj = jbus_call_sj_sync(jbus, _start_, query);
177 if (interface->verbosity)
178 fprintf(stderr, "(afm-main-plugin) start(%s) -> %s\n", query,
179 obj ? json_object_to_json_string(obj) : "NULL");
184 afb_req_fail(request, "failed", "framework daemon failure");
188 /* embed if needed */
189 if (json_object_get_type(obj) == json_type_int)
190 obj = embed(_runid_, obj);
191 afb_req_success(request, obj, NULL);
194 static void terminate(struct afb_req request)
196 call_runid(request, _terminate_);
199 static void stop(struct afb_req request)
201 call_runid(request, _stop_);
204 static void continue_(struct afb_req request)
206 call_runid(request, _continue_);
209 static void runners(struct afb_req request)
211 embed_call_void(request, _runners_);
214 static void state(struct afb_req request)
216 call_runid(request, _state_);
219 static void install(struct afb_req request)
221 struct json_object *obj;
223 const char *filename;
226 /* get the argument */
227 arg = afb_req_get(request, "widget");
229 if (filename == NULL) {
230 afb_req_fail(request, "bad-request", "missing 'widget' file");
234 /* makes the query */
235 if (0 >= asprintf(&query, "\"%s\"", filename)) {
236 afb_req_fail(request, "server-error", "out of memory");
240 obj = jbus_call_sj_sync(jbus, _install_, query);
241 if (interface->verbosity)
242 fprintf(stderr, "(afm-main-plugin) install(%s) -> %s\n", query,
243 obj ? json_object_to_json_string(obj) : "NULL");
248 afb_req_fail(request, "failed", "framework daemon failure");
252 /* embed if needed */
253 obj = embed(_id_, obj);
254 afb_req_success(request, obj, NULL);
257 static void uninstall(struct afb_req request)
259 call_appid(request, _uninstall_);
262 static const struct AFB_restapi plug_apis[] =
264 {_runnables_, AFB_SESSION_CHECK, runnables, "Get list of runnable applications"},
265 {_detail_ , AFB_SESSION_CHECK, detail, "Get the details for one application"},
266 {_start_ , AFB_SESSION_CHECK, start, "Start an application"},
267 {_terminate_, AFB_SESSION_CHECK, terminate, "Terminate a running application"},
268 {_stop_ , AFB_SESSION_CHECK, stop, "Stop (pause) a running application"},
269 {_continue_ , AFB_SESSION_CHECK, continue_, "Continue (resume) a stopped application"},
270 {_runners_ , AFB_SESSION_CHECK, runners, "Get the list of running applications"},
271 {_state_ , AFB_SESSION_CHECK, state, "Get the state of a running application"},
272 {_install_ , AFB_SESSION_CHECK, install, "Install an application using a widget file"},
273 {_uninstall_, AFB_SESSION_CHECK, uninstall, "Uninstall an application"},
274 { NULL, 0, NULL, NULL }
277 static const struct AFB_plugin plug_desc = {
278 .type = AFB_PLUGIN_JSON,
279 .info = "Application Framework Master Service",
280 .prefix = "afm-main",
284 static struct sbus_itf sbusitf;
286 const struct AFB_plugin *pluginRegister(const struct AFB_interface *itf)
290 /* records the interface */
291 assert (interface == NULL);
293 /* creates the sbus for session */
294 sbusitf.wait = itf->pollitf->wait;
295 sbusitf.open = itf->pollitf->open;
296 sbusitf.on_readable = itf->pollitf->on_readable;
297 sbusitf.on_writable = itf->pollitf->on_writable;
298 sbusitf.on_hangup = itf->pollitf->on_hangup;
299 sbusitf.close = itf->pollitf->close;
300 sbus = sbus_session(&sbusitf, itf->pollclosure);
302 fprintf(stderr, "ERROR: %s:%d: can't connect to DBUS session\n", __FILE__, __LINE__);
306 /* creates the jbus for accessing afm-user-daemon */
307 jbus = create_jbus(sbus, "/org/AGL/afm/user");