-
Notifications
You must be signed in to change notification settings - Fork 1
/
desip_message_checksum_check.sh
executable file
·206 lines (186 loc) · 3.85 KB
/
desip_message_checksum_check.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
#!/bin/bash
#Color
RED="\033[31;1m"
GREEN="\033[32;1m"
RESET="\033[0m"
INPUT_FILE="invalid"
SORT="no"
START_TIME=`date +'%Y-%m-%d %H:%M:%S'`
filter=(
"17 41"
)
Usage() {
echo " "
echo "`basename $1` - version:20190410"
echo " "
echo "Usage:"
echo "`basename $1` -f <input_file> [-s] [-h]"
echo " "
return 0
}
#Check necessary tools
#Get argument from terminal
while getopts "f:sh" arg
do
case $arg in
f)
INPUT_FILE="$OPTARG"
;;
s)
SORT="yes"
;;
h|?|*)
Usage $0
exit 0
esac
done
if [ "Y"$INPUT_FILE = "Y" -o ! -f $INPUT_FILE ];then
echo -e $RED "[ERROR] Please give desip message file name." $RESET
Usage $0
exit 1
fi
TMP_NAME="`basename $INPUT_FILE`"
OUTPUT_FILE1="${TMP_NAME%.*}_update.txt"
OUTPUT_FILE2="${TMP_NAME%.*}_output.txt"
ERROR_LOG="${TMP_NAME%.*}_invalid_checksum.txt"
if [ -f $OUTPUT_FILE1 ];then
rm -rf $OUTPUT_FILE1
fi
if [ -f $OUTPUT_FILE2 ];then
rm -rf $OUTPUT_FILE2
fi
if [ -f $ERROR_LOG ];then
rm -rf $ERROR_LOG
fi
dos2unix $INPUT_FILE
echo -e $GREEN "==================== Begin ====================" $RESET
#Convert Uart data to Desip message data
# 1C => SEP
echo "[1/2]Convert UART data to DESIP message data."
while read line
do
if [[ ! -z $line ]];
then
PRE=`echo $line | cut -d - -f 1`
DATA=`echo $line | cut -d - -f 2`
echo -n "$PRE - " >> $OUTPUT_FILE1
arr=($DATA)
count=0
skip=0
for word in ${arr[@]}
do
if [ $skip -gt 0 ];
then
((skip --))
((count ++))
continue
fi
case $word in
"1B")
if [ ${arr[((count+1))]} == "1D" ];
then
echo -n "1B " >> $OUTPUT_FILE1
((skip ++))
echo -n "."
elif [ ${arr[((count+1))]} == "1E" ];
then
echo -n "1C " >> $OUTPUT_FILE1
((skip ++))
echo -n "."
else
echo " " >> $OUTPUT_FILE1
echo $word >> $OUTPUT_FILE1
echo -n "."
fi
;;
"1C")
echo "" >> $OUTPUT_FILE1
echo -n "|"
;;
*)
echo -n "$word " >> $OUTPUT_FILE1
echo -n "."
;;
esac
((count ++))
done
fi
done < $INPUT_FILE
echo ""
echo "[1/2]Done"
echo "[2/3]Filter special desip message."
if [ ${#filter} -eq 0 ];
then
echo "[Warning]No filter assigned,so we will calculate all of desip message's checksum."
cp $OUTPUT_FILE1 tmp.txt
else
IFSBAK=$IFS
IFS="\n"
echo "Filter:"
for key in ${filter[@]}
do
echo " $key"
done
while read line
do
echo -n ">"
for key in ${filter[@]}
do
result=`echo $line | cut -d "-" -f 2| grep "^ $key"`
if [ -n "$result" ];
then
echo "$line" >> tmp.txt
fi
echo -n "-"
done
done < $OUTPUT_FILE1
IFS=$IFSBAK
echo " "
fi
if [ $SORT = "yes" ];then
sort tmp.txt -o tmp.txt
fi
echo "[2/3]Done."
echo "[3/3]Calculating the checksum of desip message."
while read line
do
if [[ ! -z $line ]];
then
PRE=`echo $line | cut -d - -f 1`
DATA=`echo $line | cut -d - -f 2`
echo -n "$PRE - " >> $OUTPUT_FILE2
arr=($DATA)
checksum=0
checkvalid=0
length=${#arr[@]}
for word in ${arr[@]}
do
echo -n "$word " >> $OUTPUT_FILE2
((checksum = checksum + 0x$word))
done
checkvalid=$(( $checksum % 0x100 ))
if [ $checkvalid != 255 ];
then
echo ""
echo -e $RED $line $RESET
printf $RED"\nchecksum: 0x%X, length: %d\n\n"$RESET $checksum $length
echo -e $RED $line $RESET >> $ERROR_LOG
printf $RED"\nchecksum: 0x%X, length: %d\n\n"$RESET $checksum $length >> $ERROR_LOG
fi
printf "\nchecksum: 0x%X, length: %d\n\n" $checksum $length >> $OUTPUT_FILE2
echo -n "."
fi
done < tmp.txt
#Delete tmp.txt
rm -rf tmp.txt
echo ""
echo "[3/3]Done."
echo -e $GREEN "==================== Finish ====================" $RESET
END_TIME=`date +'%Y-%m-%d %H:%M:%S'`
start_seconds=$(date --date="$START_TIME" +%s);
end_seconds=$(date --date="$END_TIME" +%s);
cost=$((end_seconds -start_seconds))
echo "# START: $START_TIME"
echo "# END: $END_TIME"
echo "# COST:" $(( $cost / 60 ))"m" $(( $cost % 60 ))"s"
exit 0