From bb847fcf8a4c14506085f0b78a840f11cad86853 Mon Sep 17 00:00:00 2001 From: Simone Margaritelli Date: Mon, 26 Aug 2024 16:37:06 +0200 Subject: [PATCH] new: can.dump reader will now sleep for the correct amount of time --- modules/can/can_dump_reader.go | 43 ++++++++++++++++++++++++++++++---- 1 file changed, 39 insertions(+), 4 deletions(-) diff --git a/modules/can/can_dump_reader.go b/modules/can/can_dump_reader.go index fbdf29ef..78dcb85d 100644 --- a/modules/can/can_dump_reader.go +++ b/modules/can/can_dump_reader.go @@ -3,8 +3,12 @@ package can import ( "bufio" "context" + "fmt" "os" "regexp" + "strconv" + "strings" + "time" "github.com/evilsocket/islazy/str" "go.einride.tech/can" @@ -14,11 +18,30 @@ import ( var dumpLineParser = regexp.MustCompile(`(?m)^\(([\d\.]+)\)\s+([^\s]+)\s+(.+)`) type dumpEntry struct { - Time string + Time time.Time Device string Frame string } +func parseTimeval(timeval string) (time.Time, error) { + parts := strings.Split(timeval, ".") + if len(parts) != 2 { + return time.Time{}, fmt.Errorf("invalid timeval format") + } + + seconds, err := strconv.ParseInt(parts[0], 10, 64) + if err != nil { + return time.Time{}, fmt.Errorf("invalid seconds value: %v", err) + } + + microseconds, err := strconv.ParseInt(parts[1], 10, 64) + if err != nil { + return time.Time{}, fmt.Errorf("invalid microseconds value: %v", err) + } + + return time.Unix(seconds, microseconds*1000), nil +} + func (mod *CANModule) startDumpReader() error { mod.Info("loading CAN dump from %s ...", mod.dumpName) @@ -36,9 +59,11 @@ func (mod *CANModule) startDumpReader() error { if line != "" { if m := dumpLineParser.FindStringSubmatch(line); len(m) != 4 { mod.Warning("unexpected line: '%s' -> %d matches", line, len(m)) + } else if timeval, err := parseTimeval(m[1]); err != nil { + mod.Warning("can't parse (seconds.microseconds) from line: '%s': %v", line, err) } else { entries = append(entries, dumpEntry{ - Time: m[1], + Time: timeval, Device: m[2], Frame: m[3], }) @@ -50,12 +75,15 @@ func (mod *CANModule) startDumpReader() error { return err } - mod.Info("loaded %d entries from candump log", len(entries)) + numEntries := len(entries) + lastEntry := numEntries - 1 + + mod.Info("loaded %d entries from candump log", numEntries) go func() { mod.Info("candump reader started ...") - for _, entry := range entries { + for i, entry := range entries { frame := can.Frame{} if err := frame.UnmarshalString(entry.Frame); err != nil { mod.Error("could not unmarshal CAN frame: %v", err) @@ -69,6 +97,13 @@ func (mod *CANModule) startDumpReader() error { } else { mod.onFrame(frame) } + + // compute delay before the next frame + if i < lastEntry { + next := entries[i+1] + diff := next.Time.Sub(entry.Time) + time.Sleep(diff) + } } }()