Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(482)

Side by Side Diff: device/bluetooth/bluetooth_task_manager_win.cc

Issue 1690133002: Implement BluetoothRemoteGattServiceWin and related unit tests (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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_task_manager_win.h" 5 #include "device/bluetooth/bluetooth_task_manager_win.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 #include <winsock2.h> 8 #include <winsock2.h>
9 9
10 #include <string> 10 #include <string>
11 11
12 #include "base/bind.h" 12 #include "base/bind.h"
13 #include "base/memory/ref_counted.h" 13 #include "base/memory/ref_counted.h"
14 #include "base/message_loop/message_loop.h" 14 #include "base/message_loop/message_loop.h"
15 #include "base/sequenced_task_runner.h" 15 #include "base/sequenced_task_runner.h"
16 #include "base/strings/stringprintf.h" 16 #include "base/strings/stringprintf.h"
17 #include "base/strings/sys_string_conversions.h" 17 #include "base/strings/sys_string_conversions.h"
18 #include "base/threading/sequenced_worker_pool.h" 18 #include "base/threading/sequenced_worker_pool.h"
19 #include "device/bluetooth/bluetooth_classic_win.h" 19 #include "device/bluetooth/bluetooth_classic_win.h"
20 #include "device/bluetooth/bluetooth_device.h" 20 #include "device/bluetooth/bluetooth_device.h"
21 #include "device/bluetooth/bluetooth_init_win.h" 21 #include "device/bluetooth/bluetooth_init_win.h"
22 #include "device/bluetooth/bluetooth_low_energy_win.h"
23 #include "device/bluetooth/bluetooth_service_record_win.h" 22 #include "device/bluetooth/bluetooth_service_record_win.h"
24 #include "net/base/winsock_init.h" 23 #include "net/base/winsock_init.h"
25 24
26 namespace { 25 namespace {
27 26
28 const int kNumThreadsInWorkerPool = 3; 27 const int kNumThreadsInWorkerPool = 3;
29 const char kBluetoothThreadName[] = "BluetoothPollingThreadWin"; 28 const char kBluetoothThreadName[] = "BluetoothPollingThreadWin";
30 const int kMaxNumDeviceAddressChar = 127; 29 const int kMaxNumDeviceAddressChar = 127;
31 const int kServiceDiscoveryResultBufferSize = 5000; 30 const int kServiceDiscoveryResultBufferSize = 5000;
32 31
(...skipping 13 matching lines...) Expand all
46 btha.rgBytes[5], 45 btha.rgBytes[5],
47 btha.rgBytes[4], 46 btha.rgBytes[4],
48 btha.rgBytes[3], 47 btha.rgBytes[3],
49 btha.rgBytes[2], 48 btha.rgBytes[2],
50 btha.rgBytes[1], 49 btha.rgBytes[1],
51 btha.rgBytes[0]); 50 btha.rgBytes[0]);
52 DCHECK_EQ(result, device::BluetoothDevice::CanonicalizeAddress(result)); 51 DCHECK_EQ(result, device::BluetoothDevice::CanonicalizeAddress(result));
53 return result; 52 return result;
54 } 53 }
55 54
56 device::BluetoothUUID BluetoothLowEnergyUuidToBluetoothUuid( 55 bool BluetoothUUIDToWinBLEUUID(const device::BluetoothUUID& uuid,
57 const BTH_LE_UUID& bth_le_uuid) { 56 BTH_LE_UUID* out_win_uuid) {
58 if (bth_le_uuid.IsShortUuid) { 57 if (!uuid.IsValid())
59 std::string uuid_hex = 58 return false;
60 base::StringPrintf("%04x", bth_le_uuid.Value.ShortUuid); 59
61 return device::BluetoothUUID(uuid_hex); 60 if (uuid.format() == device::BluetoothUUID::kFormat16Bit) {
61 out_win_uuid->IsShortUuid = true;
62 unsigned int data = 0;
63 int result = sscanf_s(uuid.value().c_str(), "%04x", &data);
64 if (result != 1)
65 return false;
66 out_win_uuid->Value.ShortUuid = data;
62 } else { 67 } else {
63 return device::BluetoothUUID( 68 out_win_uuid->IsShortUuid = false;
64 base::StringPrintf("%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x", 69 unsigned int data[11];
65 bth_le_uuid.Value.LongUuid.Data1, 70 int result =
66 bth_le_uuid.Value.LongUuid.Data2, 71 sscanf_s(uuid.value().c_str(),
67 bth_le_uuid.Value.LongUuid.Data3, 72 "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x", &data[0],
68 bth_le_uuid.Value.LongUuid.Data4[0], 73 &data[1], &data[2], &data[3], &data[4], &data[5], &data[6],
69 bth_le_uuid.Value.LongUuid.Data4[1], 74 &data[7], &data[8], &data[9], &data[10]);
70 bth_le_uuid.Value.LongUuid.Data4[2], 75 if (result != 11)
71 bth_le_uuid.Value.LongUuid.Data4[3], 76 return false;
72 bth_le_uuid.Value.LongUuid.Data4[4], 77 out_win_uuid->Value.LongUuid.Data1 = data[0];
73 bth_le_uuid.Value.LongUuid.Data4[5], 78 out_win_uuid->Value.LongUuid.Data2 = data[1];
74 bth_le_uuid.Value.LongUuid.Data4[6], 79 out_win_uuid->Value.LongUuid.Data3 = data[2];
75 bth_le_uuid.Value.LongUuid.Data4[7])); 80 out_win_uuid->Value.LongUuid.Data4[0] = data[3];
81 out_win_uuid->Value.LongUuid.Data4[1] = data[4];
82 out_win_uuid->Value.LongUuid.Data4[2] = data[5];
83 out_win_uuid->Value.LongUuid.Data4[3] = data[6];
84 out_win_uuid->Value.LongUuid.Data4[4] = data[7];
85 out_win_uuid->Value.LongUuid.Data4[5] = data[8];
86 out_win_uuid->Value.LongUuid.Data4[6] = data[9];
87 out_win_uuid->Value.LongUuid.Data4[7] = data[10];
76 } 88 }
89
90 return true;
77 } 91 }
78 92
79 // Populates bluetooth adapter state using adapter_handle. 93 // Populates bluetooth adapter state using adapter_handle.
80 void GetAdapterState(HANDLE adapter_handle, 94 void GetAdapterState(HANDLE adapter_handle,
81 device::BluetoothTaskManagerWin::AdapterState* state) { 95 device::BluetoothTaskManagerWin::AdapterState* state) {
82 std::string name; 96 std::string name;
83 std::string address; 97 std::string address;
84 bool powered = false; 98 bool powered = false;
85 BLUETOOTH_RADIO_INFO adapter_info = {sizeof(BLUETOOTH_RADIO_INFO)}; 99 BLUETOOTH_RADIO_INFO adapter_info = {sizeof(BLUETOOTH_RADIO_INFO)};
86 if (adapter_handle && 100 if (adapter_handle &&
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
142 : ui_task_runner_(ui_task_runner), 156 : ui_task_runner_(ui_task_runner),
143 adapter_handle_(NULL), 157 adapter_handle_(NULL),
144 discovering_(false), 158 discovering_(false),
145 current_logging_batch_count_(0) {} 159 current_logging_batch_count_(0) {}
146 160
147 BluetoothTaskManagerWin::~BluetoothTaskManagerWin() { 161 BluetoothTaskManagerWin::~BluetoothTaskManagerWin() {
148 win::BluetoothLowEnergyWrapper::DeleteInstance(); 162 win::BluetoothLowEnergyWrapper::DeleteInstance();
149 win::BluetoothClassicWrapper::DeleteInstance(); 163 win::BluetoothClassicWrapper::DeleteInstance();
150 } 164 }
151 165
166 BluetoothUUID BluetoothTaskManagerWin::BluetoothLowEnergyUuidToBluetoothUuid(
167 const BTH_LE_UUID& bth_le_uuid) {
168 if (bth_le_uuid.IsShortUuid) {
169 std::string uuid_hex =
170 base::StringPrintf("%04x", bth_le_uuid.Value.ShortUuid);
171 return BluetoothUUID(uuid_hex);
172 } else {
173 return BluetoothUUID(base::StringPrintf(
174 "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
175 bth_le_uuid.Value.LongUuid.Data1, bth_le_uuid.Value.LongUuid.Data2,
176 bth_le_uuid.Value.LongUuid.Data3, bth_le_uuid.Value.LongUuid.Data4[0],
177 bth_le_uuid.Value.LongUuid.Data4[1],
178 bth_le_uuid.Value.LongUuid.Data4[2],
179 bth_le_uuid.Value.LongUuid.Data4[3],
180 bth_le_uuid.Value.LongUuid.Data4[4],
181 bth_le_uuid.Value.LongUuid.Data4[5],
182 bth_le_uuid.Value.LongUuid.Data4[6],
183 bth_le_uuid.Value.LongUuid.Data4[7]));
184 }
185 }
186
152 void BluetoothTaskManagerWin::AddObserver(Observer* observer) { 187 void BluetoothTaskManagerWin::AddObserver(Observer* observer) {
153 DCHECK(observer); 188 DCHECK(observer);
154 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); 189 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread());
155 observers_.AddObserver(observer); 190 observers_.AddObserver(observer);
156 } 191 }
157 192
158 void BluetoothTaskManagerWin::RemoveObserver(Observer* observer) { 193 void BluetoothTaskManagerWin::RemoveObserver(Observer* observer) {
159 DCHECK(observer); 194 DCHECK(observer);
160 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); 195 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread());
161 observers_.RemoveObserver(observer); 196 observers_.RemoveObserver(observer);
(...skipping 558 matching lines...) Expand 10 before | Expand all | Expand 10 after
720 service_record_state->path = gatt_service_device->path; 755 service_record_state->path = gatt_service_device->path;
721 break; 756 break;
722 } 757 }
723 } 758 }
724 } 759 }
725 } 760 }
726 761
727 return true; 762 return true;
728 } 763 }
729 764
765 void BluetoothTaskManagerWin::GetGattIncludedServices(
766 base::FilePath service_path,
767 BluetoothUUID uuid,
768 uint16_t attribute_handle,
769 const GetGattIncludedServicesCallback& callback) {
770 HRESULT hr = S_OK;
771 scoped_ptr<BTH_LE_GATT_SERVICE> win_services_info;
772 uint16_t number_of_services = 0;
773
774 BTH_LE_GATT_SERVICE win_service;
775 if (BluetoothUUIDToWinBLEUUID(uuid, &(win_service.ServiceUuid))) {
776 win_service.AttributeHandle = attribute_handle;
777 hr = win::BluetoothLowEnergyWrapper::GetInstance()
778 ->ReadIncludedServicesOfAService(service_path, &win_service,
779 &win_services_info,
780 &number_of_services);
781 } else {
782 hr = HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER);
783 }
784
785 ui_task_runner_->PostTask(
786 FROM_HERE, base::Bind(callback, base::Passed(&win_services_info),
787 number_of_services, hr));
788 }
789
790 void BluetoothTaskManagerWin::GetGattIncludedCharacteristics(
791 base::FilePath service_path,
792 BluetoothUUID uuid,
793 uint16_t attribute_handle,
794 const GetGattIncludedCharacteristicsCallback& callback) {
795 HRESULT hr = S_OK;
796 scoped_ptr<BTH_LE_GATT_CHARACTERISTIC> win_characteristics_info;
797 uint16_t number_of_charateristics = 0;
798
799 BTH_LE_GATT_SERVICE win_service;
800 if (BluetoothUUIDToWinBLEUUID(uuid, &(win_service.ServiceUuid))) {
801 win_service.AttributeHandle = attribute_handle;
802 hr = win::BluetoothLowEnergyWrapper::GetInstance()
803 ->ReadCharacteristicsOfAService(service_path, &win_service,
804 &win_characteristics_info,
805 &number_of_charateristics);
806 } else {
807 hr = HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER);
808 }
809
810 ui_task_runner_->PostTask(
811 FROM_HERE, base::Bind(callback, base::Passed(&win_characteristics_info),
812 number_of_charateristics, hr));
813 }
814
815 void BluetoothTaskManagerWin::PostGetGattIncludedServices(
816 const base::FilePath& service_path,
817 const BluetoothUUID& uuid,
818 uint16_t attribute_handle,
819 const GetGattIncludedServicesCallback& callback) {
820 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread());
821 bluetooth_task_runner_->PostTask(
822 FROM_HERE,
823 base::Bind(&BluetoothTaskManagerWin::GetGattIncludedServices, this,
824 service_path, uuid, attribute_handle, callback));
825 }
826
827 void BluetoothTaskManagerWin::PostGetGattIncludedCharacteristics(
828 const base::FilePath& service_path,
829 const BluetoothUUID& uuid,
830 uint16_t attribute_handle,
831 const GetGattIncludedCharacteristicsCallback& callback) {
832 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread());
833 bluetooth_task_runner_->PostTask(
834 FROM_HERE,
835 base::Bind(&BluetoothTaskManagerWin::GetGattIncludedCharacteristics, this,
836 service_path, uuid, attribute_handle, callback));
837 }
838
730 } // namespace device 839 } // namespace device
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698