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" |
| 8 | #include "chromecast/base/bind_to_task_runner.h" |
Bailey Forrest | 801a41d | 2018-03-31 01:01:22 | [diff] [blame] | 9 | #include "chromecast/device/bluetooth/le/gatt_client_manager_impl.h" |
| 10 | #include "chromecast/device/bluetooth/le/remote_characteristic_impl.h" |
| 11 | #include "chromecast/device/bluetooth/le/remote_descriptor_impl.h" |
| 12 | #include "chromecast/device/bluetooth/le/remote_service_impl.h" |
Bailey Forrest | 1276102 | 2018-03-22 18:50:16 | [diff] [blame] | 13 | |
| 14 | namespace chromecast { |
| 15 | namespace bluetooth { |
| 16 | |
| 17 | #define RUN_ON_IO_THREAD(method, ...) \ |
| 18 | io_task_runner_->PostTask( \ |
Bailey Forrest | 801a41d | 2018-03-31 01:01:22 | [diff] [blame] | 19 | FROM_HERE, \ |
| 20 | base::BindOnce(&RemoteDeviceImpl::method, this, ##__VA_ARGS__)); |
Bailey Forrest | 1276102 | 2018-03-22 18:50:16 | [diff] [blame] | 21 | |
| 22 | #define MAKE_SURE_IO_THREAD(method, ...) \ |
| 23 | DCHECK(io_task_runner_); \ |
| 24 | if (!io_task_runner_->BelongsToCurrentThread()) { \ |
| 25 | RUN_ON_IO_THREAD(method, ##__VA_ARGS__) \ |
| 26 | return; \ |
| 27 | } |
| 28 | |
| 29 | #define EXEC_CB_AND_RET(cb, ret, ...) \ |
| 30 | do { \ |
| 31 | if (cb) { \ |
| 32 | std::move(cb).Run(ret, ##__VA_ARGS__); \ |
| 33 | } \ |
| 34 | return; \ |
| 35 | } while (0) |
| 36 | |
| 37 | #define CHECK_CONNECTED(cb) \ |
| 38 | do { \ |
| 39 | if (!connected_) { \ |
| 40 | LOG(ERROR) << __func__ << "failed: Not connected"; \ |
| 41 | EXEC_CB_AND_RET(cb, false); \ |
| 42 | } \ |
| 43 | } while (0) |
| 44 | |
| 45 | #define LOG_EXEC_CB_AND_RET(cb, ret) \ |
| 46 | do { \ |
| 47 | if (!ret) { \ |
| 48 | LOG(ERROR) << __func__ << "failed"; \ |
| 49 | } \ |
| 50 | EXEC_CB_AND_RET(cb, ret); \ |
| 51 | } while (0) |
| 52 | |
Bailey Forrest | 801a41d | 2018-03-31 01:01:22 | [diff] [blame] | 53 | RemoteDeviceImpl::RemoteDeviceImpl( |
Bailey Forrest | 1276102 | 2018-03-22 18:50:16 | [diff] [blame] | 54 | const bluetooth_v2_shlib::Addr& addr, |
Bailey Forrest | 801a41d | 2018-03-31 01:01:22 | [diff] [blame] | 55 | base::WeakPtr<GattClientManagerImpl> gatt_client_manager, |
Bailey Forrest | 1276102 | 2018-03-22 18:50:16 | [diff] [blame] | 56 | scoped_refptr<base::SingleThreadTaskRunner> io_task_runner) |
| 57 | : gatt_client_manager_(gatt_client_manager), |
| 58 | addr_(addr), |
| 59 | io_task_runner_(io_task_runner) { |
| 60 | DCHECK(gatt_client_manager); |
| 61 | DCHECK(io_task_runner_->BelongsToCurrentThread()); |
| 62 | } |
| 63 | |
Bailey Forrest | 801a41d | 2018-03-31 01:01:22 | [diff] [blame] | 64 | RemoteDeviceImpl::~RemoteDeviceImpl() = default; |
Bailey Forrest | 1276102 | 2018-03-22 18:50:16 | [diff] [blame] | 65 | |
Bailey Forrest | 801a41d | 2018-03-31 01:01:22 | [diff] [blame] | 66 | void RemoteDeviceImpl::Connect(StatusCallback cb) { |
Kyle Lund | 1de248a | 2018-03-27 17:26:44 | [diff] [blame] | 67 | MAKE_SURE_IO_THREAD(Connect, BindToCurrentSequence(std::move(cb))); |
Bailey Forrest | 1276102 | 2018-03-22 18:50:16 | [diff] [blame] | 68 | if (!ConnectSync()) { |
| 69 | // Error logged. |
| 70 | EXEC_CB_AND_RET(cb, false); |
| 71 | } |
| 72 | |
| 73 | connect_cb_ = std::move(cb); |
| 74 | } |
| 75 | |
Bailey Forrest | 801a41d | 2018-03-31 01:01:22 | [diff] [blame] | 76 | bool RemoteDeviceImpl::ConnectSync() { |
Bailey Forrest | 1276102 | 2018-03-22 18:50:16 | [diff] [blame] | 77 | DCHECK(io_task_runner_->BelongsToCurrentThread()); |
| 78 | if (!gatt_client_manager_) { |
| 79 | LOG(ERROR) << __func__ << " failed: Destroyed"; |
| 80 | return false; |
| 81 | } |
| 82 | if (connect_pending_) { |
| 83 | LOG(ERROR) << __func__ << " failed: Connection pending"; |
| 84 | return false; |
| 85 | } |
| 86 | |
| 87 | gatt_client_manager_->NotifyConnect(addr_); |
| 88 | if (!gatt_client_manager_->gatt_client()->Connect(addr_)) { |
| 89 | LOG(ERROR) << __func__ << " failed"; |
| 90 | return false; |
| 91 | } |
| 92 | connect_pending_ = true; |
| 93 | return true; |
| 94 | } |
| 95 | |
Bailey Forrest | 801a41d | 2018-03-31 01:01:22 | [diff] [blame] | 96 | void RemoteDeviceImpl::Disconnect(StatusCallback cb) { |
Kyle Lund | 1de248a | 2018-03-27 17:26:44 | [diff] [blame] | 97 | MAKE_SURE_IO_THREAD(Disconnect, BindToCurrentSequence(std::move(cb))); |
Bailey Forrest | 1276102 | 2018-03-22 18:50:16 | [diff] [blame] | 98 | if (!DisconnectSync()) { |
| 99 | // Error logged. |
| 100 | EXEC_CB_AND_RET(cb, false); |
| 101 | } |
| 102 | |
| 103 | disconnect_cb_ = std::move(cb); |
| 104 | } |
| 105 | |
Bailey Forrest | 801a41d | 2018-03-31 01:01:22 | [diff] [blame] | 106 | bool RemoteDeviceImpl::DisconnectSync() { |
Bailey Forrest | 1276102 | 2018-03-22 18:50:16 | [diff] [blame] | 107 | DCHECK(io_task_runner_->BelongsToCurrentThread()); |
| 108 | if (!gatt_client_manager_) { |
| 109 | LOG(ERROR) << __func__ << " failed: Destroyed"; |
| 110 | return false; |
| 111 | } |
| 112 | |
| 113 | if (!connected_) { |
| 114 | LOG(ERROR) << "Not connected"; |
| 115 | return false; |
| 116 | } |
| 117 | |
| 118 | if (!gatt_client_manager_->gatt_client()->Disconnect(addr_)) { |
| 119 | LOG(ERROR) << __func__ << " failed"; |
| 120 | return false; |
| 121 | } |
| 122 | disconnect_pending_ = true; |
| 123 | |
| 124 | return true; |
| 125 | } |
| 126 | |
Bailey Forrest | 801a41d | 2018-03-31 01:01:22 | [diff] [blame] | 127 | void RemoteDeviceImpl::ReadRemoteRssi(RssiCallback cb) { |
Kyle Lund | 1de248a | 2018-03-27 17:26:44 | [diff] [blame] | 128 | MAKE_SURE_IO_THREAD(ReadRemoteRssi, BindToCurrentSequence(std::move(cb))); |
Bailey Forrest | 1276102 | 2018-03-22 18:50:16 | [diff] [blame] | 129 | if (!gatt_client_manager_) { |
| 130 | LOG(ERROR) << __func__ << " failed: Destroyed"; |
| 131 | EXEC_CB_AND_RET(cb, false, 0); |
| 132 | } |
| 133 | |
| 134 | if (rssi_pending_) { |
| 135 | LOG(ERROR) << "Read remote RSSI already pending"; |
| 136 | EXEC_CB_AND_RET(cb, false, 0); |
| 137 | } |
| 138 | if (!gatt_client_manager_->gatt_client()->ReadRemoteRssi(addr_)) { |
| 139 | LOG(ERROR) << __func__ << " failed"; |
| 140 | EXEC_CB_AND_RET(cb, false, 0); |
| 141 | } |
| 142 | rssi_pending_ = true; |
| 143 | rssi_cb_ = std::move(cb); |
| 144 | } |
| 145 | |
Bailey Forrest | 801a41d | 2018-03-31 01:01:22 | [diff] [blame] | 146 | void RemoteDeviceImpl::RequestMtu(int mtu, StatusCallback cb) { |
Kyle Lund | 1de248a | 2018-03-27 17:26:44 | [diff] [blame] | 147 | MAKE_SURE_IO_THREAD(RequestMtu, mtu, BindToCurrentSequence(std::move(cb))); |
Bailey Forrest | 1276102 | 2018-03-22 18:50:16 | [diff] [blame] | 148 | if (!gatt_client_manager_) { |
| 149 | LOG(ERROR) << __func__ << " failed: Destroyed"; |
| 150 | EXEC_CB_AND_RET(cb, false); |
| 151 | } |
| 152 | CHECK_CONNECTED(cb); |
| 153 | if (mtu_pending_) { |
| 154 | LOG(ERROR) << "MTU change already pending"; |
| 155 | EXEC_CB_AND_RET(cb, false); |
| 156 | } |
| 157 | |
| 158 | if (!gatt_client_manager_->gatt_client()->RequestMtu(addr_, mtu)) { |
| 159 | LOG(ERROR) << __func__ << " failed"; |
| 160 | EXEC_CB_AND_RET(cb, false); |
| 161 | } |
| 162 | |
| 163 | mtu_pending_ = true; |
| 164 | mtu_cb_ = std::move(cb); |
| 165 | } |
| 166 | |
Bailey Forrest | 801a41d | 2018-03-31 01:01:22 | [diff] [blame] | 167 | void RemoteDeviceImpl::ConnectionParameterUpdate(int min_interval, |
| 168 | int max_interval, |
| 169 | int latency, |
| 170 | int timeout, |
| 171 | StatusCallback cb) { |
Bailey Forrest | 1276102 | 2018-03-22 18:50:16 | [diff] [blame] | 172 | MAKE_SURE_IO_THREAD(ConnectionParameterUpdate, min_interval, max_interval, |
Kyle Lund | 1de248a | 2018-03-27 17:26:44 | [diff] [blame] | 173 | latency, timeout, BindToCurrentSequence(std::move(cb))); |
Bailey Forrest | 1276102 | 2018-03-22 18:50:16 | [diff] [blame] | 174 | if (!gatt_client_manager_) { |
| 175 | LOG(ERROR) << __func__ << " failed: Destroyed"; |
| 176 | EXEC_CB_AND_RET(cb, false); |
| 177 | } |
| 178 | CHECK_CONNECTED(cb); |
| 179 | bool ret = gatt_client_manager_->gatt_client()->ConnectionParameterUpdate( |
| 180 | addr_, min_interval, max_interval, latency, timeout); |
| 181 | LOG_EXEC_CB_AND_RET(cb, ret); |
| 182 | } |
| 183 | |
Bailey Forrest | 801a41d | 2018-03-31 01:01:22 | [diff] [blame] | 184 | bool RemoteDeviceImpl::IsConnected() { |
Bailey Forrest | 1276102 | 2018-03-22 18:50:16 | [diff] [blame] | 185 | return connected_; |
| 186 | } |
| 187 | |
Bailey Forrest | 801a41d | 2018-03-31 01:01:22 | [diff] [blame] | 188 | int RemoteDeviceImpl::GetMtu() { |
Bailey Forrest | 1276102 | 2018-03-22 18:50:16 | [diff] [blame] | 189 | return mtu_; |
| 190 | } |
| 191 | |
Bailey Forrest | 801a41d | 2018-03-31 01:01:22 | [diff] [blame] | 192 | void RemoteDeviceImpl::GetServices( |
Bailey Forrest | 1276102 | 2018-03-22 18:50:16 | [diff] [blame] | 193 | base::OnceCallback<void(std::vector<scoped_refptr<RemoteService>>)> cb) { |
Kyle Lund | 1de248a | 2018-03-27 17:26:44 | [diff] [blame] | 194 | MAKE_SURE_IO_THREAD(GetServices, BindToCurrentSequence(std::move(cb))); |
Bailey Forrest | 1276102 | 2018-03-22 18:50:16 | [diff] [blame] | 195 | auto ret = GetServicesSync(); |
| 196 | EXEC_CB_AND_RET(cb, std::move(ret)); |
| 197 | } |
| 198 | |
Bailey Forrest | 801a41d | 2018-03-31 01:01:22 | [diff] [blame] | 199 | std::vector<scoped_refptr<RemoteService>> RemoteDeviceImpl::GetServicesSync() { |
Bailey Forrest | 1276102 | 2018-03-22 18:50:16 | [diff] [blame] | 200 | DCHECK(io_task_runner_->BelongsToCurrentThread()); |
| 201 | std::vector<scoped_refptr<RemoteService>> services; |
| 202 | services.reserve(uuid_to_service_.size()); |
| 203 | for (const auto& pair : uuid_to_service_) |
| 204 | services.push_back(pair.second); |
| 205 | |
| 206 | return services; |
| 207 | } |
| 208 | |
Bailey Forrest | 801a41d | 2018-03-31 01:01:22 | [diff] [blame] | 209 | void RemoteDeviceImpl::GetServiceByUuid( |
Bailey Forrest | 1276102 | 2018-03-22 18:50:16 | [diff] [blame] | 210 | const bluetooth_v2_shlib::Uuid& uuid, |
| 211 | base::OnceCallback<void(scoped_refptr<RemoteService>)> cb) { |
| 212 | MAKE_SURE_IO_THREAD(GetServiceByUuid, uuid, |
Kyle Lund | 1de248a | 2018-03-27 17:26:44 | [diff] [blame] | 213 | BindToCurrentSequence(std::move(cb))); |
Bailey Forrest | 1276102 | 2018-03-22 18:50:16 | [diff] [blame] | 214 | auto ret = GetServiceByUuidSync(uuid); |
| 215 | EXEC_CB_AND_RET(cb, std::move(ret)); |
| 216 | } |
| 217 | |
Bailey Forrest | 801a41d | 2018-03-31 01:01:22 | [diff] [blame] | 218 | const bluetooth_v2_shlib::Addr& RemoteDeviceImpl::addr() const { |
| 219 | return addr_; |
| 220 | } |
| 221 | |
| 222 | scoped_refptr<RemoteService> RemoteDeviceImpl::GetServiceByUuidSync( |
Bailey Forrest | 1276102 | 2018-03-22 18:50:16 | [diff] [blame] | 223 | const bluetooth_v2_shlib::Uuid& uuid) { |
| 224 | DCHECK(io_task_runner_->BelongsToCurrentThread()); |
| 225 | auto it = uuid_to_service_.find(uuid); |
| 226 | if (it == uuid_to_service_.end()) |
| 227 | return nullptr; |
| 228 | |
| 229 | return it->second; |
| 230 | } |
| 231 | |
Bailey Forrest | 801a41d | 2018-03-31 01:01:22 | [diff] [blame] | 232 | void RemoteDeviceImpl::SetConnected(bool connected) { |
Bailey Forrest | 1276102 | 2018-03-22 18:50:16 | [diff] [blame] | 233 | DCHECK(io_task_runner_->BelongsToCurrentThread()); |
Bailey Forrest | 78fa960 | 2018-06-12 23:10:57 | [diff] [blame^] | 234 | // We only set connected = true and call the callback after services are |
| 235 | // discovered. |
Bailey Forrest | 3de5924 | 2018-04-26 17:45:33 | [diff] [blame] | 236 | if (!connected) { |
| 237 | connected_ = false; |
Bailey Forrest | 78fa960 | 2018-06-12 23:10:57 | [diff] [blame^] | 238 | ConnectComplete(false); |
Bailey Forrest | 1276102 | 2018-03-22 18:50:16 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | if (disconnect_pending_) { |
| 242 | disconnect_pending_ = false; |
| 243 | if (disconnect_cb_) { |
| 244 | std::move(disconnect_cb_).Run(!connected); |
| 245 | } |
| 246 | } |
| 247 | |
Bailey Forrest | 1276102 | 2018-03-22 18:50:16 | [diff] [blame] | 248 | if (!connected && rssi_pending_) { |
| 249 | LOG(ERROR) << "Read remote RSSI failed: disconnected"; |
| 250 | if (rssi_cb_) { |
| 251 | std::move(rssi_cb_).Run(false, 0); |
| 252 | } |
| 253 | rssi_pending_ = false; |
| 254 | } |
| 255 | |
| 256 | if (!connected && mtu_pending_) { |
| 257 | LOG(ERROR) << "Set MTU failed: disconnected"; |
| 258 | if (mtu_cb_) { |
| 259 | std::move(mtu_cb_).Run(false); |
| 260 | } |
| 261 | |
| 262 | mtu_pending_ = false; |
| 263 | } |
| 264 | |
Bailey Forrest | 1276102 | 2018-03-22 18:50:16 | [diff] [blame] | 265 | for (const auto& characteristic : handle_to_characteristic_) { |
Bailey Forrest | 801a41d | 2018-03-31 01:01:22 | [diff] [blame] | 266 | auto* char_impl = |
| 267 | static_cast<RemoteCharacteristicImpl*>(characteristic.second.get()); |
| 268 | char_impl->OnConnectChanged(connected); |
Bailey Forrest | 1276102 | 2018-03-22 18:50:16 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | for (const auto& descriptor : handle_to_descriptor_) { |
Bailey Forrest | 801a41d | 2018-03-31 01:01:22 | [diff] [blame] | 272 | auto* desc_impl = |
| 273 | static_cast<RemoteDescriptorImpl*>(descriptor.second.get()); |
| 274 | desc_impl->OnConnectChanged(connected); |
Bailey Forrest | 1276102 | 2018-03-22 18:50:16 | [diff] [blame] | 275 | } |
Bailey Forrest | 3de5924 | 2018-04-26 17:45:33 | [diff] [blame] | 276 | |
| 277 | if (connected) { |
| 278 | if (!gatt_client_manager_) { |
| 279 | LOG(ERROR) << "Couldn't discover services: Destroyed"; |
| 280 | return; |
| 281 | } |
| 282 | |
| 283 | if (!gatt_client_manager_->gatt_client()->GetServices(addr_)) { |
| 284 | LOG(ERROR) << "Couldn't discover services, disconnecting"; |
| 285 | Disconnect({}); |
Bailey Forrest | 78fa960 | 2018-06-12 23:10:57 | [diff] [blame^] | 286 | ConnectComplete(false); |
Bailey Forrest | 3de5924 | 2018-04-26 17:45:33 | [diff] [blame] | 287 | } |
Bailey Forrest | fda3509 | 2018-05-16 00:30:44 | [diff] [blame] | 288 | } else { |
| 289 | uuid_to_service_.clear(); |
| 290 | handle_to_characteristic_.clear(); |
| 291 | handle_to_descriptor_.clear(); |
Bailey Forrest | 3de5924 | 2018-04-26 17:45:33 | [diff] [blame] | 292 | } |
| 293 | } |
| 294 | |
Bailey Forrest | 56e71abb | 2018-05-11 18:50:56 | [diff] [blame] | 295 | void RemoteDeviceImpl::SetServicesDiscovered(bool discovered) { |
Bailey Forrest | 3de5924 | 2018-04-26 17:45:33 | [diff] [blame] | 296 | DCHECK(io_task_runner_->BelongsToCurrentThread()); |
Bailey Forrest | 56e71abb | 2018-05-11 18:50:56 | [diff] [blame] | 297 | services_discovered_ = discovered; |
| 298 | if (!discovered) { |
| 299 | return; |
| 300 | } |
Bailey Forrest | 3de5924 | 2018-04-26 17:45:33 | [diff] [blame] | 301 | connected_ = true; |
Bailey Forrest | 78fa960 | 2018-06-12 23:10:57 | [diff] [blame^] | 302 | ConnectComplete(true); |
Bailey Forrest | 3de5924 | 2018-04-26 17:45:33 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | bool RemoteDeviceImpl::GetServicesDiscovered() { |
| 306 | DCHECK(io_task_runner_->BelongsToCurrentThread()); |
| 307 | return services_discovered_; |
Bailey Forrest | 1276102 | 2018-03-22 18:50:16 | [diff] [blame] | 308 | } |
| 309 | |
Bailey Forrest | 801a41d | 2018-03-31 01:01:22 | [diff] [blame] | 310 | void RemoteDeviceImpl::SetMtu(int mtu) { |
Bailey Forrest | 1276102 | 2018-03-22 18:50:16 | [diff] [blame] | 311 | DCHECK(io_task_runner_->BelongsToCurrentThread()); |
| 312 | mtu_pending_ = false; |
| 313 | mtu_ = mtu; |
| 314 | |
| 315 | if (mtu_cb_) { |
| 316 | std::move(mtu_cb_).Run(true); |
| 317 | } |
| 318 | } |
| 319 | |
Bailey Forrest | 801a41d | 2018-03-31 01:01:22 | [diff] [blame] | 320 | scoped_refptr<RemoteCharacteristic> RemoteDeviceImpl::CharacteristicFromHandle( |
Bailey Forrest | 1276102 | 2018-03-22 18:50:16 | [diff] [blame] | 321 | uint16_t handle) { |
| 322 | DCHECK(io_task_runner_->BelongsToCurrentThread()); |
| 323 | auto it = handle_to_characteristic_.find(handle); |
| 324 | if (it == handle_to_characteristic_.end()) |
| 325 | return nullptr; |
| 326 | |
| 327 | return it->second; |
| 328 | } |
| 329 | |
Bailey Forrest | 801a41d | 2018-03-31 01:01:22 | [diff] [blame] | 330 | scoped_refptr<RemoteDescriptor> RemoteDeviceImpl::DescriptorFromHandle( |
Bailey Forrest | 1276102 | 2018-03-22 18:50:16 | [diff] [blame] | 331 | uint16_t handle) { |
| 332 | DCHECK(io_task_runner_->BelongsToCurrentThread()); |
| 333 | auto it = handle_to_descriptor_.find(handle); |
| 334 | if (it == handle_to_descriptor_.end()) |
| 335 | return nullptr; |
| 336 | |
| 337 | return it->second; |
| 338 | } |
| 339 | |
Bailey Forrest | 801a41d | 2018-03-31 01:01:22 | [diff] [blame] | 340 | void RemoteDeviceImpl::OnGetServices( |
Bailey Forrest | 1276102 | 2018-03-22 18:50:16 | [diff] [blame] | 341 | const std::vector<bluetooth_v2_shlib::Gatt::Service>& services) { |
| 342 | DCHECK(io_task_runner_->BelongsToCurrentThread()); |
| 343 | uuid_to_service_.clear(); |
| 344 | handle_to_characteristic_.clear(); |
| 345 | handle_to_descriptor_.clear(); |
| 346 | OnServicesAdded(services); |
Bailey Forrest | 1276102 | 2018-03-22 18:50:16 | [diff] [blame] | 347 | } |
| 348 | |
Bailey Forrest | 801a41d | 2018-03-31 01:01:22 | [diff] [blame] | 349 | void RemoteDeviceImpl::OnServicesRemoved(uint16_t start_handle, |
| 350 | uint16_t end_handle) { |
Bailey Forrest | 1276102 | 2018-03-22 18:50:16 | [diff] [blame] | 351 | DCHECK(io_task_runner_->BelongsToCurrentThread()); |
| 352 | for (auto it = uuid_to_service_.begin(); it != uuid_to_service_.end();) { |
| 353 | if (it->second->handle() >= start_handle && |
| 354 | it->second->handle() <= end_handle) { |
| 355 | for (auto& characteristic : it->second->GetCharacteristics()) { |
| 356 | for (auto& descriptor : characteristic->GetDescriptors()) |
| 357 | handle_to_descriptor_.erase(descriptor->handle()); |
| 358 | |
| 359 | handle_to_characteristic_.erase(characteristic->handle()); |
| 360 | } |
| 361 | it = uuid_to_service_.erase(it); |
| 362 | } else { |
| 363 | ++it; |
| 364 | } |
| 365 | } |
Bailey Forrest | 1276102 | 2018-03-22 18:50:16 | [diff] [blame] | 366 | } |
| 367 | |
Bailey Forrest | 801a41d | 2018-03-31 01:01:22 | [diff] [blame] | 368 | void RemoteDeviceImpl::OnServicesAdded( |
Bailey Forrest | 1276102 | 2018-03-22 18:50:16 | [diff] [blame] | 369 | const std::vector<bluetooth_v2_shlib::Gatt::Service>& services) { |
| 370 | DCHECK(io_task_runner_->BelongsToCurrentThread()); |
| 371 | for (const auto& service : services) { |
Bailey Forrest | 801a41d | 2018-03-31 01:01:22 | [diff] [blame] | 372 | uuid_to_service_[service.uuid] = new RemoteServiceImpl( |
| 373 | this, gatt_client_manager_, service, io_task_runner_); |
Bailey Forrest | 1276102 | 2018-03-22 18:50:16 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | for (const auto& pair : uuid_to_service_) { |
| 377 | for (auto& characteristic : pair.second->GetCharacteristics()) { |
| 378 | handle_to_characteristic_.emplace(characteristic->handle(), |
| 379 | characteristic); |
| 380 | for (auto& descriptor : characteristic->GetDescriptors()) { |
| 381 | handle_to_descriptor_.emplace(descriptor->handle(), descriptor); |
| 382 | } |
| 383 | } |
| 384 | } |
Bailey Forrest | 1276102 | 2018-03-22 18:50:16 | [diff] [blame] | 385 | } |
| 386 | |
Bailey Forrest | 801a41d | 2018-03-31 01:01:22 | [diff] [blame] | 387 | void RemoteDeviceImpl::OnReadRemoteRssiComplete(bool status, int rssi) { |
Bailey Forrest | 1276102 | 2018-03-22 18:50:16 | [diff] [blame] | 388 | DCHECK(io_task_runner_->BelongsToCurrentThread()); |
| 389 | rssi_pending_ = false; |
| 390 | if (rssi_cb_) { |
| 391 | std::move(rssi_cb_).Run(true, rssi); |
| 392 | } |
| 393 | } |
| 394 | |
Bailey Forrest | 78fa960 | 2018-06-12 23:10:57 | [diff] [blame^] | 395 | void RemoteDeviceImpl::ConnectComplete(bool success) { |
| 396 | DCHECK(io_task_runner_->BelongsToCurrentThread()); |
| 397 | if (connect_pending_) { |
| 398 | connect_pending_ = false; |
| 399 | if (connect_cb_) { |
| 400 | std::move(connect_cb_).Run(success); |
| 401 | } |
| 402 | } |
| 403 | } |
| 404 | |
Bailey Forrest | 1276102 | 2018-03-22 18:50:16 | [diff] [blame] | 405 | } // namespace bluetooth |
| 406 | } // namespace chromecast |