mukesh agrawal | 7c1fece | 2012-01-13 11:31:27 -0800 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Darin Petkov | 394b7d4 | 2011-11-03 15:48:02 +0100 | [diff] [blame] | 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_proxy.h" |
| 6 | |
Darin Petkov | 394b7d4 | 2011-11-03 15:48:02 +0100 | [diff] [blame] | 7 | #include <chromeos/dbus/service_constants.h> |
Daniel Erat | 0818cca | 2012-12-14 10:16:21 -0800 | [diff] [blame] | 8 | #include <google/protobuf/message_lite.h> |
Darin Petkov | 394b7d4 | 2011-11-03 15:48:02 +0100 | [diff] [blame] | 9 | |
Liam McLoughlin | ef342b4 | 2013-09-13 21:05:36 +0100 | [diff] [blame] | 10 | #include "power_manager/proto_bindings/suspend.pb.h" |
Alex Vakulenko | a41ab51 | 2014-07-23 14:24:23 -0700 | [diff] [blame] | 11 | #include "shill/logging.h" |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 12 | |
mukesh agrawal | 7c1fece | 2012-01-13 11:31:27 -0800 | [diff] [blame] | 13 | using std::string; |
Daniel Erat | 0818cca | 2012-12-14 10:16:21 -0800 | [diff] [blame] | 14 | using std::vector; |
mukesh agrawal | 7c1fece | 2012-01-13 11:31:27 -0800 | [diff] [blame] | 15 | |
Darin Petkov | 394b7d4 | 2011-11-03 15:48:02 +0100 | [diff] [blame] | 16 | namespace shill { |
| 17 | |
Daniel Erat | 0818cca | 2012-12-14 10:16:21 -0800 | [diff] [blame] | 18 | namespace { |
| 19 | |
| 20 | // Serializes |protobuf| to |out| and returns true on success. |
Paul Stewart | 1a212a6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 21 | bool SerializeProtocolBuffer(const google::protobuf::MessageLite& protobuf, |
| 22 | vector<uint8_t>* out) { |
Daniel Erat | 0818cca | 2012-12-14 10:16:21 -0800 | [diff] [blame] | 23 | CHECK(out); |
| 24 | out->clear(); |
| 25 | string serialized_protobuf; |
| 26 | if (!protobuf.SerializeToString(&serialized_protobuf)) |
| 27 | return false; |
| 28 | out->assign(serialized_protobuf.begin(), serialized_protobuf.end()); |
| 29 | return true; |
| 30 | } |
| 31 | |
| 32 | // Deserializes |serialized_protobuf| to |protobuf_out| and returns true on |
| 33 | // success. |
Paul Stewart | 1a212a6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 34 | bool DeserializeProtocolBuffer(const vector<uint8_t>& serialized_protobuf, |
| 35 | google::protobuf::MessageLite* protobuf_out) { |
Daniel Erat | 0818cca | 2012-12-14 10:16:21 -0800 | [diff] [blame] | 36 | CHECK(protobuf_out); |
| 37 | if (serialized_protobuf.empty()) |
| 38 | return false; |
| 39 | return protobuf_out->ParseFromArray(&serialized_protobuf.front(), |
| 40 | serialized_protobuf.size()); |
| 41 | } |
| 42 | |
| 43 | } // namespace |
| 44 | |
Paul Stewart | 1a212a6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 45 | PowerManagerProxy::PowerManagerProxy(PowerManagerProxyDelegate* delegate, |
| 46 | DBus::Connection* connection) |
Darin Petkov | 394b7d4 | 2011-11-03 15:48:02 +0100 | [diff] [blame] | 47 | : proxy_(delegate, connection) {} |
| 48 | |
| 49 | PowerManagerProxy::~PowerManagerProxy() {} |
| 50 | |
Prathmesh Prabhu | 64ad238 | 2014-08-26 11:19:30 -0700 | [diff] [blame] | 51 | bool PowerManagerProxy::RegisterSuspendDelay( |
| 52 | base::TimeDelta timeout, |
Paul Stewart | 1a212a6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 53 | const string& description, |
| 54 | int* delay_id_out) { |
Prathmesh Prabhu | 64ad238 | 2014-08-26 11:19:30 -0700 | [diff] [blame] | 55 | return RegisterSuspendDelayInternal(false, |
| 56 | timeout, |
| 57 | description, |
| 58 | delay_id_out); |
| 59 | } |
| 60 | |
| 61 | bool PowerManagerProxy::UnregisterSuspendDelay(int delay_id) { |
| 62 | return UnregisterSuspendDelayInternal(false, delay_id); |
| 63 | } |
| 64 | |
| 65 | bool PowerManagerProxy::ReportSuspendReadiness(int delay_id, int suspend_id) { |
| 66 | return ReportSuspendReadinessInternal(false, delay_id, suspend_id); |
| 67 | } |
| 68 | |
| 69 | bool PowerManagerProxy::RegisterDarkSuspendDelay( |
| 70 | base::TimeDelta timeout, |
Paul Stewart | 1a212a6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 71 | const string& description, |
| 72 | int* delay_id_out) { |
Prathmesh Prabhu | 64ad238 | 2014-08-26 11:19:30 -0700 | [diff] [blame] | 73 | return RegisterSuspendDelayInternal(true, |
| 74 | timeout, |
| 75 | description, |
| 76 | delay_id_out); |
| 77 | } |
| 78 | |
| 79 | bool PowerManagerProxy::UnregisterDarkSuspendDelay(int delay_id) { |
| 80 | return UnregisterSuspendDelayInternal(true, delay_id); |
| 81 | } |
| 82 | |
| 83 | bool PowerManagerProxy::ReportDarkSuspendReadiness(int delay_id, |
| 84 | int suspend_id ) { |
| 85 | return ReportSuspendReadinessInternal(true, delay_id, suspend_id); |
| 86 | } |
| 87 | |
Paul Stewart | 1a212a6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 88 | bool PowerManagerProxy::RecordDarkResumeWakeReason(const string& wake_reason) { |
Samuel Tan | 1897afa | 2015-05-21 14:21:56 -0700 | [diff] [blame] | 89 | LOG(INFO) << __func__; |
| 90 | |
| 91 | power_manager::DarkResumeWakeReason proto; |
| 92 | proto.set_wake_reason(wake_reason); |
| 93 | vector<uint8_t> serialized_proto; |
| 94 | CHECK(SerializeProtocolBuffer(proto, &serialized_proto)); |
| 95 | |
| 96 | try { |
| 97 | proxy_.RecordDarkResumeWakeReason(serialized_proto); |
Paul Stewart | 1a212a6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 98 | } catch (const DBus::Error& e) { |
Samuel Tan | 1897afa | 2015-05-21 14:21:56 -0700 | [diff] [blame] | 99 | LOG(ERROR) << "DBus exception: " << e.name() << ": " << e.what(); |
| 100 | return false; |
| 101 | } |
| 102 | return true; |
| 103 | } |
| 104 | |
Prathmesh Prabhu | 64ad238 | 2014-08-26 11:19:30 -0700 | [diff] [blame] | 105 | bool PowerManagerProxy::RegisterSuspendDelayInternal( |
| 106 | bool is_dark, |
| 107 | base::TimeDelta timeout, |
Paul Stewart | 1a212a6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 108 | const string& description, |
| 109 | int* delay_id_out) { |
Prathmesh Prabhu | 64ad238 | 2014-08-26 11:19:30 -0700 | [diff] [blame] | 110 | const string is_dark_arg = (is_dark ? "dark=true" : "dark=false"); |
| 111 | LOG(INFO) << __func__ << "(" << timeout.InMilliseconds() |
| 112 | << ", " << is_dark_arg <<")"; |
Darin Petkov | 394b7d4 | 2011-11-03 15:48:02 +0100 | [diff] [blame] | 113 | |
Daniel Erat | 0818cca | 2012-12-14 10:16:21 -0800 | [diff] [blame] | 114 | power_manager::RegisterSuspendDelayRequest request_proto; |
| 115 | request_proto.set_timeout(timeout.ToInternalValue()); |
Daniel Erat | f975367 | 2013-01-24 10:17:02 -0800 | [diff] [blame] | 116 | request_proto.set_description(description); |
Daniel Erat | 0818cca | 2012-12-14 10:16:21 -0800 | [diff] [blame] | 117 | vector<uint8_t> serialized_request; |
| 118 | CHECK(SerializeProtocolBuffer(request_proto, &serialized_request)); |
| 119 | |
| 120 | vector<uint8_t> serialized_reply; |
Darin Petkov | 3ec5534 | 2012-09-28 14:04:44 +0200 | [diff] [blame] | 121 | try { |
Prathmesh Prabhu | 64ad238 | 2014-08-26 11:19:30 -0700 | [diff] [blame] | 122 | if (is_dark) |
| 123 | serialized_reply = proxy_.RegisterDarkSuspendDelay(serialized_request); |
| 124 | else |
| 125 | serialized_reply = proxy_.RegisterSuspendDelay(serialized_request); |
Paul Stewart | 1a212a6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 126 | } catch (const DBus::Error& e) { |
Darin Petkov | 3ec5534 | 2012-09-28 14:04:44 +0200 | [diff] [blame] | 127 | LOG(ERROR) << "DBus exception: " << e.name() << ": " << e.what(); |
Daniel Erat | 0818cca | 2012-12-14 10:16:21 -0800 | [diff] [blame] | 128 | return false; |
| 129 | } |
| 130 | |
| 131 | power_manager::RegisterSuspendDelayReply reply_proto; |
| 132 | if (!DeserializeProtocolBuffer(serialized_reply, &reply_proto)) { |
Prathmesh Prabhu | 64ad238 | 2014-08-26 11:19:30 -0700 | [diff] [blame] | 133 | LOG(ERROR) << "Failed to register " |
| 134 | << (is_dark ? "dark " : "") |
| 135 | << "suspend delay. Couldn't parse response."; |
Daniel Erat | 0818cca | 2012-12-14 10:16:21 -0800 | [diff] [blame] | 136 | return false; |
| 137 | } |
| 138 | *delay_id_out = reply_proto.delay_id(); |
| 139 | return true; |
| 140 | } |
| 141 | |
Prathmesh Prabhu | 64ad238 | 2014-08-26 11:19:30 -0700 | [diff] [blame] | 142 | bool PowerManagerProxy::UnregisterSuspendDelayInternal(bool is_dark, |
| 143 | int delay_id) { |
| 144 | const string is_dark_arg = (is_dark ? "dark=true" : "dark=false"); |
| 145 | LOG(INFO) << __func__ << "(" << delay_id << ", " << is_dark_arg << ")"; |
Daniel Erat | 0818cca | 2012-12-14 10:16:21 -0800 | [diff] [blame] | 146 | |
| 147 | power_manager::UnregisterSuspendDelayRequest request_proto; |
| 148 | request_proto.set_delay_id(delay_id); |
| 149 | vector<uint8_t> serialized_request; |
| 150 | CHECK(SerializeProtocolBuffer(request_proto, &serialized_request)); |
| 151 | |
| 152 | try { |
Prathmesh Prabhu | 64ad238 | 2014-08-26 11:19:30 -0700 | [diff] [blame] | 153 | if (is_dark) |
| 154 | proxy_.UnregisterDarkSuspendDelay(serialized_request); |
| 155 | else |
| 156 | proxy_.UnregisterSuspendDelay(serialized_request); |
Paul Stewart | 1a212a6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 157 | } catch (const DBus::Error& e) { |
Daniel Erat | 0818cca | 2012-12-14 10:16:21 -0800 | [diff] [blame] | 158 | LOG(ERROR) << "DBus exception: " << e.name() << ": " << e.what(); |
| 159 | return false; |
Darin Petkov | 3ec5534 | 2012-09-28 14:04:44 +0200 | [diff] [blame] | 160 | } |
Prathmesh Prabhu | 64ad238 | 2014-08-26 11:19:30 -0700 | [diff] [blame] | 161 | return true; |
Darin Petkov | 3ec5534 | 2012-09-28 14:04:44 +0200 | [diff] [blame] | 162 | } |
| 163 | |
Prathmesh Prabhu | 64ad238 | 2014-08-26 11:19:30 -0700 | [diff] [blame] | 164 | bool PowerManagerProxy::ReportSuspendReadinessInternal(bool is_dark, |
| 165 | int delay_id, |
| 166 | int suspend_id) { |
| 167 | const string is_dark_arg = (is_dark ? "dark=true" : "dark=false"); |
| 168 | LOG(INFO) << __func__ |
| 169 | << "(" << delay_id |
| 170 | << ", " << suspend_id |
| 171 | << ", " << is_dark_arg << ")"; |
Daniel Erat | 0818cca | 2012-12-14 10:16:21 -0800 | [diff] [blame] | 172 | |
| 173 | power_manager::SuspendReadinessInfo proto; |
| 174 | proto.set_delay_id(delay_id); |
| 175 | proto.set_suspend_id(suspend_id); |
| 176 | vector<uint8_t> serialized_proto; |
| 177 | CHECK(SerializeProtocolBuffer(proto, &serialized_proto)); |
| 178 | |
| 179 | try { |
Prathmesh Prabhu | 64ad238 | 2014-08-26 11:19:30 -0700 | [diff] [blame] | 180 | if (is_dark) |
| 181 | proxy_.HandleDarkSuspendReadiness(serialized_proto); |
| 182 | else |
| 183 | proxy_.HandleSuspendReadiness(serialized_proto); |
Paul Stewart | 1a212a6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 184 | } catch (const DBus::Error& e) { |
Daniel Erat | 0818cca | 2012-12-14 10:16:21 -0800 | [diff] [blame] | 185 | LOG(ERROR) << "DBus exception: " << e.name() << ": " << e.what(); |
| 186 | return false; |
Darin Petkov | 3ec5534 | 2012-09-28 14:04:44 +0200 | [diff] [blame] | 187 | } |
Prathmesh Prabhu | 64ad238 | 2014-08-26 11:19:30 -0700 | [diff] [blame] | 188 | return true; |
Darin Petkov | 3ec5534 | 2012-09-28 14:04:44 +0200 | [diff] [blame] | 189 | } |
| 190 | |
Paul Stewart | 1a212a6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 191 | PowerManagerProxy::Proxy::Proxy(PowerManagerProxyDelegate* delegate, |
| 192 | DBus::Connection* connection) |
Darin Petkov | 394b7d4 | 2011-11-03 15:48:02 +0100 | [diff] [blame] | 193 | : DBus::ObjectProxy(*connection, |
mukesh agrawal | 5c05b29 | 2012-03-07 10:12:52 -0800 | [diff] [blame] | 194 | power_manager::kPowerManagerServicePath, |
Darin Petkov | 394b7d4 | 2011-11-03 15:48:02 +0100 | [diff] [blame] | 195 | power_manager::kPowerManagerServiceName), |
| 196 | delegate_(delegate) {} |
| 197 | |
| 198 | PowerManagerProxy::Proxy::~Proxy() {} |
| 199 | |
Daniel Erat | 0818cca | 2012-12-14 10:16:21 -0800 | [diff] [blame] | 200 | void PowerManagerProxy::Proxy::SuspendImminent( |
Paul Stewart | 1a212a6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 201 | const vector<uint8_t>& serialized_proto) { |
Daniel Erat | 0818cca | 2012-12-14 10:16:21 -0800 | [diff] [blame] | 202 | LOG(INFO) << __func__; |
Daniel Erat | 0818cca | 2012-12-14 10:16:21 -0800 | [diff] [blame] | 203 | power_manager::SuspendImminent proto; |
| 204 | if (!DeserializeProtocolBuffer(serialized_proto, &proto)) { |
| 205 | LOG(ERROR) << "Failed to parse SuspendImminent signal."; |
| 206 | return; |
| 207 | } |
| 208 | delegate_->OnSuspendImminent(proto.suspend_id()); |
Darin Petkov | 394b7d4 | 2011-11-03 15:48:02 +0100 | [diff] [blame] | 209 | } |
| 210 | |
Daniel Erat | fac0953 | 2014-04-17 20:25:59 -0700 | [diff] [blame] | 211 | void PowerManagerProxy::Proxy::SuspendDone( |
Paul Stewart | 1a212a6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 212 | const vector<uint8_t>& serialized_proto) { |
Daniel Erat | fac0953 | 2014-04-17 20:25:59 -0700 | [diff] [blame] | 213 | LOG(INFO) << __func__; |
| 214 | power_manager::SuspendDone proto; |
| 215 | if (!DeserializeProtocolBuffer(serialized_proto, &proto)) { |
| 216 | LOG(ERROR) << "Failed to parse SuspendDone signal."; |
| 217 | return; |
mukesh agrawal | 7c1fece | 2012-01-13 11:31:27 -0800 | [diff] [blame] | 218 | } |
Daniel Erat | fac0953 | 2014-04-17 20:25:59 -0700 | [diff] [blame] | 219 | delegate_->OnSuspendDone(proto.suspend_id()); |
mukesh agrawal | 7c1fece | 2012-01-13 11:31:27 -0800 | [diff] [blame] | 220 | } |
| 221 | |
Prathmesh Prabhu | 64ad238 | 2014-08-26 11:19:30 -0700 | [diff] [blame] | 222 | void PowerManagerProxy::Proxy::DarkSuspendImminent( |
Paul Stewart | 1a212a6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 223 | const vector<uint8_t>& serialized_proto) { |
Prathmesh Prabhu | 64ad238 | 2014-08-26 11:19:30 -0700 | [diff] [blame] | 224 | LOG(INFO) << __func__; |
| 225 | power_manager::SuspendImminent proto; |
| 226 | if (!DeserializeProtocolBuffer(serialized_proto, &proto)) { |
| 227 | LOG(ERROR) << "Failed to parse DarkSuspendImminent signal."; |
| 228 | return; |
| 229 | } |
| 230 | delegate_->OnDarkSuspendImminent(proto.suspend_id()); |
| 231 | } |
| 232 | |
Darin Petkov | 394b7d4 | 2011-11-03 15:48:02 +0100 | [diff] [blame] | 233 | } // namespace shill |