1 /*
2  * Copyright (c) 2023 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/kernel.h>
8 #include <zephyr/sys/byteorder.h>
9 #include <zephyr/sys/__assert.h>
10 
11 #include <zephyr/net_buf.h>
12 #include <zephyr/bluetooth/buf.h>
13 
14 #include <zephyr/bluetooth/bluetooth.h>
15 #include <zephyr/bluetooth/hci.h>
16 #include <zephyr/bluetooth/hci_raw.h>
17 #include <zephyr/bluetooth/hci_types.h>
18 
19 #include "common/bt_str.h"
20 
21 #include "host/conn_internal.h"
22 #include "host/l2cap_internal.h"
23 
24 #include "utils.h"
25 #include "common.h"
26 #include "bstests.h"
27 
28 #include <zephyr/logging/log.h>
29 LOG_MODULE_REGISTER(bt_tinyhost, 3);
30 
31 DEFINE_FLAG(is_connected);
32 DEFINE_FLAG(flag_l2cap_connected);
33 DEFINE_FLAG(flag_data_length_updated);
34 
35 static K_FIFO_DEFINE(rx_queue);
36 
37 #define CMD_BUF_SIZE MAX(BT_BUF_EVT_RX_SIZE, BT_BUF_CMD_TX_SIZE)
38 NET_BUF_POOL_FIXED_DEFINE(hci_cmd_pool, CONFIG_BT_BUF_CMD_TX_COUNT,
39 			  CMD_BUF_SIZE, 8, NULL);
40 
41 static K_SEM_DEFINE(cmd_sem, 1, 1);
42 static struct k_sem acl_pkts;
43 static struct k_sem tx_credits;
44 static uint16_t peer_mps;
45 static uint16_t conn_handle;
46 
47 static uint16_t active_opcode = 0xFFFF;
48 static struct net_buf *cmd_rsp;
49 
bt_hci_cmd_create(uint16_t opcode,uint8_t param_len)50 struct net_buf *bt_hci_cmd_create(uint16_t opcode, uint8_t param_len)
51 {
52 	struct bt_hci_cmd_hdr *hdr;
53 	struct net_buf *buf;
54 
55 	LOG_DBG("opcode 0x%04x param_len %u", opcode, param_len);
56 
57 	buf = net_buf_alloc(&hci_cmd_pool, K_FOREVER);
58 	__ASSERT_NO_MSG(buf);
59 
60 	LOG_DBG("buf %p", buf);
61 
62 	net_buf_reserve(buf, BT_BUF_RESERVE);
63 
64 	bt_buf_set_type(buf, BT_BUF_CMD);
65 
66 	hdr = net_buf_add(buf, sizeof(*hdr));
67 	hdr->opcode = sys_cpu_to_le16(opcode);
68 	hdr->param_len = param_len;
69 
70 	return buf;
71 }
72 
handle_cmd_complete(struct net_buf * buf)73 static void handle_cmd_complete(struct net_buf *buf)
74 {
75 	struct bt_hci_evt_hdr *hdr;
76 	uint8_t status, ncmd;
77 	uint16_t opcode;
78 
79 	struct net_buf_simple_state state;
80 
81 	net_buf_simple_save(&buf->b, &state);
82 
83 	hdr = net_buf_pull_mem(buf, sizeof(*hdr));
84 
85 	if (hdr->evt == BT_HCI_EVT_CMD_COMPLETE) {
86 		struct bt_hci_evt_cmd_complete *evt;
87 
88 		evt = net_buf_pull_mem(buf, sizeof(*evt));
89 		status = 0;
90 		ncmd = evt->ncmd;
91 		opcode = sys_le16_to_cpu(evt->opcode);
92 
93 	} else if (hdr->evt == BT_HCI_EVT_CMD_STATUS) {
94 		struct bt_hci_evt_cmd_status *evt;
95 
96 		evt = net_buf_pull_mem(buf, sizeof(*evt));
97 		status = buf->data[0];
98 		ncmd = evt->ncmd;
99 		opcode = sys_le16_to_cpu(evt->opcode);
100 
101 	} else {
102 		__ASSERT_NO_MSG(0);
103 	}
104 
105 	LOG_DBG("opcode 0x%04x status %x", opcode, status);
106 
107 	__ASSERT(status == 0x00, "cmd status: %x", status);
108 
109 	__ASSERT(active_opcode == opcode, "unexpected opcode %x != %x", active_opcode, opcode);
110 
111 	if (active_opcode) {
112 		active_opcode = 0xFFFF;
113 		cmd_rsp = net_buf_ref(buf);
114 		net_buf_simple_restore(&buf->b, &state);
115 	}
116 
117 	if (ncmd) {
118 		k_sem_give(&cmd_sem);
119 	}
120 }
121 
handle_meta_event(struct net_buf * buf)122 static void handle_meta_event(struct net_buf *buf)
123 {
124 	uint8_t code = buf->data[2];
125 
126 	switch (code) {
127 	case BT_HCI_EVT_LE_ENH_CONN_COMPLETE:
128 	case BT_HCI_EVT_LE_ENH_CONN_COMPLETE_V2:
129 		conn_handle = sys_get_le16(&buf->data[4]);
130 		LOG_DBG("connected: handle: %d", conn_handle);
131 		SET_FLAG(is_connected);
132 		break;
133 	case BT_HCI_EVT_LE_DATA_LEN_CHANGE:
134 		SET_FLAG(flag_data_length_updated);
135 		break;
136 	case BT_HCI_EVT_LE_CHAN_SEL_ALGO:
137 		/* do nothing */
138 		break;
139 	default:
140 		LOG_ERR("unhandled meta event %x", code);
141 		LOG_HEXDUMP_ERR(buf->data, buf->len, "HCI META EVT");
142 	}
143 }
144 
handle_ncp(struct net_buf * buf)145 static void handle_ncp(struct net_buf *buf)
146 {
147 	struct bt_hci_evt_hdr *hdr;
148 
149 	hdr = net_buf_pull_mem(buf, sizeof(*hdr));
150 
151 	struct bt_hci_evt_num_completed_packets *evt = (void *)buf->data;
152 
153 	uint16_t handle, count;
154 
155 	handle = sys_le16_to_cpu(evt->h[0].handle);
156 	count = sys_le16_to_cpu(evt->h[0].count);
157 
158 	LOG_DBG("sent %d packets", count);
159 
160 	while (count--) {
161 		k_sem_give(&acl_pkts);
162 	}
163 }
164 
handle_l2cap_credits(struct net_buf * buf)165 static void handle_l2cap_credits(struct net_buf *buf)
166 {
167 	struct bt_l2cap_le_credits *ev = (void *)buf->data;
168 	uint16_t credits = sys_le16_to_cpu(ev->credits);
169 
170 	LOG_DBG("got credits: %d", credits);
171 	while (credits--) {
172 		k_sem_give(&tx_credits);
173 	}
174 }
175 
handle_l2cap_connected(struct net_buf * buf)176 static void handle_l2cap_connected(struct net_buf *buf)
177 {
178 	struct bt_l2cap_le_conn_rsp *rsp = (void *)buf->data;
179 
180 	uint16_t credits = sys_le16_to_cpu(rsp->credits);
181 	uint16_t mtu = sys_le16_to_cpu(rsp->mtu);
182 	uint16_t mps = sys_le16_to_cpu(rsp->mps);
183 
184 	peer_mps = mps;
185 
186 	LOG_DBG("l2cap connected: mtu %d mps %d credits: %d", mtu, mps, credits);
187 
188 	k_sem_init(&tx_credits, credits, credits);
189 	SET_FLAG(flag_l2cap_connected);
190 }
191 
handle_sig(struct net_buf * buf)192 static void handle_sig(struct net_buf *buf)
193 {
194 	struct bt_l2cap_sig_hdr *hdr;
195 
196 	hdr = net_buf_pull_mem(buf, sizeof(*hdr));
197 
198 	switch (hdr->code) {
199 	case BT_L2CAP_LE_CONN_RSP:
200 		handle_l2cap_connected(buf);
201 		return;
202 	case BT_L2CAP_LE_CREDITS:
203 		handle_l2cap_credits(buf);
204 		return;
205 	case BT_L2CAP_DISCONN_REQ:
206 		FAIL("channel disconnected\n");
207 		return;
208 	default:
209 		FAIL("unhandled opcode %x\n", hdr->code);
210 		return;
211 	}
212 }
213 
handle_l2cap(struct net_buf * buf)214 static void handle_l2cap(struct net_buf *buf)
215 {
216 	struct bt_l2cap_hdr *hdr;
217 	uint16_t cid;
218 
219 	hdr = net_buf_pull_mem(buf, sizeof(*hdr));
220 	cid = sys_le16_to_cpu(hdr->cid);
221 
222 	__ASSERT_NO_MSG(buf->len == hdr->len);
223 	LOG_DBG("Packet for CID %u len %u", cid, buf->len);
224 	LOG_HEXDUMP_DBG(buf->data, buf->len, "l2cap");
225 
226 	/* signaling PDU */
227 	if (cid == 0x0005) {
228 		handle_sig(buf);
229 		return;
230 	}
231 
232 	/* CoC PDU */
233 	if (cid == 0x0040) {
234 		FAIL("unexpected data rx");
235 	}
236 }
237 
handle_acl(struct net_buf * buf)238 static void handle_acl(struct net_buf *buf)
239 {
240 	struct bt_hci_acl_hdr *hdr;
241 	uint16_t len, handle;
242 	uint8_t flags;
243 
244 	hdr = net_buf_pull_mem(buf, sizeof(*hdr));
245 	len = sys_le16_to_cpu(hdr->len);
246 	handle = sys_le16_to_cpu(hdr->handle);
247 
248 	flags = bt_acl_flags(handle);
249 	handle = bt_acl_handle(handle);
250 
251 	/* fragmentation not supported */
252 	__ASSERT_NO_MSG(flags == BT_ACL_START);
253 
254 	LOG_DBG("ACL: conn %d len %d flags %d", handle, len, flags);
255 	LOG_HEXDUMP_DBG(buf->data, buf->len, "HCI ACL");
256 
257 	handle_l2cap(buf);
258 }
259 
recv(struct net_buf * buf)260 static void recv(struct net_buf *buf)
261 {
262 	LOG_HEXDUMP_DBG(buf->data, buf->len, "HCI RX");
263 
264 	uint8_t code = buf->data[0];
265 
266 	if (bt_buf_get_type(buf) == BT_BUF_EVT) {
267 		switch (code) {
268 		case BT_HCI_EVT_CMD_COMPLETE:
269 		case BT_HCI_EVT_CMD_STATUS:
270 			handle_cmd_complete(buf);
271 			break;
272 		case BT_HCI_EVT_LE_META_EVENT:
273 			handle_meta_event(buf);
274 			break;
275 		case BT_HCI_EVT_DISCONN_COMPLETE:
276 			UNSET_FLAG(is_connected);
277 			break;
278 		case BT_HCI_EVT_NUM_COMPLETED_PACKETS:
279 			handle_ncp(buf);
280 			break;
281 		default:
282 			LOG_ERR("unhandled msg %x", code);
283 			LOG_HEXDUMP_ERR(buf->data, buf->len, "HCI EVT");
284 		}
285 
286 		/* handlers should take a ref if they want to access the buffer
287 		 * later
288 		 */
289 		net_buf_unref(buf);
290 		return;
291 	}
292 
293 	if (bt_buf_get_type(buf) == BT_BUF_ACL_IN) {
294 		handle_acl(buf);
295 		net_buf_unref(buf);
296 		return;
297 	}
298 
299 	LOG_ERR("HCI RX (not data or event)");
300 	net_buf_unref(buf);
301 }
302 
send_cmd(uint16_t opcode,struct net_buf * cmd,struct net_buf ** rsp)303 static void send_cmd(uint16_t opcode, struct net_buf *cmd, struct net_buf **rsp)
304 {
305 	LOG_DBG("opcode %x", opcode);
306 
307 	if (!cmd) {
308 		cmd = bt_hci_cmd_create(opcode, 0);
309 	}
310 
311 	k_sem_take(&cmd_sem, K_FOREVER);
312 	__ASSERT_NO_MSG(active_opcode == 0xFFFF);
313 
314 	active_opcode = opcode;
315 
316 	LOG_HEXDUMP_DBG(cmd->data, cmd->len, "HCI TX");
317 	bt_send(cmd);
318 
319 	/* Wait until the command completes */
320 	k_sem_take(&cmd_sem, K_FOREVER);
321 	k_sem_give(&cmd_sem);
322 
323 	net_buf_unref(cmd);
324 
325 	/* return response. it's okay if cmd_rsp gets overwritten, since the app
326 	 * gets the ref to the underlying buffer when this fn returns.
327 	 */
328 	if (rsp) {
329 		*rsp = cmd_rsp;
330 	} else {
331 		net_buf_unref(cmd_rsp);
332 		cmd_rsp = NULL;
333 	}
334 }
335 
336 static K_THREAD_STACK_DEFINE(rx_thread_stack, 1024);
337 static struct k_thread rx_thread_data;
338 
rx_thread(void * p1,void * p2,void * p3)339 static void rx_thread(void *p1, void *p2, void *p3)
340 {
341 	LOG_DBG("start HCI rx");
342 
343 	while (1) {
344 		struct net_buf *buf;
345 
346 		/* Wait until a buffer is available */
347 		buf = k_fifo_get(&rx_queue, K_FOREVER);
348 		recv(buf);
349 	}
350 }
351 
le_read_buffer_size_complete(struct net_buf * rsp)352 static void le_read_buffer_size_complete(struct net_buf *rsp)
353 {
354 	struct bt_hci_rp_le_read_buffer_size *rp = (void *)rsp->data;
355 
356 	LOG_DBG("status 0x%02x", rp->status);
357 	LOG_DBG("max len %d max num %d", rp->le_max_len, rp->le_max_num);
358 
359 	k_sem_init(&acl_pkts, rp->le_max_num, rp->le_max_num);
360 	net_buf_unref(rsp);
361 }
362 
read_max_data_len(uint16_t * tx_octets,uint16_t * tx_time)363 static void read_max_data_len(uint16_t *tx_octets, uint16_t *tx_time)
364 {
365 	struct bt_hci_rp_le_read_max_data_len *rp;
366 	struct net_buf *rsp;
367 
368 	send_cmd(BT_HCI_OP_LE_READ_MAX_DATA_LEN, NULL, &rsp);
369 
370 	rp = (void *)rsp->data;
371 	*tx_octets = sys_le16_to_cpu(rp->max_tx_octets);
372 	*tx_time = sys_le16_to_cpu(rp->max_tx_time);
373 	net_buf_unref(rsp);
374 }
375 
write_default_data_len(uint16_t tx_octets,uint16_t tx_time)376 static void write_default_data_len(uint16_t tx_octets, uint16_t tx_time)
377 {
378 	struct bt_hci_cp_le_write_default_data_len *cp;
379 	struct net_buf *buf = bt_hci_cmd_create(BT_HCI_OP_LE_WRITE_DEFAULT_DATA_LEN, sizeof(*cp));
380 
381 	__ASSERT_NO_MSG(buf);
382 
383 	cp = net_buf_add(buf, sizeof(*cp));
384 	cp->max_tx_octets = sys_cpu_to_le16(tx_octets);
385 	cp->max_tx_time = sys_cpu_to_le16(tx_time);
386 
387 	send_cmd(BT_HCI_OP_LE_WRITE_DEFAULT_DATA_LEN, buf, NULL);
388 }
389 
set_data_len(void)390 static void set_data_len(void)
391 {
392 	uint16_t tx_octets, tx_time;
393 
394 	read_max_data_len(&tx_octets, &tx_time);
395 	write_default_data_len(tx_octets, tx_time);
396 }
397 
set_event_mask(uint16_t opcode)398 static void set_event_mask(uint16_t opcode)
399 {
400 	struct bt_hci_cp_set_event_mask *cp_mask;
401 	struct net_buf *buf;
402 	uint64_t mask = 0U;
403 
404 	/* The two commands have the same length/params */
405 	buf = bt_hci_cmd_create(opcode, sizeof(*cp_mask));
406 	__ASSERT_NO_MSG(buf);
407 
408 	/* Forward all events */
409 	cp_mask = net_buf_add(buf, sizeof(*cp_mask));
410 	mask = UINT64_MAX;
411 	sys_put_le64(mask, cp_mask->events);
412 
413 	send_cmd(opcode, buf, NULL);
414 }
415 
set_random_address(void)416 static void set_random_address(void)
417 {
418 	struct net_buf *buf;
419 	bt_addr_le_t addr = {BT_ADDR_LE_RANDOM, {{0x0A, 0x89, 0x67, 0x45, 0x23, 0xC1}}};
420 
421 	LOG_DBG("%s", bt_addr_str(&addr.a));
422 
423 	buf = bt_hci_cmd_create(BT_HCI_OP_LE_SET_RANDOM_ADDRESS, sizeof(addr.a));
424 	__ASSERT_NO_MSG(buf);
425 
426 	net_buf_add_mem(buf, &addr.a, sizeof(addr.a));
427 	send_cmd(BT_HCI_OP_LE_SET_RANDOM_ADDRESS, buf, NULL);
428 }
429 
start_adv(uint16_t interval)430 void start_adv(uint16_t interval)
431 {
432 	struct bt_hci_cp_le_set_adv_param set_param;
433 	struct net_buf *buf;
434 
435 	(void)memset(&set_param, 0, sizeof(set_param));
436 
437 	set_param.min_interval = sys_cpu_to_le16(interval);
438 	set_param.max_interval = sys_cpu_to_le16(interval);
439 	set_param.channel_map = 0x07;
440 	set_param.filter_policy = BT_LE_ADV_FP_NO_FILTER;
441 	set_param.type = BT_HCI_ADV_IND;
442 	set_param.own_addr_type = BT_HCI_OWN_ADDR_RANDOM;
443 
444 	buf = bt_hci_cmd_create(BT_HCI_OP_LE_SET_ADV_PARAM, sizeof(set_param));
445 	__ASSERT_NO_MSG(buf);
446 	net_buf_add_mem(buf, &set_param, sizeof(set_param));
447 
448 	send_cmd(BT_HCI_OP_LE_SET_ADV_PARAM, buf, NULL);
449 
450 	buf = bt_hci_cmd_create(BT_HCI_OP_LE_SET_ADV_ENABLE, 1);
451 	__ASSERT_NO_MSG(buf);
452 
453 	net_buf_add_u8(buf, BT_HCI_LE_ADV_ENABLE);
454 	send_cmd(BT_HCI_OP_LE_SET_ADV_ENABLE, buf, NULL);
455 }
456 
457 NET_BUF_POOL_DEFINE(acl_tx_pool, 5, BT_L2CAP_BUF_SIZE(200), 8, NULL);
458 
alloc_l2cap_pdu(void)459 struct net_buf *alloc_l2cap_pdu(void)
460 {
461 	struct net_buf *buf;
462 	uint16_t reserve;
463 
464 	buf = net_buf_alloc(&acl_tx_pool, K_FOREVER);
465 	__ASSERT_NO_MSG(buf);
466 
467 	reserve = sizeof(struct bt_l2cap_hdr);
468 	reserve += sizeof(struct bt_hci_acl_hdr) + BT_BUF_RESERVE;
469 
470 	net_buf_reserve(buf, reserve);
471 
472 	return buf;
473 }
474 
l2cap_create_le_sig_pdu(uint8_t code,uint8_t ident,uint16_t len)475 static struct net_buf *l2cap_create_le_sig_pdu(uint8_t code, uint8_t ident, uint16_t len)
476 {
477 	struct bt_l2cap_sig_hdr *hdr;
478 	struct net_buf *buf;
479 
480 	buf = alloc_l2cap_pdu();
481 
482 	hdr = net_buf_add(buf, sizeof(*hdr));
483 	hdr->code = code;
484 	hdr->ident = ident;
485 	hdr->len = sys_cpu_to_le16(len);
486 
487 	return buf;
488 }
489 
send_acl(struct net_buf * buf)490 static int send_acl(struct net_buf *buf)
491 {
492 	struct bt_hci_acl_hdr *hdr;
493 	uint8_t flags = BT_ACL_START_NO_FLUSH;
494 
495 	hdr = net_buf_push(buf, sizeof(*hdr));
496 	hdr->handle = sys_cpu_to_le16(bt_acl_handle_pack(conn_handle, flags));
497 	hdr->len = sys_cpu_to_le16(buf->len - sizeof(*hdr));
498 
499 	bt_buf_set_type(buf, BT_BUF_ACL_OUT);
500 
501 	k_sem_take(&acl_pkts, K_FOREVER);
502 
503 	return bt_send(buf);
504 }
505 
send_l2cap_packet(struct net_buf * buf,uint16_t cid)506 static void send_l2cap_packet(struct net_buf *buf, uint16_t cid)
507 {
508 	struct bt_l2cap_hdr *hdr;
509 
510 	hdr = net_buf_push(buf, sizeof(*hdr));
511 	hdr->len = sys_cpu_to_le16(buf->len - sizeof(*hdr));
512 	hdr->cid = sys_cpu_to_le16(cid);
513 
514 	/* Always entire packets, no HCI fragmentation */
515 	__ASSERT_NO_MSG(buf->len <= CONFIG_BT_BUF_ACL_TX_SIZE);
516 
517 	send_acl(buf);
518 }
519 
open_l2cap(void)520 static void open_l2cap(void)
521 {
522 	struct net_buf *buf;
523 	struct bt_l2cap_le_conn_req *req;
524 
525 	buf = l2cap_create_le_sig_pdu(BT_L2CAP_LE_CONN_REQ, 1, sizeof(*req));
526 
527 	req = net_buf_add(buf, sizeof(*req));
528 	req->psm = sys_cpu_to_le16(L2CAP_PSM);
529 	req->scid = sys_cpu_to_le16(L2CAP_CID);
530 
531 	/* we don't intend on receiving anything. use the smallest allowed
532 	 * values and no initial credits.
533 	 */
534 	req->mtu = sys_cpu_to_le16(23);
535 	req->mps = sys_cpu_to_le16(23);
536 	req->credits = sys_cpu_to_le16(0);
537 
538 	send_l2cap_packet(buf, BT_L2CAP_CID_LE_SIG);
539 
540 	WAIT_FOR_FLAG(flag_l2cap_connected);
541 }
542 
send_l2cap_sdu(uint8_t * data,uint16_t sdu_len,uint16_t mps)543 static void send_l2cap_sdu(uint8_t *data, uint16_t sdu_len, uint16_t mps)
544 {
545 	uint16_t pdu_len;
546 
547 	/* have some fun with the packet size if not specified */
548 	bool shenanigans = !mps;
549 	int increment = -1;
550 
551 	if (!mps) {
552 		/* need at least two bytes to fit the SDU length */
553 		mps = 2;
554 	}
555 
556 	for (int i = 0; sdu_len; i++) {
557 		struct net_buf *buf = net_buf_alloc(&acl_tx_pool, K_FOREVER);
558 
559 		__ASSERT_NO_MSG(buf);
560 		net_buf_reserve(buf, BT_L2CAP_SDU_CHAN_SEND_RESERVE);
561 
562 		pdu_len = MIN(sdu_len, mps);
563 
564 		if (i == 0) {
565 			/* add SDU len to first packet */
566 			net_buf_push_le16(buf, sdu_len);
567 			pdu_len -= BT_L2CAP_SDU_HDR_SIZE;
568 		}
569 
570 		net_buf_add_mem(buf, data, pdu_len); /* add data */
571 
572 		data = &data[pdu_len];
573 		sdu_len -= pdu_len;
574 
575 		if (shenanigans) {
576 			if (mps == 1) {
577 				increment = 1;
578 			} else if (mps == 10) {
579 				increment = -1;
580 			}
581 			mps += increment;
582 		}
583 
584 		LOG_INF("send PDU %d (%d bytes, remaining %d)", i, buf->len, sdu_len);
585 		LOG_HEXDUMP_DBG(buf->data, buf->len, "PDU");
586 
587 		k_sem_take(&tx_credits, K_FOREVER);
588 		send_l2cap_packet(buf, 0x0040);
589 	}
590 
591 	LOG_INF("SDU sent ok");
592 }
593 
test_procedure_0(void)594 void test_procedure_0(void)
595 {
596 	bt_enable_raw(&rx_queue);
597 
598 	/* Start the RX thread */
599 	k_thread_create(&rx_thread_data, rx_thread_stack,
600 			K_THREAD_STACK_SIZEOF(rx_thread_stack), rx_thread,
601 			NULL, NULL, NULL, K_PRIO_PREEMPT(0), 0, K_NO_WAIT);
602 	k_thread_name_set(&rx_thread_data, "HCI RX");
603 
604 	k_thread_priority_set(k_current_get(), K_PRIO_PREEMPT(0));
605 
606 	/* Initialize controller */
607 	struct net_buf *rsp;
608 
609 	send_cmd(BT_HCI_OP_RESET, NULL, NULL);
610 	send_cmd(BT_HCI_OP_LE_READ_BUFFER_SIZE, NULL, &rsp);
611 	le_read_buffer_size_complete(rsp);
612 
613 	set_data_len();
614 	set_event_mask(BT_HCI_OP_SET_EVENT_MASK);
615 	set_event_mask(BT_HCI_OP_LE_SET_EVENT_MASK);
616 	set_random_address();
617 
618 	/* Start advertising & wait for a connection */
619 	start_adv(60); /* Interval doesn't matter */
620 	WAIT_FOR_FLAG(is_connected);
621 	LOG_DBG("connected");
622 
623 	/* We need this to be able to send whole L2CAP PDUs on-air. */
624 	WAIT_FOR_FLAG(flag_data_length_updated);
625 
626 	/* Connect to the central's dynamic L2CAP server */
627 	open_l2cap();
628 
629 	/* Prepare the data for sending */
630 	uint8_t data[L2CAP_SDU_LEN];
631 
632 	for (int i = 0; i < ARRAY_SIZE(data); i++) {
633 		data[i] = (uint8_t)i;
634 	}
635 
636 	/* Send the first SDU, lowering the PDU size for each subsequent PDU */
637 	send_l2cap_sdu(data, sizeof(data), 0);
638 
639 	/* Send the second SDU respecting peer's MPS */
640 	send_l2cap_sdu(data, sizeof(data), peer_mps);
641 
642 	WAIT_FOR_FLAG_UNSET(is_connected);
643 	LOG_DBG("disconnected");
644 
645 	PASS("Tester done\n");
646 }
647 
test_tick(bs_time_t HW_device_time)648 void test_tick(bs_time_t HW_device_time)
649 {
650 	bs_trace_debug_time(0, "Simulation ends now.\n");
651 	if (bst_result != Passed) {
652 		bst_result = Failed;
653 		bs_trace_error("Test did not pass before simulation ended.\n");
654 	}
655 }
656 
test_init(void)657 void test_init(void)
658 {
659 	bst_ticker_set_next_tick_absolute(TEST_TIMEOUT_SIMULATED);
660 	bst_result = In_progress;
661 }
662 
663 static const struct bst_test_instance test_to_add[] = {
664 	{
665 		.test_id = "test_0",
666 		.test_pre_init_f = test_init,
667 		.test_tick_f = test_tick,
668 		.test_main_f = test_procedure_0,
669 	},
670 	BSTEST_END_MARKER,
671 };
672 
install(struct bst_test_list * tests)673 static struct bst_test_list *install(struct bst_test_list *tests)
674 {
675 	return bst_add_tests(tests, test_to_add);
676 };
677 
678 bst_test_install_t test_installers[] = {install, NULL};
679 
680 
main(void)681 int main(void)
682 {
683 	bst_main();
684 
685 	return 0;
686 }
687