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 | |
Christopher Wiley | b691efd | 2012-08-09 13:51:51 -0700 | [diff] [blame] | 10 | #include "shill/logging.h" |
Daniel Erat | 0818cca | 2012-12-14 10:16:21 -0800 | [diff] [blame] | 11 | #include "shill/proto_bindings/power_manager/suspend.pb.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. |
| 21 | bool 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. |
| 34 | bool 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 Petkov | 394b7d4 | 2011-11-03 15:48:02 +0100 | [diff] [blame] | 45 | PowerManagerProxy::PowerManagerProxy(PowerManagerProxyDelegate *delegate, |
| 46 | DBus::Connection *connection) |
| 47 | : proxy_(delegate, connection) {} |
| 48 | |
| 49 | PowerManagerProxy::~PowerManagerProxy() {} |
| 50 | |
Daniel Erat | 0818cca | 2012-12-14 10:16:21 -0800 | [diff] [blame] | 51 | bool PowerManagerProxy::RegisterSuspendDelay(base::TimeDelta timeout, |
| 52 | int *delay_id_out) { |
| 53 | LOG(INFO) << __func__ << "(" << timeout.InMilliseconds() << ")"; |
Darin Petkov | 394b7d4 | 2011-11-03 15:48:02 +0100 | [diff] [blame] | 54 | |
Daniel Erat | 0818cca | 2012-12-14 10:16:21 -0800 | [diff] [blame] | 55 | 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 Petkov | 3ec5534 | 2012-09-28 14:04:44 +0200 | [diff] [blame] | 61 | try { |
Daniel Erat | 0818cca | 2012-12-14 10:16:21 -0800 | [diff] [blame] | 62 | serialized_reply = proxy_.RegisterSuspendDelay(serialized_request); |
Darin Petkov | 3ec5534 | 2012-09-28 14:04:44 +0200 | [diff] [blame] | 63 | } catch (const DBus::Error &e) { |
| 64 | LOG(ERROR) << "DBus exception: " << e.name() << ": " << e.what(); |
Daniel Erat | 0818cca | 2012-12-14 10:16:21 -0800 | [diff] [blame] | 65 | 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 | |
| 77 | bool 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 Petkov | 3ec5534 | 2012-09-28 14:04:44 +0200 | [diff] [blame] | 91 | } |
| 92 | } |
| 93 | |
Daniel Erat | 0818cca | 2012-12-14 10:16:21 -0800 | [diff] [blame] | 94 | bool 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 Petkov | 3ec5534 | 2012-09-28 14:04:44 +0200 | [diff] [blame] | 109 | } |
| 110 | } |
| 111 | |
Darin Petkov | 394b7d4 | 2011-11-03 15:48:02 +0100 | [diff] [blame] | 112 | PowerManagerProxy::Proxy::Proxy(PowerManagerProxyDelegate *delegate, |
| 113 | DBus::Connection *connection) |
| 114 | : DBus::ObjectProxy(*connection, |
mukesh agrawal | 5c05b29 | 2012-03-07 10:12:52 -0800 | [diff] [blame] | 115 | power_manager::kPowerManagerServicePath, |
Darin Petkov | 394b7d4 | 2011-11-03 15:48:02 +0100 | [diff] [blame] | 116 | power_manager::kPowerManagerServiceName), |
| 117 | delegate_(delegate) {} |
| 118 | |
| 119 | PowerManagerProxy::Proxy::~Proxy() {} |
| 120 | |
Daniel Erat | 0818cca | 2012-12-14 10:16:21 -0800 | [diff] [blame] | 121 | void 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 Petkov | 394b7d4 | 2011-11-03 15:48:02 +0100 | [diff] [blame] | 131 | } |
| 132 | |
mukesh agrawal | 7c1fece | 2012-01-13 11:31:27 -0800 | [diff] [blame] | 133 | void PowerManagerProxy::Proxy::PowerStateChanged( |
| 134 | const string &new_power_state) { |
Darin Petkov | 3ec5534 | 2012-09-28 14:04:44 +0200 | [diff] [blame] | 135 | LOG(INFO) << __func__ << "(" << new_power_state << ")"; |
mukesh agrawal | 7c1fece | 2012-01-13 11:31:27 -0800 | [diff] [blame] | 136 | |
| 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 Petkov | 394b7d4 | 2011-11-03 15:48:02 +0100 | [diff] [blame] | 152 | } // namespace shill |