blob: e3fa8f9138bae6db53418ab672b6dd95d590f773 [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>
Alex Deymod6deb1d2015-08-28 15:54:37 -070023#include <shill/dbus-constants.h>
24#include <shill/dbus-proxies.h>
Alex Deymob8803bb2015-08-19 23:14:49 -070025
Alex Deymo30534502015-07-20 15:06:33 -070026using org::chromium::flimflam::ManagerProxyInterface;
27using org::chromium::flimflam::ServiceProxyInterface;
Gilad Arnold5ef9c482014-03-03 13:51:02 -080028using std::string;
29
Alex Deymo63784a52014-05-28 10:46:14 -070030namespace chromeos_update_manager {
Gilad Arnold55f39b72014-01-28 12:51:45 -080031
Alex Deymo30534502015-07-20 15:06:33 -070032ConnectionType RealShillProvider::ParseConnectionType(const string& type_str) {
33 if (type_str == shill::kTypeEthernet) {
Gilad Arnoldef120fa2014-04-09 12:52:10 -070034 return ConnectionType::kEthernet;
Alex Deymo30534502015-07-20 15:06:33 -070035 } else if (type_str == shill::kTypeWifi) {
Gilad Arnoldef120fa2014-04-09 12:52:10 -070036 return ConnectionType::kWifi;
Alex Deymo30534502015-07-20 15:06:33 -070037 } else if (type_str == shill::kTypeWimax) {
Gilad Arnoldef120fa2014-04-09 12:52:10 -070038 return ConnectionType::kWimax;
Alex Deymo30534502015-07-20 15:06:33 -070039 } else if (type_str == shill::kTypeBluetooth) {
Gilad Arnoldef120fa2014-04-09 12:52:10 -070040 return ConnectionType::kBluetooth;
Alex Deymo30534502015-07-20 15:06:33 -070041 } else if (type_str == shill::kTypeCellular) {
Gilad Arnoldef120fa2014-04-09 12:52:10 -070042 return ConnectionType::kCellular;
Alex Deymo30534502015-07-20 15:06:33 -070043 }
Gilad Arnoldef120fa2014-04-09 12:52:10 -070044 return ConnectionType::kUnknown;
45}
46
47ConnectionTethering RealShillProvider::ParseConnectionTethering(
Alex Deymo30534502015-07-20 15:06:33 -070048 const string& tethering_str) {
49 if (tethering_str == shill::kTetheringNotDetectedState) {
Gilad Arnoldef120fa2014-04-09 12:52:10 -070050 return ConnectionTethering::kNotDetected;
Alex Deymo30534502015-07-20 15:06:33 -070051 } else if (tethering_str == shill::kTetheringSuspectedState) {
Gilad Arnoldef120fa2014-04-09 12:52:10 -070052 return ConnectionTethering::kSuspected;
Alex Deymo30534502015-07-20 15:06:33 -070053 } else if (tethering_str == shill::kTetheringConfirmedState) {
Gilad Arnoldef120fa2014-04-09 12:52:10 -070054 return ConnectionTethering::kConfirmed;
Alex Deymo30534502015-07-20 15:06:33 -070055 }
Gilad Arnoldef120fa2014-04-09 12:52:10 -070056 return ConnectionTethering::kUnknown;
57}
58
Alex Deymo42c30c32014-04-24 18:41:18 -070059bool RealShillProvider::Init() {
Alex Deymo30534502015-07-20 15:06:33 -070060 ManagerProxyInterface* manager_proxy = shill_proxy_->GetManagerProxy();
61 if (!manager_proxy)
Gilad Arnold5ef9c482014-03-03 13:51:02 -080062 return false;
Gilad Arnold55f39b72014-01-28 12:51:45 -080063
Gilad Arnoldbeb39e92014-03-11 11:34:50 -070064 // Subscribe to the manager's PropertyChanged signal.
Alex Deymo30534502015-07-20 15:06:33 -070065 manager_proxy->RegisterPropertyChangedSignalHandler(
66 base::Bind(&RealShillProvider::OnManagerPropertyChanged,
67 base::Unretained(this)),
68 base::Bind(&RealShillProvider::OnSignalConnected,
69 base::Unretained(this)));
Gilad Arnold5ef9c482014-03-03 13:51:02 -080070
Gilad Arnoldef120fa2014-04-09 12:52:10 -070071 // Attempt to read initial connection status. Even if this fails because shill
72 // is not responding (e.g. it is down) we'll be notified via "PropertyChanged"
73 // signal as soon as it comes up, so this is not a critical step.
Alex Deymo30534502015-07-20 15:06:33 -070074 chromeos::VariantDictionary properties;
75 chromeos::ErrorPtr error;
76 if (!manager_proxy->GetProperties(&properties, &error))
77 return true;
78
79 const auto& prop_default_service =
80 properties.find(shill::kDefaultServiceProperty);
81 if (prop_default_service != properties.end()) {
82 OnManagerPropertyChanged(prop_default_service->first,
83 prop_default_service->second);
Gilad Arnold5ef9c482014-03-03 13:51:02 -080084 }
85
Gilad Arnoldef120fa2014-04-09 12:52:10 -070086 return true;
Gilad Arnold5ef9c482014-03-03 13:51:02 -080087}
88
Alex Deymo30534502015-07-20 15:06:33 -070089void RealShillProvider::OnManagerPropertyChanged(const string& name,
90 const chromeos::Any& value) {
91 if (name == shill::kDefaultServiceProperty)
92 ProcessDefaultService(value.TryGet<dbus::ObjectPath>().value());
Gilad Arnold5ef9c482014-03-03 13:51:02 -080093}
94
Alex Deymo30534502015-07-20 15:06:33 -070095void RealShillProvider::OnSignalConnected(const string& interface_name,
96 const string& signal_name,
97 bool successful) {
98 if (!successful) {
99 LOG(ERROR) << "Couldn't connect to the signal " << interface_name << "."
100 << signal_name;
Gilad Arnold5ef9c482014-03-03 13:51:02 -0800101 }
Gilad Arnold5ef9c482014-03-03 13:51:02 -0800102}
103
Alex Deymo30534502015-07-20 15:06:33 -0700104bool RealShillProvider::ProcessDefaultService(
105 const string& default_service_path) {
106 // We assume that if the service path didn't change, then the connection
107 // type and the tethering status of it also didn't change.
108 if (default_service_path_ == default_service_path)
Gilad Arnoldef120fa2014-04-09 12:52:10 -0700109 return true;
110
Gilad Arnoldd3df25f2014-04-22 08:39:48 -0700111 // Update the connection status.
Alex Deymo30534502015-07-20 15:06:33 -0700112 default_service_path_ = default_service_path;
Gilad Arnoldd3df25f2014-04-22 08:39:48 -0700113 bool is_connected = (default_service_path_ != "/");
114 var_is_connected_.SetValue(is_connected);
115 var_conn_last_changed_.SetValue(clock_->GetWallclockTime());
Gilad Arnoldef120fa2014-04-09 12:52:10 -0700116
Alex Deymo30534502015-07-20 15:06:33 -0700117 if (!is_connected) {
Gilad Arnoldd3df25f2014-04-22 08:39:48 -0700118 var_conn_type_.UnsetValue();
119 var_conn_tethering_.UnsetValue();
Alex Deymo30534502015-07-20 15:06:33 -0700120 return true;
121 }
122
123 // We create and dispose the ServiceProxyInterface on every request.
124 std::unique_ptr<ServiceProxyInterface> service =
125 shill_proxy_->GetServiceForPath(default_service_path_);
126
127 // Get the connection properties synchronously.
128 chromeos::VariantDictionary properties;
129 chromeos::ErrorPtr error;
130 if (!service->GetProperties(&properties, &error)) {
131 var_conn_type_.UnsetValue();
132 var_conn_tethering_.UnsetValue();
133 return false;
134 }
135
136 // Get the connection tethering mode.
137 const auto& prop_tethering = properties.find(shill::kTetheringProperty);
138 if (prop_tethering == properties.end()) {
139 // Remove the value if not present on the service. This most likely means an
140 // error in shill and the policy will handle it, but we will print a log
141 // message as well for accessing an unused variable.
142 var_conn_tethering_.UnsetValue();
143 LOG(ERROR) << "Could not find connection type (" << default_service_path_
144 << ")";
145 } else {
146 // If the property doesn't contain a string value, the empty string will
147 // become kUnknown.
148 var_conn_tethering_.SetValue(
149 ParseConnectionTethering(prop_tethering->second.TryGet<string>()));
150 }
151
152 // Get the connection type.
153 const auto& prop_type = properties.find(shill::kTypeProperty);
154 if (prop_type == properties.end()) {
155 var_conn_type_.UnsetValue();
156 LOG(ERROR) << "Could not find connection tethering mode ("
157 << default_service_path_ << ")";
158 } else {
159 string type_str = prop_type->second.TryGet<string>();
160 if (type_str == shill::kTypeVPN) {
161 const auto& prop_physical =
162 properties.find(shill::kPhysicalTechnologyProperty);
163 if (prop_physical == properties.end()) {
164 LOG(ERROR) << "No PhysicalTechnology property found for a VPN"
165 << " connection (service: " << default_service_path
166 << "). Using default kUnknown value.";
167 var_conn_type_.SetValue(ConnectionType::kUnknown);
168 } else {
169 var_conn_type_.SetValue(
170 ParseConnectionType(prop_physical->second.TryGet<string>()));
171 }
172 } else {
173 var_conn_type_.SetValue(ParseConnectionType(type_str));
174 }
Gilad Arnoldef120fa2014-04-09 12:52:10 -0700175 }
Gilad Arnolddf3dd242014-04-09 07:15:51 -0700176
Gilad Arnoldbeb39e92014-03-11 11:34:50 -0700177 return true;
178}
179
Alex Deymo63784a52014-05-28 10:46:14 -0700180} // namespace chromeos_update_manager