• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_supplicant_network_proxy.h"
18 
19 #include <string>
20 
21 #include <base/bind.h>
22 
23 #include "shill/logging.h"
24 #include "shill/supplicant/wpa_supplicant.h"
25 
26 using std::string;
27 
28 namespace shill {
29 
30 namespace Logging {
31 static auto kModuleLogScope = ScopeLogger::kDBus;
ObjectID(const dbus::ObjectPath * p)32 static string ObjectID(const dbus::ObjectPath* p) { return p->value(); }
33 }
34 
35 // static.
36 const char ChromeosSupplicantNetworkProxy::kInterfaceName[] =
37     "fi.w1.wpa_supplicant1.Network";
38 const char ChromeosSupplicantNetworkProxy::kPropertyEnabled[] = "Enabled";
39 const char ChromeosSupplicantNetworkProxy::kPropertyProperties[] =
40     "Properties";
41 
PropertySet(dbus::ObjectProxy * object_proxy,const std::string & interface_name,const PropertyChangedCallback & callback)42 ChromeosSupplicantNetworkProxy::PropertySet::PropertySet(
43     dbus::ObjectProxy* object_proxy,
44     const std::string& interface_name,
45     const PropertyChangedCallback& callback)
46     : dbus::PropertySet(object_proxy, interface_name, callback) {
47   RegisterProperty(kPropertyEnabled, &enabled);
48 }
49 
ChromeosSupplicantNetworkProxy(const scoped_refptr<dbus::Bus> & bus,const std::string & object_path)50 ChromeosSupplicantNetworkProxy::ChromeosSupplicantNetworkProxy(
51     const scoped_refptr<dbus::Bus>& bus,
52     const std::string& object_path)
53     : network_proxy_(
54         new fi::w1::wpa_supplicant1::NetworkProxy(
55             bus,
56             WPASupplicant::kDBusAddr,
57             dbus::ObjectPath(object_path))) {
58   // Register properties.
59   properties_.reset(
60       new PropertySet(
61           network_proxy_->GetObjectProxy(),
62           kInterfaceName,
63           base::Bind(&ChromeosSupplicantNetworkProxy::OnPropertyChanged,
64                      weak_factory_.GetWeakPtr())));
65 
66   // Register signal handler.
67   network_proxy_->RegisterPropertiesChangedSignalHandler(
68       base::Bind(&ChromeosSupplicantNetworkProxy::PropertiesChanged,
69                  weak_factory_.GetWeakPtr()),
70       base::Bind(&ChromeosSupplicantNetworkProxy::OnSignalConnected,
71                  weak_factory_.GetWeakPtr()));
72 
73   // Connect property signals and initialize cached values. Based on
74   // recommendations from src/dbus/property.h.
75   properties_->ConnectSignals();
76   properties_->GetAll();
77 }
78 
~ChromeosSupplicantNetworkProxy()79 ChromeosSupplicantNetworkProxy::~ChromeosSupplicantNetworkProxy() {
80   network_proxy_->ReleaseObjectProxy(base::Bind(&base::DoNothing));
81 }
82 
SetEnabled(bool enabled)83 bool ChromeosSupplicantNetworkProxy::SetEnabled(bool enabled) {
84   SLOG(&network_proxy_->GetObjectPath(), 2) << __func__;
85   if(!properties_->enabled.SetAndBlock(enabled)) {
86     LOG(ERROR) << "Failed to SetEnabled: " << enabled;
87     return false;
88   }
89   return true;
90 }
91 
PropertiesChanged(const brillo::VariantDictionary &)92 void ChromeosSupplicantNetworkProxy::PropertiesChanged(
93     const brillo::VariantDictionary& /*properties*/) {
94   SLOG(&network_proxy_->GetObjectPath(), 2) << __func__;
95 }
96 
OnPropertyChanged(const std::string & property_name)97 void ChromeosSupplicantNetworkProxy::OnPropertyChanged(
98     const std::string& property_name) {
99   SLOG(&network_proxy_->GetObjectPath(), 2) << __func__ << ": "
100       << property_name;
101 }
102 
103 // Called when signal is connected to the ObjectProxy.
OnSignalConnected(const std::string & interface_name,const std::string & signal_name,bool success)104 void ChromeosSupplicantNetworkProxy::OnSignalConnected(
105     const std::string& interface_name,
106     const std::string& signal_name,
107     bool success) {
108   SLOG(&network_proxy_->GetObjectPath(), 2) << __func__
109       << "interface: " << interface_name << " signal: " << signal_name
110       << "success: " << success;
111   if (!success) {
112     LOG(ERROR) << "Failed to connect signal " << signal_name
113         << " to interface " << interface_name;
114   }
115 }
116 
117 }  // namespace shill
118