| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/bluetooth_low_energy_win.h" | 5 #include "device/bluetooth/bluetooth_low_energy_win.h" |
| 6 | 6 |
| 7 #include "base/files/file.h" | 7 #include "base/files/file.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/macros.h" | 9 #include "base/macros.h" |
| 10 #include "base/strings/sys_string_conversions.h" | 10 #include "base/strings/sys_string_conversions.h" |
| (...skipping 632 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 643 } | 643 } |
| 644 | 644 |
| 645 BluetoothLowEnergyDeviceInfo::BluetoothLowEnergyDeviceInfo() | 645 BluetoothLowEnergyDeviceInfo::BluetoothLowEnergyDeviceInfo() |
| 646 : visible(false), authenticated(false), connected(false) { | 646 : visible(false), authenticated(false), connected(false) { |
| 647 address.ullLong = BLUETOOTH_NULL_ADDRESS; | 647 address.ullLong = BLUETOOTH_NULL_ADDRESS; |
| 648 } | 648 } |
| 649 | 649 |
| 650 BluetoothLowEnergyDeviceInfo::~BluetoothLowEnergyDeviceInfo() { | 650 BluetoothLowEnergyDeviceInfo::~BluetoothLowEnergyDeviceInfo() { |
| 651 } | 651 } |
| 652 | 652 |
| 653 bool IsBluetoothLowEnergySupported() { | |
| 654 return base::win::GetVersion() >= base::win::VERSION_WIN8; | |
| 655 } | |
| 656 | |
| 657 bool ExtractBluetoothAddressFromDeviceInstanceIdForTesting( | 653 bool ExtractBluetoothAddressFromDeviceInstanceIdForTesting( |
| 658 const std::string& instance_id, | 654 const std::string& instance_id, |
| 659 BLUETOOTH_ADDRESS* btha, | 655 BLUETOOTH_ADDRESS* btha, |
| 660 std::string* error) { | 656 std::string* error) { |
| 661 return ExtractBluetoothAddressFromDeviceInstanceId(instance_id, btha, error); | 657 return ExtractBluetoothAddressFromDeviceInstanceId(instance_id, btha, error); |
| 662 } | 658 } |
| 663 | 659 |
| 664 BluetoothLowEnergyWrapper* BluetoothLowEnergyWrapper::GetInstance() { | 660 BluetoothLowEnergyWrapper* BluetoothLowEnergyWrapper::GetInstance() { |
| 665 if (g_instance_ == nullptr) { | 661 if (g_instance_ == nullptr) { |
| 666 g_instance_ = new BluetoothLowEnergyWrapper(); | 662 g_instance_ = new BluetoothLowEnergyWrapper(); |
| 667 } | 663 } |
| 668 return g_instance_; | 664 return g_instance_; |
| 669 } | 665 } |
| 670 | 666 |
| 671 void BluetoothLowEnergyWrapper::DeleteInstance() { | 667 void BluetoothLowEnergyWrapper::DeleteInstance() { |
| 672 delete g_instance_; | 668 delete g_instance_; |
| 673 g_instance_ = nullptr; | 669 g_instance_ = nullptr; |
| 674 } | 670 } |
| 675 | 671 |
| 676 void BluetoothLowEnergyWrapper::SetInstanceForTest( | 672 void BluetoothLowEnergyWrapper::SetInstanceForTest( |
| 677 BluetoothLowEnergyWrapper* instance) { | 673 BluetoothLowEnergyWrapper* instance) { |
| 678 delete g_instance_; | 674 delete g_instance_; |
| 679 g_instance_ = instance; | 675 g_instance_ = instance; |
| 680 } | 676 } |
| 681 | 677 |
| 682 BluetoothLowEnergyWrapper::BluetoothLowEnergyWrapper() {} | 678 BluetoothLowEnergyWrapper::BluetoothLowEnergyWrapper() {} |
| 683 BluetoothLowEnergyWrapper::~BluetoothLowEnergyWrapper() {} | 679 BluetoothLowEnergyWrapper::~BluetoothLowEnergyWrapper() {} |
| 684 | 680 |
| 681 bool BluetoothLowEnergyWrapper::IsBluetoothLowEnergySupported() { |
| 682 return base::win::GetVersion() >= base::win::VERSION_WIN8; |
| 683 } |
| 684 |
| 685 bool BluetoothLowEnergyWrapper::EnumerateKnownBluetoothLowEnergyDevices( | 685 bool BluetoothLowEnergyWrapper::EnumerateKnownBluetoothLowEnergyDevices( |
| 686 ScopedVector<BluetoothLowEnergyDeviceInfo>* devices, | 686 ScopedVector<BluetoothLowEnergyDeviceInfo>* devices, |
| 687 std::string* error) { | 687 std::string* error) { |
| 688 if (!IsBluetoothLowEnergySupported()) { | 688 if (!IsBluetoothLowEnergySupported()) { |
| 689 *error = kPlatformNotSupported; | 689 *error = kPlatformNotSupported; |
| 690 return false; | 690 return false; |
| 691 } | 691 } |
| 692 | 692 |
| 693 return EnumerateKnownBLEOrBLEGattServiceDevices( | 693 return EnumerateKnownBLEOrBLEGattServiceDevices( |
| 694 GUID_BLUETOOTHLE_DEVICE_INTERFACE, devices, error); | 694 GUID_BLUETOOTHLE_DEVICE_INTERFACE, devices, error); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 712 ScopedVector<BluetoothLowEnergyServiceInfo>* services, | 712 ScopedVector<BluetoothLowEnergyServiceInfo>* services, |
| 713 std::string* error) { | 713 std::string* error) { |
| 714 if (!IsBluetoothLowEnergySupported()) { | 714 if (!IsBluetoothLowEnergySupported()) { |
| 715 *error = kPlatformNotSupported; | 715 *error = kPlatformNotSupported; |
| 716 return false; | 716 return false; |
| 717 } | 717 } |
| 718 | 718 |
| 719 return CollectBluetoothLowEnergyDeviceServices(device_path, services, error); | 719 return CollectBluetoothLowEnergyDeviceServices(device_path, services, error); |
| 720 } | 720 } |
| 721 | 721 |
| 722 HRESULT BluetoothLowEnergyWrapper::ReadCharacteristicsOfAService( |
| 723 base::FilePath& service_path, |
| 724 const PBTH_LE_GATT_SERVICE service, |
| 725 scoped_ptr<BTH_LE_GATT_CHARACTERISTIC>* out_included_characteristics, |
| 726 USHORT* out_counts) { |
| 727 base::File file(service_path, base::File::FLAG_OPEN | base::File::FLAG_READ); |
| 728 if (!file.IsValid()) |
| 729 return HRESULT_FROM_WIN32(ERROR_OPEN_FAILED); |
| 730 |
| 731 USHORT required_length = 0; |
| 732 HRESULT hr = BluetoothGATTGetCharacteristics(file.GetPlatformFile(), service, |
| 733 0, NULL, &required_length, |
| 734 BLUETOOTH_GATT_FLAG_NONE); |
| 735 if (hr != HRESULT_FROM_WIN32(ERROR_MORE_DATA)) |
| 736 return hr; |
| 737 |
| 738 out_included_characteristics->reset( |
| 739 new BTH_LE_GATT_CHARACTERISTIC[required_length]); |
| 740 USHORT actual_length = required_length; |
| 741 hr = BluetoothGATTGetCharacteristics( |
| 742 file.GetPlatformFile(), service, actual_length, |
| 743 out_included_characteristics->get(), &required_length, |
| 744 BLUETOOTH_GATT_FLAG_NONE); |
| 745 if (SUCCEEDED(hr) && required_length != actual_length) { |
| 746 LOG(ERROR) << "Retrieved charactersitics is not equal to expected" |
| 747 << " actual_length " << actual_length << " required_length " |
| 748 << required_length; |
| 749 hr = HRESULT_FROM_WIN32(ERROR_INVALID_USER_BUFFER); |
| 750 } |
| 751 *out_counts = actual_length; |
| 752 |
| 753 if (FAILED(hr)) { |
| 754 out_included_characteristics->reset(nullptr); |
| 755 *out_counts = 0; |
| 756 } |
| 757 return hr; |
| 758 } |
| 759 |
| 722 } // namespace win | 760 } // namespace win |
| 723 } // namespace device | 761 } // namespace device |
| OLD | NEW |