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