Add include.
[chromium-blink-merge.git] / chromeos / dbus / fake_bluetooth_gatt_characteristic_client.h
blobcba953be0c6403c772be2052bcd745a7acf05afa
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 CHROMEOS_DBUS_FAKE_BLUETOOTH_GATT_CHARACTERISTIC_CLIENT_H_
6 #define CHROMEOS_DBUS_FAKE_BLUETOOTH_GATT_CHARACTERISTIC_CLIENT_H_
8 #include <string>
9 #include <vector>
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"
20 namespace chromeos {
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.
25 class CHROMEOS_EXPORT FakeBluetoothGattCharacteristicClient
26 : public BluetoothGattCharacteristicClient {
27 public:
28 struct Properties : public BluetoothGattCharacteristicClient::Properties {
29 explicit Properties(const PropertyChangedCallback& callback);
30 virtual ~Properties();
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;
40 FakeBluetoothGattCharacteristicClient();
41 virtual ~FakeBluetoothGattCharacteristicClient();
43 // DBusClient override.
44 virtual void Init(dbus::Bus* bus) override;
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;
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();
72 // Returns whether or not the heart rate characteristics are visible and
73 // performs the appropriate assertions.
74 bool IsHeartRateVisible() const;
76 // Makes this characteristic client really slow.
77 // So slow, that it is guaranteed that |requests| requests will
78 // come in while the client is doing the previous request.
79 // Setting |requests| to zero will cause all delayed actions to
80 // complete immediately.
81 void SetExtraProcessing(size_t requests);
83 size_t GetExtraProcessing() const;
85 // Sets whether the client is authorized or not.
86 // Defaults to authorized.
87 void SetAuthorized(bool authorized) { authorized_ = authorized; }
89 // Get the current Authorization state.
90 bool IsAuthorized() const { return authorized_; }
92 // Whether the client is Authenticated
93 // Defaults to authenticated.
94 void SetAuthenticated(bool authenticated) { authenticated_ = authenticated; }
96 // Get the current Authenticated state.
97 bool IsAuthenticated() const { return authenticated_; }
99 // Returns the current object paths of exposed characteristics. If the
100 // characteristic is not visible, returns an invalid empty path.
101 dbus::ObjectPath GetHeartRateMeasurementPath() const;
102 dbus::ObjectPath GetBodySensorLocationPath() const;
103 dbus::ObjectPath GetHeartRateControlPointPath() const;
105 // Object path components and UUIDs of GATT characteristics.
106 // Heart Rate Service:
107 static const char kHeartRateMeasurementPathComponent[];
108 static const char kHeartRateMeasurementUUID[];
109 static const char kBodySensorLocationPathComponent[];
110 static const char kBodySensorLocationUUID[];
111 static const char kHeartRateControlPointPathComponent[];
112 static const char kHeartRateControlPointUUID[];
114 private:
115 // Property callback passed when we create Properties structures.
116 void OnPropertyChanged(const dbus::ObjectPath& object_path,
117 const std::string& property_name);
119 // Notifies observers.
120 void NotifyCharacteristicAdded(const dbus::ObjectPath& object_path);
121 void NotifyCharacteristicRemoved(const dbus::ObjectPath& object_path);
123 // Schedules a heart rate measurement value change, if the heart rate
124 // characteristics are visible.
125 void ScheduleHeartRateMeasurementValueChange();
127 // Returns a random Heart Rate Measurement value. All the fields of the value
128 // are populated according to the the fake behavior. The measurement value
129 // is a random value within a reasonable range.
130 std::vector<uint8> GetHeartRateMeasurementValue();
132 // If true, characteristics of the Heart Rate Service are visible. Use
133 // IsHeartRateVisible() to check the value.
134 bool heart_rate_visible_;
136 // If true, the client is authorized to read and write.
137 bool authorized_;
139 // If true, the client is authenticated.
140 bool authenticated_;
142 // Total calories burned, used for the Heart Rate Measurement characteristic.
143 uint16 calories_burned_;
145 // Static properties returned for simulated characteristics for the Heart
146 // Rate Service. These pointers are not NULL only if the characteristics are
147 // actually exposed.
148 scoped_ptr<Properties> heart_rate_measurement_properties_;
149 scoped_ptr<Properties> body_sensor_location_properties_;
150 scoped_ptr<Properties> heart_rate_control_point_properties_;
152 // Object paths of the exposed characteristics. If a characteristic is not
153 // exposed, these will be empty.
154 std::string heart_rate_measurement_path_;
155 std::string heart_rate_measurement_ccc_desc_path_;
156 std::string body_sensor_location_path_;
157 std::string heart_rate_control_point_path_;
159 // Number of extra requests that need to come in simulating slowness.
160 size_t extra_requests_;
162 // Current countdowns for extra requests for various actions.
163 struct DelayedCallback {
164 public:
165 DelayedCallback(base::Closure callback, size_t delay);
166 ~DelayedCallback();
168 base::Closure callback_;
169 size_t delay_;
172 // Map of delayed callbacks.
173 std::map<std::string, DelayedCallback*> action_extra_requests_;
175 // List of observers interested in event notifications from us.
176 ObserverList<Observer> observers_;
178 // Weak pointer factory for generating 'this' pointers that might live longer
179 // than we do.
180 // Note: This should remain the last member so it'll be destroyed and
181 // invalidate its weak pointers before any other members are destroyed.
182 base::WeakPtrFactory<FakeBluetoothGattCharacteristicClient>
183 weak_ptr_factory_;
185 DISALLOW_COPY_AND_ASSIGN(FakeBluetoothGattCharacteristicClient);
188 } // namespace chromeos
190 #endif // CHROMEOS_DBUS_FAKE_BLUETOOTH_GATT_CHARACTERISTIC_CLIENT_H_