item_updater: sort ubi specific code

Add item_updater_helper and implement it in ubi and static layouts.

Tested: Build this repo on both Romulus and Witherspoon OK;
        Tested on Romulus and Witherspoon that code update works fine.

Change-Id: I706cfe63900f89bb41672dcc745b3483e06838c7
Signed-off-by: Lei YU <mine260309@gmail.com>
diff --git a/Makefile.am b/Makefile.am
index 1ac3914..18c8763 100755
--- a/Makefile.am
+++ b/Makefile.am
@@ -8,7 +8,9 @@
 	images.hpp \
 	image_manager.hpp \
 	item_updater.hpp \
-	activation.hpp
+	activation.hpp \
+	flash.hpp \
+	item_updater_helper.hpp
 
 sbin_PROGRAMS = \
 	phosphor-version-software-manager \
diff --git a/item_updater.cpp b/item_updater.cpp
index 51523d4..1bbf200 100644
--- a/item_updater.cpp
+++ b/item_updater.cpp
@@ -292,12 +292,7 @@
                         entry("VERSIONID=%s", entryId.c_str()));
     }
 
-    // Remove the priority environment variable.
-    auto serviceFile = "obmc-flash-bmc-setenv@" + entryId + ".service";
-    auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
-                                      SYSTEMD_INTERFACE, "StartUnit");
-    method.append(serviceFile, "replace");
-    bus.call_noreply(method);
+    helper.clearEntry(entryId);
 
     // Removing entry in activations map
     auto ita = activations.find(entryId);
@@ -332,11 +327,7 @@
         ItemUpdater::erase(deletableIt);
     }
 
-    // Remove any volumes that do not match current versions.
-    auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
-                                      SYSTEMD_INTERFACE, "StartUnit");
-    method.append("obmc-flash-bmc-cleanup.service", "replace");
-    bus.call_noreply(method);
+    helper.cleanup();
 }
 
 ItemUpdater::ActivationStatus
@@ -421,26 +412,14 @@
 
 void ItemUpdater::reset()
 {
-    // Mark the read-write partition for recreation upon reboot.
-    auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
-                                      SYSTEMD_INTERFACE, "StartUnit");
-    method.append("obmc-flash-bmc-setenv@rwreset\\x3dtrue.service", "replace");
-    bus.call_noreply(method);
+    helper.factoryReset();
 
     log<level::INFO>("BMC factory reset will take effect upon reboot.");
-
-    return;
 }
 
 void ItemUpdater::removeReadOnlyPartition(std::string versionId)
 {
-    auto serviceFile = "obmc-flash-bmc-ubiro-remove@" + versionId + ".service";
-
-    // Remove the read-only partitions.
-    auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
-                                      SYSTEMD_INTERFACE, "StartUnit");
-    method.append(serviceFile, "replace");
-    bus.call_noreply(method);
+    helper.removeVersion(versionId);
 }
 
 bool ItemUpdater::fieldModeEnabled(bool value)
@@ -450,23 +429,7 @@
     {
         control::FieldMode::fieldModeEnabled(value);
 
-        auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
-                                          SYSTEMD_INTERFACE, "StartUnit");
-        method.append("obmc-flash-bmc-setenv@fieldmode\\x3dtrue.service",
-                      "replace");
-        bus.call_noreply(method);
-
-        method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
-                                     SYSTEMD_INTERFACE, "StopUnit");
-        method.append("usr-local.mount", "replace");
-        bus.call_noreply(method);
-
-        std::vector<std::string> usrLocal = {"usr-local.mount"};
-
-        method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
-                                     SYSTEMD_INTERFACE, "MaskUnitFiles");
-        method.append(usrLocal, false, true);
-        bus.call_noreply(method);
+        helper.enableFieldMode();
     }
 
     return control::FieldMode::fieldModeEnabled();
@@ -564,19 +527,7 @@
 
 void ItemUpdater::updateUbootEnvVars(const std::string& versionId)
 {
-    auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
-                                      SYSTEMD_INTERFACE, "StartUnit");
-    auto updateEnvVarsFile =
-        "obmc-flash-bmc-updateubootvars@" + versionId + ".service";
-    method.append(updateEnvVarsFile, "replace");
-    auto result = bus.call(method);
-
-    // Check that the bus call didn't result in an error
-    if (result.is_method_error())
-    {
-        log<level::ERR>("Failed to update u-boot env variables",
-                        entry("VERSIONID=%s", versionId.c_str()));
-    }
+    helper.updateUbootVersionId(versionId);
 }
 
 void ItemUpdater::resetUbootEnvVars()
