Android: Print the error code string from the client.

The update_engine_client needs to translate the numeric ErrorCode to a
string name that can be printed on the output. This patch moves the
ErrorCodeToString() function to a new error_code_utils.{h,cc} pair of
files so it can be included easily from the client binary and uses it
in the Android update_engine_client.

Bug: 25631767
Bug: 25598547
TEST=`update_engine_client --update` prints the error message in a non-Brillo device.

Change-Id: Ib40813924ec676f3e703412de90d389b2596177e
diff --git a/common/error_code_utils.cc b/common/error_code_utils.cc
new file mode 100644
index 0000000..dc9eaf4
--- /dev/null
+++ b/common/error_code_utils.cc
@@ -0,0 +1,153 @@
+//
+// Copyright (C) 2012 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+#include "update_engine/common/error_code_utils.h"
+
+#include <base/strings/string_number_conversions.h>
+
+using std::string;
+
+namespace chromeos_update_engine {
+namespace utils {
+
+string ErrorCodeToString(ErrorCode code) {
+  // If the given code has both parts (i.e. the error code part and the flags
+  // part) then strip off the flags part since the switch statement below
+  // has case statements only for the base error code or a single flag but
+  // doesn't support any combinations of those.
+  if ((static_cast<int>(code) & static_cast<int>(ErrorCode::kSpecialFlags)) &&
+      (static_cast<int>(code) & ~static_cast<int>(ErrorCode::kSpecialFlags)))
+    code = static_cast<ErrorCode>(
+        static_cast<int>(code) & ~static_cast<int>(ErrorCode::kSpecialFlags));
+  switch (code) {
+    case ErrorCode::kSuccess: return "ErrorCode::kSuccess";
+    case ErrorCode::kError: return "ErrorCode::kError";
+    case ErrorCode::kOmahaRequestError: return "ErrorCode::kOmahaRequestError";
+    case ErrorCode::kOmahaResponseHandlerError:
+      return "ErrorCode::kOmahaResponseHandlerError";
+    case ErrorCode::kFilesystemCopierError:
+      return "ErrorCode::kFilesystemCopierError";
+    case ErrorCode::kPostinstallRunnerError:
+      return "ErrorCode::kPostinstallRunnerError";
+    case ErrorCode::kPayloadMismatchedType:
+      return "ErrorCode::kPayloadMismatchedType";
+    case ErrorCode::kInstallDeviceOpenError:
+      return "ErrorCode::kInstallDeviceOpenError";
+    case ErrorCode::kKernelDeviceOpenError:
+      return "ErrorCode::kKernelDeviceOpenError";
+    case ErrorCode::kDownloadTransferError:
+      return "ErrorCode::kDownloadTransferError";
+    case ErrorCode::kPayloadHashMismatchError:
+      return "ErrorCode::kPayloadHashMismatchError";
+    case ErrorCode::kPayloadSizeMismatchError:
+      return "ErrorCode::kPayloadSizeMismatchError";
+    case ErrorCode::kDownloadPayloadVerificationError:
+      return "ErrorCode::kDownloadPayloadVerificationError";
+    case ErrorCode::kDownloadNewPartitionInfoError:
+      return "ErrorCode::kDownloadNewPartitionInfoError";
+    case ErrorCode::kDownloadWriteError:
+      return "ErrorCode::kDownloadWriteError";
+    case ErrorCode::kNewRootfsVerificationError:
+      return "ErrorCode::kNewRootfsVerificationError";
+    case ErrorCode::kNewKernelVerificationError:
+      return "ErrorCode::kNewKernelVerificationError";
+    case ErrorCode::kSignedDeltaPayloadExpectedError:
+      return "ErrorCode::kSignedDeltaPayloadExpectedError";
+    case ErrorCode::kDownloadPayloadPubKeyVerificationError:
+      return "ErrorCode::kDownloadPayloadPubKeyVerificationError";
+    case ErrorCode::kPostinstallBootedFromFirmwareB:
+      return "ErrorCode::kPostinstallBootedFromFirmwareB";
+    case ErrorCode::kDownloadStateInitializationError:
+      return "ErrorCode::kDownloadStateInitializationError";
+    case ErrorCode::kDownloadInvalidMetadataMagicString:
+      return "ErrorCode::kDownloadInvalidMetadataMagicString";
+    case ErrorCode::kDownloadSignatureMissingInManifest:
+      return "ErrorCode::kDownloadSignatureMissingInManifest";
+    case ErrorCode::kDownloadManifestParseError:
+      return "ErrorCode::kDownloadManifestParseError";
+    case ErrorCode::kDownloadMetadataSignatureError:
+      return "ErrorCode::kDownloadMetadataSignatureError";
+    case ErrorCode::kDownloadMetadataSignatureVerificationError:
+      return "ErrorCode::kDownloadMetadataSignatureVerificationError";
+    case ErrorCode::kDownloadMetadataSignatureMismatch:
+      return "ErrorCode::kDownloadMetadataSignatureMismatch";
+    case ErrorCode::kDownloadOperationHashVerificationError:
+      return "ErrorCode::kDownloadOperationHashVerificationError";
+    case ErrorCode::kDownloadOperationExecutionError:
+      return "ErrorCode::kDownloadOperationExecutionError";
+    case ErrorCode::kDownloadOperationHashMismatch:
+      return "ErrorCode::kDownloadOperationHashMismatch";
+    case ErrorCode::kOmahaRequestEmptyResponseError:
+      return "ErrorCode::kOmahaRequestEmptyResponseError";
+    case ErrorCode::kOmahaRequestXMLParseError:
+      return "ErrorCode::kOmahaRequestXMLParseError";
+    case ErrorCode::kDownloadInvalidMetadataSize:
+      return "ErrorCode::kDownloadInvalidMetadataSize";
+    case ErrorCode::kDownloadInvalidMetadataSignature:
+      return "ErrorCode::kDownloadInvalidMetadataSignature";
+    case ErrorCode::kOmahaResponseInvalid:
+      return "ErrorCode::kOmahaResponseInvalid";
+    case ErrorCode::kOmahaUpdateIgnoredPerPolicy:
+      return "ErrorCode::kOmahaUpdateIgnoredPerPolicy";
+    case ErrorCode::kOmahaUpdateDeferredPerPolicy:
+      return "ErrorCode::kOmahaUpdateDeferredPerPolicy";
+    case ErrorCode::kOmahaErrorInHTTPResponse:
+      return "ErrorCode::kOmahaErrorInHTTPResponse";
+    case ErrorCode::kDownloadOperationHashMissingError:
+      return "ErrorCode::kDownloadOperationHashMissingError";
+    case ErrorCode::kDownloadMetadataSignatureMissingError:
+      return "ErrorCode::kDownloadMetadataSignatureMissingError";
+    case ErrorCode::kOmahaUpdateDeferredForBackoff:
+      return "ErrorCode::kOmahaUpdateDeferredForBackoff";
+    case ErrorCode::kPostinstallPowerwashError:
+      return "ErrorCode::kPostinstallPowerwashError";
+    case ErrorCode::kUpdateCanceledByChannelChange:
+      return "ErrorCode::kUpdateCanceledByChannelChange";
+    case ErrorCode::kUmaReportedMax:
+      return "ErrorCode::kUmaReportedMax";
+    case ErrorCode::kOmahaRequestHTTPResponseBase:
+      return "ErrorCode::kOmahaRequestHTTPResponseBase";
+    case ErrorCode::kResumedFlag:
+      return "Resumed";
+    case ErrorCode::kDevModeFlag:
+      return "DevMode";
+    case ErrorCode::kTestImageFlag:
+      return "TestImage";
+    case ErrorCode::kTestOmahaUrlFlag:
+      return "TestOmahaUrl";
+    case ErrorCode::kSpecialFlags:
+      return "ErrorCode::kSpecialFlags";
+    case ErrorCode::kPostinstallFirmwareRONotUpdatable:
+      return "ErrorCode::kPostinstallFirmwareRONotUpdatable";
+    case ErrorCode::kUnsupportedMajorPayloadVersion:
+      return "ErrorCode::kUnsupportedMajorPayloadVersion";
+    case ErrorCode::kUnsupportedMinorPayloadVersion:
+      return "ErrorCode::kUnsupportedMinorPayloadVersion";
+    case ErrorCode::kOmahaRequestXMLHasEntityDecl:
+      return "ErrorCode::kOmahaRequestXMLHasEntityDecl";
+    case ErrorCode::kFilesystemVerifierError:
+      return "ErrorCode::kFilesystemVerifierError";
+    case ErrorCode::kUserCanceled:
+      return "ErrorCode::kUserCanceled";
+    // Don't add a default case to let the compiler warn about newly added
+    // error codes which should be added here.
+  }
+
+  return "Unknown error: " + base::UintToString(static_cast<unsigned>(code));
+}
+
+}  // namespace utils
+}  // namespace chromeos_update_engine
diff --git a/common/error_code_utils.h b/common/error_code_utils.h
new file mode 100644
index 0000000..ae3958e
--- /dev/null
+++ b/common/error_code_utils.h
@@ -0,0 +1,34 @@
+//
+// Copyright (C) 2012 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+#ifndef UPDATE_ENGINE_COMMON_ERROR_CODE_UTILS_H_
+#define UPDATE_ENGINE_COMMON_ERROR_CODE_UTILS_H_
+
+#include <string>
+
+#include "update_engine/common/error_code.h"
+
+namespace chromeos_update_engine {
+namespace utils {
+
+// Returns a string representation of the ErrorCodes (either the base
+// error codes or the bit flags) for logging purposes.
+std::string ErrorCodeToString(ErrorCode code);
+
+}  // namespace utils
+}  // namespace chromeos_update_engine
+
+#endif  // UPDATE_ENGINE_COMMON_ERROR_CODE_UTILS_H_
diff --git a/common/utils.cc b/common/utils.cc
index a09b425..91dcfc8 100644
--- a/common/utils.cc
+++ b/common/utils.cc
@@ -993,132 +993,6 @@
   return base_code;
 }
 
