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) */
22 #include <json-c/json.h>
24 #define AFB_BINDING_VERSION 2
25 #include <afb/afb-binding.h>
27 #include "utils-jbus.h"
29 static const char _added_[] = "added";
30 static const char _continue_[] = "continue";
31 static const char _changed_[] = "changed";
32 static const char _detail_[] = "detail";
33 static const char _id_[] = "id";
34 static const char _install_[] = "install";
35 static const char _once_[] = "once";
36 static const char _pause_[] = "pause";
37 static const char _resume_[] = "resume";
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";
47 static struct jbus *jbus;
50 * Structure for asynchronous call handling
54 struct afb_req request; /* the recorded request */
55 const char *method; /* the called method */
59 * Creates the memo for the 'request' and the 'method'.
60 * Returns the memo in case of success.
61 * In case of error, send a failure answer and returns NULL.
63 static struct memo *memo_create(struct afb_req request, const char *method)
65 struct memo *memo = malloc(sizeof *memo);
67 afb_req_fail(request, "failed", "out of memory");
69 memo->request = request;
70 memo->method = method;
71 afb_req_addref(request);
77 * Sends the asynchronous failed reply to the request recorded by 'memo'
78 * Then fress the resources.
80 static void memo_fail(struct memo *memo, const char *info)
82 afb_req_fail(memo->request, "failed", info);
83 afb_req_unref(memo->request);
88 * Sends the asynchronous success reply to the request recorded by 'memo'
89 * Then fress the resources.
91 static void memo_success(struct memo *memo, struct json_object *obj, const char *info)
93 afb_req_success(memo->request, obj, info);
94 afb_req_unref(memo->request);
99 * Broadcast the event "application-list-changed".
100 * This event is sent was the event "changed" is received from dbus.
102 static void application_list_changed(const char *data, void *closure)
104 afb_daemon_broadcast_event("application-list-changed", NULL);
108 * Builds if possible the json object having one field of name 'tag'
109 * whose value is 'obj' like here: { tag: obj } and returns it.
110 * In case of error or when 'tag'==NULL or 'obj'==NULL, 'obj' is returned.
111 * The reference count of 'obj' is not incremented.
113 static struct json_object *embed(const char *tag, struct json_object *obj)
115 struct json_object *result;
117 if (obj == NULL || tag == NULL)
120 result = json_object_new_object();
121 if (result == NULL) {
126 /* TODO why is json-c not returning a status? */
127 json_object_object_add(result, tag, obj);
134 * Callback for replies made by 'embed_call_void'.
136 static void embed_call_void_callback(int iserror, struct json_object *obj, struct memo *memo)
138 AFB_DEBUG("(afm-main-binding) %s(true) -> %s\n", memo->method,
139 obj ? json_object_to_json_string(obj) : "NULL");
142 memo_fail(memo, obj ? json_object_get_string(obj) : "framework daemon failure");
144 memo_success(memo, embed(memo->method, json_object_get(obj)), NULL);
149 * Calls with DBus the 'method' of the user daemon without arguments.
151 static void embed_call_void(struct afb_req request, const char *method)
155 /* store the request */
156 memo = memo_create(request, method);
160 if (jbus_call_sj(jbus, method, "true", (void*)embed_call_void_callback, memo) < 0)
161 memo_fail(memo, "dbus failure");
165 * Callback for replies made by 'call_appid' and 'call_runid'.
167 static void call_xxxid_callback(int iserror, struct json_object *obj, struct memo *memo)
169 AFB_DEBUG("(afm-main-binding) %s -> %s\n", memo->method,
170 obj ? json_object_to_json_string(obj) : "NULL");
173 memo_fail(memo, obj ? json_object_get_string(obj) : "framework daemon failure");
175 memo_success(memo, json_object_get(obj), NULL);
180 * Calls with DBus the 'method' of the user daemon with the argument "id".
182 static void call_appid(struct afb_req request, const char *method)
188 id = afb_req_value(request, _id_);
190 afb_req_fail(request, "bad-request", "missing 'id'");
194 memo = memo_create(request, method);
198 if (asprintf(&sid, "\"%s\"", id) <= 0) {
199 memo_fail(memo, "out of memory");
203 if (jbus_call_sj(jbus, method, sid, (void*)call_xxxid_callback, memo) < 0)
204 memo_fail(memo, "dbus failure");
209 static void call_runid(struct afb_req request, const char *method)
214 id = afb_req_value(request, _runid_);
216 afb_req_fail(request, "bad-request", "missing 'runid'");
220 memo = memo_create(request, method);
224 if (jbus_call_sj(jbus, method, id, (void*)call_xxxid_callback, memo) < 0)
225 memo_fail(memo, "dbus failure");
228 /************************** entries ******************************/
230 static void runnables(struct afb_req request)
232 embed_call_void(request, _runnables_);
235 static void detail(struct afb_req request)
237 call_appid(request, _detail_);
240 static void start_callback(int iserror, struct json_object *obj, struct memo *memo)
242 AFB_DEBUG("(afm-main-binding) %s -> %s\n", memo->method,
243 obj ? json_object_to_json_string(obj) : "NULL");
246 memo_fail(memo, obj ? json_object_get_string(obj) : "framework daemon failure");
248 obj = json_object_get(obj);
249 if (json_object_get_type(obj) == json_type_int)
250 obj = embed(_runid_, obj);
251 memo_success(memo, obj, NULL);
255 static void start(struct afb_req request)
263 id = afb_req_value(request, _id_);
265 afb_req_fail(request, "bad-request", "missing 'id'");
269 /* prepares asynchronous request */
270 memo = memo_create(request, _start_);
274 /* create the query */
275 rc = asprintf(&query, "{\"id\":\"%s\"}", id);
277 memo_fail(memo, "out of memory");
281 /* calls the service asynchronously */
282 if (jbus_call_sj(jbus, _start_, query, (void*)start_callback, memo) < 0)
283 memo_fail(memo, "dbus failure");
287 static void once(struct afb_req request)
289 call_appid(request, _once_);
292 static void terminate(struct afb_req request)
294 call_runid(request, _terminate_);
297 static void pause(struct afb_req request)
299 call_runid(request, _pause_);
302 static void resume(struct afb_req request)
304 call_runid(request, _resume_);
307 static void runners(struct afb_req request)
309 embed_call_void(request, _runners_);
312 static void state(struct afb_req request)
314 call_runid(request, _state_);
317 static void install_callback(int iserror, struct json_object *obj, struct memo *memo)
319 struct json_object *added;
322 memo_fail(memo, obj ? json_object_get_string(obj) : "framework daemon failure");
324 if (json_object_object_get_ex(obj, _added_, &added))
326 obj = json_object_get(obj);
327 obj = embed(_id_, obj);
328 memo_success(memo, obj, NULL);
331 static void install(struct afb_req request)
335 const char *filename;
338 /* get the argument */
339 arg = afb_req_get(request, "widget");
341 if (filename == NULL) {
342 afb_req_fail(request, "bad-request", "missing 'widget' file");
346 /* prepares asynchronous request */
347 memo = memo_create(request, _install_);
351 /* makes the query */
352 if (0 >= asprintf(&query, "\"%s\"", filename)) {
353 afb_req_fail(request, "server-error", "out of memory");
357 /* calls the service asynchronously */
358 if (jbus_call_sj(jbus, _install_, query, (void*)install_callback, memo) < 0)
359 memo_fail(memo, "dbus failure");
363 static void uninstall(struct afb_req request)
365 call_appid(request, _uninstall_);
373 /* creates the jbus for accessing afm-user-daemon */
374 sbus = afb_daemon_get_user_bus();
377 jbus = create_jbus(sbus, "/org/AGL/afm/user");
381 /* records the signal handler */
382 rc = jbus_on_signal_s(jbus, _changed_, application_list_changed, NULL);
391 static const struct afb_verb_v2 verbs[] =
393 {_runnables_, runnables, NULL, "Get list of runnable applications", AFB_SESSION_CHECK },
394 {_detail_ , detail, NULL, "Get the details for one application", AFB_SESSION_CHECK },
395 {_start_ , start, NULL, "Start an application", AFB_SESSION_CHECK },
396 {_once_ , once, NULL, "Start once an application", AFB_SESSION_CHECK },
397 {_terminate_, terminate, NULL, "Terminate a running application", AFB_SESSION_CHECK },
398 {_pause_ , pause, NULL, "Pause a running application", AFB_SESSION_CHECK },
399 {_resume_ , resume, NULL, "Resume a paused application", AFB_SESSION_CHECK },
400 {_stop_ , pause, NULL, "Obsolete since 2016/11/08, use 'pause' instead", AFB_SESSION_CHECK },
401 {_continue_ , resume, NULL, "Obsolete since 2016/11/08, use 'resume' instead", AFB_SESSION_CHECK },
402 {_runners_ , runners, NULL, "Get the list of running applications", AFB_SESSION_CHECK },
403 {_state_ , state, NULL, "Get the state of a running application", AFB_SESSION_CHECK },
404 {_install_ , install, NULL, "Install an application using a widget file", AFB_SESSION_CHECK },
405 {_uninstall_, uninstall, NULL, "Uninstall an application", AFB_SESSION_CHECK },
406 { NULL, NULL, NULL, NULL, 0 }
409 const struct afb_binding_v2 afbBindingV2 = {
411 .specification = NULL,
412 .info = "Application Framework Master Service",