1#!/bin/bash
2
3get_gpio()
4{
5    local NET_NAME=$1
6    local RET_VAL
7
8    mapfile -t -d " " GPIO_INFO < <(gpiofind "$NET_NAME")
9    if [ "${#GPIO_INFO[@]}" -ne 2 ]; then
10        echo "get_gpio: can not find gpio, $NET_NAME" >&2
11        return 1
12    fi
13    if ! RET_VAL=$(gpioget "${GPIO_INFO[0]}" "${GPIO_INFO[1]%$'\n'}") ; then
14        echo "get_gpio: get ${NET_NAME} failed" >&2
15        return 1
16    fi
17    echo "${RET_VAL}"
18    return 0
19}
20
21set_gpio()
22{
23    local NET_NAME=$1
24    local OUT_VAL=$2
25    mapfile -t -d " " GPIO_INFO < <(gpiofind "$NET_NAME")
26    if [ "${#GPIO_INFO[@]}" -ne 2 ]; then
27        echo "set_gpio: can not find gpio, $NET_NAME"
28        return 1
29    fi
30
31    echo -n "set_gpio: set $NET_NAME = $OUT_VAL"
32    if ! gpioset "${GPIO_INFO[0]}" "${GPIO_INFO[1]%$'\n'}"="$OUT_VAL"; then
33        echo " failed"
34        return 1
35    fi
36
37    echo " success"
38    return 0
39}
40
41set_fan()
42{
43    FAN_ID=$1
44    FAN_DUTY=$2
45    SYSFA_PWM_PATH=""
46
47    for file in /sys/devices/platform/pwm-fan"$FAN_ID"/hwmon/hwmon*/pwm1
48    do
49        if [ -e "$file" ]; then
50            SYSFA_PWM_PATH="$file"
51            break
52        fi
53    done
54
55    if [ -z "$SYSFA_PWM_PATH" ]; then
56        echo "set_fan: pwm file not found, chekc fan id ($FAN_ID)"
57        return 1
58    fi
59
60    if [ "$FAN_DUTY" -lt 0 ] || [ "$FAN_DUTY" -gt 100 ]; then
61        echo "set_fan: incorrect fan duty, $FAN_DUTY"
62        return 1
63    fi
64
65    # convert duty (0-100) to pwm value (0-255)
66    PWM_VAL=$(printf "%.0f" $((FAN_DUTY*255/100)))
67
68    echo -n "set_fan: set fan$FAN_ID = $FAN_DUTY"
69    if ! echo "$PWM_VAL" > "$SYSFA_PWM_PATH"; then
70        echo " failed"
71        return 1
72    fi
73
74    echo " success"
75    return 0
76}
77
78
79is_sled_present()
80{
81    local SLED_ID=$1
82    local SRV_NAME="xyz.openbmc_project.Inventory.Manager"
83    local OBJ_PATH="/xyz/openbmc_project/inventory/system/chassis/presence/presence_sled${SLED_ID}"
84    local INTF_NAME="xyz.openbmc_project.Inventory.Item"
85    local PRST_VAL
86
87    PRST_VAL=$(busctl get-property "${SRV_NAME}" "${OBJ_PATH}" "${INTF_NAME}" Present | awk '{print $2}')
88    if [ "$PRST_VAL" != "true" ]; then
89        # not present
90        return 1
91    fi
92
93    # present
94    return 0
95}
96