blob: ae53dc1732420e116b565ce3662e984412b05713 [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.
21bool SerializeProtocolBuffer(const google::protobuf::MessageLite &protobuf,
22 vector<uint8_t> *out) {
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.
34bool DeserializeProtocolBuffer(const vector<uint8_t> &serialized_protobuf,
35 google::protobuf::MessageLite *protobuf_out) {
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
Darin Petkov394b7d42011-11-03 15:48:02 +010045PowerManagerProxy::PowerManagerProxy(PowerManagerProxyDelegate *delegate,
46 DBus::Connection *connection)
47 : proxy_(delegate, connection) {}
48
49PowerManagerProxy::~PowerManagerProxy() {}
50
Prathmesh Prabhu64ad2382014-08-26 11:19:30 -070051bool PowerManagerProxy::RegisterSuspendDelay(
52 base::TimeDelta timeout,
53 const string &description,
54 int *delay_id_out) {
55 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,
71 const string &description,
72 int *delay_id_out) {
73 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
88bool PowerManagerProxy::RegisterSuspendDelayInternal(
89 bool is_dark,
90 base::TimeDelta timeout,
91 const string &description,
92 int *delay_id_out) {
93 const string is_dark_arg = (is_dark ? "dark=true" : "dark=false");
94 LOG(INFO) << __func__ << "(" << timeout.InMilliseconds()
95 << ", " << is_dark_arg <<")";
Darin Petkov394b7d42011-11-03 15:48:02 +010096
Daniel Erat0818cca2012-12-14 10:16:21 -080097 power_manager::RegisterSuspendDelayRequest request_proto;
98 request_proto.set_timeout(timeout.ToInternalValue());
Daniel Eratf9753672013-01-24 10:17:02 -080099 request_proto.set_description(description);
Daniel Erat0818cca2012-12-14 10:16:21 -0800100 vector<uint8_t> serialized_request;
101 CHECK(SerializeProtocolBuffer(request_proto, &serialized_request));
102
103 vector<uint8_t> serialized_reply;
Darin Petkov3ec55342012-09-28 14:04:44 +0200104 try {
Prathmesh Prabhu64ad2382014-08-26 11:19:30 -0700105 if (is_dark)
106 serialized_reply = proxy_.RegisterDarkSuspendDelay(serialized_request);
107 else
108 serialized_reply = proxy_.RegisterSuspendDelay(serialized_request);
Darin Petkov3ec55342012-09-28 14:04:44 +0200109 } catch (const DBus::Error &e) {
110 LOG(ERROR) << "DBus exception: " << e.name() << ": " << e.what();
Daniel Erat0818cca2012-12-14 10:16:21 -0800111 return false;
112 }
113
114 power_manager::RegisterSuspendDelayReply reply_proto;
115 if (!DeserializeProtocolBuffer(serialized_reply, &reply_proto)) {
Prathmesh Prabhu64ad2382014-08-26 11:19:30 -0700116 LOG(ERROR) << "Failed to register "
117 << (is_dark ? "dark " : "")
118 << "suspend delay. Couldn't parse response.";
Daniel Erat0818cca2012-12-14 10:16:21 -0800119 return false;
120 }
121 *delay_id_out = reply_proto.delay_id();
122 return true;
123}
124
Prathmesh Prabhu64ad2382014-08-26 11:19:30 -0700125bool PowerManagerProxy::UnregisterSuspendDelayInternal(bool is_dark,
126 int delay_id) {
127 const string is_dark_arg = (is_dark ? "dark=true" : "dark=false");
128 LOG(INFO) << __func__ << "(" << delay_id << ", " << is_dark_arg << ")";
Daniel Erat0818cca2012-12-14 10:16:21 -0800129
130 power_manager::UnregisterSuspendDelayRequest request_proto;
131 request_proto.set_delay_id(delay_id);
132 vector<uint8_t> serialized_request;
133 CHECK(SerializeProtocolBuffer(request_proto, &serialized_request));
134
135 try {
Prathmesh Prabhu64ad2382014-08-26 11:19:30 -0700136 if (is_dark)
137 proxy_.UnregisterDarkSuspendDelay(serialized_request);
138 else
139 proxy_.UnregisterSuspendDelay(serialized_request);
Daniel Erat0818cca2012-12-14 10:16:21 -0800140 } catch (const DBus::Error &e) {
141 LOG(ERROR) << "DBus exception: " << e.name() << ": " << e.what();
142 return false;
Darin Petkov3ec55342012-09-28 14:04:44 +0200143 }
Prathmesh Prabhu64ad2382014-08-26 11:19:30 -0700144 return true;
Darin Petkov3ec55342012-09-28 14:04:44 +0200145}
146
Prathmesh Prabhu64ad2382014-08-26 11:19:30 -0700147bool PowerManagerProxy::ReportSuspendReadinessInternal(bool is_dark,
148 int delay_id,
149 int suspend_id) {
150 const string is_dark_arg = (is_dark ? "dark=true" : "dark=false");
151 LOG(INFO) << __func__
152 << "(" << delay_id
153 << ", " << suspend_id
154 << ", " << is_dark_arg << ")";
Daniel Erat0818cca2012-12-14 10:16:21 -0800155
156 power_manager::SuspendReadinessInfo proto;
157 proto.set_delay_id(delay_id);
158 proto.set_suspend_id(suspend_id);
159 vector<uint8_t> serialized_proto;
160 CHECK(SerializeProtocolBuffer(proto, &serialized_proto));
161
162 try {
Prathmesh Prabhu64ad2382014-08-26 11:19:30 -0700163 if (is_dark)
164 proxy_.HandleDarkSuspendReadiness(serialized_proto);
165 else
166 proxy_.HandleSuspendReadiness(serialized_proto);
Daniel Erat0818cca2012-12-14 10:16:21 -0800167 } catch (const DBus::Error &e) {
168 LOG(ERROR) << "DBus exception: " << e.name() << ": " << e.what();
169 return false;
Darin Petkov3ec55342012-09-28 14:04:44 +0200170 }
Prathmesh Prabhu64ad2382014-08-26 11:19:30 -0700171 return true;
Darin Petkov3ec55342012-09-28 14:04:44 +0200172}
173
Darin Petkov394b7d42011-11-03 15:48:02 +0100174PowerManagerProxy::Proxy::Proxy(PowerManagerProxyDelegate *delegate,
175 DBus::Connection *connection)
176 : DBus::ObjectProxy(*connection,
mukesh agrawal5c05b292012-03-07 10:12:52 -0800177 power_manager::kPowerManagerServicePath,
Darin Petkov394b7d42011-11-03 15:48:02 +0100178 power_manager::kPowerManagerServiceName),
179 delegate_(delegate) {}
180
181PowerManagerProxy::Proxy::~Proxy() {}
182
Daniel Erat0818cca2012-12-14 10:16:21 -0800183void PowerManagerProxy::Proxy::SuspendImminent(
184 const vector<uint8_t> &serialized_proto) {
185 LOG(INFO) << __func__;
Daniel Erat0818cca2012-12-14 10:16:21 -0800186 power_manager::SuspendImminent proto;
187 if (!DeserializeProtocolBuffer(serialized_proto, &proto)) {
188 LOG(ERROR) << "Failed to parse SuspendImminent signal.";
189 return;
190 }
191 delegate_->OnSuspendImminent(proto.suspend_id());
Darin Petkov394b7d42011-11-03 15:48:02 +0100192}
193
Daniel Eratfac09532014-04-17 20:25:59 -0700194void PowerManagerProxy::Proxy::SuspendDone(
195 const vector<uint8_t> &serialized_proto) {
196 LOG(INFO) << __func__;
197 power_manager::SuspendDone proto;
198 if (!DeserializeProtocolBuffer(serialized_proto, &proto)) {
199 LOG(ERROR) << "Failed to parse SuspendDone signal.";
200 return;
mukesh agrawal7c1fece2012-01-13 11:31:27 -0800201 }
Daniel Eratfac09532014-04-17 20:25:59 -0700202 delegate_->OnSuspendDone(proto.suspend_id());
mukesh agrawal7c1fece2012-01-13 11:31:27 -0800203}
204
Prathmesh Prabhu64ad2382014-08-26 11:19:30 -0700205void PowerManagerProxy::Proxy::DarkSuspendImminent(
206 const vector<uint8_t> &serialized_proto) {
207 LOG(INFO) << __func__;
208 power_manager::SuspendImminent proto;
209 if (!DeserializeProtocolBuffer(serialized_proto, &proto)) {
210 LOG(ERROR) << "Failed to parse DarkSuspendImminent signal.";
211 return;
212 }
213 delegate_->OnDarkSuspendImminent(proto.suspend_id());
214}
215
Darin Petkov394b7d42011-11-03 15:48:02 +0100216} // namespace shill