blob: 12a733557bc9c9ef971f11b5378cd34bb115f31f [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:
Eric Shienbrood30bc0ec2012-03-21 18:19:46 -040028 static const char kStorageAPN[];
29 static const char kStorageLastGoodAPN[];
30
Darin Petkov381928f2012-02-02 23:00:12 +010031 // Online payment portal.
32 class OLP {
33 public:
34 OLP();
35 ~OLP();
36
37 void CopyFrom(const OLP &olp);
38 bool Equals(const OLP &olp) const;
39
40 const std::string &GetURL() const;
41 void SetURL(const std::string &url);
42
43 const std::string &GetMethod() const;
44 void SetMethod(const std::string &method);
45
46 const std::string &GetPostData() const;
47 void SetPostData(const std::string &post_data);
48
49 const Stringmap &ToDict() const;
50
51 private:
52 Stringmap dict_;
53
54 DISALLOW_COPY_AND_ASSIGN(OLP);
55 };
56
Prathmesh Prabhu0d36b4f2013-04-01 11:45:54 -070057 CellularService(ModemInfo *modem_info,
mukesh agrawal51a7e932011-07-27 16:18:26 -070058 const CellularRefPtr &device);
Darin Petkovd9661952011-08-03 16:25:42 -070059 virtual ~CellularService();
60
Darin Petkovc408e692011-08-17 13:47:15 -070061 // Inherited from Service.
Thieu Le7cf36b02013-01-30 17:15:56 -080062 virtual void AutoConnect();
mukesh agrawaldc7b8442012-09-27 13:48:14 -070063 virtual void Connect(Error *error, const char *reason);
Darin Petkovfb0625e2012-01-16 13:05:56 +010064 virtual void Disconnect(Error *error);
Eric Shienbrood5de44ab2011-12-05 10:46:27 -050065 virtual void ActivateCellularModem(const std::string &carrier,
Eric Shienbrood9a245532012-03-07 14:20:39 -050066 Error *error,
67 const ResultCallback &callback);
Arman Ugurayc7b15602013-02-16 00:56:18 -080068 virtual void CompleteCellularActivation(Error *error);
Thieu Le398b1da2013-03-11 17:31:10 -070069 virtual void SetState(ConnectState new_state);
Darin Petkovc408e692011-08-17 13:47:15 -070070
Darin Petkov31332412012-01-28 01:50:02 +010071 virtual std::string GetStorageIdentifier() const;
72 void SetStorageIdentifier(const std::string &identifier);
Chris Masone34af2182011-08-22 11:59:36 -070073
Ben Chan3d6de0e2012-12-10 12:01:34 -080074 void SetActivateOverNonCellularNetwork(bool state);
75 bool activate_over_non_cellular_network() const {
76 return activate_over_non_cellular_network_;
77 }
78
Arman Ugurayc7b15602013-02-16 00:56:18 -080079 virtual void SetActivationState(const std::string &state);
Darin Petkovc408e692011-08-17 13:47:15 -070080 const std::string &activation_state() const { return activation_state_; }
Chris Masone3bd3c8c2011-06-13 08:20:26 -070081
Darin Petkov381928f2012-02-02 23:00:12 +010082 void SetOLP(const OLP &olp);
83 const OLP &olp() const { return olp_; }
Darin Petkovb27e5442011-08-16 14:36:45 -070084
Darin Petkov381928f2012-02-02 23:00:12 +010085 void SetUsageURL(const std::string &url);
Darin Petkovb27e5442011-08-16 14:36:45 -070086 const std::string &usage_url() const { return usage_url_; }
Darin Petkovb27e5442011-08-16 14:36:45 -070087
Darin Petkov9cb02682012-01-28 00:17:38 +010088 void SetServingOperator(const Cellular::Operator &oper);
Darin Petkov3335b372011-08-22 11:05:32 -070089 const Cellular::Operator &serving_operator() const;
Darin Petkov3335b372011-08-22 11:05:32 -070090
Darin Petkovb72cf402011-11-22 14:51:39 +010091 // Sets network technology to |technology| and broadcasts the property change.
92 void SetNetworkTechnology(const std::string &technology);
93 const std::string &network_technology() const { return network_technology_; }
Darin Petkovd2045802011-08-23 11:09:25 -070094
Darin Petkovb72cf402011-11-22 14:51:39 +010095 // Sets roaming state to |state| and broadcasts the property change.
96 void SetRoamingState(const std::string &state);
Darin Petkovd2045802011-08-23 11:09:25 -070097 const std::string &roaming_state() const { return roaming_state_; }
Darin Petkovd2045802011-08-23 11:09:25 -070098
Thieu Le7cf36b02013-01-30 17:15:56 -080099 bool is_auto_connecting() const {
100 return is_auto_connecting_;
101 }
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);
Arman Ugurayf84a4242013-04-09 20:01:07 -0700135 FRIEND_TEST(CellularTest, OnConnectionHealthCheckerResult);
Eric Shienbroodc7073302012-04-03 14:41:44 -0400136 FRIEND_TEST(CellularServiceTest, SetApn);
137 FRIEND_TEST(CellularServiceTest, ClearApn);
138 FRIEND_TEST(CellularServiceTest, LastGoodApn);
Ben Chan19f83972012-10-03 23:25:56 -0700139 FRIEND_TEST(CellularServiceTest, IsAutoConnectable);
Thieu Le398b1da2013-03-11 17:31:10 -0700140 FRIEND_TEST(CellularServiceTest, OutOfCreditsDetected);
Thieu Le99dc56d2013-04-01 14:22:21 -0700141 FRIEND_TEST(CellularServiceTest,
142 OutOfCreditsDetectionNotSkippedAfterSlowResume);
Thieu Le3a8683d2013-04-17 13:57:24 -0700143 FRIEND_TEST(CellularServiceTest, OutOfCreditsDetectionSkippedAfterResume);
144 FRIEND_TEST(CellularServiceTest,
145 OutOfCreditsDetectionSkippedAlreadyOutOfCredits);
Thieu Le398b1da2013-03-11 17:31:10 -0700146 FRIEND_TEST(CellularServiceTest,
147 OutOfCreditsDetectionSkippedExplicitDisconnect);
148 FRIEND_TEST(CellularServiceTest, OutOfCreditsNotDetectedConnectionNotDropped);
149 FRIEND_TEST(CellularServiceTest, OutOfCreditsNotDetectedIntermittentNetwork);
150 FRIEND_TEST(CellularServiceTest, OutOfCreditsNotEnforced);
mukesh agrawalbebf1b82013-04-23 15:06:33 -0700151 FRIEND_TEST(CellularServiceTest, CustomSetterNoopChange);
Ben Chan19f83972012-10-03 23:25:56 -0700152
Christopher Wiley1582bdd2012-11-15 11:31:14 -0800153 static const char kAutoConnActivating[];
Ben Chan19f83972012-10-03 23:25:56 -0700154 static const char kAutoConnDeviceDisabled[];
Thieu Le398b1da2013-03-11 17:31:10 -0700155 static const char kAutoConnOutOfCredits[];
156 static const char kAutoConnOutOfCreditsDetectionInProgress[];
157 static const int64 kOutOfCreditsConnectionDropSeconds;
158 static const int kOutOfCreditsMaxConnectAttempts;
Thieu Le99dc56d2013-04-01 14:22:21 -0700159 static const int64 kOutOfCreditsResumeIgnoreSeconds;
Darin Petkovb72cf402011-11-22 14:51:39 +0100160
Eric Shienbrood30bc0ec2012-03-21 18:19:46 -0400161 void HelpRegisterDerivedStringmap(
162 const std::string &name,
163 Stringmap(CellularService::*get)(Error *error),
mukesh agrawalbebf1b82013-04-23 15:06:33 -0700164 bool(CellularService::*set)(const Stringmap &value, Error *error));
Eric Shienbrood30bc0ec2012-03-21 18:19:46 -0400165
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800166 virtual std::string GetDeviceRpcId(Error *error);
Paul Stewartac4ac002011-08-26 12:04:26 -0700167
Eric Shienbrood30bc0ec2012-03-21 18:19:46 -0400168 Stringmap GetApn(Error *error);
mukesh agrawalbebf1b82013-04-23 15:06:33 -0700169 bool SetApn(const Stringmap &value, Error *error);
Eric Shienbrood30bc0ec2012-03-21 18:19:46 -0400170 static void SaveApn(StoreInterface *storage,
171 const std::string &storage_group,
172 const Stringmap *apn_info,
173 const std::string &keytag);
174 static void SaveApnField(StoreInterface *storage,
175 const std::string &storage_group,
176 const Stringmap *apn_info,
177 const std::string &keytag,
178 const std::string &apntag);
179 static void LoadApn(StoreInterface *storage,
180 const std::string &storage_group,
181 const std::string &keytag,
182 Stringmap *apn_info);
183 static bool LoadApnField(StoreInterface *storage,
184 const std::string &storage_group,
185 const std::string &keytag,
186 const std::string &apntag,
187 Stringmap *apn_info);
188
Thieu Le398b1da2013-03-11 17:31:10 -0700189 void PerformOutOfCreditsDetection(ConnectState curr_state,
190 ConnectState new_state);
191 void OutOfCreditsReconnect();
192 void ResetOutOfCreditsState();
193
194 base::WeakPtrFactory<CellularService> weak_ptr_factory_;
195
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700196 // Properties
Ben Chan3d6de0e2012-12-10 12:01:34 -0800197 bool activate_over_non_cellular_network_;
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700198 std::string activation_state_;
Darin Petkov3335b372011-08-22 11:05:32 -0700199 Cellular::Operator serving_operator_;
Darin Petkovb72cf402011-11-22 14:51:39 +0100200 std::string network_technology_;
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700201 std::string roaming_state_;
Darin Petkov381928f2012-02-02 23:00:12 +0100202 OLP olp_;
Darin Petkovb27e5442011-08-16 14:36:45 -0700203 std::string usage_url_;
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700204
Eric Shienbrood30bc0ec2012-03-21 18:19:46 -0400205 Stringmap apn_info_;
206 Stringmap last_good_apn_info_;
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700207
Darin Petkov31332412012-01-28 01:50:02 +0100208 std::string storage_identifier_;
209
Chris Masone2b105542011-06-22 10:58:09 -0700210 CellularRefPtr cellular_;
Darin Petkovd9661952011-08-03 16:25:42 -0700211
Thieu Le7cf36b02013-01-30 17:15:56 -0800212 // Flag indicating that a connect request is an auto-connect request.
213 // Note: Since Connect() is asynchronous, this flag is only set during the
214 // call to Connect(). It does not remain set while the async request is
215 // in flight.
216 bool is_auto_connecting_;
217
Thieu Le398b1da2013-03-11 17:31:10 -0700218 // Flag indicating whether we need to perform out-of-credits detection.
219 bool enforce_out_of_credits_detection_;
220 // Time when the last connect request started.
221 base::Time connect_start_time_;
222 // Number of connect attempts.
223 int num_connect_attempts_;
224 // Flag indicating whether out-of-credits detection is in progress.
225 bool out_of_credits_detection_in_progress_;
226 // Flag indicating if the SIM is out-of-credits.
227 bool out_of_credits_;
Thieu Le99dc56d2013-04-01 14:22:21 -0700228 // Time when the last resume occurred.
229 base::Time resume_start_time_;
Thieu Le398b1da2013-03-11 17:31:10 -0700230
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700231 DISALLOW_COPY_AND_ASSIGN(CellularService);
232};
233
234} // namespace shill
235
Ben Chan19f83972012-10-03 23:25:56 -0700236#endif // SHILL_CELLULAR_SERVICE_H_