summaryrefslogtreecommitdiffstats
path: root/tools/testing/selftests/power_supply/helpers.sh
blob: 1ec90d7c91082e109f3b3c5ef07bf033f7a55f29 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#!/bin/sh
# SPDX-License-Identifier: GPL-2.0
#
# Copyright (c) 2022, 2024 Collabora Ltd
SYSFS_SUPPLIES=/sys/class/power_supply

calc() {
	awk "BEGIN { print $* }";
}

test_sysfs_prop() {
	PROP="$1"
	VALUE="$2" # optional

	PROP_PATH="$SYSFS_SUPPLIES"/"$DEVNAME"/"$PROP"
	TEST_NAME="$DEVNAME".sysfs."$PROP"

	if [ -z "$VALUE" ]; then
		ktap_test_result "$TEST_NAME" [ -f "$PROP_PATH" ]
	else
		ktap_test_result "$TEST_NAME" grep -q "$VALUE" "$PROP_PATH"
	fi
}

to_human_readable_unit() {
	VALUE="$1"
	UNIT="$2"

	case "$VALUE" in
		*[!0-9]* ) return ;; # Not a number
	esac

	if [ "$UNIT" = "uA" ]; then
		new_unit="mA"
		div=1000
	elif [ "$UNIT" = "uV" ]; then
		new_unit="V"
		div=1000000
	elif [ "$UNIT" = "uAh" ]; then
		new_unit="Ah"
		div=1000000
	elif [ "$UNIT" = "uW" ]; then
		new_unit="mW"
		div=1000
	elif [ "$UNIT" = "uWh" ]; then
		new_unit="Wh"
		div=1000000
	else
		return
	fi

	value_converted=$(calc "$VALUE"/"$div")
	echo "$value_converted" "$new_unit"
}

_check_sysfs_prop_available() {
	PROP=$1

	PROP_PATH="$SYSFS_SUPPLIES"/"$DEVNAME"/"$PROP"
	TEST_NAME="$DEVNAME".sysfs."$PROP"

	if [ ! -e "$PROP_PATH" ] ; then
		ktap_test_skip "$TEST_NAME"
		return 1
	fi

	if ! cat "$PROP_PATH" >/dev/null; then
		ktap_print_msg "Failed to read"
		ktap_test_fail "$TEST_NAME"
		return 1
	fi

	return 0
}

test_sysfs_prop_optional() {
	PROP=$1
	UNIT=$2 # optional

	TEST_NAME="$DEVNAME".sysfs."$PROP"

	_check_sysfs_prop_available "$PROP" || return
	DATA=$(cat "$SYSFS_SUPPLIES"/"$DEVNAME"/"$PROP")

	ktap_print_msg "Reported: '$DATA' $UNIT ($(to_human_readable_unit "$DATA" "$UNIT"))"
	ktap_test_pass "$TEST_NAME"
}

test_sysfs_prop_optional_range() {
	PROP=$1
	MIN=$2
	MAX=$3
	UNIT=$4 # optional

	TEST_NAME="$DEVNAME".sysfs."$PROP"

	_check_sysfs_prop_available "$PROP" || return
	DATA=$(cat "$SYSFS_SUPPLIES"/"$DEVNAME"/"$PROP")

	if [ "$DATA" -lt "$MIN" ] || [ "$DATA" -gt "$MAX" ]; then
		ktap_print_msg "'$DATA' is out of range (min=$MIN, max=$MAX)"
		ktap_test_fail "$TEST_NAME"
	else
		ktap_print_msg "Reported: '$DATA' $UNIT ($(to_human_readable_unit "$DATA" "$UNIT"))"
		ktap_test_pass "$TEST_NAME"
	fi
}

test_sysfs_prop_optional_list() {
	PROP=$1
	LIST=$2

	TEST_NAME="$DEVNAME".sysfs."$PROP"

	_check_sysfs_prop_available "$PROP" || return
	DATA=$(cat "$SYSFS_SUPPLIES"/"$DEVNAME"/"$PROP")

	valid=0

	OLDIFS=$IFS
	IFS=","
	for item in $LIST; do
		if [ "$DATA" = "$item" ]; then
			valid=1
			break
		fi
	done
	if [ "$valid" -eq 1 ]; then
		ktap_print_msg "Reported: '$DATA'"
		ktap_test_pass "$TEST_NAME"
	else
		ktap_print_msg "'$DATA' is not a valid value for this property"
		ktap_test_fail "$TEST_NAME"
	fi
	IFS=$OLDIFS
}

dump_file() {
	FILE="$1"
	while read -r line; do
		ktap_print_msg "$line"
	done < "$FILE"
}

__test_uevent_prop() {
	PROP="$1"
	OPTIONAL="$2"
	VALUE="$3" # optional

	UEVENT_PATH="$SYSFS_SUPPLIES"/"$DEVNAME"/uevent
	TEST_NAME="$DEVNAME".uevent."$PROP"

	if ! grep -q "POWER_SUPPLY_$PROP=" "$UEVENT_PATH"; then
		if [ "$OPTIONAL" -eq 1 ]; then
			ktap_test_skip "$TEST_NAME"
		else
			ktap_print_msg "Missing property"
			ktap_test_fail "$TEST_NAME"
		fi
		return
	fi

	if ! grep -q "POWER_SUPPLY_$PROP=$VALUE" "$UEVENT_PATH"; then
		ktap_print_msg "Invalid value for uevent property, dumping..."
		dump_file "$UEVENT_PATH"
		ktap_test_fail "$TEST_NAME"
	else
		ktap_test_pass "$TEST_NAME"
	fi
}

test_uevent_prop() {
	__test_uevent_prop "$1" 0 "$2"
}

test_uevent_prop_optional() {
	__test_uevent_prop "$1" 1 "$2"
}