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 710 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
721 | 721 |
722 HRESULT BluetoothLowEnergyWrapper::ReadCharacteristicsOfAService( | 722 HRESULT BluetoothLowEnergyWrapper::ReadCharacteristicsOfAService( |
723 base::FilePath& service_path, | 723 base::FilePath& service_path, |
724 const PBTH_LE_GATT_SERVICE service, | 724 const PBTH_LE_GATT_SERVICE service, |
725 scoped_ptr<BTH_LE_GATT_CHARACTERISTIC>* out_included_characteristics, | 725 scoped_ptr<BTH_LE_GATT_CHARACTERISTIC>* out_included_characteristics, |
726 USHORT* out_counts) { | 726 USHORT* out_counts) { |
727 base::File file(service_path, base::File::FLAG_OPEN | base::File::FLAG_READ); | 727 base::File file(service_path, base::File::FLAG_OPEN | base::File::FLAG_READ); |
728 if (!file.IsValid()) | 728 if (!file.IsValid()) |
729 return HRESULT_FROM_WIN32(ERROR_OPEN_FAILED); | 729 return HRESULT_FROM_WIN32(ERROR_OPEN_FAILED); |
730 | 730 |
731 USHORT required_length = 0; | 731 USHORT allocated_length = 0; |
732 HRESULT hr = BluetoothGATTGetCharacteristics(file.GetPlatformFile(), service, | 732 HRESULT hr = BluetoothGATTGetCharacteristics(file.GetPlatformFile(), service, |
733 0, NULL, &required_length, | 733 0, NULL, &allocated_length, |
734 BLUETOOTH_GATT_FLAG_NONE); | 734 BLUETOOTH_GATT_FLAG_NONE); |
735 if (hr != HRESULT_FROM_WIN32(ERROR_MORE_DATA)) | 735 if (hr != HRESULT_FROM_WIN32(ERROR_MORE_DATA)) |
736 return hr; | 736 return hr; |
737 | 737 |
738 out_included_characteristics->reset( | 738 out_included_characteristics->reset( |
739 new BTH_LE_GATT_CHARACTERISTIC[required_length]); | 739 new BTH_LE_GATT_CHARACTERISTIC[allocated_length]); |
740 USHORT actual_length = required_length; | 740 hr = BluetoothGATTGetCharacteristics(file.GetPlatformFile(), service, |
741 hr = BluetoothGATTGetCharacteristics( | 741 allocated_length, |
742 file.GetPlatformFile(), service, actual_length, | 742 out_included_characteristics->get(), |
743 out_included_characteristics->get(), &required_length, | 743 out_counts, BLUETOOTH_GATT_FLAG_NONE); |
744 BLUETOOTH_GATT_FLAG_NONE); | 744 if (SUCCEEDED(hr) && allocated_length != *out_counts) { |
745 if (SUCCEEDED(hr) && required_length != actual_length) { | |
746 LOG(ERROR) << "Retrieved charactersitics is not equal to expected" | 745 LOG(ERROR) << "Retrieved charactersitics is not equal to expected" |
747 << " actual_length " << actual_length << " required_length " | 746 << " allocated_length " << allocated_length << " got " |
748 << required_length; | 747 << *out_counts; |
749 hr = HRESULT_FROM_WIN32(ERROR_INVALID_USER_BUFFER); | 748 hr = HRESULT_FROM_WIN32(ERROR_INVALID_USER_BUFFER); |
750 } | 749 } |
751 *out_counts = actual_length; | |
752 | 750 |
753 if (FAILED(hr)) { | 751 if (FAILED(hr)) { |
754 out_included_characteristics->reset(nullptr); | 752 out_included_characteristics->reset(nullptr); |
755 *out_counts = 0; | 753 *out_counts = 0; |
756 } | 754 } |
757 return hr; | 755 return hr; |
758 } | 756 } |
759 | 757 |
| 758 HRESULT BluetoothLowEnergyWrapper::ReadDescriptorsOfACharacteristic( |
| 759 base::FilePath& service_path, |
| 760 const PBTH_LE_GATT_CHARACTERISTIC characteristic, |
| 761 scoped_ptr<BTH_LE_GATT_DESCRIPTOR>* out_included_descriptors, |
| 762 USHORT* out_counts) { |
| 763 base::File file(service_path, base::File::FLAG_OPEN | base::File::FLAG_READ); |
| 764 if (!file.IsValid()) |
| 765 return HRESULT_FROM_WIN32(ERROR_OPEN_FAILED); |
| 766 |
| 767 USHORT allocated_length = 0; |
| 768 HRESULT hr = BluetoothGATTGetDescriptors( |
| 769 file.GetPlatformFile(), characteristic, 0, NULL, &allocated_length, |
| 770 BLUETOOTH_GATT_FLAG_NONE); |
| 771 if (hr != HRESULT_FROM_WIN32(ERROR_MORE_DATA)) |
| 772 return hr; |
| 773 |
| 774 out_included_descriptors->reset(new BTH_LE_GATT_DESCRIPTOR[allocated_length]); |
| 775 hr = BluetoothGATTGetDescriptors( |
| 776 file.GetPlatformFile(), characteristic, allocated_length, |
| 777 out_included_descriptors->get(), out_counts, BLUETOOTH_GATT_FLAG_NONE); |
| 778 if (SUCCEEDED(hr) && allocated_length != *out_counts) { |
| 779 LOG(ERROR) << "Retrieved descriptors is not equal to expected" |
| 780 << " allocated_length " << allocated_length << " got " |
| 781 << *out_counts; |
| 782 hr = HRESULT_FROM_WIN32(ERROR_INVALID_USER_BUFFER); |
| 783 } |
| 784 |
| 785 if (FAILED(hr)) { |
| 786 out_included_descriptors->reset(nullptr); |
| 787 *out_counts = 0; |
| 788 } |
| 789 return hr; |
| 790 } |
| 791 |
760 } // namespace win | 792 } // namespace win |
761 } // namespace device | 793 } // namespace device |
OLD | NEW |