meta-facebook: yosemitev2 : Add support to take actions if
Fan/NIC sensor values crossed thresholds

Added support to trigger hard shutdown alarms and power off the
slots of the platform if the Fan/NIC sensors values getting low
compared to threshold values.

Added platform specific service file and script to take
platorm specific actions.

TESTED : Verified all the slots getting powered Off when fan/nic
sensor
values crossed threshold values.

Signed-off-by: Kumar Thangavel <thangavel.k@hcl.com>
Change-Id: I6deb6707e574a375fac8f2379f47661a5a0ed332
diff --git a/meta-facebook/recipes-phosphor/fans/phosphor-fan/bletchley/host-poweroff b/meta-facebook/recipes-phosphor/fans/phosphor-fan/bletchley/host-poweroff
new file mode 100644
index 0000000..893facb
--- /dev/null
+++ b/meta-facebook/recipes-phosphor/fans/phosphor-fan/bletchley/host-poweroff
@@ -0,0 +1,5 @@
+#!/bin/bash
+# Power off the hosts when fan sensors crossed thresholds.
+
+echo "This feature is not implemented in bletchley and it will be implemented in future."
+
diff --git a/meta-facebook/recipes-phosphor/fans/phosphor-fan/obmc-chassis-hard-poweroff@.target b/meta-facebook/recipes-phosphor/fans/phosphor-fan/obmc-chassis-hard-poweroff@.target
new file mode 100644
index 0000000..0512fa4
--- /dev/null
+++ b/meta-facebook/recipes-phosphor/fans/phosphor-fan/obmc-chassis-hard-poweroff@.target
@@ -0,0 +1,4 @@
+[Unit]
+Description=Chassis%i (Hard Power Off)
+After=multi-user.target
+Requires=obmc-poweroff.service
diff --git a/meta-facebook/recipes-phosphor/fans/phosphor-fan/obmc-chassis-poweroff@0.target b/meta-facebook/recipes-phosphor/fans/phosphor-fan/obmc-chassis-poweroff@0.target
new file mode 100644
index 0000000..0389cf7
--- /dev/null
+++ b/meta-facebook/recipes-phosphor/fans/phosphor-fan/obmc-chassis-poweroff@0.target
@@ -0,0 +1,3 @@
+[Unit]
+Description=Chassis%i (Power Off)
+After=multi-user.target
diff --git a/meta-facebook/recipes-phosphor/fans/phosphor-fan/obmc-poweroff.service b/meta-facebook/recipes-phosphor/fans/phosphor-fan/obmc-poweroff.service
new file mode 100644
index 0000000..5f7ff4c
--- /dev/null
+++ b/meta-facebook/recipes-phosphor/fans/phosphor-fan/obmc-poweroff.service
@@ -0,0 +1,7 @@
+[Unit]
+Description=power off host
+
+[Service]
+RemainAfterExit=no
+Type=oneshot
+ExecStart=/usr/libexec/phosphor-fan-sensor-monitor/host-poweroff
diff --git a/meta-facebook/recipes-phosphor/fans/phosphor-fan/tiogapass/host-poweroff b/meta-facebook/recipes-phosphor/fans/phosphor-fan/tiogapass/host-poweroff
new file mode 100644
index 0000000..3d1212b
--- /dev/null
+++ b/meta-facebook/recipes-phosphor/fans/phosphor-fan/tiogapass/host-poweroff
@@ -0,0 +1,5 @@
+#!/bin/bash
+# Power off the hosts when fan sensors crossed thresholds.
+
+echo "This feature is not implemented in Tiogapass and it will be implemented in future."
+
diff --git a/meta-facebook/recipes-phosphor/fans/phosphor-fan/yosemitev2/host-poweroff b/meta-facebook/recipes-phosphor/fans/phosphor-fan/yosemitev2/host-poweroff
new file mode 100644
index 0000000..782f085
--- /dev/null
+++ b/meta-facebook/recipes-phosphor/fans/phosphor-fan/yosemitev2/host-poweroff
@@ -0,0 +1,28 @@
+#!/bin/bash
+# Power off the hosts when fan sensors crossed thresholds.
+
+echo "Power off the hosts if fansensors threshold crossed ::"
+
+HOST_INSTANCES="HOST_INSTANCES_SED_REPLACEMENT_VALUE"
+
+DBUS_SERVICE="xyz.openbmc_project.State.Chassis"
+DBUS_OBJECT="/xyz/openbmc_project/state/chassis"
+DBUS_INTERFACE="xyz.openbmc_project.State.Chassis"
+DBUS_PROPERTY="RequestedPowerTransition"
+PROPERTY_VALUE="xyz.openbmc_project.State.Chassis.Transition.Off"
+
+# Power off the hosts.
+power-off-all-hosts()
+{
+    for host_id in $HOST_INSTANCES
+    do
+        echo "chosen host id :::$host_id"
+
+        # host power off
+        output=$(busctl set-property $DBUS_SERVICE$host_id $DBUS_OBJECT$host_id $DBUS_INTERFACE $DBUS_PROPERTY s $PROPERTY_VALUE)
+        echo "$output"
+
+    done
+}
+
+power-off-all-hosts
diff --git a/meta-facebook/recipes-phosphor/fans/phosphor-fan_%.bbappend b/meta-facebook/recipes-phosphor/fans/phosphor-fan_%.bbappend
new file mode 100644
index 0000000..23aac0a
--- /dev/null
+++ b/meta-facebook/recipes-phosphor/fans/phosphor-fan_%.bbappend
@@ -0,0 +1,51 @@
+FILESEXTRAPATHS:prepend := "${THISDIR}/${PN}:"
+
+SRC_URI:append:fb-withhost = " file://obmc-chassis-hard-poweroff@.target \
+                               file://obmc-chassis-poweroff@0.target \
+                               file://obmc-poweroff.service \
+                               file://host-poweroff"
+
+PACKAGECONFIG:append = " json sensor-monitor"
+
+EXTRA_OECONF = "enable_host_state=yes"
+
+RDEPENDS:sensor-monitor = " bash"
+
+do_install:append:fb-withhost() {
+
+install -d ${D}${systemd_system_unitdir}
+install -m 0644 ${WORKDIR}/obmc-chassis-hard-poweroff@.target ${D}${systemd_system_unitdir}
+install -m 0644 ${WORKDIR}/obmc-chassis-poweroff@0.target ${D}${systemd_system_unitdir}
+install -m 0644 ${WORKDIR}/obmc-poweroff.service ${D}${systemd_system_unitdir}
+
+# Store the bitbake variable OBMC_HOST_INSTANCES  inside the script as HOST_INSTANCES variable using sed.
+sed -i -e "s,HOST_INSTANCES_SED_REPLACEMENT_VALUE,${OBMC_HOST_INSTANCES},g" ${WORKDIR}/host-poweroff
+
+install -m 0755 -d ${D}/var/lib/phosphor-fan-presence/sensor-monitor
+
+install -d ${D}/usr/libexec/phosphor-fan-sensor-monitor
+install -m 0777 ${WORKDIR}/host-poweroff ${D}/usr/libexec/phosphor-fan-sensor-monitor/
+
+}
+
+pkg_postinst:${PN}() {
+
+mkdir -p $D$systemd_system_unitdir/obmc-chassis-hard-poweroff@0.target.requires
+mkdir -p $D$systemd_system_unitdir/obmc-chassis-hard-poweroff@0.target.requires/obmc-chassis-poweroff@0.target.requires
+
+LINK="$D$systemd_system_unitdir/obmc-chassis-hard-poweroff@0.target.requires/obmc-chassis-poweroff@0.target"
+TARGET="../obmc-chassis-poweroff@0.target"
+ln -s $TARGET $LINK
+
+LINK="$D$systemd_system_unitdir/obmc-chassis-hard-poweroff@0.target.requires/obmc-chassis-poweroff@0.target.requires/obmc-poweroff.service"
+TARGET="../../obmc-poweroff.service"
+ln -s $TARGET $LINK
+
+}
+
+FILES:sensor-monitor += "/usr/libexec/phosphor-fan-sensor-monitor/host-poweroff"
+
+FILES:sensor-monitor += "${systemd_system_unitdir}/obmc-chassis-hard-poweroff@.target"
+FILES:sensor-monitor += "${systemd_system_unitdir}/obmc-chassis-poweroff@0.target "
+FILES:sensor-monitor += "${systemd_system_unitdir}/obmc-poweroff.service"
+
diff --git a/meta-ibm/recipes-phosphor/fans/phosphor-fan_%.bbappend b/meta-ibm/recipes-phosphor/fans/phosphor-fan_%.bbappend
index a6d0357..e0d4913 100644
--- a/meta-ibm/recipes-phosphor/fans/phosphor-fan_%.bbappend
+++ b/meta-ibm/recipes-phosphor/fans/phosphor-fan_%.bbappend
@@ -61,7 +61,6 @@
 EXTRA_OECONF:append:witherspoon = " --disable-json-control"
 
 PACKAGECONFIG:append:p10bmc = " json sensor-monitor"
