#!/bin/bash

SV_KEY='\x28\xd1\x88\xbd\x56\x7b\x28\x2b'

SV_CMD_ID='\x0E\x01\x0B\x00\x0E\xF5\x00\x00\x00\xD1\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x2E'
SV_CMD_DEL_ID='\x0e\x01\x04\x00\x15\xe1\x00\x00\x00\x50\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x69\x00'
SV_CMD_DEL_PW='\x0e\x01\x06\x00\x16\xe0\x00\x00\x00\x21\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'

echo -ne $SV_CMD_ID  > /tmp/SV_CMD_ID.bin  #Place commands on filesystem, because shell variables cant cope with 0x00es!
echo -ne $SV_CMD_DEL_ID  > /tmp/SV_CMD_DEL_ID.bin
echo -ne $SV_CMD_DEL_PW  > /tmp/SV_CMD_DEL_PW.bin
echo -ne $SV_KEY  > /tmp/KEY.bin

stty 4800 < /dev/ttyUSB0 raw -echo # Init serial port. SmartCD: (modprobe ftdi_sio ; echo 0403 c980 >/sys/bus/usb-serial/drivers/ftdi_sio/new_id)

echo -e "Read_ID cmd:"
xxd -p /tmp/SV_CMD_ID.bin
rm /tmp/SV_ID_RESULT.bin
dd bs=1 if=/dev/ttyUSB0 of=/tmp/SV_ID_RESULT.bin  status=none &          #REDIRECT SERIAL INPUT TO FILE
  PID=$!
    cat /tmp/SV_CMD_ID.bin >  /dev/ttyUSB0 # send command
    while [ ! -s  "/tmp/SV_ID_RESULT.bin" ]  # WAIT FOR RESPONSE
    do
    sleep 0.2s
    done
  kill $PID                             #KILL DD PROCESS
  wait $PID 2>/dev/null                 #SUPRESS "Terminated" output
echo "Read_ID Response:"
xxd -p /tmp/SV_ID_RESULT.bin


dd bs=1 skip=6 count=2 seek=10 if=/tmp/SV_ID_RESULT.bin of=/tmp/SV_CMD_DEL_ID.bin conv=notrunc status=none # copy ID and Key into commands
dd bs=1 skip=8 count=2 seek=6 if=/tmp/SV_ID_RESULT.bin of=/tmp/SV_CMD_DEL_ID.bin conv=notrunc status=none
dd bs=1 skip=0 count=8 seek=12 if=/tmp/KEY.bin of=/tmp/SV_CMD_DEL_ID.bin  conv=notrunc status=none
dd bs=1 skip=0 count=8 seek=12 if=/tmp/KEY.bin of=/tmp/SV_CMD_DEL_PW.bin  conv=notrunc status=none

od -t u1 -An -w1 -v  /tmp/SV_CMD_DEL_PW.bin | awk '{s=xor(s,$1);} END {printf("%02X\n",s)}'| xxd -r -p  | dd bs=1 count=1 seek=28 of=/tmp/SV_CMD_DEL_PW.bin status=none # Build Checksum
od -t u1 -An -w1 -v  /tmp/SV_CMD_DEL_ID.bin | awk '{s=xor(s,$1);} END {printf("%02X\n",s)}' | xxd -r -p | dd bs=1 count=1 seek=27 of=/tmp/SV_CMD_DEL_ID.bin status=none # Build Checksum

echo "Delete_ID cmd:"
xxd -p /tmp/SV_CMD_DEL_ID.bin
rm /tmp/SV_ID_RESULT.bin
dd bs=1 if=/dev/ttyUSB0 of=/tmp/SV_ID_RESULT.bin  status=none &          #REDIRECT SERIAL INPUT TO FILE
  PID=$!
    cat /tmp/SV_CMD_DEL_ID.bin >  /dev/ttyUSB0 # send command
    while [ ! -s  "/tmp/SV_ID_RESULT.bin" ]  # WAIT FOR RESPONSE
    do
    sleep 0.1s
    done
  kill $PID                             #KILL DD PROCESS
  wait $PID 2>/dev/null                 #SUPRESS "Terminated" output

echo "Delete_ID Response:" # 0x06 -> OK 0x0a -> ERR 0x0b -> Key Err
xxd -p /tmp/SV_ID_RESULT.bin

echo "Delete_PW cmd:"
xxd -p /tmp/SV_CMD_DEL_PW.bin
rm /tmp/SV_ID_RESULT.bin
dd bs=1 if=/dev/ttyUSB0 of=/tmp/SV_ID_RESULT.bin  status=none &          #REDIRECT SERIAL INPUT TO FILE
  PID=$!
    cat /tmp/SV_CMD_DEL_PW.bin >  /dev/ttyUSB0 # send command
    while [ ! -s  "/tmp/SV_ID_RESULT.bin" ]  # WAIT FOR RESPONSE
    do
    sleep 0.1s
    done
  kill $PID                             #KILL DD PROCESS
  wait $PID 2>/dev/null                 #SUPRESS "Terminated" output

echo "Delete_PW Response:" # 0x06 -> OK 0x0a -> ERR 0x0b -> Key Err
xxd -p /tmp/SV_ID_RESULT.bin

exit 0
