socketcan

package
v0.0.0-...-f2c46a0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 5, 2023 License: MIT Imports: 19 Imported by: 0

Documentation

Index

Constants

View Source
const (

	// LengthOfControllerSpecificInformation is the number of error frame bytes with controller-specific information.
	LengthOfControllerSpecificInformation = 3
)

error frame flag indices.

Variables

This section is empty.

Functions

func Dial

func Dial(network, address string, opt ...DialOption) (net.Conn, error)

Dial connects to the address on the named net.

Linux only: If net is "can" it creates a SocketCAN connection to the device (address is interpreted as a device name).

If net is "udp" it assumes UDP multicast and sets up 2 connections, one for receiving and one for transmitting. See: https://golang.org/pkg/net/#Dial

func DialContext

func DialContext(ctx context.Context, network, address string, opt ...DialOption) (net.Conn, error)

DialContext connects to the address on the named net using the provided context.

Linux only: If net is "can" it creates a SocketCAN connection to the device (address is interpreted as a device name).

See: https://golang.org/pkg/net/#Dialer.DialContext

func NoLogger

func NoLogger(cfg *emulatorCfg)

NoLogger disables logging in the Emulator.

Types

type ControllerError

type ControllerError uint8
const (
	ControllerErrorUnspecified      ControllerError = 0x00
	ControllerErrorRxBufferOverflow ControllerError = 0x01
	ControllerErrorTxBufferOverflow ControllerError = 0x02
	ControllerErrorRxWarning        ControllerError = 0x04
	ControllerErrorTxWarning        ControllerError = 0x08
	ControllerErrorRxPassive        ControllerError = 0x10
	ControllerErrorTxPassive        ControllerError = 0x20 // at least one error counter exceeds 127
	ControllerErrorActive           ControllerError = 0x40
)

func (ControllerError) String

func (i ControllerError) String() string

type DialOption

type DialOption func(*dialOpts)

func WithReceiveErrorFrames

func WithReceiveErrorFrames() DialOption

WithReceiveErrorFrames returns a DialOption which enables can error frame receiving on can port.

type Emulator

type Emulator struct {
	sync.Mutex
	// contains filtered or unexported fields
}

Emulator emulates a CAN bus.

Emulator emulates a CAN bus by using UDP multicast. The emulator itself does not own the multicast group but rather establishes a common address/port pair for the CAN bus to be emulated on. Emulator exposes a thread-safe API to callees and may therefore be shared among different goroutines.

func NewEmulator

func NewEmulator(options ...EmulatorOption) (*Emulator, error)

NewEmulator creates an Emulator to emulate a CAN bus.

If no error is returned it is safe to `socketcan.Dial` the address of the Emulator. The emulator will default to using multicast group `239.64.142.206` with a random port that's decided when calling Emulator

N.B. It is not possible to simply use `net.Dial` as for UDP multicast both a transmitting connection and a writing connection. This is handled by `socketcan.Dial` under the hood.

func (*Emulator) Addr

func (e *Emulator) Addr() net.Addr

Addr returns the address of the Emulator's multicast group.

func (*Emulator) Receiver

func (e *Emulator) Receiver() (*Receiver, error)

Receiver returns a Receiver connected to the Emulator.

The emulator owns the underlying network connection an will close it when the emulator is closed.

func (*Emulator) Run

func (e *Emulator) Run(ctx context.Context) error

Run an Emulator.

This starts the listener and waits until the context is canceled before tidying up.

func (*Emulator) TransmitFrame

func (e *Emulator) TransmitFrame(ctx context.Context, f can.Frame) error

TransmitFrame sends a CAN frame to the Emulator's multicast group.

func (*Emulator) TransmitMessage

func (e *Emulator) TransmitMessage(ctx context.Context, m can.Message) error

TransmitMessage sends a CAN message to every emulator connection.

func (*Emulator) WaitForSenders

func (e *Emulator) WaitForSenders(n int, timeout time.Duration) error

WaitForSenders waits until either, n unique senders have been sending messages to the multicast group, or the timeout is reached.

type EmulatorOption

type EmulatorOption func(*emulatorCfg)

EmulatorOption represents a way to configure an Emulator prior to creating it.

func WithLogger

