Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 1 | // 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 Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 5 | #ifndef SHILL_SERVICE_ |
| 6 | #define SHILL_SERVICE_ |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 7 | |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 8 | #include <string> |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 9 | #include <map> |
| 10 | #include <vector> |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 11 | |
| 12 | #include <base/memory/ref_counted.h> |
Paul Stewart | ba41b99 | 2011-05-26 07:02:46 -0700 | [diff] [blame] | 13 | #include <base/memory/scoped_ptr.h> |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 14 | #include <gtest/gtest_prod.h> // for FRIEND_TEST |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 15 | |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 16 | #include "shill/accessor_interface.h" |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 17 | #include "shill/property_store.h" |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 18 | #include "shill/refptr_types.h" |
Chris Masone | c1e5041 | 2011-06-07 13:04:53 -0700 | [diff] [blame] | 19 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 20 | namespace shill { |
| 21 | |
| 22 | class Connection; |
| 23 | class Configuration; |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 24 | class ControlInterface; |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 25 | class Endpoint; |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 26 | class Error; |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 27 | class EventDispatcher; |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 28 | class Manager; |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 29 | class ServiceAdaptorInterface; |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 30 | class StoreInterface; |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 31 | |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 32 | // 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 Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 38 | class Service : public base::RefCounted<Service> { |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 39 | public: |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 40 | static const char kCheckPortalAuto[]; |
| 41 | static const char kCheckPortalFalse[]; |
| 42 | static const char kCheckPortalTrue[]; |
| 43 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 44 | enum ConnectFailure { |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 45 | kFailureUnknown, |
| 46 | kFailureActivationFailure, |
| 47 | kFailureOutOfRange, |
| 48 | kFailurePinMissing, |
| 49 | kFailureConfigurationFailed, |
| 50 | kFailureBadCredentials, |
| 51 | kFailureNeedEVDO, |
| 52 | kFailureNeedHomeNetwork, |
| 53 | kFailureOTASPFailure, |
| 54 | kFailureAAAFailure |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 55 | }; |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 56 | enum ConnectState { |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 57 | kStateUnknown, |
| 58 | kStateIdle, |
| 59 | kStateAssociating, |
| 60 | kStateConfiguring, |
| 61 | kStateConnected, |
| 62 | kStateDisconnected, |
| 63 | kStateFailure |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 64 | }; |
Chris Masone | b2e326b | 2011-07-12 13:28:51 -0700 | [diff] [blame] | 65 | 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 Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 83 | |
Paul Stewart | ac4ac00 | 2011-08-26 12:04:26 -0700 | [diff] [blame^] | 84 | static const int kPriorityNone; |
| 85 | |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 86 | // A constructor for the Service object |
| 87 | Service(ControlInterface *control_interface, |
| 88 | EventDispatcher *dispatcher, |
mukesh agrawal | 51a7e93 | 2011-07-27 16:18:26 -0700 | [diff] [blame] | 89 | Manager *manager); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 90 | virtual ~Service(); |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 91 | |
Darin Petkov | 4d6d941 | 2011-08-24 13:19:54 -0700 | [diff] [blame] | 92 | virtual void Connect(Error *error) = 0; |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 93 | virtual void Disconnect() = 0; |
Chris Masone | a82b711 | 2011-05-25 15:16:29 -0700 | [diff] [blame] | 94 | |
Darin Petkov | b100ae7 | 2011-08-24 16:19:45 -0700 | [diff] [blame] | 95 | // The default implementation sets |error| to kInvalidArguments. |
| 96 | virtual void ActivateCellularModem(const std::string &carrier, Error *error); |
Darin Petkov | c408e69 | 2011-08-17 13:47:15 -0700 | [diff] [blame] | 97 | |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 98 | 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 Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 108 | |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 109 | // Returns a string that is guaranteed to uniquely identify this Service |
| 110 | // instance. |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 111 | const std::string &UniqueName() const { return name_; } |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 112 | |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 113 | virtual std::string GetRpcIdentifier() const; |
Chris Masone | 3c3f6a1 | 2011-07-01 10:01:41 -0700 | [diff] [blame] | 114 | |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 115 | // Returns the unique persistent storage identifier for the service. |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 116 | // |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 Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 119 | |
| 120 | // Loads the service from persistent |storage|. Returns true on success. |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 121 | virtual bool Load(StoreInterface *storage, const std::string &id_suffix); |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 122 | |
| 123 | // Saves the service to persistent |storage|. Returns true on success. |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 124 | virtual bool Save(StoreInterface *storage, const std::string &id_suffix); |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 125 | |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 126 | bool auto_connect() const { return auto_connect_; } |
| 127 | void set_auto_connect(bool connect) { auto_connect_ = connect; } |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 128 | |
Darin Petkov | 5148900 | 2011-08-18 13:13:20 -0700 | [diff] [blame] | 129 | const std::string &error() const { return error_; } |
| 130 | void set_error(const std::string &error) { error_ = error; } |
| 131 | |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 132 | // These are defined in service.cc so that we don't have to include profile.h |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 133 | const ProfileRefPtr &profile() const; |
| 134 | void set_profile(const ProfileRefPtr &p); |
| 135 | |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 136 | PropertyStore *store() { return &store_; } |
| 137 | |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 138 | protected: |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 139 | // 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 Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 142 | virtual std::string CalculateState() = 0; |
| 143 | |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 144 | void HelpRegisterDerivedBool(const std::string &name, |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 145 | bool(Service::*get)(void), |
| 146 | bool(Service::*set)(const bool&)); |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 147 | void HelpRegisterDerivedString(const std::string &name, |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 148 | std::string(Service::*get)(void), |
| 149 | bool(Service::*set)(const std::string&)); |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 150 | |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 151 | // 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 Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 155 | const std::string &id, |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 156 | const std::string &key, |
| 157 | const std::string &value, |
| 158 | bool crypted, |
| 159 | bool save); |
| 160 | |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 161 | void LoadEapCredentials(StoreInterface *storage, const std::string &id); |
| 162 | void SaveEapCredentials(StoreInterface *storage, const std::string &id); |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 163 | |
Paul Stewart | ac4ac00 | 2011-08-26 12:04:26 -0700 | [diff] [blame^] | 164 | // 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 agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 168 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 169 | private: |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 170 | friend class ServiceAdaptorInterface; |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 171 | FRIEND_TEST(DeviceTest, SelectedService); |
Darin Petkov | ba40dd3 | 2011-07-11 20:06:39 -0700 | [diff] [blame] | 172 | 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 Masone | 95207da | 2011-06-29 16:50:49 -0700 | [diff] [blame] | 202 | virtual std::string GetDeviceRpcId() = 0; |
| 203 | |
| 204 | std::string GetProfileRpcId() { |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 205 | return ""; // Will need to call Profile to get this. |
| 206 | } |
| 207 | |
Paul Stewart | ac4ac00 | 2011-08-26 12:04:26 -0700 | [diff] [blame^] | 208 | 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 agrawal | 51a7e93 | 2011-07-27 16:18:26 -0700 | [diff] [blame] | 224 | static unsigned int serial_number_; |
Chris Masone | a82b711 | 2011-05-25 15:16:29 -0700 | [diff] [blame] | 225 | const std::string name_; |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 226 | bool available_; |
| 227 | bool configured_; |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 228 | Configuration *configuration_; |
| 229 | Connection *connection_; |
Paul Stewart | ba41b99 | 2011-05-26 07:02:46 -0700 | [diff] [blame] | 230 | scoped_ptr<ServiceAdaptorInterface> adaptor_; |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 231 | Manager *manager_; |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 232 | |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 233 | DISALLOW_COPY_AND_ASSIGN(Service); |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 234 | }; |
| 235 | |
| 236 | } // namespace shill |
| 237 | |
| 238 | #endif // SHILL_SERVICE_ |