• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_BLUETOOTH_GATT_CHARACTERISTIC_CLIENT_H_
6 #define CHROMEOS_DBUS_BLUETOOTH_GATT_CHARACTERISTIC_CLIENT_H_
7 
8 #include <string>
9 
10 #include "base/basictypes.h"
11 #include "base/callback.h"
12 #include "chromeos/chromeos_export.h"
13 #include "chromeos/dbus/dbus_client.h"
14 #include "dbus/object_path.h"
15 #include "dbus/property.h"
16 
17 namespace chromeos {
18 
19 // BluetoothGattCharacteristicClient is used to communicate with remote GATT
20 // characteristic objects exposed by the Bluetooth daemon.
21 class CHROMEOS_EXPORT BluetoothGattCharacteristicClient : public DBusClient {
22  public:
23   // Structure of properties associated with GATT characteristics.
24   struct Properties : public dbus::PropertySet {
25     // The 128-bit characteristic UUID. [read-only]
26     dbus::Property<std::string> uuid;
27 
28     // Object path of the GATT service the characteristic belongs to.
29     // [read-only]
30     dbus::Property<dbus::ObjectPath> service;
31 
32     // Whether or not this characteristic is currently sending ValueUpdated
33     // signals.
34     dbus::Property<bool> notifying;
35 
36     // List of flags representing the GATT "Characteristic Properties bit field"
37     // and properties read from the GATT "Characteristic Extended Properties"
38     // descriptor bit field. [read-only, optional]
39     dbus::Property<std::vector<std::string> > flags;
40 
41     Properties(dbus::ObjectProxy* object_proxy,
42                const std::string& interface_name,
43                const PropertyChangedCallback& callback);
44     virtual ~Properties();
45   };
46 
47   // Interface for observing changes from a remote GATT characteristic.
48   class Observer {
49    public:
~Observer()50     virtual ~Observer() {}
51 
52     // Called when the GATT characteristic with object path |object_path| is
53     // added to the system.
GattCharacteristicAdded(const dbus::ObjectPath & object_path)54     virtual void GattCharacteristicAdded(const dbus::ObjectPath& object_path) {}
55 
56     // Called when the GATT characteristic with object path |object_path| is
57     // removed from the system.
GattCharacteristicRemoved(const dbus::ObjectPath & object_path)58     virtual void GattCharacteristicRemoved(
59         const dbus::ObjectPath& object_path) {}
60 
61     // Called when the GATT characteristic with object path |object_path| has a
62     // change in the value of the property named |property_name|.
GattCharacteristicPropertyChanged(const dbus::ObjectPath & object_path,const std::string & property_name)63     virtual void GattCharacteristicPropertyChanged(
64         const dbus::ObjectPath& object_path,
65         const std::string& property_name) {}
66 
67     // Called when a "ValueUpdated" signal is received from the remote GATT
68     // characteristic with object path |object_path| with characteristic value
69     // |value|.
GattCharacteristicValueUpdated(const dbus::ObjectPath & object_path,const std::vector<uint8> & value)70     virtual void GattCharacteristicValueUpdated(
71         const dbus::ObjectPath& object_path,
72         const std::vector<uint8>& value) {}
73   };
74 
75   // Callbacks used to report the result of asynchronous methods.
76   typedef base::Callback<void(const std::string& error_name,
77                               const std::string& error_message)> ErrorCallback;
78   typedef base::Callback<void(const std::vector<uint8>& value)> ValueCallback;
79 
80   virtual ~BluetoothGattCharacteristicClient();
81 
82   // Adds and removes observers for events on all remote GATT characteristics.
83   // Check the |object_path| parameter of observer methods to determine which
84   // GATT characteristic is issuing the event.
85   virtual void AddObserver(Observer* observer) = 0;
86   virtual void RemoveObserver(Observer* observer) = 0;
87 
88   // Returns the list of GATT characteristic object paths known to the system.
89   virtual std::vector<dbus::ObjectPath> GetCharacteristics() = 0;
90 
91   // Obtain the properties for the GATT characteristic with object path
92   // |object_path|. Values should be copied if needed.
93   virtual Properties* GetProperties(const dbus::ObjectPath& object_path) = 0;
94 
95   // Issues a request to read the value of GATT characteristic with object path
96   // |object_path| and returns the value in |callback| on success. On error,
97   // invokes |error_callback|.
98   virtual void ReadValue(const dbus::ObjectPath& object_path,
99                          const ValueCallback& callback,
100                          const ErrorCallback& error_callback) = 0;
101 
102   // Issues a request to write the value of GATT characteristic with object path
103   // |object_path| with value |value|. Invokes |callback| on success and
104   // |error_callback| on failure.
105   virtual void WriteValue(const dbus::ObjectPath& object_path,
106                           const std::vector<uint8>& value,
107                           const base::Closure& callback,
108                           const ErrorCallback& error_callback) = 0;
109 
110   // Starts a notification session from this characteristic with object path
111   // |object_path| if it supports value notifications or indications. Invokes
112   // |callback| on success and |error_callback| on failure.
113   virtual void StartNotify(const dbus::ObjectPath& object_path,
114                            const base::Closure& callback,
115                            const ErrorCallback& error_callback) = 0;
116 
117   // Cancels any previous StartNotify transaction for characteristic with
118   // object path |object_path|. Invokes |callback| on success and
119   // |error_callback| on failure.
120   virtual void StopNotify(const dbus::ObjectPath& object_path,
121                           const base::Closure& callback,
122                           const ErrorCallback& error_callback) = 0;
123 
124   // Creates the instance.
125   static BluetoothGattCharacteristicClient* Create();
126 
127   // Constants used to indicate exceptional error conditions.
128   static const char kNoResponseError[];
129   static const char kUnknownCharacteristicError[];
130 
131  protected:
132   BluetoothGattCharacteristicClient();
133 
134  private:
135   DISALLOW_COPY_AND_ASSIGN(BluetoothGattCharacteristicClient);
136 };
137 
138 }  // namespace chromeos
139 
140 #endif  // CHROMEOS_DBUS_BLUETOOTH_GATT_CHARACTERISTIC_CLIENT_H_
141