• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #ifndef DEVICE_BLUETOOTH_BLUETOOTH_REMOTE_GATT_SERVICE_CHROMEOS_H_
6 #define DEVICE_BLUETOOTH_BLUETOOTH_REMOTE_GATT_SERVICE_CHROMEOS_H_
7 
8 #include <map>
9 #include <string>
10 #include <vector>
11 
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"
20 
21 namespace device {
22 
23 class BluetoothAdapter;
24 class BluetoothGattCharacteristic;
25 
26 }  // namespace device
27 
28 namespace chromeos {
29 
30 class BluetoothAdapterChromeOS;
31 class BluetoothDeviceChromeOS;
32 class BluetoothRemoteGattCharacteristicChromeOS;
33 class BluetoothRemoteGattDescriptorChromeOS;
34 
35 // The BluetoothRemoteGattServiceChromeOS class implements BluetootGattService
36 // for remote GATT services on the the Chrome OS platform.
37 class BluetoothRemoteGattServiceChromeOS
38     : public device::BluetoothGattService,
39       public BluetoothGattServiceClient::Observer,
40       public BluetoothGattCharacteristicClient::Observer {
41  public:
42   // device::BluetoothGattService overrides.
43   virtual void AddObserver(
44       device::BluetoothGattService::Observer* observer) OVERRIDE;
45   virtual void RemoveObserver(
46       device::BluetoothGattService::Observer* observer) OVERRIDE;
47   virtual std::string GetIdentifier() const OVERRIDE;
48   virtual device::BluetoothUUID GetUUID() const OVERRIDE;
49   virtual bool IsLocal() const OVERRIDE;
50   virtual bool IsPrimary() const OVERRIDE;
51   virtual device::BluetoothDevice* GetDevice() const OVERRIDE;
52   virtual std::vector<device::BluetoothGattCharacteristic*>
53       GetCharacteristics() const OVERRIDE;
54   virtual std::vector<device::BluetoothGattService*>
55       GetIncludedServices() const OVERRIDE;
56   virtual device::BluetoothGattCharacteristic* GetCharacteristic(
57       const std::string& identifier) const OVERRIDE;
58   virtual bool AddCharacteristic(
59       device::BluetoothGattCharacteristic* characteristic) OVERRIDE;
60   virtual bool AddIncludedService(
61       device::BluetoothGattService* service) OVERRIDE;
62   virtual void Register(const base::Closure& callback,
63                         const ErrorCallback& error_callback) OVERRIDE;
64   virtual void Unregister(const base::Closure& callback,
65                           const ErrorCallback& error_callback) OVERRIDE;
66 
67   // Object path of the underlying service.
object_path()68   const dbus::ObjectPath& object_path() const { return object_path_; }
69 
70   // Returns the adapter associated with this service.
71   scoped_refptr<device::BluetoothAdapter> GetAdapter() const;
72 
73   // Notifies its observers that the GATT service has changed. This is mainly
74   // used by BluetoothRemoteGattCharacteristicChromeOS instances to notify
75   // service observers when characteristic descriptors get added and removed.
76   void NotifyServiceChanged();
77 
78   // Notifies its observers that the value of a characteristic has changed.
79   // Called by BluetoothRemoteGattCharacteristicChromeOS instances to notify
80   // service observers when their cached value is updated after a successful
81   // read request or when a "ValueUpdated" signal is received.
82   void NotifyCharacteristicValueChanged(
83       BluetoothRemoteGattCharacteristicChromeOS* characteristic,
84       const std::vector<uint8>& value);
85 
86   // Notifies its observers that a descriptor |descriptor| belonging to
87   // characteristic |characteristic| has been added or removed. This is used
88   // by BluetoothRemoteGattCharacteristicChromeOS instances to notify service
89   // observers when characteristic descriptors get added and removed. If |added|
90   // is true, an "Added" event will be sent. Otherwise, a "Removed" event will
91   // be sent.
92   void NotifyDescriptorAddedOrRemoved(
93       BluetoothRemoteGattCharacteristicChromeOS* characteristic,
94       BluetoothRemoteGattDescriptorChromeOS* descriptor,
95       bool added);
96 
97   // Notifies its observers that the value of a descriptor has changed. Called
98   // by BluetoothRemoteGattDescriptorChromeOS instances to notify service
99   // observers when their cached value gets updated after a read request.
100   void NotifyDescriptorValueChanged(
101       BluetoothRemoteGattCharacteristicChromeOS* characteristic,
102       BluetoothRemoteGattDescriptorChromeOS* descriptor,
103       const std::vector<uint8>& value);
104 
105  private:
106   friend class BluetoothDeviceChromeOS;
107 
108   BluetoothRemoteGattServiceChromeOS(BluetoothAdapterChromeOS* adapter,
109                                      BluetoothDeviceChromeOS* device,
110                                      const dbus::ObjectPath& object_path);
111   virtual ~BluetoothRemoteGattServiceChromeOS();
112 
113   // BluetoothGattServiceClient::Observer override.
114   virtual void GattServicePropertyChanged(
115       const dbus::ObjectPath& object_path,
116       const std::string& property_name) OVERRIDE;
117 
118   // BluetoothGattCharacteristicClient::Observer override.
119   virtual void GattCharacteristicAdded(
120       const dbus::ObjectPath& object_path) OVERRIDE;
121   virtual void GattCharacteristicRemoved(
122       const dbus::ObjectPath& object_path) OVERRIDE;
123   virtual void GattCharacteristicPropertyChanged(
124       const dbus::ObjectPath& object_path,
125       const std::string& property_name) OVERRIDE;
126 
127   // Object path of the GATT service.
128   dbus::ObjectPath object_path_;
129 
130   // List of observers interested in event notifications from us.
131   ObserverList<device::BluetoothGattService::Observer> observers_;
132 
133   // The adapter associated with this service. It's ok to store a raw pointer
134   // here since |adapter_| indirectly owns this instance.
135   BluetoothAdapterChromeOS* adapter_;
136 
137   // The device this GATT service belongs to. It's ok to store a raw pointer
138   // here since |device_| owns this instance.
139   BluetoothDeviceChromeOS* device_;
140 
141   // Mapping from GATT characteristic object paths to characteristic objects.
142   // owned by this service. Since the Chrome OS implementation uses object
143   // paths as unique identifiers, we also use this mapping to return
144   // characteristics by identifier.
145   typedef std::map<dbus::ObjectPath, BluetoothRemoteGattCharacteristicChromeOS*>
146       CharacteristicMap;
147   CharacteristicMap characteristics_;
148 
149   // Note: This should remain the last member so it'll be destroyed and
150   // invalidate its weak pointers before any other members are destroyed.
151   base::WeakPtrFactory<BluetoothRemoteGattServiceChromeOS> weak_ptr_factory_;
152 
153   DISALLOW_COPY_AND_ASSIGN(BluetoothRemoteGattServiceChromeOS);
154 };
155 
156 }  // namespace chromeos
157 
158 #endif  // DEVICE_BLUETOOTH_BLUETOOTH_REMOTE_GATT_SERVICE_CHROMEOS_H_
159