Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "shill/power_manager.h" |
| 6 | |
| 7 | #include <map> |
| 8 | #include <string> |
| 9 | |
Prathmesh Prabhu | d9c786c | 2014-04-23 17:37:28 -0700 | [diff] [blame] | 10 | #include <base/bind.h> |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 11 | #include <base/stl_util.h> |
Prathmesh Prabhu | d9c786c | 2014-04-23 17:37:28 -0700 | [diff] [blame] | 12 | #include <chromeos/dbus/service_constants.h> |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 13 | |
Prathmesh Prabhu | d9c786c | 2014-04-23 17:37:28 -0700 | [diff] [blame] | 14 | #include "shill/dbus_manager.h" |
Darin Petkov | 3ec5534 | 2012-09-28 14:04:44 +0200 | [diff] [blame] | 15 | #include "shill/event_dispatcher.h" |
Christopher Wiley | b691efd | 2012-08-09 13:51:51 -0700 | [diff] [blame] | 16 | #include "shill/logging.h" |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 17 | #include "shill/power_manager_proxy_interface.h" |
| 18 | #include "shill/proxy_factory.h" |
| 19 | |
Prathmesh Prabhu | d9c786c | 2014-04-23 17:37:28 -0700 | [diff] [blame] | 20 | using base::Bind; |
| 21 | using base::TimeDelta; |
| 22 | using base::Unretained; |
Gary Morain | 52fabab | 2012-08-22 09:27:31 -0700 | [diff] [blame] | 23 | using std::map; |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 24 | using std::string; |
| 25 | |
| 26 | namespace shill { |
| 27 | |
Prathmesh Prabhu | 9fdb84b | 2014-08-21 18:41:58 -0700 | [diff] [blame] | 28 | // static |
Prathmesh Prabhu | 0f4e0a4 | 2014-08-27 15:29:00 -0700 | [diff] [blame] | 29 | const int PowerManager::kInvalidSuspendId = -1; |
Prathmesh Prabhu | 9fdb84b | 2014-08-21 18:41:58 -0700 | [diff] [blame] | 30 | const char PowerManager::kSuspendDelayDescription[] = "shill"; |
Prathmesh Prabhu | 64ad238 | 2014-08-26 11:19:30 -0700 | [diff] [blame] | 31 | const char PowerManager::kDarkSuspendDelayDescription[] = "shill"; |
Darin Petkov | 3ec5534 | 2012-09-28 14:04:44 +0200 | [diff] [blame] | 32 | const int PowerManager::kSuspendTimeoutMilliseconds = 15 * 1000; |
| 33 | |
Paul Stewart | 1a212a6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 34 | PowerManager::PowerManager(EventDispatcher* dispatcher, |
| 35 | ProxyFactory* proxy_factory) |
Darin Petkov | 3ec5534 | 2012-09-28 14:04:44 +0200 | [diff] [blame] | 36 | : dispatcher_(dispatcher), |
| 37 | power_manager_proxy_(proxy_factory->CreatePowerManagerProxy(this)), |
Prathmesh Prabhu | 9fdb84b | 2014-08-21 18:41:58 -0700 | [diff] [blame] | 38 | suspend_delay_registered_(false), |
| 39 | suspend_delay_id_(0), |
Prathmesh Prabhu | 64ad238 | 2014-08-26 11:19:30 -0700 | [diff] [blame] | 40 | dark_suspend_delay_registered_(false), |
| 41 | dark_suspend_delay_id_(0), |
Prathmesh Prabhu | 9fdb84b | 2014-08-21 18:41:58 -0700 | [diff] [blame] | 42 | suspending_(false), |
Samuel Tan | 787a1ce | 2014-11-11 17:17:27 -0800 | [diff] [blame] | 43 | in_dark_resume_(false), |
Prathmesh Prabhu | 64ad238 | 2014-08-26 11:19:30 -0700 | [diff] [blame] | 44 | current_suspend_id_(0), |
| 45 | current_dark_suspend_id_(0) {} |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 46 | |
Prathmesh Prabhu | 9fdb84b | 2014-08-21 18:41:58 -0700 | [diff] [blame] | 47 | PowerManager::~PowerManager() {} |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 48 | |
Prathmesh Prabhu | 9fdb84b | 2014-08-21 18:41:58 -0700 | [diff] [blame] | 49 | void PowerManager::Start( |
Paul Stewart | 1a212a6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 50 | DBusManager* dbus_manager, |
Prathmesh Prabhu | 9fdb84b | 2014-08-21 18:41:58 -0700 | [diff] [blame] | 51 | TimeDelta suspend_delay, |
Paul Stewart | 1a212a6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 52 | const SuspendImminentCallback& suspend_imminent_callback, |
| 53 | const SuspendDoneCallback& suspend_done_callback, |
| 54 | const DarkSuspendImminentCallback& dark_suspend_imminent_callback) { |
Prathmesh Prabhu | d9c786c | 2014-04-23 17:37:28 -0700 | [diff] [blame] | 55 | power_manager_name_watcher_.reset( |
| 56 | dbus_manager->CreateNameWatcher( |
| 57 | power_manager::kPowerManagerServiceName, |
| 58 | Bind(&PowerManager::OnPowerManagerAppeared, Unretained(this)), |
| 59 | Bind(&PowerManager::OnPowerManagerVanished, Unretained(this)))); |
Prathmesh Prabhu | 9fdb84b | 2014-08-21 18:41:58 -0700 | [diff] [blame] | 60 | |
| 61 | suspend_delay_ = suspend_delay; |
| 62 | suspend_imminent_callback_ = suspend_imminent_callback; |
| 63 | suspend_done_callback_ = suspend_done_callback; |
Prathmesh Prabhu | 64ad238 | 2014-08-26 11:19:30 -0700 | [diff] [blame] | 64 | dark_suspend_imminent_callback_ = dark_suspend_imminent_callback; |
Prathmesh Prabhu | d9c786c | 2014-04-23 17:37:28 -0700 | [diff] [blame] | 65 | } |
| 66 | |
Prathmesh Prabhu | 9fdb84b | 2014-08-21 18:41:58 -0700 | [diff] [blame] | 67 | void PowerManager::Stop() { |
| 68 | LOG(INFO) << __func__; |
| 69 | power_manager_name_watcher_.reset(); |
| 70 | // We may attempt to unregister with a stale |suspend_delay_id_| if powerd |
Prathmesh Prabhu | d9c786c | 2014-04-23 17:37:28 -0700 | [diff] [blame] | 71 | // reappeared behind our back. It is safe to do so. |
Prathmesh Prabhu | 9fdb84b | 2014-08-21 18:41:58 -0700 | [diff] [blame] | 72 | if (suspend_delay_registered_) |
| 73 | power_manager_proxy_->UnregisterSuspendDelay(suspend_delay_id_); |
Prathmesh Prabhu | 64ad238 | 2014-08-26 11:19:30 -0700 | [diff] [blame] | 74 | if (dark_suspend_delay_registered_) |
| 75 | power_manager_proxy_->UnregisterDarkSuspendDelay(dark_suspend_delay_id_); |
Daniel Erat | fac0953 | 2014-04-17 20:25:59 -0700 | [diff] [blame] | 76 | |
Prathmesh Prabhu | 9fdb84b | 2014-08-21 18:41:58 -0700 | [diff] [blame] | 77 | suspend_delay_registered_ = false; |
Prathmesh Prabhu | 64ad238 | 2014-08-26 11:19:30 -0700 | [diff] [blame] | 78 | dark_suspend_delay_registered_ = false; |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 79 | } |
| 80 | |
Prathmesh Prabhu | 9fdb84b | 2014-08-21 18:41:58 -0700 | [diff] [blame] | 81 | bool PowerManager::ReportSuspendReadiness() { |
| 82 | if (!suspending_) { |
| 83 | LOG(INFO) << __func__ << ": Suspend attempt (" |
| 84 | << current_suspend_id_ << ") not active. Ignoring signal."; |
Daniel Erat | fac0953 | 2014-04-17 20:25:59 -0700 | [diff] [blame] | 85 | return false; |
| 86 | } |
Prathmesh Prabhu | 9fdb84b | 2014-08-21 18:41:58 -0700 | [diff] [blame] | 87 | return power_manager_proxy_->ReportSuspendReadiness(suspend_delay_id_, |
| 88 | current_suspend_id_); |
Gary Morain | 52fabab | 2012-08-22 09:27:31 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Prathmesh Prabhu | 64ad238 | 2014-08-26 11:19:30 -0700 | [diff] [blame] | 91 | bool PowerManager::ReportDarkSuspendReadiness() { |
| 92 | return power_manager_proxy_->ReportDarkSuspendReadiness( |
| 93 | dark_suspend_delay_id_, |
| 94 | current_dark_suspend_id_); |
| 95 | } |
| 96 | |
Paul Stewart | 1a212a6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 97 | bool PowerManager::RecordDarkResumeWakeReason(const string& wake_reason) { |
Samuel Tan | 1897afa | 2015-05-21 14:21:56 -0700 | [diff] [blame] | 98 | return power_manager_proxy_->RecordDarkResumeWakeReason(wake_reason); |
| 99 | } |
| 100 | |
Daniel Erat | 0818cca | 2012-12-14 10:16:21 -0800 | [diff] [blame] | 101 | void PowerManager::OnSuspendImminent(int suspend_id) { |
| 102 | LOG(INFO) << __func__ << "(" << suspend_id << ")"; |
Prathmesh Prabhu | 9fdb84b | 2014-08-21 18:41:58 -0700 | [diff] [blame] | 103 | current_suspend_id_ = suspend_id; |
| 104 | |
Prathmesh Prabhu | 9fdb84b | 2014-08-21 18:41:58 -0700 | [diff] [blame] | 105 | // If we're already suspending, don't call the |suspend_imminent_callback_| |
| 106 | // again. |
| 107 | if (!suspending_) { |
| 108 | // Change the power state to suspending as soon as this signal is received |
| 109 | // so that the manager can suppress auto-connect, for example. |
Prathmesh Prabhu | 0f4e0a4 | 2014-08-27 15:29:00 -0700 | [diff] [blame] | 110 | // Also, we must set this before running the callback below, because the |
| 111 | // callback may synchronously report suspend readiness. |
Prathmesh Prabhu | 9fdb84b | 2014-08-21 18:41:58 -0700 | [diff] [blame] | 112 | suspending_ = true; |
| 113 | suspend_imminent_callback_.Run(); |
Daniel Erat | fac0953 | 2014-04-17 20:25:59 -0700 | [diff] [blame] | 114 | } |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 115 | } |
| 116 | |
Daniel Erat | fac0953 | 2014-04-17 20:25:59 -0700 | [diff] [blame] | 117 | void PowerManager::OnSuspendDone(int suspend_id) { |
Prathmesh Prabhu | 0f4e0a4 | 2014-08-27 15:29:00 -0700 | [diff] [blame] | 118 | // NB: |suspend_id| could be -1. See OnPowerManagerVanished. |
Daniel Erat | fac0953 | 2014-04-17 20:25:59 -0700 | [diff] [blame] | 119 | LOG(INFO) << __func__ << "(" << suspend_id << ")"; |
Prathmesh Prabhu | 9fdb84b | 2014-08-21 18:41:58 -0700 | [diff] [blame] | 120 | if (!suspending_) { |
| 121 | LOG(WARNING) << "Recieved unexpected SuspendDone (" |
| 122 | << suspend_id << "). Ignoring."; |
| 123 | return; |
| 124 | } |
| 125 | |
Daniel Erat | fac0953 | 2014-04-17 20:25:59 -0700 | [diff] [blame] | 126 | suspending_ = false; |
Samuel Tan | 787a1ce | 2014-11-11 17:17:27 -0800 | [diff] [blame] | 127 | in_dark_resume_ = false; |
Prathmesh Prabhu | 9fdb84b | 2014-08-21 18:41:58 -0700 | [diff] [blame] | 128 | suspend_done_callback_.Run(); |
Gary Morain | 52fabab | 2012-08-22 09:27:31 -0700 | [diff] [blame] | 129 | } |
| 130 | |
Prathmesh Prabhu | 64ad238 | 2014-08-26 11:19:30 -0700 | [diff] [blame] | 131 | void PowerManager::OnDarkSuspendImminent(int suspend_id) { |
| 132 | LOG(INFO) << __func__ << "(" << suspend_id << ")"; |
| 133 | if (!dark_suspend_delay_registered_) { |
| 134 | LOG(WARNING) << "Ignoring DarkSuspendImminent signal from powerd. shill " |
| 135 | << "does not have a dark suspend delay registered. This " |
| 136 | << "means that shill is not guaranteed any time before a " |
| 137 | << "resuspend."; |
| 138 | return; |
| 139 | } |
Samuel Tan | 787a1ce | 2014-11-11 17:17:27 -0800 | [diff] [blame] | 140 | in_dark_resume_ = true; |
Prathmesh Prabhu | 64ad238 | 2014-08-26 11:19:30 -0700 | [diff] [blame] | 141 | current_dark_suspend_id_ = suspend_id; |
| 142 | dark_suspend_imminent_callback_.Run(); |
| 143 | } |
| 144 | |
Paul Stewart | 1a212a6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 145 | void PowerManager::OnPowerManagerAppeared(const string& /*name*/, |
| 146 | const string& /*owner*/) { |
Prathmesh Prabhu | 9fdb84b | 2014-08-21 18:41:58 -0700 | [diff] [blame] | 147 | LOG(INFO) << __func__; |
| 148 | CHECK(!suspend_delay_registered_); |
| 149 | if (power_manager_proxy_->RegisterSuspendDelay(suspend_delay_, |
| 150 | kSuspendDelayDescription, |
| 151 | &suspend_delay_id_)) |
| 152 | suspend_delay_registered_ = true; |
Prathmesh Prabhu | 64ad238 | 2014-08-26 11:19:30 -0700 | [diff] [blame] | 153 | |
| 154 | if (power_manager_proxy_->RegisterDarkSuspendDelay( |
| 155 | suspend_delay_, |
| 156 | kDarkSuspendDelayDescription, |
| 157 | &dark_suspend_delay_id_)) |
| 158 | dark_suspend_delay_registered_ = true; |
Prathmesh Prabhu | d9c786c | 2014-04-23 17:37:28 -0700 | [diff] [blame] | 159 | } |
| 160 | |
Paul Stewart | 1a212a6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 161 | void PowerManager::OnPowerManagerVanished(const string& /*name*/) { |
Prathmesh Prabhu | 9fdb84b | 2014-08-21 18:41:58 -0700 | [diff] [blame] | 162 | LOG(INFO) << __func__; |
Prathmesh Prabhu | 0f4e0a4 | 2014-08-27 15:29:00 -0700 | [diff] [blame] | 163 | // If powerd vanished during a suspend, we need to wake ourselves up. |
| 164 | if (suspending_) |
| 165 | OnSuspendDone(kInvalidSuspendId); |
Prathmesh Prabhu | 9fdb84b | 2014-08-21 18:41:58 -0700 | [diff] [blame] | 166 | suspend_delay_registered_ = false; |
Prathmesh Prabhu | 64ad238 | 2014-08-26 11:19:30 -0700 | [diff] [blame] | 167 | dark_suspend_delay_registered_ = false; |
Prathmesh Prabhu | d9c786c | 2014-04-23 17:37:28 -0700 | [diff] [blame] | 168 | } |
| 169 | |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 170 | } // namespace shill |