OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <stdint.h> | 5 #include <stdint.h> |
6 #include <utility> | 6 #include <utility> |
7 | 7 |
8 #include "base/bind.h" | 8 #include "base/bind.h" |
9 #include "base/macros.h" | 9 #include "base/macros.h" |
10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
(...skipping 21 matching lines...) Expand all Loading... |
32 void FakeCharacteristicBoilerplate(int properties = 0) { | 32 void FakeCharacteristicBoilerplate(int properties = 0) { |
33 InitWithFakeAdapter(); | 33 InitWithFakeAdapter(); |
34 StartLowEnergyDiscoverySession(); | 34 StartLowEnergyDiscoverySession(); |
35 device_ = SimulateLowEnergyDevice(3); | 35 device_ = SimulateLowEnergyDevice(3); |
36 device_->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), | 36 device_->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), |
37 GetConnectErrorCallback(Call::NOT_EXPECTED)); | 37 GetConnectErrorCallback(Call::NOT_EXPECTED)); |
38 SimulateGattConnection(device_); | 38 SimulateGattConnection(device_); |
39 base::RunLoop().RunUntilIdle(); | 39 base::RunLoop().RunUntilIdle(); |
40 | 40 |
41 std::vector<std::string> services; | 41 std::vector<std::string> services; |
42 std::string uuid("00000000-0000-1000-8000-00805f9b34fb"); | 42 services.push_back(kTestUUIDGenericAccess); |
43 services.push_back(uuid); | |
44 SimulateGattServicesDiscovered(device_, services); | 43 SimulateGattServicesDiscovered(device_, services); |
45 base::RunLoop().RunUntilIdle(); | 44 base::RunLoop().RunUntilIdle(); |
46 ASSERT_EQ(1u, device_->GetGattServices().size()); | 45 ASSERT_EQ(1u, device_->GetGattServices().size()); |
47 service_ = device_->GetGattServices()[0]; | 46 service_ = device_->GetGattServices()[0]; |
48 SimulateGattCharacteristic(service_, uuid, properties); | 47 SimulateGattCharacteristic(service_, kTestUUIDDeviceName, properties); |
49 SimulateGattCharacteristic(service_, uuid, properties); | 48 SimulateGattCharacteristic(service_, kTestUUIDDeviceName, properties); |
50 ASSERT_EQ(2u, service_->GetCharacteristics().size()); | 49 ASSERT_EQ(2u, service_->GetCharacteristics().size()); |
51 characteristic1_ = service_->GetCharacteristics()[0]; | 50 characteristic1_ = service_->GetCharacteristics()[0]; |
52 characteristic2_ = service_->GetCharacteristics()[1]; | 51 characteristic2_ = service_->GetCharacteristics()[1]; |
53 ResetEventCounts(); | 52 ResetEventCounts(); |
54 } | 53 } |
55 | 54 |
56 enum class StartNotifySetupError { | 55 enum class StartNotifySetupError { |
57 CHARACTERISTIC_PROPERTIES, | 56 CHARACTERISTIC_PROPERTIES, |
58 CONFIG_DESCRIPTOR_MISSING, | 57 CONFIG_DESCRIPTOR_MISSING, |
59 CONFIG_DESCRIPTOR_DUPLICATE, | 58 CONFIG_DESCRIPTOR_DUPLICATE, |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
153 device2->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), | 152 device2->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), |
154 GetConnectErrorCallback(Call::NOT_EXPECTED)); | 153 GetConnectErrorCallback(Call::NOT_EXPECTED)); |
155 SimulateGattConnection(device1); | 154 SimulateGattConnection(device1); |
156 SimulateGattConnection(device2); | 155 SimulateGattConnection(device2); |
157 base::RunLoop().RunUntilIdle(); | 156 base::RunLoop().RunUntilIdle(); |
158 | 157 |
159 // 3 services (all with same UUID). | 158 // 3 services (all with same UUID). |
160 // 1 on the first device (to test characteristic instances across devices). | 159 // 1 on the first device (to test characteristic instances across devices). |
161 // 2 on the second device (to test same device, multiple service instances). | 160 // 2 on the second device (to test same device, multiple service instances). |
162 std::vector<std::string> services; | 161 std::vector<std::string> services; |
163 std::string uuid = "00000000-0000-1000-8000-00805f9b34fb"; | 162 services.push_back(kTestUUIDGenericAccess); |
164 services.push_back(uuid); | |
165 SimulateGattServicesDiscovered(device1, services); | 163 SimulateGattServicesDiscovered(device1, services); |
166 base::RunLoop().RunUntilIdle(); | 164 base::RunLoop().RunUntilIdle(); |
167 services.push_back(uuid); | 165 services.push_back(kTestUUIDGenericAccess); |
168 SimulateGattServicesDiscovered(device2, services); | 166 SimulateGattServicesDiscovered(device2, services); |
169 base::RunLoop().RunUntilIdle(); | 167 base::RunLoop().RunUntilIdle(); |
170 BluetoothRemoteGattService* service1 = device1->GetGattServices()[0]; | 168 BluetoothRemoteGattService* service1 = device1->GetGattServices()[0]; |
171 BluetoothRemoteGattService* service2 = device2->GetGattServices()[0]; | 169 BluetoothRemoteGattService* service2 = device2->GetGattServices()[0]; |
172 BluetoothRemoteGattService* service3 = device2->GetGattServices()[1]; | 170 BluetoothRemoteGattService* service3 = device2->GetGattServices()[1]; |
173 // 6 characteristics (same UUID), 2 on each service. | 171 // 6 characteristics (same UUID), 2 on each service. |
174 SimulateGattCharacteristic(service1, uuid, /* properties */ 0); | 172 SimulateGattCharacteristic(service1, kTestUUIDDeviceName, /* properties */ 0); |
175 SimulateGattCharacteristic(service1, uuid, /* properties */ 0); | 173 SimulateGattCharacteristic(service1, kTestUUIDDeviceName, /* properties */ 0); |
176 SimulateGattCharacteristic(service2, uuid, /* properties */ 0); | 174 SimulateGattCharacteristic(service2, kTestUUIDDeviceName, /* properties */ 0); |
177 SimulateGattCharacteristic(service2, uuid, /* properties */ 0); | 175 SimulateGattCharacteristic(service2, kTestUUIDDeviceName, /* properties */ 0); |
178 SimulateGattCharacteristic(service3, uuid, /* properties */ 0); | 176 SimulateGattCharacteristic(service3, kTestUUIDDeviceName, /* properties */ 0); |
179 SimulateGattCharacteristic(service3, uuid, /* properties */ 0); | 177 SimulateGattCharacteristic(service3, kTestUUIDDeviceName, /* properties */ 0); |
180 BluetoothRemoteGattCharacteristic* char1 = service1->GetCharacteristics()[0]; | 178 BluetoothRemoteGattCharacteristic* char1 = service1->GetCharacteristics()[0]; |
181 BluetoothRemoteGattCharacteristic* char2 = service1->GetCharacteristics()[1]; | 179 BluetoothRemoteGattCharacteristic* char2 = service1->GetCharacteristics()[1]; |
182 BluetoothRemoteGattCharacteristic* char3 = service2->GetCharacteristics()[0]; | 180 BluetoothRemoteGattCharacteristic* char3 = service2->GetCharacteristics()[0]; |
183 BluetoothRemoteGattCharacteristic* char4 = service2->GetCharacteristics()[1]; | 181 BluetoothRemoteGattCharacteristic* char4 = service2->GetCharacteristics()[1]; |
184 BluetoothRemoteGattCharacteristic* char5 = service3->GetCharacteristics()[0]; | 182 BluetoothRemoteGattCharacteristic* char5 = service3->GetCharacteristics()[0]; |
185 BluetoothRemoteGattCharacteristic* char6 = service3->GetCharacteristics()[1]; | 183 BluetoothRemoteGattCharacteristic* char6 = service3->GetCharacteristics()[1]; |
186 | 184 |
187 // All IDs are unique, even though they have the same UUID. | 185 // All IDs are unique, even though they have the same UUID. |
188 EXPECT_NE(char1->GetIdentifier(), char2->GetIdentifier()); | 186 EXPECT_NE(char1->GetIdentifier(), char2->GetIdentifier()); |
189 EXPECT_NE(char1->GetIdentifier(), char3->GetIdentifier()); | 187 EXPECT_NE(char1->GetIdentifier(), char3->GetIdentifier()); |
(...skipping 24 matching lines...) Expand all Loading... |
214 return; | 212 return; |
215 } | 213 } |
216 InitWithFakeAdapter(); | 214 InitWithFakeAdapter(); |
217 StartLowEnergyDiscoverySession(); | 215 StartLowEnergyDiscoverySession(); |
218 BluetoothDevice* device = SimulateLowEnergyDevice(3); | 216 BluetoothDevice* device = SimulateLowEnergyDevice(3); |
219 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), | 217 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), |
220 GetConnectErrorCallback(Call::NOT_EXPECTED)); | 218 GetConnectErrorCallback(Call::NOT_EXPECTED)); |
221 SimulateGattConnection(device); | 219 SimulateGattConnection(device); |
222 base::RunLoop().RunUntilIdle(); | 220 base::RunLoop().RunUntilIdle(); |
223 std::vector<std::string> services; | 221 std::vector<std::string> services; |
224 services.push_back("00000000-0000-1000-8000-00805f9b34fb"); | 222 services.push_back(kTestUUIDGenericAccess); |
225 SimulateGattServicesDiscovered(device, services); | 223 SimulateGattServicesDiscovered(device, services); |
226 base::RunLoop().RunUntilIdle(); | 224 base::RunLoop().RunUntilIdle(); |
227 BluetoothRemoteGattService* service = device->GetGattServices()[0]; | 225 BluetoothRemoteGattService* service = device->GetGattServices()[0]; |
228 | 226 |
229 // Create 3 characteristics. Two of them are duplicates. | 227 // Create 3 characteristics. Two of them are duplicates. |
230 std::string uuid_str1("11111111-0000-1000-8000-00805f9b34fb"); | 228 BluetoothUUID uuid1(kTestUUIDDeviceName); |
231 std::string uuid_str2("22222222-0000-1000-8000-00805f9b34fb"); | 229 BluetoothUUID uuid2(kTestUUIDAppearance); |
232 BluetoothUUID uuid1(uuid_str1); | 230 SimulateGattCharacteristic(service, kTestUUIDDeviceName, /* properties */ 0); |
233 BluetoothUUID uuid2(uuid_str2); | 231 SimulateGattCharacteristic(service, kTestUUIDAppearance, /* properties */ 0); |
234 SimulateGattCharacteristic(service, uuid_str1, /* properties */ 0); | 232 SimulateGattCharacteristic(service, kTestUUIDAppearance, /* properties */ 0); |
235 SimulateGattCharacteristic(service, uuid_str2, /* properties */ 0); | |
236 SimulateGattCharacteristic(service, uuid_str2, /* properties */ 0); | |
237 BluetoothRemoteGattCharacteristic* char1 = service->GetCharacteristics()[0]; | 233 BluetoothRemoteGattCharacteristic* char1 = service->GetCharacteristics()[0]; |
238 BluetoothRemoteGattCharacteristic* char2 = service->GetCharacteristics()[1]; | 234 BluetoothRemoteGattCharacteristic* char2 = service->GetCharacteristics()[1]; |
239 BluetoothRemoteGattCharacteristic* char3 = service->GetCharacteristics()[2]; | 235 BluetoothRemoteGattCharacteristic* char3 = service->GetCharacteristics()[2]; |
240 | 236 |
241 // Swap as needed to have char1 point to the the characteristic with uuid1. | 237 // Swap as needed to have char1 point to the the characteristic with uuid1. |
242 if (char2->GetUUID() == uuid1) { | 238 if (char2->GetUUID() == uuid1) { |
243 std::swap(char1, char2); | 239 std::swap(char1, char2); |
244 } else if (char3->GetUUID() == uuid1) { | 240 } else if (char3->GetUUID() == uuid1) { |
245 std::swap(char1, char3); | 241 std::swap(char1, char3); |
246 } | 242 } |
(...skipping 11 matching lines...) Expand all Loading... |
258 return; | 254 return; |
259 } | 255 } |
260 InitWithFakeAdapter(); | 256 InitWithFakeAdapter(); |
261 StartLowEnergyDiscoverySession(); | 257 StartLowEnergyDiscoverySession(); |
262 BluetoothDevice* device = SimulateLowEnergyDevice(3); | 258 BluetoothDevice* device = SimulateLowEnergyDevice(3); |
263 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), | 259 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), |
264 GetConnectErrorCallback(Call::NOT_EXPECTED)); | 260 GetConnectErrorCallback(Call::NOT_EXPECTED)); |
265 SimulateGattConnection(device); | 261 SimulateGattConnection(device); |
266 base::RunLoop().RunUntilIdle(); | 262 base::RunLoop().RunUntilIdle(); |
267 std::vector<std::string> services; | 263 std::vector<std::string> services; |
268 std::string uuid("00000000-0000-1000-8000-00805f9b34fb"); | 264 services.push_back(kTestUUIDGenericAccess); |
269 services.push_back(uuid); | |
270 SimulateGattServicesDiscovered(device, services); | 265 SimulateGattServicesDiscovered(device, services); |
271 base::RunLoop().RunUntilIdle(); | 266 base::RunLoop().RunUntilIdle(); |
272 BluetoothRemoteGattService* service = device->GetGattServices()[0]; | 267 BluetoothRemoteGattService* service = device->GetGattServices()[0]; |
273 | 268 |
274 // Create two characteristics with different properties: | 269 // Create two characteristics with different properties: |
275 SimulateGattCharacteristic(service, uuid, /* properties */ 0); | 270 SimulateGattCharacteristic(service, kTestUUIDDeviceName, /* properties */ 0); |
276 SimulateGattCharacteristic(service, uuid, /* properties */ 7); | 271 SimulateGattCharacteristic(service, kTestUUIDDeviceName, /* properties */ 7); |
277 | 272 |
278 // Read the properties. Because ordering is unknown swap as necessary. | 273 // Read the properties. Because ordering is unknown swap as necessary. |
279 int properties1 = service->GetCharacteristics()[0]->GetProperties(); | 274 int properties1 = service->GetCharacteristics()[0]->GetProperties(); |
280 int properties2 = service->GetCharacteristics()[1]->GetProperties(); | 275 int properties2 = service->GetCharacteristics()[1]->GetProperties(); |
281 if (properties2 == 0) | 276 if (properties2 == 0) |
282 std::swap(properties1, properties2); | 277 std::swap(properties1, properties2); |
283 EXPECT_EQ(0, properties1); | 278 EXPECT_EQ(0, properties1); |
284 EXPECT_EQ(7, properties2); | 279 EXPECT_EQ(7, properties2); |
285 } | 280 } |
286 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN) | 281 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN) |
(...skipping 1914 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2201 EXPECT_EQ(0u, characteristic1_->GetDescriptors().size()); | 2196 EXPECT_EQ(0u, characteristic1_->GetDescriptors().size()); |
2202 } | 2197 } |
2203 #endif // defined(OS_ANDROID) || defined(OS_WIN) | 2198 #endif // defined(OS_ANDROID) || defined(OS_WIN) |
2204 | 2199 |
2205 #if defined(OS_ANDROID) || defined(OS_WIN) | 2200 #if defined(OS_ANDROID) || defined(OS_WIN) |
2206 TEST_F(BluetoothRemoteGattCharacteristicTest, | 2201 TEST_F(BluetoothRemoteGattCharacteristicTest, |
2207 GetDescriptors_and_GetDescriptor) { | 2202 GetDescriptors_and_GetDescriptor) { |
2208 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate()); | 2203 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate()); |
2209 | 2204 |
2210 // Add several Descriptors: | 2205 // Add several Descriptors: |
2211 BluetoothUUID uuid1("11111111-0000-1000-8000-00805f9b34fb"); | 2206 BluetoothUUID uuid1(kTestUUIDCharacteristicUserDescription); |
2212 BluetoothUUID uuid2("22222222-0000-1000-8000-00805f9b34fb"); | 2207 BluetoothUUID uuid2(kTestUUIDClientCharacteristicConfiguration); |
2213 BluetoothUUID uuid3("33333333-0000-1000-8000-00805f9b34fb"); | 2208 BluetoothUUID uuid3(kTestUUIDServerCharacteristicConfiguration); |
2214 BluetoothUUID uuid4("44444444-0000-1000-8000-00805f9b34fb"); | 2209 BluetoothUUID uuid4(kTestUUIDCharacteristicPresentationFormat); |
2215 SimulateGattDescriptor(characteristic1_, uuid1.canonical_value()); | 2210 SimulateGattDescriptor(characteristic1_, uuid1.canonical_value()); |
2216 SimulateGattDescriptor(characteristic1_, uuid2.canonical_value()); | 2211 SimulateGattDescriptor(characteristic1_, uuid2.canonical_value()); |
2217 SimulateGattDescriptor(characteristic2_, uuid3.canonical_value()); | 2212 SimulateGattDescriptor(characteristic2_, uuid3.canonical_value()); |
2218 SimulateGattDescriptor(characteristic2_, uuid4.canonical_value()); | 2213 SimulateGattDescriptor(characteristic2_, uuid4.canonical_value()); |
2219 | 2214 |
2220 // Verify that GetDescriptor can retrieve descriptors again by ID, | 2215 // Verify that GetDescriptor can retrieve descriptors again by ID, |
2221 // and that the same Descriptor is returned when searched by ID. | 2216 // and that the same Descriptor is returned when searched by ID. |
2222 EXPECT_EQ(2u, characteristic1_->GetDescriptors().size()); | 2217 EXPECT_EQ(2u, characteristic1_->GetDescriptors().size()); |
2223 EXPECT_EQ(2u, characteristic2_->GetDescriptors().size()); | 2218 EXPECT_EQ(2u, characteristic2_->GetDescriptors().size()); |
2224 std::string c1_id1 = characteristic1_->GetDescriptors()[0]->GetIdentifier(); | 2219 std::string c1_id1 = characteristic1_->GetDescriptors()[0]->GetIdentifier(); |
(...skipping 21 matching lines...) Expand all Loading... |
2246 // ... but not uuid 3 | 2241 // ... but not uuid 3 |
2247 EXPECT_FALSE(c1_uuid1 == uuid3 || c1_uuid2 == uuid3); | 2242 EXPECT_FALSE(c1_uuid1 == uuid3 || c1_uuid2 == uuid3); |
2248 } | 2243 } |
2249 #endif // defined(OS_ANDROID) || defined(OS_WIN) | 2244 #endif // defined(OS_ANDROID) || defined(OS_WIN) |
2250 | 2245 |
2251 #if defined(OS_ANDROID) || defined(OS_WIN) | 2246 #if defined(OS_ANDROID) || defined(OS_WIN) |
2252 TEST_F(BluetoothRemoteGattCharacteristicTest, GetDescriptorsByUUID) { | 2247 TEST_F(BluetoothRemoteGattCharacteristicTest, GetDescriptorsByUUID) { |
2253 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate()); | 2248 ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate()); |
2254 | 2249 |
2255 // Add several Descriptors: | 2250 // Add several Descriptors: |
2256 BluetoothUUID id1("11111111-0000-1000-8000-00805f9b34fb"); | 2251 BluetoothUUID id1(kTestUUIDCharacteristicUserDescription); |
2257 BluetoothUUID id2("22222222-0000-1000-8000-00805f9b34fb"); | 2252 BluetoothUUID id2(kTestUUIDClientCharacteristicConfiguration); |
2258 BluetoothUUID id3("33333333-0000-1000-8000-00805f9b34fb"); | 2253 BluetoothUUID id3(kTestUUIDServerCharacteristicConfiguration); |
2259 SimulateGattDescriptor(characteristic1_, id1.canonical_value()); | 2254 SimulateGattDescriptor(characteristic1_, id1.canonical_value()); |
2260 SimulateGattDescriptor(characteristic1_, id2.canonical_value()); | 2255 SimulateGattDescriptor(characteristic1_, id2.canonical_value()); |
2261 SimulateGattDescriptor(characteristic2_, id3.canonical_value()); | 2256 SimulateGattDescriptor(characteristic2_, id3.canonical_value()); |
2262 SimulateGattDescriptor(characteristic2_, id3.canonical_value()); | 2257 SimulateGattDescriptor(characteristic2_, id3.canonical_value()); |
2263 | 2258 |
2264 EXPECT_NE(characteristic2_->GetDescriptorsByUUID(id3).at(0)->GetIdentifier(), | 2259 EXPECT_NE(characteristic2_->GetDescriptorsByUUID(id3).at(0)->GetIdentifier(), |
2265 characteristic2_->GetDescriptorsByUUID(id3).at(1)->GetIdentifier()); | 2260 characteristic2_->GetDescriptorsByUUID(id3).at(1)->GetIdentifier()); |
2266 | 2261 |
2267 EXPECT_EQ(id1, characteristic1_->GetDescriptorsByUUID(id1).at(0)->GetUUID()); | 2262 EXPECT_EQ(id1, characteristic1_->GetDescriptorsByUUID(id1).at(0)->GetUUID()); |
2268 EXPECT_EQ(id2, characteristic1_->GetDescriptorsByUUID(id2).at(0)->GetUUID()); | 2263 EXPECT_EQ(id2, characteristic1_->GetDescriptorsByUUID(id2).at(0)->GetUUID()); |
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2392 ASSERT_NO_FATAL_FAILURE(StartNotifyBoilerplate( | 2387 ASSERT_NO_FATAL_FAILURE(StartNotifyBoilerplate( |
2393 /* properties: NOTIFY */ 0x10, NotifyValueState::NOTIFY)); | 2388 /* properties: NOTIFY */ 0x10, NotifyValueState::NOTIFY)); |
2394 | 2389 |
2395 SimulateGattDisconnection(device_); | 2390 SimulateGattDisconnection(device_); |
2396 // Don't run the disconnect task. | 2391 // Don't run the disconnect task. |
2397 notify_sessions_.clear(); | 2392 notify_sessions_.clear(); |
2398 base::RunLoop().RunUntilIdle(); | 2393 base::RunLoop().RunUntilIdle(); |
2399 } | 2394 } |
2400 #endif | 2395 #endif |
2401 } // namespace device | 2396 } // namespace device |
OLD | NEW |