blob: 10d73a84ae2c946a83b440e85e1d1a8c94fae741 [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,
52 int *delay_id_out) {
53 LOG(INFO) << __func__ << "(" << timeout.InMilliseconds() << ")";
Darin Petkov394b7d42011-11-03 15:48:02 +010054
Daniel Erat0818cca2012-12-14 10:16:21 -080055 power_manager::RegisterSuspendDelayRequest request_proto;
56 request_proto.set_timeout(timeout.ToInternalValue());
57 vector<uint8_t> serialized_request;
58 CHECK(SerializeProtocolBuffer(request_proto, &serialized_request));
59
60 vector<uint8_t> serialized_reply;
Darin Petkov3ec55342012-09-28 14:04:44 +020061 try {
Daniel Erat0818cca2012-12-14 10:16:21 -080062 serialized_reply = proxy_.RegisterSuspendDelay(serialized_request);
Darin Petkov3ec55342012-09-28 14:04:44 +020063 } catch (const DBus::Error &e) {
64 LOG(ERROR) << "DBus exception: " << e.name() << ": " << e.what();
Daniel Erat0818cca2012-12-14 10:16:21 -080065 return false;
66 }
67
68 power_manager::RegisterSuspendDelayReply reply_proto;
69 if (!DeserializeProtocolBuffer(serialized_reply, &reply_proto)) {
70 LOG(ERROR) << "Failed to register suspend delay. Couldn't parse response.";
71 return false;
72 }
73 *delay_id_out = reply_proto.delay_id();
74 return true;
75}
76
77bool PowerManagerProxy::UnregisterSuspendDelay(int delay_id) {
78 LOG(INFO) << __func__ << "(" << delay_id << ")";
79
80 power_manager::UnregisterSuspendDelayRequest request_proto;
81 request_proto.set_delay_id(delay_id);
82 vector<uint8_t> serialized_request;
83 CHECK(SerializeProtocolBuffer(request_proto, &serialized_request));
84
85 try {
86 proxy_.UnregisterSuspendDelay(serialized_request);
87 return true;
88 } catch (const DBus::Error &e) {
89 LOG(ERROR) << "DBus exception: " << e.name() << ": " << e.what();
90 return false;
Darin Petkov3ec55342012-09-28 14:04:44 +020091 }
92}
93
Daniel Erat0818cca2012-12-14 10:16:21 -080094bool PowerManagerProxy::ReportSuspendReadiness(int delay_id, int suspend_id) {
95 LOG(INFO) << __func__ << "(" << delay_id << ", " << suspend_id << ")";
96
97 power_manager::SuspendReadinessInfo proto;
98 proto.set_delay_id(delay_id);
99 proto.set_suspend_id(suspend_id);
100 vector<uint8_t> serialized_proto;
101 CHECK(SerializeProtocolBuffer(proto, &serialized_proto));
102
103 try {
104 proxy_.HandleSuspendReadiness(serialized_proto);
105 return true;
106 } catch (const DBus::Error &e) {
107 LOG(ERROR) << "DBus exception: " << e.name() << ": " << e.what();
108 return false;
Darin Petkov3ec55342012-09-28 14:04:44 +0200109 }
110}
111
Darin Petkov394b7d42011-11-03 15:48:02 +0100112PowerManagerProxy::Proxy::Proxy(PowerManagerProxyDelegate *delegate,
113 DBus::Connection *connection)
114 : DBus::ObjectProxy(*connection,
mukesh agrawal5c05b292012-03-07 10:12:52 -0800115 power_manager::kPowerManagerServicePath,
Darin Petkov394b7d42011-11-03 15:48:02 +0100116 power_manager::kPowerManagerServiceName),
117 delegate_(delegate) {}
118
119PowerManagerProxy::Proxy::~Proxy() {}
120
Daniel Erat0818cca2012-12-14 10:16:21 -0800121void PowerManagerProxy::Proxy::SuspendImminent(
122 const vector<uint8_t> &serialized_proto) {
123 LOG(INFO) << __func__;
124
125 power_manager::SuspendImminent proto;
126 if (!DeserializeProtocolBuffer(serialized_proto, &proto)) {
127 LOG(ERROR) << "Failed to parse SuspendImminent signal.";
128 return;
129 }
130 delegate_->OnSuspendImminent(proto.suspend_id());
Darin Petkov394b7d42011-11-03 15:48:02 +0100131}
132
mukesh agrawal7c1fece2012-01-13 11:31:27 -0800133void PowerManagerProxy::Proxy::PowerStateChanged(
134 const string &new_power_state) {
Darin Petkov3ec55342012-09-28 14:04:44 +0200135 LOG(INFO) << __func__ << "(" << new_power_state << ")";
mukesh agrawal7c1fece2012-01-13 11:31:27 -0800136
137 PowerManagerProxyDelegate::SuspendState suspend_state;
138 if (new_power_state == "on") {
139 suspend_state = PowerManagerProxyDelegate::kOn;
140 } else if (new_power_state == "standby") {
141 suspend_state = PowerManagerProxyDelegate::kStandby;
142 } else if (new_power_state == "mem") {
143 suspend_state = PowerManagerProxyDelegate::kMem;
144 } else if (new_power_state == "disk") {
145 suspend_state = PowerManagerProxyDelegate::kDisk;
146 } else {
147 suspend_state = PowerManagerProxyDelegate::kUnknown;
148 }
149 delegate_->OnPowerStateChanged(suspend_state);
150}
151
Darin Petkov394b7d42011-11-03 15:48:02 +0100152} // namespace shill