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 EnableAdvertising( | |
|
rkc
2016/08/18 00:06:09
These functions are very, vague. Can we add commen
| |
| 272 int32_t min_interval, | |
| 273 int32_t max_interval, | |
| 274 device::BluetoothAdvertisement::AdvertisementType adv_type, | |
| 275 int32_t channel_map, | |
| 276 int32_t tx_power, | |
| 277 int32_t timeout_s, | |
| 278 const EnableAdvertisingCallback& callback) override; | |
| 279 void SetAdvertisingData( | |
| 280 int32_t adv_handle, | |
| 281 bool include_name, | |
| 282 bool include_tx_power, | |
| 283 int32_t appearance, | |
|
rkc
2016/08/18 00:06:10
Document what |appearance| means.
| |
| 284 mojo::Array<mojom::BluetoothAdvertisingDataPtr> adv_data, | |
| 285 const SetAdvertisingDataCallback& callback) override; | |
| 286 void DisableAdvertising(int32_t adv_handle, | |
|
rkc
2016/08/18 00:06:10
Document what this really does. Does it forget all
| |
| 287 const DisableAdvertisingCallback& callback) override; | |
| 288 | |
| 269 // Chrome observer callbacks | 289 // Chrome observer callbacks |
| 270 void OnPoweredOn( | 290 void OnPoweredOn( |
| 271 const base::Callback<void(mojom::BluetoothAdapterState)>& callback) const; | 291 const base::Callback<void(mojom::BluetoothAdapterState)>& callback) const; |
| 272 void OnPoweredOff( | 292 void OnPoweredOff( |
| 273 const base::Callback<void(mojom::BluetoothAdapterState)>& callback) const; | 293 const base::Callback<void(mojom::BluetoothAdapterState)>& callback) const; |
| 274 void OnPoweredError( | 294 void OnPoweredError( |
| 275 const base::Callback<void(mojom::BluetoothAdapterState)>& callback) const; | 295 const base::Callback<void(mojom::BluetoothAdapterState)>& callback) const; |
| 276 void OnDiscoveryStarted( | 296 void OnDiscoveryStarted( |
| 277 std::unique_ptr<device::BluetoothDiscoverySession> session); | 297 std::unique_ptr<device::BluetoothDiscoverySession> session); |
| 278 void OnDiscoveryStopped(); | 298 void OnDiscoveryStopped(); |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 305 void OnStopLEListenError( | 325 void OnStopLEListenError( |
| 306 const StopLEListenCallback& callback, | 326 const StopLEListenCallback& callback, |
| 307 device::BluetoothAdvertisement::ErrorCode error_code); | 327 device::BluetoothAdvertisement::ErrorCode error_code); |
| 308 | 328 |
| 309 void OnGattNotifyStartDone( | 329 void OnGattNotifyStartDone( |
| 310 const RegisterForGattNotificationCallback& callback, | 330 const RegisterForGattNotificationCallback& callback, |
| 311 const std::string char_string_id, | 331 const std::string char_string_id, |
| 312 std::unique_ptr<device::BluetoothGattNotifySession> notify_session); | 332 std::unique_ptr<device::BluetoothGattNotifySession> notify_session); |
| 313 | 333 |
| 314 private: | 334 private: |
| 335 // Structure to do bookkeeping for advertisements registered via | |
| 336 // multi advertisement across API calls. | |
| 337 struct InstanceAdvertisement { | |
|
rkc
2016/08/18 00:06:10
s/InstanceAdvertisement/AdvertisementInstance
| |
| 338 bool in_use; | |
|
Luis Héctor Chávez
2016/08/18 04:29:13
data members should go at the end: https://engdoc.
| |
| 339 device::BluetoothAdvertisement::AdvertisementType adv_type; | |
| 340 scoped_refptr<device::BluetoothAdvertisement> advertisement; | |
| 341 | |
| 342 void Clear() { | |
| 343 advertisement = nullptr; | |
| 344 in_use = false; | |
| 345 } | |
| 346 }; | |
| 347 | |
| 315 mojo::Array<mojom::BluetoothPropertyPtr> GetDeviceProperties( | 348 mojo::Array<mojom::BluetoothPropertyPtr> GetDeviceProperties( |
| 316 mojom::BluetoothPropertyType type, | 349 mojom::BluetoothPropertyType type, |
| 317 device::BluetoothDevice* device) const; | 350 device::BluetoothDevice* device) const; |
| 318 mojo::Array<mojom::BluetoothPropertyPtr> GetAdapterProperties( | 351 mojo::Array<mojom::BluetoothPropertyPtr> GetAdapterProperties( |
| 319 mojom::BluetoothPropertyType type) const; | 352 mojom::BluetoothPropertyType type) const; |
| 320 mojo::Array<mojom::BluetoothAdvertisingDataPtr> GetAdvertisingData( | 353 mojo::Array<mojom::BluetoothAdvertisingDataPtr> GetAdvertisingData( |
| 321 device::BluetoothDevice* device) const; | 354 device::BluetoothDevice* device) const; |
| 322 | 355 |
| 323 void SendCachedDevicesFound() const; | 356 void SendCachedDevicesFound() const; |
| 324 bool HasBluetoothInstance() const; | 357 bool HasBluetoothInstance() const; |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 353 | 386 |
| 354 // Common code for OnCharacteristicWriteRequest and OnDescriptorWriteRequest | 387 // Common code for OnCharacteristicWriteRequest and OnDescriptorWriteRequest |
| 355 template <class LocalGattAttribute> | 388 template <class LocalGattAttribute> |
| 356 void OnGattAttributeWriteRequest(const device::BluetoothDevice* device, | 389 void OnGattAttributeWriteRequest(const device::BluetoothDevice* device, |
| 357 const LocalGattAttribute* attribute, | 390 const LocalGattAttribute* attribute, |
| 358 const std::vector<uint8_t>& value, | 391 const std::vector<uint8_t>& value, |
| 359 int offset, | 392 int offset, |
| 360 const base::Closure& success_callback, | 393 const base::Closure& success_callback, |
| 361 const ErrorCallback& error_callback); | 394 const ErrorCallback& error_callback); |
| 362 | 395 |
| 396 // Callbacks for managing multiple advertisements | |
| 397 void OnRegisterAdvertisementDone( | |
| 398 const SetAdvertisingDataCallback& callback, | |
| 399 int32_t adv_handle, | |
| 400 scoped_refptr<device::BluetoothAdvertisement> advertisement); | |
| 401 void OnRegisterAdvertisementError( | |
| 402 const SetAdvertisingDataCallback& callback, | |
| 403 int32_t adv_handle, | |
| 404 device::BluetoothAdvertisement::ErrorCode error_code); | |
| 405 void OnUnregisterAdvertisementDone(const DisableAdvertisingCallback& callback, | |
| 406 int32_t adv_handle); | |
| 407 void OnUnregisterAdvertisementError( | |
| 408 const DisableAdvertisingCallback& callback, | |
| 409 int32_t adv_handle, | |
| 410 device::BluetoothAdvertisement::ErrorCode error_code); | |
| 411 | |
| 363 bool CalledOnValidThread(); | 412 bool CalledOnValidThread(); |
| 364 | 413 |
| 365 mojo::Binding<mojom::BluetoothHost> binding_; | 414 mojo::Binding<mojom::BluetoothHost> binding_; |
| 366 | 415 |
| 367 scoped_refptr<bluez::BluetoothAdapterBlueZ> bluetooth_adapter_; | 416 scoped_refptr<bluez::BluetoothAdapterBlueZ> bluetooth_adapter_; |
| 368 scoped_refptr<device::BluetoothAdvertisement> advertisment_; | 417 scoped_refptr<device::BluetoothAdvertisement> advertisment_; |
| 369 std::unique_ptr<device::BluetoothDiscoverySession> discovery_session_; | 418 std::unique_ptr<device::BluetoothDiscoverySession> discovery_session_; |
| 370 std::unordered_map<std::string, | 419 std::unordered_map<std::string, |
| 371 std::unique_ptr<device::BluetoothGattNotifySession>> | 420 std::unique_ptr<device::BluetoothGattNotifySession>> |
| 372 notification_session_; | 421 notification_session_; |
| 373 // Map from Android int handle to Chrome (BlueZ) string identifier. | 422 // Map from Android int handle to Chrome (BlueZ) string identifier. |
| 374 std::unordered_map<int32_t, std::string> gatt_identifier_; | 423 std::unordered_map<int32_t, std::string> gatt_identifier_; |
| 375 // Map from Chrome (BlueZ) string identifier to android int handle. | 424 // Map from Chrome (BlueZ) string identifier to android int handle. |
| 376 std::unordered_map<std::string, int32_t> gatt_handle_; | 425 std::unordered_map<std::string, int32_t> gatt_handle_; |
| 377 // Store last GattCharacteristic added to each GattService for GattServer. | 426 // Store last GattCharacteristic added to each GattService for GattServer. |
| 378 std::unordered_map<int32_t, int32_t> last_characteristic_; | 427 std::unordered_map<int32_t, int32_t> last_characteristic_; |
| 379 // Monotonically increasing value to use as handle to give to Android side. | 428 // Monotonically increasing value to use as handle to give to Android side. |
| 380 int32_t gatt_server_attribute_next_handle_ = 0; | 429 int32_t gatt_server_attribute_next_handle_ = 0; |
| 381 // Keeps track of all devices which initiated a GATT connection to us. | 430 // Keeps track of all devices which initiated a GATT connection to us. |
| 382 std::unordered_set<std::string> gatt_connection_cache_; | 431 std::unordered_set<std::string> gatt_connection_cache_; |
| 383 | 432 |
| 433 // Copied from Android at system/bt/stack/btm/btm_ble_int.h | |
| 434 // https://goo.gl/k7PM6u | |
|
puthik_chromium
2016/08/17 23:22:04
This is wrong comment
| |
| 435 static constexpr uint16_t kMaxAdvertisement = 5; | |
|
rkc
2016/08/18 00:06:10
s/kMaxAdvertisement/kMaxAdvertisements
| |
| 436 // Holds advertising data registered by the instance. | |
| 437 InstanceAdvertisement advertisements_[kMaxAdvertisement]; | |
|
rkc
2016/08/18 00:06:09
Instead of having this as a static array and needi
| |
| 438 | |
| 384 base::ThreadChecker thread_checker_; | 439 base::ThreadChecker thread_checker_; |
| 385 | 440 |
| 386 // WeakPtrFactory to use for callbacks. | 441 // WeakPtrFactory to use for callbacks. |
| 387 base::WeakPtrFactory<ArcBluetoothBridge> weak_factory_; | 442 base::WeakPtrFactory<ArcBluetoothBridge> weak_factory_; |
| 388 | 443 |
| 389 DISALLOW_COPY_AND_ASSIGN(ArcBluetoothBridge); | 444 DISALLOW_COPY_AND_ASSIGN(ArcBluetoothBridge); |
| 390 }; | 445 }; |
| 391 | 446 |
| 392 } // namespace arc | 447 } // namespace arc |
| 393 | 448 |
| 394 #endif // COMPONENTS_ARC_BLUETOOTH_ARC_BLUETOOTH_BRIDGE_H_ | 449 #endif // COMPONENTS_ARC_BLUETOOTH_ARC_BLUETOOTH_BRIDGE_H_ |
| OLD | NEW |