Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(299)

Side by Side Diff: device/bluetooth/bluetooth_device.h

Issue 1292263002: bluetooth: Create base class BluetoothDevice::CreateGattConnection impl. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@bt-adapter-
Patch Set: addressed jyasskin comments, fixed BluetoothDevice destructor re-entrancy. Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 DEVICE_BLUETOOTH_BLUETOOTH_DEVICE_H_ 5 #ifndef DEVICE_BLUETOOTH_BLUETOOTH_DEVICE_H_
6 #define DEVICE_BLUETOOTH_BLUETOOTH_DEVICE_H_ 6 #define DEVICE_BLUETOOTH_BLUETOOTH_DEVICE_H_
7 7
8 #include <map> 8 #include <map>
9 #include <set>
9 #include <string> 10 #include <string>
10 #include <vector> 11 #include <vector>
11 12
12 #include "base/callback.h" 13 #include "base/callback.h"
13 #include "base/memory/ref_counted.h" 14 #include "base/memory/ref_counted.h"
15 #include "base/numerics/safe_math.h"
14 #include "base/strings/string16.h" 16 #include "base/strings/string16.h"
15 #include "device/bluetooth/bluetooth_export.h" 17 #include "device/bluetooth/bluetooth_export.h"
16 #include "device/bluetooth/bluetooth_uuid.h" 18 #include "device/bluetooth/bluetooth_uuid.h"
17 #include "net/log/net_log.h" 19 #include "net/log/net_log.h"
18 20
19 namespace base { 21 namespace base {
20 class BinaryValue; 22 class BinaryValue;
21 } 23 }
22 24
23 namespace device { 25 namespace device {
(...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after
232 virtual bool IsPaired() const = 0; 234 virtual bool IsPaired() const = 0;
233 235
234 // Indicates whether the device is currently connected to the adapter. 236 // Indicates whether the device is currently connected to the adapter.
235 // Note that if IsConnected() is true, does not imply that the device is 237 // Note that if IsConnected() is true, does not imply that the device is
236 // connected to any application or service. If the device is not paired, it 238 // connected to any application or service. If the device is not paired, it
237 // could be still connected to the adapter for other reason, for example, to 239 // could be still connected to the adapter for other reason, for example, to
238 // request the adapter's SDP records. The same holds for paired devices, since 240 // request the adapter's SDP records. The same holds for paired devices, since
239 // they could be connected to the adapter but not to an application. 241 // they could be connected to the adapter but not to an application.
240 virtual bool IsConnected() const = 0; 242 virtual bool IsConnected() const = 0;
241 243
244 // Indicates whether an active GATT connection exists to the device.
245 virtual bool IsGattConnected() const = 0;
246
242 // Indicates whether the paired device accepts connections initiated from the 247 // Indicates whether the paired device accepts connections initiated from the
243 // adapter. This value is undefined for unpaired devices. 248 // adapter. This value is undefined for unpaired devices.
244 virtual bool IsConnectable() const = 0; 249 virtual bool IsConnectable() const = 0;
245 250
246 // Indicates whether there is a call to Connect() ongoing. For this attribute, 251 // Indicates whether there is a call to Connect() ongoing. For this attribute,
247 // we consider a call is ongoing if none of the callbacks passed to Connect() 252 // we consider a call is ongoing if none of the callbacks passed to Connect()
248 // were called after the corresponding call to Connect(). 253 // were called after the corresponding call to Connect().
249 virtual bool IsConnecting() const = 0; 254 virtual bool IsConnecting() const = 0;
250 255
251 // Indicates whether the device can be trusted, based on device properties, 256 // Indicates whether the device can be trusted, based on device properties,
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after
399 // BluetoothGattConnection will be handed to the caller via |callback|. On 404 // BluetoothGattConnection will be handed to the caller via |callback|. On
400 // error, |error_callback| will be called. The connection will be kept alive, 405 // error, |error_callback| will be called. The connection will be kept alive,
401 // as long as there is at least one active GATT connection. In the case that 406 // as long as there is at least one active GATT connection. In the case that
402 // the underlying connection gets terminated, either due to a call to 407 // the underlying connection gets terminated, either due to a call to
403 // BluetoothDevice::Disconnect or other unexpected circumstances, the 408 // BluetoothDevice::Disconnect or other unexpected circumstances, the
404 // returned BluetoothGattConnection will be automatically marked as inactive. 409 // returned BluetoothGattConnection will be automatically marked as inactive.
405 // To monitor the state of the connection, observe the 410 // To monitor the state of the connection, observe the
406 // BluetoothAdapter::Observer::DeviceChanged method. 411 // BluetoothAdapter::Observer::DeviceChanged method.
407 typedef base::Callback<void(scoped_ptr<BluetoothGattConnection>)> 412 typedef base::Callback<void(scoped_ptr<BluetoothGattConnection>)>
408 GattConnectionCallback; 413 GattConnectionCallback;
409 virtual void CreateGattConnection( 414 virtual void CreateGattConnection(const GattConnectionCallback& callback,
410 const GattConnectionCallback& callback, 415 const ConnectErrorCallback& error_callback);
411 const ConnectErrorCallback& error_callback) = 0;
412 416
413 // Returns the list of discovered GATT services. 417 // Returns the list of discovered GATT services.
414 virtual std::vector<BluetoothGattService*> GetGattServices() const; 418 virtual std::vector<BluetoothGattService*> GetGattServices() const;
415 419
416 // Returns the GATT service with device-specific identifier |identifier|. 420 // Returns the GATT service with device-specific identifier |identifier|.
417 // Returns NULL, if no such service exists. 421 // Returns NULL, if no such service exists.
418 virtual BluetoothGattService* GetGattService( 422 virtual BluetoothGattService* GetGattService(
419 const std::string& identifier) const; 423 const std::string& identifier) const;
420 424
421 // Returns service data of a service given its UUID. 425 // Returns service data of a service given its UUID.
422 virtual base::BinaryValue* GetServiceData(BluetoothUUID serviceUUID) const; 426 virtual base::BinaryValue* GetServiceData(BluetoothUUID serviceUUID) const;
423 427
424 // Returns the list UUIDs of services that have service data. 428 // Returns the list UUIDs of services that have service data.
425 virtual UUIDList GetServiceDataUUIDs() const; 429 virtual UUIDList GetServiceDataUUIDs() const;
426 430
427 // Returns the |address| in the canonical format: XX:XX:XX:XX:XX:XX, where 431 // Returns the |address| in the canonical format: XX:XX:XX:XX:XX:XX, where
428 // each 'X' is a hex digit. If the input |address| is invalid, returns an 432 // each 'X' is a hex digit. If the input |address| is invalid, returns an
429 // empty string. 433 // empty string.
430 static std::string CanonicalizeAddress(const std::string& address); 434 static std::string CanonicalizeAddress(const std::string& address);
431 435
432 protected: 436 protected:
437 // BluetoothGattConnection is a friend to call Add/RemoveGattConnection.
438 friend BluetoothGattConnection;
439
433 BluetoothDevice(BluetoothAdapter* adapter); 440 BluetoothDevice(BluetoothAdapter* adapter);
434 441
435 // Returns the internal name of the Bluetooth device, used by GetName(). 442 // Returns the internal name of the Bluetooth device, used by GetName().
436 virtual std::string GetDeviceName() const = 0; 443 virtual std::string GetDeviceName() const = 0;
437 444
445 // Implements platform specific operations to initiate a GATT connection.
446 // Subclasses must also call DidConnectGatt, DidFailToConnectGatt, or
447 // DidDisconnectGatt immediately or asynchronously as the connection state
448 // changes.
449 virtual void CreateGattConnectionImpl() = 0;
450
451 // Disconnects GATT connection on platforms that maintain a specific GATT
452 // connection.
453 virtual void DisconnectGatt() = 0;
454
455 // Calls any pending callbacks for CreateGattConnection based on result of
456 // subclasses actions initiated in CreateGattConnectionImpl or related
457 // disconnection events. These may be called at any time, even multiple times,
Jeffrey Yasskin 2015/09/16 00:45:39 The implementation says DidDisconnectGatt() should
scheib 2015/09/16 21:19:08 Done.
458 // to ensure a change in platform state is correctly tracked.
459 void DidConnectGatt();
460 void DidFailToConnectGatt(ConnectErrorCode);
461 void DidDisconnectGatt();
462
463 // Maintains GattConnection reference count by tracking friend class
464 // BluetoothGattConnection instances.
Jeffrey Yasskin 2015/09/16 00:45:39 Is it too obvious to say that the BluetoothGattCon
scheib 2015/09/16 21:19:08 Done.
465 void AddGattConnection(BluetoothGattConnection*);
466 void RemoveGattConnection(BluetoothGattConnection*);
467
438 // Clears the list of service data. 468 // Clears the list of service data.
439 void ClearServiceData(); 469 void ClearServiceData();
440 470
441 // Set the data of a given service designated by its UUID. 471 // Set the data of a given service designated by its UUID.
442 void SetServiceData(BluetoothUUID serviceUUID, const char* buffer, 472 void SetServiceData(BluetoothUUID serviceUUID, const char* buffer,
443 size_t size); 473 size_t size);
444 474
445 // Raw pointer to adapter owning this device object. Subclasses use platform 475 // Raw pointer to adapter owning this device object. Subclasses use platform
446 // specific pointers via adapter_. 476 // specific pointers via adapter_.
447 BluetoothAdapter* adapter_; 477 BluetoothAdapter* adapter_;
448 478
479 // Callbacks for pending success and error result of CreateGattConnection.
480 std::vector<GattConnectionCallback> create_gatt_connection_success_callbacks_;
481 std::vector<ConnectErrorCallback> create_gatt_connection_error_callbacks_;
482
483 // BluetoothGattConnection objects keeping the GATT connection alive.
484 std::set<BluetoothGattConnection*> gatt_connections_;
485
449 // Mapping from the platform-specific GATT service identifiers to 486 // Mapping from the platform-specific GATT service identifiers to
450 // BluetoothGattService objects. 487 // BluetoothGattService objects.
451 typedef std::map<std::string, BluetoothGattService*> GattServiceMap; 488 typedef std::map<std::string, BluetoothGattService*> GattServiceMap;
452 GattServiceMap gatt_services_; 489 GattServiceMap gatt_services_;
453 490
454 // Mapping from service UUID represented as a std::string of a bluetooth 491 // Mapping from service UUID represented as a std::string of a bluetooth
455 // service to 492 // service to
456 // the specific data. The data is stored as BinaryValue. 493 // the specific data. The data is stored as BinaryValue.
457 scoped_ptr<base::DictionaryValue> services_data_; 494 scoped_ptr<base::DictionaryValue> services_data_;
458 495
459 private: 496 private:
460 // Returns a localized string containing the device's bluetooth address and 497 // Returns a localized string containing the device's bluetooth address and
461 // a device type for display when |name_| is empty. 498 // a device type for display when |name_| is empty.
462 base::string16 GetAddressWithLocalizedDeviceTypeName() const; 499 base::string16 GetAddressWithLocalizedDeviceTypeName() const;
463 }; 500 };
464 501
465 } // namespace device 502 } // namespace device
466 503
467 #endif // DEVICE_BLUETOOTH_BLUETOOTH_DEVICE_H_ 504 #endif // DEVICE_BLUETOOTH_BLUETOOTH_DEVICE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698