aboutsummaryrefslogtreecommitdiffstats
path: root/plugins/afm-main-plugin/afm-main-plugin.c
blob: c6408a5130e078be9424732e8684e1268de717c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/*
 * Copyright (C) 2015 "IoT.bzh"
 * Author "Fulup Ar Foll"
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#define _GNU_SOURCE         /* See feature_test_macros(7) */
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <json.h>

#include "afb-plugin.h"

#include "utils-jbus.h"

static const char _added_[]     = "added";
static const char _auto_[]      = "auto";
static const char _continue_[]  = "continue";
static const char _changed_[]   = "changed";
static const char _detail_[]    = "detail";
static const char _id_[]        = "id";
static const char _install_[]   = "install";
static const char _local_[]     = "local";
static const char _mode_[]      = "mode";
static const char _remote_[]    = "remote";
static const char _runid_[]     = "runid";
static const char _runnables_[] = "runnables";
static const char _runners_[]   = "runners";
static const char _start_[]     = "start";
static const char _state_[]     = "state";
static const char _stop_[]      = "stop";
static const char _terminate_[] = "terminate";
static const char _uninstall_[] = "uninstall";
static const char _uri_[]       = "uri";

static const struct AFB_interface *interface;
static struct afb_evmgr evmgr;

static struct jbus *jbus;

struct memo
{
	struct afb_req request;
	const char *method;
};

static struct memo *make_memo(struct afb_req request, const char *method)
{
	struct memo *memo = malloc(sizeof *memo);
	if (memo != NULL) {
		memo->request = request;
		memo->method = method;
	}
	return memo;
}

static void application_list_changed(const char *data, void *closure)
{
	afb_evmgr_push(evmgr, "application-list-changed", NULL);
}

static struct json_object *embed(const char *tag, struct json_object *obj)
{
	struct json_object *result;

	if (obj == NULL)
		result = NULL;
	else if (!tag)
		result = obj;
	else {
		result = json_object_new_object();
		if (result == NULL) {
			/* can't embed */
			result = obj;
		}
		else {
			/* TODO why is json-c not returning a status? */
			json_object_object_add(result, tag, obj);
		}
	}
	return result;
}

static void embed_call_void_callback(int status, struct json_object *obj, struct memo *memo)
{
	if (interface->verbosity)
		fprintf(stderr, "(afm-main-plugin) %s(true) -> %s\n", memo->method,
			obj ? json_object_to_json_string(obj) : "NULL");
	if (obj == NULL) {
		afb_req_fail(memo->request, "failed", "framework daemon failure");
	} else {
		obj = json_object_get(obj);
		obj = embed(memo->method, obj);
		if (obj == NULL) {
			afb_req_fail(memo->request, "failed", "framework daemon failure");
		} else {
			afb_req_success(memo->request, obj, NULL);
		}
	}
	free(memo);
}

static void embed_call_void(struct afb_req request, const char *method)
{
	struct memo *memo = make_memo(request, method);
	if (memo == NULL)
		afb_req_fail(request, "failed", "out of memory");
	else if (jbus_call_sj(jbus, method, "true", (void*)embed_call_void_callback, memo) < 0) {
		afb_req_fail(request, "failed", "dbus failure");
		free(memo);
	}
}

static void call_appid_callback(int status, struct json_object *obj, struct memo *memo)
{
	if (interface->verbosity)
		fprintf(stderr, "(afm-main-plugin) %s -> %s\n", memo->method, 
			obj ? json_object_to_json_string(obj) : "NULL");
	if (obj == NULL) {
		afb_req_fail(memo->request, "failed", "framework daemon failure");
	} else {
		obj = json_object_get(obj);
		afb_req_success(memo->request, obj, NULL);
	}
	free(memo);
}

static void call_appid(struct afb_req request, const char *method)
{
	struct memo *memo;
	char *sid;
	const char *id = afb_req_value(request, _id_);
	if (id == NULL) {
		afb_req_fail(request, "bad-request", "missing 'id'");
		return;
	}
	memo = make_memo(request, method);
	if (asprintf(&sid, "\"%s\"", id) <= 0 || memo == NULL) {
		afb_req_fail(request, "server-error", "out of memory");
		free(memo);
		return;
	}
	if (jbus_call_sj(jbus, method, sid, (void*)call_appid_callback, memo) < 0) {
		afb_req_fail(request, "failed", "dbus failure");
		free(memo);
	}
	free(sid);
}

static void call_runid(struct afb_req request, const char *method)
{
	struct json_object *obj;
	const char *id = afb_req_value(request, _runid_);
	if (id == NULL) {
		afb_req_fail(request, "bad-request", "missing 'runid'");
		return;
	}
	obj = jbus_call_sj_sync(jbus, method, id);
	if (interface->verbosity)
		fprintf(stderr, "(afm-main-plugin) %s(%s) -> %s\n", method, id,
				obj ? json_object_to_json_string(obj) : "NULL");
	if (obj == NULL) {
		afb_req_fail(request, "failed", "framework daemon failure");
		return;
	}
	obj = json_object_get(obj);
	afb_req_success(request, obj, NULL);
}

/************************** entries ******************************/

static void runnables(struct afb_req request)
{
	embed_call_void(request, _runnables_);
}

