can

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: May 22, 2020 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package can provides an implemention of a CAN bus to send and receive CAN frames.

Index

Constants

View Source
const (
	// MaskIDSff is used to extract the valid 11-bit CAN identifier bits from the frame ID of a standard frame format.
	MaskIDSff = 0x000007FF
	// MaskIDEff is used to extract the valid 29-bit CAN identifier bits from the frame ID of an extended frame format.
	MaskIDEff = 0x1FFFFFFF
	// MaskErr is used to extract the the error flag (0 = data frame, 1 = error message) from the frame ID.
	MaskErr = 0x20000000
	// MaskRtr is used to extract the rtr flag (1 = rtr frame) from the frame ID
	MaskRtr = 0x40000000
	// MaskEff is used to extract the eff flag (0 = standard frame, 1 = extended frame) from the frame ID
	MaskEff = 0x80000000
)
View Source
const (
	// MaxFrameDataLength defines the max length of a CAN data frame defined in ISO 11898-1.
	MaxFrameDataLength = 8
	// MaxExtFrameDataLength defines the max length of an CAN extended data frame defined in ISO ISO 11898-7.
	MaxExtFrameDataLength = 64
)
View Source
const AF_CAN = syscall.AF_CAN

Variables

View Source
var ErrorKernelFilterNotSupported = errors.New("Not possible to set kernel filter.")

ErrorKernelFilterNotSupported is returned if the socket attribute is 0. Then the method setsockopt can't be called.

Functions

func Marshal

func Marshal(frm Frame) (b []byte, err error)

Marshal returns the byte encoding of frm.

func NewSockaddr

func NewSockaddr(proto uint16, Ifindex int) syscall.Sockaddr

func Unmarshal

func Unmarshal(b []byte, frm *Frame) (err error)

Unmarshal parses the bytes b and stores the result in the value pointed to by frm.

func Wait

func Wait(bus *Bus, id uint32, timeout time.Duration) <-chan WaitResponse

Wait returns a channel, which receives a frame or an error, if the frame with the expected id didn't arrive on time.

Types

type Bus

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

Bus represents the CAN bus. Handlers can subscribe to receive frames. Frame are sent using the *Publish* method.

func NewBus

func NewBus(rwc ReadWriteCloser) *Bus

NewBus returns a new CAN bus.

func NewBusForInterfaceWithName

func NewBusForInterfaceWithName(ifaceName string) (*Bus, error)

NewBusForInterfaceWithName returns a bus from the network interface with name ifaceName.

func (*Bus) ConnectAndPublish

func (b *Bus) ConnectAndPublish() error

ConnectAndPublish starts handling CAN frames to publish them to handlers.

func (*Bus) DeleteFilter

func (b *Bus) DeleteFilter() error

DeleteFilter deltes all kernel filter.

func (*Bus) Disconnect

func (b *Bus) Disconnect() error

Disconnect stops handling CAN frames.

func (*Bus) Publish

func (b *Bus) Publish(frame Frame) error

Publish publishes a frame on the bus.

Frames publishes with the Publish methods are not received by handlers.

func (*Bus) SetFilter

func (b *Bus) SetFilter(filter []unix.CanFilter) error

SetFilter sets CAN filter on kernel level.

func (*Bus) Subscribe

func (b *Bus) Subscribe(handler Handler)

Subscribe adds a handler to the bus.

func (*Bus) SubscribeFunc

func (b *Bus) SubscribeFunc(fn HandlerFunc)

SubscribeFunc adds a function as handler.

func (*Bus) Unsubscribe

func (b *Bus) Unsubscribe(handler Handler)

Unsubscribe removes a handler.

type 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   [MaxFrameDataLength]uint8
}

Frame represents a standard CAN data frame

type Handler

type Handler interface {
	Handle(frame Frame)
}

The Handler interfaces defines a method to receive a frame.

func NewHandler

func NewHandler(fn HandlerFunc) Handler

NewHandler returns a new handler which calls fn when a frame is received.

type HandlerFunc

type HandlerFunc func(frame Frame)

HandlerFunc defines the function type to handle a frame.

type ReadWriteCloser

type ReadWriteCloser interface {
	Reader
	Writer

	io.Closer
	// contains filtered or unexported methods
}

The ReadWriteCloser interface combines the Reader and Writer and `io.Closer` interface.

func NewEchoReadWriteCloser

func NewEchoReadWriteCloser() ReadWriteCloser

NewEchoReadWriteCloser returns a ReadWriteCloser which echoes received bytes.

func NewReadWriteCloser

func NewReadWriteCloser(rwc io.ReadWriteCloser) ReadWriteCloser

NewReadWriteCloser returns a ReadWriteCloser for an `io.ReadWriteCloser`.

func NewReadWriteCloserForInterface

func NewReadWriteCloserForInterface(i *net.Interface) (ReadWriteCloser, error)

type Reader

type Reader interface {
	io.Reader
	ReadFrame(*Frame) error
}

The Reader interface extends the `io.Reader` interface by method to read a frame.

type WaitResponse

type WaitResponse struct {
	Frame Frame
	Err   error
}

A WaitResponse encapsulates the response of waiting for a frame.

type Writer

type Writer interface {
	io.Writer
	WriteFrame(Frame) error
}

The Writer interface extends the `io.Writer` interface by method to write a frame.

Jump to

Keyboard shortcuts

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