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/weak_ptr.h"
13 #include "base/observer_list.h"
14 #include "chromeos/dbus/bluetooth_gatt_characteristic_client.h"
15 #include "chromeos/dbus/bluetooth_gatt_service_client.h"
16 #include "dbus/object_path.h"
17 #include "device/bluetooth/bluetooth_gatt_service.h"
18 #include "device/bluetooth/bluetooth_uuid.h"
22 class BluetoothGattCharacteristic;
28 class BluetoothDeviceChromeOS;
29 class BluetoothRemoteGattCharacteristicChromeOS;
30 class BluetoothRemoteGattDescriptorChromeOS;
32 // The BluetoothRemoteGattServiceChromeOS class implements BluetootGattService
33 // for remote GATT services on the the Chrome OS platform.
34 class BluetoothRemoteGattServiceChromeOS
35 : public device::BluetoothGattService,
36 public BluetoothGattServiceClient::Observer,
37 public BluetoothGattCharacteristicClient::Observer {
39 // device::BluetoothGattService overrides.
40 virtual void AddObserver(
41 device::BluetoothGattService::Observer* observer) OVERRIDE;
42 virtual void RemoveObserver(
43 device::BluetoothGattService::Observer* observer) OVERRIDE;
44 virtual std::string GetIdentifier() const OVERRIDE;
45 virtual device::BluetoothUUID GetUUID() const OVERRIDE;
46 virtual bool IsLocal() const OVERRIDE;
47 virtual bool IsPrimary() const OVERRIDE;
48 virtual device::BluetoothDevice* GetDevice() const OVERRIDE;
49 virtual std::vector<device::BluetoothGattCharacteristic*>
50 GetCharacteristics() const OVERRIDE;
51 virtual std::vector<device::BluetoothGattService*>
52 GetIncludedServices() const OVERRIDE;
53 virtual device::BluetoothGattCharacteristic* GetCharacteristic(
54 const std::string& identifier) const OVERRIDE;
55 virtual bool AddCharacteristic(
56 device::BluetoothGattCharacteristic* characteristic) OVERRIDE;
57 virtual bool AddIncludedService(
58 device::BluetoothGattService* service) OVERRIDE;
59 virtual void Register(const base::Closure& callback,
60 const ErrorCallback& error_callback) OVERRIDE;
61 virtual void Unregister(const base::Closure& callback,
62 const ErrorCallback& error_callback) OVERRIDE;
64 // Object path of the underlying service.
65 const dbus::ObjectPath& object_path() const { return object_path_; }
67 // Notifies its observers that the GATT service has changed. This is mainly
68 // used by BluetoothRemoteGattCharacteristicChromeOS instances to notify
69 // service observers when characteristic descriptors get added and removed.
70 void NotifyServiceChanged();
72 // Notifies its observers that a descriptor |descriptor| belonging to
73 // characteristic |characteristic| has been added or removed. This is used
74 // by BluetoothRemoteGattCharacteristicChromeOS instances to notify service
75 // observers when characteristic descriptors get added and removed. If |added|
76 // is true, an "Added" event will be sent. Otherwise, a "Removed" event will
78 void NotifyDescriptorAddedOrRemoved(
79 BluetoothRemoteGattCharacteristicChromeOS* characteristic,
80 BluetoothRemoteGattDescriptorChromeOS* descriptor,
83 // Notifies its observers that the value of a descriptor has changed. Called
84 // by BluetoothRemoteGattCharacteristicChromeOS instances to notify service
85 // observers when the value of one of their descriptors gets updated.
86 void NotifyDescriptorValueChanged(
87 BluetoothRemoteGattCharacteristicChromeOS* characteristic,
88 BluetoothRemoteGattDescriptorChromeOS* descriptor,
89 const std::vector<uint8>& value);
92 friend class BluetoothDeviceChromeOS;
94 BluetoothRemoteGattServiceChromeOS(BluetoothDeviceChromeOS* device,
95 const dbus::ObjectPath& object_path);
96 virtual ~BluetoothRemoteGattServiceChromeOS();
98 // BluetoothGattServiceClient::Observer override.
99 virtual void GattServicePropertyChanged(
100 const dbus::ObjectPath& object_path,
101 const std::string& property_name) OVERRIDE;
103 // BluetoothGattCharacteristicClient::Observer override.
104 virtual void GattCharacteristicAdded(
105 const dbus::ObjectPath& object_path) OVERRIDE;
106 virtual void GattCharacteristicRemoved(
107 const dbus::ObjectPath& object_path) OVERRIDE;
108 virtual void GattCharacteristicPropertyChanged(
109 const dbus::ObjectPath& object_path,
110 const std::string& property_name) OVERRIDE;
112 // Object path of the GATT service.
113 dbus::ObjectPath object_path_;
115 // List of observers interested in event notifications from us.
116 ObserverList<device::BluetoothGattService::Observer> observers_;
118 // The device this GATT service belongs to.
119 BluetoothDeviceChromeOS* device_;
121 // Mapping from GATT characteristic object paths to characteristic objects.
122 // owned by this service. Since the Chrome OS implementation uses object
123 // paths as unique identifiers, we also use this mapping to return
124 // characteristics by identifier.
125 typedef std::map<dbus::ObjectPath, BluetoothRemoteGattCharacteristicChromeOS*>
127 CharacteristicMap characteristics_;
129 // Note: This should remain the last member so it'll be destroyed and
130 // invalidate its weak pointers before any other members are destroyed.
131 base::WeakPtrFactory<BluetoothRemoteGattServiceChromeOS> weak_ptr_factory_;
133 DISALLOW_COPY_AND_ASSIGN(BluetoothRemoteGattServiceChromeOS);
136 } // namespace chromeos
138 #endif // DEVICE_BLUETOOTH_BLUETOOTH_REMOTE_GATT_SERVICE_CHROMEOS_H_