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 "components/arc/bluetooth/arc_bluetooth_bridge.h" | 5 #include "components/arc/bluetooth/arc_bluetooth_bridge.h" |
6 | 6 |
7 #include <fcntl.h> | 7 #include <fcntl.h> |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 | 9 |
10 #include <iomanip> | 10 #include <iomanip> |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
64 ArcBluetoothBridge::ArcBluetoothBridge(ArcBridgeService* bridge_service) | 64 ArcBluetoothBridge::ArcBluetoothBridge(ArcBridgeService* bridge_service) |
65 : ArcService(bridge_service), binding_(this), weak_factory_(this) { | 65 : ArcService(bridge_service), binding_(this), weak_factory_(this) { |
66 if (BluetoothAdapterFactory::IsBluetoothAdapterAvailable()) { | 66 if (BluetoothAdapterFactory::IsBluetoothAdapterAvailable()) { |
67 VLOG(1) << "registering bluetooth adapter"; | 67 VLOG(1) << "registering bluetooth adapter"; |
68 BluetoothAdapterFactory::GetAdapter(base::Bind( | 68 BluetoothAdapterFactory::GetAdapter(base::Bind( |
69 &ArcBluetoothBridge::OnAdapterInitialized, weak_factory_.GetWeakPtr())); | 69 &ArcBluetoothBridge::OnAdapterInitialized, weak_factory_.GetWeakPtr())); |
70 } else { | 70 } else { |
71 VLOG(1) << "no bluetooth adapter available"; | 71 VLOG(1) << "no bluetooth adapter available"; |
72 } | 72 } |
73 | 73 |
74 arc_bridge_service()->AddObserver(this); | 74 arc_bridge_service()->bluetooth()->AddObserver(this); |
75 } | 75 } |
76 | 76 |
77 ArcBluetoothBridge::~ArcBluetoothBridge() { | 77 ArcBluetoothBridge::~ArcBluetoothBridge() { |
78 arc_bridge_service()->RemoveObserver(this); | 78 arc_bridge_service()->bluetooth()->RemoveObserver(this); |
79 | 79 |
80 if (bluetooth_adapter_) | 80 if (bluetooth_adapter_) |
81 bluetooth_adapter_->RemoveObserver(this); | 81 bluetooth_adapter_->RemoveObserver(this); |
82 } | 82 } |
83 | 83 |
84 void ArcBluetoothBridge::OnAdapterInitialized( | 84 void ArcBluetoothBridge::OnAdapterInitialized( |
85 scoped_refptr<BluetoothAdapter> adapter) { | 85 scoped_refptr<BluetoothAdapter> adapter) { |
86 // We can downcast here because we are always running on Chrome OS, and | 86 // We can downcast here because we are always running on Chrome OS, and |
87 // so our adapter uses BlueZ. | 87 // so our adapter uses BlueZ. |
88 bluetooth_adapter_ = | 88 bluetooth_adapter_ = |
89 static_cast<bluez::BluetoothAdapterBlueZ*>(adapter.get()); | 89 static_cast<bluez::BluetoothAdapterBlueZ*>(adapter.get()); |
90 bluetooth_adapter_->AddObserver(this); | 90 bluetooth_adapter_->AddObserver(this); |
91 } | 91 } |
92 | 92 |
93 void ArcBluetoothBridge::OnBluetoothInstanceReady() { | 93 void ArcBluetoothBridge::OnInstanceReady( |
94 mojom::BluetoothInstance* bluetooth_instance = | 94 mojom::BluetoothInstance* bluetooth_instance, |
95 arc_bridge_service()->bluetooth_instance(); | 95 uint32_t version) { |
96 if (!bluetooth_instance) { | 96 bluetooth_instance->Init(binding_.CreateInterfacePtrAndBind()); |
97 LOG(ERROR) << "OnBluetoothInstanceReady called, " | |
98 << "but no bluetooth instance found"; | |
99 return; | |
100 } | |
101 | |
102 arc_bridge_service()->bluetooth_instance()->Init( | |
103 binding_.CreateInterfacePtrAndBind()); | |
104 } | 97 } |
105 | 98 |
106 void ArcBluetoothBridge::AdapterPresentChanged(BluetoothAdapter* adapter, | 99 void ArcBluetoothBridge::AdapterPresentChanged(BluetoothAdapter* adapter, |
107 bool present) { | 100 bool present) { |
108 // If the adapter goes away, remove ourselves as an observer. | 101 // If the adapter goes away, remove ourselves as an observer. |
109 if (!present && adapter == bluetooth_adapter_) { | 102 if (!present && adapter == bluetooth_adapter_) { |
110 adapter->RemoveObserver(this); | 103 adapter->RemoveObserver(this); |
111 bluetooth_adapter_ = nullptr; | 104 bluetooth_adapter_ = nullptr; |
112 } | 105 } |
113 } | 106 } |
(...skipping 11 matching lines...) Expand all Loading... |
125 BluetoothDevice* device) { | 118 BluetoothDevice* device) { |
126 if (!HasBluetoothInstance()) | 119 if (!HasBluetoothInstance()) |
127 return; | 120 return; |
128 | 121 |
129 if (device->IsConnected()) | 122 if (device->IsConnected()) |
130 return; | 123 return; |
131 | 124 |
132 mojo::Array<mojom::BluetoothPropertyPtr> properties = | 125 mojo::Array<mojom::BluetoothPropertyPtr> properties = |
133 GetDeviceProperties(mojom::BluetoothPropertyType::ALL, device); | 126 GetDeviceProperties(mojom::BluetoothPropertyType::ALL, device); |
134 | 127 |
135 arc_bridge_service()->bluetooth_instance()->OnDeviceFound( | 128 arc_bridge_service()->bluetooth()->instance()->OnDeviceFound( |
136 std::move(properties)); | 129 std::move(properties)); |
137 | 130 |
138 if (!CheckBluetoothInstanceVersion(kMinBtleVersion)) | 131 if (!CheckBluetoothInstanceVersion(kMinBtleVersion)) |
139 return; | 132 return; |
140 | 133 |
141 mojom::BluetoothAddressPtr addr = | 134 mojom::BluetoothAddressPtr addr = |
142 mojom::BluetoothAddress::From(device->GetAddress()); | 135 mojom::BluetoothAddress::From(device->GetAddress()); |
143 int rssi = device->GetInquiryRSSI(); | 136 int rssi = device->GetInquiryRSSI(); |
144 mojo::Array<mojom::BluetoothAdvertisingDataPtr> adv_data = | 137 mojo::Array<mojom::BluetoothAdvertisingDataPtr> adv_data = |
145 GetAdvertisingData(device); | 138 GetAdvertisingData(device); |
146 arc_bridge_service()->bluetooth_instance()->OnLEDeviceFound( | 139 arc_bridge_service()->bluetooth()->instance()->OnLEDeviceFound( |
147 std::move(addr), rssi, std::move(adv_data)); | 140 std::move(addr), rssi, std::move(adv_data)); |
148 } | 141 } |
149 | 142 |
150 void ArcBluetoothBridge::DeviceChanged(BluetoothAdapter* adapter, | 143 void ArcBluetoothBridge::DeviceChanged(BluetoothAdapter* adapter, |
151 BluetoothDevice* device) { | 144 BluetoothDevice* device) { |
152 // TODO(smbarber): device properties changed; inform the container. | 145 // TODO(smbarber): device properties changed; inform the container. |
153 } | 146 } |
154 | 147 |
155 void ArcBluetoothBridge::DeviceAddressChanged(BluetoothAdapter* adapter, | 148 void ArcBluetoothBridge::DeviceAddressChanged(BluetoothAdapter* adapter, |
156 BluetoothDevice* device, | 149 BluetoothDevice* device, |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
206 BluetoothDevice* device) { | 199 BluetoothDevice* device) { |
207 if (!HasBluetoothInstance()) | 200 if (!HasBluetoothInstance()) |
208 return; | 201 return; |
209 | 202 |
210 if (!CheckBluetoothInstanceVersion(kMinBtleVersion)) | 203 if (!CheckBluetoothInstanceVersion(kMinBtleVersion)) |
211 return; | 204 return; |
212 | 205 |
213 mojom::BluetoothAddressPtr addr = | 206 mojom::BluetoothAddressPtr addr = |
214 mojom::BluetoothAddress::From(device->GetAddress()); | 207 mojom::BluetoothAddress::From(device->GetAddress()); |
215 | 208 |
216 arc_bridge_service()->bluetooth_instance()->OnSearchComplete( | 209 arc_bridge_service()->bluetooth()->instance()->OnSearchComplete( |
217 std::move(addr), mojom::BluetoothGattStatus::GATT_SUCCESS); | 210 std::move(addr), mojom::BluetoothGattStatus::GATT_SUCCESS); |
218 } | 211 } |
219 | 212 |
220 void ArcBluetoothBridge::GattDiscoveryCompleteForService( | 213 void ArcBluetoothBridge::GattDiscoveryCompleteForService( |
221 BluetoothAdapter* adapter, | 214 BluetoothAdapter* adapter, |
222 BluetoothRemoteGattService* service) { | 215 BluetoothRemoteGattService* service) { |
223 // Placeholder for GATT client functionality | 216 // Placeholder for GATT client functionality |
224 } | 217 } |
225 | 218 |
226 void ArcBluetoothBridge::GattServiceChanged( | 219 void ArcBluetoothBridge::GattServiceChanged( |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
271 mojom::BluetoothGattServiceID::New(); | 264 mojom::BluetoothGattServiceID::New(); |
272 service_id->is_primary = service->IsPrimary(); | 265 service_id->is_primary = service->IsPrimary(); |
273 service_id->id = mojom::BluetoothGattID::New(); | 266 service_id->id = mojom::BluetoothGattID::New(); |
274 service_id->id->inst_id = ConvertGattIdentifierToId(service->GetIdentifier()); | 267 service_id->id->inst_id = ConvertGattIdentifierToId(service->GetIdentifier()); |
275 service_id->id->uuid = mojom::BluetoothUUID::From(service->GetUUID()); | 268 service_id->id->uuid = mojom::BluetoothUUID::From(service->GetUUID()); |
276 | 269 |
277 mojom::BluetoothGattIDPtr char_id = mojom::BluetoothGattID::New(); | 270 mojom::BluetoothGattIDPtr char_id = mojom::BluetoothGattID::New(); |
278 char_id->inst_id = ConvertGattIdentifierToId(characteristic->GetIdentifier()); | 271 char_id->inst_id = ConvertGattIdentifierToId(characteristic->GetIdentifier()); |
279 char_id->uuid = mojom::BluetoothUUID::From(characteristic->GetUUID()); | 272 char_id->uuid = mojom::BluetoothUUID::From(characteristic->GetUUID()); |
280 | 273 |
281 arc_bridge_service()->bluetooth_instance()->OnGattNotify( | 274 arc_bridge_service()->bluetooth()->instance()->OnGattNotify( |
282 std::move(address), std::move(service_id), std::move(char_id), | 275 std::move(address), std::move(service_id), std::move(char_id), |
283 true /* is_notify */, mojo::Array<uint8_t>::From(value)); | 276 true /* is_notify */, mojo::Array<uint8_t>::From(value)); |
284 } | 277 } |
285 | 278 |
286 void ArcBluetoothBridge::GattDescriptorValueChanged( | 279 void ArcBluetoothBridge::GattDescriptorValueChanged( |
287 BluetoothAdapter* adapter, | 280 BluetoothAdapter* adapter, |
288 BluetoothRemoteGattDescriptor* descriptor, | 281 BluetoothRemoteGattDescriptor* descriptor, |
289 const std::vector<uint8_t>& value) { | 282 const std::vector<uint8_t>& value) { |
290 // Placeholder for GATT client functionality | 283 // Placeholder for GATT client functionality |
291 } | 284 } |
(...skipping 23 matching lines...) Expand all Loading... |
315 } | 308 } |
316 | 309 |
317 void ArcBluetoothBridge::GetAdapterProperty(mojom::BluetoothPropertyType type) { | 310 void ArcBluetoothBridge::GetAdapterProperty(mojom::BluetoothPropertyType type) { |
318 DCHECK(bluetooth_adapter_); | 311 DCHECK(bluetooth_adapter_); |
319 if (!HasBluetoothInstance()) | 312 if (!HasBluetoothInstance()) |
320 return; | 313 return; |
321 | 314 |
322 mojo::Array<mojom::BluetoothPropertyPtr> properties = | 315 mojo::Array<mojom::BluetoothPropertyPtr> properties = |
323 GetAdapterProperties(type); | 316 GetAdapterProperties(type); |
324 | 317 |
325 arc_bridge_service()->bluetooth_instance()->OnAdapterProperties( | 318 arc_bridge_service()->bluetooth()->instance()->OnAdapterProperties( |
326 mojom::BluetoothStatus::SUCCESS, std::move(properties)); | 319 mojom::BluetoothStatus::SUCCESS, std::move(properties)); |
327 } | 320 } |
328 | 321 |
329 void ArcBluetoothBridge::SetAdapterProperty( | 322 void ArcBluetoothBridge::SetAdapterProperty( |
330 mojom::BluetoothPropertyPtr property) { | 323 mojom::BluetoothPropertyPtr property) { |
331 DCHECK(bluetooth_adapter_); | 324 DCHECK(bluetooth_adapter_); |
332 if (!HasBluetoothInstance()) | 325 if (!HasBluetoothInstance()) |
333 return; | 326 return; |
334 | 327 |
335 // TODO(smbarber): Implement SetAdapterProperty | 328 // TODO(smbarber): Implement SetAdapterProperty |
336 arc_bridge_service()->bluetooth_instance()->OnAdapterProperties( | 329 arc_bridge_service()->bluetooth()->instance()->OnAdapterProperties( |
337 mojom::BluetoothStatus::FAIL, | 330 mojom::BluetoothStatus::FAIL, |
338 mojo::Array<mojom::BluetoothPropertyPtr>::New(0)); | 331 mojo::Array<mojom::BluetoothPropertyPtr>::New(0)); |
339 } | 332 } |
340 | 333 |
341 void ArcBluetoothBridge::GetRemoteDeviceProperty( | 334 void ArcBluetoothBridge::GetRemoteDeviceProperty( |
342 mojom::BluetoothAddressPtr remote_addr, | 335 mojom::BluetoothAddressPtr remote_addr, |
343 mojom::BluetoothPropertyType type) { | 336 mojom::BluetoothPropertyType type) { |
344 DCHECK(bluetooth_adapter_); | 337 DCHECK(bluetooth_adapter_); |
345 if (!HasBluetoothInstance()) | 338 if (!HasBluetoothInstance()) |
346 return; | 339 return; |
347 | 340 |
348 std::string addr_str = remote_addr->To<std::string>(); | 341 std::string addr_str = remote_addr->To<std::string>(); |
349 BluetoothDevice* device = bluetooth_adapter_->GetDevice(addr_str); | 342 BluetoothDevice* device = bluetooth_adapter_->GetDevice(addr_str); |
350 | 343 |
351 mojo::Array<mojom::BluetoothPropertyPtr> properties = | 344 mojo::Array<mojom::BluetoothPropertyPtr> properties = |
352 GetDeviceProperties(type, device); | 345 GetDeviceProperties(type, device); |
353 mojom::BluetoothStatus status = mojom::BluetoothStatus::SUCCESS; | 346 mojom::BluetoothStatus status = mojom::BluetoothStatus::SUCCESS; |
354 | 347 |
355 if (!device) { | 348 if (!device) { |
356 VLOG(1) << __func__ << ": device " << addr_str << " not available"; | 349 VLOG(1) << __func__ << ": device " << addr_str << " not available"; |
357 status = mojom::BluetoothStatus::FAIL; | 350 status = mojom::BluetoothStatus::FAIL; |
358 } | 351 } |
359 | 352 |
360 arc_bridge_service()->bluetooth_instance()->OnRemoteDeviceProperties( | 353 arc_bridge_service()->bluetooth()->instance()->OnRemoteDeviceProperties( |
361 status, std::move(remote_addr), std::move(properties)); | 354 status, std::move(remote_addr), std::move(properties)); |
362 } | 355 } |
363 | 356 |
364 void ArcBluetoothBridge::SetRemoteDeviceProperty( | 357 void ArcBluetoothBridge::SetRemoteDeviceProperty( |
365 mojom::BluetoothAddressPtr remote_addr, | 358 mojom::BluetoothAddressPtr remote_addr, |
366 mojom::BluetoothPropertyPtr property) { | 359 mojom::BluetoothPropertyPtr property) { |
367 DCHECK(bluetooth_adapter_); | 360 DCHECK(bluetooth_adapter_); |
368 if (!HasBluetoothInstance()) | 361 if (!HasBluetoothInstance()) |
369 return; | 362 return; |
370 | 363 |
371 // TODO(smbarber): Implement SetRemoteDeviceProperty | 364 // TODO(smbarber): Implement SetRemoteDeviceProperty |
372 arc_bridge_service()->bluetooth_instance()->OnRemoteDeviceProperties( | 365 arc_bridge_service()->bluetooth()->instance()->OnRemoteDeviceProperties( |
373 mojom::BluetoothStatus::FAIL, std::move(remote_addr), | 366 mojom::BluetoothStatus::FAIL, std::move(remote_addr), |
374 mojo::Array<mojom::BluetoothPropertyPtr>::New(0)); | 367 mojo::Array<mojom::BluetoothPropertyPtr>::New(0)); |
375 } | 368 } |
376 | 369 |
377 void ArcBluetoothBridge::GetRemoteServiceRecord( | 370 void ArcBluetoothBridge::GetRemoteServiceRecord( |
378 mojom::BluetoothAddressPtr remote_addr, | 371 mojom::BluetoothAddressPtr remote_addr, |
379 mojom::BluetoothUUIDPtr uuid) { | 372 mojom::BluetoothUUIDPtr uuid) { |
380 // TODO(smbarber): Implement GetRemoteServiceRecord | 373 // TODO(smbarber): Implement GetRemoteServiceRecord |
381 } | 374 } |
382 | 375 |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
432 : mojom::BluetoothAdapterState::OFF); | 425 : mojom::BluetoothAdapterState::OFF); |
433 } | 426 } |
434 | 427 |
435 void ArcBluetoothBridge::OnDiscoveryStarted( | 428 void ArcBluetoothBridge::OnDiscoveryStarted( |
436 std::unique_ptr<BluetoothDiscoverySession> session) { | 429 std::unique_ptr<BluetoothDiscoverySession> session) { |
437 if (!HasBluetoothInstance()) | 430 if (!HasBluetoothInstance()) |
438 return; | 431 return; |
439 | 432 |
440 discovery_session_ = std::move(session); | 433 discovery_session_ = std::move(session); |
441 | 434 |
442 arc_bridge_service()->bluetooth_instance()->OnDiscoveryStateChanged( | 435 arc_bridge_service()->bluetooth()->instance()->OnDiscoveryStateChanged( |
443 mojom::BluetoothDiscoveryState::STARTED); | 436 mojom::BluetoothDiscoveryState::STARTED); |
444 | 437 |
445 SendCachedDevicesFound(); | 438 SendCachedDevicesFound(); |
446 } | 439 } |
447 | 440 |
448 void ArcBluetoothBridge::OnDiscoveryStopped() { | 441 void ArcBluetoothBridge::OnDiscoveryStopped() { |
449 if (!HasBluetoothInstance()) | 442 if (!HasBluetoothInstance()) |
450 return; | 443 return; |
451 | 444 |
452 discovery_session_.reset(); | 445 discovery_session_.reset(); |
453 | 446 |
454 arc_bridge_service()->bluetooth_instance()->OnDiscoveryStateChanged( | 447 arc_bridge_service()->bluetooth()->instance()->OnDiscoveryStateChanged( |
455 mojom::BluetoothDiscoveryState::STOPPED); | 448 mojom::BluetoothDiscoveryState::STOPPED); |
456 } | 449 } |
457 | 450 |
458 void ArcBluetoothBridge::CreateBond(mojom::BluetoothAddressPtr addr, | 451 void ArcBluetoothBridge::CreateBond(mojom::BluetoothAddressPtr addr, |
459 int32_t transport) { | 452 int32_t transport) { |
460 std::string addr_str = addr->To<std::string>(); | 453 std::string addr_str = addr->To<std::string>(); |
461 BluetoothDevice* device = bluetooth_adapter_->GetDevice(addr_str); | 454 BluetoothDevice* device = bluetooth_adapter_->GetDevice(addr_str); |
462 if (!device || !device->IsPairable()) { | 455 if (!device || !device->IsPairable()) { |
463 VLOG(1) << __func__ << ": device " << addr_str | 456 VLOG(1) << __func__ << ": device " << addr_str |
464 << " is no longer valid or pairable"; | 457 << " is no longer valid or pairable"; |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
558 mojom::BluetoothAddressPtr addr, | 551 mojom::BluetoothAddressPtr addr, |
559 bool connected) const { | 552 bool connected) const { |
560 if (!HasBluetoothInstance()) | 553 if (!HasBluetoothInstance()) |
561 return; | 554 return; |
562 | 555 |
563 if (!CheckBluetoothInstanceVersion(kMinBtleVersion)) | 556 if (!CheckBluetoothInstanceVersion(kMinBtleVersion)) |
564 return; | 557 return; |
565 | 558 |
566 DCHECK(addr); | 559 DCHECK(addr); |
567 | 560 |
568 arc_bridge_service()->bluetooth_instance()->OnLEConnectionStateChange( | 561 arc_bridge_service()->bluetooth()->instance()->OnLEConnectionStateChange( |
569 std::move(addr), connected); | 562 std::move(addr), connected); |
570 } | 563 } |
571 | 564 |
572 void ArcBluetoothBridge::OnGattConnected( | 565 void ArcBluetoothBridge::OnGattConnected( |
573 mojom::BluetoothAddressPtr addr, | 566 mojom::BluetoothAddressPtr addr, |
574 std::unique_ptr<BluetoothGattConnection> connection) const { | 567 std::unique_ptr<BluetoothGattConnection> connection) const { |
575 OnGattConnectStateChanged(std::move(addr), true); | 568 OnGattConnectStateChanged(std::move(addr), true); |
576 } | 569 } |
577 | 570 |
578 void ArcBluetoothBridge::OnGattConnectError( | 571 void ArcBluetoothBridge::OnGattConnectError( |
(...skipping 10 matching lines...) Expand all Loading... |
589 void ArcBluetoothBridge::ConnectLEDevice( | 582 void ArcBluetoothBridge::ConnectLEDevice( |
590 mojom::BluetoothAddressPtr remote_addr) { | 583 mojom::BluetoothAddressPtr remote_addr) { |
591 if (!HasBluetoothInstance()) | 584 if (!HasBluetoothInstance()) |
592 return; | 585 return; |
593 | 586 |
594 BluetoothDevice* device = | 587 BluetoothDevice* device = |
595 bluetooth_adapter_->GetDevice(remote_addr->To<std::string>()); | 588 bluetooth_adapter_->GetDevice(remote_addr->To<std::string>()); |
596 DCHECK(device); | 589 DCHECK(device); |
597 | 590 |
598 if (device->IsConnected()) { | 591 if (device->IsConnected()) { |
599 arc_bridge_service()->bluetooth_instance()->OnLEConnectionStateChange( | 592 arc_bridge_service()->bluetooth()->instance()->OnLEConnectionStateChange( |
600 std::move(remote_addr), true); | 593 std::move(remote_addr), true); |
601 return; | 594 return; |
602 } | 595 } |
603 | 596 |
604 // Also pass disconnect callback in error case | 597 // Also pass disconnect callback in error case |
605 // since it would be disconnected anyway. | 598 // since it would be disconnected anyway. |
606 mojom::BluetoothAddressPtr remote_addr_clone = remote_addr.Clone(); | 599 mojom::BluetoothAddressPtr remote_addr_clone = remote_addr.Clone(); |
607 device->CreateGattConnection( | 600 device->CreateGattConnection( |
608 base::Bind(&ArcBluetoothBridge::OnGattConnected, | 601 base::Bind(&ArcBluetoothBridge::OnGattConnected, |
609 weak_factory_.GetWeakPtr(), base::Passed(&remote_addr)), | 602 weak_factory_.GetWeakPtr(), base::Passed(&remote_addr)), |
610 base::Bind(&ArcBluetoothBridge::OnGattConnectError, | 603 base::Bind(&ArcBluetoothBridge::OnGattConnectError, |
611 weak_factory_.GetWeakPtr(), base::Passed(&remote_addr_clone))); | 604 weak_factory_.GetWeakPtr(), base::Passed(&remote_addr_clone))); |
612 } | 605 } |
613 | 606 |
614 void ArcBluetoothBridge::DisconnectLEDevice( | 607 void ArcBluetoothBridge::DisconnectLEDevice( |
615 mojom::BluetoothAddressPtr remote_addr) { | 608 mojom::BluetoothAddressPtr remote_addr) { |
616 if (!HasBluetoothInstance()) | 609 if (!HasBluetoothInstance()) |
617 return; | 610 return; |
618 | 611 |
619 BluetoothDevice* device = | 612 BluetoothDevice* device = |
620 bluetooth_adapter_->GetDevice(remote_addr->To<std::string>()); | 613 bluetooth_adapter_->GetDevice(remote_addr->To<std::string>()); |
621 DCHECK(device); | 614 DCHECK(device); |
622 | 615 |
623 if (!device->IsConnected()) { | 616 if (!device->IsConnected()) { |
624 arc_bridge_service()->bluetooth_instance()->OnLEConnectionStateChange( | 617 arc_bridge_service()->bluetooth()->instance()->OnLEConnectionStateChange( |
625 std::move(remote_addr), false); | 618 std::move(remote_addr), false); |
626 return; | 619 return; |
627 } | 620 } |
628 | 621 |
629 mojom::BluetoothAddressPtr remote_addr_clone = remote_addr.Clone(); | 622 mojom::BluetoothAddressPtr remote_addr_clone = remote_addr.Clone(); |
630 device->Disconnect( | 623 device->Disconnect( |
631 base::Bind(&ArcBluetoothBridge::OnGattDisconnected, | 624 base::Bind(&ArcBluetoothBridge::OnGattDisconnected, |
632 weak_factory_.GetWeakPtr(), base::Passed(&remote_addr)), | 625 weak_factory_.GetWeakPtr(), base::Passed(&remote_addr)), |
633 base::Bind(&ArcBluetoothBridge::OnGattDisconnected, | 626 base::Bind(&ArcBluetoothBridge::OnGattDisconnected, |
634 weak_factory_.GetWeakPtr(), base::Passed(&remote_addr_clone))); | 627 weak_factory_.GetWeakPtr(), base::Passed(&remote_addr_clone))); |
635 } | 628 } |
636 | 629 |
637 void ArcBluetoothBridge::SearchService(mojom::BluetoothAddressPtr remote_addr) { | 630 void ArcBluetoothBridge::SearchService(mojom::BluetoothAddressPtr remote_addr) { |
638 if (!HasBluetoothInstance()) | 631 if (!HasBluetoothInstance()) |
639 return; | 632 return; |
640 | 633 |
641 BluetoothDevice* device = | 634 BluetoothDevice* device = |
642 bluetooth_adapter_->GetDevice(remote_addr->To<std::string>()); | 635 bluetooth_adapter_->GetDevice(remote_addr->To<std::string>()); |
643 DCHECK(device); | 636 DCHECK(device); |
644 | 637 |
645 // Call the callback if discovery is completed | 638 // Call the callback if discovery is completed |
646 if (device->IsGattServicesDiscoveryComplete()) { | 639 if (device->IsGattServicesDiscoveryComplete()) { |
647 arc_bridge_service()->bluetooth_instance()->OnSearchComplete( | 640 arc_bridge_service()->bluetooth()->instance()->OnSearchComplete( |
648 std::move(remote_addr), mojom::BluetoothGattStatus::GATT_SUCCESS); | 641 std::move(remote_addr), mojom::BluetoothGattStatus::GATT_SUCCESS); |
649 return; | 642 return; |
650 } | 643 } |
651 | 644 |
652 // Discard result. Will call the callback when discovery is completed. | 645 // Discard result. Will call the callback when discovery is completed. |
653 device->GetGattServices(); | 646 device->GetGattServices(); |
654 } | 647 } |
655 | 648 |
656 void ArcBluetoothBridge::OnStartLEListenDone( | 649 void ArcBluetoothBridge::OnStartLEListenDone( |
657 const StartLEListenCallback& callback, | 650 const StartLEListenCallback& callback, |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
763 db.push_back(std::move(characteristic_element)); | 756 db.push_back(std::move(characteristic_element)); |
764 | 757 |
765 for (auto descriptor : characteristic->GetDescriptors()) { | 758 for (auto descriptor : characteristic->GetDescriptors()) { |
766 db.push_back(CreateGattDBElement<device::BluetoothRemoteGattDescriptor>( | 759 db.push_back(CreateGattDBElement<device::BluetoothRemoteGattDescriptor>( |
767 mojom::BluetoothGattDBAttributeType::BTGATT_DB_DESCRIPTOR, | 760 mojom::BluetoothGattDBAttributeType::BTGATT_DB_DESCRIPTOR, |
768 descriptor)); | 761 descriptor)); |
769 } | 762 } |
770 } | 763 } |
771 } | 764 } |
772 | 765 |
773 arc_bridge_service()->bluetooth_instance()->OnGetGattDB( | 766 arc_bridge_service()->bluetooth()->instance()->OnGetGattDB( |
774 std::move(remote_addr), std::move(db)); | 767 std::move(remote_addr), std::move(db)); |
775 } | 768 } |
776 | 769 |
777 // Find Gatt Service/Characteristic/Descriptor from std::vector from UUID. | 770 // Find Gatt Service/Characteristic/Descriptor from std::vector from UUID. |
778 template <class T> | 771 template <class T> |
779 T* ArcBluetoothBridge::FindGattObjectFromUuid( | 772 T* ArcBluetoothBridge::FindGattObjectFromUuid( |
780 const std::vector<T*> gatt_objs, | 773 const std::vector<T*> gatt_objs, |
781 const device::BluetoothUUID uuid) const { | 774 const device::BluetoothUUID uuid) const { |
782 auto it = std::find_if(gatt_objs.begin(), gatt_objs.end(), | 775 auto it = std::find_if(gatt_objs.begin(), gatt_objs.end(), |
783 [&](T* obj) { return obj->GetUUID() == uuid; }); | 776 [&](T* obj) { return obj->GetUUID() == uuid; }); |
(...skipping 241 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1025 } | 1018 } |
1026 | 1019 |
1027 void ArcBluetoothBridge::OnDiscoveryError() { | 1020 void ArcBluetoothBridge::OnDiscoveryError() { |
1028 LOG(WARNING) << "failed to change discovery state"; | 1021 LOG(WARNING) << "failed to change discovery state"; |
1029 } | 1022 } |
1030 | 1023 |
1031 void ArcBluetoothBridge::OnPairing(mojom::BluetoothAddressPtr addr) const { | 1024 void ArcBluetoothBridge::OnPairing(mojom::BluetoothAddressPtr addr) const { |
1032 if (!HasBluetoothInstance()) | 1025 if (!HasBluetoothInstance()) |
1033 return; | 1026 return; |
1034 | 1027 |
1035 arc_bridge_service()->bluetooth_instance()->OnBondStateChanged( | 1028 arc_bridge_service()->bluetooth()->instance()->OnBondStateChanged( |
1036 mojom::BluetoothStatus::SUCCESS, std::move(addr), | 1029 mojom::BluetoothStatus::SUCCESS, std::move(addr), |
1037 mojom::BluetoothBondState::BONDING); | 1030 mojom::BluetoothBondState::BONDING); |
1038 } | 1031 } |
1039 | 1032 |
1040 void ArcBluetoothBridge::OnPairedDone(mojom::BluetoothAddressPtr addr) const { | 1033 void ArcBluetoothBridge::OnPairedDone(mojom::BluetoothAddressPtr addr) const { |
1041 if (!HasBluetoothInstance()) | 1034 if (!HasBluetoothInstance()) |
1042 return; | 1035 return; |
1043 | 1036 |
1044 arc_bridge_service()->bluetooth_instance()->OnBondStateChanged( | 1037 arc_bridge_service()->bluetooth()->instance()->OnBondStateChanged( |
1045 mojom::BluetoothStatus::SUCCESS, std::move(addr), | 1038 mojom::BluetoothStatus::SUCCESS, std::move(addr), |
1046 mojom::BluetoothBondState::BONDED); | 1039 mojom::BluetoothBondState::BONDED); |
1047 } | 1040 } |
1048 | 1041 |
1049 void ArcBluetoothBridge::OnPairedError( | 1042 void ArcBluetoothBridge::OnPairedError( |
1050 mojom::BluetoothAddressPtr addr, | 1043 mojom::BluetoothAddressPtr addr, |
1051 BluetoothDevice::ConnectErrorCode error_code) const { | 1044 BluetoothDevice::ConnectErrorCode error_code) const { |
1052 if (!HasBluetoothInstance()) | 1045 if (!HasBluetoothInstance()) |
1053 return; | 1046 return; |
1054 | 1047 |
1055 arc_bridge_service()->bluetooth_instance()->OnBondStateChanged( | 1048 arc_bridge_service()->bluetooth()->instance()->OnBondStateChanged( |
1056 mojom::BluetoothStatus::FAIL, std::move(addr), | 1049 mojom::BluetoothStatus::FAIL, std::move(addr), |
1057 mojom::BluetoothBondState::NONE); | 1050 mojom::BluetoothBondState::NONE); |
1058 } | 1051 } |
1059 | 1052 |
1060 void ArcBluetoothBridge::OnForgetDone(mojom::BluetoothAddressPtr addr) const { | 1053 void ArcBluetoothBridge::OnForgetDone(mojom::BluetoothAddressPtr addr) const { |
1061 if (!HasBluetoothInstance()) | 1054 if (!HasBluetoothInstance()) |
1062 return; | 1055 return; |
1063 | 1056 |
1064 arc_bridge_service()->bluetooth_instance()->OnBondStateChanged( | 1057 arc_bridge_service()->bluetooth()->instance()->OnBondStateChanged( |
1065 mojom::BluetoothStatus::SUCCESS, std::move(addr), | 1058 mojom::BluetoothStatus::SUCCESS, std::move(addr), |
1066 mojom::BluetoothBondState::NONE); | 1059 mojom::BluetoothBondState::NONE); |
1067 } | 1060 } |
1068 | 1061 |
1069 void ArcBluetoothBridge::OnForgetError(mojom::BluetoothAddressPtr addr) const { | 1062 void ArcBluetoothBridge::OnForgetError(mojom::BluetoothAddressPtr addr) const { |
1070 if (!HasBluetoothInstance()) | 1063 if (!HasBluetoothInstance()) |
1071 return; | 1064 return; |
1072 | 1065 |
1073 BluetoothDevice* device = | 1066 BluetoothDevice* device = |
1074 bluetooth_adapter_->GetDevice(addr->To<std::string>()); | 1067 bluetooth_adapter_->GetDevice(addr->To<std::string>()); |
1075 mojom::BluetoothBondState bond_state = mojom::BluetoothBondState::NONE; | 1068 mojom::BluetoothBondState bond_state = mojom::BluetoothBondState::NONE; |
1076 if (device && device->IsPaired()) { | 1069 if (device && device->IsPaired()) { |
1077 bond_state = mojom::BluetoothBondState::BONDED; | 1070 bond_state = mojom::BluetoothBondState::BONDED; |
1078 } | 1071 } |
1079 arc_bridge_service()->bluetooth_instance()->OnBondStateChanged( | 1072 arc_bridge_service()->bluetooth()->instance()->OnBondStateChanged( |
1080 mojom::BluetoothStatus::FAIL, std::move(addr), bond_state); | 1073 mojom::BluetoothStatus::FAIL, std::move(addr), bond_state); |
1081 } | 1074 } |
1082 | 1075 |
1083 mojo::Array<mojom::BluetoothPropertyPtr> | 1076 mojo::Array<mojom::BluetoothPropertyPtr> |
1084 ArcBluetoothBridge::GetDeviceProperties(mojom::BluetoothPropertyType type, | 1077 ArcBluetoothBridge::GetDeviceProperties(mojom::BluetoothPropertyType type, |
1085 BluetoothDevice* device) const { | 1078 BluetoothDevice* device) const { |
1086 mojo::Array<mojom::BluetoothPropertyPtr> properties; | 1079 mojo::Array<mojom::BluetoothPropertyPtr> properties; |
1087 | 1080 |
1088 if (!device) { | 1081 if (!device) { |
1089 return properties; | 1082 return properties; |
(...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1283 return; | 1276 return; |
1284 | 1277 |
1285 BluetoothAdapter::DeviceList devices = bluetooth_adapter_->GetDevices(); | 1278 BluetoothAdapter::DeviceList devices = bluetooth_adapter_->GetDevices(); |
1286 for (auto device : devices) { | 1279 for (auto device : devices) { |
1287 if (device->IsPaired()) | 1280 if (device->IsPaired()) |
1288 continue; | 1281 continue; |
1289 | 1282 |
1290 mojo::Array<mojom::BluetoothPropertyPtr> properties = | 1283 mojo::Array<mojom::BluetoothPropertyPtr> properties = |
1291 GetDeviceProperties(mojom::BluetoothPropertyType::ALL, device); | 1284 GetDeviceProperties(mojom::BluetoothPropertyType::ALL, device); |
1292 | 1285 |
1293 arc_bridge_service()->bluetooth_instance()->OnDeviceFound( | 1286 arc_bridge_service()->bluetooth()->instance()->OnDeviceFound( |
1294 std::move(properties)); | 1287 std::move(properties)); |
1295 | 1288 |
1296 if (arc_bridge_service()->bluetooth_version() >= kMinBtleVersion) { | 1289 if (arc_bridge_service()->bluetooth()->version() >= kMinBtleVersion) { |
1297 mojom::BluetoothAddressPtr addr = | 1290 mojom::BluetoothAddressPtr addr = |
1298 mojom::BluetoothAddress::From(device->GetAddress()); | 1291 mojom::BluetoothAddress::From(device->GetAddress()); |
1299 int rssi = device->GetInquiryRSSI(); | 1292 int rssi = device->GetInquiryRSSI(); |
1300 mojo::Array<mojom::BluetoothAdvertisingDataPtr> adv_data = | 1293 mojo::Array<mojom::BluetoothAdvertisingDataPtr> adv_data = |
1301 GetAdvertisingData(device); | 1294 GetAdvertisingData(device); |
1302 arc_bridge_service()->bluetooth_instance()->OnLEDeviceFound( | 1295 arc_bridge_service()->bluetooth()->instance()->OnLEDeviceFound( |
1303 std::move(addr), rssi, std::move(adv_data)); | 1296 std::move(addr), rssi, std::move(adv_data)); |
1304 } | 1297 } |
1305 } | 1298 } |
1306 } | 1299 } |
1307 | 1300 |
1308 bool ArcBluetoothBridge::HasBluetoothInstance() const { | 1301 bool ArcBluetoothBridge::HasBluetoothInstance() const { |
1309 if (!arc_bridge_service()->bluetooth_instance()) { | 1302 if (!arc_bridge_service()->bluetooth()->instance()) { |
1310 LOG(WARNING) << "no Bluetooth instance available"; | 1303 LOG(WARNING) << "no Bluetooth instance available"; |
1311 return false; | 1304 return false; |
1312 } | 1305 } |
1313 | 1306 |
1314 return true; | 1307 return true; |
1315 } | 1308 } |
1316 | 1309 |
1317 void ArcBluetoothBridge::SendCachedPairedDevices() const { | 1310 void ArcBluetoothBridge::SendCachedPairedDevices() const { |
1318 DCHECK(bluetooth_adapter_); | 1311 DCHECK(bluetooth_adapter_); |
1319 if (!HasBluetoothInstance()) | 1312 if (!HasBluetoothInstance()) |
1320 return; | 1313 return; |
1321 | 1314 |
1322 BluetoothAdapter::DeviceList devices = bluetooth_adapter_->GetDevices(); | 1315 BluetoothAdapter::DeviceList devices = bluetooth_adapter_->GetDevices(); |
1323 for (auto device : devices) { | 1316 for (auto device : devices) { |
1324 if (!device->IsPaired()) | 1317 if (!device->IsPaired()) |
1325 continue; | 1318 continue; |
1326 | 1319 |
1327 mojo::Array<mojom::BluetoothPropertyPtr> properties = | 1320 mojo::Array<mojom::BluetoothPropertyPtr> properties = |
1328 GetDeviceProperties(mojom::BluetoothPropertyType::ALL, device); | 1321 GetDeviceProperties(mojom::BluetoothPropertyType::ALL, device); |
1329 | 1322 |
1330 arc_bridge_service()->bluetooth_instance()->OnDeviceFound( | 1323 arc_bridge_service()->bluetooth()->instance()->OnDeviceFound( |
1331 std::move(properties)); | 1324 std::move(properties)); |
1332 | 1325 |
1333 mojom::BluetoothAddressPtr addr = | 1326 mojom::BluetoothAddressPtr addr = |
1334 mojom::BluetoothAddress::From(device->GetAddress()); | 1327 mojom::BluetoothAddress::From(device->GetAddress()); |
1335 | 1328 |
1336 if (arc_bridge_service()->bluetooth_version() >= kMinBtleVersion) { | 1329 if (arc_bridge_service()->bluetooth()->version() >= kMinBtleVersion) { |
1337 int rssi = device->GetInquiryRSSI(); | 1330 int rssi = device->GetInquiryRSSI(); |
1338 mojo::Array<mojom::BluetoothAdvertisingDataPtr> adv_data = | 1331 mojo::Array<mojom::BluetoothAdvertisingDataPtr> adv_data = |
1339 GetAdvertisingData(device); | 1332 GetAdvertisingData(device); |
1340 arc_bridge_service()->bluetooth_instance()->OnLEDeviceFound( | 1333 arc_bridge_service()->bluetooth()->instance()->OnLEDeviceFound( |
1341 addr->Clone(), rssi, std::move(adv_data)); | 1334 addr->Clone(), rssi, std::move(adv_data)); |
1342 } | 1335 } |
1343 | 1336 |
1344 // OnBondStateChanged must be called with mojom::BluetoothBondState::BONDING | 1337 // OnBondStateChanged must be called with mojom::BluetoothBondState::BONDING |
1345 // to | 1338 // to |
1346 // make sure the bond state machine on Android is ready to take the | 1339 // make sure the bond state machine on Android is ready to take the |
1347 // pair-done event. Otherwise the pair-done event will be dropped as an | 1340 // pair-done event. Otherwise the pair-done event will be dropped as an |
1348 // invalid change of paired status. | 1341 // invalid change of paired status. |
1349 OnPairing(addr->Clone()); | 1342 OnPairing(addr->Clone()); |
1350 OnPairedDone(std::move(addr)); | 1343 OnPairedDone(std::move(addr)); |
1351 } | 1344 } |
1352 } | 1345 } |
1353 | 1346 |
1354 bool ArcBluetoothBridge::CheckBluetoothInstanceVersion( | 1347 bool ArcBluetoothBridge::CheckBluetoothInstanceVersion( |
1355 int32_t version_need) const { | 1348 uint32_t version_need) const { |
1356 int32_t version = arc_bridge_service()->bluetooth_version(); | 1349 uint32_t version = arc_bridge_service()->bluetooth()->version(); |
1357 if (version >= version_need) | 1350 if (version >= version_need) |
1358 return true; | 1351 return true; |
1359 LOG(WARNING) << "Bluetooth instance is too old (version " << version | 1352 LOG(WARNING) << "Bluetooth instance is too old (version " << version |
1360 << ") need version " << version_need; | 1353 << ") need version " << version_need; |
1361 return false; | 1354 return false; |
1362 } | 1355 } |
1363 | 1356 |
1364 } // namespace arc | 1357 } // namespace arc |
OLD | NEW |