]> git.corax.cc Git - toolbox-linux/commitdiff
include/{acpi,net}: Add modules from toolbox repository
authorMatthias Kruk <m@m10k.eu>
Mon, 29 Nov 2021 02:05:09 +0000 (11:05 +0900)
committerMatthias Kruk <m@m10k.eu>
Mon, 29 Nov 2021 02:05:09 +0000 (11:05 +0900)
This commit adds the acpi/ac, acpi/battery, and net/iface modules
that have been removed from the main toolbox source tree.

include/acpi/ac.sh [new file with mode: 0644]
include/acpi/battery.sh [new file with mode: 0644]
include/net/iface.sh [new file with mode: 0644]

diff --git a/include/acpi/ac.sh b/include/acpi/ac.sh
new file mode 100644 (file)
index 0000000..c1da36e
--- /dev/null
@@ -0,0 +1,19 @@
+#!/bin/bash
+
+__init() {
+       declare -xgr __acpi_ac_path="/sys/class/power_supply"
+
+       return 0
+}
+
+acpi_ac_get_state() {
+       local psu
+
+       psu="$1"
+
+       if ! cat "$__acpi_ac_path/$psu/online" 2>/dev/null; then
+               return 1
+       fi
+
+       return 0
+}
diff --git a/include/acpi/battery.sh b/include/acpi/battery.sh
new file mode 100644 (file)
index 0000000..4a39e0c
--- /dev/null
@@ -0,0 +1,66 @@
+#!/bin/bash
+
+__init() {
+       declare -xgr __acpi_battery_path="/sys/class/power_supply"
+
+       return 0
+}
+
+acpi_battery_get_charge_full() {
+       local battery
+
+       battery="$1"
+
+       if cat "$__acpi_battery_path/$battery/charge_full" 2>/dev/null; then
+               return 0
+       fi
+
+       if cat "$__acpi_battery_path/$battery/energy_full" 2>/dev/null; then
+               return 0
+       fi
+
+       return 1
+}
+
+acpi_battery_get_charge_now() {
+       local battery
+
+       battery="$1"
+
+       if cat "$__acpi_battery_path/$battery/charge_now" 2>/dev/null; then
+               return 0
+       fi
+
+       if cat "$__acpi_battery_path/$battery/energy_now" 2>/dev/null; then
+               return 0
+       fi
+
+       return 1
+}
+
+acpi_battery_get_level() {
+       local battery
+
+        local full
+       local now
+       local lvl
+
+       battery="$1"
+
+       if ! full=$(acpi_battery_get_charge_full "$battery"); then
+               return 1
+       fi
+
+       if ! now=$(acpi_battery_get_charge_now "$battery"); then
+               return 1
+       fi
+
+       if (( full == 0 )); then
+               return 1
+       fi
+
+       lvl=$((now * 100 / full))
+       echo "$lvl"
+
+       return 0
+}
diff --git a/include/net/iface.sh b/include/net/iface.sh
new file mode 100644 (file)
index 0000000..fb7f07b
--- /dev/null
@@ -0,0 +1,156 @@
+#!/bin/bash
+
+__init() {
+       return 0
+}
+
+net_iface_exists() {
+       local iface
+
+       iface="$1"
+
+       if grep -F "$iface" < /proc/net/dev &> /dev/null; then
+               return 0
+       fi
+
+       return 1
+}
+
+net_iface_get_state() {
+       local iface
+       local state
+
+       iface="$1"
+
+       if ! state=$(ip link show "$iface" 2>/dev/null); then
+               return 1
+       fi
+
+       if ! state=$(echo "$state" | grep -oP 'state[ ]+\K[^ ]+'); then
+               return 1
+       fi
+
+       case "$state" in
+               "UP")
+                       echo "1"
+                       ;;
+
+               "DOWN")
+                       echo "0"
+                       ;;
+
+               *)
+                       return 1
+                       ;;
+       esac
+
+       return 0
+}
+
+net_iface_set_state() {
+       local iface
+       local state
+
+       iface="$1"
+       state="$2"
+
+       case "$state" in
+               "0")
+                       state="down"
+                       ;;
+
+               "1")
+                       state="up"
+                       ;;
+
+               *)
+                       return 1
+                       ;;
+       esac
+
+       if ! /sbin/ifconfig "$iface" "$state" &>/dev/null; then
+               return 1
+       fi
+
+       return 0
+}
+
+net_iface_get_address() {
+       local iface
+       local proto
+
+        local addr_all
+
+        iface="$1"
+        proto="$2"
+
+        if ! addr_all=$(ip address show dev "$iface"); then
+                return 1
+        fi
+
+        if ! echo "$addr_all" | grep -oP "${proto}[ \\t]+\\K[^ ]+"; then
+                return 1
+        fi
+
+        return 0
+}
+
+net_iface_get_essid() {
+        local iface
+        local addr_all
+
+        iface="$1"
+
+        if ! addr_all=$(/sbin/iwconfig 2>&1); then
+                return 1
+        fi
+
+        if ! echo "$addr_all" | grep -oP "$iface.*ESSID:\"\\K[^\"]+"; then
+                return 1
+        fi
+
+        return 0
+}
+
+_net_iface_parse_iwlist() {
+       local regex_ssid='Cell [0-9]+ - Address: ([0-9A-Fa-f:]+)'
+       local regex_signal='Quality=([0-9/]+)'
+       local regex_essid='ESSID:"(.*)"'
+
+        local line
+        local ssid
+        local essid
+        local strength
+
+        while read -r line; do
+                if [[ "$line" =~ $regex_ssid ]]; then
+                        #start of a new network
+                        ssid="${BASH_REMATCH[1]}"
+                        essid=""
+                        strength=""
+                elif [[ "$line" =~ $regex_signal ]]; then
+                        strength="${BASH_REMATCH[1]}"
+                elif [[ "$line" =~ $regex_essid ]]; then
+                        essid="${BASH_REMATCH[1]}"
+                fi
+
+                if [ -n "$ssid" ] && [ -n "$essid" ] && [ -n "$strength" ]; then
+                        echo "$ssid $strength $essid"
+                       ssid=""
+                fi
+        done
+}
+
+net_iface_scan() {
+       local iface
+       local raw
+
+       iface="$1"
+
+       if ! raw=$(iwlist "$iface" scan 2>&1); then
+               return 1
+       fi
+
+       echo "$raw" | _iface_parse_iwlist
+       return 0
+}