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