Add powerOff utility function
Logs an error so the shutdown reason is known and then
does a hard power off.
Change-Id: I52e2d07668a777c0aacf5c269f2632fbd6f15246
Signed-off-by: Matt Spinler <spinler@us.ibm.com>
diff --git a/utility.cpp b/utility.cpp
index 8692518..25e401d 100644
--- a/utility.cpp
+++ b/utility.cpp
@@ -13,6 +13,8 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
+#include <xyz/openbmc_project/Power/Fault/error.hpp>
+#include "elog-errors.hpp"
#include "utility.hpp"
namespace witherspoon
@@ -25,8 +27,14 @@
constexpr auto MAPPER_BUSNAME = "xyz.openbmc_project.ObjectMapper";
constexpr auto MAPPER_PATH = "/xyz/openbmc_project/object_mapper";
constexpr auto MAPPER_INTERFACE = "xyz.openbmc_project.ObjectMapper";
+constexpr auto SYSTEMD_SERVICE = "org.freedesktop.systemd1";
+constexpr auto SYSTEMD_ROOT = "/org/freedesktop/systemd1";
+constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager";
+constexpr auto POWEROFF_TARGET = "obmc-chassis-hard-poweroff@0.target";
using namespace phosphor::logging;
+using namespace sdbusplus::xyz::openbmc_project::Power::Fault::Error;
+
std::string getService(const std::string& path,
const std::string& interface,
@@ -66,6 +74,24 @@
return response.begin()->first;
}
+
+void powerOff(sdbusplus::bus::bus& bus)
+{
+ log<level::INFO>("Powering off due to a power fault");
+ report<Shutdown>();
+
+ auto method = bus.new_method_call(SYSTEMD_SERVICE,
+ SYSTEMD_ROOT,
+ SYSTEMD_INTERFACE,
+ "StartUnit");
+
+ method.append(POWEROFF_TARGET);
+ method.append("replace");
+
+ bus.call_noreply(method);
+}
+
+
}
}
}
diff --git a/utility.hpp b/utility.hpp
index a4df0cb..fdae252 100644
--- a/utility.hpp
+++ b/utility.hpp
@@ -70,6 +70,14 @@
value = sdbusplus::message::variant_ns::get<T>(property);
}
+/**
+ * Powers off the system and logs an error
+ * saying it was due to a power fault.
+ *
+ * @param[in] bus - D-Bus object
+ */
+void powerOff(sdbusplus::bus::bus& bus);
+
}
}
}