Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 1 | // |
| 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 Arnold | 55f39b7 | 2014-01-28 12:51:45 -0800 | [diff] [blame] | 16 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 17 | #include "update_engine/update_manager/real_shill_provider.h" |
Gilad Arnold | 55f39b7 | 2014-01-28 12:51:45 -0800 | [diff] [blame] | 18 | |
Gilad Arnold | 5ef9c48 | 2014-03-03 13:51:02 -0800 | [diff] [blame] | 19 | #include <string> |
| 20 | |
| 21 | #include <base/logging.h> |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 22 | #include <base/strings/stringprintf.h> |
Alex Deymo | d6deb1d | 2015-08-28 15:54:37 -0700 | [diff] [blame^] | 23 | #include <shill/dbus-constants.h> |
| 24 | #include <shill/dbus-proxies.h> |
Alex Deymo | b8803bb | 2015-08-19 23:14:49 -0700 | [diff] [blame] | 25 | |
Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 26 | using org::chromium::flimflam::ManagerProxyInterface; |
| 27 | using org::chromium::flimflam::ServiceProxyInterface; |
Gilad Arnold | 5ef9c48 | 2014-03-03 13:51:02 -0800 | [diff] [blame] | 28 | using std::string; |
| 29 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 30 | namespace chromeos_update_manager { |
Gilad Arnold | 55f39b7 | 2014-01-28 12:51:45 -0800 | [diff] [blame] | 31 | |
Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 32 | ConnectionType RealShillProvider::ParseConnectionType(const string& type_str) { |
| 33 | if (type_str == shill::kTypeEthernet) { |
Gilad Arnold | ef120fa | 2014-04-09 12:52:10 -0700 | [diff] [blame] | 34 | return ConnectionType::kEthernet; |
Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 35 | } else if (type_str == shill::kTypeWifi) { |
Gilad Arnold | ef120fa | 2014-04-09 12:52:10 -0700 | [diff] [blame] | 36 | return ConnectionType::kWifi; |
Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 37 | } else if (type_str == shill::kTypeWimax) { |
Gilad Arnold | ef120fa | 2014-04-09 12:52:10 -0700 | [diff] [blame] | 38 | return ConnectionType::kWimax; |
Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 39 | } else if (type_str == shill::kTypeBluetooth) { |
Gilad Arnold | ef120fa | 2014-04-09 12:52:10 -0700 | [diff] [blame] | 40 | return ConnectionType::kBluetooth; |
Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 41 | } else if (type_str == shill::kTypeCellular) { |
Gilad Arnold | ef120fa | 2014-04-09 12:52:10 -0700 | [diff] [blame] | 42 | return ConnectionType::kCellular; |
Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 43 | } |
Gilad Arnold | ef120fa | 2014-04-09 12:52:10 -0700 | [diff] [blame] | 44 | return ConnectionType::kUnknown; |
| 45 | } |
| 46 | |
| 47 | ConnectionTethering RealShillProvider::ParseConnectionTethering( |
Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 48 | const string& tethering_str) { |
| 49 | if (tethering_str == shill::kTetheringNotDetectedState) { |
Gilad Arnold | ef120fa | 2014-04-09 12:52:10 -0700 | [diff] [blame] | 50 | return ConnectionTethering::kNotDetected; |
Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 51 | } else if (tethering_str == shill::kTetheringSuspectedState) { |
Gilad Arnold | ef120fa | 2014-04-09 12:52:10 -0700 | [diff] [blame] | 52 | return ConnectionTethering::kSuspected; |
Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 53 | } else if (tethering_str == shill::kTetheringConfirmedState) { |
Gilad Arnold | ef120fa | 2014-04-09 12:52:10 -0700 | [diff] [blame] | 54 | return ConnectionTethering::kConfirmed; |
Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 55 | } |
Gilad Arnold | ef120fa | 2014-04-09 12:52:10 -0700 | [diff] [blame] | 56 | return ConnectionTethering::kUnknown; |
| 57 | } |
| 58 | |
Alex Deymo | 42c30c3 | 2014-04-24 18:41:18 -0700 | [diff] [blame] | 59 | bool RealShillProvider::Init() { |
Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 60 | ManagerProxyInterface* manager_proxy = shill_proxy_->GetManagerProxy(); |
| 61 | if (!manager_proxy) |
Gilad Arnold | 5ef9c48 | 2014-03-03 13:51:02 -0800 | [diff] [blame] | 62 | return false; |
Gilad Arnold | 55f39b7 | 2014-01-28 12:51:45 -0800 | [diff] [blame] | 63 | |
Gilad Arnold | beb39e9 | 2014-03-11 11:34:50 -0700 | [diff] [blame] | 64 | // Subscribe to the manager's PropertyChanged signal. |
Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 65 | manager_proxy->RegisterPropertyChangedSignalHandler( |
| 66 | base::Bind(&RealShillProvider::OnManagerPropertyChanged, |
| 67 | base::Unretained(this)), |
| 68 | base::Bind(&RealShillProvider::OnSignalConnected, |
| 69 | base::Unretained(this))); |
Gilad Arnold | 5ef9c48 | 2014-03-03 13:51:02 -0800 | [diff] [blame] | 70 | |
Gilad Arnold | ef120fa | 2014-04-09 12:52:10 -0700 | [diff] [blame] | 71 | // 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 Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 74 | 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 Arnold | 5ef9c48 | 2014-03-03 13:51:02 -0800 | [diff] [blame] | 84 | } |
| 85 | |
Gilad Arnold | ef120fa | 2014-04-09 12:52:10 -0700 | [diff] [blame] | 86 | return true; |
Gilad Arnold | 5ef9c48 | 2014-03-03 13:51:02 -0800 | [diff] [blame] | 87 | } |
| 88 | |
Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 89 | void RealShillProvider::OnManagerPropertyChanged(const string& name, |
| 90 | const chromeos::Any& value) { |
| 91 | if (name == shill::kDefaultServiceProperty) |
| 92 | ProcessDefaultService(value.TryGet<dbus::ObjectPath>().value()); |
Gilad Arnold | 5ef9c48 | 2014-03-03 13:51:02 -0800 | [diff] [blame] | 93 | } |
| 94 | |
Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 95 | void 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 Arnold | 5ef9c48 | 2014-03-03 13:51:02 -0800 | [diff] [blame] | 101 | } |
Gilad Arnold | 5ef9c48 | 2014-03-03 13:51:02 -0800 | [diff] [blame] | 102 | } |
| 103 | |
Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 104 | bool 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 Arnold | ef120fa | 2014-04-09 12:52:10 -0700 | [diff] [blame] | 109 | return true; |
| 110 | |
Gilad Arnold | d3df25f | 2014-04-22 08:39:48 -0700 | [diff] [blame] | 111 | // Update the connection status. |
Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 112 | default_service_path_ = default_service_path; |
Gilad Arnold | d3df25f | 2014-04-22 08:39:48 -0700 | [diff] [blame] | 113 | bool is_connected = (default_service_path_ != "/"); |
| 114 | var_is_connected_.SetValue(is_connected); |
| 115 | var_conn_last_changed_.SetValue(clock_->GetWallclockTime()); |
Gilad Arnold | ef120fa | 2014-04-09 12:52:10 -0700 | [diff] [blame] | 116 | |
Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 117 | if (!is_connected) { |
Gilad Arnold | d3df25f | 2014-04-22 08:39:48 -0700 | [diff] [blame] | 118 | var_conn_type_.UnsetValue(); |
| 119 | var_conn_tethering_.UnsetValue(); |
Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 120 | 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 Arnold | ef120fa | 2014-04-09 12:52:10 -0700 | [diff] [blame] | 175 | } |
Gilad Arnold | df3dd24 | 2014-04-09 07:15:51 -0700 | [diff] [blame] | 176 | |
Gilad Arnold | beb39e9 | 2014-03-11 11:34:50 -0700 | [diff] [blame] | 177 | return true; |
| 178 | } |
| 179 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 180 | } // namespace chromeos_update_manager |