1 //
2 // Copyright (C) 2015 The Android Open Source Project
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 #include "shill/dbus/chromeos_dbus_properties_proxy.h"
18
19 #include "shill/logging.h"
20
21 namespace shill {
22
23 using std::string;
24 using std::vector;
25
26 namespace Logging {
27 static auto kModuleLogScope = ScopeLogger::kDBus;
ObjectID(const dbus::ObjectPath * p)28 static string ObjectID(const dbus::ObjectPath* p) { return p->value(); }
29 }
30
ChromeosDBusPropertiesProxy(const scoped_refptr<dbus::Bus> & bus,const string & path,const string & service)31 ChromeosDBusPropertiesProxy::ChromeosDBusPropertiesProxy(
32 const scoped_refptr<dbus::Bus>& bus,
33 const string& path,
34 const string& service)
35 : proxy_(
36 new org::freedesktop::DBus::PropertiesProxy(
37 bus, service, dbus::ObjectPath(path))) {
38 // Register signal handlers.
39 proxy_->RegisterPropertiesChangedSignalHandler(
40 base::Bind(&ChromeosDBusPropertiesProxy::PropertiesChanged,
41 weak_factory_.GetWeakPtr()),
42 base::Bind(&ChromeosDBusPropertiesProxy::OnSignalConnected,
43 weak_factory_.GetWeakPtr()));
44 proxy_->RegisterMmPropertiesChangedSignalHandler(
45 base::Bind(&ChromeosDBusPropertiesProxy::MmPropertiesChanged,
46 weak_factory_.GetWeakPtr()),
47 base::Bind(&ChromeosDBusPropertiesProxy::OnSignalConnected,
48 weak_factory_.GetWeakPtr()));
49 }
50
~ChromeosDBusPropertiesProxy()51 ChromeosDBusPropertiesProxy::~ChromeosDBusPropertiesProxy() {}
52
GetAll(const string & interface_name)53 KeyValueStore ChromeosDBusPropertiesProxy::GetAll(
54 const string& interface_name) {
55 SLOG(&proxy_->GetObjectPath(), 2) << __func__ << "(" << interface_name << ")";
56 brillo::VariantDictionary properties_dict;
57 brillo::ErrorPtr error;
58 if (!proxy_->GetAll(interface_name, &properties_dict, &error)) {
59 LOG(ERROR) << __func__ << " failed on " << interface_name
60 << ": " << error->GetCode() << " " << error->GetMessage();
61 return KeyValueStore();
62 }
63 KeyValueStore properties_store;
64 KeyValueStore::ConvertFromVariantDictionary(properties_dict,
65 &properties_store);
66 return properties_store;
67 }
68
Get(const string & interface_name,const string & property)69 brillo::Any ChromeosDBusPropertiesProxy::Get(const string& interface_name,
70 const string& property) {
71 SLOG(&proxy_->GetObjectPath(), 2) << __func__ << "(" << interface_name
72 << ", " << property << ")";
73 brillo::Any value;
74 brillo::ErrorPtr error;
75 if (!proxy_->Get(interface_name, property, &value, &error)) {
76 LOG(ERROR) << __func__ << " failed for " << interface_name
77 << " " << property << ": "
78 << error->GetCode() << " " << error->GetMessage();
79 }
80 return value;
81 }
82
MmPropertiesChanged(const string & interface,const brillo::VariantDictionary & properties)83 void ChromeosDBusPropertiesProxy::MmPropertiesChanged(
84 const string& interface,
85 const brillo::VariantDictionary& properties) {
86 SLOG(&proxy_->GetObjectPath(), 2) << __func__ << "(" << interface << ")";
87 KeyValueStore properties_store;
88 KeyValueStore::ConvertFromVariantDictionary(properties, &properties_store);
89 mm_properties_changed_callback_.Run(interface, properties_store);
90 }
91
PropertiesChanged(const string & interface,const brillo::VariantDictionary & changed_properties,const vector<string> & invalidated_properties)92 void ChromeosDBusPropertiesProxy::PropertiesChanged(
93 const string& interface,
94 const brillo::VariantDictionary& changed_properties,
95 const vector<string>& invalidated_properties) {
96 SLOG(&proxy_->GetObjectPath(), 2) << __func__ << "(" << interface << ")";
97 KeyValueStore properties_store;
98 KeyValueStore::ConvertFromVariantDictionary(changed_properties,
99 &properties_store);
100 properties_changed_callback_.Run(
101 interface, properties_store, invalidated_properties);
102 }
103
OnSignalConnected(const string & interface_name,const string & signal_name,bool success)104 void ChromeosDBusPropertiesProxy::OnSignalConnected(
105 const string& interface_name, const string& signal_name, bool success) {
106 SLOG(&proxy_->GetObjectPath(), 2) << __func__
107 << "interface: " << interface_name
108 << " signal: " << signal_name << "success: " << success;
109 if (!success) {
110 LOG(ERROR) << "Failed to connect signal " << signal_name
111 << " to interface " << interface_name;
112 }
113 }
114
115 } // namespace shill
116