1 /*
2  * Copyright (c) 2024 Nordic Semiconductor
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include "bstests.h"
8 #include "babblekit/testcase.h"
9 #include <zephyr/bluetooth/conn.h>
10 
11 static K_SEM_DEFINE(sem_connected, 0, 1);
12 
connected_cb(struct bt_conn * conn,uint8_t err)13 static void connected_cb(struct bt_conn *conn, uint8_t err)
14 {
15 	TEST_ASSERT(conn);
16 	TEST_ASSERT(err == 0, "Expected success");
17 
18 	k_sem_give(&sem_connected);
19 	bt_conn_unref(conn);
20 }
21 
22 static struct bt_conn_cb conn_cb = {
23 	.connected = connected_cb,
24 };
25 
test_peripheral_dummy(void)26 static void test_peripheral_dummy(void)
27 {
28 	int err;
29 	const struct bt_data ad[] = {
30 		BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR))};
31 
32 	bt_conn_cb_register(&conn_cb);
33 
34 	bt_addr_le_t addr = {.type = BT_ADDR_LE_RANDOM,
35 			     .a.val = {0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0}};
36 
37 	err = bt_id_create(&addr, NULL);
38 	TEST_ASSERT(err == 0, "Failed to create iD (err %d)", err);
39 
40 	/* Initialize Bluetooth */
41 	err = bt_enable(NULL);
42 	TEST_ASSERT(err == 0, "Can't enable Bluetooth (err %d)", err);
43 
44 	err = bt_le_adv_start(BT_LE_ADV_CONN_FAST_1, ad, ARRAY_SIZE(ad), NULL, 0);
45 	TEST_ASSERT(err == 0, "Advertising failed to start (err %d)", err);
46 
47 	err = k_sem_take(&sem_connected, K_FOREVER);
48 	TEST_ASSERT(err == 0, "Failed getting connected timeout", err);
49 
50 	TEST_PASS("Passed");
51 }
52 
53 static const struct bst_test_instance test_def[] = {
54 	{
55 		.test_id = "peripheral_dummy",
56 		.test_descr = "Connectable peripheral",
57 		.test_tick_f = bst_tick,
58 		.test_main_f = test_peripheral_dummy,
59 	},
60 	BSTEST_END_MARKER,
61 };
62 
test_peripheral_install(struct bst_test_list * tests)63 struct bst_test_list *test_peripheral_install(struct bst_test_list *tests)
64 {
65 	return bst_add_tests(tests, test_def);
66 }
67