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 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
)
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
;
60 // Makes the group of characteristics belonging to a particular GATT based
61 // profile available under the GATT service with object path |service_path|.
62 // Characteristic paths are hierarchical to service paths.
63 void ExposeHeartRateCharacteristics(const dbus::ObjectPath
& service_path
);
64 void HideHeartRateCharacteristics();
66 // Returns whether or not the heart rate characteristics are visible and
67 // performs the appropriate assertions.
68 bool IsHeartRateVisible() const;
70 // Returns the current object paths of exposed characteristics. If the
71 // characteristic is not visible, returns an invalid empty path.
72 dbus::ObjectPath
GetHeartRateMeasurementPath() const;
73 dbus::ObjectPath
GetBodySensorLocationPath() const;
74 dbus::ObjectPath
GetHeartRateControlPointPath() const;
76 // Object path components and UUIDs of GATT characteristics.
77 // Heart Rate Service:
78 static const char kHeartRateMeasurementPathComponent
[];
79 static const char kHeartRateMeasurementUUID
[];
80 static const char kBodySensorLocationPathComponent
[];
81 static const char kBodySensorLocationUUID
[];
82 static const char kHeartRateControlPointPathComponent
[];
83 static const char kHeartRateControlPointUUID
[];
86 // Property callback passed when we create Properties structures.
87 void OnPropertyChanged(const dbus::ObjectPath
& object_path
,
88 const std::string
& property_name
);
90 // Notifies observers.
91 void NotifyCharacteristicAdded(const dbus::ObjectPath
& object_path
);
92 void NotifyCharacteristicRemoved(const dbus::ObjectPath
& object_path
);
94 // Schedules a heart rate measurement value change, if the heart rate
95 // characteristics are visible.
96 void ScheduleHeartRateMeasurementValueChange();
98 // Returns a random Heart Rate Measurement value. All the fields of the value
99 // are populated according to the the fake behavior. The measurement value
100 // is a random value within a reasonable range.
101 std::vector
<uint8
> GetHeartRateMeasurementValue();
103 // If true, characteristics of the Heart Rate Service are visible. Use
104 // IsHeartRateVisible() to check the value.
105 bool heart_rate_visible_
;
107 // Total calories burned, used for the Heart Rate Measurement characteristic.
108 uint16 calories_burned_
;
110 // Static properties returned for simulated characteristics for the Heart
111 // Rate Service. These pointers are not NULL only if the characteristics are
113 scoped_ptr
<Properties
> heart_rate_measurement_properties_
;
114 scoped_ptr
<Properties
> body_sensor_location_properties_
;
115 scoped_ptr
<Properties
> heart_rate_control_point_properties_
;
117 // Object paths of the exposed characteristics. If a characteristic is not
118 // exposed, these will be empty.
119 std::string heart_rate_measurement_path_
;
120 std::string heart_rate_measurement_ccc_desc_path_
;
121 std::string body_sensor_location_path_
;
122 std::string heart_rate_control_point_path_
;
124 // List of observers interested in event notifications from us.
125 ObserverList
<Observer
> observers_
;
127 // Weak pointer factory for generating 'this' pointers that might live longer
129 // Note: This should remain the last member so it'll be destroyed and
130 // invalidate its weak pointers before any other members are destroyed.
131 base::WeakPtrFactory
<FakeBluetoothGattCharacteristicClient
>
134 DISALLOW_COPY_AND_ASSIGN(FakeBluetoothGattCharacteristicClient
);
137 } // namespace chromeos
139 #endif // CHROMEOS_DBUS_FAKE_BLUETOOTH_GATT_CHARACTERISTIC_CLIENT_H_