Remove deprecated file afm.h
[src/app-framework-main.git] / src / afm-main-binding.c
1 /*
2  * Copyright (C) 2015, 2016, 2017 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 #define AFB_BINDING_VERSION 2
25 #include <afb/afb-binding.h>
26
27 #include "utils-jbus.h"
28
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";
46
47 static struct jbus *jbus;
48
49 /*
50  * Structure for asynchronous call handling
51  */
52 struct memo
53 {
54         struct afb_req request; /* the recorded request */
55         const char *method;     /* the called method */
56 };
57
58 /*
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.
62  */
63 static struct memo *memo_create(struct afb_req request, const char *method)
64 {
65         struct memo *memo = malloc(sizeof *memo);
66         if (memo == NULL)
67                 afb_req_fail(request, "failed", "out of memory");
68         else {
69                 memo->request = request;
70                 memo->method = method;
71                 afb_req_addref(request);
72         }
73         return memo;
74 }
75
76 /*
77  * Sends the asynchronous failed reply to the request recorded by 'memo'
78  * Then fress the resources.
79  */
80 static void memo_fail(struct memo *memo, const char *info)
81 {
82         afb_req_fail(memo->request, "failed", info);
83         afb_req_unref(memo->request);
84         free(memo);
85 }
86
87 /*
88  * Sends the asynchronous success reply to the request recorded by 'memo'
89  * Then fress the resources.
90  */
91 static void memo_success(struct memo *memo, struct json_object *obj, const char *info)
92 {
93         afb_req_success(memo->request, obj, info);
94         afb_req_unref(memo->request);
95         free(memo);
96 }
97
98 /*
99  * Broadcast the event "application-list-changed".
100  * This event is sent was the event "changed" is received from dbus.
101  */
102 static void application_list_changed(const char *data, void *closure)
103 {
104         afb_daemon_broadcast_event("application-list-changed", NULL);
105 }
106
107 /*
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.
112  */
113 static struct json_object *embed(const char *tag, struct json_object *obj)
114 {
115         struct json_object *result;
116
117         if (obj == NULL || tag == NULL)
118                 result = obj;
119         else {
120                 result = json_object_new_object();
121                 if (result == NULL) {
122                         /* can't embed */
123                         result = obj;
124                 }
125                 else {
126                         /* TODO why is json-c not returning a status? */
127                         json_object_object_add(result, tag, obj);
128                 }
129         }
130         return result;
131 }
132
133 /*
134  * Callback for replies made by 'embed_call_void'.
135  */
136 static void embed_call_void_callback(int iserror, struct json_object *obj, struct memo *memo)
137 {
138         AFB_DEBUG("(afm-main-binding) %s(true) -> %s\n", memo->method,
139                         obj ? json_object_to_json_string(obj) : "NULL");
140
141         if (iserror) {
142                 memo_fail(memo, obj ? json_object_get_string(obj) : "framework daemon failure");
143         } else {
144                 memo_success(memo, embed(memo->method, json_object_get(obj)), NULL);
145         }
146 }
147
148 /*
149  * Calls with DBus the 'method' of the user daemon without arguments.
150  */
151 static void embed_call_void(struct afb_req request, const char *method)
152 {
153         struct memo *memo;
154
155         /* store the request */
156         memo = memo_create(request, method);
157         if (memo == NULL)
158                 return;
159
160         if (jbus_call_sj(jbus, method, "true", (void*)embed_call_void_callback, memo) < 0)
161                 memo_fail(memo, "dbus failure");
162 }
163
164 /*
165  * Callback for replies made by 'call_appid' and 'call_runid'.
166  */
167 static void call_xxxid_callback(int iserror, struct json_object *obj, struct memo *memo)
168 {
169         AFB_DEBUG("(afm-main-binding) %s -> %s\n", memo->method, 
170                         obj ? json_object_to_json_string(obj) : "NULL");
171
172         if (iserror) {
173                 memo_fail(memo, obj ? json_object_get_string(obj) : "framework daemon failure");
174         } else {
175                 memo_success(memo, json_object_get(obj), NULL);
176         }
177 }
178
179 /*
180  * Calls with DBus the 'method' of the user daemon with the argument "id".
181  */
182 static void call_appid(struct afb_req request, const char *method)
183 {
184         struct memo *memo;
185         char *sid;
186         const char *id;
187
188         id = afb_req_value(request, _id_);
189         if (id == NULL) {
190                 afb_req_fail(request, "bad-request", "missing 'id'");
191                 return;
192         }
193
194         memo = memo_create(request, method);
195         if (memo == NULL)
196                 return;
197
198         if (asprintf(&sid, "\"%s\"", id) <= 0) {
199                 memo_fail(memo, "out of memory");
200                 return;
201         }
202
203         if (jbus_call_sj(jbus, method, sid, (void*)call_xxxid_callback, memo) < 0)
204                 memo_fail(memo, "dbus failure");
205
206         free(sid);
207 }
208
209 static void call_runid(struct afb_req request, const char *method)
210 {
211         struct memo *memo;
212         const char *id;
213
214         id = afb_req_value(request, _runid_);
215         if (id == NULL) {
216                 afb_req_fail(request, "bad-request", "missing 'runid'");
217                 return;
218         }
219
220         memo = memo_create(request, method);
221         if (memo == NULL)
222                 return;
223
224         if (jbus_call_sj(jbus, method, id, (void*)call_xxxid_callback, memo) < 0)
225                 memo_fail(memo, "dbus failure");
226 }
227
228 /************************** entries ******************************/
229
230 static void runnables(struct afb_req request)
231 {
232         embed_call_void(request, _runnables_);
233 }
234
235 static void detail(struct afb_req request)
236 {
237         call_appid(request, _detail_);
238 }
239
240 static void start_callback(int iserror, struct json_object *obj, struct memo *memo)
241 {
242         AFB_DEBUG("(afm-main-binding) %s -> %s\n", memo->method, 
243                         obj ? json_object_to_json_string(obj) : "NULL");
244
245         if (iserror) {
246                 memo_fail(memo, obj ? json_object_get_string(obj) : "framework daemon failure");
247         } else {
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);
252         }
253 }
254
255 static void start(struct afb_req request)
256 {
257         struct memo *memo;
258         const char *id;
259         char *query;
260         int rc;
261
262         /* get the id */
263         id = afb_req_value(request, _id_);
264         if (id == NULL) {
265                 afb_req_fail(request, "bad-request", "missing 'id'");
266                 return;
267         }
268
269         /* prepares asynchronous request */
270         memo = memo_create(request, _start_);
271         if (memo == NULL)
272                 return;
273
274         /* create the query */
275         rc = asprintf(&query, "{\"id\":\"%s\"}", id);
276         if (rc < 0) {
277                 memo_fail(memo, "out of memory");
278                 return;
279         }
280
281         /* calls the service asynchronously */
282         if (jbus_call_sj(jbus, _start_, query, (void*)start_callback, memo) < 0)
283                 memo_fail(memo, "dbus failure");
284         free(query);
285 }
286
287 static void once(struct afb_req request)
288 {
289         call_appid(request, _once_);
290 }
291
292 static void terminate(struct afb_req request)
293 {
294         call_runid(request, _terminate_);
295 }
296
297 static void pause(struct afb_req request)
298 {
299         call_runid(request, _pause_);
300 }
301
302 static void resume(struct afb_req request)
303 {
304         call_runid(request, _resume_);
305 }
306
307 static void runners(struct afb_req request)
308 {
309         embed_call_void(request, _runners_);
310 }
311
312 static void state(struct afb_req request)
313 {
314         call_runid(request, _state_);
315 }
316
317 static void install_callback(int iserror, struct json_object *obj, struct memo *memo)
318 {
319         struct json_object *added;
320
321         if (iserror) {
322                 memo_fail(memo, obj ? json_object_get_string(obj) : "framework daemon failure");
323         } else {
324                 if (json_object_object_get_ex(obj, _added_, &added))
325                         obj = added;
326                 obj = json_object_get(obj);
327                 obj = embed(_id_, obj);
328                 memo_success(memo, obj, NULL);
329         }
330 }
331 static void install(struct afb_req request)
332 {
333         struct memo *memo;
334         char *query;
335         const char *filename;
336         struct afb_arg arg;
337
338         /* get the argument */
339         arg = afb_req_get(request, "widget");
340         filename = arg.path;
341         if (filename == NULL) {
342                 afb_req_fail(request, "bad-request", "missing 'widget' file");
343                 return;
344         }
345
346         /* prepares asynchronous request */
347         memo = memo_create(request, _install_);
348         if (memo == NULL)
349                 return;
350
351         /* makes the query */
352         if (0 >= asprintf(&query, "\"%s\"", filename)) {
353                 afb_req_fail(request, "server-error", "out of memory");
354                 return;
355         }
356
357         /* calls the service asynchronously */
358         if (jbus_call_sj(jbus, _install_, query, (void*)install_callback, memo) < 0)
359                 memo_fail(memo, "dbus failure");
360         free(query);
361 }
362
363 static void uninstall(struct afb_req request)
364 {
365         call_appid(request, _uninstall_);
366 }
367
368 static int init()
369 {
370         int rc;
371         struct sd_bus *sbus;
372
373         /* creates the jbus for accessing afm-user-daemon */
374         sbus = afb_daemon_get_user_bus();
375         if (sbus == NULL)
376                 return -1;
377         jbus = create_jbus(sbus, "/org/AGL/afm/user");
378         if (jbus == NULL)
379                 return -1;
380
381         /* records the signal handler */
382         rc = jbus_on_signal_s(jbus, _changed_, application_list_changed, NULL);
383         if (rc < 0) {
384                 jbus_unref(jbus);
385                 return -1;
386         }
387
388         return 0;
389 }
390
391 static const struct afb_verb_v2 verbs[] =
392 {
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 }
407 };
408
409 const struct afb_binding_v2 afbBindingV2 = {
410         .api = "afm-main",
411         .specification = NULL,
412         .info = "Application Framework Master Service",
413         .verbs = verbs,
414         .preinit = NULL,
415         .init = init,
416         .onevent = NULL,
417         .noconcurrency = 0
418 };
419