1 /* main_disable.c - Application main entry point */
2 
3 /*
4  * Copyright (c) 2022 Nordic Semiconductor
5  *
6  * SPDX-License-Identifier: Apache-2.0
7  */
8 
9 #include <zephyr/bluetooth/bluetooth.h>
10 
11 #define LOG_MODULE_NAME main_disable
12 #include <zephyr/logging/log.h>
13 LOG_MODULE_REGISTER(LOG_MODULE_NAME, LOG_LEVEL_DBG);
14 
15 #include "babblekit/testcase.h"
16 #include "babblekit/flags.h"
17 #include "common.h"
18 
19 extern enum bst_result_t bst_result;
20 
21 #define NUM_ITERATIONS 35
22 
test_disable_main(void)23 static void test_disable_main(void)
24 {
25 	for (int i = 0; i < NUM_ITERATIONS; i++) {
26 		int err;
27 
28 		err = bt_enable(NULL);
29 		if (err != 0) {
30 			TEST_FAIL("Enable failed (err %d)", err);
31 		}
32 
33 		err = bt_disable();
34 		if (err) {
35 			TEST_FAIL("Enable failed (err %d)", err);
36 		}
37 	}
38 
39 	TEST_PASS("Disable test passed");
40 }
41 
test_disable_set_default_id(void)42 static void test_disable_set_default_id(void)
43 {
44 	/* FIXME: Temporary workaround to get around a bug in the controller.
45 	 * The controller gets stuck in the POWER_CLOCK ISR without this.
46 	 * See open PR: https://github.com/zephyrproject-rtos/zephyr/pull/73342
47 	 * for more details.
48 	 */
49 	k_sleep(K_MSEC(1));
50 
51 	for (int i = 0; i < NUM_ITERATIONS; i++) {
52 		int err;
53 		size_t id_count;
54 		bt_addr_le_t stored_id;
55 		bt_addr_le_t addr = {.type = BT_ADDR_LE_RANDOM,
56 				     .a = {.val = {i, 2, 3, 4, 5, 0xc0}}};
57 
58 		err = bt_id_create(&addr, NULL);
59 		if (err != 0) {
60 			TEST_FAIL("Creating ID failed (err %d)", err);
61 		}
62 
63 		err = bt_enable(NULL);
64 		if (err != 0) {
65 			TEST_FAIL("Enable failed (err %d)", err);
66 		}
67 
68 		bt_id_get(NULL, &id_count);
69 		if (id_count != 1) {
70 			TEST_FAIL("Expected only one ID, but got: %u", id_count);
71 		}
72 
73 		id_count = 1;
74 		bt_id_get(&stored_id, &id_count);
75 		if (id_count != 1) {
76 			TEST_FAIL("Expected only one ID, but got: %u", id_count);
77 		}
78 
79 		if (!bt_addr_le_eq(&stored_id, &addr)) {
80 			uint8_t addr_set_str[BT_ADDR_LE_STR_LEN];
81 			uint8_t addr_stored_str[BT_ADDR_LE_STR_LEN];
82 
83 			bt_addr_le_to_str(&addr, addr_set_str, BT_ADDR_LE_STR_LEN);
84 			bt_addr_le_to_str(&stored_id, addr_stored_str, BT_ADDR_LE_STR_LEN);
85 			TEST_FAIL("Expected stored ID to be equal to set ID: %s, %s", addr_set_str,
86 				  addr_stored_str);
87 		}
88 
89 		err = bt_disable();
90 		if (err) {
91 			TEST_FAIL("Enable failed (err %d)", err);
92 		}
93 	}
94 
95 	TEST_PASS("Disable set default ID test passed");
96 }
97 
98 static const struct bst_test_instance test_def[] = {
99 	{
100 		.test_id = "disable",
101 		.test_descr = "disable_test",
102 		.test_main_f = test_disable_main
103 	},
104 	{
105 		.test_id = "disable_set_default_id",
106 		.test_descr = "disable_test where each iteration sets the default ID",
107 		.test_main_f = test_disable_set_default_id
108 	},
109 	BSTEST_END_MARKER
110 };
111 
test_main_disable_install(struct bst_test_list * tests)112 struct bst_test_list *test_main_disable_install(struct bst_test_list *tests)
113 {
114 	return bst_add_tests(tests, test_def);
115 }
116