-string CodeToString(ErrorCode code) {
-  // If the given code has both parts (i.e. the error code part and the flags
-  // part) then strip off the flags part since the switch statement below
-  // has case statements only for the base error code or a single flag but
-  // doesn't support any combinations of those.
-  if ((static_cast<int>(code) & static_cast<int>(ErrorCode::kSpecialFlags)) &&
-      (static_cast<int>(code) & ~static_cast<int>(ErrorCode::kSpecialFlags)))
-    code = static_cast<ErrorCode>(
-        static_cast<int>(code) & ~static_cast<int>(ErrorCode::kSpecialFlags));
-  switch (code) {
-    case ErrorCode::kSuccess: return "ErrorCode::kSuccess";
-    case ErrorCode::kError: return "ErrorCode::kError";
-    case ErrorCode::kOmahaRequestError: return "ErrorCode::kOmahaRequestError";
-    case ErrorCode::kOmahaResponseHandlerError:
-      return "ErrorCode::kOmahaResponseHandlerError";
-    case ErrorCode::kFilesystemCopierError:
-      return "ErrorCode::kFilesystemCopierError";
-    case ErrorCode::kPostinstallRunnerError:
-      return "ErrorCode::kPostinstallRunnerError";
-    case ErrorCode::kPayloadMismatchedType:
-      return "ErrorCode::kPayloadMismatchedType";
-    case ErrorCode::kInstallDeviceOpenError:
-      return "ErrorCode::kInstallDeviceOpenError";
-    case ErrorCode::kKernelDeviceOpenError:
-      return "ErrorCode::kKernelDeviceOpenError";
-    case ErrorCode::kDownloadTransferError:
-      return "ErrorCode::kDownloadTransferError";
-    case ErrorCode::kPayloadHashMismatchError:
-      return "ErrorCode::kPayloadHashMismatchError";
-    case ErrorCode::kPayloadSizeMismatchError:
-      return "ErrorCode::kPayloadSizeMismatchError";
-    case ErrorCode::kDownloadPayloadVerificationError:
-      return "ErrorCode::kDownloadPayloadVerificationError";
-    case ErrorCode::kDownloadNewPartitionInfoError:
-      return "ErrorCode::kDownloadNewPartitionInfoError";
-    case ErrorCode::kDownloadWriteError:
-      return "ErrorCode::kDownloadWriteError";
-    case ErrorCode::kNewRootfsVerificationError:
-      return "ErrorCode::kNewRootfsVerificationError";
-    case ErrorCode::kNewKernelVerificationError:
-      return "ErrorCode::kNewKernelVerificationError";
-    case ErrorCode::kSignedDeltaPayloadExpectedError:
-      return "ErrorCode::kSignedDeltaPayloadExpectedError";
-    case ErrorCode::kDownloadPayloadPubKeyVerificationError:
-      return "ErrorCode::kDownloadPayloadPubKeyVerificationError";
-    case ErrorCode::kPostinstallBootedFromFirmwareB:
-      return "ErrorCode::kPostinstallBootedFromFirmwareB";
-    case ErrorCode::kDownloadStateInitializationError:
-      return "ErrorCode::kDownloadStateInitializationError";
-    case ErrorCode::kDownloadInvalidMetadataMagicString:
-      return "ErrorCode::kDownloadInvalidMetadataMagicString";
-    case ErrorCode::kDownloadSignatureMissingInManifest:
-      return "ErrorCode::kDownloadSignatureMissingInManifest";
-    case ErrorCode::kDownloadManifestParseError:
-      return "ErrorCode::kDownloadManifestParseError";
-    case ErrorCode::kDownloadMetadataSignatureError:
-      return "ErrorCode::kDownloadMetadataSignatureError";
-    case ErrorCode::kDownloadMetadataSignatureVerificationError:
-      return "ErrorCode::kDownloadMetadataSignatureVerificationError";
-    case ErrorCode::kDownloadMetadataSignatureMismatch:
-      return "ErrorCode::kDownloadMetadataSignatureMismatch";
-    case ErrorCode::kDownloadOperationHashVerificationError:
-      return "ErrorCode::kDownloadOperationHashVerificationError";
-    case ErrorCode::kDownloadOperationExecutionError:
-      return "ErrorCode::kDownloadOperationExecutionError";
-    case ErrorCode::kDownloadOperationHashMismatch:
-      return "ErrorCode::kDownloadOperationHashMismatch";
-    case ErrorCode::kOmahaRequestEmptyResponseError:
-      return "ErrorCode::kOmahaRequestEmptyResponseError";
-    case ErrorCode::kOmahaRequestXMLParseError:
-      return "ErrorCode::kOmahaRequestXMLParseError";
-    case ErrorCode::kDownloadInvalidMetadataSize:
-      return "ErrorCode::kDownloadInvalidMetadataSize";
-    case ErrorCode::kDownloadInvalidMetadataSignature:
-      return "ErrorCode::kDownloadInvalidMetadataSignature";
-    case ErrorCode::kOmahaResponseInvalid:
-      return "ErrorCode::kOmahaResponseInvalid";
-    case ErrorCode::kOmahaUpdateIgnoredPerPolicy:
-      return "ErrorCode::kOmahaUpdateIgnoredPerPolicy";
-    case ErrorCode::kOmahaUpdateDeferredPerPolicy:
-      return "ErrorCode::kOmahaUpdateDeferredPerPolicy";
-    case ErrorCode::kOmahaErrorInHTTPResponse:
-      return "ErrorCode::kOmahaErrorInHTTPResponse";
-    case ErrorCode::kDownloadOperationHashMissingError:
-      return "ErrorCode::kDownloadOperationHashMissingError";
-    case ErrorCode::kDownloadMetadataSignatureMissingError:
-      return "ErrorCode::kDownloadMetadataSignatureMissingError";
-    case ErrorCode::kOmahaUpdateDeferredForBackoff:
-      return "ErrorCode::kOmahaUpdateDeferredForBackoff";
-    case ErrorCode::kPostinstallPowerwashError:
-      return "ErrorCode::kPostinstallPowerwashError";
-    case ErrorCode::kUpdateCanceledByChannelChange:
-      return "ErrorCode::kUpdateCanceledByChannelChange";
-    case ErrorCode::kUmaReportedMax:
-      return "ErrorCode::kUmaReportedMax";
-    case ErrorCode::kOmahaRequestHTTPResponseBase:
-      return "ErrorCode::kOmahaRequestHTTPResponseBase";
-    case ErrorCode::kResumedFlag:
-      return "Resumed";
-    case ErrorCode::kDevModeFlag:
-      return "DevMode";
-    case ErrorCode::kTestImageFlag:
-      return "TestImage";
-    case ErrorCode::kTestOmahaUrlFlag:
-      return "TestOmahaUrl";
-    case ErrorCode::kSpecialFlags:
-      return "ErrorCode::kSpecialFlags";
-    case ErrorCode::kPostinstallFirmwareRONotUpdatable:
-      return "ErrorCode::kPostinstallFirmwareRONotUpdatable";
-    case ErrorCode::kUnsupportedMajorPayloadVersion:
-      return "ErrorCode::kUnsupportedMajorPayloadVersion";
-    case ErrorCode::kUnsupportedMinorPayloadVersion:
-      return "ErrorCode::kUnsupportedMinorPayloadVersion";
-    case ErrorCode::kOmahaRequestXMLHasEntityDecl:
-      return "ErrorCode::kOmahaRequestXMLHasEntityDecl";
-    case ErrorCode::kFilesystemVerifierError:
-      return "ErrorCode::kFilesystemVerifierError";
-    case ErrorCode::kUserCanceled:
-      return "ErrorCode::kUserCanceled";
-    // Don't add a default case to let the compiler warn about newly added
-    // error codes which should be added here.
-  }
-
-  return "Unknown error: " + base::UintToString(static_cast<unsigned>(code));
-}
-
 bool CreatePowerwashMarkerFile(const char* file_path) {
   const char* marker_file = file_path ? file_path : kPowerwashMarkerFile;
   bool result = utils::WriteFile(marker_file,
diff --git a/common/utils.h b/common/utils.h
index ecb7fb9..5bf1422 100644
--- a/common/utils.h
+++ b/common/utils.h
@@ -299,10 +299,6 @@
 // it'll return the same value again.
 ErrorCode GetBaseErrorCode(ErrorCode code);
 
-// Returns a string representation of the ErrorCodes (either the base
-// error codes or the bit flags) for logging purposes.
-std::string CodeToString(ErrorCode code);
-
 // Creates the powerwash marker file with the appropriate commands in it.  Uses
 // |file_path| as the path to the marker file if non-null, otherwise uses the
 // global default. Returns true if successfully created.  False otherwise.