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