@@ -644,17 +595,7 @@
 
 void ItemUpdater::mirrorUbootToAlt()
 {
-    auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
-                                      SYSTEMD_INTERFACE, "StartUnit");
-    auto mirrorUbootFile = "obmc-flash-bmc-mirroruboot.service";
-    method.append(mirrorUbootFile, "replace");
-    auto result = bus.call(method);
-
-    // Check that the bus call didn't result in an error
-    if (result.is_method_error())
-    {
-        log<level::ERR>("Failed to copy U-Boot to alternate chip");
-    }
+    helper.mirrorAlt();
 }
 
 } // namespace updater
diff --git a/item_updater.hpp b/item_updater.hpp
index 9fda860..330ec82 100644
--- a/item_updater.hpp
+++ b/item_updater.hpp
@@ -2,6 +2,7 @@
 
 #include <sdbusplus/server.hpp>
 #include "activation.hpp"
+#include "item_updater_helper.hpp"
 #include "version.hpp"
 #include <xyz/openbmc_project/Common/FactoryReset/server.hpp>
 #include <xyz/openbmc_project/Control/FieldMode/server.hpp>
@@ -47,7 +48,7 @@
      * @param[in] bus    - The D-Bus bus object
      */
     ItemUpdater(sdbusplus::bus::bus& bus, const std::string& path) :
