blob: c1ad05a2060f2c3d2a36fd3955c652969496fddb [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";
Darin Petkovba40dd32011-07-11 20:06:39 -070083const char Service::kStorageName[] = "Name";
84const char Service::kStoragePriority[] = "Priority";
85const char Service::kStorageProxyConfig[] = "ProxyConfig";
86const char Service::kStorageSaveCredentials[] = "SaveCredentials";
Paul Stewart2706aaf2011-12-14 16:44:04 -080087const char Service::kStorageType[] = "Type";
Paul Stewart987e71e2011-12-05 09:45:06 -080088const char Service::kStorageUIData[] = "UIData";
Darin Petkovba40dd32011-07-11 20:06:39 -070089
mukesh agrawal8f3f7752012-02-17 19:42:09 -080090const uint8 Service::kStrengthMax = 100;
91const uint8 Service::kStrengthMin = 0;
92
mukesh agrawal51a7e932011-07-27 16:18:26 -070093// static
94unsigned int Service::serial_number_ = 0;
95
Paul Stewart75897df2011-04-27 09:05:53 -070096Service::Service(ControlInterface *control_interface,
mukesh agrawalb54601c2011-06-07 17:39:22 -070097 EventDispatcher *dispatcher,
Thieu Le3426c8f2012-01-11 17:35:11 -080098 Metrics *metrics,
mukesh agrawal7a4e4002011-09-06 11:26:05 -070099 Manager *manager,
Gaurav Shah435de2c2011-11-17 19:01:07 -0800100 Technology::Identifier technology)
Eric Shienbroodcc95c5d2012-03-30 15:25:49 -0400101 : state_(kStateIdle),
Paul Stewart03dba0b2011-08-22 16:32:45 -0700102 failure_(kFailureUnknown),
103 auto_connect_(false),
Darin Petkovba40dd32011-07-11 20:06:39 -0700104 check_portal_(kCheckPortalAuto),
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700105 connectable_(false),
mukesh agrawaladb68482012-01-17 16:31:51 -0800106 explicitly_disconnected_(false),
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700107 favorite_(false),
Darin Petkovba40dd32011-07-11 20:06:39 -0700108 priority_(kPriorityNone),
Paul Stewart1ca3e852011-11-04 07:50:49 -0700109 security_level_(0),
Paul Stewart22aa71b2011-09-16 12:15:11 -0700110 strength_(0),
Darin Petkovba40dd32011-07-11 20:06:39 -0700111 save_credentials_(true),
Gaurav Shah435de2c2011-11-17 19:01:07 -0800112 technology_(technology),
mukesh agrawal568b5c62012-02-28 14:44:47 -0800113 failed_time_(0),
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700114 dispatcher_(dispatcher),
mukesh agrawald835b202011-10-07 15:26:47 -0700115 unique_name_(base::UintToString(serial_number_++)),
116 friendly_name_(unique_name_),
Chris Masonea82b7112011-05-25 15:16:29 -0700117 available_(false),
118 configured_(false),
Chris Masonea82b7112011-05-25 15:16:29 -0700119 configuration_(NULL),
Chris Masone6791a432011-07-12 13:23:19 -0700120 adaptor_(control_interface->CreateServiceAdaptor(this)),
Thieu Le3426c8f2012-01-11 17:35:11 -0800121 metrics_(metrics),
Darin Petkov5eb05422012-05-11 15:45:25 +0200122 manager_(manager),
123 sockets_(new Sockets()) {
Thieu Le284fe792012-01-31 17:53:19 -0800124 HelpRegisterDerivedBool(flimflam::kAutoConnectProperty,
125 &Service::GetAutoConnect,
126 &Service::SetAutoConnect);
Chris Masone4d42df82011-07-02 17:09:39 -0700127
128 // flimflam::kActivationStateProperty: Registered in CellularService
129 // flimflam::kCellularApnProperty: Registered in CellularService
130 // flimflam::kCellularLastGoodApnProperty: Registered in CellularService
131 // flimflam::kNetworkTechnologyProperty: Registered in CellularService
Darin Petkov3335b372011-08-22 11:05:32 -0700132 // flimflam::kOperatorNameProperty: DEPRECATED
133 // flimflam::kOperatorCodeProperty: DEPRECATED
Chris Masone4d42df82011-07-02 17:09:39 -0700134 // flimflam::kRoamingStateProperty: Registered in CellularService
Darin Petkov3335b372011-08-22 11:05:32 -0700135 // flimflam::kServingOperatorProperty: Registered in CellularService
Chris Masone4d42df82011-07-02 17:09:39 -0700136 // flimflam::kPaymentURLProperty: Registered in CellularService
137
Paul Stewartd215af62012-04-24 23:25:50 -0700138 HelpRegisterDerivedString(flimflam::kCheckPortalProperty,
139 &Service::GetCheckPortal,
140 &Service::SetCheckPortal);
Chris Masone27c4aa52011-07-02 13:10:14 -0700141 store_.RegisterConstBool(flimflam::kConnectableProperty, &connectable_);
Jason Glasgowacdc11f2012-03-30 14:12:22 -0400142 HelpRegisterDerivedRpcIdentifier(flimflam::kDeviceProperty,
143 &Service::GetDeviceRpcId,
144 NULL);
Paul Stewart4c561612012-03-21 12:49:01 -0700145 store_.RegisterString(flimflam::kGuidProperty, &guid_);
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700146
Chris Masoneb2e326b2011-07-12 13:28:51 -0700147 store_.RegisterString(flimflam::kEapIdentityProperty, &eap_.identity);
148 store_.RegisterString(flimflam::kEAPEAPProperty, &eap_.eap);
149 store_.RegisterString(flimflam::kEapPhase2AuthProperty, &eap_.inner_eap);
Chris Masone27c4aa52011-07-02 13:10:14 -0700150 store_.RegisterString(flimflam::kEapAnonymousIdentityProperty,
Chris Masoneb2e326b2011-07-12 13:28:51 -0700151 &eap_.anonymous_identity);
152 store_.RegisterString(flimflam::kEAPClientCertProperty, &eap_.client_cert);
153 store_.RegisterString(flimflam::kEAPCertIDProperty, &eap_.cert_id);
154 store_.RegisterString(flimflam::kEapPrivateKeyProperty, &eap_.private_key);
mukesh agrawal292dc0f2012-01-26 18:02:46 -0800155 HelpRegisterWriteOnlyDerivedString(flimflam::kEapPrivateKeyPasswordProperty,
156 &Service::SetEAPPrivateKeyPassword,
157 NULL,
158 &eap_.private_key_password);
Chris Masoneb2e326b2011-07-12 13:28:51 -0700159 store_.RegisterString(flimflam::kEAPKeyIDProperty, &eap_.key_id);
160 store_.RegisterString(flimflam::kEapCaCertProperty, &eap_.ca_cert);
161 store_.RegisterString(flimflam::kEapCaCertIDProperty, &eap_.ca_cert_id);
Paul Stewartecf4cd12012-04-17 11:08:39 -0700162 store_.RegisterString(flimflam::kEapCaCertNssProperty, &eap_.ca_cert_nss);
Chris Masoneb2e326b2011-07-12 13:28:51 -0700163 store_.RegisterString(flimflam::kEAPPINProperty, &eap_.pin);
mukesh agrawal292dc0f2012-01-26 18:02:46 -0800164 HelpRegisterWriteOnlyDerivedString(flimflam::kEapPasswordProperty,
165 &Service::SetEAPPassword,
166 NULL,
167 &eap_.password);
Chris Masoneb2e326b2011-07-12 13:28:51 -0700168 store_.RegisterString(flimflam::kEapKeyMgmtProperty, &eap_.key_management);
169 store_.RegisterBool(flimflam::kEapUseSystemCAsProperty, &eap_.use_system_cas);
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700170
Eric Shienbroodcc95c5d2012-03-30 15:25:49 -0400171 // TODO(ers): in flimflam clearing Error has the side-effect of
172 // setting the service state to IDLE. Is this important? I could
173 // see an autotest depending on it.
Chris Masone27c4aa52011-07-02 13:10:14 -0700174 store_.RegisterConstString(flimflam::kErrorProperty, &error_);
175 store_.RegisterConstBool(flimflam::kFavoriteProperty, &favorite_);
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800176 HelpRegisterDerivedUint16(shill::kHTTPProxyPortProperty,
177 &Service::GetHTTPProxyPort,
178 NULL);
Paul Stewart10241e32012-04-23 18:15:06 -0700179 HelpRegisterDerivedRpcIdentifier(shill::kIPConfigProperty,
180 &Service::GetIPConfigRpcIdentifier,
181 NULL);
Chris Masone27c4aa52011-07-02 13:10:14 -0700182 HelpRegisterDerivedBool(flimflam::kIsActiveProperty,
Chris Masoneb2e326b2011-07-12 13:28:51 -0700183 &Service::IsActive,
184 NULL);
Chris Masone4d42df82011-07-02 17:09:39 -0700185 // flimflam::kModeProperty: Registered in WiFiService
Paul Stewart0c438332012-04-11 07:55:27 -0700186
187 // Although this is a read-only property, some callers want to blindly
188 // set this value to its current value.
189 HelpRegisterDerivedString(flimflam::kNameProperty,
190 &Service::GetNameProperty,
191 &Service::AssertTrivialSetNameProperty);
Chris Masone4d42df82011-07-02 17:09:39 -0700192 // flimflam::kPassphraseProperty: Registered in WiFiService
193 // flimflam::kPassphraseRequiredProperty: Registered in WiFiService
Chris Masone27c4aa52011-07-02 13:10:14 -0700194 store_.RegisterInt32(flimflam::kPriorityProperty, &priority_);
195 HelpRegisterDerivedString(flimflam::kProfileProperty,
196 &Service::GetProfileRpcId,
Paul Stewart1b1a7f22012-01-06 16:24:06 -0800197 &Service::SetProfileRpcId);
Chris Masone4d42df82011-07-02 17:09:39 -0700198 store_.RegisterString(flimflam::kProxyConfigProperty, &proxy_config_);
Chris Masoneb2e326b2011-07-12 13:28:51 -0700199 store_.RegisterBool(flimflam::kSaveCredentialsProperty, &save_credentials_);
Gaurav Shah435de2c2011-11-17 19:01:07 -0800200 HelpRegisterDerivedString(flimflam::kTypeProperty,
201 &Service::GetTechnologyString,
202 NULL);
Chris Masone4d42df82011-07-02 17:09:39 -0700203 // flimflam::kSecurityProperty: Registered in WiFiService
Chris Masone27c4aa52011-07-02 13:10:14 -0700204 HelpRegisterDerivedString(flimflam::kStateProperty,
205 &Service::CalculateState,
206 NULL);
Darin Petkovd78ee7e2012-01-12 11:21:10 +0100207 store_.RegisterConstUint8(flimflam::kSignalStrengthProperty, &strength_);
Paul Stewart987e71e2011-12-05 09:45:06 -0800208 store_.RegisterString(flimflam::kUIDataProperty, &ui_data_);
Chris Masone4d42df82011-07-02 17:09:39 -0700209 // flimflam::kWifiAuthMode: Registered in WiFiService
210 // flimflam::kWifiHiddenSsid: Registered in WiFiService
211 // flimflam::kWifiFrequency: Registered in WiFiService
212 // flimflam::kWifiPhyMode: Registered in WiFiService
213 // flimflam::kWifiHexSsid: Registered in WiFiService
Thieu Le48e6d6d2011-12-06 00:40:27 +0000214
215 metrics_->RegisterService(this);
216
Paul Stewart1062d9d2012-04-27 10:42:27 -0700217 static_ip_parameters_.PlumbPropertyStore(&store_);
218
Paul Stewartcb59fed2012-03-21 21:14:46 -0700219 IgnoreParameterForConfigure(flimflam::kTypeProperty);
Paul Stewart7f61e522012-03-22 11:13:45 -0700220 IgnoreParameterForConfigure(flimflam::kProfileProperty);
Paul Stewartcb59fed2012-03-21 21:14:46 -0700221
Ben Chanfad4a0b2012-04-18 15:49:59 -0700222 SLOG(Service, 2) << "Service initialized.";
Paul Stewart75897df2011-04-27 09:05:53 -0700223}
224
Thieu Le48e6d6d2011-12-06 00:40:27 +0000225Service::~Service() {
226 metrics_->DeregisterService(this);
227}
Paul Stewart75897df2011-04-27 09:05:53 -0700228
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000229void Service::AutoConnect() {
mukesh agrawalbf14e942012-03-02 14:36:34 -0800230 const char *reason;
231 if (this->IsAutoConnectable(&reason)) {
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000232 Error error;
233 Connect(&error);
mukesh agrawal76d13882012-01-12 15:23:11 -0800234 } else {
mukesh agrawalbf14e942012-03-02 14:36:34 -0800235 LOG(INFO) << "Suppressed autoconnect to " << friendly_name() << " "
236 << "(" << reason << ")";
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000237 }
238}
239
mukesh agrawaladb68482012-01-17 16:31:51 -0800240void Service::Connect(Error */*error*/) {
241 explicitly_disconnected_ = false;
Eric Shienbroodcc95c5d2012-03-30 15:25:49 -0400242 // clear any failure state from a previous connect attempt
243 SetState(kStateIdle);
mukesh agrawaladb68482012-01-17 16:31:51 -0800244}
245
246void Service::Disconnect(Error */*error*/) {
247 explicitly_disconnected_ = true;
248}
249
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500250void Service::ActivateCellularModem(const string &/*carrier*/,
Eric Shienbrood9a245532012-03-07 14:20:39 -0500251 Error *error,
252 const ResultCallback &/*callback*/) {
253 Error::PopulateAndLog(error, Error::kNotSupported,
254 "Service doesn't support cellular modem activation.");
Darin Petkovc408e692011-08-17 13:47:15 -0700255}
256
mukesh agrawal1830fa12011-09-26 14:31:40 -0700257bool Service::TechnologyIs(const Technology::Identifier /*type*/) const {
Paul Stewart22aa71b2011-09-16 12:15:11 -0700258 return false;
259}
260
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800261bool Service::IsActive(Error */*error*/) {
Paul Stewart03dba0b2011-08-22 16:32:45 -0700262 return state_ != kStateUnknown &&
263 state_ != kStateIdle &&
264 state_ != kStateFailure;
265}
266
267void Service::SetState(ConnectState state) {
Gaurav Shahc6d6c722011-11-17 18:59:39 -0800268 LOG(INFO) << "In " << __func__ << "(): Service " << friendly_name_
269 << " state " << ConnectStateToString(state_) << " -> "
270 << ConnectStateToString(state);
mukesh agrawal7ec71312011-11-10 02:08:26 +0000271
Paul Stewart03dba0b2011-08-22 16:32:45 -0700272 if (state == state_) {
273 return;
274 }
275
276 state_ = state;
277 if (state != kStateFailure) {
278 failure_ = kFailureUnknown;
mukesh agrawal568b5c62012-02-28 14:44:47 -0800279 failed_time_ = 0;
Paul Stewart03dba0b2011-08-22 16:32:45 -0700280 }
281 manager_->UpdateService(this);
Thieu Le48e6d6d2011-12-06 00:40:27 +0000282 metrics_->NotifyServiceStateChanged(this, state);
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800283 Error error;
284 adaptor_->EmitStringChanged(flimflam::kStateProperty, CalculateState(&error));
Paul Stewart03dba0b2011-08-22 16:32:45 -0700285}
286
287void Service::SetFailure(ConnectFailure failure) {
288 failure_ = failure;
mukesh agrawal568b5c62012-02-28 14:44:47 -0800289 failed_time_ = time(NULL);
Paul Stewart03dba0b2011-08-22 16:32:45 -0700290 SetState(kStateFailure);
291}
292
mukesh agrawal568b5c62012-02-28 14:44:47 -0800293void Service::SetFailureSilent(ConnectFailure failure) {
294 // Note that order matters here, since SetState modifies |failure_| and
295 // |failed_time_|.
296 SetState(kStateIdle);
297 failure_ = failure;
298 failed_time_ = time(NULL);
299}
300
Chris Masone6791a432011-07-12 13:23:19 -0700301string Service::GetRpcIdentifier() const {
Chris Masone3c3f6a12011-07-01 10:01:41 -0700302 return adaptor_->GetRpcIdentifier();
303}
304
Paul Stewartbba6a5b2011-11-02 18:45:59 -0700305bool Service::IsLoadableFrom(StoreInterface *storage) const {
306 return storage->ContainsGroup(GetStorageIdentifier());
307}
308
Chris Masone9d779932011-08-25 16:33:41 -0700309bool Service::Load(StoreInterface *storage) {
310 const string id = GetStorageIdentifier();
Chris Masone34af2182011-08-22 11:59:36 -0700311 if (!storage->ContainsGroup(id)) {
312 LOG(WARNING) << "Service is not available in the persistent store: " << id;
313 return false;
314 }
315 storage->GetBool(id, kStorageAutoConnect, &auto_connect_);
316 storage->GetString(id, kStorageCheckPortal, &check_portal_);
317 storage->GetBool(id, kStorageFavorite, &favorite_);
318 storage->GetInt(id, kStoragePriority, &priority_);
319 storage->GetString(id, kStorageProxyConfig, &proxy_config_);
320 storage->GetBool(id, kStorageSaveCredentials, &save_credentials_);
Paul Stewart987e71e2011-12-05 09:45:06 -0800321 storage->GetString(id, kStorageUIData, &ui_data_);
Paul Stewart4c561612012-03-21 12:49:01 -0700322 storage->GetString(id, kStorageGUID, &guid_);
Chris Masone34af2182011-08-22 11:59:36 -0700323
324 LoadEapCredentials(storage, id);
Paul Stewart1062d9d2012-04-27 10:42:27 -0700325 static_ip_parameters_.Load(storage, id);
Chris Masone34af2182011-08-22 11:59:36 -0700326 // TODO(petkov): Load these:
327
Chris Masone34af2182011-08-22 11:59:36 -0700328 // "Failure"
329 // "Modified"
330 // "LastAttempt"
Chris Masone34af2182011-08-22 11:59:36 -0700331 // "APN"
332 // "LastGoodAPN"
333
mukesh agrawaladb68482012-01-17 16:31:51 -0800334 explicitly_disconnected_ = false;
Paul Stewarta41e38d2011-11-11 07:47:29 -0800335 favorite_ = true;
336
Chris Masone34af2182011-08-22 11:59:36 -0700337 return true;
Darin Petkovba40dd32011-07-11 20:06:39 -0700338}
339
Paul Stewart65512e12012-03-26 18:01:08 -0700340bool Service::Unload() {
Paul Stewarta41e38d2011-11-11 07:47:29 -0800341 auto_connect_ = false;
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800342 check_portal_ = kCheckPortalAuto;
Paul Stewarta41e38d2011-11-11 07:47:29 -0800343 favorite_ = false;
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800344 priority_ = kPriorityNone;
345 proxy_config_ = "";
346 save_credentials_ = true;
347 ui_data_ = "";
348
349 UnloadEapCredentials();
Wade Guthrie9e0c2502012-04-19 15:26:40 -0700350 Error error; // Ignored.
351 Disconnect(&error);
Paul Stewart65512e12012-03-26 18:01:08 -0700352 return false;
Paul Stewarta41e38d2011-11-11 07:47:29 -0800353}
354
Chris Masone9d779932011-08-25 16:33:41 -0700355bool Service::Save(StoreInterface *storage) {
356 const string id = GetStorageIdentifier();
Chris Masone34af2182011-08-22 11:59:36 -0700357
Paul Stewart2706aaf2011-12-14 16:44:04 -0800358 storage->SetString(id, kStorageType, GetTechnologyString(NULL));
359
Chris Masone34af2182011-08-22 11:59:36 -0700360 // TODO(petkov): We could choose to simplify the saving code by removing most
361 // conditionals thus saving even default values.
Wade Guthrie4f28e8b2012-04-11 10:52:07 -0700362 storage->SetBool(id, kStorageAutoConnect, auto_connect_);
Chris Masone34af2182011-08-22 11:59:36 -0700363 if (check_portal_ == kCheckPortalAuto) {
364 storage->DeleteKey(id, kStorageCheckPortal);
365 } else {
366 storage->SetString(id, kStorageCheckPortal, check_portal_);
367 }
368 storage->SetBool(id, kStorageFavorite, favorite_);
mukesh agrawald835b202011-10-07 15:26:47 -0700369 storage->SetString(id, kStorageName, friendly_name_);
Chris Masone34af2182011-08-22 11:59:36 -0700370 if (priority_ != kPriorityNone) {
371 storage->SetInt(id, kStoragePriority, priority_);
372 } else {
373 storage->DeleteKey(id, kStoragePriority);
374 }
Paul Stewart987e71e2011-12-05 09:45:06 -0800375 SaveString(storage, id, kStorageProxyConfig, proxy_config_, false, true);
Chris Masone34af2182011-08-22 11:59:36 -0700376 if (save_credentials_) {
377 storage->DeleteKey(id, kStorageSaveCredentials);
378 } else {
379 storage->SetBool(id, kStorageSaveCredentials, false);
380 }
Paul Stewart987e71e2011-12-05 09:45:06 -0800381 SaveString(storage, id, kStorageUIData, ui_data_, false, true);
Paul Stewart4c561612012-03-21 12:49:01 -0700382 SaveString(storage, id, kStorageGUID, guid_, false, true);
Chris Masone34af2182011-08-22 11:59:36 -0700383
384 SaveEapCredentials(storage, id);
Paul Stewart1062d9d2012-04-27 10:42:27 -0700385 static_ip_parameters_.Save(storage, id);
Chris Masone34af2182011-08-22 11:59:36 -0700386
387 // TODO(petkov): Save these:
388
389 // "WiFi.HiddenSSID"
390 // "SSID"
391 // "Failure"
392 // "Modified"
393 // "LastAttempt"
394 // WiFiService: "Passphrase"
Chris Masone34af2182011-08-22 11:59:36 -0700395
396 return true;
397}
398
Gary Moraind93615e2012-04-27 11:50:03 -0700399void Service::SaveToCurrentProfile() {
400 // Some unittests do not specify a manager.
401 if (manager()) {
402 manager()->SaveServiceToProfile(this);
403 }
Thieu Led4e9e552012-02-16 16:26:07 -0800404}
405
Paul Stewartcb59fed2012-03-21 21:14:46 -0700406void Service::Configure(const KeyValueStore &args, Error *error) {
407 map<string, bool>::const_iterator bool_it;
Ben Chanfad4a0b2012-04-18 15:49:59 -0700408 SLOG(Service, 5) << "Configuring bool properties:";
Paul Stewartcb59fed2012-03-21 21:14:46 -0700409 for (bool_it = args.bool_properties().begin();
410 bool_it != args.bool_properties().end();
411 ++bool_it) {
412 if (ContainsKey(parameters_ignored_for_configure_, bool_it->first)) {
413 continue;
414 }
Ben Chanfad4a0b2012-04-18 15:49:59 -0700415 SLOG(Service, 5) << " " << bool_it->first;
Paul Stewartcb59fed2012-03-21 21:14:46 -0700416 Error set_error;
417 store_.SetBoolProperty(bool_it->first, bool_it->second, &set_error);
418 if (error->IsSuccess() && set_error.IsFailure()) {
419 error->CopyFrom(set_error);
420 }
421 }
Ben Chanfad4a0b2012-04-18 15:49:59 -0700422 SLOG(Service, 5) << "Configuring string properties:";
Paul Stewartcb59fed2012-03-21 21:14:46 -0700423 map<string, string>::const_iterator string_it;
424 for (string_it = args.string_properties().begin();
425 string_it != args.string_properties().end();
426 ++string_it) {
427 if (ContainsKey(parameters_ignored_for_configure_, string_it->first)) {
428 continue;
429 }
Ben Chanfad4a0b2012-04-18 15:49:59 -0700430 SLOG(Service, 5) << " " << string_it->first;
Paul Stewartcb59fed2012-03-21 21:14:46 -0700431 Error set_error;
432 store_.SetStringProperty(string_it->first, string_it->second, &set_error);
433 if (error->IsSuccess() && set_error.IsFailure()) {
434 error->CopyFrom(set_error);
435 }
436 }
Ben Chanfad4a0b2012-04-18 15:49:59 -0700437 SLOG(Service, 5) << "Configuring uint32 properties:";
Paul Stewartcb59fed2012-03-21 21:14:46 -0700438 map<string, uint32>::const_iterator int_it;
439 for (int_it = args.uint_properties().begin();
440 int_it != args.uint_properties().end();
441 ++int_it) {
442 if (ContainsKey(parameters_ignored_for_configure_, int_it->first)) {
443 continue;
444 }
Ben Chanfad4a0b2012-04-18 15:49:59 -0700445 SLOG(Service, 5) << " " << int_it->first;
Paul Stewartcb59fed2012-03-21 21:14:46 -0700446 Error set_error;
447 store_.SetUint32Property(int_it->first, int_it->second, &set_error);
448 if (error->IsSuccess() && set_error.IsFailure()) {
449 error->CopyFrom(set_error);
450 }
451 }
452}
453
Paul Stewart10ccbb32012-04-26 15:59:30 -0700454bool Service::IsRemembered() const {
455 return profile_ && !manager_->IsServiceEphemeral(this);
456}
457
mukesh agrawal00917ce2011-11-22 23:56:55 +0000458void Service::MakeFavorite() {
459 if (favorite_) {
460 // We do not want to clobber the value of auto_connect_ (it may
461 // be user-set). So return early.
462 return;
463 }
464
465 auto_connect_ = true;
466 favorite_ = true;
467}
468
Darin Petkov5eb05422012-05-11 15:45:25 +0200469void Service::SetConnection(const ConnectionRefPtr &connection) {
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800470 if (connection.get()) {
Paul Stewartc8f4bef2011-12-13 09:45:51 -0800471 http_proxy_.reset(new HTTPProxy(connection));
Darin Petkov5eb05422012-05-11 15:45:25 +0200472 http_proxy_->Start(dispatcher_, sockets_.get());
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800473 } else {
474 http_proxy_.reset();
475 }
476 connection_ = connection;
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800477}
478
Gaurav Shah10109f22011-11-11 20:16:22 -0800479bool Service::Is8021xConnectable() const {
480 // We mirror all the flimflam checks (see service.c:is_connectable()).
481
482 // Identity is required.
483 if (eap_.identity.empty()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700484 SLOG(Service, 2) << "Not connectable: Identity is empty.";
Gaurav Shah10109f22011-11-11 20:16:22 -0800485 return false;
486 }
487
488 if (!eap_.client_cert.empty() || !eap_.cert_id.empty()) {
489 // If a client certificate is being used, we must have a private key.
490 if (eap_.private_key.empty() && eap_.key_id.empty()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700491 SLOG(Service, 2)
492 << "Not connectable. Client certificate but no private key.";
Gaurav Shah10109f22011-11-11 20:16:22 -0800493 return false;
494 }
495 }
496 if (!eap_.cert_id.empty() || !eap_.key_id.empty() ||
497 !eap_.ca_cert_id.empty()) {
498 // If PKCS#11 data is needed, a PIN is required.
499 if (eap_.pin.empty()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700500 SLOG(Service, 2) << "Not connectable. PKCS#11 data but no PIN.";
Gaurav Shah10109f22011-11-11 20:16:22 -0800501 return false;
502 }
503 }
504
505 // For EAP-TLS, a client certificate is required.
506 if (eap_.eap.empty() || eap_.eap == "TLS") {
Paul Stewart81426132012-05-16 10:05:10 -0700507 if ((!eap_.client_cert.empty() || !eap_.cert_id.empty()) &&
508 (!eap_.private_key.empty() || !eap_.key_id.empty())) {
509 SLOG(Service, 2) << "Connectable. EAP-TLS with a client cert and key.";
Gaurav Shah10109f22011-11-11 20:16:22 -0800510 return true;
511 }
512 }
513
514 // For EAP types other than TLS (e.g. EAP-TTLS or EAP-PEAP, password is the
515 // minimum requirement), at least an identity + password is required.
516 if (eap_.eap.empty() || eap_.eap != "TLS") {
517 if (!eap_.password.empty()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700518 SLOG(Service, 2) << "Connectable. !EAP-TLS and has a password.";
Gaurav Shah10109f22011-11-11 20:16:22 -0800519 return true;
520 }
521 }
522
Ben Chanfad4a0b2012-04-18 15:49:59 -0700523 SLOG(Service, 2)
524 << "Not connectable. No suitable EAP configuration was found.";
Gaurav Shah10109f22011-11-11 20:16:22 -0800525 return false;
526}
527
528void Service::set_eap(const EapCredentials &eap) {
529 eap_ = eap;
530 // Note: Connectability can only be updated by a subclass of Service
531 // with knowledge of whether the service actually uses 802.1x credentials.
532}
533
mukesh agrawal00917ce2011-11-22 23:56:55 +0000534// static
Gaurav Shahc6d6c722011-11-17 18:59:39 -0800535const char *Service::ConnectFailureToString(const ConnectFailure &state) {
536 switch (state) {
537 case kFailureUnknown:
538 return "Unknown";
539 case kFailureActivationFailure:
540 return "Activation Failure";
541 case kFailureOutOfRange:
542 return "Out of range";
543 case kFailurePinMissing:
544 return "PIN missing";
545 case kFailureConfigurationFailed:
546 return "Configuration Failed";
547 case kFailureBadCredentials:
548 return "Bad Credentials";
549 case kFailureNeedEVDO:
550 return "Need EVDO";
551 case kFailureNeedHomeNetwork:
552 return "Need Home Network";
553 case kFailureOTASPFailure:
554 return "OTASP Failure";
555 case kFailureAAAFailure:
556 return "AAA Failure";
Thieu Le48e6d6d2011-12-06 00:40:27 +0000557 case kFailureMax:
558 return "Max failure error code";
Gaurav Shahc6d6c722011-11-17 18:59:39 -0800559 }
560 return "Invalid";
561}
562
563// static
564const char *Service::ConnectStateToString(const ConnectState &state) {
565 switch (state) {
566 case kStateUnknown:
567 return "Unknown";
568 case kStateIdle:
569 return "Idle";
570 case kStateAssociating:
571 return "Associating";
572 case kStateConfiguring:
573 return "Configuring";
574 case kStateConnected:
575 return "Connected";
576 case kStateDisconnected:
577 return "Disconnected";
Thieu Le48e6d6d2011-12-06 00:40:27 +0000578 case kStatePortal:
579 return "Portal";
Gaurav Shahc6d6c722011-11-17 18:59:39 -0800580 case kStateFailure:
581 return "Failure";
582 case kStateOnline:
583 return "Online";
584 }
585 return "Invalid";
586}
587
Gaurav Shah435de2c2011-11-17 19:01:07 -0800588string Service::GetTechnologyString(Error */*error*/) {
589 return Technology::NameFromIdentifier(technology());
590}
591
Jason Glasgowb5790052012-01-27 01:03:52 -0500592// static
Paul Stewart22aa71b2011-09-16 12:15:11 -0700593bool Service::DecideBetween(int a, int b, bool *decision) {
594 if (a == b)
595 return false;
596 *decision = (a > b);
597 return true;
598}
599
mukesh agrawal00917ce2011-11-22 23:56:55 +0000600// static
Paul Stewart22aa71b2011-09-16 12:15:11 -0700601bool Service::Compare(ServiceRefPtr a,
602 ServiceRefPtr b,
mukesh agrawalddc378f2012-02-17 18:26:20 -0800603 const vector<Technology::Identifier> &tech_order,
604 const char **reason) {
Paul Stewart22aa71b2011-09-16 12:15:11 -0700605 bool ret;
606
607 if (a->state() != b->state()) {
608 if (DecideBetween(a->IsConnected(), b->IsConnected(), &ret)) {
mukesh agrawalddc378f2012-02-17 18:26:20 -0800609 *reason = kServiceSortIsConnected;
Paul Stewart22aa71b2011-09-16 12:15:11 -0700610 return ret;
611 }
612
613 // TODO(pstew): Services don't know about portal state yet
614
615 if (DecideBetween(a->IsConnecting(), b->IsConnecting(), &ret)) {
mukesh agrawalddc378f2012-02-17 18:26:20 -0800616 *reason = kServiceSortIsConnecting;
Paul Stewart22aa71b2011-09-16 12:15:11 -0700617 return ret;
618 }
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000619
620 if (DecideBetween(!a->IsFailed(), !b->IsFailed(), &ret)) {
mukesh agrawalddc378f2012-02-17 18:26:20 -0800621 *reason = kServiceSortIsFailed;
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000622 return ret;
623 }
Paul Stewart22aa71b2011-09-16 12:15:11 -0700624 }
625
Darin Petkov2f903b32012-04-18 12:56:43 +0200626 if (DecideBetween(a->connectable(), b->connectable(), &ret)) {
627 *reason = kServiceSortConnectable;
628 return ret;
629 }
630
631 // Ignore the auto-connect property if both services are connected
632 // already. This allows connected non-autoconnectable VPN services to be
633 // sorted higher than other connected services based on technology order.
634 if (!a->IsConnected() &&
635 DecideBetween(a->auto_connect(), b->auto_connect(), &ret)) {
636 *reason = kServiceSortAutoConnect;
637 return ret;
638 }
639
640 if (DecideBetween(a->favorite(), b->favorite(), &ret)) {
641 *reason = kServiceSortFavorite;
642 return ret;
643 }
644
645 if (DecideBetween(a->priority(), b->priority(), &ret)) {
646 *reason = kServiceSortPriority;
Paul Stewart22aa71b2011-09-16 12:15:11 -0700647 return ret;
648 }
649
650 // TODO(pstew): Below this point we are making value judgements on
651 // services that are not related to anything intrinsic or
652 // user-specified. These heuristics should be richer (contain
653 // historical information, for example) and be subject to user
654 // customization.
Paul Stewart22aa71b2011-09-16 12:15:11 -0700655 for (vector<Technology::Identifier>::const_iterator it = tech_order.begin();
656 it != tech_order.end();
657 ++it) {
mukesh agrawalddc378f2012-02-17 18:26:20 -0800658 if (DecideBetween(a->TechnologyIs(*it), b->TechnologyIs(*it), &ret)) {
659 *reason = kServiceSortTechnology;
Paul Stewart22aa71b2011-09-16 12:15:11 -0700660 return ret;
mukesh agrawalddc378f2012-02-17 18:26:20 -0800661 }
Paul Stewart22aa71b2011-09-16 12:15:11 -0700662 }
663
Paul Stewart1ca3e852011-11-04 07:50:49 -0700664 if (DecideBetween(a->security_level(), b->security_level(), &ret) ||
Paul Stewart22aa71b2011-09-16 12:15:11 -0700665 DecideBetween(a->strength(), b->strength(), &ret)) {
mukesh agrawalddc378f2012-02-17 18:26:20 -0800666 *reason = kServiceSortSecurityEtc;
Paul Stewart22aa71b2011-09-16 12:15:11 -0700667 return ret;
668 }
669
mukesh agrawalddc378f2012-02-17 18:26:20 -0800670 *reason = kServiceSortUniqueName;
Paul Stewart22aa71b2011-09-16 12:15:11 -0700671 return a->UniqueName() < b->UniqueName();
672}
673
Chris Masone34af2182011-08-22 11:59:36 -0700674const ProfileRefPtr &Service::profile() const { return profile_; }
675
676void Service::set_profile(const ProfileRefPtr &p) { profile_ = p; }
677
Paul Stewartff14b022012-04-24 20:06:23 -0700678void Service::OnPropertyChanged(const string &property) {
Paul Stewart81426132012-05-16 10:05:10 -0700679 if (Is8021x() &&
680 (property == flimflam::kEAPCertIDProperty ||
681 property == flimflam::kEAPClientCertProperty ||
682 property == flimflam::kEAPKeyIDProperty ||
683 property == flimflam::kEAPPINProperty ||
684 property == flimflam::kEapCaCertIDProperty ||
685 property == flimflam::kEapIdentityProperty ||
686 property == flimflam::kEapPasswordProperty ||
687 property == flimflam::kEapPrivateKeyProperty)) {
688 // This notifies subclassess that EAP parameters have been changed.
689 set_eap(eap_);
690 }
Paul Stewartff14b022012-04-24 20:06:23 -0700691 if (profile_.get() && profile_->GetConstStorage()) {
692 profile_->UpdateService(this);
693 }
Paul Stewartd215af62012-04-24 23:25:50 -0700694 if ((property == flimflam::kCheckPortalProperty ||
695 property == flimflam::kProxyConfigProperty) &&
696 (state_ == kStateConnected ||
697 state_ == kStatePortal ||
698 state_ == kStateOnline)) {
699 manager_->RecheckPortalOnService(this);
700 }
Paul Stewartff14b022012-04-24 20:06:23 -0700701}
702
Paul Stewart10241e32012-04-23 18:15:06 -0700703string Service::GetIPConfigRpcIdentifier(Error *error) {
704 if (!connection_) {
705 error->Populate(Error::kNotFound);
706 return "/";
707 }
708
709 string id = connection_->ipconfig_rpc_identifier();
710
711 if (id.empty()) {
712 // Do not return an empty IPConfig.
713 error->Populate(Error::kNotFound);
714 return "/";
715 }
716
717 return id;
718}
719
mukesh agrawal29c13a12011-11-24 00:09:19 +0000720void Service::set_connectable(bool connectable) {
721 connectable_ = connectable;
722 adaptor_->EmitBoolChanged(flimflam::kConnectableProperty, connectable_);
723}
724
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800725string Service::CalculateState(Error */*error*/) {
mukesh agrawalf2f68a52011-09-01 12:15:48 -0700726 switch (state_) {
Gaurav Shahc6d6c722011-11-17 18:59:39 -0800727 case kStateIdle:
728 return flimflam::kStateIdle;
729 case kStateAssociating:
730 return flimflam::kStateAssociation;
731 case kStateConfiguring:
732 return flimflam::kStateConfiguration;
mukesh agrawalf2f68a52011-09-01 12:15:48 -0700733 case kStateConnected:
Paul Stewart20088d82012-02-16 06:58:55 -0800734 return flimflam::kStateReady;
Gaurav Shahc6d6c722011-11-17 18:59:39 -0800735 case kStateDisconnected:
736 return flimflam::kStateDisconnect;
737 case kStateFailure:
738 return flimflam::kStateFailure;
Paul Stewart20088d82012-02-16 06:58:55 -0800739 case kStatePortal:
740 return flimflam::kStatePortal;
Gaurav Shahc6d6c722011-11-17 18:59:39 -0800741 case kStateOnline:
742 return flimflam::kStateOnline;
743 case kStateUnknown:
mukesh agrawalf2f68a52011-09-01 12:15:48 -0700744 default:
Gaurav Shahc6d6c722011-11-17 18:59:39 -0800745 return "";
mukesh agrawalf2f68a52011-09-01 12:15:48 -0700746 }
747}
748
mukesh agrawalbf14e942012-03-02 14:36:34 -0800749bool Service::IsAutoConnectable(const char **reason) const {
750 if (!connectable()) {
751 *reason = kAutoConnNotConnectable;
752 return false;
753 }
754
755 if (IsConnected()) {
756 *reason = kAutoConnConnected;
757 return false;
758 }
759
760 if (IsConnecting()) {
761 *reason = kAutoConnConnecting;
762 return false;
763 }
764
765 if (explicitly_disconnected_) {
766 *reason = kAutoConnExplicitDisconnect;
767 return false;
768 }
769
770 return true;
mukesh agrawal76d13882012-01-12 15:23:11 -0800771}
772
Paul Stewartd215af62012-04-24 23:25:50 -0700773bool Service::IsPortalDetectionDisabled() const {
774 return check_portal_ == kCheckPortalFalse;
775}
776
777bool Service::IsPortalDetectionAuto() const {
778 return check_portal_ == kCheckPortalAuto;
779}
780
mukesh agrawalffa3d042011-10-06 15:26:10 -0700781void Service::HelpRegisterDerivedBool(
782 const string &name,
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800783 bool(Service::*get)(Error *),
mukesh agrawalffa3d042011-10-06 15:26:10 -0700784 void(Service::*set)(const bool&, Error *)) {
Chris Masone27c4aa52011-07-02 13:10:14 -0700785 store_.RegisterDerivedBool(
786 name,
787 BoolAccessor(new CustomAccessor<Service, bool>(this, get, set)));
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700788}
789
mukesh agrawalffa3d042011-10-06 15:26:10 -0700790void Service::HelpRegisterDerivedString(
791 const string &name,
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800792 string(Service::*get)(Error *),
mukesh agrawalffa3d042011-10-06 15:26:10 -0700793 void(Service::*set)(const string&, Error *)) {
Chris Masone27c4aa52011-07-02 13:10:14 -0700794 store_.RegisterDerivedString(
795 name,
796 StringAccessor(new CustomAccessor<Service, string>(this, get, set)));
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700797}
798
Jason Glasgowacdc11f2012-03-30 14:12:22 -0400799void Service::HelpRegisterDerivedRpcIdentifier(
800 const string &name,
801 RpcIdentifier(Service::*get)(Error *),
802 void(Service::*set)(const RpcIdentifier&, Error *)) {
803 store_.RegisterDerivedRpcIdentifier(
804 name,
805 RpcIdentifierAccessor(new CustomAccessor<Service, RpcIdentifier>(
806 this, get, set)));
807}
808
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800809void Service::HelpRegisterDerivedUint16(
810 const string &name,
811 uint16(Service::*get)(Error *),
812 void(Service::*set)(const uint16&, Error *)) {
813 store_.RegisterDerivedUint16(
814 name,
815 Uint16Accessor(new CustomAccessor<Service, uint16>(this, get, set)));
816}
817
mukesh agrawal292dc0f2012-01-26 18:02:46 -0800818void Service::HelpRegisterWriteOnlyDerivedString(
819 const string &name,
820 void(Service::*set)(const string &, Error *),
821 void(Service::*clear)(Error *),
822 const string *default_value) {
823 store_.RegisterDerivedString(
824 name,
825 StringAccessor(
826 new CustomWriteOnlyAccessor<Service, string>(
827 this, set, clear, default_value)));
828}
829
Darin Petkovba40dd32011-07-11 20:06:39 -0700830void Service::SaveString(StoreInterface *storage,
Chris Masone34af2182011-08-22 11:59:36 -0700831 const string &id,
Darin Petkovba40dd32011-07-11 20:06:39 -0700832 const string &key,
833 const string &value,
834 bool crypted,
835 bool save) {
836 if (value.empty() || !save) {
Chris Masone34af2182011-08-22 11:59:36 -0700837 storage->DeleteKey(id, key);
Darin Petkovba40dd32011-07-11 20:06:39 -0700838 return;
839 }
840 if (crypted) {
Chris Masone34af2182011-08-22 11:59:36 -0700841 storage->SetCryptedString(id, key, value);
Darin Petkovba40dd32011-07-11 20:06:39 -0700842 return;
843 }
Chris Masone34af2182011-08-22 11:59:36 -0700844 storage->SetString(id, key, value);
Darin Petkovba40dd32011-07-11 20:06:39 -0700845}
846
Chris Masone34af2182011-08-22 11:59:36 -0700847void Service::LoadEapCredentials(StoreInterface *storage, const string &id) {
Gaurav Shah10109f22011-11-11 20:16:22 -0800848 EapCredentials eap;
849 storage->GetCryptedString(id, kStorageEapIdentity, &eap.identity);
850 storage->GetString(id, kStorageEapEap, &eap.eap);
851 storage->GetString(id, kStorageEapInnerEap, &eap.inner_eap);
Darin Petkovba40dd32011-07-11 20:06:39 -0700852 storage->GetCryptedString(id,
853 kStorageEapAnonymousIdentity,
Gaurav Shah10109f22011-11-11 20:16:22 -0800854 &eap.anonymous_identity);
855 storage->GetString(id, kStorageEapClientCert, &eap.client_cert);
856 storage->GetString(id, kStorageEapCertID, &eap.cert_id);
857 storage->GetString(id, kStorageEapPrivateKey, &eap.private_key);
Darin Petkovba40dd32011-07-11 20:06:39 -0700858 storage->GetCryptedString(id,
859 kStorageEapPrivateKeyPassword,
Gaurav Shah10109f22011-11-11 20:16:22 -0800860 &eap.private_key_password);
861 storage->GetString(id, kStorageEapKeyID, &eap.key_id);
862 storage->GetString(id, kStorageEapCACert, &eap.ca_cert);
863 storage->GetString(id, kStorageEapCACertID, &eap.ca_cert_id);
Paul Stewartecf4cd12012-04-17 11:08:39 -0700864 storage->GetString(id, kStorageEapCACertNSS, &eap.ca_cert_nss);
Gaurav Shah10109f22011-11-11 20:16:22 -0800865 storage->GetBool(id, kStorageEapUseSystemCAs, &eap.use_system_cas);
866 storage->GetString(id, kStorageEapPIN, &eap.pin);
867 storage->GetCryptedString(id, kStorageEapPassword, &eap.password);
868 storage->GetString(id, kStorageEapKeyManagement, &eap.key_management);
869 set_eap(eap);
Darin Petkovba40dd32011-07-11 20:06:39 -0700870}
871
Chris Masone34af2182011-08-22 11:59:36 -0700872void Service::SaveEapCredentials(StoreInterface *storage, const string &id) {
Darin Petkovba40dd32011-07-11 20:06:39 -0700873 bool save = save_credentials_;
Chris Masone34af2182011-08-22 11:59:36 -0700874 SaveString(storage, id, kStorageEapIdentity, eap_.identity, true, save);
875 SaveString(storage, id, kStorageEapEap, eap_.eap, false, true);
876 SaveString(storage, id, kStorageEapInnerEap, eap_.inner_eap, false, true);
Darin Petkovba40dd32011-07-11 20:06:39 -0700877 SaveString(storage,
Chris Masone34af2182011-08-22 11:59:36 -0700878 id,
Darin Petkovba40dd32011-07-11 20:06:39 -0700879 kStorageEapAnonymousIdentity,
880 eap_.anonymous_identity,
881 true,
882 save);
Chris Masone34af2182011-08-22 11:59:36 -0700883 SaveString(storage, id, kStorageEapClientCert, eap_.client_cert, false, save);
884 SaveString(storage, id, kStorageEapCertID, eap_.cert_id, false, save);
885 SaveString(storage, id, kStorageEapPrivateKey, eap_.private_key, false, save);
Darin Petkovba40dd32011-07-11 20:06:39 -0700886 SaveString(storage,
Chris Masone34af2182011-08-22 11:59:36 -0700887 id,
Darin Petkovba40dd32011-07-11 20:06:39 -0700888 kStorageEapPrivateKeyPassword,
889 eap_.private_key_password,
890 true,
891 save);
Chris Masone34af2182011-08-22 11:59:36 -0700892 SaveString(storage, id, kStorageEapKeyID, eap_.key_id, false, save);
893 SaveString(storage, id, kStorageEapCACert, eap_.ca_cert, false, true);
894 SaveString(storage, id, kStorageEapCACertID, eap_.ca_cert_id, false, true);
Paul Stewartecf4cd12012-04-17 11:08:39 -0700895 SaveString(storage, id, kStorageEapCACertNSS, eap_.ca_cert_nss, false, true);
Chris Masone34af2182011-08-22 11:59:36 -0700896 storage->SetBool(id, kStorageEapUseSystemCAs, eap_.use_system_cas);
897 SaveString(storage, id, kStorageEapPIN, eap_.pin, false, save);
898 SaveString(storage, id, kStorageEapPassword, eap_.password, true, save);
Darin Petkovba40dd32011-07-11 20:06:39 -0700899 SaveString(storage,
Chris Masone34af2182011-08-22 11:59:36 -0700900 id,
Darin Petkovba40dd32011-07-11 20:06:39 -0700901 kStorageEapKeyManagement,
902 eap_.key_management,
903 false,
904 true);
905}
906
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800907void Service::UnloadEapCredentials() {
908 eap_.identity = "";
909 eap_.eap = "";
910 eap_.inner_eap = "";
911 eap_.anonymous_identity = "";
912 eap_.client_cert = "";
913 eap_.cert_id = "";
914 eap_.private_key = "";
915 eap_.private_key_password = "";
916 eap_.key_id = "";
917 eap_.ca_cert = "";
918 eap_.ca_cert_id = "";
Paul Stewart20550982012-04-16 12:16:11 -0700919 eap_.use_system_cas = true;
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800920 eap_.pin = "";
921 eap_.password = "";
Paul Stewartd8ad3c42012-01-09 12:39:38 -0800922}
923
Paul Stewartcb59fed2012-03-21 21:14:46 -0700924void Service::IgnoreParameterForConfigure(const string &parameter) {
925 parameters_ignored_for_configure_.insert(parameter);
926}
927
Paul Stewartac4ac002011-08-26 12:04:26 -0700928const string &Service::GetEAPKeyManagement() const {
929 return eap_.key_management;
930}
931
932void Service::SetEAPKeyManagement(const string &key_management) {
933 eap_.key_management = key_management;
934}
935
Thieu Le284fe792012-01-31 17:53:19 -0800936bool Service::GetAutoConnect(Error */*error*/) {
937 return auto_connect();
938}
939
940void Service::SetAutoConnect(const bool &connect, Error *error) {
Wade Guthrie4f28e8b2012-04-11 10:52:07 -0700941 set_auto_connect(connect);
Thieu Le284fe792012-01-31 17:53:19 -0800942}
943
Paul Stewartd215af62012-04-24 23:25:50 -0700944string Service::GetCheckPortal(Error *error) {
945 return check_portal_;
946}
947
948void Service::SetCheckPortal(const string &check_portal, Error *error) {
949 if (check_portal == check_portal_) {
950 return;
951 }
952 if (check_portal != kCheckPortalFalse &&
953 check_portal != kCheckPortalTrue &&
954 check_portal != kCheckPortalAuto) {
955 Error::PopulateAndLog(error, Error::kInvalidArguments,
956 base::StringPrintf(
957 "Invalid Service CheckPortal property value: %s",
958 check_portal.c_str()));
959 return;
960 }
961 check_portal_ = check_portal;
962}
963
Paul Stewart9f32d192012-01-30 20:37:50 -0800964void Service::SetEAPPassword(const string &password, Error */*error*/) {
965 eap_.password = password;
966}
967
968void Service::SetEAPPrivateKeyPassword(const string &password,
969 Error */*error*/) {
970 eap_.private_key_password = password;
971}
972
Paul Stewart0c438332012-04-11 07:55:27 -0700973string Service::GetNameProperty(Error *error) {
974 return friendly_name_;
975}
976
977void Service::AssertTrivialSetNameProperty(const string &name, Error *error) {
978 if (name != friendly_name_) {
979 Error::PopulateAndLog(error, Error::kInvalidArguments,
Paul Stewart2bf424f2012-04-11 18:59:39 -0700980 base::StringPrintf(
981 "Service Name property cannot be modified "
982 "(%s to %s)", friendly_name_.c_str(),
983 name.c_str()));
Paul Stewart0c438332012-04-11 07:55:27 -0700984 }
985}
986
Paul Stewart1b1a7f22012-01-06 16:24:06 -0800987string Service::GetProfileRpcId(Error *error) {
988 if (!profile_) {
989 // This happens in some unit tests where profile_ is not set.
990 error->Populate(Error::kNotFound);
991 return "";
992 }
993 return profile_->GetRpcIdentifier();
994}
995
996void Service::SetProfileRpcId(const string &profile, Error *error) {
997 manager_->SetProfileForService(this, profile, error);
998}
999
Paul Stewartbe5f5b32011-12-07 17:11:11 -08001000uint16 Service::GetHTTPProxyPort(Error */*error*/) {
1001 if (http_proxy_.get()) {
1002 return static_cast<uint16>(http_proxy_->proxy_port());
1003 }
1004 return 0;
1005}
1006
Darin Petkovd78ee7e2012-01-12 11:21:10 +01001007void Service::SetStrength(uint8 strength) {
1008 if (strength == strength_) {
1009 return;
1010 }
1011 strength_ = strength;
1012 adaptor_->EmitUint8Changed(flimflam::kSignalStrengthProperty, strength);
1013}
1014
Paul Stewart75897df2011-04-27 09:05:53 -07001015} // namespace shill