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_
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"
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
{
28 struct Properties
: public BluetoothGattCharacteristicClient::Properties
{
29 explicit Properties(const PropertyChangedCallback
& callback
);
30 ~Properties() override
;
32 // dbus::PropertySet override
33 void Get(dbus::PropertyBase
* property
,
34 dbus::PropertySet::GetCallback callback
) override
;
35 void GetAll() override
;
36 void Set(dbus::PropertyBase
* property
,
37 dbus::PropertySet::SetCallback callback
) override
;
40 FakeBluetoothGattCharacteristicClient();
41 ~FakeBluetoothGattCharacteristicClient() override
;
43 // DBusClient override.
44 void Init(dbus::Bus
* bus
) override
;
46 // BluetoothGattCharacteristicClient overrides.
47 void AddObserver(Observer
* observer
) override
;
48 void RemoveObserver(Observer
* observer
) override
;
49 std::vector
<dbus::ObjectPath
> GetCharacteristics() override
;
50 Properties
* GetProperties(const dbus::ObjectPath
& object_path
) override
;
51 void ReadValue(const dbus::ObjectPath
& object_path
,
52 const ValueCallback
& callback
,
53 const ErrorCallback
& error_callback
) override
;
54 void WriteValue(const dbus::ObjectPath
& object_path
,
55 const std::vector
<uint8
>& value
,
56 const base::Closure
& callback
,
57 const ErrorCallback
& error_callback
) override
;
58 void StartNotify(const dbus::ObjectPath
& object_path
,
59 const base::Closure
& callback
,
60 const ErrorCallback
& error_callback
) override
;
61 void StopNotify(const dbus::ObjectPath
& object_path
,
62 const base::Closure
& callback
,
63 const ErrorCallback
& error_callback
) override
;
65 // Makes the group of characteristics belonging to a particular GATT based
66 // profile available under the GATT service with object path |service_path|.
67 // Characteristic paths are hierarchical to service paths.
68 void ExposeHeartRateCharacteristics(const dbus::ObjectPath
& service_path
);
69 void HideHeartRateCharacteristics();
71 // Returns whether or not the heart rate characteristics are visible and
72 // performs the appropriate assertions.
73 bool IsHeartRateVisible() const;
75 // Makes this characteristic client really slow.
76 // So slow, that it is guaranteed that |requests| requests will
77 // come in while the client is doing the previous request.
78 // Setting |requests| to zero will cause all delayed actions to
79 // complete immediately.
80 void SetExtraProcessing(size_t requests
);
82 size_t GetExtraProcessing() const;
84 // Sets whether the client is authorized or not.
85 // Defaults to authorized.
86 void SetAuthorized(bool authorized
) { authorized_
= authorized
; }
88 // Get the current Authorization state.
89 bool IsAuthorized() const { return authorized_
; }
91 // Whether the client is Authenticated
92 // Defaults to authenticated.
93 void SetAuthenticated(bool authenticated
) { authenticated_
= authenticated
; }
95 // Get the current Authenticated state.
96 bool IsAuthenticated() const { return authenticated_
; }
98 // Returns the current object paths of exposed characteristics. If the
99 // characteristic is not visible, returns an invalid empty path.
100 dbus::ObjectPath
GetHeartRateMeasurementPath() const;
101 dbus::ObjectPath
GetBodySensorLocationPath() const;
102 dbus::ObjectPath
GetHeartRateControlPointPath() const;
104 // Object path components and UUIDs of GATT characteristics.
105 // Heart Rate Service:
106 static const char kHeartRateMeasurementPathComponent
[];
107 static const char kHeartRateMeasurementUUID
[];
108 static const char kBodySensorLocationPathComponent
[];
109 static const char kBodySensorLocationUUID
[];
110 static const char kHeartRateControlPointPathComponent
[];
111 static const char kHeartRateControlPointUUID
[];
114 // Property callback passed when we create Properties structures.
115 void OnPropertyChanged(const dbus::ObjectPath
& object_path
,
116 const std::string
& property_name
);
118 // Notifies observers.
119 void NotifyCharacteristicAdded(const dbus::ObjectPath
& object_path
);
120 void NotifyCharacteristicRemoved(const dbus::ObjectPath
& object_path
);
122 // Schedules a heart rate measurement value change, if the heart rate
123 // characteristics are visible.
124 void ScheduleHeartRateMeasurementValueChange();
126 // Returns a random Heart Rate Measurement value. All the fields of the value
127 // are populated according to the the fake behavior. The measurement value
128 // is a random value within a reasonable range.
129 std::vector
<uint8
> GetHeartRateMeasurementValue();
131 // Callback that executes a delayed ReadValue action by updating the
132 // appropriate "Value" property and invoking the ValueCallback.
133 void DelayedReadValueCallback(const dbus::ObjectPath
& object_path
,
134 const ValueCallback
& callback
,
135 const std::vector
<uint8_t>& value
);
137 // If true, characteristics of the Heart Rate Service are visible. Use
138 // IsHeartRateVisible() to check the value.
139 bool heart_rate_visible_
;
141 // If true, the client is authorized to read and write.
144 // If true, the client is authenticated.
147 // Total calories burned, used for the Heart Rate Measurement characteristic.
148 uint16 calories_burned_
;
150 // Static properties returned for simulated characteristics for the Heart
151 // Rate Service. These pointers are not NULL only if the characteristics are
153 scoped_ptr
<Properties
> heart_rate_measurement_properties_
;
154 scoped_ptr
<Properties
> body_sensor_location_properties_
;
155 scoped_ptr
<Properties
> heart_rate_control_point_properties_
;
157 // Object paths of the exposed characteristics. If a characteristic is not
158 // exposed, these will be empty.
159 std::string heart_rate_measurement_path_
;
160 std::string heart_rate_measurement_ccc_desc_path_
;
161 std::string body_sensor_location_path_
;
162 std::string heart_rate_control_point_path_
;
164 // Number of extra requests that need to come in simulating slowness.
165 size_t extra_requests_
;
167 // Current countdowns for extra requests for various actions.
168 struct DelayedCallback
{
170 DelayedCallback(base::Closure callback
, size_t delay
);
173 base::Closure callback_
;
177 // Map of delayed callbacks.
178 std::map
<std::string
, DelayedCallback
*> action_extra_requests_
;
180 // List of observers interested in event notifications from us.
181 base::ObserverList
<Observer
> observers_
;
183 // Weak pointer factory for generating 'this' pointers that might live longer
185 // Note: This should remain the last member so it'll be destroyed and
186 // invalidate its weak pointers before any other members are destroyed.
187 base::WeakPtrFactory
<FakeBluetoothGattCharacteristicClient
>
190 DISALLOW_COPY_AND_ASSIGN(FakeBluetoothGattCharacteristicClient
);
193 } // namespace chromeos
195 #endif // CHROMEOS_DBUS_FAKE_BLUETOOTH_GATT_CHARACTERISTIC_CLIENT_H_