wake_on_wifi.h revision 0e0ac0de9ac592495f780d7c372ba1212d4d374c
1d03b95db5d05928ea63fc9f8aac5151e93c43a2fSamuel Tan// Copyright 2014 The Chromium OS Authors. All rights reserved. 2d03b95db5d05928ea63fc9f8aac5151e93c43a2fSamuel Tan// Use of this source code is governed by a BSD-style license that can be 3d03b95db5d05928ea63fc9f8aac5151e93c43a2fSamuel Tan// found in the LICENSE file. 4d03b95db5d05928ea63fc9f8aac5151e93c43a2fSamuel Tan 5d6a8b519e3057f52313e502f502a4819a942e089Ben Chan#ifndef SHILL_WIFI_WAKE_ON_WIFI_H_ 6d6a8b519e3057f52313e502f502a4819a942e089Ben Chan#define SHILL_WIFI_WAKE_ON_WIFI_H_ 7d03b95db5d05928ea63fc9f8aac5151e93c43a2fSamuel Tan 83a9c098afcd0456734092388d4aa7275716d33aaSamuel Tan#include <linux/if_ether.h> 93a9c098afcd0456734092388d4aa7275716d33aaSamuel Tan#include <netinet/ip.h> 103a9c098afcd0456734092388d4aa7275716d33aaSamuel Tan#include <netinet/ip6.h> 113a9c098afcd0456734092388d4aa7275716d33aaSamuel Tan 125f4b175bca4a997f186010c177c44489c8ae95b4Samuel Tan#include <set> 13cb3ecf321c52de1e72cdf037a459cd893b7c51dbSamuel Tan#include <string> 143a9c098afcd0456734092388d4aa7275716d33aaSamuel Tan#include <utility> 153a9c098afcd0456734092388d4aa7275716d33aaSamuel Tan#include <vector> 163a9c098afcd0456734092388d4aa7275716d33aaSamuel Tan 173a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan#include <base/cancelable_callback.h> 183a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan#include <gtest/gtest_prod.h> // for FRIEND_TEST 193a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan#include <base/memory/ref_counted.h> 203a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan#include <base/memory/weak_ptr.h> 21787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan#include <components/timers/alarm_timer.h> 223a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan 233a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan#include "shill/callbacks.h" 243a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan#include "shill/ip_address_store.h" 258d6b59704591ba9fad57751858835dc332dbdd37Peter Qiu#include "shill/net/ip_address.h" 2602e3dc3bc6e9fdb475bd13944f1c6764c921abbbPeter Qiu#include "shill/net/netlink_manager.h" 273a9c098afcd0456734092388d4aa7275716d33aaSamuel Tan#include "shill/refptr_types.h" 283a9c098afcd0456734092388d4aa7275716d33aaSamuel Tan 29d03b95db5d05928ea63fc9f8aac5151e93c43a2fSamuel Tannamespace shill { 30d03b95db5d05928ea63fc9f8aac5151e93c43a2fSamuel Tan 31d03b95db5d05928ea63fc9f8aac5151e93c43a2fSamuel Tanclass ByteString; 32d03b95db5d05928ea63fc9f8aac5151e93c43a2fSamuel Tanclass Error; 333a1bf99b484ad8a9a054518f49046221251d5612Samuel Tanclass EventDispatcher; 343a9c098afcd0456734092388d4aa7275716d33aaSamuel Tanclass GetWakeOnPacketConnMessage; 3566bddc67c71dd197211ca08c5a4835bd6cfa5ed2Samuel Tanclass Metrics; 363a9c098afcd0456734092388d4aa7275716d33aaSamuel Tanclass Nl80211Message; 3796e35cf43e6d5aa2378cdae6fea507e5335d12bbSamuel Tanclass PropertyStore; 38d03b95db5d05928ea63fc9f8aac5151e93c43a2fSamuel Tanclass SetWakeOnPacketConnMessage; 393a1bf99b484ad8a9a054518f49046221251d5612Samuel Tanclass WiFi; 403a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan 413a1bf99b484ad8a9a054518f49046221251d5612Samuel Tanclass WakeOnWiFi { 423a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan public: 43a757752df1b85d8af4590b64d0946b7b2bb0779fSamuel Tan WakeOnWiFi(NetlinkManager *netlink_manager, EventDispatcher *dispatcher, 4466bddc67c71dd197211ca08c5a4835bd6cfa5ed2Samuel Tan Metrics *metrics); 45fb02175e039a683cf9ac875ba0924858de6a08ffSamuel Tan virtual ~WakeOnWiFi(); 463a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan 4796e35cf43e6d5aa2378cdae6fea507e5335d12bbSamuel Tan // Registers |store| with properties related to wake on WiFi. 4896e35cf43e6d5aa2378cdae6fea507e5335d12bbSamuel Tan void InitPropertyStore(PropertyStore *store); 4996e35cf43e6d5aa2378cdae6fea507e5335d12bbSamuel Tan 5066bddc67c71dd197211ca08c5a4835bd6cfa5ed2Samuel Tan // Starts |metrics_timer_| so that wake on WiFi related metrics are 5166bddc67c71dd197211ca08c5a4835bd6cfa5ed2Samuel Tan // periodically collected. 5266bddc67c71dd197211ca08c5a4835bd6cfa5ed2Samuel Tan void StartMetricsTimer(); 5366bddc67c71dd197211ca08c5a4835bd6cfa5ed2Samuel Tan 543a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan // Types of triggers that can cause the NIC to wake the WiFi device. 55787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan enum WakeOnWiFiTrigger { kPattern, kDisconnect, kSSID }; 563a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan 573a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan // Enable the NIC to wake on packets received from |ip_endpoint|. 583a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan // Note: The actual programming of the NIC only happens before the system 593a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan // suspends, in |OnBeforeSuspend|. 6096bdaecf58097e1cc54c5459e3c165de25c82134Samuel Tan void AddWakeOnPacketConnection(const std::string &ip_endpoint, Error *error); 613a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan // Remove rule to wake on packets received from |ip_endpoint| from the NIC. 623a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan // Note: The actual programming of the NIC only happens before the system 633a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan // suspends, in |OnBeforeSuspend|. 6496bdaecf58097e1cc54c5459e3c165de25c82134Samuel Tan void RemoveWakeOnPacketConnection(const std::string &ip_endpoint, 6596bdaecf58097e1cc54c5459e3c165de25c82134Samuel Tan Error *error); 663a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan // Remove all rules to wake on incoming packets from the NIC. 673a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan // Note: The actual programming of the NIC only happens before the system 683a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan // suspends, in |OnBeforeSuspend|. 693a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan void RemoveAllWakeOnPacketConnections(Error *error); 703a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan // Given a NL80211_CMD_NEW_WIPHY message |nl80211_message|, parses the 7196bdaecf58097e1cc54c5459e3c165de25c82134Samuel Tan // wake on WiFi capabilities of the NIC and set relevant members of this 723a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan // WakeOnWiFi object to reflect the supported capbilities. 733a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan void ParseWakeOnWiFiCapabilities(const Nl80211Message &nl80211_message); 743a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan // Given a NL80211_CMD_NEW_WIPHY message |nl80211_message|, parses the 753a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan // wiphy index of the NIC and sets |wiphy_index_| with the parsed index. 763a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan void ParseWiphyIndex(const Nl80211Message &nl80211_message); 773a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan // Performs pre-suspend actions relevant to wake on wireless functionality. 78787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan // Initiates DHCP lease renewal if there is a lease due to renewal soon, 79787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan // then calls WakeOnWiFi::BeforeSuspendActions. 80787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan // 81787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan // Arguments: 82787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan // - |is_connected|: whether the WiFi device is connected. 83c420dd27964a85c8dec8f09d9049287ccba5f95aSamuel Tan // - |have_service_configured_for_autoconnect|: whether there exists at 84c420dd27964a85c8dec8f09d9049287ccba5f95aSamuel Tan // least one service (hidden or not) that is configured for auto-connect. 85787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan // - |done_callback|: callback to invoke when suspend actions have 86787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan // completed. 87787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan // - |renew_dhcp_lease_callback|: callback to invoke to initiate DHCP lease 88787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan // renewal. 89787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan // - |remove_supplicant_networks_callback|: callback to invoke 90787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan // to remove all networks from WPA supplicant. 91787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan // - |have_dhcp_lease|: whether or not there is a DHCP lease to renew. 92787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan // - |time_to_next_lease_renewal|: number of seconds until next DHCP lease 93787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan // renewal is due. 94787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan virtual void OnBeforeSuspend( 95787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan bool is_connected, 96c420dd27964a85c8dec8f09d9049287ccba5f95aSamuel Tan bool have_service_configured_for_autoconnect, 97787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan const ResultCallback &done_callback, 98787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan const base::Closure &renew_dhcp_lease_callback, 99787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan const base::Closure &remove_supplicant_networks_callback, 100787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan bool have_dhcp_lease, 101787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan uint32_t time_to_next_lease_renewal); 1023a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan // Performs post-resume actions relevant to wake on wireless functionality. 103fb02175e039a683cf9ac875ba0924858de6a08ffSamuel Tan virtual void OnAfterResume(); 104787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan // Performs and post actions to be performed in dark resume. 105787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan // When we wake up in dark resume and wake on WiFi is supported, we start a 106787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan // timeout timer, and do one of two things depending on whether the WiFi 107787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan // device is connected to a service: 108787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan // - If the WiFi device is connected before suspend, initiate DHCP lease 109787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan // renewal. 110787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan // - Otherwise, WiFi device is not connected, so initiate a scan. 111787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan // There are two possible outcomes from these actions: 112787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan // - A DHCP lease is obtained, either because of connection to a network 113787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan // or successful lease renewal. 114787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan // - The timer expires, so dark resume actions time out. 115787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan // In either case, WakeOnWiFi::BeforeSuspendActions is invoked asynchronously. 116787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan // If WiFi device is connected (in the case where we got the DHCP lease), 117787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan // we start the lease renewal timer, disable wake on SSID, and enable wake on 118787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan // disconnect. Otherwise, we stop the lease renewal timer, enable wake on 119787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan // SSID, disable wake on disconnect, and remove all networks from WPA 120787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan // supplicant. This is the same logic applied before regular suspend, and 121787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan // should ensure that the correct actions are taken both before regular 122787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan // suspend and suspend in dark resume to maintain connectivity. 123787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan // 124787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan // Arguments: 125787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan // - |is_connected|: whether the WiFi device is connected. 126c420dd27964a85c8dec8f09d9049287ccba5f95aSamuel Tan // - |have_service_configured_for_autoconnect|: whether there exists at 127c420dd27964a85c8dec8f09d9049287ccba5f95aSamuel Tan // least one service (hidden or not) that is configured for auto-connect. 128787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan // - |done_callback|: callback to invoke when dark resume actions have 129787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan // completed. 130787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan // - |renew_dhcp_lease_callback|: callback to invoke to initiate DHCP lease 131787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan // renewal. 132787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan // - |initate_scan_callback|: callback to invoke to initiate a scan. 133787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan // - |remove_supplicant_networks_callback|: callback to invoke 134787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan // to remove all networks from WPA supplicant. 135787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan virtual void OnDarkResume( 136787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan bool is_connected, 137c420dd27964a85c8dec8f09d9049287ccba5f95aSamuel Tan bool have_service_configured_for_autoconnect, 138787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan const ResultCallback &done_callback, 139787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan const base::Closure &renew_dhcp_lease_callback, 140787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan const base::Closure &initiate_scan_callback, 141787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan const base::Closure &remove_supplicant_networks_callback); 142787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan // Wrapper around WakeOnWiFi::BeforeSuspendActions that checks if shill is 143787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan // currently in dark resume before invoking the function. 144787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan virtual void OnDHCPLeaseObtained(bool start_lease_renewal_timer, 145787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan uint32_t time_to_next_lease_renewal); 1461964b5de186ef06fa89a78b27c3a112686bc36f7Samuel Tan // Callback invoked to report whether this WiFi device is connected to 1471964b5de186ef06fa89a78b27c3a112686bc36f7Samuel Tan // a service after waking from suspend. 1481964b5de186ef06fa89a78b27c3a112686bc36f7Samuel Tan virtual void ReportConnectedToServiceAfterWake(bool is_connected); 1493a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan 1500e0ac0de9ac592495f780d7c372ba1212d4d374cSamuel Tan bool in_dark_resume() { return in_dark_resume_; } 1510e0ac0de9ac592495f780d7c372ba1212d4d374cSamuel Tan 1523a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan private: 1533a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan friend class WakeOnWiFiTest; // access to several members for tests 1543a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan friend class WiFiObjectTest; // netlink_manager_ 155fb02175e039a683cf9ac875ba0924858de6a08ffSamuel Tan // Tests that need kWakeOnWiFiDisabled. 156787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan FRIEND_TEST(WakeOnWiFiTestWithMockDispatcher, 157787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan WakeOnWiFiDisabled_AddWakeOnPacketConnection_ReturnsError); 158787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan FRIEND_TEST(WakeOnWiFiTestWithMockDispatcher, 159787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan WakeOnWiFiDisabled_RemoveWakeOnPacketConnection_ReturnsError); 160787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan FRIEND_TEST(WakeOnWiFiTestWithMockDispatcher, 161787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan WakeOnWiFiDisabled_RemoveAllWakeOnPacketConnections_ReturnsError); 162787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan FRIEND_TEST(WakeOnWiFiTestWithMockDispatcher, 163787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan ParseWiphyIndex_Success); // kDefaultWiphyIndex 164fb02175e039a683cf9ac875ba0924858de6a08ffSamuel Tan // Tests that need kMaxSetWakeOnPacketRetries. 165787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan FRIEND_TEST(WakeOnWiFiTestWithMockDispatcher, 1663a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan RetrySetWakeOnPacketConnections_LessThanMaxRetries); 167787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan FRIEND_TEST(WakeOnWiFiTestWithMockDispatcher, 1683a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan RetrySetWakeOnPacketConnections_MaxAttemptsWithCallbackSet); 169787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan FRIEND_TEST(WakeOnWiFiTestWithMockDispatcher, 1703a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan RetrySetWakeOnPacketConnections_MaxAttemptsCallbackUnset); 171787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan // Tests that need WakeOnWiFi::kDarkResumeActionsTimeoutMilliseconds 172787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan FRIEND_TEST(WakeOnWiFiTestWithMockDispatcher, 173787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan OnBeforeSuspend_DHCPLeaseRenewal); 1743a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan 175fb02175e039a683cf9ac875ba0924858de6a08ffSamuel Tan static const char kWakeOnIPAddressPatternsNotSupported[]; 176fb02175e039a683cf9ac875ba0924858de6a08ffSamuel Tan static const char kWakeOnPacketDisabled[]; 177fb02175e039a683cf9ac875ba0924858de6a08ffSamuel Tan static const char kWakeOnWiFiDisabled[]; 1783a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan static const uint32_t kDefaultWiphyIndex; 179f8c0b3bcc1b9e918400c1fe76a9f6991bc32b55bSamuel Tan static const int kVerifyWakeOnWiFiSettingsDelayMilliseconds; 1803a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan static const int kMaxSetWakeOnPacketRetries; 18166bddc67c71dd197211ca08c5a4835bd6cfa5ed2Samuel Tan static const int kMetricsReportingFrequencySeconds; 1824889ff847d57835820f189b622e945d70ec41494Samuel Tan static const uint32_t kDefaultWakeToScanFrequencySeconds; 183787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan static const uint32_t kImmediateDHCPLeaseRenewalThresholdSeconds; 184787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan static int64_t DarkResumeActionsTimeoutMilliseconds; // non-const for testing 1853a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan 18696e35cf43e6d5aa2378cdae6fea507e5335d12bbSamuel Tan std::string GetWakeOnWiFiFeaturesEnabled(Error *error); 18796e35cf43e6d5aa2378cdae6fea507e5335d12bbSamuel Tan bool SetWakeOnWiFiFeaturesEnabled(const std::string &enabled, Error *error); 188740ee07e13d98ee9e0535ffea391326c8da13499Samuel Tan // Helper function to run and reset |suspend_actions_done_callback_|. 189740ee07e13d98ee9e0535ffea391326c8da13499Samuel Tan void RunAndResetSuspendActionsDoneCallback(const Error &error); 1903a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan // Used for comparison of ByteString pairs in a set. 1913a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan static bool ByteStringPairIsLessThan( 1923a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan const std::pair<ByteString, ByteString> &lhs, 1933a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan const std::pair<ByteString, ByteString> &rhs); 1943a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan // Creates a mask which specifies which bytes in pattern of length 1953a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan // |pattern_len| to match against. Bits |offset| to |pattern_len| - 1 are set, 1963a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan // which bits 0 to bits 0 to |offset| - 1 are unset. This mask is saved in 1973a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan // |mask|. 1983a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan static void SetMask(ByteString *mask, uint32_t pattern_len, uint32_t offset); 1993a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan // Creates a pattern and mask for a NL80211 message that programs the NIC to 2003a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan // wake on packets originating from IP address |ip_addr|. The pattern and mask 2013a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan // are saved in |pattern| and |mask| respectively. Returns true iff the 2023a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan // pattern and mask are successfully created and written to |pattern| and 2033a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan // |mask| respectively. 2043a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan static bool CreateIPAddressPatternAndMask(const IPAddress &ip_addr, 2053a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan ByteString *pattern, 2063a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan ByteString *mask); 2073a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan static void CreateIPV4PatternAndMask(const IPAddress &ip_addr, 2083a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan ByteString *pattern, ByteString *mask); 2093a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan static void CreateIPV6PatternAndMask(const IPAddress &ip_addr, 2103a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan ByteString *pattern, ByteString *mask); 2113a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan // Creates and sets an attribute in a NL80211 message |msg| which indicates 2123a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan // the index of the wiphy interface to program. Returns true iff |msg| is 2133a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan // successfully configured. 2143a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan static bool ConfigureWiphyIndex(Nl80211Message *msg, int32_t index); 2153a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan // Creates and sets attributes in an SetWakeOnPacketConnMessage |msg| so that 2163a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan // the message will disable wake-on-packet functionality of the NIC with wiphy 2173a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan // index |wiphy_index|. Returns true iff |msg| is successfully configured. 2183a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan // NOTE: Assumes that |msg| has not been altered since construction. 2193a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan static bool ConfigureDisableWakeOnWiFiMessage(SetWakeOnPacketConnMessage *msg, 2203a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan uint32_t wiphy_index, 2213a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan Error *error); 2223a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan // Creates and sets attributes in a SetWakeOnPacketConnMessage |msg| 2233a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan // so that the message will program the NIC with wiphy index |wiphy_index| 2243a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan // with wake on wireless triggers in |trigs|. If |trigs| contains the 225787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan // kPattern trigger, the NIC is programmed to wake on packets from the 2263a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan // IP addresses in |addrs|. Returns true iff |msg| is successfully configured. 2273a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan // NOTE: Assumes that |msg| has not been altered since construction. 2283a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan static bool ConfigureSetWakeOnWiFiSettingsMessage( 2293a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan SetWakeOnPacketConnMessage *msg, const std::set<WakeOnWiFiTrigger> &trigs, 2303a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan const IPAddressStore &addrs, uint32_t wiphy_index, Error *error); 2313a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan // Helper function to ConfigureSetWakeOnWiFiSettingsMessage that creates a 2323a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan // single nested attribute inside the attribute list referenced by |patterns| 2333a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan // representing a wake-on-packet pattern matching rule with index |patnum|. 2343a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan // Returns true iff the attribute is successfully created and set. 2353a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan // NOTE: |patterns| is assumed to reference the nested attribute list 2363a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan // NL80211_WOWLAN_TRIG_PKT_PATTERN. 2373a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan // NOTE: |patnum| should be unique across multiple calls to this function to 2383a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan // prevent the formation of a erroneous nl80211 message or the overwriting of 2393a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan // pattern matching rules. 2403a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan static bool CreateSinglePattern(const IPAddress &ip_addr, 2413a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan AttributeListRefPtr patterns, uint8_t patnum, 2423a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan Error *error); 2433a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan // Creates and sets attributes in an GetWakeOnPacketConnMessage msg| so that 2443a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan // the message will request for wake-on-packet settings information from the 2453a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan // NIC with wiphy index |wiphy_index|. Returns true iff |msg| is successfully 2463a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan // configured. 2473a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan // NOTE: Assumes that |msg| has not been altered since construction. 2483a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan static bool ConfigureGetWakeOnWiFiSettingsMessage( 2493a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan GetWakeOnPacketConnMessage *msg, uint32_t wiphy_index, Error *error); 2503a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan // Given a NL80211_CMD_GET_WOWLAN response or NL80211_CMD_SET_WOWLAN request 2513a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan // |msg|, returns true iff the wake-on-wifi trigger settings in |msg| match 2523a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan // those in |trigs|. Checks that source IP addresses in |msg| match those in 2533a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan // |addrs| if the kIPAddress flag is in |trigs|. 2543a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan static bool WakeOnWiFiSettingsMatch(const Nl80211Message &msg, 2553a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan const std::set<WakeOnWiFiTrigger> &trigs, 2563a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan const IPAddressStore &addrs); 257742866741cd8902bfabd3d9731ccc6b4d5052de6Samuel Tan // Handler for NL80211 message error responses from NIC wake on WiFi setting 258742866741cd8902bfabd3d9731ccc6b4d5052de6Samuel Tan // programming attempts. 259742866741cd8902bfabd3d9731ccc6b4d5052de6Samuel Tan void OnWakeOnWiFiSettingsErrorResponse( 260742866741cd8902bfabd3d9731ccc6b4d5052de6Samuel Tan NetlinkManager::AuxilliaryMessageType type, 261742866741cd8902bfabd3d9731ccc6b4d5052de6Samuel Tan const NetlinkMessage *raw_message); 2623a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan // Message handler for NL80211_CMD_SET_WOWLAN responses. 2633a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan static void OnSetWakeOnPacketConnectionResponse( 2643a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan const Nl80211Message &nl80211_message); 2653a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan // Request wake on WiFi settings for this WiFi device. 2663a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan void RequestWakeOnPacketSettings(); 2673a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan // Verify that the wake on WiFi settings programmed into the NIC match 2683a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan // those recorded locally for this device in |wake_on_packet_connections_| 2693a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan // and |wake_on_wifi_triggers_|. 2703a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan void VerifyWakeOnWiFiSettings(const Nl80211Message &nl80211_message); 2713a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan // Sends an NL80211 message to program the NIC with wake on WiFi settings 2723a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan // configured in |wake_on_packet_connections_| and |wake_on_wifi_triggers_|. 2733a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan // If |wake_on_wifi_triggers_| is empty, calls |DisableWakeOnWiFi|. 2743a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan void ApplyWakeOnWiFiSettings(); 2753a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan // Helper function called by |ApplyWakeOnWiFiSettings| that sends an NL80211 2763a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan // message to program the NIC to disable wake on WiFi. 2773a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan void DisableWakeOnWiFi(); 2783a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan // Calls |ApplyWakeOnWiFiSettings| and counts this call as 2793a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan // a retry. If |kMaxSetWakeOnPacketRetries| retries have already been 2803a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan // performed, resets counter and returns. 2813a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan void RetrySetWakeOnPacketConnections(); 282cb3ecf321c52de1e72cdf037a459cd893b7c51dbSamuel Tan // Utility functions to check which wake on WiFi features are currently 283787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan // enabled based on the descriptor |wake_on_wifi_features_enabled_| and 284787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan // are supported by the NIC. 285787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan bool WakeOnPacketEnabledAndSupported(); 286787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan bool WakeOnSSIDEnabledAndSupported(); 28766bddc67c71dd197211ca08c5a4835bd6cfa5ed2Samuel Tan // Called by metrics_timer_ to reports metrics. 28866bddc67c71dd197211ca08c5a4835bd6cfa5ed2Samuel Tan void ReportMetrics(); 289787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan // Actions executed before normal suspend and dark resume suspend. 290787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan // 291787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan // Arguments: 292787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan // - |is_connected|: whether the WiFi device is connected. 293c420dd27964a85c8dec8f09d9049287ccba5f95aSamuel Tan // - |have_service_configured_for_autoconnect|: whether there exists at 294c420dd27964a85c8dec8f09d9049287ccba5f95aSamuel Tan // least one service (hidden or not) that is configured for auto-connect. 295787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan // - |start_lease_renewal_timer|: whether or not to start the DHCP lease 296787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan // renewal timer. 297787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan // - |time_to_next_lease_renewal|: number of seconds until next DHCP lease 298787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan // renewal is due. 299787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan // - |remove_supplicant_networks_callback|: callback to invoke 300787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan // to remove all networks from WPA supplicant. 301787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan void BeforeSuspendActions( 302787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan bool is_connected, 303c420dd27964a85c8dec8f09d9049287ccba5f95aSamuel Tan bool have_service_configured_for_autoconnect, 304787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan bool start_lease_renewal_timer, 305787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan uint32_t time_to_next_lease_renewal, 306787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan const base::Closure &remove_supplicant_networks_callback); 307787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan 308787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan // Needed for |dhcp_lease_renewal_timer_| and |wake_to_scan_timer_| since 309787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan // passing a empty base::Closure() causes a run-time DCHECK error when 310787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan // AlarmTimer::Start or AlarmTimer::Reset are called. 311787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan void OnTimerWakeDoNothing() {} 31266bddc67c71dd197211ca08c5a4835bd6cfa5ed2Samuel Tan 3133a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan // Pointers to objects owned by the WiFi object that created this object. 3143a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan EventDispatcher *dispatcher_; 3153a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan NetlinkManager *netlink_manager_; 31666bddc67c71dd197211ca08c5a4835bd6cfa5ed2Samuel Tan Metrics *metrics_; 3173a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan // Executes after the NIC's wake-on-packet settings are configured via 3183a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan // NL80211 messages to verify that the new configuration has taken effect. 3193a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan // Calls RequestWakeOnPacketSettings. 3203a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan base::CancelableClosure verify_wake_on_packet_settings_callback_; 321787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan // Callback to be invoked after all suspend actions finish executing both 322787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan // before regular suspend and before suspend in dark resume. 3233a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan ResultCallback suspend_actions_done_callback_; 32466bddc67c71dd197211ca08c5a4835bd6cfa5ed2Samuel Tan // Callback to report wake on WiFi related metrics. 32566bddc67c71dd197211ca08c5a4835bd6cfa5ed2Samuel Tan base::CancelableClosure report_metrics_callback_; 3263a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan // Number of retry attempts to program the NIC's wake-on-packet settings. 3273a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan int num_set_wake_on_packet_retries_; 3283a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan // Keeps track of triggers that the NIC will be programmed to wake from 3293a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan // while suspended. 3303a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan std::set<WakeOnWiFi::WakeOnWiFiTrigger> wake_on_wifi_triggers_; 3313a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan // Keeps track of what wake on wifi triggers this WiFi device supports. 3323a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan std::set<WakeOnWiFi::WakeOnWiFiTrigger> wake_on_wifi_triggers_supported_; 3333a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan // Max number of patterns this WiFi device can be programmed to wake on 3343a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan // at one time. 3353a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan size_t wake_on_wifi_max_patterns_; 3363a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan // Keeps track of IP addresses whose packets this device will wake upon 3373a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan // receiving while the device is suspended. 3383a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan IPAddressStore wake_on_packet_connections_; 3393a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan uint32_t wiphy_index_; 3403a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan bool wiphy_index_received_; 34196e35cf43e6d5aa2378cdae6fea507e5335d12bbSamuel Tan // Describes the wake on WiFi features that are currently enabled. 34296e35cf43e6d5aa2378cdae6fea507e5335d12bbSamuel Tan std::string wake_on_wifi_features_enabled_; 343787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan // Timer that wakes the system to renew DHCP leases. 344787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan timers::AlarmTimer dhcp_lease_renewal_timer_; 345787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan // Timer that wakes the system to scan for networks. 346787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan timers::AlarmTimer wake_to_scan_timer_; 347787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan // Executes when the dark resume actions timer expires. Calls 348787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan // ScanTimerHandler. 349787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan base::CancelableClosure dark_resume_actions_timeout_callback_; 350787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan // Whether shill is currently in dark resume. 351787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan bool in_dark_resume_; 3524889ff847d57835820f189b622e945d70ec41494Samuel Tan // Frequency (in seconds) that the system is woken during suspend to perform 3534889ff847d57835820f189b622e945d70ec41494Samuel Tan // scans. 3544889ff847d57835820f189b622e945d70ec41494Samuel Tan uint32_t wake_to_scan_frequency_; 35566bddc67c71dd197211ca08c5a4835bd6cfa5ed2Samuel Tan 3563a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan base::WeakPtrFactory<WakeOnWiFi> weak_ptr_factory_; 357d03b95db5d05928ea63fc9f8aac5151e93c43a2fSamuel Tan 3583a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan DISALLOW_COPY_AND_ASSIGN(WakeOnWiFi); 3593a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan}; 360d03b95db5d05928ea63fc9f8aac5151e93c43a2fSamuel Tan 361d03b95db5d05928ea63fc9f8aac5151e93c43a2fSamuel Tan} // namespace shill 362d03b95db5d05928ea63fc9f8aac5151e93c43a2fSamuel Tan 363d6a8b519e3057f52313e502f502a4819a942e089Ben Chan#endif // SHILL_WIFI_WAKE_ON_WIFI_H_ 364