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

fix: Untangling can.Frame/canadapter.Frame by moving all to brutella/can #21

Merged
merged 4 commits into from
Oct 9, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
8 changes: 4 additions & 4 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
steps:
- uses: actions/checkout@v4
- name: Run golangci-lint
uses: golangci/golangci-lint-action@v2
uses: golangci/golangci-lint-action@v3
with:
version: v1.51.1
test:
Expand All @@ -26,11 +26,11 @@ jobs:

### ASDF Logic
- name: Setup asdf
uses: asdf-vm/actions/setup@v1
uses: asdf-vm/actions/setup@v2
- name: Install asdf plugins
uses: asdf-vm/actions/plugins-add@v1
uses: asdf-vm/actions/plugins-add@v2
- name: Install asdf languages
uses: asdf-vm/actions/install@v1
uses: asdf-vm/actions/install@v2
### END

- name: lintroller
Expand Down
2 changes: 1 addition & 1 deletion .tool-versions
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
golang 1.21.1
golang 1.21.2
golangci-lint 1.54.2
mage 1.15.0
2 changes: 0 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ module github.com/boatkit-io/n2k

go 1.21

toolchain go1.21.1

require (
github.com/Masterminds/sprig/v3 v3.2.3
github.com/boatkit-io/tugboat v0.5.1
Expand Down
5 changes: 3 additions & 2 deletions pkg/adapter/canadapter/canadapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package canadapter

import (
"github.com/brutella/can"
"github.com/sirupsen/logrus"

"github.com/boatkit-io/n2k/pkg/adapter"
Expand Down Expand Up @@ -37,7 +38,7 @@ func (c *CANAdapter) SetOutput(ph PacketHandler) {
// HandleMessage is how you tell CanAdapter to start processing a new message into a packet
func (c *CANAdapter) HandleMessage(message adapter.Message) {
switch f := message.(type) {
case *Frame:
case *can.Frame:
pInfo := NewPacketInfo(f)
packet := pkt.NewPacket(pInfo, f.Data[:])

Expand All @@ -59,7 +60,7 @@ func (c *CANAdapter) HandleMessage(message adapter.Message) {
c.packetReady(packet)
}
default:
c.log.Warnf("CanAdapter expected Frame, received: %T\n", f)
c.log.Warnf("CanAdapter expected *can.Frame, received: %T", f)
}
}

Expand Down
17 changes: 2 additions & 15 deletions pkg/adapter/canadapter/frame.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"time"

"github.com/boatkit-io/n2k/pkg/pgn"
"github.com/brutella/can"
)

// This data structure is copied from
Expand Down Expand Up @@ -35,22 +36,8 @@ SOFTWARE.

*/

// Frame defines a canbus wire frame.
type Frame struct {
// bit 0-28: CAN identifier (11/29 bit)
// bit 29: error message flag (ERR)
// bit 30: remote transmision request (RTR)
// bit 31: extended frame format (EFF)
ID uint32
Length uint8
Flags uint8
Res0 uint8
Res1 uint8
Data [8]uint8
}

// NewPacketInfo instantiates a new Packet from a canbus Frame and surrounding context.
func NewPacketInfo(message *Frame) pgn.MessageInfo {
func NewPacketInfo(message *can.Frame) pgn.MessageInfo {
p := pgn.MessageInfo{
Timestamp: time.Now(),
SourceId: uint8(message.ID & 0xFF),
Expand Down
7 changes: 4 additions & 3 deletions pkg/adapter/canadapter/multibuilder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"

"github.com/boatkit-io/n2k/pkg/pkt"
"github.com/brutella/can"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -83,7 +84,7 @@ func TestFastPacket(t *testing.T) {
m := NewMultiBuilder(log)

// test fast packet that's actually Complete in single-frame
pInfo := NewPacketInfo(&Frame{ID: CanIdFromData(130820, 10, 1, 0), Length: 8})
pInfo := NewPacketInfo(&can.Frame{ID: CanIdFromData(130820, 10, 1, 0), Length: 8})
data := []uint8{160, 5, 163, 153, 32, 128, 1, 255}
p := pkt.NewPacket(pInfo, data)
m.Add(p)
Expand All @@ -92,7 +93,7 @@ func TestFastPacket(t *testing.T) {

// we allow out of order frames
m = NewMultiBuilder(log)
p = pkt.NewPacket(NewPacketInfo(&Frame{ID: CanIdFromData(130820, 10, 1, 0), Length: 8}), []uint8{161, 5, 163, 153, 32, 128, 1, 255})
p = pkt.NewPacket(NewPacketInfo(&can.Frame{ID: CanIdFromData(130820, 10, 1, 0), Length: 8}), []uint8{161, 5, 163, 153, 32, 128, 1, 255})
m.Add(p)
assert.False(t, p.Complete)
assert.NotNil(t, m.sequences[10])
Expand All @@ -106,7 +107,7 @@ func TestFastPacket(t *testing.T) {
// not much of a test) might be a fast variant, but it's a weak heuristic.
// Instead we'll return each packet as unknown.
m = NewMultiBuilder(log)
pInfo = NewPacketInfo(&Frame{ID: 0x09F20183})
pInfo = NewPacketInfo(&can.Frame{ID: 0x09F20183})
data = []uint8{0x60, 0x20, 0x00, 0x10, 0x13, 0x80, 0x0C, 0x70}
p = pkt.NewPacket(pInfo, data)
m.Add(p)
Expand Down
6 changes: 4 additions & 2 deletions pkg/adapter/canadapter/testutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package canadapter
import (
"strconv"
"strings"

"github.com/brutella/can"
)

// CanFrameFromRaw parses an input string into a can.Frame.
func CanFrameFromRaw(in string) Frame {
func CanFrameFromRaw(in string) can.Frame {
elems := strings.Split(in, ",")
priority, _ := strconv.ParseUint(elems[1], 10, 8)
pgn, _ := strconv.ParseUint(elems[2], 10, 32)
Expand All @@ -15,7 +17,7 @@ func CanFrameFromRaw(in string) Frame {
length, _ := strconv.ParseUint(elems[5], 10, 8)

id := CanIdFromData(uint32(pgn), uint8(source), uint8(priority), uint8(destination))
retval := Frame{
retval := can.Frame{
ID: id,
Length: 8,
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/endpoint/n2kfileendpoint/n2kfileendpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
"time"

"github.com/boatkit-io/n2k/pkg/adapter"
"github.com/boatkit-io/n2k/pkg/adapter/canadapter"
"github.com/boatkit-io/n2k/pkg/endpoint"
"github.com/brutella/can"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
Expand Down Expand Up @@ -69,7 +69,7 @@ func (n *N2kFileEndpoint) Run(ctx context.Context) error {
if len(line) == 0 {
continue
}
var frame canadapter.Frame
var frame can.Frame
var canDead string
var timeDelta float32
fmt.Sscanf(line, " (%f) %s %8X [%d] %X %X %X %X %X %X %X %X", &timeDelta, &canDead, &frame.ID, &frame.Length, &frame.Data[0], &frame.Data[1], &frame.Data[2], &frame.Data[3], &frame.Data[4], &frame.Data[5], &frame.Data[6], &frame.Data[7])
Expand Down
Loading