blob: b2f83cd751233c2dc0817d10e9d46489f4c5217f [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 Petkovba40dd32011-07-11 20:06:39 -070031#include "shill/store_interface.h"
Paul Stewart75897df2011-04-27 09:05:53 -070032
Chris Masone8fe2c7e2011-06-09 15:51:19 -070033using std::map;
Paul Stewart75897df2011-04-27 09:05:53 -070034using std::string;
Chris Masone8fe2c7e2011-06-09 15:51:19 -070035using std::vector;
Paul Stewart75897df2011-04-27 09:05:53 -070036
37namespace shill {
Darin Petkovba40dd32011-07-11 20:06:39 -070038
mukesh agrawalbf14e942012-03-02 14:36:34 -080039const char Service::kAutoConnConnected[] = "connected";
40const char Service::kAutoConnConnecting[] = "connecting";
41const char Service::kAutoConnExplicitDisconnect[] = "explicitly disconnected";
42const char Service::kAutoConnNotConnectable[] = "not connectable";
43
Darin Petkovba40dd32011-07-11 20:06:39 -070044const char Service::kCheckPortalAuto[] = "auto";
45const char Service::kCheckPortalFalse[] = "false";
46const char Service::kCheckPortalTrue[] = "true";
47
Paul Stewartac4ac002011-08-26 12:04:26 -070048const int Service::kPriorityNone = 0;
49
Darin Petkov2f903b32012-04-18 12:56:43 +020050const char Service::kServiceSortAutoConnect[] = "AutoConnect";
51const char Service::kServiceSortConnectable[] = "Connectable";
52const char Service::kServiceSortFavorite[] = "Favorite";
mukesh agrawalddc378f2012-02-17 18:26:20 -080053const char Service::kServiceSortIsConnected[] = "IsConnected";
54const char Service::kServiceSortIsConnecting[] = "IsConnecting";
55const char Service::kServiceSortIsFailed[] = "IsFailed";
Darin Petkov2f903b32012-04-18 12:56:43 +020056const char Service::kServiceSortPriority[] = "Priority";
mukesh agrawalddc378f2012-02-17 18:26:20 -080057const char Service::kServiceSortSecurityEtc[] = "SecurityEtc";
Darin Petkov2f903b32012-04-18 12:56:43 +020058const char Service::kServiceSortTechnology[] = "Technology";
mukesh agrawalddc378f2012-02-17 18:26:20 -080059const char Service::kServiceSortUniqueName[] = "UniqueName";
60
Darin Petkovba40dd32011-07-11 20:06:39 -070061const char Service::kStorageAutoConnect[] = "AutoConnect";
62const char Service::kStorageCheckPortal[] = "CheckPortal";
63const char Service::kStorageEapAnonymousIdentity[] = "EAP.AnonymousIdentity";
64const char Service::kStorageEapCACert[] = "EAP.CACert";
65const char Service::kStorageEapCACertID[] = "EAP.CACertID";
Paul Stewartecf4cd12012-04-17 11:08:39 -070066const char Service::kStorageEapCACertNSS[] = "EAP.CACertNSS";
Darin Petkovba40dd32011-07-11 20:06:39 -070067const char Service::kStorageEapCertID[] = "EAP.CertID";
68const char Service::kStorageEapClientCert[] = "EAP.ClientCert";
69const char Service::kStorageEapEap[] = "EAP.EAP";
70const char Service::kStorageEapIdentity[] = "EAP.Identity";
71const char Service::kStorageEapInnerEap[] = "EAP.InnerEAP";
72const char Service::kStorageEapKeyID[] = "EAP.KeyID";
73const char Service::kStorageEapKeyManagement[] = "EAP.KeyMgmt";
74const char Service::kStorageEapPIN[] = "EAP.PIN";
75const char Service::kStorageEapPassword[] = "EAP.Password";
76const char Service::kStorageEapPrivateKey[] = "EAP.PrivateKey";
77const char Service::kStorageEapPrivateKeyPassword[] = "EAP.PrivateKeyPassword";
78const char Service::kStorageEapUseSystemCAs[] = "EAP.UseSystemCAs";
Paul Stewart0756db92012-01-27 08:34:47 -080079const char Service::kStorageError[] = "Error";
Darin Petkovba40dd32011-07-11 20:06:39 -070080const char Service::kStorageFavorite[] = "Favorite";
Paul Stewart0756db92012-01-27 08:34:47 -080081const char Service::kStorageGUID[] = "GUID";
Darin Petkovba40dd32011-07-11 20:06:39 -070082const char Service::kStorageName[] = "Name";
83const char Service::kStoragePriority[] = "Priority";
84const char Service::kStorageProxyConfig[] = "ProxyConfig";
85const char Service::kStorageSaveCredentials[] = "SaveCredentials";
Paul Stewart2706aaf2011-12-14 16:44:04 -080086const char Service::kStorageType[] = "Type";
Paul Stewart987e71e2011-12-05 09:45:06 -080087const char Service::kStorageUIData[] = "UIData";
Darin Petkovba40dd32011-07-11 20:06:39 -070088
mukesh agrawal8f3f7752012-02-17 19:42:09 -080089const uint8 Service::kStrengthMax = 100;
90const uint8 Service::kStrengthMin = 0;
91
mukesh agrawal51a7e932011-07-27 16:18:26 -070092// static
93unsigned int Service::serial_number_ = 0;
94
Paul Stewart75897df2011-04-27 09:05:53 -070095Service::Service(ControlInterface *control_interface,
mukesh agrawalb54601c2011-06-07 17:39:22 -070096 EventDispatcher *dispatcher,
Thieu Le3426c8f2012-01-11 17:35:11 -080097 Metrics *metrics,
mukesh agrawal7a4e4002011-09-06 11:26:05 -070098 Manager *manager,
Gaurav Shah435de2c2011-11-17 19:01:07 -080099 Technology::Identifier technology)
Eric Shienbroodcc95c5d2012-03-30 15:25:49 -0400100 : state_(kStateIdle),
Paul Stewart03dba0b2011-08-22 16:32:45 -0700101 failure_(kFailureUnknown),
102 auto_connect_(false),
Darin Petkovba40dd32011-07-11 20:06:39 -0700103 check_portal_(kCheckPortalAuto),
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700104 connectable_(false),
mukesh agrawaladb68482012-01-17 16:31:51 -0800105 explicitly_disconnected_(false),
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700106 favorite_(false),
Darin Petkovba40dd32011-07-11 20:06:39 -0700107 priority_(kPriorityNone),
Paul Stewart1ca3e852011-11-04 07:50:49 -0700108 security_level_(0),
Paul Stewart22aa71b2011-09-16 12:15:11 -0700109 strength_(0),
Darin Petkovba40dd32011-07-11 20:06:39 -0700110 save_credentials_(true),
Gaurav Shah435de2c2011-11-17 19:01:07 -0800111 technology_(technology),
mukesh agrawal568b5c62012-02-28 14:44:47 -0800112 failed_time_(0),
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700113 dispatcher_(dispatcher),
mukesh agrawald835b202011-10-07 15:26:47 -0700114 unique_name_(base::UintToString(serial_number_++)),
115 friendly_name_(unique_name_),
Chris Masonea82b7112011-05-25 15:16:29 -0700116 available_(false),
117 configured_(false),
Chris Masonea82b7112011-05-25 15:16:29 -0700118 configuration_(NULL),
Chris Masone6791a432011-07-12 13:23:19 -0700119 adaptor_(control_interface->CreateServiceAdaptor(this)),
Thieu Le3426c8f2012-01-11 17:35:11 -0800120 metrics_(metrics),
121 manager_(manager) {
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700122
Thieu Le284fe792012-01-31 17:53:19 -0800123 HelpRegisterDerivedBool(flimflam::kAutoConnectProperty,
124 &Service::GetAutoConnect,
125 &Service::SetAutoConnect);
Chris Masone4d42df82011-07-02 17:09:39 -0700126
127 // flimflam::kActivationStateProperty: Registered in CellularService
128 // flimflam::kCellularApnProperty: Registered in CellularService
129 // flimflam::kCellularLastGoodApnProperty: Registered in CellularService
130 // flimflam::kNetworkTechnologyProperty: Registered in CellularService
Darin Petkov3335b372011-08-22 11:05:32 -0700131 // flimflam::kOperatorNameProperty: DEPRECATED
132 // flimflam::kOperatorCodeProperty: DEPRECATED
Chris Masone4d42df82011-07-02 17:09:39 -0700133 // flimflam::kRoamingStateProperty: Registered in CellularService
Darin Petkov3335b372011-08-22 11:05:32 -0700134 // flimflam::kServingOperatorProperty: Registered in CellularService
Chris Masone4d42df82011-07-02 17:09:39 -0700135 // flimflam::kPaymentURLProperty: Registered in CellularService
136
Chris Masone27c4aa52011-07-02 13:10:14 -0700137 store_.RegisterString(flimflam::kCheckPortalProperty, &check_portal_);
138 store_.RegisterConstBool(flimflam::kConnectableProperty, &connectable_);
Jason Glasgowacdc11f2012-03-30 14:12:22 -0400139 HelpRegisterDerivedRpcIdentifier(flimflam::kDeviceProperty,
140 &Service::GetDeviceRpcId,
141 NULL);
Paul Stewart4c561612012-03-21 12:49:01 -0700142 store_.RegisterString(flimflam::kGuidProperty, &guid_);
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700143
Chris Masoneb2e326b2011-07-12 13:28:51 -0700144 store_.RegisterString(flimflam::kEapIdentityProperty, &eap_.identity);
145 store_.RegisterString(flimflam::kEAPEAPProperty, &eap_.eap);
146 store_.RegisterString(flimflam::kEapPhase2AuthProperty, &eap_.inner_eap);
Chris Masone27c4aa52011-07-02 13:10:14 -0700147 store_.RegisterString(flimflam::kEapAnonymousIdentityProperty,
Chris Masoneb2e326b2011-07-12 13:28:51 -0700148 &eap_.anonymous_identity);
149 store_.RegisterString(flimflam::kEAPClientCertProperty, &eap_.client_cert);
150 store_.RegisterString(flimflam::kEAPCertIDProperty, &eap_.cert_id);
151 store_.RegisterString(flimflam::kEapPrivateKeyProperty, &eap_.private_key);
mukesh agrawal292dc0f2012-01-26 18:02:46 -0800152 HelpRegisterWriteOnlyDerivedString(flimflam::kEapPrivateKeyPasswordProperty,
153 &Service::SetEAPPrivateKeyPassword,
154 NULL,
155 &eap_.private_key_password);
Chris Masoneb2e326b2011-07-12 13:28:51 -0700156 store_.RegisterString(flimflam::kEAPKeyIDProperty, &eap_.key_id);
157 store_.RegisterString(flimflam::kEapCaCertProperty, &eap_.ca_cert);
158 store_.RegisterString(flimflam::kEapCaCertIDProperty, &eap_.ca_cert_id);
Paul Stewartecf4cd12012-04-17 11:08:39 -0700159 store_.RegisterString(flimflam::kEapCaCertNssProperty, &eap_.ca_cert_nss);
Chris Masoneb2e326b2011-07-12 13:28:51 -0700160 store_.RegisterString(flimflam::kEAPPINProperty, &eap_.pin);
mukesh agrawal292dc0f2012-01-26 18:02:46 -0800161 HelpRegisterWriteOnlyDerivedString(flimflam::kEapPasswordProperty,
162 &Service::SetEAPPassword,
163 NULL,
164 &eap_.password);
Chris Masoneb2e326b2011-07-12 13:28:51 -0700165 store_.RegisterString(flimflam::kEapKeyMgmtProperty, &eap_.key_management);
166 store_.RegisterBool(flimflam::kEapUseSystemCAsProperty, &eap_.use_system_cas);
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700167
Eric Shienbroodcc95c5d2012-03-30 15:25:49 -0400168 // TODO(ers): in flimflam clearing Error has the side-effect of
169 // setting the service state to IDLE. Is this important? I could
170 // see an autotest depending on it.
Chris Masone27c4aa52011-07-02 13:10:14 -0700171 store_.RegisterConstString(flimflam::kErrorProperty, &error_);
172 store_.RegisterConstBool(flimflam::kFavoriteProperty, &favorite_);
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800173 HelpRegisterDerivedUint16(shill::kHTTPProxyPortProperty,
174 &Service::GetHTTPProxyPort,
175 NULL);
Paul Stewart10241e32012-04-23 18:15:06 -0700176 HelpRegisterDerivedRpcIdentifier(shill::kIPConfigProperty,
177 &Service::GetIPConfigRpcIdentifier,
178 NULL);
Chris Masone27c4aa52011-07-02 13:10:14 -0700179 HelpRegisterDerivedBool(flimflam::kIsActiveProperty,
Chris Masoneb2e326b2011-07-12 13:28:51 -0700180 &Service::IsActive,
181 NULL);
Chris Masone4d42df82011-07-02 17:09:39 -0700182 // flimflam::kModeProperty: Registered in WiFiService
Paul Stewart0c438332012-04-11 07:55:27 -0700183
184 // Although this is a read-only property, some callers want to blindly
185 // set this value to its current value.
186 HelpRegisterDerivedString(flimflam::kNameProperty,
187 &Service::GetNameProperty,
188 &Service::AssertTrivialSetNameProperty);
Chris Masone4d42df82011-07-02 17:09:39 -0700189 // flimflam::kPassphraseProperty: Registered in WiFiService
190 // flimflam::kPassphraseRequiredProperty: Registered in WiFiService
Chris Masone27c4aa52011-07-02 13:10:14 -0700191 store_.RegisterInt32(flimflam::kPriorityProperty, &priority_);
192 HelpRegisterDerivedString(flimflam::kProfileProperty,
193 &Service::GetProfileRpcId,
Paul Stewart1b1a7f22012-01-06 16:24:06 -0800194 &Service::SetProfileRpcId);
Chris Masone4d42df82011-07-02 17:09:39 -0700195 store_.RegisterString(flimflam::kProxyConfigProperty, &proxy_config_);
Chris Masoneb2e326b2011-07-12 13:28:51 -0700196 store_.RegisterBool(flimflam::kSaveCredentialsProperty, &save_credentials_);
Gaurav Shah435de2c2011-11-17 19:01:07 -0800197 HelpRegisterDerivedString(flimflam::kTypeProperty,
198 &Service::GetTechnologyString,
199 NULL);
Chris Masone4d42df82011-07-02 17:09:39 -0700200 // flimflam::kSecurityProperty: Registered in WiFiService
Chris Masone27c4aa52011-07-02 13:10:14 -0700201 HelpRegisterDerivedString(flimflam::kStateProperty,
202 &Service::CalculateState,
203 NULL);
Darin Petkovd78ee7e2012-01-12 11:21:10 +0100204 store_.RegisterConstUint8(flimflam::kSignalStrengthProperty, &strength_);
Paul Stewart987e71e2011-12-05 09:45:06 -0800205 store_.RegisterString(flimflam::kUIDataProperty, &ui_data_);
Chris Masone4d42df82011-07-02 17:09:39 -0700206 // flimflam::kWifiAuthMode: Registered in WiFiService
207 // flimflam::kWifiHiddenSsid: Registered in WiFiService
208 // flimflam::kWifiFrequency: Registered in WiFiService
209 // flimflam::kWifiPhyMode: Registered in WiFiService
210 // flimflam::kWifiHexSsid: Registered in WiFiService
Thieu Le48e6d6d2011-12-06 00:40:27 +0000211
212 metrics_->RegisterService(this);
213
Paul Stewartcb59fed2012-03-21 21:14:46 -0700214 IgnoreParameterForConfigure(flimflam::kTypeProperty);
Paul Stewart7f61e522012-03-22 11:13:45 -0700215 IgnoreParameterForConfigure(flimflam::kProfileProperty);
Paul Stewartcb59fed2012-03-21 21:14:46 -0700216
Ben Chanfad4a0b2012-04-18 15:49:59 -0700217 SLOG(Service, 2) << "Service initialized.";
Paul Stewart75897df2011-04-27 09:05:53 -0700218}
219
Thieu Le48e6d6d2011-12-06 00:40:27 +0000220Service::~Service() {
221 metrics_->DeregisterService(this);
222}
Paul Stewart75897df2011-04-27 09:05:53 -0700223
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000224void Service::AutoConnect() {
mukesh agrawalbf14e942012-03-02 14:36:34 -0800225 const char *reason;
226 if (this->IsAutoConnectable(&reason)) {
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000227 Error error;
228 Connect(&error);
mukesh agrawal76d13882012-01-12 15:23:11 -0800229 } else {
mukesh agrawalbf14e942012-03-02 14:36:34 -0800230 LOG(INFO) << "Suppressed autoconnect to " << friendly_name() << " "
231 << "(" << reason << ")";
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000232 }
233}
234
mukesh agrawaladb68482012-01-17 16:31:51 -0800235void Service::Connect(Error */*error*/) {
236 explicitly_disconnected_ = false;
Eric Shienbroodcc95c5d2012-03-30 15:25:49 -0400237 // clear any failure state from a previous connect attempt
238 SetState(kStateIdle);
mukesh agrawaladb68482012-01-17 16:31:51 -0800239}
240
241void Service::Disconnect(Error */*error*/) {
242 explicitly_disconnected_ = true;
243}
244
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500245void Service::ActivateCellularModem(const string &/*carrier*/,
Eric Shienbrood9a245532012-03-07 14:20:39 -0500246 Error *error,
247 const ResultCallback &/*callback*/) {
248 Error::PopulateAndLog(error, Error::kNotSupported,
249 "Service doesn't support cellular modem activation.");
Darin Petkovc408e692011-08-17 13:47:15 -0700250}
251
mukesh agrawal1830fa12011-09-26 14:31:40 -0700252bool Service::TechnologyIs(const Technology::Identifier /*type*/) const {
Paul Stewart22aa71b2011-09-16 12:15:11 -0700253 return false;
254}
255
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800256bool Service::IsActive(Error */*error*/) {
Paul Stewart03dba0b2011-08-22 16:32:45 -0700257 return state_ != kStateUnknown &&
258 state_ != kStateIdle &&
259 state_ != kStateFailure;
260}
261
262void Service::SetState(ConnectState state) {
Gaurav Shahc6d6c722011-11-17 18:59:39 -0800263 LOG(INFO) << "In " << __func__ << "(): Service " << friendly_name_
264 << " state " << ConnectStateToString(state_) << " -> "
265 << ConnectStateToString(state);
mukesh agrawal7ec71312011-11-10 02:08:26 +0000266
Paul Stewart03dba0b2011-08-22 16:32:45 -0700267 if (state == state_) {
268 return;
269 }
270
271 state_ = state;
272 if (state != kStateFailure) {
273 failure_ = kFailureUnknown;
mukesh agrawal568b5c62012-02-28 14:44:47 -0800274 failed_time_ = 0;
Paul Stewart03dba0b2011-08-22 16:32:45 -0700275 }
276 manager_->UpdateService(this);
Thieu Le48e6d6d2011-12-06 00:40:27 +0000277 metrics_->NotifyServiceStateChanged(this, state);
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800278 Error error;
279 adaptor_->EmitStringChanged(flimflam::kStateProperty, CalculateState(&error));
Paul Stewart03dba0b2011-08-22 16:32:45 -0700280}
281
282void Service::SetFailure(ConnectFailure failure) {
283 failure_ = failure;
mukesh agrawal568b5c62012-02-28 14:44:47 -0800284 failed_time_ = time(NULL);
Paul Stewart03dba0b2011-08-22 16:32:45 -0700285 SetState(kStateFailure);
286}
287
mukesh agrawal568b5c62012-02-28 14:44:47 -0800288void Service::SetFailureSilent(ConnectFailure failure) {
289 // Note that order matters here, since SetState modifies |failure_| and
290 // |failed_time_|.
291 SetState(kStateIdle);
292 failure_ = failure;
293 failed_time_ = time(NULL);
294}
295
Chris Masone6791a432011-07-12 13:23:19 -0700296string Service::GetRpcIdentifier() const {
Chris Masone3c3f6a12011-07-01 10:01:41 -0700297 return adaptor_->GetRpcIdentifier();
298}
299
Paul Stewartbba6a5b2011-11-02 18:45:59 -0700300bool Service::IsLoadableFrom(StoreInterface *storage) const {
301 return storage->ContainsGroup(GetStorageIdentifier());
302}
303
Chris Masone9d779932011-08-25 16:33:41 -0700304bool Service::Load(StoreInterface *storage) {
305 const string id = GetStorageIdentifier();
Chris Masone34af2182011-08-22 11:59:36 -0700306 if (!storage->ContainsGroup(id)) {
307 LOG(WARNING) << "Service is not available in the persistent store: " << id;
308 return false;
309 }
310 storage->GetBool(id, kStorageAutoConnect, &auto_connect_);
311 storage->GetString(id, kStorageCheckPortal, &check_portal_);
312 storage->GetBool(id, kStorageFavorite, &favorite_);
313 storage->GetInt(id, kStoragePriority, &priority_);
314 storage->GetString(id, kStorageProxyConfig, &proxy_config_);
315 storage->GetBool(id, kStorageSaveCredentials, &save_credentials_);
Paul Stewart987e71e2011-12-05 09:45:06 -0800316 storage->GetString(id, kStorageUIData, &ui_data_);
Paul Stewart4c561612012-03-21 12:49:01 -0700317 storage->GetString(id, kStorageGUID, &guid_);
Chris Masone34af2182011-08-22 11:59:36 -0700318
319 LoadEapCredentials(storage, id);
320
321 // TODO(petkov): Load these:
322
Chris Masone34af2182011-08-22 11:59:36 -0700323 // "Failure"
324 // "Modified"
325 // "LastAttempt"
Chris Masone34af2182011-08-22 11:59:36 -0700326 // "APN"
327 // "LastGoodAPN"
328
mukesh agrawaladb68482012-01-17 16:31:51 -0800329 explicitly_disconnected_ = false;
Paul Stewarta41e38d2011-11-11 07:47:29 -0800330 favorite_ = true;
331
Chris Masone34af2182011-08-22 11:59:36 -0700332 return true;
Darin Petkovba40dd32011-07-11 20:06:39 -0700333}
334
Paul Stewart65512e12012-03-26 18:01:08 -0700335bool Service::Unload() {
Paul Stewarta41e38d2011-11-11 07:47:29 -0800336 auto_connect_ = false;
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800337 check_portal_ = kCheckPortalAuto;
Paul Stewarta41e38d2011-11-11 07:47:29 -0800338 favorite_ = false;
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800339 priority_ = kPriorityNone;
340 proxy_config_ = "";
341 save_credentials_ = true;
342 ui_data_ = "";
343
344 UnloadEapCredentials();
Wade Guthrie9e0c2502012-04-19 15:26:40 -0700345 Error error; // Ignored.
346 Disconnect(&error);
Paul Stewart65512e12012-03-26 18:01:08 -0700347 return false;
Paul Stewarta41e38d2011-11-11 07:47:29 -0800348}
349
Chris Masone9d779932011-08-25 16:33:41 -0700350bool Service::Save(StoreInterface *storage) {
351 const string id = GetStorageIdentifier();
Chris Masone34af2182011-08-22 11:59:36 -0700352
Paul Stewart2706aaf2011-12-14 16:44:04 -0800353 storage->SetString(id, kStorageType, GetTechnologyString(NULL));
354
Chris Masone34af2182011-08-22 11:59:36 -0700355 // TODO(petkov): We could choose to simplify the saving code by removing most
356 // conditionals thus saving even default values.
Wade Guthrie4f28e8b2012-04-11 10:52:07 -0700357 storage->SetBool(id, kStorageAutoConnect, auto_connect_);
Chris Masone34af2182011-08-22 11:59:36 -0700358 if (check_portal_ == kCheckPortalAuto) {
359 storage->DeleteKey(id, kStorageCheckPortal);
360 } else {
361 storage->SetString(id, kStorageCheckPortal, check_portal_);
362 }
363 storage->SetBool(id, kStorageFavorite, favorite_);
mukesh agrawald835b202011-10-07 15:26:47 -0700364 storage->SetString(id, kStorageName, friendly_name_);
Chris Masone34af2182011-08-22 11:59:36 -0700365 if (priority_ != kPriorityNone) {
366 storage->SetInt(id, kStoragePriority, priority_);
367 } else {
368 storage->DeleteKey(id, kStoragePriority);
369 }
Paul Stewart987e71e2011-12-05 09:45:06 -0800370 SaveString(storage, id, kStorageProxyConfig, proxy_config_, false, true);
Chris Masone34af2182011-08-22 11:59:36 -0700371 if (save_credentials_) {
372 storage->DeleteKey(id, kStorageSaveCredentials);
373 } else {
374 storage->SetBool(id, kStorageSaveCredentials, false);
375 }
Paul Stewart987e71e2011-12-05 09:45:06 -0800376 SaveString(storage, id, kStorageUIData, ui_data_, false, true);
Paul Stewart4c561612012-03-21 12:49:01 -0700377 SaveString(storage, id, kStorageGUID, guid_, false, true);
Chris Masone34af2182011-08-22 11:59:36 -0700378
379 SaveEapCredentials(storage, id);
380
381 // TODO(petkov): Save these:
382
383 // "WiFi.HiddenSSID"
384 // "SSID"
385 // "Failure"
386 // "Modified"
387 // "LastAttempt"
388 // WiFiService: "Passphrase"
Chris Masone34af2182011-08-22 11:59:36 -0700389
390 return true;
391}
392
Thieu Led4e9e552012-02-16 16:26:07 -0800393bool Service::SaveToCurrentProfile() {
394 return profile()->UpdateService(this);
395}
396
Paul Stewartcb59fed2012-03-21 21:14:46 -0700397void Service::Configure(const KeyValueStore &args, Error *error) {
398 map<string, bool>::const_iterator bool_it;
Ben Chanfad4a0b2012-04-18 15:49:59 -0700399 SLOG(Service, 5) << "Configuring bool properties:";
Paul Stewartcb59fed2012-03-21 21:14:46 -0700400 for (bool_it = args.bool_properties().begin();
401 bool_it != args.bool_properties().end();
402 ++bool_it) {
403 if (ContainsKey(parameters_ignored_for_configure_, bool_it->first)) {
404 continue;
405 }
Ben Chanfad4a0b2012-04-18 15:49:59 -0700406 SLOG(Service, 5) << " " << bool_it->first;
Paul Stewartcb59fed2012-03-21 21:14:46 -0700407 Error set_error;
408 store_.SetBoolProperty(bool_it->first, bool_it->second, &set_error);
409 if (error->IsSuccess() && set_error.IsFailure()) {
410 error->CopyFrom(set_error);
411 }
412 }
Ben Chanfad4a0b2012-04-18 15:49:59 -0700413 SLOG(Service, 5) << "Configuring string properties:";
Paul Stewartcb59fed2012-03-21 21:14:46 -0700414 map<string, string>::const_iterator string_it;
415 for (string_it = args.string_properties().begin();
416 string_it != args.string_properties().end();
417 ++string_it) {
418 if (ContainsKey(parameters_ignored_for_configure_, string_it->first)) {
419 continue;
420 }
Ben Chanfad4a0b2012-04-18 15:49:59 -0700421 SLOG(Service, 5) << " " << string_it->first;
Paul Stewartcb59fed2012-03-21 21:14:46 -0700422 Error set_error;
423 store_.SetStringProperty(string_it->first, string_it->second, &set_error);
424 if (error->IsSuccess() && set_error.IsFailure()) {
425 error->CopyFrom(set_error);
426 }
427 }
Ben Chanfad4a0b2012-04-18 15:49:59 -0700428 SLOG(Service, 5) << "Configuring uint32 properties:";
Paul Stewartcb59fed2012-03-21 21:14:46 -0700429 map<string, uint32>::const_iterator int_it;
430 for (int_it = args.uint_properties().begin();
431 int_it != args.uint_properties().end();
432 ++int_it) {
433 if (ContainsKey(parameters_ignored_for_configure_, int_it->first)) {
434 continue;
435 }
Ben Chanfad4a0b2012-04-18 15:49:59 -0700436 SLOG(Service, 5) << " " << int_it->first;
Paul Stewartcb59fed2012-03-21 21:14:46 -0700437 Error set_error;
438 store_.SetUint32Property(int_it->first, int_it->second, &set_error);
439 if (error->IsSuccess() && set_error.IsFailure()) {
440 error->CopyFrom(set_error);
441 }
442 }
443}
444
mukesh agrawal00917ce2011-11-22 23:56:55 +0000445void Service::MakeFavorite() {
446 if (favorite_) {
447 // We do not want to clobber the value of auto_connect_ (it may
448 // be user-set). So return early.
449 return;
450 }
451
452 auto_connect_ = true;
453 favorite_ = true;
454}
455
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800456void Service::SetConnection(ConnectionRefPtr connection) {
457 if (connection.get()) {
Paul Stewartc8f4bef2011-12-13 09:45:51 -0800458 http_proxy_.reset(new HTTPProxy(connection));
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800459 http_proxy_->Start(dispatcher_, &sockets_);
460 } else {
461 http_proxy_.reset();
462 }
463 connection_ = connection;
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800464}
465
Gaurav Shah10109f22011-11-11 20:16:22 -0800466bool Service::Is8021xConnectable() const {
467 // We mirror all the flimflam checks (see service.c:is_connectable()).
468
469 // Identity is required.
470 if (eap_.identity.empty()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700471 SLOG(Service, 2) << "Not connectable: Identity is empty.";
Gaurav Shah10109f22011-11-11 20:16:22 -0800472 return false;
473 }
474
475 if (!eap_.client_cert.empty() || !eap_.cert_id.empty()) {
476 // If a client certificate is being used, we must have a private key.
477 if (eap_.private_key.empty() && eap_.key_id.empty()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700478 SLOG(Service, 2)
479 << "Not connectable. Client certificate but no private key.";
Gaurav Shah10109f22011-11-11 20:16:22 -0800480 return false;
481 }
482 }
483 if (!eap_.cert_id.empty() || !eap_.key_id.empty() ||
484 !eap_.ca_cert_id.empty()) {
485 // If PKCS#11 data is needed, a PIN is required.
486 if (eap_.pin.empty()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700487 SLOG(Service, 2) << "Not connectable. PKCS#11 data but no PIN.";
Gaurav Shah10109f22011-11-11 20:16:22 -0800488 return false;
489 }
490 }
491
492 // For EAP-TLS, a client certificate is required.
493 if (eap_.eap.empty() || eap_.eap == "TLS") {
494 if (!eap_.client_cert.empty() || !eap_.cert_id.empty()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700495 SLOG(Service, 2) << "Connectable. EAP-TLS with a client cert.";
Gaurav Shah10109f22011-11-11 20:16:22 -0800496 return true;
497 }
498 }
499
500 // For EAP types other than TLS (e.g. EAP-TTLS or EAP-PEAP, password is the
501 // minimum requirement), at least an identity + password is required.
502 if (eap_.eap.empty() || eap_.eap != "TLS") {
503 if (!eap_.password.empty()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700504 SLOG(Service, 2) << "Connectable. !EAP-TLS and has a password.";
Gaurav Shah10109f22011-11-11 20:16:22 -0800505 return true;
506 }
507 }
508
Ben Chanfad4a0b2012-04-18 15:49:59 -0700509 SLOG(Service, 2)
510 << "Not connectable. No suitable EAP configuration was found.";
Gaurav Shah10109f22011-11-11 20:16:22 -0800511 return false;
512}
513
514void Service::set_eap(const EapCredentials &eap) {
515 eap_ = eap;
516 // Note: Connectability can only be updated by a subclass of Service
517 // with knowledge of whether the service actually uses 802.1x credentials.
518}
519
mukesh agrawal00917ce2011-11-22 23:56:55 +0000520// static
Gaurav Shahc6d6c722011-11-17 18:59:39 -0800521const char *Service::ConnectFailureToString(const ConnectFailure &state) {
522 switch (state) {
523 case kFailureUnknown:
524 return "Unknown";
525 case kFailureActivationFailure:
526 return "Activation Failure";
527 case kFailureOutOfRange:
528 return "Out of range";
529 case kFailurePinMissing:
530 return "PIN missing";
531 case kFailureConfigurationFailed:
532 return "Configuration Failed";
533 case kFailureBadCredentials:
534 return "Bad Credentials";
535 case kFailureNeedEVDO:
536 return "Need EVDO";
537 case kFailureNeedHomeNetwork:
538 return "Need Home Network";
539 case kFailureOTASPFailure:
540 return "OTASP Failure";
541 case kFailureAAAFailure:
542 return "AAA Failure";
Thieu Le48e6d6d2011-12-06 00:40:27 +0000543 case kFailureMax:
544 return "Max failure error code";
Gaurav Shahc6d6c722011-11-17 18:59:39 -0800545 }
546 return "Invalid";
547}
548
549// static
550const char *Service::ConnectStateToString(const ConnectState &state) {
551 switch (state) {
552 case kStateUnknown:
553 return "Unknown";
554 case kStateIdle:
555 return "Idle";
556 case kStateAssociating:
557 return "Associating";
558 case kStateConfiguring:
559 return "Configuring";
560 case kStateConnected:
561 return "Connected";
562 case kStateDisconnected:
563 return "Disconnected";
Thieu Le48e6d6d2011-12-06 00:40:27 +0000564 case kStatePortal:
565 return "Portal";
Gaurav Shahc6d6c722011-11-17 18:59:39 -0800566 case kStateFailure:
567 return "Failure";
568 case kStateOnline:
569 return "Online";
570 }
571 return "Invalid";
572}
573
Gaurav Shah435de2c2011-11-17 19:01:07 -0800574string Service::GetTechnologyString(Error */*error*/) {
575 return Technology::NameFromIdentifier(technology());
576}
577
Jason Glasgowb5790052012-01-27 01:03:52 -0500578// static
Paul Stewart22aa71b2011-09-16 12:15:11 -0700579bool Service::DecideBetween(int a, int b, bool *decision) {
580 if (a == b)
581 return false;
582 *decision = (a > b);
583 return true;
584}
585
mukesh agrawal00917ce2011-11-22 23:56:55 +0000586// static
Paul Stewart22aa71b2011-09-16 12:15:11 -0700587bool Service::Compare(ServiceRefPtr a,
588 ServiceRefPtr b,
mukesh agrawalddc378f2012-02-17 18:26:20 -0800589 const vector<Technology::Identifier> &tech_order,
590 const char **reason) {
Paul Stewart22aa71b2011-09-16 12:15:11 -0700591 bool ret;
592
593 if (a->state() != b->state()) {
594 if (DecideBetween(a->IsConnected(), b->IsConnected(), &ret)) {
mukesh agrawalddc378f2012-02-17 18:26:20 -0800595 *reason = kServiceSortIsConnected;
Paul Stewart22aa71b2011-09-16 12:15:11 -0700596 return ret;
597 }
598
599 // TODO(pstew): Services don't know about portal state yet
600
601 if (DecideBetween(a->IsConnecting(), b->IsConnecting(), &ret)) {
mukesh agrawalddc378f2012-02-17 18:26:20 -0800602 *reason = kServiceSortIsConnecting;
Paul Stewart22aa71b2011-09-16 12:15:11 -0700603 return ret;
604 }
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000605
606 if (DecideBetween(!a->IsFailed(), !b->IsFailed(), &ret)) {
mukesh agrawalddc378f2012-02-17 18:26:20 -0800607 *reason = kServiceSortIsFailed;
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000608 return ret;
609 }
Paul Stewart22aa71b2011-09-16 12:15:11 -0700610 }
611
Darin Petkov2f903b32012-04-18 12:56:43 +0200612 if (DecideBetween(a->connectable(), b->connectable(), &ret)) {
613 *reason = kServiceSortConnectable;
614 return ret;
615 }
616
617 // Ignore the auto-connect property if both services are connected
618 // already. This allows connected non-autoconnectable VPN services to be
619 // sorted higher than other connected services based on technology order.
620 if (!a->IsConnected() &&
621 DecideBetween(a->auto_connect(), b->auto_connect(), &ret)) {
622 *reason = kServiceSortAutoConnect;
623 return ret;
624 }
625
626 if (DecideBetween(a->favorite(), b->favorite(), &ret)) {
627 *reason = kServiceSortFavorite;
628 return ret;
629 }
630
631 if (DecideBetween(a->priority(), b->priority(), &ret)) {
632 *reason = kServiceSortPriority;
Paul Stewart22aa71b2011-09-16 12:15:11 -0700633 return ret;
634 }
635
636 // TODO(pstew): Below this point we are making value judgements on
637 // services that are not related to anything intrinsic or
638 // user-specified. These heuristics should be richer (contain
639 // historical information, for example) and be subject to user
640 // customization.
Paul Stewart22aa71b2011-09-16 12:15:11 -0700641 for (vector<Technology::Identifier>::const_iterator it = tech_order.begin();
642 it != tech_order.end();
643 ++it) {
mukesh agrawalddc378f2012-02-17 18:26:20 -0800644 if (DecideBetween(a->TechnologyIs(*it), b->TechnologyIs(*it), &ret)) {
645 *reason = kServiceSortTechnology;
Paul Stewart22aa71b2011-09-16 12:15:11 -0700646 return ret;
mukesh agrawalddc378f2012-02-17 18:26:20 -0800647 }
Paul Stewart22aa71b2011-09-16 12:15:11 -0700648 }
649
Paul Stewart1ca3e852011-11-04 07:50:49 -0700650 if (DecideBetween(a->security_level(), b->security_level(), &ret) ||
Paul Stewart22aa71b2011-09-16 12:15:11 -0700651 DecideBetween(a->strength(), b->strength(), &ret)) {
mukesh agrawalddc378f2012-02-17 18:26:20 -0800652 *reason = kServiceSortSecurityEtc;
Paul Stewart22aa71b2011-09-16 12:15:11 -0700653 return ret;
654 }
655
mukesh agrawalddc378f2012-02-17 18:26:20 -0800656 *reason = kServiceSortUniqueName;
Paul Stewart22aa71b2011-09-16 12:15:11 -0700657 return a->UniqueName() < b->UniqueName();
658}
659
Chris Masone34af2182011-08-22 11:59:36 -0700660const ProfileRefPtr &Service::profile() const { return profile_; }
661
662void Service::set_profile(const ProfileRefPtr &p) { profile_ = p; }
663
Paul Stewartff14b022012-04-24 20:06:23 -0700664void Service::OnPropertyChanged(const string &property) {
665 if (profile_.get() && profile_->GetConstStorage()) {
666 profile_->UpdateService(this);
667 }
668}
669
Paul Stewart10241e32012-04-23 18:15:06 -0700670string Service::GetIPConfigRpcIdentifier(Error *error) {
671 if (!connection_) {
672 error->Populate(Error::kNotFound);
673 return "/";
674 }
675
676 string id = connection_->ipconfig_rpc_identifier();
677
678 if (id.empty()) {
679 // Do not return an empty IPConfig.
680 error->Populate(Error::kNotFound);
681 return "/";
682 }
683
684 return id;
685}
686
mukesh agrawal29c13a12011-11-24 00:09:19 +0000687void Service::set_connectable(bool connectable) {
688 connectable_ = connectable;
689 adaptor_->EmitBoolChanged(flimflam::kConnectableProperty, connectable_);
690}
691
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800692string Service::CalculateState(Error */*error*/) {
mukesh agrawalf2f68a52011-09-01 12:15:48 -0700693 switch (state_) {
Gaurav Shahc6d6c722011-11-17 18:59:39 -0800694 case kStateIdle:
695 return flimflam::kStateIdle;
696 case kStateAssociating:
697 return flimflam::kStateAssociation;
698 case kStateConfiguring:
699 return flimflam::kStateConfiguration;
mukesh agrawalf2f68a52011-09-01 12:15:48 -0700700 case kStateConnected:
Paul Stewart20088d82012-02-16 06:58:55 -0800701 return flimflam::kStateReady;
Gaurav Shahc6d6c722011-11-17 18:59:39 -0800702 case kStateDisconnected:
703 return flimflam::kStateDisconnect;
704 case kStateFailure:
705 return flimflam::kStateFailure;
Paul Stewart20088d82012-02-16 06:58:55 -0800706 case kStatePortal:
707 return flimflam::kStatePortal;
Gaurav Shahc6d6c722011-11-17 18:59:39 -0800708 case kStateOnline:
709 return flimflam::kStateOnline;
710 case kStateUnknown:
mukesh agrawalf2f68a52011-09-01 12:15:48 -0700711 default:
Gaurav Shahc6d6c722011-11-17 18:59:39 -0800712 return "";
mukesh agrawalf2f68a52011-09-01 12:15:48 -0700713 }
714}
715
mukesh agrawalbf14e942012-03-02 14:36:34 -0800716bool Service::IsAutoConnectable(const char **reason) const {
717 if (!connectable()) {
718 *reason = kAutoConnNotConnectable;
719 return false;
720 }
721
722 if (IsConnected()) {
723 *reason = kAutoConnConnected;
724 return false;
725 }
726
727 if (IsConnecting()) {
728 *reason = kAutoConnConnecting;
729 return false;
730 }
731
732 if (explicitly_disconnected_) {
733 *reason = kAutoConnExplicitDisconnect;
734 return false;
735 }
736
737 return true;
mukesh agrawal76d13882012-01-12 15:23:11 -0800738}
739
mukesh agrawalffa3d042011-10-06 15:26:10 -0700740void Service::HelpRegisterDerivedBool(
741 const string &name,
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800742 bool(Service::*get)(Error *),
mukesh agrawalffa3d042011-10-06 15:26:10 -0700743 void(Service::*set)(const bool&, Error *)) {
Chris Masone27c4aa52011-07-02 13:10:14 -0700744 store_.RegisterDerivedBool(
745 name,
746 BoolAccessor(new CustomAccessor<Service, bool>(this, get, set)));
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700747}
748
mukesh agrawalffa3d042011-10-06 15:26:10 -0700749void Service::HelpRegisterDerivedString(
750 const string &name,
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800751 string(Service::*get)(Error *),
mukesh agrawalffa3d042011-10-06 15:26:10 -0700752 void(Service::*set)(const string&, Error *)) {
Chris Masone27c4aa52011-07-02 13:10:14 -0700753 store_.RegisterDerivedString(
754 name,
755 StringAccessor(new CustomAccessor<Service, string>(this, get, set)));
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700756}
757
Jason Glasgowacdc11f2012-03-30 14:12:22 -0400758void Service::HelpRegisterDerivedRpcIdentifier(
759 const string &name,
760 RpcIdentifier(Service::*get)(Error *),
761 void(Service::*set)(const RpcIdentifier&, Error *)) {
762 store_.RegisterDerivedRpcIdentifier(
763 name,
764 RpcIdentifierAccessor(new CustomAccessor<Service, RpcIdentifier>(
765 this, get, set)));
766}
767
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800768void Service::HelpRegisterDerivedUint16(
769 const string &name,
770 uint16(Service::*get)(Error *),
771 void(Service::*set)(const uint16&, Error *)) {
772 store_.RegisterDerivedUint16(
773 name,
774 Uint16Accessor(new CustomAccessor<Service, uint16>(this, get, set)));
775}
776
mukesh agrawal292dc0f2012-01-26 18:02:46 -0800777void Service::HelpRegisterWriteOnlyDerivedString(
778 const string &name,
779 void(Service::*set)(const string &, Error *),
780 void(Service::*clear)(Error *),
781 const string *default_value) {
782 store_.RegisterDerivedString(
783 name,
784 StringAccessor(
785 new CustomWriteOnlyAccessor<Service, string>(
786 this, set, clear, default_value)));
787}
788
Darin Petkovba40dd32011-07-11 20:06:39 -0700789void Service::SaveString(StoreInterface *storage,
Chris Masone34af2182011-08-22 11:59:36 -0700790 const string &id,
Darin Petkovba40dd32011-07-11 20:06:39 -0700791 const string &key,
792 const string &value,
793 bool crypted,
794 bool save) {
795 if (value.empty() || !save) {
Chris Masone34af2182011-08-22 11:59:36 -0700796 storage->DeleteKey(id, key);
Darin Petkovba40dd32011-07-11 20:06:39 -0700797 return;
798 }
799 if (crypted) {
Chris Masone34af2182011-08-22 11:59:36 -0700800 storage->SetCryptedString(id, key, value);
Darin Petkovba40dd32011-07-11 20:06:39 -0700801 return;
802 }
Chris Masone34af2182011-08-22 11:59:36 -0700803 storage->SetString(id, key, value);
Darin Petkovba40dd32011-07-11 20:06:39 -0700804}
805
Chris Masone34af2182011-08-22 11:59:36 -0700806void Service::LoadEapCredentials(StoreInterface *storage, const string &id) {
Gaurav Shah10109f22011-11-11 20:16:22 -0800807 EapCredentials eap;
808 storage->GetCryptedString(id, kStorageEapIdentity, &eap.identity);
809 storage->GetString(id, kStorageEapEap, &eap.eap);
810 storage->GetString(id, kStorageEapInnerEap, &eap.inner_eap);
Darin Petkovba40dd32011-07-11 20:06:39 -0700811 storage->GetCryptedString(id,
812 kStorageEapAnonymousIdentity,
Gaurav Shah10109f22011-11-11 20:16:22 -0800813 &eap.anonymous_identity);
814 storage->GetString(id, kStorageEapClientCert, &eap.client_cert);
815 storage->GetString(id, kStorageEapCertID, &eap.cert_id);
816 storage->GetString(id, kStorageEapPrivateKey, &eap.private_key);
Darin Petkovba40dd32011-07-11 20:06:39 -0700817 storage->GetCryptedString(id,
818 kStorageEapPrivateKeyPassword,
Gaurav Shah10109f22011-11-11 20:16:22 -0800819 &eap.private_key_password);
820 storage->GetString(id, kStorageEapKeyID, &eap.key_id);
821 storage->GetString(id, kStorageEapCACert, &eap.ca_cert);
822 storage->GetString(id, kStorageEapCACertID, &eap.ca_cert_id);
Paul Stewartecf4cd12012-04-17 11:08:39 -0700823 storage->GetString(id, kStorageEapCACertNSS, &eap.ca_cert_nss);
Gaurav Shah10109f22011-11-11 20:16:22 -0800824 storage->GetBool(id, kStorageEapUseSystemCAs, &eap.use_system_cas);
825 storage->GetString(id, kStorageEapPIN, &eap.pin);
826 storage->GetCryptedString(id, kStorageEapPassword, &eap.password);
827 storage->GetString(id, kStorageEapKeyManagement, &eap.key_management);
828 set_eap(eap);
Darin Petkovba40dd32011-07-11 20:06:39 -0700829}
830
Chris Masone34af2182011-08-22 11:59:36 -0700831void Service::SaveEapCredentials(StoreInterface *storage, const string &id) {
Darin Petkovba40dd32011-07-11 20:06:39 -0700832 bool save = save_credentials_;
Chris Masone34af2182011-08-22 11:59:36 -0700833 SaveString(storage, id, kStorageEapIdentity, eap_.identity, true, save);
834 SaveString(storage, id, kStorageEapEap, eap_.eap, false, true);
835 SaveString(storage, id, kStorageEapInnerEap, eap_.inner_eap, false, true);
Darin Petkovba40dd32011-07-11 20:06:39 -0700836 SaveString(storage,
Chris Masone34af2182011-08-22 11:59:36 -0700837 id,
Darin Petkovba40dd32011-07-11 20:06:39 -0700838 kStorageEapAnonymousIdentity,
839 eap_.anonymous_identity,
840 true,
841 save);
Chris Masone34af2182011-08-22 11:59:36 -0700842 SaveString(storage, id, kStorageEapClientCert, eap_.client_cert, false, save);
843 SaveString(storage, id, kStorageEapCertID, eap_.cert_id, false, save);
844 SaveString(storage, id, kStorageEapPrivateKey, eap_.private_key, false, save);
Darin Petkovba40dd32011-07-11 20:06:39 -0700845 SaveString(storage,
Chris Masone34af2182011-08-22 11:59:36 -0700846 id,
Darin Petkovba40dd32011-07-11 20:06:39 -0700847 kStorageEapPrivateKeyPassword,
848 eap_.private_key_password,
849 true,
850 save);
Chris Masone34af2182011-08-22 11:59:36 -0700851 SaveString(storage, id, kStorageEapKeyID, eap_.key_id, false, save);
852 SaveString(storage, id, kStorageEapCACert, eap_.ca_cert, false, true);
853 SaveString(storage, id, kStorageEapCACertID, eap_.ca_cert_id, false, true);
Paul Stewartecf4cd12012-04-17 11:08:39 -0700854 SaveString(storage, id, kStorageEapCACertNSS, eap_.ca_cert_nss, false, true);
Chris Masone34af2182011-08-22 11:59:36 -0700855 storage->SetBool(id, kStorageEapUseSystemCAs, eap_.use_system_cas);
856 SaveString(storage, id, kStorageEapPIN, eap_.pin, false, save);
857 SaveString(storage, id, kStorageEapPassword, eap_.password, true, save);
Darin Petkovba40dd32011-07-11 20:06:39 -0700858 SaveString(storage,
Chris Masone34af2182011-08-22 11:59:36 -0700859 id,
Darin Petkovba40dd32011-07-11 20:06:39 -0700860 kStorageEapKeyManagement,
861 eap_.key_management,
862 false,
863 true);
864}
865
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800866void Service::UnloadEapCredentials() {
867 eap_.identity = "";
868 eap_.eap = "";
869 eap_.inner_eap = "";
870 eap_.anonymous_identity = "";
871 eap_.client_cert = "";
872 eap_.cert_id = "";
873 eap_.private_key = "";
874 eap_.private_key_password = "";
875 eap_.key_id = "";
876 eap_.ca_cert = "";
877 eap_.ca_cert_id = "";
Paul Stewart20550982012-04-16 12:16:11 -0700878 eap_.use_system_cas = true;
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800879 eap_.pin = "";
880 eap_.password = "";
881 eap_.key_management = "";
882}
883
Paul Stewartcb59fed2012-03-21 21:14:46 -0700884void Service::IgnoreParameterForConfigure(const string &parameter) {
885 parameters_ignored_for_configure_.insert(parameter);
886}
887
Paul Stewartac4ac002011-08-26 12:04:26 -0700888const string &Service::GetEAPKeyManagement() const {
889 return eap_.key_management;
890}
891
892void Service::SetEAPKeyManagement(const string &key_management) {
893 eap_.key_management = key_management;
894}
895
Thieu Le284fe792012-01-31 17:53:19 -0800896bool Service::GetAutoConnect(Error */*error*/) {
897 return auto_connect();
898}
899
900void Service::SetAutoConnect(const bool &connect, Error *error) {
Wade Guthrie4f28e8b2012-04-11 10:52:07 -0700901 set_auto_connect(connect);
Thieu Le284fe792012-01-31 17:53:19 -0800902}
903
Paul Stewart9f32d192012-01-30 20:37:50 -0800904void Service::SetEAPPassword(const string &password, Error */*error*/) {
905 eap_.password = password;
906}
907
908void Service::SetEAPPrivateKeyPassword(const string &password,
909 Error */*error*/) {
910 eap_.private_key_password = password;
911}
912
Paul Stewart0c438332012-04-11 07:55:27 -0700913string Service::GetNameProperty(Error *error) {
914 return friendly_name_;
915}
916
917void Service::AssertTrivialSetNameProperty(const string &name, Error *error) {
918 if (name != friendly_name_) {
919 Error::PopulateAndLog(error, Error::kInvalidArguments,
Paul Stewart2bf424f2012-04-11 18:59:39 -0700920 base::StringPrintf(
921 "Service Name property cannot be modified "
922 "(%s to %s)", friendly_name_.c_str(),
923 name.c_str()));
Paul Stewart0c438332012-04-11 07:55:27 -0700924 }
925}
926
Paul Stewart1b1a7f22012-01-06 16:24:06 -0800927string Service::GetProfileRpcId(Error *error) {
928 if (!profile_) {
929 // This happens in some unit tests where profile_ is not set.
930 error->Populate(Error::kNotFound);
931 return "";
932 }
933 return profile_->GetRpcIdentifier();
934}
935
936void Service::SetProfileRpcId(const string &profile, Error *error) {
937 manager_->SetProfileForService(this, profile, error);
938}
939
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800940uint16 Service::GetHTTPProxyPort(Error */*error*/) {
941 if (http_proxy_.get()) {
942 return static_cast<uint16>(http_proxy_->proxy_port());
943 }
944 return 0;
945}
946
Darin Petkovd78ee7e2012-01-12 11:21:10 +0100947void Service::SetStrength(uint8 strength) {
948 if (strength == strength_) {
949 return;
950 }
951 strength_ = strength;
952 adaptor_->EmitUint8Changed(flimflam::kSignalStrengthProperty, strength);
953}
954
Paul Stewart75897df2011-04-27 09:05:53 -0700955} // namespace shill