blob: 8ad513239096188c6295b6988bc9da14cc4b97ac [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"
Chris Masonec1e50412011-06-07 13:04:53 -070019
Paul Stewart75897df2011-04-27 09:05:53 -070020namespace shill {
21
22class Connection;
23class Configuration;
Chris Masone9be4a9d2011-05-16 15:44:09 -070024class ControlInterface;
Paul Stewart75897df2011-04-27 09:05:53 -070025class Endpoint;
Chris Masone8fe2c7e2011-06-09 15:51:19 -070026class Error;
Chris Masone9be4a9d2011-05-16 15:44:09 -070027class EventDispatcher;
Chris Masone6791a432011-07-12 13:23:19 -070028class Manager;
Chris Masone9be4a9d2011-05-16 15:44:09 -070029class ServiceAdaptorInterface;
Darin Petkovba40dd32011-07-11 20:06:39 -070030class StoreInterface;
Paul Stewart75897df2011-04-27 09:05:53 -070031
Chris Masone7aa5f902011-07-11 11:13:35 -070032// A Service is a uniquely named entity, which the system can
33// connect in order to begin sending and receiving network traffic.
34// All Services are bound to an Entry, which represents the persistable
35// state of the Service. If the Entry is populated at the time of Service
36// creation, that information is used to prime the Service. If not, the Entry
37// becomes populated over time.
Chris Masone27c4aa52011-07-02 13:10:14 -070038class Service : public base::RefCounted<Service> {
Paul Stewart75897df2011-04-27 09:05:53 -070039 public:
Darin Petkovba40dd32011-07-11 20:06:39 -070040 static const char kCheckPortalAuto[];
41 static const char kCheckPortalFalse[];
42 static const char kCheckPortalTrue[];
43
Paul Stewart75897df2011-04-27 09:05:53 -070044 enum ConnectFailure {
Paul Stewart03dba0b2011-08-22 16:32:45 -070045 kFailureUnknown,
46 kFailureActivationFailure,
47 kFailureOutOfRange,
48 kFailurePinMissing,
49 kFailureConfigurationFailed,
50 kFailureBadCredentials,
51 kFailureNeedEVDO,
52 kFailureNeedHomeNetwork,
53 kFailureOTASPFailure,
54 kFailureAAAFailure
Paul Stewart75897df2011-04-27 09:05:53 -070055 };
Chris Masone9be4a9d2011-05-16 15:44:09 -070056 enum ConnectState {
Paul Stewart03dba0b2011-08-22 16:32:45 -070057 kStateUnknown,
58 kStateIdle,
59 kStateAssociating,
60 kStateConfiguring,
61 kStateConnected,
62 kStateDisconnected,
63 kStateFailure
Chris Masone9be4a9d2011-05-16 15:44:09 -070064 };
Chris Masoneb2e326b2011-07-12 13:28:51 -070065 struct EapCredentials {
66 EapCredentials() : use_system_cas(false) {}
67 std::string identity;
68 std::string eap;
69 std::string inner_eap;
70 std::string anonymous_identity;
71 std::string client_cert;
72 std::string cert_id;
73 std::string private_key;
74 std::string private_key_password;
75 std::string key_id;
76 std::string ca_cert;
77 std::string ca_cert_id;
78 bool use_system_cas;
79 std::string pin;
80 std::string password;
81 std::string key_management;
82 };
Chris Masone9be4a9d2011-05-16 15:44:09 -070083
Paul Stewartac4ac002011-08-26 12:04:26 -070084 static const int kPriorityNone;
85
Chris Masone9be4a9d2011-05-16 15:44:09 -070086 // A constructor for the Service object
87 Service(ControlInterface *control_interface,
88 EventDispatcher *dispatcher,
mukesh agrawal51a7e932011-07-27 16:18:26 -070089 Manager *manager);
Chris Masone9be4a9d2011-05-16 15:44:09 -070090 virtual ~Service();
Darin Petkovba40dd32011-07-11 20:06:39 -070091
Darin Petkov4d6d9412011-08-24 13:19:54 -070092 virtual void Connect(Error *error) = 0;
Chris Masone9be4a9d2011-05-16 15:44:09 -070093 virtual void Disconnect() = 0;
Chris Masonea82b7112011-05-25 15:16:29 -070094
Darin Petkovb100ae72011-08-24 16:19:45 -070095 // The default implementation sets |error| to kInvalidArguments.
96 virtual void ActivateCellularModem(const std::string &carrier, Error *error);
Darin Petkovc408e692011-08-17 13:47:15 -070097
Paul Stewart03dba0b2011-08-22 16:32:45 -070098 virtual bool IsActive();
99
100 virtual ConnectState state() const { return state_; }
101 // Updates the state of the Service and alerts the manager. Also
102 // clears |failure_| if the new state isn't a failure.
103 virtual void SetState(ConnectState state);
104
105 virtual ConnectFailure failure() const { return failure_; }
106 // Records the failure mode, and sets the Service state to "Failure".
107 virtual void SetFailure(ConnectFailure failure);
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700108
Darin Petkovba40dd32011-07-11 20:06:39 -0700109 // Returns a string that is guaranteed to uniquely identify this Service
110 // instance.
Chris Masone6791a432011-07-12 13:23:19 -0700111 const std::string &UniqueName() const { return name_; }
Darin Petkovafa6fc42011-06-21 16:21:08 -0700112
Chris Masone6791a432011-07-12 13:23:19 -0700113 virtual std::string GetRpcIdentifier() const;
Chris Masone3c3f6a12011-07-01 10:01:41 -0700114
Darin Petkovba40dd32011-07-11 20:06:39 -0700115 // Returns the unique persistent storage identifier for the service.
Chris Masone34af2182011-08-22 11:59:36 -0700116 // |mac| is the associated Device's MAC address to be used in the storage id.
117 // We do this for flimflam compatibility, for now.
118 virtual std::string GetStorageIdentifier(const std::string &mac) = 0;
Darin Petkovba40dd32011-07-11 20:06:39 -0700119
120 // Loads the service from persistent |storage|. Returns true on success.
Chris Masone34af2182011-08-22 11:59:36 -0700121 virtual bool Load(StoreInterface *storage, const std::string &id_suffix);
Darin Petkovba40dd32011-07-11 20:06:39 -0700122
123 // Saves the service to persistent |storage|. Returns true on success.
Chris Masone34af2182011-08-22 11:59:36 -0700124 virtual bool Save(StoreInterface *storage, const std::string &id_suffix);
Darin Petkovba40dd32011-07-11 20:06:39 -0700125
Darin Petkovafa6fc42011-06-21 16:21:08 -0700126 bool auto_connect() const { return auto_connect_; }
127 void set_auto_connect(bool connect) { auto_connect_ = connect; }
Paul Stewart75897df2011-04-27 09:05:53 -0700128
Darin Petkov51489002011-08-18 13:13:20 -0700129 const std::string &error() const { return error_; }
130 void set_error(const std::string &error) { error_ = error; }
131
Chris Masone34af2182011-08-22 11:59:36 -0700132 // These are defined in service.cc so that we don't have to include profile.h
Chris Masone6791a432011-07-12 13:23:19 -0700133 const ProfileRefPtr &profile() const;
134 void set_profile(const ProfileRefPtr &p);
135
Chris Masone27c4aa52011-07-02 13:10:14 -0700136 PropertyStore *store() { return &store_; }
137
mukesh agrawalb54601c2011-06-07 17:39:22 -0700138 protected:
Chris Masone34af2182011-08-22 11:59:36 -0700139 // Returns true if a character is allowed to be in a service storage id.
140 static bool LegalChar(char a) { return isalnum(a) || a == '_'; }
141
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700142 virtual std::string CalculateState() = 0;
143
Chris Masone27c4aa52011-07-02 13:10:14 -0700144 void HelpRegisterDerivedBool(const std::string &name,
Chris Masone7aa5f902011-07-11 11:13:35 -0700145 bool(Service::*get)(void),
146 bool(Service::*set)(const bool&));
Chris Masone27c4aa52011-07-02 13:10:14 -0700147 void HelpRegisterDerivedString(const std::string &name,
Chris Masone7aa5f902011-07-11 11:13:35 -0700148 std::string(Service::*get)(void),
149 bool(Service::*set)(const std::string&));
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700150
Darin Petkovba40dd32011-07-11 20:06:39 -0700151 // Assigns |value| to |key| in |storage| if |value| is non-empty and |save| is
152 // true. Otherwise, removes |key| from |storage|. If |crypted| is true, the
153 // value is encrypted.
154 void SaveString(StoreInterface *storage,
Chris Masone34af2182011-08-22 11:59:36 -0700155 const std::string &id,
Darin Petkovba40dd32011-07-11 20:06:39 -0700156 const std::string &key,
157 const std::string &value,
158 bool crypted,
159 bool save);
160
Chris Masone34af2182011-08-22 11:59:36 -0700161 void LoadEapCredentials(StoreInterface *storage, const std::string &id);
162 void SaveEapCredentials(StoreInterface *storage, const std::string &id);
Darin Petkovba40dd32011-07-11 20:06:39 -0700163
Paul Stewartac4ac002011-08-26 12:04:26 -0700164 // Property accessors reserved for subclasses
165 EventDispatcher *dispatcher() const { return dispatcher_; }
166 const std::string &GetEAPKeyManagement() const;
167 void SetEAPKeyManagement(const std::string &key_management);
mukesh agrawalb54601c2011-06-07 17:39:22 -0700168
Paul Stewart75897df2011-04-27 09:05:53 -0700169 private:
Darin Petkovba40dd32011-07-11 20:06:39 -0700170 friend class ServiceAdaptorInterface;
Paul Stewart03dba0b2011-08-22 16:32:45 -0700171 FRIEND_TEST(DeviceTest, SelectedService);
Darin Petkovba40dd32011-07-11 20:06:39 -0700172 FRIEND_TEST(ServiceTest, Constructor);
173 FRIEND_TEST(ServiceTest, Save);
174 FRIEND_TEST(ServiceTest, SaveString);
175 FRIEND_TEST(ServiceTest, SaveStringCrypted);
176 FRIEND_TEST(ServiceTest, SaveStringDontSave);
177 FRIEND_TEST(ServiceTest, SaveStringEmpty);
178
179 static const char kStorageAutoConnect[];
180 static const char kStorageCheckPortal[];
181 static const char kStorageEapAnonymousIdentity[];
182 static const char kStorageEapCACert[];
183 static const char kStorageEapCACertID[];
184 static const char kStorageEapCertID[];
185 static const char kStorageEapClientCert[];
186 static const char kStorageEapEap[];
187 static const char kStorageEapIdentity[];
188 static const char kStorageEapInnerEap[];
189 static const char kStorageEapKeyID[];
190 static const char kStorageEapKeyManagement[];
191 static const char kStorageEapPIN[];
192 static const char kStorageEapPassword[];
193 static const char kStorageEapPrivateKey[];
194 static const char kStorageEapPrivateKeyPassword[];
195 static const char kStorageEapUseSystemCAs[];
196 static const char kStorageFavorite[];
197 static const char kStorageName[];
198 static const char kStoragePriority[];
199 static const char kStorageProxyConfig[];
200 static const char kStorageSaveCredentials[];
201
Chris Masone95207da2011-06-29 16:50:49 -0700202 virtual std::string GetDeviceRpcId() = 0;
203
204 std::string GetProfileRpcId() {
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700205 return ""; // Will need to call Profile to get this.
206 }
207
Paul Stewartac4ac002011-08-26 12:04:26 -0700208 ConnectState state_;
209 ConnectFailure failure_;
210 bool auto_connect_;
211 std::string check_portal_;
212 bool connectable_;
213 std::string error_;
214 bool favorite_;
215 int32 priority_;
216 std::string proxy_config_;
217 bool save_credentials_;
218 EapCredentials eap_; // Only saved if |save_credentials_| is true.
219
220 ProfileRefPtr profile_;
221 PropertyStore store_;
222
223 EventDispatcher *dispatcher_;
mukesh agrawal51a7e932011-07-27 16:18:26 -0700224 static unsigned int serial_number_;
Chris Masonea82b7112011-05-25 15:16:29 -0700225 const std::string name_;
Paul Stewart75897df2011-04-27 09:05:53 -0700226 bool available_;
227 bool configured_;
Paul Stewart75897df2011-04-27 09:05:53 -0700228 Configuration *configuration_;
229 Connection *connection_;
Paul Stewartba41b992011-05-26 07:02:46 -0700230 scoped_ptr<ServiceAdaptorInterface> adaptor_;
Chris Masone6791a432011-07-12 13:23:19 -0700231 Manager *manager_;
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700232
mukesh agrawalb54601c2011-06-07 17:39:22 -0700233 DISALLOW_COPY_AND_ASSIGN(Service);
Paul Stewart75897df2011-04-27 09:05:53 -0700234};
235
236} // namespace shill
237
238#endif // SHILL_SERVICE_