func WithLogger(l *log.Logger) EmulatorOption

WithLogger makes the Emulator print out status messages with the provided logger.

func WithMulticastAddress

func WithMulticastAddress(address string) EmulatorOption

WithMulticastAddress sets the address for the multicast group that the Emulator should listen on. A multicast address starts with 239.x.x.x, and using an address that does not conform to this will lead to undefined behavior.

type ErrorClass

type ErrorClass uint32
const (
	ErrorClassTxTimeout         ErrorClass = 0x00000001
	ErrorClassLostArbitration   ErrorClass = 0x00000002
	ErrorClassController        ErrorClass = 0x00000004
	ErrorClassProtocolViolation ErrorClass = 0x00000008
	ErrorClassTransceiver       ErrorClass = 0x00000010
	ErrorClassNoAck             ErrorClass = 0x00000020
	ErrorClassBusOff            ErrorClass = 0x00000040
	ErrorClassBusError          ErrorClass = 0x00000080
	ErrorClassRestarted         ErrorClass = 0x00000100
)

func (ErrorClass) String

func (i ErrorClass) String() string

type ErrorFrame

type ErrorFrame struct {
	// Class is the error class
	ErrorClass ErrorClass
	// LostArbitrationBit contains the bit number when the error class is LostArbitration.
	LostArbitrationBit uint8
	// ControllerError contains error information when the error class is Controller.
	ControllerError ControllerError
	// ProtocolViolationError contains error information when the error class is Protocol.
	ProtocolError ProtocolViolationError
	// ProtocolViolationErrorLocation contains error location when the error class is Protocol.
	ProtocolViolationErrorLocation ProtocolViolationErrorLocation
	// TransceiverError contains error information when the error class is Transceiver.
	TransceiverError TransceiverError
	// ControllerSpecificInformation contains controller-specific additional error information.
	ControllerSpecificInformation [3]byte
}

func (*ErrorFrame) String

func (e *ErrorFrame) String() string

type FrameInterceptor

type FrameInterceptor func(fr can.Frame)

FrameInterceptor provides a hook to intercept the transmission of a CAN frame. The interceptor is called if and only if the frame transmission/receival is a success.

type ProtocolViolationError

type ProtocolViolationError uint8
const (
	ProtocolViolationErrorUnspecified ProtocolViolationError = 0x00
	ProtocolViolationErrorSingleBit   ProtocolViolationError = 0x01
	ProtocolViolationErrorFrameFormat ProtocolViolationError = 0x02
	ProtocolViolationErrorBitStuffing ProtocolViolationError = 0x04
	ProtocolViolationErrorBit0        ProtocolViolationError = 0x08 // unable to send dominant bit
	ProtocolViolationErrorBit1        ProtocolViolationError = 0x10 // unable to send recessive bit
	ProtocolViolationErrorBusOverload ProtocolViolationError = 0x20
	ProtocolViolationErrorActive      ProtocolViolationError = 0x40 // active error announcement
	ProtocolViolationErrorTx          ProtocolViolationError = 0x80 // error occurred on transmission
)

func (ProtocolViolationError) String

func (i ProtocolViolationError) String() string

type ProtocolViolationErrorLocation

