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 CHROMEOS_DBUS_FAKE_BLUETOOTH_GATT_CHARACTERISTIC_CLIENT_H_
6#define CHROMEOS_DBUS_FAKE_BLUETOOTH_GATT_CHARACTERISTIC_CLIENT_H_
7
8#include <string>
9#include <vector>
10
11#include "base/basictypes.h"
12#include "base/memory/scoped_ptr.h"
13#include "base/memory/weak_ptr.h"
14#include "base/observer_list.h"
15#include "chromeos/chromeos_export.h"
16#include "chromeos/dbus/bluetooth_gatt_characteristic_client.h"
17#include "dbus/object_path.h"
18#include "dbus/property.h"
19
20namespace chromeos {
21
22// FakeBluetoothGattCharacteristicClient simulates the behavior of the
23// Bluetooth Daemon GATT characteristic objects and is used in test cases in
24// place of a mock and on the Linux desktop.
25class CHROMEOS_EXPORT FakeBluetoothGattCharacteristicClient
26    : public BluetoothGattCharacteristicClient {
27 public:
28  struct Properties : public BluetoothGattCharacteristicClient::Properties {
29    explicit Properties(const PropertyChangedCallback& callback);
30    virtual ~Properties();
31
32    // dbus::PropertySet override
33    virtual void Get(dbus::PropertyBase* property,
34                     dbus::PropertySet::GetCallback callback) OVERRIDE;
35    virtual void GetAll() OVERRIDE;
36    virtual void Set(dbus::PropertyBase* property,
37                     dbus::PropertySet::SetCallback callback) OVERRIDE;
38  };
39
40  FakeBluetoothGattCharacteristicClient();
41  virtual ~FakeBluetoothGattCharacteristicClient();
42
43  // DBusClient override.
44  virtual void Init(dbus::Bus* bus) OVERRIDE;
45
46  // BluetoothGattCharacteristicClient overrides.
47  virtual void AddObserver(Observer* observer) OVERRIDE;
48  virtual void RemoveObserver(Observer* observer) OVERRIDE;
49  virtual std::vector<dbus::ObjectPath> GetCharacteristics() OVERRIDE;
50  virtual Properties* GetProperties(const dbus::ObjectPath& object_path)
51      OVERRIDE;
52  virtual void ReadValue(const dbus::ObjectPath& object_path,
53                         const ValueCallback& callback,
54                         const ErrorCallback& error_callback) OVERRIDE;
55  virtual void WriteValue(const dbus::ObjectPath& object_path,
56                          const std::vector<uint8>& value,
57                          const base::Closure& callback,
58                          const ErrorCallback& error_callback) OVERRIDE;
59  virtual void StartNotify(const dbus::ObjectPath& object_path,
60                           const base::Closure& callback,
61                           const ErrorCallback& error_callback) OVERRIDE;
62  virtual void StopNotify(const dbus::ObjectPath& object_path,
63                          const base::Closure& callback,
64                          const ErrorCallback& error_callback) OVERRIDE;
65
66  // Makes the group of characteristics belonging to a particular GATT based
67  // profile available under the GATT service with object path |service_path|.
68  // Characteristic paths are hierarchical to service paths.
69  void ExposeHeartRateCharacteristics(const dbus::ObjectPath& service_path);
70  void HideHeartRateCharacteristics();
71
72  // Returns whether or not the heart rate characteristics are visible and
73  // performs the appropriate assertions.
74  bool IsHeartRateVisible() const;
75
76  // Returns the current object paths of exposed characteristics. If the
77  // characteristic is not visible, returns an invalid empty path.
78  dbus::ObjectPath GetHeartRateMeasurementPath() const;
79  dbus::ObjectPath GetBodySensorLocationPath() const;
80  dbus::ObjectPath GetHeartRateControlPointPath() const;
81
82  // Object path components and UUIDs of GATT characteristics.
83  // Heart Rate Service:
84  static const char kHeartRateMeasurementPathComponent[];
85  static const char kHeartRateMeasurementUUID[];
86  static const char kBodySensorLocationPathComponent[];
87  static const char kBodySensorLocationUUID[];
88  static const char kHeartRateControlPointPathComponent[];
89  static const char kHeartRateControlPointUUID[];
90
91 private:
92  // Property callback passed when we create Properties structures.
93  void OnPropertyChanged(const dbus::ObjectPath& object_path,
94                         const std::string& property_name);
95
96  // Notifies observers.
97  void NotifyCharacteristicAdded(const dbus::ObjectPath& object_path);
98  void NotifyCharacteristicRemoved(const dbus::ObjectPath& object_path);
99
100  // Schedules a heart rate measurement value change, if the heart rate
101  // characteristics are visible.
102  void ScheduleHeartRateMeasurementValueChange();
103
104  // Returns a random Heart Rate Measurement value. All the fields of the value
105  // are populated according to the the fake behavior. The measurement value
106  // is a random value within a reasonable range.
107  std::vector<uint8> GetHeartRateMeasurementValue();
108
109  // If true, characteristics of the Heart Rate Service are visible. Use
110  // IsHeartRateVisible() to check the value.
111  bool heart_rate_visible_;
112
113  // Total calories burned, used for the Heart Rate Measurement characteristic.
114  uint16 calories_burned_;
115
116  // Static properties returned for simulated characteristics for the Heart
117  // Rate Service. These pointers are not NULL only if the characteristics are
118  // actually exposed.
119  scoped_ptr<Properties> heart_rate_measurement_properties_;
120  scoped_ptr<Properties> body_sensor_location_properties_;
121  scoped_ptr<Properties> heart_rate_control_point_properties_;
122
123  // Object paths of the exposed characteristics. If a characteristic is not
124  // exposed, these will be empty.
125  std::string heart_rate_measurement_path_;
126  std::string heart_rate_measurement_ccc_desc_path_;
127  std::string body_sensor_location_path_;
128  std::string heart_rate_control_point_path_;
129
130  // List of observers interested in event notifications from us.
131  ObserverList<Observer> observers_;
132
133  // Weak pointer factory for generating 'this' pointers that might live longer
134  // than we do.
135  // Note: This should remain the last member so it'll be destroyed and
136  // invalidate its weak pointers before any other members are destroyed.
137  base::WeakPtrFactory<FakeBluetoothGattCharacteristicClient>
138      weak_ptr_factory_;
139
140  DISALLOW_COPY_AND_ASSIGN(FakeBluetoothGattCharacteristicClient);
141};
142
143}  // namespace chromeos
144
145#endif  // CHROMEOS_DBUS_FAKE_BLUETOOTH_GATT_CHARACTERISTIC_CLIENT_H_
146