1#!/bin/bash
2
3FAN_TABLE_FILE_IN="/usr/share/swampd/config.json.in"
4TEMP_FILE="$(mktemp)"
5cp "$FAN_TABLE_FILE_IN" "$TEMP_FILE"
6
7# wait for fan dbus
8mapper wait /xyz/openbmc_project/sensors/fan_tach/fan0
9mapper wait /xyz/openbmc_project/sensors/fan_tach/fan1
10mapper wait /xyz/openbmc_project/sensors/fan_tach/fb_fan0
11mapper wait /xyz/openbmc_project/sensors/fan_tach/fb_fan1
12mapper wait /xyz/openbmc_project/sensors/fan_tach/fb_fan2
13
14# generate fan table writePath
15Fan_0_To_4_Hwmon="$(ls /sys/devices/platform/ahb/ahb\:*/*pwm-fan-controller/hwmon/)"
16
17if [[ "$Fan_0_To_4_Hwmon" != "" ]]; then
18    sed -i "s/@Fan_0_To_4_Hwmon@/$Fan_0_To_4_Hwmon/g" $TEMP_FILE
19fi
20
21presentGpio=()
22presentState=()
23gpioPath="/sys/class/gpio/gpio"
24if [[ -f "/etc/nvme/nvme_config.json" ]]; then
25    presentGpio=($(cat /etc/nvme/nvme_config.json | grep NVMeDrivePresentPin \
26                   | awk '{print $2}' | cut -d "," -f 0))
27fi
28
29nvmePath="/xyz/openbmc_project/sensors/temperature/nvme"
30nvmeInventoryPath="/xyz/openbmc_project/inventory/system/chassis/motherboard/nvme"
31# Get and Set WCTEMP
32for ((i = 0; i < 16; i++)); do
33    name="@WCTemp$(printf "%02d" $i)@"
34    wcTemp=72000
35
36    if [[ -d "${gpioPath}${presentGpio[i]}" ]] &&
37       [[ "$(cat ${gpioPath}${presentGpio[i]}/value)" == "0" ]]; then
38        presentState[i]="true"
39    else
40        presentState[i]="false"
41    fi
42
43    if [[ "${presentState[i]}" == "true" ]]; then
44        actualWCTemp=0
45        for ((j = 0; j < 3; j++)); do
46            actualWCTemp="$(
47                busctl get-property xyz.openbmc_project.nvme.manager \
48                    ${nvmePath}${i} \
49                    xyz.openbmc_project.Sensor.Threshold.Critical \
50                    CriticalHigh | awk '{print $2}'
51            )"
52            if [[ "${actualWCTemp}" -ne 0 ]]; then
53                break
54            fi
55
56            echo "${nvmePath}${i} WCTemp was read to be 0, retrying after 1 sec sleep"
57            sleep 1
58        done
59
60        if [[ "${actualWCTemp}" -ne 0 ]]; then
61            wcTemp="$(echo "${actualWCTemp} -7" | awk '{printf $1 + $2}')"
62        else
63            echo "${nvmePath}${i} WCTemp was read to be 0, using default WCTemp: ${wcTemp}"
64        fi
65    fi
66
67    sed -i "s/$name/${wcTemp}/g" "$TEMP_FILE"
68
69    if [[ "${presentState[i]}" == "false" ]]; then
70        sensorPath="${nvmePath}${i}"
71        pathLine=$(grep -nw "$sensorPath" "$TEMP_FILE" | awk -F ':' '{print $1}')
72        sed -i "$TEMP_FILE" -re "$((pathLine - 3)),$((pathLine + 6))d"
73        if [ $i -eq 15 ]; then
74            sed -i "$((pathLine - 4))s/,//" "$TEMP_FILE"
75        fi
76
77        listLine=$(grep -n "\"name\": \"nvme${i}\"" "$TEMP_FILE" | awk -F ':' '{print $1}')
78        sed -i "$TEMP_FILE" -re "$((listLine - 1)),$((listLine + 21))d"
79        if [ $i -eq 15 ]; then
80            sed -i "$((listLine - 2))s/,//" "$TEMP_FILE"
81        fi
82    fi
83done
84
85# Use shell parameter expansion to trim the ".in" suffix
86mv "$TEMP_FILE" "${FAN_TABLE_FILE_IN%".in"}"
87
88exit 0
89