Skip to content

Instantly share code, notes, and snippets.

@tdrkDev
Created August 5, 2023 21:49
Show Gist options
  • Save tdrkDev/59d165c63fd4beebfcf25a05a47caa0e to your computer and use it in GitHub Desktop.
Save tdrkDev/59d165c63fd4beebfcf25a05a47caa0e to your computer and use it in GitHub Desktop.
ADB (Termux) temperature monitor
#!/bin/bash
TEST_RUN_FILE="$HOME/Downloads/alo.txt"
TEST_RUN=
round() {
printf '%.0f' "$1"
}
get_stats() {
[ ! "$TEST_RUN" ] && ($ADB $DUMPSYS thermalservice > "$TEMPFILE" || exit 1)
THROTTLING="$(grep 'Thermal Status' "$TEMPFILE" | cut -d ' ' -f 3)"
case "$THROTTLING" in
0)
THROTTLING_DESC="No."
;;
1)
THROTTLING_DESC="Light. UX isn't impacted."
;;
2)
THROTTLING_DESC="Moderate. UX isn't greatly impacted."
;;
3)
THROTTLING_DESC="Severe. UX is largely impacted."
;;
4)
THROTTLING_DESC="Critical. Platform has done everything to reduce power."
;;
5)
THROTTLING_DESC="Emergency. Key components in the platform are shutting down due to thermal conditions."
;;
5)
THROTTLING_DESC="Shutdown immediately."
;;
*)
THROTTLING_DESC="Unknown. Value: $THROTTLING"
;;
esac
TEMP_TYPES=($(sed -n '/Current temperatures/,/Current cooling/p;/from HAL/d' "$TEMPFILE" | sed '/from HAL/d' | cut -d ',' -f 3 | cut -d '=' -f 2))
TEMP_VALUES=($(sed -n '/Current temperatures/,/Current cooling/p;/from HAL/d' "$TEMPFILE" | sed '/from HAL/d' | cut -d ',' -f 1 | cut -d '=' -f 2))
TEMP_COUNT="${#TEMP_TYPES[@]}"
TEMP_COUNT_ZERO=0
for ((i=0; $i<$TEMP_COUNT; i++)) {
TEMP_TYPE="${TEMP_TYPES[$i]}"
TEMP_VALUE="${TEMP_VALUES[$i]}"
check_is_temp_valid || ((TEMP_COUNT_ZERO++))
}
}
remap_type() {
case "$TEMP_TYPE" in
"AP")
TEMP_TYPE="CPU"
[ "$IS_SAMSUNG" ] && TEMP_TYPE="Exynos heater"
;;
"BAT"|"battery")
TEMP_TYPE="Battery"
;;
"SKIN"|"skin")
TEMP_TYPE="Device case"
;;
"USB")
TEMP_TYPE="Type-C port"
;;
"PA")
TEMP_TYPE="Power control"
;;
CPU[0-9])
TEMP_TYPE="CPU core $(echo "$TEMP_TYPE" | sed 's/CPU//g')"
;;
"GPU0")
TEMP_TYPE="GPU hot spot"
;;
"GPU1")
TEMP_TYPE="GPU crystal"
;;
"nsp0")
TEMP_TYPE="NPU hot spot"
;;
"nsp1")
TEMP_TYPE="NPU crystal"
;;
esac
}
thrott_color() {
if [ "$THROTTLING" -gt 4 ]; then
echo -e "\033[1m\033[0;31m" # red
elif [ "$THROTTLING" -gt 3 ]; then
echo -e "\033[1m\033[1;31m" # light red
elif [ "$THROTTLING" -gt 2 ]; then
echo -e "\033[1m\033[1;33m" # red
else
echo -e "\033[1m\033[1;32m" # green
fi
}
temp_color() {
TEM="$(round $TEMP_VALUE)"
if [ "$TEM" -gt 60 ]; then
echo -e "\033[1m\033[0;31m !!! !!! " # red
elif [ "$TEM" -gt 50 ]; then
echo -e "\033[1m\033[1;31m" # light red
elif [ "$TEM" -gt 40 ]; then
echo -e "\033[1m\033[1;33m" # yellow
else
echo -e "\033[1m\033[1;32m" # green
fi
}
check_is_temp_valid() {
# 0.0 degrees
[ "$(round $TEMP_VALUE)" -eq 0 ] && return 1
# negative value, fuck qualcomm
[ "$(round $TEMP_VALUE)" -lt 0 ] && return 1
# battery voltage, fuck qualcomm
[ "$TEMP_TYPE" = "vbat" ] && return 1
return 0
}
print_stats() {
get_stats
echo -e "\033[1mThrottling: $(thrott_color)$THROTTLING_DESC\033[0m"
echo
for ((i=0; $i<$TEMP_COUNT; i++)) {
TEMP_TYPE="${TEMP_TYPES[$i]}"
TEMP_VALUE="${TEMP_VALUES[$i]}"
check_is_temp_valid || continue
remap_type
echo -e "\033[1m$TEMP_TYPE:\033[0m\t$(temp_color)${TEMP_VALUE} °C\033[0m "
}
}
check_cpu() {
CPUBRAND="$($ADB getprop ro.soc.manufacturer)"
[ "$CPUBRAND" = "Samsung" ] && IS_SAMSUNG=true
}
check_os() {
which getprop >/dev/null 2>/dev/null
if [ "$?" = "0" ]; then
ADB=""
TEMPFILE="$HOME/.exynostemp"
DUMPSYS="/system/bin/dumpsys"
echo -e "Running on \033[1mAndroid\033[0m."
else
ADB="adb shell"
TEMPFILE="/tmp/exynostemp"
[ "$TEST_RUN" ] && TEMPFILE="$TEST_RUN_FILE"
DUMPSYS="dumpsys"
echo -e "Running on \033[1mUnix-like desktop OS\033[0m."
fi
}
check_os
check_cpu
get_stats
[ "$TEST_RUN" ] && echo -e "\033[1m\033[1;31mTest run from $TEMPFILE. All data is predefined.\033[0m"
echo -e "Available \033[1m${TEMP_COUNT} thermal sensors\033[0m from ThermalHAL."
echo -e "Skipping \033[1m${TEMP_COUNT_ZERO} thermal sensors\033[0m because their values are invalid."
echo
echo "Press any key to exit."
r="$(expr ${TEMP_COUNT} - ${TEMP_COUNT_ZERO} + 3)"
r2="$(expr $r - 1)"
for ((i=0;$i<$r;i++)); do
echo
done
while true; do
echo -en "\033[${r2}A"
print_stats
read -t 1 && exit 0
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment