pw_software_update: fixup update backend API

Renames the backend header to match the object name.

Adjusts the FinalizeUpdate() API to use Status again to report
finalize failures before the reboot.

Also changes ApplyTargetFile() to be pure virtual.

Changes the backend API to use the Manifest object instead of the
nanopb struct for the manifest.

Moves the Manifest definition to a separate header.

Adds missing pw_protobuf dependency.

Change-Id: I295db6021b26d8ba96512f4627e39b5a76d92815
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/61820
Commit-Queue: Ewout van Bekkum <ewout@google.com>
Reviewed-by: David Rogers <davidrogers@google.com>
Reviewed-by: Keir Mierle <keir@google.com>
diff --git a/pw_software_update/BUILD.bazel b/pw_software_update/BUILD.bazel
index 5ae1d87..5dcd665 100644
--- a/pw_software_update/BUILD.bazel
+++ b/pw_software_update/BUILD.bazel
@@ -36,7 +36,9 @@
     name = "update_bundle",
     srcs = ["update_bundle.cc"],
     hdrs = [
+        "public/pw_software_update/bundled_update_backend.h",
         "public/pw_software_update/config.h",
+        "public/pw_software_update/manifest.h",
         "public/pw_software_update/update_bundle.h",
     ],
     includes = ["public"],
@@ -44,16 +46,9 @@
         "//pw_blob_store",
         "//pw_kvs",
         "//pw_log",
-    ],
-)
-
-pw_cc_library(
-    name = "update_backend",
-    hdrs = ["public/pw_software_update/update_backend.h"],
-    includes = ["public"],
-    deps = [
-        ":update_bundle_proto",
+        "//pw_protobuf",
         "//pw_status",
+        "//pw_stream",
     ],
 )
 
diff --git a/pw_software_update/BUILD.gn b/pw_software_update/BUILD.gn
index 7a8a420..63a7f9b 100644
--- a/pw_software_update/BUILD.gn
+++ b/pw_software_update/BUILD.gn
@@ -56,6 +56,28 @@
   sources = [ "docs.rst" ]
 }
 
+pw_source_set("update_bundle") {
+  public_configs = [ ":public_include_path" ]
+  public_deps = [
+    dir_pw_blob_store,
+    dir_pw_kvs,
+    dir_pw_protobuf,
+    dir_pw_status,
+    dir_pw_stream,
+  ]
+  public = [
+    "public/pw_software_update/bundled_update_backend.h",
+    "public/pw_software_update/config.h",
+    "public/pw_software_update/manifest.h",
+    "public/pw_software_update/update_bundle.h",
+  ]
+  deps = [
+    ":protos.pwpb",
+    dir_pw_log,
+  ]
+  sources = [ "update_bundle.cc" ]
+}
+
 if (dir_pw_third_party_nanopb != "" && dir_pw_third_party_protobuf != "") {
   pw_source_set("rpc_service") {
     public_configs = [ ":public_include_path" ]
@@ -66,36 +88,9 @@
       "service.cc",
     ]
   }
