-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathebuscrc
executable file
·70 lines (59 loc) · 2.82 KB
/
ebuscrc
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
#!/bin/bash
# this shellscript calculates a CRC8-value of a given hex-string with a CRC-polynome of value 0x5C
# usage: ebuscrc b268 -> ACB268
#
# usage within ebuctl-command:
# ebusctl hex 08500003"$(ebuscrc b268)" -> ebusctl hex 08500003ACB268
# CRC-lookuptable for polynom 0x5C
lookuptable=(
0x00 0x5C 0xB8 0xE4 0x2C 0x70 0x94 0xC8 0x58 0x04 0xE0 0xBC 0x74 0x28 0xCC 0x90
0xB0 0xEC 0x08 0x54 0x9C 0xC0 0x24 0x78 0xE8 0xB4 0x50 0x0C 0xC4 0x98 0x7C 0x20
0x3C 0x60 0x84 0xD8 0x10 0x4C 0xA8 0xF4 0x64 0x38 0xDC 0x80 0x48 0x14 0xF0 0xAC
0x8C 0xD0 0x34 0x68 0xA0 0xFC 0x18 0x44 0xD4 0x88 0x6C 0x30 0xF8 0xA4 0x40 0x1C
0x78 0x24 0xC0 0x9C 0x54 0x08 0xEC 0xB0 0x20 0x7C 0x98 0xC4 0x0C 0x50 0xB4 0xE8
0xC8 0x94 0x70 0x2C 0xE4 0xB8 0x5C 0x00 0x90 0xCC 0x28 0x74 0xBC 0xE0 0x04 0x58
0x44 0x18 0xFC 0xA0 0x68 0x34 0xD0 0x8C 0x1C 0x40 0xA4 0xF8 0x30 0x6C 0x88 0xD4
0xF4 0xA8 0x4C 0x10 0xD8 0x84 0x60 0x3C 0xAC 0xF0 0x14 0x48 0x80 0xDC 0x38 0x64
0xF0 0xAC 0x48 0x14 0xDC 0x80 0x64 0x38 0xA8 0xF4 0x10 0x4C 0x84 0xD8 0x3C 0x60
0x40 0x1C 0xF8 0xA4 0x6C 0x30 0xD4 0x88 0x18 0x44 0xA0 0xFC 0x34 0x68 0x8C 0xD0
0xCC 0x90 0x74 0x28 0xE0 0xBC 0x58 0x04 0x94 0xC8 0x2C 0x70 0xB8 0xE4 0x00 0x5C
0x7C 0x20 0xC4 0x98 0x50 0x0C 0xE8 0xB4 0x24 0x78 0x9C 0xC0 0x08 0x54 0xB0 0xEC
0x88 0xD4 0x30 0x6C 0xA4 0xF8 0x1C 0x40 0xD0 0x8C 0x68 0x34 0xFC 0xA0 0x44 0x18
0x38 0x64 0x80 0xDC 0x14 0x48 0xAC 0xF0 0x60 0x3C 0xD8 0x84 0x4C 0x10 0xF4 0xA8
0xB4 0xE8 0x0C 0x50 0x98 0xC4 0x20 0x7C 0xEC 0xB0 0x54 0x08 0xC0 0x9C 0x78 0x24
0x04 0x58 0xBC 0xE0 0x28 0x74 0x90 0xCC 0x5C 0x00 0xE4 0xB8 0x70 0x2C 0xC8 0x94
)
# uppercase argument string
whcmd=${1^^}
# get the number of chars
charcount=${#whcmd}
# insert leading '0' to get full 2-char bytes
if [[ $((charcount%2)) != 0 ]]
then
whcmd="0$whcmd"
charcount+=1
fi
# init CRC
crcresult_dec=0
for(( currentpos=0; currentpos<charcount; currentpos+=2 ))
do
# get substring at current position -> current byte-string in hex
substring=${whcmd:currentpos:2}
# convert hex-byte to decimal value
value_dec=$((16#$substring))
# echo "$currentpos: substring:=$substring value_dec:=$value_dec"
# get lookupvalue with last loops result -> string looks like 0xXX
lookupvalue_hex=${lookuptable[$crcresult_dec]}
# cut last 2-char byte from 0xXX -> XX and convert to decimal
lookupvalue_dec=$((16#${lookupvalue_hex:${#lookupvalue_hex}-2:2}))
# echo "$currentpos: crcresult_dec:=$crcresult_dec lookupvalue_hex:=$lookupvalue_hex lookupvalue_dec:=$lookupvalue_dec"
# XOR lookupvalue and currentvalue
currentcrc_dec=$(( $lookupvalue_dec ^ $value_dec ))
# convert to hex-Byte
# printf -v currentcrc_hex "%02X" "$currentcrc_dec"
# echo "$currentpos: currentcrc_dec:=$currentcrc_dec currentcrc_hex:=$currentcrc_hex"
crcresult_dec=$currentcrc_dec
done
# convert to hex-Byte
printf -v crcresult_hex "%02X" "$crcresult_dec"
echo "$crcresult_hex$whcmd"