| 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 "device/bluetooth/test/bluetooth_test_mac.h" | 5 #include "device/bluetooth/test/bluetooth_test_mac.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include "base/mac/foundation_util.h" | 9 #include "base/mac/foundation_util.h" |
| 10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
| (...skipping 281 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 292 CBCentralManager* central_manager = | 292 CBCentralManager* central_manager = |
| 293 ObjCCast<CBCentralManager>(mock_central_manager_->get()); | 293 ObjCCast<CBCentralManager>(mock_central_manager_->get()); |
| 294 [central_manager.delegate centralManager:central_manager | 294 [central_manager.delegate centralManager:central_manager |
| 295 didDisconnectPeripheral:peripheral_mock.peripheral | 295 didDisconnectPeripheral:peripheral_mock.peripheral |
| 296 error:nil]; | 296 error:nil]; |
| 297 } | 297 } |
| 298 | 298 |
| 299 void BluetoothTestMac::SimulateGattServicesDiscovered( | 299 void BluetoothTestMac::SimulateGattServicesDiscovered( |
| 300 BluetoothDevice* device, | 300 BluetoothDevice* device, |
| 301 const std::vector<std::string>& uuids) { | 301 const std::vector<std::string>& uuids) { |
| 302 AddServicesToDevice(device, uuids); | 302 AddServicesToDeviceMac(device, uuids); |
| 303 [GetMockCBPeripheral(device) mockDidDiscoverEvents]; | 303 [GetMockCBPeripheral(device) mockDidDiscoverEvents]; |
| 304 } | 304 } |
| 305 | 305 |
| 306 void BluetoothTestMac::SimulateGattServicesChanged(BluetoothDevice* device) { | 306 void BluetoothTestMac::SimulateGattServicesChanged(BluetoothDevice* device) { |
| 307 [GetMockCBPeripheral(device) didModifyServices:@[]]; | 307 [GetMockCBPeripheral(device) didModifyServices:@[]]; |
| 308 } | 308 } |
| 309 | 309 |
| 310 void BluetoothTestMac::SimulateGattServiceRemoved( | 310 void BluetoothTestMac::SimulateGattServiceRemoved( |
| 311 BluetoothRemoteGattService* service) { | 311 BluetoothRemoteGattService* service) { |
| 312 BluetoothUUID bluetooth_service_uuid = service->GetUUID(); | 312 BluetoothUUID bluetooth_service_uuid = service->GetUUID(); |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 372 NSError* error = BluetoothDeviceMac::GetNSErrorFromGattErrorCode(error_code); | 372 NSError* error = BluetoothDeviceMac::GetNSErrorFromGattErrorCode(error_code); |
| 373 [characteristic_mock simulateWriteWithError:error]; | 373 [characteristic_mock simulateWriteWithError:error]; |
| 374 } | 374 } |
| 375 | 375 |
| 376 void BluetoothTestMac::SimulateGattDescriptor( | 376 void BluetoothTestMac::SimulateGattDescriptor( |
| 377 BluetoothRemoteGattCharacteristic* characteristic, | 377 BluetoothRemoteGattCharacteristic* characteristic, |
| 378 const std::string& uuid) { | 378 const std::string& uuid) { |
| 379 MockCBCharacteristic* characteristic_mock = | 379 MockCBCharacteristic* characteristic_mock = |
| 380 GetCBMockCharacteristic(characteristic); | 380 GetCBMockCharacteristic(characteristic); |
| 381 CBUUID* cb_uuid = [CBUUID UUIDWithString:@(uuid.c_str())]; | 381 CBUUID* cb_uuid = [CBUUID UUIDWithString:@(uuid.c_str())]; |
| 382 [characteristic_mock simulateDescriptorWithUUID:cb_uuid]; | 382 [characteristic_mock addDescriptorWithUUID:cb_uuid]; |
| 383 MockCBPeripheral* peripheral_mock = GetMockCBPeripheral(characteristic); | 383 MockCBPeripheral* peripheral_mock = GetMockCBPeripheral(characteristic); |
| 384 [peripheral_mock didModifyServices:@[]]; | 384 [peripheral_mock didModifyServices:@[]]; |
| 385 // After -[MockCBPeripheral didModifyServices:], BluetoothLowEnergyDeviceMac | 385 // After -[MockCBPeripheral didModifyServices:], BluetoothLowEnergyDeviceMac |
| 386 // is expected to call -[CBPeripheral discoverServices:] | 386 // is expected to call -[CBPeripheral discoverServices:] |
| 387 [peripheral_mock mockDidDiscoverEvents]; | 387 [peripheral_mock mockDidDiscoverEvents]; |
| 388 } | 388 } |
| 389 | 389 |
| 390 void BluetoothTestMac::SimulateGattNotifySessionStarted( | 390 void BluetoothTestMac::SimulateGattNotifySessionStarted( |
| 391 BluetoothRemoteGattCharacteristic* characteristic) { | 391 BluetoothRemoteGattCharacteristic* characteristic) { |
| 392 MockCBCharacteristic* characteristic_mock = | 392 MockCBCharacteristic* characteristic_mock = |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 444 MockCBCharacteristic* characteristic_mock = | 444 MockCBCharacteristic* characteristic_mock = |
| 445 ObjCCast<MockCBCharacteristic>(cb_characteristic); | 445 ObjCCast<MockCBCharacteristic>(cb_characteristic); |
| 446 [service_mock removeCharacteristicMock:characteristic_mock]; | 446 [service_mock removeCharacteristicMock:characteristic_mock]; |
| 447 [peripheral_mock didModifyServices:@[]]; | 447 [peripheral_mock didModifyServices:@[]]; |
| 448 [peripheral_mock mockDidDiscoverEvents]; | 448 [peripheral_mock mockDidDiscoverEvents]; |
| 449 } | 449 } |
| 450 | 450 |
| 451 void BluetoothTestMac::SimulateGattDescriptorRead( | 451 void BluetoothTestMac::SimulateGattDescriptorRead( |
| 452 BluetoothRemoteGattDescriptor* descriptor, | 452 BluetoothRemoteGattDescriptor* descriptor, |
| 453 const std::vector<uint8_t>& value) { | 453 const std::vector<uint8_t>& value) { |
| 454 SimulateGattDescriptorReadNSData(descriptor, value); | 454 SimulateGattDescriptorReadNSDataMac(descriptor, value); |
| 455 } | 455 } |
| 456 | 456 |
| 457 void BluetoothTestMac::SimulateGattDescriptorReadError( | 457 void BluetoothTestMac::SimulateGattDescriptorReadError( |
| 458 BluetoothRemoteGattDescriptor* descriptor, | 458 BluetoothRemoteGattDescriptor* descriptor, |
| 459 BluetoothRemoteGattService::GattErrorCode error_code) { | 459 BluetoothRemoteGattService::GattErrorCode error_code) { |
| 460 NSError* error = BluetoothDeviceMac::GetNSErrorFromGattErrorCode(error_code); | 460 NSError* error = BluetoothDeviceMac::GetNSErrorFromGattErrorCode(error_code); |
| 461 [GetCBMockDescriptor(descriptor) simulateReadWithValue:nil error:error]; | 461 [GetCBMockDescriptor(descriptor) simulateReadWithValue:nil error:error]; |
| 462 } | 462 } |
| 463 | 463 |
| 464 void BluetoothTestMac::SimulateGattDescriptorWrite( | 464 void BluetoothTestMac::SimulateGattDescriptorWrite( |
| (...skipping 18 matching lines...) Expand all Loading... |
| 483 case NotifyValueState::NONE: | 483 case NotifyValueState::NONE: |
| 484 EXPECT_FALSE(last_notify_value_); | 484 EXPECT_FALSE(last_notify_value_); |
| 485 break; | 485 break; |
| 486 case NotifyValueState::NOTIFY: | 486 case NotifyValueState::NOTIFY: |
| 487 case NotifyValueState::INDICATE: | 487 case NotifyValueState::INDICATE: |
| 488 EXPECT_TRUE(last_notify_value_); | 488 EXPECT_TRUE(last_notify_value_); |
| 489 break; | 489 break; |
| 490 } | 490 } |
| 491 } | 491 } |
| 492 | 492 |
| 493 void BluetoothTestMac::SimulateDidDiscoverServices( | 493 void BluetoothTestMac::SimulateDidDiscoverServicesMac(BluetoothDevice* device) { |
| 494 BluetoothDevice* device, | |
| 495 const std::vector<std::string>& uuids) { | |
| 496 AddServicesToDevice(device, uuids); | |
| 497 [GetMockCBPeripheral(device) mockDidDiscoverServices]; | 494 [GetMockCBPeripheral(device) mockDidDiscoverServices]; |
| 498 } | 495 } |
| 499 | 496 |
| 500 void BluetoothTestMac::SimulateGattDescriptorReadNSData( | 497 void BluetoothTestMac::SimulateDidDiscoverCharacteristicsMac( |
| 498 BluetoothRemoteGattService* service) { |
| 499 BluetoothRemoteGattServiceMac* mac_gatt_service = |
| 500 static_cast<BluetoothRemoteGattServiceMac*>(service); |
| 501 CBService* cb_service = mac_gatt_service->GetService(); |
| 502 [GetMockCBPeripheral(service) |
| 503 mockDidDiscoverCharacteristicsForService:cb_service]; |
| 504 } |
| 505 |
| 506 void BluetoothTestMac::SimulateDidDiscoverDescriptorsMac( |
| 507 BluetoothRemoteGattCharacteristic* characteristic) { |
| 508 BluetoothRemoteGattCharacteristicMac* mac_gatt_characteristic = |
| 509 static_cast<BluetoothRemoteGattCharacteristicMac*>(characteristic); |
| 510 CBCharacteristic* cb_characteristic = |
| 511 mac_gatt_characteristic->GetCBCharacteristic(); |
| 512 [GetMockCBPeripheral(characteristic) |
| 513 mockDidDiscoverDescriptorsForCharacteristic:cb_characteristic]; |
| 514 } |
| 515 |
| 516 void BluetoothTestMac::SimulateGattDescriptorReadNSDataMac( |
| 501 BluetoothRemoteGattDescriptor* descriptor, | 517 BluetoothRemoteGattDescriptor* descriptor, |
| 502 const std::vector<uint8_t>& value) { | 518 const std::vector<uint8_t>& value) { |
| 503 scoped_nsobject<NSData> data( | 519 scoped_nsobject<NSData> data( |
| 504 [[NSData alloc] initWithBytes:value.data() length:value.size()]); | 520 [[NSData alloc] initWithBytes:value.data() length:value.size()]); |
| 505 [GetCBMockDescriptor(descriptor) simulateReadWithValue:data error:nil]; | 521 [GetCBMockDescriptor(descriptor) simulateReadWithValue:data error:nil]; |
| 506 } | 522 } |
| 507 | 523 |
| 508 void BluetoothTestMac::SimulateGattDescriptorReadNSString( | 524 void BluetoothTestMac::SimulateGattDescriptorReadNSStringMac( |
| 509 BluetoothRemoteGattDescriptor* descriptor, | 525 BluetoothRemoteGattDescriptor* descriptor, |
| 510 const std::string& value) { | 526 const std::string& value) { |
| 511 NSString* string = base::SysUTF8ToNSString(value); | 527 NSString* string = base::SysUTF8ToNSString(value); |
| 512 [GetCBMockDescriptor(descriptor) simulateReadWithValue:string error:nil]; | 528 [GetCBMockDescriptor(descriptor) simulateReadWithValue:string error:nil]; |
| 513 } | 529 } |
| 514 | 530 |
| 515 void BluetoothTestMac::SimulateGattDescriptorReadNSNumber( | 531 void BluetoothTestMac::SimulateGattDescriptorReadNSNumberMac( |
| 516 BluetoothRemoteGattDescriptor* descriptor, | 532 BluetoothRemoteGattDescriptor* descriptor, |
| 517 short value) { | 533 short value) { |
| 518 NSNumber* number = [NSNumber numberWithShort:value]; | 534 NSNumber* number = [NSNumber numberWithShort:value]; |
| 519 [GetCBMockDescriptor(descriptor) simulateReadWithValue:number error:nil]; | 535 [GetCBMockDescriptor(descriptor) simulateReadWithValue:number error:nil]; |
| 520 } | 536 } |
| 521 | 537 |
| 538 void BluetoothTest::AddServicesToDeviceMac( |
| 539 BluetoothDevice* device, |
| 540 const std::vector<std::string>& uuids) { |
| 541 scoped_nsobject<NSMutableArray> services([[NSMutableArray alloc] init]); |
| 542 for (auto uuid : uuids) { |
| 543 CBUUID* cb_service_uuid = [CBUUID UUIDWithString:@(uuid.c_str())]; |
| 544 [services addObject:cb_service_uuid]; |
| 545 } |
| 546 [GetMockCBPeripheral(device) addServices:services]; |
| 547 } |
| 548 |
| 549 void BluetoothTestMac::AddCharacteristicToServiceMac( |
| 550 BluetoothRemoteGattService* service, |
| 551 const std::string& characteristic_uuid, |
| 552 int properties) { |
| 553 BluetoothRemoteGattServiceMac* mac_gatt_service = |
| 554 static_cast<BluetoothRemoteGattServiceMac*>(service); |
| 555 CBService* cb_service = mac_gatt_service->GetService(); |
| 556 MockCBService* service_mock = ObjCCast<MockCBService>(cb_service); |
| 557 CBUUID* cb_uuid = [CBUUID UUIDWithString:@(characteristic_uuid.c_str())]; |
| 558 [service_mock addCharacteristicWithUUID:cb_uuid properties:properties]; |
| 559 } |
| 560 |
| 561 void BluetoothTestMac::AddDescriptorToCharacteristicMac( |
| 562 BluetoothRemoteGattCharacteristic* characteristic, |
| 563 const std::string& uuid) { |
| 564 BluetoothRemoteGattCharacteristicMac* mac_gatt_characteristic = |
| 565 static_cast<BluetoothRemoteGattCharacteristicMac*>(characteristic); |
| 566 CBCharacteristic* cb_characteristic = |
| 567 mac_gatt_characteristic->GetCBCharacteristic(); |
| 568 MockCBCharacteristic* characteristic_mock = |
| 569 ObjCCast<MockCBCharacteristic>(cb_characteristic); |
| 570 CBUUID* cb_uuid = [CBUUID UUIDWithString:@(uuid.c_str())]; |
| 571 [characteristic_mock addDescriptorWithUUID:cb_uuid]; |
| 572 } |
| 573 |
| 522 void BluetoothTestMac::OnFakeBluetoothDeviceConnectGattCalled() { | 574 void BluetoothTestMac::OnFakeBluetoothDeviceConnectGattCalled() { |
| 523 gatt_connection_attempts_++; | 575 gatt_connection_attempts_++; |
| 524 } | 576 } |
| 525 | 577 |
| 526 void BluetoothTestMac::OnFakeBluetoothGattDisconnect() { | 578 void BluetoothTestMac::OnFakeBluetoothGattDisconnect() { |
| 527 gatt_disconnection_attempts_++; | 579 gatt_disconnection_attempts_++; |
| 528 } | 580 } |
| 529 | 581 |
| 530 void BluetoothTestMac::OnFakeBluetoothServiceDiscovery() { | 582 void BluetoothTestMac::OnFakeBluetoothServiceDiscovery() { |
| 531 gatt_discovery_attempts_++; | 583 gatt_discovery_attempts_++; |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 614 return ObjCCast<MockCBCharacteristic>(cb_characteristic); | 666 return ObjCCast<MockCBCharacteristic>(cb_characteristic); |
| 615 } | 667 } |
| 616 | 668 |
| 617 MockCBDescriptor* BluetoothTest::GetCBMockDescriptor( | 669 MockCBDescriptor* BluetoothTest::GetCBMockDescriptor( |
| 618 BluetoothRemoteGattDescriptor* descriptor) const { | 670 BluetoothRemoteGattDescriptor* descriptor) const { |
| 619 device::BluetoothRemoteGattDescriptorMac* mac_gatt_descriptor = | 671 device::BluetoothRemoteGattDescriptorMac* mac_gatt_descriptor = |
| 620 static_cast<device::BluetoothRemoteGattDescriptorMac*>(descriptor); | 672 static_cast<device::BluetoothRemoteGattDescriptorMac*>(descriptor); |
| 621 CBDescriptor* cb_descriptor = mac_gatt_descriptor->GetCBDescriptor(); | 673 CBDescriptor* cb_descriptor = mac_gatt_descriptor->GetCBDescriptor(); |
| 622 return ObjCCast<MockCBDescriptor>(cb_descriptor); | 674 return ObjCCast<MockCBDescriptor>(cb_descriptor); |
| 623 } | 675 } |
| 624 | |
| 625 void BluetoothTest::AddServicesToDevice(BluetoothDevice* device, | |
| 626 const std::vector<std::string>& uuids) { | |
| 627 scoped_nsobject<NSMutableArray> services([[NSMutableArray alloc] init]); | |
| 628 for (auto uuid : uuids) { | |
| 629 CBUUID* cb_service_uuid = [CBUUID UUIDWithString:@(uuid.c_str())]; | |
| 630 [services addObject:cb_service_uuid]; | |
| 631 } | |
| 632 [GetMockCBPeripheral(device) addServices:services]; | |
| 633 } | |
| 634 | |
| 635 // Utility function for generating new (CBUUID, address) pairs where CBUUID | 676 // Utility function for generating new (CBUUID, address) pairs where CBUUID |
| 636 // hashes to address. For use when adding a new device address to the testing | 677 // hashes to address. For use when adding a new device address to the testing |
| 637 // suite because CoreBluetooth peripherals have CBUUIDs in place of addresses, | 678 // suite because CoreBluetooth peripherals have CBUUIDs in place of addresses, |
| 638 // and we construct fake addresses for them by hashing the CBUUID. By changing | 679 // and we construct fake addresses for them by hashing the CBUUID. By changing |
| 639 // |target| the user can generate sequentially numbered test addresses. | 680 // |target| the user can generate sequentially numbered test addresses. |
| 640 // | 681 // |
| 641 // std::string BluetoothTestMac::FindCBUUIDForHashTarget() { | 682 // std::string BluetoothTestMac::FindCBUUIDForHashTarget() { |
| 642 // // The desired first 6 digits of the hash. For example 0100000, 020000, | 683 // // The desired first 6 digits of the hash. For example 0100000, 020000, |
| 643 // // 030000, ... | 684 // // 030000, ... |
| 644 // const std::string target = "010000"; | 685 // const std::string target = "010000"; |
| (...skipping 16 matching lines...) Expand all Loading... |
| 661 // crypto::SHA256HashString(input_str, raw, sizeof(raw)); | 702 // crypto::SHA256HashString(input_str, raw, sizeof(raw)); |
| 662 // if (base::HexEncode(raw, sizeof(raw)) == target) { | 703 // if (base::HexEncode(raw, sizeof(raw)) == target) { |
| 663 // return input_str; | 704 // return input_str; |
| 664 // } | 705 // } |
| 665 // ++input[0]; | 706 // ++input[0]; |
| 666 // } | 707 // } |
| 667 // return ""; | 708 // return ""; |
| 668 // } | 709 // } |
| 669 | 710 |
| 670 } // namespace device | 711 } // namespace device |
| OLD | NEW |