Add Wireshark packet dissector plugin and associated tools

This commit is contained in:
Allen Hill
2024-08-23 11:02:40 -07:00
parent fadd2c4259
commit e857bbb09f
26 changed files with 1475 additions and 0 deletions
+73
View File
@@ -0,0 +1,73 @@
module AVCLANPipe
using PcapTools, Dates, UnixTimes
export AVCLANframe, avclan_text_to_pcap, tobytes
mutable struct AVCLANframe
broadcast::Bool
controller_addr::UInt16
peripheral_addr::UInt16
control::UInt8
length::UInt8
data::NTuple{32,UInt8}
end
AVCLANframe() = AVCLANframe(false, 0x0000, 0x0000, 0xf, 0x0, ntuple(x -> 0x0, 32))
function Base.tryparse(::Type{AVCLANframe}, str::String)
vals = split(str)
broadcast = tryparse(Bool, vals[1])
isnothing(broadcast) && return nothing
controller_addr = tryparse(UInt16, vals[2])
isnothing(controller_addr) && return nothing
peripheral_addr = tryparse(UInt16, vals[3])
isnothing(peripheral_addr) && return nothing
control = tryparse(UInt8, vals[4])
isnothing(control) && return nothing
len = tryparse(UInt8, vals[5])
isnothing(len) && return nothing
if (length(vals) - 5) != len || len > 32
return nothing
end
_data = tryparse.(UInt8, vals[6:end])
data = ntuple(i -> checkindex(Bool, axes(_data, 1), i) ? _data[i] : 0x0, 32)
return AVCLANframe(broadcast, controller_addr, peripheral_addr, control, len, data)
end
function tobytes(frame::AVCLANframe)
data = Vector{UInt8}(undef, 0)
push!(data, frame.broadcast)
append!(data, reverse(reinterpret(reshape, UInt8, [frame.controller_addr])),
reverse(reinterpret(reshape, UInt8, [frame.peripheral_addr])),
reinterpret(reshape, UInt8, [frame.control]),
reinterpret(reshape, UInt8, [frame.length]),
frame.data[1:frame.length])
return data
end
function avclan_text_to_pcap(textlog::String, pcap_fn::String)
pcapstream = PcapStreamWriter(pcap_fn; snaplen=64, linktype = 162)
t = UnixTime(now())
for line in eachline(textlog)
frame = tryparse(AVCLANframe, line)
t += Microsecond(rand(1:15))
if !isnothing(frame)
write(pcapstream, t, tobytes(frame))
end
end
close(pcapstream)
return pcap_fn
end
end # module AVCLANPipe
@@ -0,0 +1,55 @@
using AVCLANPipe, PcapTools, Dates, UnixTimes
file = ARGS[1]
outfile = ARGS[2]
function convert_fields_to_pcap(file, outfile)
io = open(file, "r")
outio = PcapStreamWriter(outfile; snaplen=64, linktype = 162)
packet = AVCLANframe()
packetcomplete = false
local t::UnixTime
i = 1
for line in readlines(io)
m = match(r"(\d+)-(\d+) IEBus: Raw Fields: (?<field>\w+)(:? ((?<hex>0x[0-9a-f]{2,3})|WRITE_DATA|Length: (?<length>\d+)))?",
line)
if m[:field] == "Broadcast"
packet.broadcast = true
t = UnixTime(1970,1,1,0,0,0,0, something(tryparse(Int, m[1]), 0))
elseif m[:field] == "Unicast"
packet.broadcast = false
t = UnixTime(1970,1,1,0,0,0,0, something(tryparse(Int, m[1]), 0))
elseif m[:field] == "Master"
packet.controller_addr = tryparse(UInt16, m[:hex])
elseif m[:field] == "Slave"
packet.peripheral_addr = tryparse(UInt16, m[:hex])
elseif m[:field] == "Data"
if !isnothing(m[:length])
packet.length = convert(UInt8, tryparse(Int, m[:length]))
else
data = collect(packet.data)
data[i] = tryparse(UInt8, m[:hex])
packet.data = ntuple(n -> data[n], 32)
i += 1
end
end
if i > packet.length
packetcomplete = true
end
if packetcomplete
packetcomplete = false
i = 1
write(outio, t, tobytes(packet))
end
end
close(io)
close(outio)
end
@@ -0,0 +1,45 @@
include("AVCLANPipe.jl")
using .AVCLANPipe, PcapTools, Dates, UnixTimes, LibSerialPort
serial_port="/dev/ttyUSB0"
baud=1200000
pcapstream = PcapStreamWriter(stdout; snaplen=64, linktype=162)
serial = LibSerialPort.open(serial_port, baud)
set_flow_control(serial) # Disable flow-control (stops it from eating raw byte 0x11)
write(serial, 'X')
buf = IOBuffer()
function quit(serialio=serial)
close(serialio)
exit()
end
while true
iswritable(stdout) || isopen(stdout) || quit()
if bytesavailable(serial) > 0
t = UnixTime(now())
line = readline(serial)
# allthebytes = Vector{UInt8}(undef, bytesavailable(serial))
# readbytes!(serial, allthebytes)
# write(buf, allthebytes)
# l = findfirst(==('\n'), buf.data)
# if !isnothing(l)
# println(stderr, "fucku")
# bytes = Vector{UInt8}(undef, l)
# seekstart(buf)
# readbytes!(buf, bytes)
# seekend(buf)
bytes = Vector{UInt8}(line)
if bytes[1] == 0x10 && bytes[end] == 0x17
write(pcapstream, t, bytes[2:end-1])
else
@error line
end
# end
# println(stderr, "wtf")
end
yield()
end