Bailey Forrest | 070f5a1e | 2018-03-28 18:17:14 | [diff] [blame] | 1 | // Copyright 2018 The Chromium Authors. All rights reserved. |
Bailey Forrest | 1276102 | 2018-03-22 18:50:16 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Bailey Forrest | 801a41d | 2018-03-31 01:01:22 | [diff] [blame] | 5 | #include "chromecast/device/bluetooth/le/remote_device_impl.h" |
Bailey Forrest | 1276102 | 2018-03-22 18:50:16 | [diff] [blame] | 6 | |
| 7 | #include "base/bind.h" |
Sebastien Marchand | 17fa278 | 2019-01-25 19:28:10 | [diff] [blame^] | 8 | #include "base/bind_helpers.h" |
Bailey Forrest | 1276102 | 2018-03-22 18:50:16 | [diff] [blame] | 9 | #include "chromecast/base/bind_to_task_runner.h" |
Bailey Forrest | 9625bbf1 | 2018-08-15 01:27:17 | [diff] [blame] | 10 | #include "chromecast/device/bluetooth/bluetooth_util.h" |
Bailey Forrest | 801a41d | 2018-03-31 01:01:22 | [diff] [blame] | 11 | #include "chromecast/device/bluetooth/le/gatt_client_manager_impl.h" |
| 12 | #include "chromecast/device/bluetooth/le/remote_characteristic_impl.h" |
| 13 | #include "chromecast/device/bluetooth/le/remote_descriptor_impl.h" |
| 14 | #include "chromecast/device/bluetooth/le/remote_service_impl.h" |
Bailey Forrest | 1276102 | 2018-03-22 18:50:16 | [diff] [blame] | 15 | |
| 16 | namespace chromecast { |
| 17 | namespace bluetooth { |
| 18 | |
| 19 | #define RUN_ON_IO_THREAD(method, ...) \ |
| 20 | io_task_runner_->PostTask( \ |
Bailey Forrest | 801a41d | 2018-03-31 01:01:22 | [diff] [blame] | 21 | FROM_HERE, \ |
| 22 | base::BindOnce(&RemoteDeviceImpl::method, this, ##__VA_ARGS__)); |
Bailey Forrest | 1276102 | 2018-03-22 18:50:16 | [diff] [blame] | 23 | |
| 24 | #define MAKE_SURE_IO_THREAD(method, ...) \ |
| 25 | DCHECK(io_task_runner_); \ |
| 26 | if (!io_task_runner_->BelongsToCurrentThread()) { \ |
| 27 | RUN_ON_IO_THREAD(method, ##__VA_ARGS__) \ |
| 28 | return; \ |
| 29 | } |
| 30 | |
| 31 | #define EXEC_CB_AND_RET(cb, ret, ...) \ |
| 32 | do { \ |
| 33 | if (cb) { \ |
| 34 | std::move(cb).Run(ret, ##__VA_ARGS__); \ |
| 35 | } \ |
| 36 | return; \ |
| 37 | } while (0) |
| 38 | |
| 39 | #define CHECK_CONNECTED(cb) \ |
| 40 | do { \ |
| 41 | if (!connected_) { \ |
| 42 | LOG(ERROR) << __func__ << "failed: Not connected"; \ |
| 43 | EXEC_CB_AND_RET(cb, false); \ |
| 44 | } \ |
| 45 | } while (0) |
| 46 | |
| 47 | #define LOG_EXEC_CB_AND_RET(cb, ret) \ |
| 48 | do { \ |
| 49 | if (!ret) { \ |
| 50 | LOG(ERROR) << __func__ << "failed"; \ |
| 51 | } \ |
| 52 | EXEC_CB_AND_RET(cb, ret); \ |
| 53 | } while (0) |
| 54 | |
Bailey Forrest | 9625bbf1 | 2018-08-15 01:27:17 | [diff] [blame] | 55 | // static |
| 56 | constexpr base::TimeDelta RemoteDeviceImpl::kCommandTimeout; |
| 57 | |
Bailey Forrest | 801a41d | 2018-03-31 01:01:22 | [diff] [blame] | 58 | RemoteDeviceImpl::RemoteDeviceImpl( |
Bailey Forrest | 1276102 | 2018-03-22 18:50:16 | [diff] [blame] | 59 | const bluetooth_v2_shlib::Addr& addr, |
Bailey Forrest | 801a41d | 2018-03-31 01:01:22 | [diff] [blame] | 60 | base::WeakPtr<GattClientManagerImpl> gatt_client_manager, |
Bailey Forrest | 1276102 | 2018-03-22 18:50:16 | [diff] [blame] | 61 | scoped_refptr<base::SingleThreadTaskRunner> io_task_runner) |
| 62 | : gatt_client_manager_(gatt_client_manager), |
| 63 | addr_(addr), |
| 64 | io_task_runner_(io_task_runner) { |
| 65 | DCHECK(gatt_client_manager); |
| 66 | DCHECK(io_task_runner_->BelongsToCurrentThread()); |
| 67 | } |
| 68 | |
Bailey Forrest | 801a41d | 2018-03-31 01:01:22 | [diff] [blame] | 69 | RemoteDeviceImpl::~RemoteDeviceImpl() = default; |
Bailey Forrest | 1276102 | 2018-03-22 18:50:16 | [diff] [blame] | 70 | |
Bailey Forrest | 801a41d | 2018-03-31 01:01:22 | [diff] [blame] | 71 | void RemoteDeviceImpl::Connect(StatusCallback cb) { |
Kyle Lund | 1de248a | 2018-03-27 17:26:44 | [diff] [blame] | 72 | MAKE_SURE_IO_THREAD(Connect, BindToCurrentSequence(std::move(cb))); |
Bailey Forrest | 1276102 | 2018-03-22 18:50:16 | [diff] [blame] | 73 | if (!ConnectSync()) { |
| 74 | // Error logged. |
| 75 | EXEC_CB_AND_RET(cb, false); |
| 76 | } |
| 77 | |
| 78 | connect_cb_ = std::move(cb); |
| 79 | } |
| 80 | |
Bailey Forrest | 801a41d | 2018-03-31 01:01:22 | [diff] [blame] | 81 | bool RemoteDeviceImpl::ConnectSync() { |
Bailey Forrest | 1276102 | 2018-03-22 18:50:16 | [diff] [blame] | 82 | DCHECK(io_task_runner_->BelongsToCurrentThread()); |
Bailey Forrest | 8a2e40fe | 2018-08-21 23:43:00 | [diff] [blame] | 83 | LOG(INFO) << "Connect(" << util::AddrLastByteString(addr_) << ")"; |
| 84 | |
Bailey Forrest | 1276102 | 2018-03-22 18:50:16 | [diff] [blame] | 85 | if (!gatt_client_manager_) { |
| 86 | LOG(ERROR) << __func__ << " failed: Destroyed"; |
| 87 | return false; |
| 88 | } |
| 89 | if (connect_pending_) { |
| 90 | LOG(ERROR) << __func__ << " failed: Connection pending"; |
| 91 | return false; |
| 92 | } |
| 93 | |
| 94 | gatt_client_manager_->NotifyConnect(addr_); |
Tiansong Cui | 2464034 | 2018-07-31 16:57:46 | [diff] [blame] | 95 | |
Bailey Forrest | 1276102 | 2018-03-22 18:50:16 | [diff] [blame] | 96 | connect_pending_ = true; |
Tiansong Cui | 2464034 | 2018-07-31 16:57:46 | [diff] [blame] | 97 | gatt_client_manager_->EnqueueConnectRequest(addr_); |
| 98 | |
Bailey Forrest | 1276102 | 2018-03-22 18:50:16 | [diff] [blame] | 99 | return true; |
| 100 | } |
| 101 | |
Bailey Forrest | 801a41d | 2018-03-31 01:01:22 | [diff] [blame] | 102 | void RemoteDeviceImpl::Disconnect(StatusCallback cb) { |
Kyle Lund | 1de248a | 2018-03-27 17:26:44 | [diff] [blame] | 103 | MAKE_SURE_IO_THREAD(Disconnect, BindToCurrentSequence(std::move(cb))); |
Bailey Forrest | 1276102 | 2018-03-22 18:50:16 | [diff] [blame] | 104 | if (!DisconnectSync()) { |
| 105 | // Error logged. |
| 106 | EXEC_CB_AND_RET(cb, false); |
| 107 | } |
| 108 | |
| 109 | disconnect_cb_ = std::move(cb); |
| 110 | } |
| 111 | |
Bailey Forrest | 801a41d | 2018-03-31 01:01:22 | [diff] [blame] | 112 | bool RemoteDeviceImpl::DisconnectSync() { |
Bailey Forrest | 1276102 | 2018-03-22 18:50:16 | [diff] [blame] | 113 | DCHECK(io_task_runner_->BelongsToCurrentThread()); |
Bailey Forrest | 8a2e40fe | 2018-08-21 23:43:00 | [diff] [blame] | 114 | LOG(INFO) << "Disconnect(" << util::AddrLastByteString(addr_) << ")"; |
Bailey Forrest | 1276102 | 2018-03-22 18:50:16 | [diff] [blame] | 115 | if (!gatt_client_manager_) { |
| 116 | LOG(ERROR) << __func__ << " failed: Destroyed"; |
| 117 | return false; |
| 118 | } |
| 119 | |
| 120 | if (!connected_) { |
| 121 | LOG(ERROR) << "Not connected"; |
| 122 | return false; |
| 123 | } |
| 124 | |
| 125 | if (!gatt_client_manager_->gatt_client()->Disconnect(addr_)) { |
| 126 | LOG(ERROR) << __func__ << " failed"; |
| 127 | return false; |
| 128 | } |
| 129 | disconnect_pending_ = true; |
| 130 | |
| 131 | return true; |
| 132 | } |
| 133 | |
Tiansong Cui | 2732254 | 2019-01-08 00:56:13 | [diff] [blame] | 134 | void RemoteDeviceImpl::CreateBond(StatusCallback cb) { |
| 135 | MAKE_SURE_IO_THREAD(CreateBond, BindToCurrentSequence(std::move(cb))); |
| 136 | LOG(INFO) << "CreateBond(" << util::AddrLastByteString(addr_) << ")"; |
| 137 | if (!gatt_client_manager_) { |
| 138 | LOG(ERROR) << __func__ << " failed: Destroyed"; |
| 139 | EXEC_CB_AND_RET(cb, false); |
| 140 | } |
| 141 | |
| 142 | if (!connected_) { |
| 143 | LOG(ERROR) << "Not connected"; |
| 144 | EXEC_CB_AND_RET(cb, false); |
| 145 | } |
| 146 | |
| 147 | if (create_bond_pending_ || remove_bond_pending_) { |
| 148 | // TODO(tiansong): b/120489954 Implement queuing and timeout logic. |
| 149 | LOG(ERROR) << __func__ << " failed: waiting for pending bond command"; |
| 150 | EXEC_CB_AND_RET(cb, false); |
| 151 | } |
| 152 | |
| 153 | if (bonded_) { |
| 154 | LOG(ERROR) << "Already bonded"; |
| 155 | EXEC_CB_AND_RET(cb, false); |
| 156 | } |
| 157 | |
| 158 | if (!gatt_client_manager_->gatt_client()->CreateBond(addr_)) { |
| 159 | LOG(ERROR) << __func__ << " failed"; |
| 160 | EXEC_CB_AND_RET(cb, false); |
| 161 | } |
| 162 | |
| 163 | create_bond_pending_ = true; |
| 164 | create_bond_cb_ = std::move(cb); |
| 165 | } |
| 166 | |
| 167 | void RemoteDeviceImpl::RemoveBond(StatusCallback cb) { |
| 168 | MAKE_SURE_IO_THREAD(RemoveBond, BindToCurrentSequence(std::move(cb))); |
| 169 | LOG(INFO) << "RemoveBond(" << util::AddrLastByteString(addr_) << ")"; |
| 170 | if (!gatt_client_manager_) { |
| 171 | LOG(ERROR) << __func__ << " failed: Destroyed"; |
| 172 | EXEC_CB_AND_RET(cb, false); |
| 173 | } |
| 174 | |
| 175 | if (create_bond_pending_ || remove_bond_pending_) { |
| 176 | // TODO(tiansong): b/120489954 Implement queuing and timeout logic. |
| 177 | LOG(ERROR) << __func__ << " failed: waiting for pending bond command"; |
| 178 | EXEC_CB_AND_RET(cb, false); |
| 179 | } |
| 180 | |
| 181 | if (!bonded_) { |
| 182 | LOG(WARNING) << "Not bonded"; |
| 183 | } |
| 184 | |
| 185 | if (!gatt_client_manager_->gatt_client()->RemoveBond(addr_)) { |
| 186 | LOG(ERROR) << __func__ << " failed"; |
| 187 | EXEC_CB_AND_RET(cb, false); |
| 188 | } |
| 189 | |
| 190 | remove_bond_pending_ = true; |
| 191 | remove_bond_cb_ = std::move(cb); |
| 192 | } |
| 193 | |
Bailey Forrest | 801a41d | 2018-03-31 01:01:22 | [diff] [blame] | 194 | void RemoteDeviceImpl::ReadRemoteRssi(RssiCallback cb) { |
Kyle Lund | 1de248a | 2018-03-27 17:26:44 | [diff] [blame] | 195 | MAKE_SURE_IO_THREAD(ReadRemoteRssi, BindToCurrentSequence(std::move(cb))); |
Bailey Forrest | 1276102 | 2018-03-22 18:50:16 | [diff] [blame] | 196 | if (!gatt_client_manager_) { |
| 197 | LOG(ERROR) << __func__ << " failed: Destroyed"; |
| 198 | EXEC_CB_AND_RET(cb, false, 0); |
| 199 | } |
| 200 | |
| 201 | if (rssi_pending_) { |
| 202 | LOG(ERROR) << "Read remote RSSI already pending"; |
| 203 | EXEC_CB_AND_RET(cb, false, 0); |
| 204 | } |
Tiansong Cui | cbfc37e | 2018-07-30 20:06:27 | [diff] [blame] | 205 | |
Bailey Forrest | 1276102 | 2018-03-22 18:50:16 | [diff] [blame] | 206 | rssi_pending_ = true; |
| 207 | rssi_cb_ = std::move(cb); |
Tiansong Cui | cbfc37e | 2018-07-30 20:06:27 | [diff] [blame] | 208 | gatt_client_manager_->EnqueueReadRemoteRssiRequest(addr_); |
Bailey Forrest | 1276102 | 2018-03-22 18:50:16 | [diff] [blame] | 209 | } |
| 210 | |
Bailey Forrest | 801a41d | 2018-03-31 01:01:22 | [diff] [blame] | 211 | void RemoteDeviceImpl::RequestMtu(int mtu, StatusCallback cb) { |
Kyle Lund | 1de248a | 2018-03-27 17:26:44 | [diff] [blame] | 212 | MAKE_SURE_IO_THREAD(RequestMtu, mtu, BindToCurrentSequence(std::move(cb))); |
Bailey Forrest | 8a2e40fe | 2018-08-21 23:43:00 | [diff] [blame] | 213 | LOG(INFO) << "RequestMtu(" << util::AddrLastByteString(addr_) << ", " << mtu |
| 214 | << ")"; |
Bailey Forrest | 5ea6fa6 | 2018-06-29 00:02:44 | [diff] [blame] | 215 | DCHECK(cb); |
Bailey Forrest | 1276102 | 2018-03-22 18:50:16 | [diff] [blame] | 216 | if (!gatt_client_manager_) { |
| 217 | LOG(ERROR) << __func__ << " failed: Destroyed"; |
| 218 | EXEC_CB_AND_RET(cb, false); |
| 219 | } |
| 220 | CHECK_CONNECTED(cb); |
Bailey Forrest | 5ea6fa6 | 2018-06-29 00:02:44 | [diff] [blame] | 221 | mtu_callbacks_.push(std::move(cb)); |
| 222 | EnqueueOperation( |
Bailey Forrest | 9625bbf1 | 2018-08-15 01:27:17 | [diff] [blame] | 223 | __func__, base::BindOnce(&RemoteDeviceImpl::RequestMtuImpl, this, mtu)); |
Bailey Forrest | 1276102 | 2018-03-22 18:50:16 | [diff] [blame] | 224 | } |
| 225 | |
Bailey Forrest | 801a41d | 2018-03-31 01:01:22 | [diff] [blame] | 226 | void RemoteDeviceImpl::ConnectionParameterUpdate(int min_interval, |
| 227 | int max_interval, |
| 228 | int latency, |
| 229 | int timeout, |
| 230 | StatusCallback cb) { |
Bailey Forrest | 1276102 | 2018-03-22 18:50:16 | [diff] [blame] | 231 | MAKE_SURE_IO_THREAD(ConnectionParameterUpdate, min_interval, max_interval, |
Kyle Lund | 1de248a | 2018-03-27 17:26:44 | [diff] [blame] | 232 | latency, timeout, BindToCurrentSequence(std::move(cb))); |
Bailey Forrest | 8a2e40fe | 2018-08-21 23:43:00 | [diff] [blame] | 233 | LOG(INFO) << "ConnectionParameterUpdate(" << util::AddrLastByteString(addr_) |
| 234 | << ", " << min_interval << ", " << max_interval << ", " << latency |
| 235 | << ", " << timeout << ")"; |
Bailey Forrest | 1276102 | 2018-03-22 18:50:16 | [diff] [blame] | 236 | if (!gatt_client_manager_) { |
| 237 | LOG(ERROR) << __func__ << " failed: Destroyed"; |
| 238 | EXEC_CB_AND_RET(cb, false); |
| 239 | } |
| 240 | CHECK_CONNECTED(cb); |
| 241 | bool ret = gatt_client_manager_->gatt_client()->ConnectionParameterUpdate( |
| 242 | addr_, min_interval, max_interval, latency, timeout); |
| 243 | LOG_EXEC_CB_AND_RET(cb, ret); |
| 244 | } |
| 245 | |
Bailey Forrest | 801a41d | 2018-03-31 01:01:22 | [diff] [blame] | 246 | bool RemoteDeviceImpl::IsConnected() { |
Bailey Forrest | 1276102 | 2018-03-22 18:50:16 | [diff] [blame] | 247 | return connected_; |
| 248 | } |
| 249 | |
Tiansong Cui | 2732254 | 2019-01-08 00:56:13 | [diff] [blame] | 250 | bool RemoteDeviceImpl::IsBonded() { |
| 251 | return bonded_; |
| 252 | } |
| 253 | |
Bailey Forrest | 801a41d | 2018-03-31 01:01:22 | [diff] [blame] | 254 | int RemoteDeviceImpl::GetMtu() { |
Bailey Forrest | 1276102 | 2018-03-22 18:50:16 | [diff] [blame] | 255 | return mtu_; |
| 256 | } |
| 257 | |
Bailey Forrest | 801a41d | 2018-03-31 01:01:22 | [diff] [blame] | 258 | void RemoteDeviceImpl::GetServices( |
Bailey Forrest | 1276102 | 2018-03-22 18:50:16 | [diff] [blame] | 259 | base::OnceCallback<void(std::vector<scoped_refptr<RemoteService>>)> cb) { |
Kyle Lund | 1de248a | 2018-03-27 17:26:44 | [diff] [blame] | 260 | MAKE_SURE_IO_THREAD(GetServices, BindToCurrentSequence(std::move(cb))); |
Bailey Forrest | 1276102 | 2018-03-22 18:50:16 | [diff] [blame] | 261 | auto ret = GetServicesSync(); |
| 262 | EXEC_CB_AND_RET(cb, std::move(ret)); |
| 263 | } |
| 264 | |
Bailey Forrest | 801a41d | 2018-03-31 01:01:22 | [diff] [blame] | 265 | std::vector<scoped_refptr<RemoteService>> RemoteDeviceImpl::GetServicesSync() { |
Bailey Forrest | 1276102 | 2018-03-22 18:50:16 | [diff] [blame] | 266 | DCHECK(io_task_runner_->BelongsToCurrentThread()); |
| 267 | std::vector<scoped_refptr<RemoteService>> services; |
| 268 | services.reserve(uuid_to_service_.size()); |
| 269 | for (const auto& pair : uuid_to_service_) |
| 270 | services.push_back(pair.second); |
| 271 | |
| 272 | return services; |
| 273 | } |
| 274 | |
Bailey Forrest | 801a41d | 2018-03-31 01:01:22 | [diff] [blame] | 275 | void RemoteDeviceImpl::GetServiceByUuid( |
Bailey Forrest | 1276102 | 2018-03-22 18:50:16 | [diff] [blame] | 276 | const bluetooth_v2_shlib::Uuid& uuid, |
| 277 | base::OnceCallback<void(scoped_refptr<RemoteService>)> cb) { |
| 278 | MAKE_SURE_IO_THREAD(GetServiceByUuid, uuid, |
Kyle Lund | 1de248a | 2018-03-27 17:26:44 | [diff] [blame] | 279 | BindToCurrentSequence(std::move(cb))); |
Bailey Forrest | 1276102 | 2018-03-22 18:50:16 | [diff] [blame] | 280 | auto ret = GetServiceByUuidSync(uuid); |
| 281 | EXEC_CB_AND_RET(cb, std::move(ret)); |
| 282 | } |
| 283 | |
Bailey Forrest | 801a41d | 2018-03-31 01:01:22 | [diff] [blame] | 284 | const bluetooth_v2_shlib::Addr& RemoteDeviceImpl::addr() const { |
| 285 | return addr_; |
| 286 | } |
| 287 | |
Bailey Forrest | 5ea6fa6 | 2018-06-29 00:02:44 | [diff] [blame] | 288 | void RemoteDeviceImpl::ReadCharacteristic( |
| 289 | scoped_refptr<RemoteCharacteristicImpl> characteristic, |
| 290 | bluetooth_v2_shlib::Gatt::Client::AuthReq auth_req, |
| 291 | RemoteCharacteristic::ReadCallback cb) { |
| 292 | DCHECK(io_task_runner_->BelongsToCurrentThread()); |
| 293 | handle_to_characteristic_read_cbs_[characteristic->handle()].push( |
| 294 | std::move(cb)); |
| 295 | |
Bailey Forrest | 9625bbf1 | 2018-08-15 01:27:17 | [diff] [blame] | 296 | EnqueueOperation( |
| 297 | __func__, base::BindOnce(&RemoteDeviceImpl::ReadCharacteristicImpl, this, |
| 298 | std::move(characteristic), auth_req)); |
Bailey Forrest | 5ea6fa6 | 2018-06-29 00:02:44 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | void RemoteDeviceImpl::WriteCharacteristic( |
| 302 | scoped_refptr<RemoteCharacteristicImpl> characteristic, |
| 303 | bluetooth_v2_shlib::Gatt::Client::AuthReq auth_req, |
| 304 | bluetooth_v2_shlib::Gatt::WriteType write_type, |
| 305 | std::vector<uint8_t> value, |
| 306 | RemoteCharacteristic::StatusCallback cb) { |
| 307 | DCHECK(io_task_runner_->BelongsToCurrentThread()); |
| 308 | handle_to_characteristic_write_cbs_[characteristic->handle()].push( |
| 309 | std::move(cb)); |
Bailey Forrest | 9625bbf1 | 2018-08-15 01:27:17 | [diff] [blame] | 310 | EnqueueOperation( |
| 311 | __func__, base::BindOnce(&RemoteDeviceImpl::WriteCharacteristicImpl, this, |
| 312 | std::move(characteristic), auth_req, write_type, |
| 313 | std::move(value))); |
Bailey Forrest | 5ea6fa6 | 2018-06-29 00:02:44 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | void RemoteDeviceImpl::ReadDescriptor( |
| 317 | scoped_refptr<RemoteDescriptorImpl> descriptor, |
| 318 | bluetooth_v2_shlib::Gatt::Client::AuthReq auth_req, |
| 319 | RemoteDescriptor::ReadCallback cb) { |
| 320 | DCHECK(io_task_runner_->BelongsToCurrentThread()); |
| 321 | handle_to_descriptor_read_cbs_[descriptor->handle()].push(std::move(cb)); |
| 322 | |
Bailey Forrest | 9625bbf1 | 2018-08-15 01:27:17 | [diff] [blame] | 323 | EnqueueOperation(__func__, |
| 324 | base::BindOnce(&RemoteDeviceImpl::ReadDescriptorImpl, this, |
Bailey Forrest | 5ea6fa6 | 2018-06-29 00:02:44 | [diff] [blame] | 325 | std::move(descriptor), auth_req)); |
| 326 | } |
| 327 | |
| 328 | void RemoteDeviceImpl::WriteDescriptor( |
| 329 | scoped_refptr<RemoteDescriptorImpl> descriptor, |
| 330 | bluetooth_v2_shlib::Gatt::Client::AuthReq auth_req, |
| 331 | std::vector<uint8_t> value, |
| 332 | RemoteDescriptor::StatusCallback cb) { |
| 333 | DCHECK(io_task_runner_->BelongsToCurrentThread()); |
| 334 | handle_to_descriptor_write_cbs_[descriptor->handle()].push(std::move(cb)); |
Bailey Forrest | 9625bbf1 | 2018-08-15 01:27:17 | [diff] [blame] | 335 | EnqueueOperation( |
| 336 | __func__, |
| 337 | base::BindOnce(&RemoteDeviceImpl::WriteDescriptorImpl, this, |
| 338 | std::move(descriptor), auth_req, std::move(value))); |
Bailey Forrest | 5ea6fa6 | 2018-06-29 00:02:44 | [diff] [blame] | 339 | } |
| 340 | |
Bailey Forrest | 801a41d | 2018-03-31 01:01:22 | [diff] [blame] | 341 | scoped_refptr<RemoteService> RemoteDeviceImpl::GetServiceByUuidSync( |
Bailey Forrest | 1276102 | 2018-03-22 18:50:16 | [diff] [blame] | 342 | const bluetooth_v2_shlib::Uuid& uuid) { |
| 343 | DCHECK(io_task_runner_->BelongsToCurrentThread()); |
| 344 | auto it = uuid_to_service_.find(uuid); |
| 345 | if (it == uuid_to_service_.end()) |
| 346 | return nullptr; |
| 347 | |
| 348 | return it->second; |
| 349 | } |
| 350 | |
Bailey Forrest | 801a41d | 2018-03-31 01:01:22 | [diff] [blame] | 351 | void RemoteDeviceImpl::SetConnected(bool connected) { |
Bailey Forrest | 1276102 | 2018-03-22 18:50:16 | [diff] [blame] | 352 | DCHECK(io_task_runner_->BelongsToCurrentThread()); |
Bailey Forrest | 78fa960 | 2018-06-12 23:10:57 | [diff] [blame] | 353 | // We only set connected = true and call the callback after services are |
| 354 | // discovered. |
Bailey Forrest | 3de5924 | 2018-04-26 17:45:33 | [diff] [blame] | 355 | if (!connected) { |
| 356 | connected_ = false; |
Bailey Forrest | 78fa960 | 2018-06-12 23:10:57 | [diff] [blame] | 357 | ConnectComplete(false); |
Bailey Forrest | 1276102 | 2018-03-22 18:50:16 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | if (disconnect_pending_) { |
| 361 | disconnect_pending_ = false; |
| 362 | if (disconnect_cb_) { |
| 363 | std::move(disconnect_cb_).Run(!connected); |
| 364 | } |
| 365 | } |
| 366 | |
Bailey Forrest | 1276102 | 2018-03-22 18:50:16 | [diff] [blame] | 367 | if (!connected && rssi_pending_) { |
| 368 | LOG(ERROR) << "Read remote RSSI failed: disconnected"; |
| 369 | if (rssi_cb_) { |
| 370 | std::move(rssi_cb_).Run(false, 0); |
| 371 | } |
| 372 | rssi_pending_ = false; |
| 373 | } |
| 374 | |
Bailey Forrest | 3de5924 | 2018-04-26 17:45:33 | [diff] [blame] | 375 | if (connected) { |
| 376 | if (!gatt_client_manager_) { |
| 377 | LOG(ERROR) << "Couldn't discover services: Destroyed"; |
| 378 | return; |
| 379 | } |
| 380 | |
| 381 | if (!gatt_client_manager_->gatt_client()->GetServices(addr_)) { |
| 382 | LOG(ERROR) << "Couldn't discover services, disconnecting"; |
| 383 | Disconnect({}); |
Bailey Forrest | 78fa960 | 2018-06-12 23:10:57 | [diff] [blame] | 384 | ConnectComplete(false); |
Bailey Forrest | 3de5924 | 2018-04-26 17:45:33 | [diff] [blame] | 385 | } |
Bailey Forrest | fda3509 | 2018-05-16 00:30:44 | [diff] [blame] | 386 | } else { |
Bailey Forrest | 5ea6fa6 | 2018-06-29 00:02:44 | [diff] [blame] | 387 | // Reset state after disconnection |
| 388 | mtu_ = kDefaultMtu; |
| 389 | ClearServices(); |
Bailey Forrest | 3de5924 | 2018-04-26 17:45:33 | [diff] [blame] | 390 | } |
| 391 | } |
| 392 | |
Tiansong Cui | 2732254 | 2019-01-08 00:56:13 | [diff] [blame] | 393 | void RemoteDeviceImpl::SetBonded(bool bonded) { |
| 394 | DCHECK(io_task_runner_->BelongsToCurrentThread()); |
| 395 | |
| 396 | bonded_ = bonded; |
| 397 | |
| 398 | if (create_bond_pending_) { |
| 399 | create_bond_pending_ = false; |
| 400 | if (create_bond_cb_) { |
| 401 | std::move(create_bond_cb_).Run(bonded); |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | if (remove_bond_pending_) { |
| 406 | remove_bond_pending_ = false; |
| 407 | if (remove_bond_cb_) { |
| 408 | std::move(remove_bond_cb_).Run(!bonded); |
| 409 | } |
| 410 | } |
| 411 | } |
| 412 | |
Bailey Forrest | 56e71abb | 2018-05-11 18:50:56 | [diff] [blame] | 413 | void RemoteDeviceImpl::SetServicesDiscovered(bool discovered) { |
Bailey Forrest | 3de5924 | 2018-04-26 17:45:33 | [diff] [blame] | 414 | DCHECK(io_task_runner_->BelongsToCurrentThread()); |
Bailey Forrest | 56e71abb | 2018-05-11 18:50:56 | [diff] [blame] | 415 | services_discovered_ = discovered; |
| 416 | if (!discovered) { |
| 417 | return; |
| 418 | } |
Bailey Forrest | 3de5924 | 2018-04-26 17:45:33 | [diff] [blame] | 419 | connected_ = true; |
Bailey Forrest | 78fa960 | 2018-06-12 23:10:57 | [diff] [blame] | 420 | ConnectComplete(true); |
Bailey Forrest | 3de5924 | 2018-04-26 17:45:33 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | bool RemoteDeviceImpl::GetServicesDiscovered() { |
| 424 | DCHECK(io_task_runner_->BelongsToCurrentThread()); |
| 425 | return services_discovered_; |
Bailey Forrest | 1276102 | 2018-03-22 18:50:16 | [diff] [blame] | 426 | } |
| 427 | |
Bailey Forrest | 801a41d | 2018-03-31 01:01:22 | [diff] [blame] | 428 | void RemoteDeviceImpl::SetMtu(int mtu) { |
Bailey Forrest | 1276102 | 2018-03-22 18:50:16 | [diff] [blame] | 429 | DCHECK(io_task_runner_->BelongsToCurrentThread()); |
Bailey Forrest | 1276102 | 2018-03-22 18:50:16 | [diff] [blame] | 430 | mtu_ = mtu; |
Bailey Forrest | 5ea6fa6 | 2018-06-29 00:02:44 | [diff] [blame] | 431 | if (!mtu_callbacks_.empty()) { |
| 432 | std::move(mtu_callbacks_.front()).Run(true); |
| 433 | mtu_callbacks_.pop(); |
| 434 | NotifyQueueOperationComplete(); |
Bailey Forrest | 1276102 | 2018-03-22 18:50:16 | [diff] [blame] | 435 | } |
| 436 | } |
| 437 | |
Bailey Forrest | 801a41d | 2018-03-31 01:01:22 | [diff] [blame] | 438 | scoped_refptr<RemoteCharacteristic> RemoteDeviceImpl::CharacteristicFromHandle( |
Bailey Forrest | 1276102 | 2018-03-22 18:50:16 | [diff] [blame] | 439 | uint16_t handle) { |
| 440 | DCHECK(io_task_runner_->BelongsToCurrentThread()); |
| 441 | auto it = handle_to_characteristic_.find(handle); |
| 442 | if (it == handle_to_characteristic_.end()) |
| 443 | return nullptr; |
| 444 | |
| 445 | return it->second; |
| 446 | } |
| 447 | |
Bailey Forrest | 5ea6fa6 | 2018-06-29 00:02:44 | [diff] [blame] | 448 | void RemoteDeviceImpl::OnCharacteristicRead(bool status, |
| 449 | uint16_t handle, |
| 450 | const std::vector<uint8_t>& value) { |
Bailey Forrest | 1276102 | 2018-03-22 18:50:16 | [diff] [blame] | 451 | DCHECK(io_task_runner_->BelongsToCurrentThread()); |
Bailey Forrest | 5ea6fa6 | 2018-06-29 00:02:44 | [diff] [blame] | 452 | auto it = handle_to_characteristic_read_cbs_.find(handle); |
| 453 | if (it == handle_to_characteristic_read_cbs_.end() || it->second.empty()) { |
| 454 | LOG(ERROR) << "No such characteristic read"; |
| 455 | } else { |
| 456 | std::move(it->second.front()).Run(status, value); |
| 457 | it->second.pop(); |
| 458 | } |
| 459 | NotifyQueueOperationComplete(); |
| 460 | } |
Bailey Forrest | 1276102 | 2018-03-22 18:50:16 | [diff] [blame] | 461 | |
Bailey Forrest | 5ea6fa6 | 2018-06-29 00:02:44 | [diff] [blame] | 462 | void RemoteDeviceImpl::OnCharacteristicWrite(bool status, uint16_t handle) { |
| 463 | DCHECK(io_task_runner_->BelongsToCurrentThread()); |
| 464 | auto it = handle_to_characteristic_write_cbs_.find(handle); |
| 465 | if (it == handle_to_characteristic_write_cbs_.end() || it->second.empty()) { |
| 466 | LOG(ERROR) << "No such characteristic write"; |
| 467 | } else { |
| 468 | std::move(it->second.front()).Run(status); |
| 469 | it->second.pop(); |
| 470 | } |
| 471 | NotifyQueueOperationComplete(); |
| 472 | } |
| 473 | |
| 474 | void RemoteDeviceImpl::OnDescriptorRead(bool status, |
| 475 | uint16_t handle, |
| 476 | const std::vector<uint8_t>& value) { |
| 477 | DCHECK(io_task_runner_->BelongsToCurrentThread()); |
| 478 | auto it = handle_to_descriptor_read_cbs_.find(handle); |
| 479 | if (it == handle_to_descriptor_read_cbs_.end() || it->second.empty()) { |
| 480 | LOG(ERROR) << "No such descriptor read"; |
| 481 | } else { |
| 482 | std::move(it->second.front()).Run(status, value); |
| 483 | it->second.pop(); |
| 484 | } |
| 485 | NotifyQueueOperationComplete(); |
| 486 | } |
| 487 | |
| 488 | void RemoteDeviceImpl::OnDescriptorWrite(bool status, uint16_t handle) { |
| 489 | DCHECK(io_task_runner_->BelongsToCurrentThread()); |
| 490 | auto it = handle_to_descriptor_write_cbs_.find(handle); |
| 491 | if (it == handle_to_descriptor_write_cbs_.end() || it->second.empty()) { |
| 492 | LOG(ERROR) << "No such descriptor write"; |
| 493 | } else { |
| 494 | std::move(it->second.front()).Run(status); |
| 495 | it->second.pop(); |
| 496 | } |
| 497 | NotifyQueueOperationComplete(); |
Bailey Forrest | 1276102 | 2018-03-22 18:50:16 | [diff] [blame] | 498 | } |
| 499 | |
Bailey Forrest | 801a41d | 2018-03-31 01:01:22 | [diff] [blame] | 500 | void RemoteDeviceImpl::OnGetServices( |
Bailey Forrest | 1276102 | 2018-03-22 18:50:16 | [diff] [blame] | 501 | const std::vector<bluetooth_v2_shlib::Gatt::Service>& services) { |
| 502 | DCHECK(io_task_runner_->BelongsToCurrentThread()); |
Bailey Forrest | 5ea6fa6 | 2018-06-29 00:02:44 | [diff] [blame] | 503 | ClearServices(); |
Bailey Forrest | 1276102 | 2018-03-22 18:50:16 | [diff] [blame] | 504 | OnServicesAdded(services); |
Bailey Forrest | 1276102 | 2018-03-22 18:50:16 | [diff] [blame] | 505 | } |
| 506 | |
Bailey Forrest | 801a41d | 2018-03-31 01:01:22 | [diff] [blame] | 507 | void RemoteDeviceImpl::OnServicesRemoved(uint16_t start_handle, |
| 508 | uint16_t end_handle) { |
Bailey Forrest | 1276102 | 2018-03-22 18:50:16 | [diff] [blame] | 509 | DCHECK(io_task_runner_->BelongsToCurrentThread()); |
| 510 | for (auto it = uuid_to_service_.begin(); it != uuid_to_service_.end();) { |
| 511 | if (it->second->handle() >= start_handle && |
| 512 | it->second->handle() <= end_handle) { |
| 513 | for (auto& characteristic : it->second->GetCharacteristics()) { |
Bailey Forrest | 1276102 | 2018-03-22 18:50:16 | [diff] [blame] | 514 | handle_to_characteristic_.erase(characteristic->handle()); |
| 515 | } |
| 516 | it = uuid_to_service_.erase(it); |
| 517 | } else { |
| 518 | ++it; |
| 519 | } |
| 520 | } |
Bailey Forrest | 1276102 | 2018-03-22 18:50:16 | [diff] [blame] | 521 | } |
| 522 | |
Bailey Forrest | 801a41d | 2018-03-31 01:01:22 | [diff] [blame] | 523 | void RemoteDeviceImpl::OnServicesAdded( |
Bailey Forrest | 1276102 | 2018-03-22 18:50:16 | [diff] [blame] | 524 | const std::vector<bluetooth_v2_shlib::Gatt::Service>& services) { |
| 525 | DCHECK(io_task_runner_->BelongsToCurrentThread()); |
| 526 | for (const auto& service : services) { |
Bailey Forrest | 801a41d | 2018-03-31 01:01:22 | [diff] [blame] | 527 | uuid_to_service_[service.uuid] = new RemoteServiceImpl( |
| 528 | this, gatt_client_manager_, service, io_task_runner_); |
Bailey Forrest | 1276102 | 2018-03-22 18:50:16 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | for (const auto& pair : uuid_to_service_) { |
| 532 | for (auto& characteristic : pair.second->GetCharacteristics()) { |
Bailey Forrest | 5ea6fa6 | 2018-06-29 00:02:44 | [diff] [blame] | 533 | handle_to_characteristic_.emplace( |
| 534 | characteristic->handle(), |
| 535 | static_cast<RemoteCharacteristicImpl*>(characteristic.get())); |
Bailey Forrest | 1276102 | 2018-03-22 18:50:16 | [diff] [blame] | 536 | } |
| 537 | } |
Bailey Forrest | 1276102 | 2018-03-22 18:50:16 | [diff] [blame] | 538 | } |
| 539 | |
Bailey Forrest | 801a41d | 2018-03-31 01:01:22 | [diff] [blame] | 540 | void RemoteDeviceImpl::OnReadRemoteRssiComplete(bool status, int rssi) { |
Bailey Forrest | 1276102 | 2018-03-22 18:50:16 | [diff] [blame] | 541 | DCHECK(io_task_runner_->BelongsToCurrentThread()); |
| 542 | rssi_pending_ = false; |
| 543 | if (rssi_cb_) { |
Tiansong Cui | cbfc37e | 2018-07-30 20:06:27 | [diff] [blame] | 544 | std::move(rssi_cb_).Run(status, rssi); |
Bailey Forrest | 1276102 | 2018-03-22 18:50:16 | [diff] [blame] | 545 | } |
| 546 | } |
| 547 | |
Bailey Forrest | 78fa960 | 2018-06-12 23:10:57 | [diff] [blame] | 548 | void RemoteDeviceImpl::ConnectComplete(bool success) { |
| 549 | DCHECK(io_task_runner_->BelongsToCurrentThread()); |
| 550 | if (connect_pending_) { |
| 551 | connect_pending_ = false; |
| 552 | if (connect_cb_) { |
| 553 | std::move(connect_cb_).Run(success); |
| 554 | } |
| 555 | } |
| 556 | } |
| 557 | |
Bailey Forrest | 9625bbf1 | 2018-08-15 01:27:17 | [diff] [blame] | 558 | void RemoteDeviceImpl::EnqueueOperation(const std::string& name, |
| 559 | base::OnceClosure op) { |
Bailey Forrest | 5ea6fa6 | 2018-06-29 00:02:44 | [diff] [blame] | 560 | DCHECK(io_task_runner_->BelongsToCurrentThread()); |
Bailey Forrest | 9625bbf1 | 2018-08-15 01:27:17 | [diff] [blame] | 561 | command_queue_.emplace_back(name, std::move(op)); |
Bailey Forrest | 5ea6fa6 | 2018-06-29 00:02:44 | [diff] [blame] | 562 | |
| 563 | // Run the operation if this is the only operation in the queue. Otherwise, it |
| 564 | // will be executed when the current operation completes. |
| 565 | if (command_queue_.size() == 1) { |
Bailey Forrest | 9625bbf1 | 2018-08-15 01:27:17 | [diff] [blame] | 566 | RunNextOperation(); |
Bailey Forrest | 5ea6fa6 | 2018-06-29 00:02:44 | [diff] [blame] | 567 | } |
| 568 | } |
| 569 | |
| 570 | void RemoteDeviceImpl::NotifyQueueOperationComplete() { |
| 571 | DCHECK(io_task_runner_->BelongsToCurrentThread()); |
| 572 | DCHECK(!command_queue_.empty()); |
| 573 | command_queue_.pop_front(); |
Bailey Forrest | 9625bbf1 | 2018-08-15 01:27:17 | [diff] [blame] | 574 | command_timeout_timer_.Stop(); |
Bailey Forrest | 5ea6fa6 | 2018-06-29 00:02:44 | [diff] [blame] | 575 | |
| 576 | // Run the next operation if there is one in the queue. |
| 577 | if (!command_queue_.empty()) { |
Bailey Forrest | 9625bbf1 | 2018-08-15 01:27:17 | [diff] [blame] | 578 | RunNextOperation(); |
Bailey Forrest | 5ea6fa6 | 2018-06-29 00:02:44 | [diff] [blame] | 579 | } |
| 580 | } |
| 581 | |
Bailey Forrest | 9625bbf1 | 2018-08-15 01:27:17 | [diff] [blame] | 582 | void RemoteDeviceImpl::RunNextOperation() { |
| 583 | DCHECK(io_task_runner_->BelongsToCurrentThread()); |
| 584 | DCHECK(!command_queue_.empty()); |
| 585 | auto& front = command_queue_.front(); |
| 586 | command_timeout_timer_.Start( |
| 587 | FROM_HERE, kCommandTimeout, |
| 588 | base::BindRepeating(&RemoteDeviceImpl::OnCommandTimeout, this, |
| 589 | front.first)); |
| 590 | std::move(front.second).Run(); |
| 591 | } |
| 592 | |
Bailey Forrest | 5ea6fa6 | 2018-06-29 00:02:44 | [diff] [blame] | 593 | void RemoteDeviceImpl::RequestMtuImpl(int mtu) { |
| 594 | DCHECK(io_task_runner_->BelongsToCurrentThread()); |
| 595 | if (gatt_client_manager_->gatt_client()->RequestMtu(addr_, mtu)) { |
| 596 | return; |
| 597 | } |
| 598 | |
| 599 | LOG(ERROR) << __func__ << " failed"; |
| 600 | DCHECK(!mtu_callbacks_.empty()); |
| 601 | std::move(mtu_callbacks_.front()).Run(false); |
| 602 | mtu_callbacks_.pop(); |
| 603 | NotifyQueueOperationComplete(); |
| 604 | } |
| 605 | |
| 606 | void RemoteDeviceImpl::ReadCharacteristicImpl( |
| 607 | scoped_refptr<RemoteCharacteristicImpl> characteristic, |
| 608 | bluetooth_v2_shlib::Gatt::Client::AuthReq auth_req) { |
| 609 | DCHECK(io_task_runner_->BelongsToCurrentThread()); |
| 610 | if (gatt_client_manager_->gatt_client()->ReadCharacteristic( |
| 611 | addr(), characteristic->characteristic(), auth_req)) { |
| 612 | return; |
| 613 | } |
| 614 | |
| 615 | LOG(ERROR) << __func__ << " failed"; |
| 616 | auto it = handle_to_characteristic_read_cbs_.find(characteristic->handle()); |
| 617 | DCHECK(it != handle_to_characteristic_read_cbs_.end()); |
| 618 | DCHECK(!it->second.empty()); |
| 619 | std::move(it->second.front()).Run(false, {}); |
| 620 | it->second.pop(); |
| 621 | NotifyQueueOperationComplete(); |
| 622 | } |
| 623 | |
| 624 | void RemoteDeviceImpl::WriteCharacteristicImpl( |
| 625 | scoped_refptr<RemoteCharacteristicImpl> characteristic, |
| 626 | bluetooth_v2_shlib::Gatt::Client::AuthReq auth_req, |
| 627 | bluetooth_v2_shlib::Gatt::WriteType write_type, |
| 628 | std::vector<uint8_t> value) { |
| 629 | DCHECK(io_task_runner_->BelongsToCurrentThread()); |
| 630 | if (gatt_client_manager_->gatt_client()->WriteCharacteristic( |
| 631 | addr(), characteristic->characteristic(), auth_req, write_type, |
| 632 | value)) { |
| 633 | return; |
| 634 | } |
| 635 | |
| 636 | LOG(ERROR) << __func__ << " failed"; |
| 637 | auto it = handle_to_characteristic_write_cbs_.find(characteristic->handle()); |
| 638 | DCHECK(it != handle_to_characteristic_write_cbs_.end()); |
| 639 | DCHECK(!it->second.empty()); |
| 640 | std::move(it->second.front()).Run(false); |
| 641 | it->second.pop(); |
| 642 | NotifyQueueOperationComplete(); |
| 643 | } |
| 644 | |
| 645 | void RemoteDeviceImpl::ReadDescriptorImpl( |
| 646 | scoped_refptr<RemoteDescriptorImpl> descriptor, |
| 647 | bluetooth_v2_shlib::Gatt::Client::AuthReq auth_req) { |
| 648 | DCHECK(io_task_runner_->BelongsToCurrentThread()); |
| 649 | if (gatt_client_manager_->gatt_client()->ReadDescriptor( |
| 650 | addr(), descriptor->descriptor(), auth_req)) { |
| 651 | return; |
| 652 | } |
| 653 | |
| 654 | LOG(ERROR) << __func__ << " failed"; |
| 655 | auto it = handle_to_descriptor_read_cbs_.find(descriptor->handle()); |
| 656 | DCHECK(it != handle_to_descriptor_read_cbs_.end()); |
| 657 | DCHECK(!it->second.empty()); |
| 658 | std::move(it->second.front()).Run(false, {}); |
| 659 | it->second.pop(); |
| 660 | NotifyQueueOperationComplete(); |
| 661 | } |
| 662 | |
| 663 | void RemoteDeviceImpl::WriteDescriptorImpl( |
| 664 | scoped_refptr<RemoteDescriptorImpl> descriptor, |
| 665 | bluetooth_v2_shlib::Gatt::Client::AuthReq auth_req, |
| 666 | std::vector<uint8_t> value) { |
| 667 | DCHECK(io_task_runner_->BelongsToCurrentThread()); |
| 668 | if (gatt_client_manager_->gatt_client()->WriteDescriptor( |
| 669 | addr(), descriptor->descriptor(), auth_req, value)) { |
| 670 | return; |
| 671 | } |
| 672 | |
| 673 | LOG(ERROR) << __func__ << " failed"; |
| 674 | auto it = handle_to_descriptor_write_cbs_.find(descriptor->handle()); |
| 675 | DCHECK(it != handle_to_descriptor_write_cbs_.end()); |
| 676 | DCHECK(!it->second.empty()); |
| 677 | std::move(it->second.front()).Run(false); |
| 678 | it->second.pop(); |
| 679 | NotifyQueueOperationComplete(); |
| 680 | } |
| 681 | |
| 682 | void RemoteDeviceImpl::ClearServices() { |
| 683 | for (auto& item : handle_to_characteristic_) { |
| 684 | item.second->Invalidate(); |
| 685 | } |
| 686 | |
| 687 | uuid_to_service_.clear(); |
| 688 | handle_to_characteristic_.clear(); |
| 689 | command_queue_.clear(); |
Bailey Forrest | 9625bbf1 | 2018-08-15 01:27:17 | [diff] [blame] | 690 | command_timeout_timer_.Stop(); |
Bailey Forrest | 5ea6fa6 | 2018-06-29 00:02:44 | [diff] [blame] | 691 | |
| 692 | while (!mtu_callbacks_.empty()) { |
| 693 | LOG(ERROR) << "RequestMtu failed: disconnected"; |
| 694 | std::move(mtu_callbacks_.front()).Run(false); |
| 695 | mtu_callbacks_.pop(); |
| 696 | } |
| 697 | |
| 698 | for (auto& item : handle_to_characteristic_read_cbs_) { |
| 699 | auto& queue = item.second; |
| 700 | while (!queue.empty()) { |
| 701 | LOG(ERROR) << "Characteristic read failed: disconnected"; |
| 702 | std::move(queue.front()).Run(false, {}); |
| 703 | queue.pop(); |
| 704 | } |
| 705 | } |
| 706 | handle_to_characteristic_read_cbs_.clear(); |
| 707 | |
| 708 | for (auto& item : handle_to_characteristic_write_cbs_) { |
| 709 | auto& queue = item.second; |
| 710 | while (!queue.empty()) { |
| 711 | LOG(ERROR) << "Characteristic write failed: disconnected"; |
| 712 | std::move(queue.front()).Run(false); |
| 713 | queue.pop(); |
| 714 | } |
| 715 | } |
| 716 | handle_to_characteristic_write_cbs_.clear(); |
| 717 | |
| 718 | for (auto& item : handle_to_descriptor_read_cbs_) { |
| 719 | auto& queue = item.second; |
| 720 | while (!queue.empty()) { |
| 721 | LOG(ERROR) << "Descriptor read failed: disconnected"; |
| 722 | std::move(queue.front()).Run(false, {}); |
| 723 | queue.pop(); |
| 724 | } |
| 725 | } |
| 726 | handle_to_descriptor_read_cbs_.clear(); |
| 727 | |
| 728 | for (auto& item : handle_to_descriptor_write_cbs_) { |
| 729 | auto& queue = item.second; |
| 730 | while (!queue.empty()) { |
| 731 | LOG(ERROR) << "Descriptor write failed: disconnected"; |
| 732 | std::move(queue.front()).Run(false); |
| 733 | queue.pop(); |
| 734 | } |
| 735 | } |
| 736 | handle_to_descriptor_write_cbs_.clear(); |
| 737 | } |
| 738 | |
Bailey Forrest | 9625bbf1 | 2018-08-15 01:27:17 | [diff] [blame] | 739 | void RemoteDeviceImpl::OnCommandTimeout(const std::string& name) { |
| 740 | DCHECK(io_task_runner_->BelongsToCurrentThread()); |
Bailey Forrest | 8a2e40fe | 2018-08-21 23:43:00 | [diff] [blame] | 741 | LOG(ERROR) << name << "(" << util::AddrLastByteString(addr_) << ")" |
Bailey Forrest | 9625bbf1 | 2018-08-15 01:27:17 | [diff] [blame] | 742 | << " timed out. Disconnecting"; |
| 743 | Disconnect(base::DoNothing()); |
| 744 | } |
| 745 | |
Bailey Forrest | 1276102 | 2018-03-22 18:50:16 | [diff] [blame] | 746 | } // namespace bluetooth |
| 747 | } // namespace chromecast |