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