2023-04-22 07:48:04 +00:00
|
|
|
// Package can provides generic CAN interfaces and types.
|
|
|
|
//
|
|
|
|
// It has a generic can Frame (packet), as well as a filter type.
|
|
|
|
// we also define standard interfaces for objects that can accept
|
2024-02-12 15:45:23 +00:00
|
|
|
// can frames. We can use this pattern to easily extend the capabilities of the program
|
|
|
|
// by writing "adapters" to various devices/formats (xbee, socketcan)
|
|
|
|
package can
|
2023-04-15 15:03:35 +00:00
|
|
|
|
2024-02-12 15:45:23 +00:00
|
|
|
type CanID struct {
|
2024-03-07 12:18:49 +00:00
|
|
|
Id uint32
|
2024-02-12 15:45:23 +00:00
|
|
|
Extended bool // since the id itself is not enough.
|
|
|
|
}
|
2024-03-07 12:18:49 +00:00
|
|
|
|
2023-04-22 07:48:04 +00:00
|
|
|
// Frame represents a protocol-agnostic CAN frame. The Id can be standard or extended,
|
|
|
|
// but if it is extended, the Kind should be EFF.
|
2023-04-15 15:03:35 +00:00
|
|
|
type Frame struct {
|
2024-03-07 12:18:49 +00:00
|
|
|
Id CanID
|
2023-04-16 07:46:41 +00:00
|
|
|
Data []byte
|
2023-04-15 15:03:35 +00:00
|
|
|
Kind Kind
|
|
|
|
}
|
|
|
|
|
2024-02-12 15:45:23 +00:00
|
|
|
// TODO: should this be replaced
|
2023-05-03 15:38:37 +00:00
|
|
|
type CANFrame interface {
|
2024-03-07 12:18:49 +00:00
|
|
|
Id()
|
2023-05-03 15:38:37 +00:00
|
|
|
Data() []byte
|
|
|
|
Type() Kind
|
|
|
|
}
|
|
|
|
|
2023-04-15 15:03:35 +00:00
|
|
|
//go:generate stringer -output=frame_kind.go -type Kind
|
2023-04-22 07:48:04 +00:00
|
|
|
|
|
|
|
// Kind is the type of the can Frame
|
2023-04-15 15:03:35 +00:00
|
|
|
type Kind uint8
|
|
|
|
|
|
|
|
const (
|
2024-02-12 15:45:23 +00:00
|
|
|
CanDataFrame Kind = iota // Standard ID Frame
|
2024-03-07 12:18:49 +00:00
|
|
|
CanRTRFrame // Remote Transmission Request Frame
|
|
|
|
CanErrFrame // Error Frame
|
2023-04-15 15:03:35 +00:00
|
|
|
)
|
|
|
|
|
2023-04-22 07:48:04 +00:00
|
|
|
// CanFilter is a basic filter for masking out data. It has an Inverted flag
|
|
|
|
// which indicates opposite behavior (reject all packets that match Id and Mask).
|
|
|
|
// The filter matches when (packet.Id & filter.Mask) == filter.Id
|
2023-09-19 19:17:22 +00:00
|
|
|
// TODO: is this needed anymore since we are using firmware based version instead?
|
2023-04-16 07:46:41 +00:00
|
|
|
type CanFilter struct {
|
|
|
|
Id uint32
|
|
|
|
Mask uint32
|
|
|
|
Inverted bool
|
|
|
|
}
|
2023-04-15 15:03:35 +00:00
|
|
|
|
2023-04-22 07:48:04 +00:00
|
|
|
// CanSink is an object that can accept Frames to transmit.
|
2023-04-15 15:03:35 +00:00
|
|
|
type CanSink interface {
|
2023-04-16 07:46:41 +00:00
|
|
|
Send(*Frame) error
|
2023-04-15 15:03:35 +00:00
|
|
|
}
|
|
|
|
|
2023-04-22 07:48:04 +00:00
|
|
|
// CanSource is an object that can receive Frames.
|
2023-04-15 15:03:35 +00:00
|
|
|
type CanSource interface {
|
2023-04-15 20:41:51 +00:00
|
|
|
Recv() (*Frame, error)
|
2023-04-15 15:03:35 +00:00
|
|
|
}
|
2023-04-20 20:26:29 +00:00
|
|
|
|
2023-04-22 07:48:04 +00:00
|
|
|
// CanTransciever is an object that can both send and receive Frames.
|
2023-04-20 20:26:29 +00:00
|
|
|
type CanTransciever interface {
|
|
|
|
CanSink
|
|
|
|
CanSource
|
|
|
|
}
|