blob: 38c27685a0d6a69b2f4dd0a1f535d47bf0129aaa [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.
Arman Ugurayf3070622013-10-03 20:27:42 -070029 // TODO(armansito): Move this to CellularOperatorInfo.
Darin Petkov381928f2012-02-02 23:00:12 +010030 class OLP {
31 public:
32 OLP();
33 ~OLP();
34
35 void CopyFrom(const OLP &olp);
36 bool Equals(const OLP &olp) const;
37
38 const std::string &GetURL() const;
39 void SetURL(const std::string &url);
40
41 const std::string &GetMethod() const;
42 void SetMethod(const std::string &method);
43
44 const std::string &GetPostData() const;
45 void SetPostData(const std::string &post_data);
46
47 const Stringmap &ToDict() const;
48
49 private:
50 Stringmap dict_;
51
52 DISALLOW_COPY_AND_ASSIGN(OLP);
53 };
54
Prathmesh Prabhu0d36b4f2013-04-01 11:45:54 -070055 CellularService(ModemInfo *modem_info,
mukesh agrawal51a7e932011-07-27 16:18:26 -070056 const CellularRefPtr &device);
Darin Petkovd9661952011-08-03 16:25:42 -070057 virtual ~CellularService();
58
Darin Petkovc408e692011-08-17 13:47:15 -070059 // Inherited from Service.
Thieu Le7cf36b02013-01-30 17:15:56 -080060 virtual void AutoConnect();
mukesh agrawaldc7b8442012-09-27 13:48:14 -070061 virtual void Connect(Error *error, const char *reason);
Darin Petkovfb0625e2012-01-16 13:05:56 +010062 virtual void Disconnect(Error *error);
Eric Shienbrood5de44ab2011-12-05 10:46:27 -050063 virtual void ActivateCellularModem(const std::string &carrier,
Eric Shienbrood9a245532012-03-07 14:20:39 -050064 Error *error,
65 const ResultCallback &callback);
Arman Ugurayc7b15602013-02-16 00:56:18 -080066 virtual void CompleteCellularActivation(Error *error);
Thieu Le398b1da2013-03-11 17:31:10 -070067 virtual void SetState(ConnectState new_state);
Darin Petkovc408e692011-08-17 13:47:15 -070068
Darin Petkov31332412012-01-28 01:50:02 +010069 virtual std::string GetStorageIdentifier() const;
70 void SetStorageIdentifier(const std::string &identifier);
Chris Masone34af2182011-08-22 11:59:36 -070071
Ben Chan3d6de0e2012-12-10 12:01:34 -080072 void SetActivateOverNonCellularNetwork(bool state);
73 bool activate_over_non_cellular_network() const {
74 return activate_over_non_cellular_network_;
75 }
76
Arman Ugurayc7b15602013-02-16 00:56:18 -080077 virtual void SetActivationState(const std::string &state);
Darin Petkovc408e692011-08-17 13:47:15 -070078 const std::string &activation_state() const { return activation_state_; }
Chris Masone3bd3c8c2011-06-13 08:20:26 -070079
Darin Petkov381928f2012-02-02 23:00:12 +010080 void SetOLP(const OLP &olp);
81 const OLP &olp() const { return olp_; }
Darin Petkovb27e5442011-08-16 14:36:45 -070082
Darin Petkov381928f2012-02-02 23:00:12 +010083 void SetUsageURL(const std::string &url);
Darin Petkovb27e5442011-08-16 14:36:45 -070084 const std::string &usage_url() const { return usage_url_; }
Darin Petkovb27e5442011-08-16 14:36:45 -070085
Darin Petkov9cb02682012-01-28 00:17:38 +010086 void SetServingOperator(const Cellular::Operator &oper);
Darin Petkov3335b372011-08-22 11:05:32 -070087 const Cellular::Operator &serving_operator() const;
Darin Petkov3335b372011-08-22 11:05:32 -070088
Darin Petkovb72cf402011-11-22 14:51:39 +010089 // Sets network technology to |technology| and broadcasts the property change.
90 void SetNetworkTechnology(const std::string &technology);
91 const std::string &network_technology() const { return network_technology_; }
Darin Petkovd2045802011-08-23 11:09:25 -070092
Darin Petkovb72cf402011-11-22 14:51:39 +010093 // Sets roaming state to |state| and broadcasts the property change.
94 void SetRoamingState(const std::string &state);
Darin Petkovd2045802011-08-23 11:09:25 -070095 const std::string &roaming_state() const { return roaming_state_; }
Darin Petkovd2045802011-08-23 11:09:25 -070096
Thieu Le7cf36b02013-01-30 17:15:56 -080097 bool is_auto_connecting() const {
98 return is_auto_connecting_;
99 }
100
mukesh agrawal3ffe52c2013-06-20 15:21:29 -0700101 const std::string &ppp_username() const { return ppp_username_; }
102 const std::string &ppp_password() const { return ppp_password_; }
103
Thieu Le398b1da2013-03-11 17:31:10 -0700104 void set_enforce_out_of_credits_detection(bool state) {
105 enforce_out_of_credits_detection_ = state;
106 }
mukesh agrawal81599e02013-04-22 16:40:00 -0700107 bool out_of_credits() const { return out_of_credits_; }
Thieu Le398b1da2013-03-11 17:31:10 -0700108 void SetOutOfCredits(bool state);
109
Ben Chan19f83972012-10-03 23:25:56 -0700110 // Overrides Load and Save from parent Service class. We will call
Eric Shienbrood30bc0ec2012-03-21 18:19:46 -0400111 // the parent method.
112 virtual bool Load(StoreInterface *storage);
113 virtual bool Save(StoreInterface *storage);
114
115 Stringmap *GetUserSpecifiedApn();
116 Stringmap *GetLastGoodApn();
Jason Glasgow14521872012-05-07 19:12:15 -0400117 virtual void SetLastGoodApn(const Stringmap &apn_info);
118 virtual void ClearLastGoodApn();
Eric Shienbrood30bc0ec2012-03-21 18:19:46 -0400119
Thieu Le99dc56d2013-04-01 14:22:21 -0700120 virtual void OnAfterResume();
121
Ben Chan19f83972012-10-03 23:25:56 -0700122 protected:
123 // Overrides IsAutoConnectable from parent Service class.
124 virtual bool IsAutoConnectable(const char **reason) const;
125
Paul Stewartac4ac002011-08-26 12:04:26 -0700126 private:
Arman Ugurayc9533572013-01-22 17:34:20 -0800127 friend class CellularCapabilityUniversalTest;
Darin Petkovb72cf402011-11-22 14:51:39 +0100128 friend class CellularServiceTest;
Eric Shienbrood30bc0ec2012-03-21 18:19:46 -0400129 FRIEND_TEST(CellularCapabilityGSMTest, SetupApnTryList);
130 FRIEND_TEST(CellularCapabilityTest, TryApns);
Arman Uguray0a3e2792013-01-17 16:31:50 -0800131 FRIEND_TEST(CellularCapabilityUniversalMainTest,
132 UpdatePendingActivationState);
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);
mukesh agrawal3ffe52c2013-06-20 15:21:29 -0700136 FRIEND_TEST(CellularTest, GetLogin); // ppp_username_, ppp_password_
Arman Ugurayf84a4242013-04-09 20:01:07 -0700137 FRIEND_TEST(CellularTest, OnConnectionHealthCheckerResult);
Eric Shienbroodc7073302012-04-03 14:41:44 -0400138 FRIEND_TEST(CellularServiceTest, SetApn);
139 FRIEND_TEST(CellularServiceTest, ClearApn);
140 FRIEND_TEST(CellularServiceTest, LastGoodApn);
mukesh agrawala70ec272013-08-19 16:08:31 -0700141 FRIEND_TEST(CellularServiceTest, LoadResetsPPPAuthFailure);
Ben Chan19f83972012-10-03 23:25:56 -0700142 FRIEND_TEST(CellularServiceTest, IsAutoConnectable);
Thieu Le398b1da2013-03-11 17:31:10 -0700143 FRIEND_TEST(CellularServiceTest, OutOfCreditsDetected);
Thieu Le99dc56d2013-04-01 14:22:21 -0700144 FRIEND_TEST(CellularServiceTest,
145 OutOfCreditsDetectionNotSkippedAfterSlowResume);
Thieu Le3a8683d2013-04-17 13:57:24 -0700146 FRIEND_TEST(CellularServiceTest, OutOfCreditsDetectionSkippedAfterResume);
147 FRIEND_TEST(CellularServiceTest,
148 OutOfCreditsDetectionSkippedAlreadyOutOfCredits);
Thieu Le398b1da2013-03-11 17:31:10 -0700149 FRIEND_TEST(CellularServiceTest,
150 OutOfCreditsDetectionSkippedExplicitDisconnect);
151 FRIEND_TEST(CellularServiceTest, OutOfCreditsNotDetectedConnectionNotDropped);
152 FRIEND_TEST(CellularServiceTest, OutOfCreditsNotDetectedIntermittentNetwork);
153 FRIEND_TEST(CellularServiceTest, OutOfCreditsNotEnforced);
mukesh agrawalbebf1b82013-04-23 15:06:33 -0700154 FRIEND_TEST(CellularServiceTest, CustomSetterNoopChange);
Ben Chan19f83972012-10-03 23:25:56 -0700155
Christopher Wiley1582bdd2012-11-15 11:31:14 -0800156 static const char kAutoConnActivating[];
mukesh agrawald4773882013-08-14 17:21:14 -0700157 static const char kAutoConnBadPPPCredentials[];
Ben Chan19f83972012-10-03 23:25:56 -0700158 static const char kAutoConnDeviceDisabled[];
Thieu Le398b1da2013-03-11 17:31:10 -0700159 static const char kAutoConnOutOfCredits[];
160 static const char kAutoConnOutOfCreditsDetectionInProgress[];
161 static const int64 kOutOfCreditsConnectionDropSeconds;
162 static const int kOutOfCreditsMaxConnectAttempts;
Thieu Le99dc56d2013-04-01 14:22:21 -0700163 static const int64 kOutOfCreditsResumeIgnoreSeconds;
mukesh agrawala70ec272013-08-19 16:08:31 -0700164 static const char kStoragePPPUsername[];
165 static const char kStoragePPPPassword[];
Darin Petkovb72cf402011-11-22 14:51:39 +0100166
Eric Shienbrood30bc0ec2012-03-21 18:19:46 -0400167 void HelpRegisterDerivedStringmap(
168 const std::string &name,
169 Stringmap(CellularService::*get)(Error *error),
mukesh agrawalbebf1b82013-04-23 15:06:33 -0700170 bool(CellularService::*set)(const Stringmap &value, Error *error));
Eric Shienbrood30bc0ec2012-03-21 18:19:46 -0400171
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800172 virtual std::string GetDeviceRpcId(Error *error);
Paul Stewartac4ac002011-08-26 12:04:26 -0700173
Eric Shienbrood30bc0ec2012-03-21 18:19:46 -0400174 Stringmap GetApn(Error *error);
mukesh agrawalbebf1b82013-04-23 15:06:33 -0700175 bool SetApn(const Stringmap &value, Error *error);
Eric Shienbrood30bc0ec2012-03-21 18:19:46 -0400176 static void SaveApn(StoreInterface *storage,
177 const std::string &storage_group,
178 const Stringmap *apn_info,
179 const std::string &keytag);
180 static void SaveApnField(StoreInterface *storage,
181 const std::string &storage_group,
182 const Stringmap *apn_info,
183 const std::string &keytag,
184 const std::string &apntag);
185 static void LoadApn(StoreInterface *storage,
186 const std::string &storage_group,
187 const std::string &keytag,
188 Stringmap *apn_info);
189 static bool LoadApnField(StoreInterface *storage,
190 const std::string &storage_group,
191 const std::string &keytag,
192 const std::string &apntag,
193 Stringmap *apn_info);
194
Thieu Le398b1da2013-03-11 17:31:10 -0700195 void PerformOutOfCreditsDetection(ConnectState curr_state,
196 ConnectState new_state);
197 void OutOfCreditsReconnect();
198 void ResetOutOfCreditsState();
199
200 base::WeakPtrFactory<CellularService> weak_ptr_factory_;
201
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700202 // Properties
Ben Chan3d6de0e2012-12-10 12:01:34 -0800203 bool activate_over_non_cellular_network_;
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700204 std::string activation_state_;
Darin Petkov3335b372011-08-22 11:05:32 -0700205 Cellular::Operator serving_operator_;
Darin Petkovb72cf402011-11-22 14:51:39 +0100206 std::string network_technology_;
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700207 std::string roaming_state_;
Darin Petkov381928f2012-02-02 23:00:12 +0100208 OLP olp_;
Darin Petkovb27e5442011-08-16 14:36:45 -0700209 std::string usage_url_;
Eric Shienbrood30bc0ec2012-03-21 18:19:46 -0400210 Stringmap apn_info_;
211 Stringmap last_good_apn_info_;
mukesh agrawal3ffe52c2013-06-20 15:21:29 -0700212 std::string ppp_username_;
213 std::string ppp_password_;
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700214
Darin Petkov31332412012-01-28 01:50:02 +0100215 std::string storage_identifier_;
216
Chris Masone2b105542011-06-22 10:58:09 -0700217 CellularRefPtr cellular_;
Darin Petkovd9661952011-08-03 16:25:42 -0700218
Thieu Le7cf36b02013-01-30 17:15:56 -0800219 // Flag indicating that a connect request is an auto-connect request.
220 // Note: Since Connect() is asynchronous, this flag is only set during the
221 // call to Connect(). It does not remain set while the async request is
222 // in flight.
223 bool is_auto_connecting_;
224
Thieu Le398b1da2013-03-11 17:31:10 -0700225 // Flag indicating whether we need to perform out-of-credits detection.
226 bool enforce_out_of_credits_detection_;
227 // Time when the last connect request started.
228 base::Time connect_start_time_;
229 // Number of connect attempts.
230 int num_connect_attempts_;
231 // Flag indicating whether out-of-credits detection is in progress.
232 bool out_of_credits_detection_in_progress_;
233 // Flag indicating if the SIM is out-of-credits.
234 bool out_of_credits_;
Thieu Le99dc56d2013-04-01 14:22:21 -0700235 // Time when the last resume occurred.
236 base::Time resume_start_time_;
Thieu Le398b1da2013-03-11 17:31:10 -0700237
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700238 DISALLOW_COPY_AND_ASSIGN(CellularService);
239};
240
241} // namespace shill
242
Ben Chan19f83972012-10-03 23:25:56 -0700243#endif // SHILL_CELLULAR_SERVICE_H_