-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcommon.sh
executable file
·1252 lines (1125 loc) · 26.9 KB
/
common.sh
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
dir=$(dirname $(pwd)/$0)
getval=${dir}/../getval
__verbose=0
adapter_path()
{
echo "/sys/class/i2c-dev/i2c-$1/device"
}
pr_err()
{
echo $* >&2
}
pr_verbose()
{
if [[ "${__verbose}" -ge "$1" ]]; then
pr_err "$2"
fi
}
install_regs ()
{
local adapter=$1
local i2c_addr=$(echo $2 | sed -e 's/0x//')
local regs=("${!3}")
local size=$4
local i
i=0
while [ $i -lt ${#regs[*]} ]
do
i2cset -f -y ${adapter} 0x${i2c_addr} $i 0x${regs[$i]} ${size}
i=$((i + 1))
done
}
containsElement()
{
local e
for e in "${@:2}"
do
if [ "$e" = "$1" ]
then
return 0
fi
done
return 1
}
dotest ()
{
local permissive=0
if [[ $1 = "-p" ]]; then
permissive=1
shift
fi
local quiet=0
if [[ $1 = "-q" ]]; then
quiet=1
shift
fi
local a=("${!1}")
local v=("${!2}")
local p=("${!3}")
local f
local known=("device" "driver" "hwmon" "modalias" "power" "subsystem" "uevent")
local rv=0
local perm
local val
local i
for f in $(ls); do
if ! containsElement "$f" "${known[@]}"; then
if ! containsElement "$f" "${a[@]}"; then
pr_err "Unexpected attribute \"$f\", value=\"$(cat $f)\""
rv=$((rv + 1))
fi
fi
done
i=0
while [ $i -lt ${#a[*]} ]
do
if [ ! -e ${a[$i]} ]
then
pr_err "${a[$i]}: Attribute not found"
rv=$((rv + 1))
i=$((i + 1))
continue
fi
# Don't try to read the attribute if it is write-only
perm="$(ls -l "${a[$i]}" | cut -f1 -d' ')"
# we can not use "test -r" because that does not work for root
if [[ "${perm}" != "--w-------" ]]; then
val=$(cat "${a[$i]}")
if [[ "${val}" != "${v[$i]}" ]]; then
if [[ ${permissive} -eq 0 || ${a[$i]%_input} = ${a[$i]} ]];
then
pr_err "${a[$i]}: bad value ${val}, expected ${v[$i]}"
rv=$((rv + 1))
elif [[ ${permissive} -eq 1 && ${quiet} -eq 0 ]]; then
echo "Note: ${a[$i]}: value difference: reported ${val}, expected ${v[$i]}"
fi
fi
fi
if [ -n "${p[$i]}" ]
then
if [ "${perm}" != "${p[$i]}" ]
then
pr_err "${a[$i]}: bad permissions: ${perm}, expected ${p[$i]}"
rv=$((rv + 1))
fi
fi
i=$((i + 1))
done
return ${rv}
}
ecode=-19
error_test()
{
local i=0
local rv=0
local rc
local adapter=$1
local a=("${!2}")
local val
local efile="$(adapter_path ${adapter})/error"
# If i2c-stub doesn't support error code insertion,
# there is nothing we can do.
if [ ! -e ${efile} ]
then
return 0
fi
echo ${ecode} > ${efile}
while [ $i -lt ${#a[*]} ]
do
val=$(${getval} ${a[$i]})
rc=$?
if [ ${rc} -eq 0 ]
then
pr_err ${a[$i]} returned no error
rv=1
else
if [ ${val} -ne ${ecode} ]
then
pr_err ${a[$i]} returned ${val}, expected ${ecode}
rv=1
fi
fi
i=$((i + 1))
done
echo 0 > ${efile}
return ${rv}
}
# New API: Write basedir to global variable 'basedir', exit on error
getbasedir()
{
local dev
local addr="00$(echo $1 | sed -e 's/0x//')"
dev="$(ls $(adapter_path ${i2c_adapter})/${i2c_adapter}-${addr}/hwmon 2>/dev/null)"
if [ "${dev}" = "" ]
then
# Give it a second, then retry
sleep 1
dev="$(ls $(adapter_path ${i2c_adapter})/${i2c_adapter}-${addr}/hwmon 2>/dev/null)"
if [ "${dev}" = "" ]
then
pr_err "hwmon device entry not found"
exit 1
fi
fi
if [ -e "/sys/class/hwmon/${dev}/name" ]; then
basedir="/sys/class/hwmon/${dev}"
else
basedir="/sys/class/hwmon/${dev}/device"
fi
if [ ! -d "${basedir}" ]; then
pr_err "Directory ${basedir} does not exist"
exit 1
fi
}
# Legacy API: Print basedir to stdout
# Caller has to validate return value (getbasedir will exit on error)
getbase()
{
i2c_adapter=$1
getbasedir $(echo $2 | sed -e 's/00//')
echo ${basedir}
return 0
}
DEFAULT_MIN=-100000001
DEFAULT_MAX=100000001
UNDERFLOW_MIN=(-2147483648 # 0x80000000
-9223372036854775808 # 0x8000000000000000
-1
0
)
OVERFLOW_MAX=(2147483647 # 0x7fffffff
2147483648 # 0x80000000
4294967295 # 0xffffffff
4294967296 # 0x100000000
4294967296001 # (0x100000000 * 1000) + 1
4611686018427387904 # 0x4000000000000000
9223372036854775807 # 0x7fffffffffffffff
9223372036854775808 # 0x8000000000000000
18446744073709551615 # 0xffffffffffffffff
)
fixup_writeattr()
{
:
}
writeattr()
{
local attr="$1"
local value="$2"
local rv
echo "${value}" > "${attr}" 2>/dev/null
if [[ $? -ne 0 ]]; then
return 1
fi
fixup_writeattr "${attr}" "${value}"
return $?
}
underflow_check_val()
{
local attr=$1
local underflow=$2
local min=$3
local waittime=$4
local omin
# Make sure that the write doesn't fail because the value
# written is too low to be accepted by the infrastructure.
# Keep trying with higher values until the write succeeds or
# the underflow is 0.
while :; do
if writeattr ${attr} ${underflow}; then
break
fi
if [ ${underflow} -ge ${min} ]; then
break
fi
if [ ${underflow} -eq 0 ]; then
break
fi
underflow=$((underflow / 2))
done
# If the written value is larger than the expected minimum, we can't
# be sure if any observed differences are due to rounding or due to
# a real problem. Declare success in that case.
if [ ${underflow} -gt ${min} ]; then
return 0
fi
omin=$(cat ${attr})
if [ ${omin} -ne ${min} -a "${omin}" != "${underflow}" ]; then
pr_err "$(basename ${attr}): Suspected underflow: [min=${min}, read ${omin}, written ${underflow}]"
return 1
fi
if [ ${waittime} -ne 0 ]; then
sleep ${waittime}
omax=$(cat ${attr})
if [ ${omin} -ne ${min} ]; then
pr_err "$(basename ${attr}): Cache mismatch: [${min} vs. ${omin}]"
return 1
fi
fi
return 0
}
underflow_check()
{
local i=0
while [ $i -lt ${#UNDERFLOW_MIN[*]} ]; do
if ! underflow_check_val $1 ${UNDERFLOW_MIN[$i]} $2 $3; then
return 1
fi
i=$((i + 1))
done
return 0
}
overflow_check_val()
{
local attr=$1
local overflow=$2
local max=$3
local waittime=$4
# Make sure that the write doesn't fail because the value
# written is too high to be accepted by the infrastructure.
# Keep trying with lower values until the write succeeds or
# the overflow is 0.
while :
do
writeattr ${attr} ${overflow}
if [ $? -eq 0 ]
then
break
fi
if [ "${overflow}" = "9223372036854775808" ]
then
# Neither bash nor expr can handle this number
overflow=4611686018427387904
continue
fi
if [ "${overflow}" = "18446744073709551615" ]
then
# Neither bash nor expr can handle this number
overflow=9223372036854775807
continue
fi
if [ ${overflow} -le ${max} ]
then
break
fi
overflow=$((overflow / 2))
done
omax=$(cat ${attr})
if [ ${omax} -ne ${max} -a "${omax}" != "${overflow}" ]
then
pr_err "$(basename ${attr}): Suspected overflow: [max=${max}, read ${omax}, written ${overflow}]"
return 1
fi
if [ ${waittime} -ne 0 ]
then
sleep ${waittime}
omax=$(cat ${attr})
if [ ${omax} -ne ${max} ]
then
pr_err "$(basename ${attr}): Cache mismatch: [${max} vs. ${omax}]"
return 1
fi
fi
return 0
}
overflow_check()
{
local i=0
while [ $i -lt ${#OVERFLOW_MAX[*]} ]
do
overflow_check_val $1 ${OVERFLOW_MAX[$i]} $2 $3
if [ $? -ne 0 ]
then
return 1
fi
i=$((i + 1))
done
return 0
}
findmin()
{
local attr=$1
local min=-134217728
local found=$(cat ${attr})
local tmp
local written="none"
local val
# Try 0 and 1 as baseline
for val in 0 1; do
if writeattr "${attr}" "${val}"; then
tmp=$(cat ${attr})
if [ "${tmp}" -lt "${found}" ]; then
found="${tmp}"
written="${val}"
break
fi
fi
done
while [ ${min} -gt -9223372036854775808 ]
do
min=$((min * 2))
writeattr ${attr} ${min}
tmp=$(cat ${attr})
if [ ${tmp} -lt ${found} ]
then
found=${tmp}
written="${tmp}"
fi
done
pr_verbose 1 "$(basename "${attr}"): Found min ${found} when writing ${written}"
echo "${found}"
}
findmax()
{
local attr=$1
# local max=134217727 # 7FFFFFF
# local max=1048575 # FFFFF
local max=65535 # FFFF
local found=$(cat ${attr})
local tmp
local written="${found}"
while [ ${max} -lt 9223372036854775807 ] # 7FFFFFFFFFFFFFFF
do
max=$((max * 2))
max=$((max + 1))
writeattr ${attr} ${max}
tmp=$(cat ${attr})
if [ ${tmp} -gt ${found} ]
then
found="${tmp}"
written="${max}"
fi
# The following suggests an overflow. Stop looking.
if [ ${tmp} -lt ${found} ]
then
break
fi
done
pr_verbose 1 "$(basename "${attr}"): Found max ${found} when writing ${written}"
echo "${found}"
}
check_read_write_read()
{
local attr="$1"
local val="$2"
if [[ -z "${val}" ]]; then
val="$(cat ${attr})"
fi
writeattr "${attr}" "${val}"
if [[ $? -ne 0 ]]; then
pr_err "$(basename ${attr}): Failed to write ${val}"
else
local x=$(cat ${attr})
if [[ $x -ne ${val} ]]; then
pr_err "$(basename ${attr}): Failed read->write->read sequence: ${val} -> ${x}"
fi
fi
}
check_range()
{
local base="./"
local attr
local mdev=${DEFAULT_MAX}
local min=${DEFAULT_MIN}
local max=${DEFAULT_MAX}
local omax
local stepsize=0
local deviation=0
local OPTIND=1
local opt
local range
local i
local restore=0
local orig
local rv=0
local quiet=0
local waittime=0
local prev
local ignore=0
local silent=0
local range=""
__verbose=0
while getopts "R:Sb:d:il:qrs:u:vw:" opt
do
case ${opt} in
b) base=${OPTARG}/
;;
d) mdev=${OPTARG} # maximum permitted deviation
;;
i) ignore=1
;;
l) min=${OPTARG}
;;
q) quiet=1
;;
v) __verbose="$((__verbose + 1))"
quiet=0
;;
w) waittime=${OPTARG}
;;
r) restore=1
;;
s) stepsize=${OPTARG}
;;
S) silent=1
;;
u) max=${OPTARG}
;;
R) range="${OPTARG}"
;;
:) pr_err "Option ${OPTARG} requires an argument"
return 1
;;
esac
done
shift $((OPTIND - 1))
attr=${base}$1
if [ ! -e "${attr}" ]
then
if [ ${ignore} -eq 1 ]
then
return 0
fi
pr_err $(basename ${attr}): No such attribute
return 1
fi
orig=$(cat ${attr})
if [[ -z "${range}" ]]; then
if [ ${min} -eq ${DEFAULT_MIN} ]; then
min="$(findmin ${attr})"
if [ ${quiet} -eq 0 -a ${__verbose} -eq 0 ]; then
__verbose=1
fi
# Try to trigger an underflow
if ! underflow_check ${attr} ${min} ${waittime}; then
return 1
fi
check_read_write_read "${attr}" "${min}"
else
writeattr ${attr} $((min - 1))
if [ $? -eq 0 ]
then
pr_err "Out of range value accepted writing into $(basename ${attr}): val=$((min - 1)) min=${min}"
fi
fi
if [ ${max} -eq ${DEFAULT_MAX} ]
then
max="$(findmax ${attr})"
if [ ${quiet} -eq 0 -a ${__verbose} -eq 0 ]; then
__verbose=1
fi
# Try to trigger an overflow
if ! overflow_check ${attr} ${max} ${waittime}; then
return 1
fi
check_read_write_read "${attr}" "${max}"
else
writeattr ${attr} $((max + 1))
if [ $? -eq 0 ]
then
pr_err "Out of range value accepted writing into $(basename ${attr}): val=$((max + 1)) max=${max}"
fi
fi
if [[ "${min}" -eq "${max}" && "${silent}" -eq 0 ]]; then
# Not necessarily an error but let's report it.
pr_err "$(basename ${attr}): min [${min}] is equal to max [${max}]"
elif [[ "${max}" -lt "${min}" ]]; then
pr_err "$(basename ${attr}): max [${max}] must be larger or equal to min [${min}]"
writeattr ${attr} ${orig}
return 1
fi
pr_verbose 2 "range check attribute ${attr} (min ${min} max ${max} original ${orig})"
if [ -z "${stepsize}" -o ${stepsize} -le 0 ]
then
range=$((max - min))
stepsize=$((range / 100))
if [ ${stepsize} -lt 1 ]
then
stepsize=1
fi
fi
prev=${min}
for i in $(seq ${min} ${stepsize} ${max})
do
writeattr ${attr} ${i}
if [ $? -ne 0 ]
then
pr_err "failed to write ${i} into ${attr}"
return 1
fi
x=$(cat ${attr})
if [ $x -lt ${prev} ]
then
pr_err "$(basename ${attr}): suspected error: Decreased value $x when expecting at least ${prev}"
fi
prev=${x}
d=0
if [ $i -gt $x ]
then
d=$((i - x))
elif [ $i -lt $x ]
then
d=$((x - i))
fi
pr_verbose 2 "write $i read $x deviation $d"
if [ $d -gt ${deviation} ]
then
devi=$i
devv=$x
deviation=$d
fi
done
pr_verbose 1 "$(basename ${attr}): value range [${min},${max}], deviation ${deviation}"
if [ ${mdev} -lt ${deviation} ]; then
pr_err "$(basename "${attr}"): Deviation out of range [max ${mdev}, seen ${deviation} (val=${devv}) with ${devi}]"
rv=1
fi
else
pr_verbose 2 "range check attribute ${attr} (range \"${range}\" original ${orig})"
local reverse=0
for v in ${range}; do
if [[ "$v" = ":" ]]; then
reverse=1
continue
fi
if ! writeattr ${attr} ${v}; then
if [[ "${reverse}" -eq 0 ]]; then
pr_err "$(basename "${attr}"): failed to write ${v}"
rv=1
fi
continue
fi
if [[ "${reverse}" -eq 1 ]]; then
pr_err "$(basename "${attr}"): write ${v} expected to fail, but succeeded"
rv=1
continue
fi
x="$(cat ${attr})"
if [[ "$x" -ne "$v" ]]; then
pr_err "$(basename "${attr}"): Value error: Expected $i got $x"
rv=1
fi
done
fi
if [ ${restore} -ne 0 ]; then
if ! writeattr ${attr} ${orig}; then
pr_err "$(basename "${attr}"): Failed to write back original value ${orig} (got error)"
rv=1
else
x="$(cat ${attr})"
if [[ "$x" -ne "${orig}" ]]; then
pr_err "$(basename "${attr}"): Failed to write back original value ${orig} (got $x)"
rv=1
fi
fi
fi
return ${rv}
}
check_values ()
{
local attr=$1/$2
local v=("${!3}")
local f=("${!4}")
if [ ! -e ${attr} ]
then
pr_err ${attr} does not exist
return 1
fi
i=0
while [ $i -lt ${#v[*]} ]
do
writeattr ${attr} ${v[$i]}
if [ $? -ne 0 ]
then
pr_err $2: Did not accept write of ${v[$i]}
return 1
fi
val=$(cat ${attr})
if [ ${val} -ne ${v[$i]} ]
then
pr_err ${attr}: bad value ${val} expected ${v[$i]}
return 1
fi
i=$((i + 1))
done
i=0
while [ $i -lt ${#f[*]} ]
do
writeattr ${attr} ${f[$i]}
if [ $? -eq 0 ]
then
pr_err $2: Unexpected: write of ${f[$i]} succeeded
return 1
fi
i=$((i + 1))
done
return 0
}
check_alarms()
{
local i2c_adapter="$1"
local i2c_addr="$2"
local alarms=("${!3}")
local reg
local mask
local attr
local oattr
local i
local regval
local size="b"
local rv=0
i=0
while [ $i -lt ${#alarms[*]} ]
do
reg="${alarms[$i]}"
mask="${alarms[$((i + 1))]}"
attr="${alarms[$((i + 2))]}"
if [[ "${#mask}" -eq 6 ]]; then
size="w"
fi
regval="$(i2cget -y -f "${i2c_adapter}" "${i2c_addr}" "${reg}" "${size}")"
oattr="$(cat "${attr}")"
i2cset -y -f "${i2c_adapter}" "${i2c_addr}" "${reg}" "$((regval | mask))" "${size}"
if [[ "$(cat "${attr}")" -eq 0 ]]; then
echo "${attr}: Expected alarm not set"
rv="$((rv + 1))"
fi
i2cset -y -f "${i2c_adapter}" "${i2c_addr}" "${reg}" "$((regval & ~mask))" "${size}"
if [[ "$(cat "${attr}")" -ne 0 ]]; then
echo "${attr}: alarm set but not expected"
rv="$((rv + 1))"
fi
i2cset -y -f "${i2c_adapter}" "${i2c_addr}" "${reg}" "${regval}" "${size}"
if [[ "$(cat "${attr}")" -ne "${oattr}" ]]; then
echo "${attr}: original alarm value not restored"
rv="$((rv + 1))"
fi
i=$((i + 3))
done
return ${rv}
}
fixup_regwrite()
{
:
}
check_volatile()
{
local sleeptime=""
local val
local rval
local aval
local rv=0
local change_only=0
local OPTIND=1
local opt
while getopts "cs:" opt
do
case ${opt} in
c) change_only=1
;;
s) sleeptime="${OPTARG}"
;;
esac
done
shift $(($OPTIND - 1))
local i2c_adapter="$1"
local i2c_addr="${2//0x/}"
local reg=$3
local regsize=$4
local _regsize="${regsize//s/}"
local fromval=$5
local toval=$6
local stepsize=$7
local attribute=$8
local attr_startval="${9}"
local attr_stepsize="${10}"
local attrval="${attr_startval}"
for val in $(seq ${fromval} ${stepsize} ${toval}); do
rval="${val}"
if [[ "${regsize}" == "ws" ]]; then
# swap register write value if needed
rval=$((((val & 0xff) << 8) | ((val & 0xff00) >> 8)))
fi
i2cset -f -y "${i2c_adapter}" "0x${i2c_addr}" ${reg} ${rval} ${_regsize}
fixup_regwrite "${i2c_adapter}" "0x${i2c_addr}" ${reg} ${rval}
if [[ -n "${sleeptime}" ]]; then
sleep "${sleeptime}"
fi
aval="$(cat ${attribute})"
if [[ "${change_only}" -eq 0 ]]; then
if [[ "${aval}" != "${attrval}" ]]; then
echo "${attribute} error read ${aval} expected ${attrval}"
rv=1
fi
attrval="$((attrval + attr_stepsize))"
else
if [[ "${val}" -ne "${fromval}" && "${aval}" == "${attrval}" ]]; then
echo "${attribute}: value did not change"
rv=1
fi
attrval="${aval}"
fi
done
return ${rv}
}
# Check if file is writeable. We can not use '-w' since that always
# returns true when running as super-user
is_writeable()
{
if [[ ! -e "$1" ]]; then
return 1
fi
ls -l "$1" | grep -q -- "-rw-r--r--"
return $?
}
check_alarm()
{
local input=$1
local limit=$2
local hyst_index=$3
local alarm=$4
local offset=$5
local expect=$6
local val
local aflag
local rv=0
local index
local limit_val="$(cat ${limit})"
local hyst_base_attr
local hyst_attr
local hyst_val
if [[ -n "${hyst_index}" ]]; then
if is_writeable "temp${hyst_index}_crit_hyst"; then
hyst_base_attr="temp${hyst_index}_crit"
hyst_attr="temp${hyst_index}_crit_hyst"
else
hyst_base_attr="temp${hyst_index}_max"
hyst_attr="temp${hyst_index}_max_hyst"
fi
fi
val="$(cat ${input})"
echo "$((offset + val))" > "${limit}"
# echo "${input}: ${val} ${limit}: $((offset + val)) [$(cat ${limit})]"
if [[ -n "${hyst_index}" ]]; then
hyst_val="$(cat ${hyst_attr})"
hyst_base_val="$(cat ${hyst_base_attr})"
echo "$((offset + hyst_base_val - 1000))" > "${hyst_attr}"
# echo "${hyst_attr}: $((offset + hyst_base_val - 1000)) [$(cat ${hyst_attr})]"
fi
# wait up to one second for the alarm to clear/set
sleep 0.2
cat ${alarm} >/dev/null
sleep 0.2
for index in $(seq 1 10); do
aflag="$(cat ${alarm})"
if [[ "${aflag}" -eq "${expect}" ]]; then
break
fi
sleep 0.1
done
if [[ "${aflag}" -ne "${expect}" ]]; then
echo "${alarm} is ${aflag}, expected ${expect} (${input}=$(cat ${input}), ${limit}=$(cat ${limit}))"
if [[ -e "${hyst_base_attr}" ]]; then
echo " ${hyst_base_attr}: $(cat ${hyst_base_attr}) ${hyst_attr}: $(cat ${hyst_attr})"
fi
rv=1
fi
echo "${limit_val}" > "${limit}"
if [[ -n "${hyst_index}" ]]; then
echo "${hyst_val}" > "${hyst_attr}"
fi
return $rv
}
_test_one()
{
local a=("${!1}")
local channels
local t
local rv
local temp
local temp2
local fault
local enable
local temp_max_hyst_index=""
local temp_crit_hyst_index=""
# determine number of channels to test
channels=0
for t in $(seq 9 -1 1); do
if [[ -e "temp${t}_input" ]]; then
channels=$t
break
fi
done
# Make sure that updates happen as fast as possible
if is_writeable update_interval; then
echo 1 > update_interval
fi
if is_writeable temp_samples; then
echo 1 > temp_samples
fi
# Clear temperature offsets
for t in $(seq 1 ${channels}); do
if is_writeable "temp${t}_offset"; then
echo 0 > "temp${t}_offset"
fi
done
sleep 0.2
temp="$(cat temp1_input)"
if [[ temp -le 0 || temp -gt 50000 ]]; then
local r
echo -n "Waiting for temperature to settle ..."
for r in $(seq 1 20); do
temp="$(cat temp1_input)"
if [[ temp -gt 0 && temp -lt 50000 ]]; then
break
fi
sleep 1
echo -n "."
done
if [[ temp -le 0 || temp -gt 50000 ]]; then
echo "Temperature failed to settle. Aborting."
return 1
fi
echo
fi
# dummy value reads to give alarms time to settle
cat ${a[@]} >/dev/null 2>&1
sleep 0.2
local vals=($(cat ${a[@]} 2>/dev/null))
# give alarms time to recover
sleep 0.2
dotest -p -q a[@] vals[@]
if [[ $? -ne 0 ]]; then
return 1
fi
for t in $(seq 1 ${channels}); do
if [[ -e "temp${t}_min" ]]; then
check_range -b ${basedir} -d 500 -r -q temp${t}_min
rv=$((rv + $?))
fi
if [[ -e "temp${t}_max" ]]; then
check_range -b ${basedir} -d 500 -r -q temp${t}_max
rv=$((rv + $?))
if is_writeable "temp${t}_max_hyst"; then
check_range -b ${basedir} -d 500 -r -q temp${t}_max_hyst
rv=$((rv + $?))