-
-  pw_source_set("update_backend") {
-    public_configs = [ ":public_include_path" ]
-    public_deps = [
-      ":protos.nanopb",
-      dir_pw_status,
-    ]
-    sources = [ "public/pw_software_update/update_backend.h" ]
-  }
-
-  pw_source_set("update_bundle") {
-    public_configs = [ ":public_include_path" ]
-    public_deps = [
-      ":protos.pwpb",
-      ":update_backend",
-      dir_pw_blob_store,
-      dir_pw_kvs,
-    ]
-    deps = [ dir_pw_log ]
-    public = [
-      "public/pw_software_update/config.h",
-      "public/pw_software_update/update_bundle.h",
-    ]
-    sources = [ "update_bundle.cc" ]
-  }
 } else {
   group("rpc_service") {
   }
-  group("update_bundle") {
-  }
 }
 
 pw_python_action("generate_test_bundle") {
diff --git a/pw_software_update/public/pw_software_update/update_backend.h b/pw_software_update/public/pw_software_update/bundled_update_backend.h
similarity index 88%
rename from pw_software_update/public/pw_software_update/update_backend.h
rename to pw_software_update/public/pw_software_update/bundled_update_backend.h
index c2aaf90..2a93455 100644
--- a/pw_software_update/public/pw_software_update/update_backend.h
+++ b/pw_software_update/public/pw_software_update/bundled_update_backend.h
@@ -14,8 +14,9 @@
 
 #pragma once
 
-#include "pw_software_update/update_bundle.pb.h"
+#include "pw_software_update/manifest.h"
 #include "pw_status/status.h"
+#include "pw_stream/stream.h"
 
 namespace pw::software_update {
 
@@ -37,8 +38,7 @@
 
   // Perform any product-specific bundle verification tasks (e.g. hw version
   // match check), done after TUF bundle verification process.
-  virtual Status VerifyMetadata(
-      [[maybe_unused]] const pw_software_update_Manifest& manifest) {
+  virtual Status VerifyMetadata([[maybe_unused]] const Manifest& manifest) {
     return OkStatus();
   };
 
@@ -50,7 +50,7 @@
   // (e.g. by checksum, if failed abort partial update and wipe/mark-invalid
   // running manifest)
   virtual Status VerifyTargetFile(
-      [[maybe_unused]] const pw_software_update_Manifest& manifest,
+      [[maybe_unused]] const Manifest& manifest,
       [[maybe_unused]] std::string_view target_file_name) {
     return OkStatus();
   };
@@ -64,18 +64,18 @@
   virtual int64_t GetStatus() { return 0; }
 
   // Update the specific target file on the device.
-  virtual Status ApplyTargetFile(
-      [[maybe_unused]] std::string_view target_file_name,
-      [[maybe_unused]] stream::Reader& target_payload) {
-    return OkStatus();
-  };
+  virtual Status ApplyTargetFile(std::string_view target_file_name,
+                                 stream::Reader& target_payload) = 0;
 
   // Do any work needed to finalize the update including doing a required
   // reboot of the device! This is called after all software update state and
   // breadcrumbs have been cleaned up.
   //
   // After the reboot the update is fully complete.
-  PW_NO_RETURN virtual void FinalizeUpdate() = 0;
+  //
+  // NOTE: If successful this method does not return and reboots the device, it
+  // only returns on failure to finalize.
+  virtual Status FinalizeUpdate() = 0;
 
   // Get reader of the device's current manifest.
   virtual Status GetCurrentManifestReader(
diff --git a/pw_software_update/public/pw_software_update/manifest.h b/pw_software_update/public/pw_software_update/manifest.h
new file mode 100644
index 0000000..3b9cb18
--- /dev/null
+++ b/pw_software_update/public/pw_software_update/manifest.h
@@ -0,0 +1,23 @@
+// Copyright 2021 The Pigweed Authors
+//
+// 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
+//
+//     https://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.
+
+#pragma once
+
+namespace pw::software_update {
+
+// TODO(pwbug/456): Place-holder declaration for now. To be implemented
+// and moved elsewhere.
+class Manifest {};
+
+}  // namespace pw::software_update
diff --git a/pw_software_update/public/pw_software_update/update_bundle.h b/pw_software_update/public/pw_software_update/update_bundle.h
index 17fff95..b9f85a0 100644
--- a/pw_software_update/public/pw_software_update/update_bundle.h
+++ b/pw_software_update/public/pw_software_update/update_bundle.h
@@ -19,21 +19,18 @@
 #include "pw_blob_store/blob_store.h"
 #include "pw_protobuf/map_utils.h"
 #include "pw_protobuf/message.h"
-#include "pw_software_update/update_backend.h"
+#include "pw_software_update/bundled_update_backend.h"
+#include "pw_software_update/manifest.h"
 #include "pw_stream/memory_stream.h"
 
 namespace pw::software_update {
 
-// TODO(pwbug/456): Place-holder declaration for now. To be implemented
-// and moved elsewhere.
-class Manifest {};
-
 // UpdateBundle is responsible for parsing, verifying and providing
 // target payload access of a software update bundle. It takes the following as
 // inputs:
 //
 // 1. A software update bundle via `BlobStore`.
-// 2. A `BundledUpdateHelper`, which implements project-specific update
+// 2. A `BundledUpdateBackend`, which implements project-specific update
 //    operations such as enforcing project update policies and
 //    verifying/applying target files on device.
 //
@@ -127,4 +124,4 @@
   protobuf::Message decoder_;
 };
 
-}  // namespace pw::software_update
\ No newline at end of file
+}  // namespace pw::software_update