static void detail(struct afb_req request)
{
	call_appid(request, _detail_);
}

static void start(struct afb_req request)
{
	struct json_object *obj;
	const char *id, *mode;
	char *query;
	int rc;

	/* get the id */
	id = afb_req_value(request, _id_);
	if (id == NULL) {
		afb_req_fail(request, "bad-request", "missing 'id'");
		return;
	}
	/* get the mode */
	mode = afb_req_value(request, _mode_);
	if (mode == NULL || !strcmp(mode, _auto_)) {
		mode = interface->mode == AFB_MODE_REMOTE ? _remote_ : _local_;
	}

	/* create the query */
	rc = asprintf(&query, "{\"id\":\"%s\",\"mode\":\"%s\"}", id, mode);
	if (rc < 0) {
		afb_req_fail(request, "server-error", "out of memory");
		return;
	}

	/* calls the service */
	obj = jbus_call_sj_sync(jbus, _start_, query);
	if (interface->verbosity)
		fprintf(stderr, "(afm-main-plugin) start(%s) -> %s\n", query,
			obj ? json_object_to_json_string(obj) : "NULL");
	free(query);

	/* check status */
	obj = json_object_get(obj);
	if (obj == NULL) {
		afb_req_fail(request, "failed", "framework daemon failure");
		return;
	}

	/* embed if needed */
	if (json_object_get_type(obj) == json_type_int)
		obj = embed(_runid_, obj);
	afb_req_success(request, obj, NULL);
}

static void terminate(struct afb_req request)
{
	call_runid(request, _terminate_);
}

static void stop(struct afb_req request)
{
	call_runid(request, _stop_);
}

static void continue_(struct afb_req request)
{
	call_runid(request, _continue_);
}

static void runners(struct afb_req request)
{
	embed_call_void(request, _runners_);
}

static void state(struct afb_req request)
{
	call_runid(request, _state_);
}

static void install(struct afb_req request)
{
	struct json_object *obj, *added;
	char *query;
	const char *filename;
	struct afb_arg arg;

	/* get the argument */
	arg = afb_req_get(request, "widget");
	filename = arg.path;
	if (filename == NULL) {
		afb_req_fail(request, "bad-request", "missing 'widget' file");
		return;
	}

	/* makes the query */
	if (0 >= asprintf(&query, "\"%s\"", filename)) {
		afb_req_fail(request, "server-error", "out of memory");
		return;
	}

	obj = jbus_call_sj_sync(jbus, _install_, query);
	if (interface->verbosity)
		fprintf(stderr, "(afm-main-plugin) install(%s) -> %s\n", query,
			obj ? json_object_to_json_string(obj) : "NULL");
	free(query);

	/* check status */
	if (obj == NULL) {
		afb_req_fail(request, "failed", "framework daemon failure");
		return;
	}

	/* embed if needed */
	if (json_object_object_get_ex(obj, _added_, &added))
		obj = added;
	obj = json_object_get(obj);
	obj = embed(_id_, obj);
	afb_req_success(request, obj, NULL);
}

static void uninstall(struct afb_req request)
{
	call_appid(request, _uninstall_);
}

static const struct AFB_restapi plug_apis[] =
{
	{_runnables_, AFB_SESSION_CHECK, runnables,  "Get list of runnable applications"},
	{_detail_   , AFB_SESSION_CHECK, detail, "Get the details for one application"},
	{_start_    , AFB_SESSION_CHECK, start, "Start an application"},
	{_terminate_, AFB_SESSION_CHECK, terminate, "Terminate a running application"},
	{_stop_     , AFB_SESSION_CHECK, stop, "Stop (pause) a running application"},
	{_continue_ , AFB_SESSION_CHECK, continue_, "Continue (resume) a stopped application"},
	{_runners_  , AFB_SESSION_CHECK, runners,  "Get the list of running applications"},
	{_state_    , AFB_SESSION_CHECK, state, "Get the state of a running application"},
	{_install_  , AFB_SESSION_CHECK, install,  "Install an application using a widget file"},
	{_uninstall_, AFB_SESSION_CHECK, uninstall, "Uninstall an application"},
	{ NULL, 0, NULL, NULL }
};

static const struct AFB_plugin plug_desc = {
	.type = AFB_PLUGIN_JSON,
	.info = "Application Framework Master Service",
	.prefix = "afm-main",
	.apis = plug_apis
};

const struct AFB_plugin *pluginRegister(const struct AFB_interface *itf)
{
	int rc;
	struct sd_bus *sbus;

	/* records the interface */
	assert (interface == NULL);
	interface = itf;
	evmgr = afb_daemon_get_evmgr(itf->daemon);

	/* creates the jbus for accessing afm-user-daemon */
	sbus = afb_daemon_get_user_bus(itf->daemon);
	if (sbus == NULL)
		return NULL;
	jbus = create_jbus(sbus, "/org/AGL/afm/user");
        if (jbus == NULL)
		return NULL;

	/* records the signal handler */
	rc = jbus_on_signal_s(jbus, _changed_, application_list_changed, NULL);
	if (rc < 0) {
		jbus_unref(jbus);
		return NULL;
	}

	return &plug_desc;
}