Blob: bip.c

Blob id: ec75fdd67a8116c19e6d4e5397368776555fe89f

Size: 10.4 KB

  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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
 *
 *  OBEX Client
 *
 *  Copyright (C) 2024  Collabora Ltd.
 *
 *
 */

#define _GNU_SOURCE
#include <errno.h>
#include <stdlib.h>
#include <string.h>

#include "gdbus/gdbus.h"
#include "gobex/gobex.h"

#include "obexd/src/log.h"
#include "obexd/src/obexd.h"

#include "transfer.h"
#include "session.h"
#include "driver.h"
#include "bip-common.h"
#include "bip.h"

#define OBEX_BIP_AVRCP_UUID \
	"\x71\x63\xDD\x54\x4A\x7E\x11\xE2\xB4\x7C\x00\x50\xC2\x49\x00\x48"
#define OBEX_BIP_AVRCP_UUID_LEN 16

#define IMAGE_INTERFACE "org.bluez.obex.Image1"
#define ERROR_INTERFACE "org.bluez.obex.Error"
#define IMAGE_UUID "0000111A-0000-1000-8000-00805f9b34fb"

#define IMG_HANDLE_TAG  0x30
#define IMG_DESC_TAG    0x71

#define EOL_CHARS "\n"
#define IMG_DESC_BEGIN "<image-descriptor version=\"1.0\">" EOL_CHARS
#define IMG_BEGIN "<image encoding=\"%s\" pixel=\"%s\""
#define IMG_TRANSFORM " transformation=\"%s\""
#define IMG_END "/>" EOL_CHARS
#define IMG_DESC_END "</image-descriptor>" EOL_CHARS

static DBusConnection *conn;

struct bip_avrcp_data {
	struct obc_session *session;
};

static void image_properties_complete_cb(struct obc_session *session,
						struct obc_transfer *transfer,
						GError *err, void *user_data)
{
	DBusMessage *message = user_data;
	DBusMessage *reply = NULL;
	DBusMessageIter iter;
	char *contents = NULL;
	size_t size;
	int perr;
	struct prop_object *prop = NULL;

	if (err != NULL) {
		reply = g_dbus_create_error(message,
					ERROR_INTERFACE ".Failed",
					"%s", err->message);
		goto done;
	}

	perr = obc_transfer_get_contents(transfer, &contents, &size);
	if (perr < 0) {
		reply = g_dbus_create_error(message,
						ERROR_INTERFACE ".Failed",
						"Error reading contents: %s",
						strerror(-perr));
		goto done;
	}

	prop = parse_properties(contents, size, &perr);
	if (prop == NULL) {
		reply = g_dbus_create_error(message,
						ERROR_INTERFACE ".Failed",
						"Error parsing contents: %s",
						strerror(-perr));
		goto done;
	}

	if (!verify_properties(prop)) {
		reply = g_dbus_create_error(message,
						ERROR_INTERFACE ".Failed",
						"Error verifying contents");
		goto done;
	}

	reply = dbus_message_new_method_return(message);
	dbus_message_iter_init_append(reply, &iter);
	append_properties(&iter, prop);

done:
	g_dbus_send_message(conn, reply);
	g_free(contents);
	dbus_message_unref(message);
}

static DBusMessage *get_image_properties(DBusConnection *connection,
					DBusMessage *message, void *user_data)
{
	struct bip_avrcp_data *bip_avrcp = user_data;
	const char *handle = NULL;
	struct obc_transfer *transfer;
	GObexHeader *header;
	DBusMessage *reply = NULL;
	GError *err = NULL;

	DBG("");

	if (dbus_message_get_args(message, NULL,
				DBUS_TYPE_STRING, &handle,
				DBUS_TYPE_INVALID) == FALSE) {
		reply = g_dbus_create_error(message,
				ERROR_INTERFACE ".InvalidArguments", NULL);
		return reply;
	}

	transfer = obc_transfer_get("x-bt/img-properties", NULL, NULL, &err);
	if (transfer == NULL)
		goto fail;

	header = g_obex_header_new_unicode(IMG_HANDLE_TAG, handle);
	obc_transfer_add_header(transfer, header);

	if (!obc_session_queue(bip_avrcp->session, transfer,
			image_properties_complete_cb, message, &err))
		goto fail;

	dbus_message_ref(message);

	return NULL;

fail:
	reply = g_dbus_create_error(message, ERROR_INTERFACE ".Failed", "%s",
								err->message);
	g_error_free(err);
	return reply;
}

