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/bletchley-common-tool_0.1.bb b/meta-facebook/meta-bletchley/recipes-bletchley/plat-tools/bletchley-common-tool_0.1.bb
new file mode 100644
index 0000000..170463f
--- /dev/null
+++ b/meta-facebook/meta-bletchley/recipes-bletchley/plat-tools/bletchley-common-tool_0.1.bb
@@ -0,0 +1,23 @@
+LICENSE = "Apache-2.0"
+LIC_FILES_CHKSUM = "file://${COREBASE}/meta/files/common-licenses/Apache-2.0;md5=89aea4e17d99a7cacdbeed46a0096b10"
+
+inherit obmc-phosphor-systemd
+
+RDEPENDS:${PN} += " bash motor-ctrl"
+
+SRC_URI += " \
+    file://bletchley-system-state-init \
+    file://bletchley-system-state-init@.service \
+    "
+
+do_install() {
+    install -d ${D}${libexecdir}
+    install -m 0755 ${WORKDIR}/bletchley-system-state-init ${D}${libexecdir}
+}
+
+
+TGT = "${SYSTEMD_DEFAULT_TARGET}"
+BLETCHLEY_SYS_ST_INIT_INSTFMT="../bletchley-system-state-init@.service:${TGT}.wants/bletchley-system-state-init@{0}.service"
+
+SYSTEMD_SERVICE:${PN} += "bletchley-system-state-init@.service"
+SYSTEMD_LINK:${PN} += "${@compose_list(d, 'BLETCHLEY_SYS_ST_INIT_INSTFMT', 'OBMC_HOST_INSTANCES')}"
\ No newline at end of file
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
diff --git a/meta-facebook/meta-bletchley/recipes-phosphor/state/phosphor-state-manager/bletchley-system-state-init.conf b/meta-facebook/meta-bletchley/recipes-phosphor/state/phosphor-state-manager/bletchley-system-state-init.conf
new file mode 100644
index 0000000..5008bfa
--- /dev/null
+++ b/meta-facebook/meta-bletchley/recipes-phosphor/state/phosphor-state-manager/bletchley-system-state-init.conf
@@ -0,0 +1,3 @@
+[Unit]
+Requires=bletchley-system-state-init@%i.service
+After=bletchley-system-state-init@%i.service
diff --git a/meta-facebook/meta-bletchley/recipes-phosphor/state/phosphor-state-manager_%.bbappend b/meta-facebook/meta-bletchley/recipes-phosphor/state/phosphor-state-manager_%.bbappend
new file mode 100644
index 0000000..2b533e1
--- /dev/null
+++ b/meta-facebook/meta-bletchley/recipes-phosphor/state/phosphor-state-manager_%.bbappend
@@ -0,0 +1,10 @@
+FILESEXTRAPATHS:prepend := "${THISDIR}/${BPN}:"
+RDEPENDS:${PN}-discover:append = " bletchley-common-tool"
+
+# 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.
+
+BLETCHLEY_SYS_ST_INIT_CONF_FMT = "bletchley-system-state-init.conf:phosphor-discover-system-state@{0}.service.d/bletchley-system-state-init.conf"
+SYSTEMD_OVERRIDE:${PN}-discover:bletchley += "${@compose_list_zip(d, 'BLETCHLEY_SYS_ST_INIT_CONF_FMT', 'OBMC_HOST_INSTANCES')}"