Chromium Code Reviews| OLD | NEW |
|---|---|
| 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> |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 115 void GetDeviceState(const BLUETOOTH_DEVICE_INFO& device_info, | 115 void GetDeviceState(const BLUETOOTH_DEVICE_INFO& device_info, |
| 116 device::BluetoothTaskManagerWin::DeviceState* state) { | 116 device::BluetoothTaskManagerWin::DeviceState* state) { |
| 117 state->name = base::SysWideToUTF8(device_info.szName); | 117 state->name = base::SysWideToUTF8(device_info.szName); |
| 118 state->address = BluetoothAddressToCanonicalString(device_info.Address); | 118 state->address = BluetoothAddressToCanonicalString(device_info.Address); |
| 119 state->bluetooth_class = device_info.ulClassofDevice; | 119 state->bluetooth_class = device_info.ulClassofDevice; |
| 120 state->visible = true; | 120 state->visible = true; |
| 121 state->connected = !!device_info.fConnected; | 121 state->connected = !!device_info.fConnected; |
| 122 state->authenticated = !!device_info.fAuthenticated; | 122 state->authenticated = !!device_info.fAuthenticated; |
| 123 } | 123 } |
| 124 | 124 |
| 125 typedef std::pair< | |
| 126 BLUETOOTH_GATT_EVENT_HANDLE, | |
| 127 device::BluetoothTaskManagerWin::GattCharacteristicValueChangedCallback> | |
| 128 CharacteristicValueChangedRegistration; | |
| 129 | |
| 130 // The key of CharacteristicValueChangedRegistrationMap is a | |
| 131 // GattCharacteristicValueChangedCallback pointer (cast to PVOID) to make it | |
| 132 // unique for different callbacks. | |
| 133 typedef std::unordered_map<PVOID, CharacteristicValueChangedRegistration> | |
|
scheib
2016/04/05 22:44:02
Why does the pointer need to be cast to PVOID?
gogerald1
2016/04/06 22:54:48
It was passed into OS as context which is PVOID wh
| |
| 134 CharacteristicValueChangedRegistrationMap; | |
| 135 | |
| 136 CharacteristicValueChangedRegistrationMap | |
| 137 characteristic_value_changed_registrations; | |
|
scheib
2016/04/05 22:44:02
Move these to private member variables, unless the
gogerald1
2016/04/06 22:54:48
Done. These variables are used by BluetoothTaskMan
| |
| 138 base::Lock characteristic_value_changed_registrations_lock; | |
| 139 | |
| 140 // Function to be registered to OS to monitor Bluetooth LE GATT event. | |
| 141 void OnGetGattEventWin(BTH_LE_GATT_EVENT_TYPE type, | |
| 142 PVOID event_parameter, | |
| 143 PVOID context) { | |
| 144 if (type != CharacteristicValueChangedEvent) { | |
|
scheib
2016/04/05 22:44:02
Add DCHECK to assert what threads this is or is no
gogerald1
2016/04/06 22:54:48
Post callback to ui_task_runner_ below and accordi
| |
| 145 // Right now, only characteristic value changed event is supported. | |
| 146 NOTREACHED(); | |
| 147 return; | |
| 148 } | |
| 149 | |
| 150 BLUETOOTH_GATT_VALUE_CHANGED_EVENT* event = | |
| 151 (BLUETOOTH_GATT_VALUE_CHANGED_EVENT*)event_parameter; | |
| 152 PBTH_LE_GATT_CHARACTERISTIC_VALUE new_value_win = event->CharacteristicValue; | |
| 153 scoped_ptr<std::vector<uint8_t>> new_value( | |
| 154 new std::vector<uint8_t>(new_value_win->DataSize)); | |
| 155 for (ULONG i = 0; i < new_value_win->DataSize; i++) | |
| 156 (*new_value)[i] = new_value_win->Data[i]; | |
| 157 | |
| 158 // Lock shared data structure since this callback is invoked in | |
| 159 // BluetoothApis.dll thread. | |
| 160 base::AutoLock auto_lock(characteristic_value_changed_registrations_lock); | |
| 161 CharacteristicValueChangedRegistrationMap::const_iterator it = | |
| 162 characteristic_value_changed_registrations.find(context); | |
| 163 if (it == characteristic_value_changed_registrations.end()) | |
| 164 return; | |
| 165 | |
| 166 it->second.second.Run(std::move(new_value)); | |
| 167 } | |
| 168 | |
| 125 } // namespace | 169 } // namespace |
| 126 | 170 |
| 127 namespace device { | 171 namespace device { |
| 128 | 172 |
| 129 // static | 173 // static |
| 130 const int BluetoothTaskManagerWin::kPollIntervalMs = 500; | 174 const int BluetoothTaskManagerWin::kPollIntervalMs = 500; |
| 131 | 175 |
| 132 BluetoothTaskManagerWin::AdapterState::AdapterState() : powered(false) { | 176 BluetoothTaskManagerWin::AdapterState::AdapterState() : powered(false) { |
| 133 } | 177 } |
| 134 | 178 |
| (...skipping 702 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 837 win_new_value->Data[i] = new_value[i]; | 881 win_new_value->Data[i] = new_value[i]; |
| 838 | 882 |
| 839 HRESULT hr = | 883 HRESULT hr = |
| 840 win::BluetoothLowEnergyWrapper::GetInstance()->WriteCharacteristicValue( | 884 win::BluetoothLowEnergyWrapper::GetInstance()->WriteCharacteristicValue( |
| 841 service_path, (PBTH_LE_GATT_CHARACTERISTIC)(&characteristic), | 885 service_path, (PBTH_LE_GATT_CHARACTERISTIC)(&characteristic), |
| 842 win_new_value.get()); | 886 win_new_value.get()); |
| 843 | 887 |
| 844 ui_task_runner_->PostTask(FROM_HERE, base::Bind(callback, hr)); | 888 ui_task_runner_->PostTask(FROM_HERE, base::Bind(callback, hr)); |
| 845 } | 889 } |
| 846 | 890 |
| 891 void BluetoothTaskManagerWin::RegisterGattCharacteristicValueChangedEvent( | |
| 892 base::FilePath service_path, | |
| 893 BTH_LE_GATT_CHARACTERISTIC characteristic, | |
| 894 BTH_LE_GATT_DESCRIPTOR ccc_descriptor, | |
| 895 const GattEventRegistrationCallback& callback, | |
| 896 const GattCharacteristicValueChangedCallback& registered_callback) { | |
| 897 BLUETOOTH_GATT_EVENT_HANDLE win_event_handle = NULL; | |
|
scheib
2016/04/05 22:44:02
DCHECK(bluetooth_task_runner_->RunsTasksOnCurrentT
gogerald1
2016/04/06 22:54:48
Done.
| |
| 898 | |
| 899 BLUETOOTH_GATT_VALUE_CHANGED_EVENT_REGISTRATION win_event_parameter; | |
| 900 memcpy(&(win_event_parameter.Characteristics[0]), &characteristic, | |
| 901 sizeof(BTH_LE_GATT_CHARACTERISTIC)); | |
| 902 win_event_parameter.NumCharacteristics = 1; | |
| 903 PVOID user_event_handle = (PVOID)®istered_callback; | |
| 904 HRESULT hr = | |
| 905 win::BluetoothLowEnergyWrapper::GetInstance()->RegisterGattEvents( | |
| 906 service_path, CharacteristicValueChangedEvent, &win_event_parameter, | |
| 907 &OnGetGattEventWin, user_event_handle, &win_event_handle); | |
| 908 | |
| 909 // Sets the Client Characteristic Configuration descriptor. | |
| 910 if (SUCCEEDED(hr)) { | |
| 911 BTH_LE_GATT_DESCRIPTOR_VALUE new_cccd_value; | |
| 912 RtlZeroMemory(&new_cccd_value, sizeof(new_cccd_value)); | |
| 913 new_cccd_value.DescriptorType = ClientCharacteristicConfiguration; | |
| 914 if (characteristic.IsNotifiable) { | |
| 915 new_cccd_value.ClientCharacteristicConfiguration | |
| 916 .IsSubscribeToNotification = TRUE; | |
| 917 } else { | |
| 918 new_cccd_value.ClientCharacteristicConfiguration.IsSubscribeToIndication = | |
| 919 TRUE; | |
| 920 } | |
| 921 | |
| 922 hr = win::BluetoothLowEnergyWrapper::GetInstance()->WriteDescriptorValue( | |
| 923 service_path, (PBTH_LE_GATT_DESCRIPTOR)(&ccc_descriptor), | |
| 924 &new_cccd_value); | |
| 925 } | |
| 926 | |
| 927 if (SUCCEEDED(hr)) { | |
| 928 characteristic_value_changed_registrations[user_event_handle] = | |
|
scheib
2016/04/05 22:44:02
This also needs to be guarded?
gogerald1
2016/04/06 22:54:48
may have no problem since remove and add operation
scheib
2016/04/06 23:56:39
The map was searched and added to from different t
gogerald1
2016/04/08 17:03:30
Acknowledged.
| |
| 929 std::make_pair(win_event_handle, registered_callback); | |
| 930 } | |
| 931 | |
| 932 ui_task_runner_->PostTask(FROM_HERE, | |
| 933 base::Bind(callback, user_event_handle, hr)); | |
| 934 } | |
| 935 | |
| 936 void BluetoothTaskManagerWin::UnregisterGattCharacteristicValueChangedEvent( | |
| 937 PVOID event_handle) { | |
| 938 DCHECK(bluetooth_task_runner_->RunsTasksOnCurrentThread()); | |
| 939 | |
| 940 base::AutoLock auto_lock(characteristic_value_changed_registrations_lock); | |
| 941 CharacteristicValueChangedRegistrationMap::const_iterator it = | |
| 942 characteristic_value_changed_registrations.find(event_handle); | |
| 943 if (it != characteristic_value_changed_registrations.end()) { | |
| 944 win::BluetoothLowEnergyWrapper::GetInstance()->UnregisterGattEvent( | |
| 945 it->second.first); | |
| 946 characteristic_value_changed_registrations.erase(event_handle); | |
| 947 } | |
| 948 } | |
| 949 | |
| 847 void BluetoothTaskManagerWin::PostGetGattIncludedCharacteristics( | 950 void BluetoothTaskManagerWin::PostGetGattIncludedCharacteristics( |
| 848 const base::FilePath& service_path, | 951 const base::FilePath& service_path, |
| 849 const BluetoothUUID& uuid, | 952 const BluetoothUUID& uuid, |
| 850 uint16_t attribute_handle, | 953 uint16_t attribute_handle, |
| 851 const GetGattIncludedCharacteristicsCallback& callback) { | 954 const GetGattIncludedCharacteristicsCallback& callback) { |
| 852 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); | 955 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); |
| 853 bluetooth_task_runner_->PostTask( | 956 bluetooth_task_runner_->PostTask( |
| 854 FROM_HERE, | 957 FROM_HERE, |
| 855 base::Bind(&BluetoothTaskManagerWin::GetGattIncludedCharacteristics, this, | 958 base::Bind(&BluetoothTaskManagerWin::GetGattIncludedCharacteristics, this, |
| 856 service_path, uuid, attribute_handle, callback)); | 959 service_path, uuid, attribute_handle, callback)); |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 869 | 972 |
| 870 void BluetoothTaskManagerWin::PostReadGattCharacteristicValue( | 973 void BluetoothTaskManagerWin::PostReadGattCharacteristicValue( |
| 871 const base::FilePath& service_path, | 974 const base::FilePath& service_path, |
| 872 const PBTH_LE_GATT_CHARACTERISTIC characteristic, | 975 const PBTH_LE_GATT_CHARACTERISTIC characteristic, |
| 873 const ReadGattCharacteristicValueCallback& callback) { | 976 const ReadGattCharacteristicValueCallback& callback) { |
| 874 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); | 977 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); |
| 875 bluetooth_task_runner_->PostTask( | 978 bluetooth_task_runner_->PostTask( |
| 876 FROM_HERE, | 979 FROM_HERE, |
| 877 base::Bind(&BluetoothTaskManagerWin::ReadGattCharacteristicValue, this, | 980 base::Bind(&BluetoothTaskManagerWin::ReadGattCharacteristicValue, this, |
| 878 service_path, *characteristic, callback)); | 981 service_path, *characteristic, callback)); |
| 879 FOR_EACH_OBSERVER(BluetoothTaskManagerWin::Observer, observers_, | |
| 880 OnAttemptReadGattCharacteristic()); | |
| 881 } | 982 } |
| 882 | 983 |
| 883 void BluetoothTaskManagerWin::PostWriteGattCharacteristicValue( | 984 void BluetoothTaskManagerWin::PostWriteGattCharacteristicValue( |
| 884 const base::FilePath& service_path, | 985 const base::FilePath& service_path, |
| 885 const PBTH_LE_GATT_CHARACTERISTIC characteristic, | 986 const PBTH_LE_GATT_CHARACTERISTIC characteristic, |
| 886 const std::vector<uint8_t>& new_value, | 987 const std::vector<uint8_t>& new_value, |
| 887 const HResultCallback& callback) { | 988 const HResultCallback& callback) { |
| 888 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); | 989 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); |
| 889 bluetooth_task_runner_->PostTask( | 990 bluetooth_task_runner_->PostTask( |
| 890 FROM_HERE, | 991 FROM_HERE, |
| 891 base::Bind(&BluetoothTaskManagerWin::WriteGattCharacteristicValue, this, | 992 base::Bind(&BluetoothTaskManagerWin::WriteGattCharacteristicValue, this, |
| 892 service_path, *characteristic, new_value, callback)); | 993 service_path, *characteristic, new_value, callback)); |
| 893 FOR_EACH_OBSERVER(BluetoothTaskManagerWin::Observer, observers_, | 994 } |
| 894 OnAttemptWriteGattCharacteristic()); | 995 |
| 996 void BluetoothTaskManagerWin::PostRegisterGattCharacteristicValueChangedEvent( | |
| 997 const base::FilePath& service_path, | |
| 998 const PBTH_LE_GATT_CHARACTERISTIC characteristic, | |
| 999 const PBTH_LE_GATT_DESCRIPTOR ccc_descriptor, | |
| 1000 const GattEventRegistrationCallback& callback, | |
| 1001 const GattCharacteristicValueChangedCallback& registered_callback) { | |
| 1002 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); | |
| 1003 bluetooth_task_runner_->PostTask( | |
| 1004 FROM_HERE, | |
| 1005 base::Bind( | |
| 1006 &BluetoothTaskManagerWin::RegisterGattCharacteristicValueChangedEvent, | |
| 1007 this, service_path, *characteristic, *ccc_descriptor, callback, | |
| 1008 registered_callback)); | |
| 1009 } | |
| 1010 | |
| 1011 void BluetoothTaskManagerWin::PostUnregisterGattCharacteristicValueChangedEvent( | |
| 1012 PVOID event_handle) { | |
| 1013 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); | |
| 1014 bluetooth_task_runner_->PostTask( | |
| 1015 FROM_HERE, base::Bind(&BluetoothTaskManagerWin:: | |
| 1016 UnregisterGattCharacteristicValueChangedEvent, | |
| 1017 this, event_handle)); | |
| 895 } | 1018 } |
| 896 | 1019 |
| 897 } // namespace device | 1020 } // namespace device |
| OLD | NEW |