static DBusMessage *get_thumbnail(DBusConnection *connection,
					DBusMessage *message, void *user_data)
{
	struct bip_avrcp_data *bip_avrcp = user_data;
	const char *handle = NULL, *image_path = NULL;
	struct obc_transfer *transfer;
	GObexHeader *header;
	DBusMessage *reply = NULL;
	GError *err = NULL;

	DBG("");

	if (dbus_message_get_args(message, NULL,
				DBUS_TYPE_STRING, &image_path,
				DBUS_TYPE_STRING, &handle,
				DBUS_TYPE_INVALID) == FALSE) {
		reply = g_dbus_create_error(message,
				ERROR_INTERFACE ".InvalidArguments", NULL);
		return reply;
	}

	transfer = obc_transfer_get("x-bt/img-thm", NULL, image_path, &err);
	if (transfer == NULL)
		goto fail;

	header = g_obex_header_new_unicode(IMG_HANDLE_TAG, handle);
	obc_transfer_add_header(transfer, header);

	if (!obc_session_queue(bip_avrcp->session, transfer, NULL, NULL, &err))
		goto fail;

	return obc_transfer_create_dbus_reply(transfer, message);

fail:
	reply = g_dbus_create_error(message, ERROR_INTERFACE ".Failed", "%s",
								err->message);
	g_error_free(err);
	return reply;
}

static gboolean parse_get_image_dict(DBusMessage *msg, char **path,
					char **handle, char **pixel,
					char **encoding, uint64_t *maxsize,
							char **transform)
{
	DBusMessageIter iter, array;

	DBG("");

	*path = NULL;
	*handle = NULL;
	*pixel = NULL;
	*encoding = NULL;
	*transform = NULL;

	dbus_message_iter_init(msg, &iter);

	if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
		goto failed;
	dbus_message_iter_get_basic(&iter, path);
	*path = g_strdup(*path);
	if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
		goto failed;
	dbus_message_iter_next(&iter);
	dbus_message_iter_get_basic(&iter, handle);
	*handle = g_strdup(*handle);
	dbus_message_iter_next(&iter);
	if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY)
		goto failed;

	dbus_message_iter_recurse(&iter, &array);

	while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
		DBusMessageIter entry, value;
		const char *key, *val;

		dbus_message_iter_recurse(&array, &entry);

		if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
			return FALSE;
		dbus_message_iter_get_basic(&entry, &key);
		dbus_message_iter_next(&entry);
		dbus_message_iter_recurse(&entry, &value);
		switch (dbus_message_iter_get_arg_type(&value)) {
		case DBUS_TYPE_STRING:
			dbus_message_iter_get_basic(&value, &val);
			if (g_str_equal(key, "pixel")) {
				if (!parse_pixel_range(val, NULL, NULL, NULL))
					goto failed;
				*pixel = g_strdup(val);
			} else if (g_str_equal(key, "encoding")) {
				if (!verify_encoding(val))
					goto failed;
				*encoding = g_strdup(val);
				if (*encoding == NULL)
					goto failed;
			} else if (g_str_equal(key, "transformation")) {
				*transform = parse_transform(val);
				if (*transform == NULL)
					goto failed;
			}
			break;
		case DBUS_TYPE_UINT64:
			if (g_str_equal(key, "maxsize") == TRUE) {
				dbus_message_iter_get_basic(&value, maxsize);
				if (*maxsize == 0)
					goto failed;
			}
			break;
		}
		dbus_message_iter_next(&array);
	}

	if (*pixel == NULL)
		*pixel = strdup("");
	if (*encoding == NULL)
		*encoding = strdup("");

	DBG("pixel: '%s' encoding: '%s' maxsize: '%lu' transform: '%s'",
			*pixel, *encoding, *maxsize, *transform
	);

	return TRUE;
failed:
	g_free(*path);
	g_free(*handle);
	g_free(*pixel);
	g_free(*encoding);
	g_free(*transform);
	return FALSE;
}

