OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 <utility> | 5 #include <utility> |
6 #include <vector> | 6 #include <vector> |
7 | 7 |
8 #include "base/memory/ptr_util.h" | 8 #include "base/memory/ptr_util.h" |
9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
10 #include "device/bluetooth/device.h" | 10 #include "device/bluetooth/device.h" |
| 11 #include "device/bluetooth/public/interfaces/gatt_result_type_converter.h" |
11 #include "mojo/public/cpp/bindings/strong_binding.h" | 12 #include "mojo/public/cpp/bindings/strong_binding.h" |
12 | 13 |
13 namespace bluetooth { | 14 namespace bluetooth { |
14 Device::~Device() { | 15 Device::~Device() { |
15 adapter_->RemoveObserver(this); | 16 adapter_->RemoveObserver(this); |
16 } | 17 } |
17 | 18 |
18 // static | 19 // static |
19 void Device::Create(scoped_refptr<device::BluetoothAdapter> adapter, | 20 void Device::Create(scoped_refptr<device::BluetoothAdapter> adapter, |
20 std::unique_ptr<device::BluetoothGattConnection> connection, | 21 std::unique_ptr<device::BluetoothGattConnection> connection, |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
107 | 108 |
108 // pending_services_requests_ is owned by Device, so base::Unretained is | 109 // pending_services_requests_ is owned by Device, so base::Unretained is |
109 // safe. | 110 // safe. |
110 pending_services_requests_.push_back( | 111 pending_services_requests_.push_back( |
111 base::Bind(&Device::GetCharacteristicsImpl, base::Unretained(this), | 112 base::Bind(&Device::GetCharacteristicsImpl, base::Unretained(this), |
112 service_id, callback)); | 113 service_id, callback)); |
113 } | 114 } |
114 | 115 |
115 Device::Device(scoped_refptr<device::BluetoothAdapter> adapter, | 116 Device::Device(scoped_refptr<device::BluetoothAdapter> adapter, |
116 std::unique_ptr<device::BluetoothGattConnection> connection) | 117 std::unique_ptr<device::BluetoothGattConnection> connection) |
117 : adapter_(std::move(adapter)), connection_(std::move(connection)) { | 118 : adapter_(std::move(adapter)), |
| 119 connection_(std::move(connection)), |
| 120 weak_ptr_factory_(this) { |
118 adapter_->AddObserver(this); | 121 adapter_->AddObserver(this); |
119 } | 122 } |
120 | 123 |
| 124 void Device::ReadValueForCharacteristic( |
| 125 const std::string& service_id, |
| 126 const std::string& characteristic_id, |
| 127 const ReadValueForCharacteristicCallback& callback) { |
| 128 device::BluetoothDevice* device = adapter_->GetDevice(GetAddress()); |
| 129 DCHECK(device); |
| 130 |
| 131 device::BluetoothRemoteGattService* service = |
| 132 device->GetGattService(service_id); |
| 133 if (service == nullptr) { |
| 134 callback.Run(mojom::GattResult::SERVICE_NOT_FOUND, |
| 135 base::nullopt /* value */); |
| 136 return; |
| 137 } |
| 138 |
| 139 device::BluetoothRemoteGattCharacteristic* characteristic = |
| 140 service->GetCharacteristic(characteristic_id); |
| 141 if (characteristic == nullptr) { |
| 142 callback.Run(mojom::GattResult::CHARACTERISTIC_NOT_FOUND, |
| 143 base::nullopt /* value */); |
| 144 return; |
| 145 } |
| 146 |
| 147 characteristic->ReadRemoteCharacteristic( |
| 148 base::Bind(&Device::OnReadRemoteCharacteristic, |
| 149 weak_ptr_factory_.GetWeakPtr(), callback), |
| 150 base::Bind(&Device::OnReadRemoteCharacteristicError, |
| 151 weak_ptr_factory_.GetWeakPtr(), callback)); |
| 152 } |
| 153 |
| 154 void Device::WriteValueForCharacteristic( |
| 155 const std::string& service_id, |
| 156 const std::string& characteristic_id, |
| 157 const std::vector<uint8_t>& value, |
| 158 const WriteValueForCharacteristicCallback& callback) { |
| 159 device::BluetoothDevice* device = adapter_->GetDevice(GetAddress()); |
| 160 DCHECK(device); |
| 161 |
| 162 device::BluetoothRemoteGattService* service = |
| 163 device->GetGattService(service_id); |
| 164 if (service == nullptr) { |
| 165 callback.Run(mojom::GattResult::SERVICE_NOT_FOUND); |
| 166 return; |
| 167 } |
| 168 |
| 169 device::BluetoothRemoteGattCharacteristic* characteristic = |
| 170 service->GetCharacteristic(characteristic_id); |
| 171 if (characteristic == nullptr) { |
| 172 callback.Run(mojom::GattResult::CHARACTERISTIC_NOT_FOUND); |
| 173 return; |
| 174 } |
| 175 |
| 176 characteristic->WriteRemoteCharacteristic( |
| 177 value, base::Bind(&Device::OnWriteRemoteCharacteristic, |
| 178 weak_ptr_factory_.GetWeakPtr(), callback), |
| 179 base::Bind(&Device::OnWriteRemoteCharacteristicError, |
| 180 weak_ptr_factory_.GetWeakPtr(), callback)); |
| 181 } |
| 182 |
121 void Device::GetServicesImpl(const GetServicesCallback& callback) { | 183 void Device::GetServicesImpl(const GetServicesCallback& callback) { |
122 device::BluetoothDevice* device = adapter_->GetDevice(GetAddress()); | 184 device::BluetoothDevice* device = adapter_->GetDevice(GetAddress()); |
123 DCHECK(device); | 185 DCHECK(device); |
124 | 186 |
125 std::vector<mojom::ServiceInfoPtr> services; | 187 std::vector<mojom::ServiceInfoPtr> services; |
126 | 188 |
127 for (const device::BluetoothRemoteGattService* service : | 189 for (const device::BluetoothRemoteGattService* service : |
128 device->GetGattServices()) { | 190 device->GetGattServices()) { |
129 services.push_back(ConstructServiceInfoStruct(*service)); | 191 services.push_back(ConstructServiceInfoStruct(*service)); |
130 } | 192 } |
(...skipping 23 matching lines...) Expand all Loading... |
154 | 216 |
155 std::vector<mojom::CharacteristicInfoPtr> characteristics; | 217 std::vector<mojom::CharacteristicInfoPtr> characteristics; |
156 | 218 |
157 for (const auto* characteristic : service->GetCharacteristics()) { | 219 for (const auto* characteristic : service->GetCharacteristics()) { |
158 mojom::CharacteristicInfoPtr characteristic_info = | 220 mojom::CharacteristicInfoPtr characteristic_info = |
159 mojom::CharacteristicInfo::New(); | 221 mojom::CharacteristicInfo::New(); |
160 | 222 |
161 characteristic_info->id = characteristic->GetIdentifier(); | 223 characteristic_info->id = characteristic->GetIdentifier(); |
162 characteristic_info->uuid = characteristic->GetUUID(); | 224 characteristic_info->uuid = characteristic->GetUUID(); |
163 characteristic_info->properties = characteristic->GetProperties(); | 225 characteristic_info->properties = characteristic->GetProperties(); |
| 226 characteristic_info->permissions = characteristic->GetPermissions(); |
| 227 characteristic_info->last_known_value = characteristic->GetValue(); |
164 | 228 |
165 characteristics.push_back(std::move(characteristic_info)); | 229 characteristics.push_back(std::move(characteristic_info)); |
166 } | 230 } |
167 | 231 |
168 callback.Run(std::move(characteristics)); | 232 callback.Run(std::move(characteristics)); |
169 } | 233 } |
170 | 234 |
| 235 void Device::OnReadRemoteCharacteristic( |
| 236 const ReadValueForCharacteristicCallback& callback, |
| 237 const std::vector<uint8_t>& value) { |
| 238 callback.Run(mojom::GattResult::SUCCESS, std::move(value)); |
| 239 } |
| 240 |
| 241 void Device::OnReadRemoteCharacteristicError( |
| 242 const ReadValueForCharacteristicCallback& callback, |
| 243 device::BluetoothGattService::GattErrorCode error_code) { |
| 244 callback.Run(mojo::ConvertTo<mojom::GattResult>(error_code), |
| 245 base::nullopt /* value */); |
| 246 } |
| 247 |
| 248 void Device::OnWriteRemoteCharacteristic( |
| 249 const WriteValueForCharacteristicCallback& callback) { |
| 250 callback.Run(mojom::GattResult::SUCCESS); |
| 251 } |
| 252 |
| 253 void Device::OnWriteRemoteCharacteristicError( |
| 254 const WriteValueForCharacteristicCallback& callback, |
| 255 device::BluetoothGattService::GattErrorCode error_code) { |
| 256 callback.Run(mojo::ConvertTo<mojom::GattResult>(error_code)); |
| 257 } |
| 258 |
171 const std::string& Device::GetAddress() { | 259 const std::string& Device::GetAddress() { |
172 return connection_->GetDeviceAddress(); | 260 return connection_->GetDeviceAddress(); |
173 } | 261 } |
174 | 262 |
175 } // namespace bluetooth | 263 } // namespace bluetooth |
OLD | NEW |