786468a199c257f09c46456b2450bd91b220f6e8
[src/app-framework-main.git] / src / afm-main-plugin.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-plugin.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_interface *afb_interface;
49 static struct afb_event_sender event_sender;
50
51 static struct jbus *jbus;
52
53 struct memo
54 {
55         struct afb_req request;
56         const char *method;
57 };
58
59 static struct memo *make_memo(struct afb_req request, const char *method)
60 {
61         struct memo *memo = malloc(sizeof *memo);
62         if (memo != NULL) {
63                 memo->request = request;
64                 memo->method = method;
65                 afb_req_addref(request);
66         }
67         return memo;
68 }
69
70 static void application_list_changed(const char *data, void *closure)
71 {
72         afb_event_sender_push(event_sender, "application-list-changed", NULL);
73 }
74
75 static struct json_object *embed(const char *tag, struct json_object *obj)
76 {
77         struct json_object *result;
78
79         if (obj == NULL)
80                 result = NULL;
81         else if (!tag)
82                 result = obj;
83         else {
84                 result = json_object_new_object();
85                 if (result == NULL) {
86                         /* can't embed */
87                         result = obj;
88                 }
89                 else {
90                         /* TODO why is json-c not returning a status? */
91                         json_object_object_add(result, tag, obj);
92                 }
93         }
94         return result;
95 }
96
97 static void embed_call_void_callback(int status, struct json_object *obj, struct memo *memo)
98 {
99         if (afb_interface->verbosity)
100                 fprintf(stderr, "(afm-main-plugin) %s(true) -> %s\n", memo->method,
101                         obj ? json_object_to_json_string(obj) : "NULL");
102         if (obj == NULL) {
103                 afb_req_fail(memo->request, "failed", "framework daemon failure");
104         } else {
105                 obj = json_object_get(obj);
106                 obj = embed(memo->method, obj);
107                 if (obj == NULL) {
108                         afb_req_fail(memo->request, "failed", "framework daemon failure");
109                 } else {
110                         afb_req_success(memo->request, obj, NULL);
111                 }
112         }
113         afb_req_unref(memo->request);
114         free(memo);
115 }
116
117 static void embed_call_void(struct afb_req request, const char *method)
118 {
119         struct memo *memo = make_memo(request, method);
120         if (memo == NULL)
121                 afb_req_fail(request, "failed", "out of memory");
122         else if (jbus_call_sj(jbus, method, "true", (void*)embed_call_void_callback, memo) < 0) {
123                 afb_req_fail(request, "failed", "dbus failure");
124                 free(memo);
125         }
126 }
127
128 static void call_appid_callback(int status, struct json_object *obj, struct memo *memo)
129 {
130         if (afb_interface->verbosity)
131                 fprintf(stderr, "(afm-main-plugin) %s -> %s\n", memo->method, 
132                         obj ? json_object_to_json_string(obj) : "NULL");
133         if (obj == NULL) {
134                 afb_req_fail(memo->request, "failed", "framework daemon failure");
135         } else {
136                 obj = json_object_get(obj);
137                 afb_req_success(memo->request, obj, NULL);
138         }
139         afb_req_unref(memo->request);
140         free(memo);
141 }
142
143 static void call_appid(struct afb_req request, const char *method)
144 {
145         struct memo *memo;
146         char *sid;
147         const char *id = afb_req_value(request, _id_);
148         if (id == NULL) {
149                 afb_req_fail(request, "bad-request", "missing 'id'");
150                 return;
151         }
152         memo = make_memo(request, method);
153         if (asprintf(&sid, "\"%s\"", id) <= 0 || memo == NULL) {
154                 afb_req_fail(request, "server-error", "out of memory");
155                 free(memo);
156                 return;
157         }
158         if (jbus_call_sj(jbus, method, sid, (void*)call_appid_callback, memo) < 0) {
159                 afb_req_fail(request, "failed", "dbus failure");
160                 free(memo);
161         }
162         free(sid);
163 }
164
165 static void call_runid(struct afb_req request, const char *method)
166 {
167         struct json_object *obj;
168         const char *id = afb_req_value(request, _runid_);
169         if (id == NULL) {
170                 afb_req_fail(request, "bad-request", "missing 'runid'");
171                 return;
172         }
173         obj = jbus_call_sj_sync(jbus, method, id);
174         if (afb_interface->verbosity)
175                 fprintf(stderr, "(afm-main-plugin) %s(%s) -> %s\n", method, id,
176                                 obj ? json_object_to_json_string(obj) : "NULL");
177         if (obj == NULL) {
178                 afb_req_fail(request, "failed", "framework daemon failure");
179                 return;
180         }
181         obj = json_object_get(obj);
182         afb_req_success(request, obj, NULL);
183 }
184
185 /************************** entries ******************************/
186
187 static void runnables(struct afb_req request)
188 {
189         embed_call_void(request, _runnables_);
190 }
191
192 static void detail(struct afb_req request)
193 {
194         call_appid(request, _detail_);
195 }
196
197 static void start(struct afb_req request)
198 {
199         struct json_object *obj;
200         const char *id, *mode;
201         char *query;
202         int rc;
203
204         /* get the id */
205         id = afb_req_value(request, _id_);
206         if (id == NULL) {
207                 afb_req_fail(request, "bad-request", "missing 'id'");
208                 return;
209         }
210         /* get the mode */
211         mode = afb_req_value(request, _mode_);
212         if (mode == NULL || !strcmp(mode, _auto_)) {
213                 mode = afb_interface->mode == AFB_MODE_REMOTE ? _remote_ : _local_;
214         }
215
216         /* create the query */
217         rc = asprintf(&query, "{\"id\":\"%s\",\"mode\":\"%s\"}", id, mode);
218         if (rc < 0) {
219                 afb_req_fail(request, "server-error", "out of memory");
220                 return;
221         }
222
223         /* calls the service */
224         obj = jbus_call_sj_sync(jbus, _start_, query);
225         if (afb_interface->verbosity)
226                 fprintf(stderr, "(afm-main-plugin) start(%s) -> %s\n", query,
227                         obj ? json_object_to_json_string(obj) : "NULL");
228         free(query);
229
230         /* check status */
231         obj = json_object_get(obj);
232         if (obj == NULL) {
233                 afb_req_fail(request, "failed", "framework daemon failure");
234                 return;
235         }
236
237         /* embed if needed */
238         if (json_object_get_type(obj) == json_type_int)
239                 obj = embed(_runid_, obj);
240         afb_req_success(request, obj, NULL);
241 }
242
243 static void terminate(struct afb_req request)
244 {
245         call_runid(request, _terminate_);
246 }
247
248 static void stop(struct afb_req request)
249 {
250         call_runid(request, _stop_);
251 }
252
253 static void continue_(struct afb_req request)
254 {
255         call_runid(request, _continue_);
256 }
257
258 static void runners(struct afb_req request)
259 {
260         embed_call_void(request, _runners_);
261 }
262
263 static void state(struct afb_req request)
264 {
265         call_runid(request, _state_);
266 }
267
268 static void install(struct afb_req request)
269 {
270         struct json_object *obj, *added;
271         char *query;
272         const char *filename;
273         struct afb_arg arg;
274
275         /* get the argument */
276         arg = afb_req_get(request, "widget");
277         filename = arg.path;
278         if (filename == NULL) {
279                 afb_req_fail(request, "bad-request", "missing 'widget' file");
280                 return;
281         }
282
283         /* makes the query */
284         if (0 >= asprintf(&query, "\"%s\"", filename)) {
285                 afb_req_fail(request, "server-error", "out of memory");
286                 return;
287         }
288
289         obj = jbus_call_sj_sync(jbus, _install_, query);
290         if (afb_interface->verbosity)
291                 fprintf(stderr, "(afm-main-plugin) install(%s) -> %s\n", query,
292                         obj ? json_object_to_json_string(obj) : "NULL");
293         free(query);
294
295         /* check status */
296         if (obj == NULL) {
297                 afb_req_fail(request, "failed", "framework daemon failure");
298                 return;
299         }
300
301         /* embed if needed */
302         if (json_object_object_get_ex(obj, _added_, &added))
303                 obj = added;
304         obj = json_object_get(obj);
305         obj = embed(_id_, obj);
306         afb_req_success(request, obj, NULL);
307 }
308
309 static void uninstall(struct afb_req request)
310 {
311         call_appid(request, _uninstall_);
312 }
313
314 static const struct AFB_verb_desc_v1 verbs[] =
315 {
316         {_runnables_, AFB_SESSION_CHECK, runnables,  "Get list of runnable applications"},
317         {_detail_   , AFB_SESSION_CHECK, detail, "Get the details for one application"},
318         {_start_    , AFB_SESSION_CHECK, start, "Start an application"},
319         {_terminate_, AFB_SESSION_CHECK, terminate, "Terminate a running application"},
320         {_stop_     , AFB_SESSION_CHECK, stop, "Stop (pause) a running application"},
321         {_continue_ , AFB_SESSION_CHECK, continue_, "Continue (resume) a stopped application"},
322         {_runners_  , AFB_SESSION_CHECK, runners,  "Get the list of running applications"},
323         {_state_    , AFB_SESSION_CHECK, state, "Get the state of a running application"},
324         {_install_  , AFB_SESSION_CHECK, install,  "Install an application using a widget file"},
325         {_uninstall_, AFB_SESSION_CHECK, uninstall, "Uninstall an application"},
326         { NULL, 0, NULL, NULL }
327 };
328
329 static const struct AFB_plugin plug_desc = {
330         .type = AFB_PLUGIN_VERSION_1,
331         .v1 = {
332                 .info = "Application Framework Master Service",
333                 .prefix = "afm-main",
334                 .verbs = verbs
335         }
336 };
337
338 const struct AFB_plugin *pluginAfbV1Register(const struct AFB_interface *itf)
339 {
340         int rc;
341         struct sd_bus *sbus;
342
343         /* records the interface */
344         assert (afb_interface == NULL);
345         afb_interface = itf;
346         event_sender = afb_daemon_get_event_sender(itf->daemon);
347
348         /* creates the jbus for accessing afm-user-daemon */
349         sbus = afb_daemon_get_user_bus(itf->daemon);
350         if (sbus == NULL)
351                 return NULL;
352         jbus = create_jbus(sbus, "/org/AGL/afm/user");
353         if (jbus == NULL)
354                 return NULL;
355
356         /* records the signal handler */
357         rc = jbus_on_signal_s(jbus, _changed_, application_list_changed, NULL);
358         if (rc < 0) {
359                 jbus_unref(jbus);
360                 return NULL;
361         }
362
363         return &plug_desc;
364 }
365