Move setting the priority U-Boot variable to ubi

The service that sets the priority value is unique to the ubi
layout because there is only one version at a time on the
static layout.
Move the calling of the service to the ubi implementation.

Tested:
- Witherspoon: Priorities are still set.
- Romulus: The BMC.Updater app does not core dump with the
latest sdbusplus changes due to calling a non-existent service.

Change-Id: Ica1c68f00d5cb43c51ee09c5a3851613edf941d5
Signed-off-by: Adriana Kobylak <anoo@us.ibm.com>
diff --git a/activation.cpp b/activation.cpp
index 863ea56..8fac93c 100644
--- a/activation.cpp
+++ b/activation.cpp
@@ -240,14 +240,14 @@
     // Set the priority value so that the freePriority() function can order
     // the versions by priority.
     auto newPriority = softwareServer::RedundancyPriority::priority(value);
-    storeToFile(parent.versionId, value);
+    parent.parent.savePriority(parent.versionId, value);
     parent.parent.freePriority(value, parent.versionId);
     return newPriority;
 }
 
 uint8_t RedundancyPriority::sdbusPriority(uint8_t value)
 {
-    storeToFile(parent.versionId, value);
+    parent.parent.savePriority(parent.versionId, value);
     return softwareServer::RedundancyPriority::priority(value);
 }
 
diff --git a/item_updater.cpp b/item_updater.cpp
index 1bbf200..22734ef 100644
--- a/item_updater.cpp
+++ b/item_updater.cpp
@@ -356,6 +356,12 @@
     return ItemUpdater::ActivationStatus::ready;
 }
 
+void ItemUpdater::savePriority(const std::string& versionId, uint8_t value)
+{
+    storeToFile(versionId, value);
+    helper.setEntry(versionId, value);
+}
+
 void ItemUpdater::freePriority(uint8_t value, const std::string& versionId)
 {
     std::map<std::string, uint8_t> priorityMap;
diff --git a/item_updater.hpp b/item_updater.hpp
index 330ec82..ec7aee0 100644
--- a/item_updater.hpp
+++ b/item_updater.hpp
@@ -61,6 +61,15 @@
         emit_object_added();
     };
 
+    /** @brief Save priority value to persistent storage (flash and optionally
+     *  a U-Boot environment variable)
+     *
+     *  @param[in] versionId - The Id of the version
+     *  @param[in] value - The priority value
+     *  @return None
+     */
+    void savePriority(const std::string& versionId, uint8_t value);
+
     /** @brief Sets the given priority free by incrementing
      *  any existing priority with the same value by 1
      *
diff --git a/item_updater_helper.hpp b/item_updater_helper.hpp
index 33b3cdb..a2d90b8 100644
--- a/item_updater_helper.hpp
+++ b/item_updater_helper.hpp
@@ -28,6 +28,13 @@
         // Empty
     }
 
+    /** @brief Set an environment variable to the specified value
+     *
+     * @param[in] entryId - The variable name
+     * @param[in] value - The variable value
+     */
+    void setEntry(const std::string& entryId, uint8_t value);
+
     /** @brief Clear an image with the entry id
      *
      * @param[in] entryId - The image entry id
diff --git a/serialize.cpp b/serialize.cpp
index b59ff46..722b71b 100644
--- a/serialize.cpp
+++ b/serialize.cpp
@@ -16,8 +16,6 @@
 
 void storeToFile(std::string versionId, uint8_t priority)
 {
-    auto bus = sdbusplus::bus::new_default();
-
     if (!fs::is_directory(PERSIST_DIR))
     {
         fs::create_directories(PERSIST_DIR);
@@ -27,13 +25,6 @@
     std::ofstream os(path.c_str());
     cereal::JSONOutputArchive oarchive(os);
     oarchive(cereal::make_nvp("priority", priority));
-
-    std::string serviceFile = "obmc-flash-bmc-setenv@" + versionId + "\\x3d" +
-                              std::to_string(priority) + ".service";
-    auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
-                                      SYSTEMD_INTERFACE, "StartUnit");
-    method.append(serviceFile, "replace");
-    bus.call_noreply(method);
 }
 
 bool restoreFromFile(std::string versionId, uint8_t& priority)
diff --git a/static/item_updater_helper.cpp b/static/item_updater_helper.cpp
index 5cd8b9e..e639e63 100644
--- a/static/item_updater_helper.cpp
+++ b/static/item_updater_helper.cpp
@@ -7,6 +7,11 @@
 namespace updater
 {
 
+void Helper::setEntry(const std::string& entryId, uint8_t value)
+{
+    // Empty
+}
+
 void Helper::clearEntry(const std::string& entryId)
 {
     // Empty
diff --git a/ubi/item_updater_helper.cpp b/ubi/item_updater_helper.cpp
index 6704c21..b05291e 100644
--- a/ubi/item_updater_helper.cpp
+++ b/ubi/item_updater_helper.cpp
@@ -11,6 +11,17 @@
 {
 
 using namespace phosphor::logging;
+
+void Helper::setEntry(const std::string& entryId, uint8_t value)
+{
+    std::string serviceFile = "obmc-flash-bmc-setenv@" + entryId + "\\x3d" +
+                              std::to_string(value) + ".service";
+    auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
+                                      SYSTEMD_INTERFACE, "StartUnit");
+    method.append(serviceFile, "replace");
+    bus.call_noreply(method);
+}
+
 void Helper::clearEntry(const std::string& entryId)
 {
     // Remove the priority environment variable.