Mystro256 292d5f4
commit 9498a85d1cedb78a38b54f243c9c4a3daad5bcc6
Mystro256 292d5f4
Author: Thomas Koch <linrunner@gmx.net>
Mystro256 292d5f4
Date:   Tue Feb 28 20:33:44 2017 +0100
Mystro256 292d5f4
Mystro256 292d5f4
    tlp-stat: intercept non-existing or invalid charge values
Mystro256 292d5f4
    
Mystro256 292d5f4
    Reference:
Mystro256 292d5f4
    * Issue #256: https://github.com/linrunner/TLP/issues/256
Mystro256 292d5f4
Mystro256 8210c43
diff --git a/tlp-stat.in b/tlp-stat.in
Mystro256 292d5f4
index ca08527..8aeb6a6 100644
Mystro256 8210c43
--- a/tlp-stat.in
Mystro256 8210c43
+++ b/tlp-stat.in
Mystro256 8210c43
@@ -58,6 +58,15 @@ no_runtimepm=0
Mystro256 8210c43
 
Mystro256 8210c43
 # --- Functions
Mystro256 8210c43
 
Mystro256 8210c43
+catparm () { # echo contents of sysfile or default if non-existent or unreadable
Mystro256 8210c43
+    # $1: sysfile, $2: default value
Mystro256 8210c43
+    local val="$(cat $1 2> /dev/null)"
Mystro256 8210c43
+    [ -n "$val" ] || val="$2" # sysfile nonexistent or read failed
Mystro256 8210c43
+
Mystro256 8210c43
+    printf "%s" $val
Mystro256 8210c43
+    return 0
Mystro256 8210c43
+}
Mystro256 8210c43
+
Mystro256 8210c43
 printparm () { # formatted output of sysfile - general
Mystro256 8210c43
     # $1: format, $2: sysfile, $3: namsg, $4: cutoff
Mystro256 8210c43
     local format="$1"
Mystro256 292d5f4
@@ -257,7 +266,7 @@ check_ata_errors () { # check kernel log for ata errors
Mystro256 292d5f4
         # count matching error lines
Mystro256 292d5f4
         echo $( dmesg | egrep -c "${RE_ATA_ERROR}" 2> /dev/null )
Mystro256 292d5f4
     else
Mystro256 292d5f4
-        # no values in question configured 
Mystro256 292d5f4
+        # no values in question configured
Mystro256 292d5f4
         echo "0"
Mystro256 292d5f4
     fi
Mystro256 292d5f4
 
Mystro256 8210c43
@@ -1115,9 +1124,9 @@ if [ "$show_bat" = "1" ] || [ "$show_all" = "1" ]; then
Mystro256 8210c43
                     echo
Mystro256 8210c43
 
Mystro256 8210c43
                     # store values for charge / capacity calculation below
Mystro256 8210c43
-                    ed=$(cat $batd/design_capacity)
Mystro256 8210c43
-                    ef=$(cat $batd/last_full_capacity)
Mystro256 8210c43
-                    en=$(cat $batd/remaining_capacity)
Mystro256 8210c43
+                    ed=$(catparm $batd/design_capacity 0)
Mystro256 8210c43
+                    ef=$(catparm $batd/last_full_capacity 0)
Mystro256 8210c43
+                    en=$(catparm $batd/remaining_capacity 0)
Mystro256 8210c43
 
Mystro256 8210c43
                     # show charge + capacity
Mystro256 8210c43
                     lcnt=0
Mystro256 8210c43
@@ -1184,9 +1193,9 @@ if [ "$show_bat" = "1" ] || [ "$show_all" = "1" ]; then
Mystro256 8210c43
                     printparm "%-59s = ##%6d## [mW]" $batd/power_now "" 000
Mystro256 8210c43
 
Mystro256 8210c43
                     # store values for charge / capacity calculation below
Mystro256 8210c43
-                    ed=$(cat $batd/energy_full_design)
Mystro256 8210c43
-                    ef=$(cat $batd/energy_full)
Mystro256 8210c43
-                    en=$(cat $batd/energy_now)
Mystro256 8210c43
+                    ed=$(catparm $batd/energy_full_design 0)
Mystro256 8210c43
+                    ef=$(catparm $batd/energy_full 0)
Mystro256 8210c43
+                    en=$(catparm $batd/energy_now 0)
Mystro256 8210c43
 
Mystro256 8210c43
                 elif [ -f $batd/charge_full ]; then
Mystro256 8210c43
                     printparm "%-59s = ##%6d## [mAh]" $batd/charge_full_design "" 000
Mystro256 8210c43
@@ -1195,9 +1204,15 @@ if [ "$show_bat" = "1" ] || [ "$show_all" = "1" ]; then
Mystro256 8210c43
                     printparm "%-59s = ##%6d## [mA]" $batd/current_now "" 000
Mystro256 8210c43
 
Mystro256 8210c43
                     # store values for charge / capacity calculation below
Mystro256 8210c43
-                    ed=$(cat $batd/charge_full_design)
Mystro256 8210c43
-                    ef=$(cat $batd/charge_full)
Mystro256 8210c43
-                    en=$(cat $batd/charge_now)
Mystro256 8210c43
+                    ed=$(catparm $batd/charge_full_design 0)
Mystro256 8210c43
+                    ef=$(catparm $batd/charge_full 0)
Mystro256 8210c43
+                    en=$(catparm $batd/charge_now 0)
Mystro256 8210c43
+
Mystro256 8210c43
+                else
Mystro256 8210c43
+                    ed=0
Mystro256 8210c43
+                    ef=0
Mystro256 8210c43
+                    en=0
Mystro256 8210c43
+
Mystro256 8210c43
                 fi
Mystro256 8210c43
                 if [ $tpbat -eq 1 ]; then
Mystro256 8210c43
                     print_tp_batstate $batd/status