OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROMEOS_DBUS_BLUETOOTH_PROPERTY_H_ | 5 #ifndef CHROMEOS_DBUS_BLUETOOTH_PROPERTY_H_ |
6 #define CHROMEOS_DBUS_BLUETOOTH_PROPERTY_H_ | 6 #define CHROMEOS_DBUS_BLUETOOTH_PROPERTY_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <string> | 9 #include <string> |
10 | 10 |
11 #include "base/compiler_specific.h" | 11 #include "base/compiler_specific.h" |
12 #include "dbus/message.h" | 12 #include "dbus/message.h" |
13 #include "dbus/object_proxy.h" | 13 #include "dbus/object_proxy.h" |
14 #include "dbus/property.h" | 14 #include "dbus/property.h" |
15 #include "third_party/cros_system_api/dbus/service_constants.h" | 15 #include "third_party/cros_system_api/dbus/service_constants.h" |
16 | 16 |
17 namespace chromeos { | 17 namespace chromeos { |
18 | 18 |
19 // BlueZ predates the common D-Bus Properties API (though it inspired it), | 19 // BlueZ predates the common D-Bus Properties API (though it inspired it), |
20 // override dbus::PropertySet to generate the correct method call to get | 20 // override dbus::PropertySet to generate the correct method call to get |
21 // all properties, conenect to the correct signal and parse it correctly. | 21 // all properties, conenect to the correct signal and parse it correctly. |
22 // | |
23 // BluetoothPropertySet should be used with BluetoothProperty<>. | |
24 class BluetoothPropertySet : public dbus::PropertySet { | 22 class BluetoothPropertySet : public dbus::PropertySet { |
25 public: | 23 public: |
26 BluetoothPropertySet(dbus::ObjectProxy* object_proxy, | 24 BluetoothPropertySet(dbus::ObjectProxy* object_proxy, |
27 const std::string& interface, | 25 const std::string& interface, |
28 const PropertyChangedCallback& callback) | 26 const PropertyChangedCallback& callback) |
29 : dbus::PropertySet(object_proxy, interface, callback) {} | 27 : dbus::PropertySet(object_proxy, interface, callback) {} |
30 | 28 |
31 // dbus::PropertySet override. | 29 // dbus::PropertySet override. |
32 // | 30 // |
33 // Call after construction to connect property change notification | 31 // Call after construction to connect property change notification |
34 // signals. Sub-classes may override to use different D-Bus signals. | 32 // signals. Sub-classes may override to use different D-Bus signals. |
35 virtual void ConnectSignals() OVERRIDE; | 33 virtual void ConnectSignals() OVERRIDE; |
36 | 34 |
37 // dbus::PropertySet override. | 35 // dbus::PropertySet override. |
38 // | 36 // |
| 37 // Requests an updated value from the remote object incurring a round-trip. |
| 38 virtual void Get(dbus::PropertyBase* property, |
| 39 GetCallback callback) OVERRIDE; |
| 40 |
| 41 // dbus::PropertySet override. |
| 42 // |
39 // Queries the remote object for values of all properties and updates | 43 // Queries the remote object for values of all properties and updates |
40 // initial values. | 44 // initial values. |
41 virtual void GetAll() OVERRIDE; | 45 virtual void GetAll() OVERRIDE; |
42 | 46 |
43 // dbus::PropertySet override. | 47 // dbus::PropertySet override. |
44 // | 48 // |
| 49 // Requests that the remote object change the property to its new value. |
| 50 virtual void Set(dbus::PropertyBase* property, |
| 51 SetCallback callback) OVERRIDE; |
| 52 |
| 53 // dbus::PropertySet override. |
| 54 // |
45 // Method connected by ConnectSignals() and called by dbus:: when | 55 // Method connected by ConnectSignals() and called by dbus:: when |
46 // a property is changed. | 56 // a property is changed. |
47 virtual void ChangedReceived(dbus::Signal* signal) OVERRIDE; | 57 virtual void ChangedReceived(dbus::Signal* signal) OVERRIDE; |
48 }; | 58 }; |
49 | 59 |
50 // BlueZ predates the common D-Bus Properties API (though it inspired it), | |
51 // override dbus::Property<> to generate the correct method call to set a | |
52 // new property value. | |
53 template <class T> | |
54 class BluetoothProperty : public dbus::Property<T> { | |
55 public: | |
56 // Import the callbacks into our namespace (this is a template derived from | |
57 // a template, the C++ standard gets a bit wibbly and doesn't do it for us). | |
58 // | |
59 // |success| indicates whether or not the value could be retrived, or new | |
60 // value set. For Get, if true the new value can be obtained by calling | |
61 // value() on the property; for Set() a Get() call may be necessary. | |
62 typedef typename dbus::Property<T>::GetCallback GetCallback; | |
63 typedef typename dbus::Property<T>::SetCallback SetCallback; | |
64 | |
65 // dbus::Property<> override. | |
66 // | |
67 // Requests an updated value from the remote object incurring a | |
68 // round-trip. |callback| will be called when the new value is available. | |
69 virtual void Get(GetCallback callback) OVERRIDE { | |
70 NOTREACHED() << "BlueZ does not implement Get for properties"; | |
71 } | |
72 | |
73 // dbus::Property<> override. | |
74 // | |
75 // Requests that the remote object change the property value to |value|, | |
76 // |callback| will be called to indicate the success or failure of the | |
77 // request, however the new value may not be available depending on the | |
78 // remote object. | |
79 virtual void Set(const T& value, SetCallback callback) OVERRIDE { | |
80 dbus::MethodCall method_call(this->property_set()->interface(), | |
81 bluetooth_common::kSetProperty); | |
82 dbus::MessageWriter writer(&method_call); | |
83 writer.AppendString(this->name()); | |
84 this->AppendToWriter(&writer, value); | |
85 | |
86 dbus::ObjectProxy *object_proxy = this->property_set()->object_proxy(); | |
87 DCHECK(object_proxy); | |
88 object_proxy->CallMethod(&method_call, | |
89 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
90 base::Bind(&dbus::Property<T>::OnSet, | |
91 this->GetWeakPtr(), | |
92 callback)); | |
93 } | |
94 }; | |
95 | |
96 } // namespace chromeos | 60 } // namespace chromeos |
97 | 61 |
98 #endif // CHROMEOS_DBUS_BLUETOOTH_PROPERTY_H_ | 62 #endif // CHROMEOS_DBUS_BLUETOOTH_PROPERTY_H_ |
OLD | NEW |