blob: 05f52fdc32c88ffd37866adc5c5c5656605644d7 [file] [log] [blame]
Darin Petkovb9c99332012-01-12 13:13:00 +01001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Chris Masone3bd3c8c2011-06-13 08:20:26 -07002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Ben Chan19f83972012-10-03 23:25:56 -07005#ifndef SHILL_CELLULAR_SERVICE_H_
6#define SHILL_CELLULAR_SERVICE_H_
Chris Masone3bd3c8c2011-06-13 08:20:26 -07007
8#include <map>
9#include <string>
10
11#include <base/basictypes.h>
Thieu Le398b1da2013-03-11 17:31:10 -070012#include <base/time.h>
Darin Petkovb72cf402011-11-22 14:51:39 +010013#include <gtest/gtest_prod.h> // for FRIEND_TEST
Chris Masone3bd3c8c2011-06-13 08:20:26 -070014
Darin Petkov3335b372011-08-22 11:05:32 -070015#include "shill/cellular.h"
Chris Masone2b105542011-06-22 10:58:09 -070016#include "shill/refptr_types.h"
Chris Masone3bd3c8c2011-06-13 08:20:26 -070017#include "shill/service.h"
18
19namespace shill {
20
Chris Masone6791a432011-07-12 13:23:19 -070021class ControlInterface;
Darin Petkovb100ae72011-08-24 16:19:45 -070022class Error;
Chris Masone6791a432011-07-12 13:23:19 -070023class EventDispatcher;
24class Manager;
25
Chris Masone3bd3c8c2011-06-13 08:20:26 -070026class CellularService : public Service {
27 public:
Darin Petkov381928f2012-02-02 23:00:12 +010028 // Online payment portal.
29 class OLP {
30 public:
31 OLP();
32 ~OLP();
33
34 void CopyFrom(const OLP &olp);
35 bool Equals(const OLP &olp) const;
36
37 const std::string &GetURL() const;
38 void SetURL(const std::string &url);
39
40 const std::string &GetMethod() const;
41 void SetMethod(const std::string &method);
42
43 const std::string &GetPostData() const;
44 void SetPostData(const std::string &post_data);
45
46 const Stringmap &ToDict() const;
47
48 private:
49 Stringmap dict_;
50
51 DISALLOW_COPY_AND_ASSIGN(OLP);
52 };
53
Prathmesh Prabhu0d36b4f2013-04-01 11:45:54 -070054 CellularService(ModemInfo *modem_info,
mukesh agrawal51a7e932011-07-27 16:18:26 -070055 const CellularRefPtr &device);
Darin Petkovd9661952011-08-03 16:25:42 -070056 virtual ~CellularService();
57
Darin Petkovc408e692011-08-17 13:47:15 -070058 // Inherited from Service.
Thieu Le7cf36b02013-01-30 17:15:56 -080059 virtual void AutoConnect();
mukesh agrawaldc7b8442012-09-27 13:48:14 -070060 virtual void Connect(Error *error, const char *reason);
Darin Petkovfb0625e2012-01-16 13:05:56 +010061 virtual void Disconnect(Error *error);
Eric Shienbrood5de44ab2011-12-05 10:46:27 -050062 virtual void ActivateCellularModem(const std::string &carrier,
Eric Shienbrood9a245532012-03-07 14:20:39 -050063 Error *error,
64 const ResultCallback &callback);
Arman Ugurayc7b15602013-02-16 00:56:18 -080065 virtual void CompleteCellularActivation(Error *error);
Thieu Le398b1da2013-03-11 17:31:10 -070066 virtual void SetState(ConnectState new_state);
Darin Petkovc408e692011-08-17 13:47:15 -070067
Darin Petkov31332412012-01-28 01:50:02 +010068 virtual std::string GetStorageIdentifier() const;
69 void SetStorageIdentifier(const std::string &identifier);
Chris Masone34af2182011-08-22 11:59:36 -070070
Ben Chan3d6de0e2012-12-10 12:01:34 -080071 void SetActivateOverNonCellularNetwork(bool state);
72 bool activate_over_non_cellular_network() const {
73 return activate_over_non_cellular_network_;
74 }
75
Arman Ugurayc7b15602013-02-16 00:56:18 -080076 virtual void SetActivationState(const std::string &state);
Darin Petkovc408e692011-08-17 13:47:15 -070077 const std::string &activation_state() const { return activation_state_; }
Chris Masone3bd3c8c2011-06-13 08:20:26 -070078
Darin Petkov381928f2012-02-02 23:00:12 +010079 void SetOLP(const OLP &olp);
80 const OLP &olp() const { return olp_; }
Darin Petkovb27e5442011-08-16 14:36:45 -070081
Darin Petkov381928f2012-02-02 23:00:12 +010082 void SetUsageURL(const std::string &url);
Darin Petkovb27e5442011-08-16 14:36:45 -070083 const std::string &usage_url() const { return usage_url_; }
Darin Petkovb27e5442011-08-16 14:36:45 -070084
Darin Petkov9cb02682012-01-28 00:17:38 +010085 void SetServingOperator(const Cellular::Operator &oper);
Darin Petkov3335b372011-08-22 11:05:32 -070086 const Cellular::Operator &serving_operator() const;
Darin Petkov3335b372011-08-22 11:05:32 -070087
Darin Petkovb72cf402011-11-22 14:51:39 +010088 // Sets network technology to |technology| and broadcasts the property change.
89 void SetNetworkTechnology(const std::string &technology);
90 const std::string &network_technology() const { return network_technology_; }
Darin Petkovd2045802011-08-23 11:09:25 -070091
Darin Petkovb72cf402011-11-22 14:51:39 +010092 // Sets roaming state to |state| and broadcasts the property change.
93 void SetRoamingState(const std::string &state);
Darin Petkovd2045802011-08-23 11:09:25 -070094 const std::string &roaming_state() const { return roaming_state_; }
Darin Petkovd2045802011-08-23 11:09:25 -070095
Thieu Le7cf36b02013-01-30 17:15:56 -080096 bool is_auto_connecting() const {
97 return is_auto_connecting_;
98 }
99
mukesh agrawal3ffe52c2013-06-20 15:21:29 -0700100 const std::string &ppp_username() const { return ppp_username_; }
101 const std::string &ppp_password() const { return ppp_password_; }
102
Thieu Le398b1da2013-03-11 17:31:10 -0700103 void set_enforce_out_of_credits_detection(bool state) {
104 enforce_out_of_credits_detection_ = state;
105 }
mukesh agrawal81599e02013-04-22 16:40:00 -0700106 bool out_of_credits() const { return out_of_credits_; }
Thieu Le398b1da2013-03-11 17:31:10 -0700107 void SetOutOfCredits(bool state);
108
Ben Chan19f83972012-10-03 23:25:56 -0700109 // Overrides Load and Save from parent Service class. We will call
Eric Shienbrood30bc0ec2012-03-21 18:19:46 -0400110 // the parent method.
111 virtual bool Load(StoreInterface *storage);
112 virtual bool Save(StoreInterface *storage);
113
114 Stringmap *GetUserSpecifiedApn();
115 Stringmap *GetLastGoodApn();
Jason Glasgow14521872012-05-07 19:12:15 -0400116 virtual void SetLastGoodApn(const Stringmap &apn_info);
117 virtual void ClearLastGoodApn();
Eric Shienbrood30bc0ec2012-03-21 18:19:46 -0400118
Thieu Le99dc56d2013-04-01 14:22:21 -0700119 virtual void OnAfterResume();
120
Ben Chan19f83972012-10-03 23:25:56 -0700121 protected:
122 // Overrides IsAutoConnectable from parent Service class.
123 virtual bool IsAutoConnectable(const char **reason) const;
124
Paul Stewartac4ac002011-08-26 12:04:26 -0700125 private:
Arman Ugurayc9533572013-01-22 17:34:20 -0800126 friend class CellularCapabilityUniversalTest;
Darin Petkovb72cf402011-11-22 14:51:39 +0100127 friend class CellularServiceTest;
Eric Shienbrood30bc0ec2012-03-21 18:19:46 -0400128 FRIEND_TEST(CellularCapabilityGSMTest, SetupApnTryList);
129 FRIEND_TEST(CellularCapabilityTest, TryApns);
Arman Uguray0a3e2792013-01-17 16:31:50 -0800130 FRIEND_TEST(CellularCapabilityUniversalMainTest,
131 UpdatePendingActivationState);
Arman Uguray1361c032013-02-11 17:53:39 -0800132 FRIEND_TEST(CellularCapabilityUniversalMainTest, UpdateServiceName);
133 FRIEND_TEST(CellularCapabilityUniversalMainTest, UpdateStorageIdentifier);
Darin Petkovb72cf402011-11-22 14:51:39 +0100134 FRIEND_TEST(CellularTest, Connect);
mukesh agrawal3ffe52c2013-06-20 15:21:29 -0700135 FRIEND_TEST(CellularTest, GetLogin); // ppp_username_, ppp_password_
Arman Ugurayf84a4242013-04-09 20:01:07 -0700136 FRIEND_TEST(CellularTest, OnConnectionHealthCheckerResult);
Eric Shienbroodc7073302012-04-03 14:41:44 -0400137 FRIEND_TEST(CellularServiceTest, SetApn);
138 FRIEND_TEST(CellularServiceTest, ClearApn);
139 FRIEND_TEST(CellularServiceTest, LastGoodApn);
Ben Chan19f83972012-10-03 23:25:56 -0700140 FRIEND_TEST(CellularServiceTest, IsAutoConnectable);
Thieu Le398b1da2013-03-11 17:31:10 -0700141 FRIEND_TEST(CellularServiceTest, OutOfCreditsDetected);
Thieu Le99dc56d2013-04-01 14:22:21 -0700142 FRIEND_TEST(CellularServiceTest,
143 OutOfCreditsDetectionNotSkippedAfterSlowResume);
Thieu Le3a8683d2013-04-17 13:57:24 -0700144 FRIEND_TEST(CellularServiceTest, OutOfCreditsDetectionSkippedAfterResume);
145 FRIEND_TEST(CellularServiceTest,
146 OutOfCreditsDetectionSkippedAlreadyOutOfCredits);
Thieu Le398b1da2013-03-11 17:31:10 -0700147 FRIEND_TEST(CellularServiceTest,
148 OutOfCreditsDetectionSkippedExplicitDisconnect);
149 FRIEND_TEST(CellularServiceTest, OutOfCreditsNotDetectedConnectionNotDropped);
150 FRIEND_TEST(CellularServiceTest, OutOfCreditsNotDetectedIntermittentNetwork);
151 FRIEND_TEST(CellularServiceTest, OutOfCreditsNotEnforced);
mukesh agrawalbebf1b82013-04-23 15:06:33 -0700152 FRIEND_TEST(CellularServiceTest, CustomSetterNoopChange);
Ben Chan19f83972012-10-03 23:25:56 -0700153
Christopher Wiley1582bdd2012-11-15 11:31:14 -0800154 static const char kAutoConnActivating[];
Ben Chan19f83972012-10-03 23:25:56 -0700155 static const char kAutoConnDeviceDisabled[];
Thieu Le398b1da2013-03-11 17:31:10 -0700156 static const char kAutoConnOutOfCredits[];
157 static const char kAutoConnOutOfCreditsDetectionInProgress[];
158 static const int64 kOutOfCreditsConnectionDropSeconds;
159 static const int kOutOfCreditsMaxConnectAttempts;
Thieu Le99dc56d2013-04-01 14:22:21 -0700160 static const int64 kOutOfCreditsResumeIgnoreSeconds;
Darin Petkovb72cf402011-11-22 14:51:39 +0100161
Eric Shienbrood30bc0ec2012-03-21 18:19:46 -0400162 void HelpRegisterDerivedStringmap(
163 const std::string &name,
164 Stringmap(CellularService::*get)(Error *error),
mukesh agrawalbebf1b82013-04-23 15:06:33 -0700165 bool(CellularService::*set)(const Stringmap &value, Error *error));
Eric Shienbrood30bc0ec2012-03-21 18:19:46 -0400166
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800167 virtual std::string GetDeviceRpcId(Error *error);
Paul Stewartac4ac002011-08-26 12:04:26 -0700168
Eric Shienbrood30bc0ec2012-03-21 18:19:46 -0400169 Stringmap GetApn(Error *error);
mukesh agrawalbebf1b82013-04-23 15:06:33 -0700170 bool SetApn(const Stringmap &value, Error *error);
Eric Shienbrood30bc0ec2012-03-21 18:19:46 -0400171 static void SaveApn(StoreInterface *storage,
172 const std::string &storage_group,
173 const Stringmap *apn_info,
174 const std::string &keytag);
175 static void SaveApnField(StoreInterface *storage,
176 const std::string &storage_group,
177 const Stringmap *apn_info,
178 const std::string &keytag,
179 const std::string &apntag);
180 static void LoadApn(StoreInterface *storage,
181 const std::string &storage_group,
182 const std::string &keytag,
183 Stringmap *apn_info);
184 static bool LoadApnField(StoreInterface *storage,
185 const std::string &storage_group,
186 const std::string &keytag,
187 const std::string &apntag,
188 Stringmap *apn_info);
189
Thieu Le398b1da2013-03-11 17:31:10 -0700190 void PerformOutOfCreditsDetection(ConnectState curr_state,
191 ConnectState new_state);
192 void OutOfCreditsReconnect();
193 void ResetOutOfCreditsState();
194
195 base::WeakPtrFactory<CellularService> weak_ptr_factory_;
196
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700197 // Properties
Ben Chan3d6de0e2012-12-10 12:01:34 -0800198 bool activate_over_non_cellular_network_;
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700199 std::string activation_state_;
Darin Petkov3335b372011-08-22 11:05:32 -0700200 Cellular::Operator serving_operator_;
Darin Petkovb72cf402011-11-22 14:51:39 +0100201 std::string network_technology_;
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700202 std::string roaming_state_;
Darin Petkov381928f2012-02-02 23:00:12 +0100203 OLP olp_;
Darin Petkovb27e5442011-08-16 14:36:45 -0700204 std::string usage_url_;
Eric Shienbrood30bc0ec2012-03-21 18:19:46 -0400205 Stringmap apn_info_;
206 Stringmap last_good_apn_info_;
mukesh agrawal3ffe52c2013-06-20 15:21:29 -0700207 std::string ppp_username_;
208 std::string ppp_password_;
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700209
Darin Petkov31332412012-01-28 01:50:02 +0100210 std::string storage_identifier_;
211
Chris Masone2b105542011-06-22 10:58:09 -0700212 CellularRefPtr cellular_;
Darin Petkovd9661952011-08-03 16:25:42 -0700213
Thieu Le7cf36b02013-01-30 17:15:56 -0800214 // Flag indicating that a connect request is an auto-connect request.
215 // Note: Since Connect() is asynchronous, this flag is only set during the
216 // call to Connect(). It does not remain set while the async request is
217 // in flight.
218 bool is_auto_connecting_;
219
Thieu Le398b1da2013-03-11 17:31:10 -0700220 // Flag indicating whether we need to perform out-of-credits detection.
221 bool enforce_out_of_credits_detection_;
222 // Time when the last connect request started.
223 base::Time connect_start_time_;
224 // Number of connect attempts.
225 int num_connect_attempts_;
226 // Flag indicating whether out-of-credits detection is in progress.
227 bool out_of_credits_detection_in_progress_;
228 // Flag indicating if the SIM is out-of-credits.
229 bool out_of_credits_;
Thieu Le99dc56d2013-04-01 14:22:21 -0700230 // Time when the last resume occurred.
231 base::Time resume_start_time_;
Thieu Le398b1da2013-03-11 17:31:10 -0700232
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700233 DISALLOW_COPY_AND_ASSIGN(CellularService);
234};
235
236} // namespace shill
237
Ben Chan19f83972012-10-03 23:25:56 -0700238#endif // SHILL_CELLULAR_SERVICE_H_