diff --git a/packages/bsp/common/etc/cron.d/armbian-check-battery b/packages/bsp/common/etc/cron.d/armbian-check-battery new file mode 100644 index 0000000000..58d2da22cf --- /dev/null +++ b/packages/bsp/common/etc/cron.d/armbian-check-battery @@ -0,0 +1,2 @@ +# Uncomment the line below to enable 5-minutes check for a low battery level +# */5 * * * * root bash /usr/lib/armbian/armbian-check-battery-shutdown diff --git a/packages/bsp/common/etc/update-motd.d/30-armbian-sysinfo b/packages/bsp/common/etc/update-motd.d/30-armbian-sysinfo index 575a315534..417a6f941e 100755 --- a/packages/bsp/common/etc/update-motd.d/30-armbian-sysinfo +++ b/packages/bsp/common/etc/update-motd.d/30-armbian-sysinfo @@ -34,6 +34,12 @@ done # don't edit below here +# Include functions: +# getboardtemp() +# batteryinfo() +# ambienttemp() +source /usr/lib/armbian/armbian-allwinner-battery + function display() { # $1=name $2=value $3=red_limit $4=minimal_show_limit $5=unit $6=after $7=acs/desc{ # battery red color is opposite, lower number @@ -47,89 +53,6 @@ function display() { fi } # display -function getboardtemp() { - if [ -f /etc/armbianmonitor/datasources/soctemp ]; then - read raw_temp /dev/null - if [ ! -z $(echo "$raw_temp" | grep -o "^[1-9][0-9]*\.\?[0-9]*$") ] && (( $(echo "${raw_temp} < 200" |bc -l) )); then - # Allwinner legacy kernels output degree C - board_temp=${raw_temp} - else - board_temp=$(awk '{printf("%d",$1/1000)}' <<<${raw_temp}) - fi - elif [ -f /etc/armbianmonitor/datasources/pmictemp ]; then - # fallback to PMIC temperature - board_temp=$(awk '{printf("%d",$1/1000)}' /dev/null - if [[ "$status_battery_connected" == "1" ]]; then - read status_battery_charging < $mainline_dir/charger/charging - read status_ac_connect < $mainline_dir/ac/connected - read battery_percent< $mainline_dir/battery/capacity - # dispay charging / percentage - if [[ "$status_ac_connect" == "1" && "$battery_percent" -lt "100" ]]; then - status_battery_text=" charging" - elif [[ "$status_ac_connect" == "1" && "$battery_percent" -eq "100" ]]; then - status_battery_text=" charged" - else - status_battery_text=" discharging" - fi - fi - elif [[ -e "$legacy_dir/axp813-ac" ]]; then - read status_battery_connected < $legacy_dir/axp20x-battery/present - if [[ "$status_battery_connected" == "1" ]]; then - status_battery_text=" "$(awk '{print tolower($0)}' < $legacy_dir/axp20x-battery/status) - read status_ac_connect < $legacy_dir/axp813-ac/present - read battery_percent< $legacy_dir/axp20x-battery/capacity - fi - elif [[ -e "$legacy_dir/battery" ]]; then - if [[ (("$(cat $legacy_dir/battery/voltage_now)" -gt "5" )) ]]; then - status_battery_text=" "$(awk '{print tolower($0)}' < $legacy_dir/battery/status) - read battery_percent <$legacy_dir/battery/capacity - fi - fi -} # batteryinfo - -function ambienttemp() { - # define where w1 usually shows up - W1_DIR="/sys/devices/w1_bus_master1/" - if [ -f /etc/armbianmonitor/datasources/ambienttemp ]; then - read raw_temp /dev/null - amb_temp=$(awk '{printf("%d",$1/1000)}' <<<${raw_temp}) - echo $amb_temp - elif [[ -d $W1_DIR && $ONE_WIRE == yes ]]; then - device=$(ls -1 $W1_DIR | grep -E '^[0-9]{1,4}' | head -1) - if [[ -n $device ]]; then - read raw_temp < ${W1_DIR}${device}/hwmon/$(ls -1 ${W1_DIR}${device}/hwmon)/temp1_input 2>/dev/null - amb_temp=$(awk '{printf("%d",$1/1000)}' <<<${raw_temp}) - echo $amb_temp - fi - else - # read ambient temperature from USB device if available - if [[ ! -f /usr/bin/temper ]]; then - echo "" - return - fi - amb_temp=$(temper -c 2>/dev/null) - case ${amb_temp} in - *"find the USB device"*) - echo "" - ;; - *) - amb_temp=$(awk '{print $NF}' <<<$amb_temp | sed 's/C//g') - echo -n "scale=1;${amb_temp}/1" | grep -oE "\-?[[:digit:]]+\.[[:digit:]]" - esac - fi -} # ambienttemp - function get_ip_addresses() { local ips=() for f in /sys/class/net/*; do @@ -164,8 +87,6 @@ function storage_info() { fi } # storage_info - - # query various systems and send some stuff to the background for overall faster execution. # Works only with ambienttemp and batteryinfo since A20 is slow enough :) amb_temp=$(ambienttemp &) diff --git a/packages/bsp/common/usr/lib/armbian/armbian-allwinner-battery b/packages/bsp/common/usr/lib/armbian/armbian-allwinner-battery new file mode 100755 index 0000000000..5d18d017fb --- /dev/null +++ b/packages/bsp/common/usr/lib/armbian/armbian-allwinner-battery @@ -0,0 +1,92 @@ +#!/bin/bash +# +# SPDX-License-Identifier: GPL-2.0-only +# +# Copyright (c) Authors: https://www.armbian.com/authors +# +# This file is licensed under the terms of the GNU General Public +# License version 2. This program is licensed "as is" without any +# warranty of any kind, whether express or implied. + +function getboardtemp() { + if [ -f /etc/armbianmonitor/datasources/soctemp ]; then + read raw_temp /dev/null + if [ ! -z $(echo "$raw_temp" | grep -o "^[1-9][0-9]*\.\?[0-9]*$") ] && (( $(echo "${raw_temp} < 200" |bc -l) )); then + # Allwinner legacy kernels output degree C + board_temp=${raw_temp} + else + board_temp=$(awk '{printf("%d",$1/1000)}' <<<${raw_temp}) + fi + elif [ -f /etc/armbianmonitor/datasources/pmictemp ]; then + # fallback to PMIC temperature + board_temp=$(awk '{printf("%d",$1/1000)}' /dev/null + if [[ "$status_battery_connected" == "1" ]]; then + read status_battery_charging < $mainline_dir/charger/charging + read status_ac_connect < $mainline_dir/ac/connected + read battery_percent< $mainline_dir/battery/capacity + # dispay charging / percentage + if [[ "$status_ac_connect" == "1" && "$battery_percent" -lt "100" ]]; then + status_battery_text=" charging" + elif [[ "$status_ac_connect" == "1" && "$battery_percent" -eq "100" ]]; then + status_battery_text=" charged" + else + status_battery_text=" discharging" + fi + fi + elif [[ -e "$legacy_dir/axp813-ac" ]]; then + read status_battery_connected < $legacy_dir/axp20x-battery/present + if [[ "$status_battery_connected" == "1" ]]; then + status_battery_text=" "$(awk '{print tolower($0)}' < $legacy_dir/axp20x-battery/status) + read status_ac_connect < $legacy_dir/axp813-ac/present + read battery_percent< $legacy_dir/axp20x-battery/capacity + fi + elif [[ -e "$legacy_dir/battery" ]]; then + if [[ (("$(cat $legacy_dir/battery/voltage_now)" -gt "5" )) ]]; then + status_battery_text=" "$(awk '{print tolower($0)}' < $legacy_dir/battery/status) + read battery_percent <$legacy_dir/battery/capacity + fi + fi +} # batteryinfo + +function ambienttemp() { + # define where w1 usually shows up + W1_DIR="/sys/devices/w1_bus_master1/" + if [ -f /etc/armbianmonitor/datasources/ambienttemp ]; then + read raw_temp /dev/null + amb_temp=$(awk '{printf("%d",$1/1000)}' <<<${raw_temp}) + echo $amb_temp + elif [[ -d $W1_DIR && $ONE_WIRE == yes ]]; then + device=$(ls -1 $W1_DIR | grep -E '^[0-9]{1,4}' | head -1) + if [[ -n $device ]]; then + read raw_temp < ${W1_DIR}${device}/hwmon/$(ls -1 ${W1_DIR}${device}/hwmon)/temp1_input 2>/dev/null + amb_temp=$(awk '{printf("%d",$1/1000)}' <<<${raw_temp}) + echo $amb_temp + fi + else + # read ambient temperature from USB device if available + if [[ ! -f /usr/bin/temper ]]; then + echo "" + return + fi + amb_temp=$(temper -c 2>/dev/null) + case ${amb_temp} in + *"find the USB device"*) + echo "" + ;; + *) + amb_temp=$(awk '{print $NF}' <<<$amb_temp | sed 's/C//g') + echo -n "scale=1;${amb_temp}/1" | grep -oE "\-?[[:digit:]]+\.[[:digit:]]" + esac + fi +} # ambienttemp diff --git a/packages/bsp/common/usr/lib/armbian/armbian-check-battery-shutdown b/packages/bsp/common/usr/lib/armbian/armbian-check-battery-shutdown new file mode 100755 index 0000000000..acfcd1f67a --- /dev/null +++ b/packages/bsp/common/usr/lib/armbian/armbian-check-battery-shutdown @@ -0,0 +1,29 @@ +#!/bin/bash +set -o nounset +set -o errexit +set -o pipefail +shopt -s dotglob + +# SPDX-License-Identifier: MIT + +# This script should be executed every 5 minutes by a cron job `/etc/cron.d/armbian-check-battery`. +# It checks if battery is discharging and battery level is more than 10%. If less, then start a system shutdown. +# Script uses `batteryinfo` function from `30-armbian-sysinfo` file of Armbian distribution. + +BATTERY_PERCENT_MIN='10' + +# Include functions: +# getboardtemp() +# batteryinfo() +# ambienttemp() +source /usr/lib/armbian/armbian-allwinner-battery +batteryinfo + +# `status_battery_text` has a leading whitespace +if [ "$status_battery_connected" == '1' ] && [[ "$status_battery_text" =~ [[:space:]]*discharging ]]; then + # When no battery connected, variable `battery_percent` is not defined! + if [ "$battery_percent" -lt "$BATTERY_PERCENT_MIN" ]; then + logger --tag cron_check_battery_shutdown "battery_percent = $battery_percent, running shutdown" + shutdown -h +1 + fi +fi diff --git a/packages/bsp/common/usr/lib/armbian/armbian-common b/packages/bsp/common/usr/lib/armbian/armbian-common index 107b77bff4..2f1eb72943 100755 --- a/packages/bsp/common/usr/lib/armbian/armbian-common +++ b/packages/bsp/common/usr/lib/armbian/armbian-common @@ -31,7 +31,7 @@ MACADDR=$(printf ${prefixes[$random]}':%02X:%02X:%02X:%02X:%02X\n' $[RANDOM%256] } -# set fixed IP address from first randomly assigned one. If nothing is deteceted, generate one. +# set fixed IP address from first randomly assigned one. If nothing is detected, generate one. set_fixed_mac () { if [ -n "$(command -v nmcli)" ]; then @@ -47,10 +47,10 @@ set_fixed_mac () [[ -z $MACADDR ]] && get_random_mac if [[ -n "$(command -v nmcli)" && -n $UUID ]]; then - nmcli connection modify $UUID ethernet.cloned-mac-address $MACADDR - nmcli connection modify $UUID -ethernet.mac-address "" - nmcli connection down $UUID >/dev/null 2>&1 - nmcli connection up $UUID >/dev/null 2>&1 + nmcli connection modify "$UUID" ethernet.cloned-mac-address "$MACADDR" + nmcli connection modify "$UUID" -ethernet.mac-address "" + nmcli connection down "$UUID" >/dev/null 2>&1 + nmcli connection up "$UUID" >/dev/null 2>&1 elif [[ -f /etc/systemd/network/$DEVNAME.network ]]; then if ! grep '^ *MACAddress=' /etc/systemd/network/$DEVNAME.network > /dev/null; then sed -i "s/#MACAddress=/MACAddress=$MACADDR/g" /etc/systemd/network/$DEVNAME.network