summaryrefslogtreecommitdiff
path: root/monitor/msft.c
blob: 38af6722e6d096c38160971201acb0815990c60c (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
/*
 *
 *  BlueZ - Bluetooth protocol stack for Linux
 *
 *  Copyright (C) 2011-2014  Intel Corporation
 *  Copyright (C) 2002-2010  Marcel Holtmann <marcel@holtmann.org>
 *
 *
 *  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

#define _GNU_SOURCE
#include <stdio.h>
#include <inttypes.h>

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

#include "src/shared/util.h"
#include "display.h"
#include "packet.h"
#include "vendor.h"
#include "msft.h"

#define COLOR_COMMAND		COLOR_BLUE
#define COLOR_COMMAND_UNKNOWN	COLOR_WHITE_BG

static void null_cmd(const void *data, uint16_t size)
{
}

static void null_rsp(const void *data, uint16_t size)
{
}

static void read_supported_features_rsp(const void *data, uint16_t size)
{
	const struct msft_rsp_read_supported_features *rsp = data;

	packet_print_features_msft(rsp->features);
	print_field("Event prefix length: %u", rsp->evt_prefix_len);
	print_field("Event prefix:");
	packet_hexdump(rsp->evt_prefix, rsp->evt_prefix_len);
	packet_set_msft_evt_prefix(rsp->evt_prefix, rsp->evt_prefix_len);
}

static void monitor_rssi_cmd(const void *data, uint16_t size)
{
	const struct msft_cmd_monitor_rssi *cmd = data;

	print_field("Connection handle: 0x%04x", cmd->handle);
	packet_print_rssi("RSSI threshold high", cmd->rssi_high);
	packet_print_rssi("RSSI threshold low", cmd->rssi_low);
	print_field("RSSI threshold low time interval: %u sec (0x%2.2x)",
						cmd->rssi_low_interval,
						cmd->rssi_low_interval);
	print_field("RSSI sampling period: %u msec (0x%2.2x)",
						cmd->rssi_period * 100,
						cmd->rssi_period);
}

static void cancel_monitor_rssi_cmd(const void *data, uint16_t size)
{
	const struct msft_cmd_cancel_monitor_rssi *cmd = data;

	print_field("Connection handle: 0x%04x", cmd->handle);
}

static void le_monitor_advertisement_cmd(const void *data, uint16_t size)
{
	const struct msft_cmd_le_monitor_adv *cmd = data;
	const struct msft_le_monitor_adv_patterns *patterns;
	const struct msft_le_monitor_adv_uuid *uuid;
	const struct msft_le_monitor_adv_irk *irk;
	const struct msft_le_monitor_adv_addr *addr;
	const char *str;
	char uuidstr[MAX_LEN_UUID_STR];

	packet_print_rssi("RSSI threshold high", cmd->rssi_high);
	packet_print_rssi("RSSI threshold low", cmd->rssi_low);
	print_field("RSSI threshold low time interval: %u sec (0x%2.2x)",
						cmd->rssi_low_interval,
						cmd->rssi_low_interval);
	print_field("RSSI sampling period: %u msec (0x%2.2x)",
						cmd->rssi_period * 100,
						cmd->rssi_period);

	switch (cmd->type) {
	case MSFT_LE_MONITOR_ADV_PATTERN:
		print_field("Type: Pattern (0x%2.2x)", cmd->type);
		patterns = (void *)cmd->data;
		print_field("Number of patterns: %u", patterns->num);
		packet_hexdump((void *)patterns->data,
			       size - (sizeof(*cmd) + sizeof(*patterns)));
		break;
	case MSFT_LE_MONITOR_ADV_UUID:
		print_field("Type: UUID (0x%2.2x)", cmd->type);
		uuid = (void *)cmd->data;

		switch (uuid->type) {
		case 0x01:
			str = bt_uuid16_to_str(uuid->value.u16);
			print_field("UUID: %s (0x%4.4x)", str, uuid->value.u16);
			break;
		case 0x02:
			str = bt_uuid32_to_str(uuid->value.u32);
			print_field("UUID: %s (0x%8.8x)", str, uuid->value.u32);
			break;
		case 0x03:
			sprintf(uuidstr, "%8.8x-%4.4x-%4.4x-%4.4x-%8.8x%4.4x",
				get_le32(uuid->value.u128 + 12),
				get_le16(uuid->value.u128 + 10),
				get_le16(uuid->value.u128 + 8),
				get_le16(uuid->value.u128 + 6),
				get_le32(uuid->value.u128 + 2),
				get_le16(uuid->value.u128 + 0));
			str = bt_uuidstr_to_str(uuidstr);
			print_field("UUID: %s (%s)", str, uuidstr);
			break;
		default:
			packet_hexdump((void *)&uuid->value,
					size - sizeof(*cmd));
			break;
		}
		break;
	case MSFT_LE_MONITOR_ADV_IRK:
		print_field("Type: IRK (0x%2.2x)", cmd->type);
		irk = (void *)cmd->data;
		print_field("IRK:");
		packet_hexdump(irk->irk, size - sizeof(*cmd));
		break;
	case MSFT_LE_MONITOR_ADV_ADDR:
		print_field("Type: Adderss (0x%2.2x)", cmd->type);
		addr = (void *)cmd->data;
		packet_print_addr(NULL, addr->addr, addr->type);
		break;
	default:
		print_field("Type: Unknown (0x%2.2x)", cmd->type);
		packet_hexdump(cmd->data, size - sizeof(*cmd));
		break;
	}
}

static void le_monitor_advertisement_rsp(const void *data, uint16_t size)
{
	const struct msft_rsp_le_monitor_adv *rsp = data;

	print_field("Monitor handle: %u", rsp->handle);
}

static void le_cancel_monitor_adv_cmd(const void *data, uint16_t size)
{
	const struct msft_cmd_le_cancel_monitor_adv *cmd = data;

	print_field("Monitor handle: %u", cmd->handle);
}

static void set_adv_filter_enable_cmd(const void *data, uint16_t size)
{
	const struct msft_cmd_le_monitor_adv_enable *cmd = data;
	const char *str;

	switch (cmd->enable) {
	case 0x00:
		str = "Current allow list";
		break;
	case 0x01:
		str = "All filter conditions";
		break;
	default:
		str = "Reserved";
		break;
	}

	print_field("Enable: %s (0x%2.2x)", str, cmd->enable);
}

typedef void (*func_t) (const void *data, uint16_t size);

static const struct {
	uint8_t code;
	const char *str;
	func_t cmd_func;
	func_t rsp_func;
} cmd_table[] = {
	{ 0x00, "Read Supported Features",
			null_cmd,
			read_supported_features_rsp },
	{ 0x01, "Monitor RSSI",
			monitor_rssi_cmd },
	{ 0x02, "Cancel Monitor RSSI",
			cancel_monitor_rssi_cmd },
	{ 0x03, "LE Monitor Advertisement",
			le_monitor_advertisement_cmd,
			le_monitor_advertisement_rsp },
	{ 0x04, "LE Cancel Monitor Advertisement",
			le_cancel_monitor_adv_cmd },
	{ 0x05, "LE Set Advertisement Filter Enable",
			set_adv_filter_enable_cmd,
			null_rsp },
	{ 0x06, "Read Absolute RSSI" },
	{ }
};

static void msft_cmd(uint16_t index, const void *data, uint8_t size)
{
	uint8_t code = get_u8(data);
	const char *code_color, *code_str = NULL;
	func_t code_func = NULL;
	int i;

	for (i = 0; cmd_table[i].str; i++) {
		if (cmd_table[i].code == code) {
			code_str = cmd_table[i].str;
			code_func = cmd_table[i].cmd_func;
			break;
		}
	}

	if (code_str) {
		if (code_func)
			code_color = COLOR_COMMAND;
		else
			code_color = COLOR_COMMAND_UNKNOWN;
	} else {
		code_color = COLOR_COMMAND_UNKNOWN;
		code_str = "Unknown";
	}

	print_indent(6, code_color, "", code_str, COLOR_OFF,
						" (0x%2.2x)", code);

	if (code_func)
		code_func(data, size);
	else
		packet_hexdump(data + 1, size - 1);
}

static void msft_rsp(uint16_t index, const void *data, uint8_t size)
{
	uint8_t status = get_u8(data);
	uint8_t code = get_u8(data + 1);
	const char *code_color, *code_str = NULL;
	func_t code_func = NULL;
	int i;

	for (i = 0; cmd_table[i].str; i++) {
		if (cmd_table[i].code == code) {
			code_str = cmd_table[i].str;
			code_func = cmd_table[i].rsp_func;
			break;
		}
	}

	if (code_str) {
		if (code_func)
			code_color = COLOR_COMMAND;
		else
			code_color = COLOR_COMMAND_UNKNOWN;
	} else {
		code_color = COLOR_COMMAND_UNKNOWN;
		code_str = "Unknown";
	}

	print_indent(6, code_color, "", code_str, COLOR_OFF,
						" (0x%2.2x)", code);

	packet_print_error("Status", status);

	if (code_func)
		code_func(data, size);
	else
		packet_hexdump(data + 2, size - 2);
}

static const struct vendor_ocf vendor_ocf_entry = {
	0x000, "Extension", msft_cmd, 1, false, msft_rsp, 2, false
};

const struct vendor_ocf *msft_vendor_ocf(void)
{
	return &vendor_ocf_entry;
}

static void msft_evt(uint16_t index, const void *data, uint8_t size)
{
	packet_hexdump(data, size);
}

static const struct vendor_evt vendor_evt_entry = {
	0x00, "Extension", msft_evt, 1, false
};

const struct vendor_evt *msft_vendor_evt(void)
{
	return &vendor_evt_entry;
}