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_SERVICE_CLIENT_H_
6 #define CHROMEOS_DBUS_FAKE_BLUETOOTH_GATT_SERVICE_CLIENT_H_
11 #include "base/memory/scoped_ptr.h"
12 #include "base/memory/weak_ptr.h"
13 #include "base/observer_list.h"
14 #include "chromeos/chromeos_export.h"
15 #include "chromeos/dbus/bluetooth_gatt_service_client.h"
16 #include "dbus/object_path.h"
17 #include "dbus/property.h"
21 // FakeBluetoothGattServiceClient simulates the behavior of the Bluetooth Daemon
22 // GATT service objects and is used in test cases in place of a mock and on the
24 class CHROMEOS_EXPORT FakeBluetoothGattServiceClient
25 : public BluetoothGattServiceClient
{
27 struct Properties
: public BluetoothGattServiceClient::Properties
{
28 explicit Properties(const PropertyChangedCallback
& callback
);
29 ~Properties() override
;
31 // dbus::PropertySet override
32 void Get(dbus::PropertyBase
* property
,
33 dbus::PropertySet::GetCallback callback
) override
;
34 void GetAll() override
;
35 void Set(dbus::PropertyBase
* property
,
36 dbus::PropertySet::SetCallback callback
) override
;
39 FakeBluetoothGattServiceClient();
40 ~FakeBluetoothGattServiceClient() override
;
42 // DBusClient override.
43 void Init(dbus::Bus
* bus
) override
;
45 // BluetoothGattServiceClient overrides.
46 void AddObserver(Observer
* observer
) override
;
47 void RemoveObserver(Observer
* observer
) override
;
48 std::vector
<dbus::ObjectPath
> GetServices() override
;
49 Properties
* GetProperties(const dbus::ObjectPath
& object_path
) override
;
51 // Makes a service visible for device with object path |device_path|. Note
52 // that only one instance of a specific service is simulated at a time. Hence,
53 // this method will fail, if the service is already visible.
54 void ExposeHeartRateService(const dbus::ObjectPath
& device_path
);
55 void HideHeartRateService();
57 // Returns whether or not the Heart Rate Service is visible.
58 bool IsHeartRateVisible() const;
60 // Returns the current object path of the visible Heart Rate service. If the
61 // service is not visible, returns an invalid empty path.
62 dbus::ObjectPath
GetHeartRateServicePath() const;
64 // Final object path components and the corresponding UUIDs of the GATT
65 // services that we emulate. Service paths are hierarchical to Bluetooth
66 // device paths, so if the path component is "service0000", and the device
67 // path is "/org/foo/device0", the service path is
68 // "/org/foo/device0/service0000".
69 static const char kHeartRateServicePathComponent
[];
70 static const char kHeartRateServiceUUID
[];
73 // Property callback passed when we create Properties structures.
74 void OnPropertyChanged(const dbus::ObjectPath
& object_path
,
75 const std::string
& property_name
);
77 // Notifies observers.
78 void NotifyServiceAdded(const dbus::ObjectPath
& object_path
);
79 void NotifyServiceRemoved(const dbus::ObjectPath
& object_path
);
81 // Tells FakeBluetoothGattCharacteristicClient to expose GATT characteristics.
82 // This is scheduled from ExposeHeartRateService to simulate asynchronous
83 // retrieval of characteristics. If the Heart Rate Service is hidden at the
84 // time this method is called, then it does nothing.
85 void ExposeHeartRateCharacteristics();
87 // Static properties we return. As long as a service is exposed, this will be
88 // non-null. Otherwise it will be null.
89 scoped_ptr
<Properties
> heart_rate_service_properties_
;
90 std::string heart_rate_service_path_
;
92 // List of observers interested in event notifications from us.
93 base::ObserverList
<Observer
> observers_
;
95 // Weak pointer factory for generating 'this' pointers that might live longer
97 // Note: This should remain the last member so it'll be destroyed and
98 // invalidate its weak pointers before any other members are destroyed.
99 base::WeakPtrFactory
<FakeBluetoothGattServiceClient
> weak_ptr_factory_
;
101 DISALLOW_COPY_AND_ASSIGN(FakeBluetoothGattServiceClient
);
104 } // namespace chromeos
106 #endif // CHROMEOS_DBUS_FAKE_BLUETOOTH_GATT_SERVICE_CLIENT_H_