1 /*
2 * Copyright (c) 2025 Nordic Semiconductor ASA
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6 #include <stddef.h>
7 #include <stdint.h>
8
9 #include <zephyr/bluetooth/addr.h>
10 #include <zephyr/kernel.h>
11 #include <zephyr/logging/log.h>
12 #include <zephyr/net_buf.h>
13 #include <zephyr/sys/util_macro.h>
14
15 #include "babblekit/testcase.h"
16 #include "bstests.h"
17
18 #include "btp/btp.h"
19 #include "bsim_btp.h"
20
21 LOG_MODULE_REGISTER(bsim_gap_central, CONFIG_BSIM_BTTESTER_LOG_LEVEL);
22
test_gap_central(void)23 static void test_gap_central(void)
24 {
25 char addr_str[BT_ADDR_LE_STR_LEN];
26 bt_addr_le_t remote_addr;
27 bt_addr_le_t ev_addr;
28
29 bsim_btp_uart_init();
30
31 bsim_btp_wait_for_evt(BTP_SERVICE_ID_CORE, BTP_CORE_EV_IUT_READY, NULL);
32
33 bsim_btp_core_register(BTP_SERVICE_ID_GAP);
34 bsim_btp_gap_start_discovery(BTP_GAP_DISCOVERY_FLAG_LE);
35 bsim_btp_wait_for_gap_device_found(&remote_addr);
36 bt_addr_le_to_str(&remote_addr, addr_str, sizeof(addr_str));
37 LOG_INF("Found remote device %s", addr_str);
38
39 bsim_btp_gap_stop_discovery();
40 bsim_btp_gap_connect(&remote_addr, BTP_GAP_ADDR_TYPE_IDENTITY);
41 bsim_btp_wait_for_gap_device_connected(&ev_addr);
42 TEST_ASSERT(bt_addr_le_eq(&remote_addr, &ev_addr));
43 LOG_INF("Device %s connected", addr_str);
44
45 /* Keep alive for a little while */
46 k_sleep(K_SECONDS(10));
47
48 bsim_btp_gap_disconnect(&remote_addr);
49 bsim_btp_wait_for_gap_device_disconnected(&ev_addr);
50 TEST_ASSERT(bt_addr_le_eq(&remote_addr, &ev_addr));
51 LOG_INF("Device %s disconnected", addr_str);
52
53 TEST_PASS("PASSED\n");
54 }
55
56 static const struct bst_test_instance test_sample[] = {
57 {
58 .test_id = "gap_central",
59 .test_descr = "Smoketest for the GAP central BT Tester behavior",
60 .test_main_f = test_gap_central,
61 },
62 BSTEST_END_MARKER,
63 };
64
test_gap_central_install(struct bst_test_list * tests)65 struct bst_test_list *test_gap_central_install(struct bst_test_list *tests)
66 {
67 return bst_add_tests(tests, test_sample);
68 }
69