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 #ifndef COMPONENTS_ARC_BLUETOOTH_ARC_BLUETOOTH_BRIDGE_H_ | 5 #ifndef COMPONENTS_ARC_BLUETOOTH_ARC_BLUETOOTH_BRIDGE_H_ |
6 #define COMPONENTS_ARC_BLUETOOTH_ARC_BLUETOOTH_BRIDGE_H_ | 6 #define COMPONENTS_ARC_BLUETOOTH_ARC_BLUETOOTH_BRIDGE_H_ |
7 | 7 |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <memory> | 10 #include <memory> |
11 #include <string> | 11 #include <string> |
12 #include <unordered_map> | 12 #include <unordered_map> |
13 #include <unordered_set> | 13 #include <unordered_set> |
14 #include <vector> | 14 #include <vector> |
15 | 15 |
16 #include "base/callback.h" | 16 #include "base/callback.h" |
17 #include "base/timer/timer.h" | 17 #include "base/timer/timer.h" |
18 #include "components/arc/arc_bridge_service.h" | 18 #include "components/arc/arc_bridge_service.h" |
19 #include "components/arc/arc_service.h" | 19 #include "components/arc/arc_service.h" |
20 #include "components/arc/common/bluetooth.mojom.h" | 20 #include "components/arc/common/bluetooth.mojom.h" |
21 #include "components/arc/instance_holder.h" | 21 #include "components/arc/instance_holder.h" |
22 #include "device/bluetooth/bluetooth_adapter.h" | 22 #include "device/bluetooth/bluetooth_adapter.h" |
23 #include "device/bluetooth/bluetooth_adapter_factory.h" | 23 #include "device/bluetooth/bluetooth_adapter_factory.h" |
| 24 #include "device/bluetooth/bluetooth_advertisement.h" |
24 #include "device/bluetooth/bluetooth_device.h" | 25 #include "device/bluetooth/bluetooth_device.h" |
25 #include "device/bluetooth/bluetooth_discovery_session.h" | 26 #include "device/bluetooth/bluetooth_discovery_session.h" |
26 #include "device/bluetooth/bluetooth_local_gatt_service.h" | 27 #include "device/bluetooth/bluetooth_local_gatt_service.h" |
27 #include "device/bluetooth/bluetooth_remote_gatt_characteristic.h" | 28 #include "device/bluetooth/bluetooth_remote_gatt_characteristic.h" |
28 #include "device/bluetooth/bluetooth_remote_gatt_descriptor.h" | 29 #include "device/bluetooth/bluetooth_remote_gatt_descriptor.h" |
29 #include "device/bluetooth/bluetooth_remote_gatt_service.h" | 30 #include "device/bluetooth/bluetooth_remote_gatt_service.h" |
30 #include "device/bluetooth/bluez/bluetooth_adapter_bluez.h" | 31 #include "device/bluetooth/bluez/bluetooth_adapter_bluez.h" |
31 #include "mojo/public/cpp/bindings/binding.h" | 32 #include "mojo/public/cpp/bindings/binding.h" |
32 | 33 |
33 namespace arc { | 34 namespace arc { |
(...skipping 232 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
266 const SendIndicationCallback& callback) override; | 267 const SendIndicationCallback& callback) override; |
267 | 268 |
268 // Bluetooth Mojo host interface - Bluetooth SDP functions | 269 // Bluetooth Mojo host interface - Bluetooth SDP functions |
269 void GetSdpRecords(mojom::BluetoothAddressPtr remote_addr, | 270 void GetSdpRecords(mojom::BluetoothAddressPtr remote_addr, |
270 const device::BluetoothUUID& target_uuid) override; | 271 const device::BluetoothUUID& target_uuid) override; |
271 void CreateSdpRecord(mojom::BluetoothSdpRecordPtr record_mojo, | 272 void CreateSdpRecord(mojom::BluetoothSdpRecordPtr record_mojo, |
272 const CreateSdpRecordCallback& callback) override; | 273 const CreateSdpRecordCallback& callback) override; |
273 void RemoveSdpRecord(uint32_t service_handle, | 274 void RemoveSdpRecord(uint32_t service_handle, |
274 const RemoveSdpRecordCallback& callback) override; | 275 const RemoveSdpRecordCallback& callback) override; |
275 | 276 |
| 277 // Set up or disable multiple advertising. |
| 278 void ReserveAdvertisementHandle( |
| 279 const ReserveAdvertisementHandleCallback& callback) override; |
| 280 void BroadcastAdvertisement( |
| 281 int32_t adv_handle, |
| 282 std::unique_ptr<device::BluetoothAdvertisement::Data> advertisement, |
| 283 const BroadcastAdvertisementCallback& callback) override; |
| 284 void ReleaseAdvertisementHandle( |
| 285 int32_t adv_handle, |
| 286 const ReleaseAdvertisementHandleCallback& callback) override; |
| 287 |
276 // Chrome observer callbacks | 288 // Chrome observer callbacks |
277 void OnPoweredOn( | 289 void OnPoweredOn( |
278 const base::Callback<void(mojom::BluetoothAdapterState)>& callback) const; | 290 const base::Callback<void(mojom::BluetoothAdapterState)>& callback) const; |
279 void OnPoweredOff( | 291 void OnPoweredOff( |
280 const base::Callback<void(mojom::BluetoothAdapterState)>& callback) const; | 292 const base::Callback<void(mojom::BluetoothAdapterState)>& callback) const; |
281 void OnPoweredError( | 293 void OnPoweredError( |
282 const base::Callback<void(mojom::BluetoothAdapterState)>& callback) const; | 294 const base::Callback<void(mojom::BluetoothAdapterState)>& callback) const; |
283 void OnDiscoveryStarted( | 295 void OnDiscoveryStarted( |
284 std::unique_ptr<device::BluetoothDiscoverySession> session); | 296 std::unique_ptr<device::BluetoothDiscoverySession> session); |
285 void OnDiscoveryStopped(); | 297 void OnDiscoveryStopped(); |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
373 const device::BluetoothUUID& target_uuid, | 385 const device::BluetoothUUID& target_uuid, |
374 const std::vector<bluez::BluetoothServiceRecordBlueZ>& records_bluez); | 386 const std::vector<bluez::BluetoothServiceRecordBlueZ>& records_bluez); |
375 void OnGetServiceRecordsError( | 387 void OnGetServiceRecordsError( |
376 mojom::BluetoothAddressPtr remote_addr, | 388 mojom::BluetoothAddressPtr remote_addr, |
377 const device::BluetoothUUID& target_uuid, | 389 const device::BluetoothUUID& target_uuid, |
378 bluez::BluetoothServiceRecordBlueZ::ErrorCode error_code); | 390 bluez::BluetoothServiceRecordBlueZ::ErrorCode error_code); |
379 | 391 |
380 void OnSetAdapterProperty(mojom::BluetoothStatus success, | 392 void OnSetAdapterProperty(mojom::BluetoothStatus success, |
381 mojom::BluetoothPropertyPtr property); | 393 mojom::BluetoothPropertyPtr property); |
382 | 394 |
| 395 // Callbacks for managing advertisements registered from the instance. |
| 396 |
| 397 // Called when we have an open slot in the advertisement map and want to |
| 398 // register the advertisement given by |data| for handle |adv_handle|. |
| 399 void OnReadyToRegisterAdvertisement( |
| 400 const BroadcastAdvertisementCallback& callback, |
| 401 int32_t adv_handle, |
| 402 std::unique_ptr<device::BluetoothAdvertisement::Data> data); |
| 403 // Called when we've successfully registered a new advertisement for |
| 404 // handle |adv_handle|. |
| 405 void OnRegisterAdvertisementDone( |
| 406 const BroadcastAdvertisementCallback& callback, |
| 407 int32_t adv_handle, |
| 408 scoped_refptr<device::BluetoothAdvertisement> advertisement); |
| 409 // Called when the attempt to register an advertisement for handle |
| 410 // |adv_handle| has failed. |adv_handle| remains reserved, but no |
| 411 // advertisement is associated with it. |
| 412 void OnRegisterAdvertisementError( |
| 413 const BroadcastAdvertisementCallback& callback, |
| 414 int32_t adv_handle, |
| 415 device::BluetoothAdvertisement::ErrorCode error_code); |
| 416 // Both of the following are called after we've tried to unregister |
| 417 // the advertisement for |adv_handle|. Either way, we will no |
| 418 // longer be broadcasting this advertisement, so in either case, the |
| 419 // handle can be released. |
| 420 void OnUnregisterAdvertisementDone( |
| 421 const ReleaseAdvertisementHandleCallback& callback, |
| 422 int32_t adv_handle); |
| 423 void OnUnregisterAdvertisementError( |
| 424 const ReleaseAdvertisementHandleCallback& callback, |
| 425 int32_t adv_handle, |
| 426 device::BluetoothAdvertisement::ErrorCode error_code); |
| 427 // Find the next free advertisement handle and put it in *adv_handle, |
| 428 // or return false if the advertisement map is full. |
| 429 bool GetAdvertisementHandle(int32_t* adv_handle); |
| 430 |
383 bool CalledOnValidThread(); | 431 bool CalledOnValidThread(); |
384 | 432 |
385 mojo::Binding<mojom::BluetoothHost> binding_; | 433 mojo::Binding<mojom::BluetoothHost> binding_; |
386 | 434 |
387 scoped_refptr<bluez::BluetoothAdapterBlueZ> bluetooth_adapter_; | 435 scoped_refptr<bluez::BluetoothAdapterBlueZ> bluetooth_adapter_; |
388 scoped_refptr<device::BluetoothAdvertisement> advertisment_; | 436 scoped_refptr<device::BluetoothAdvertisement> advertisment_; |
389 std::unique_ptr<device::BluetoothDiscoverySession> discovery_session_; | 437 std::unique_ptr<device::BluetoothDiscoverySession> discovery_session_; |
390 std::unordered_map<std::string, | 438 std::unordered_map<std::string, |
391 std::unique_ptr<device::BluetoothGattNotifySession>> | 439 std::unique_ptr<device::BluetoothGattNotifySession>> |
392 notification_session_; | 440 notification_session_; |
(...skipping 11 matching lines...) Expand all Loading... |
404 // have made. We need to hang on to these as long as the connection is | 452 // have made. We need to hang on to these as long as the connection is |
405 // active since their destructors will drop the connections otherwise. | 453 // active since their destructors will drop the connections otherwise. |
406 std::unordered_map<std::string, | 454 std::unordered_map<std::string, |
407 std::unique_ptr<device::BluetoothGattConnection>> | 455 std::unique_ptr<device::BluetoothGattConnection>> |
408 gatt_connections_; | 456 gatt_connections_; |
409 // Timer to turn discovery off. | 457 // Timer to turn discovery off. |
410 base::OneShotTimer discovery_off_timer_; | 458 base::OneShotTimer discovery_off_timer_; |
411 // Timer to turn adapter discoverable off. | 459 // Timer to turn adapter discoverable off. |
412 base::OneShotTimer discoverable_off_timer_; | 460 base::OneShotTimer discoverable_off_timer_; |
413 | 461 |
| 462 // Holds advertising data registered by the instance. |
| 463 // |
| 464 // When a handle is reserved, an entry is placed into the advertisements_ |
| 465 // map. This entry is not yet associated with a device::BluetoothAdvertisement |
| 466 // because the instance hasn't sent us any advertising data yet, so its |
| 467 // mapped value is nullptr until that happens. Thus we have three states for a |
| 468 // handle: |
| 469 // * unmapped -> free |
| 470 // * mapped to nullptr -> reserved, awaiting data |
| 471 // * mapped to a device::BluetoothAdvertisement -> in use, and the mapped |
| 472 // BluetoothAdvertisement is currently registered with the adapter. |
| 473 enum { kMaxAdvertisements = 5 }; |
| 474 std::map<int32_t, scoped_refptr<device::BluetoothAdvertisement>> |
| 475 advertisements_; |
| 476 |
414 base::ThreadChecker thread_checker_; | 477 base::ThreadChecker thread_checker_; |
415 | 478 |
416 // WeakPtrFactory to use for callbacks. | 479 // WeakPtrFactory to use for callbacks. |
417 base::WeakPtrFactory<ArcBluetoothBridge> weak_factory_; | 480 base::WeakPtrFactory<ArcBluetoothBridge> weak_factory_; |
418 | 481 |
419 DISALLOW_COPY_AND_ASSIGN(ArcBluetoothBridge); | 482 DISALLOW_COPY_AND_ASSIGN(ArcBluetoothBridge); |
420 }; | 483 }; |
421 | 484 |
422 } // namespace arc | 485 } // namespace arc |
423 | 486 |
424 #endif // COMPONENTS_ARC_BLUETOOTH_ARC_BLUETOOTH_BRIDGE_H_ | 487 #endif // COMPONENTS_ARC_BLUETOOTH_ARC_BLUETOOTH_BRIDGE_H_ |
OLD | NEW |