Add handling of base.reboot to weaved

Now that weaved can use binder on Brillo, add a command handler for
base.reboot command and delegate rebooting to native power manager
daemon.

BUG: 23759426
Change-Id: I88f18d953ae3072b154d6cc822d5064207316189
diff --git a/Android.mk b/Android.mk
index 10d7ba3..d1b171d 100644
--- a/Android.mk
+++ b/Android.mk
@@ -48,6 +48,7 @@
 	libchrome-dbus \
 	libcutils \
 	libdbus \
+	libnativepower \
 	libshill-client \
 	libutils \
 	libweave \
diff --git a/buffet/manager.cc b/buffet/manager.cc
index 27610ec..1323850 100644
--- a/buffet/manager.cc
+++ b/buffet/manager.cc
@@ -60,6 +60,8 @@
 
 const char kErrorDomain[] = "buffet";
 const char kFileReadError[] = "file_read_error";
+const char kBaseComponent[] = "base";
+const char kRebootCommand[] = "base.reboot";
 
 bool LoadFile(const base::FilePath& file_path,
               std::string* data,
@@ -188,6 +190,7 @@
 }
 
 void Manager::Start(AsyncEventSequencer* sequencer) {
+  power_manager_client_.Init();
   RestartWeave(sequencer);
 }
 
@@ -260,6 +263,10 @@
       base::Bind(&Manager::OnPairingStart, weak_ptr_factory_.GetWeakPtr()),
       base::Bind(&Manager::OnPairingEnd, weak_ptr_factory_.GetWeakPtr()));
 
+  device_->AddCommandHandler(kBaseComponent, kRebootCommand,
+                             base::Bind(&Manager::OnRebootDevice,
+                                        weak_ptr_factory_.GetWeakPtr()));
+
   CreateServicesForClients();
 }
 
@@ -339,6 +346,21 @@
   NotifyServiceManagerChange(ids);
 }
 
+void Manager::OnRebootDevice(const std::weak_ptr<weave::Command>& cmd) {
+  auto command = cmd.lock();
+  if (!command || !command->Complete({}, nullptr))
+    return;
+
+  task_runner_->PostDelayedTask(
+      FROM_HERE,
+      base::Bind(&Manager::RebootDeviceNow, weak_ptr_factory_.GetWeakPtr()),
+      base::TimeDelta::FromSeconds(2));
+}
+
+void Manager::RebootDeviceNow() {
+  power_manager_client_.Reboot(android::RebootReason::DEFAULT);
+}
+
 android::binder::Status Manager::connect(
     const android::sp<android::weave::IWeaveClient>& client) {
   pending_clients_.push_back(client);
diff --git a/buffet/manager.h b/buffet/manager.h
index 18e1aea..e152e40 100644
--- a/buffet/manager.h
+++ b/buffet/manager.h
@@ -26,6 +26,7 @@
 #include <base/values.h>
 #include <brillo/dbus/async_event_sequencer.h>
 #include <brillo/errors/error.h>
+#include <nativepower/power_manager_client.h>
 #include <weave/device.h>
 
 #include "android/weave/BnWeaveServiceManager.h"
@@ -103,6 +104,8 @@
   void OnNotificationListenerDestroyed(
       const WeaveServiceManagerNotificationListener& notification_listener);
   void NotifyServiceManagerChange(const std::vector<int>& notification_ids);
+  void OnRebootDevice(const std::weak_ptr<weave::Command>& cmd);
+  void RebootDeviceNow();
 
   Options options_;
   scoped_refptr<dbus::Bus> bus_;
@@ -121,6 +124,7 @@
   std::map<android::sp<android::weave::IWeaveClient>,
            android::sp<BinderWeaveService>> services_;
   std::set<WeaveServiceManagerNotificationListener> notification_listeners_;
+  android::PowerManagerClient power_manager_client_;
 
   // State properties.
   std::string cloud_id_;