Port all available calls to libupdate_engine_client

This is as far as the port can go without improving the library itself.

Change-Id: I5d5dbf14b62746786364b2a7e1a9131d8ef7bfb1
Test: All affected commands still signal the daemon on dragonboard
Bug: 26233663
diff --git a/client_library/client_impl.cc b/client_library/client_impl.cc
index 3dba873..af9cb05 100644
--- a/client_library/client_impl.cc
+++ b/client_library/client_impl.cc
@@ -70,10 +70,11 @@
   return proxy_->ResetStatus(nullptr);
 }
 
-bool UpdateEngineClientImpl::SetTargetChannel(const string& in_target_channel) {
+bool UpdateEngineClientImpl::SetTargetChannel(const string& in_target_channel,
+                                              bool allow_powerwash) {
   return proxy_->SetChannel(
       in_target_channel,
-      true,
+      allow_powerwash,
       nullptr);
 }
 
diff --git a/client_library/client_impl.h b/client_library/client_impl.h
index 912449f..c55f753 100644
--- a/client_library/client_impl.h
+++ b/client_library/client_impl.h
@@ -46,7 +46,8 @@
 
   bool ResetStatus() override;
 
-  bool SetTargetChannel(const std::string& target_channel) override;
+  bool SetTargetChannel(const std::string& target_channel,
+                        bool allow_powerwash) override;
 
   bool GetTargetChannel(std::string* out_channel) override;
 
diff --git a/client_library/include/update_engine/client.h b/client_library/include/update_engine/client.h
index 3406ca6..ffc4a8b 100644
--- a/client_library/include/update_engine/client.h
+++ b/client_library/include/update_engine/client.h
@@ -70,7 +70,8 @@
   virtual bool ResetStatus() = 0;
 
   // Changes the current channel of the device to the target channel.
-  virtual bool SetTargetChannel(const std::string& target_channel) = 0;
+  virtual bool SetTargetChannel(const std::string& target_channel,
+                                bool allow_powerwash) = 0;
 
   // Get the channel the device will switch to on reboot.
   virtual bool GetTargetChannel(std::string* out_channel) = 0;
diff --git a/update_engine_client.cc b/update_engine_client.cc
index 1413806..52c2cd2 100644
--- a/update_engine_client.cc
+++ b/update_engine_client.cc
@@ -28,14 +28,18 @@
 #include <brillo/daemons/dbus_daemon.h>
 #include <brillo/flag_helper.h>
 #include <dbus/bus.h>
-#include <update_engine/client.h>
-#include <update_engine/dbus-constants.h>
-#include <update_engine/dbus-proxies.h>
+
+#include "update_engine/client.h"
+#include "update_engine/update_status.h"
+#include "update_engine/dbus-constants.h"
+#include "update_engine/dbus-proxies.h"
+#include "update_status_utils.h"
 
 using std::string;
 using std::unique_ptr;
 using update_engine::kAttemptUpdateFlagNonInteractive;
 using update_engine::kUpdateEngineServiceName;
+using chromeos_update_engine::UpdateStatusToString;
 
 namespace {
 
@@ -99,28 +103,19 @@
 
   // Show the status of the update engine in stdout.
   // Blocking call. Exits the program with error 1 in case of an error.
-  void ShowStatus();
+  bool ShowStatus();
 
-  // Return the current operation status, such as UPDATE_STATUS_IDLE.
-  // Blocking call. Exits the program with error 1 in case of an error.
-  string GetCurrentOperation();
+  // Return whether we need to reboot. 0 if reboot is needed, 1 if an error
+  // occurred, 2 if no reboot is needed.
+  int GetNeedReboot();
 
   void Rollback(bool rollback);
   string GetRollbackPartition();
-  void CheckForUpdates(const string& app_version,
-                       const string& omaha_url,
-                       bool interactive);
 
   // Reboot the device if a reboot is needed.
   // Blocking call. Ignores failures.
   void RebootIfNeeded();
 
-  // Getter and setter for the target channel. If |get_current_channel| is true,
-  // the current channel instead of the target channel will be returned.
-  // Blocking call. Exits the program with error 1 in case of an error.
-  void SetTargetChannel(const string& target_channel, bool allow_powerwash);
-  string GetChannel(bool get_current_channel);
-
   // Getter and setter for the updates over cellular connections.
   // Blocking call. Exits the program with error 1 in case of an error.
   void SetUpdateOverCellularPermission(bool allowed);
@@ -144,10 +139,6 @@
   // Blocking call. Exits the program with error 1 in case of an error.
   void ShowPrevVersion();
 
-  // Returns whether the current status is such that a reboot is needed.
-  // Blocking call. Exits the program with error 1 in case of an error.
-  bool GetIsRebootNeeded();
-
   // Blocks until a reboot is needed. If the reboot is needed, exits the program
   // with 0. Otherwise it exits the program with 1 if an error occurs before
   // the reboot is needed.
@@ -247,38 +238,46 @@
                  base::Unretained(this)));
 }
 
