blob: 26704ae634888cb55e2838975e2cea776e607f5a [file] [log] [blame]
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Paul Stewart75897df2011-04-27 09:05:53 -07002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Chris Masoneb2e326b2011-07-12 13:28:51 -07005#include "shill/service.h"
6
Paul Stewart75897df2011-04-27 09:05:53 -07007#include <time.h>
Paul Stewart75897df2011-04-27 09:05:53 -07008#include <stdio.h>
Chris Masoneee929b72011-05-10 10:02:18 -07009
Chris Masone8fe2c7e2011-06-09 15:51:19 -070010#include <map>
Paul Stewart75897df2011-04-27 09:05:53 -070011#include <string>
Chris Masone8fe2c7e2011-06-09 15:51:19 -070012#include <vector>
Paul Stewart75897df2011-04-27 09:05:53 -070013
Chris Masoneee929b72011-05-10 10:02:18 -070014#include <base/logging.h>
Chris Masone3bd3c8c2011-06-13 08:20:26 -070015#include <base/memory/scoped_ptr.h>
mukesh agrawal51a7e932011-07-27 16:18:26 -070016#include <base/string_number_conversions.h>
Paul Stewart2bf424f2012-04-11 18:59:39 -070017#include <base/stringprintf.h>
Chris Masone3bd3c8c2011-06-13 08:20:26 -070018#include <chromeos/dbus/service_constants.h>
Chris Masoneee929b72011-05-10 10:02:18 -070019
Paul Stewartbe5f5b32011-12-07 17:11:11 -080020#include "shill/connection.h"
Paul Stewart75897df2011-04-27 09:05:53 -070021#include "shill/control_interface.h"
Chris Masone8fe2c7e2011-06-09 15:51:19 -070022#include "shill/error.h"
Paul Stewartbe5f5b32011-12-07 17:11:11 -080023#include "shill/http_proxy.h"
Chris Masone6791a432011-07-12 13:23:19 -070024#include "shill/manager.h"
Thieu Le48e6d6d2011-12-06 00:40:27 +000025#include "shill/metrics.h"
Chris Masone7aa5f902011-07-11 11:13:35 -070026#include "shill/profile.h"
Chris Masone3bd3c8c2011-06-13 08:20:26 -070027#include "shill/property_accessor.h"
Chris Masone7aa5f902011-07-11 11:13:35 -070028#include "shill/refptr_types.h"
Ben Chanfad4a0b2012-04-18 15:49:59 -070029#include "shill/scope_logger.h"
Chris Masoned7732e42011-05-20 11:08:56 -070030#include "shill/service_dbus_adaptor.h"
Darin Petkov5eb05422012-05-11 15:45:25 +020031#include "shill/sockets.h"
Darin Petkovba40dd32011-07-11 20:06:39 -070032#include "shill/store_interface.h"
Paul Stewart75897df2011-04-27 09:05:53 -070033
Chris Masone8fe2c7e2011-06-09 15:51:19 -070034using std::map;
Paul Stewart75897df2011-04-27 09:05:53 -070035using std::string;
Chris Masone8fe2c7e2011-06-09 15:51:19 -070036using std::vector;
Paul Stewart75897df2011-04-27 09:05:53 -070037
38namespace shill {
Darin Petkovba40dd32011-07-11 20:06:39 -070039
mukesh agrawalbf14e942012-03-02 14:36:34 -080040const char Service::kAutoConnConnected[] = "connected";
41const char Service::kAutoConnConnecting[] = "connecting";
42const char Service::kAutoConnExplicitDisconnect[] = "explicitly disconnected";
43const char Service::kAutoConnNotConnectable[] = "not connectable";
44
Darin Petkovba40dd32011-07-11 20:06:39 -070045const char Service::kCheckPortalAuto[] = "auto";
46const char Service::kCheckPortalFalse[] = "false";
47const char Service::kCheckPortalTrue[] = "true";
48
Paul Stewartac4ac002011-08-26 12:04:26 -070049const int Service::kPriorityNone = 0;
50
Darin Petkov2f903b32012-04-18 12:56:43 +020051const char Service::kServiceSortAutoConnect[] = "AutoConnect";
52const char Service::kServiceSortConnectable[] = "Connectable";
53const char Service::kServiceSortFavorite[] = "Favorite";
mukesh agrawalddc378f2012-02-17 18:26:20 -080054const char Service::kServiceSortIsConnected[] = "IsConnected";
55const char Service::kServiceSortIsConnecting[] = "IsConnecting";
56const char Service::kServiceSortIsFailed[] = "IsFailed";
Darin Petkov2f903b32012-04-18 12:56:43 +020057const char Service::kServiceSortPriority[] = "Priority";
mukesh agrawalddc378f2012-02-17 18:26:20 -080058const char Service::kServiceSortSecurityEtc[] = "SecurityEtc";
Darin Petkov2f903b32012-04-18 12:56:43 +020059const char Service::kServiceSortTechnology[] = "Technology";
mukesh agrawalddc378f2012-02-17 18:26:20 -080060const char Service::kServiceSortUniqueName[] = "UniqueName";
61
Darin Petkovba40dd32011-07-11 20:06:39 -070062const char Service::kStorageAutoConnect[] = "AutoConnect";
63const char Service::kStorageCheckPortal[] = "CheckPortal";
64const char Service::kStorageEapAnonymousIdentity[] = "EAP.AnonymousIdentity";
65const char Service::kStorageEapCACert[] = "EAP.CACert";
66const char Service::kStorageEapCACertID[] = "EAP.CACertID";
Paul Stewartecf4cd12012-04-17 11:08:39 -070067const char Service::kStorageEapCACertNSS[] = "EAP.CACertNSS";
Darin Petkovba40dd32011-07-11 20:06:39 -070068const char Service::kStorageEapCertID[] = "EAP.CertID";
69const char Service::kStorageEapClientCert[] = "EAP.ClientCert";
70const char Service::kStorageEapEap[] = "EAP.EAP";
71const char Service::kStorageEapIdentity[] = "EAP.Identity";
72const char Service::kStorageEapInnerEap[] = "EAP.InnerEAP";
73const char Service::kStorageEapKeyID[] = "EAP.KeyID";
74const char Service::kStorageEapKeyManagement[] = "EAP.KeyMgmt";
75const char Service::kStorageEapPIN[] = "EAP.PIN";
76const char Service::kStorageEapPassword[] = "EAP.Password";
77const char Service::kStorageEapPrivateKey[] = "EAP.PrivateKey";
78const char Service::kStorageEapPrivateKeyPassword[] = "EAP.PrivateKeyPassword";
79const char Service::kStorageEapUseSystemCAs[] = "EAP.UseSystemCAs";
Paul Stewart0756db92012-01-27 08:34:47 -080080const char Service::kStorageError[] = "Error";
Darin Petkovba40dd32011-07-11 20:06:39 -070081const char Service::kStorageFavorite[] = "Favorite";
Paul Stewart0756db92012-01-27 08:34:47 -080082const char Service::kStorageGUID[] = "GUID";
mukesh agrawalcf24a242012-05-21 16:46:11 -070083const char Service::kStorageHasEverConnected[] = "HasEverConnected";
Darin Petkovba40dd32011-07-11 20:06:39 -070084const char Service::kStorageName[] = "Name";
85const char Service::kStoragePriority[] = "Priority";
86const char Service::kStorageProxyConfig[] = "ProxyConfig";
87const char Service::kStorageSaveCredentials[] = "SaveCredentials";
Paul Stewart2706aaf2011-12-14 16:44:04 -080088const char Service::kStorageType[] = "Type";
Paul Stewart987e71e2011-12-05 09:45:06 -080089const char Service::kStorageUIData[] = "UIData";
Darin Petkovba40dd32011-07-11 20:06:39 -070090
mukesh agrawal8f3f7752012-02-17 19:42:09 -080091const uint8 Service::kStrengthMax = 100;
92const uint8 Service::kStrengthMin = 0;
93
mukesh agrawal51a7e932011-07-27 16:18:26 -070094// static
95unsigned int Service::serial_number_ = 0;
96
Paul Stewart75897df2011-04-27 09:05:53 -070097Service::Service(ControlInterface *control_interface,
mukesh agrawalb54601c2011-06-07 17:39:22 -070098 EventDispatcher *dispatcher,
Thieu Le3426c8f2012-01-11 17:35:11 -080099 Metrics *metrics,
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700100 Manager *manager,
Gaurav Shah435de2c2011-11-17 19:01:07 -0800101 Technology::Identifier technology)
Eric Shienbroodcc95c5d2012-03-30 15:25:49 -0400102 : state_(kStateIdle),
Paul Stewart03dba0b2011-08-22 16:32:45 -0700103 failure_(kFailureUnknown),
104 auto_connect_(false),
Darin Petkovba40dd32011-07-11 20:06:39 -0700105 check_portal_(kCheckPortalAuto),
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700106 connectable_(false),
mukesh agrawaladb68482012-01-17 16:31:51 -0800107 explicitly_disconnected_(false),
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700108 favorite_(false),
Darin Petkovba40dd32011-07-11 20:06:39 -0700109 priority_(kPriorityNone),
Paul Stewart1ca3e852011-11-04 07:50:49 -0700110 security_level_(0),
Paul Stewart22aa71b2011-09-16 12:15:11 -0700111 strength_(0),
Darin Petkovba40dd32011-07-11 20:06:39 -0700112 save_credentials_(true),
Gaurav Shah435de2c2011-11-17 19:01:07 -0800113 technology_(technology),
mukesh agrawal568b5c62012-02-28 14:44:47 -0800114 failed_time_(0),
mukesh agrawalcf24a242012-05-21 16:46:11 -0700115 has_ever_connected_(false),
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700116 dispatcher_(dispatcher),
mukesh agrawald835b202011-10-07 15:26:47 -0700117 unique_name_(base::UintToString(serial_number_++)),
118 friendly_name_(unique_name_),
Chris Masonea82b7112011-05-25 15:16:29 -0700119 available_(false),
120 configured_(false),
Chris Masonea82b7112011-05-25 15:16:29 -0700121 configuration_(NULL),
Chris Masone6791a432011-07-12 13:23:19 -0700122 adaptor_(control_interface->CreateServiceAdaptor(this)),
Thieu Le3426c8f2012-01-11 17:35:11 -0800123 metrics_(metrics),
Darin Petkov5eb05422012-05-11 15:45:25 +0200124 manager_(manager),
125 sockets_(new Sockets()) {
Thieu Le284fe792012-01-31 17:53:19 -0800126 HelpRegisterDerivedBool(flimflam::kAutoConnectProperty,
127 &Service::GetAutoConnect,
128 &Service::SetAutoConnect);
Chris Masone4d42df82011-07-02 17:09:39 -0700129
130 // flimflam::kActivationStateProperty: Registered in CellularService
131 // flimflam::kCellularApnProperty: Registered in CellularService
132 // flimflam::kCellularLastGoodApnProperty: Registered in CellularService
133 // flimflam::kNetworkTechnologyProperty: Registered in CellularService
Darin Petkov3335b372011-08-22 11:05:32 -0700134 // flimflam::kOperatorNameProperty: DEPRECATED
135 // flimflam::kOperatorCodeProperty: DEPRECATED
Chris Masone4d42df82011-07-02 17:09:39 -0700136 // flimflam::kRoamingStateProperty: Registered in CellularService
Darin Petkov3335b372011-08-22 11:05:32 -0700137 // flimflam::kServingOperatorProperty: Registered in CellularService
Chris Masone4d42df82011-07-02 17:09:39 -0700138 // flimflam::kPaymentURLProperty: Registered in CellularService
139
Paul Stewartd215af62012-04-24 23:25:50 -0700140 HelpRegisterDerivedString(flimflam::kCheckPortalProperty,
141 &Service::GetCheckPortal,
142 &Service::SetCheckPortal);
Chris Masone27c4aa52011-07-02 13:10:14 -0700143 store_.RegisterConstBool(flimflam::kConnectableProperty, &connectable_);
Jason Glasgowacdc11f2012-03-30 14:12:22 -0400144 HelpRegisterDerivedRpcIdentifier(flimflam::kDeviceProperty,
145 &Service::GetDeviceRpcId,
146 NULL);
Paul Stewart4c561612012-03-21 12:49:01 -0700147 store_.RegisterString(flimflam::kGuidProperty, &guid_);
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700148
Chris Masoneb2e326b2011-07-12 13:28:51 -0700149 store_.RegisterString(flimflam::kEapIdentityProperty, &eap_.identity);
150 store_.RegisterString(flimflam::kEAPEAPProperty, &eap_.eap);
151 store_.RegisterString(flimflam::kEapPhase2AuthProperty, &eap_.inner_eap);
Chris Masone27c4aa52011-07-02 13:10:14 -0700152 store_.RegisterString(flimflam::kEapAnonymousIdentityProperty,
Chris Masoneb2e326b2011-07-12 13:28:51 -0700153 &eap_.anonymous_identity);
154 store_.RegisterString(flimflam::kEAPClientCertProperty, &eap_.client_cert);
155 store_.RegisterString(flimflam::kEAPCertIDProperty, &eap_.cert_id);
156 store_.RegisterString(flimflam::kEapPrivateKeyProperty, &eap_.private_key);
mukesh agrawal292dc0f2012-01-26 18:02:46 -0800157 HelpRegisterWriteOnlyDerivedString(flimflam::kEapPrivateKeyPasswordProperty,
158 &Service::SetEAPPrivateKeyPassword,
159 NULL,
160 &eap_.private_key_password);
Chris Masoneb2e326b2011-07-12 13:28:51 -0700161 store_.RegisterString(flimflam::kEAPKeyIDProperty, &eap_.key_id);
162 store_.RegisterString(flimflam::kEapCaCertProperty, &eap_.ca_cert);
163 store_.RegisterString(flimflam::kEapCaCertIDProperty, &eap_.ca_cert_id);
Paul Stewartecf4cd12012-04-17 11:08:39 -0700164 store_.RegisterString(flimflam::kEapCaCertNssProperty, &eap_.ca_cert_nss);
Chris Masoneb2e326b2011-07-12 13:28:51 -0700165 store_.RegisterString(flimflam::kEAPPINProperty, &eap_.pin);
mukesh agrawal292dc0f2012-01-26 18:02:46 -0800166 HelpRegisterWriteOnlyDerivedString(flimflam::kEapPasswordProperty,
167 &Service::SetEAPPassword,
168 NULL,
169 &eap_.password);
Chris Masoneb2e326b2011-07-12 13:28:51 -0700170 store_.RegisterString(flimflam::kEapKeyMgmtProperty, &eap_.key_management);
171 store_.RegisterBool(flimflam::kEapUseSystemCAsProperty, &eap_.use_system_cas);
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700172
Eric Shienbroodcc95c5d2012-03-30 15:25:49 -0400173 // TODO(ers): in flimflam clearing Error has the side-effect of
174 // setting the service state to IDLE. Is this important? I could
175 // see an autotest depending on it.
Chris Masone27c4aa52011-07-02 13:10:14 -0700176 store_.RegisterConstString(flimflam::kErrorProperty, &error_);
177 store_.RegisterConstBool(flimflam::kFavoriteProperty, &favorite_);
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800178 HelpRegisterDerivedUint16(shill::kHTTPProxyPortProperty,
179 &Service::GetHTTPProxyPort,
180 NULL);
Paul Stewart10241e32012-04-23 18:15:06 -0700181 HelpRegisterDerivedRpcIdentifier(shill::kIPConfigProperty,
182 &Service::GetIPConfigRpcIdentifier,
183 NULL);
Chris Masone27c4aa52011-07-02 13:10:14 -0700184 HelpRegisterDerivedBool(flimflam::kIsActiveProperty,
Chris Masoneb2e326b2011-07-12 13:28:51 -0700185 &Service::IsActive,
186 NULL);
Chris Masone4d42df82011-07-02 17:09:39 -0700187 // flimflam::kModeProperty: Registered in WiFiService
Paul Stewart0c438332012-04-11 07:55:27 -0700188
189 // Although this is a read-only property, some callers want to blindly
190 // set this value to its current value.
191 HelpRegisterDerivedString(flimflam::kNameProperty,
192 &Service::GetNameProperty,
193 &Service::AssertTrivialSetNameProperty);
Chris Masone4d42df82011-07-02 17:09:39 -0700194 // flimflam::kPassphraseProperty: Registered in WiFiService
195 // flimflam::kPassphraseRequiredProperty: Registered in WiFiService
Chris Masone27c4aa52011-07-02 13:10:14 -0700196 store_.RegisterInt32(flimflam::kPriorityProperty, &priority_);
197 HelpRegisterDerivedString(flimflam::kProfileProperty,
198 &Service::GetProfileRpcId,
Paul Stewart1b1a7f22012-01-06 16:24:06 -0800199 &Service::SetProfileRpcId);
Chris Masone4d42df82011-07-02 17:09:39 -0700200 store_.RegisterString(flimflam::kProxyConfigProperty, &proxy_config_);
Chris Masoneb2e326b2011-07-12 13:28:51 -0700201 store_.RegisterBool(flimflam::kSaveCredentialsProperty, &save_credentials_);
Gaurav Shah435de2c2011-11-17 19:01:07 -0800202 HelpRegisterDerivedString(flimflam::kTypeProperty,
203 &Service::GetTechnologyString,
204 NULL);
Chris Masone4d42df82011-07-02 17:09:39 -0700205 // flimflam::kSecurityProperty: Registered in WiFiService
Chris Masone27c4aa52011-07-02 13:10:14 -0700206 HelpRegisterDerivedString(flimflam::kStateProperty,
207 &Service::CalculateState,
208 NULL);
Darin Petkovd78ee7e2012-01-12 11:21:10 +0100209 store_.RegisterConstUint8(flimflam::kSignalStrengthProperty, &strength_);
Paul Stewart987e71e2011-12-05 09:45:06 -0800210 store_.RegisterString(flimflam::kUIDataProperty, &ui_data_);
Chris Masone4d42df82011-07-02 17:09:39 -0700211 // flimflam::kWifiAuthMode: Registered in WiFiService
212 // flimflam::kWifiHiddenSsid: Registered in WiFiService
213 // flimflam::kWifiFrequency: Registered in WiFiService
214 // flimflam::kWifiPhyMode: Registered in WiFiService
215 // flimflam::kWifiHexSsid: Registered in WiFiService
Thieu Le48e6d6d2011-12-06 00:40:27 +0000216
217 metrics_->RegisterService(this);
218
Paul Stewart1062d9d2012-04-27 10:42:27 -0700219 static_ip_parameters_.PlumbPropertyStore(&store_);
220
Paul Stewartcb59fed2012-03-21 21:14:46 -0700221 IgnoreParameterForConfigure(flimflam::kTypeProperty);
Paul Stewart7f61e522012-03-22 11:13:45 -0700222 IgnoreParameterForConfigure(flimflam::kProfileProperty);
Paul Stewartcb59fed2012-03-21 21:14:46 -0700223
Ben Chanfad4a0b2012-04-18 15:49:59 -0700224 SLOG(Service, 2) << "Service initialized.";
Paul Stewart75897df2011-04-27 09:05:53 -0700225}
226
Thieu Le48e6d6d2011-12-06 00:40:27 +0000227Service::~Service() {
228 metrics_->DeregisterService(this);
229}
Paul Stewart75897df2011-04-27 09:05:53 -0700230
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000231void Service::AutoConnect() {
mukesh agrawalbf14e942012-03-02 14:36:34 -0800232 const char *reason;
233 if (this->IsAutoConnectable(&reason)) {
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000234 Error error;
235 Connect(&error);
mukesh agrawal76d13882012-01-12 15:23:11 -0800236 } else {
mukesh agrawalbf14e942012-03-02 14:36:34 -0800237 LOG(INFO) << "Suppressed autoconnect to " << friendly_name() << " "
238 << "(" << reason << ")";
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000239 }
240}
241
mukesh agrawaladb68482012-01-17 16:31:51 -0800242void Service::Connect(Error */*error*/) {
243 explicitly_disconnected_ = false;
Eric Shienbroodcc95c5d2012-03-30 15:25:49 -0400244 // clear any failure state from a previous connect attempt
245 SetState(kStateIdle);
mukesh agrawaladb68482012-01-17 16:31:51 -0800246}
247
248void Service::Disconnect(Error */*error*/) {
249 explicitly_disconnected_ = true;
250}
251
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500252void Service::ActivateCellularModem(const string &/*carrier*/,
Eric Shienbrood9a245532012-03-07 14:20:39 -0500253 Error *error,
254 const ResultCallback &/*callback*/) {
255 Error::PopulateAndLog(error, Error::kNotSupported,
256 "Service doesn't support cellular modem activation.");
Darin Petkovc408e692011-08-17 13:47:15 -0700257}
258
mukesh agrawal1830fa12011-09-26 14:31:40 -0700259bool Service::TechnologyIs(const Technology::Identifier /*type*/) const {
Paul Stewart22aa71b2011-09-16 12:15:11 -0700260 return false;
261}
262
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800263bool Service::IsActive(Error */*error*/) {
Paul Stewart03dba0b2011-08-22 16:32:45 -0700264 return state_ != kStateUnknown &&
265 state_ != kStateIdle &&
266 state_ != kStateFailure;
267}
268
269void Service::SetState(ConnectState state) {
Gaurav Shahc6d6c722011-11-17 18:59:39 -0800270 LOG(INFO) << "In " << __func__ << "(): Service " << friendly_name_
271 << " state " << ConnectStateToString(state_) << " -> "
272 << ConnectStateToString(state);
mukesh agrawal7ec71312011-11-10 02:08:26 +0000273
Paul Stewart03dba0b2011-08-22 16:32:45 -0700274 if (state == state_) {
275 return;
276 }
277
278 state_ = state;
279 if (state != kStateFailure) {
280 failure_ = kFailureUnknown;
mukesh agrawal568b5c62012-02-28 14:44:47 -0800281 failed_time_ = 0;
Paul Stewart03dba0b2011-08-22 16:32:45 -0700282 }
mukesh agrawalcf24a242012-05-21 16:46:11 -0700283 if (state == kStateConnected) {
284 has_ever_connected_ = true;
285 SaveToProfile();
286 }
Paul Stewart03dba0b2011-08-22 16:32:45 -0700287 manager_->UpdateService(this);
Thieu Le48e6d6d2011-12-06 00:40:27 +0000288 metrics_->NotifyServiceStateChanged(this, state);
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800289 Error error;
290 adaptor_->EmitStringChanged(flimflam::kStateProperty, CalculateState(&error));
Paul Stewart03dba0b2011-08-22 16:32:45 -0700291}
292
293void Service::SetFailure(ConnectFailure failure) {
294 failure_ = failure;
mukesh agrawal568b5c62012-02-28 14:44:47 -0800295 failed_time_ = time(NULL);
Paul Stewart03dba0b2011-08-22 16:32:45 -0700296 SetState(kStateFailure);
297}
298
mukesh agrawal568b5c62012-02-28 14:44:47 -0800299void Service::SetFailureSilent(ConnectFailure failure) {
300 // Note that order matters here, since SetState modifies |failure_| and
301 // |failed_time_|.
302 SetState(kStateIdle);
303 failure_ = failure;
304 failed_time_ = time(NULL);
305}
306
Chris Masone6791a432011-07-12 13:23:19 -0700307string Service::GetRpcIdentifier() const {
Chris Masone3c3f6a12011-07-01 10:01:41 -0700308 return adaptor_->GetRpcIdentifier();
309}
310
Paul Stewartbba6a5b2011-11-02 18:45:59 -0700311bool Service::IsLoadableFrom(StoreInterface *storage) const {
312 return storage->ContainsGroup(GetStorageIdentifier());
313}
314
Chris Masone9d779932011-08-25 16:33:41 -0700315bool Service::Load(StoreInterface *storage) {
316 const string id = GetStorageIdentifier();
Chris Masone34af2182011-08-22 11:59:36 -0700317 if (!storage->ContainsGroup(id)) {
318 LOG(WARNING) << "Service is not available in the persistent store: " << id;
319 return false;
320 }
321 storage->GetBool(id, kStorageAutoConnect, &auto_connect_);
322 storage->GetString(id, kStorageCheckPortal, &check_portal_);
323 storage->GetBool(id, kStorageFavorite, &favorite_);
mukesh agrawalcf24a242012-05-21 16:46:11 -0700324 storage->GetString(id, kStorageGUID, &guid_);
325 storage->GetBool(id, kStorageHasEverConnected, &has_ever_connected_);
Chris Masone34af2182011-08-22 11:59:36 -0700326 storage->GetInt(id, kStoragePriority, &priority_);
327 storage->GetString(id, kStorageProxyConfig, &proxy_config_);
328 storage->GetBool(id, kStorageSaveCredentials, &save_credentials_);
Paul Stewart987e71e2011-12-05 09:45:06 -0800329 storage->GetString(id, kStorageUIData, &ui_data_);
Chris Masone34af2182011-08-22 11:59:36 -0700330
331 LoadEapCredentials(storage, id);
Paul Stewart1062d9d2012-04-27 10:42:27 -0700332 static_ip_parameters_.Load(storage, id);
Chris Masone34af2182011-08-22 11:59:36 -0700333 // TODO(petkov): Load these:
334
Chris Masone34af2182011-08-22 11:59:36 -0700335 // "Failure"
336 // "Modified"
337 // "LastAttempt"
Chris Masone34af2182011-08-22 11:59:36 -0700338
mukesh agrawaladb68482012-01-17 16:31:51 -0800339 explicitly_disconnected_ = false;
Paul Stewarta41e38d2011-11-11 07:47:29 -0800340 favorite_ = true;
341
Chris Masone34af2182011-08-22 11:59:36 -0700342 return true;
Darin Petkovba40dd32011-07-11 20:06:39 -0700343}
344
Paul Stewart65512e12012-03-26 18:01:08 -0700345bool Service::Unload() {
Paul Stewarta41e38d2011-11-11 07:47:29 -0800346 auto_connect_ = false;
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800347 check_portal_ = kCheckPortalAuto;
Paul Stewarta41e38d2011-11-11 07:47:29 -0800348 favorite_ = false;
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800349 priority_ = kPriorityNone;
350 proxy_config_ = "";
351 save_credentials_ = true;
352 ui_data_ = "";
353
354 UnloadEapCredentials();
Wade Guthrie9e0c2502012-04-19 15:26:40 -0700355 Error error; // Ignored.
356 Disconnect(&error);
Paul Stewart65512e12012-03-26 18:01:08 -0700357 return false;
Paul Stewarta41e38d2011-11-11 07:47:29 -0800358}
359
Chris Masone9d779932011-08-25 16:33:41 -0700360bool Service::Save(StoreInterface *storage) {
361 const string id = GetStorageIdentifier();
Chris Masone34af2182011-08-22 11:59:36 -0700362
Paul Stewart2706aaf2011-12-14 16:44:04 -0800363 storage->SetString(id, kStorageType, GetTechnologyString(NULL));
364
Chris Masone34af2182011-08-22 11:59:36 -0700365 // TODO(petkov): We could choose to simplify the saving code by removing most
366 // conditionals thus saving even default values.
Wade Guthrie4f28e8b2012-04-11 10:52:07 -0700367 storage->SetBool(id, kStorageAutoConnect, auto_connect_);
Chris Masone34af2182011-08-22 11:59:36 -0700368 if (check_portal_ == kCheckPortalAuto) {
369 storage->DeleteKey(id, kStorageCheckPortal);
370 } else {
371 storage->SetString(id, kStorageCheckPortal, check_portal_);
372 }
373 storage->SetBool(id, kStorageFavorite, favorite_);
mukesh agrawalcf24a242012-05-21 16:46:11 -0700374 SaveString(storage, id, kStorageGUID, guid_, false, true);
375 storage->SetBool(id, kStorageHasEverConnected, has_ever_connected_);
mukesh agrawald835b202011-10-07 15:26:47 -0700376 storage->SetString(id, kStorageName, friendly_name_);
Chris Masone34af2182011-08-22 11:59:36 -0700377 if (priority_ != kPriorityNone) {
378 storage->SetInt(id, kStoragePriority, priority_);
379 } else {
380 storage->DeleteKey(id, kStoragePriority);
381 }
Paul Stewart987e71e2011-12-05 09:45:06 -0800382 SaveString(storage, id, kStorageProxyConfig, proxy_config_, false, true);
Chris Masone34af2182011-08-22 11:59:36 -0700383 if (save_credentials_) {
384 storage->DeleteKey(id, kStorageSaveCredentials);
385 } else {
386 storage->SetBool(id, kStorageSaveCredentials, false);
387 }
Paul Stewart987e71e2011-12-05 09:45:06 -0800388 SaveString(storage, id, kStorageUIData, ui_data_, false, true);
Chris Masone34af2182011-08-22 11:59:36 -0700389
390 SaveEapCredentials(storage, id);
Paul Stewart1062d9d2012-04-27 10:42:27 -0700391 static_ip_parameters_.Save(storage, id);
Chris Masone34af2182011-08-22 11:59:36 -0700392
393 // TODO(petkov): Save these:
394
Chris Masone34af2182011-08-22 11:59:36 -0700395 // "Failure"
396 // "Modified"
397 // "LastAttempt"
Chris Masone34af2182011-08-22 11:59:36 -0700398
399 return true;
400}
401
Gary Moraind93615e2012-04-27 11:50:03 -0700402void Service::SaveToCurrentProfile() {
403 // Some unittests do not specify a manager.
404 if (manager()) {
405 manager()->SaveServiceToProfile(this);
406 }
Thieu Led4e9e552012-02-16 16:26:07 -0800407}
408
Paul Stewartcb59fed2012-03-21 21:14:46 -0700409void Service::Configure(const KeyValueStore &args, Error *error) {
410 map<string, bool>::const_iterator bool_it;
Ben Chanfad4a0b2012-04-18 15:49:59 -0700411 SLOG(Service, 5) << "Configuring bool properties:";
Paul Stewartcb59fed2012-03-21 21:14:46 -0700412 for (bool_it = args.bool_properties().begin();
413 bool_it != args.bool_properties().end();
414 ++bool_it) {
415 if (ContainsKey(parameters_ignored_for_configure_, bool_it->first)) {
416 continue;
417 }
Ben Chanfad4a0b2012-04-18 15:49:59 -0700418 SLOG(Service, 5) << " " << bool_it->first;
Paul Stewartcb59fed2012-03-21 21:14:46 -0700419 Error set_error;
420 store_.SetBoolProperty(bool_it->first, bool_it->second, &set_error);
421 if (error->IsSuccess() && set_error.IsFailure()) {
422 error->CopyFrom(set_error);
423 }
424 }
Ben Chanfad4a0b2012-04-18 15:49:59 -0700425 SLOG(Service, 5) << "Configuring string properties:";
Paul Stewartcb59fed2012-03-21 21:14:46 -0700426 map<string, string>::const_iterator string_it;
427 for (string_it = args.string_properties().begin();
428 string_it != args.string_properties().end();
429 ++string_it) {
430 if (ContainsKey(parameters_ignored_for_configure_, string_it->first)) {
431 continue;
432 }
Ben Chanfad4a0b2012-04-18 15:49:59 -0700433 SLOG(Service, 5) << " " << string_it->first;
Paul Stewartcb59fed2012-03-21 21:14:46 -0700434 Error set_error;
435 store_.SetStringProperty(string_it->first, string_it->second, &set_error);
436 if (error->IsSuccess() && set_error.IsFailure()) {
437 error->CopyFrom(set_error);
438 }
439 }
Ben Chanfad4a0b2012-04-18 15:49:59 -0700440 SLOG(Service, 5) << "Configuring uint32 properties:";
Paul Stewartcb59fed2012-03-21 21:14:46 -0700441 map<string, uint32>::const_iterator int_it;
442 for (int_it = args.uint_properties().begin();
443 int_it != args.uint_properties().end();
444 ++int_it) {
445 if (ContainsKey(parameters_ignored_for_configure_, int_it->first)) {
446 continue;
447 }
Ben Chanfad4a0b2012-04-18 15:49:59 -0700448 SLOG(Service, 5) << " " << int_it->first;
Paul Stewartcb59fed2012-03-21 21:14:46 -0700449 Error set_error;
450 store_.SetUint32Property(int_it->first, int_it->second, &set_error);
451 if (error->IsSuccess() && set_error.IsFailure()) {
452 error->CopyFrom(set_error);
453 }
454 }
455}
456
Paul Stewart10ccbb32012-04-26 15:59:30 -0700457bool Service::IsRemembered() const {
458 return profile_ && !manager_->IsServiceEphemeral(this);
459}
460
mukesh agrawal00917ce2011-11-22 23:56:55 +0000461void Service::MakeFavorite() {
462 if (favorite_) {
463 // We do not want to clobber the value of auto_connect_ (it may
464 // be user-set). So return early.
465 return;
466 }
467
468 auto_connect_ = true;
469 favorite_ = true;
470}
471
Darin Petkov5eb05422012-05-11 15:45:25 +0200472void Service::SetConnection(const ConnectionRefPtr &connection) {
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800473 if (connection.get()) {
Paul Stewartc8f4bef2011-12-13 09:45:51 -0800474 http_proxy_.reset(new HTTPProxy(connection));
Darin Petkov5eb05422012-05-11 15:45:25 +0200475 http_proxy_->Start(dispatcher_, sockets_.get());
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800476 } else {
477 http_proxy_.reset();
478 }
479 connection_ = connection;
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800480}
481
Gaurav Shah10109f22011-11-11 20:16:22 -0800482bool Service::Is8021xConnectable() const {
483 // We mirror all the flimflam checks (see service.c:is_connectable()).
484
485 // Identity is required.
486 if (eap_.identity.empty()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700487 SLOG(Service, 2) << "Not connectable: Identity is empty.";
Gaurav Shah10109f22011-11-11 20:16:22 -0800488 return false;
489 }
490
491 if (!eap_.client_cert.empty() || !eap_.cert_id.empty()) {
492 // If a client certificate is being used, we must have a private key.
493 if (eap_.private_key.empty() && eap_.key_id.empty()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700494 SLOG(Service, 2)
495 << "Not connectable. Client certificate but no private key.";
Gaurav Shah10109f22011-11-11 20:16:22 -0800496 return false;
497 }
498 }
499 if (!eap_.cert_id.empty() || !eap_.key_id.empty() ||
500 !eap_.ca_cert_id.empty()) {
501 // If PKCS#11 data is needed, a PIN is required.
502 if (eap_.pin.empty()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700503 SLOG(Service, 2) << "Not connectable. PKCS#11 data but no PIN.";
Gaurav Shah10109f22011-11-11 20:16:22 -0800504 return false;
505 }
506 }
507
508 // For EAP-TLS, a client certificate is required.
509 if (eap_.eap.empty() || eap_.eap == "TLS") {
Paul Stewart81426132012-05-16 10:05:10 -0700510 if ((!eap_.client_cert.empty() || !eap_.cert_id.empty()) &&
511 (!eap_.private_key.empty() || !eap_.key_id.empty())) {
512 SLOG(Service, 2) << "Connectable. EAP-TLS with a client cert and key.";
Gaurav Shah10109f22011-11-11 20:16:22 -0800513 return true;
514 }
515 }
516
517 // For EAP types other than TLS (e.g. EAP-TTLS or EAP-PEAP, password is the
518 // minimum requirement), at least an identity + password is required.
519 if (eap_.eap.empty() || eap_.eap != "TLS") {
520 if (!eap_.password.empty()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700521 SLOG(Service, 2) << "Connectable. !EAP-TLS and has a password.";
Gaurav Shah10109f22011-11-11 20:16:22 -0800522 return true;
523 }
524 }
525
Ben Chanfad4a0b2012-04-18 15:49:59 -0700526 SLOG(Service, 2)
527 << "Not connectable. No suitable EAP configuration was found.";
Gaurav Shah10109f22011-11-11 20:16:22 -0800528 return false;
529}
530
531void Service::set_eap(const EapCredentials &eap) {
532 eap_ = eap;
533 // Note: Connectability can only be updated by a subclass of Service
534 // with knowledge of whether the service actually uses 802.1x credentials.
535}
536
mukesh agrawal00917ce2011-11-22 23:56:55 +0000537// static
Gaurav Shahc6d6c722011-11-17 18:59:39 -0800538const char *Service::ConnectFailureToString(const ConnectFailure &state) {
539 switch (state) {
540 case kFailureUnknown:
541 return "Unknown";
542 case kFailureActivationFailure:
543 return "Activation Failure";
544 case kFailureOutOfRange:
545 return "Out of range";
546 case kFailurePinMissing:
547 return "PIN missing";
548 case kFailureConfigurationFailed:
549 return "Configuration Failed";
550 case kFailureBadCredentials:
551 return "Bad Credentials";
552 case kFailureNeedEVDO:
553 return "Need EVDO";
554 case kFailureNeedHomeNetwork:
555 return "Need Home Network";
556 case kFailureOTASPFailure:
557 return "OTASP Failure";
558 case kFailureAAAFailure:
559 return "AAA Failure";
Thieu Le48e6d6d2011-12-06 00:40:27 +0000560 case kFailureMax:
561 return "Max failure error code";
Gaurav Shahc6d6c722011-11-17 18:59:39 -0800562 }
563 return "Invalid";
564}
565
566// static
567const char *Service::ConnectStateToString(const ConnectState &state) {
568 switch (state) {
569 case kStateUnknown:
570 return "Unknown";
571 case kStateIdle:
572 return "Idle";
573 case kStateAssociating:
574 return "Associating";
575 case kStateConfiguring:
576 return "Configuring";
577 case kStateConnected:
578 return "Connected";
579 case kStateDisconnected:
580 return "Disconnected";
Thieu Le48e6d6d2011-12-06 00:40:27 +0000581 case kStatePortal:
582 return "Portal";
Gaurav Shahc6d6c722011-11-17 18:59:39 -0800583 case kStateFailure:
584 return "Failure";
585 case kStateOnline:
586 return "Online";
587 }
588 return "Invalid";
589}
590
Gaurav Shah435de2c2011-11-17 19:01:07 -0800591string Service::GetTechnologyString(Error */*error*/) {
592 return Technology::NameFromIdentifier(technology());
593}
594
Jason Glasgowb5790052012-01-27 01:03:52 -0500595// static
Paul Stewart22aa71b2011-09-16 12:15:11 -0700596bool Service::DecideBetween(int a, int b, bool *decision) {
597 if (a == b)
598 return false;
599 *decision = (a > b);
600 return true;
601}
602
mukesh agrawal00917ce2011-11-22 23:56:55 +0000603// static
Paul Stewart22aa71b2011-09-16 12:15:11 -0700604bool Service::Compare(ServiceRefPtr a,
605 ServiceRefPtr b,
mukesh agrawalddc378f2012-02-17 18:26:20 -0800606 const vector<Technology::Identifier> &tech_order,
607 const char **reason) {
Paul Stewart22aa71b2011-09-16 12:15:11 -0700608 bool ret;
609
610 if (a->state() != b->state()) {
611 if (DecideBetween(a->IsConnected(), b->IsConnected(), &ret)) {
mukesh agrawalddc378f2012-02-17 18:26:20 -0800612 *reason = kServiceSortIsConnected;
Paul Stewart22aa71b2011-09-16 12:15:11 -0700613 return ret;
614 }
615
616 // TODO(pstew): Services don't know about portal state yet
617
618 if (DecideBetween(a->IsConnecting(), b->IsConnecting(), &ret)) {
mukesh agrawalddc378f2012-02-17 18:26:20 -0800619 *reason = kServiceSortIsConnecting;
Paul Stewart22aa71b2011-09-16 12:15:11 -0700620 return ret;
621 }
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000622
623 if (DecideBetween(!a->IsFailed(), !b->IsFailed(), &ret)) {
mukesh agrawalddc378f2012-02-17 18:26:20 -0800624 *reason = kServiceSortIsFailed;
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000625 return ret;
626 }
Paul Stewart22aa71b2011-09-16 12:15:11 -0700627 }
628
Darin Petkov2f903b32012-04-18 12:56:43 +0200629 if (DecideBetween(a->connectable(), b->connectable(), &ret)) {
630 *reason = kServiceSortConnectable;
631 return ret;
632 }
633
634 // Ignore the auto-connect property if both services are connected
635 // already. This allows connected non-autoconnectable VPN services to be
636 // sorted higher than other connected services based on technology order.
637 if (!a->IsConnected() &&
638 DecideBetween(a->auto_connect(), b->auto_connect(), &ret)) {
639 *reason = kServiceSortAutoConnect;
640 return ret;
641 }
642
643 if (DecideBetween(a->favorite(), b->favorite(), &ret)) {
644 *reason = kServiceSortFavorite;
645 return ret;
646 }
647
648 if (DecideBetween(a->priority(), b->priority(), &ret)) {
649 *reason = kServiceSortPriority;
Paul Stewart22aa71b2011-09-16 12:15:11 -0700650 return ret;
651 }
652
653 // TODO(pstew): Below this point we are making value judgements on
654 // services that are not related to anything intrinsic or
655 // user-specified. These heuristics should be richer (contain
656 // historical information, for example) and be subject to user
657 // customization.
Paul Stewart22aa71b2011-09-16 12:15:11 -0700658 for (vector<Technology::Identifier>::const_iterator it = tech_order.begin();
659 it != tech_order.end();
660 ++it) {
mukesh agrawalddc378f2012-02-17 18:26:20 -0800661 if (DecideBetween(a->TechnologyIs(*it), b->TechnologyIs(*it), &ret)) {
662 *reason = kServiceSortTechnology;
Paul Stewart22aa71b2011-09-16 12:15:11 -0700663 return ret;
mukesh agrawalddc378f2012-02-17 18:26:20 -0800664 }
Paul Stewart22aa71b2011-09-16 12:15:11 -0700665 }
666
Paul Stewart1ca3e852011-11-04 07:50:49 -0700667 if (DecideBetween(a->security_level(), b->security_level(), &ret) ||
Paul Stewart22aa71b2011-09-16 12:15:11 -0700668 DecideBetween(a->strength(), b->strength(), &ret)) {
mukesh agrawalddc378f2012-02-17 18:26:20 -0800669 *reason = kServiceSortSecurityEtc;
Paul Stewart22aa71b2011-09-16 12:15:11 -0700670 return ret;
671 }
672
mukesh agrawalddc378f2012-02-17 18:26:20 -0800673 *reason = kServiceSortUniqueName;
Paul Stewart22aa71b2011-09-16 12:15:11 -0700674 return a->UniqueName() < b->UniqueName();
675}
676
Chris Masone34af2182011-08-22 11:59:36 -0700677const ProfileRefPtr &Service::profile() const { return profile_; }
678
679void Service::set_profile(const ProfileRefPtr &p) { profile_ = p; }
680
Paul Stewartff14b022012-04-24 20:06:23 -0700681void Service::OnPropertyChanged(const string &property) {
Paul Stewart81426132012-05-16 10:05:10 -0700682 if (Is8021x() &&
683 (property == flimflam::kEAPCertIDProperty ||
684 property == flimflam::kEAPClientCertProperty ||
685 property == flimflam::kEAPKeyIDProperty ||
686 property == flimflam::kEAPPINProperty ||
687 property == flimflam::kEapCaCertIDProperty ||
688 property == flimflam::kEapIdentityProperty ||
689 property == flimflam::kEapPasswordProperty ||
690 property == flimflam::kEapPrivateKeyProperty)) {
691 // This notifies subclassess that EAP parameters have been changed.
692 set_eap(eap_);
693 }
mukesh agrawalcf24a242012-05-21 16:46:11 -0700694 SaveToProfile();
Paul Stewartd215af62012-04-24 23:25:50 -0700695 if ((property == flimflam::kCheckPortalProperty ||
696 property == flimflam::kProxyConfigProperty) &&
697 (state_ == kStateConnected ||
698 state_ == kStatePortal ||
699 state_ == kStateOnline)) {
700 manager_->RecheckPortalOnService(this);
701 }
Paul Stewartff14b022012-04-24 20:06:23 -0700702}
703
Paul Stewart10241e32012-04-23 18:15:06 -0700704string Service::GetIPConfigRpcIdentifier(Error *error) {
705 if (!connection_) {
706 error->Populate(Error::kNotFound);
707 return "/";
708 }
709
710 string id = connection_->ipconfig_rpc_identifier();
711
712 if (id.empty()) {
713 // Do not return an empty IPConfig.
714 error->Populate(Error::kNotFound);
715 return "/";
716 }
717
718 return id;
719}
720
mukesh agrawal29c13a12011-11-24 00:09:19 +0000721void Service::set_connectable(bool connectable) {
722 connectable_ = connectable;
723 adaptor_->EmitBoolChanged(flimflam::kConnectableProperty, connectable_);
724}
725
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800726string Service::CalculateState(Error */*error*/) {
mukesh agrawalf2f68a52011-09-01 12:15:48 -0700727 switch (state_) {
Gaurav Shahc6d6c722011-11-17 18:59:39 -0800728 case kStateIdle:
729 return flimflam::kStateIdle;
730 case kStateAssociating:
731 return flimflam::kStateAssociation;
732 case kStateConfiguring:
733 return flimflam::kStateConfiguration;
mukesh agrawalf2f68a52011-09-01 12:15:48 -0700734 case kStateConnected:
Paul Stewart20088d82012-02-16 06:58:55 -0800735 return flimflam::kStateReady;
Gaurav Shahc6d6c722011-11-17 18:59:39 -0800736 case kStateDisconnected:
737 return flimflam::kStateDisconnect;
738 case kStateFailure:
739 return flimflam::kStateFailure;
Paul Stewart20088d82012-02-16 06:58:55 -0800740 case kStatePortal:
741 return flimflam::kStatePortal;
Gaurav Shahc6d6c722011-11-17 18:59:39 -0800742 case kStateOnline:
743 return flimflam::kStateOnline;
744 case kStateUnknown:
mukesh agrawalf2f68a52011-09-01 12:15:48 -0700745 default:
Gaurav Shahc6d6c722011-11-17 18:59:39 -0800746 return "";
mukesh agrawalf2f68a52011-09-01 12:15:48 -0700747 }
748}
749
mukesh agrawalbf14e942012-03-02 14:36:34 -0800750bool Service::IsAutoConnectable(const char **reason) const {
751 if (!connectable()) {
752 *reason = kAutoConnNotConnectable;
753 return false;
754 }
755
756 if (IsConnected()) {
757 *reason = kAutoConnConnected;
758 return false;
759 }
760
761 if (IsConnecting()) {
762 *reason = kAutoConnConnecting;
763 return false;
764 }
765
766 if (explicitly_disconnected_) {
767 *reason = kAutoConnExplicitDisconnect;
768 return false;
769 }
770
771 return true;
mukesh agrawal76d13882012-01-12 15:23:11 -0800772}
773
Paul Stewartd215af62012-04-24 23:25:50 -0700774bool Service::IsPortalDetectionDisabled() const {
775 return check_portal_ == kCheckPortalFalse;
776}
777
778bool Service::IsPortalDetectionAuto() const {
779 return check_portal_ == kCheckPortalAuto;
780}
781
mukesh agrawalffa3d042011-10-06 15:26:10 -0700782void Service::HelpRegisterDerivedBool(
783 const string &name,
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800784 bool(Service::*get)(Error *),
mukesh agrawalffa3d042011-10-06 15:26:10 -0700785 void(Service::*set)(const bool&, Error *)) {
Chris Masone27c4aa52011-07-02 13:10:14 -0700786 store_.RegisterDerivedBool(
787 name,
788 BoolAccessor(new CustomAccessor<Service, bool>(this, get, set)));
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700789}
790
mukesh agrawalffa3d042011-10-06 15:26:10 -0700791void Service::HelpRegisterDerivedString(
792 const string &name,
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800793 string(Service::*get)(Error *),
mukesh agrawalffa3d042011-10-06 15:26:10 -0700794 void(Service::*set)(const string&, Error *)) {
Chris Masone27c4aa52011-07-02 13:10:14 -0700795 store_.RegisterDerivedString(
796 name,
797 StringAccessor(new CustomAccessor<Service, string>(this, get, set)));
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700798}
799
Jason Glasgowacdc11f2012-03-30 14:12:22 -0400800void Service::HelpRegisterDerivedRpcIdentifier(
801 const string &name,
802 RpcIdentifier(Service::*get)(Error *),
803 void(Service::*set)(const RpcIdentifier&, Error *)) {
804 store_.RegisterDerivedRpcIdentifier(
805 name,
806 RpcIdentifierAccessor(new CustomAccessor<Service, RpcIdentifier>(
807 this, get, set)));
808}
809
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800810void Service::HelpRegisterDerivedUint16(
811 const string &name,
812 uint16(Service::*get)(Error *),
813 void(Service::*set)(const uint16&, Error *)) {
814 store_.RegisterDerivedUint16(
815 name,
816 Uint16Accessor(new CustomAccessor<Service, uint16>(this, get, set)));
817}
818
mukesh agrawal292dc0f2012-01-26 18:02:46 -0800819void Service::HelpRegisterWriteOnlyDerivedString(
820 const string &name,
821 void(Service::*set)(const string &, Error *),
822 void(Service::*clear)(Error *),
823 const string *default_value) {
824 store_.RegisterDerivedString(
825 name,
826 StringAccessor(
827 new CustomWriteOnlyAccessor<Service, string>(
828 this, set, clear, default_value)));
829}
830
Darin Petkovba40dd32011-07-11 20:06:39 -0700831void Service::SaveString(StoreInterface *storage,
Chris Masone34af2182011-08-22 11:59:36 -0700832 const string &id,
Darin Petkovba40dd32011-07-11 20:06:39 -0700833 const string &key,
834 const string &value,
835 bool crypted,
836 bool save) {
837 if (value.empty() || !save) {
Chris Masone34af2182011-08-22 11:59:36 -0700838 storage->DeleteKey(id, key);
Darin Petkovba40dd32011-07-11 20:06:39 -0700839 return;
840 }
841 if (crypted) {
Chris Masone34af2182011-08-22 11:59:36 -0700842 storage->SetCryptedString(id, key, value);
Darin Petkovba40dd32011-07-11 20:06:39 -0700843 return;
844 }
Chris Masone34af2182011-08-22 11:59:36 -0700845 storage->SetString(id, key, value);
Darin Petkovba40dd32011-07-11 20:06:39 -0700846}
847
Chris Masone34af2182011-08-22 11:59:36 -0700848void Service::LoadEapCredentials(StoreInterface *storage, const string &id) {
Gaurav Shah10109f22011-11-11 20:16:22 -0800849 EapCredentials eap;
850 storage->GetCryptedString(id, kStorageEapIdentity, &eap.identity);
851 storage->GetString(id, kStorageEapEap, &eap.eap);
852 storage->GetString(id, kStorageEapInnerEap, &eap.inner_eap);
Darin Petkovba40dd32011-07-11 20:06:39 -0700853 storage->GetCryptedString(id,
854 kStorageEapAnonymousIdentity,
Gaurav Shah10109f22011-11-11 20:16:22 -0800855 &eap.anonymous_identity);
856 storage->GetString(id, kStorageEapClientCert, &eap.client_cert);
857 storage->GetString(id, kStorageEapCertID, &eap.cert_id);
858 storage->GetString(id, kStorageEapPrivateKey, &eap.private_key);
Darin Petkovba40dd32011-07-11 20:06:39 -0700859 storage->GetCryptedString(id,
860 kStorageEapPrivateKeyPassword,
Gaurav Shah10109f22011-11-11 20:16:22 -0800861 &eap.private_key_password);
862 storage->GetString(id, kStorageEapKeyID, &eap.key_id);
863 storage->GetString(id, kStorageEapCACert, &eap.ca_cert);
864 storage->GetString(id, kStorageEapCACertID, &eap.ca_cert_id);
Paul Stewartecf4cd12012-04-17 11:08:39 -0700865 storage->GetString(id, kStorageEapCACertNSS, &eap.ca_cert_nss);
Gaurav Shah10109f22011-11-11 20:16:22 -0800866 storage->GetBool(id, kStorageEapUseSystemCAs, &eap.use_system_cas);
867 storage->GetString(id, kStorageEapPIN, &eap.pin);
868 storage->GetCryptedString(id, kStorageEapPassword, &eap.password);
869 storage->GetString(id, kStorageEapKeyManagement, &eap.key_management);
870 set_eap(eap);
Darin Petkovba40dd32011-07-11 20:06:39 -0700871}
872
Chris Masone34af2182011-08-22 11:59:36 -0700873void Service::SaveEapCredentials(StoreInterface *storage, const string &id) {
Darin Petkovba40dd32011-07-11 20:06:39 -0700874 bool save = save_credentials_;
Chris Masone34af2182011-08-22 11:59:36 -0700875 SaveString(storage, id, kStorageEapIdentity, eap_.identity, true, save);
876 SaveString(storage, id, kStorageEapEap, eap_.eap, false, true);
877 SaveString(storage, id, kStorageEapInnerEap, eap_.inner_eap, false, true);
Darin Petkovba40dd32011-07-11 20:06:39 -0700878 SaveString(storage,
Chris Masone34af2182011-08-22 11:59:36 -0700879 id,
Darin Petkovba40dd32011-07-11 20:06:39 -0700880 kStorageEapAnonymousIdentity,
881 eap_.anonymous_identity,
882 true,
883 save);
Chris Masone34af2182011-08-22 11:59:36 -0700884 SaveString(storage, id, kStorageEapClientCert, eap_.client_cert, false, save);
885 SaveString(storage, id, kStorageEapCertID, eap_.cert_id, false, save);
886 SaveString(storage, id, kStorageEapPrivateKey, eap_.private_key, false, save);
Darin Petkovba40dd32011-07-11 20:06:39 -0700887 SaveString(storage,
Chris Masone34af2182011-08-22 11:59:36 -0700888 id,
Darin Petkovba40dd32011-07-11 20:06:39 -0700889 kStorageEapPrivateKeyPassword,
890 eap_.private_key_password,
891 true,
892 save);
Chris Masone34af2182011-08-22 11:59:36 -0700893 SaveString(storage, id, kStorageEapKeyID, eap_.key_id, false, save);
894 SaveString(storage, id, kStorageEapCACert, eap_.ca_cert, false, true);
895 SaveString(storage, id, kStorageEapCACertID, eap_.ca_cert_id, false, true);
Paul Stewartecf4cd12012-04-17 11:08:39 -0700896 SaveString(storage, id, kStorageEapCACertNSS, eap_.ca_cert_nss, false, true);
Chris Masone34af2182011-08-22 11:59:36 -0700897 storage->SetBool(id, kStorageEapUseSystemCAs, eap_.use_system_cas);
898 SaveString(storage, id, kStorageEapPIN, eap_.pin, false, save);
899 SaveString(storage, id, kStorageEapPassword, eap_.password, true, save);
Darin Petkovba40dd32011-07-11 20:06:39 -0700900 SaveString(storage,
Chris Masone34af2182011-08-22 11:59:36 -0700901 id,
Darin Petkovba40dd32011-07-11 20:06:39 -0700902 kStorageEapKeyManagement,
903 eap_.key_management,
904 false,
905 true);
906}
907
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800908void Service::UnloadEapCredentials() {
909 eap_.identity = "";
910 eap_.eap = "";
911 eap_.inner_eap = "";
912 eap_.anonymous_identity = "";
913 eap_.client_cert = "";
914 eap_.cert_id = "";
915 eap_.private_key = "";
916 eap_.private_key_password = "";
917 eap_.key_id = "";
918 eap_.ca_cert = "";
919 eap_.ca_cert_id = "";
Paul Stewart20550982012-04-16 12:16:11 -0700920 eap_.use_system_cas = true;
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800921 eap_.pin = "";
922 eap_.password = "";
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800923}
924
Paul Stewartcb59fed2012-03-21 21:14:46 -0700925void Service::IgnoreParameterForConfigure(const string &parameter) {
926 parameters_ignored_for_configure_.insert(parameter);
927}
928
Paul Stewartac4ac002011-08-26 12:04:26 -0700929const string &Service::GetEAPKeyManagement() const {
930 return eap_.key_management;
931}
932
933void Service::SetEAPKeyManagement(const string &key_management) {
934 eap_.key_management = key_management;
935}
936
Thieu Le284fe792012-01-31 17:53:19 -0800937bool Service::GetAutoConnect(Error */*error*/) {
938 return auto_connect();
939}
940
941void Service::SetAutoConnect(const bool &connect, Error *error) {
Wade Guthrie4f28e8b2012-04-11 10:52:07 -0700942 set_auto_connect(connect);
Thieu Le284fe792012-01-31 17:53:19 -0800943}
944
Paul Stewartd215af62012-04-24 23:25:50 -0700945string Service::GetCheckPortal(Error *error) {
946 return check_portal_;
947}
948
949void Service::SetCheckPortal(const string &check_portal, Error *error) {
950 if (check_portal == check_portal_) {
951 return;
952 }
953 if (check_portal != kCheckPortalFalse &&
954 check_portal != kCheckPortalTrue &&
955 check_portal != kCheckPortalAuto) {
956 Error::PopulateAndLog(error, Error::kInvalidArguments,
957 base::StringPrintf(
958 "Invalid Service CheckPortal property value: %s",
959 check_portal.c_str()));
960 return;
961 }
962 check_portal_ = check_portal;
963}
964
Paul Stewart9f32d192012-01-30 20:37:50 -0800965void Service::SetEAPPassword(const string &password, Error */*error*/) {
966 eap_.password = password;
967}
968
969void Service::SetEAPPrivateKeyPassword(const string &password,
970 Error */*error*/) {
971 eap_.private_key_password = password;
972}
973
Paul Stewart0c438332012-04-11 07:55:27 -0700974string Service::GetNameProperty(Error *error) {
975 return friendly_name_;
976}
977
978void Service::AssertTrivialSetNameProperty(const string &name, Error *error) {
979 if (name != friendly_name_) {
980 Error::PopulateAndLog(error, Error::kInvalidArguments,
Paul Stewart2bf424f2012-04-11 18:59:39 -0700981 base::StringPrintf(
982 "Service Name property cannot be modified "
983 "(%s to %s)", friendly_name_.c_str(),
984 name.c_str()));
Paul Stewart0c438332012-04-11 07:55:27 -0700985 }
986}
987
Paul Stewart1b1a7f22012-01-06 16:24:06 -0800988string Service::GetProfileRpcId(Error *error) {
989 if (!profile_) {
990 // This happens in some unit tests where profile_ is not set.
991 error->Populate(Error::kNotFound);
992 return "";
993 }
994 return profile_->GetRpcIdentifier();
995}
996
997void Service::SetProfileRpcId(const string &profile, Error *error) {
998 manager_->SetProfileForService(this, profile, error);
999}
1000
Paul Stewartbe5f5b32011-12-07 17:11:11 -08001001uint16 Service::GetHTTPProxyPort(Error */*error*/) {
1002 if (http_proxy_.get()) {
1003 return static_cast<uint16>(http_proxy_->proxy_port());
1004 }
1005 return 0;
1006}
1007
mukesh agrawalcf24a242012-05-21 16:46:11 -07001008void Service::SaveToProfile() {
1009 if (profile_.get() && profile_->GetConstStorage()) {
1010 profile_->UpdateService(this);
1011 }
1012}
1013
Darin Petkovd78ee7e2012-01-12 11:21:10 +01001014void Service::SetStrength(uint8 strength) {
1015 if (strength == strength_) {
1016 return;
1017 }
1018 strength_ = strength;
1019 adaptor_->EmitUint8Changed(flimflam::kSignalStrengthProperty, strength);
1020}
1021
Paul Stewart75897df2011-04-27 09:05:53 -07001022} // namespace shill