Arman Uguray | 2717a10 | 2013-01-29 23:36:06 -0800 | [diff] [blame] | 1 | // Copyright (c) 2013 The Chromium OS Authors. All rights reserved. |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Chris Masone | b2e326b | 2011-07-12 13:28:51 -0700 | [diff] [blame] | 5 | #include "shill/service.h" |
| 6 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 7 | #include <time.h> |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 8 | #include <stdio.h> |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 9 | |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 10 | #include <map> |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 11 | #include <string> |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 12 | #include <vector> |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 13 | |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 14 | #include <base/memory/scoped_ptr.h> |
mukesh agrawal | 51a7e93 | 2011-07-27 16:18:26 -0700 | [diff] [blame] | 15 | #include <base/string_number_conversions.h> |
Paul Stewart | 2bf424f | 2012-04-11 18:59:39 -0700 | [diff] [blame] | 16 | #include <base/stringprintf.h> |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 17 | #include <chromeos/dbus/service_constants.h> |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 18 | |
Paul Stewart | be5f5b3 | 2011-12-07 17:11:11 -0800 | [diff] [blame] | 19 | #include "shill/connection.h" |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 20 | #include "shill/control_interface.h" |
Darin Petkov | 385b9bc | 2012-12-03 15:25:05 +0100 | [diff] [blame] | 21 | #include "shill/diagnostics_reporter.h" |
Paul Stewart | c43cbbe | 2013-04-11 06:29:30 -0700 | [diff] [blame] | 22 | #include "shill/eap_credentials.h" |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 23 | #include "shill/error.h" |
Paul Stewart | be5f5b3 | 2011-12-07 17:11:11 -0800 | [diff] [blame] | 24 | #include "shill/http_proxy.h" |
Christopher Wiley | b691efd | 2012-08-09 13:51:51 -0700 | [diff] [blame] | 25 | #include "shill/logging.h" |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 26 | #include "shill/manager.h" |
Thieu Le | 48e6d6d | 2011-12-06 00:40:27 +0000 | [diff] [blame] | 27 | #include "shill/metrics.h" |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 28 | #include "shill/profile.h" |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 29 | #include "shill/property_accessor.h" |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 30 | #include "shill/refptr_types.h" |
Chris Masone | d7732e4 | 2011-05-20 11:08:56 -0700 | [diff] [blame] | 31 | #include "shill/service_dbus_adaptor.h" |
Darin Petkov | 5eb0542 | 2012-05-11 15:45:25 +0200 | [diff] [blame] | 32 | #include "shill/sockets.h" |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 33 | #include "shill/store_interface.h" |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 34 | |
Christopher Wiley | 0801d19 | 2012-09-24 11:57:15 -0700 | [diff] [blame] | 35 | using base::Bind; |
Darin Petkov | 385b9bc | 2012-12-03 15:25:05 +0100 | [diff] [blame] | 36 | using std::deque; |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 37 | using std::map; |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 38 | using std::string; |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 39 | using std::vector; |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 40 | |
| 41 | namespace shill { |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 42 | |
mukesh agrawal | 03c15ce | 2012-11-29 17:36:21 -0800 | [diff] [blame] | 43 | const char Service::kAutoConnBusy[] = "busy"; |
mukesh agrawal | bf14e94 | 2012-03-02 14:36:34 -0800 | [diff] [blame] | 44 | const char Service::kAutoConnConnected[] = "connected"; |
| 45 | const char Service::kAutoConnConnecting[] = "connecting"; |
| 46 | const char Service::kAutoConnExplicitDisconnect[] = "explicitly disconnected"; |
| 47 | const char Service::kAutoConnNotConnectable[] = "not connectable"; |
Darin Petkov | 4cbff5b | 2013-01-29 16:29:05 +0100 | [diff] [blame] | 48 | const char Service::kAutoConnOffline[] = "offline"; |
Christopher Wiley | 0801d19 | 2012-09-24 11:57:15 -0700 | [diff] [blame] | 49 | const char Service::kAutoConnThrottled[] = "throttled"; |
mukesh agrawal | bf14e94 | 2012-03-02 14:36:34 -0800 | [diff] [blame] | 50 | |
Paul Stewart | bc6e739 | 2012-05-24 07:07:48 -0700 | [diff] [blame] | 51 | const size_t Service::kEAPMaxCertificationElements = 10; |
| 52 | |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 53 | const char Service::kCheckPortalAuto[] = "auto"; |
| 54 | const char Service::kCheckPortalFalse[] = "false"; |
| 55 | const char Service::kCheckPortalTrue[] = "true"; |
| 56 | |
Darin Petkov | aba8932 | 2013-03-11 14:48:22 +0100 | [diff] [blame] | 57 | const char Service::kErrorDetailsNone[] = ""; |
| 58 | |
Paul Stewart | ac4ac00 | 2011-08-26 12:04:26 -0700 | [diff] [blame] | 59 | const int Service::kPriorityNone = 0; |
| 60 | |
Darin Petkov | 2f903b3 | 2012-04-18 12:56:43 +0200 | [diff] [blame] | 61 | const char Service::kServiceSortAutoConnect[] = "AutoConnect"; |
| 62 | const char Service::kServiceSortConnectable[] = "Connectable"; |
Paul Stewart | df3c0a8 | 2012-11-09 15:54:33 -0800 | [diff] [blame] | 63 | const char Service::kServiceSortDependency[] = "Dependency"; |
Paul Stewart | 2da34c0 | 2013-10-17 15:28:56 -0700 | [diff] [blame] | 64 | const char Service::kServiceSortHasEverConnected[] = "HasEverConnected"; |
mukesh agrawal | ddc378f | 2012-02-17 18:26:20 -0800 | [diff] [blame] | 65 | const char Service::kServiceSortIsConnected[] = "IsConnected"; |
| 66 | const char Service::kServiceSortIsConnecting[] = "IsConnecting"; |
| 67 | const char Service::kServiceSortIsFailed[] = "IsFailed"; |
Paul Stewart | a121c44 | 2012-06-09 14:12:58 -0700 | [diff] [blame] | 68 | const char Service::kServiceSortIsPortalled[] = "IsPortal"; |
Darin Petkov | 2f903b3 | 2012-04-18 12:56:43 +0200 | [diff] [blame] | 69 | const char Service::kServiceSortPriority[] = "Priority"; |
mukesh agrawal | ddc378f | 2012-02-17 18:26:20 -0800 | [diff] [blame] | 70 | const char Service::kServiceSortSecurityEtc[] = "SecurityEtc"; |
mukesh agrawal | e37ad32 | 2013-10-08 16:33:56 -0700 | [diff] [blame] | 71 | const char Service::kServiceSortSerialNumber[] = "SerialNumber"; |
Darin Petkov | 2f903b3 | 2012-04-18 12:56:43 +0200 | [diff] [blame] | 72 | const char Service::kServiceSortTechnology[] = "Technology"; |
mukesh agrawal | ddc378f | 2012-02-17 18:26:20 -0800 | [diff] [blame] | 73 | |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 74 | const char Service::kStorageAutoConnect[] = "AutoConnect"; |
| 75 | const char Service::kStorageCheckPortal[] = "CheckPortal"; |
Paul Stewart | 0756db9 | 2012-01-27 08:34:47 -0800 | [diff] [blame] | 76 | const char Service::kStorageError[] = "Error"; |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 77 | const char Service::kStorageFavorite[] = "Favorite"; |
Paul Stewart | 0756db9 | 2012-01-27 08:34:47 -0800 | [diff] [blame] | 78 | const char Service::kStorageGUID[] = "GUID"; |
mukesh agrawal | cf24a24 | 2012-05-21 16:46:11 -0700 | [diff] [blame] | 79 | const char Service::kStorageHasEverConnected[] = "HasEverConnected"; |
Paul Stewart | dded007 | 2013-10-24 12:38:54 -0700 | [diff] [blame] | 80 | const char Service::kStorageLastDHCPOptionFailure[] = "LastDHCPOptionFailure"; |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 81 | const char Service::kStorageName[] = "Name"; |
| 82 | const char Service::kStoragePriority[] = "Priority"; |
| 83 | const char Service::kStorageProxyConfig[] = "ProxyConfig"; |
| 84 | const char Service::kStorageSaveCredentials[] = "SaveCredentials"; |
Paul Stewart | 2706aaf | 2011-12-14 16:44:04 -0800 | [diff] [blame] | 85 | const char Service::kStorageType[] = "Type"; |
Paul Stewart | 987e71e | 2011-12-05 09:45:06 -0800 | [diff] [blame] | 86 | const char Service::kStorageUIData[] = "UIData"; |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 87 | |
mukesh agrawal | 8f3f775 | 2012-02-17 19:42:09 -0800 | [diff] [blame] | 88 | const uint8 Service::kStrengthMax = 100; |
| 89 | const uint8 Service::kStrengthMin = 0; |
| 90 | |
Christopher Wiley | 0801d19 | 2012-09-24 11:57:15 -0700 | [diff] [blame] | 91 | const uint64 Service::kMaxAutoConnectCooldownTimeMilliseconds = 30 * 60 * 1000; |
| 92 | const uint64 Service::kMinAutoConnectCooldownTimeMilliseconds = 1000; |
| 93 | const uint64 Service::kAutoConnectCooldownBackoffFactor = 2; |
| 94 | |
Darin Petkov | 385b9bc | 2012-12-03 15:25:05 +0100 | [diff] [blame] | 95 | const int Service::kDisconnectsMonitorSeconds = 5 * 60; |
| 96 | const int Service::kMisconnectsMonitorSeconds = 5 * 60; |
| 97 | const int Service::kReportDisconnectsThreshold = 2; |
| 98 | const int Service::kReportMisconnectsThreshold = 3; |
| 99 | const int Service::kMaxDisconnectEventHistory = 20; |
Paul Stewart | dded007 | 2013-10-24 12:38:54 -0700 | [diff] [blame] | 100 | const int Service::kMaxDHCPOptionFailures = 2; |
| 101 | |
| 102 | // If we encounter MTU issues, hold off sending a full DHCP request for 30 days. |
| 103 | const int Service::kDHCPOptionHoldOffPeriodSeconds = 30 * 24 * 60 * 60; |
Darin Petkov | 385b9bc | 2012-12-03 15:25:05 +0100 | [diff] [blame] | 104 | |
mukesh agrawal | 51a7e93 | 2011-07-27 16:18:26 -0700 | [diff] [blame] | 105 | // static |
mukesh agrawal | e37ad32 | 2013-10-08 16:33:56 -0700 | [diff] [blame] | 106 | unsigned int Service::next_serial_number_ = 0; |
mukesh agrawal | 51a7e93 | 2011-07-27 16:18:26 -0700 | [diff] [blame] | 107 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 108 | Service::Service(ControlInterface *control_interface, |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 109 | EventDispatcher *dispatcher, |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 110 | Metrics *metrics, |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 111 | Manager *manager, |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 112 | Technology::Identifier technology) |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 113 | : weak_ptr_factory_(this), |
| 114 | state_(kStateIdle), |
Darin Petkov | c8d91e5 | 2013-01-21 11:43:47 +0100 | [diff] [blame] | 115 | previous_state_(kStateIdle), |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 116 | failure_(kFailureUnknown), |
| 117 | auto_connect_(false), |
Paul Stewart | 2da34c0 | 2013-10-17 15:28:56 -0700 | [diff] [blame] | 118 | retain_auto_connect_(false), |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 119 | check_portal_(kCheckPortalAuto), |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 120 | connectable_(false), |
Paul Stewart | f2d6091 | 2012-07-15 08:37:30 -0700 | [diff] [blame] | 121 | error_(ConnectFailureToString(failure_)), |
Darin Petkov | aba8932 | 2013-03-11 14:48:22 +0100 | [diff] [blame] | 122 | error_details_(kErrorDetailsNone), |
mukesh agrawal | adb6848 | 2012-01-17 16:31:51 -0800 | [diff] [blame] | 123 | explicitly_disconnected_(false), |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 124 | priority_(kPriorityNone), |
mukesh agrawal | 43970a2 | 2013-02-15 16:00:07 -0800 | [diff] [blame] | 125 | crypto_algorithm_(kCryptoNone), |
| 126 | key_rotation_(false), |
| 127 | endpoint_auth_(false), |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 128 | strength_(0), |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 129 | save_credentials_(true), |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 130 | technology_(technology), |
mukesh agrawal | 568b5c6 | 2012-02-28 14:44:47 -0800 | [diff] [blame] | 131 | failed_time_(0), |
mukesh agrawal | cf24a24 | 2012-05-21 16:46:11 -0700 | [diff] [blame] | 132 | has_ever_connected_(false), |
Christopher Wiley | 0801d19 | 2012-09-24 11:57:15 -0700 | [diff] [blame] | 133 | auto_connect_cooldown_milliseconds_(0), |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 134 | store_(PropertyStore::PropertyChangeCallback( |
| 135 | base::Bind(&Service::OnPropertyChanged, |
| 136 | weak_ptr_factory_.GetWeakPtr()))), |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 137 | dispatcher_(dispatcher), |
mukesh agrawal | e37ad32 | 2013-10-08 16:33:56 -0700 | [diff] [blame] | 138 | serial_number_(next_serial_number_++), |
| 139 | unique_name_(base::UintToString(serial_number_)), |
mukesh agrawal | d835b20 | 2011-10-07 15:26:47 -0700 | [diff] [blame] | 140 | friendly_name_(unique_name_), |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 141 | adaptor_(control_interface->CreateServiceAdaptor(this)), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 142 | metrics_(metrics), |
Darin Petkov | 5eb0542 | 2012-05-11 15:45:25 +0200 | [diff] [blame] | 143 | manager_(manager), |
Christopher Wiley | 0801d19 | 2012-09-24 11:57:15 -0700 | [diff] [blame] | 144 | sockets_(new Sockets()), |
Darin Petkov | 385b9bc | 2012-12-03 15:25:05 +0100 | [diff] [blame] | 145 | time_(Time::GetInstance()), |
Paul Stewart | dded007 | 2013-10-24 12:38:54 -0700 | [diff] [blame] | 146 | diagnostics_reporter_(DiagnosticsReporter::GetInstance()), |
| 147 | consecutive_dhcp_failures_(0), |
| 148 | dhcp_option_failure_state_(kDHCPOptionFailureNotDetected) { |
Ben Chan | 923a502 | 2013-09-20 11:23:23 -0700 | [diff] [blame] | 149 | HelpRegisterDerivedBool(kAutoConnectProperty, |
Thieu Le | 284fe79 | 2012-01-31 17:53:19 -0800 | [diff] [blame] | 150 | &Service::GetAutoConnect, |
Paul Stewart | 43d8dc0 | 2013-10-17 10:32:53 -0700 | [diff] [blame] | 151 | &Service::SetAutoConnectFull, |
| 152 | &Service::ClearAutoConnect); |
Chris Masone | 4d42df8 | 2011-07-02 17:09:39 -0700 | [diff] [blame] | 153 | |
Ben Chan | 408a856 | 2014-02-05 00:02:18 -0800 | [diff] [blame^] | 154 | // kActivateOverNonCellularNetworkProperty: Registered in CellularService |
Ben Chan | 923a502 | 2013-09-20 11:23:23 -0700 | [diff] [blame] | 155 | // kActivationStateProperty: Registered in CellularService |
| 156 | // kCellularApnProperty: Registered in CellularService |
| 157 | // kCellularLastGoodApnProperty: Registered in CellularService |
| 158 | // kNetworkTechnologyProperty: Registered in CellularService |
Ben Chan | 408a856 | 2014-02-05 00:02:18 -0800 | [diff] [blame^] | 159 | // kOutOfCreditsProperty: Registered in CellularService |
| 160 | // kPaymentPortalProperty: Registered in CellularService |
Ben Chan | 923a502 | 2013-09-20 11:23:23 -0700 | [diff] [blame] | 161 | // kRoamingStateProperty: Registered in CellularService |
| 162 | // kServingOperatorProperty: Registered in CellularService |
Ben Chan | 408a856 | 2014-02-05 00:02:18 -0800 | [diff] [blame^] | 163 | // kUsageURLProperty: Registered in CellularService |
| 164 | // kCellularPPPUsernameProperty: Registered in CellularService |
| 165 | // kCellularPPPPasswordProperty: Registered in CellularService |
| 166 | |
| 167 | // kNetworkIdProperty: Registered in WiMaxService |
Chris Masone | 4d42df8 | 2011-07-02 17:09:39 -0700 | [diff] [blame] | 168 | |
Ben Chan | 923a502 | 2013-09-20 11:23:23 -0700 | [diff] [blame] | 169 | HelpRegisterDerivedString(kCheckPortalProperty, |
Paul Stewart | d215af6 | 2012-04-24 23:25:50 -0700 | [diff] [blame] | 170 | &Service::GetCheckPortal, |
| 171 | &Service::SetCheckPortal); |
Ben Chan | 923a502 | 2013-09-20 11:23:23 -0700 | [diff] [blame] | 172 | store_.RegisterConstBool(kConnectableProperty, &connectable_); |
| 173 | HelpRegisterConstDerivedRpcIdentifier(kDeviceProperty, |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 174 | &Service::GetDeviceRpcId); |
Paul Stewart | c43cbbe | 2013-04-11 06:29:30 -0700 | [diff] [blame] | 175 | store_.RegisterConstStrings(kEapRemoteCertificationProperty, |
| 176 | &remote_certification_); |
Ben Chan | 923a502 | 2013-09-20 11:23:23 -0700 | [diff] [blame] | 177 | HelpRegisterDerivedString(kGuidProperty, |
mukesh agrawal | cbfb34e | 2013-04-17 19:33:25 -0700 | [diff] [blame] | 178 | &Service::GetGuid, |
| 179 | &Service::SetGuid); |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 180 | |
Eric Shienbrood | cc95c5d | 2012-03-30 15:25:49 -0400 | [diff] [blame] | 181 | // TODO(ers): in flimflam clearing Error has the side-effect of |
| 182 | // setting the service state to IDLE. Is this important? I could |
| 183 | // see an autotest depending on it. |
Ben Chan | 923a502 | 2013-09-20 11:23:23 -0700 | [diff] [blame] | 184 | store_.RegisterConstString(kErrorProperty, &error_); |
Ben Chan | 39a7beb | 2013-09-21 11:28:00 -0700 | [diff] [blame] | 185 | store_.RegisterConstString(kErrorDetailsProperty, &error_details_); |
Ben Chan | 39a7beb | 2013-09-21 11:28:00 -0700 | [diff] [blame] | 186 | HelpRegisterConstDerivedUint16(kHTTPProxyPortProperty, |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 187 | &Service::GetHTTPProxyPort); |
Ben Chan | 39a7beb | 2013-09-21 11:28:00 -0700 | [diff] [blame] | 188 | HelpRegisterConstDerivedRpcIdentifier(kIPConfigProperty, |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 189 | &Service::GetIPConfigRpcIdentifier); |
Paul Stewart | 43d8dc0 | 2013-10-17 10:32:53 -0700 | [diff] [blame] | 190 | HelpRegisterDerivedBool(kIsActiveProperty, &Service::IsActive, NULL, NULL); |
Ben Chan | 923a502 | 2013-09-20 11:23:23 -0700 | [diff] [blame] | 191 | // kModeProperty: Registered in WiFiService |
Paul Stewart | 0c43833 | 2012-04-11 07:55:27 -0700 | [diff] [blame] | 192 | |
Ben Chan | 923a502 | 2013-09-20 11:23:23 -0700 | [diff] [blame] | 193 | HelpRegisterDerivedString(kNameProperty, |
Paul Stewart | 0c43833 | 2012-04-11 07:55:27 -0700 | [diff] [blame] | 194 | &Service::GetNameProperty, |
Darin Petkov | 9c6e981 | 2013-03-26 13:49:07 +0100 | [diff] [blame] | 195 | &Service::SetNameProperty); |
Ben Chan | 923a502 | 2013-09-20 11:23:23 -0700 | [diff] [blame] | 196 | // kPassphraseProperty: Registered in WiFiService |
Ben Chan | 408a856 | 2014-02-05 00:02:18 -0800 | [diff] [blame^] | 197 | // kPassphraseRequiredProperty: Registered in WiFiService, WiMaxService |
Ben Chan | 923a502 | 2013-09-20 11:23:23 -0700 | [diff] [blame] | 198 | HelpRegisterDerivedInt32(kPriorityProperty, |
mukesh agrawal | cbfb34e | 2013-04-17 19:33:25 -0700 | [diff] [blame] | 199 | &Service::GetPriority, |
| 200 | &Service::SetPriority); |
Ben Chan | 923a502 | 2013-09-20 11:23:23 -0700 | [diff] [blame] | 201 | HelpRegisterDerivedString(kProfileProperty, |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 202 | &Service::GetProfileRpcId, |
Paul Stewart | 1b1a7f2 | 2012-01-06 16:24:06 -0800 | [diff] [blame] | 203 | &Service::SetProfileRpcId); |
Ben Chan | 923a502 | 2013-09-20 11:23:23 -0700 | [diff] [blame] | 204 | HelpRegisterDerivedString(kProxyConfigProperty, |
Philipp Neubeck | 7917360 | 2012-11-13 21:10:09 +0100 | [diff] [blame] | 205 | &Service::GetProxyConfig, |
| 206 | &Service::SetProxyConfig); |
Ben Chan | 923a502 | 2013-09-20 11:23:23 -0700 | [diff] [blame] | 207 | store_.RegisterBool(kSaveCredentialsProperty, &save_credentials_); |
Paul Stewart | fa11e28 | 2013-12-02 22:04:25 -0800 | [diff] [blame] | 208 | HelpRegisterConstDerivedString(kTetheringProperty, |
| 209 | &Service::GetTethering); |
Ben Chan | 923a502 | 2013-09-20 11:23:23 -0700 | [diff] [blame] | 210 | HelpRegisterDerivedString(kTypeProperty, |
Darin Petkov | 58f0b6d | 2012-06-12 12:52:30 +0200 | [diff] [blame] | 211 | &Service::CalculateTechnology, |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 212 | NULL); |
Ben Chan | 923a502 | 2013-09-20 11:23:23 -0700 | [diff] [blame] | 213 | // kSecurityProperty: Registered in WiFiService |
| 214 | HelpRegisterDerivedString(kStateProperty, |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 215 | &Service::CalculateState, |
| 216 | NULL); |
Ben Chan | 923a502 | 2013-09-20 11:23:23 -0700 | [diff] [blame] | 217 | store_.RegisterConstUint8(kSignalStrengthProperty, &strength_); |
| 218 | store_.RegisterString(kUIDataProperty, &ui_data_); |
Ben Chan | 39a7beb | 2013-09-21 11:28:00 -0700 | [diff] [blame] | 219 | HelpRegisterConstDerivedStrings(kDiagnosticsDisconnectsProperty, |
Darin Petkov | 0c65bdd | 2012-12-05 13:42:41 +0100 | [diff] [blame] | 220 | &Service::GetDisconnectsProperty); |
Ben Chan | 39a7beb | 2013-09-21 11:28:00 -0700 | [diff] [blame] | 221 | HelpRegisterConstDerivedStrings(kDiagnosticsMisconnectsProperty, |
Darin Petkov | 0c65bdd | 2012-12-05 13:42:41 +0100 | [diff] [blame] | 222 | &Service::GetMisconnectsProperty); |
Wade Guthrie | 7ac610b | 2013-10-01 17:48:14 -0700 | [diff] [blame] | 223 | metrics_->RegisterService(*this); |
Thieu Le | 48e6d6d | 2011-12-06 00:40:27 +0000 | [diff] [blame] | 224 | |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 225 | static_ip_parameters_.PlumbPropertyStore(&store_); |
| 226 | |
Ben Chan | 923a502 | 2013-09-20 11:23:23 -0700 | [diff] [blame] | 227 | IgnoreParameterForConfigure(kTypeProperty); |
| 228 | IgnoreParameterForConfigure(kProfileProperty); |
Paul Stewart | cb59fed | 2012-03-21 21:14:46 -0700 | [diff] [blame] | 229 | |
Darin Petkov | 9c36699 | 2013-01-10 12:32:21 +0100 | [diff] [blame] | 230 | LOG(INFO) << Technology::NameFromIdentifier(technology) << " service " |
| 231 | << unique_name_ << " constructed."; |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 232 | } |
| 233 | |
Thieu Le | 48e6d6d | 2011-12-06 00:40:27 +0000 | [diff] [blame] | 234 | Service::~Service() { |
Wade Guthrie | 7ac610b | 2013-10-01 17:48:14 -0700 | [diff] [blame] | 235 | metrics_->DeregisterService(*this); |
mukesh agrawal | 6cfe53f | 2013-08-13 13:39:01 -0700 | [diff] [blame] | 236 | LOG(INFO) << "Service " << unique_name_ << " destroyed."; |
Thieu Le | 48e6d6d | 2011-12-06 00:40:27 +0000 | [diff] [blame] | 237 | } |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 238 | |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 239 | void Service::AutoConnect() { |
Darin Petkov | 3abc3be | 2012-06-27 10:48:23 +0200 | [diff] [blame] | 240 | const char *reason = NULL; |
| 241 | if (IsAutoConnectable(&reason)) { |
mukesh agrawal | dc7b844 | 2012-09-27 13:48:14 -0700 | [diff] [blame] | 242 | Error error; |
Darin Petkov | 457728b | 2013-01-09 09:49:08 +0100 | [diff] [blame] | 243 | LOG(INFO) << "Auto-connecting to service " << unique_name_; |
Christopher Wiley | 0801d19 | 2012-09-24 11:57:15 -0700 | [diff] [blame] | 244 | ThrottleFutureAutoConnects(); |
mukesh agrawal | dc7b844 | 2012-09-27 13:48:14 -0700 | [diff] [blame] | 245 | Connect(&error, __func__); |
mukesh agrawal | 76d1388 | 2012-01-12 15:23:11 -0800 | [diff] [blame] | 246 | } else { |
mukesh agrawal | 03c15ce | 2012-11-29 17:36:21 -0800 | [diff] [blame] | 247 | if (reason == kAutoConnConnected || reason == kAutoConnBusy) { |
| 248 | SLOG(Service, 1) |
Darin Petkov | 457728b | 2013-01-09 09:49:08 +0100 | [diff] [blame] | 249 | << "Suppressed autoconnect to service " << unique_name_ << " " |
mukesh agrawal | 03c15ce | 2012-11-29 17:36:21 -0800 | [diff] [blame] | 250 | << "(" << reason << ")"; |
| 251 | } else { |
Darin Petkov | 457728b | 2013-01-09 09:49:08 +0100 | [diff] [blame] | 252 | LOG(INFO) << "Suppressed autoconnect to service " << unique_name_ << " " |
mukesh agrawal | 03c15ce | 2012-11-29 17:36:21 -0800 | [diff] [blame] | 253 | << "(" << reason << ")"; |
| 254 | } |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 255 | } |
| 256 | } |
| 257 | |
mukesh agrawal | dc7b844 | 2012-09-27 13:48:14 -0700 | [diff] [blame] | 258 | void Service::Connect(Error */*error*/, const char *reason) { |
| 259 | LOG(INFO) << "Connect to service " << unique_name() <<": " << reason; |
mukesh agrawal | adb6848 | 2012-01-17 16:31:51 -0800 | [diff] [blame] | 260 | explicitly_disconnected_ = false; |
Thieu Le | af47141 | 2013-06-27 14:12:37 -0700 | [diff] [blame] | 261 | // Clear any failure state from a previous connect attempt. |
| 262 | if (state() == kStateFailure) |
| 263 | SetState(kStateIdle); |
mukesh agrawal | adb6848 | 2012-01-17 16:31:51 -0800 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | void Service::Disconnect(Error */*error*/) { |
mukesh agrawal | dc7b844 | 2012-09-27 13:48:14 -0700 | [diff] [blame] | 267 | LOG(INFO) << "Disconnecting from service " << unique_name_; |
Christopher Wiley | 3e7635e | 2012-08-15 09:46:17 -0700 | [diff] [blame] | 268 | MemoryLog::GetInstance()->FlushToDisk(); |
mukesh agrawal | adb6848 | 2012-01-17 16:31:51 -0800 | [diff] [blame] | 269 | } |
| 270 | |
Christopher Wiley | abd3b50 | 2012-09-26 13:08:52 -0700 | [diff] [blame] | 271 | void Service::DisconnectWithFailure(ConnectFailure failure, Error *error) { |
| 272 | Disconnect(error); |
| 273 | SetFailure(failure); |
| 274 | } |
| 275 | |
| 276 | void Service::UserInitiatedDisconnect(Error *error) { |
| 277 | Disconnect(error); |
| 278 | explicitly_disconnected_ = true; |
| 279 | } |
| 280 | |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 281 | void Service::ActivateCellularModem(const string &/*carrier*/, |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 282 | Error *error, |
| 283 | const ResultCallback &/*callback*/) { |
| 284 | Error::PopulateAndLog(error, Error::kNotSupported, |
| 285 | "Service doesn't support cellular modem activation."); |
Darin Petkov | c408e69 | 2011-08-17 13:47:15 -0700 | [diff] [blame] | 286 | } |
| 287 | |
Ben Chan | 5d92454 | 2013-02-14 17:49:08 -0800 | [diff] [blame] | 288 | void Service::CompleteCellularActivation(Error *error) { |
| 289 | Error::PopulateAndLog( |
| 290 | error, Error::kNotSupported, |
| 291 | "Service doesn't support cellular activation completion."); |
| 292 | } |
| 293 | |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 294 | bool Service::IsActive(Error */*error*/) { |
Paul Stewart | bfb8255 | 2012-10-24 16:48:48 -0700 | [diff] [blame] | 295 | return state() != kStateUnknown && |
| 296 | state() != kStateIdle && |
| 297 | state() != kStateFailure; |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 298 | } |
| 299 | |
Darin Petkov | c8d91e5 | 2013-01-21 11:43:47 +0100 | [diff] [blame] | 300 | // static |
| 301 | bool Service::IsConnectedState(ConnectState state) { |
| 302 | return (state == kStateConnected || |
| 303 | state == kStatePortal || |
| 304 | state == kStateOnline); |
| 305 | } |
| 306 | |
| 307 | // static |
| 308 | bool Service::IsConnectingState(ConnectState state) { |
| 309 | return (state == kStateAssociating || |
| 310 | state == kStateConfiguring); |
| 311 | } |
| 312 | |
| 313 | bool Service::IsConnected() const { |
| 314 | return IsConnectedState(state()); |
| 315 | } |
| 316 | |
| 317 | bool Service::IsConnecting() const { |
| 318 | return IsConnectingState(state()); |
| 319 | } |
| 320 | |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 321 | void Service::SetState(ConnectState state) { |
| 322 | if (state == state_) { |
| 323 | return; |
| 324 | } |
| 325 | |
Darin Petkov | 457728b | 2013-01-09 09:49:08 +0100 | [diff] [blame] | 326 | LOG(INFO) << "Service " << unique_name_ << ": state " |
| 327 | << ConnectStateToString(state_) << " -> " |
| 328 | << ConnectStateToString(state); |
| 329 | |
Darin Petkov | 0857f8e | 2012-12-21 10:49:17 +0100 | [diff] [blame] | 330 | if (state == kStateFailure) { |
Darin Petkov | 385b9bc | 2012-12-03 15:25:05 +0100 | [diff] [blame] | 331 | NoteDisconnectEvent(); |
| 332 | } |
| 333 | |
Darin Petkov | c8d91e5 | 2013-01-21 11:43:47 +0100 | [diff] [blame] | 334 | previous_state_ = state_; |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 335 | state_ = state; |
| 336 | if (state != kStateFailure) { |
| 337 | failure_ = kFailureUnknown; |
Darin Petkov | aba8932 | 2013-03-11 14:48:22 +0100 | [diff] [blame] | 338 | SetErrorDetails(kErrorDetailsNone); |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 339 | } |
mukesh agrawal | cf24a24 | 2012-05-21 16:46:11 -0700 | [diff] [blame] | 340 | if (state == kStateConnected) { |
Christopher Wiley | 0801d19 | 2012-09-24 11:57:15 -0700 | [diff] [blame] | 341 | failed_time_ = 0; |
mukesh agrawal | cf24a24 | 2012-05-21 16:46:11 -0700 | [diff] [blame] | 342 | has_ever_connected_ = true; |
| 343 | SaveToProfile(); |
Christopher Wiley | 0801d19 | 2012-09-24 11:57:15 -0700 | [diff] [blame] | 344 | // When we succeed in connecting, forget that connects failed in the past. |
| 345 | // Give services one chance at a fast autoconnect retry by resetting the |
| 346 | // cooldown to 0 to indicate that the last connect was successful. |
| 347 | auto_connect_cooldown_milliseconds_ = 0; |
| 348 | reenable_auto_connect_task_.Cancel(); |
mukesh agrawal | cf24a24 | 2012-05-21 16:46:11 -0700 | [diff] [blame] | 349 | } |
Paul Stewart | f2d6091 | 2012-07-15 08:37:30 -0700 | [diff] [blame] | 350 | UpdateErrorProperty(); |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 351 | manager_->UpdateService(this); |
Wade Guthrie | 7ac610b | 2013-10-01 17:48:14 -0700 | [diff] [blame] | 352 | metrics_->NotifyServiceStateChanged(*this, state); |
Ben Chan | 923a502 | 2013-09-20 11:23:23 -0700 | [diff] [blame] | 353 | adaptor_->EmitStringChanged(kStateProperty, GetStateString()); |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 354 | } |
| 355 | |
Christopher Wiley | 0801d19 | 2012-09-24 11:57:15 -0700 | [diff] [blame] | 356 | void Service::ReEnableAutoConnectTask() { |
| 357 | // Kill the thing blocking AutoConnect(). |
| 358 | reenable_auto_connect_task_.Cancel(); |
| 359 | // Post to the manager, giving it an opportunity to AutoConnect again. |
| 360 | manager_->UpdateService(this); |
| 361 | } |
| 362 | |
| 363 | void Service::ThrottleFutureAutoConnects() { |
| 364 | if (auto_connect_cooldown_milliseconds_ > 0) { |
mukesh agrawal | dc7b844 | 2012-09-27 13:48:14 -0700 | [diff] [blame] | 365 | LOG(INFO) << "Throttling future autoconnects to service " << unique_name_ |
| 366 | << ". Next autoconnect in " |
Christopher Wiley | 0801d19 | 2012-09-24 11:57:15 -0700 | [diff] [blame] | 367 | << auto_connect_cooldown_milliseconds_ << " milliseconds."; |
| 368 | reenable_auto_connect_task_.Reset(Bind(&Service::ReEnableAutoConnectTask, |
| 369 | weak_ptr_factory_.GetWeakPtr())); |
| 370 | dispatcher_->PostDelayedTask(reenable_auto_connect_task_.callback(), |
| 371 | auto_connect_cooldown_milliseconds_); |
| 372 | } |
| 373 | auto_connect_cooldown_milliseconds_ = |
| 374 | std::min(kMaxAutoConnectCooldownTimeMilliseconds, |
| 375 | std::max(kMinAutoConnectCooldownTimeMilliseconds, |
| 376 | auto_connect_cooldown_milliseconds_ * |
| 377 | kAutoConnectCooldownBackoffFactor)); |
| 378 | } |
| 379 | |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 380 | void Service::SetFailure(ConnectFailure failure) { |
| 381 | failure_ = failure; |
mukesh agrawal | 568b5c6 | 2012-02-28 14:44:47 -0800 | [diff] [blame] | 382 | failed_time_ = time(NULL); |
Paul Stewart | f2d6091 | 2012-07-15 08:37:30 -0700 | [diff] [blame] | 383 | UpdateErrorProperty(); |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 384 | SetState(kStateFailure); |
| 385 | } |
| 386 | |
mukesh agrawal | 568b5c6 | 2012-02-28 14:44:47 -0800 | [diff] [blame] | 387 | void Service::SetFailureSilent(ConnectFailure failure) { |
Darin Petkov | 0857f8e | 2012-12-21 10:49:17 +0100 | [diff] [blame] | 388 | NoteDisconnectEvent(); |
mukesh agrawal | 568b5c6 | 2012-02-28 14:44:47 -0800 | [diff] [blame] | 389 | // Note that order matters here, since SetState modifies |failure_| and |
| 390 | // |failed_time_|. |
| 391 | SetState(kStateIdle); |
| 392 | failure_ = failure; |
Paul Stewart | f2d6091 | 2012-07-15 08:37:30 -0700 | [diff] [blame] | 393 | UpdateErrorProperty(); |
mukesh agrawal | 568b5c6 | 2012-02-28 14:44:47 -0800 | [diff] [blame] | 394 | failed_time_ = time(NULL); |
| 395 | } |
| 396 | |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 397 | string Service::GetRpcIdentifier() const { |
Chris Masone | 3c3f6a1 | 2011-07-01 10:01:41 -0700 | [diff] [blame] | 398 | return adaptor_->GetRpcIdentifier(); |
| 399 | } |
| 400 | |
Paul Stewart | e7de294 | 2013-04-25 17:07:31 -0700 | [diff] [blame] | 401 | string Service::GetLoadableStorageIdentifier( |
| 402 | const StoreInterface &storage) const { |
| 403 | return IsLoadableFrom(storage) ? GetStorageIdentifier() : ""; |
| 404 | } |
| 405 | |
| 406 | bool Service::IsLoadableFrom(const StoreInterface &storage) const { |
| 407 | return storage.ContainsGroup(GetStorageIdentifier()); |
Paul Stewart | bba6a5b | 2011-11-02 18:45:59 -0700 | [diff] [blame] | 408 | } |
| 409 | |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 410 | bool Service::Load(StoreInterface *storage) { |
| 411 | const string id = GetStorageIdentifier(); |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 412 | if (!storage->ContainsGroup(id)) { |
| 413 | LOG(WARNING) << "Service is not available in the persistent store: " << id; |
| 414 | return false; |
| 415 | } |
Paul Stewart | 2da34c0 | 2013-10-17 15:28:56 -0700 | [diff] [blame] | 416 | |
Paul Stewart | 000f033 | 2013-10-25 13:30:28 -0700 | [diff] [blame] | 417 | auto_connect_ = IsAutoConnectByDefault(); |
Paul Stewart | 2da34c0 | 2013-10-17 15:28:56 -0700 | [diff] [blame] | 418 | retain_auto_connect_ = |
| 419 | storage->GetBool(id, kStorageAutoConnect, &auto_connect_); |
| 420 | // The legacy "Favorite" flag will override retain_auto_connect_ if present. |
| 421 | storage->GetBool(id, kStorageFavorite, &retain_auto_connect_); |
| 422 | |
Paul Stewart | c3dbff1 | 2013-07-17 10:32:48 -0700 | [diff] [blame] | 423 | LoadString(storage, id, kStorageCheckPortal, kCheckPortalAuto, |
| 424 | &check_portal_); |
Paul Stewart | c3dbff1 | 2013-07-17 10:32:48 -0700 | [diff] [blame] | 425 | LoadString(storage, id, kStorageGUID, "", &guid_); |
mukesh agrawal | cf24a24 | 2012-05-21 16:46:11 -0700 | [diff] [blame] | 426 | storage->GetBool(id, kStorageHasEverConnected, &has_ever_connected_); |
Paul Stewart | c3dbff1 | 2013-07-17 10:32:48 -0700 | [diff] [blame] | 427 | if (!storage->GetInt(id, kStoragePriority, &priority_)) { |
| 428 | priority_ = kPriorityNone; |
| 429 | } |
| 430 | LoadString(storage, id, kStorageProxyConfig, "", &proxy_config_); |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 431 | storage->GetBool(id, kStorageSaveCredentials, &save_credentials_); |
Paul Stewart | c3dbff1 | 2013-07-17 10:32:48 -0700 | [diff] [blame] | 432 | LoadString(storage, id, kStorageUIData, "", &ui_data_); |
Paul Stewart | c43cbbe | 2013-04-11 06:29:30 -0700 | [diff] [blame] | 433 | |
Paul Stewart | dded007 | 2013-10-24 12:38:54 -0700 | [diff] [blame] | 434 | uint64 last_dhcp_option_failure; |
| 435 | if (storage->GetUint64(id, kStorageLastDHCPOptionFailure, |
| 436 | &last_dhcp_option_failure)) { |
| 437 | struct timeval tv = { 0, 0 }; |
| 438 | tv.tv_sec = last_dhcp_option_failure; |
| 439 | last_dhcp_option_failure_ = Timestamp(tv, ""); |
| 440 | dhcp_option_failure_state_ = kDHCPOptionFailureConfirmed; |
| 441 | } else { |
| 442 | last_dhcp_option_failure_ = Timestamp(); |
| 443 | dhcp_option_failure_state_ = kDHCPOptionFailureNotDetected; |
| 444 | } |
| 445 | |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 446 | static_ip_parameters_.Load(storage, id); |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 447 | |
Paul Stewart | c43cbbe | 2013-04-11 06:29:30 -0700 | [diff] [blame] | 448 | if (mutable_eap()) { |
| 449 | mutable_eap()->Load(storage, id); |
| 450 | OnEapCredentialsChanged(); |
| 451 | } |
| 452 | |
mukesh agrawal | adb6848 | 2012-01-17 16:31:51 -0800 | [diff] [blame] | 453 | explicitly_disconnected_ = false; |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 454 | |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 455 | return true; |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 456 | } |
| 457 | |
Paul Stewart | 65512e1 | 2012-03-26 18:01:08 -0700 | [diff] [blame] | 458 | bool Service::Unload() { |
Paul Stewart | 7fb0938 | 2012-07-18 17:32:27 -0700 | [diff] [blame] | 459 | auto_connect_ = IsAutoConnectByDefault(); |
Paul Stewart | 2da34c0 | 2013-10-17 15:28:56 -0700 | [diff] [blame] | 460 | retain_auto_connect_ = false; |
Paul Stewart | d8ad3c4 | 2012-01-09 12:39:38 -0800 | [diff] [blame] | 461 | check_portal_ = kCheckPortalAuto; |
Christopher Wiley | abd3b50 | 2012-09-26 13:08:52 -0700 | [diff] [blame] | 462 | explicitly_disconnected_ = false; |
Paul Stewart | e7cce8f | 2012-09-11 10:56:38 -0700 | [diff] [blame] | 463 | guid_ = ""; |
Paul Stewart | 88769de | 2012-09-21 13:14:36 -0700 | [diff] [blame] | 464 | has_ever_connected_ = false; |
Paul Stewart | d8ad3c4 | 2012-01-09 12:39:38 -0800 | [diff] [blame] | 465 | priority_ = kPriorityNone; |
| 466 | proxy_config_ = ""; |
| 467 | save_credentials_ = true; |
| 468 | ui_data_ = ""; |
Paul Stewart | dded007 | 2013-10-24 12:38:54 -0700 | [diff] [blame] | 469 | consecutive_dhcp_failures_ = 0; |
| 470 | last_dhcp_option_failure_ = Timestamp(); |
| 471 | dhcp_option_failure_state_ = kDHCPOptionFailureNotDetected; |
Paul Stewart | c43cbbe | 2013-04-11 06:29:30 -0700 | [diff] [blame] | 472 | if (mutable_eap()) { |
| 473 | mutable_eap()->Reset(); |
| 474 | } |
| 475 | ClearEAPCertification(); |
Paul Stewart | d8ad3c4 | 2012-01-09 12:39:38 -0800 | [diff] [blame] | 476 | |
Wade Guthrie | 9e0c250 | 2012-04-19 15:26:40 -0700 | [diff] [blame] | 477 | Error error; // Ignored. |
| 478 | Disconnect(&error); |
Paul Stewart | 65512e1 | 2012-03-26 18:01:08 -0700 | [diff] [blame] | 479 | return false; |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 480 | } |
| 481 | |
Albert Chaulk | 0e1cdea | 2013-02-27 15:32:55 -0800 | [diff] [blame] | 482 | void Service::Remove(Error */*error*/) { |
Paul Stewart | d3d0388 | 2013-08-29 15:43:42 -0700 | [diff] [blame] | 483 | manager()->RemoveService(this); |
| 484 | // |this| may no longer be valid now. |
Albert Chaulk | 0e1cdea | 2013-02-27 15:32:55 -0800 | [diff] [blame] | 485 | } |
| 486 | |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 487 | bool Service::Save(StoreInterface *storage) { |
| 488 | const string id = GetStorageIdentifier(); |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 489 | |
Darin Petkov | 58f0b6d | 2012-06-12 12:52:30 +0200 | [diff] [blame] | 490 | storage->SetString(id, kStorageType, GetTechnologyString()); |
Paul Stewart | 2706aaf | 2011-12-14 16:44:04 -0800 | [diff] [blame] | 491 | |
Paul Stewart | 2da34c0 | 2013-10-17 15:28:56 -0700 | [diff] [blame] | 492 | if (retain_auto_connect_) { |
| 493 | storage->SetBool(id, kStorageAutoConnect, auto_connect_); |
| 494 | } else { |
| 495 | storage->DeleteKey(id, kStorageAutoConnect); |
| 496 | } |
| 497 | |
| 498 | // Remove this legacy flag. |
| 499 | storage->DeleteKey(id, kStorageFavorite); |
| 500 | |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 501 | if (check_portal_ == kCheckPortalAuto) { |
| 502 | storage->DeleteKey(id, kStorageCheckPortal); |
| 503 | } else { |
| 504 | storage->SetString(id, kStorageCheckPortal, check_portal_); |
| 505 | } |
Paul Stewart | 2da34c0 | 2013-10-17 15:28:56 -0700 | [diff] [blame] | 506 | |
mukesh agrawal | cf24a24 | 2012-05-21 16:46:11 -0700 | [diff] [blame] | 507 | SaveString(storage, id, kStorageGUID, guid_, false, true); |
| 508 | storage->SetBool(id, kStorageHasEverConnected, has_ever_connected_); |
mukesh agrawal | d835b20 | 2011-10-07 15:26:47 -0700 | [diff] [blame] | 509 | storage->SetString(id, kStorageName, friendly_name_); |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 510 | if (priority_ != kPriorityNone) { |
| 511 | storage->SetInt(id, kStoragePriority, priority_); |
| 512 | } else { |
| 513 | storage->DeleteKey(id, kStoragePriority); |
| 514 | } |
Paul Stewart | 987e71e | 2011-12-05 09:45:06 -0800 | [diff] [blame] | 515 | SaveString(storage, id, kStorageProxyConfig, proxy_config_, false, true); |
Darin Petkov | 0debec0 | 2013-01-22 10:40:05 +0100 | [diff] [blame] | 516 | storage->SetBool(id, kStorageSaveCredentials, save_credentials_); |
Paul Stewart | 987e71e | 2011-12-05 09:45:06 -0800 | [diff] [blame] | 517 | SaveString(storage, id, kStorageUIData, ui_data_, false, true); |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 518 | |
Paul Stewart | dded007 | 2013-10-24 12:38:54 -0700 | [diff] [blame] | 519 | if (dhcp_option_failure_state_ == kDHCPOptionFailureConfirmed || |
| 520 | dhcp_option_failure_state_ == kDHCPOptionFailureRetestMinimalRequest || |
| 521 | dhcp_option_failure_state_ == kDHCPOptionFailureRetestFullRequest || |
| 522 | dhcp_option_failure_state_ == kDHCPOptionFailureRetestGotNoReply) { |
| 523 | // In any of the states where we maintain a history of DHCP option |
| 524 | // failures and we haven't confirmed that this is no longer an issue, |
| 525 | // we should save the last option failure time even if it is very long ago. |
| 526 | storage->SetUint64(id, kStorageLastDHCPOptionFailure, |
| 527 | last_dhcp_option_failure_.monotonic.tv_sec); |
| 528 | } else { |
| 529 | storage->DeleteKey(id, kStorageLastDHCPOptionFailure); |
| 530 | } |
| 531 | |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 532 | static_ip_parameters_.Save(storage, id); |
Paul Stewart | c43cbbe | 2013-04-11 06:29:30 -0700 | [diff] [blame] | 533 | if (eap()) { |
| 534 | eap()->Save(storage, id, save_credentials_); |
| 535 | } |
Paul Stewart | dded007 | 2013-10-24 12:38:54 -0700 | [diff] [blame] | 536 | |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 537 | return true; |
| 538 | } |
| 539 | |
Gary Morain | d93615e | 2012-04-27 11:50:03 -0700 | [diff] [blame] | 540 | void Service::SaveToCurrentProfile() { |
| 541 | // Some unittests do not specify a manager. |
| 542 | if (manager()) { |
| 543 | manager()->SaveServiceToProfile(this); |
| 544 | } |
Thieu Le | d4e9e55 | 2012-02-16 16:26:07 -0800 | [diff] [blame] | 545 | } |
| 546 | |
Paul Stewart | cb59fed | 2012-03-21 21:14:46 -0700 | [diff] [blame] | 547 | void Service::Configure(const KeyValueStore &args, Error *error) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 548 | SLOG(Service, 5) << "Configuring bool properties:"; |
Paul Stewart | 99dc9f3 | 2013-06-27 07:39:25 -0700 | [diff] [blame] | 549 | for (const auto &bool_it : args.bool_properties()) { |
| 550 | if (ContainsKey(parameters_ignored_for_configure_, bool_it.first)) { |
Paul Stewart | cb59fed | 2012-03-21 21:14:46 -0700 | [diff] [blame] | 551 | continue; |
| 552 | } |
Paul Stewart | 99dc9f3 | 2013-06-27 07:39:25 -0700 | [diff] [blame] | 553 | SLOG(Service, 5) << " " << bool_it.first; |
Paul Stewart | cb59fed | 2012-03-21 21:14:46 -0700 | [diff] [blame] | 554 | Error set_error; |
Paul Stewart | 99dc9f3 | 2013-06-27 07:39:25 -0700 | [diff] [blame] | 555 | store_.SetBoolProperty(bool_it.first, bool_it.second, &set_error); |
| 556 | OnPropertyChanged(bool_it.first); |
Paul Stewart | cb59fed | 2012-03-21 21:14:46 -0700 | [diff] [blame] | 557 | if (error->IsSuccess() && set_error.IsFailure()) { |
| 558 | error->CopyFrom(set_error); |
| 559 | } |
| 560 | } |
Paul Stewart | 7a20aa4 | 2013-01-17 12:21:41 -0800 | [diff] [blame] | 561 | SLOG(Service, 5) << "Configuring int32 properties:"; |
Paul Stewart | 99dc9f3 | 2013-06-27 07:39:25 -0700 | [diff] [blame] | 562 | for (const auto &int_it : args.int_properties()) { |
| 563 | if (ContainsKey(parameters_ignored_for_configure_, int_it.first)) { |
Paul Stewart | cb59fed | 2012-03-21 21:14:46 -0700 | [diff] [blame] | 564 | continue; |
| 565 | } |
Paul Stewart | 99dc9f3 | 2013-06-27 07:39:25 -0700 | [diff] [blame] | 566 | SLOG(Service, 5) << " " << int_it.first; |
Paul Stewart | cb59fed | 2012-03-21 21:14:46 -0700 | [diff] [blame] | 567 | Error set_error; |
Paul Stewart | 99dc9f3 | 2013-06-27 07:39:25 -0700 | [diff] [blame] | 568 | store_.SetInt32Property(int_it.first, int_it.second, &set_error); |
| 569 | OnPropertyChanged(int_it.first); |
| 570 | if (error->IsSuccess() && set_error.IsFailure()) { |
| 571 | error->CopyFrom(set_error); |
| 572 | } |
| 573 | } |
| 574 | SLOG(Service, 5) << "Configuring string properties:"; |
| 575 | for (const auto &string_it : args.string_properties()) { |
| 576 | if (ContainsKey(parameters_ignored_for_configure_, string_it.first)) { |
| 577 | continue; |
| 578 | } |
| 579 | SLOG(Service, 5) << " " << string_it.first; |
| 580 | Error set_error; |
| 581 | store_.SetStringProperty(string_it.first, string_it.second, &set_error); |
| 582 | OnPropertyChanged(string_it.first); |
| 583 | if (error->IsSuccess() && set_error.IsFailure()) { |
| 584 | error->CopyFrom(set_error); |
| 585 | } |
| 586 | } |
| 587 | SLOG(Service, 5) << "Configuring string array properties:"; |
| 588 | for (const auto &strings_it : args.strings_properties()) { |
| 589 | if (ContainsKey(parameters_ignored_for_configure_, strings_it.first)) { |
| 590 | continue; |
| 591 | } |
| 592 | SLOG(Service, 5) << " " << strings_it.first; |
| 593 | Error set_error; |
| 594 | store_.SetStringsProperty(strings_it.first, strings_it.second, &set_error); |
| 595 | OnPropertyChanged(strings_it.first); |
Paul Stewart | cb59fed | 2012-03-21 21:14:46 -0700 | [diff] [blame] | 596 | if (error->IsSuccess() && set_error.IsFailure()) { |
| 597 | error->CopyFrom(set_error); |
| 598 | } |
| 599 | } |
Arman Uguray | 631c7e4 | 2013-07-30 16:41:12 -0700 | [diff] [blame] | 600 | SLOG(Service, 5) << "Configuring string map properties:"; |
| 601 | for (const auto &stringmap_it : args.stringmap_properties()) { |
| 602 | if (ContainsKey(parameters_ignored_for_configure_, stringmap_it.first)) { |
| 603 | continue; |
| 604 | } |
| 605 | SLOG(Service, 5) << " " << stringmap_it.first; |
| 606 | Error set_error; |
| 607 | store_.SetStringmapProperty( |
| 608 | stringmap_it.first, stringmap_it.second, &set_error); |
| 609 | OnPropertyChanged(stringmap_it.first); |
| 610 | if (error->IsSuccess() && set_error.IsFailure()) { |
| 611 | error->CopyFrom(set_error); |
| 612 | } |
| 613 | } |
Paul Stewart | cb59fed | 2012-03-21 21:14:46 -0700 | [diff] [blame] | 614 | } |
| 615 | |
Paul Stewart | 7a20aa4 | 2013-01-17 12:21:41 -0800 | [diff] [blame] | 616 | bool Service::DoPropertiesMatch(const KeyValueStore &args) const { |
Paul Stewart | 7a20aa4 | 2013-01-17 12:21:41 -0800 | [diff] [blame] | 617 | SLOG(Service, 5) << "Checking bool properties:"; |
Paul Stewart | 99dc9f3 | 2013-06-27 07:39:25 -0700 | [diff] [blame] | 618 | for (const auto &bool_it: args.bool_properties()) { |
| 619 | SLOG(Service, 5) << " " << bool_it.first; |
Paul Stewart | 7a20aa4 | 2013-01-17 12:21:41 -0800 | [diff] [blame] | 620 | Error get_error; |
| 621 | bool value; |
Paul Stewart | 99dc9f3 | 2013-06-27 07:39:25 -0700 | [diff] [blame] | 622 | if (!store_.GetBoolProperty(bool_it.first, &value, &get_error) || |
| 623 | value != bool_it.second) { |
Paul Stewart | 7a20aa4 | 2013-01-17 12:21:41 -0800 | [diff] [blame] | 624 | return false; |
| 625 | } |
| 626 | } |
| 627 | SLOG(Service, 5) << "Checking int32 properties:"; |
Paul Stewart | 99dc9f3 | 2013-06-27 07:39:25 -0700 | [diff] [blame] | 628 | for (const auto &int_it : args.int_properties()) { |
| 629 | SLOG(Service, 5) << " " << int_it.first; |
Paul Stewart | 7a20aa4 | 2013-01-17 12:21:41 -0800 | [diff] [blame] | 630 | Error get_error; |
| 631 | int32 value; |
Paul Stewart | 99dc9f3 | 2013-06-27 07:39:25 -0700 | [diff] [blame] | 632 | if (!store_.GetInt32Property(int_it.first, &value, &get_error) || |
| 633 | value != int_it.second) { |
| 634 | return false; |
| 635 | } |
| 636 | } |
| 637 | SLOG(Service, 5) << "Checking string properties:"; |
| 638 | for (const auto &string_it : args.string_properties()) { |
| 639 | SLOG(Service, 5) << " " << string_it.first; |
| 640 | Error get_error; |
| 641 | string value; |
| 642 | if (!store_.GetStringProperty(string_it.first, &value, &get_error) || |
| 643 | value != string_it.second) { |
| 644 | return false; |
| 645 | } |
| 646 | } |
| 647 | SLOG(Service, 5) << "Checking string array properties:"; |
| 648 | for (const auto &strings_it : args.strings_properties()) { |
| 649 | SLOG(Service, 5) << " " << strings_it.first; |
| 650 | Error get_error; |
| 651 | vector<string> value; |
| 652 | if (!store_.GetStringsProperty(strings_it.first, &value, &get_error) || |
| 653 | value != strings_it.second) { |
Paul Stewart | 7a20aa4 | 2013-01-17 12:21:41 -0800 | [diff] [blame] | 654 | return false; |
| 655 | } |
| 656 | } |
| 657 | return true; |
| 658 | } |
| 659 | |
Paul Stewart | 10ccbb3 | 2012-04-26 15:59:30 -0700 | [diff] [blame] | 660 | bool Service::IsRemembered() const { |
| 661 | return profile_ && !manager_->IsServiceEphemeral(this); |
| 662 | } |
| 663 | |
Paul Stewart | df3c0a8 | 2012-11-09 15:54:33 -0800 | [diff] [blame] | 664 | bool Service::IsDependentOn(const ServiceRefPtr &b) const { |
Paul Stewart | cd7f585 | 2013-03-27 13:54:23 -0700 | [diff] [blame] | 665 | if (!connection_ || !b || !b->connection()) { |
Paul Stewart | df3c0a8 | 2012-11-09 15:54:33 -0800 | [diff] [blame] | 666 | return false; |
| 667 | } |
| 668 | return connection_->GetLowerConnection() == b->connection(); |
| 669 | } |
| 670 | |
Paul Stewart | 2da34c0 | 2013-10-17 15:28:56 -0700 | [diff] [blame] | 671 | void Service::EnableAndRetainAutoConnect() { |
| 672 | if (retain_auto_connect_) { |
mukesh agrawal | 00917ce | 2011-11-22 23:56:55 +0000 | [diff] [blame] | 673 | // We do not want to clobber the value of auto_connect_ (it may |
| 674 | // be user-set). So return early. |
| 675 | return; |
| 676 | } |
| 677 | |
mukesh agrawal | cbfb34e | 2013-04-17 19:33:25 -0700 | [diff] [blame] | 678 | SetAutoConnect(true); |
Paul Stewart | 2da34c0 | 2013-10-17 15:28:56 -0700 | [diff] [blame] | 679 | RetainAutoConnect(); |
mukesh agrawal | 00917ce | 2011-11-22 23:56:55 +0000 | [diff] [blame] | 680 | } |
| 681 | |
Darin Petkov | 5eb0542 | 2012-05-11 15:45:25 +0200 | [diff] [blame] | 682 | void Service::SetConnection(const ConnectionRefPtr &connection) { |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 683 | if (connection.get()) { |
Paul Stewart | 1e3bc496 | 2012-09-14 12:20:22 -0700 | [diff] [blame] | 684 | // TODO(pstew): Make this function testable by using a factory here. |
Paul Stewart | ee6b3d7 | 2013-07-12 16:07:51 -0700 | [diff] [blame] | 685 | // http://crbug.com/216664 |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 686 | http_proxy_.reset(new HTTPProxy(connection)); |
Darin Petkov | 5eb0542 | 2012-05-11 15:45:25 +0200 | [diff] [blame] | 687 | http_proxy_->Start(dispatcher_, sockets_.get()); |
Paul Stewart | 7f5d9c0 | 2013-12-03 18:26:00 -0800 | [diff] [blame] | 688 | Error unused_error; |
| 689 | connection->set_tethering(GetTethering(&unused_error)); |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 690 | } else { |
| 691 | http_proxy_.reset(); |
Paul Stewart | def189e | 2012-08-02 20:12:09 -0700 | [diff] [blame] | 692 | static_ip_parameters_.ClearSavedParameters(); |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 693 | } |
| 694 | connection_ = connection; |
Paul Stewart | 1e3bc496 | 2012-09-14 12:20:22 -0700 | [diff] [blame] | 695 | Error error; |
| 696 | string ipconfig = GetIPConfigRpcIdentifier(&error); |
| 697 | if (error.IsSuccess()) { |
Ben Chan | 39a7beb | 2013-09-21 11:28:00 -0700 | [diff] [blame] | 698 | adaptor_->EmitRpcIdentifierChanged(kIPConfigProperty, ipconfig); |
Paul Stewart | 1e3bc496 | 2012-09-14 12:20:22 -0700 | [diff] [blame] | 699 | } |
Paul Stewart | be5f5b3 | 2011-12-07 17:11:11 -0800 | [diff] [blame] | 700 | } |
| 701 | |
Gaurav Shah | 10109f2 | 2011-11-11 20:16:22 -0800 | [diff] [blame] | 702 | bool Service::Is8021xConnectable() const { |
Paul Stewart | c43cbbe | 2013-04-11 06:29:30 -0700 | [diff] [blame] | 703 | return eap() && eap()->IsConnectable(); |
Gaurav Shah | 10109f2 | 2011-11-11 20:16:22 -0800 | [diff] [blame] | 704 | } |
| 705 | |
Paul Stewart | bc6e739 | 2012-05-24 07:07:48 -0700 | [diff] [blame] | 706 | bool Service::AddEAPCertification(const string &name, size_t depth) { |
| 707 | if (depth >= kEAPMaxCertificationElements) { |
| 708 | LOG(WARNING) << "Ignoring certification " << name |
| 709 | << " because depth " << depth |
| 710 | << " exceeds our maximum of " |
| 711 | << kEAPMaxCertificationElements; |
| 712 | return false; |
| 713 | } |
| 714 | |
Paul Stewart | c43cbbe | 2013-04-11 06:29:30 -0700 | [diff] [blame] | 715 | if (depth >= remote_certification_.size()) { |
| 716 | remote_certification_.resize(depth + 1); |
| 717 | } else if (name == remote_certification_[depth]) { |
Paul Stewart | bc6e739 | 2012-05-24 07:07:48 -0700 | [diff] [blame] | 718 | return true; |
| 719 | } |
| 720 | |
Paul Stewart | c43cbbe | 2013-04-11 06:29:30 -0700 | [diff] [blame] | 721 | remote_certification_[depth] = name; |
Paul Stewart | bc6e739 | 2012-05-24 07:07:48 -0700 | [diff] [blame] | 722 | LOG(INFO) << "Received certification for " |
| 723 | << name |
| 724 | << " at depth " |
| 725 | << depth; |
| 726 | return true; |
| 727 | } |
| 728 | |
| 729 | void Service::ClearEAPCertification() { |
Paul Stewart | c43cbbe | 2013-04-11 06:29:30 -0700 | [diff] [blame] | 730 | remote_certification_.clear(); |
Paul Stewart | bc6e739 | 2012-05-24 07:07:48 -0700 | [diff] [blame] | 731 | } |
| 732 | |
mukesh agrawal | cbfb34e | 2013-04-17 19:33:25 -0700 | [diff] [blame] | 733 | void Service::SetAutoConnect(bool connect) { |
| 734 | if (auto_connect() == connect) { |
| 735 | return; |
| 736 | } |
| 737 | auto_connect_ = connect; |
Ben Chan | 923a502 | 2013-09-20 11:23:23 -0700 | [diff] [blame] | 738 | adaptor_->EmitBoolChanged(kAutoConnectProperty, auto_connect()); |
mukesh agrawal | cbfb34e | 2013-04-17 19:33:25 -0700 | [diff] [blame] | 739 | } |
| 740 | |
Paul Stewart | c43cbbe | 2013-04-11 06:29:30 -0700 | [diff] [blame] | 741 | void Service::SetEapCredentials(EapCredentials *eap) { |
| 742 | // This operation must be done at most once for the lifetime of the service. |
| 743 | CHECK(eap && !eap_); |
| 744 | |
| 745 | eap_.reset(eap); |
| 746 | eap_->InitPropertyStore(mutable_store()); |
Gaurav Shah | 10109f2 | 2011-11-11 20:16:22 -0800 | [diff] [blame] | 747 | } |
| 748 | |
mukesh agrawal | 00917ce | 2011-11-22 23:56:55 +0000 | [diff] [blame] | 749 | // static |
Gaurav Shah | c6d6c72 | 2011-11-17 18:59:39 -0800 | [diff] [blame] | 750 | const char *Service::ConnectFailureToString(const ConnectFailure &state) { |
| 751 | switch (state) { |
| 752 | case kFailureUnknown: |
| 753 | return "Unknown"; |
Paul Stewart | f2d6091 | 2012-07-15 08:37:30 -0700 | [diff] [blame] | 754 | case kFailureAAA: |
Ben Chan | 923a502 | 2013-09-20 11:23:23 -0700 | [diff] [blame] | 755 | return kErrorAaaFailed; |
Paul Stewart | f2d6091 | 2012-07-15 08:37:30 -0700 | [diff] [blame] | 756 | case kFailureActivation: |
Ben Chan | 923a502 | 2013-09-20 11:23:23 -0700 | [diff] [blame] | 757 | return kErrorActivationFailed; |
Paul Stewart | f2d6091 | 2012-07-15 08:37:30 -0700 | [diff] [blame] | 758 | case kFailureBadPassphrase: |
Ben Chan | 923a502 | 2013-09-20 11:23:23 -0700 | [diff] [blame] | 759 | return kErrorBadPassphrase; |
Paul Stewart | f2d6091 | 2012-07-15 08:37:30 -0700 | [diff] [blame] | 760 | case kFailureBadWEPKey: |
Ben Chan | 923a502 | 2013-09-20 11:23:23 -0700 | [diff] [blame] | 761 | return kErrorBadWEPKey; |
Paul Stewart | f2d6091 | 2012-07-15 08:37:30 -0700 | [diff] [blame] | 762 | case kFailureConnect: |
Ben Chan | 923a502 | 2013-09-20 11:23:23 -0700 | [diff] [blame] | 763 | return kErrorConnectFailed; |
Paul Stewart | f2d6091 | 2012-07-15 08:37:30 -0700 | [diff] [blame] | 764 | case kFailureDNSLookup: |
Ben Chan | 923a502 | 2013-09-20 11:23:23 -0700 | [diff] [blame] | 765 | return kErrorDNSLookupFailed; |
Paul Stewart | f2d6091 | 2012-07-15 08:37:30 -0700 | [diff] [blame] | 766 | case kFailureDHCP: |
Ben Chan | 923a502 | 2013-09-20 11:23:23 -0700 | [diff] [blame] | 767 | return kErrorDhcpFailed; |
Darin Petkov | 1c049c7 | 2013-03-21 13:15:45 +0100 | [diff] [blame] | 768 | case kFailureEAPAuthentication: |
Ben Chan | 39a7beb | 2013-09-21 11:28:00 -0700 | [diff] [blame] | 769 | return kErrorEapAuthenticationFailed; |
Darin Petkov | 1c049c7 | 2013-03-21 13:15:45 +0100 | [diff] [blame] | 770 | case kFailureEAPLocalTLS: |
Ben Chan | 39a7beb | 2013-09-21 11:28:00 -0700 | [diff] [blame] | 771 | return kErrorEapLocalTlsFailed; |
Darin Petkov | 1c049c7 | 2013-03-21 13:15:45 +0100 | [diff] [blame] | 772 | case kFailureEAPRemoteTLS: |
Ben Chan | 39a7beb | 2013-09-21 11:28:00 -0700 | [diff] [blame] | 773 | return kErrorEapRemoteTlsFailed; |
Paul Stewart | f2d6091 | 2012-07-15 08:37:30 -0700 | [diff] [blame] | 774 | case kFailureHTTPGet: |
Ben Chan | 923a502 | 2013-09-20 11:23:23 -0700 | [diff] [blame] | 775 | return kErrorHTTPGetFailed; |
Darin Petkov | 1c049c7 | 2013-03-21 13:15:45 +0100 | [diff] [blame] | 776 | case kFailureInternal: |
Ben Chan | 923a502 | 2013-09-20 11:23:23 -0700 | [diff] [blame] | 777 | return kErrorInternal; |
Darin Petkov | 1c049c7 | 2013-03-21 13:15:45 +0100 | [diff] [blame] | 778 | case kFailureIPSecCertAuth: |
Ben Chan | 923a502 | 2013-09-20 11:23:23 -0700 | [diff] [blame] | 779 | return kErrorIpsecCertAuthFailed; |
Darin Petkov | 1c049c7 | 2013-03-21 13:15:45 +0100 | [diff] [blame] | 780 | case kFailureIPSecPSKAuth: |
Ben Chan | 923a502 | 2013-09-20 11:23:23 -0700 | [diff] [blame] | 781 | return kErrorIpsecPskAuthFailed; |
Gaurav Shah | c6d6c72 | 2011-11-17 18:59:39 -0800 | [diff] [blame] | 782 | case kFailureNeedEVDO: |
Ben Chan | 923a502 | 2013-09-20 11:23:23 -0700 | [diff] [blame] | 783 | return kErrorNeedEvdo; |
Gaurav Shah | c6d6c72 | 2011-11-17 18:59:39 -0800 | [diff] [blame] | 784 | case kFailureNeedHomeNetwork: |
Ben Chan | 923a502 | 2013-09-20 11:23:23 -0700 | [diff] [blame] | 785 | return kErrorNeedHomeNetwork; |
Paul Stewart | f2d6091 | 2012-07-15 08:37:30 -0700 | [diff] [blame] | 786 | case kFailureOTASP: |
Ben Chan | 923a502 | 2013-09-20 11:23:23 -0700 | [diff] [blame] | 787 | return kErrorOtaspFailed; |
Paul Stewart | f2d6091 | 2012-07-15 08:37:30 -0700 | [diff] [blame] | 788 | case kFailureOutOfRange: |
Ben Chan | 923a502 | 2013-09-20 11:23:23 -0700 | [diff] [blame] | 789 | return kErrorOutOfRange; |
Paul Stewart | f2d6091 | 2012-07-15 08:37:30 -0700 | [diff] [blame] | 790 | case kFailurePinMissing: |
Ben Chan | 923a502 | 2013-09-20 11:23:23 -0700 | [diff] [blame] | 791 | return kErrorPinMissing; |
Paul Stewart | f2d6091 | 2012-07-15 08:37:30 -0700 | [diff] [blame] | 792 | case kFailurePPPAuth: |
Ben Chan | 923a502 | 2013-09-20 11:23:23 -0700 | [diff] [blame] | 793 | return kErrorPppAuthFailed; |
Thieu Le | 48e6d6d | 2011-12-06 00:40:27 +0000 | [diff] [blame] | 794 | case kFailureMax: |
Darin Petkov | 1c049c7 | 2013-03-21 13:15:45 +0100 | [diff] [blame] | 795 | NOTREACHED(); |
Gaurav Shah | c6d6c72 | 2011-11-17 18:59:39 -0800 | [diff] [blame] | 796 | } |
| 797 | return "Invalid"; |
| 798 | } |
| 799 | |
| 800 | // static |
| 801 | const char *Service::ConnectStateToString(const ConnectState &state) { |
| 802 | switch (state) { |
| 803 | case kStateUnknown: |
| 804 | return "Unknown"; |
| 805 | case kStateIdle: |
| 806 | return "Idle"; |
| 807 | case kStateAssociating: |
| 808 | return "Associating"; |
| 809 | case kStateConfiguring: |
| 810 | return "Configuring"; |
| 811 | case kStateConnected: |
| 812 | return "Connected"; |
Thieu Le | 48e6d6d | 2011-12-06 00:40:27 +0000 | [diff] [blame] | 813 | case kStatePortal: |
| 814 | return "Portal"; |
Gaurav Shah | c6d6c72 | 2011-11-17 18:59:39 -0800 | [diff] [blame] | 815 | case kStateFailure: |
| 816 | return "Failure"; |
| 817 | case kStateOnline: |
| 818 | return "Online"; |
| 819 | } |
| 820 | return "Invalid"; |
| 821 | } |
| 822 | |
Darin Petkov | 58f0b6d | 2012-06-12 12:52:30 +0200 | [diff] [blame] | 823 | string Service::GetTechnologyString() const { |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 824 | return Technology::NameFromIdentifier(technology()); |
| 825 | } |
| 826 | |
Darin Petkov | 58f0b6d | 2012-06-12 12:52:30 +0200 | [diff] [blame] | 827 | string Service::CalculateTechnology(Error */*error*/) { |
| 828 | return GetTechnologyString(); |
| 829 | } |
| 830 | |
Paul Stewart | 483e472 | 2012-12-18 10:23:17 -0800 | [diff] [blame] | 831 | // static |
| 832 | void Service::ExpireEventsBefore( |
| 833 | int seconds_ago, const Timestamp &now, std::deque<Timestamp> *events) { |
| 834 | struct timeval period = (const struct timeval){ seconds_ago }; |
| 835 | while (!events->empty()) { |
| 836 | if (events->size() < static_cast<size_t>(kMaxDisconnectEventHistory)) { |
Paul Stewart | dded007 | 2013-10-24 12:38:54 -0700 | [diff] [blame] | 837 | struct timeval elapsed { 0 }; |
Paul Stewart | 483e472 | 2012-12-18 10:23:17 -0800 | [diff] [blame] | 838 | timersub(&now.monotonic, &events->front().monotonic, &elapsed); |
| 839 | if (timercmp(&elapsed, &period, <)) { |
| 840 | break; |
| 841 | } |
| 842 | } |
| 843 | events->pop_front(); |
| 844 | } |
| 845 | } |
| 846 | |
Darin Petkov | 385b9bc | 2012-12-03 15:25:05 +0100 | [diff] [blame] | 847 | void Service::NoteDisconnectEvent() { |
| 848 | SLOG(Service, 2) << __func__; |
Darin Petkov | cb0b566 | 2012-12-13 09:59:44 +0100 | [diff] [blame] | 849 | // Ignore the event if it's user-initiated explicit disconnect. |
Darin Petkov | 385b9bc | 2012-12-03 15:25:05 +0100 | [diff] [blame] | 850 | if (explicitly_disconnected_) { |
Darin Petkov | cb0b566 | 2012-12-13 09:59:44 +0100 | [diff] [blame] | 851 | SLOG(Service, 2) << "Explicit disconnect ignored."; |
| 852 | return; |
| 853 | } |
| 854 | // Ignore the event if manager is not running (e.g., service disconnects on |
| 855 | // shutdown). |
| 856 | if (!manager_->running()) { |
| 857 | SLOG(Service, 2) << "Disconnect while manager stopped ignored."; |
| 858 | return; |
| 859 | } |
| 860 | // Ignore the event if the power state is not on (e.g., when suspending). |
| 861 | PowerManager *power_manager = manager_->power_manager(); |
| 862 | if (!power_manager || |
| 863 | (power_manager->power_state() != PowerManager::kOn && |
| 864 | power_manager->power_state() != PowerManager::kUnknown)) { |
| 865 | SLOG(Service, 2) << "Disconnect in transitional power state ignored."; |
Darin Petkov | 385b9bc | 2012-12-03 15:25:05 +0100 | [diff] [blame] | 866 | return; |
| 867 | } |
Paul Stewart | 483e472 | 2012-12-18 10:23:17 -0800 | [diff] [blame] | 868 | int period = 0; |
Darin Petkov | 385b9bc | 2012-12-03 15:25:05 +0100 | [diff] [blame] | 869 | size_t threshold = 0; |
Darin Petkov | 0c65bdd | 2012-12-05 13:42:41 +0100 | [diff] [blame] | 870 | deque<Timestamp> *events = NULL; |
Darin Petkov | c8d91e5 | 2013-01-21 11:43:47 +0100 | [diff] [blame] | 871 | // Sometimes services transition to Idle before going into a failed state so |
| 872 | // take into account the last non-idle state. |
| 873 | ConnectState state = state_ == kStateIdle ? previous_state_ : state_; |
| 874 | if (IsConnectedState(state)) { |
Darin Petkov | cb0b566 | 2012-12-13 09:59:44 +0100 | [diff] [blame] | 875 | LOG(INFO) << "Noting an unexpected connection drop."; |
Paul Stewart | 483e472 | 2012-12-18 10:23:17 -0800 | [diff] [blame] | 876 | period = kDisconnectsMonitorSeconds; |
Darin Petkov | 385b9bc | 2012-12-03 15:25:05 +0100 | [diff] [blame] | 877 | threshold = kReportDisconnectsThreshold; |
| 878 | events = &disconnects_; |
Darin Petkov | c8d91e5 | 2013-01-21 11:43:47 +0100 | [diff] [blame] | 879 | } else if (IsConnectingState(state)) { |
Darin Petkov | cb0b566 | 2012-12-13 09:59:44 +0100 | [diff] [blame] | 880 | LOG(INFO) << "Noting an unexpected failure to connect."; |
Paul Stewart | 483e472 | 2012-12-18 10:23:17 -0800 | [diff] [blame] | 881 | period = kMisconnectsMonitorSeconds; |
Darin Petkov | 385b9bc | 2012-12-03 15:25:05 +0100 | [diff] [blame] | 882 | threshold = kReportMisconnectsThreshold; |
| 883 | events = &misconnects_; |
| 884 | } else { |
Darin Petkov | cb0b566 | 2012-12-13 09:59:44 +0100 | [diff] [blame] | 885 | SLOG(Service, 2) |
| 886 | << "Not connected or connecting, state transition ignored."; |
Darin Petkov | 385b9bc | 2012-12-03 15:25:05 +0100 | [diff] [blame] | 887 | return; |
| 888 | } |
Darin Petkov | 0c65bdd | 2012-12-05 13:42:41 +0100 | [diff] [blame] | 889 | Timestamp now = time_->GetNow(); |
Paul Stewart | 483e472 | 2012-12-18 10:23:17 -0800 | [diff] [blame] | 890 | // Discard old events first. |
| 891 | ExpireEventsBefore(period, now, events); |
Darin Petkov | 385b9bc | 2012-12-03 15:25:05 +0100 | [diff] [blame] | 892 | events->push_back(now); |
| 893 | if (events->size() >= threshold) { |
| 894 | diagnostics_reporter_->OnConnectivityEvent(); |
| 895 | } |
| 896 | } |
| 897 | |
Paul Stewart | 483e472 | 2012-12-18 10:23:17 -0800 | [diff] [blame] | 898 | bool Service::HasRecentConnectionIssues() { |
| 899 | Timestamp now = time_->GetNow(); |
| 900 | ExpireEventsBefore(kDisconnectsMonitorSeconds, now, &disconnects_); |
| 901 | ExpireEventsBefore(kMisconnectsMonitorSeconds, now, &misconnects_); |
| 902 | return !disconnects_.empty() || !misconnects_.empty(); |
| 903 | } |
| 904 | |
Jason Glasgow | b579005 | 2012-01-27 01:03:52 -0500 | [diff] [blame] | 905 | // static |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 906 | bool Service::DecideBetween(int a, int b, bool *decision) { |
| 907 | if (a == b) |
| 908 | return false; |
| 909 | *decision = (a > b); |
| 910 | return true; |
| 911 | } |
| 912 | |
mukesh agrawal | 43970a2 | 2013-02-15 16:00:07 -0800 | [diff] [blame] | 913 | uint16 Service::SecurityLevel() { |
| 914 | return (crypto_algorithm_ << 2) | (key_rotation_ << 1) | endpoint_auth_; |
| 915 | } |
| 916 | |
mukesh agrawal | 00917ce | 2011-11-22 23:56:55 +0000 | [diff] [blame] | 917 | // static |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 918 | bool Service::Compare(ServiceRefPtr a, |
| 919 | ServiceRefPtr b, |
Paul Stewart | 39db5ca | 2013-03-18 14:15:17 -0700 | [diff] [blame] | 920 | bool compare_connectivity_state, |
mukesh agrawal | ddc378f | 2012-02-17 18:26:20 -0800 | [diff] [blame] | 921 | const vector<Technology::Identifier> &tech_order, |
| 922 | const char **reason) { |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 923 | bool ret; |
| 924 | |
Paul Stewart | 39db5ca | 2013-03-18 14:15:17 -0700 | [diff] [blame] | 925 | if (compare_connectivity_state && a->state() != b->state()) { |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 926 | if (DecideBetween(a->IsConnected(), b->IsConnected(), &ret)) { |
mukesh agrawal | ddc378f | 2012-02-17 18:26:20 -0800 | [diff] [blame] | 927 | *reason = kServiceSortIsConnected; |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 928 | return ret; |
| 929 | } |
| 930 | |
Paul Stewart | a121c44 | 2012-06-09 14:12:58 -0700 | [diff] [blame] | 931 | if (DecideBetween(!a->IsPortalled(), !b->IsPortalled(), &ret)) { |
| 932 | *reason = kServiceSortIsPortalled; |
| 933 | return ret; |
| 934 | } |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 935 | |
| 936 | if (DecideBetween(a->IsConnecting(), b->IsConnecting(), &ret)) { |
mukesh agrawal | ddc378f | 2012-02-17 18:26:20 -0800 | [diff] [blame] | 937 | *reason = kServiceSortIsConnecting; |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 938 | return ret; |
| 939 | } |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 940 | |
| 941 | if (DecideBetween(!a->IsFailed(), !b->IsFailed(), &ret)) { |
mukesh agrawal | ddc378f | 2012-02-17 18:26:20 -0800 | [diff] [blame] | 942 | *reason = kServiceSortIsFailed; |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 943 | return ret; |
| 944 | } |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 945 | } |
| 946 | |
Darin Petkov | 2f903b3 | 2012-04-18 12:56:43 +0200 | [diff] [blame] | 947 | if (DecideBetween(a->connectable(), b->connectable(), &ret)) { |
| 948 | *reason = kServiceSortConnectable; |
| 949 | return ret; |
| 950 | } |
| 951 | |
Paul Stewart | df3c0a8 | 2012-11-09 15:54:33 -0800 | [diff] [blame] | 952 | if (DecideBetween(a->IsDependentOn(b), b->IsDependentOn(a), &ret)) { |
| 953 | *reason = kServiceSortDependency; |
| 954 | return ret; |
| 955 | } |
| 956 | |
Darin Petkov | 2f903b3 | 2012-04-18 12:56:43 +0200 | [diff] [blame] | 957 | // Ignore the auto-connect property if both services are connected |
| 958 | // already. This allows connected non-autoconnectable VPN services to be |
| 959 | // sorted higher than other connected services based on technology order. |
| 960 | if (!a->IsConnected() && |
| 961 | DecideBetween(a->auto_connect(), b->auto_connect(), &ret)) { |
| 962 | *reason = kServiceSortAutoConnect; |
| 963 | return ret; |
| 964 | } |
| 965 | |
Paul Stewart | 2da34c0 | 2013-10-17 15:28:56 -0700 | [diff] [blame] | 966 | if (DecideBetween(a->has_ever_connected(), b->has_ever_connected(), &ret)) { |
| 967 | *reason = kServiceSortHasEverConnected; |
Darin Petkov | 2f903b3 | 2012-04-18 12:56:43 +0200 | [diff] [blame] | 968 | return ret; |
| 969 | } |
| 970 | |
| 971 | if (DecideBetween(a->priority(), b->priority(), &ret)) { |
| 972 | *reason = kServiceSortPriority; |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 973 | return ret; |
| 974 | } |
| 975 | |
| 976 | // TODO(pstew): Below this point we are making value judgements on |
| 977 | // services that are not related to anything intrinsic or |
| 978 | // user-specified. These heuristics should be richer (contain |
| 979 | // historical information, for example) and be subject to user |
| 980 | // customization. |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 981 | for (vector<Technology::Identifier>::const_iterator it = tech_order.begin(); |
| 982 | it != tech_order.end(); |
| 983 | ++it) { |
Joshua Kroll | 053fa82 | 2012-06-05 09:50:43 -0700 | [diff] [blame] | 984 | if (DecideBetween(a->technology() == *it, b->technology() == *it, &ret)) { |
mukesh agrawal | ddc378f | 2012-02-17 18:26:20 -0800 | [diff] [blame] | 985 | *reason = kServiceSortTechnology; |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 986 | return ret; |
mukesh agrawal | ddc378f | 2012-02-17 18:26:20 -0800 | [diff] [blame] | 987 | } |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 988 | } |
| 989 | |
mukesh agrawal | 43970a2 | 2013-02-15 16:00:07 -0800 | [diff] [blame] | 990 | if (DecideBetween(a->SecurityLevel(), b->SecurityLevel(), &ret) || |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 991 | DecideBetween(a->strength(), b->strength(), &ret)) { |
mukesh agrawal | ddc378f | 2012-02-17 18:26:20 -0800 | [diff] [blame] | 992 | *reason = kServiceSortSecurityEtc; |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 993 | return ret; |
| 994 | } |
| 995 | |
mukesh agrawal | e37ad32 | 2013-10-08 16:33:56 -0700 | [diff] [blame] | 996 | *reason = kServiceSortSerialNumber; |
| 997 | return a->serial_number_ < b->serial_number_; |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 998 | } |
| 999 | |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 1000 | const ProfileRefPtr &Service::profile() const { return profile_; } |
| 1001 | |
| 1002 | void Service::set_profile(const ProfileRefPtr &p) { profile_ = p; } |
| 1003 | |
Philipp Neubeck | 7917360 | 2012-11-13 21:10:09 +0100 | [diff] [blame] | 1004 | void Service::SetProfile(const ProfileRefPtr &p) { |
| 1005 | SLOG(Service, 2) << "SetProfile from " |
| 1006 | << (profile_ ? profile_->GetFriendlyName() : "") |
| 1007 | << " to " << (p ? p->GetFriendlyName() : ""); |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 1008 | if (profile_ == p) { |
| 1009 | return; |
| 1010 | } |
Philipp Neubeck | 7917360 | 2012-11-13 21:10:09 +0100 | [diff] [blame] | 1011 | profile_ = p; |
| 1012 | Error error; |
| 1013 | string profile_rpc_id = GetProfileRpcId(&error); |
| 1014 | if (!error.IsSuccess()) { |
| 1015 | return; |
| 1016 | } |
Ben Chan | 923a502 | 2013-09-20 11:23:23 -0700 | [diff] [blame] | 1017 | adaptor_->EmitStringChanged(kProfileProperty, profile_rpc_id); |
Philipp Neubeck | 7917360 | 2012-11-13 21:10:09 +0100 | [diff] [blame] | 1018 | } |
| 1019 | |
Paul Stewart | ff14b02 | 2012-04-24 20:06:23 -0700 | [diff] [blame] | 1020 | void Service::OnPropertyChanged(const string &property) { |
Paul Stewart | c43cbbe | 2013-04-11 06:29:30 -0700 | [diff] [blame] | 1021 | if (Is8021x() && EapCredentials::IsEapAuthenticationProperty(property)) { |
| 1022 | OnEapCredentialsChanged(); |
Paul Stewart | 8142613 | 2012-05-16 10:05:10 -0700 | [diff] [blame] | 1023 | } |
mukesh agrawal | cf24a24 | 2012-05-21 16:46:11 -0700 | [diff] [blame] | 1024 | SaveToProfile(); |
Ben Chan | 923a502 | 2013-09-20 11:23:23 -0700 | [diff] [blame] | 1025 | if ((property == kCheckPortalProperty || |
| 1026 | property == kProxyConfigProperty) && |
Paul Stewart | d215af6 | 2012-04-24 23:25:50 -0700 | [diff] [blame] | 1027 | (state_ == kStateConnected || |
| 1028 | state_ == kStatePortal || |
| 1029 | state_ == kStateOnline)) { |
| 1030 | manager_->RecheckPortalOnService(this); |
| 1031 | } |
Paul Stewart | ff14b02 | 2012-04-24 20:06:23 -0700 | [diff] [blame] | 1032 | } |
| 1033 | |
Christopher Wiley | 0801d19 | 2012-09-24 11:57:15 -0700 | [diff] [blame] | 1034 | void Service::OnAfterResume() { |
| 1035 | // Forget old autoconnect failures across suspend/resume. |
| 1036 | auto_connect_cooldown_milliseconds_ = 0; |
| 1037 | reenable_auto_connect_task_.Cancel(); |
Christopher Wiley | a4c61ae | 2012-10-01 11:04:30 -0700 | [diff] [blame] | 1038 | // Forget if the user disconnected us, we might be able to connect now. |
| 1039 | explicitly_disconnected_ = false; |
Christopher Wiley | 0801d19 | 2012-09-24 11:57:15 -0700 | [diff] [blame] | 1040 | } |
| 1041 | |
Paul Stewart | 1cf7eb8 | 2013-12-03 19:34:36 -0800 | [diff] [blame] | 1042 | string Service::GetIPConfigRpcIdentifier(Error *error) const { |
Paul Stewart | 10241e3 | 2012-04-23 18:15:06 -0700 | [diff] [blame] | 1043 | if (!connection_) { |
| 1044 | error->Populate(Error::kNotFound); |
mukesh agrawal | cbfb34e | 2013-04-17 19:33:25 -0700 | [diff] [blame] | 1045 | return DBusAdaptor::kNullPath; |
Paul Stewart | 10241e3 | 2012-04-23 18:15:06 -0700 | [diff] [blame] | 1046 | } |
| 1047 | |
| 1048 | string id = connection_->ipconfig_rpc_identifier(); |
| 1049 | |
| 1050 | if (id.empty()) { |
| 1051 | // Do not return an empty IPConfig. |
| 1052 | error->Populate(Error::kNotFound); |
mukesh agrawal | cbfb34e | 2013-04-17 19:33:25 -0700 | [diff] [blame] | 1053 | return DBusAdaptor::kNullPath; |
Paul Stewart | 10241e3 | 2012-04-23 18:15:06 -0700 | [diff] [blame] | 1054 | } |
| 1055 | |
| 1056 | return id; |
| 1057 | } |
| 1058 | |
mukesh agrawal | cbfb34e | 2013-04-17 19:33:25 -0700 | [diff] [blame] | 1059 | void Service::SetConnectable(bool connectable) { |
| 1060 | if (connectable_ == connectable) |
| 1061 | return; |
mukesh agrawal | 29c13a1 | 2011-11-24 00:09:19 +0000 | [diff] [blame] | 1062 | connectable_ = connectable; |
Ben Chan | 923a502 | 2013-09-20 11:23:23 -0700 | [diff] [blame] | 1063 | adaptor_->EmitBoolChanged(kConnectableProperty, connectable_); |
mukesh agrawal | 29c13a1 | 2011-11-24 00:09:19 +0000 | [diff] [blame] | 1064 | } |
| 1065 | |
mukesh agrawal | cbfb34e | 2013-04-17 19:33:25 -0700 | [diff] [blame] | 1066 | void Service::SetConnectableFull(bool connectable) { |
Darin Petkov | b2ba39f | 2012-06-06 10:33:43 +0200 | [diff] [blame] | 1067 | if (connectable_ == connectable) { |
| 1068 | return; |
| 1069 | } |
mukesh agrawal | cbfb34e | 2013-04-17 19:33:25 -0700 | [diff] [blame] | 1070 | SetConnectable(connectable); |
Darin Petkov | b2ba39f | 2012-06-06 10:33:43 +0200 | [diff] [blame] | 1071 | if (manager_->HasService(this)) { |
| 1072 | manager_->UpdateService(this); |
| 1073 | } |
| 1074 | } |
| 1075 | |
Darin Petkov | 58f0b6d | 2012-06-12 12:52:30 +0200 | [diff] [blame] | 1076 | string Service::GetStateString() const { |
Ben Chan | 923a502 | 2013-09-20 11:23:23 -0700 | [diff] [blame] | 1077 | // TODO(benchan): We may want to rename shill::kState* to avoid name clashing |
| 1078 | // with Service::kState*. |
Paul Stewart | 03e29f7 | 2013-09-26 00:49:48 -0700 | [diff] [blame] | 1079 | switch (state()) { |
Gaurav Shah | c6d6c72 | 2011-11-17 18:59:39 -0800 | [diff] [blame] | 1080 | case kStateIdle: |
Ben Chan | 923a502 | 2013-09-20 11:23:23 -0700 | [diff] [blame] | 1081 | return shill::kStateIdle; |
Gaurav Shah | c6d6c72 | 2011-11-17 18:59:39 -0800 | [diff] [blame] | 1082 | case kStateAssociating: |
Ben Chan | 923a502 | 2013-09-20 11:23:23 -0700 | [diff] [blame] | 1083 | return shill::kStateAssociation; |
Gaurav Shah | c6d6c72 | 2011-11-17 18:59:39 -0800 | [diff] [blame] | 1084 | case kStateConfiguring: |
Ben Chan | 923a502 | 2013-09-20 11:23:23 -0700 | [diff] [blame] | 1085 | return shill::kStateConfiguration; |
mukesh agrawal | f2f68a5 | 2011-09-01 12:15:48 -0700 | [diff] [blame] | 1086 | case kStateConnected: |
Ben Chan | 923a502 | 2013-09-20 11:23:23 -0700 | [diff] [blame] | 1087 | return shill::kStateReady; |
Gaurav Shah | c6d6c72 | 2011-11-17 18:59:39 -0800 | [diff] [blame] | 1088 | case kStateFailure: |
Ben Chan | 923a502 | 2013-09-20 11:23:23 -0700 | [diff] [blame] | 1089 | return shill::kStateFailure; |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 1090 | case kStatePortal: |
Ben Chan | 923a502 | 2013-09-20 11:23:23 -0700 | [diff] [blame] | 1091 | return shill::kStatePortal; |
Gaurav Shah | c6d6c72 | 2011-11-17 18:59:39 -0800 | [diff] [blame] | 1092 | case kStateOnline: |
Ben Chan | 923a502 | 2013-09-20 11:23:23 -0700 | [diff] [blame] | 1093 | return shill::kStateOnline; |
Gaurav Shah | c6d6c72 | 2011-11-17 18:59:39 -0800 | [diff] [blame] | 1094 | case kStateUnknown: |
mukesh agrawal | f2f68a5 | 2011-09-01 12:15:48 -0700 | [diff] [blame] | 1095 | default: |
Gaurav Shah | c6d6c72 | 2011-11-17 18:59:39 -0800 | [diff] [blame] | 1096 | return ""; |
mukesh agrawal | f2f68a5 | 2011-09-01 12:15:48 -0700 | [diff] [blame] | 1097 | } |
| 1098 | } |
| 1099 | |
Darin Petkov | 58f0b6d | 2012-06-12 12:52:30 +0200 | [diff] [blame] | 1100 | string Service::CalculateState(Error */*error*/) { |
| 1101 | return GetStateString(); |
| 1102 | } |
| 1103 | |
mukesh agrawal | bf14e94 | 2012-03-02 14:36:34 -0800 | [diff] [blame] | 1104 | bool Service::IsAutoConnectable(const char **reason) const { |
| 1105 | if (!connectable()) { |
| 1106 | *reason = kAutoConnNotConnectable; |
| 1107 | return false; |
| 1108 | } |
| 1109 | |
| 1110 | if (IsConnected()) { |
| 1111 | *reason = kAutoConnConnected; |
| 1112 | return false; |
| 1113 | } |
| 1114 | |
| 1115 | if (IsConnecting()) { |
| 1116 | *reason = kAutoConnConnecting; |
| 1117 | return false; |
| 1118 | } |
| 1119 | |
| 1120 | if (explicitly_disconnected_) { |
| 1121 | *reason = kAutoConnExplicitDisconnect; |
| 1122 | return false; |
| 1123 | } |
| 1124 | |
Christopher Wiley | 0801d19 | 2012-09-24 11:57:15 -0700 | [diff] [blame] | 1125 | if (!reenable_auto_connect_task_.IsCancelled()) { |
| 1126 | *reason = kAutoConnThrottled; |
| 1127 | return false; |
| 1128 | } |
| 1129 | |
Darin Petkov | 4cbff5b | 2013-01-29 16:29:05 +0100 | [diff] [blame] | 1130 | if (!Technology::IsPrimaryConnectivityTechnology(technology_) && |
| 1131 | !manager_->IsOnline()) { |
| 1132 | *reason = kAutoConnOffline; |
| 1133 | return false; |
| 1134 | } |
| 1135 | |
mukesh agrawal | bf14e94 | 2012-03-02 14:36:34 -0800 | [diff] [blame] | 1136 | return true; |
mukesh agrawal | 76d1388 | 2012-01-12 15:23:11 -0800 | [diff] [blame] | 1137 | } |
| 1138 | |
Paul Stewart | d215af6 | 2012-04-24 23:25:50 -0700 | [diff] [blame] | 1139 | bool Service::IsPortalDetectionDisabled() const { |
| 1140 | return check_portal_ == kCheckPortalFalse; |
| 1141 | } |
| 1142 | |
| 1143 | bool Service::IsPortalDetectionAuto() const { |
| 1144 | return check_portal_ == kCheckPortalAuto; |
| 1145 | } |
| 1146 | |
mukesh agrawal | ffa3d04 | 2011-10-06 15:26:10 -0700 | [diff] [blame] | 1147 | void Service::HelpRegisterDerivedBool( |
| 1148 | const string &name, |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 1149 | bool(Service::*get)(Error *), |
Paul Stewart | 43d8dc0 | 2013-10-17 10:32:53 -0700 | [diff] [blame] | 1150 | bool(Service::*set)(const bool&, Error *), |
| 1151 | void(Service::*clear)(Error *)) { |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 1152 | store_.RegisterDerivedBool( |
| 1153 | name, |
Paul Stewart | 43d8dc0 | 2013-10-17 10:32:53 -0700 | [diff] [blame] | 1154 | BoolAccessor(new CustomAccessor<Service, bool>(this, get, set, clear))); |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 1155 | } |
| 1156 | |
mukesh agrawal | cbfb34e | 2013-04-17 19:33:25 -0700 | [diff] [blame] | 1157 | void Service::HelpRegisterDerivedInt32( |
| 1158 | const string &name, |
| 1159 | int32(Service::*get)(Error *), |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 1160 | bool(Service::*set)(const int32&, Error *)) { |
mukesh agrawal | cbfb34e | 2013-04-17 19:33:25 -0700 | [diff] [blame] | 1161 | store_.RegisterDerivedInt32( |
| 1162 | name, |
| 1163 | Int32Accessor(new CustomAccessor<Service, int32>(this, get, set))); |
| 1164 | } |
| 1165 | |
mukesh agrawal | ffa3d04 | 2011-10-06 15:26:10 -0700 | [diff] [blame] | 1166 | void Service::HelpRegisterDerivedString( |
| 1167 | const string &name, |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 1168 | string(Service::*get)(Error *), |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 1169 | bool(Service::*set)(const string&, Error *)) { |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 1170 | store_.RegisterDerivedString( |
| 1171 | name, |
| 1172 | StringAccessor(new CustomAccessor<Service, string>(this, get, set))); |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 1173 | } |
| 1174 | |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 1175 | void Service::HelpRegisterConstDerivedRpcIdentifier( |
Jason Glasgow | acdc11f | 2012-03-30 14:12:22 -0400 | [diff] [blame] | 1176 | const string &name, |
Paul Stewart | 1cf7eb8 | 2013-12-03 19:34:36 -0800 | [diff] [blame] | 1177 | RpcIdentifier(Service::*get)(Error *) const) { |
Jason Glasgow | acdc11f | 2012-03-30 14:12:22 -0400 | [diff] [blame] | 1178 | store_.RegisterDerivedRpcIdentifier( |
| 1179 | name, |
Paul Stewart | 1cf7eb8 | 2013-12-03 19:34:36 -0800 | [diff] [blame] | 1180 | RpcIdentifierAccessor(new CustomReadOnlyAccessor<Service, RpcIdentifier>( |
| 1181 | this, get))); |
Jason Glasgow | acdc11f | 2012-03-30 14:12:22 -0400 | [diff] [blame] | 1182 | } |
| 1183 | |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 1184 | void Service::HelpRegisterConstDerivedUint16( |
Paul Stewart | be5f5b3 | 2011-12-07 17:11:11 -0800 | [diff] [blame] | 1185 | const string &name, |
Paul Stewart | 1cf7eb8 | 2013-12-03 19:34:36 -0800 | [diff] [blame] | 1186 | uint16(Service::*get)(Error *) const) { |
Paul Stewart | be5f5b3 | 2011-12-07 17:11:11 -0800 | [diff] [blame] | 1187 | store_.RegisterDerivedUint16( |
| 1188 | name, |
Paul Stewart | 1cf7eb8 | 2013-12-03 19:34:36 -0800 | [diff] [blame] | 1189 | Uint16Accessor(new CustomReadOnlyAccessor<Service, uint16>(this, get))); |
Paul Stewart | be5f5b3 | 2011-12-07 17:11:11 -0800 | [diff] [blame] | 1190 | } |
| 1191 | |
Darin Petkov | 0c65bdd | 2012-12-05 13:42:41 +0100 | [diff] [blame] | 1192 | void Service::HelpRegisterConstDerivedStrings( |
Paul Stewart | 1cf7eb8 | 2013-12-03 19:34:36 -0800 | [diff] [blame] | 1193 | const string &name, Strings(Service::*get)(Error *error) const) { |
Darin Petkov | 0c65bdd | 2012-12-05 13:42:41 +0100 | [diff] [blame] | 1194 | store_.RegisterDerivedStrings( |
| 1195 | name, |
Paul Stewart | 1cf7eb8 | 2013-12-03 19:34:36 -0800 | [diff] [blame] | 1196 | StringsAccessor(new CustomReadOnlyAccessor<Service, Strings>(this, get))); |
| 1197 | } |
| 1198 | |
| 1199 | void Service::HelpRegisterConstDerivedString( |
| 1200 | const string &name, string(Service::*get)(Error *error) const) { |
| 1201 | store_.RegisterDerivedString( |
| 1202 | name, |
| 1203 | StringAccessor(new CustomReadOnlyAccessor<Service, string>(this, get))); |
Darin Petkov | 0c65bdd | 2012-12-05 13:42:41 +0100 | [diff] [blame] | 1204 | } |
| 1205 | |
Paul Stewart | c43cbbe | 2013-04-11 06:29:30 -0700 | [diff] [blame] | 1206 | // static |
Paul Stewart | c3dbff1 | 2013-07-17 10:32:48 -0700 | [diff] [blame] | 1207 | void Service::LoadString(StoreInterface *storage, |
| 1208 | const string &id, |
| 1209 | const string &key, |
| 1210 | const string &default_value, |
| 1211 | string *value) { |
| 1212 | if (!storage->GetString(id, key, value)) { |
| 1213 | *value = default_value; |
| 1214 | } |
| 1215 | } |
| 1216 | |
| 1217 | // static |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 1218 | void Service::SaveString(StoreInterface *storage, |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 1219 | const string &id, |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 1220 | const string &key, |
| 1221 | const string &value, |
| 1222 | bool crypted, |
| 1223 | bool save) { |
| 1224 | if (value.empty() || !save) { |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 1225 | storage->DeleteKey(id, key); |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 1226 | return; |
| 1227 | } |
| 1228 | if (crypted) { |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 1229 | storage->SetCryptedString(id, key, value); |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 1230 | return; |
| 1231 | } |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 1232 | storage->SetString(id, key, value); |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 1233 | } |
| 1234 | |
Paul Stewart | 967eaeb | 2013-04-25 19:53:07 -0700 | [diff] [blame] | 1235 | map<string, string> Service::GetLoadableProfileEntries() { |
| 1236 | return manager_->GetLoadableProfileEntriesForService(this); |
| 1237 | } |
| 1238 | |
Paul Stewart | cb59fed | 2012-03-21 21:14:46 -0700 | [diff] [blame] | 1239 | void Service::IgnoreParameterForConfigure(const string ¶meter) { |
| 1240 | parameters_ignored_for_configure_.insert(parameter); |
| 1241 | } |
| 1242 | |
Paul Stewart | ac4ac00 | 2011-08-26 12:04:26 -0700 | [diff] [blame] | 1243 | const string &Service::GetEAPKeyManagement() const { |
Paul Stewart | c43cbbe | 2013-04-11 06:29:30 -0700 | [diff] [blame] | 1244 | CHECK(eap()); |
| 1245 | return eap()->key_management(); |
Paul Stewart | ac4ac00 | 2011-08-26 12:04:26 -0700 | [diff] [blame] | 1246 | } |
| 1247 | |
| 1248 | void Service::SetEAPKeyManagement(const string &key_management) { |
Paul Stewart | c43cbbe | 2013-04-11 06:29:30 -0700 | [diff] [blame] | 1249 | CHECK(mutable_eap()); |
| 1250 | mutable_eap()->SetKeyManagement(key_management, NULL); |
Paul Stewart | ac4ac00 | 2011-08-26 12:04:26 -0700 | [diff] [blame] | 1251 | } |
| 1252 | |
Thieu Le | 284fe79 | 2012-01-31 17:53:19 -0800 | [diff] [blame] | 1253 | bool Service::GetAutoConnect(Error */*error*/) { |
| 1254 | return auto_connect(); |
| 1255 | } |
| 1256 | |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 1257 | bool Service::SetAutoConnectFull(const bool &connect, Error */*error*/) { |
Darin Petkov | 36d962d | 2013-03-25 13:03:14 +0100 | [diff] [blame] | 1258 | LOG(INFO) << "Service " << unique_name() << ": AutoConnect=" |
| 1259 | << auto_connect() << "->" << connect; |
Paul Stewart | 36e67be | 2013-12-12 14:36:10 -0800 | [diff] [blame] | 1260 | RetainAutoConnect(); |
| 1261 | |
| 1262 | if (auto_connect() == connect) { |
| 1263 | return false; |
Darin Petkov | 36d962d | 2013-03-25 13:03:14 +0100 | [diff] [blame] | 1264 | } |
Paul Stewart | 96a6d09 | 2013-08-26 09:32:49 -0700 | [diff] [blame] | 1265 | |
Paul Stewart | 36e67be | 2013-12-12 14:36:10 -0800 | [diff] [blame] | 1266 | SetAutoConnect(connect); |
| 1267 | manager_->UpdateService(this); |
| 1268 | return true; |
Thieu Le | 284fe79 | 2012-01-31 17:53:19 -0800 | [diff] [blame] | 1269 | } |
| 1270 | |
Paul Stewart | 43d8dc0 | 2013-10-17 10:32:53 -0700 | [diff] [blame] | 1271 | void Service::ClearAutoConnect(Error */*error*/) { |
Paul Stewart | 43d8dc0 | 2013-10-17 10:32:53 -0700 | [diff] [blame] | 1272 | if (auto_connect()) { |
| 1273 | SetAutoConnect(false); |
Paul Stewart | 43d8dc0 | 2013-10-17 10:32:53 -0700 | [diff] [blame] | 1274 | manager_->UpdateService(this); |
| 1275 | } |
Paul Stewart | 2da34c0 | 2013-10-17 15:28:56 -0700 | [diff] [blame] | 1276 | |
| 1277 | retain_auto_connect_ = false; |
Paul Stewart | 43d8dc0 | 2013-10-17 10:32:53 -0700 | [diff] [blame] | 1278 | } |
| 1279 | |
Paul Stewart | d215af6 | 2012-04-24 23:25:50 -0700 | [diff] [blame] | 1280 | string Service::GetCheckPortal(Error *error) { |
| 1281 | return check_portal_; |
| 1282 | } |
| 1283 | |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 1284 | bool Service::SetCheckPortal(const string &check_portal, Error *error) { |
Paul Stewart | d215af6 | 2012-04-24 23:25:50 -0700 | [diff] [blame] | 1285 | if (check_portal != kCheckPortalFalse && |
| 1286 | check_portal != kCheckPortalTrue && |
| 1287 | check_portal != kCheckPortalAuto) { |
| 1288 | Error::PopulateAndLog(error, Error::kInvalidArguments, |
| 1289 | base::StringPrintf( |
| 1290 | "Invalid Service CheckPortal property value: %s", |
| 1291 | check_portal.c_str())); |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 1292 | return false; |
| 1293 | } |
| 1294 | if (check_portal == check_portal_) { |
| 1295 | return false; |
Paul Stewart | d215af6 | 2012-04-24 23:25:50 -0700 | [diff] [blame] | 1296 | } |
| 1297 | check_portal_ = check_portal; |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 1298 | return true; |
Paul Stewart | d215af6 | 2012-04-24 23:25:50 -0700 | [diff] [blame] | 1299 | } |
| 1300 | |
mukesh agrawal | cbfb34e | 2013-04-17 19:33:25 -0700 | [diff] [blame] | 1301 | string Service::GetGuid(Error *error) { |
| 1302 | return guid_; |
| 1303 | } |
| 1304 | |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 1305 | bool Service::SetGuid(const string &guid, Error */*error*/) { |
| 1306 | if (guid_ == guid) { |
| 1307 | return false; |
mukesh agrawal | cbfb34e | 2013-04-17 19:33:25 -0700 | [diff] [blame] | 1308 | } |
| 1309 | guid_ = guid; |
Ben Chan | 923a502 | 2013-09-20 11:23:23 -0700 | [diff] [blame] | 1310 | adaptor_->EmitStringChanged(kGuidProperty, guid_); |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 1311 | return true; |
mukesh agrawal | cbfb34e | 2013-04-17 19:33:25 -0700 | [diff] [blame] | 1312 | } |
| 1313 | |
Paul Stewart | 2da34c0 | 2013-10-17 15:28:56 -0700 | [diff] [blame] | 1314 | void Service::RetainAutoConnect() { |
| 1315 | retain_auto_connect_ = true; |
mukesh agrawal | cbfb34e | 2013-04-17 19:33:25 -0700 | [diff] [blame] | 1316 | } |
| 1317 | |
mukesh agrawal | 43970a2 | 2013-02-15 16:00:07 -0800 | [diff] [blame] | 1318 | void Service::SetSecurity(CryptoAlgorithm crypto_algorithm, bool key_rotation, |
| 1319 | bool endpoint_auth) { |
| 1320 | crypto_algorithm_ = crypto_algorithm; |
| 1321 | key_rotation_ = key_rotation; |
| 1322 | endpoint_auth_ = endpoint_auth; |
| 1323 | } |
| 1324 | |
mukesh agrawal | d4dc083 | 2013-03-25 14:38:26 -0700 | [diff] [blame] | 1325 | string Service::GetNameProperty(Error */*error*/) { |
Paul Stewart | 0c43833 | 2012-04-11 07:55:27 -0700 | [diff] [blame] | 1326 | return friendly_name_; |
| 1327 | } |
| 1328 | |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 1329 | bool Service::SetNameProperty(const string &name, Error *error) { |
Paul Stewart | 0c43833 | 2012-04-11 07:55:27 -0700 | [diff] [blame] | 1330 | if (name != friendly_name_) { |
| 1331 | Error::PopulateAndLog(error, Error::kInvalidArguments, |
Paul Stewart | 2bf424f | 2012-04-11 18:59:39 -0700 | [diff] [blame] | 1332 | base::StringPrintf( |
Darin Petkov | 457728b | 2013-01-09 09:49:08 +0100 | [diff] [blame] | 1333 | "Service %s Name property cannot be modified.", |
| 1334 | unique_name_.c_str())); |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 1335 | return false; |
Paul Stewart | 0c43833 | 2012-04-11 07:55:27 -0700 | [diff] [blame] | 1336 | } |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 1337 | return false; |
Paul Stewart | 0c43833 | 2012-04-11 07:55:27 -0700 | [diff] [blame] | 1338 | } |
| 1339 | |
mukesh agrawal | cbfb34e | 2013-04-17 19:33:25 -0700 | [diff] [blame] | 1340 | int32 Service::GetPriority(Error *error) { |
| 1341 | return priority_; |
| 1342 | } |
| 1343 | |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 1344 | bool Service::SetPriority(const int32 &priority, Error *error) { |
| 1345 | if (priority_ == priority) { |
| 1346 | return false; |
mukesh agrawal | cbfb34e | 2013-04-17 19:33:25 -0700 | [diff] [blame] | 1347 | } |
| 1348 | priority_ = priority; |
Ben Chan | 923a502 | 2013-09-20 11:23:23 -0700 | [diff] [blame] | 1349 | adaptor_->EmitIntChanged(kPriorityProperty, priority_); |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 1350 | return true; |
mukesh agrawal | cbfb34e | 2013-04-17 19:33:25 -0700 | [diff] [blame] | 1351 | } |
| 1352 | |
Paul Stewart | 1b1a7f2 | 2012-01-06 16:24:06 -0800 | [diff] [blame] | 1353 | string Service::GetProfileRpcId(Error *error) { |
| 1354 | if (!profile_) { |
| 1355 | // This happens in some unit tests where profile_ is not set. |
| 1356 | error->Populate(Error::kNotFound); |
| 1357 | return ""; |
| 1358 | } |
| 1359 | return profile_->GetRpcIdentifier(); |
| 1360 | } |
| 1361 | |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 1362 | bool Service::SetProfileRpcId(const string &profile, Error *error) { |
| 1363 | if (profile_ && profile_->GetRpcIdentifier() == profile) { |
| 1364 | return false; |
| 1365 | } |
| 1366 | ProfileConstRefPtr old_profile = profile_; |
| 1367 | // No need to Emit afterwards, since SetProfileForService will call |
| 1368 | // into SetProfile (if the profile actually changes). |
Paul Stewart | 1b1a7f2 | 2012-01-06 16:24:06 -0800 | [diff] [blame] | 1369 | manager_->SetProfileForService(this, profile, error); |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 1370 | // Can't just use error.IsSuccess(), because that also requires saving |
| 1371 | // the profile to succeed. (See Profile::AdoptService) |
| 1372 | return (profile_ != old_profile); |
Paul Stewart | 1b1a7f2 | 2012-01-06 16:24:06 -0800 | [diff] [blame] | 1373 | } |
| 1374 | |
Paul Stewart | 1cf7eb8 | 2013-12-03 19:34:36 -0800 | [diff] [blame] | 1375 | uint16 Service::GetHTTPProxyPort(Error */*error*/) const { |
Paul Stewart | be5f5b3 | 2011-12-07 17:11:11 -0800 | [diff] [blame] | 1376 | if (http_proxy_.get()) { |
| 1377 | return static_cast<uint16>(http_proxy_->proxy_port()); |
| 1378 | } |
| 1379 | return 0; |
| 1380 | } |
| 1381 | |
Philipp Neubeck | 7917360 | 2012-11-13 21:10:09 +0100 | [diff] [blame] | 1382 | string Service::GetProxyConfig(Error *error) { |
| 1383 | return proxy_config_; |
| 1384 | } |
| 1385 | |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 1386 | bool Service::SetProxyConfig(const string &proxy_config, Error *error) { |
| 1387 | if (proxy_config_ == proxy_config) |
| 1388 | return false; |
Philipp Neubeck | 7917360 | 2012-11-13 21:10:09 +0100 | [diff] [blame] | 1389 | proxy_config_ = proxy_config; |
Ben Chan | 923a502 | 2013-09-20 11:23:23 -0700 | [diff] [blame] | 1390 | adaptor_->EmitStringChanged(kProxyConfigProperty, proxy_config_); |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 1391 | return true; |
Philipp Neubeck | 7917360 | 2012-11-13 21:10:09 +0100 | [diff] [blame] | 1392 | } |
| 1393 | |
Paul Stewart | fa11e28 | 2013-12-02 22:04:25 -0800 | [diff] [blame] | 1394 | string Service::GetTethering(Error *error) const { |
| 1395 | // The "Tethering" property isn't supported by the Service base class, and |
| 1396 | // therefore should not be listed in the properties returned by |
| 1397 | // the GetProperties() RPC method. |
| 1398 | error->Populate(Error::kNotSupported); |
| 1399 | return ""; |
| 1400 | } |
| 1401 | |
Darin Petkov | 0c65bdd | 2012-12-05 13:42:41 +0100 | [diff] [blame] | 1402 | // static |
| 1403 | Strings Service::ExtractWallClockToStrings( |
| 1404 | const deque<Timestamp> ×tamps) { |
| 1405 | Strings strings; |
| 1406 | for (deque<Timestamp>::const_iterator it = timestamps.begin(); |
| 1407 | it != timestamps.end(); ++it) { |
| 1408 | strings.push_back(it->wall_clock); |
| 1409 | } |
| 1410 | return strings; |
| 1411 | } |
| 1412 | |
Paul Stewart | 1cf7eb8 | 2013-12-03 19:34:36 -0800 | [diff] [blame] | 1413 | Strings Service::GetDisconnectsProperty(Error */*error*/) const { |
Darin Petkov | 0c65bdd | 2012-12-05 13:42:41 +0100 | [diff] [blame] | 1414 | return ExtractWallClockToStrings(disconnects_); |
| 1415 | } |
| 1416 | |
Paul Stewart | 1cf7eb8 | 2013-12-03 19:34:36 -0800 | [diff] [blame] | 1417 | Strings Service::GetMisconnectsProperty(Error */*error*/) const { |
Darin Petkov | 0c65bdd | 2012-12-05 13:42:41 +0100 | [diff] [blame] | 1418 | return ExtractWallClockToStrings(misconnects_); |
| 1419 | } |
| 1420 | |
mukesh agrawal | cf24a24 | 2012-05-21 16:46:11 -0700 | [diff] [blame] | 1421 | void Service::SaveToProfile() { |
| 1422 | if (profile_.get() && profile_->GetConstStorage()) { |
| 1423 | profile_->UpdateService(this); |
| 1424 | } |
| 1425 | } |
| 1426 | |
Arman Uguray | 2717a10 | 2013-01-29 23:36:06 -0800 | [diff] [blame] | 1427 | void Service::SetFriendlyName(const string &friendly_name) { |
| 1428 | if (friendly_name == friendly_name_) |
| 1429 | return; |
| 1430 | friendly_name_ = friendly_name; |
Ben Chan | 923a502 | 2013-09-20 11:23:23 -0700 | [diff] [blame] | 1431 | adaptor()->EmitStringChanged(kNameProperty, friendly_name_); |
Arman Uguray | 2717a10 | 2013-01-29 23:36:06 -0800 | [diff] [blame] | 1432 | } |
| 1433 | |
Darin Petkov | d78ee7e | 2012-01-12 11:21:10 +0100 | [diff] [blame] | 1434 | void Service::SetStrength(uint8 strength) { |
| 1435 | if (strength == strength_) { |
| 1436 | return; |
| 1437 | } |
| 1438 | strength_ = strength; |
Ben Chan | 923a502 | 2013-09-20 11:23:23 -0700 | [diff] [blame] | 1439 | adaptor_->EmitUint8Changed(kSignalStrengthProperty, strength); |
Darin Petkov | d78ee7e | 2012-01-12 11:21:10 +0100 | [diff] [blame] | 1440 | } |
| 1441 | |
Darin Petkov | aba8932 | 2013-03-11 14:48:22 +0100 | [diff] [blame] | 1442 | void Service::SetErrorDetails(const string &details) { |
| 1443 | if (error_details_ == details) { |
| 1444 | return; |
| 1445 | } |
| 1446 | error_details_ = details; |
Ben Chan | 39a7beb | 2013-09-21 11:28:00 -0700 | [diff] [blame] | 1447 | adaptor_->EmitStringChanged(kErrorDetailsProperty, error_details_); |
Darin Petkov | aba8932 | 2013-03-11 14:48:22 +0100 | [diff] [blame] | 1448 | } |
| 1449 | |
Paul Stewart | f2d6091 | 2012-07-15 08:37:30 -0700 | [diff] [blame] | 1450 | void Service::UpdateErrorProperty() { |
| 1451 | const string error(ConnectFailureToString(failure_)); |
| 1452 | if (error == error_) { |
| 1453 | return; |
| 1454 | } |
| 1455 | error_ = error; |
Ben Chan | 923a502 | 2013-09-20 11:23:23 -0700 | [diff] [blame] | 1456 | adaptor_->EmitStringChanged(kErrorProperty, error); |
Paul Stewart | f2d6091 | 2012-07-15 08:37:30 -0700 | [diff] [blame] | 1457 | } |
| 1458 | |
Paul Stewart | dded007 | 2013-10-24 12:38:54 -0700 | [diff] [blame] | 1459 | void Service::OnDHCPFailure() { |
| 1460 | consecutive_dhcp_failures_++; |
| 1461 | switch (dhcp_option_failure_state_) { |
| 1462 | case kDHCPOptionFailureNotDetected: |
| 1463 | // If we run into too many consecutive DHCP failures, we must suspect |
| 1464 | // that this may be due in part to the number of options we are requesting |
| 1465 | // from the server. Next time, we should try asking for less options. |
| 1466 | if (consecutive_dhcp_failures_ >= kMaxDHCPOptionFailures) { |
| 1467 | SLOG(Service, 2) << "Service " << unique_name_ << " is failing to " |
| 1468 | << "receive DHCP responses. The next attempt will " |
| 1469 | << "be with minimal DHCP options."; |
| 1470 | dhcp_option_failure_state_ = kDHCPOptionFailureSuspected; |
| 1471 | } |
| 1472 | break; |
| 1473 | |
| 1474 | case kDHCPOptionFailureSuspected: |
| 1475 | // Requesting a shorter DHCP reply does not seem to have helped. As |
| 1476 | // such, we should exonerate DHCP options as the source of the failures. |
| 1477 | SLOG(Service, 2) << "Service " << unique_name_ << " did not get a DHCP " |
| 1478 | << "response even with minimal options set."; |
| 1479 | dhcp_option_failure_state_ = kDHCPOptionFailureNotDetected; |
| 1480 | |
| 1481 | // Unless we set our failure count back to 0, we'll end up toggling back |
| 1482 | // and forth endlessly between the Suspected and NotDetected states. |
| 1483 | consecutive_dhcp_failures_ = 0; |
| 1484 | break; |
| 1485 | |
| 1486 | case kDHCPOptionFailureConfirmed: |
| 1487 | // Nothing to do here. We've previously confirmed that this network |
| 1488 | // has problems with requests for a large number of DHCP options, so |
| 1489 | // we'll continue to send small requests until our timeout expires. |
| 1490 | break; |
| 1491 | |
| 1492 | case kDHCPOptionFailureRetestFullRequest: |
| 1493 | // This means that we are still running into issues with DHCP responses |
| 1494 | // when we send a full request. It's likely that the problem with this |
| 1495 | // network still exists. Let's confirm this by switching back to minimal |
| 1496 | // requests. |
| 1497 | SLOG(Service, 2) << "Service " << unique_name_ << " tried a full DHCP " |
| 1498 | << "request and it still appears to be failing."; |
| 1499 | dhcp_option_failure_state_ = kDHCPOptionFailureRetestMinimalRequest; |
| 1500 | break; |
| 1501 | |
| 1502 | case kDHCPOptionFailureRetestMinimalRequest: |
| 1503 | // Requesting a shorter DHCP reply does not seem to have helped. |
| 1504 | // However we still have a memory of this once being a problem, so |
| 1505 | // it is probably best to continue sending short requests and postpone |
| 1506 | // a re-test until we get a reply. |
| 1507 | SLOG(Service, 2) << "Service " << unique_name_ << " seems not to be " |
| 1508 | << "getting any DHCP responses at all."; |
| 1509 | dhcp_option_failure_state_ = kDHCPOptionFailureRetestGotNoReply; |
| 1510 | break; |
| 1511 | |
| 1512 | case kDHCPOptionFailureRetestGotNoReply: |
| 1513 | // Nothing to do here. We are still failing to receive a reply. |
| 1514 | break; |
| 1515 | } |
| 1516 | } |
| 1517 | |
| 1518 | void Service::OnDHCPSuccess() { |
| 1519 | consecutive_dhcp_failures_ = 0; |
| 1520 | switch (dhcp_option_failure_state_) { |
| 1521 | case kDHCPOptionFailureNotDetected: |
| 1522 | // Nothing to do. All is well with the world. |
| 1523 | break; |
| 1524 | |
| 1525 | case kDHCPOptionFailureSuspected: // FALLTHROUGH |
| 1526 | case kDHCPOptionFailureRetestMinimalRequest: |
| 1527 | LOG(WARNING) << "Service " << unique_name_ << " can get a DHCP " |
| 1528 | << "response only with minimal options requested."; |
| 1529 | // We just performed an experiment to prove that the DHCP failures we |
| 1530 | // have been having might be related to the number of options we have |
| 1531 | // been requesting. It appears that as soon as we reduced the option |
| 1532 | // count, our request succeeded. |
| 1533 | dhcp_option_failure_state_ = kDHCPOptionFailureConfirmed; |
| 1534 | last_dhcp_option_failure_ = time_->GetNow(); |
| 1535 | metrics_->NotifyDHCPOptionFailure(*this); |
| 1536 | break; |
| 1537 | |
| 1538 | case kDHCPOptionFailureConfirmed: |
| 1539 | // Nothing to do here. We've previously confirmed that this network |
| 1540 | // has problems with requests for a large number of DHCP options, so |
| 1541 | // we'll continue to send small requests until our timeout expires. |
| 1542 | break; |
| 1543 | |
| 1544 | case kDHCPOptionFailureRetestFullRequest: |
| 1545 | // We expected this request to fail, since we attempted a full request |
| 1546 | // on a network we previously believed dropped DHCP replies for such |
| 1547 | // requests. Let's exonerate this network. |
| 1548 | SLOG(Service, 2) << "Service " << unique_name_ << " was able to receive " |
| 1549 | << "a response to a full DHCP request. Switching back " |
| 1550 | << "to full requests by default."; |
| 1551 | dhcp_option_failure_state_ = kDHCPOptionFailureNotDetected; |
| 1552 | break; |
| 1553 | |
| 1554 | case kDHCPOptionFailureRetestGotNoReply: |
| 1555 | // We are finally receiving DHCP replies again. Resume the test |
| 1556 | // starting with the next time we perform a DHCP request. |
| 1557 | SLOG(Service, 2) << "Service " << unique_name_ << " was finally able to " |
| 1558 | << "receive a DHCP response."; |
| 1559 | dhcp_option_failure_state_ = kDHCPOptionFailureRetestFullRequest; |
| 1560 | break; |
| 1561 | } |
| 1562 | } |
| 1563 | |
| 1564 | bool Service::ShouldUseMinimalDHCPConfig() { |
| 1565 | // If it's been a while since we have tested for options failures, we should |
| 1566 | // re-confirm if this issue still exists. |
| 1567 | if (dhcp_option_failure_state_ == kDHCPOptionFailureConfirmed) { |
| 1568 | Timestamp now = time_->GetNow(); |
| 1569 | struct timeval elapsed { 0 }; |
| 1570 | timersub(&now.monotonic, &last_dhcp_option_failure_.monotonic, &elapsed); |
| 1571 | struct timeval period { kDHCPOptionHoldOffPeriodSeconds }; |
| 1572 | if (timercmp(&elapsed, &period, >=)) { |
| 1573 | SLOG(Service, 2) << "Service " << unique_name_ << " will be re-tested to " |
| 1574 | << "see if the server responds to a full DHCP request."; |
| 1575 | dhcp_option_failure_state_ = kDHCPOptionFailureRetestFullRequest; |
| 1576 | } |
| 1577 | } |
| 1578 | |
| 1579 | return |
| 1580 | dhcp_option_failure_state_ == kDHCPOptionFailureSuspected || |
| 1581 | dhcp_option_failure_state_ == kDHCPOptionFailureConfirmed || |
| 1582 | dhcp_option_failure_state_ == kDHCPOptionFailureRetestMinimalRequest || |
| 1583 | dhcp_option_failure_state_ == kDHCPOptionFailureRetestGotNoReply; |
| 1584 | } |
| 1585 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 1586 | } // namespace shill |