-        ItemUpdaterInherit(bus, path.c_str(), false), bus(bus),
+        ItemUpdaterInherit(bus, path.c_str(), false), bus(bus), helper(bus),
         versionMatch(bus,
                      MatchRules::interfacesAdded() +
                          MatchRules::path("/xyz/openbmc_project/software"),
@@ -188,6 +189,9 @@
     /** @brief Persistent sdbusplus D-Bus bus connection. */
     sdbusplus::bus::bus& bus;
 
+    /** @brief The helper of image updater. */
+    Helper helper;
+
     /** @brief Persistent map of Activation D-Bus objects and their
      * version id */
     std::map<std::string, std::unique_ptr<Activation>> activations;
diff --git a/item_updater_helper.hpp b/item_updater_helper.hpp
new file mode 100644
index 0000000..33b3cdb
--- /dev/null
+++ b/item_updater_helper.hpp
@@ -0,0 +1,68 @@
+#pragma once
+
+#include <string>
+#include <sdbusplus/bus.hpp>
+
+namespace phosphor
+{
+namespace software
+{
+namespace updater
+{
+
+class Helper
+{
+  public:
+    Helper() = delete;
+    Helper(const Helper&) = delete;
+    Helper& operator=(const Helper&) = delete;
+    Helper(Helper&&) = default;
+    Helper& operator=(Helper&&) = default;
+
+    /** @brief Constructor
+     *
+     *  @param[in] bus - sdbusplus D-Bus bus connection
+     */
+    Helper(sdbusplus::bus::bus& bus) : bus(bus)
+    {
+        // Empty
+    }
+
+    /** @brief Clear an image with the entry id
+     *
+     * @param[in] entryId - The image entry id
+     */
+    void clearEntry(const std::string& entryId);
+
+    /** @brief Clean up all the unused images */
+    void cleanup();
+
+    /** @brief Do factory reset */
+    void factoryReset();
+
+    /** @brief Remove the image with the version id
+     *
+     * @param[in] versionId - The version id of the image
+     */
+    void removeVersion(const std::string& versionId);
+
+    /** @brief Update version id in uboot env
+     *
+     * @param[in] versionId - The version id of the image
+     */
+    void updateUbootVersionId(const std::string& versionId);
+
+    /** @brief Enable field mode */
+    void enableFieldMode();
+
+    /** @brief Mirror Uboot to the alt uboot partition */
+    void mirrorAlt();
+
+  private:
+    /** @brief Persistent sdbusplus D-Bus bus connection. */
+    sdbusplus::bus::bus& bus;
+};
+
+} // namespace updater
+} // namespace software
+} // namespace phosphor
diff --git a/static/Makefile.am.include b/static/Makefile.am.include
index 7bcfcb2..1a5128a 100644
--- a/static/Makefile.am.include
+++ b/static/Makefile.am.include
@@ -1,2 +1,3 @@
 phosphor_image_updater_SOURCES += \
-	%reldir%/flash.cpp
+	%reldir%/flash.cpp \
+	%reldir%/item_updater_helper.cpp
diff --git a/static/item_updater_helper.cpp b/static/item_updater_helper.cpp
new file mode 100644
index 0000000..5cd8b9e
--- /dev/null
+++ b/static/item_updater_helper.cpp
@@ -0,0 +1,47 @@
+#include "item_updater_helper.hpp"
+
+namespace phosphor
+{
+namespace software
+{
+namespace updater
+{
+
+void Helper::clearEntry(const std::string& entryId)
+{
+    // Empty
+}
+
+void Helper::cleanup()
+{
+    // Empty
+}
+
+void Helper::factoryReset()
+{
+    // TODO
+}
+
+void Helper::removeVersion(const std::string& versionId)
+{
+    // Empty
+}
+
+void Helper::updateUbootVersionId(const std::string& versionId)
+{
+    // Empty
+}
+
+void Helper::enableFieldMode()
+{
+    // TODO
+}
+
+void Helper::mirrorAlt()
+{
+    // Empty
+}
+
+} // namespace updater
+} // namespace software
+} // namespace phosphor
diff --git a/ubi/Makefile.am.include b/ubi/Makefile.am.include
index 7bcfcb2..1a5128a 100644
--- a/ubi/Makefile.am.include
+++ b/ubi/Makefile.am.include
@@ -1,2 +1,3 @@
 phosphor_image_updater_SOURCES += \
-	%reldir%/flash.cpp
+	%reldir%/flash.cpp \
+	%reldir%/item_updater_helper.cpp
diff --git a/ubi/item_updater_helper.cpp b/ubi/item_updater_helper.cpp
new file mode 100644
index 0000000..6704c21
--- /dev/null
+++ b/ubi/item_updater_helper.cpp
@@ -0,0 +1,107 @@
+#include <phosphor-logging/log.hpp>
+
+#include "config.h"
+#include "item_updater_helper.hpp"
+
+namespace phosphor
+{
+namespace software
+{
+namespace updater
+{
+
+using namespace phosphor::logging;
+void Helper::clearEntry(const std::string& entryId)
+{
+    // Remove the priority environment variable.
+    auto serviceFile = "obmc-flash-bmc-setenv@" + entryId + ".service";
+    auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
+                                      SYSTEMD_INTERFACE, "StartUnit");
+    method.append(serviceFile, "replace");
+    bus.call_noreply(method);
+}
+
+void Helper::cleanup()
+{
+    // Remove any volumes that do not match current versions.
+    auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
+                                      SYSTEMD_INTERFACE, "StartUnit");
+    method.append("obmc-flash-bmc-cleanup.service", "replace");
+    bus.call_noreply(method);
+}
+
+void Helper::factoryReset()
+{
+    // Mark the read-write partition for recreation upon reboot.
+    auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
+                                      SYSTEMD_INTERFACE, "StartUnit");
+    method.append("obmc-flash-bmc-setenv@rwreset\\x3dtrue.service", "replace");
+    bus.call_noreply(method);
+}
+
+void Helper::removeVersion(const std::string& versionId)
+{
+    auto serviceFile = "obmc-flash-bmc-ubiro-remove@" + versionId + ".service";
+
+    // Remove the read-only partitions.
+    auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
+                                      SYSTEMD_INTERFACE, "StartUnit");
+    method.append(serviceFile, "replace");
+    bus.call_noreply(method);
+}
+
+void Helper::updateUbootVersionId(const std::string& versionId)
+{
+    auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
+                                      SYSTEMD_INTERFACE, "StartUnit");
+    auto updateEnvVarsFile =
+        "obmc-flash-bmc-updateubootvars@" + versionId + ".service";
+    method.append(updateEnvVarsFile, "replace");
+    auto result = bus.call(method);
+
+    // Check that the bus call didn't result in an error
+    if (result.is_method_error())
+    {
+        log<level::ERR>("Failed to update u-boot env variables",
+                        entry("VERSIONID=%s", versionId.c_str()));
+    }
+}
+
+void Helper::enableFieldMode()
+{
+    auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
+                                      SYSTEMD_INTERFACE, "StartUnit");
+    method.append("obmc-flash-bmc-setenv@fieldmode\\x3dtrue.service",
+                  "replace");
+    bus.call_noreply(method);
+
+    method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
+                                 SYSTEMD_INTERFACE, "StopUnit");
+    method.append("usr-local.mount", "replace");
+    bus.call_noreply(method);
+
+    std::vector<std::string> usrLocal = {"usr-local.mount"};
+
+    method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
+                                 SYSTEMD_INTERFACE, "MaskUnitFiles");
+    method.append(usrLocal, false, true);
+    bus.call_noreply(method);
+}
+void Helper::mirrorAlt()
+{
+    auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
+                                      SYSTEMD_INTERFACE, "StartUnit");
+    auto mirrorUbootFile = "obmc-flash-bmc-mirroruboot.service";
+    method.append(mirrorUbootFile, "replace");
+    auto result = bus.call(method);
+
+    // Check that the bus call didn't result in an error
+    if (result.is_method_error())
+    {
+        log<level::ERR>("Failed to copy U-Boot to alternate chip");
+    }
+}
+
+} // namespace updater
+} // namespace software
+} // namespace phosphor