1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef DEVICE_BLUETOOTH_BLUETOOTH_REMOTE_GATT_SERVICE_CHROMEOS_H_
6 #define DEVICE_BLUETOOTH_BLUETOOTH_REMOTE_GATT_SERVICE_CHROMEOS_H_
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/weak_ptr.h"
14 #include "base/observer_list.h"
15 #include "chromeos/dbus/bluetooth_gatt_characteristic_client.h"
16 #include "chromeos/dbus/bluetooth_gatt_service_client.h"
17 #include "dbus/object_path.h"
18 #include "device/bluetooth/bluetooth_gatt_service.h"
19 #include "device/bluetooth/bluetooth_uuid.h"
23 class BluetoothAdapter
;
24 class BluetoothGattCharacteristic
;
30 class BluetoothAdapterChromeOS
;
31 class BluetoothDeviceChromeOS
;
32 class BluetoothRemoteGattCharacteristicChromeOS
;
33 class BluetoothRemoteGattDescriptorChromeOS
;
35 // The BluetoothRemoteGattServiceChromeOS class implements BluetootGattService
36 // for remote GATT services on the Chrome OS platform.
37 class BluetoothRemoteGattServiceChromeOS
38 : public device::BluetoothGattService
,
39 public BluetoothGattServiceClient::Observer
,
40 public BluetoothGattCharacteristicClient::Observer
{
42 // device::BluetoothGattService overrides.
43 std::string
GetIdentifier() const override
;
44 device::BluetoothUUID
GetUUID() const override
;
45 bool IsLocal() const override
;
46 bool IsPrimary() const override
;
47 device::BluetoothDevice
* GetDevice() const override
;
48 std::vector
<device::BluetoothGattCharacteristic
*> GetCharacteristics()
50 std::vector
<device::BluetoothGattService
*> GetIncludedServices()
52 device::BluetoothGattCharacteristic
* GetCharacteristic(
53 const std::string
& identifier
) const override
;
54 bool AddCharacteristic(
55 device::BluetoothGattCharacteristic
* characteristic
) override
;
56 bool AddIncludedService(device::BluetoothGattService
* service
) override
;
57 void Register(const base::Closure
& callback
,
58 const ErrorCallback
& error_callback
) override
;
59 void Unregister(const base::Closure
& callback
,
60 const ErrorCallback
& error_callback
) override
;
62 // Object path of the underlying service.
63 const dbus::ObjectPath
& object_path() const { return object_path_
; }
65 // Parses a named D-Bus error into a service error code.
66 static device::BluetoothGattService::GattErrorCode
DBusErrorToServiceError(
67 const std::string error_name
);
69 // Returns the adapter associated with this service.
70 BluetoothAdapterChromeOS
* GetAdapter() const;
72 // Notifies its observers that the GATT service has changed. This is mainly
73 // used by BluetoothRemoteGattCharacteristicChromeOS instances to notify
74 // service observers when characteristic descriptors get added and removed.
75 void NotifyServiceChanged();
77 // Notifies its observers that a descriptor |descriptor| belonging to
78 // characteristic |characteristic| has been added or removed. This is used
79 // by BluetoothRemoteGattCharacteristicChromeOS instances to notify service
80 // observers when characteristic descriptors get added and removed. If |added|
81 // is true, an "Added" event will be sent. Otherwise, a "Removed" event will
83 void NotifyDescriptorAddedOrRemoved(
84 BluetoothRemoteGattCharacteristicChromeOS
* characteristic
,
85 BluetoothRemoteGattDescriptorChromeOS
* descriptor
,
88 // Notifies its observers that the value of a descriptor has changed. Called
89 // by BluetoothRemoteGattCharacteristicChromeOS instances to notify service
91 void NotifyDescriptorValueChanged(
92 BluetoothRemoteGattCharacteristicChromeOS
* characteristic
,
93 BluetoothRemoteGattDescriptorChromeOS
* descriptor
,
94 const std::vector
<uint8
>& value
);
97 friend class BluetoothDeviceChromeOS
;
99 BluetoothRemoteGattServiceChromeOS(BluetoothAdapterChromeOS
* adapter
,
100 BluetoothDeviceChromeOS
* device
,
101 const dbus::ObjectPath
& object_path
);
102 ~BluetoothRemoteGattServiceChromeOS() override
;
104 // BluetoothGattServiceClient::Observer override.
105 void GattServicePropertyChanged(const dbus::ObjectPath
& object_path
,
106 const std::string
& property_name
) override
;
108 // BluetoothGattCharacteristicClient::Observer override.
109 void GattCharacteristicAdded(const dbus::ObjectPath
& object_path
) override
;
110 void GattCharacteristicRemoved(const dbus::ObjectPath
& object_path
) override
;
111 void GattCharacteristicPropertyChanged(
112 const dbus::ObjectPath
& object_path
,
113 const std::string
& property_name
) override
;
115 // Object path of the GATT service.
116 dbus::ObjectPath object_path_
;
118 // The adapter associated with this service. It's ok to store a raw pointer
119 // here since |adapter_| indirectly owns this instance.
120 BluetoothAdapterChromeOS
* adapter_
;
122 // The device this GATT service belongs to. It's ok to store a raw pointer
123 // here since |device_| owns this instance.
124 BluetoothDeviceChromeOS
* device_
;
126 // Mapping from GATT characteristic object paths to characteristic objects.
127 // owned by this service. Since the Chrome OS implementation uses object
128 // paths as unique identifiers, we also use this mapping to return
129 // characteristics by identifier.
130 typedef std::map
<dbus::ObjectPath
, BluetoothRemoteGattCharacteristicChromeOS
*>
132 CharacteristicMap characteristics_
;
134 // Indicates whether or not the characteristics of this service are known to
135 // have been discovered.
136 bool discovery_complete_
;
138 // Note: This should remain the last member so it'll be destroyed and
139 // invalidate its weak pointers before any other members are destroyed.
140 base::WeakPtrFactory
<BluetoothRemoteGattServiceChromeOS
> weak_ptr_factory_
;
142 DISALLOW_COPY_AND_ASSIGN(BluetoothRemoteGattServiceChromeOS
);
145 } // namespace chromeos
147 #endif // DEVICE_BLUETOOTH_BLUETOOTH_REMOTE_GATT_SERVICE_CHROMEOS_H_