meta-bletchley: introduce Bletchley common functions

Add a common functions script for reusing the functions which are
called frequently.

This patch depends on the patchs below:
* https://lore.kernel.org/all/20220613095150.21917-4-potin.lai.pt@gmail.com/

Signed-off-by: Potin Lai <potin.lai@quantatw.com>
Change-Id: I6e1602b00f1b0a19e77506be6eabe18c796e52fe
diff --git a/meta-facebook/meta-bletchley/recipes-bletchley/motor-ctrl/files/motor-ctrl b/meta-facebook/meta-bletchley/recipes-bletchley/motor-ctrl/files/motor-ctrl
index d74909c..b4a6988 100755
--- a/meta-facebook/meta-bletchley/recipes-bletchley/motor-ctrl/files/motor-ctrl
+++ b/meta-facebook/meta-bletchley/recipes-bletchley/motor-ctrl/files/motor-ctrl
@@ -2,41 +2,9 @@
 #
 # Control step motor rotate of sled
 
-function set_gpio()
-{
-    NET_NAME=$1
-    OUT_VAL=$2
-    mapfile -t -d " " GPIO_INFO < <(gpiofind "$NET_NAME")
-    if [ "${#GPIO_INFO[@]}" -ne 2 ]; then
-        echo "set_gpio: can not find gpio, $NET_NAME"
-        return 1
-    fi
-    echo -n "set_gpio: set $NET_NAME = $OUT_VAL"
-    if ! gpioset "${GPIO_INFO[0]}" "${GPIO_INFO[1]%$'\n'}"="$OUT_VAL"; then
-        echo " failed"
-        return 1
-    fi
-    echo " success"
-    return 0
-}
+# shellcheck source=meta-facebook/meta-bletchley/recipes-bletchley/plat-tools/files/bletchley-common-functions
+source /usr/libexec/bletchley-common-functions
 
-function get_gpio()
-{
-    NET_NAME=$1
-    RET_VAL=2
-
-    mapfile -t -d " " GPIO_INFO < <(gpiofind "$NET_NAME")
-    if [ "${#GPIO_INFO[@]}" -ne 2 ]; then
-        echo "get_gpio: can not find gpio, $NET_NAME" >&2
-        return 1
-    fi
-    if ! RET_VAL=$(gpioget "${GPIO_INFO[0]}" "${GPIO_INFO[1]%$'\n'}") ; then
-        echo "get_gpio: get ${NET_NAME} failed" >&2
-        return 1
-    fi
-    echo "${RET_VAL}"
-    return 0
-}
 #######################################
 # Setting step motor control pins to start/stop motor
 # Arguments:
@@ -79,8 +47,7 @@
 fi
 
 #Check if sled is present
-SLED_PRESENT=$(get_gpio "presence-sled${SLED_NUM}")
-if [ "$SLED_PRESENT" != 0 ];then
+if ! is_sled_present "${SLED_NUM}"; then
     echo "${SLED} is not present!"
     exit 1
 fi
diff --git a/meta-facebook/meta-bletchley/recipes-bletchley/motor-ctrl/files/motor-init b/meta-facebook/meta-bletchley/recipes-bletchley/motor-ctrl/files/motor-init
index 89369ac..87b3a17 100755
--- a/meta-facebook/meta-bletchley/recipes-bletchley/motor-ctrl/files/motor-init
+++ b/meta-facebook/meta-bletchley/recipes-bletchley/motor-ctrl/files/motor-init
@@ -7,6 +7,9 @@
 
 export PATH=$PATH:/usr/libexec
 
+# shellcheck source=meta-facebook/meta-bletchley/recipes-bletchley/plat-tools/files/bletchley-common-functions
+source /usr/libexec/bletchley-common-functions
+
 PWM_CLASS_PATH="/sys/class/pwm/pwmchip0"
 #Sleld 1~6 using bmc pwm8~13 as motor driver stick
 PWM_NUM_OFFSET=7
@@ -45,42 +48,6 @@
     fi
 }
 
