1 /*
2  * Copyright (c) 2022 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/bluetooth/bluetooth.h>
8 #include <zephyr/bluetooth/gatt.h>
9 
10 #include "common.h"
11 
12 CREATE_FLAG(flag_is_connected);
13 CREATE_FLAG(flag_is_encrypted);
14 CREATE_FLAG(flag_discover_complete);
15 CREATE_FLAG(flag_write_complete);
16 CREATE_FLAG(flag_subscribed_short);
17 CREATE_FLAG(flag_subscribed_long);
18 
19 static struct bt_conn *g_conn;
20 static uint16_t chrc_handle;
21 static uint16_t long_chrc_handle;
22 static uint16_t csf_handle;
23 static const struct bt_uuid *test_svc_uuid = TEST_SERVICE_UUID;
24 
exchange_func(struct bt_conn * conn,uint8_t err,struct bt_gatt_exchange_params * params)25 static void exchange_func(struct bt_conn *conn, uint8_t err, struct bt_gatt_exchange_params *params)
26 {
27 	if (!err) {
28 		printk("MTU exchange done\n");
29 	} else {
30 		printk("MTU exchange failed (err %" PRIu8 ")\n", err);
31 	}
32 }
33 
connected(struct bt_conn * conn,uint8_t err)34 static void connected(struct bt_conn *conn, uint8_t err)
35 {
36 	char addr[BT_ADDR_LE_STR_LEN];
37 	static struct bt_gatt_exchange_params exchange_params;
38 
39 	bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
40 
41 	if (err != 0) {
42 		FAIL("Failed to connect to %s (%u)\n", addr, err);
43 		return;
44 	}
45 
46 	printk("Connected to %s\n", addr);
47 
48 	SET_FLAG(flag_is_connected);
49 
50 	exchange_params.func = exchange_func;
51 	err = bt_gatt_exchange_mtu(conn, &exchange_params);
52 	if (err) {
53 		printk("MTU exchange failed (err %d)", err);
54 	}
55 }
56 
disconnected(struct bt_conn * conn,uint8_t reason)57 static void disconnected(struct bt_conn *conn, uint8_t reason)
58 {
59 	char addr[BT_ADDR_LE_STR_LEN];
60 
61 	if (conn != g_conn) {
62 		return;
63 	}
64 
65 	bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
66 
67 	printk("Disconnected: %s (reason 0x%02x)\n", addr, reason);
68 
69 	bt_conn_unref(g_conn);
70 
71 	g_conn = NULL;
72 	UNSET_FLAG(flag_is_connected);
73 }
74 
security_changed(struct bt_conn * conn,bt_security_t level,enum bt_security_err err)75 void security_changed(struct bt_conn *conn, bt_security_t level, enum bt_security_err err)
76 {
77 	if (err) {
78 		FAIL("Encryption failer (%d)\n", err);
79 	} else if (level < BT_SECURITY_L2) {
80 		FAIL("Insufficient sec level (%d)\n", level);
81 	} else {
82 		SET_FLAG(flag_is_encrypted);
83 	}
84 }
85 
86 BT_CONN_CB_DEFINE(conn_callbacks) = {
87 	.connected = connected,
88 	.disconnected = disconnected,
89 	.security_changed = security_changed,
90 };
91 
device_found(const bt_addr_le_t * addr,int8_t rssi,uint8_t type,struct net_buf_simple * ad)92 void device_found(const bt_addr_le_t *addr, int8_t rssi, uint8_t type, struct net_buf_simple *ad)
93 {
94 	char addr_str[BT_ADDR_LE_STR_LEN];
95 	int err;
96 
97 	if (g_conn != NULL) {
98 		return;
99 	}
100 
101 	/* We're only interested in connectable events */
102 	if (type != BT_HCI_ADV_IND && type != BT_HCI_ADV_DIRECT_IND) {
103 		return;
104 	}
105 
106 	bt_addr_le_to_str(addr, addr_str, sizeof(addr_str));
107 	printk("Device found: %s (RSSI %d)\n", addr_str, rssi);
108 
109 	printk("Stopping scan\n");
110 	err = bt_le_scan_stop();
111 	if (err != 0) {
112 		FAIL("Could not stop scan: %d");
113 		return;
114 	}
115 
116 	err = bt_conn_le_create(addr, BT_CONN_LE_CREATE_CONN, BT_LE_CONN_PARAM_DEFAULT, &g_conn);
117 	if (err != 0) {
118 		FAIL("Could not connect to peer: %d", err);
119 	}
120 }
121 
discover_func(struct bt_conn * conn,const struct bt_gatt_attr * attr,struct bt_gatt_discover_params * params)122 static uint8_t discover_func(struct bt_conn *conn, const struct bt_gatt_attr *attr,
123 			     struct bt_gatt_discover_params *params)
124 {
125 	int err;
126 
127 	if (attr == NULL) {
128 		if (chrc_handle == 0 || long_chrc_handle == 0) {
129 			FAIL("Did not discover chrc (%x) or long_chrc (%x)", chrc_handle,
130 			     long_chrc_handle);
131 		}
132 
133 		(void)memset(params, 0, sizeof(*params));
134 
135 		SET_FLAG(flag_discover_complete);
136 
137 		return BT_GATT_ITER_STOP;
138 	}
139 
140 	printk("[ATTRIBUTE] handle %u\n", attr->handle);
141 
142 	if (params->type == BT_GATT_DISCOVER_PRIMARY &&
143 	    bt_uuid_cmp(params->uuid, TEST_SERVICE_UUID) == 0) {
144 		printk("Found test service\n");
145 		params->uuid = NULL;
146 		params->start_handle = attr->handle + 1;
147 		params->type = BT_GATT_DISCOVER_CHARACTERISTIC;
148 
149 		err = bt_gatt_discover(conn, params);
150 		if (err != 0) {
151 			FAIL("Discover failed (err %d)\n", err);
152 		}
153 
154 		return BT_GATT_ITER_STOP;
155 	} else if (params->type == BT_GATT_DISCOVER_CHARACTERISTIC) {
156 		const struct bt_gatt_chrc *chrc = (struct bt_gatt_chrc *)attr->user_data;
157 
158 		if (bt_uuid_cmp(chrc->uuid, TEST_CHRC_UUID) == 0) {
159 			printk("Found chrc\n");
160 			chrc_handle = chrc->value_handle;
161 		} else if (bt_uuid_cmp(chrc->uuid, TEST_LONG_CHRC_UUID) == 0) {
162 			printk("Found long_chrc\n");
163 			long_chrc_handle = chrc->value_handle;
164 		} else if (bt_uuid_cmp(chrc->uuid, BT_UUID_GATT_CLIENT_FEATURES) == 0) {
165 			printk("Found csf\n");
166 			csf_handle = chrc->value_handle;
167 		}
168 	}
169 
170 	return BT_GATT_ITER_CONTINUE;
171 }
172 
gatt_discover(const struct bt_uuid * uuid,uint8_t type)173 static void gatt_discover(const struct bt_uuid *uuid, uint8_t type)
174 {
175 	static struct bt_gatt_discover_params discover_params;
176 	int err;
177 
178 	printk("Discovering services and characteristics\n");
179 
180 	discover_params.uuid = uuid;
181 	discover_params.func = discover_func;
182 	discover_params.start_handle = BT_ATT_FIRST_ATTRIBUTE_HANDLE;
183 	discover_params.end_handle = BT_ATT_LAST_ATTRIBUTE_HANDLE;
184 	discover_params.type = type;
185 
186 	UNSET_FLAG(flag_discover_complete);
187 
188 	err = bt_gatt_discover(g_conn, &discover_params);
189 	if (err != 0) {
190 		FAIL("Discover failed(err %d)\n", err);
191 	}
192 
193 	WAIT_FOR_FLAG(flag_discover_complete);
194 	printk("Discover complete\n");
195 }
196 
test_subscribed(struct bt_conn * conn,uint8_t err,struct bt_gatt_subscribe_params * params)197 static void test_subscribed(struct bt_conn *conn,
198 			    uint8_t err,
199 			    struct bt_gatt_subscribe_params *params)
200 {
201 	if (err) {
202 		FAIL("Subscribe failed (err %d)\n", err);
203 	}
204 
205 	if (!params) {
206 		FAIL("params NULL\n");
207 	}
208 
209 	if (params->value_handle == chrc_handle) {
210 		FORCE_FLAG(flag_subscribed_short, (bool)params->value);
211 		printk("Subscribed to short characteristic\n");
212 	} else if (params->value_handle == long_chrc_handle) {
213 		FORCE_FLAG(flag_subscribed_long, (bool)params->value);
214 		printk("Subscribed to long characteristic\n");
215 	} else {
216 		FAIL("Unknown handle %d\n", params->value_handle);
217 	}
218 }
219 
220 static volatile size_t num_notifications;
test_notify(struct bt_conn * conn,struct bt_gatt_subscribe_params * params,const void * data,uint16_t length)221 uint8_t test_notify(struct bt_conn *conn, struct bt_gatt_subscribe_params *params, const void *data,
222 		    uint16_t length)
223 {
224 	printk("Received notification #%u with length %d\n", num_notifications++, length);
225 
226 	return BT_GATT_ITER_CONTINUE;
227 }
228 
229 static struct bt_gatt_discover_params disc_params_short;
230 static struct bt_gatt_subscribe_params sub_params_short = {
231 	.notify = test_notify,
232 	.subscribe = test_subscribed,
233 	.ccc_handle = BT_GATT_AUTO_DISCOVER_CCC_HANDLE,
234 	.disc_params = &disc_params_short,
235 	.end_handle = BT_ATT_LAST_ATTRIBUTE_HANDLE,
236 	.value = BT_GATT_CCC_NOTIFY,
237 };
238 static struct bt_gatt_discover_params disc_params_long;
239 static struct bt_gatt_subscribe_params sub_params_long = {
240 	.notify = test_notify,
241 	.subscribe = test_subscribed,
242 	.ccc_handle = BT_GATT_AUTO_DISCOVER_CCC_HANDLE,
243 	.disc_params = &disc_params_long,
244 	.end_handle = BT_ATT_LAST_ATTRIBUTE_HANDLE,
245 	.value = BT_GATT_CCC_NOTIFY,
246 };
247 
write_cb(struct bt_conn * conn,uint8_t err,struct bt_gatt_write_params * params)248 static void write_cb(struct bt_conn *conn, uint8_t err, struct bt_gatt_write_params *params)
249 {
250 	if (err != BT_ATT_ERR_SUCCESS) {
251 		FAIL("Write failed: 0x%02X\n", err);
252 	}
253 
254 	SET_FLAG(flag_write_complete);
255 }
256 
write_csf(void)257 static void write_csf(void)
258 {
259 	/* Client Supported Features Characteristic Value
260 	 * Bit 0: Robust Caching
261 	 * Bit 1: EATT
262 	 * Bit 2: Multiple Handle Value Notifications
263 	 */
264 	static const uint8_t csf[] = { BIT(2) };
265 	static struct bt_gatt_write_params write_params = {
266 		.func = write_cb,
267 		.offset = 0,
268 		.data = csf,
269 		.length = sizeof(csf),
270 	};
271 	int err;
272 
273 	printk("Writing to Client Supported Features Characteristic\n");
274 
275 	write_params.handle = csf_handle;
276 	UNSET_FLAG(flag_write_complete);
277 
278 	err = bt_gatt_write(g_conn, &write_params);
279 	if (err) {
280 		FAIL("bt_gatt_write failed (err %d)\n", err);
281 	}
282 
283 	WAIT_FOR_FLAG(flag_write_complete);
284 	printk("Success\n");
285 }
286 
subscribe(struct bt_gatt_subscribe_params * params,bool subscribe)287 static void subscribe(struct bt_gatt_subscribe_params *params, bool subscribe)
288 {
289 	int err;
290 
291 	if (subscribe) {
292 		err = bt_gatt_subscribe(g_conn, params);
293 	} else {
294 		err = bt_gatt_unsubscribe(g_conn, params);
295 	}
296 
297 	if (err < 0) {
298 		FAIL("Failed to %ssubscribe (err %d)\n", subscribe ? "un":"", err);
299 	} else {
300 		printk("%ssubscribe request sent\n", subscribe ? "un":"");
301 	}
302 
303 }
304 
test_main(void)305 static void test_main(void)
306 {
307 	int err;
308 
309 	err = bt_enable(NULL);
310 	if (err != 0) {
311 		FAIL("Bluetooth discover failed (err %d)\n", err);
312 	}
313 
314 	err = bt_le_scan_start(BT_LE_SCAN_PASSIVE, device_found);
315 	if (err != 0) {
316 		FAIL("Scanning failed to start (err %d)\n", err);
317 	}
318 
319 	printk("Scanning successfully started\n");
320 
321 	WAIT_FOR_FLAG(flag_is_connected);
322 
323 	err = bt_conn_set_security(g_conn, BT_SECURITY_L2);
324 	if (err) {
325 		FAIL("Starting encryption procedure failed (%d)\n", err);
326 	}
327 
328 	WAIT_FOR_FLAG(flag_is_encrypted);
329 
330 	while (bt_eatt_count(g_conn) < CONFIG_BT_EATT_MAX) {
331 		k_sleep(K_MSEC(10));
332 	}
333 
334 	printk("EATT connected\n");
335 
336 	gatt_discover(test_svc_uuid, BT_GATT_DISCOVER_PRIMARY);
337 	gatt_discover(BT_UUID_GATT_CLIENT_FEATURES, BT_GATT_DISCOVER_CHARACTERISTIC);
338 	write_csf();
339 
340 	sub_params_short.value_handle = chrc_handle;
341 	sub_params_long.value_handle = long_chrc_handle;
342 
343 	subscribe(&sub_params_short, true);
344 	subscribe(&sub_params_long, true);
345 	WAIT_FOR_FLAG(flag_subscribed_short);
346 	WAIT_FOR_FLAG(flag_subscribed_long);
347 
348 	printk("Subscribed\n");
349 
350 	while (num_notifications < NOTIFICATION_COUNT) {
351 		k_sleep(K_MSEC(100));
352 	}
353 
354 	subscribe(&sub_params_short, false);
355 	subscribe(&sub_params_long, false);
356 	WAIT_FOR_FLAG_UNSET(flag_subscribed_short);
357 	WAIT_FOR_FLAG_UNSET(flag_subscribed_long);
358 
359 
360 	printk("Unsubscribed\n");
361 
362 	PASS("GATT client Passed\n");
363 }
364 
365 static const struct bst_test_instance test_vcs[] = {
366 	{
367 		.test_id = "gatt_client",
368 		.test_pre_init_f = test_init,
369 		.test_tick_f = test_tick,
370 		.test_main_f = test_main,
371 	},
372 	BSTEST_END_MARKER,
373 };
374 
test_gatt_client_install(struct bst_test_list * tests)375 struct bst_test_list *test_gatt_client_install(struct bst_test_list *tests)
376 {
377 	return bst_add_tests(tests, test_vcs);
378 }
379