1 //
2 // Copyright (C) 2012 The Android Open Source Project
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //      http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 
17 #ifndef SHILL_CELLULAR_CELLULAR_SERVICE_H_
18 #define SHILL_CELLULAR_CELLULAR_SERVICE_H_
19 
20 #include <map>
21 #include <memory>
22 #include <string>
23 
24 #include <base/macros.h>
25 #include <base/time/time.h>
26 #include <gtest/gtest_prod.h>  // for FRIEND_TEST
27 
28 #include "shill/cellular/cellular.h"
29 #include "shill/cellular/out_of_credits_detector.h"
30 #include "shill/refptr_types.h"
31 #include "shill/service.h"
32 
33 namespace shill {
34 
35 class ControlInterface;
36 class Error;
37 class EventDispatcher;
38 class Manager;
39 class OutOfCreditsDetector;
40 
41 class CellularService : public Service {
42  public:
43   enum ActivationType {
44     kActivationTypeNonCellular,  // For future use
45     kActivationTypeOMADM,  // For future use
46     kActivationTypeOTA,
47     kActivationTypeOTASP,
48     kActivationTypeUnknown
49   };
50 
51   CellularService(ModemInfo* modem_info,
52                   const CellularRefPtr& device);
53   ~CellularService() override;
54 
55   // Inherited from Service.
56   void AutoConnect() override;
57   void Connect(Error* error, const char* reason) override;
58   void Disconnect(Error* error, const char* reason) override;
59   void ActivateCellularModem(const std::string& carrier,
60                              Error* error,
61                              const ResultCallback& callback) override;
62   void CompleteCellularActivation(Error* error) override;
63   void SetState(ConnectState new_state) override;
64 
65   std::string GetStorageIdentifier() const override;
66   void SetStorageIdentifier(const std::string& identifier);
67 
cellular()68   const CellularRefPtr& cellular() const { return cellular_; }
69 
70   void SetActivationType(ActivationType type);
71   std::string GetActivationTypeString() const;
72 
73   virtual void SetActivationState(const std::string& state);
activation_state()74   virtual const std::string& activation_state() const {
75       return activation_state_;
76   }
77 
78   void SetOLP(const std::string& url,
79               const std::string& method,
80               const std::string& post_data);
olp()81   const Stringmap& olp() const { return olp_; }
82 
83   void SetUsageURL(const std::string& url);
usage_url()84   const std::string& usage_url() const { return usage_url_; }
85 
86   void set_serving_operator(const Stringmap& serving_operator);
serving_operator()87   const Stringmap& serving_operator() const { return serving_operator_; }
88 
89   // Sets network technology to |technology| and broadcasts the property change.
90   void SetNetworkTechnology(const std::string& technology);
network_technology()91   const std::string& network_technology() const { return network_technology_; }
92 
93   // Sets roaming state to |state| and broadcasts the property change.
94   void SetRoamingState(const std::string& state);
roaming_state()95   const std::string& roaming_state() const { return roaming_state_; }
96 
is_auto_connecting()97   bool is_auto_connecting() const {
98     return is_auto_connecting_;
99   }
100 
ppp_username()101   const std::string& ppp_username() const { return ppp_username_; }
ppp_password()102   const std::string& ppp_password() const { return ppp_password_; }
103 
resume_start_time()104   virtual const base::Time& resume_start_time() const {
105     return resume_start_time_;
106   }
107 
out_of_credits_detector()108   OutOfCreditsDetector* out_of_credits_detector() {
109     return out_of_credits_detector_.get();
110   }
111   void SignalOutOfCreditsChanged(bool state) const;
112 
113   // Overrides Load and Save from parent Service class.  We will call
114   // the parent method.
115   bool Load(StoreInterface* storage) override;
116   bool Save(StoreInterface* storage) override;
117 
118   Stringmap* GetUserSpecifiedApn();
119   Stringmap* GetLastGoodApn();
120   virtual void SetLastGoodApn(const Stringmap& apn_info);
121   virtual void ClearLastGoodApn();
122 
123   void OnAfterResume() override;
124 
125   // Initialize out-of-credits detection.
126   void InitOutOfCreditsDetection(OutOfCreditsDetector::OOCType ooc_type);
127 
128  protected:
129   // Overrides IsAutoConnectable from parent Service class.
130   bool IsAutoConnectable(const char** reason) const override;
131 
132  private:
133   friend class CellularCapabilityUniversalTest;
134   friend class CellularServiceTest;
135   FRIEND_TEST(CellularCapabilityGSMTest, SetupApnTryList);
136   FRIEND_TEST(CellularCapabilityTest, TryApns);
137   FRIEND_TEST(CellularCapabilityUniversalMainTest,
138               UpdatePendingActivationState);
139   FRIEND_TEST(CellularCapabilityUniversalMainTest, UpdateServiceName);
140   FRIEND_TEST(CellularTest, Connect);
141   FRIEND_TEST(CellularTest, GetLogin);  // ppp_username_, ppp_password_
142   FRIEND_TEST(CellularTest, OnConnectionHealthCheckerResult);
143   FRIEND_TEST(CellularServiceTest, SetApn);
144   FRIEND_TEST(CellularServiceTest, ClearApn);
145   FRIEND_TEST(CellularServiceTest, LastGoodApn);
146   FRIEND_TEST(CellularServiceTest, LoadResetsPPPAuthFailure);
147   FRIEND_TEST(CellularServiceTest, IsAutoConnectable);
148   FRIEND_TEST(CellularServiceTest, OutOfCreditsDetected);
149   FRIEND_TEST(CellularServiceTest,
150               OutOfCreditsDetectionNotSkippedAfterSlowResume);
151   FRIEND_TEST(CellularServiceTest, OutOfCreditsDetectionSkippedAfterResume);
152   FRIEND_TEST(CellularServiceTest,
153               OutOfCreditsDetectionSkippedAlreadyOutOfCredits);
154   FRIEND_TEST(CellularServiceTest,
155               OutOfCreditsDetectionSkippedExplicitDisconnect);
156   FRIEND_TEST(CellularServiceTest, OutOfCreditsNotDetectedConnectionNotDropped);
157   FRIEND_TEST(CellularServiceTest, OutOfCreditsNotDetectedIntermittentNetwork);
158   FRIEND_TEST(CellularServiceTest, OutOfCreditsNotEnforced);
159   FRIEND_TEST(CellularServiceTest, CustomSetterNoopChange);
160 
161   static const char kAutoConnActivating[];
162   static const char kAutoConnBadPPPCredentials[];
163   static const char kAutoConnDeviceDisabled[];
164   static const char kAutoConnOutOfCredits[];
165   static const char kAutoConnOutOfCreditsDetectionInProgress[];
166   static const char kStoragePPPUsername[];
167   static const char kStoragePPPPassword[];
168 
169   void HelpRegisterDerivedString(
170       const std::string& name,
171       std::string(CellularService::*get)(Error* error),
172       bool(CellularService::*set)(const std::string& value, Error* error));
173   void HelpRegisterDerivedStringmap(
174       const std::string& name,
175       Stringmap(CellularService::*get)(Error* error),
176       bool(CellularService::*set)(const Stringmap& value, Error* error));
177   void HelpRegisterDerivedBool(
178       const std::string& name,
179       bool(CellularService::*get)(Error* error),
180       bool(CellularService::*set)(const bool&, Error*));
181 
182   std::string GetDeviceRpcId(Error* error) const override;
183 
184   std::string CalculateActivationType(Error* error);
185 
186   Stringmap GetApn(Error* error);
187   bool SetApn(const Stringmap& value, Error* error);
188   static void SaveApn(StoreInterface* storage,
189                       const std::string& storage_group,
190                       const Stringmap* apn_info,
191                       const std::string& keytag);
192   static void SaveApnField(StoreInterface* storage,
193                            const std::string& storage_group,
194                            const Stringmap* apn_info,
195                            const std::string& keytag,
196                            const std::string& apntag);
197   static void LoadApn(StoreInterface* storage,
198                       const std::string& storage_group,
199                       const std::string& keytag,
200                       Stringmap* apn_info);
201   static bool LoadApnField(StoreInterface* storage,
202                            const std::string& storage_group,
203                            const std::string& keytag,
204                            const std::string& apntag,
205                            Stringmap* apn_info);
206   bool IsOutOfCredits(Error* /*error*/);
207 
208   // For unit test.
209   void set_out_of_credits_detector(OutOfCreditsDetector* detector);
210 
211   base::WeakPtrFactory<CellularService> weak_ptr_factory_;
212 
213   // Properties
214   ActivationType activation_type_;
215   std::string activation_state_;
216   Stringmap serving_operator_;
217   std::string network_technology_;
218   std::string roaming_state_;
219   Stringmap olp_;
220   std::string usage_url_;
221   Stringmap apn_info_;
222   Stringmap last_good_apn_info_;
223   std::string ppp_username_;
224   std::string ppp_password_;
225 
226   std::string storage_identifier_;
227 
228   CellularRefPtr cellular_;
229 
230   // Flag indicating that a connect request is an auto-connect request.
231   // Note: Since Connect() is asynchronous, this flag is only set during the
232   // call to Connect().  It does not remain set while the async request is
233   // in flight.
234   bool is_auto_connecting_;
235   // Time when the last resume occurred.
236   base::Time resume_start_time_;
237   // Out-of-credits detector.
238   std::unique_ptr<OutOfCreditsDetector> out_of_credits_detector_;
239 
240   DISALLOW_COPY_AND_ASSIGN(CellularService);
241 };
242 
243 }  // namespace shill
244 
245 #endif  // SHILL_CELLULAR_CELLULAR_SERVICE_H_
246