-function set_gpio()
-{
-    NET_NAME=$1
-    OUT_VAL=$2
-    mapfile -t -d " " GPIO_INFO < <(gpiofind "$NET_NAME")
-    if [ "${#GPIO_INFO[@]}" -ne 2 ]; then
-        echo "set_gpio: can not find gpio, $NET_NAME"
-        return 1
-    fi
-    echo -n "set_gpio: set $NET_NAME = $OUT_VAL"
-    if ! gpioset "${GPIO_INFO[0]}" "${GPIO_INFO[1]%$'\n'}"="$OUT_VAL"; then
-        echo " failed"
-        return 1
-    fi
-    echo " success"
-    return 0
-}
-
-function get_gpio()
-{
-    NET_NAME=$1
-    RET_VAL=2
-
-    mapfile -t -d " " GPIO_INFO < <(gpiofind "$NET_NAME")
-    if [ "${#GPIO_INFO[@]}" -ne 2 ]; then
-        echo "get_gpio: can not find gpio, $NET_NAME" >&2
-        return 1
-    fi
-    if ! RET_VAL=$(gpioget "${GPIO_INFO[0]}" "${GPIO_INFO[1]%$'\n'}") ; then
-        echo "get_gpio: get ${NET_NAME} failed" >&2
-        return 1
-    fi
-    echo "${RET_VAL}"
-    return 0
-}
-
 #Init gpio pins for step motor control
 function init_gpios() {
     echo "Init GPIOs:"
@@ -107,8 +74,7 @@
 fi
 
 #Check if sled is present
-SLED_PRESENT=$(get_gpio "presence-sled${SLED_NUM}")
-if [ "$SLED_PRESENT" != 0 ];then
+if ! is_sled_present "${SLED_NUM}"; then
     echo "${SLED} is not present, skip motor initialize"
     exit 1
 fi
diff --git a/meta-facebook/meta-bletchley/recipes-bletchley/motor-ctrl/files/motor-init-calibration@.service b/meta-facebook/meta-bletchley/recipes-bletchley/motor-ctrl/files/motor-init-calibration@.service
index 41f81c2..f28ab5e 100644
--- a/meta-facebook/meta-bletchley/recipes-bletchley/motor-ctrl/files/motor-init-calibration@.service
+++ b/meta-facebook/meta-bletchley/recipes-bletchley/motor-ctrl/files/motor-init-calibration@.service
@@ -2,6 +2,8 @@
 Description=Motor Initialize for sled%i
 StartLimitIntervalSec=90
 StartLimitBurst=5
+After=mapper-wait@-xyz-openbmc_project-inventory-system-chassis-presence-presence_sled%i
+Wants=mapper-wait@-xyz-openbmc_project-inventory-system-chassis-presence-presence_sled%i
 
 [Service]
 ExecStart=/usr/libexec/motor-init sled%i
diff --git a/meta-facebook/meta-bletchley/recipes-bletchley/motor-ctrl/files/power-ctrl b/meta-facebook/meta-bletchley/recipes-bletchley/motor-ctrl/files/power-ctrl
index def4689..e430f8f 100755
--- a/meta-facebook/meta-bletchley/recipes-bletchley/motor-ctrl/files/power-ctrl
+++ b/meta-facebook/meta-bletchley/recipes-bletchley/motor-ctrl/files/power-ctrl
@@ -6,6 +6,9 @@
 
 export PATH=$PATH:/usr/sbin:/usr/libexec
 
+# shellcheck source=meta-facebook/meta-bletchley/recipes-bletchley/plat-tools/files/bletchley-common-functions
+source /usr/libexec/bletchley-common-functions
+
 DELAY_POWER_ON="0.5"
 DELAY_POWER_OFF="5"
 DELAY_POWER_RECOVERY_MODE="10"
@@ -189,42 +192,6 @@
     echo "$bus"
 }
 