-void UpdateEngineClient::ShowStatus() {
+bool UpdateEngineClient::ShowStatus() {
   int64_t last_checked_time = 0;
   double progress = 0.0;
-  string current_op;
+  update_engine::UpdateStatus current_op;
   string new_version;
   int64_t new_size = 0;
 
-  bool ret = proxy_->GetStatus(
-      &last_checked_time, &progress, &current_op, &new_version, &new_size,
-      nullptr);
-  CHECK(ret) << "GetStatus() failed";
+  if (!client_->GetStatus(
+      &last_checked_time, &progress, &current_op, &new_version, &new_size)) {
+    return false;
+  }
+
   printf("LAST_CHECKED_TIME=%" PRIi64 "\nPROGRESS=%f\nCURRENT_OP=%s\n"
          "NEW_VERSION=%s\nNEW_SIZE=%" PRIi64 "\n",
          last_checked_time,
          progress,
-         current_op.c_str(),
+         UpdateStatusToString(current_op),
          new_version.c_str(),
          new_size);
+
+  return true;
 }
 
-string UpdateEngineClient::GetCurrentOperation() {
+int UpdateEngineClient::GetNeedReboot() {
   int64_t last_checked_time = 0;
   double progress = 0.0;
-  string current_op;
+  update_engine::UpdateStatus current_op;
   string new_version;
   int64_t new_size = 0;
 
-  bool ret = proxy_->GetStatus(
-      &last_checked_time, &progress, &current_op, &new_version, &new_size,
-      nullptr);
-  CHECK(ret) << "GetStatus() failed";
-  return current_op;
+  if (!client_->GetStatus(
+      &last_checked_time, &progress, &current_op, &new_version, &new_size)) {
+    return 1;
+  }
+
+  if (current_op == update_engine::UpdateStatus::UPDATED_NEED_REBOOT) {
+    return 0;
+  }
+
+  return 2;
 }
 
 void UpdateEngineClient::Rollback(bool rollback) {
@@ -293,15 +292,6 @@
   return rollback_partition;
 }
 
-void UpdateEngineClient::CheckForUpdates(const string& app_version,
-                                         const string& omaha_url,
-                                         bool interactive) {
-  int32_t flags = interactive ? 0 : kAttemptUpdateFlagNonInteractive;
-  bool ret = proxy_->AttemptUpdateWithFlags(app_version, omaha_url, flags,
-                                            nullptr);
-  CHECK(ret) << "Error checking for update.";
-}
-
 void UpdateEngineClient::RebootIfNeeded() {
   bool ret = proxy_->RebootIfNeeded(nullptr);
   if (!ret) {
@@ -312,20 +302,6 @@
   }
 }
 
-void UpdateEngineClient::SetTargetChannel(const string& target_channel,
-                                          bool allow_powerwash) {
-  bool ret = proxy_->SetChannel(target_channel, allow_powerwash, nullptr);
-  CHECK(ret) << "Error setting the channel.";
-  LOG(INFO) << "Channel permanently set to: " << target_channel;
-}
-
-string UpdateEngineClient::GetChannel(bool get_current_channel) {
-  string channel;
-  bool ret = proxy_->GetChannel(get_current_channel, &channel, nullptr);
-  CHECK(ret) << "Error getting the channel.";
-  return channel;
-}
-
 void UpdateEngineClient::SetUpdateOverCellularPermission(bool allowed) {
   bool ret = proxy_->SetUpdateOverCellularPermission(allowed, nullptr);
   CHECK(ret) << "Error setting the update over cellular setting.";
@@ -385,10 +361,6 @@
   }
 }
 
-bool UpdateEngineClient::GetIsRebootNeeded() {
-  return GetCurrentOperation() == update_engine::kUpdateStatusUpdatedNeedReboot;
-}
-
 void UpdateEngineClient::OnRebootNeededCheck(
     int64_t /* last_checked_time */,
     double /* progress */,
@@ -405,8 +377,17 @@
     const string& interface,
     const string& signal_name,
     bool success) {
-  if (GetIsRebootNeeded())
+  int got = GetNeedReboot();
+
+  if (got == 1) {
+    LOG(ERROR) << "Could not query the current operation.";
+    exit(1);
+  }
+
+  if (got == 0) {
     exit(0);
+  }
+
   if (!success) {
     LOG(ERROR) << "Couldn't connect to the " << signal_name << " signal.";
     exit(1);
@@ -421,7 +402,14 @@
                  base::Unretained(this)),
       base::Bind(&UpdateEngineClient::OnStatusUpdateSignalRegistration,
                  base::Unretained(this)));
-  if (GetIsRebootNeeded())
+  int got = GetNeedReboot();
+
+  if (got == 1) {
+    LOG(ERROR) << "Could not query the current operation.";
+    exit(1);
+  }
+
+  if (got == 0)
     exit(0);
 }
 
