blob: ab4900d08760e2c7d7b20a0d7ede8b9856596f01 [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
2// Copyright (C) 2014 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//
Gilad Arnold55f39b72014-01-28 12:51:45 -080016
Alex Deymo63784a52014-05-28 10:46:14 -070017#include "update_engine/update_manager/real_shill_provider.h"
Gilad Arnold55f39b72014-01-28 12:51:45 -080018
Gilad Arnold5ef9c482014-03-03 13:51:02 -080019#include <string>
20
21#include <base/logging.h>
Alex Vakulenko75039d72014-03-25 12:36:28 -070022#include <base/strings/stringprintf.h>
Gilad Arnold5ef9c482014-03-03 13:51:02 -080023#include <chromeos/dbus/service_constants.h>
24
Alex Deymo30534502015-07-20 15:06:33 -070025using org::chromium::flimflam::ManagerProxyInterface;
26using org::chromium::flimflam::ServiceProxyInterface;
Gilad Arnold5ef9c482014-03-03 13:51:02 -080027using std::string;
28
Alex Deymo63784a52014-05-28 10:46:14 -070029namespace chromeos_update_manager {
Gilad Arnold55f39b72014-01-28 12:51:45 -080030
Alex Deymo30534502015-07-20 15:06:33 -070031ConnectionType RealShillProvider::ParseConnectionType(const string& type_str) {
32 if (type_str == shill::kTypeEthernet) {
Gilad Arnoldef120fa2014-04-09 12:52:10 -070033 return ConnectionType::kEthernet;
Alex Deymo30534502015-07-20 15:06:33 -070034 } else if (type_str == shill::kTypeWifi) {
Gilad Arnoldef120fa2014-04-09 12:52:10 -070035 return ConnectionType::kWifi;
Alex Deymo30534502015-07-20 15:06:33 -070036 } else if (type_str == shill::kTypeWimax) {
Gilad Arnoldef120fa2014-04-09 12:52:10 -070037 return ConnectionType::kWimax;
Alex Deymo30534502015-07-20 15:06:33 -070038 } else if (type_str == shill::kTypeBluetooth) {
Gilad Arnoldef120fa2014-04-09 12:52:10 -070039 return ConnectionType::kBluetooth;
Alex Deymo30534502015-07-20 15:06:33 -070040 } else if (type_str == shill::kTypeCellular) {
Gilad Arnoldef120fa2014-04-09 12:52:10 -070041 return ConnectionType::kCellular;
Alex Deymo30534502015-07-20 15:06:33 -070042 }
Gilad Arnoldef120fa2014-04-09 12:52:10 -070043 return ConnectionType::kUnknown;
44}
45
46ConnectionTethering RealShillProvider::ParseConnectionTethering(
Alex Deymo30534502015-07-20 15:06:33 -070047 const string& tethering_str) {
48 if (tethering_str == shill::kTetheringNotDetectedState) {
Gilad Arnoldef120fa2014-04-09 12:52:10 -070049 return ConnectionTethering::kNotDetected;
Alex Deymo30534502015-07-20 15:06:33 -070050 } else if (tethering_str == shill::kTetheringSuspectedState) {
Gilad Arnoldef120fa2014-04-09 12:52:10 -070051 return ConnectionTethering::kSuspected;
Alex Deymo30534502015-07-20 15:06:33 -070052 } else if (tethering_str == shill::kTetheringConfirmedState) {
Gilad Arnoldef120fa2014-04-09 12:52:10 -070053 return ConnectionTethering::kConfirmed;
Alex Deymo30534502015-07-20 15:06:33 -070054 }
Gilad Arnoldef120fa2014-04-09 12:52:10 -070055 return ConnectionTethering::kUnknown;
56}
57
Alex Deymo42c30c32014-04-24 18:41:18 -070058bool RealShillProvider::Init() {
Alex Deymo30534502015-07-20 15:06:33 -070059 ManagerProxyInterface* manager_proxy = shill_proxy_->GetManagerProxy();
60 if (!manager_proxy)
Gilad Arnold5ef9c482014-03-03 13:51:02 -080061 return false;
Gilad Arnold55f39b72014-01-28 12:51:45 -080062
Gilad Arnoldbeb39e92014-03-11 11:34:50 -070063 // Subscribe to the manager's PropertyChanged signal.
Alex Deymo30534502015-07-20 15:06:33 -070064 manager_proxy->RegisterPropertyChangedSignalHandler(
65 base::Bind(&RealShillProvider::OnManagerPropertyChanged,
66 base::Unretained(this)),
67 base::Bind(&RealShillProvider::OnSignalConnected,
68 base::Unretained(this)));
Gilad Arnold5ef9c482014-03-03 13:51:02 -080069
Gilad Arnoldef120fa2014-04-09 12:52:10 -070070 // Attempt to read initial connection status. Even if this fails because shill
71 // is not responding (e.g. it is down) we'll be notified via "PropertyChanged"
72 // signal as soon as it comes up, so this is not a critical step.
Alex Deymo30534502015-07-20 15:06:33 -070073 chromeos::VariantDictionary properties;
74 chromeos::ErrorPtr error;
75 if (!manager_proxy->GetProperties(&properties, &error))
76 return true;
77
78 const auto& prop_default_service =
79 properties.find(shill::kDefaultServiceProperty);
80 if (prop_default_service != properties.end()) {
81 OnManagerPropertyChanged(prop_default_service->first,
82 prop_default_service->second);
Gilad Arnold5ef9c482014-03-03 13:51:02 -080083 }
84
Gilad Arnoldef120fa2014-04-09 12:52:10 -070085 return true;
Gilad Arnold5ef9c482014-03-03 13:51:02 -080086}
87
Alex Deymo30534502015-07-20 15:06:33 -070088void RealShillProvider::OnManagerPropertyChanged(const string& name,
89 const chromeos::Any& value) {
90 if (name == shill::kDefaultServiceProperty)
91 ProcessDefaultService(value.TryGet<dbus::ObjectPath>().value());
Gilad Arnold5ef9c482014-03-03 13:51:02 -080092}
93
Alex Deymo30534502015-07-20 15:06:33 -070094void RealShillProvider::OnSignalConnected(const string& interface_name,
95 const string& signal_name,
96 bool successful) {
97 if (!successful) {
98 LOG(ERROR) << "Couldn't connect to the signal " << interface_name << "."
99 << signal_name;
Gilad Arnold5ef9c482014-03-03 13:51:02 -0800100 }
Gilad Arnold5ef9c482014-03-03 13:51:02 -0800101}
102
Alex Deymo30534502015-07-20 15:06:33 -0700103bool RealShillProvider::ProcessDefaultService(
104 const string& default_service_path) {
105 // We assume that if the service path didn't change, then the connection
106 // type and the tethering status of it also didn't change.
107 if (default_service_path_ == default_service_path)
Gilad Arnoldef120fa2014-04-09 12:52:10 -0700108 return true;
109
Gilad Arnoldd3df25f2014-04-22 08:39:48 -0700110 // Update the connection status.
Alex Deymo30534502015-07-20 15:06:33 -0700111 default_service_path_ = default_service_path;
Gilad Arnoldd3df25f2014-04-22 08:39:48 -0700112 bool is_connected = (default_service_path_ != "/");
113 var_is_connected_.SetValue(is_connected);
114 var_conn_last_changed_.SetValue(clock_->GetWallclockTime());
Gilad Arnoldef120fa2014-04-09 12:52:10 -0700115
Alex Deymo30534502015-07-20 15:06:33 -0700116 if (!is_connected) {
Gilad Arnoldd3df25f2014-04-22 08:39:48 -0700117 var_conn_type_.UnsetValue();
118 var_conn_tethering_.UnsetValue();
Alex Deymo30534502015-07-20 15:06:33 -0700119 return true;
120 }
121
122 // We create and dispose the ServiceProxyInterface on every request.
123 std::unique_ptr<ServiceProxyInterface> service =
124 shill_proxy_->GetServiceForPath(default_service_path_);
125
126 // Get the connection properties synchronously.
127 chromeos::VariantDictionary properties;
128 chromeos::ErrorPtr error;
129 if (!service->GetProperties(&properties, &error)) {
130 var_conn_type_.UnsetValue();
131 var_conn_tethering_.UnsetValue();
132 return false;
133 }
134
135 // Get the connection tethering mode.
136 const auto& prop_tethering = properties.find(shill::kTetheringProperty);
137 if (prop_tethering == properties.end()) {
138 // Remove the value if not present on the service. This most likely means an
139 // error in shill and the policy will handle it, but we will print a log
140 // message as well for accessing an unused variable.
141 var_conn_tethering_.UnsetValue();
142 LOG(ERROR) << "Could not find connection type (" << default_service_path_
143 << ")";
144 } else {
145 // If the property doesn't contain a string value, the empty string will
146 // become kUnknown.
147 var_conn_tethering_.SetValue(
148 ParseConnectionTethering(prop_tethering->second.TryGet<string>()));
149 }
150
151 // Get the connection type.
152 const auto& prop_type = properties.find(shill::kTypeProperty);
153 if (prop_type == properties.end()) {
154 var_conn_type_.UnsetValue();
155 LOG(ERROR) << "Could not find connection tethering mode ("
156 << default_service_path_ << ")";
157 } else {
158 string type_str = prop_type->second.TryGet<string>();
159 if (type_str == shill::kTypeVPN) {
160 const auto& prop_physical =
161 properties.find(shill::kPhysicalTechnologyProperty);
162 if (prop_physical == properties.end()) {
163 LOG(ERROR) << "No PhysicalTechnology property found for a VPN"
164 << " connection (service: " << default_service_path
165 << "). Using default kUnknown value.";
166 var_conn_type_.SetValue(ConnectionType::kUnknown);
167 } else {
168 var_conn_type_.SetValue(
169 ParseConnectionType(prop_physical->second.TryGet<string>()));
170 }
171 } else {
172 var_conn_type_.SetValue(ParseConnectionType(type_str));
173 }
Gilad Arnoldef120fa2014-04-09 12:52:10 -0700174 }
Gilad Arnolddf3dd242014-04-09 07:15:51 -0700175
Gilad Arnoldbeb39e92014-03-11 11:34:50 -0700176 return true;
177}
178
Alex Deymo63784a52014-05-28 10:46:14 -0700179} // namespace chromeos_update_manager