-function set_gpio()
-{
-    NET_NAME=$1
-    OUT_VAL=$2
-    mapfile -t -d " " GPIO_INFO < <(gpiofind "$NET_NAME")
-    if [ "${#GPIO_INFO[@]}" -ne 2 ]; then
-        echo "set_gpio: can not find gpio, $NET_NAME"
-        return 1
-    fi
-    echo -n "set_gpio: set $NET_NAME = $OUT_VAL"
-    if ! gpioset "${GPIO_INFO[0]}" "${GPIO_INFO[1]%$'\n'}"="$OUT_VAL"; then
-        echo " failed"
-        return 1
-    fi
-    echo " success"
-    return 0
-}
-
-function get_gpio()
-{
-    NET_NAME=$1
-    RET_VAL=2
-
-    mapfile -t -d " " GPIO_INFO < <(gpiofind "$NET_NAME")
-    if [ "${#GPIO_INFO[@]}" -ne 2 ]; then
-        echo "get_gpio: can not find gpio, $NET_NAME" >&2
-        return 1
-    fi
-    if ! RET_VAL=$(gpioget "${GPIO_INFO[0]}" "${GPIO_INFO[1]%$'\n'}") ; then
-        echo "get_gpio: get ${NET_NAME} failed" >&2
-        return 1
-    fi
-    echo "${RET_VAL}"
-    return 0
-}
-
 function get_ac_status()
 {
     i2c_bus=$(get_bus_num "$1")
@@ -619,8 +586,7 @@
 fi
 
 #Check if sled is present
-SLED_PRESENT=$(get_gpio "presence-sled${SLED_NUM}")
-if [ "$SLED_PRESENT" != 0 ];then
+if ! is_sled_present "${SLED_NUM}"; then
     echo "${SLED} is not present!"
     exit 1
 elif ! is_valid_sled_action "$ACTION"; then
diff --git a/meta-facebook/meta-bletchley/recipes-bletchley/motor-ctrl/motor-ctrl_0.1.bb b/meta-facebook/meta-bletchley/recipes-bletchley/motor-ctrl/motor-ctrl_0.1.bb
index 88f3349..9c2c68c 100644
--- a/meta-facebook/meta-bletchley/recipes-bletchley/motor-ctrl/motor-ctrl_0.1.bb
+++ b/meta-facebook/meta-bletchley/recipes-bletchley/motor-ctrl/motor-ctrl_0.1.bb
@@ -9,6 +9,7 @@
 RDEPENDS:${PN} += "i2c-tools"
 RDEPENDS:${PN} += "libgpiod-tools"
 RDEPENDS:${PN} += "mdio-util"
+RDEPENDS:${PN} += "bletchley-common-functions"
 
 S = "${WORKDIR}"
 SRC_URI += " \
diff --git a/meta-facebook/meta-bletchley/recipes-bletchley/plat-svc/files/bletchley-early-sys-init b/meta-facebook/meta-bletchley/recipes-bletchley/plat-svc/files/bletchley-early-sys-init
index 2d41c94..a0b5c5a 100755
--- a/meta-facebook/meta-bletchley/recipes-bletchley/plat-svc/files/bletchley-early-sys-init
+++ b/meta-facebook/meta-bletchley/recipes-bletchley/plat-svc/files/bletchley-early-sys-init
@@ -1,61 +1,7 @@
 #!/bin/bash -e
 
-set_gpio()
-{
-    NET_NAME=$1
-    OUT_VAL=$2
-    mapfile -t -d " " GPIO_INFO < <(gpiofind "$NET_NAME")
-    if [ "${#GPIO_INFO[@]}" -ne 2 ]; then
-        echo "set_gpio: can not find gpio, $NET_NAME"
-        return 1
-    fi
-
-    echo -n "set_gpio: set $NET_NAME = $OUT_VAL"
-    if ! gpioset "${GPIO_INFO[0]}" "${GPIO_INFO[1]%$'\n'}"="$OUT_VAL"; then
-        echo " failed"
-        return 1
-    fi
-
-    echo " success"
-    return 0
-}
-
-set_fan()
-{
-    FAN_ID=$1
-    FAN_DUTY=$2
-    SYSFA_PWM_PATH=""
-
-    for file in /sys/devices/platform/pwm-fan"$FAN_ID"/hwmon/hwmon*/pwm1
-    do
-        if [ -e "$file" ]; then
-            SYSFA_PWM_PATH="$file"
-            break
-        fi
-    done
-
-    if [ -z "$SYSFA_PWM_PATH" ]; then
-        echo "set_fan: pwm file not found, chekc fan id ($FAN_ID)"
-        return 1
-    fi
-
-    if [ "$FAN_DUTY" -lt 0 ] || [ "$FAN_DUTY" -gt 100 ]; then
-        echo "set_fan: incorrect fan duty, $FAN_DUTY"
-        return 1
-    fi
-
-    # convert duty (0-100) to pwm value (0-255)
-    PWM_VAL=$(printf "%.0f" $((FAN_DUTY*255/100)))
-
-    echo -n "set_fan: set fan$FAN_ID = $FAN_DUTY"
-    if ! echo "$PWM_VAL" > "$SYSFA_PWM_PATH"; then
-        echo " failed"
-        return 1
-    fi
-
-    echo " success"
-    return 0
-}
+# shellcheck source=meta-facebook/meta-bletchley/recipes-bletchley/plat-tools/files/bletchley-common-functions
+source /usr/libexec/bletchley-common-functions
 
 # set initial value for GPIO output pins
 set_gpio SEL_SPI2_MUX       1
diff --git a/meta-facebook/meta-bletchley/recipes-bletchley/plat-svc/plat-svc_0.1.bb b/meta-facebook/meta-bletchley/recipes-bletchley/plat-svc/plat-svc_0.1.bb
index 99af81d..23b2d1f 100644
--- a/meta-facebook/meta-bletchley/recipes-bletchley/plat-svc/plat-svc_0.1.bb
+++ b/meta-facebook/meta-bletchley/recipes-bletchley/plat-svc/plat-svc_0.1.bb
@@ -5,6 +5,7 @@
 
 RDEPENDS:${PN} += "bash"
 RDEPENDS:${PN} += "libgpiod-tools"
+RDEPENDS:${PN} += "bletchley-common-functions"
 
 SRC_URI += " \
     file://bletchley-early-sys-init \
diff --git a/meta-facebook/meta-bletchley/recipes-bletchley/plat-tools/bletchley-common-functions_0.1.bb b/meta-facebook/meta-bletchley/recipes-bletchley/plat-tools/bletchley-common-functions_0.1.bb
new file mode 100644
index 0000000..0581f23
--- /dev/null
+++ b/meta-facebook/meta-bletchley/recipes-bletchley/plat-tools/bletchley-common-functions_0.1.bb
@@ -0,0 +1,13 @@
+LICENSE = "Apache-2.0"
+LIC_FILES_CHKSUM = "file://${COREBASE}/meta/files/common-licenses/Apache-2.0;md5=89aea4e17d99a7cacdbeed46a0096b10"
+
+RDEPENDS:${PN} += " bash libgpiod-tools"
+
+SRC_URI += " \
+    file://bletchley-common-functions \
+    "
+
+do_install() {
+    install -d ${D}${libexecdir}
+    install -m 0755 ${WORKDIR}/bletchley-common-functions ${D}${libexecdir}
+}
diff --git a/meta-facebook/meta-bletchley/recipes-bletchley/plat-tools/files/bletchley-common-functions b/meta-facebook/meta-bletchley/recipes-bletchley/plat-tools/files/bletchley-common-functions
new file mode 100644
index 0000000..e3ca00a
--- /dev/null
+++ b/meta-facebook/meta-bletchley/recipes-bletchley/plat-tools/files/bletchley-common-functions
@@ -0,0 +1,95 @@
+#!/bin/bash
+
+get_gpio()
+{
+    local NET_NAME=$1
+    local RET_VAL
+
+    mapfile -t -d " " GPIO_INFO < <(gpiofind "$NET_NAME")
+    if [ "${#GPIO_INFO[@]}" -ne 2 ]; then
+        echo "get_gpio: can not find gpio, $NET_NAME" >&2
+        return 1
+    fi
+    if ! RET_VAL=$(gpioget "${GPIO_INFO[0]}" "${GPIO_INFO[1]%$'\n'}") ; then
+        echo "get_gpio: get ${NET_NAME} failed" >&2
+        return 1
+    fi
+    echo "${RET_VAL}"
+    return 0
+}
+
+set_gpio()
+{
+    local NET_NAME=$1
+    local OUT_VAL=$2
+    mapfile -t -d " " GPIO_INFO < <(gpiofind "$NET_NAME")
+    if [ "${#GPIO_INFO[@]}" -ne 2 ]; then
+        echo "set_gpio: can not find gpio, $NET_NAME"
+        return 1
+    fi
+
+    echo -n "set_gpio: set $NET_NAME = $OUT_VAL"
+    if ! gpioset "${GPIO_INFO[0]}" "${GPIO_INFO[1]%$'\n'}"="$OUT_VAL"; then
+        echo " failed"
+        return 1
+    fi
+
+    echo " success"
+    return 0
+}
+
+set_fan()
+{
+    FAN_ID=$1
+    FAN_DUTY=$2
+    SYSFA_PWM_PATH=""
+
+    for file in /sys/devices/platform/pwm-fan"$FAN_ID"/hwmon/hwmon*/pwm1
+    do
+        if [ -e "$file" ]; then
+            SYSFA_PWM_PATH="$file"
+            break
+        fi
+    done
+
+    if [ -z "$SYSFA_PWM_PATH" ]; then
+        echo "set_fan: pwm file not found, chekc fan id ($FAN_ID)"
+        return 1
+    fi
+
+    if [ "$FAN_DUTY" -lt 0 ] || [ "$FAN_DUTY" -gt 100 ]; then
+        echo "set_fan: incorrect fan duty, $FAN_DUTY"
+        return 1
+    fi
+
+    # convert duty (0-100) to pwm value (0-255)
+    PWM_VAL=$(printf "%.0f" $((FAN_DUTY*255/100)))
+
+    echo -n "set_fan: set fan$FAN_ID = $FAN_DUTY"
+    if ! echo "$PWM_VAL" > "$SYSFA_PWM_PATH"; then
+        echo " failed"
+        return 1
+    fi
+
+    echo " success"
+    return 0
+}
+
+
+is_sled_present()
+{
+    local SLED_ID=$1
+    local SRV_NAME="xyz.openbmc_project.Inventory.Manager"
+    local OBJ_PATH="/xyz/openbmc_project/inventory/system/chassis/presence/presence_sled${SLED_ID}"
+    local INTF_NAME="xyz.openbmc_project.Inventory.Item"
+    local PRST_VAL
+
+    PRST_VAL=$(busctl get-property "${SRV_NAME}" "${OBJ_PATH}" "${INTF_NAME}" Present | awk '{print $2}')
+    if [ "$PRST_VAL" != "true" ]; then
+        # not present
+        return 1
+    fi
+
+    # present
+    return 0
+}