1 /*
2  * Copyright (c) 2024 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <stddef.h>
8 
9 #include <zephyr/bluetooth/gap.h>
10 #include <zephyr/ztest.h>
11 #include <zephyr/ztest_test.h>
12 #include <zephyr/ztest_assert.h>
13 
14 ZTEST_SUITE(gap_test_suite, NULL, NULL, NULL, NULL, NULL);
15 
ZTEST(gap_test_suite,test_bt_gap_conversion_macros)16 static ZTEST(gap_test_suite, test_bt_gap_conversion_macros)
17 {
18 	zassert_equal(BT_GAP_ADV_INTERVAL_TO_US(0x0020U), 20000U);
19 	zassert_equal(BT_GAP_ADV_INTERVAL_TO_US(0x0021U), 20625U);
20 	zassert_equal(BT_GAP_ADV_INTERVAL_TO_US(0x0022U), 21250U);
21 
22 	zassert_equal(BT_GAP_ADV_INTERVAL_TO_MS(0x0020U), 20U);
23 	/* Round down expected from 20.625 */
24 	zassert_equal(BT_GAP_ADV_INTERVAL_TO_MS(0x0021U), 20U);
25 	/* Round down expected from 21.250 */
26 	zassert_equal(BT_GAP_ADV_INTERVAL_TO_MS(0x0022U), 21U);
27 
28 	zassert_equal(BT_GAP_ISO_INTERVAL_TO_US(0x0004U), 5000U);
29 	zassert_equal(BT_GAP_ISO_INTERVAL_TO_US(0x0005U), 6250U);
30 	zassert_equal(BT_GAP_ISO_INTERVAL_TO_US(0x0006U), 7500U);
31 
32 	zassert_equal(BT_GAP_ISO_INTERVAL_TO_MS(0x0004U), 5U);
33 	/* Round down expected from 6.25 */
34 	zassert_equal(BT_GAP_ISO_INTERVAL_TO_MS(0x0005U), 6U);
35 	/* Round down expected from 7.50 */
36 	zassert_equal(BT_GAP_ISO_INTERVAL_TO_MS(0x0006U), 7U);
37 
38 	zassert_equal(BT_GAP_PER_ADV_INTERVAL_TO_US(0x0008U), 10000U);
39 	zassert_equal(BT_GAP_PER_ADV_INTERVAL_TO_US(0x0009U), 11250U);
40 	zassert_equal(BT_GAP_PER_ADV_INTERVAL_TO_US(0x000aU), 12500U);
41 
42 	zassert_equal(BT_GAP_PER_ADV_INTERVAL_TO_MS(0x0008U), 10U);
43 	/* Round down expected from 11.25 */
44 	zassert_equal(BT_GAP_PER_ADV_INTERVAL_TO_MS(0x0009U), 11U);
45 	/* Round down expected from 12.50 */
46 	zassert_equal(BT_GAP_PER_ADV_INTERVAL_TO_MS(0x000aU), 12U);
47 
48 	zassert_equal(BT_GAP_US_TO_ADV_INTERVAL(20000U), 0x0020U);
49 	/* Round down expected from 33.60 */
50 	zassert_equal(BT_GAP_US_TO_ADV_INTERVAL(21000U), 0x0021U);
51 	/* Round down expected from 35.20 */
52 	zassert_equal(BT_GAP_US_TO_ADV_INTERVAL(22000U), 0x0023U);
53 
54 	zassert_equal(BT_GAP_MS_TO_ADV_INTERVAL(20U), 0x0020U);
55 	/* Round down expected from 33.60 */
56 	zassert_equal(BT_GAP_MS_TO_ADV_INTERVAL(21U), 0x0021U);
57 	/* Round down expected from 35.20 */
58 	zassert_equal(BT_GAP_MS_TO_ADV_INTERVAL(22U), 0x0023U);
59 
60 	zassert_equal(BT_GAP_US_TO_PER_ADV_INTERVAL(10000U), 0x0008U);
61 	/* Round down expected from 8.8 */
62 	zassert_equal(BT_GAP_US_TO_PER_ADV_INTERVAL(11000U), 0x0008U);
63 	/* Round down expected from 9.6 */
64 	zassert_equal(BT_GAP_US_TO_PER_ADV_INTERVAL(12000U), 0x0009U);
65 
66 	zassert_equal(BT_GAP_MS_TO_PER_ADV_INTERVAL(10U), 0x0008U);
67 	/* Round down expected from 8.8 */
68 	zassert_equal(BT_GAP_MS_TO_PER_ADV_INTERVAL(11U), 0x0008U);
69 	/* Round down expected from 9.6 */
70 	zassert_equal(BT_GAP_MS_TO_PER_ADV_INTERVAL(12U), 0x0009U);
71 
72 	zassert_equal(BT_GAP_MS_TO_PER_ADV_SYNC_TIMEOUT(4000U), 0x0190U);
73 	/* Round down expected from 400.5 */
74 	zassert_equal(BT_GAP_MS_TO_PER_ADV_SYNC_TIMEOUT(4005U), 0x0190U);
75 
76 	zassert_equal(BT_GAP_US_TO_PER_ADV_SYNC_TIMEOUT(4000000U), 0x0190U);
77 	/* Round down expected from 400.5 */
78 	zassert_equal(BT_GAP_US_TO_PER_ADV_SYNC_TIMEOUT(4005000U), 0x0190U);
79 
80 	zassert_equal(BT_GAP_US_TO_SCAN_INTERVAL(20000U), 0x0020U);
81 	/* Round down expected from 33.60 */
82 	zassert_equal(BT_GAP_US_TO_SCAN_INTERVAL(21000U), 0x0021U);
83 	/* Round down expected from 35.20 */
84 	zassert_equal(BT_GAP_US_TO_SCAN_INTERVAL(22000U), 0x0023U);
85 
86 	zassert_equal(BT_GAP_MS_TO_SCAN_INTERVAL(20U), 0x0020U);
87 	/* Round down expected from 33.60 */
88 	zassert_equal(BT_GAP_MS_TO_SCAN_INTERVAL(21U), 0x0021U);
89 	/* Round down expected from 35.20 */
90 	zassert_equal(BT_GAP_MS_TO_SCAN_INTERVAL(22U), 0x0023U);
91 
92 	zassert_equal(BT_GAP_US_TO_SCAN_WINDOW(20000U), 0x0020U);
93 	/* Round down expected from 33.60 */
94 	zassert_equal(BT_GAP_US_TO_SCAN_WINDOW(21000U), 0x0021U);
95 	/* Round down expected from 35.20 */
96 	zassert_equal(BT_GAP_US_TO_SCAN_WINDOW(22000U), 0x0023U);
97 
98 	zassert_equal(BT_GAP_MS_TO_SCAN_WINDOW(20U), 0x0020U);
99 	/* Round down expected from 33.60 */
100 	zassert_equal(BT_GAP_MS_TO_SCAN_WINDOW(21U), 0x0021U);
101 	/* Round down expected from 35.20 */
102 	zassert_equal(BT_GAP_MS_TO_SCAN_WINDOW(22U), 0x0023U);
103 
104 	zassert_equal(BT_GAP_US_TO_CONN_INTERVAL(10000U), 0x0008U);
105 	/* Round down expected from 8.8 */
106 	zassert_equal(BT_GAP_US_TO_CONN_INTERVAL(11000U), 0x0008U);
107 	/* Round down expected from 9.6 */
108 	zassert_equal(BT_GAP_US_TO_CONN_INTERVAL(12000U), 0x0009U);
109 
110 	zassert_equal(BT_GAP_MS_TO_CONN_INTERVAL(10U), 0x0008U);
111 	/* Round down expected from 8.8 */
112 	zassert_equal(BT_GAP_MS_TO_CONN_INTERVAL(11U), 0x0008U);
113 	/* Round down expected from 9.6 */
114 	zassert_equal(BT_GAP_MS_TO_CONN_INTERVAL(12U), 0x0009U);
115 
116 	zassert_equal(BT_GAP_MS_TO_CONN_TIMEOUT(4000U), 0x0190U);
117 	/* Round down expected from 400.5 */
118 	zassert_equal(BT_GAP_MS_TO_CONN_TIMEOUT(4005U), 0x0190U);
119 
120 	zassert_equal(BT_GAP_US_TO_CONN_TIMEOUT(4000000U), 0x0190U);
121 	/* Round down expected from 400.5 */
122 	zassert_equal(BT_GAP_US_TO_CONN_TIMEOUT(4005000U), 0x0190U);
123 
124 	zassert_equal(BT_GAP_US_TO_CONN_EVENT_LEN(20000U), 0x0020U);
125 	/* Round down expected from 33.60 */
126 	zassert_equal(BT_GAP_US_TO_CONN_EVENT_LEN(21000U), 0x0021U);
127 	/* Round down expected from 35.20 */
128 	zassert_equal(BT_GAP_US_TO_CONN_EVENT_LEN(22000U), 0x0023U);
129 
130 	zassert_equal(BT_GAP_MS_TO_CONN_EVENT_LEN(20U), 0x0020U);
131 	/* Round down expected from 33.60 */
132 	zassert_equal(BT_GAP_MS_TO_CONN_EVENT_LEN(21U), 0x0021U);
133 	/* Round down expected from 35.20 */
134 	zassert_equal(BT_GAP_MS_TO_CONN_EVENT_LEN(22U), 0x0023U);
135 }
136