Chromium Code Reviews| 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::ReadIncludedServicesOfAService( | |
| 723 base::FilePath& service_path, | |
| 724 const PBTH_LE_GATT_SERVICE service, | |
| 725 scoped_ptr<BTH_LE_GATT_SERVICE>* out_included_services, | |
| 726 USHORT* out_counts) { | |
| 727 HRESULT hr = S_OK; | |
| 728 base::File file(service_path, base::File::FLAG_OPEN | base::File::FLAG_READ); | |
| 729 if (!file.IsValid()) { | |
| 730 hr = HRESULT_FROM_WIN32(ERROR_OPEN_FAILED); | |
| 731 return hr; | |
| 732 } | |
| 733 | |
| 734 USHORT required_length = 0; | |
| 735 hr = BluetoothGATTGetIncludedServices(file.GetPlatformFile(), service, 0, | |
| 736 NULL, &required_length, | |
| 737 BLUETOOTH_GATT_FLAG_NONE); | |
| 738 if (hr != HRESULT_FROM_WIN32(ERROR_MORE_DATA)) { | |
| 739 return hr; | |
| 740 } | |
| 741 | |
| 742 out_included_services->reset(new BTH_LE_GATT_SERVICE[required_length]); | |
| 743 USHORT actual_length = required_length; | |
| 744 hr = BluetoothGATTGetIncludedServices( | |
| 745 file.GetPlatformFile(), service, actual_length, | |
| 746 out_included_services->get(), &required_length, BLUETOOTH_GATT_FLAG_NONE); | |
|
scheib
2016/02/24 01:39:10
actual_length and required_length are in wrong arg
gogerald1
2016/02/24 16:09:06
Here actual_length means actual buffer size offere
scheib
2016/02/25 06:22:43
When others are confused by your code and suspect
| |
| 747 if (SUCCEEDED(hr) && required_length != actual_length) { | |
| 748 LOG(ERROR) << "Retrieved included services is not equal to expected" | |
| 749 << " actual_length " << actual_length << " required_length " | |
| 750 << required_length; | |
| 751 hr = HRESULT_FROM_WIN32(ERROR_INVALID_USER_BUFFER); | |
| 752 } | |
| 753 *out_counts = actual_length; | |
| 754 | |
| 755 if (FAILED(hr)) { | |
| 756 out_included_services->reset(nullptr); | |
| 757 *out_counts = 0; | |
| 758 } | |
| 759 return hr; | |
| 760 } | |
| 761 | |
| 762 HRESULT BluetoothLowEnergyWrapper::ReadCharacteristicsOfAService( | |
| 763 base::FilePath& service_path, | |
| 764 const PBTH_LE_GATT_SERVICE service, | |
| 765 scoped_ptr<BTH_LE_GATT_CHARACTERISTIC>* out_included_characteristics, | |
| 766 USHORT* out_counts) { | |
| 767 HRESULT hr = S_OK; | |
| 768 base::File file(service_path, base::File::FLAG_OPEN | base::File::FLAG_READ); | |
| 769 if (!file.IsValid()) { | |
| 770 hr = HRESULT_FROM_WIN32(ERROR_OPEN_FAILED); | |
| 771 return hr; | |
| 772 } | |
| 773 | |
| 774 USHORT required_length = 0; | |
| 775 hr = BluetoothGATTGetCharacteristics(file.GetPlatformFile(), service, 0, NULL, | |
| 776 &required_length, | |
| 777 BLUETOOTH_GATT_FLAG_NONE); | |
| 778 if (hr != HRESULT_FROM_WIN32(ERROR_MORE_DATA)) { | |
| 779 return hr; | |
| 780 } | |
| 781 | |
| 782 out_included_characteristics->reset( | |
| 783 new BTH_LE_GATT_CHARACTERISTIC[required_length]); | |
| 784 USHORT actual_length = required_length; | |
| 785 hr = BluetoothGATTGetCharacteristics( | |
| 786 file.GetPlatformFile(), service, actual_length, | |
|
scheib
2016/02/24 01:39:09
ditto
gogerald1
2016/02/24 16:09:06
Acknowledged.
| |
| 787 out_included_characteristics->get(), &required_length, | |
| 788 BLUETOOTH_GATT_FLAG_NONE); | |
| 789 if (SUCCEEDED(hr) && required_length != actual_length) { | |
| 790 LOG(ERROR) << "Retrieved charactersitics is not equal to expected" | |
| 791 << " actual_length " << actual_length << " required_length " | |
| 792 << required_length; | |
| 793 hr = HRESULT_FROM_WIN32(ERROR_INVALID_USER_BUFFER); | |
| 794 } | |
| 795 *out_counts = actual_length; | |
| 796 | |
| 797 if (FAILED(hr)) { | |
| 798 out_included_characteristics->reset(nullptr); | |
| 799 *out_counts = 0; | |
| 800 } | |
| 801 return hr; | |
| 802 } | |
| 803 | |
| 722 } // namespace win | 804 } // namespace win |
| 723 } // namespace device | 805 } // namespace device |
| OLD | NEW |