-FAN_PACKAGES:append:p10bmc = " sensor-monitor"
 
 # Set the appropriate i2c address used within the overridden phosphor-fan-control@.service
 # file that's used for witherspoon type(including witherspoon-tacoma) machines
diff --git a/meta-phosphor/recipes-phosphor/fans/phosphor-fan_git.bb b/meta-phosphor/recipes-phosphor/fans/phosphor-fan_git.bb
index 28da7fe..9540c8b 100644
--- a/meta-phosphor/recipes-phosphor/fans/phosphor-fan_git.bb
+++ b/meta-phosphor/recipes-phosphor/fans/phosphor-fan_git.bb
@@ -30,11 +30,12 @@
         ${PN}-presence-tach \
         ${PN}-control \
         ${PN}-monitor \
+        ${PN}-sensor-monitor \
 "
 
 ALLOW_EMPTY:${PN} = "1"
 PACKAGE_BEFORE_PN += "${FAN_PACKAGES}"
-PACKAGECONFIG ?= "presence control monitor"
+PACKAGECONFIG ?= "presence control monitor sensor-monitor"
 SYSTEMD_PACKAGES = "${FAN_PACKAGES}"
 PKG_DEFAULT_MACHINE ??= "${MACHINE}"
 PACKAGE_ARCH = "${MACHINE_ARCH}"
@@ -162,6 +163,8 @@
 # ${PN}-sensor-monitor specific configuration
 PACKAGECONFIG[sensor-monitor] = "--enable-sensor-monitor, --disable-sensor-monitor"
 
+FAN_PACKAGES:append = "${@bb.utils.contains('PACKAGECONFIG', 'sensor-monitor', ' sensor-monitor', '', d)}"
+
 FILES:sensor-monitor += " ${bindir}/sensor-monitor"
 SYSTEMD_SERVICE:sensor-monitor += "sensor-monitor.service"
 SYSTEMD_LINK:sensor-monitor += "../sensor-monitor.service:${MULTI_USR_TGT}.wants/sensor-monitor.service"