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 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 10 | #include <base/stl_util.h> |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 11 | |
Darin Petkov | 3ec5534 | 2012-09-28 14:04:44 +0200 | [diff] [blame] | 12 | #include "shill/event_dispatcher.h" |
Christopher Wiley | b691efd | 2012-08-09 13:51:51 -0700 | [diff] [blame] | 13 | #include "shill/logging.h" |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 14 | #include "shill/power_manager_proxy_interface.h" |
| 15 | #include "shill/proxy_factory.h" |
| 16 | |
Gary Morain | 52fabab | 2012-08-22 09:27:31 -0700 | [diff] [blame] | 17 | using std::map; |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 18 | using std::string; |
| 19 | |
| 20 | namespace shill { |
| 21 | |
Darin Petkov | 3ec5534 | 2012-09-28 14:04:44 +0200 | [diff] [blame] | 22 | const int PowerManager::kSuspendTimeoutMilliseconds = 15 * 1000; |
| 23 | |
| 24 | PowerManager::PowerManager(EventDispatcher *dispatcher, |
| 25 | ProxyFactory *proxy_factory) |
| 26 | : dispatcher_(dispatcher), |
| 27 | power_manager_proxy_(proxy_factory->CreatePowerManagerProxy(this)), |
Darin Petkov | ca62154 | 2012-07-25 14:25:56 +0200 | [diff] [blame] | 28 | power_state_(kUnknown) {} |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 29 | |
| 30 | PowerManager::~PowerManager() { |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | void PowerManager::AddStateChangeCallback(const string &key, |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 34 | const PowerStateCallback &callback) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 35 | SLOG(Power, 2) << __func__ << " key " << key; |
Gary Morain | 52fabab | 2012-08-22 09:27:31 -0700 | [diff] [blame] | 36 | AddCallback(key, callback, &state_change_callbacks_); |
| 37 | } |
| 38 | |
| 39 | void PowerManager::AddSuspendDelayCallback( |
| 40 | const string &key, const SuspendDelayCallback &callback) { |
| 41 | SLOG(Power, 2) << __func__ << " key " << key; |
| 42 | AddCallback(key, callback, &suspend_delay_callbacks_); |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | void PowerManager::RemoveStateChangeCallback(const string &key) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 46 | SLOG(Power, 2) << __func__ << " key " << key; |
Gary Morain | 52fabab | 2012-08-22 09:27:31 -0700 | [diff] [blame] | 47 | RemoveCallback(key, &state_change_callbacks_); |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 48 | } |
| 49 | |
Gary Morain | 52fabab | 2012-08-22 09:27:31 -0700 | [diff] [blame] | 50 | void PowerManager::RemoveSuspendDelayCallback(const string &key) { |
| 51 | SLOG(Power, 2) << __func__ << " key " << key; |
| 52 | RemoveCallback(key, &suspend_delay_callbacks_); |
| 53 | } |
| 54 | |
Daniel Erat | 0818cca | 2012-12-14 10:16:21 -0800 | [diff] [blame] | 55 | void PowerManager::OnSuspendImminent(int suspend_id) { |
| 56 | LOG(INFO) << __func__ << "(" << suspend_id << ")"; |
Darin Petkov | 3ec5534 | 2012-09-28 14:04:44 +0200 | [diff] [blame] | 57 | // Change the power state to suspending as soon as this signal is received so |
| 58 | // that the manager can suppress auto-connect, for example. Schedules a |
| 59 | // suspend timeout in case the suspend attempt failed or got interrupted, and |
| 60 | // there's no proper notification from the power manager. |
| 61 | power_state_ = kSuspending; |
| 62 | suspend_timeout_.Reset( |
| 63 | base::Bind(&PowerManager::OnSuspendTimeout, base::Unretained(this))); |
| 64 | dispatcher_->PostDelayedTask(suspend_timeout_.callback(), |
| 65 | kSuspendTimeoutMilliseconds); |
Daniel Erat | 0818cca | 2012-12-14 10:16:21 -0800 | [diff] [blame] | 66 | OnEvent(suspend_id, &suspend_delay_callbacks_); |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | void PowerManager::OnPowerStateChanged(SuspendState new_power_state) { |
Darin Petkov | ca62154 | 2012-07-25 14:25:56 +0200 | [diff] [blame] | 70 | LOG(INFO) << "Power state changed: " |
| 71 | << power_state_ << "->" << new_power_state; |
Darin Petkov | 3ec5534 | 2012-09-28 14:04:44 +0200 | [diff] [blame] | 72 | suspend_timeout_.Cancel(); |
Darin Petkov | ca62154 | 2012-07-25 14:25:56 +0200 | [diff] [blame] | 73 | power_state_ = new_power_state; |
Gary Morain | 52fabab | 2012-08-22 09:27:31 -0700 | [diff] [blame] | 74 | OnEvent(new_power_state, &state_change_callbacks_); |
| 75 | } |
| 76 | |
Daniel Erat | 0818cca | 2012-12-14 10:16:21 -0800 | [diff] [blame] | 77 | bool PowerManager::RegisterSuspendDelay(base::TimeDelta timeout, |
Daniel Erat | f975367 | 2013-01-24 10:17:02 -0800 | [diff] [blame] | 78 | const string &description, |
Daniel Erat | 0818cca | 2012-12-14 10:16:21 -0800 | [diff] [blame] | 79 | int *delay_id_out) { |
Daniel Erat | f975367 | 2013-01-24 10:17:02 -0800 | [diff] [blame] | 80 | return power_manager_proxy_->RegisterSuspendDelay(timeout, description, |
| 81 | delay_id_out); |
Gary Morain | 52fabab | 2012-08-22 09:27:31 -0700 | [diff] [blame] | 82 | } |
| 83 | |
Daniel Erat | 0818cca | 2012-12-14 10:16:21 -0800 | [diff] [blame] | 84 | bool PowerManager::UnregisterSuspendDelay(int delay_id) { |
| 85 | return power_manager_proxy_->UnregisterSuspendDelay(delay_id); |
Darin Petkov | 3ec5534 | 2012-09-28 14:04:44 +0200 | [diff] [blame] | 86 | } |
| 87 | |
Daniel Erat | 0818cca | 2012-12-14 10:16:21 -0800 | [diff] [blame] | 88 | bool PowerManager::ReportSuspendReadiness(int delay_id, int suspend_id) { |
| 89 | return power_manager_proxy_->ReportSuspendReadiness(delay_id, suspend_id); |
Darin Petkov | 3ec5534 | 2012-09-28 14:04:44 +0200 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | void PowerManager::OnSuspendTimeout() { |
| 93 | LOG(ERROR) << "Suspend timed out -- assuming power-on state."; |
| 94 | OnPowerStateChanged(kOn); |
| 95 | } |
| 96 | |
Gary Morain | 52fabab | 2012-08-22 09:27:31 -0700 | [diff] [blame] | 97 | template<class Callback> |
| 98 | void PowerManager::AddCallback(const string &key, const Callback &callback, |
| 99 | map<const string, Callback> *callback_map) { |
| 100 | CHECK(callback_map != NULL); |
| 101 | CHECK(!callback.is_null()); |
| 102 | if (ContainsKey(*callback_map, key)) { |
| 103 | LOG(DFATAL) << "Inserting duplicate key " << key; |
| 104 | LOG(INFO) << "Removing previous callback for key " << key; |
| 105 | RemoveCallback(key, callback_map); |
| 106 | } |
| 107 | (*callback_map)[key] = callback; |
| 108 | } |
| 109 | |
| 110 | template<class Callback> |
| 111 | void PowerManager::RemoveCallback(const string &key, |
| 112 | map<const string, Callback> *callback_map) { |
| 113 | CHECK(callback_map != NULL); |
| 114 | DCHECK(ContainsKey(*callback_map, key)) << "Removing unknown key " << key; |
Daniel Erat | f975367 | 2013-01-24 10:17:02 -0800 | [diff] [blame] | 115 | typename map<const string, Callback>::iterator it = callback_map->find(key); |
Gary Morain | 52fabab | 2012-08-22 09:27:31 -0700 | [diff] [blame] | 116 | if (it != callback_map->end()) { |
| 117 | callback_map->erase(it); |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 118 | } |
| 119 | } |
| 120 | |
Gary Morain | 52fabab | 2012-08-22 09:27:31 -0700 | [diff] [blame] | 121 | template<class Param, class Callback> |
| 122 | void PowerManager::OnEvent(const Param ¶m, |
| 123 | map<const string, Callback> *callback_map) const { |
| 124 | CHECK(callback_map != NULL); |
| 125 | for (typename map<const string, Callback>::const_iterator it = |
| 126 | callback_map->begin(); |
| 127 | it != callback_map->end(); ++it) { |
| 128 | CHECK(!it->second.is_null()); |
| 129 | it->second.Run(param); |
| 130 | } |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 131 | } |
| 132 | |
Gary Morain | 52fabab | 2012-08-22 09:27:31 -0700 | [diff] [blame] | 133 | |
| 134 | |
Gary Morain | 43bc627 | 2012-01-30 14:01:15 -0800 | [diff] [blame] | 135 | } // namespace shill |