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> | |
| 134 CharacteristicValueChangedRegistrationMap; | |
| 135 | |
| 136 CharacteristicValueChangedRegistrationMap | |
| 137 characteristic_value_changed_registrations; | |
| 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) { | |
| 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); | |
|
ortuno
2016/03/15 02:55:31
Shouldn't you try to acquire this lock as soon as
gogerald1
2016/03/15 18:28:23
Acquire it as later as possible to give more multi
| |
| 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 const GattEventRegistrationCallback& callback, | |
| 895 const GattCharacteristicValueChangedCallback& registered_callback) { | |
| 896 BLUETOOTH_GATT_EVENT_HANDLE win_event_handle = NULL; | |
| 897 | |
| 898 BLUETOOTH_GATT_VALUE_CHANGED_EVENT_REGISTRATION win_event_parameter; | |
| 899 memcpy(&(win_event_parameter.Characteristics[0]), &characteristic, | |
| 900 sizeof(BTH_LE_GATT_CHARACTERISTIC)); | |
| 901 win_event_parameter.NumCharacteristics = 1; | |
| 902 PVOID user_event_handle = (PVOID)®istered_callback; | |
| 903 HRESULT hr = | |
| 904 win::BluetoothLowEnergyWrapper::GetInstance()->RegisterGattEvents( | |
| 905 service_path, CharacteristicValueChangedEvent, &win_event_parameter, | |
| 906 &OnGetGattEventWin, user_event_handle, &win_event_handle); | |
| 907 if (SUCCEEDED(hr)) { | |
| 908 characteristic_value_changed_registrations[user_event_handle] = | |
| 909 std::make_pair(win_event_handle, registered_callback); | |
| 910 } | |
| 911 | |
| 912 ui_task_runner_->PostTask(FROM_HERE, | |
| 913 base::Bind(callback, user_event_handle, hr)); | |
| 914 } | |
| 915 | |
| 916 void BluetoothTaskManagerWin::UnregisterGattCharacteristicValueChangedEvent( | |
| 917 PVOID event_handle) { | |
| 918 DCHECK(bluetooth_task_runner_->RunsTasksOnCurrentThread()); | |
| 919 | |
| 920 base::AutoLock auto_lock(characteristic_value_changed_registrations_lock); | |
| 921 CharacteristicValueChangedRegistrationMap::const_iterator it = | |
| 922 characteristic_value_changed_registrations.find(event_handle); | |
| 923 if (it != characteristic_value_changed_registrations.end()) { | |
| 924 win::BluetoothLowEnergyWrapper::GetInstance()->UnregisterGattEvent( | |
| 925 it->second.first); | |
| 926 characteristic_value_changed_registrations.erase(event_handle); | |
| 927 } | |
| 928 } | |
| 929 | |
| 847 void BluetoothTaskManagerWin::PostGetGattIncludedCharacteristics( | 930 void BluetoothTaskManagerWin::PostGetGattIncludedCharacteristics( |
| 848 const base::FilePath& service_path, | 931 const base::FilePath& service_path, |
| 849 const BluetoothUUID& uuid, | 932 const BluetoothUUID& uuid, |
| 850 uint16_t attribute_handle, | 933 uint16_t attribute_handle, |
| 851 const GetGattIncludedCharacteristicsCallback& callback) { | 934 const GetGattIncludedCharacteristicsCallback& callback) { |
| 852 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); | 935 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); |
| 853 bluetooth_task_runner_->PostTask( | 936 bluetooth_task_runner_->PostTask( |
| 854 FROM_HERE, | 937 FROM_HERE, |
| 855 base::Bind(&BluetoothTaskManagerWin::GetGattIncludedCharacteristics, this, | 938 base::Bind(&BluetoothTaskManagerWin::GetGattIncludedCharacteristics, this, |
| 856 service_path, uuid, attribute_handle, callback)); | 939 service_path, uuid, attribute_handle, callback)); |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 869 | 952 |
| 870 void BluetoothTaskManagerWin::PostReadGattCharacteristicValue( | 953 void BluetoothTaskManagerWin::PostReadGattCharacteristicValue( |
| 871 const base::FilePath& service_path, | 954 const base::FilePath& service_path, |
| 872 const PBTH_LE_GATT_CHARACTERISTIC characteristic, | 955 const PBTH_LE_GATT_CHARACTERISTIC characteristic, |
| 873 const ReadGattCharacteristicValueCallback& callback) { | 956 const ReadGattCharacteristicValueCallback& callback) { |
| 874 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); | 957 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); |
| 875 bluetooth_task_runner_->PostTask( | 958 bluetooth_task_runner_->PostTask( |
| 876 FROM_HERE, | 959 FROM_HERE, |
| 877 base::Bind(&BluetoothTaskManagerWin::ReadGattCharacteristicValue, this, | 960 base::Bind(&BluetoothTaskManagerWin::ReadGattCharacteristicValue, this, |
| 878 service_path, *characteristic, callback)); | 961 service_path, *characteristic, callback)); |
| 879 FOR_EACH_OBSERVER(BluetoothTaskManagerWin::Observer, observers_, | |
| 880 OnAttemptReadGattCharacteristic()); | |
| 881 } | 962 } |
| 882 | 963 |
| 883 void BluetoothTaskManagerWin::PostWriteGattCharacteristicValue( | 964 void BluetoothTaskManagerWin::PostWriteGattCharacteristicValue( |
| 884 const base::FilePath& service_path, | 965 const base::FilePath& service_path, |
| 885 const PBTH_LE_GATT_CHARACTERISTIC characteristic, | 966 const PBTH_LE_GATT_CHARACTERISTIC characteristic, |
| 886 const std::vector<uint8_t>& new_value, | 967 const std::vector<uint8_t>& new_value, |
| 887 const HResultCallback& callback) { | 968 const HResultCallback& callback) { |
| 888 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); | 969 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); |
| 889 bluetooth_task_runner_->PostTask( | 970 bluetooth_task_runner_->PostTask( |
| 890 FROM_HERE, | 971 FROM_HERE, |
| 891 base::Bind(&BluetoothTaskManagerWin::WriteGattCharacteristicValue, this, | 972 base::Bind(&BluetoothTaskManagerWin::WriteGattCharacteristicValue, this, |
| 892 service_path, *characteristic, new_value, callback)); | 973 service_path, *characteristic, new_value, callback)); |
| 893 FOR_EACH_OBSERVER(BluetoothTaskManagerWin::Observer, observers_, | 974 } |
| 894 OnAttemptWriteGattCharacteristic()); | 975 |
| 976 void BluetoothTaskManagerWin::PostRegisterGattCharacteristicValueChangedEvent( | |
| 977 const base::FilePath& service_path, | |
| 978 const PBTH_LE_GATT_CHARACTERISTIC characteristic, | |
| 979 const GattEventRegistrationCallback& callback, | |
| 980 const GattCharacteristicValueChangedCallback& registered_callback) { | |
| 981 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); | |
| 982 bluetooth_task_runner_->PostTask( | |
| 983 FROM_HERE, | |
| 984 base::Bind( | |
| 985 &BluetoothTaskManagerWin::RegisterGattCharacteristicValueChangedEvent, | |
| 986 this, service_path, *characteristic, callback, registered_callback)); | |
| 987 } | |
| 988 | |
| 989 void BluetoothTaskManagerWin::PostUnregisterGattCharacteristicValueChangedEvent( | |
| 990 PVOID event_handle) { | |
| 991 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); | |
| 992 bluetooth_task_runner_->PostTask( | |
| 993 FROM_HERE, base::Bind(&BluetoothTaskManagerWin:: | |
| 994 UnregisterGattCharacteristicValueChangedEvent, | |
| 995 this, event_handle)); | |
| 895 } | 996 } |
| 896 | 997 |
| 897 } // namespace device | 998 } // namespace device |
| OLD | NEW |