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
3 // found in the LICENSE file.
5 #include "chromeos/dbus/shill_device_client.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/stl_util.h"
10 #include "base/values.h"
11 #include "chromeos/dbus/shill_property_changed_observer.h"
13 #include "dbus/message.h"
14 #include "dbus/object_path.h"
15 #include "dbus/object_proxy.h"
16 #include "dbus/values_util.h"
17 #include "net/base/ip_endpoint.h"
18 #include "third_party/cros_system_api/dbus/service_constants.h"
24 // The ShillDeviceClient implementation.
25 class ShillDeviceClientImpl
: public ShillDeviceClient
{
27 ShillDeviceClientImpl()
31 virtual ~ShillDeviceClientImpl() {
32 for (HelperMap::iterator iter
= helpers_
.begin();
33 iter
!= helpers_
.end(); ++iter
) {
34 // This *should* never happen, yet we're getting crash reports that
35 // seem to imply that it does happen sometimes. Adding CHECKs here
36 // so we can determine more accurately where the problem lies.
37 // See: http://crbug.com/170541
38 CHECK(iter
->second
) << "NULL Helper found in helper list.";
44 ///////////////////////////////////////
45 // ShillDeviceClient overrides.
46 virtual void AddPropertyChangedObserver(
47 const dbus::ObjectPath
& device_path
,
48 ShillPropertyChangedObserver
* observer
) override
{
49 GetHelper(device_path
)->AddPropertyChangedObserver(observer
);
52 virtual void RemovePropertyChangedObserver(
53 const dbus::ObjectPath
& device_path
,
54 ShillPropertyChangedObserver
* observer
) override
{
55 GetHelper(device_path
)->RemovePropertyChangedObserver(observer
);
58 virtual void GetProperties(const dbus::ObjectPath
& device_path
,
59 const DictionaryValueCallback
& callback
) override
{
60 dbus::MethodCall
method_call(shill::kFlimflamDeviceInterface
,
61 shill::kGetPropertiesFunction
);
62 GetHelper(device_path
)->CallDictionaryValueMethod(&method_call
, callback
);
65 virtual void ProposeScan(const dbus::ObjectPath
& device_path
,
66 const VoidDBusMethodCallback
& callback
) override
{
67 dbus::MethodCall
method_call(shill::kFlimflamDeviceInterface
,
68 shill::kProposeScanFunction
);
69 GetHelper(device_path
)->CallVoidMethod(&method_call
, callback
);
72 virtual void SetProperty(const dbus::ObjectPath
& device_path
,
73 const std::string
& name
,
74 const base::Value
& value
,
75 const base::Closure
& callback
,
76 const ErrorCallback
& error_callback
) override
{
77 dbus::MethodCall
method_call(shill::kFlimflamDeviceInterface
,
78 shill::kSetPropertyFunction
);
79 dbus::MessageWriter
writer(&method_call
);
80 writer
.AppendString(name
);
81 ShillClientHelper::AppendValueDataAsVariant(&writer
, value
);
82 GetHelper(device_path
)->CallVoidMethodWithErrorCallback(&method_call
,
87 virtual void ClearProperty(const dbus::ObjectPath
& device_path
,
88 const std::string
& name
,
89 const VoidDBusMethodCallback
& callback
) override
{
90 dbus::MethodCall
method_call(shill::kFlimflamDeviceInterface
,
91 shill::kClearPropertyFunction
);
92 dbus::MessageWriter
writer(&method_call
);
93 writer
.AppendString(name
);
94 GetHelper(device_path
)->CallVoidMethod(&method_call
, callback
);
97 virtual void AddIPConfig(
98 const dbus::ObjectPath
& device_path
,
99 const std::string
& method
,
100 const ObjectPathDBusMethodCallback
& callback
) override
{
101 dbus::MethodCall
method_call(shill::kFlimflamDeviceInterface
,
102 shill::kAddIPConfigFunction
);
103 dbus::MessageWriter
writer(&method_call
);
104 writer
.AppendString(method
);
105 GetHelper(device_path
)->CallObjectPathMethod(&method_call
, callback
);
108 virtual void RequirePin(const dbus::ObjectPath
& device_path
,
109 const std::string
& pin
,
111 const base::Closure
& callback
,
112 const ErrorCallback
& error_callback
) override
{
113 dbus::MethodCall
method_call(shill::kFlimflamDeviceInterface
,
114 shill::kRequirePinFunction
);
115 dbus::MessageWriter
writer(&method_call
);
116 writer
.AppendString(pin
);
117 writer
.AppendBool(require
);
118 GetHelper(device_path
)->CallVoidMethodWithErrorCallback(
119 &method_call
, callback
, error_callback
);
122 virtual void EnterPin(const dbus::ObjectPath
& device_path
,
123 const std::string
& pin
,
124 const base::Closure
& callback
,
125 const ErrorCallback
& error_callback
) override
{
126 dbus::MethodCall
method_call(shill::kFlimflamDeviceInterface
,
127 shill::kEnterPinFunction
);
128 dbus::MessageWriter
writer(&method_call
);
129 writer
.AppendString(pin
);
130 GetHelper(device_path
)->CallVoidMethodWithErrorCallback(
131 &method_call
, callback
, error_callback
);
134 virtual void UnblockPin(const dbus::ObjectPath
& device_path
,
135 const std::string
& puk
,
136 const std::string
& pin
,
137 const base::Closure
& callback
,
138 const ErrorCallback
& error_callback
) override
{
139 dbus::MethodCall
method_call(shill::kFlimflamDeviceInterface
,
140 shill::kUnblockPinFunction
);
141 dbus::MessageWriter
writer(&method_call
);
142 writer
.AppendString(puk
);
143 writer
.AppendString(pin
);
144 GetHelper(device_path
)->CallVoidMethodWithErrorCallback(
145 &method_call
, callback
, error_callback
);
148 virtual void ChangePin(const dbus::ObjectPath
& device_path
,
149 const std::string
& old_pin
,
150 const std::string
& new_pin
,
151 const base::Closure
& callback
,
152 const ErrorCallback
& error_callback
) override
{
153 dbus::MethodCall
method_call(shill::kFlimflamDeviceInterface
,
154 shill::kChangePinFunction
);
155 dbus::MessageWriter
writer(&method_call
);
156 writer
.AppendString(old_pin
);
157 writer
.AppendString(new_pin
);
158 GetHelper(device_path
)->CallVoidMethodWithErrorCallback(
159 &method_call
, callback
, error_callback
);
162 virtual void Register(const dbus::ObjectPath
& device_path
,
163 const std::string
& network_id
,
164 const base::Closure
& callback
,
165 const ErrorCallback
& error_callback
) override
{
166 dbus::MethodCall
method_call(shill::kFlimflamDeviceInterface
,
167 shill::kRegisterFunction
);
168 dbus::MessageWriter
writer(&method_call
);
169 writer
.AppendString(network_id
);
170 GetHelper(device_path
)->CallVoidMethodWithErrorCallback(
171 &method_call
, callback
, error_callback
);
174 virtual void SetCarrier(const dbus::ObjectPath
& device_path
,
175 const std::string
& carrier
,
176 const base::Closure
& callback
,
177 const ErrorCallback
& error_callback
) override
{
178 dbus::MethodCall
method_call(shill::kFlimflamDeviceInterface
,
179 shill::kSetCarrierFunction
);
180 dbus::MessageWriter
writer(&method_call
);
181 writer
.AppendString(carrier
);
182 GetHelper(device_path
)->CallVoidMethodWithErrorCallback(
183 &method_call
, callback
, error_callback
);
186 virtual void Reset(const dbus::ObjectPath
& device_path
,
187 const base::Closure
& callback
,
188 const ErrorCallback
& error_callback
) override
{
189 dbus::MethodCall
method_call(shill::kFlimflamDeviceInterface
,
190 shill::kResetFunction
);
191 GetHelper(device_path
)->CallVoidMethodWithErrorCallback(
192 &method_call
, callback
, error_callback
);
195 virtual void PerformTDLSOperation(
196 const dbus::ObjectPath
& device_path
,
197 const std::string
& operation
,
198 const std::string
& peer
,
199 const StringCallback
& callback
,
200 const ErrorCallback
& error_callback
) override
{
201 dbus::MethodCall
method_call(shill::kFlimflamDeviceInterface
,
202 shill::kPerformTDLSOperationFunction
);
203 dbus::MessageWriter
writer(&method_call
);
204 writer
.AppendString(operation
);
205 writer
.AppendString(peer
);
206 GetHelper(device_path
)->CallStringMethodWithErrorCallback(
207 &method_call
, callback
, error_callback
);
210 void AddWakeOnPacketConnection(
211 const dbus::ObjectPath
& device_path
,
212 const net::IPEndPoint
& ip_endpoint
,
213 const base::Closure
& callback
,
214 const ErrorCallback
& error_callback
) override
{
215 if (ip_endpoint
.address().empty()) {
216 LOG(ERROR
) << "AddWakeOnPacketConnection: null address";
219 dbus::MethodCall
method_call(shill::kFlimflamDeviceInterface
,
220 shill::kAddWakeOnPacketConnectionFunction
);
221 dbus::MessageWriter
writer(&method_call
);
222 writer
.AppendString(ip_endpoint
.ToStringWithoutPort());
223 GetHelper(device_path
)->CallVoidMethodWithErrorCallback(&method_call
,
228 void RemoveWakeOnPacketConnection(
229 const dbus::ObjectPath
& device_path
,
230 const net::IPEndPoint
& ip_endpoint
,
231 const base::Closure
& callback
,
232 const ErrorCallback
& error_callback
) override
{
233 if (ip_endpoint
.address().empty()) {
234 LOG(ERROR
) << "RemoveWakeOnPacketConnection: null address";
237 dbus::MethodCall
method_call(shill::kFlimflamDeviceInterface
,
238 shill::kRemoveWakeOnPacketConnectionFunction
);
239 dbus::MessageWriter
writer(&method_call
);
240 writer
.AppendString(ip_endpoint
.ToStringWithoutPort());
241 GetHelper(device_path
)->CallVoidMethodWithErrorCallback(&method_call
,
246 void RemoveAllWakeOnPacketConnections(
247 const dbus::ObjectPath
& device_path
,
248 const base::Closure
& callback
,
249 const ErrorCallback
& error_callback
) override
{
250 dbus::MethodCall
method_call(
251 shill::kFlimflamDeviceInterface
,
252 shill::kRemoveAllWakeOnPacketConnectionsFunction
);
253 GetHelper(device_path
)->CallVoidMethodWithErrorCallback(&method_call
,
258 virtual TestInterface
* GetTestInterface() override
{
263 virtual void Init(dbus::Bus
* bus
) override
{
268 typedef std::map
<std::string
, ShillClientHelper
*> HelperMap
;
270 // Returns the corresponding ShillClientHelper for the profile.
271 ShillClientHelper
* GetHelper(const dbus::ObjectPath
& device_path
) {
272 HelperMap::iterator it
= helpers_
.find(device_path
.value());
273 if (it
!= helpers_
.end()) {
274 CHECK(it
->second
) << "Found a NULL helper in the list.";
278 // There is no helper for the profile, create it.
279 dbus::ObjectProxy
* object_proxy
=
280 bus_
->GetObjectProxy(shill::kFlimflamServiceName
, device_path
);
281 ShillClientHelper
* helper
= new ShillClientHelper(object_proxy
);
282 CHECK(helper
) << "Unable to create Shill client helper.";
283 helper
->MonitorPropertyChanged(shill::kFlimflamDeviceInterface
);
284 helpers_
.insert(HelperMap::value_type(device_path
.value(), helper
));
291 DISALLOW_COPY_AND_ASSIGN(ShillDeviceClientImpl
);
296 ShillDeviceClient::ShillDeviceClient() {}
298 ShillDeviceClient::~ShillDeviceClient() {}
301 ShillDeviceClient
* ShillDeviceClient::Create() {
302 return new ShillDeviceClientImpl();
305 } // namespace chromeos