changing the license to apache 2
[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 <json.h>
22
23 #include "afb-plugin.h"
24 #include "afb-req-itf.h"
25 #include "afb-poll-itf.h"
26
27 #include "utils-sbus.h"
28 #include "utils-jbus.h"
29
30 static const char _auto_[]      = "auto";
31 static const char _continue_[]  = "continue";
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
50 static struct jbus *jbus;
51
52 static struct json_object *embed(const char *tag, struct json_object *obj)
53 {
54         struct json_object *result;
55
56         if (obj == NULL)
57                 result = NULL;
58         else if (!tag)
59                 result = obj;
60         else {
61                 result = json_object_new_object();
62                 if (result == NULL) {
63                         /* can't embed */
64                         result = obj;
65                 }
66                 else {
67                         /* TODO why is json-c not returning a status? */
68                         json_object_object_add(result, tag, obj);
69                 }
70         }
71         return result;
72 }
73
74 static void embed_call_void(struct afb_req request, const char *method)
75 {
76         struct json_object *obj = jbus_call_sj_sync(jbus, method, "true");
77         if (interface->verbosity)
78                 fprintf(stderr, "(afm-main-plugin) %s(true) -> %s\n", method, obj ? json_object_to_json_string(obj) : "NULL");
79         if (obj == NULL) {
80                 afb_req_fail(request, "failed", "framework daemon failure");
81                 return;
82         }
83         obj = embed(method, obj);
84         if (obj == NULL) {
85                 afb_req_fail(request, "failed", "framework daemon failure");
86                 return;
87         }
88         afb_req_success(request, obj, NULL);
89 }
90
91 static void call_appid(struct afb_req request, const char *method)
92 {
93         struct json_object *obj;
94         char *sid;
95         const char *id = afb_req_value(request, _id_);
96         if (id == NULL) {
97                 afb_req_fail(request, "bad-request", "missing 'id'");
98                 return;
99         }
100         if (asprintf(&sid, "\"%s\"", id) <= 0) {
101                 afb_req_fail(request, "server-error", "out of memory");
102                 return;
103         }
104         obj = jbus_call_sj_sync(jbus, method, sid);
105         if (interface->verbosity)
106                 fprintf(stderr, "(afm-main-plugin) %s(%s) -> %s\n", method, sid, obj ? json_object_to_json_string(obj) : "NULL");
107         free(sid);
108         if (obj == NULL) {
109                 afb_req_fail(request, "failed", "framework daemon failure");
110                 return;
111         }
112         afb_req_success(request, obj, NULL);
113 }
114
115 static void call_runid(struct afb_req request, const char *method)
116 {
117         struct json_object *obj;
118         const char *id = afb_req_value(request, _runid_);
119         if (id == NULL) {
120                 afb_req_fail(request, "bad-request", "missing 'runid'");
121                 return;
122         }
123         obj = jbus_call_sj_sync(jbus, method, id);
124         if (interface->verbosity)
125                 fprintf(stderr, "(afm-main-plugin) %s(%s) -> %s\n", method, id,
126                                 obj ? json_object_to_json_string(obj) : "NULL");
127         if (obj == NULL) {
128                 afb_req_fail(request, "failed", "framework daemon failure");
129                 return;
130         }
131         afb_req_success(request, obj, NULL);
132 }
133
134
135 /************************** entries ******************************/
136
137 static void runnables(struct afb_req request)
138 {
139         embed_call_void(request, _runnables_);
140 }
141
142 static void detail(struct afb_req request)
143 {
144         call_appid(request, _detail_);
145 }
146
147 static void start(struct afb_req request)
148 {
149         struct json_object *obj;
150         const char *id, *mode;
151         char *query;
152         int rc;
153
154         /* get the id */
155         id = afb_req_value(request, _id_);
156         if (id == NULL) {
157                 afb_req_fail(request, "bad-request", "missing 'id'");
158                 return;
159         }
160         /* get the mode */
161         mode = afb_req_value(request, _mode_);
162         if (mode == NULL || !strcmp(mode, _auto_)) {
163                 mode = interface->mode == AFB_MODE_REMOTE ? _remote_ : _local_;
164         }
165
166         /* create the query */
167         rc = asprintf(&query, "{\"id\":\"%s\",\"mode\":\"%s\"}", id, mode);
168         if (rc < 0) {
169                 afb_req_fail(request, "server-error", "out of memory");
170                 return;
171         }
172
173         /* calls the service */
174         obj = jbus_call_sj_sync(jbus, _start_, query);
175         if (interface->verbosity)
176                 fprintf(stderr, "(afm-main-plugin) start(%s) -> %s\n", query, obj ? json_object_to_json_string(obj) : "NULL");
177         free(query);
178
179         /* check status */
180         if (obj == NULL) {
181                 afb_req_fail(request, "failed", "framework daemon failure");
182                 return;
183         }
184
185         /* embed if needed */
186         if (json_object_get_type(obj) == json_type_int)
187                 obj = embed(_runid_, obj);
188         afb_req_success(request, obj, NULL);
189 }
190
191 static void terminate(struct afb_req request)
192 {
193         call_runid(request, _terminate_);
194 }
195
196 static void stop(struct afb_req request)
197 {
198         call_runid(request, _stop_);
199 }
200
201 static void continue_(struct afb_req request)
202 {
203         call_runid(request, _continue_);
204 }
205
206 static void runners(struct afb_req request)
207 {
208         embed_call_void(request, _runners_);
209 }
210
211 static void state(struct afb_req request)
212 {
213         call_runid(request, _state_);
214 }
215
216 static void install(struct afb_req request)
217 {
218         struct json_object *obj;
219         char *query;
220         const char *filename;
221         struct afb_arg arg;
222
223         /* get the argument */
224         arg = afb_req_get(request, "widget");
225         filename = arg.path;
226         if (filename == NULL) {
227                 afb_req_fail(request, "bad-request", "missing 'widget' file");
228                 return;
229         }
230
231         /* makes the query */
232         if (0 >= asprintf(&query, "\"%s\"", filename)) {
233                 afb_req_fail(request, "server-error", "out of memory");
234                 return;
235         }
236
237         obj = jbus_call_sj_sync(jbus, _install_, query);
238         if (interface->verbosity)
239                 fprintf(stderr, "(afm-main-plugin) install(%s) -> %s\n", query, obj ? json_object_to_json_string(obj) : "NULL");
240         free(query);
241
242         /* check status */
243         if (obj == NULL) {
244                 afb_req_fail(request, "failed", "framework daemon failure");
245                 return;
246         }
247
248         /* embed if needed */
249         obj = embed(_id_, obj);
250         afb_req_success(request, obj, NULL);
251 }
252
253 static void uninstall(struct afb_req request)
254 {
255         call_appid(request, _uninstall_);
256 }
257
258 static const struct AFB_restapi plug_apis[] =
259 {
260         {_runnables_, AFB_SESSION_CHECK, runnables,  "Get list of runnable applications"},
261         {_detail_   , AFB_SESSION_CHECK, detail, "Get the details for one application"},
262         {_start_    , AFB_SESSION_CHECK, start, "Start an application"},
263         {_terminate_, AFB_SESSION_CHECK, terminate, "Terminate a running application"},
264         {_stop_     , AFB_SESSION_CHECK, stop, "Stop (pause) a running application"},
265         {_continue_ , AFB_SESSION_CHECK, continue_, "Continue (resume) a stopped application"},
266         {_runners_  , AFB_SESSION_CHECK, runners,  "Get the list of running applications"},
267         {_state_    , AFB_SESSION_CHECK, state, "Get the state of a running application"},
268         {_install_  , AFB_SESSION_CHECK, install,  "Install an application using a widget file"},
269         {_uninstall_, AFB_SESSION_CHECK, uninstall, "Uninstall an application"},
270         { NULL, 0, NULL, NULL }
271 };
272
273 static const struct AFB_plugin plug_desc = {
274         .type = AFB_PLUGIN_JSON,
275         .info = "Application Framework Master Service",
276         .prefix = "afm-main",
277         .apis = plug_apis
278 };
279
280 static struct sbus_itf sbusitf;
281
282 const struct AFB_plugin *pluginRegister(const struct AFB_interface *itf)
283 {
284         struct sbus *sbus;
285
286         if (interface != NULL)
287                 return NULL;
288
289         interface = itf;
290         sbusitf.wait = itf->pollitf->wait;
291         sbusitf.open = itf->pollitf->open;
292         sbusitf.on_readable = itf->pollitf->on_readable;
293         sbusitf.on_writable = itf->pollitf->on_writable;
294         sbusitf.on_hangup = itf->pollitf->on_hangup;
295         sbusitf.close = itf->pollitf->close;
296
297         sbus = sbus_session(&sbusitf, itf->pollclosure);
298         if (sbus == NULL) {
299                 fprintf(stderr, "ERROR: %s:%d: can't connect to DBUS session\n", __FILE__, __LINE__);
300                 return NULL;
301         }
302
303         jbus = create_jbus(sbus, "/org/AGL/afm/user");
304         if (jbus)
305                 return &plug_desc;
306         sbus_unref(sbus);
307         return NULL;
308 }
309