summaryrefslogtreecommitdiffstats
path: root/chromeos/dbus/fake_bluetooth_gatt_characteristic_client.h
blob: dc1eab39d96b0a21f04206cc458f82a5c43b5d4b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROMEOS_DBUS_FAKE_BLUETOOTH_GATT_CHARACTERISTIC_CLIENT_H_
#define CHROMEOS_DBUS_FAKE_BLUETOOTH_GATT_CHARACTERISTIC_CLIENT_H_

#include <string>
#include <vector>

#include "base/basictypes.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/observer_list.h"
#include "chromeos/chromeos_export.h"
#include "chromeos/dbus/bluetooth_gatt_characteristic_client.h"
#include "dbus/object_path.h"
#include "dbus/property.h"

namespace chromeos {

// FakeBluetoothGattCharacteristicClient simulates the behavior of the
// Bluetooth Daemon GATT characteristic objects and is used in test cases in
// place of a mock and on the Linux desktop.
class CHROMEOS_EXPORT FakeBluetoothGattCharacteristicClient
    : public BluetoothGattCharacteristicClient {
 public:
  struct Properties : public BluetoothGattCharacteristicClient::Properties {
    explicit Properties(const PropertyChangedCallback& callback);
    ~Properties() override;

    // dbus::PropertySet override
    void Get(dbus::PropertyBase* property,
             dbus::PropertySet::GetCallback callback) override;
    void GetAll() override;
    void Set(dbus::PropertyBase* property,
             dbus::PropertySet::SetCallback callback) override;
  };

  FakeBluetoothGattCharacteristicClient();
  ~FakeBluetoothGattCharacteristicClient() override;

  // DBusClient override.
  void Init(dbus::Bus* bus) override;

  // BluetoothGattCharacteristicClient overrides.
  void AddObserver(Observer* observer) override;
  void RemoveObserver(Observer* observer) override;
  std::vector<dbus::ObjectPath> GetCharacteristics() override;
  Properties* GetProperties(const dbus::ObjectPath& object_path) override;
  void ReadValue(const dbus::ObjectPath& object_path,
                 const ValueCallback& callback,
                 const ErrorCallback& error_callback) override;
  void WriteValue(const dbus::ObjectPath& object_path,
                  const std::vector<uint8>& value,
                  const base::Closure& callback,
                  const ErrorCallback& error_callback) override;
  void StartNotify(const dbus::ObjectPath& object_path,
                   const base::Closure& callback,
                   const ErrorCallback& error_callback) override;
  void StopNotify(const dbus::ObjectPath& object_path,
                  const base::Closure& callback,
                  const ErrorCallback& error_callback) override;

  // Makes the group of characteristics belonging to a particular GATT based
  // profile available under the GATT service with object path |service_path|.
  // Characteristic paths are hierarchical to service paths.
  void ExposeHeartRateCharacteristics(const dbus::ObjectPath& service_path);
  void HideHeartRateCharacteristics();

  // Returns whether or not the heart rate characteristics are visible and
  // performs the appropriate assertions.
  bool IsHeartRateVisible() const;

  // Makes this characteristic client really slow.
  // So slow, that it is guaranteed that |requests| requests will
  // come in while the client is doing the previous request.
  // Setting |requests| to zero will cause all delayed actions to
  // complete immediately.
  void SetExtraProcessing(size_t requests);

  size_t GetExtraProcessing() const;

  // Sets whether the client is authorized or not.
  // Defaults to authorized.
  void SetAuthorized(bool authorized) { authorized_ = authorized; }

  // Get the current Authorization state.
  bool IsAuthorized() const { return authorized_; }

  // Whether the client is Authenticated
  // Defaults to authenticated.
  void SetAuthenticated(bool authenticated) { authenticated_ = authenticated; }

  // Get the current Authenticated state.
  bool IsAuthenticated() const { return authenticated_; }

  // Returns the current object paths of exposed characteristics. If the
  // characteristic is not visible, returns an invalid empty path.
  dbus::ObjectPath GetHeartRateMeasurementPath() const;
  dbus::ObjectPath GetBodySensorLocationPath() const;
  dbus::ObjectPath GetHeartRateControlPointPath() const;

  // Object path components and UUIDs of GATT characteristics.
  // Heart Rate Service:
  static const char kHeartRateMeasurementPathComponent[];
  static const char kHeartRateMeasurementUUID[];
  static const char kBodySensorLocationPathComponent[];
  static const char kBodySensorLocationUUID[];
  static const char kHeartRateControlPointPathComponent[];
  static const char kHeartRateControlPointUUID[];

 private:
  // Property callback passed when we create Properties structures.
  void OnPropertyChanged(const dbus::ObjectPath& object_path,
                         const std::string& property_name);

  // Notifies observers.
  void NotifyCharacteristicAdded(const dbus::ObjectPath& object_path);
  void NotifyCharacteristicRemoved(const dbus::ObjectPath& object_path);

  // Schedules a heart rate measurement value change, if the heart rate
  // characteristics are visible.
  void ScheduleHeartRateMeasurementValueChange();

  // Returns a random Heart Rate Measurement value. All the fields of the value
  // are populated according to the the fake behavior. The measurement value
  // is a random value within a reasonable range.
  std::vector<uint8> GetHeartRateMeasurementValue();

  // Callback that executes a delayed ReadValue action by updating the
  // appropriate "Value" property and invoking the ValueCallback.
  void DelayedReadValueCallback(const dbus::ObjectPath& object_path,
                                const ValueCallback& callback,
                                const std::vector<uint8_t>& value);

  // If true, characteristics of the Heart Rate Service are visible. Use
  // IsHeartRateVisible() to check the value.
  bool heart_rate_visible_;

  // If true, the client is authorized to read and write.
  bool authorized_;

  // If true, the client is authenticated.
  bool authenticated_;

  // Total calories burned, used for the Heart Rate Measurement characteristic.
  uint16 calories_burned_;

  // Static properties returned for simulated characteristics for the Heart
  // Rate Service. These pointers are not NULL only if the characteristics are
  // actually exposed.
  scoped_ptr<Properties> heart_rate_measurement_properties_;
  scoped_ptr<Properties> body_sensor_location_properties_;
  scoped_ptr<Properties> heart_rate_control_point_properties_;

  // Object paths of the exposed characteristics. If a characteristic is not
  // exposed, these will be empty.
  std::string heart_rate_measurement_path_;
  std::string heart_rate_measurement_ccc_desc_path_;
  std::string body_sensor_location_path_;
  std::string heart_rate_control_point_path_;

  // Number of extra requests that need to come in simulating slowness.
  size_t extra_requests_;

  // Current countdowns for extra requests for various actions.
  struct DelayedCallback {
   public:
    DelayedCallback(base::Closure callback, size_t delay);
    ~DelayedCallback();

    base::Closure callback_;
    size_t delay_;
  };

  // Map of delayed callbacks.
  std::map<std::string, DelayedCallback*> action_extra_requests_;

  // List of observers interested in event notifications from us.
  base::ObserverList<Observer> observers_;

  // Weak pointer factory for generating 'this' pointers that might live longer
  // than we do.
  // Note: This should remain the last member so it'll be destroyed and
  // invalidate its weak pointers before any other members are destroyed.
  base::WeakPtrFactory<FakeBluetoothGattCharacteristicClient> weak_ptr_factory_;

  DISALLOW_COPY_AND_ASSIGN(FakeBluetoothGattCharacteristicClient);
};

}  // namespace chromeos

#endif  // CHROMEOS_DBUS_FAKE_BLUETOOTH_GATT_CHARACTERISTIC_CLIENT_H_