blob: bbc37ad46c66d914e551023459c24d50ce756d32 [file] [log] [blame]
Paul Stewart75897df2011-04-27 09:05:53 -07001// Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Chris Masone9be4a9d2011-05-16 15:44:09 -07005#ifndef SHILL_SERVICE_
6#define SHILL_SERVICE_
Paul Stewart75897df2011-04-27 09:05:53 -07007
Chris Masone9be4a9d2011-05-16 15:44:09 -07008#include <string>
Chris Masone8fe2c7e2011-06-09 15:51:19 -07009#include <map>
10#include <vector>
Chris Masone9be4a9d2011-05-16 15:44:09 -070011
12#include <base/memory/ref_counted.h>
Paul Stewartba41b992011-05-26 07:02:46 -070013#include <base/memory/scoped_ptr.h>
Darin Petkovba40dd32011-07-11 20:06:39 -070014#include <gtest/gtest_prod.h> // for FRIEND_TEST
Paul Stewart75897df2011-04-27 09:05:53 -070015
Chris Masone3bd3c8c2011-06-13 08:20:26 -070016#include "shill/accessor_interface.h"
Chris Masoneb925cc82011-06-22 15:39:57 -070017#include "shill/property_store.h"
Chris Masone7aa5f902011-07-11 11:13:35 -070018#include "shill/refptr_types.h"
Paul Stewart22aa71b2011-09-16 12:15:11 -070019#include "shill/technology.h"
Chris Masonec1e50412011-06-07 13:04:53 -070020
Paul Stewart75897df2011-04-27 09:05:53 -070021namespace shill {
22
23class Connection;
24class Configuration;
Chris Masone9be4a9d2011-05-16 15:44:09 -070025class ControlInterface;
Paul Stewart75897df2011-04-27 09:05:53 -070026class Endpoint;
Chris Masone8fe2c7e2011-06-09 15:51:19 -070027class Error;
Chris Masone9be4a9d2011-05-16 15:44:09 -070028class EventDispatcher;
mukesh agrawal7a4e4002011-09-06 11:26:05 -070029class KeyValueStore;
Chris Masone6791a432011-07-12 13:23:19 -070030class Manager;
Chris Masone9be4a9d2011-05-16 15:44:09 -070031class ServiceAdaptorInterface;
Darin Petkovba40dd32011-07-11 20:06:39 -070032class StoreInterface;
Paul Stewart75897df2011-04-27 09:05:53 -070033
Chris Masone7aa5f902011-07-11 11:13:35 -070034// A Service is a uniquely named entity, which the system can
35// connect in order to begin sending and receiving network traffic.
36// All Services are bound to an Entry, which represents the persistable
37// state of the Service. If the Entry is populated at the time of Service
38// creation, that information is used to prime the Service. If not, the Entry
39// becomes populated over time.
Chris Masone27c4aa52011-07-02 13:10:14 -070040class Service : public base::RefCounted<Service> {
Paul Stewart75897df2011-04-27 09:05:53 -070041 public:
Darin Petkovba40dd32011-07-11 20:06:39 -070042 static const char kCheckPortalAuto[];
43 static const char kCheckPortalFalse[];
44 static const char kCheckPortalTrue[];
45
Paul Stewart75897df2011-04-27 09:05:53 -070046 enum ConnectFailure {
Paul Stewart03dba0b2011-08-22 16:32:45 -070047 kFailureUnknown,
48 kFailureActivationFailure,
49 kFailureOutOfRange,
50 kFailurePinMissing,
51 kFailureConfigurationFailed,
52 kFailureBadCredentials,
53 kFailureNeedEVDO,
54 kFailureNeedHomeNetwork,
55 kFailureOTASPFailure,
56 kFailureAAAFailure
Paul Stewart75897df2011-04-27 09:05:53 -070057 };
Chris Masone9be4a9d2011-05-16 15:44:09 -070058 enum ConnectState {
Paul Stewart03dba0b2011-08-22 16:32:45 -070059 kStateUnknown,
60 kStateIdle,
61 kStateAssociating,
62 kStateConfiguring,
63 kStateConnected,
64 kStateDisconnected,
65 kStateFailure
Chris Masone9be4a9d2011-05-16 15:44:09 -070066 };
Chris Masoneb2e326b2011-07-12 13:28:51 -070067 struct EapCredentials {
68 EapCredentials() : use_system_cas(false) {}
69 std::string identity;
70 std::string eap;
71 std::string inner_eap;
72 std::string anonymous_identity;
73 std::string client_cert;
74 std::string cert_id;
75 std::string private_key;
76 std::string private_key_password;
77 std::string key_id;
78 std::string ca_cert;
79 std::string ca_cert_id;
80 bool use_system_cas;
81 std::string pin;
82 std::string password;
83 std::string key_management;
84 };
Chris Masone9be4a9d2011-05-16 15:44:09 -070085
Paul Stewartac4ac002011-08-26 12:04:26 -070086 static const int kPriorityNone;
87
Chris Masone9be4a9d2011-05-16 15:44:09 -070088 // A constructor for the Service object
89 Service(ControlInterface *control_interface,
90 EventDispatcher *dispatcher,
mukesh agrawal7a4e4002011-09-06 11:26:05 -070091 Manager *manager,
92 const std::string &type);
Chris Masone9be4a9d2011-05-16 15:44:09 -070093 virtual ~Service();
Darin Petkovba40dd32011-07-11 20:06:39 -070094
Darin Petkov4d6d9412011-08-24 13:19:54 -070095 virtual void Connect(Error *error) = 0;
Chris Masone9be4a9d2011-05-16 15:44:09 -070096 virtual void Disconnect() = 0;
Chris Masonea82b7112011-05-25 15:16:29 -070097
Darin Petkovb100ae72011-08-24 16:19:45 -070098 // The default implementation sets |error| to kInvalidArguments.
99 virtual void ActivateCellularModem(const std::string &carrier, Error *error);
Darin Petkovc408e692011-08-17 13:47:15 -0700100
Paul Stewart22aa71b2011-09-16 12:15:11 -0700101 // Base method always returns false.
102 virtual bool TechnologyIs(const Technology::Identifier type) const;
103
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800104 virtual bool IsActive(Error *error);
Paul Stewart03dba0b2011-08-22 16:32:45 -0700105
106 virtual ConnectState state() const { return state_; }
107 // Updates the state of the Service and alerts the manager. Also
108 // clears |failure_| if the new state isn't a failure.
109 virtual void SetState(ConnectState state);
110
Paul Stewart22aa71b2011-09-16 12:15:11 -0700111 // State utility functions
112 bool IsConnected() const { return state() == kStateConnected; }
113 bool IsConnecting() const {
114 return state() == kStateAssociating || state() == kStateConfiguring;
115 }
116
Paul Stewart03dba0b2011-08-22 16:32:45 -0700117 virtual ConnectFailure failure() const { return failure_; }
118 // Records the failure mode, and sets the Service state to "Failure".
119 virtual void SetFailure(ConnectFailure failure);
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700120
Darin Petkovba40dd32011-07-11 20:06:39 -0700121 // Returns a string that is guaranteed to uniquely identify this Service
122 // instance.
mukesh agrawald835b202011-10-07 15:26:47 -0700123 const std::string &UniqueName() const { return unique_name_; }
Darin Petkovafa6fc42011-06-21 16:21:08 -0700124
Chris Masone6791a432011-07-12 13:23:19 -0700125 virtual std::string GetRpcIdentifier() const;
Chris Masone3c3f6a12011-07-01 10:01:41 -0700126
Darin Petkovba40dd32011-07-11 20:06:39 -0700127 // Returns the unique persistent storage identifier for the service.
Chris Masone6515aab2011-10-12 16:19:09 -0700128 virtual std::string GetStorageIdentifier() const = 0;
Darin Petkovba40dd32011-07-11 20:06:39 -0700129
Paul Stewartbba6a5b2011-11-02 18:45:59 -0700130 // Returns whether the service configuration can be loaded from |storage|.
131 virtual bool IsLoadableFrom(StoreInterface *storage) const;
132
Darin Petkovba40dd32011-07-11 20:06:39 -0700133 // Loads the service from persistent |storage|. Returns true on success.
Chris Masone9d779932011-08-25 16:33:41 -0700134 virtual bool Load(StoreInterface *storage);
Darin Petkovba40dd32011-07-11 20:06:39 -0700135
136 // Saves the service to persistent |storage|. Returns true on success.
Chris Masone9d779932011-08-25 16:33:41 -0700137 virtual bool Save(StoreInterface *storage);
Darin Petkovba40dd32011-07-11 20:06:39 -0700138
Darin Petkovafa6fc42011-06-21 16:21:08 -0700139 bool auto_connect() const { return auto_connect_; }
140 void set_auto_connect(bool connect) { auto_connect_ = connect; }
Paul Stewart75897df2011-04-27 09:05:53 -0700141
Paul Stewart22aa71b2011-09-16 12:15:11 -0700142 bool favorite() const { return favorite_; }
143 void set_favorite(bool favorite) { favorite_ = favorite; }
144
145 int32 priority() const { return priority_; }
146 void set_priority(int32 priority) { priority_ = priority; }
147
Paul Stewart1ca3e852011-11-04 07:50:49 -0700148 int32 security_level() const { return security_level_; }
149 void set_security_level(int32 security) { security_level_ = security; }
Paul Stewart22aa71b2011-09-16 12:15:11 -0700150
151 int32 strength() const { return strength_; }
152 void set_strength(int32 strength) { strength_ = strength; }
153
Darin Petkov51489002011-08-18 13:13:20 -0700154 const std::string &error() const { return error_; }
155 void set_error(const std::string &error) { error_ = error; }
156
Paul Stewart22aa71b2011-09-16 12:15:11 -0700157 // Compare two services. Returns true if Service a should be displayed
158 // above Service b
159 static bool Compare(ServiceRefPtr a,
160 ServiceRefPtr b,
161 const std::vector<Technology::Identifier> &tech_order);
162
Chris Masone34af2182011-08-22 11:59:36 -0700163 // These are defined in service.cc so that we don't have to include profile.h
Chris Masone9d779932011-08-25 16:33:41 -0700164 // TODO(cmasone): right now, these are here only so that we can get the
165 // profile name as a property. Can we store just the name, and then handle
166 // setting the profile for this service via |manager_|?
Chris Masone6791a432011-07-12 13:23:19 -0700167 const ProfileRefPtr &profile() const;
168 void set_profile(const ProfileRefPtr &p);
169
mukesh agrawalde29fa82011-09-16 16:16:36 -0700170 PropertyStore *mutable_store() { return &store_; }
171 const PropertyStore &store() const { return store_; }
Chris Masone27c4aa52011-07-02 13:10:14 -0700172
mukesh agrawalb54601c2011-06-07 17:39:22 -0700173 protected:
Chris Masone34af2182011-08-22 11:59:36 -0700174 // Returns true if a character is allowed to be in a service storage id.
175 static bool LegalChar(char a) { return isalnum(a) || a == '_'; }
176
mukesh agrawald835b202011-10-07 15:26:47 -0700177 const std::string &friendly_name() const { return friendly_name_; }
178 void set_friendly_name(const std::string &n) { friendly_name_ = n; }
Chris Masone9d779932011-08-25 16:33:41 -0700179
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800180 virtual std::string CalculateState(Error *error);
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700181
mukesh agrawalffa3d042011-10-06 15:26:10 -0700182 void HelpRegisterDerivedBool(
183 const std::string &name,
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800184 bool(Service::*get)(Error *),
mukesh agrawalffa3d042011-10-06 15:26:10 -0700185 void(Service::*set)(const bool&, Error *));
186 void HelpRegisterDerivedString(
187 const std::string &name,
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800188 std::string(Service::*get)(Error *),
mukesh agrawalffa3d042011-10-06 15:26:10 -0700189 void(Service::*set)(const std::string&, Error *));
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700190
Darin Petkovba40dd32011-07-11 20:06:39 -0700191 // Assigns |value| to |key| in |storage| if |value| is non-empty and |save| is
192 // true. Otherwise, removes |key| from |storage|. If |crypted| is true, the
193 // value is encrypted.
194 void SaveString(StoreInterface *storage,
Chris Masone34af2182011-08-22 11:59:36 -0700195 const std::string &id,
Darin Petkovba40dd32011-07-11 20:06:39 -0700196 const std::string &key,
197 const std::string &value,
198 bool crypted,
199 bool save);
200
Chris Masone34af2182011-08-22 11:59:36 -0700201 void LoadEapCredentials(StoreInterface *storage, const std::string &id);
202 void SaveEapCredentials(StoreInterface *storage, const std::string &id);
Darin Petkovba40dd32011-07-11 20:06:39 -0700203
Paul Stewartac4ac002011-08-26 12:04:26 -0700204 // Property accessors reserved for subclasses
205 EventDispatcher *dispatcher() const { return dispatcher_; }
206 const std::string &GetEAPKeyManagement() const;
207 void SetEAPKeyManagement(const std::string &key_management);
mukesh agrawalb54601c2011-06-07 17:39:22 -0700208
Paul Stewart75897df2011-04-27 09:05:53 -0700209 private:
Darin Petkovba40dd32011-07-11 20:06:39 -0700210 friend class ServiceAdaptorInterface;
Paul Stewart03dba0b2011-08-22 16:32:45 -0700211 FRIEND_TEST(DeviceTest, SelectedService);
Darin Petkovba40dd32011-07-11 20:06:39 -0700212 FRIEND_TEST(ServiceTest, Constructor);
213 FRIEND_TEST(ServiceTest, Save);
214 FRIEND_TEST(ServiceTest, SaveString);
215 FRIEND_TEST(ServiceTest, SaveStringCrypted);
216 FRIEND_TEST(ServiceTest, SaveStringDontSave);
217 FRIEND_TEST(ServiceTest, SaveStringEmpty);
218
219 static const char kStorageAutoConnect[];
220 static const char kStorageCheckPortal[];
221 static const char kStorageEapAnonymousIdentity[];
222 static const char kStorageEapCACert[];
223 static const char kStorageEapCACertID[];
224 static const char kStorageEapCertID[];
225 static const char kStorageEapClientCert[];
226 static const char kStorageEapEap[];
227 static const char kStorageEapIdentity[];
228 static const char kStorageEapInnerEap[];
229 static const char kStorageEapKeyID[];
230 static const char kStorageEapKeyManagement[];
231 static const char kStorageEapPIN[];
232 static const char kStorageEapPassword[];
233 static const char kStorageEapPrivateKey[];
234 static const char kStorageEapPrivateKeyPassword[];
235 static const char kStorageEapUseSystemCAs[];
236 static const char kStorageFavorite[];
237 static const char kStorageName[];
238 static const char kStoragePriority[];
239 static const char kStorageProxyConfig[];
240 static const char kStorageSaveCredentials[];
241
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800242 virtual std::string GetDeviceRpcId(Error *error) = 0;
Chris Masone95207da2011-06-29 16:50:49 -0700243
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800244 std::string GetProfileRpcId(Error */*error*/) {
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700245 return ""; // Will need to call Profile to get this.
246 }
247
Paul Stewart22aa71b2011-09-16 12:15:11 -0700248 // Utility function that returns true if a is different from b. When they
249 // are, "decision" is populated with the boolean value of "a > b".
250 static bool DecideBetween(int a, int b, bool *decision);
251
Paul Stewartac4ac002011-08-26 12:04:26 -0700252 ConnectState state_;
253 ConnectFailure failure_;
254 bool auto_connect_;
255 std::string check_portal_;
256 bool connectable_;
257 std::string error_;
258 bool favorite_;
259 int32 priority_;
Paul Stewart1ca3e852011-11-04 07:50:49 -0700260 int32 security_level_;
Paul Stewart22aa71b2011-09-16 12:15:11 -0700261 int32 strength_;
Paul Stewartac4ac002011-08-26 12:04:26 -0700262 std::string proxy_config_;
263 bool save_credentials_;
264 EapCredentials eap_; // Only saved if |save_credentials_| is true.
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700265 const std::string type_;
Paul Stewartac4ac002011-08-26 12:04:26 -0700266
267 ProfileRefPtr profile_;
268 PropertyStore store_;
269
270 EventDispatcher *dispatcher_;
mukesh agrawal51a7e932011-07-27 16:18:26 -0700271 static unsigned int serial_number_;
mukesh agrawald835b202011-10-07 15:26:47 -0700272 std::string unique_name_; // MUST be unique amongst service instances
273 std::string friendly_name_; // MAY be same as |unique_name_|
Paul Stewart75897df2011-04-27 09:05:53 -0700274 bool available_;
275 bool configured_;
Paul Stewart75897df2011-04-27 09:05:53 -0700276 Configuration *configuration_;
277 Connection *connection_;
Paul Stewartba41b992011-05-26 07:02:46 -0700278 scoped_ptr<ServiceAdaptorInterface> adaptor_;
Chris Masone6791a432011-07-12 13:23:19 -0700279 Manager *manager_;
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700280
mukesh agrawalb54601c2011-06-07 17:39:22 -0700281 DISALLOW_COPY_AND_ASSIGN(Service);
Paul Stewart75897df2011-04-27 09:05:53 -0700282};
283
284} // namespace shill
285
286#endif // SHILL_SERVICE_