wake_on_wifi.h revision d1bec5d563afc9074a1875e2302d5ecf54c95f95
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"
25ff59a1896ea250b4450b54db697692e0b1949528Samuel Tan#include "shill/net/event_history.h"
268d6b59704591ba9fad57751858835dc332dbdd37Peter Qiu#include "shill/net/ip_address.h"
2702e3dc3bc6e9fdb475bd13944f1c6764c921abbbPeter Qiu#include "shill/net/netlink_manager.h"
283a9c098afcd0456734092388d4aa7275716d33aaSamuel Tan#include "shill/refptr_types.h"
293a9c098afcd0456734092388d4aa7275716d33aaSamuel Tan
30d03b95db5d05928ea63fc9f8aac5151e93c43a2fSamuel Tannamespace shill {
31d03b95db5d05928ea63fc9f8aac5151e93c43a2fSamuel Tan
32d03b95db5d05928ea63fc9f8aac5151e93c43a2fSamuel Tanclass ByteString;
33d03b95db5d05928ea63fc9f8aac5151e93c43a2fSamuel Tanclass Error;
343a1bf99b484ad8a9a054518f49046221251d5612Samuel Tanclass EventDispatcher;
353a9c098afcd0456734092388d4aa7275716d33aaSamuel Tanclass GetWakeOnPacketConnMessage;
3666bddc67c71dd197211ca08c5a4835bd6cfa5ed2Samuel Tanclass Metrics;
373a9c098afcd0456734092388d4aa7275716d33aaSamuel Tanclass Nl80211Message;
3896e35cf43e6d5aa2378cdae6fea507e5335d12bbSamuel Tanclass PropertyStore;
39d03b95db5d05928ea63fc9f8aac5151e93c43a2fSamuel Tanclass SetWakeOnPacketConnMessage;
403a1bf99b484ad8a9a054518f49046221251d5612Samuel Tanclass WiFi;
413a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan
42c51454cb6a4442475449720549ac883731506909Samuel Tan// |WakeOnWiFi| performs all wake on WiFi related tasks and logic (e.g.
43c51454cb6a4442475449720549ac883731506909Samuel Tan// suspend/dark resume/resume logic, NIC wowlan programming via nl80211), and
44c51454cb6a4442475449720549ac883731506909Samuel Tan// stores the state necessary to perform these actions.
45c51454cb6a4442475449720549ac883731506909Samuel Tan//
46c51454cb6a4442475449720549ac883731506909Samuel Tan// One of the most significant roles of |WakeOnWiFi| is performing the correct
47c51454cb6a4442475449720549ac883731506909Samuel Tan// actions before suspend, during dark resume, and after resume in order to
48c51454cb6a4442475449720549ac883731506909Samuel Tan// maintain system connectivity (if the relevant wake on WiFi features  are
49c51454cb6a4442475449720549ac883731506909Samuel Tan// supported and enabled). The state machines determining which actions are
50c51454cb6a4442475449720549ac883731506909Samuel Tan// performed in these situations are described below:
51c51454cb6a4442475449720549ac883731506909Samuel Tan//
52c51454cb6a4442475449720549ac883731506909Samuel Tan// OnBeforeSuspend
53c51454cb6a4442475449720549ac883731506909Samuel Tan// ================
54c51454cb6a4442475449720549ac883731506909Samuel Tan// This function is run when Manager announces an upcoming system suspend.
55c51454cb6a4442475449720549ac883731506909Samuel Tan//
56c51454cb6a4442475449720549ac883731506909Samuel Tan//         +--------------+
57c51454cb6a4442475449720549ac883731506909Samuel Tan//         |          Yes |   +----------------+
58c51454cb6a4442475449720549ac883731506909Samuel Tan// +-------+--------+     +-->|Renew DHCP Lease|
59c51454cb6a4442475449720549ac883731506909Samuel Tan// |  Connected &   |         +------+---------+
60c51454cb6a4442475449720549ac883731506909Samuel Tan// |holding expiring|                |
61c51454cb6a4442475449720549ac883731506909Samuel Tan// |  DHCP lease?   |                v
62c51454cb6a4442475449720549ac883731506909Samuel Tan// +------+---------+         +--------------------+
63c51454cb6a4442475449720549ac883731506909Samuel Tan//        |               +-> |BeforeSuspendActions|
64c51454cb6a4442475449720549ac883731506909Samuel Tan//        |           No  |   +--------------------+
65c51454cb6a4442475449720549ac883731506909Samuel Tan//        +---------------+
66c51454cb6a4442475449720549ac883731506909Samuel Tan//
67c51454cb6a4442475449720549ac883731506909Samuel Tan// OnDarkResume
68c51454cb6a4442475449720549ac883731506909Samuel Tan// =============
69c51454cb6a4442475449720549ac883731506909Samuel Tan// This function is run when Manager announces that the system has entered
70c51454cb6a4442475449720549ac883731506909Samuel Tan// dark resume and that there is an upcoming system suspend.
71c51454cb6a4442475449720549ac883731506909Samuel Tan//
72c51454cb6a4442475449720549ac883731506909Samuel Tan// +-------------+      +------------+     Unsupported     +----------+
73c51454cb6a4442475449720549ac883731506909Samuel Tan// |  Too many   +----->|Wake reason?+-------------------->|Connected?|
74d1bec5d563afc9074a1875e2302d5ecf54c95f95Samuel Tan// |dark resumes?|  No  +-+----------+                     +-+-----+--+
75c51454cb6a4442475449720549ac883731506909Samuel Tan// +------+------+        |       |                          |     |
76c51454cb6a4442475449720549ac883731506909Samuel Tan//        | Yes           |       | Disconnect/           No |     | Yes
77c51454cb6a4442475449720549ac883731506909Samuel Tan//        v               |       |    SSID                  |     |
78c51454cb6a4442475449720549ac883731506909Samuel Tan// +----------------+     |       v                          |     |
79c51454cb6a4442475449720549ac883731506909Samuel Tan// |  Disable Wake  |     |     +------------+               |     v
80c51454cb6a4442475449720549ac883731506909Samuel Tan// |   on WiFi &    |     |     |  Initiate  |<--------------+    +--------+
81c51454cb6a4442475449720549ac883731506909Samuel Tan// |report readiness|     |     |passive scan|                    |Get DHCP|
82c51454cb6a4442475449720549ac883731506909Samuel Tan// +----------------+     |     +-+----------+           +------->| Lease  |
83c51454cb6a4442475449720549ac883731506909Samuel Tan//                        |       | ScanDone         Yes |        +--+---+-+
84c51454cb6a4442475449720549ac883731506909Samuel Tan//    +-------------------+       v                      |           |   |
85c51454cb6a4442475449720549ac883731506909Samuel Tan//    | Pattern                 +-------------+      +---------+     |   |
86c51454cb6a4442475449720549ac883731506909Samuel Tan//    |                    No   | Any services| Yes  |Connected|     |   |
87c51454cb6a4442475449720549ac883731506909Samuel Tan//    |    +--------------------+available for+----->| to AP?  |     |   |
88c51454cb6a4442475449720549ac883731506909Samuel Tan//    |    |                    | autoconnect?|      +---+-----+     |   |
89c51454cb6a4442475449720549ac883731506909Samuel Tan//    |    |                    +-------------+          |           |   |
90c51454cb6a4442475449720549ac883731506909Samuel Tan//    |    |                                             |No         |   |
91c51454cb6a4442475449720549ac883731506909Samuel Tan//    v    v                                             |           |   |
92c51454cb6a4442475449720549ac883731506909Samuel Tan// +--------------------+       +-------+                |           |   |
93c51454cb6a4442475449720549ac883731506909Samuel Tan// |BeforeSuspendActions|<------+Timeout|<---------------+       No  |   |
94c51454cb6a4442475449720549ac883731506909Samuel Tan// +--------------------+       +-------+<---------------------------+   |
95c51454cb6a4442475449720549ac883731506909Samuel Tan//         ^                                                             |
96c51454cb6a4442475449720549ac883731506909Samuel Tan//         |                   +-------------------+                     |
97c51454cb6a4442475449720549ac883731506909Samuel Tan//         +-------------------+ OnIPConfigUpdated/|             Yes     |
98c51454cb6a4442475449720549ac883731506909Samuel Tan//                             |OnIPv6ConfigUpdated|<--------------------+
99c51454cb6a4442475449720549ac883731506909Samuel Tan//                             +-------------------+
100c51454cb6a4442475449720549ac883731506909Samuel Tan//
101c51454cb6a4442475449720549ac883731506909Samuel Tan// BeforeSuspendActions
102c51454cb6a4442475449720549ac883731506909Samuel Tan// =====================
103c51454cb6a4442475449720549ac883731506909Samuel Tan// This function is run immediately before the system reports suspend readiness
104c51454cb6a4442475449720549ac883731506909Samuel Tan// to Manager. This is the common "exit path" taken by OnBeforeSuspend and
105c51454cb6a4442475449720549ac883731506909Samuel Tan// OnDarkResume before suspending.
106c51454cb6a4442475449720549ac883731506909Samuel Tan//
107c51454cb6a4442475449720549ac883731506909Samuel Tan//            Yes   +----------------------------+                   +---------+
108c51454cb6a4442475449720549ac883731506909Samuel Tan//          +-----> |Set Wake on Disconnect flag,+--+    +-------+   |Report   |
109c51454cb6a4442475449720549ac883731506909Samuel Tan//          |       |Start Lease Renewal Timer*  |  |    |Program|   |Suspend  |
110c51454cb6a4442475449720549ac883731506909Samuel Tan//          |       +----------------------------+  +--> |  NIC  |   |Readiness|
111c51454cb6a4442475449720549ac883731506909Samuel Tan// +--------+-+                                     |    +-+-----+   +---------+
112c51454cb6a4442475449720549ac883731506909Samuel Tan// |Connected?|                                     |      |   ^        ^
113c51454cb6a4442475449720549ac883731506909Samuel Tan// +--------+-+                                     |      |   |Failed  |
114c51454cb6a4442475449720549ac883731506909Samuel Tan//          |                                       |      v   |        |Success
115c51454cb6a4442475449720549ac883731506909Samuel Tan//          |       +----------------------------+  |  +-------+---+    |
116c51454cb6a4442475449720549ac883731506909Samuel Tan//          +-----> |Set Wake on SSID flag,      +--+  |  Verify   +----+
117c51454cb6a4442475449720549ac883731506909Samuel Tan//            No    |Start Wake To Scan Timer**  |     |Programming|
118c51454cb6a4442475449720549ac883731506909Samuel Tan//                  +----------------------------+     +-----------+
119c51454cb6a4442475449720549ac883731506909Samuel Tan//
120c51454cb6a4442475449720549ac883731506909Samuel Tan// *  if necessary (as indicated by caller of BeforeSuspendActions).
121c51454cb6a4442475449720549ac883731506909Samuel Tan// ** if we want to whitelist more SSIDs than our NIC supports.
122c51454cb6a4442475449720549ac883731506909Samuel Tan//
123c51454cb6a4442475449720549ac883731506909Samuel Tan// OnAfterResume
124c51454cb6a4442475449720549ac883731506909Samuel Tan// ==============
125c51454cb6a4442475449720549ac883731506909Samuel Tan// This is run after Manager announces that the system has fully resumed from
126c51454cb6a4442475449720549ac883731506909Samuel Tan// suspend.
127c51454cb6a4442475449720549ac883731506909Samuel Tan//
128c51454cb6a4442475449720549ac883731506909Samuel Tan// Wake on WiFi is disabled on the NIC if it was enabled before suspend or
129c51454cb6a4442475449720549ac883731506909Samuel Tan// dark resume, and both the wake to scan timer and DHCP lease renewal timers
130c51454cb6a4442475449720549ac883731506909Samuel Tan// are stopped.
131c51454cb6a4442475449720549ac883731506909Samuel Tan
1323a1bf99b484ad8a9a054518f49046221251d5612Samuel Tanclass WakeOnWiFi {
1333a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan public:
1340932171ae1b59449130783c28ac54332d06d4e23Samuel Tan  typedef std::pair<std::vector<uint8_t>, std::vector<uint32_t>>
1350932171ae1b59449130783c28ac54332d06d4e23Samuel Tan      SSIDFreqListPair;
1360932171ae1b59449130783c28ac54332d06d4e23Samuel Tan  typedef std::vector<std::pair<std::vector<uint8_t>, std::vector<uint32_t>>>
1370932171ae1b59449130783c28ac54332d06d4e23Samuel Tan      WakeOnSSIDResults;
1380932171ae1b59449130783c28ac54332d06d4e23Samuel Tan
1396d61faf84ef5eb2545bc6eee8a4d74109dfdd6d1Samuel Tan  // Types of triggers that we can program the NIC to wake the WiFi device.
1406d61faf84ef5eb2545bc6eee8a4d74109dfdd6d1Samuel Tan  enum WakeOnWiFiTrigger {
1416d61faf84ef5eb2545bc6eee8a4d74109dfdd6d1Samuel Tan    kWakeTriggerUnsupported = 0,  // Used for reporting, not programming NIC.
1426d61faf84ef5eb2545bc6eee8a4d74109dfdd6d1Samuel Tan    kWakeTriggerPattern = 1,
1436d61faf84ef5eb2545bc6eee8a4d74109dfdd6d1Samuel Tan    kWakeTriggerDisconnect = 2,
1446d61faf84ef5eb2545bc6eee8a4d74109dfdd6d1Samuel Tan    kWakeTriggerSSID = 3
1456d61faf84ef5eb2545bc6eee8a4d74109dfdd6d1Samuel Tan  };
1466d61faf84ef5eb2545bc6eee8a4d74109dfdd6d1Samuel Tan
147a757752df1b85d8af4590b64d0946b7b2bb0779fSamuel Tan  WakeOnWiFi(NetlinkManager *netlink_manager, EventDispatcher *dispatcher,
14866bddc67c71dd197211ca08c5a4835bd6cfa5ed2Samuel Tan             Metrics *metrics);
149fb02175e039a683cf9ac875ba0924858de6a08ffSamuel Tan  virtual ~WakeOnWiFi();
1503a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan
15196e35cf43e6d5aa2378cdae6fea507e5335d12bbSamuel Tan  // Registers |store| with properties related to wake on WiFi.
15296e35cf43e6d5aa2378cdae6fea507e5335d12bbSamuel Tan  void InitPropertyStore(PropertyStore *store);
15396e35cf43e6d5aa2378cdae6fea507e5335d12bbSamuel Tan
15466bddc67c71dd197211ca08c5a4835bd6cfa5ed2Samuel Tan  // Starts |metrics_timer_| so that wake on WiFi related metrics are
15566bddc67c71dd197211ca08c5a4835bd6cfa5ed2Samuel Tan  // periodically collected.
15666bddc67c71dd197211ca08c5a4835bd6cfa5ed2Samuel Tan  void StartMetricsTimer();
15766bddc67c71dd197211ca08c5a4835bd6cfa5ed2Samuel Tan
1583a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan  // Enable the NIC to wake on packets received from |ip_endpoint|.
1593a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan  // Note: The actual programming of the NIC only happens before the system
1603a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan  // suspends, in |OnBeforeSuspend|.
16196bdaecf58097e1cc54c5459e3c165de25c82134Samuel Tan  void AddWakeOnPacketConnection(const std::string &ip_endpoint, Error *error);
1623a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan  // Remove rule to wake on packets received from |ip_endpoint| from the NIC.
1633a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan  // Note: The actual programming of the NIC only happens before the system
1643a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan  // suspends, in |OnBeforeSuspend|.
16596bdaecf58097e1cc54c5459e3c165de25c82134Samuel Tan  void RemoveWakeOnPacketConnection(const std::string &ip_endpoint,
16696bdaecf58097e1cc54c5459e3c165de25c82134Samuel Tan                                    Error *error);
1673a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan  // Remove all rules to wake on incoming packets from the NIC.
1683a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan  // Note: The actual programming of the NIC only happens before the system
1693a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan  // suspends, in |OnBeforeSuspend|.
1703a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan  void RemoveAllWakeOnPacketConnections(Error *error);
1713a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan  // Given a NL80211_CMD_NEW_WIPHY message |nl80211_message|, parses the
17296bdaecf58097e1cc54c5459e3c165de25c82134Samuel Tan  // wake on WiFi capabilities of the NIC and set relevant members of this
1733a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan  // WakeOnWiFi object to reflect the supported capbilities.
174235dcec779704b782f0608421d890fe6d183e7ccSamuel Tan  virtual void ParseWakeOnWiFiCapabilities(
175235dcec779704b782f0608421d890fe6d183e7ccSamuel Tan      const Nl80211Message &nl80211_message);
1760932171ae1b59449130783c28ac54332d06d4e23Samuel Tan  // Callback invoked when the system reports its wakeup reason.
1770932171ae1b59449130783c28ac54332d06d4e23Samuel Tan  //
1780932171ae1b59449130783c28ac54332d06d4e23Samuel Tan  // Arguments:
1790932171ae1b59449130783c28ac54332d06d4e23Samuel Tan  //  - |netlink_message|: wakeup report message (note: must manually check
1800932171ae1b59449130783c28ac54332d06d4e23Samuel Tan  //    this message to make sure it is a wakeup report message).
1810932171ae1b59449130783c28ac54332d06d4e23Samuel Tan  //
1820932171ae1b59449130783c28ac54332d06d4e23Samuel Tan  // Note: Assumes only one wakeup reason is received. If more than one is
1830932171ae1b59449130783c28ac54332d06d4e23Samuel Tan  // received, the only first one parsed will be handled.
1840932171ae1b59449130783c28ac54332d06d4e23Samuel Tan  virtual void OnWakeupReasonReceived(const NetlinkMessage &netlink_message);
185c51454cb6a4442475449720549ac883731506909Samuel Tan  // Performs pre-suspend actions relevant to wake on WiFi functionality.
186787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan  //
187787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan  // Arguments:
188787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan  //  - |is_connected|: whether the WiFi device is connected.
189d70ecdf36c94516b71cca8212caeb71da8c63d02Samuel Tan  //  - |ssid_whitelist|: list of SSIDs that the NIC will be programmed to wake
190d70ecdf36c94516b71cca8212caeb71da8c63d02Samuel Tan  //    the system on if the NIC is programmed to wake on SSID.
191787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan  //  - |done_callback|: callback to invoke when suspend  actions have
192787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan  //    completed.
193787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan  //  - |renew_dhcp_lease_callback|: callback to invoke to initiate DHCP lease
194787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan  //    renewal.
195787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan  //  - |remove_supplicant_networks_callback|: callback to invoke
196787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan  //    to remove all networks from WPA supplicant.
197787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan  //  - |have_dhcp_lease|: whether or not there is a DHCP lease to renew.
198787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan  //  - |time_to_next_lease_renewal|: number of seconds until next DHCP lease
199787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan  //    renewal is due.
200787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan  virtual void OnBeforeSuspend(
201787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan      bool is_connected,
202d70ecdf36c94516b71cca8212caeb71da8c63d02Samuel Tan      const std::vector<ByteString> &ssid_whitelist,
203787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan      const ResultCallback &done_callback,
204787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan      const base::Closure &renew_dhcp_lease_callback,
205787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan      const base::Closure &remove_supplicant_networks_callback,
206787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan      bool have_dhcp_lease,
207787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan      uint32_t time_to_next_lease_renewal);
2083a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan  // Performs post-resume actions relevant to wake on wireless functionality.
209fb02175e039a683cf9ac875ba0924858de6a08ffSamuel Tan  virtual void OnAfterResume();
210787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan  // Performs and post actions to be performed in dark resume.
211787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan  //
212787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan  // Arguments:
213787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan  //  - |is_connected|: whether the WiFi device is connected.
214d70ecdf36c94516b71cca8212caeb71da8c63d02Samuel Tan  //  - |ssid_whitelist|: list of SSIDs that the NIC will be programmed to wake
215d70ecdf36c94516b71cca8212caeb71da8c63d02Samuel Tan  //    the system on if the NIC is programmed to wake on SSID.
216787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan  //  - |done_callback|: callback to invoke when dark resume actions have
217787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan  //    completed.
218787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan  //  - |renew_dhcp_lease_callback|: callback to invoke to initiate DHCP lease
219787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan  //    renewal.
220787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan  //  - |initate_scan_callback|: callback to invoke to initiate a scan.
221787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan  //  - |remove_supplicant_networks_callback|: callback to invoke
222787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan  //    to remove all networks from WPA supplicant.
223787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan  virtual void OnDarkResume(
224787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan      bool is_connected,
225d70ecdf36c94516b71cca8212caeb71da8c63d02Samuel Tan      const std::vector<ByteString> &ssid_whitelist,
226787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan      const ResultCallback &done_callback,
227787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan      const base::Closure &renew_dhcp_lease_callback,
228787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan      const base::Closure &initiate_scan_callback,
229787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan      const base::Closure &remove_supplicant_networks_callback);
230787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan  // Wrapper around WakeOnWiFi::BeforeSuspendActions that checks if shill is
231787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan  // currently in dark resume before invoking the function.
232787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan  virtual void OnDHCPLeaseObtained(bool start_lease_renewal_timer,
233787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan                                   uint32_t time_to_next_lease_renewal);
2341964b5de186ef06fa89a78b27c3a112686bc36f7Samuel Tan  // Callback invoked to report whether this WiFi device is connected to
2351964b5de186ef06fa89a78b27c3a112686bc36f7Samuel Tan  // a service after waking from suspend.
2361964b5de186ef06fa89a78b27c3a112686bc36f7Samuel Tan  virtual void ReportConnectedToServiceAfterWake(bool is_connected);
237402bf2959e8b2c68627a7b66203ec84760bf2e03Samuel Tan  // Called in WiFi::ScanDoneTask when there are no WiFi services available
238402bf2959e8b2c68627a7b66203ec84760bf2e03Samuel Tan  // for auto-connect after a scan.
239402bf2959e8b2c68627a7b66203ec84760bf2e03Samuel Tan  virtual void OnNoAutoConnectableServicesAfterScan(
240d70ecdf36c94516b71cca8212caeb71da8c63d02Samuel Tan      const std::vector<ByteString> &ssid_whitelist,
241402bf2959e8b2c68627a7b66203ec84760bf2e03Samuel Tan      const base::Closure &remove_supplicant_networks_callback);
242d1bec5d563afc9074a1875e2302d5ecf54c95f95Samuel Tan  // Called by WiFi when it is notified by the kernel that a scan has started.
243d1bec5d563afc9074a1875e2302d5ecf54c95f95Samuel Tan  // If |is_active_scan| is true, the scan is an active scan. Otherwise, the
244d1bec5d563afc9074a1875e2302d5ecf54c95f95Samuel Tan  // scan is a passive scan.
245d1bec5d563afc9074a1875e2302d5ecf54c95f95Samuel Tan  virtual void OnScanStarted(bool is_active_scan);
2463a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan
2470e0ac0de9ac592495f780d7c372ba1212d4d374cSamuel Tan  bool in_dark_resume() { return in_dark_resume_; }
2480e0ac0de9ac592495f780d7c372ba1212d4d374cSamuel Tan
249235dcec779704b782f0608421d890fe6d183e7ccSamuel Tan  virtual void OnWiphyIndexReceived(uint32_t index);
250235dcec779704b782f0608421d890fe6d183e7ccSamuel Tan
2513a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan private:
2523a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan  friend class WakeOnWiFiTest;  // access to several members for tests
2533a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan  friend class WiFiObjectTest;  // netlink_manager_
254e58fa86282bb07867793ba78e9ff1c56fdc2d2b3Samuel Tan  // Tests that need kWakeOnWiFiNotSupported.
255e58fa86282bb07867793ba78e9ff1c56fdc2d2b3Samuel Tan  FRIEND_TEST(WakeOnWiFiTestWithMockDispatcher,
256e58fa86282bb07867793ba78e9ff1c56fdc2d2b3Samuel Tan              WakeOnWiFiDisabled_SetWakeOnWiFiFeaturesEnabled);
257787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan  FRIEND_TEST(WakeOnWiFiTestWithMockDispatcher,
258787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan              WakeOnWiFiDisabled_AddWakeOnPacketConnection_ReturnsError);
259787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan  FRIEND_TEST(WakeOnWiFiTestWithMockDispatcher,
260787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan              WakeOnWiFiDisabled_RemoveWakeOnPacketConnection_ReturnsError);
261787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan  FRIEND_TEST(WakeOnWiFiTestWithMockDispatcher,
262787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan              WakeOnWiFiDisabled_RemoveAllWakeOnPacketConnections_ReturnsError);
263fb02175e039a683cf9ac875ba0924858de6a08ffSamuel Tan  // Tests that need kMaxSetWakeOnPacketRetries.
264787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan  FRIEND_TEST(WakeOnWiFiTestWithMockDispatcher,
2653a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan              RetrySetWakeOnPacketConnections_LessThanMaxRetries);
266787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan  FRIEND_TEST(WakeOnWiFiTestWithMockDispatcher,
2673a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan              RetrySetWakeOnPacketConnections_MaxAttemptsWithCallbackSet);
268787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan  FRIEND_TEST(WakeOnWiFiTestWithMockDispatcher,
2693a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan              RetrySetWakeOnPacketConnections_MaxAttemptsCallbackUnset);
270787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan  // Tests that need WakeOnWiFi::kDarkResumeActionsTimeoutMilliseconds
271787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan  FRIEND_TEST(WakeOnWiFiTestWithMockDispatcher,
272787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan              OnBeforeSuspend_DHCPLeaseRenewal);
273bc78c1fefe3bd5d3e4397cb1b4b42cc78ad1e87eSamuel Tan  // Tests that need WakeOnWiFi::kMaxDarkResumesPerPeriodShort
274ff59a1896ea250b4450b54db697692e0b1949528Samuel Tan  FRIEND_TEST(WakeOnWiFiTestWithDispatcher, OnBeforeSuspend_ClearsEventHistory);
275bc78c1fefe3bd5d3e4397cb1b4b42cc78ad1e87eSamuel Tan  FRIEND_TEST(WakeOnWiFiTestWithDispatcher,
276bc78c1fefe3bd5d3e4397cb1b4b42cc78ad1e87eSamuel Tan              OnDarkResume_NotConnected_MaxDarkResumes_ShortPeriod);
277bc78c1fefe3bd5d3e4397cb1b4b42cc78ad1e87eSamuel Tan  // Tests that need WakeOnWiFi::kMaxDarkResumesPerPeriodLong
278bc78c1fefe3bd5d3e4397cb1b4b42cc78ad1e87eSamuel Tan  FRIEND_TEST(WakeOnWiFiTestWithDispatcher,
279bc78c1fefe3bd5d3e4397cb1b4b42cc78ad1e87eSamuel Tan              OnDarkResume_NotConnected_MaxDarkResumes_LongPeriod);
2803a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan
281fb02175e039a683cf9ac875ba0924858de6a08ffSamuel Tan  static const char kWakeOnIPAddressPatternsNotSupported[];
282e58fa86282bb07867793ba78e9ff1c56fdc2d2b3Samuel Tan  static const char kWakeOnWiFiNotSupported[];
283f8c0b3bcc1b9e918400c1fe76a9f6991bc32b55bSamuel Tan  static const int kVerifyWakeOnWiFiSettingsDelayMilliseconds;
2843a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan  static const int kMaxSetWakeOnPacketRetries;
28566bddc67c71dd197211ca08c5a4835bd6cfa5ed2Samuel Tan  static const int kMetricsReportingFrequencySeconds;
2865768ad4f08851b73eddb3328794eaa0ab3094bd3Samuel Tan  static const uint32_t kDefaultWakeToScanPeriodSeconds;
2877804df459a7f29a7463dbd7f0e8e2fea93ee766dSamuel Tan  static const uint32_t kDefaultNetDetectScanPeriodSeconds;
288787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan  static const uint32_t kImmediateDHCPLeaseRenewalThresholdSeconds;
289bc78c1fefe3bd5d3e4397cb1b4b42cc78ad1e87eSamuel Tan  static const int kDarkResumeFrequencySamplingPeriodShortMinutes;
290bc78c1fefe3bd5d3e4397cb1b4b42cc78ad1e87eSamuel Tan  static const int kDarkResumeFrequencySamplingPeriodLongMinutes;
291bc78c1fefe3bd5d3e4397cb1b4b42cc78ad1e87eSamuel Tan  static const int kMaxDarkResumesPerPeriodShort;
292bc78c1fefe3bd5d3e4397cb1b4b42cc78ad1e87eSamuel Tan  static const int kMaxDarkResumesPerPeriodLong;
293787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan  static int64_t DarkResumeActionsTimeoutMilliseconds;  // non-const for testing
2943a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan
29596e35cf43e6d5aa2378cdae6fea507e5335d12bbSamuel Tan  std::string GetWakeOnWiFiFeaturesEnabled(Error *error);
29696e35cf43e6d5aa2378cdae6fea507e5335d12bbSamuel Tan  bool SetWakeOnWiFiFeaturesEnabled(const std::string &enabled, Error *error);
297740ee07e13d98ee9e0535ffea391326c8da13499Samuel Tan  // Helper function to run and reset |suspend_actions_done_callback_|.
298740ee07e13d98ee9e0535ffea391326c8da13499Samuel Tan  void RunAndResetSuspendActionsDoneCallback(const Error &error);
2993a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan  // Used for comparison of ByteString pairs in a set.
3003a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan  static bool ByteStringPairIsLessThan(
3013a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan      const std::pair<ByteString, ByteString> &lhs,
3023a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan      const std::pair<ByteString, ByteString> &rhs);
3033a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan  // Creates a mask which specifies which bytes in pattern of length
3043a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan  // |pattern_len| to match against. Bits |offset| to |pattern_len| - 1 are set,
3053a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan  // which bits 0 to bits 0 to |offset| - 1 are unset. This mask is saved in
3063a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan  // |mask|.
3073a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan  static void SetMask(ByteString *mask, uint32_t pattern_len, uint32_t offset);
3083a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan  // Creates a pattern and mask for a NL80211 message that programs the NIC to
3093a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan  // wake on packets originating from IP address |ip_addr|. The pattern and mask
3103a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan  // are saved in |pattern| and |mask| respectively. Returns true iff the
3113a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan  // pattern and mask are successfully created and written to |pattern| and
3123a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan  // |mask| respectively.
3133a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan  static bool CreateIPAddressPatternAndMask(const IPAddress &ip_addr,
3143a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan                                            ByteString *pattern,
3153a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan                                            ByteString *mask);
3163a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan  static void CreateIPV4PatternAndMask(const IPAddress &ip_addr,
3173a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan                                       ByteString *pattern, ByteString *mask);
3183a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan  static void CreateIPV6PatternAndMask(const IPAddress &ip_addr,
3193a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan                                       ByteString *pattern, ByteString *mask);
3203a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan  // Creates and sets an attribute in a NL80211 message |msg| which indicates
3213a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan  // the index of the wiphy interface to program. Returns true iff |msg| is
3223a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan  // successfully configured.
3233a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan  static bool ConfigureWiphyIndex(Nl80211Message *msg, int32_t index);
3243a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan  // Creates and sets attributes in an SetWakeOnPacketConnMessage |msg| so that
3253a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan  // the message will disable wake-on-packet functionality of the NIC with wiphy
3263a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan  // index |wiphy_index|. Returns true iff |msg| is successfully configured.
3273a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan  // NOTE: Assumes that |msg| has not been altered since construction.
3283a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan  static bool ConfigureDisableWakeOnWiFiMessage(SetWakeOnPacketConnMessage *msg,
3293a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan                                                uint32_t wiphy_index,
3303a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan                                                Error *error);
3313a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan  // Creates and sets attributes in a SetWakeOnPacketConnMessage |msg|
3323a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan  // so that the message will program the NIC with wiphy index |wiphy_index|
3333a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan  // with wake on wireless triggers in |trigs|. If |trigs| contains the
3346d61faf84ef5eb2545bc6eee8a4d74109dfdd6d1Samuel Tan  // kWakeTriggerPattern trigger, the message is configured to program the NIC
3356d61faf84ef5eb2545bc6eee8a4d74109dfdd6d1Samuel Tan  // to wake on packets from the IP addresses in |addrs|. If |trigs| contains
3366d61faf84ef5eb2545bc6eee8a4d74109dfdd6d1Samuel Tan  // the kSSID trigger, the message is configured to program the NIC to wake on
3376d61faf84ef5eb2545bc6eee8a4d74109dfdd6d1Samuel Tan  // the SSIDs in |ssid_whitelist|.
338d70ecdf36c94516b71cca8212caeb71da8c63d02Samuel Tan  // Returns true iff |msg| is successfully configured.
3393a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan  // NOTE: Assumes that |msg| has not been altered since construction.
3403a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan  static bool ConfigureSetWakeOnWiFiSettingsMessage(
3413a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan      SetWakeOnPacketConnMessage *msg, const std::set<WakeOnWiFiTrigger> &trigs,
342d70ecdf36c94516b71cca8212caeb71da8c63d02Samuel Tan      const IPAddressStore &addrs, uint32_t wiphy_index,
343d70ecdf36c94516b71cca8212caeb71da8c63d02Samuel Tan      uint32_t net_detect_scan_period_seconds,
344d70ecdf36c94516b71cca8212caeb71da8c63d02Samuel Tan      const std::vector<ByteString> &ssid_whitelist,
345d70ecdf36c94516b71cca8212caeb71da8c63d02Samuel Tan      Error *error);
3463a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan  // Helper function to ConfigureSetWakeOnWiFiSettingsMessage that creates a
3473a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan  // single nested attribute inside the attribute list referenced by |patterns|
3483a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan  // representing a wake-on-packet pattern matching rule with index |patnum|.
3493a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan  // Returns true iff the attribute is successfully created and set.
3503a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan  // NOTE: |patterns| is assumed to reference the nested attribute list
3513a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan  // NL80211_WOWLAN_TRIG_PKT_PATTERN.
3523a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan  // NOTE: |patnum| should be unique across multiple calls to this function to
3533a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan  // prevent the formation of a erroneous nl80211 message or the overwriting of
3543a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan  // pattern matching rules.
3553a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan  static bool CreateSinglePattern(const IPAddress &ip_addr,
3563a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan                                  AttributeListRefPtr patterns, uint8_t patnum,
3573a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan                                  Error *error);
3583a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan  // Creates and sets attributes in an GetWakeOnPacketConnMessage msg| so that
3593a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan  // the message will request for wake-on-packet settings information from the
3603a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan  // NIC with wiphy index |wiphy_index|. Returns true iff |msg| is successfully
3613a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan  // configured.
3623a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan  // NOTE: Assumes that |msg| has not been altered since construction.
3633a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan  static bool ConfigureGetWakeOnWiFiSettingsMessage(
3643a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan      GetWakeOnPacketConnMessage *msg, uint32_t wiphy_index, Error *error);
3653a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan  // Given a NL80211_CMD_GET_WOWLAN response or NL80211_CMD_SET_WOWLAN request
3663a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan  // |msg|, returns true iff the wake-on-wifi trigger settings in |msg| match
367d70ecdf36c94516b71cca8212caeb71da8c63d02Samuel Tan  // those in |trigs|. Performs the following checks for the following triggers:
3686d61faf84ef5eb2545bc6eee8a4d74109dfdd6d1Samuel Tan  // - kWakeTriggerDisconnect: checks that the wake on disconnect flag is
3696d61faf84ef5eb2545bc6eee8a4d74109dfdd6d1Samuel Tan  //   present and set.
370d70ecdf36c94516b71cca8212caeb71da8c63d02Samuel Tan  // - kIPAddress: checks that source IP addresses in |msg| match those reported
371d70ecdf36c94516b71cca8212caeb71da8c63d02Samuel Tan  //   in |addrs|.
372d70ecdf36c94516b71cca8212caeb71da8c63d02Samuel Tan  // - kSSID: checks that the SSIDs in |ssid_whitelist| and the scan interval
373d70ecdf36c94516b71cca8212caeb71da8c63d02Samuel Tan  //   |net_detect_scan_period_seconds| match those reported in |msg|.
374d70ecdf36c94516b71cca8212caeb71da8c63d02Samuel Tan  // Note: finding a trigger is in |msg| that is not expected based on the flags
375d70ecdf36c94516b71cca8212caeb71da8c63d02Samuel Tan  // in |trig| also counts as a mismatch.
376d70ecdf36c94516b71cca8212caeb71da8c63d02Samuel Tan  static bool WakeOnWiFiSettingsMatch(
377d70ecdf36c94516b71cca8212caeb71da8c63d02Samuel Tan      const Nl80211Message &msg, const std::set<WakeOnWiFiTrigger> &trigs,
378d70ecdf36c94516b71cca8212caeb71da8c63d02Samuel Tan      const IPAddressStore &addrs, uint32_t net_detect_scan_period_seconds,
379d70ecdf36c94516b71cca8212caeb71da8c63d02Samuel Tan      const std::vector<ByteString> &ssid_whitelist);
380742866741cd8902bfabd3d9731ccc6b4d5052de6Samuel Tan  // Handler for NL80211 message error responses from NIC wake on WiFi setting
381742866741cd8902bfabd3d9731ccc6b4d5052de6Samuel Tan  // programming attempts.
382742866741cd8902bfabd3d9731ccc6b4d5052de6Samuel Tan  void OnWakeOnWiFiSettingsErrorResponse(
383742866741cd8902bfabd3d9731ccc6b4d5052de6Samuel Tan      NetlinkManager::AuxilliaryMessageType type,
384742866741cd8902bfabd3d9731ccc6b4d5052de6Samuel Tan      const NetlinkMessage *raw_message);
3853a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan  // Message handler for NL80211_CMD_SET_WOWLAN responses.
3863a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan  static void OnSetWakeOnPacketConnectionResponse(
3873a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan      const Nl80211Message &nl80211_message);
3883a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan  // Request wake on WiFi settings for this WiFi device.
3893a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan  void RequestWakeOnPacketSettings();
3903a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan  // Verify that the wake on WiFi settings programmed into the NIC match
391d70ecdf36c94516b71cca8212caeb71da8c63d02Samuel Tan  // those recorded locally for this device in |wake_on_packet_connections_|,
392d70ecdf36c94516b71cca8212caeb71da8c63d02Samuel Tan  // |wake_on_wifi_triggers_|, and |wake_on_ssid_whitelist_|.
3933a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan  void VerifyWakeOnWiFiSettings(const Nl80211Message &nl80211_message);
3943a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan  // Sends an NL80211 message to program the NIC with wake on WiFi settings
395d70ecdf36c94516b71cca8212caeb71da8c63d02Samuel Tan  // configured in |wake_on_packet_connections_|, |wake_on_ssid_whitelist_|, and
396d70ecdf36c94516b71cca8212caeb71da8c63d02Samuel Tan  // |wake_on_wifi_triggers_|. If |wake_on_wifi_triggers_| is empty, calls
397d70ecdf36c94516b71cca8212caeb71da8c63d02Samuel Tan  // WakeOnWiFi::DisableWakeOnWiFi.
3983a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan  void ApplyWakeOnWiFiSettings();
3993a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan  // Helper function called by |ApplyWakeOnWiFiSettings| that sends an NL80211
4003a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan  // message to program the NIC to disable wake on WiFi.
4013a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan  void DisableWakeOnWiFi();
4023a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan  // Calls |ApplyWakeOnWiFiSettings| and counts this call as
4033a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan  // a retry. If |kMaxSetWakeOnPacketRetries| retries have already been
4043a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan  // performed, resets counter and returns.
4053a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan  void RetrySetWakeOnPacketConnections();
406cb3ecf321c52de1e72cdf037a459cd893b7c51dbSamuel Tan  // Utility functions to check which wake on WiFi features are currently
407787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan  // enabled based on the descriptor |wake_on_wifi_features_enabled_| and
408787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan  // are supported by the NIC.
409787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan  bool WakeOnPacketEnabledAndSupported();
410787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan  bool WakeOnSSIDEnabledAndSupported();
41166bddc67c71dd197211ca08c5a4835bd6cfa5ed2Samuel Tan  // Called by metrics_timer_ to reports metrics.
41266bddc67c71dd197211ca08c5a4835bd6cfa5ed2Samuel Tan  void ReportMetrics();
413787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan  // Actions executed before normal suspend and dark resume suspend.
414787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan  //
415787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan  // Arguments:
416787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan  //  - |is_connected|: whether the WiFi device is connected.
417787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan  //  - |start_lease_renewal_timer|: whether or not to start the DHCP lease
418787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan  //    renewal timer.
419787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan  //  - |time_to_next_lease_renewal|: number of seconds until next DHCP lease
420787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan  //    renewal is due.
421787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan  //  - |remove_supplicant_networks_callback|: callback to invoke
422787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan  //    to remove all networks from WPA supplicant.
423787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan  void BeforeSuspendActions(
424787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan      bool is_connected,
425787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan      bool start_lease_renewal_timer,
426787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan      uint32_t time_to_next_lease_renewal,
427787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan      const base::Closure &remove_supplicant_networks_callback);
428787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan
429787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan  // Needed for |dhcp_lease_renewal_timer_| and |wake_to_scan_timer_| since
430787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan  // passing a empty base::Closure() causes a run-time DCHECK error when
431787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan  // AlarmTimer::Start or AlarmTimer::Reset are called.
432787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan  void OnTimerWakeDoNothing() {}
43366bddc67c71dd197211ca08c5a4835bd6cfa5ed2Samuel Tan
4340932171ae1b59449130783c28ac54332d06d4e23Samuel Tan  // Parses an attribute list containing the SSID matches that caused the
4350932171ae1b59449130783c28ac54332d06d4e23Samuel Tan  // system wake, along with the corresponding channels that these SSIDs were
4360932171ae1b59449130783c28ac54332d06d4e23Samuel Tan  // detected in. Returns a list on wake on SSID results (SSID-frequency list
4370932171ae1b59449130783c28ac54332d06d4e23Samuel Tan  // pairs) representing the reported SSID matches.
4380932171ae1b59449130783c28ac54332d06d4e23Samuel Tan  //
4390932171ae1b59449130783c28ac54332d06d4e23Samuel Tan  // Arguments:
4400932171ae1b59449130783c28ac54332d06d4e23Samuel Tan  //  - |results_list|: Nested attribute list containing an array of nested
4410932171ae1b59449130783c28ac54332d06d4e23Samuel Tan  //    attributes which contain the NL80211_ATTR_SSID or
4420932171ae1b59449130783c28ac54332d06d4e23Samuel Tan  //    NL80211_ATTR_SCAN_FREQUENCIES attributes. This attribute list is assumed
4430932171ae1b59449130783c28ac54332d06d4e23Samuel Tan  //    to have been extracted from a NL80211_CMD_SET_WOWLAN response message
4440932171ae1b59449130783c28ac54332d06d4e23Samuel Tan  //    using the NL80211_WOWLAN_TRIG_NET_DETECT_RESULTS id.
4450932171ae1b59449130783c28ac54332d06d4e23Samuel Tan  static WakeOnSSIDResults ParseWakeOnWakeOnSSIDResults(
4460932171ae1b59449130783c28ac54332d06d4e23Samuel Tan      AttributeListConstRefPtr results_list);
4470932171ae1b59449130783c28ac54332d06d4e23Samuel Tan
4483a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan  // Pointers to objects owned by the WiFi object that created this object.
4493a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan  EventDispatcher *dispatcher_;
4503a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan  NetlinkManager *netlink_manager_;
45166bddc67c71dd197211ca08c5a4835bd6cfa5ed2Samuel Tan  Metrics *metrics_;
4523a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan  // Executes after the NIC's wake-on-packet settings are configured via
4533a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan  // NL80211 messages to verify that the new configuration has taken effect.
4543a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan  // Calls RequestWakeOnPacketSettings.
4553a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan  base::CancelableClosure verify_wake_on_packet_settings_callback_;
456787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan  // Callback to be invoked after all suspend actions finish executing both
457787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan  // before regular suspend and before suspend in dark resume.
4583a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan  ResultCallback suspend_actions_done_callback_;
45966bddc67c71dd197211ca08c5a4835bd6cfa5ed2Samuel Tan  // Callback to report wake on WiFi related metrics.
46066bddc67c71dd197211ca08c5a4835bd6cfa5ed2Samuel Tan  base::CancelableClosure report_metrics_callback_;
4613a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan  // Number of retry attempts to program the NIC's wake-on-packet settings.
4623a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan  int num_set_wake_on_packet_retries_;
4633a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan  // Keeps track of triggers that the NIC will be programmed to wake from
4643a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan  // while suspended.
4653a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan  std::set<WakeOnWiFi::WakeOnWiFiTrigger> wake_on_wifi_triggers_;
4663a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan  // Keeps track of what wake on wifi triggers this WiFi device supports.
4673a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan  std::set<WakeOnWiFi::WakeOnWiFiTrigger> wake_on_wifi_triggers_supported_;
468dda4da49919c0d10d84bf70ef4313837345cfb6cSamuel Tan  // Max number of patterns this WiFi device can be programmed to wake on at one
469dda4da49919c0d10d84bf70ef4313837345cfb6cSamuel Tan  // time.
4703a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan  size_t wake_on_wifi_max_patterns_;
471dda4da49919c0d10d84bf70ef4313837345cfb6cSamuel Tan  // Max number of SSIDs this WiFi device can be programmed to wake on at one
472dda4da49919c0d10d84bf70ef4313837345cfb6cSamuel Tan  // time.
473dda4da49919c0d10d84bf70ef4313837345cfb6cSamuel Tan  uint32_t wake_on_wifi_max_ssids_;
4743a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan  // Keeps track of IP addresses whose packets this device will wake upon
475d70ecdf36c94516b71cca8212caeb71da8c63d02Samuel Tan  // receiving while the device is suspended. Only used if the NIC is programmed
476d70ecdf36c94516b71cca8212caeb71da8c63d02Samuel Tan  // to wake on IP address patterns.
4773a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan  IPAddressStore wake_on_packet_connections_;
478d70ecdf36c94516b71cca8212caeb71da8c63d02Samuel Tan  // Keeps track of SSIDs that this device will wake on the appearance of while
479d70ecdf36c94516b71cca8212caeb71da8c63d02Samuel Tan  // the device is suspended. Only used if the NIC is programmed to wake on
480d70ecdf36c94516b71cca8212caeb71da8c63d02Samuel Tan  // SSIDs.
481d70ecdf36c94516b71cca8212caeb71da8c63d02Samuel Tan  std::vector<ByteString> wake_on_ssid_whitelist_;
4823a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan  uint32_t wiphy_index_;
4833a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan  bool wiphy_index_received_;
48496e35cf43e6d5aa2378cdae6fea507e5335d12bbSamuel Tan  // Describes the wake on WiFi features that are currently enabled.
48596e35cf43e6d5aa2378cdae6fea507e5335d12bbSamuel Tan  std::string wake_on_wifi_features_enabled_;
486787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan  // Timer that wakes the system to renew DHCP leases.
487787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan  timers::AlarmTimer dhcp_lease_renewal_timer_;
488787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan  // Timer that wakes the system to scan for networks.
489787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan  timers::AlarmTimer wake_to_scan_timer_;
490787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan  // Executes when the dark resume actions timer expires. Calls
491787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan  // ScanTimerHandler.
492787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan  base::CancelableClosure dark_resume_actions_timeout_callback_;
493787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan  // Whether shill is currently in dark resume.
494787a1cebf2452c14bf392ab99902e686ea4a6fb4Samuel Tan  bool in_dark_resume_;
4955768ad4f08851b73eddb3328794eaa0ab3094bd3Samuel Tan  // Period (in seconds) between instances where the system wakes from suspend
4965768ad4f08851b73eddb3328794eaa0ab3094bd3Samuel Tan  // to scan for networks in dark resume.
4975768ad4f08851b73eddb3328794eaa0ab3094bd3Samuel Tan  uint32_t wake_to_scan_period_seconds_;
4987804df459a7f29a7463dbd7f0e8e2fea93ee766dSamuel Tan  // Period (in seconds) between instances where the NIC performs Net Detect
4997804df459a7f29a7463dbd7f0e8e2fea93ee766dSamuel Tan  // scans while the system is suspended.
5007804df459a7f29a7463dbd7f0e8e2fea93ee766dSamuel Tan  uint32_t net_detect_scan_period_seconds_;
501bc78c1fefe3bd5d3e4397cb1b4b42cc78ad1e87eSamuel Tan  // Timestamps of dark resume wakes that took place during the current
502bc78c1fefe3bd5d3e4397cb1b4b42cc78ad1e87eSamuel Tan  // or most recent suspend.
503bc78c1fefe3bd5d3e4397cb1b4b42cc78ad1e87eSamuel Tan  EventHistory dark_resume_history_;
5046d61faf84ef5eb2545bc6eee8a4d74109dfdd6d1Samuel Tan  // Last wake reason reported by the kernel.
5056d61faf84ef5eb2545bc6eee8a4d74109dfdd6d1Samuel Tan  WakeOnWiFiTrigger last_wake_reason_;
506c49955996cfa8f28bf3c112499351be7870319b1Samuel Tan  // Whether or not to always start |wake_to_scan_timer_| before suspend.
507c49955996cfa8f28bf3c112499351be7870319b1Samuel Tan  bool force_wake_to_scan_timer_;
50866bddc67c71dd197211ca08c5a4835bd6cfa5ed2Samuel Tan
5093a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan  base::WeakPtrFactory<WakeOnWiFi> weak_ptr_factory_;
510d03b95db5d05928ea63fc9f8aac5151e93c43a2fSamuel Tan
5113a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan  DISALLOW_COPY_AND_ASSIGN(WakeOnWiFi);
5123a1bf99b484ad8a9a054518f49046221251d5612Samuel Tan};
513d03b95db5d05928ea63fc9f8aac5151e93c43a2fSamuel Tan
514d03b95db5d05928ea63fc9f8aac5151e93c43a2fSamuel Tan}  // namespace shill
515d03b95db5d05928ea63fc9f8aac5151e93c43a2fSamuel Tan
516d6a8b519e3057f52313e502f502a4819a942e089Ben Chan#endif  // SHILL_WIFI_WAKE_ON_WIFI_H_
517