blob: 8382e75aeb1fc02045dadb81005c79db6c1b0550 [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
Christopher Wileyb691efd2012-08-09 13:51:51 -070010#include "shill/logging.h"
Daniel Erat0818cca2012-12-14 10:16:21 -080011#include "shill/proto_bindings/power_manager/suspend.pb.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
Daniel Erat0818cca2012-12-14 10:16:21 -080051bool PowerManagerProxy::RegisterSuspendDelay(base::TimeDelta timeout,
Daniel Eratf9753672013-01-24 10:17:02 -080052 const string &description,
Daniel Erat0818cca2012-12-14 10:16:21 -080053 int *delay_id_out) {
54 LOG(INFO) << __func__ << "(" << timeout.InMilliseconds() << ")";
Darin Petkov394b7d42011-11-03 15:48:02 +010055
Daniel Erat0818cca2012-12-14 10:16:21 -080056 power_manager::RegisterSuspendDelayRequest request_proto;
57 request_proto.set_timeout(timeout.ToInternalValue());
Daniel Eratf9753672013-01-24 10:17:02 -080058 request_proto.set_description(description);
Daniel Erat0818cca2012-12-14 10:16:21 -080059 vector<uint8_t> serialized_request;
60 CHECK(SerializeProtocolBuffer(request_proto, &serialized_request));
61
62 vector<uint8_t> serialized_reply;
Darin Petkov3ec55342012-09-28 14:04:44 +020063 try {
Daniel Erat0818cca2012-12-14 10:16:21 -080064 serialized_reply = proxy_.RegisterSuspendDelay(serialized_request);
Darin Petkov3ec55342012-09-28 14:04:44 +020065 } catch (const DBus::Error &e) {
66 LOG(ERROR) << "DBus exception: " << e.name() << ": " << e.what();
Daniel Erat0818cca2012-12-14 10:16:21 -080067 return false;
68 }
69
70 power_manager::RegisterSuspendDelayReply reply_proto;
71 if (!DeserializeProtocolBuffer(serialized_reply, &reply_proto)) {
72 LOG(ERROR) << "Failed to register suspend delay. Couldn't parse response.";
73 return false;
74 }
75 *delay_id_out = reply_proto.delay_id();
76 return true;
77}
78
79bool PowerManagerProxy::UnregisterSuspendDelay(int delay_id) {
80 LOG(INFO) << __func__ << "(" << delay_id << ")";
81
82 power_manager::UnregisterSuspendDelayRequest request_proto;
83 request_proto.set_delay_id(delay_id);
84 vector<uint8_t> serialized_request;
85 CHECK(SerializeProtocolBuffer(request_proto, &serialized_request));
86
87 try {
88 proxy_.UnregisterSuspendDelay(serialized_request);
89 return true;
90 } catch (const DBus::Error &e) {
91 LOG(ERROR) << "DBus exception: " << e.name() << ": " << e.what();
92 return false;
Darin Petkov3ec55342012-09-28 14:04:44 +020093 }
94}
95
Daniel Erat0818cca2012-12-14 10:16:21 -080096bool PowerManagerProxy::ReportSuspendReadiness(int delay_id, int suspend_id) {
97 LOG(INFO) << __func__ << "(" << delay_id << ", " << suspend_id << ")";
98
99 power_manager::SuspendReadinessInfo proto;
100 proto.set_delay_id(delay_id);
101 proto.set_suspend_id(suspend_id);
102 vector<uint8_t> serialized_proto;
103 CHECK(SerializeProtocolBuffer(proto, &serialized_proto));
104
105 try {
106 proxy_.HandleSuspendReadiness(serialized_proto);
107 return true;
108 } catch (const DBus::Error &e) {
109 LOG(ERROR) << "DBus exception: " << e.name() << ": " << e.what();
110 return false;
Darin Petkov3ec55342012-09-28 14:04:44 +0200111 }
112}
113
Darin Petkov394b7d42011-11-03 15:48:02 +0100114PowerManagerProxy::Proxy::Proxy(PowerManagerProxyDelegate *delegate,
115 DBus::Connection *connection)
116 : DBus::ObjectProxy(*connection,
mukesh agrawal5c05b292012-03-07 10:12:52 -0800117 power_manager::kPowerManagerServicePath,
Darin Petkov394b7d42011-11-03 15:48:02 +0100118 power_manager::kPowerManagerServiceName),
119 delegate_(delegate) {}
120
121PowerManagerProxy::Proxy::~Proxy() {}
122
Daniel Erat0818cca2012-12-14 10:16:21 -0800123void PowerManagerProxy::Proxy::SuspendImminent(
124 const vector<uint8_t> &serialized_proto) {
125 LOG(INFO) << __func__;
126
127 power_manager::SuspendImminent proto;
128 if (!DeserializeProtocolBuffer(serialized_proto, &proto)) {
129 LOG(ERROR) << "Failed to parse SuspendImminent signal.";
130 return;
131 }
132 delegate_->OnSuspendImminent(proto.suspend_id());
Darin Petkov394b7d42011-11-03 15:48:02 +0100133}
134
mukesh agrawal7c1fece2012-01-13 11:31:27 -0800135void PowerManagerProxy::Proxy::PowerStateChanged(
136 const string &new_power_state) {
Darin Petkov3ec55342012-09-28 14:04:44 +0200137 LOG(INFO) << __func__ << "(" << new_power_state << ")";
mukesh agrawal7c1fece2012-01-13 11:31:27 -0800138
139 PowerManagerProxyDelegate::SuspendState suspend_state;
140 if (new_power_state == "on") {
141 suspend_state = PowerManagerProxyDelegate::kOn;
142 } else if (new_power_state == "standby") {
143 suspend_state = PowerManagerProxyDelegate::kStandby;
144 } else if (new_power_state == "mem") {
145 suspend_state = PowerManagerProxyDelegate::kMem;
146 } else if (new_power_state == "disk") {
147 suspend_state = PowerManagerProxyDelegate::kDisk;
148 } else {
149 suspend_state = PowerManagerProxyDelegate::kUnknown;
150 }
151 delegate_->OnPowerStateChanged(suspend_state);
152}
153
Darin Petkov394b7d42011-11-03 15:48:02 +0100154} // namespace shill