summaryrefslogtreecommitdiff
path: root/src/shared/btp.c
blob: e54eb830f9b43c1ceb48285cd938a29fa0440887 (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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
// SPDX-License-Identifier: LGPL-2.1-or-later
/*
 *
 *  BlueZ - Bluetooth protocol stack for Linux
 *
 *  Copyright (C) 2012-2017  Intel Corporation. All rights reserved.
 *
 *
 */

#include <stdbool.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>

#include <ell/ell.h>

#include "lib/bluetooth.h"
#include "src/shared/btp.h"

#define BTP_MTU 512

struct btp_handler {
	unsigned int id;
	uint8_t service;
	uint8_t opcode;

	btp_cmd_func_t callback;
	void *user_data;
	btp_destroy_func_t destroy;
};

struct btp {
	struct l_io *io;

	struct l_queue *pending;
	bool writer_active;
	bool reader_active;

	struct l_queue *handlers;
	unsigned int next_handler;

	uint8_t buf[BTP_MTU];

	btp_disconnect_func_t disconnect_cb;
	void *disconnect_cb_data;
	btp_destroy_func_t disconnect_cb_data_destroy;
};


static struct l_io *btp_connect(const char *path)
{
	struct sockaddr_un addr;
	struct l_io *io;
	int sk;

	sk = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, 0);
	if (sk < 0)
		return NULL;

	memset(&addr, 0, sizeof(addr));
	addr.sun_family = AF_UNIX;
	strncpy(addr.sun_path, path, sizeof(addr.sun_path) - 1);

	if (connect(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
		close(sk);
		return NULL;
	}

	io = l_io_new(sk);
	if (!io) {
		close(sk);
		return NULL;
	}

	l_io_set_close_on_destroy(io, true);
	return io;
}

static void disconnect_handler(struct l_io *io, void *user_data)
{
	struct btp *btp = user_data;

	btp->disconnect_cb(btp, btp->disconnect_cb_data);
}

static void disconnect_handler_destroy(void *user_data)
{
	struct btp *btp = user_data;

	if (btp->disconnect_cb_data_destroy)
		btp->disconnect_cb_data_destroy(btp->disconnect_cb_data);
}

struct handler_match_data {
	uint8_t service;
	uint8_t opcode;
};

static bool handler_match(const void *a, const void *b)
{
	const struct btp_handler *handler = a;
	const struct handler_match_data *match = b;

	return (handler->service == match->service) &&
					(handler->opcode == match->opcode);
}

static bool can_read_data(struct l_io *io, void *user_data)
{
	struct handler_match_data match;
	struct btp *btp = user_data;
	struct btp_handler *handler;
	struct btp_hdr *hdr;
	ssize_t bytes_read;
	uint16_t data_len;

	bytes_read = read(l_io_get_fd(btp->io), btp->buf, sizeof(btp->buf));
	if (bytes_read < 0)
		return false;

	if ((size_t) bytes_read < sizeof(*hdr))
		return false;

	hdr = (void *)btp->buf;

	data_len = L_LE16_TO_CPU(hdr->data_len);

	if ((size_t) bytes_read < sizeof(*hdr) + data_len)
		return false;

	match.service = hdr->service;
	match.opcode = hdr->opcode;

	handler = l_queue_find(btp->handlers, handler_match, &match);
	if (handler) {
		handler->callback(hdr->index, hdr->data, data_len,
							handler->user_data);
		return false;
	}

	/* keep reader active if we sent error reply */
	btp_send_error(btp, match.service, hdr->index, BTP_ERROR_UNKNOWN_CMD);
	return true;
}

static void read_watch_destroy(void *user_data)
{
	struct btp *btp = user_data;

	btp->reader_active = false;
}

static void wakeup_reader(struct btp *btp)
{
	if (btp->reader_active)
		return;

	btp->reader_active = l_io_set_read_handler(btp->io, can_read_data, btp,
							read_watch_destroy);
}

struct btp *btp_new(const char *path)
{
	struct btp *btp;
	struct l_io *io;

	io = btp_connect(path);
	if (!io)
		return NULL;

	btp = l_new(struct btp, 1);
	btp->pending = l_queue_new();
	btp->handlers = l_queue_new();
	btp->io = io;
	btp->next_handler = 1;

	wakeup_reader(btp);

	return btp;
}

struct pending_message {
	size_t len;
	void *data;
	bool wakeup_read;
};

static void destroy_message(struct pending_message *msg)
{
	l_free(msg->data);
	l_free(msg);
}

void btp_cleanup(struct btp *btp)
{
	if (!btp)
		return;

	l_io_destroy(btp->io);
	l_queue_destroy(btp->pending, (l_queue_destroy_func_t)destroy_message);
	l_queue_destroy(btp->handlers, (l_queue_destroy_func_t)l_free);
	l_free(btp);
}

bool btp_set_disconnect_handler(struct btp *btp, btp_disconnect_func_t callback,
				void *user_data, btp_destroy_func_t destroy)
{
	if (callback) {
		if (!l_io_set_disconnect_handler(btp->io, disconnect_handler,
					btp, disconnect_handler_destroy))
			return false;
	} else {
		if (!l_io_set_disconnect_handler(btp->io, NULL, NULL, NULL))
			return false;
	}

	btp->disconnect_cb = callback;
	btp->disconnect_cb_data = user_data;
	btp->disconnect_cb_data_destroy = destroy;

	return true;
}

static bool can_write_data(struct l_io *io, void *user_data)
{
	struct btp *btp = user_data;
	struct pending_message *msg;

	msg = l_queue_pop_head(btp->pending);
	if (!msg)
		return false;

	if (msg->wakeup_read)
		wakeup_reader(btp);

	if (write(l_io_get_fd(btp->io), msg->data, msg->len) < 0) {
		l_error("Failed to send BTP message");
		destroy_message(msg);
		return false;
	}

	destroy_message(msg);

	return !l_queue_isempty(btp->pending);
}

static void write_watch_destroy(void *user_data)
{
	struct btp *btp = user_data;

	btp->writer_active = false;
}

static void wakeup_writer(struct btp *btp)
{
	if (l_queue_isempty(btp->pending))
		return;

	if (btp->writer_active)
		return;

	btp->writer_active = l_io_set_write_handler(btp->io, can_write_data,
						btp, write_watch_destroy);
}

bool btp_send_error(struct btp *btp, uint8_t service, uint8_t index,
								uint8_t status)
{
	struct btp_error rsp;

	rsp.status = status;

	return btp_send(btp, service, BTP_OP_ERROR, index, sizeof(rsp), &rsp);
}

bool btp_send(struct btp *btp, uint8_t service, uint8_t opcode, uint8_t index,
					uint16_t length, const void *param)
{
	struct btp_hdr *hdr;
	struct pending_message *msg;
	size_t len;

	if (!btp)
		return false;

	len = sizeof(*hdr) + length;
	hdr = l_malloc(len);
	if (!hdr)
		return false;

	hdr->service = service;
	hdr->opcode = opcode;
	hdr->index = index;
	hdr->data_len = L_CPU_TO_LE16(length);
	if (length)
		memcpy(hdr->data, param, length);

	msg = l_new(struct pending_message, 1);
	msg->len = len;
	msg->data = hdr;
	msg->wakeup_read = opcode < 0x80;

	l_queue_push_tail(btp->pending, msg);
	wakeup_writer(btp);

	return true;
}

unsigned int btp_register(struct btp *btp, uint8_t service, uint8_t opcode,
				btp_cmd_func_t callback, void *user_data,
				btp_destroy_func_t destroy)
{
	struct btp_handler *handler;

	handler = l_new(struct btp_handler, 1);

	handler->id = btp->next_handler++;
	handler->service = service;
	handler->opcode = opcode;
	handler->callback = callback;
	handler->user_data = user_data;
	handler->destroy = destroy;

	l_queue_push_tail(btp->handlers, handler);

	return handler->id;
}

static bool handler_match_by_id(const void *a, const void *b)
{
	const struct btp_handler *handler = a;
	unsigned int id = L_PTR_TO_UINT(b);

	return handler->id == id;
}

bool btp_unregister(struct btp *btp, unsigned int id)
{
	struct btp_handler *handler;

	handler = l_queue_remove_if(btp->handlers, handler_match_by_id,
							L_UINT_TO_PTR(id));
	if (!handler)
		return false;

	if (handler->destroy)
		handler->destroy(handler->user_data);

	l_free(handler);

	return true;
}

static bool handler_remove_by_service(void *a, void *b)
{
	struct btp_handler *handler = a;
	uint8_t service = L_PTR_TO_UINT(b);

	if (handler->service != service)
		return false;

	if (handler->destroy)
		handler->destroy(handler->user_data);

	l_free(handler);
	return true;
}

void btp_unregister_service(struct btp *btp, uint8_t service)
{
	l_queue_foreach_remove(btp->handlers, handler_remove_by_service,
							L_UINT_TO_PTR(service));
}