1 //
2 // Copyright (C) 2013 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_OUT_OF_CREDITS_DETECTOR_H_
18 #define SHILL_CELLULAR_OUT_OF_CREDITS_DETECTOR_H_
19 
20 #include <base/macros.h>
21 
22 #include <string>
23 
24 #include "shill/service.h"
25 
26 namespace shill {
27 
28 class CellularService;
29 class EventDispatcher;
30 class Manager;
31 class Metrics;
32 class TrafficMonitor;
33 
34 // Base class for the various out-of-credits detection mechanism.
35 class OutOfCreditsDetector {
36  public:
37   OutOfCreditsDetector(EventDispatcher* dispatcher,
38                        Manager* manager,
39                        Metrics* metrics,
40                        CellularService* service);
41   virtual ~OutOfCreditsDetector();
42 
43   // Various types of out-of-credits detections.
44   enum OOCType {
45     // No out-of-credits detection is employed.
46     OOCTypeNone = 0,
47     // Passively monitors the traffic for TX congestion and DNS failures, then
48     // actively probe the network for TX congestion to determine if the
49     // network has entered an OOC condition.
50     OOCTypeActivePassive = 1,
51     // Use ModemManager SubscriptionState property to determine OOC condition.
52     OOCTypeSubscriptionState = 2
53   };
54 
55   // Creates a specific out-of-credits detector.
56   // For OOCTypeNone, this methods returns NoOutOfCreditsDetector. For
57   // OOCTypeActivePassive, this method returns
58   // ActivePassiveOutOfCreditsDetector. For OOCTypeSubscriptionState,
59   // this method returns SubscriptionStateOutOfCreditsDetector.
60   static OutOfCreditsDetector* CreateDetector(OOCType detector_type,
61                                               EventDispatcher* dispatcher,
62                                               Manager* manager,
63                                               Metrics* metrics,
64                                               CellularService* service);
65 
66   // Resets the detector state.
67   virtual void ResetDetector() = 0;
68   // Returns |true| if this object is busy detecting out-of-credits.
69   virtual bool IsDetecting() const = 0;
70   // Notifies this object of a service state change.
71   virtual void NotifyServiceStateChanged(Service::ConnectState old_state,
72                                          Service::ConnectState new_state) = 0;
73   // Notifies this object when the subscription state has changed.
74   virtual void NotifySubscriptionStateChanged(uint32_t subscription_state) = 0;
75 
out_of_credits()76   virtual bool out_of_credits() const { return out_of_credits_; }
77 
78  protected:
79   FRIEND_TEST(ActivePassiveOutOfCreditsDetectorTest,
80       ConnectDisconnectLoopDetectionSkippedAlreadyOutOfCredits);
81 
82   // Sets the out-of-credits state for this object and also tells the service
83   // object to signal the property change.
84   void ReportOutOfCredits(bool state);
85 
86   // Property accessors reserved for subclasses.
dispatcher()87   EventDispatcher* dispatcher() const { return dispatcher_; }
manager()88   Manager* manager() const { return manager_; }
metrics()89   Metrics* metrics() const { return metrics_; }
service()90   CellularService* service() const { return service_; }
91 
92  private:
93   EventDispatcher* dispatcher_;
94   Manager* manager_;
95   Metrics* metrics_;
96   CellularService* service_;
97   // Flag indicating if the account is out-of-credits.
98   bool out_of_credits_;
99 
100   DISALLOW_COPY_AND_ASSIGN(OutOfCreditsDetector);
101 };
102 
103 }  // namespace shill
104 
105 #endif  // SHILL_CELLULAR_OUT_OF_CREDITS_DETECTOR_H_
106