Gilad Arnold | 55f39b7 | 2014-01-28 12:51:45 -0800 | [diff] [blame] | 1 | // Copyright (c) 2014 The Chromium OS 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. |
| 4 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 5 | #include "update_engine/update_manager/real_shill_provider.h" |
Gilad Arnold | 55f39b7 | 2014-01-28 12:51:45 -0800 | [diff] [blame] | 6 | |
Gilad Arnold | 5ef9c48 | 2014-03-03 13:51:02 -0800 | [diff] [blame] | 7 | #include <string> |
| 8 | |
| 9 | #include <base/logging.h> |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 10 | #include <base/strings/stringprintf.h> |
Gilad Arnold | 5ef9c48 | 2014-03-03 13:51:02 -0800 | [diff] [blame] | 11 | #include <chromeos/dbus/service_constants.h> |
| 12 | |
Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 13 | using org::chromium::flimflam::ManagerProxyInterface; |
| 14 | using org::chromium::flimflam::ServiceProxyInterface; |
Gilad Arnold | 5ef9c48 | 2014-03-03 13:51:02 -0800 | [diff] [blame] | 15 | using std::string; |
| 16 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 17 | namespace chromeos_update_manager { |
Gilad Arnold | 55f39b7 | 2014-01-28 12:51:45 -0800 | [diff] [blame] | 18 | |
Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 19 | ConnectionType RealShillProvider::ParseConnectionType(const string& type_str) { |
| 20 | if (type_str == shill::kTypeEthernet) { |
Gilad Arnold | ef120fa | 2014-04-09 12:52:10 -0700 | [diff] [blame] | 21 | return ConnectionType::kEthernet; |
Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 22 | } else if (type_str == shill::kTypeWifi) { |
Gilad Arnold | ef120fa | 2014-04-09 12:52:10 -0700 | [diff] [blame] | 23 | return ConnectionType::kWifi; |
Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 24 | } else if (type_str == shill::kTypeWimax) { |
Gilad Arnold | ef120fa | 2014-04-09 12:52:10 -0700 | [diff] [blame] | 25 | return ConnectionType::kWimax; |
Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 26 | } else if (type_str == shill::kTypeBluetooth) { |
Gilad Arnold | ef120fa | 2014-04-09 12:52:10 -0700 | [diff] [blame] | 27 | return ConnectionType::kBluetooth; |
Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 28 | } else if (type_str == shill::kTypeCellular) { |
Gilad Arnold | ef120fa | 2014-04-09 12:52:10 -0700 | [diff] [blame] | 29 | return ConnectionType::kCellular; |
Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 30 | } |
Gilad Arnold | ef120fa | 2014-04-09 12:52:10 -0700 | [diff] [blame] | 31 | return ConnectionType::kUnknown; |
| 32 | } |
| 33 | |
| 34 | ConnectionTethering RealShillProvider::ParseConnectionTethering( |
Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 35 | const string& tethering_str) { |
| 36 | if (tethering_str == shill::kTetheringNotDetectedState) { |
Gilad Arnold | ef120fa | 2014-04-09 12:52:10 -0700 | [diff] [blame] | 37 | return ConnectionTethering::kNotDetected; |
Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 38 | } else if (tethering_str == shill::kTetheringSuspectedState) { |
Gilad Arnold | ef120fa | 2014-04-09 12:52:10 -0700 | [diff] [blame] | 39 | return ConnectionTethering::kSuspected; |
Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 40 | } else if (tethering_str == shill::kTetheringConfirmedState) { |
Gilad Arnold | ef120fa | 2014-04-09 12:52:10 -0700 | [diff] [blame] | 41 | return ConnectionTethering::kConfirmed; |
Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 42 | } |
Gilad Arnold | ef120fa | 2014-04-09 12:52:10 -0700 | [diff] [blame] | 43 | return ConnectionTethering::kUnknown; |
| 44 | } |
| 45 | |
Alex Deymo | 42c30c3 | 2014-04-24 18:41:18 -0700 | [diff] [blame] | 46 | bool RealShillProvider::Init() { |
Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 47 | ManagerProxyInterface* manager_proxy = shill_proxy_->GetManagerProxy(); |
| 48 | if (!manager_proxy) |
Gilad Arnold | 5ef9c48 | 2014-03-03 13:51:02 -0800 | [diff] [blame] | 49 | return false; |
Gilad Arnold | 55f39b7 | 2014-01-28 12:51:45 -0800 | [diff] [blame] | 50 | |
Gilad Arnold | beb39e9 | 2014-03-11 11:34:50 -0700 | [diff] [blame] | 51 | // Subscribe to the manager's PropertyChanged signal. |
Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 52 | manager_proxy->RegisterPropertyChangedSignalHandler( |
| 53 | base::Bind(&RealShillProvider::OnManagerPropertyChanged, |
| 54 | base::Unretained(this)), |
| 55 | base::Bind(&RealShillProvider::OnSignalConnected, |
| 56 | base::Unretained(this))); |
Gilad Arnold | 5ef9c48 | 2014-03-03 13:51:02 -0800 | [diff] [blame] | 57 | |
Gilad Arnold | ef120fa | 2014-04-09 12:52:10 -0700 | [diff] [blame] | 58 | // Attempt to read initial connection status. Even if this fails because shill |
| 59 | // is not responding (e.g. it is down) we'll be notified via "PropertyChanged" |
| 60 | // signal as soon as it comes up, so this is not a critical step. |
Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 61 | chromeos::VariantDictionary properties; |
| 62 | chromeos::ErrorPtr error; |
| 63 | if (!manager_proxy->GetProperties(&properties, &error)) |
| 64 | return true; |
| 65 | |
| 66 | const auto& prop_default_service = |
| 67 | properties.find(shill::kDefaultServiceProperty); |
| 68 | if (prop_default_service != properties.end()) { |
| 69 | OnManagerPropertyChanged(prop_default_service->first, |
| 70 | prop_default_service->second); |
Gilad Arnold | 5ef9c48 | 2014-03-03 13:51:02 -0800 | [diff] [blame] | 71 | } |
| 72 | |
Gilad Arnold | ef120fa | 2014-04-09 12:52:10 -0700 | [diff] [blame] | 73 | return true; |
Gilad Arnold | 5ef9c48 | 2014-03-03 13:51:02 -0800 | [diff] [blame] | 74 | } |
| 75 | |
Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 76 | void RealShillProvider::OnManagerPropertyChanged(const string& name, |
| 77 | const chromeos::Any& value) { |
| 78 | if (name == shill::kDefaultServiceProperty) |
| 79 | ProcessDefaultService(value.TryGet<dbus::ObjectPath>().value()); |
Gilad Arnold | 5ef9c48 | 2014-03-03 13:51:02 -0800 | [diff] [blame] | 80 | } |
| 81 | |
Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 82 | void RealShillProvider::OnSignalConnected(const string& interface_name, |
| 83 | const string& signal_name, |
| 84 | bool successful) { |
| 85 | if (!successful) { |
| 86 | LOG(ERROR) << "Couldn't connect to the signal " << interface_name << "." |
| 87 | << signal_name; |
Gilad Arnold | 5ef9c48 | 2014-03-03 13:51:02 -0800 | [diff] [blame] | 88 | } |
Gilad Arnold | 5ef9c48 | 2014-03-03 13:51:02 -0800 | [diff] [blame] | 89 | } |
| 90 | |
Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 91 | bool RealShillProvider::ProcessDefaultService( |
| 92 | const string& default_service_path) { |
| 93 | // We assume that if the service path didn't change, then the connection |
| 94 | // type and the tethering status of it also didn't change. |
| 95 | if (default_service_path_ == default_service_path) |
Gilad Arnold | ef120fa | 2014-04-09 12:52:10 -0700 | [diff] [blame] | 96 | return true; |
| 97 | |
Gilad Arnold | d3df25f | 2014-04-22 08:39:48 -0700 | [diff] [blame] | 98 | // Update the connection status. |
Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 99 | default_service_path_ = default_service_path; |
Gilad Arnold | d3df25f | 2014-04-22 08:39:48 -0700 | [diff] [blame] | 100 | bool is_connected = (default_service_path_ != "/"); |
| 101 | var_is_connected_.SetValue(is_connected); |
| 102 | var_conn_last_changed_.SetValue(clock_->GetWallclockTime()); |
Gilad Arnold | ef120fa | 2014-04-09 12:52:10 -0700 | [diff] [blame] | 103 | |
Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 104 | if (!is_connected) { |
Gilad Arnold | d3df25f | 2014-04-22 08:39:48 -0700 | [diff] [blame] | 105 | var_conn_type_.UnsetValue(); |
| 106 | var_conn_tethering_.UnsetValue(); |
Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 107 | return true; |
| 108 | } |
| 109 | |
| 110 | // We create and dispose the ServiceProxyInterface on every request. |
| 111 | std::unique_ptr<ServiceProxyInterface> service = |
| 112 | shill_proxy_->GetServiceForPath(default_service_path_); |
| 113 | |
| 114 | // Get the connection properties synchronously. |
| 115 | chromeos::VariantDictionary properties; |
| 116 | chromeos::ErrorPtr error; |
| 117 | if (!service->GetProperties(&properties, &error)) { |
| 118 | var_conn_type_.UnsetValue(); |
| 119 | var_conn_tethering_.UnsetValue(); |
| 120 | return false; |
| 121 | } |
| 122 | |
| 123 | // Get the connection tethering mode. |
| 124 | const auto& prop_tethering = properties.find(shill::kTetheringProperty); |
| 125 | if (prop_tethering == properties.end()) { |
| 126 | // Remove the value if not present on the service. This most likely means an |
| 127 | // error in shill and the policy will handle it, but we will print a log |
| 128 | // message as well for accessing an unused variable. |
| 129 | var_conn_tethering_.UnsetValue(); |
| 130 | LOG(ERROR) << "Could not find connection type (" << default_service_path_ |
| 131 | << ")"; |
| 132 | } else { |
| 133 | // If the property doesn't contain a string value, the empty string will |
| 134 | // become kUnknown. |
| 135 | var_conn_tethering_.SetValue( |
| 136 | ParseConnectionTethering(prop_tethering->second.TryGet<string>())); |
| 137 | } |
| 138 | |
| 139 | // Get the connection type. |
| 140 | const auto& prop_type = properties.find(shill::kTypeProperty); |
| 141 | if (prop_type == properties.end()) { |
| 142 | var_conn_type_.UnsetValue(); |
| 143 | LOG(ERROR) << "Could not find connection tethering mode (" |
| 144 | << default_service_path_ << ")"; |
| 145 | } else { |
| 146 | string type_str = prop_type->second.TryGet<string>(); |
| 147 | if (type_str == shill::kTypeVPN) { |
| 148 | const auto& prop_physical = |
| 149 | properties.find(shill::kPhysicalTechnologyProperty); |
| 150 | if (prop_physical == properties.end()) { |
| 151 | LOG(ERROR) << "No PhysicalTechnology property found for a VPN" |
| 152 | << " connection (service: " << default_service_path |
| 153 | << "). Using default kUnknown value."; |
| 154 | var_conn_type_.SetValue(ConnectionType::kUnknown); |
| 155 | } else { |
| 156 | var_conn_type_.SetValue( |
| 157 | ParseConnectionType(prop_physical->second.TryGet<string>())); |
| 158 | } |
| 159 | } else { |
| 160 | var_conn_type_.SetValue(ParseConnectionType(type_str)); |
| 161 | } |
Gilad Arnold | ef120fa | 2014-04-09 12:52:10 -0700 | [diff] [blame] | 162 | } |
Gilad Arnold | df3dd24 | 2014-04-09 07:15:51 -0700 | [diff] [blame] | 163 | |
Gilad Arnold | beb39e9 | 2014-03-11 11:34:50 -0700 | [diff] [blame] | 164 | return true; |
| 165 | } |
| 166 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 167 | } // namespace chromeos_update_manager |