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