Chromium Code Reviews| 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 <vector> | 13 #include <vector> |
| 14 | 14 |
| 15 #include "base/callback.h" | 15 #include "base/callback.h" |
| 16 #include "components/arc/arc_bridge_service.h" | 16 #include "components/arc/arc_bridge_service.h" |
| 17 #include "components/arc/arc_service.h" | 17 #include "components/arc/arc_service.h" |
| 18 #include "components/arc/common/bluetooth.mojom.h" | 18 #include "components/arc/common/bluetooth.mojom.h" |
| 19 #include "components/arc/instance_holder.h" | 19 #include "components/arc/instance_holder.h" |
| 20 #include "device/bluetooth/bluetooth_adapter.h" | 20 #include "device/bluetooth/bluetooth_adapter.h" |
| 21 #include "device/bluetooth/bluetooth_adapter_factory.h" | 21 #include "device/bluetooth/bluetooth_adapter_factory.h" |
| 22 #include "device/bluetooth/bluetooth_advertisement.h" | |
| 22 #include "device/bluetooth/bluetooth_device.h" | 23 #include "device/bluetooth/bluetooth_device.h" |
| 23 #include "device/bluetooth/bluetooth_discovery_session.h" | 24 #include "device/bluetooth/bluetooth_discovery_session.h" |
| 24 #include "device/bluetooth/bluetooth_local_gatt_service.h" | 25 #include "device/bluetooth/bluetooth_local_gatt_service.h" |
| 25 #include "device/bluetooth/bluetooth_remote_gatt_characteristic.h" | 26 #include "device/bluetooth/bluetooth_remote_gatt_characteristic.h" |
| 26 #include "device/bluetooth/bluetooth_remote_gatt_descriptor.h" | 27 #include "device/bluetooth/bluetooth_remote_gatt_descriptor.h" |
| 27 #include "device/bluetooth/bluetooth_remote_gatt_service.h" | 28 #include "device/bluetooth/bluetooth_remote_gatt_service.h" |
| 28 #include "device/bluetooth/bluez/bluetooth_adapter_bluez.h" | 29 #include "device/bluetooth/bluez/bluetooth_adapter_bluez.h" |
| 29 #include "mojo/public/cpp/bindings/binding.h" | 30 #include "mojo/public/cpp/bindings/binding.h" |
| 30 | 31 |
| 31 namespace arc { | 32 namespace arc { |
| (...skipping 227 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 259 // Delete a local service. | 260 // Delete a local service. |
| 260 void DeleteService(int32_t service_handle, | 261 void DeleteService(int32_t service_handle, |
| 261 const DeleteServiceCallback& callback) override; | 262 const DeleteServiceCallback& callback) override; |
| 262 // Send value indication to a remote device. | 263 // Send value indication to a remote device. |
| 263 void SendIndication(int32_t attribute_handle, | 264 void SendIndication(int32_t attribute_handle, |
| 264 mojom::BluetoothAddressPtr address, | 265 mojom::BluetoothAddressPtr address, |
| 265 bool confirm, | 266 bool confirm, |
| 266 mojo::Array<uint8_t> value, | 267 mojo::Array<uint8_t> value, |
| 267 const SendIndicationCallback& callback) override; | 268 const SendIndicationCallback& callback) override; |
| 268 | 269 |
| 270 // Set up or disable multiple advertising. | |
| 271 void ReserveAdvertisementHandle( | |
| 272 const ReserveAdvertisementHandleCallback& callback) override; | |
| 273 void BroadcastAdvertisement( | |
| 274 int32_t adv_handle, | |
| 275 mojom::BluetoothAdvertisementPtr advertisement, | |
| 276 const BroadcastAdvertisementCallback& callback) override; | |
| 277 void ReleaseAdvertisementHandle( | |
| 278 int32_t adv_handle, | |
| 279 const ReleaseAdvertisementHandleCallback& callback) override; | |
| 280 | |
| 269 // Chrome observer callbacks | 281 // Chrome observer callbacks |
| 270 void OnPoweredOn( | 282 void OnPoweredOn( |
| 271 const base::Callback<void(mojom::BluetoothAdapterState)>& callback) const; | 283 const base::Callback<void(mojom::BluetoothAdapterState)>& callback) const; |
| 272 void OnPoweredOff( | 284 void OnPoweredOff( |
| 273 const base::Callback<void(mojom::BluetoothAdapterState)>& callback) const; | 285 const base::Callback<void(mojom::BluetoothAdapterState)>& callback) const; |
| 274 void OnPoweredError( | 286 void OnPoweredError( |
| 275 const base::Callback<void(mojom::BluetoothAdapterState)>& callback) const; | 287 const base::Callback<void(mojom::BluetoothAdapterState)>& callback) const; |
| 276 void OnDiscoveryStarted( | 288 void OnDiscoveryStarted( |
| 277 std::unique_ptr<device::BluetoothDiscoverySession> session); | 289 std::unique_ptr<device::BluetoothDiscoverySession> session); |
| 278 void OnDiscoveryStopped(); | 290 void OnDiscoveryStopped(); |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 353 | 365 |
| 354 // Common code for OnCharacteristicWriteRequest and OnDescriptorWriteRequest | 366 // Common code for OnCharacteristicWriteRequest and OnDescriptorWriteRequest |
| 355 template <class LocalGattAttribute> | 367 template <class LocalGattAttribute> |
| 356 void OnGattAttributeWriteRequest(const device::BluetoothDevice* device, | 368 void OnGattAttributeWriteRequest(const device::BluetoothDevice* device, |
| 357 const LocalGattAttribute* attribute, | 369 const LocalGattAttribute* attribute, |
| 358 const std::vector<uint8_t>& value, | 370 const std::vector<uint8_t>& value, |
| 359 int offset, | 371 int offset, |
| 360 const base::Closure& success_callback, | 372 const base::Closure& success_callback, |
| 361 const ErrorCallback& error_callback); | 373 const ErrorCallback& error_callback); |
| 362 | 374 |
| 375 // Callbacks for managing advertisements registered from the instance. | |
| 376 | |
| 377 // Called when we have an open slot in the advertisement map and want to | |
| 378 // register the advertisement given by |data| for handle |adv_handle|. | |
| 379 void OnReadyToRegisterAdvertisement( | |
| 380 const BroadcastAdvertisementCallback& callback, | |
| 381 int32_t adv_handle, | |
| 382 std::unique_ptr<device::BluetoothAdvertisement::Data> data); | |
| 383 // Called when we've successfully registered a new advertisement for | |
| 384 // handle |adv_handle|. | |
| 385 void OnRegisterAdvertisementDone( | |
| 386 const BroadcastAdvertisementCallback& callback, | |
| 387 int32_t adv_handle, | |
| 388 scoped_refptr<device::BluetoothAdvertisement> advertisement); | |
| 389 // Called when the attempt to register an advertisement for handle | |
| 390 // |adv_handle| has failed. |adv_handle| remains reserved, but no | |
| 391 // advertisement is associated with it. | |
| 392 void OnRegisterAdvertisementError( | |
| 393 const BroadcastAdvertisementCallback& callback, | |
| 394 int32_t adv_handle, | |
| 395 device::BluetoothAdvertisement::ErrorCode error_code); | |
| 396 // Both of the following are called after we've tried to unregister | |
| 397 // the advertisement for |adv_handle|. Either way, we will no | |
| 398 // longer be broadcasting this advertisement, so in either case, the | |
| 399 // handle can be released. | |
| 400 void OnUnregisterAdvertisementDone( | |
| 401 const ReleaseAdvertisementHandleCallback& callback, | |
| 402 int32_t adv_handle); | |
| 403 void OnUnregisterAdvertisementError( | |
| 404 const ReleaseAdvertisementHandleCallback& callback, | |
| 405 int32_t adv_handle, | |
| 406 device::BluetoothAdvertisement::ErrorCode error_code); | |
| 407 // Find the next free advertisement handle and put it in *adv_handle, | |
| 408 // or return false if the advertisement map is full. | |
| 409 bool GetAdvertisementHandle(int32_t* adv_handle); | |
| 410 | |
| 363 bool CalledOnValidThread(); | 411 bool CalledOnValidThread(); |
| 364 | 412 |
| 365 mojo::Binding<mojom::BluetoothHost> binding_; | 413 mojo::Binding<mojom::BluetoothHost> binding_; |
| 366 | 414 |
| 367 scoped_refptr<bluez::BluetoothAdapterBlueZ> bluetooth_adapter_; | 415 scoped_refptr<bluez::BluetoothAdapterBlueZ> bluetooth_adapter_; |
| 368 scoped_refptr<device::BluetoothAdvertisement> advertisment_; | 416 scoped_refptr<device::BluetoothAdvertisement> advertisment_; |
| 369 std::unique_ptr<device::BluetoothDiscoverySession> discovery_session_; | 417 std::unique_ptr<device::BluetoothDiscoverySession> discovery_session_; |
| 370 std::unordered_map<std::string, | 418 std::unordered_map<std::string, |
| 371 std::unique_ptr<device::BluetoothGattNotifySession>> | 419 std::unique_ptr<device::BluetoothGattNotifySession>> |
| 372 notification_session_; | 420 notification_session_; |
| 373 // Map from Android int handle to Chrome (BlueZ) string identifier. | 421 // Map from Android int handle to Chrome (BlueZ) string identifier. |
| 374 std::unordered_map<int32_t, std::string> gatt_identifier_; | 422 std::unordered_map<int32_t, std::string> gatt_identifier_; |
| 375 // Map from Chrome (BlueZ) string identifier to android int handle. | 423 // Map from Chrome (BlueZ) string identifier to android int handle. |
| 376 std::unordered_map<std::string, int32_t> gatt_handle_; | 424 std::unordered_map<std::string, int32_t> gatt_handle_; |
| 377 // Store last GattCharacteristic added to each GattService for GattServer. | 425 // Store last GattCharacteristic added to each GattService for GattServer. |
| 378 std::unordered_map<int32_t, int32_t> last_characteristic_; | 426 std::unordered_map<int32_t, int32_t> last_characteristic_; |
| 379 // Monotonically increasing value to use as handle to give to Android side. | 427 // Monotonically increasing value to use as handle to give to Android side. |
| 380 int32_t gatt_server_attribute_next_handle_ = 0; | 428 int32_t gatt_server_attribute_next_handle_ = 0; |
| 381 // Keeps track of all devices which initiated a GATT connection to us. | 429 // Keeps track of all devices which initiated a GATT connection to us. |
| 382 std::unordered_set<std::string> gatt_connection_cache_; | 430 std::unordered_set<std::string> gatt_connection_cache_; |
| 383 | 431 |
| 432 // Holds advertising data registered by the instance. | |
| 433 static constexpr uint16_t kMaxAdvertisements = 5; | |
| 434 std::map<int32_t, scoped_refptr<device::BluetoothAdvertisement>> | |
|
rickyz (no longer on Chrome)
2016/08/24 21:47:30
Did you see rkc's comment on using std::array for
Eric Caruso
2016/08/24 22:24:42
The design changed, and I got rid of Advertisement
| |
| 435 advertisements_; | |
| 436 | |
| 384 base::ThreadChecker thread_checker_; | 437 base::ThreadChecker thread_checker_; |
| 385 | 438 |
| 386 // WeakPtrFactory to use for callbacks. | 439 // WeakPtrFactory to use for callbacks. |
| 387 base::WeakPtrFactory<ArcBluetoothBridge> weak_factory_; | 440 base::WeakPtrFactory<ArcBluetoothBridge> weak_factory_; |
| 388 | 441 |
| 389 DISALLOW_COPY_AND_ASSIGN(ArcBluetoothBridge); | 442 DISALLOW_COPY_AND_ASSIGN(ArcBluetoothBridge); |
| 390 }; | 443 }; |
| 391 | 444 |
| 392 } // namespace arc | 445 } // namespace arc |
| 393 | 446 |
| 394 #endif // COMPONENTS_ARC_BLUETOOTH_ARC_BLUETOOTH_BRIDGE_H_ | 447 #endif // COMPONENTS_ARC_BLUETOOTH_ARC_BLUETOOTH_BRIDGE_H_ |
| OLD | NEW |