blob: 4717cbbebcbe72d0e203a17f090e3bdd822e91c9 [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 {
45 kServiceFailureUnknown,
46 kServiceFailureActivationFailure,
47 kServiceFailureOutOfRange,
48 kServiceFailurePinMissing,
49 kServiceFailureConfigurationFailed,
50 kServiceFailureBadCredentials,
51 kServiceFailureNeedEVDO,
52 kServiceFailureNeedHomeNetwork,
53 kServiceFailureOTASPFailure,
54 kServiceFailureAAAFailure
55 };
Chris Masone9be4a9d2011-05-16 15:44:09 -070056 enum ConnectState {
57 kServiceStateUnknown,
58 kServiceStateIdle,
59 kServiceStateAssociating,
60 kServiceStateConfiguring,
61 kServiceStateConnected,
62 kServiceStateDisconnected,
63 kServiceStateFailure
64 };
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
84 // A constructor for the Service object
85 Service(ControlInterface *control_interface,
86 EventDispatcher *dispatcher,
mukesh agrawal51a7e932011-07-27 16:18:26 -070087 Manager *manager);
Chris Masone9be4a9d2011-05-16 15:44:09 -070088 virtual ~Service();
Darin Petkovba40dd32011-07-11 20:06:39 -070089
Chris Masone9be4a9d2011-05-16 15:44:09 -070090 virtual void Connect() = 0;
91 virtual void Disconnect() = 0;
Chris Masonea82b7112011-05-25 15:16:29 -070092
Darin Petkovc408e692011-08-17 13:47:15 -070093 // The default implementation asserts.
94 virtual void ActivateCellularModem(const std::string &carrier);
95
Chris Masone3bd3c8c2011-06-13 08:20:26 -070096 virtual bool IsActive() { return false; }
97
Darin Petkovba40dd32011-07-11 20:06:39 -070098 // Returns a string that is guaranteed to uniquely identify this Service
99 // instance.
Chris Masone6791a432011-07-12 13:23:19 -0700100 const std::string &UniqueName() const { return name_; }
Darin Petkovafa6fc42011-06-21 16:21:08 -0700101
Chris Masone6791a432011-07-12 13:23:19 -0700102 virtual std::string GetRpcIdentifier() const;
Chris Masone3c3f6a12011-07-01 10:01:41 -0700103
Darin Petkovba40dd32011-07-11 20:06:39 -0700104 // Returns the unique persistent storage identifier for the service.
105 std::string GetStorageIdentifier();
106
107 // Loads the service from persistent |storage|. Returns true on success.
108 virtual bool Load(StoreInterface *storage);
109
110 // Saves the service to persistent |storage|. Returns true on success.
111 virtual bool Save(StoreInterface *storage);
112
Darin Petkovafa6fc42011-06-21 16:21:08 -0700113 bool auto_connect() const { return auto_connect_; }
114 void set_auto_connect(bool connect) { auto_connect_ = connect; }
Paul Stewart75897df2011-04-27 09:05:53 -0700115
Darin Petkov51489002011-08-18 13:13:20 -0700116 const std::string &error() const { return error_; }
117 void set_error(const std::string &error) { error_ = error; }
118
Chris Masone6791a432011-07-12 13:23:19 -0700119 const ProfileRefPtr &profile() const;
120 void set_profile(const ProfileRefPtr &p);
121
Chris Masone27c4aa52011-07-02 13:10:14 -0700122 PropertyStore *store() { return &store_; }
123
mukesh agrawalb54601c2011-06-07 17:39:22 -0700124 protected:
Darin Petkovba40dd32011-07-11 20:06:39 -0700125 static const int kPriorityNone = 0;
126
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700127 virtual std::string CalculateState() = 0;
128
Chris Masone27c4aa52011-07-02 13:10:14 -0700129 void HelpRegisterDerivedBool(const std::string &name,
Chris Masone7aa5f902011-07-11 11:13:35 -0700130 bool(Service::*get)(void),
131 bool(Service::*set)(const bool&));
Chris Masone27c4aa52011-07-02 13:10:14 -0700132 void HelpRegisterDerivedString(const std::string &name,
Chris Masone7aa5f902011-07-11 11:13:35 -0700133 std::string(Service::*get)(void),
134 bool(Service::*set)(const std::string&));
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700135
Darin Petkovba40dd32011-07-11 20:06:39 -0700136 // Assigns |value| to |key| in |storage| if |value| is non-empty and |save| is
137 // true. Otherwise, removes |key| from |storage|. If |crypted| is true, the
138 // value is encrypted.
139 void SaveString(StoreInterface *storage,
140 const std::string &key,
141 const std::string &value,
142 bool crypted,
143 bool save);
144
145 void LoadEapCredentials(StoreInterface *storage);
146 void SaveEapCredentials(StoreInterface *storage);
147
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700148 // Properties
149 bool auto_connect_;
150 std::string check_portal_;
151 bool connectable_;
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700152 std::string error_;
153 bool favorite_;
154 int32 priority_;
155 std::string proxy_config_;
Chris Masoneb2e326b2011-07-12 13:28:51 -0700156 bool save_credentials_;
157 EapCredentials eap_; // Only saved if |save_credentials_| is true.
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700158
Chris Masone7aa5f902011-07-11 11:13:35 -0700159 ProfileRefPtr profile_;
Chris Masone27c4aa52011-07-02 13:10:14 -0700160 PropertyStore store_;
161
mukesh agrawalb54601c2011-06-07 17:39:22 -0700162 EventDispatcher *dispatcher_;
163
Paul Stewart75897df2011-04-27 09:05:53 -0700164 private:
Darin Petkovba40dd32011-07-11 20:06:39 -0700165 friend class ServiceAdaptorInterface;
166 FRIEND_TEST(ServiceTest, Constructor);
167 FRIEND_TEST(ServiceTest, Save);
168 FRIEND_TEST(ServiceTest, SaveString);
169 FRIEND_TEST(ServiceTest, SaveStringCrypted);
170 FRIEND_TEST(ServiceTest, SaveStringDontSave);
171 FRIEND_TEST(ServiceTest, SaveStringEmpty);
172
173 static const char kStorageAutoConnect[];
174 static const char kStorageCheckPortal[];
175 static const char kStorageEapAnonymousIdentity[];
176 static const char kStorageEapCACert[];
177 static const char kStorageEapCACertID[];
178 static const char kStorageEapCertID[];
179 static const char kStorageEapClientCert[];
180 static const char kStorageEapEap[];
181 static const char kStorageEapIdentity[];
182 static const char kStorageEapInnerEap[];
183 static const char kStorageEapKeyID[];
184 static const char kStorageEapKeyManagement[];
185 static const char kStorageEapPIN[];
186 static const char kStorageEapPassword[];
187 static const char kStorageEapPrivateKey[];
188 static const char kStorageEapPrivateKeyPassword[];
189 static const char kStorageEapUseSystemCAs[];
190 static const char kStorageFavorite[];
191 static const char kStorageName[];
192 static const char kStoragePriority[];
193 static const char kStorageProxyConfig[];
194 static const char kStorageSaveCredentials[];
195
Chris Masone95207da2011-06-29 16:50:49 -0700196 virtual std::string GetDeviceRpcId() = 0;
197
198 std::string GetProfileRpcId() {
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700199 return ""; // Will need to call Profile to get this.
200 }
201
mukesh agrawal51a7e932011-07-27 16:18:26 -0700202 static unsigned int serial_number_;
Chris Masonea82b7112011-05-25 15:16:29 -0700203 const std::string name_;
Paul Stewart75897df2011-04-27 09:05:53 -0700204 bool available_;
205 bool configured_;
Paul Stewart75897df2011-04-27 09:05:53 -0700206 Configuration *configuration_;
207 Connection *connection_;
Paul Stewartba41b992011-05-26 07:02:46 -0700208 scoped_ptr<ServiceAdaptorInterface> adaptor_;
Chris Masone6791a432011-07-12 13:23:19 -0700209 Manager *manager_;
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700210
mukesh agrawalb54601c2011-06-07 17:39:22 -0700211 DISALLOW_COPY_AND_ASSIGN(Service);
Paul Stewart75897df2011-04-27 09:05:53 -0700212};
213
214} // namespace shill
215
216#endif // SHILL_SERVICE_