static DBusMessage *get_image(DBusConnection *connection,
					DBusMessage *message, void *user_data)
{
	struct bip_avrcp_data *bip_avrcp = user_data;
	char *handle = NULL, *image_path = NULL, *transform = NULL,
		*encoding = NULL, *pixel = NULL;
	uint64_t maxsize;
	struct obc_transfer *transfer;
	GObexHeader *header;
	DBusMessage *reply = NULL;
	GString *descriptor = NULL;
	GError *err = NULL;

	DBG("");

	if (!parse_get_image_dict(message, &image_path, &handle, &pixel,
					&encoding, &maxsize, &transform))
		return g_dbus_create_error(message,
				ERROR_INTERFACE ".InvalidArguments", NULL);

	transfer = obc_transfer_get("x-bt/img-img", NULL, image_path, &err);
	if (transfer == NULL) {
		reply = g_dbus_create_error(message, ERROR_INTERFACE ".Failed",
						"%s",
						err->message);
		g_error_free(err);
		goto fail;
	}

	header = g_obex_header_new_unicode(IMG_HANDLE_TAG, handle);
	obc_transfer_add_header(transfer, header);

	descriptor = g_string_new(IMG_DESC_BEGIN);
	g_string_append_printf(descriptor, IMG_BEGIN, encoding, pixel);
	if (transform != NULL)
		g_string_append_printf(descriptor, IMG_TRANSFORM, transform);
	g_string_append(descriptor, IMG_END);
	descriptor = g_string_append(descriptor, IMG_DESC_END);
	header = g_obex_header_new_bytes(IMG_DESC_TAG, descriptor->str,
						descriptor->len);
	obc_transfer_add_header(transfer, header);
	g_string_free(descriptor, TRUE);

	if (!obc_session_queue(bip_avrcp->session, transfer, NULL, NULL,
								&err)) {
		reply = g_dbus_create_error(message, ERROR_INTERFACE ".Failed",
						"%s",
						err->message);
		g_error_free(err);
		goto fail;
	}

	reply = obc_transfer_create_dbus_reply(transfer, message);

fail:
	g_free(handle);
	g_free(image_path);
	g_free(transform);
	g_free(encoding);
	g_free(pixel);
	return reply;
}

static const GDBusMethodTable bip_avrcp_methods[] = {
	{ GDBUS_ASYNC_METHOD("Properties",
		GDBUS_ARGS({ "handle", "s"}),
		GDBUS_ARGS({ "properties", "aa{sv}" }),
		get_image_properties) },
	{ GDBUS_ASYNC_METHOD("Get",
		GDBUS_ARGS({ "file", "s" }, { "handle", "s"},
				{"properties", "a{sv}"}),
		GDBUS_ARGS({ "transfer", "o" }, { "properties", "a{sv}" }),
		get_image) },
	{ GDBUS_ASYNC_METHOD("GetThumbnail",
		GDBUS_ARGS({ "file", "s" }, { "handle", "s"}),
		GDBUS_ARGS({ "transfer", "o" }, { "properties", "a{sv}" }),
		get_thumbnail) },
	{ }
};

static void bip_avrcp_free(void *data)
{
	struct bip_avrcp_data *bip_avrcp = data;

	obc_session_unref(bip_avrcp->session);
	g_free(bip_avrcp);
}

static int bip_avrcp_probe(struct obc_session *session)
{
	struct bip_avrcp_data *bip_avrcp;
	const char *path;

	path = obc_session_get_path(session);

	DBG("%s", path);

	bip_avrcp = g_try_new0(struct bip_avrcp_data, 1);
	if (!bip_avrcp)
		return -ENOMEM;

	bip_avrcp->session = obc_session_ref(session);

	if (!g_dbus_register_interface(conn, path, IMAGE_INTERFACE,
					bip_avrcp_methods,
					NULL, NULL,
					bip_avrcp, bip_avrcp_free)) {
		bip_avrcp_free(bip_avrcp);
		return -ENOMEM;
	}

	return 0;
}

static void bip_avrcp_remove(struct obc_session *session)
{
	const char *path = obc_session_get_path(session);

	DBG("%s", path);

	g_dbus_unregister_interface(conn, path, IMAGE_INTERFACE);
}

static struct obc_driver bip_avrcp = {
	.service = "BIP-AVRCP",
	.uuid = IMAGE_UUID,
	.target = OBEX_BIP_AVRCP_UUID,
	.target_len = OBEX_BIP_AVRCP_UUID_LEN,
	.probe = bip_avrcp_probe,
	.remove = bip_avrcp_remove
};

int bip_init(void)
{
	int err;

	DBG("");

	conn = obex_get_dbus_connection();
	if (!conn)
		return -EIO;

	err = obc_driver_register(&bip_avrcp);
	if (err < 0)
		goto failed;

	return 0;

failed:
	dbus_connection_unref(conn);
	conn = NULL;
	return err;
}

void bip_exit(void)
{
	DBG("");

	dbus_connection_unref(conn);
	conn = NULL;

	obc_driver_unregister(&bip_avrcp);
}