163 lines
5.1 KiB
Bash
Executable File
163 lines
5.1 KiB
Bash
Executable File
#!/bin/bash
|
|
set -euo pipefail
|
|
IFS=$'\n\t'
|
|
|
|
################################################################################
|
|
# This converts the hex form of a wireshark analysis follow to something
|
|
# more human readable.
|
|
#
|
|
# It is only partially implemented!
|
|
################################################################################
|
|
|
|
f_name="${1}"
|
|
|
|
if [ "$#" -eq 2 ]; then
|
|
filt="${2}"
|
|
fi
|
|
|
|
|
|
declare -A POSITION_MODE
|
|
POSITION_MODE[1]="RELATIVE"
|
|
POSITION_MODE[2]="ABSOLUTE"
|
|
POSITION_MODE[3]="INTERNAL_REFERANCE"
|
|
POSITION_MODE[4]="EXTERNAL_REFERANCE"
|
|
|
|
|
|
declare -A COMMANDS
|
|
|
|
function to_ord() {
|
|
printf "%x" "'$1"
|
|
}
|
|
|
|
function add_char() {
|
|
COMMANDS["$(to_ord "$1")"]="$2"
|
|
}
|
|
|
|
function add_ord() {
|
|
COMMANDS["$1"]="$2"
|
|
}
|
|
|
|
add_char A Start_Movement
|
|
add_char C Get_Position
|
|
add_char D Set_Position_And_Clear_Error
|
|
add_char d Set_Direction
|
|
add_char p Set_Position_Mode
|
|
add_char '$' Get_Status
|
|
add_char 0 0
|
|
add_char 1 1
|
|
add_char 2 2
|
|
add_char 3 3
|
|
add_char 4 4
|
|
add_char 5 5
|
|
add_char 6 6
|
|
add_char 7 7
|
|
add_char 8 8
|
|
add_char 9 9
|
|
add_char '#' '#'
|
|
add_ord 00 ''
|
|
add_ord 0d '' # '\r'
|
|
add_ord 2b '+'
|
|
add_ord 2d '-'
|
|
|
|
while read -r line; do
|
|
fields="$( echo "${line}" | sed -e 's/^ *//' -e 's/ /:/' -e 's/ /:/' | tr -s ' ' )"
|
|
IFS=' ' bytes=($( echo "${fields}" | cut -d':' -f2))
|
|
string="$( echo "${fields}" | cut -d':' -f3)"
|
|
|
|
parsing_number=-1
|
|
num_len=0
|
|
is_negative=0
|
|
human_readable=()
|
|
for byte in "${bytes[@]}"; do
|
|
if [[ -v COMMANDS["${byte}"] ]]; then
|
|
|
|
if [[ "${COMMANDS["${byte}"]}" == Get_Status ]] || [[ "${COMMANDS["${byte}"]}" == Get_Position ]] || [[ "${COMMANDS["${byte}"]}" == Set_Position_Mode ]] || [[ "${COMMANDS["${byte}"]}" == Set_Position_And_Clear_Error ]]; then
|
|
parsing_number=0
|
|
numlen=0
|
|
is_negative=0
|
|
elif [[ "${parsing_number}" -ge 0 ]] && ( [[ "${byte}" == 00 ]] || [[ "${byte}" == 0d ]] ); then
|
|
if [[ "${num_len}" -ge 1 ]]; then
|
|
|
|
if [[ "${human_readable[${#human_readable[@]}-1]}" == Get_Status ]]; then
|
|
if [[ "$(( parsing_number & 2#0001 ))" -eq 1 ]]; then
|
|
human_readable=("${human_readable[@]}" "READY")
|
|
fi
|
|
|
|
if [[ "$(( parsing_number & 2#0010 ))" -eq 1 ]]; then
|
|
human_readable=("${human_readable[@]}" "ZERO-POS_REACHED")
|
|
fi
|
|
|
|
if [[ "$(( parsing_number & 2#0100 ))" -eq 1 ]]; then
|
|
human_readable=("${human_readable[@]}" "POSITIONING_ERROR")
|
|
fi
|
|
|
|
if [[ "$(( parsing_number & 2#1000 ))" -eq 1 ]]; then
|
|
human_readable=("${human_readable[@]}" "SOMETHING?")
|
|
fi
|
|
|
|
elif [[ "${human_readable[${#human_readable[@]}-1]}" == Set_Position_Mode ]]; then
|
|
human_readable=("${human_readable[@]}" "${POSITION_MODE[${parsing_number}]}")
|
|
|
|
else
|
|
if [[ "${is_negative}" -eq 1 ]]; then
|
|
human_readable=("${human_readable[@]}" "$(( parsing_number * -1))")
|
|
else
|
|
human_readable=("${human_readable[@]}" "${parsing_number}")
|
|
fi
|
|
fi
|
|
fi
|
|
parsing_number=-1
|
|
continue
|
|
elif [ "${parsing_number}" -ge 0 ]; then
|
|
if [[ "${COMMANDS["${byte}"]}" == '-' ]]; then
|
|
is_negative=1
|
|
elif [[ "${COMMANDS["${byte}"]}" == '+' ]]; then
|
|
is_negative=0
|
|
else
|
|
parsing_number="$(( parsing_number * 10 + "${COMMANDS["${byte}"]}" ))"
|
|
fi
|
|
|
|
num_len=$(( num_len + 1 ))
|
|
continue
|
|
fi
|
|
|
|
if [ "${#human_readable[@]}" -gt 0 ]; then
|
|
human_readable=("${human_readable[@]}" "${COMMANDS["${byte}"]}")
|
|
else
|
|
human_readable=("${COMMANDS["${byte}"]}")
|
|
fi
|
|
else
|
|
if [ "${#human_readable[@]}" -gt 0 ]; then
|
|
human_readable=("${human_readable[@]}" "${byte}(unknown)")
|
|
else
|
|
human_readable=("${byte}(unknown)")
|
|
fi
|
|
fi
|
|
done
|
|
|
|
if [[ "${human_readable[0]}" == '#' ]]; then
|
|
if [[ -z "${filt+x}" ]]; then
|
|
echo "Sent: ${human_readable[@]}"
|
|
else
|
|
if [[ "${human_readable[1]}" == "${filt}" ]]; then
|
|
echo "Sent: ${human_readable[@]}"
|
|
fi
|
|
fi
|
|
else
|
|
# Some commands prefix the response with two 0's (I assume there can be
|
|
# more axes)
|
|
if [[ "${human_readable[0]}" == 0 ]] && [[ "${human_readable[1]}" == 0 ]]; then
|
|
IFS=' ' human_readable=("${human_readable[@]:2}")
|
|
fi
|
|
|
|
if [[ -z "${filt+x}" ]]; then
|
|
echo "Received: ${human_readable[@]}"
|
|
else
|
|
if [[ "${human_readable[0]}" == "${filt}" ]]; then
|
|
echo "Received: ${human_readable[@]}"
|
|
fi
|
|
fi
|
|
fi
|
|
|
|
done < "${f_name}"
|