summaryrefslogtreecommitdiff
path: root/util/battery_temp
diff options
context:
space:
mode:
authorTodd Broch <tbroch@chromium.org>2018-03-27 23:07:59 -0700
committerchrome-bot <chrome-bot@chromium.org>2018-03-29 19:51:40 -0700
commite0b7137f47bb6902a51290c97fabeadf21276d6c (patch)
treefb3b2ea8baaf727ff51861e8e3400015f4790e67 /util/battery_temp
parent415dba09fb0b63edd0d25baca3dfc3e9982625ab (diff)
downloadchrome-ec-e0b7137f47bb6902a51290c97fabeadf21276d6c.tar.gz
battery_temp: initial commit.
Simple script to read battery temperature and return degrees Celsius if readable, 'error' if unreadable or 'unknown' otherwise. Signed-off-by: Todd Broch <tbroch@chromium.org> BRANCH=none BUG=chromium:816744 TEST=manual, run on following duts with return values of: elm: <degC> eve: <degC> expresso: <degC> heli: <degC> peppy: 'unknown' samus: <degC> squawks: <degC> | 'error' if battery removed. Change-Id: I3147ceb3ea0e0a22c08617e212c66d0c3e58b300 Reviewed-on: https://chromium-review.googlesource.com/982815 Commit-Ready: Todd Broch <tbroch@chromium.org> Tested-by: Todd Broch <tbroch@chromium.org> Reviewed-by: Duncan Laurie <dlaurie@google.com>
Diffstat (limited to 'util/battery_temp')
-rwxr-xr-xutil/battery_temp56
1 files changed, 56 insertions, 0 deletions
diff --git a/util/battery_temp b/util/battery_temp
new file mode 100755
index 0000000000..c69e3d4778
--- /dev/null
+++ b/util/battery_temp
@@ -0,0 +1,56 @@
+#!/bin/bash
+# Copyright 2018 The Chromium OS Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+#
+# Description: Read and output temperature of device's primary battery in
+# degrees Celsius.
+#
+# TODO(tbroch) revisit for detachables with multiple batteries.
+
+# Read battery temperature from sysfs power_supply and return in degC.
+batt_temp_sysfs() {
+ local temp=""
+
+ for psdir in /sys/class/power_supply/* ; do
+ if [[ -e "${psdir}/temp" ]] ; then
+ pstype=$(cat $psdir/type)
+ if [[ "${pstype}" -eq "Battery" ]] ; then
+ temp=$(bc <<< "scale=2; $(cat ${psdir}/temp)/10")
+ break
+ fi
+ fi
+ done
+ echo ${temp}
+}
+
+# Read battery temperature from EC and return in degC.
+batt_temp_ec() {
+ local temp=""
+
+ local sensor_str=$(ectool tempsinfo all 2>/dev/null | grep Battery)
+ if [[ $? -eq 0 ]] && [[ ! -z "${sensor_str}" ]] ; then
+ local idx=$(echo ${sensor_str} | cut -d: -f1)
+ # ectool temps <idx> looks like 'Reading temperature...298 K'
+ temp_str=$(ectool temps ${idx})
+ temp="${temp_str//[!0-9]/}"
+ if [[ -z "${temp}" ]] ; then
+ temp="error"
+ else
+ temp=$(bc <<< "scale=2; ${temp} - 273.15")
+ fi
+ fi
+ echo $temp
+}
+
+# Main
+TEMP_DEGC=$(batt_temp_sysfs)
+if [[ -z "${TEMP_DEGC}" ]] ; then
+ TEMP_DEGC=$(batt_temp_ec)
+fi
+
+if [[ -z "${TEMP_DEGC}" ]] ; then
+ echo "unknown"
+else
+ echo ${TEMP_DEGC}
+fi