WIP FPC-III support
[linux/fpc-iii.git] / tools / testing / selftests / ptp / phc.sh
blobac6e5a6e1d3ad331dc3bf4e39fece4e3062e3e15
1 #!/bin/bash
2 # SPDX-License-Identifier: GPL-2.0
4 ALL_TESTS="
5 settime
6 adjtime
7 adjfreq
9 DEV=$1
11 ##############################################################################
12 # Sanity checks
14 if [[ "$(id -u)" -ne 0 ]]; then
15 echo "SKIP: need root privileges"
16 exit 0
19 if [[ "$DEV" == "" ]]; then
20 echo "SKIP: PTP device not provided"
21 exit 0
24 require_command()
26 local cmd=$1; shift
28 if [[ ! -x "$(command -v "$cmd")" ]]; then
29 echo "SKIP: $cmd not installed"
30 exit 1
34 phc_sanity()
36 phc_ctl $DEV get &> /dev/null
38 if [ $? != 0 ]; then
39 echo "SKIP: unknown clock $DEV: No such device"
40 exit 1
44 require_command phc_ctl
45 phc_sanity
47 ##############################################################################
48 # Helpers
50 # Exit status to return at the end. Set in case one of the tests fails.
51 EXIT_STATUS=0
52 # Per-test return value. Clear at the beginning of each test.
53 RET=0
55 check_err()
57 local err=$1
59 if [[ $RET -eq 0 && $err -ne 0 ]]; then
60 RET=$err
64 log_test()
66 local test_name=$1
68 if [[ $RET -ne 0 ]]; then
69 EXIT_STATUS=1
70 printf "TEST: %-60s [FAIL]\n" "$test_name"
71 return 1
74 printf "TEST: %-60s [ OK ]\n" "$test_name"
75 return 0
78 tests_run()
80 local current_test
82 for current_test in ${TESTS:-$ALL_TESTS}; do
83 $current_test
84 done
87 ##############################################################################
88 # Tests
90 settime_do()
92 local res
94 res=$(phc_ctl $DEV set 0 wait 120.5 get 2> /dev/null \
95 | awk '/clock time is/{print $5}' \
96 | awk -F. '{print $1}')
98 (( res == 120 ))
101 adjtime_do()
103 local res
105 res=$(phc_ctl $DEV set 0 adj 10 get 2> /dev/null \
106 | awk '/clock time is/{print $5}' \
107 | awk -F. '{print $1}')
109 (( res == 10 ))
112 adjfreq_do()
114 local res
116 # Set the clock to be 1% faster
117 res=$(phc_ctl $DEV freq 10000000 set 0 wait 100.5 get 2> /dev/null \
118 | awk '/clock time is/{print $5}' \
119 | awk -F. '{print $1}')
121 (( res == 101 ))
124 ##############################################################################
126 cleanup()
128 phc_ctl $DEV freq 0.0 &> /dev/null
129 phc_ctl $DEV set &> /dev/null
132 settime()
134 RET=0
136 settime_do
137 check_err $?
138 log_test "settime"
139 cleanup
142 adjtime()
144 RET=0
146 adjtime_do
147 check_err $?
148 log_test "adjtime"
149 cleanup
152 adjfreq()
154 RET=0
156 adjfreq_do
157 check_err $?
158 log_test "adjfreq"
159 cleanup
162 trap cleanup EXIT
164 tests_run
166 exit $EXIT_STATUS