summaryrefslogtreecommitdiff
path: root/android/scpp.c
blob: a432733df955ea3db6d04eb2409310c69ec8ed02 (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
/*
 *
 *  BlueZ - Bluetooth protocol stack for Linux
 *
 *  Copyright (C) 2012  Nordic Semiconductor Inc.
 *  Copyright (C) 2012  Instituto Nokia de Tecnologia - INdT
 *
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdbool.h>
#include <errno.h>

#include <glib.h>

#include "src/log.h"

#include "lib/bluetooth.h"
#include "lib/sdp.h"
#include "lib/uuid.h"

#include "src/shared/util.h"
#include "src/shared/queue.h"

#include "attrib/att.h"
#include "attrib/gattrib.h"
#include "attrib/gatt.h"

#include "android/scpp.h"

#define SCAN_INTERVAL_WIN_UUID		0x2A4F
#define SCAN_REFRESH_UUID		0x2A31

#define SCAN_INTERVAL		0x0060
#define SCAN_WINDOW		0x0030
#define SERVER_REQUIRES_REFRESH	0x00

struct bt_scpp {
	int ref_count;
	GAttrib *attrib;
	struct gatt_primary *primary;
	uint16_t interval;
	uint16_t window;
	uint16_t iwhandle;
	uint16_t refresh_handle;
	guint refresh_cb_id;
	struct queue *gatt_op;
};

static void discover_char(struct bt_scpp *scpp, GAttrib *attrib,
						uint16_t start, uint16_t end,
						bt_uuid_t *uuid, gatt_cb_t func,
						gpointer user_data)
{
	unsigned int id;

	id = gatt_discover_char(attrib, start, end, uuid, func, user_data);

	queue_push_head(scpp->gatt_op, UINT_TO_PTR(id));
}

static void discover_desc(struct bt_scpp *scpp, GAttrib *attrib,
				uint16_t start, uint16_t end, bt_uuid_t *uuid,
				gatt_cb_t func, gpointer user_data)
{
	unsigned int id;

	id = gatt_discover_desc(attrib, start, end, uuid, func, user_data);

	queue_push_head(scpp->gatt_op, UINT_TO_PTR(id));
}

static void write_char(struct bt_scpp *scan, GAttrib *attrib, uint16_t handle,
					const uint8_t *value, size_t vlen,
					GAttribResultFunc func,
					gpointer user_data)
{
	unsigned int id;

	id = gatt_write_char(attrib, handle, value, vlen, func, user_data);

	queue_push_head(scan->gatt_op, UINT_TO_PTR(id));
}

static void scpp_free(struct bt_scpp *scan)
{
	bt_scpp_detach(scan);

	g_free(scan->primary);
	queue_destroy(scan->gatt_op, NULL); /* cleared in bt_scpp_detach */
	g_free(scan);
}

struct bt_scpp *bt_scpp_new(void *primary)
{
	struct bt_scpp *scan;

	scan = g_try_new0(struct bt_scpp, 1);
	if (!scan)
		return NULL;

	scan->interval = SCAN_INTERVAL;
	scan->window = SCAN_WINDOW;

	scan->gatt_op = queue_new();

	if (primary)
		scan->primary = g_memdup(primary, sizeof(*scan->primary));

	return bt_scpp_ref(scan);
}

struct bt_scpp *bt_scpp_ref(struct bt_scpp *scan)
{
	if (!scan)
		return NULL;

	__sync_fetch_and_add(&scan->ref_count, 1);

	return scan;
}

void bt_scpp_unref(struct bt_scpp *scan)
{
	if (!scan)
		return;

	if (__sync_sub_and_fetch(&scan->ref_count, 1))
		return;

	scpp_free(scan);
}

static void write_scan_params(GAttrib *attrib, uint16_t handle,
					uint16_t interval, uint16_t window)
{
	uint8_t value[4];

	put_le16(interval, &value[0]);
	put_le16(window, &value[2]);

	gatt_write_cmd(attrib, handle, value, sizeof(value), NULL, NULL);
}

static void refresh_value_cb(const uint8_t *pdu, uint16_t len,
						gpointer user_data)
{
	struct bt_scpp *scan = user_data;

	DBG("Server requires refresh: %d", pdu[3]);

	if (pdu[3] == SERVER_REQUIRES_REFRESH)
		write_scan_params(scan->attrib, scan->iwhandle, scan->interval,
								scan->window);
}

static void ccc_written_cb(guint8 status, const guint8 *pdu,
					guint16 plen, gpointer user_data)
{
	struct bt_scpp *scan = user_data;

	if (status != 0) {
		error("Write Scan Refresh CCC failed: %s",
						att_ecode2str(status));
		return;
	}

	DBG("Scan Refresh: notification enabled");

	scan->refresh_cb_id = g_attrib_register(scan->attrib,
				ATT_OP_HANDLE_NOTIFY, scan->refresh_handle,
				refresh_value_cb, scan, NULL);
}