@@ -556,15 +544,32 @@
   }
 
   // First, update the target channel if requested.
-  if (!FLAGS_channel.empty())
-    SetTargetChannel(FLAGS_channel, FLAGS_powerwash);
+  if (!FLAGS_channel.empty()) {
+    if (!client_->SetTargetChannel(FLAGS_channel, FLAGS_powerwash)) {
+      LOG(ERROR) << "Error setting the channel.";
+      return 1;
+    }
+
+    LOG(INFO) << "Channel permanently set to: " << FLAGS_channel;
+  }
 
   // Show the current and target channels if requested.
   if (FLAGS_show_channel) {
-    string current_channel = GetChannel(true);
+    string current_channel;
+    string target_channel;
+
+    if (!client_->GetChannel(&current_channel)) {
+      LOG(ERROR) << "Error getting the current channel.";
+      return 1;
+    }
+
+    if (!client_->GetTargetChannel(&target_channel)) {
+      LOG(ERROR) << "Error getting the target channel.";
+      return 1;
+    }
+
     LOG(INFO) << "Current Channel: " << current_channel;
 
-    string target_channel = GetChannel(false);
     if (!target_channel.empty())
       LOG(INFO) << "Target Channel (pending update): " << target_channel;
   }
@@ -594,7 +599,11 @@
       LOG(INFO) << "Forcing an update by setting app_version to ForcedUpdate.";
     }
     LOG(INFO) << "Initiating update check and install.";
-    CheckForUpdates(app_version, FLAGS_omaha_url, FLAGS_interactive);
+    if (!client_->AttemptUpdate(app_version, FLAGS_omaha_url,
+                                FLAGS_interactive)) {
+      LOG(ERROR) << "Error checking for update.";
+      return 1;
+    }
   }
 
   // These final options are all mutually exclusive with one another.
@@ -610,7 +619,10 @@
 
   if (FLAGS_status) {
     LOG(INFO) << "Querying Update Engine status...";
-    ShowStatus();
+    if (!ShowStatus()) {
+      LOG(ERROR) << "Failed to query status";
+      return 1;
+    }
     return 0;
   }
 
@@ -637,12 +649,13 @@
   }
 
   if (FLAGS_is_reboot_needed) {
-    // In case of error GetIsRebootNeeded() will exit with 1.
-    if (GetIsRebootNeeded()) {
-      return 0;
-    } else {
-      return 2;
+    int ret = GetNeedReboot();
+
+    if (ret == 1) {
+      LOG(ERROR) << "Could not query the current operation.";
     }
+
+    return ret;
   }
 
   if (FLAGS_block_until_reboot_is_needed) {