summaryrefslogtreecommitdiff
path: root/src/shared/gap.c
blob: 19059e8d9d914a57b3a6f8dff0cd667a1b7d86c7 (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
/*
 *
 *  BlueZ - Bluetooth protocol stack for Linux
 *
 *  Copyright (C) 2012-2014  Intel Corporation. All rights reserved.
 *
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library 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
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; 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 <stdio.h>
#include <stdlib.h>

#include "lib/bluetooth.h"
#include "lib/mgmt.h"

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

#define FLAG_MGMT_CONN_CONTROL	(0 << 1)

struct bt_gap {
	int ref_count;
	uint16_t index;
	struct mgmt *mgmt;

	uint8_t mgmt_version;
	uint16_t mgmt_revision;
	bool mgmt_ready;

	unsigned long flags;

	bt_gap_ready_func_t ready_handler;
	bt_gap_destroy_func_t ready_destroy;
	void *ready_data;

	uint8_t static_addr[6];
	uint8_t local_irk[16];
	struct queue *irk_list;
};

struct irk_entry {
	uint8_t addr_type;
	uint8_t addr[6];
	uint8_t key[16];
};

static void irk_entry_free(void *data)
{
	struct irk_entry *irk = data;

	free(irk);
}

static void ready_status(struct bt_gap *gap, bool status)
{
	gap->mgmt_ready = status;

	if (gap->ready_handler)
		gap->ready_handler(status, gap->ready_data);
}

static void read_commands_complete(uint8_t status, uint16_t length,
					const void *param, void *user_data)
{
	struct bt_gap *gap = user_data;
	const struct mgmt_rp_read_commands *rp = param;
	uint16_t num_commands, num_events;
	const uint16_t *opcode;
	size_t expected_len;
	int i;

	if (status != MGMT_STATUS_SUCCESS) {
		ready_status(gap, false);
		return;
	}

	if (length < sizeof(*rp)) {
		ready_status(gap, false);
		return;
	}

	num_commands = le16_to_cpu(rp->num_commands);
	num_events = le16_to_cpu(rp->num_events);

	expected_len = sizeof(*rp) + num_commands * sizeof(uint16_t) +
						num_events * sizeof(uint16_t);

	if (length < expected_len) {
		ready_status(gap, false);
		return;
	}

	opcode = rp->opcodes;

	for (i = 0; i < num_commands; i++) {
		uint16_t op = get_le16(opcode++);

		if (op == MGMT_OP_ADD_DEVICE)
			gap->flags |= FLAG_MGMT_CONN_CONTROL;
	}

	ready_status(gap, true);
}

static void read_version_complete(uint8_t status, uint16_t length,
					const void *param, void *user_data)
{
	struct bt_gap *gap = user_data;
	const struct mgmt_rp_read_version *rp = param;

	if (status != MGMT_STATUS_SUCCESS) {
		ready_status(gap, false);
		return;
	}

	if (length < sizeof(*rp)) {
		ready_status(gap, false);
		return;
	}

	gap->mgmt_version = rp->version;
	gap->mgmt_revision = le16_to_cpu(rp->revision);

	if (!mgmt_send(gap->mgmt, MGMT_OP_READ_COMMANDS,
				MGMT_INDEX_NONE, 0, NULL,
				read_commands_complete, gap, NULL)) {
		ready_status(gap, false);
		return;
	}
}

struct bt_gap *bt_gap_new_default(void)
{
	return bt_gap_new_index(0x0000);
}

struct bt_gap *bt_gap_new_index(uint16_t index)
{
	struct bt_gap *gap;

	if (index == MGMT_INDEX_NONE)
		return NULL;

	gap = new0(struct bt_gap, 1);
	if (!gap)
		return NULL;

	gap->index = index;

	gap->mgmt = mgmt_new_default();
	if (!gap->mgmt) {
		free(gap);
		return NULL;
	}

	gap->irk_list = queue_new();

	gap->mgmt_ready = false;

	if (!mgmt_send(gap->mgmt, MGMT_OP_READ_VERSION,
					MGMT_INDEX_NONE, 0, NULL,
					read_version_complete, gap, NULL)) {
		mgmt_unref(gap->mgmt);
		return NULL;
	}

	return bt_gap_ref(gap);
}

struct bt_gap *bt_gap_ref(struct bt_gap *gap)
{
	if (!gap)
		return NULL;

	__sync_fetch_and_add(&gap->ref_count, 1);

	return gap;
}

void bt_gap_unref(struct bt_gap *gap)
{
	if (!gap)
		return;

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

	gap->mgmt_ready = false;

	mgmt_cancel_all(gap->mgmt);
	mgmt_unregister_all(gap->mgmt);

	if (gap->ready_destroy)
		gap->ready_destroy(gap->ready_data);

	queue_destroy(gap->irk_list, irk_entry_free);

	mgmt_unref(gap->mgmt);

	free(gap);
}

bool bt_gap_set_ready_handler(struct bt_gap *gap,
				bt_gap_ready_func_t handler, void *user_data,
				bt_gap_destroy_func_t destroy)
{
	if (!gap)
		return false;

	if (gap->ready_destroy)
		gap->ready_destroy(gap->ready_data);

	gap->ready_handler = handler;
	gap->ready_destroy = destroy;
	gap->ready_data = user_data;

	return true;
}

bool bt_gap_set_static_addr(struct bt_gap *gap, uint8_t addr[6])
{
	if (!gap)
		return false;

	memcpy(gap->static_addr, addr, 6);

	return true;
}

bool bt_gap_set_local_irk(struct bt_gap *gap, uint8_t key[16])
{
	if (!gap)
		return false;

	memcpy(gap->local_irk, key, 16);

	return true;
}

bool bt_gap_add_peer_irk(struct bt_gap *gap, uint8_t addr_type,
					uint8_t addr[6], uint8_t key[16])
{
	struct irk_entry *irk;

	if (!gap)
		return false;

	if (addr_type > BT_GAP_ADDR_TYPE_LE_RANDOM)
		return false;

	irk = new0(struct irk_entry, 1);
	if (!irk)
		return false;

	irk->addr_type = addr_type;
	memcpy(irk->addr, addr, 6);
	memcpy(irk->key, key, 16);

	return true;
}