blob: f6da1670b01b0af24b035fe65961dc360d0a22b4 [file] [log] [blame]
mukesh agrawal7c1fece2012-01-13 11:31:27 -08001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Darin Petkov394b7d42011-11-03 15:48:02 +01002// 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 Petkov394b7d42011-11-03 15:48:02 +01007#include <chromeos/dbus/service_constants.h>
Daniel Erat0818cca2012-12-14 10:16:21 -08008#include <google/protobuf/message_lite.h>
Darin Petkov394b7d42011-11-03 15:48:02 +01009
Liam McLoughlinef342b42013-09-13 21:05:36 +010010#include "power_manager/proto_bindings/suspend.pb.h"
Alex Vakulenkoa41ab512014-07-23 14:24:23 -070011#include "shill/logging.h"
Ben Chanfad4a0b2012-04-18 15:49:59 -070012
mukesh agrawal7c1fece2012-01-13 11:31:27 -080013using std::string;
Daniel Erat0818cca2012-12-14 10:16:21 -080014using std::vector;
mukesh agrawal7c1fece2012-01-13 11:31:27 -080015
Darin Petkov394b7d42011-11-03 15:48:02 +010016namespace shill {
17
Daniel Erat0818cca2012-12-14 10:16:21 -080018namespace {
19
20// Serializes |protobuf| to |out| and returns true on success.
Paul Stewart1a212a62015-06-16 13:13:10 -070021bool SerializeProtocolBuffer(const google::protobuf::MessageLite& protobuf,
22 vector<uint8_t>* out) {
Daniel Erat0818cca2012-12-14 10:16:21 -080023 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 Stewart1a212a62015-06-16 13:13:10 -070034bool DeserializeProtocolBuffer(const vector<uint8_t>& serialized_protobuf,
35 google::protobuf::MessageLite* protobuf_out) {
Daniel Erat0818cca2012-12-14 10:16:21 -080036 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 Stewart1a212a62015-06-16 13:13:10 -070045PowerManagerProxy::PowerManagerProxy(PowerManagerProxyDelegate* delegate,
46 DBus::Connection* connection)
Darin Petkov394b7d42011-11-03 15:48:02 +010047 : proxy_(delegate, connection) {}
48
49PowerManagerProxy::~PowerManagerProxy() {}
50
Prathmesh Prabhu64ad2382014-08-26 11:19:30 -070051bool PowerManagerProxy::RegisterSuspendDelay(
52 base::TimeDelta timeout,
Paul Stewart1a212a62015-06-16 13:13:10 -070053 const string& description,
54 int* delay_id_out) {
Prathmesh Prabhu64ad2382014-08-26 11:19:30 -070055 return RegisterSuspendDelayInternal(false,
56 timeout,
57 description,
58 delay_id_out);
59}
60
61bool PowerManagerProxy::UnregisterSuspendDelay(int delay_id) {
62 return UnregisterSuspendDelayInternal(false, delay_id);
63}
64
65bool PowerManagerProxy::ReportSuspendReadiness(int delay_id, int suspend_id) {
66 return ReportSuspendReadinessInternal(false, delay_id, suspend_id);
67}
68
69bool PowerManagerProxy::RegisterDarkSuspendDelay(
70 base::TimeDelta timeout,
Paul Stewart1a212a62015-06-16 13:13:10 -070071 const string& description,
72 int* delay_id_out) {
Prathmesh Prabhu64ad2382014-08-26 11:19:30 -070073 return RegisterSuspendDelayInternal(true,
74 timeout,
75 description,
76 delay_id_out);
77}
78
79bool PowerManagerProxy::UnregisterDarkSuspendDelay(int delay_id) {
80 return UnregisterSuspendDelayInternal(true, delay_id);
81}
82
83bool PowerManagerProxy::ReportDarkSuspendReadiness(int delay_id,
84 int suspend_id ) {
85 return ReportSuspendReadinessInternal(true, delay_id, suspend_id);
86}
87
Paul Stewart1a212a62015-06-16 13:13:10 -070088bool PowerManagerProxy::RecordDarkResumeWakeReason(const string& wake_reason) {
Samuel Tan1897afa2015-05-21 14:21:56 -070089 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 Stewart1a212a62015-06-16 13:13:10 -070098 } catch (const DBus::Error& e) {
Samuel Tan1897afa2015-05-21 14:21:56 -070099 LOG(ERROR) << "DBus exception: " << e.name() << ": " << e.what();
100 return false;
101 }
102 return true;
103}
104
Prathmesh Prabhu64ad2382014-08-26 11:19:30 -0700105bool PowerManagerProxy::RegisterSuspendDelayInternal(
106 bool is_dark,
107 base::TimeDelta timeout,
Paul Stewart1a212a62015-06-16 13:13:10 -0700108 const string& description,
109 int* delay_id_out) {
Prathmesh Prabhu64ad2382014-08-26 11:19:30 -0700110 const string is_dark_arg = (is_dark ? "dark=true" : "dark=false");
111 LOG(INFO) << __func__ << "(" << timeout.InMilliseconds()
112 << ", " << is_dark_arg <<")";
Darin Petkov394b7d42011-11-03 15:48:02 +0100113
Daniel Erat0818cca2012-12-14 10:16:21 -0800114 power_manager::RegisterSuspendDelayRequest request_proto;
115 request_proto.set_timeout(timeout.ToInternalValue());
Daniel Eratf9753672013-01-24 10:17:02 -0800116 request_proto.set_description(description);
Daniel Erat0818cca2012-12-14 10:16:21 -0800117 vector<uint8_t> serialized_request;
118 CHECK(SerializeProtocolBuffer(request_proto, &serialized_request));
119
120 vector<uint8_t> serialized_reply;
Darin Petkov3ec55342012-09-28 14:04:44 +0200121 try {
Prathmesh Prabhu64ad2382014-08-26 11:19:30 -0700122 if (is_dark)
123 serialized_reply = proxy_.RegisterDarkSuspendDelay(serialized_request);
124 else
125 serialized_reply = proxy_.RegisterSuspendDelay(serialized_request);
Paul Stewart1a212a62015-06-16 13:13:10 -0700126 } catch (const DBus::Error& e) {
Darin Petkov3ec55342012-09-28 14:04:44 +0200127 LOG(ERROR) << "DBus exception: " << e.name() << ": " << e.what();
Daniel Erat0818cca2012-12-14 10:16:21 -0800128 return false;
129 }
130
131 power_manager::RegisterSuspendDelayReply reply_proto;
132 if (!DeserializeProtocolBuffer(serialized_reply, &reply_proto)) {
Prathmesh Prabhu64ad2382014-08-26 11:19:30 -0700133 LOG(ERROR) << "Failed to register "
134 << (is_dark ? "dark " : "")
135 << "suspend delay. Couldn't parse response.";
Daniel Erat0818cca2012-12-14 10:16:21 -0800136 return false;
137 }
138 *delay_id_out = reply_proto.delay_id();
139 return true;
140}
141
Prathmesh Prabhu64ad2382014-08-26 11:19:30 -0700142bool 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 Erat0818cca2012-12-14 10:16:21 -0800146
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 Prabhu64ad2382014-08-26 11:19:30 -0700153 if (is_dark)
154 proxy_.UnregisterDarkSuspendDelay(serialized_request);
155 else
156 proxy_.UnregisterSuspendDelay(serialized_request);
Paul Stewart1a212a62015-06-16 13:13:10 -0700157 } catch (const DBus::Error& e) {
Daniel Erat0818cca2012-12-14 10:16:21 -0800158 LOG(ERROR) << "DBus exception: " << e.name() << ": " << e.what();
159 return false;
Darin Petkov3ec55342012-09-28 14:04:44 +0200160 }
Prathmesh Prabhu64ad2382014-08-26 11:19:30 -0700161 return true;
Darin Petkov3ec55342012-09-28 14:04:44 +0200162}
163
Prathmesh Prabhu64ad2382014-08-26 11:19:30 -0700164bool 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 Erat0818cca2012-12-14 10:16:21 -0800172
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 Prabhu64ad2382014-08-26 11:19:30 -0700180 if (is_dark)
181 proxy_.HandleDarkSuspendReadiness(serialized_proto);
182 else
183 proxy_.HandleSuspendReadiness(serialized_proto);
Paul Stewart1a212a62015-06-16 13:13:10 -0700184 } catch (const DBus::Error& e) {
Daniel Erat0818cca2012-12-14 10:16:21 -0800185 LOG(ERROR) << "DBus exception: " << e.name() << ": " << e.what();
186 return false;
Darin Petkov3ec55342012-09-28 14:04:44 +0200187 }
Prathmesh Prabhu64ad2382014-08-26 11:19:30 -0700188 return true;
Darin Petkov3ec55342012-09-28 14:04:44 +0200189}
190
Paul Stewart1a212a62015-06-16 13:13:10 -0700191PowerManagerProxy::Proxy::Proxy(PowerManagerProxyDelegate* delegate,
192 DBus::Connection* connection)
Darin Petkov394b7d42011-11-03 15:48:02 +0100193 : DBus::ObjectProxy(*connection,
mukesh agrawal5c05b292012-03-07 10:12:52 -0800194 power_manager::kPowerManagerServicePath,
Darin Petkov394b7d42011-11-03 15:48:02 +0100195 power_manager::kPowerManagerServiceName),
196 delegate_(delegate) {}
197
198PowerManagerProxy::Proxy::~Proxy() {}
199
Daniel Erat0818cca2012-12-14 10:16:21 -0800200void PowerManagerProxy::Proxy::SuspendImminent(
Paul Stewart1a212a62015-06-16 13:13:10 -0700201 const vector<uint8_t>& serialized_proto) {
Daniel Erat0818cca2012-12-14 10:16:21 -0800202 LOG(INFO) << __func__;
Daniel Erat0818cca2012-12-14 10:16:21 -0800203 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 Petkov394b7d42011-11-03 15:48:02 +0100209}
210
Daniel Eratfac09532014-04-17 20:25:59 -0700211void PowerManagerProxy::Proxy::SuspendDone(
Paul Stewart1a212a62015-06-16 13:13:10 -0700212 const vector<uint8_t>& serialized_proto) {
Daniel Eratfac09532014-04-17 20:25:59 -0700213 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 agrawal7c1fece2012-01-13 11:31:27 -0800218 }
Daniel Eratfac09532014-04-17 20:25:59 -0700219 delegate_->OnSuspendDone(proto.suspend_id());
mukesh agrawal7c1fece2012-01-13 11:31:27 -0800220}
221
Prathmesh Prabhu64ad2382014-08-26 11:19:30 -0700222void PowerManagerProxy::Proxy::DarkSuspendImminent(
Paul Stewart1a212a62015-06-16 13:13:10 -0700223 const vector<uint8_t>& serialized_proto) {
Prathmesh Prabhu64ad2382014-08-26 11:19:30 -0700224 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 Petkov394b7d42011-11-03 15:48:02 +0100233} // namespace shill