1 /* Copyright (c) 2024 Nordic Semiconductor ASA
2  * SPDX-License-Identifier: Apache-2.0
3  */
4 
5 #include <zephyr/kernel.h>
6 #include <zephyr/bluetooth/bluetooth.h>
7 #include <zephyr/bluetooth/conn.h>
8 #include <zephyr/bluetooth/l2cap.h>
9 #include <zephyr/logging/log.h>
10 #include <zephyr/net_buf.h>
11 #include <zephyr/sys/__assert.h>
12 #include <zephyr/sys/atomic.h>
13 #include <zephyr/sys/util_macro.h>
14 
15 #include <testlib/addr.h>
16 #include <testlib/adv.h>
17 #include <testlib/conn.h>
18 #include <testlib/scan.h>
19 
20 #include <babblekit/flags.h>
21 #include <babblekit/testcase.h>
22 
23 #include "data.h"
24 
25 LOG_MODULE_REGISTER(tester, LOG_LEVEL_INF);
26 
tester_chan_recv_cb(struct bt_l2cap_chan * chan,struct net_buf * buf)27 static int tester_chan_recv_cb(struct bt_l2cap_chan *chan, struct net_buf *buf)
28 {
29 	__ASSERT(false, "Unexpected recv in tester");
30 	return 0;
31 };
32 
33 static struct bt_l2cap_le_chan le_chan = {
34 	.chan.ops =
35 		&(const struct bt_l2cap_chan_ops){
36 			.recv = tester_chan_recv_cb,
37 		},
38 };
39 
40 NET_BUF_POOL_DEFINE(test_pool, 1, BT_L2CAP_SDU_BUF_SIZE(0), CONFIG_BT_CONN_TX_USER_DATA_SIZE, NULL);
41 
entrypoint_tester(void)42 void entrypoint_tester(void)
43 {
44 	struct net_buf *sdu;
45 	struct bt_conn *conn = NULL;
46 	int err;
47 
48 	TEST_START("tester");
49 
50 	err = bt_enable(NULL);
51 	__ASSERT_NO_MSG(!err);
52 
53 	err = bt_testlib_connect(&TEST_DATA_DUT_ADDR, &conn);
54 	__ASSERT_NO_MSG(!err);
55 
56 	err = bt_l2cap_chan_connect(conn, &le_chan.chan, TEST_DATA_L2CAP_PSM);
57 	__ASSERT_NO_MSG(!err);
58 
59 	/* Wait for async L2CAP connect */
60 	while (!atomic_test_bit(le_chan.chan.status, BT_L2CAP_STATUS_OUT)) {
61 		k_sleep(K_MSEC(100));
62 	}
63 
64 	sdu = net_buf_alloc(&test_pool, K_NO_WAIT);
65 	__ASSERT_NO_MSG(sdu);
66 	net_buf_reserve(sdu, BT_L2CAP_SDU_CHAN_SEND_RESERVE);
67 
68 	err = bt_l2cap_chan_send(&le_chan.chan, sdu);
69 	__ASSERT(!err, "err: %d", err);
70 
71 	TEST_PASS("tester");
72 }
73