binding: report errors correctly
[src/app-framework-main.git] / src / afm-main-binding.c
1 /*
2  * Copyright (C) 2015, 2016 "IoT.bzh"
3  * Author "Fulup Ar Foll"
4  *
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
8  *
9  *   http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
18 #define _GNU_SOURCE         /* See feature_test_macros(7) */
19 #include <stdio.h>
20 #include <string.h>
21 #include <assert.h>
22 #include <json-c/json.h>
23
24 #include <afb/afb-binding.h>
25
26 #include "utils-jbus.h"
27
28 static const char _added_[]     = "added";
29 static const char _auto_[]      = "auto";
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 _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";
47
48 static const struct afb_binding_interface *binder;
49
50 static struct jbus *jbus;
51
52 /*
53  * Structure for asynchronous call handling
54  */
55 struct memo
56 {
57         struct afb_req request; /* the recorded request */
58         const char *method;     /* the called method */
59 };
60
61 /*
62  * Creates the memo for the 'request' and the 'method'.
63  * Returns the memo in case of success.
64  * In case of error, send a failure answer and returns NULL.
65  */
66 static struct memo *memo_create(struct afb_req request, const char *method)
67 {
68         struct memo *memo = malloc(sizeof *memo);
69         if (memo == NULL)
70                 afb_req_fail(request, "failed", "out of memory");
71         else {
72                 memo->request = request;
73                 memo->method = method;
74                 afb_req_addref(request);
75         }
76         return memo;
77 }
78
79 /*
80  * Sends the asynchronous failed reply to the request recorded by 'memo'
81  * Then fress the resources.
82  */
83 static void memo_fail(struct memo *memo, const char *info)
84 {
85         afb_req_fail(memo->request, "failed", info);
86         afb_req_unref(memo->request);
87         free(memo);
88 }
89
90 /*
91  * Sends the asynchronous success reply to the request recorded by 'memo'
92  * Then fress the resources.
93  */
94 static void memo_success(struct memo *memo, struct json_object *obj, const char *info)
95 {
96         afb_req_success(memo->request, obj, info);
97         afb_req_unref(memo->request);
98         free(memo);
99 }
100
101 /*
102  * Broadcast the event "application-list-changed".
103  * This event is sent was the event "changed" is received from dbus.
104  */
105 static void application_list_changed(const char *data, void *closure)
106 {
107         afb_daemon_broadcast_event(binder->daemon, "application-list-changed", NULL);
108 }
109
110 /*
111  * Builds if possible the json object having one field of name 'tag'
112  * whose value is 'obj' like here: { tag: obj } and returns it.
113  * In case of error or when 'tag'==NULL or 'obj'==NULL, 'obj' is returned.
114  * The reference count of 'obj' is not incremented.
115  */
116 static struct json_object *embed(const char *tag, struct json_object *obj)
117 {
118         struct json_object *result;
119
120         if (obj == NULL || tag == NULL)
121                 result = obj;
122         else {
123                 result = json_object_new_object();
124                 if (result == NULL) {
125                         /* can't embed */
126                         result = obj;
127                 }
128                 else {
129                         /* TODO why is json-c not returning a status? */
130                         json_object_object_add(result, tag, obj);
131                 }
132         }
133         return result;
134 }
135
136 /*
137  * Callback for replies made by 'embed_call_void'.
138  */
139 static void embed_call_void_callback(int iserror, struct json_object *obj, struct memo *memo)
140 {
141         DEBUG(binder, "(afm-main-binding) %s(true) -> %s\n", memo->method,
142                         obj ? json_object_to_json_string(obj) : "NULL");
143
144         if (iserror) {
145                 memo_fail(memo, obj ? json_object_get_string(obj) : "framework daemon failure");
146         } else {
147                 memo_success(memo, embed(memo->method, json_object_get(obj)), NULL);
148         }
149 }
150
151 /*
152  * Calls with DBus the 'method' of the user daemon without arguments.
153  */
154 static void embed_call_void(struct afb_req request, const char *method)
155 {
156         struct memo *memo;
157
158         /* store the request */
159         memo = memo_create(request, method);
160         if (memo == NULL)
161                 return;
162
163         if (jbus_call_sj(jbus, method, "true", (void*)embed_call_void_callback, memo) < 0)
164                 memo_fail(memo, "dbus failure");
165 }
166
167 /*
168  * Callback for replies made by 'call_appid' and 'call_runid'.
169  */
170 static void call_xxxid_callback(int iserror, struct json_object *obj, struct memo *memo)
171 {
172         DEBUG(binder, "(afm-main-binding) %s -> %s\n", memo->method, 
173                         obj ? json_object_to_json_string(obj) : "NULL");
174
175         if (iserror) {
176                 memo_fail(memo, obj ? json_object_get_string(obj) : "framework daemon failure");
177         } else {
178                 memo_success(memo, json_object_get(obj), NULL);
179         }
180 }
181
182 /*
183  * Calls with DBus the 'method' of the user daemon with the argument "id".
184  */
185 static void call_appid(struct afb_req request, const char *method)
186 {
187         struct memo *memo;
188         char *sid;
189         const char *id;
190
191         id = afb_req_value(request, _id_);
192         if (id == NULL) {
193                 afb_req_fail(request, "bad-request", "missing 'id'");
194                 return;
195         }
196
197         memo = memo_create(request, method);
198         if (memo == NULL)
199                 return;
200
201         if (asprintf(&sid, "\"%s\"", id) <= 0) {
202                 memo_fail(memo, "out of memory");
203                 return;
204         }
205
206         if (jbus_call_sj(jbus, method, sid, (void*)call_xxxid_callback, memo) < 0)
207                 memo_fail(memo, "dbus failure");
208
209         free(sid);
210 }
211
212 static void call_runid(struct afb_req request, const char *method)
213 {
214         struct memo *memo;
215         const char *id;
216
217         id = afb_req_value(request, _runid_);
218         if (id == NULL) {
219                 afb_req_fail(request, "bad-request", "missing 'runid'");
220                 return;
221         }
222
223         memo = memo_create(request, method);
224         if (memo == NULL)
225                 return;
226
227         if (jbus_call_sj(jbus, method, id, (void*)call_xxxid_callback, memo) < 0)
228                 memo_fail(memo, "dbus failure");
229 }
230
231 /************************** entries ******************************/
232
233 static void runnables(struct afb_req request)
234 {
235         embed_call_void(request, _runnables_);
236 }
237
238 static void detail(struct afb_req request)
239 {
240         call_appid(request, _detail_);
241 }
242
243 static void start_callback(int iserror, struct json_object *obj, struct memo *memo)
244 {
245         DEBUG(binder, "(afm-main-binding) %s -> %s\n", memo->method, 
246                         obj ? json_object_to_json_string(obj) : "NULL");
247
248         if (iserror) {
249                 memo_fail(memo, obj ? json_object_get_string(obj) : "framework daemon failure");
250         } else {
251                 obj = json_object_get(obj);
252                 if (json_object_get_type(obj) == json_type_int)
253                         obj = embed(_runid_, obj);
254                 memo_success(memo, obj, NULL);
255         }
256 }
257
258 static void start(struct afb_req request)
259 {
260         struct memo *memo;
261         const char *id, *mode;
262         char *query;
263         int rc;
264
265         /* get the id */
266         id = afb_req_value(request, _id_);
267         if (id == NULL) {
268                 afb_req_fail(request, "bad-request", "missing 'id'");
269                 return;
270         }
271
272         /* get the mode */
273         mode = afb_req_value(request, _mode_);
274         if (mode == NULL || !strcmp(mode, _auto_)) {
275                 mode = binder->mode == AFB_MODE_REMOTE ? _remote_ : _local_;
276         }
277
278         /* prepares asynchronous request */
279         memo = memo_create(request, _start_);
280         if (memo == NULL)
281                 return;
282
283         /* create the query */
284         rc = asprintf(&query, "{\"id\":\"%s\",\"mode\":\"%s\"}", id, mode);
285         if (rc < 0) {
286                 memo_fail(memo, "out of memory");
287                 return;
288         }
289
290         /* calls the service asynchronously */
291         if (jbus_call_sj(jbus, _start_, query, (void*)start_callback, memo) < 0)
292                 memo_fail(memo, "dbus failure");
293         free(query);
294 }
295
296 static void terminate(struct afb_req request)
297 {
298         call_runid(request, _terminate_);
299 }
300
301 static void stop(struct afb_req request)
302 {
303         call_runid(request, _stop_);
304 }
305
306 static void continue_(struct afb_req request)
307 {
308         call_runid(request, _continue_);
309 }
310
311 static void runners(struct afb_req request)
312 {
313         embed_call_void(request, _runners_);
314 }
315
316 static void state(struct afb_req request)
317 {
318         call_runid(request, _state_);
319 }
320
321 static void install_callback(int iserror, struct json_object *obj, struct memo *memo)
322 {
323         struct json_object *added;
324
325         if (iserror) {
326                 memo_fail(memo, obj ? json_object_get_string(obj) : "framework daemon failure");
327         } else {
328                 if (json_object_object_get_ex(obj, _added_, &added))
329                         obj = added;
330                 obj = json_object_get(obj);
331                 obj = embed(_id_, obj);
332                 memo_success(memo, obj, NULL);
333         }
334 }
335 static void install(struct afb_req request)
336 {
337         struct memo *memo;
338         char *query;
339         const char *filename;
340         struct afb_arg arg;
341
342         /* get the argument */
343         arg = afb_req_get(request, "widget");
344         filename = arg.path;
345         if (filename == NULL) {
346                 afb_req_fail(request, "bad-request", "missing 'widget' file");
347                 return;
348         }
349
350         /* prepares asynchronous request */
351         memo = memo_create(request, _install_);
352         if (memo == NULL)
353                 return;
354
355         /* makes the query */
356         if (0 >= asprintf(&query, "\"%s\"", filename)) {
357                 afb_req_fail(request, "server-error", "out of memory");
358                 return;
359         }
360
361         /* calls the service asynchronously */
362         if (jbus_call_sj(jbus, _install_, query, (void*)install_callback, memo) < 0)
363                 memo_fail(memo, "dbus failure");
364         free(query);
365 }
366
367 static void uninstall(struct afb_req request)
368 {
369         call_appid(request, _uninstall_);
370 }
371
372 static const struct afb_verb_desc_v1 verbs[] =
373 {
374         {_runnables_, AFB_SESSION_CHECK, runnables,  "Get list of runnable applications"},
375         {_detail_   , AFB_SESSION_CHECK, detail, "Get the details for one application"},
376         {_start_    , AFB_SESSION_CHECK, start, "Start an application"},
377         {_terminate_, AFB_SESSION_CHECK, terminate, "Terminate a running application"},
378         {_stop_     , AFB_SESSION_CHECK, stop, "Stop (pause) a running application"},
379         {_continue_ , AFB_SESSION_CHECK, continue_, "Continue (resume) a stopped application"},
380         {_runners_  , AFB_SESSION_CHECK, runners,  "Get the list of running applications"},
381         {_state_    , AFB_SESSION_CHECK, state, "Get the state of a running application"},
382         {_install_  , AFB_SESSION_CHECK, install,  "Install an application using a widget file"},
383         {_uninstall_, AFB_SESSION_CHECK, uninstall, "Uninstall an application"},
384         { NULL, 0, NULL, NULL }
385 };
386
387 static const struct afb_binding plug_desc = {
388         .type = AFB_BINDING_VERSION_1,
389         .v1 = {
390                 .info = "Application Framework Master Service",
391                 .prefix = "afm-main",
392                 .verbs = verbs
393         }
394 };
395
396 const struct afb_binding *afbBindingV1Register(const struct afb_binding_interface *itf)
397 {
398         int rc;
399         struct sd_bus *sbus;
400
401         /* records the interface */
402         assert (binder == NULL);
403         binder = itf;
404
405         /* creates the jbus for accessing afm-user-daemon */
406         sbus = afb_daemon_get_user_bus(itf->daemon);
407         if (sbus == NULL)
408                 return NULL;
409         jbus = create_jbus(sbus, "/org/AGL/afm/user");
410         if (jbus == NULL)
411                 return NULL;
412
413         /* records the signal handler */
414         rc = jbus_on_signal_s(jbus, _changed_, application_list_changed, NULL);
415         if (rc < 0) {
416                 jbus_unref(jbus);
417                 return NULL;
418         }
419
420         return &plug_desc;
421 }
422