/
wrnctrl
162 lines (144 loc) · 3.98 KB
/
wrnctrl
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
#!/bin/bash
# Copyright (c) 2016 Aleksandr Borisenko
# Distributed under the terms of the GNU General Public License v2
WRND_CONFIG="/etc/conf.d/wrnd"
WRND_BIN="/usr/local/bin/wrnd"
WRND_PID=$(ps -eo pid,args | grep "${WRND_BIN}" | grep -v grep | awk '{ print $1 }')
SCRIPT_NAME=`basename $0`
LOCK_NAME="/tmp/.${SCRIPT_NAME}.fifolock"
AVRDUDE_CONF="/etc/avrdude.conf"
AVRDUDE_BAUDRATE=115200
SCRIPT_VERSION="0.1"
DEFAULT_LOG_LINES=0
FIFO_MAX_WAIT=1 # sec
FIFO_MAX_LOCK=10 # sec
E_SYSERROR=1
E_COMERROR=2
E_OPTERROR=3
# DEFAULT
WRND_DEVICE="/dev/ttyS0"
WRND_BAUDRATE=57600
WRND_CMDFIFO="/run/wrnd/cmd.fifo"
if [ -f ${WRND_CONFIG} ]; then
. ${WRND_CONFIG}
fi
echo_to_stderr()
{
echo "$@" 1>&2;
}
function device_cmd()
{
cmd=$1
(
if ! flock -w ${FIFO_MAX_WAIT} 4; then
echo_to_stderr "The another process using FIFO."
echo "--"
return
fi
if [ -O ${LOCK_NAME} ]; then chmod 666 ${LOCK_NAME}; fi
exec 3< <(timeout ${FIFO_MAX_LOCK} cat ${WRND_CMDFIFO})
sleep 0.1
if [ -r /dev/fd/3 ]; then
echo "$cmd" >${WRND_DEVICE}
while read log_line <&3; do
echo "$log_line"
done
exec 3>&- # close fd 3
fi
) 4>${LOCK_NAME}
}
function device_flash()
{
hex=$1
if [ ! -f ${hex} ]; then
echo_to_stderr "The file '${hex}' was not found."
return
fi
if ! hash avrdude 2>/dev/null; then
echo_to_stderr "Program 'avrdude' was not found in the PATH."
return
fi
stty -F "${WRND_DEVICE}" cs8 ${WRND_BAUDRATE} raw ignbrk noflsh -onlcr -iexten -echo -echoe -echok -echoctl -echoke -crtscts
echo "W1" >${WRND_DEVICE} # WDT deactivate
echo "R1" >${WRND_DEVICE} # RNG flood off
echo "C4" >${WRND_DEVICE} # unlock DTR
avrdude -C${AVRDUDE_CONF} -v -patmega328p -carduino -P${WRND_DEVICE} \
-b${AVRDUDE_BAUDRATE} -D -Uflash:w:"${hex}":i
}
function usage()
{
echo_to_stderr ""
echo_to_stderr "${SCRIPT_NAME} version ${SCRIPT_VERSION}"
echo_to_stderr "USAGE:"
echo_to_stderr " ${SCRIPT_NAME} [command]"
echo_to_stderr "COMMANDS:"
echo_to_stderr " stat - show status of the WRN device."
echo_to_stderr " wstat - show status of the WDT subsystem."
echo_to_stderr " rstat - show status of the RNG subsystem."
echo_to_stderr " log <lines> - show number of lines of the log from the device."
echo_to_stderr " DEFAULT: ${DEFAULT_LOG_LINES} (0 - show all lines)."
echo_to_stderr " synctime - sync the device time with the host time."
echo_to_stderr " cleanlog - clear the EEPROM of the device."
echo_to_stderr " reset - reboot the device to set it to the initial state."
echo_to_stderr " flash [.hex] - prepare the device and program the .hex into the board."
echo_to_stderr " NOTE: this function may be disabled at the hardware level."
exit ${E_OPTERROR}
}
if [ $# -lt 1 ] || [ $# -gt 2 ]; then
echo_to_stderr "Wrong number of arguments specified."
usage
fi
if [ $# -eq 2 ] && [ "$1" == "flash" ]; then
device_flash "$2"
exit 0
fi
if [ -z "${WRND_PID}" ]; then
echo_to_stderr "WRN daemon must be started to provide the communication with the device."
exit ${E_SYSERROR}
fi
if [ ! -p "${WRND_CMDFIFO}" ]; then
echo_to_stderr "The FIFO ${WRND_CMDFIFO} cannot be opened for reading. Please check the permissions."
exit ${E_SYSERROR}
fi
case "$1" in
"stat")
response=$(device_cmd "C2")
;;
"wstat")
response=$(device_cmd "W2")
;;
"rstat")
response=$(device_cmd "R2")
;;
"log")
if [[ $2 =~ ^[0-9]+$ ]]; then
log_lines=$2
else
log_lines=${DEFAULT_LOG_LINES}
fi
response=$(device_cmd "W4:$log_lines")
;;
"synctime")
echo "C1:"$(date +%s) >${WRND_DEVICE}
response="--"
;;
"cleanlog")
response=$(device_cmd "C5")
;;
"reset")
echo "C3" >${WRND_DEVICE}
response="A reboot request has been sent to the device."
;;
*)
echo_to_stderr "Unknown argument: $1."
usage
;;
esac
if [ -z "$response" ]; then
echo_to_stderr "No response from the device. Please try again."
exit ${E_COMERROR}
elif [ "$response" == "--" ]; then
exit 0
fi
echo "$response"
exit 0