side-switch: set power restore policy to always-on

Utilize the one-time feature of the automatic power restore
functionality to ensure a one-time automatic boot of the system occurs
after the BMC reboot.

This makes the whole process seamless to the user. They start a boot,
this function detects a bmc reboot is needed to pick up the priority 0
image, and this ensures the boot starts back up after the bmc reboot.

Tested:
- Verify policy set correctly
<6> Checking for side switch reboot
<6> Running firmware version path is /xyz/openbmc_project/software/74575136
<6> Running firmware version priority is 1
<6> Running image is at priority 1, execute side switch
<6> chassis power is off
<6> RestorePolicy set to AlwaysOn

busctl get-property xyz.openbmc_project.Settings
  /xyz/openbmc_project/control/host0/power_restore_policy/one_time
  xyz.openbmc_project.Control.Power.RestorePolicy PowerRestorePolicy
s "xyz.openbmc_project.Control.Power.RestorePolicy.Policy.AlwaysOn"

Signed-off-by: Andrew Geissler <geissonator@yahoo.com>
Change-Id: I126283386c577fd9ec899a1402ae36e6111f1780
diff --git a/side-switch/side_switch.cpp b/side-switch/side_switch.cpp
index 4c13ed0..16fe6a3 100644
--- a/side-switch/side_switch.cpp
+++ b/side-switch/side_switch.cpp
@@ -172,6 +172,30 @@
     return (false);
 }
 
+bool setAutoPowerRestart(sdbusplus::bus::bus& bus)
+{
+    try
+    {
+        // Set the one-time power on policy to AlwaysOn so system auto boots
+        // after BMC reboot
+        utils::PropertyValue restorePolicyOn =
+            "xyz.openbmc_project.Control.Power.RestorePolicy.Policy.AlwaysOn";
+
+        utils::setProperty(
+            bus,
+            "/xyz/openbmc_project/control/host0/power_restore_policy/one_time",
+            "xyz.openbmc_project.Control.Power.RestorePolicy",
+            "PowerRestorePolicy", restorePolicyOn);
+    }
+    catch (const std::exception& e)
+    {
+        error("setting power policy to always on failed: {ERROR}", "ERROR", e);
+        return (false);
+    }
+    info("RestorePolicy set to AlwaysOn");
+    return (true);
+}
+
 int main()
 {
     info("Checking for side switch reboot");
@@ -190,5 +214,12 @@
         return 0;
     }
 
+    if (!setAutoPowerRestart(bus))
+    {
+        error("unable to set the auto power on restart policy");
+        // system has been powered off, best to at least continue and
+        // switch to new firmware image so continue
+    }
+
     // TODO - Future commits in series to fill in rest of logic
 }
diff --git a/side-switch/side_switch.hpp b/side-switch/side_switch.hpp
index 4af52cb..1ffd4d9 100644
--- a/side-switch/side_switch.hpp
+++ b/side-switch/side_switch.hpp
@@ -15,3 +15,10 @@
  *  @return True if chassis off success, false otherwise
  */
 bool powerOffSystem(sdbusplus::bus::bus& bus);
+
+/** @brief Configure BMC to auto power on the host after reboot
+ *
+ *  @param[in] bus       - The Dbus bus object
+ *  @return True if policy set correctly, false otherwise
+ */
+bool setAutoPowerRestart(sdbusplus::bus::bus& bus);