Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Andrewi/97 canlink bringup #99

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions canlink/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,32 @@ during a test and dumps it into a file.

### Usage

1) Create an instance of Tracer with the _NewTracer()_ function.
1) Create an instance of Tracer with the `NewTracer()` function.
A can interface, directory and logger must be provided as arguments.
Functional options are available of type _TracerOption_ if required.
Functional options are available of type `TracerOption` if required.

```go
func main() {
writers := make([]writer.WriterIface, 0)
jsonWriter := writer.NewWriter(
logger,
".jsonl",
)
asciiWriter := writer.NewWriter(
logger,
".asc",
)
writers = append(writers, jsonWriter)
writers = append(writers, asciiWriter)

tracer := canlink.NewTracer(
"can0",
"/opt/traces",
logger,
connection,
canlink.WithBusName("PT"),
canlink.WithTimeout(5*time.Minute))
canlink.WithWriters(writers)
}
```
2) Open the Tracer using _Open()_
Expand Down
89 changes: 0 additions & 89 deletions canlink/ascii.go

This file was deleted.

18 changes: 11 additions & 7 deletions canlink/canclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package canlink
import (
"context"
"net"
"strings"

"go.einride.tech/can"
"go.einride.tech/can/pkg/generated"
Expand All @@ -13,7 +14,8 @@ import (
)

const (
_canClientLoggerName = "can_client"
_canClientLoggerName = "can_client"
_idNotInDatabaseErrorIndicator = "ID not in database"
)

// MessagesDescriptor is an interface mirroring the MessagesDescriptor struct found in Einride DBCs.
Expand Down Expand Up @@ -95,12 +97,14 @@ func (c *CanClient) Read(ctx context.Context, msgsToRead ...generated.Message) (
return nil, nil
case frame := <-c.rxChan:
msg, err := c.md.UnmarshalFrame(frame)
if err != nil && !isIdNotInDatabaseError(err) {
return nil, errors.Wrap(err, "unmarshal frame")
} else if isIdNotInDatabaseError(err) {
c.l.Debug("found a message we do not recognize")
// Here we have simply read a can frame that we do not know how to unmarshal, continue to next frame.
continue
if err != nil {
if strings.Contains(err.Error(), _idNotInDatabaseErrorIndicator) {
c.l.Debug("found a message we do not recognize")
// Here we have simply read a can frame that we do not know how to unmarshal, continue to next frame.
continue
} else {
return nil, errors.Wrap(err, "unmarshal frame")
}
}

c.l.Debug("read a message", zap.Uint32("id", msg.Frame().ID))
Expand Down
99 changes: 99 additions & 0 deletions canlink/canlink_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package canlink

import (
"context"
"testing"
"time"

"go.einride.tech/can/pkg/socketcan"
"go.uber.org/zap"

"github.com/macformula/hil/canlink/writer"
)

const (
// Can bus select
_busName = "veh"
_canIface = "vcan0"

// Env config
_timeFormat = "2006-01-02_15-04-05"
_logFilenameFormat = ".logs.asc"
_traceDir = "./traces"
_logLevel = zap.DebugLevel
)

func TestTracer(t *testing.T) {
tracer, logger, teardown := setup(t)
defer teardown(t, tracer, logger)

time.Sleep(5 * time.Second)

}

func setup(t *testing.T) (*Tracer, *zap.Logger, func(*testing.T, *Tracer, *zap.Logger)) {
ctx := context.Background()
logFileName := _logFilenameFormat

loggerConfig := zap.NewDevelopmentConfig()
loggerConfig.OutputPaths = []string{logFileName}
loggerConfig.Level = zap.NewAtomicLevelAt(_logLevel)
logger, err := loggerConfig.Build()
if err != nil {
t.Logf("Failed to create logger. Error: %v", err)
}

conn, err := socketcan.DialContext(context.Background(), "can", _canIface)
if err != nil {
t.Fatalf("Failed to create socket can connection. Error: %v", err)
}

writers := make([]writer.WriterIface, 0)
jsonWriter := writer.NewWriter(
logger,
".jsonl",
)
asciiWriter := writer.NewWriter(
logger,
".asc",
)
writers = append(writers, jsonWriter)
writers = append(writers, asciiWriter)

tracer := NewTracer(
_canIface,
_traceDir,
logger,
conn,
WithBusName(_busName),
WithWriters(writers))

err = tracer.Open(ctx)
if err != nil {
t.Fatalf("Error opening tracer. Error: %v", err)
}
err = tracer.StartTrace(ctx)
if err != nil {
t.Fatalf("Error starting trace. Error: %v", err)
}

teardown := func(t *testing.T, tracer *Tracer, logger *zap.Logger) {
logger.Info("closing trace test")

err = tracer.StopTrace()
if err != nil {
t.Logf("Failed to stop trace. Error: %v", err)
}

err = tracer.Close()
if err != nil {
t.Logf("Failed to close tracer. Error: %v", err)
}

if tracer.Error() != nil {
t.Logf("Tracer returned with error set. Error: %v", tracer.Error().Error())
}
}

return tracer, logger, teardown
}
Loading
Loading