static void write_ccc(struct bt_scpp *scan, GAttrib *attrib, uint16_t handle,
								void *user_data)
{
	uint8_t value[2];

	put_le16(GATT_CLIENT_CHARAC_CFG_NOTIF_BIT, value);

	write_char(scan, attrib, handle, value, sizeof(value), ccc_written_cb,
								user_data);
}

static void discover_descriptor_cb(uint8_t status, GSList *descs,
								void *user_data)
{
	struct bt_scpp *scan = user_data;
	struct gatt_desc *desc;

	if (status != 0) {
		error("Discover descriptors failed: %s", att_ecode2str(status));
		return;
	}

	/* There will be only one descriptor on list and it will be CCC */
	desc = descs->data;

	write_ccc(scan, scan->attrib, desc->handle, scan);
}

static void refresh_discovered_cb(uint8_t status, GSList *chars,
								void *user_data)
{
	struct bt_scpp *scan = user_data;
	struct gatt_char *chr;
	uint16_t start, end;
	bt_uuid_t uuid;

	if (status) {
		error("Scan Refresh %s", att_ecode2str(status));
		return;
	}

	if (!chars) {
		DBG("Scan Refresh not supported");
		return;
	}

	chr = chars->data;

	DBG("Scan Refresh handle: 0x%04x", chr->value_handle);

	start = chr->value_handle + 1;
	end = scan->primary->range.end;

	if (start > end)
		return;

	scan->refresh_handle = chr->value_handle;

	bt_uuid16_create(&uuid, GATT_CLIENT_CHARAC_CFG_UUID);

	discover_desc(scan, scan->attrib, start, end, &uuid,
					discover_descriptor_cb, user_data);
}

static void iwin_discovered_cb(uint8_t status, GSList *chars, void *user_data)
{
	struct bt_scpp *scan = user_data;
	struct gatt_char *chr;

	if (status) {
		error("Discover Scan Interval Window: %s",
						att_ecode2str(status));
		return;
	}

	chr = chars->data;
	scan->iwhandle = chr->value_handle;

	DBG("Scan Interval Window handle: 0x%04x", scan->iwhandle);

	write_scan_params(scan->attrib, scan->iwhandle, scan->interval,
								scan->window);
}

bool bt_scpp_attach(struct bt_scpp *scan, void *attrib)
{
	bt_uuid_t iwin_uuid, refresh_uuid;

	if (!scan || scan->attrib || !scan->primary)
		return false;

	scan->attrib = g_attrib_ref(attrib);

	if (scan->iwhandle)
		write_scan_params(scan->attrib, scan->iwhandle, scan->interval,
								scan->window);
	else {
		bt_uuid16_create(&iwin_uuid, SCAN_INTERVAL_WIN_UUID);
		discover_char(scan, scan->attrib, scan->primary->range.start,
					scan->primary->range.end, &iwin_uuid,
					iwin_discovered_cb, scan);
	}

	if (scan->refresh_handle)
		scan->refresh_cb_id = g_attrib_register(scan->attrib,
				ATT_OP_HANDLE_NOTIFY, scan->refresh_handle,
				refresh_value_cb, scan, NULL);
	else {
		bt_uuid16_create(&refresh_uuid, SCAN_REFRESH_UUID);
		discover_char(scan, scan->attrib, scan->primary->range.start,
					scan->primary->range.end, &refresh_uuid,
					refresh_discovered_cb, scan);
	}

	return true;
}

static void cancel_gatt_req(void *data, void *user_data)
{
	unsigned int id = PTR_TO_UINT(data);
	struct bt_scpp *scan = user_data;

	g_attrib_cancel(scan->attrib, id);
}

void bt_scpp_detach(struct bt_scpp *scan)
{
	if (!scan || !scan->attrib)
		return;

	if (scan->refresh_cb_id > 0) {
		g_attrib_unregister(scan->attrib, scan->refresh_cb_id);
		scan->refresh_cb_id = 0;
	}

	queue_foreach(scan->gatt_op, cancel_gatt_req, scan);
	g_attrib_unref(scan->attrib);
	scan->attrib = NULL;
}

bool bt_scpp_set_interval(struct bt_scpp *scan, uint16_t value)
{
	if (!scan)
		return false;

	/* TODO: Check valid range */

	scan->interval = value;

	return true;
}

bool bt_scpp_set_window(struct bt_scpp *scan, uint16_t value)
{
	if (!scan)
		return false;

	/* TODO: Check valid range */

	scan->window = value;

	return true;
}