Vitaly Buka | 58a288b | 2015-07-31 00:33:31 -0700 | [diff] [blame] | 1 | // Copyright 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 | |
| 5 | #include "buffet/shill_client.h" |
| 6 | |
| 7 | #include <set> |
| 8 | |
| 9 | #include <base/message_loop/message_loop.h> |
| 10 | #include <base/stl_util.h> |
Alex Vakulenko | 4170585 | 2015-10-13 10:12:06 -0700 | [diff] [blame] | 11 | #include <brillo/any.h> |
| 12 | #include <brillo/errors/error.h> |
Alex Vakulenko | 94f8eba | 2015-10-14 08:52:45 -0700 | [diff] [blame^] | 13 | #include <brillo/variant_dictionary.h> |
Peter Qiu | 786a906 | 2015-10-02 11:45:01 -0700 | [diff] [blame] | 14 | #include <dbus/shill/dbus-constants.h> |
Vitaly Buka | e2713ac | 2015-08-03 13:50:01 -0700 | [diff] [blame] | 15 | #include <weave/enum_to_string.h> |
Vitaly Buka | 58a288b | 2015-07-31 00:33:31 -0700 | [diff] [blame] | 16 | |
Vitaly Buka | 3ef4fff | 2015-07-31 01:12:07 -0700 | [diff] [blame] | 17 | #include "buffet/ap_manager_client.h" |
Vitaly Buka | 493f604 | 2015-08-12 16:17:16 -0700 | [diff] [blame] | 18 | #include "buffet/socket_stream.h" |
Vitaly Buka | 4f77153 | 2015-08-14 14:58:39 -0700 | [diff] [blame] | 19 | #include "buffet/weave_error_conversion.h" |
Vitaly Buka | 58a288b | 2015-07-31 00:33:31 -0700 | [diff] [blame] | 20 | |
Alex Vakulenko | 4170585 | 2015-10-13 10:12:06 -0700 | [diff] [blame] | 21 | using brillo::Any; |
| 22 | using brillo::VariantDictionary; |
Vitaly Buka | 58a288b | 2015-07-31 00:33:31 -0700 | [diff] [blame] | 23 | using dbus::ObjectPath; |
| 24 | using org::chromium::flimflam::DeviceProxy; |
| 25 | using org::chromium::flimflam::ServiceProxy; |
| 26 | using std::map; |
| 27 | using std::set; |
| 28 | using std::string; |
| 29 | using std::vector; |
Alex Vakulenko | e32375b | 2015-09-28 08:55:40 -0700 | [diff] [blame] | 30 | using weave::EnumToString; |
| 31 | using weave::provider::Network; |
Vitaly Buka | 58a288b | 2015-07-31 00:33:31 -0700 | [diff] [blame] | 32 | |
| 33 | namespace buffet { |
| 34 | |
| 35 | namespace { |
| 36 | |
Alex Vakulenko | e32375b | 2015-09-28 08:55:40 -0700 | [diff] [blame] | 37 | const char kErrorDomain[] = "buffet"; |
| 38 | |
Vitaly Buka | 58a288b | 2015-07-31 00:33:31 -0700 | [diff] [blame] | 39 | void IgnoreDetachEvent() {} |
| 40 | |
| 41 | bool GetStateForService(ServiceProxy* service, string* state) { |
| 42 | CHECK(service) << "|service| was nullptr in GetStateForService()"; |
| 43 | VariantDictionary properties; |
| 44 | if (!service->GetProperties(&properties, nullptr)) { |
| 45 | LOG(WARNING) << "Failed to read properties from service."; |
| 46 | return false; |
| 47 | } |
| 48 | auto property_it = properties.find(shill::kStateProperty); |
| 49 | if (property_it == properties.end()) { |
| 50 | LOG(WARNING) << "No state found in service properties."; |
| 51 | return false; |
| 52 | } |
| 53 | string new_state = property_it->second.TryGet<string>(); |
| 54 | if (new_state.empty()) { |
| 55 | LOG(WARNING) << "Invalid state value."; |
| 56 | return false; |
| 57 | } |
| 58 | *state = new_state; |
| 59 | return true; |
| 60 | } |
| 61 | |
Alex Vakulenko | e32375b | 2015-09-28 08:55:40 -0700 | [diff] [blame] | 62 | Network::State ShillServiceStateToNetworkState(const string& state) { |
Vitaly Buka | 58a288b | 2015-07-31 00:33:31 -0700 | [diff] [blame] | 63 | // TODO(wiley) What does "unconfigured" mean in a world with multiple sets |
| 64 | // of WiFi credentials? |
| 65 | // TODO(wiley) Detect disabled devices, update state appropriately. |
| 66 | if ((state.compare(shill::kStateReady) == 0) || |
| 67 | (state.compare(shill::kStatePortal) == 0) || |
| 68 | (state.compare(shill::kStateOnline) == 0)) { |
Alex Vakulenko | be39e93 | 2015-10-09 08:10:36 -0700 | [diff] [blame] | 69 | return Network::State::kOnline; |
Vitaly Buka | 58a288b | 2015-07-31 00:33:31 -0700 | [diff] [blame] | 70 | } |
| 71 | if ((state.compare(shill::kStateAssociation) == 0) || |
| 72 | (state.compare(shill::kStateConfiguration) == 0)) { |
Alex Vakulenko | e32375b | 2015-09-28 08:55:40 -0700 | [diff] [blame] | 73 | return Network::State::kConnecting; |
Vitaly Buka | 58a288b | 2015-07-31 00:33:31 -0700 | [diff] [blame] | 74 | } |
| 75 | if ((state.compare(shill::kStateFailure) == 0) || |
| 76 | (state.compare(shill::kStateActivationFailure) == 0)) { |
| 77 | // TODO(wiley) Get error information off the service object. |
Alex Vakulenko | be39e93 | 2015-10-09 08:10:36 -0700 | [diff] [blame] | 78 | return Network::State::kError; |
Vitaly Buka | 58a288b | 2015-07-31 00:33:31 -0700 | [diff] [blame] | 79 | } |
| 80 | if ((state.compare(shill::kStateIdle) == 0) || |
| 81 | (state.compare(shill::kStateOffline) == 0) || |
| 82 | (state.compare(shill::kStateDisconnect) == 0)) { |
Alex Vakulenko | e32375b | 2015-09-28 08:55:40 -0700 | [diff] [blame] | 83 | return Network::State::kOffline; |
Vitaly Buka | 58a288b | 2015-07-31 00:33:31 -0700 | [diff] [blame] | 84 | } |
| 85 | LOG(WARNING) << "Unknown state found: '" << state << "'"; |
Alex Vakulenko | e32375b | 2015-09-28 08:55:40 -0700 | [diff] [blame] | 86 | return Network::State::kOffline; |
Vitaly Buka | 58a288b | 2015-07-31 00:33:31 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | } // namespace |
| 90 | |
| 91 | ShillClient::ShillClient(const scoped_refptr<dbus::Bus>& bus, |
Alex Vakulenko | 0022b75 | 2015-10-02 11:09:59 -0700 | [diff] [blame] | 92 | const set<string>& device_whitelist, |
| 93 | bool disable_xmpp) |
Vitaly Buka | 58a288b | 2015-07-31 00:33:31 -0700 | [diff] [blame] | 94 | : bus_{bus}, |
Alex Vakulenko | e32375b | 2015-09-28 08:55:40 -0700 | [diff] [blame] | 95 | manager_proxy_{bus_}, |
Vitaly Buka | 3ef4fff | 2015-07-31 01:12:07 -0700 | [diff] [blame] | 96 | device_whitelist_{device_whitelist}, |
Alex Vakulenko | 0022b75 | 2015-10-02 11:09:59 -0700 | [diff] [blame] | 97 | disable_xmpp_{disable_xmpp}, |
Vitaly Buka | 3ef4fff | 2015-07-31 01:12:07 -0700 | [diff] [blame] | 98 | ap_manager_client_{new ApManagerClient(bus)} { |
Vitaly Buka | 58a288b | 2015-07-31 00:33:31 -0700 | [diff] [blame] | 99 | manager_proxy_.RegisterPropertyChangedSignalHandler( |
| 100 | base::Bind(&ShillClient::OnManagerPropertyChange, |
| 101 | weak_factory_.GetWeakPtr()), |
| 102 | base::Bind(&ShillClient::OnManagerPropertyChangeRegistration, |
| 103 | weak_factory_.GetWeakPtr())); |
| 104 | auto owner_changed_cb = base::Bind(&ShillClient::OnShillServiceOwnerChange, |
| 105 | weak_factory_.GetWeakPtr()); |
| 106 | bus_->GetObjectProxy(shill::kFlimflamServiceName, ObjectPath{"/"}) |
| 107 | ->SetNameOwnerChangedCallback(owner_changed_cb); |
| 108 | } |
| 109 | |
Vitaly Buka | 3ef4fff | 2015-07-31 01:12:07 -0700 | [diff] [blame] | 110 | ShillClient::~ShillClient() {} |
| 111 | |
Vitaly Buka | 58a288b | 2015-07-31 00:33:31 -0700 | [diff] [blame] | 112 | void ShillClient::Init() { |
| 113 | VLOG(2) << "ShillClient::Init();"; |
Alex Vakulenko | e32375b | 2015-09-28 08:55:40 -0700 | [diff] [blame] | 114 | CleanupConnectingService(); |
Vitaly Buka | 58a288b | 2015-07-31 00:33:31 -0700 | [diff] [blame] | 115 | devices_.clear(); |
Alex Vakulenko | e32375b | 2015-09-28 08:55:40 -0700 | [diff] [blame] | 116 | connectivity_state_ = Network::State::kOffline; |
Vitaly Buka | 58a288b | 2015-07-31 00:33:31 -0700 | [diff] [blame] | 117 | VariantDictionary properties; |
| 118 | if (!manager_proxy_.GetProperties(&properties, nullptr)) { |
| 119 | LOG(ERROR) << "Unable to get properties from Manager, waiting for " |
| 120 | "Manager to come back online."; |
| 121 | return; |
| 122 | } |
| 123 | auto it = properties.find(shill::kDevicesProperty); |
| 124 | CHECK(it != properties.end()) << "shill should always publish a device list."; |
| 125 | OnManagerPropertyChange(shill::kDevicesProperty, it->second); |
| 126 | } |
| 127 | |
Alex Vakulenko | e32375b | 2015-09-28 08:55:40 -0700 | [diff] [blame] | 128 | void ShillClient::Connect(const string& ssid, |
| 129 | const string& passphrase, |
Alex Vakulenko | 94f8eba | 2015-10-14 08:52:45 -0700 | [diff] [blame^] | 130 | const weave::DoneCallback& callback) { |
Alex Vakulenko | e32375b | 2015-09-28 08:55:40 -0700 | [diff] [blame] | 131 | if (connecting_service_) { |
| 132 | weave::ErrorPtr error; |
| 133 | weave::Error::AddTo(&error, FROM_HERE, kErrorDomain, "busy", |
| 134 | "Already connecting to WiFi network"); |
| 135 | base::MessageLoop::current()->PostTask( |
Alex Vakulenko | 94f8eba | 2015-10-14 08:52:45 -0700 | [diff] [blame^] | 136 | FROM_HERE, base::Bind(callback, base::Passed(&error))); |
Alex Vakulenko | e32375b | 2015-09-28 08:55:40 -0700 | [diff] [blame] | 137 | return; |
Vitaly Buka | 4f77153 | 2015-08-14 14:58:39 -0700 | [diff] [blame] | 138 | } |
Alex Vakulenko | e32375b | 2015-09-28 08:55:40 -0700 | [diff] [blame] | 139 | CleanupConnectingService(); |
Vitaly Buka | 58a288b | 2015-07-31 00:33:31 -0700 | [diff] [blame] | 140 | VariantDictionary service_properties; |
| 141 | service_properties[shill::kTypeProperty] = Any{string{shill::kTypeWifi}}; |
| 142 | service_properties[shill::kSSIDProperty] = Any{ssid}; |
Alex Vakulenko | e32375b | 2015-09-28 08:55:40 -0700 | [diff] [blame] | 143 | if (passphrase.empty()) { |
| 144 | service_properties[shill::kSecurityProperty] = Any{shill::kSecurityNone}; |
| 145 | } else { |
| 146 | service_properties[shill::kPassphraseProperty] = Any{passphrase}; |
| 147 | service_properties[shill::kSecurityProperty] = Any{shill::kSecurityPsk}; |
| 148 | } |
Vitaly Buka | 58a288b | 2015-07-31 00:33:31 -0700 | [diff] [blame] | 149 | service_properties[shill::kSaveCredentialsProperty] = Any{true}; |
| 150 | service_properties[shill::kAutoConnectProperty] = Any{true}; |
| 151 | ObjectPath service_path; |
Alex Vakulenko | 94f8eba | 2015-10-14 08:52:45 -0700 | [diff] [blame^] | 152 | brillo::ErrorPtr brillo_error; |
Vitaly Buka | 58a288b | 2015-07-31 00:33:31 -0700 | [diff] [blame] | 153 | if (!manager_proxy_.ConfigureService(service_properties, &service_path, |
Alex Vakulenko | 94f8eba | 2015-10-14 08:52:45 -0700 | [diff] [blame^] | 154 | &brillo_error) || |
| 155 | !manager_proxy_.RequestScan(shill::kTypeWifi, &brillo_error)) { |
Alex Vakulenko | e32375b | 2015-09-28 08:55:40 -0700 | [diff] [blame] | 156 | weave::ErrorPtr weave_error; |
Alex Vakulenko | 94f8eba | 2015-10-14 08:52:45 -0700 | [diff] [blame^] | 157 | ConvertError(*brillo_error, &weave_error); |
Alex Vakulenko | e32375b | 2015-09-28 08:55:40 -0700 | [diff] [blame] | 158 | base::MessageLoop::current()->PostTask( |
Alex Vakulenko | 94f8eba | 2015-10-14 08:52:45 -0700 | [diff] [blame^] | 159 | FROM_HERE, base::Bind(callback, base::Passed(&weave_error))); |
Alex Vakulenko | e32375b | 2015-09-28 08:55:40 -0700 | [diff] [blame] | 160 | return; |
Vitaly Buka | 58a288b | 2015-07-31 00:33:31 -0700 | [diff] [blame] | 161 | } |
| 162 | connecting_service_.reset(new ServiceProxy{bus_, service_path}); |
Alex Vakulenko | e32375b | 2015-09-28 08:55:40 -0700 | [diff] [blame] | 163 | connecting_service_->Connect(nullptr); |
Alex Vakulenko | 94f8eba | 2015-10-14 08:52:45 -0700 | [diff] [blame^] | 164 | connect_done_callback_ = callback; |
Vitaly Buka | 58a288b | 2015-07-31 00:33:31 -0700 | [diff] [blame] | 165 | connecting_service_->RegisterPropertyChangedSignalHandler( |
| 166 | base::Bind(&ShillClient::OnServicePropertyChange, |
| 167 | weak_factory_.GetWeakPtr(), service_path), |
| 168 | base::Bind(&ShillClient::OnServicePropertyChangeRegistration, |
| 169 | weak_factory_.GetWeakPtr(), service_path)); |
Alex Vakulenko | e32375b | 2015-09-28 08:55:40 -0700 | [diff] [blame] | 170 | base::MessageLoop::current()->PostDelayedTask( |
| 171 | FROM_HERE, base::Bind(&ShillClient::ConnectToServiceError, |
| 172 | weak_factory_.GetWeakPtr(), connecting_service_), |
| 173 | base::TimeDelta::FromMinutes(1)); |
Vitaly Buka | 58a288b | 2015-07-31 00:33:31 -0700 | [diff] [blame] | 174 | } |
| 175 | |
Alex Vakulenko | e32375b | 2015-09-28 08:55:40 -0700 | [diff] [blame] | 176 | void ShillClient::ConnectToServiceError( |
| 177 | std::shared_ptr<org::chromium::flimflam::ServiceProxy> connecting_service) { |
| 178 | if (connecting_service != connecting_service_ || |
Alex Vakulenko | 94f8eba | 2015-10-14 08:52:45 -0700 | [diff] [blame^] | 179 | connect_done_callback_.is_null()) { |
Alex Vakulenko | e32375b | 2015-09-28 08:55:40 -0700 | [diff] [blame] | 180 | return; |
| 181 | } |
| 182 | std::string error = have_called_connect_ ? connecting_service_error_ |
| 183 | : shill::kErrorOutOfRange; |
| 184 | if (error.empty()) |
| 185 | error = shill::kErrorInternal; |
| 186 | OnErrorChangeForConnectingService(error); |
| 187 | } |
| 188 | |
| 189 | Network::State ShillClient::GetConnectionState() const { |
Vitaly Buka | 58a288b | 2015-07-31 00:33:31 -0700 | [diff] [blame] | 190 | return connectivity_state_; |
| 191 | } |
| 192 | |
Alex Vakulenko | e32375b | 2015-09-28 08:55:40 -0700 | [diff] [blame] | 193 | void ShillClient::StartAccessPoint(const std::string& ssid) { |
Vitaly Buka | 3ef4fff | 2015-07-31 01:12:07 -0700 | [diff] [blame] | 194 | ap_manager_client_->Start(ssid); |
| 195 | } |
| 196 | |
Alex Vakulenko | e32375b | 2015-09-28 08:55:40 -0700 | [diff] [blame] | 197 | void ShillClient::StopAccessPoint() { |
Vitaly Buka | 3ef4fff | 2015-07-31 01:12:07 -0700 | [diff] [blame] | 198 | ap_manager_client_->Stop(); |
| 199 | } |
| 200 | |
Alex Vakulenko | e32375b | 2015-09-28 08:55:40 -0700 | [diff] [blame] | 201 | void ShillClient::AddConnectionChangedCallback( |
| 202 | const ConnectionChangedCallback& listener) { |
Vitaly Buka | 58a288b | 2015-07-31 00:33:31 -0700 | [diff] [blame] | 203 | connectivity_listeners_.push_back(listener); |
| 204 | } |
| 205 | |
| 206 | bool ShillClient::IsMonitoredDevice(DeviceProxy* device) { |
| 207 | if (device_whitelist_.empty()) { |
| 208 | return true; |
| 209 | } |
| 210 | VariantDictionary device_properties; |
| 211 | if (!device->GetProperties(&device_properties, nullptr)) { |
| 212 | LOG(ERROR) << "Devices without properties aren't whitelisted."; |
| 213 | return false; |
| 214 | } |
| 215 | auto it = device_properties.find(shill::kInterfaceProperty); |
| 216 | if (it == device_properties.end()) { |
| 217 | LOG(ERROR) << "Failed to find interface property in device properties."; |
| 218 | return false; |
| 219 | } |
| 220 | return ContainsKey(device_whitelist_, it->second.TryGet<string>()); |
| 221 | } |
| 222 | |
| 223 | void ShillClient::OnShillServiceOwnerChange(const string& old_owner, |
| 224 | const string& new_owner) { |
| 225 | VLOG(1) << "Shill service owner name changed to '" << new_owner << "'"; |
| 226 | if (new_owner.empty()) { |
Alex Vakulenko | e32375b | 2015-09-28 08:55:40 -0700 | [diff] [blame] | 227 | CleanupConnectingService(); |
Vitaly Buka | 58a288b | 2015-07-31 00:33:31 -0700 | [diff] [blame] | 228 | devices_.clear(); |
Alex Vakulenko | e32375b | 2015-09-28 08:55:40 -0700 | [diff] [blame] | 229 | connectivity_state_ = Network::State::kOffline; |
Vitaly Buka | 58a288b | 2015-07-31 00:33:31 -0700 | [diff] [blame] | 230 | } else { |
| 231 | Init(); // New service owner means shill reset! |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | void ShillClient::OnManagerPropertyChangeRegistration(const string& interface, |
| 236 | const string& signal_name, |
| 237 | bool success) { |
| 238 | VLOG(3) << "Registered ManagerPropertyChange handler."; |
| 239 | CHECK(success) << "privetd requires Manager signals."; |
| 240 | VariantDictionary properties; |
| 241 | if (!manager_proxy_.GetProperties(&properties, nullptr)) { |
| 242 | LOG(ERROR) << "Unable to get properties from Manager, waiting for " |
| 243 | "Manager to come back online."; |
| 244 | return; |
| 245 | } |
| 246 | auto it = properties.find(shill::kDevicesProperty); |
| 247 | CHECK(it != properties.end()) << "Shill should always publish a device list."; |
| 248 | OnManagerPropertyChange(shill::kDevicesProperty, it->second); |
| 249 | } |
| 250 | |
| 251 | void ShillClient::OnManagerPropertyChange(const string& property_name, |
| 252 | const Any& property_value) { |
| 253 | if (property_name != shill::kDevicesProperty) { |
| 254 | return; |
| 255 | } |
| 256 | VLOG(3) << "Manager's device list has changed."; |
| 257 | // We're going to remove every device we haven't seen in the update. |
| 258 | set<ObjectPath> device_paths_to_remove; |
| 259 | for (const auto& kv : devices_) { |
| 260 | device_paths_to_remove.insert(kv.first); |
| 261 | } |
| 262 | for (const auto& device_path : property_value.TryGet<vector<ObjectPath>>()) { |
| 263 | if (!device_path.IsValid()) { |
| 264 | LOG(ERROR) << "Ignoring invalid device path in Manager's device list."; |
| 265 | return; |
| 266 | } |
| 267 | auto it = devices_.find(device_path); |
| 268 | if (it != devices_.end()) { |
| 269 | // Found an existing proxy. Since the whitelist never changes, |
| 270 | // this still a valid device. |
| 271 | device_paths_to_remove.erase(device_path); |
| 272 | continue; |
| 273 | } |
| 274 | std::unique_ptr<DeviceProxy> device{new DeviceProxy{bus_, device_path}}; |
| 275 | if (!IsMonitoredDevice(device.get())) { |
| 276 | continue; |
| 277 | } |
| 278 | device->RegisterPropertyChangedSignalHandler( |
| 279 | base::Bind(&ShillClient::OnDevicePropertyChange, |
| 280 | weak_factory_.GetWeakPtr(), device_path), |
| 281 | base::Bind(&ShillClient::OnDevicePropertyChangeRegistration, |
| 282 | weak_factory_.GetWeakPtr(), device_path)); |
| 283 | VLOG(3) << "Creating device proxy at " << device_path.value(); |
| 284 | devices_[device_path].device = std::move(device); |
| 285 | } |
| 286 | // Clean up devices/services related to removed devices. |
| 287 | if (!device_paths_to_remove.empty()) { |
| 288 | for (const ObjectPath& device_path : device_paths_to_remove) { |
| 289 | devices_.erase(device_path); |
| 290 | } |
| 291 | UpdateConnectivityState(); |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | void ShillClient::OnDevicePropertyChangeRegistration( |
| 296 | const ObjectPath& device_path, |
| 297 | const string& interface, |
| 298 | const string& signal_name, |
| 299 | bool success) { |
| 300 | VLOG(3) << "Registered DevicePropertyChange handler."; |
| 301 | auto it = devices_.find(device_path); |
| 302 | if (it == devices_.end()) { |
| 303 | return; |
| 304 | } |
| 305 | CHECK(success) << "Failed to subscribe to Device property changes."; |
| 306 | DeviceProxy* device = it->second.device.get(); |
| 307 | VariantDictionary properties; |
| 308 | if (!device->GetProperties(&properties, nullptr)) { |
| 309 | LOG(WARNING) << "Failed to get device properties?"; |
| 310 | return; |
| 311 | } |
| 312 | auto prop_it = properties.find(shill::kSelectedServiceProperty); |
| 313 | if (prop_it == properties.end()) { |
| 314 | LOG(WARNING) << "Failed to get device's selected service?"; |
| 315 | return; |
| 316 | } |
| 317 | OnDevicePropertyChange(device_path, shill::kSelectedServiceProperty, |
| 318 | prop_it->second); |
| 319 | } |
| 320 | |
| 321 | void ShillClient::OnDevicePropertyChange(const ObjectPath& device_path, |
| 322 | const string& property_name, |
| 323 | const Any& property_value) { |
| 324 | // We only care about selected services anyway. |
| 325 | if (property_name != shill::kSelectedServiceProperty) { |
| 326 | return; |
| 327 | } |
| 328 | // If the device isn't our list of whitelisted devices, ignore it. |
| 329 | auto it = devices_.find(device_path); |
| 330 | if (it == devices_.end()) { |
| 331 | return; |
| 332 | } |
| 333 | DeviceState& device_state = it->second; |
| 334 | ObjectPath service_path{property_value.TryGet<ObjectPath>()}; |
| 335 | if (!service_path.IsValid()) { |
| 336 | LOG(ERROR) << "Device at " << device_path.value() |
| 337 | << " selected invalid service path."; |
| 338 | return; |
| 339 | } |
| 340 | VLOG(3) << "Device at " << it->first.value() << " has selected service at " |
| 341 | << service_path.value(); |
| 342 | bool removed_old_service{false}; |
| 343 | if (device_state.selected_service) { |
| 344 | if (device_state.selected_service->GetObjectPath() == service_path) { |
| 345 | return; // Spurious update? |
| 346 | } |
| 347 | device_state.selected_service.reset(); |
Alex Vakulenko | e32375b | 2015-09-28 08:55:40 -0700 | [diff] [blame] | 348 | device_state.service_state = Network::State::kOffline; |
Vitaly Buka | 58a288b | 2015-07-31 00:33:31 -0700 | [diff] [blame] | 349 | removed_old_service = true; |
| 350 | } |
| 351 | std::shared_ptr<ServiceProxy> new_service; |
| 352 | const bool reuse_connecting_service = |
| 353 | service_path.value() != "/" && connecting_service_ && |
| 354 | connecting_service_->GetObjectPath() == service_path; |
| 355 | if (reuse_connecting_service) { |
| 356 | new_service = connecting_service_; |
| 357 | // When we reuse the connecting service, we need to make sure that our |
| 358 | // cached state is correct. Normally, we do this by relying reading the |
| 359 | // state when our signal handlers finish registering, but this may have |
| 360 | // happened long in the past for the connecting service. |
| 361 | string state; |
| 362 | if (GetStateForService(new_service.get(), &state)) { |
| 363 | device_state.service_state = ShillServiceStateToNetworkState(state); |
| 364 | } else { |
| 365 | LOG(WARNING) << "Failed to read properties from existing service " |
| 366 | "on selection."; |
| 367 | } |
| 368 | } else if (service_path.value() != "/") { |
| 369 | // The device has selected a new service we haven't see before. |
| 370 | new_service.reset(new ServiceProxy{bus_, service_path}); |
| 371 | new_service->RegisterPropertyChangedSignalHandler( |
| 372 | base::Bind(&ShillClient::OnServicePropertyChange, |
| 373 | weak_factory_.GetWeakPtr(), service_path), |
| 374 | base::Bind(&ShillClient::OnServicePropertyChangeRegistration, |
| 375 | weak_factory_.GetWeakPtr(), service_path)); |
| 376 | } |
| 377 | device_state.selected_service = new_service; |
| 378 | if (reuse_connecting_service || removed_old_service) { |
| 379 | UpdateConnectivityState(); |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | void ShillClient::OnServicePropertyChangeRegistration(const ObjectPath& path, |
| 384 | const string& interface, |
| 385 | const string& signal_name, |
| 386 | bool success) { |
| 387 | VLOG(3) << "OnServicePropertyChangeRegistration(" << path.value() << ");"; |
| 388 | ServiceProxy* service{nullptr}; |
| 389 | if (connecting_service_ && connecting_service_->GetObjectPath() == path) { |
| 390 | // Note that the connecting service might also be a selected service. |
| 391 | service = connecting_service_.get(); |
Alex Vakulenko | e32375b | 2015-09-28 08:55:40 -0700 | [diff] [blame] | 392 | if (!success) |
| 393 | CleanupConnectingService(); |
Vitaly Buka | 58a288b | 2015-07-31 00:33:31 -0700 | [diff] [blame] | 394 | } else { |
| 395 | for (const auto& kv : devices_) { |
| 396 | if (kv.second.selected_service && |
| 397 | kv.second.selected_service->GetObjectPath() == path) { |
| 398 | service = kv.second.selected_service.get(); |
| 399 | break; |
| 400 | } |
| 401 | } |
| 402 | } |
| 403 | if (service == nullptr || !success) { |
| 404 | return; // A failure or success for a proxy we no longer care about. |
| 405 | } |
| 406 | VariantDictionary properties; |
| 407 | if (!service->GetProperties(&properties, nullptr)) { |
| 408 | return; |
| 409 | } |
| 410 | // Give ourselves property changed signals for the initial property |
| 411 | // values. |
Alex Vakulenko | e32375b | 2015-09-28 08:55:40 -0700 | [diff] [blame] | 412 | for (auto name : {shill::kStateProperty, shill::kSignalStrengthProperty, |
| 413 | shill::kErrorProperty}) { |
| 414 | auto it = properties.find(name); |
| 415 | if (it != properties.end()) |
| 416 | OnServicePropertyChange(path, name, it->second); |
Vitaly Buka | 58a288b | 2015-07-31 00:33:31 -0700 | [diff] [blame] | 417 | } |
| 418 | } |
| 419 | |
| 420 | void ShillClient::OnServicePropertyChange(const ObjectPath& service_path, |
| 421 | const string& property_name, |
| 422 | const Any& property_value) { |
| 423 | VLOG(3) << "ServicePropertyChange(" << service_path.value() << ", " |
| 424 | << property_name << ", ...);"; |
Alex Vakulenko | e32375b | 2015-09-28 08:55:40 -0700 | [diff] [blame] | 425 | |
| 426 | bool is_connecting_service = |
| 427 | connecting_service_ && |
| 428 | connecting_service_->GetObjectPath() == service_path; |
Vitaly Buka | 58a288b | 2015-07-31 00:33:31 -0700 | [diff] [blame] | 429 | if (property_name == shill::kStateProperty) { |
| 430 | const string state{property_value.TryGet<string>()}; |
| 431 | if (state.empty()) { |
| 432 | VLOG(3) << "Invalid service state update."; |
| 433 | return; |
| 434 | } |
| 435 | VLOG(3) << "New service state=" << state; |
| 436 | OnStateChangeForSelectedService(service_path, state); |
Alex Vakulenko | e32375b | 2015-09-28 08:55:40 -0700 | [diff] [blame] | 437 | if (is_connecting_service) |
| 438 | OnStateChangeForConnectingService(state); |
Vitaly Buka | 58a288b | 2015-07-31 00:33:31 -0700 | [diff] [blame] | 439 | } else if (property_name == shill::kSignalStrengthProperty) { |
Alex Vakulenko | e32375b | 2015-09-28 08:55:40 -0700 | [diff] [blame] | 440 | VLOG(3) << "Signal strength=" << property_value.TryGet<uint8_t>(); |
| 441 | if (is_connecting_service) |
| 442 | OnStrengthChangeForConnectingService(property_value.TryGet<uint8_t>()); |
| 443 | } else if (property_name == shill::kErrorProperty) { |
| 444 | VLOG(3) << "Error=" << property_value.TryGet<std::string>(); |
| 445 | if (is_connecting_service) |
| 446 | connecting_service_error_ = property_value.TryGet<std::string>(); |
Vitaly Buka | 58a288b | 2015-07-31 00:33:31 -0700 | [diff] [blame] | 447 | } |
| 448 | } |
| 449 | |
Alex Vakulenko | e32375b | 2015-09-28 08:55:40 -0700 | [diff] [blame] | 450 | void ShillClient::OnStateChangeForConnectingService(const string& state) { |
| 451 | switch (ShillServiceStateToNetworkState(state)) { |
Alex Vakulenko | be39e93 | 2015-10-09 08:10:36 -0700 | [diff] [blame] | 452 | case Network::State::kOnline: { |
Alex Vakulenko | 94f8eba | 2015-10-14 08:52:45 -0700 | [diff] [blame^] | 453 | auto callback = connect_done_callback_; |
| 454 | connect_done_callback_.Reset(); |
Alex Vakulenko | e32375b | 2015-09-28 08:55:40 -0700 | [diff] [blame] | 455 | CleanupConnectingService(); |
| 456 | |
| 457 | if (!callback.is_null()) |
Alex Vakulenko | 94f8eba | 2015-10-14 08:52:45 -0700 | [diff] [blame^] | 458 | callback.Run(nullptr); |
Alex Vakulenko | e32375b | 2015-09-28 08:55:40 -0700 | [diff] [blame] | 459 | break; |
| 460 | } |
Alex Vakulenko | be39e93 | 2015-10-09 08:10:36 -0700 | [diff] [blame] | 461 | case Network::State::kError: { |
Alex Vakulenko | e32375b | 2015-09-28 08:55:40 -0700 | [diff] [blame] | 462 | ConnectToServiceError(connecting_service_); |
| 463 | break; |
| 464 | } |
| 465 | case Network::State::kOffline: |
| 466 | case Network::State::kConnecting: |
| 467 | break; |
Vitaly Buka | 58a288b | 2015-07-31 00:33:31 -0700 | [diff] [blame] | 468 | } |
Alex Vakulenko | e32375b | 2015-09-28 08:55:40 -0700 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | void ShillClient::OnErrorChangeForConnectingService(const std::string& error) { |
| 472 | if (error.empty()) |
| 473 | return; |
| 474 | |
Alex Vakulenko | 94f8eba | 2015-10-14 08:52:45 -0700 | [diff] [blame^] | 475 | auto callback = connect_done_callback_; |
Alex Vakulenko | e32375b | 2015-09-28 08:55:40 -0700 | [diff] [blame] | 476 | CleanupConnectingService(); |
| 477 | |
| 478 | weave::ErrorPtr weave_error; |
| 479 | weave::Error::AddTo(&weave_error, FROM_HERE, kErrorDomain, error, |
| 480 | "Failed to connect to WiFi network"); |
| 481 | |
| 482 | if (!callback.is_null()) |
Alex Vakulenko | 297114c | 2015-10-12 13:45:43 -0700 | [diff] [blame] | 483 | callback.Run(std::move(weave_error)); |
Vitaly Buka | 58a288b | 2015-07-31 00:33:31 -0700 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | void ShillClient::OnStrengthChangeForConnectingService( |
Vitaly Buka | 58a288b | 2015-07-31 00:33:31 -0700 | [diff] [blame] | 487 | uint8_t signal_strength) { |
Alex Vakulenko | e32375b | 2015-09-28 08:55:40 -0700 | [diff] [blame] | 488 | if (signal_strength == 0 || have_called_connect_) { |
Vitaly Buka | 58a288b | 2015-07-31 00:33:31 -0700 | [diff] [blame] | 489 | return; |
| 490 | } |
| 491 | VLOG(1) << "Connecting service has signal. Calling Connect()."; |
| 492 | have_called_connect_ = true; |
| 493 | // Failures here indicate that we've already connected, |
| 494 | // or are connecting, or some other very unexciting thing. |
| 495 | // Ignore all that, and rely on state changes to detect |
| 496 | // connectivity. |
| 497 | connecting_service_->Connect(nullptr); |
| 498 | } |
| 499 | |
| 500 | void ShillClient::OnStateChangeForSelectedService( |
| 501 | const ObjectPath& service_path, |
| 502 | const string& state) { |
| 503 | // Find the device/service pair responsible for this update |
| 504 | VLOG(3) << "State for potentially selected service " << service_path.value() |
| 505 | << " have changed to " << state; |
| 506 | for (auto& kv : devices_) { |
| 507 | if (kv.second.selected_service && |
| 508 | kv.second.selected_service->GetObjectPath() == service_path) { |
| 509 | VLOG(3) << "Updated cached connection state for selected service."; |
| 510 | kv.second.service_state = ShillServiceStateToNetworkState(state); |
| 511 | UpdateConnectivityState(); |
| 512 | return; |
| 513 | } |
| 514 | } |
| 515 | } |
| 516 | |
| 517 | void ShillClient::UpdateConnectivityState() { |
| 518 | // Update the connectivity state of the device by picking the |
| 519 | // state of the currently most connected selected service. |
Alex Vakulenko | e32375b | 2015-09-28 08:55:40 -0700 | [diff] [blame] | 520 | Network::State new_connectivity_state{Network::State::kOffline}; |
Vitaly Buka | 58a288b | 2015-07-31 00:33:31 -0700 | [diff] [blame] | 521 | for (const auto& kv : devices_) { |
| 522 | if (kv.second.service_state > new_connectivity_state) { |
| 523 | new_connectivity_state = kv.second.service_state; |
| 524 | } |
| 525 | } |
| 526 | VLOG(1) << "Connectivity changed: " << EnumToString(connectivity_state_) |
| 527 | << " -> " << EnumToString(new_connectivity_state); |
| 528 | // Notify listeners even if state changed to the same value. Listeners may |
| 529 | // want to handle this event. |
| 530 | connectivity_state_ = new_connectivity_state; |
| 531 | // We may call UpdateConnectivityState whenever we mutate a data structure |
| 532 | // such that our connectivity status could change. However, we don't want |
| 533 | // to allow people to call into ShillClient while some other operation is |
| 534 | // underway. Therefore, call our callbacks later, when we're in a good |
| 535 | // state. |
| 536 | base::MessageLoop::current()->PostTask( |
Alex Vakulenko | 297114c | 2015-10-12 13:45:43 -0700 | [diff] [blame] | 537 | FROM_HERE, base::Bind(&ShillClient::NotifyConnectivityListeners, |
| 538 | weak_factory_.GetWeakPtr(), |
| 539 | GetConnectionState() == Network::State::kOnline)); |
Vitaly Buka | 58a288b | 2015-07-31 00:33:31 -0700 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | void ShillClient::NotifyConnectivityListeners(bool am_online) { |
| 543 | VLOG(3) << "Notifying connectivity listeners that online=" << am_online; |
Alex Vakulenko | e32375b | 2015-09-28 08:55:40 -0700 | [diff] [blame] | 544 | for (const auto& listener : connectivity_listeners_) |
| 545 | listener.Run(); |
Vitaly Buka | 58a288b | 2015-07-31 00:33:31 -0700 | [diff] [blame] | 546 | } |
| 547 | |
Alex Vakulenko | e32375b | 2015-09-28 08:55:40 -0700 | [diff] [blame] | 548 | void ShillClient::CleanupConnectingService() { |
Vitaly Buka | 58a288b | 2015-07-31 00:33:31 -0700 | [diff] [blame] | 549 | if (connecting_service_) { |
| 550 | connecting_service_->ReleaseObjectProxy(base::Bind(&IgnoreDetachEvent)); |
| 551 | connecting_service_.reset(); |
| 552 | } |
Alex Vakulenko | 94f8eba | 2015-10-14 08:52:45 -0700 | [diff] [blame^] | 553 | connect_done_callback_.Reset(); |
Vitaly Buka | 58a288b | 2015-07-31 00:33:31 -0700 | [diff] [blame] | 554 | have_called_connect_ = false; |
Vitaly Buka | 58a288b | 2015-07-31 00:33:31 -0700 | [diff] [blame] | 555 | } |
| 556 | |
Alex Vakulenko | 94f8eba | 2015-10-14 08:52:45 -0700 | [diff] [blame^] | 557 | void ShillClient::OpenSslSocket(const std::string& host, |
| 558 | uint16_t port, |
| 559 | const OpenSslSocketCallback& callback) { |
Alex Vakulenko | 0022b75 | 2015-10-02 11:09:59 -0700 | [diff] [blame] | 560 | if (disable_xmpp_) |
| 561 | return; |
Alex Vakulenko | e32375b | 2015-09-28 08:55:40 -0700 | [diff] [blame] | 562 | std::unique_ptr<weave::Stream> raw_stream{ |
| 563 | SocketStream::ConnectBlocking(host, port)}; |
| 564 | if (!raw_stream) { |
Alex Vakulenko | 4170585 | 2015-10-13 10:12:06 -0700 | [diff] [blame] | 565 | brillo::ErrorPtr error; |
| 566 | brillo::errors::system::AddSystemError(&error, FROM_HERE, errno); |
Alex Vakulenko | e32375b | 2015-09-28 08:55:40 -0700 | [diff] [blame] | 567 | weave::ErrorPtr weave_error; |
| 568 | ConvertError(*error.get(), &weave_error); |
| 569 | base::MessageLoop::current()->PostTask( |
Alex Vakulenko | 94f8eba | 2015-10-14 08:52:45 -0700 | [diff] [blame^] | 570 | FROM_HERE, base::Bind(callback, nullptr, base::Passed(&weave_error))); |
Alex Vakulenko | e32375b | 2015-09-28 08:55:40 -0700 | [diff] [blame] | 571 | return; |
| 572 | } |
| 573 | |
Alex Vakulenko | 94f8eba | 2015-10-14 08:52:45 -0700 | [diff] [blame^] | 574 | SocketStream::TlsConnect(std::move(raw_stream), host, callback); |
Vitaly Buka | 493f604 | 2015-08-12 16:17:16 -0700 | [diff] [blame] | 575 | } |
| 576 | |
Vitaly Buka | 58a288b | 2015-07-31 00:33:31 -0700 | [diff] [blame] | 577 | } // namespace buffet |