type ProtocolViolationErrorLocation uint8
const (
	ProtocolViolationErrorLocationUnspecified    ProtocolViolationErrorLocation = 0x00
	ProtocolViolationErrorLocationStartOfFrame   ProtocolViolationErrorLocation = 0x03
	ProtocolViolationErrorLocationID28To21       ProtocolViolationErrorLocation = 0x02 // standard frames: 10 - 3
	ProtocolViolationErrorLocationID20To18       ProtocolViolationErrorLocation = 0x06 // standard frames: 2 - 0
	ProtocolViolationErrorLocationSubstituteRTR  ProtocolViolationErrorLocation = 0x04 // standard frames: RTR
	ProtocolViolationErrorLocationIDExtension    ProtocolViolationErrorLocation = 0x05
	ProtocolViolationErrorLocationIDBits17To13   ProtocolViolationErrorLocation = 0x07
	ProtocolViolationErrorLocationIDBits12To05   ProtocolViolationErrorLocation = 0x0F
	ProtocolViolationErrorLocationIDBits04To00   ProtocolViolationErrorLocation = 0x0E
	ProtocolViolationErrorLocationRTR            ProtocolViolationErrorLocation = 0x0C
	ProtocolViolationErrorLocationReservedBit1   ProtocolViolationErrorLocation = 0x0D
	ProtocolViolationErrorLocationReservedBit0   ProtocolViolationErrorLocation = 0x09
	ProtocolViolationErrorLocationDataLengthCode ProtocolViolationErrorLocation = 0x0B
	ProtocolViolationErrorLocationData           ProtocolViolationErrorLocation = 0x0A
	ProtocolViolationErrorLocationCRCSequence    ProtocolViolationErrorLocation = 0x08
	ProtocolViolationErrorLocationCRCDelimiter   ProtocolViolationErrorLocation = 0x18
	ProtocolViolationErrorLocationACKSlot        ProtocolViolationErrorLocation = 0x19
	ProtocolViolationErrorLocationACKDelimiter   ProtocolViolationErrorLocation = 0x1B
	ProtocolViolationErrorLocationEndOfFrame     ProtocolViolationErrorLocation = 0x1A
	ProtocolViolationErrorLocationIntermission   ProtocolViolationErrorLocation = 0x12
)

func (ProtocolViolationErrorLocation) String

type Receiver

type Receiver struct {
	// contains filtered or unexported fields
}

func NewReceiver

func NewReceiver(rc io.ReadCloser, opt ...ReceiverOption) *Receiver

func (*Receiver) Close

func (r *Receiver) Close() error

func (*Receiver) Err

func (r *Receiver) Err() error

func (*Receiver) ErrorFrame

func (r *Receiver) ErrorFrame() ErrorFrame

func (*Receiver) Frame

func (r *Receiver) Frame() can.Frame

func (*Receiver) HasErrorFrame

func (r *Receiver) HasErrorFrame() bool

func (*Receiver) Receive

func (r *Receiver) Receive() bool

type ReceiverOption

type ReceiverOption func(*receiverOpts)

func ReceiverFrameInterceptor

func ReceiverFrameInterceptor(i FrameInterceptor) ReceiverOption

ReceiverFrameInterceptor returns a ReceiverOption that sets the FrameInterceptor for the receiver. Only one frame interceptor can be installed.

type TransceiverError

type TransceiverError uint8
const (
	TransceiverErrorUnspecified     TransceiverError = 0x00
	TransceiverErrorCANHNoWire      TransceiverError = 0x04
	TransceiverErrorCANHShortToBat  TransceiverError = 0x05
	TransceiverErrorCANHShortToVCC  TransceiverError = 0x06
	TransceiverErrorCANHShortToGND  TransceiverError = 0x07
	TransceiverErrorCANLNoWire      TransceiverError = 0x40
	TransceiverErrorCANLShortToBat  TransceiverError = 0x50
	TransceiverErrorCANLShortToVcc  TransceiverError = 0x60
	TransceiverErrorCANLShortToGND  TransceiverError = 0x70
	TransceiverErrorCANLShortToCANH TransceiverError = 0x80
)

func (TransceiverError) String

func (i TransceiverError) String() string

type Transmitter

type Transmitter struct {
	// contains filtered or unexported fields
}

Transmitter transmits CAN frames.

func NewTransmitter

func NewTransmitter(conn net.Conn, opt ...TransmitterOption) *Transmitter

NewTransmitter creates a new transmitter that transmits CAN frames to the provided io.Writer.

func (*Transmitter) Close

func (t *Transmitter) Close() error

Close the transmitter's underlying connection.

func (*Transmitter) TransmitFrame

func (t *Transmitter) TransmitFrame(ctx context.Context, f can.Frame) error

TransmitFrame transmits a CAN frame.

func (*Transmitter) TransmitMessage

func (t *Transmitter) TransmitMessage(ctx context.Context, m can.Message) error

TransmitMessage transmits a CAN message.

type TransmitterOption

type TransmitterOption func(*transmitterOpts)

func TransmitterFrameInterceptor

func TransmitterFrameInterceptor(i FrameInterceptor) TransmitterOption

TransmitterFrameInterceptor returns a TransmitterOption that sets the FrameInterceptor for the transmitter. Only one frame interceptor can be installed.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL