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