-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #57 from JuliaRobotics/tk/fix-decode-uint8-vector
Fix #56, decode breakage after UnsafeArrays change.
- Loading branch information
Showing
7 changed files
with
115 additions
and
154 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,108 +1,100 @@ | ||
# Read LCM log files directly | ||
|
||
mutable struct lcm_eventlog_t | ||
f::Ptr{Cvoid} | ||
eventcount::Int64 #Clonglong | ||
f::Ptr{Cvoid} | ||
eventcount::Int64 #Clonglong | ||
end | ||
|
||
|
||
struct lcm_eventlog_event_t | ||
eventnum::Int64 #Clonglong | ||
timestamp::Int64 #Clonglong | ||
channellen::Int32 #Cint | ||
datalen::Int32 #Cint | ||
channel::Ptr{Cuchar} | ||
data::Ptr{Cuchar} | ||
eventnum::Int64 #Clonglong | ||
timestamp::Int64 #Clonglong | ||
channellen::Int32 #Cint | ||
datalen::Int32 #Cint | ||
channel::Ptr{Cuchar} | ||
data::Ptr{Cuchar} | ||
end | ||
|
||
|
||
function lcm_eventlog_create(file::S where S <: AbstractString) | ||
ccall( | ||
ccall( | ||
(:lcm_eventlog_create, LCMCore.liblcm), | ||
Ptr{lcm_eventlog_t}, | ||
(Cstring, Cstring), | ||
file, "r" | ||
) | ||
) | ||
end | ||
|
||
|
||
function lcm_eventlog_destroy(_log::Ptr{lcm_eventlog_t}) | ||
ccall( | ||
ccall( | ||
(:lcm_eventlog_destroy, LCMCore.liblcm), | ||
Cvoid, | ||
(Ptr{lcm_eventlog_t}, ), | ||
_log | ||
) | ||
) | ||
end | ||
|
||
|
||
function lcm_eventlog_read_next_event(_log::Ptr{lcm_eventlog_t}) | ||
ccall( | ||
(:lcm_eventlog_read_next_event, LCMCore.liblcm), | ||
Ptr{lcm_eventlog_event_t}, | ||
(Ptr{lcm_eventlog_t},), | ||
_log | ||
(:lcm_eventlog_read_next_event, LCMCore.liblcm), | ||
Ptr{lcm_eventlog_event_t}, | ||
(Ptr{lcm_eventlog_t},), | ||
_log | ||
) | ||
end | ||
|
||
# | ||
# Doesn't work yet | ||
# event = unsafe_wrap(lcm_eventlog_event_t, _event, dim??) | ||
|
||
|
||
|
||
mutable struct LCMLog | ||
_log::Ptr{lcm_eventlog_t} | ||
subscriptions::Dict{AbstractString, LCMCore.SubscriptionOptions} | ||
function LCMLog(filename::S) where {S <: AbstractString} | ||
_log = lcm_eventlog_create(filename) | ||
if !isgood(_log) | ||
throw(ArgumentError("Cannot open the LCM log file at: $filename")) | ||
_log::Ptr{lcm_eventlog_t} | ||
subscriptions::Dict{AbstractString, LCMCore.SubscriptionOptions} | ||
function LCMLog(filename::S) where {S <: AbstractString} | ||
_log = lcm_eventlog_create(filename) | ||
if !isgood(_log) | ||
throw(ArgumentError("Cannot open the LCM log file at: $filename")) | ||
end | ||
return new(_log, Dict{AbstractString, LCMCore.SubscriptionOptions}()) | ||
end | ||
return new(_log, Dict{AbstractString, LCMCore.SubscriptionOptions}()) | ||
end | ||
end | ||
|
||
|
||
|
||
function close(lcmlog::LCMLog) | ||
if isgood(lcmlog) | ||
lcm_eventlog_destroy(lcmlog._log) | ||
end | ||
if isgood(lcmlog) | ||
lcm_eventlog_destroy(lcmlog._log) | ||
end | ||
end | ||
|
||
isgood(_log::Ptr{lcm_eventlog_t}) = _log != C_NULL | ||
isgood(lcmlog::LCMLog) = isgood(lcmlog._log) | ||
isgood(_event::Ptr{lcm_eventlog_event_t}) = _event != C_NULL | ||
|
||
function read_next_event(lcmlog::LCMLog)::Union{Nothing, lcm_eventlog_event_t} | ||
_event = lcm_eventlog_read_next_event(lcmlog._log) | ||
if isgood(_event) | ||
return unsafe_load(_event) | ||
end | ||
nothing | ||
_event = lcm_eventlog_read_next_event(lcmlog._log) | ||
if isgood(_event) | ||
return unsafe_load(_event) | ||
end | ||
nothing | ||
end | ||
|
||
function handle(lcmlog::LCMLog)::Bool | ||
# do memory copy and then check for subscribed channel -- This is not efficient, but easiest to do. TBD is `unsafe_wrap` to avoid memory copy of _event | ||
event = read_next_event(lcmlog) | ||
if event != nothing | ||
# need a convert from lcm_eventlog_event_t to (RecvBuf, channelbytes, chnlen) | ||
rb = LCMCore.RecvBuf(event.data, UInt32(event.datalen), event.timestamp, 0) | ||
# need a LCMCore.SubscriptionOptions{T} | ||
chn = unsafe_string(event.channel, event.channellen) | ||
if haskey(lcmlog.subscriptions, chn) | ||
opts = lcmlog.subscriptions[chn] | ||
# use onresponse similar to regular live LCM traffic | ||
LCMCore.onresponse(rb, event.channel, opts) | ||
end | ||
return true | ||
# need a convert from lcm_eventlog_event_t to (RecvBuf, channelbytes, chnlen) | ||
rb = LCMCore.RecvBuf(event.data, UInt32(event.datalen), event.timestamp, 0) | ||
# need a LCMCore.SubscriptionOptions{T} | ||
chn = unsafe_string(event.channel, event.channellen) | ||
if haskey(lcmlog.subscriptions, chn) | ||
opts = lcmlog.subscriptions[chn] | ||
# use onresponse similar to regular live LCM traffic | ||
LCMCore.onresponse(rb, event.channel, opts) | ||
end | ||
return true | ||
end | ||
return false | ||
end | ||
|
||
function subscribe(lcmlog::LCMLog, channel::S, callback::F, msgtype=Nothing) where {S <: AbstractString, F <: Function} | ||
opts = SubscriptionOptions(msgtype, callback, channel) | ||
lcmlog.subscriptions[channel] = opts | ||
nothing | ||
function subscribe(lcmlog::LCMLog, channel::S, callback::F, msgtype=Nothing) where {S <: AbstractString, F} | ||
opts = SubscriptionOptions(msgtype, callback, channel) | ||
lcmlog.subscriptions[channel] = opts | ||
nothing | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
mutable struct MyMessage | ||
field1::Int32 | ||
field2::Float64 | ||
end | ||
|
||
function encode(msg::MyMessage) | ||
buf = IOBuffer() | ||
write(buf, hton(msg.field1)) | ||
write(buf, hton(msg.field2)) | ||
buf.data | ||
end | ||
|
||
function decode(data, msg::Type{MyMessage}) | ||
buf = IOBuffer(data) | ||
MyMessage(ntoh(read(buf, Int32)), ntoh(read(buf, Float64))) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters