meta-bletchley: introduce bletchley-system-state-init

Because Bletchley does not have IPMI between Bmc & Host, the Host init
state will set to Off after Bmc booted. We require an extra service to
check and set Host state & Chassis power state to correct state before
doing any power action or power policy restore.

Signed-off-by: Potin Lai <potin.lai@quantatw.com>
Change-Id: I680bc62559ea6f7df6dbe171b54d2cda0c7ae0d4
diff --git a/meta-facebook/meta-bletchley/recipes-bletchley/plat-tools/files/bletchley-system-state-init b/meta-facebook/meta-bletchley/recipes-bletchley/plat-tools/files/bletchley-system-state-init
new file mode 100644
index 0000000..3da0e76
--- /dev/null
+++ b/meta-facebook/meta-bletchley/recipes-bletchley/plat-tools/files/bletchley-system-state-init
@@ -0,0 +1,41 @@
+#!/bin/bash
+
+DBUS_PROPERTIES_INTF_NAME="org.freedesktop.DBus.Properties"
+
+set_host_state()
+{
+    BUS_NAME="xyz.openbmc_project.State.Host$1"
+    OBJ_PATH="/xyz/openbmc_project/state/host$1"
+    INTF_NAME="xyz.openbmc_project.State.Host"
+    PROPERTY_NAME="CurrentHostState"
+    PROPERTY_VAL="xyz.openbmc_project.State.Host.HostState.$2"
+
+    busctl call "$BUS_NAME" "$OBJ_PATH" "$DBUS_PROPERTIES_INTF_NAME" Set ssv "$INTF_NAME" "$PROPERTY_NAME" s "$PROPERTY_VAL"
+}
+
+set_chassis_state()
+{
+    BUS_NAME="xyz.openbmc_project.State.Chassis$1"
+    OBJ_PATH="/xyz/openbmc_project/state/chassis$1"
+    INTF_NAME="xyz.openbmc_project.State.Chassis"
+    PROPERTY_NAME="CurrentPowerState"
+    PROPERTY_VAL="xyz.openbmc_project.State.Chassis.PowerState.$2"
+
+    busctl call "$BUS_NAME" "$OBJ_PATH" "$DBUS_PROPERTIES_INTF_NAME" Set ssv "$INTF_NAME" "$PROPERTY_NAME" s "$PROPERTY_VAL"
+}
+
+INSTENCE_ID="$1"
+HOST_PWR_STATUS=$(/usr/sbin/power-ctrl sled"$1" status)
+
+CHASSIS_STATE_VAL="Off"
+HOST_STATE_VAL="Off"
+
+if [ "$HOST_PWR_STATUS" != "AC Off" ]; then
+    CHASSIS_STATE_VAL="On"
+    if [ "$HOST_PWR_STATUS" != "Off" ]; then
+        HOST_STATE_VAL="Running"
+    fi
+fi
+
+set_chassis_state "$INSTENCE_ID" "$CHASSIS_STATE_VAL"
+set_host_state "$INSTENCE_ID" "$HOST_STATE_VAL"
\ No newline at end of file
diff --git a/meta-facebook/meta-bletchley/recipes-bletchley/plat-tools/files/bletchley-system-state-init@.service b/meta-facebook/meta-bletchley/recipes-bletchley/plat-tools/files/bletchley-system-state-init@.service
new file mode 100644
index 0000000..0c98209
--- /dev/null
+++ b/meta-facebook/meta-bletchley/recipes-bletchley/plat-tools/files/bletchley-system-state-init@.service
@@ -0,0 +1,17 @@
+[Unit]
+Description=Bletchley host state and chassis power state initialize for sled%i
+Wants=mapper-wait@-xyz-openbmc_project-state-chassis%i.service
+After=mapper-wait@-xyz-openbmc_project-state-chassis%i.service
+Wants=mapper-wait@-xyz-openbmc_project-state-host%i.service
+After=mapper-wait@-xyz-openbmc_project-state-host%i.service
+
+[Service]
+ExecStart=/usr/libexec/bletchley-system-state-init %i
+SyslogIdentifier=bletchley-system-state-init sled%i
+Type=oneshot
+RemainAfterExit=yes
+Restart=on-failure
+RestartSec=1
+
+[Install]
+WantedBy=multi-user.target