blob: 63b168cdf6da6a15ce92b5c50f4b413e419ac13e [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
Chris Masone3bd3c8c2011-06-13 08:20:26 -070057 CellularService(ControlInterface *control_interface,
58 EventDispatcher *dispatcher,
Thieu Le3426c8f2012-01-11 17:35:11 -080059 Metrics *metrics,
Chris Masone6791a432011-07-12 13:23:19 -070060 Manager *manager,
mukesh agrawal51a7e932011-07-27 16:18:26 -070061 const CellularRefPtr &device);
Darin Petkovd9661952011-08-03 16:25:42 -070062 virtual ~CellularService();
63
Darin Petkovc408e692011-08-17 13:47:15 -070064 // Inherited from Service.
Thieu Le7cf36b02013-01-30 17:15:56 -080065 virtual void AutoConnect();
mukesh agrawaldc7b8442012-09-27 13:48:14 -070066 virtual void Connect(Error *error, const char *reason);
Darin Petkovfb0625e2012-01-16 13:05:56 +010067 virtual void Disconnect(Error *error);
Eric Shienbrood5de44ab2011-12-05 10:46:27 -050068 virtual void ActivateCellularModem(const std::string &carrier,
Eric Shienbrood9a245532012-03-07 14:20:39 -050069 Error *error,
70 const ResultCallback &callback);
Arman Ugurayc7b15602013-02-16 00:56:18 -080071 virtual void CompleteCellularActivation(Error *error);
Thieu Le398b1da2013-03-11 17:31:10 -070072 virtual void SetState(ConnectState new_state);
Darin Petkovc408e692011-08-17 13:47:15 -070073
Darin Petkov31332412012-01-28 01:50:02 +010074 virtual std::string GetStorageIdentifier() const;
75 void SetStorageIdentifier(const std::string &identifier);
Chris Masone34af2182011-08-22 11:59:36 -070076
Ben Chan3d6de0e2012-12-10 12:01:34 -080077 void SetActivateOverNonCellularNetwork(bool state);
78 bool activate_over_non_cellular_network() const {
79 return activate_over_non_cellular_network_;
80 }
81
Arman Ugurayc7b15602013-02-16 00:56:18 -080082 virtual void SetActivationState(const std::string &state);
Darin Petkovc408e692011-08-17 13:47:15 -070083 const std::string &activation_state() const { return activation_state_; }
Chris Masone3bd3c8c2011-06-13 08:20:26 -070084
Darin Petkov381928f2012-02-02 23:00:12 +010085 void SetOLP(const OLP &olp);
86 const OLP &olp() const { return olp_; }
Darin Petkovb27e5442011-08-16 14:36:45 -070087
Darin Petkov381928f2012-02-02 23:00:12 +010088 void SetUsageURL(const std::string &url);
Darin Petkovb27e5442011-08-16 14:36:45 -070089 const std::string &usage_url() const { return usage_url_; }
Darin Petkovb27e5442011-08-16 14:36:45 -070090
Darin Petkov9cb02682012-01-28 00:17:38 +010091 void SetServingOperator(const Cellular::Operator &oper);
Darin Petkov3335b372011-08-22 11:05:32 -070092 const Cellular::Operator &serving_operator() const;
Darin Petkov3335b372011-08-22 11:05:32 -070093
Darin Petkovb72cf402011-11-22 14:51:39 +010094 // Sets network technology to |technology| and broadcasts the property change.
95 void SetNetworkTechnology(const std::string &technology);
96 const std::string &network_technology() const { return network_technology_; }
Darin Petkovd2045802011-08-23 11:09:25 -070097
Darin Petkovb72cf402011-11-22 14:51:39 +010098 // Sets roaming state to |state| and broadcasts the property change.
99 void SetRoamingState(const std::string &state);
Darin Petkovd2045802011-08-23 11:09:25 -0700100 const std::string &roaming_state() const { return roaming_state_; }
Darin Petkovd2045802011-08-23 11:09:25 -0700101
Thieu Le7cf36b02013-01-30 17:15:56 -0800102 bool is_auto_connecting() const {
103 return is_auto_connecting_;
104 }
105
Thieu Le398b1da2013-03-11 17:31:10 -0700106 void set_enforce_out_of_credits_detection(bool state) {
107 enforce_out_of_credits_detection_ = state;
108 }
109 void SetOutOfCredits(bool state);
110
Ben Chan19f83972012-10-03 23:25:56 -0700111 // Overrides Load and Save from parent Service class. We will call
Eric Shienbrood30bc0ec2012-03-21 18:19:46 -0400112 // the parent method.
113 virtual bool Load(StoreInterface *storage);
114 virtual bool Save(StoreInterface *storage);
115
116 Stringmap *GetUserSpecifiedApn();
117 Stringmap *GetLastGoodApn();
Jason Glasgow14521872012-05-07 19:12:15 -0400118 virtual void SetLastGoodApn(const Stringmap &apn_info);
119 virtual void ClearLastGoodApn();
Eric Shienbrood30bc0ec2012-03-21 18:19:46 -0400120
Thieu Le99dc56d2013-04-01 14:22:21 -0700121 virtual void OnAfterResume();
122
Ben Chan19f83972012-10-03 23:25:56 -0700123 protected:
124 // Overrides IsAutoConnectable from parent Service class.
125 virtual bool IsAutoConnectable(const char **reason) const;
126
Paul Stewartac4ac002011-08-26 12:04:26 -0700127 private:
Arman Ugurayc9533572013-01-22 17:34:20 -0800128 friend class CellularCapabilityUniversalTest;
Darin Petkovb72cf402011-11-22 14:51:39 +0100129 friend class CellularServiceTest;
Eric Shienbrood30bc0ec2012-03-21 18:19:46 -0400130 FRIEND_TEST(CellularCapabilityGSMTest, SetupApnTryList);
131 FRIEND_TEST(CellularCapabilityTest, TryApns);
Arman Ugurayc7b15602013-02-16 00:56:18 -0800132 FRIEND_TEST(CellularCapabilityUniversalMainTest, UpdateIccidActivationState);
Arman Uguray1361c032013-02-11 17:53:39 -0800133 FRIEND_TEST(CellularCapabilityUniversalMainTest, UpdateServiceName);
134 FRIEND_TEST(CellularCapabilityUniversalMainTest, UpdateStorageIdentifier);
Darin Petkovb72cf402011-11-22 14:51:39 +0100135 FRIEND_TEST(CellularTest, Connect);
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);
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),
164 void(CellularService::*set)(const Stringmap &value, Error *error));
165
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);
169 void SetApn(const Stringmap &value, Error *error);
170 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_