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_LINK_MONITOR_H_ 18 #define SHILL_LINK_MONITOR_H_ 19 20 #include <time.h> 21 22 #include <memory> 23 #include <string> 24 25 #include <base/callback.h> 26 #include <base/cancelable_callback.h> 27 28 #include "shill/metrics.h" 29 #include "shill/net/byte_string.h" 30 #include "shill/refptr_types.h" 31 32 namespace shill { 33 34 class ActiveLinkMonitor; 35 class DeviceInfo; 36 class EventDispatcher; 37 class PassiveLinkMonitor; 38 class Time; 39 40 class LinkMonitor { 41 public: 42 typedef base::Closure FailureCallback; 43 typedef base::Closure GatewayChangeCallback; 44 45 // The default number of milliseconds between ARP requests used by 46 // ActiveLinkMonitor. Needed by Metrics. 47 static const int kDefaultTestPeriodMilliseconds; 48 49 // The default list of technologies for which link monitoring is enabled. 50 // Needed by DefaultProfile. 51 static const char kDefaultLinkMonitorTechnologies[]; 52 53 // Failure threshold count used by ActiveLinkMonitor. Needed by Metrics. 54 static const int kFailureThreshold; 55 56 LinkMonitor(const ConnectionRefPtr& connection, 57 EventDispatcher* dispatcher, // Owned by caller; can't be NULL. 58 Metrics* metrics, // Owned by caller; must not be NULL. 59 DeviceInfo* device_info, 60 const FailureCallback& failure_callback, 61 const GatewayChangeCallback& gateway_change_callback); 62 virtual ~LinkMonitor(); 63 64 // Starts link-monitoring on the selected connection. Returns 65 // true if successful, false otherwise. 66 virtual bool Start(); 67 // Stop link-monitoring on the selected connection. Clears any 68 // accumulated statistics. 69 virtual void Stop(); 70 71 // Inform LinkMonitor that the system is resuming from sleep. 72 // LinkMonitor will immediately start the ActiveLinkMonitor, using a lower 73 // timeout than normal. 74 virtual void OnAfterResume(); 75 76 // Return modified cumulative average of the gateway ARP response 77 // time. Returns zero if no samples are available. For each 78 // missed ARP response, the sample is assumed to be the full 79 // test period. 80 virtual int GetResponseTimeMilliseconds() const; 81 82 // Returns true if the LinkMonitor was ever able to find the default 83 // gateway via broadcast ARP. 84 virtual bool IsGatewayFound() const; 85 gateway_mac_address()86 const ByteString& gateway_mac_address() const { 87 return gateway_mac_address_; 88 } 89 90 private: 91 friend class LinkMonitorTest; 92 93 void OnActiveLinkMonitorFailure(Metrics::LinkMonitorFailure failure, 94 int broadcast_failure_count, 95 int unicast_failure_count); 96 void OnActiveLinkMonitorSuccess(); 97 void OnPassiveLinkMonitorResultCallback(bool status); 98 99 // The connection on which to perform link monitoring. 100 ConnectionRefPtr connection_; 101 // Dispatcher on which to create delayed tasks. 102 EventDispatcher* dispatcher_; 103 // Metrics instance on which to post performance results. 104 Metrics* metrics_; 105 // Failure callback method to call if LinkMonitor fails. 106 FailureCallback failure_callback_; 107 // Callback method to call if gateway mac address changes. 108 GatewayChangeCallback gateway_change_callback_; 109 std::unique_ptr<ActiveLinkMonitor> active_link_monitor_; 110 std::unique_ptr<PassiveLinkMonitor> passive_link_monitor_; 111 // The MAC address of the default gateway. 112 ByteString gateway_mac_address_; 113 // The time at which the link monitor started. 114 struct timeval started_monitoring_at_; 115 // Time instance for performing GetTimeMonotonic(). 116 Time* time_; 117 118 DISALLOW_COPY_AND_ASSIGN(LinkMonitor); 119 }; 120 121 } // namespace shill 122 123 #endif // SHILL_LINK_MONITOR_H_ 124