• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2023 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14 
15 #include "pw_bluetooth_sapphire/internal/host/hci/advertising_handle_map.h"
16 
17 #include "pw_bluetooth_sapphire/internal/host/hci-spec/constants.h"
18 #include "pw_bluetooth_sapphire/internal/host/hci-spec/protocol.h"
19 #include "pw_unit_test/framework.h"
20 
21 namespace bt::hci {
22 namespace {
23 
TEST(AdvertisingHandleMapTest,Bidirectional)24 TEST(AdvertisingHandleMapTest, Bidirectional) {
25   AdvertisingHandleMap handle_map;
26 
27   DeviceAddress address_a = DeviceAddress(DeviceAddress::Type::kLEPublic, {0});
28   std::optional<hci_spec::AdvertisingHandle> handle_a =
29       handle_map.MapHandle(address_a);
30   EXPECT_LE(handle_a.value(), hci_spec::kMaxAdvertisingHandle);
31   EXPECT_TRUE(handle_a);
32 
33   DeviceAddress address_b = DeviceAddress(DeviceAddress::Type::kLEPublic, {1});
34   std::optional<hci_spec::AdvertisingHandle> handle_b =
35       handle_map.MapHandle(address_b);
36   EXPECT_TRUE(handle_b);
37   EXPECT_LE(handle_b.value(), hci_spec::kMaxAdvertisingHandle);
38 
39   EXPECT_EQ(address_a, handle_map.GetAddress(handle_a.value()));
40   EXPECT_EQ(address_b, handle_map.GetAddress(handle_b.value()));
41 }
42 
TEST(AdvertisingHandleMapTest,GetHandleDoesntCreateMapping)43 TEST(AdvertisingHandleMapTest, GetHandleDoesntCreateMapping) {
44   AdvertisingHandleMap handle_map;
45   EXPECT_EQ(0u, handle_map.Size());
46   EXPECT_TRUE(handle_map.Empty());
47 
48   DeviceAddress address = DeviceAddress(DeviceAddress::Type::kLEPublic, {0});
49   std::optional<hci_spec::AdvertisingHandle> handle =
50       handle_map.GetHandle(address);
51   EXPECT_EQ(0u, handle_map.Size());
52   EXPECT_TRUE(handle_map.Empty());
53   EXPECT_FALSE(handle);
54 
55   handle = handle_map.MapHandle(address);
56   EXPECT_EQ(1u, handle_map.Size());
57   EXPECT_FALSE(handle_map.Empty());
58   EXPECT_TRUE(handle);
59   EXPECT_EQ(0u, handle.value());
60 
61   handle = handle_map.GetHandle(address);
62   EXPECT_EQ(1u, handle_map.Size());
63   EXPECT_FALSE(handle_map.Empty());
64   EXPECT_TRUE(handle);
65 }
66 
TEST(AdvertisingHandleMapTest,MapHandleAlreadyExists)67 TEST(AdvertisingHandleMapTest, MapHandleAlreadyExists) {
68   AdvertisingHandleMap handle_map;
69 
70   DeviceAddress address = DeviceAddress(DeviceAddress::Type::kLEPublic, {0});
71   std::optional<hci_spec::AdvertisingHandle> expected =
72       handle_map.MapHandle(address);
73   EXPECT_LE(expected.value(), hci_spec::kMaxAdvertisingHandle);
74   ASSERT_TRUE(expected);
75 
76   std::optional<hci_spec::AdvertisingHandle> actual =
77       handle_map.MapHandle(address);
78   EXPECT_LE(actual.value(), hci_spec::kMaxAdvertisingHandle);
79   ASSERT_TRUE(actual);
80   EXPECT_EQ(expected, actual);
81 }
82 
TEST(AdvertisingHandleMapTest,MapHandleMoreThanSupported)83 TEST(AdvertisingHandleMapTest, MapHandleMoreThanSupported) {
84   AdvertisingHandleMap handle_map;
85 
86   for (uint8_t i = 0; i < handle_map.capacity(); i++) {
87     DeviceAddress address = DeviceAddress(DeviceAddress::Type::kLEPublic, {i});
88     std::optional<hci_spec::AdvertisingHandle> handle =
89         handle_map.MapHandle(address);
90     EXPECT_LE(handle.value(), hci_spec::kMaxAdvertisingHandle);
91     EXPECT_TRUE(handle) << "Couldn't add device address " << i;
92     EXPECT_EQ(i + 1u, handle_map.Size());
93   }
94 
95   DeviceAddress address =
96       DeviceAddress(DeviceAddress::Type::kLEPublic, {handle_map.capacity()});
97 
98   std::optional<hci_spec::AdvertisingHandle> handle =
99       handle_map.MapHandle(address);
100   EXPECT_FALSE(handle);
101   EXPECT_EQ(handle_map.capacity(), handle_map.Size());
102 }
103 
TEST(AdvertisingHandleMapTest,MapHandleSupportHandleReallocation)104 TEST(AdvertisingHandleMapTest, MapHandleSupportHandleReallocation) {
105   AdvertisingHandleMap handle_map;
106 
107   for (uint8_t i = 0; i < handle_map.capacity(); i++) {
108     DeviceAddress address = DeviceAddress(DeviceAddress::Type::kLEPublic, {i});
109     std::optional<hci_spec::AdvertisingHandle> handle =
110         handle_map.MapHandle(address);
111     EXPECT_LE(handle.value(), hci_spec::kMaxAdvertisingHandle);
112     EXPECT_TRUE(handle) << "Couldn't add device address " << i;
113     EXPECT_EQ(i + 1u, handle_map.Size());
114   }
115 
116   hci_spec::AdvertisingHandle old_handle = 0;
117   std::optional<DeviceAddress> old_address = handle_map.GetAddress(old_handle);
118   ASSERT_TRUE(old_address);
119 
120   handle_map.RemoveHandle(old_handle);
121 
122   DeviceAddress address =
123       DeviceAddress(DeviceAddress::Type::kLEPublic, {handle_map.capacity()});
124   std::optional<hci_spec::AdvertisingHandle> new_handle =
125       handle_map.MapHandle(address);
126   EXPECT_LE(new_handle.value(), hci_spec::kMaxAdvertisingHandle);
127 
128   ASSERT_TRUE(new_handle);
129   ASSERT_EQ(old_handle, new_handle.value());
130 
131   std::optional<DeviceAddress> new_address =
132       handle_map.GetAddress(new_handle.value());
133   ASSERT_TRUE(new_address);
134   ASSERT_NE(old_address, new_address);
135 }
136 
TEST(AdvertisingHandleMapTest,GetAddressNonExistent)137 TEST(AdvertisingHandleMapTest, GetAddressNonExistent) {
138   AdvertisingHandleMap handle_map;
139   std::optional<DeviceAddress> address = handle_map.GetAddress(0);
140   EXPECT_FALSE(address);
141 }
142 
TEST(AdvertisingHandleMapTest,RemoveHandle)143 TEST(AdvertisingHandleMapTest, RemoveHandle) {
144   AdvertisingHandleMap handle_map;
145   EXPECT_TRUE(handle_map.Empty());
146 
147   DeviceAddress address = DeviceAddress(DeviceAddress::Type::kLEPublic, {0});
148   std::optional<hci_spec::AdvertisingHandle> handle =
149       handle_map.MapHandle(address);
150   EXPECT_LE(handle.value(), hci_spec::kMaxAdvertisingHandle);
151   EXPECT_EQ(1u, handle_map.Size());
152   EXPECT_FALSE(handle_map.Empty());
153 
154   handle_map.RemoveHandle(handle.value());
155   EXPECT_EQ(0u, handle_map.Size());
156   EXPECT_TRUE(handle_map.Empty());
157 }
158 
TEST(AdvertisingHandleMapTest,RemoveAddress)159 TEST(AdvertisingHandleMapTest, RemoveAddress) {
160   AdvertisingHandleMap handle_map;
161   EXPECT_TRUE(handle_map.Empty());
162 
163   DeviceAddress address = DeviceAddress(DeviceAddress::Type::kLEPublic, {0});
164   handle_map.MapHandle(address);
165   EXPECT_EQ(1u, handle_map.Size());
166   EXPECT_FALSE(handle_map.Empty());
167 
168   handle_map.RemoveAddress(address);
169   EXPECT_EQ(0u, handle_map.Size());
170   EXPECT_TRUE(handle_map.Empty());
171 }
172 
TEST(AdvertisingHandleMapTest,RemoveHandleNonExistent)173 TEST(AdvertisingHandleMapTest, RemoveHandleNonExistent) {
174   AdvertisingHandleMap handle_map;
175   DeviceAddress address = DeviceAddress(DeviceAddress::Type::kLEPublic, {0});
176   std::optional<hci_spec::AdvertisingHandle> handle =
177       handle_map.MapHandle(address);
178   ASSERT_TRUE(handle);
179 
180   size_t size = handle_map.Size();
181 
182   handle_map.RemoveHandle(handle.value() + 1);
183 
184   EXPECT_EQ(size, handle_map.Size());
185   handle = handle_map.MapHandle(address);
186   EXPECT_TRUE(handle);
187 }
188 
TEST(AdvertisingHandleMapTest,RemoveAddressNonExistent)189 TEST(AdvertisingHandleMapTest, RemoveAddressNonExistent) {
190   AdvertisingHandleMap handle_map;
191   DeviceAddress address = DeviceAddress(DeviceAddress::Type::kLEPublic, {0});
192   std::optional<hci_spec::AdvertisingHandle> handle =
193       handle_map.MapHandle(address);
194   ASSERT_TRUE(handle);
195 
196   size_t size = handle_map.Size();
197 
198   DeviceAddress nonexistent_address =
199       DeviceAddress(DeviceAddress::Type::kLEPublic, {1});
200   handle_map.RemoveAddress(nonexistent_address);
201 
202   EXPECT_EQ(size, handle_map.Size());
203   handle = handle_map.MapHandle(address);
204   EXPECT_TRUE(handle);
205 }
206 
TEST(AdvertisingHandleMapTest,Clear)207 TEST(AdvertisingHandleMapTest, Clear) {
208   AdvertisingHandleMap handle_map;
209   std::optional<hci_spec::AdvertisingHandle> handle =
210       handle_map.MapHandle(DeviceAddress(DeviceAddress::Type::kLEPublic, {0}));
211   EXPECT_LE(handle.value(), hci_spec::kMaxAdvertisingHandle);
212   EXPECT_TRUE(handle);
213   EXPECT_EQ(1u, handle_map.Size());
214 
215   handle_map.Clear();
216   EXPECT_EQ(0u, handle_map.Size());
217 
218   std::optional<DeviceAddress> address = handle_map.GetAddress(handle.value());
219   EXPECT_FALSE(address);
220 }
221 
222 }  // namespace
223 }  // namespace bt::hci
224