rename can frame type enums
This commit is contained in:
parent
344353b0d6
commit
1bd003843c
43
frame.go
43
frame.go
|
@ -6,6 +6,12 @@
|
||||||
// by writing "adapters" to various devices/formats (xbee, sqlite, network socket, socketcan)
|
// by writing "adapters" to various devices/formats (xbee, sqlite, network socket, socketcan)
|
||||||
package gotelem
|
package gotelem
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
// Frame represents a protocol-agnostic CAN frame. The Id can be standard or extended,
|
// Frame represents a protocol-agnostic CAN frame. The Id can be standard or extended,
|
||||||
// but if it is extended, the Kind should be EFF.
|
// but if it is extended, the Kind should be EFF.
|
||||||
type Frame struct {
|
type Frame struct {
|
||||||
|
@ -26,10 +32,10 @@ type CANFrame interface {
|
||||||
type Kind uint8
|
type Kind uint8
|
||||||
|
|
||||||
const (
|
const (
|
||||||
SFF Kind = iota // Standard ID Frame
|
CanSFFFrame Kind = iota // Standard ID Frame
|
||||||
EFF // Extended ID Frame
|
CanEFFFrame // Extended ID Frame
|
||||||
RTR // Remote Transmission Request Frame
|
CanRTRFrame // Remote Transmission Request Frame
|
||||||
ERR // Error Frame
|
CanErrFrame // Error Frame
|
||||||
)
|
)
|
||||||
|
|
||||||
// CanFilter is a basic filter for masking out data. It has an Inverted flag
|
// CanFilter is a basic filter for masking out data. It has an Inverted flag
|
||||||
|
@ -56,3 +62,32 @@ type CanTransciever interface {
|
||||||
CanSink
|
CanSink
|
||||||
CanSource
|
CanSource
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// CanWriter
|
||||||
|
type CanWriter struct {
|
||||||
|
output *os.File
|
||||||
|
}
|
||||||
|
|
||||||
|
// send writes the frame to the file.
|
||||||
|
func (cw *CanWriter) Send(f *Frame) error {
|
||||||
|
ts := time.Now().Unix()
|
||||||
|
|
||||||
|
_, err := fmt.Fprintf(cw.output, "%d %X %X", ts, f.Id, f.Data)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cw *CanWriter) Close() error {
|
||||||
|
return cw.output.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
func OpenCanWriter(name string) (*CanWriter, error) {
|
||||||
|
f, err := os.Create(name)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
cw := &CanWriter{
|
||||||
|
output: f,
|
||||||
|
}
|
||||||
|
return cw, nil
|
||||||
|
}
|
||||||
|
|
|
@ -8,10 +8,10 @@ func _() {
|
||||||
// An "invalid array index" compiler error signifies that the constant values have changed.
|
// An "invalid array index" compiler error signifies that the constant values have changed.
|
||||||
// Re-run the stringer command to generate them again.
|
// Re-run the stringer command to generate them again.
|
||||||
var x [1]struct{}
|
var x [1]struct{}
|
||||||
_ = x[SFF-0]
|
_ = x[CanSFFFrame-0]
|
||||||
_ = x[EFF-1]
|
_ = x[CanEFFFrame-1]
|
||||||
_ = x[RTR-2]
|
_ = x[CanRTRFrame-2]
|
||||||
_ = x[ERR-3]
|
_ = x[CanErrFrame-3]
|
||||||
}
|
}
|
||||||
|
|
||||||
const _Kind_name = "SFFEFFRTRERR"
|
const _Kind_name = "SFFEFFRTRERR"
|
||||||
|
|
|
@ -134,15 +134,17 @@ func (sck *CanSocket) Send(msg *gotelem.Frame) error {
|
||||||
idToWrite := msg.Id
|
idToWrite := msg.Id
|
||||||
|
|
||||||
switch msg.Kind {
|
switch msg.Kind {
|
||||||
case gotelem.SFF:
|
case gotelem.CanSFFFrame:
|
||||||
idToWrite &= unix.CAN_SFF_MASK
|
idToWrite &= unix.CAN_SFF_MASK
|
||||||
case gotelem.EFF:
|
case gotelem.CanEFFFrame:
|
||||||
idToWrite &= unix.CAN_EFF_MASK
|
idToWrite &= unix.CAN_EFF_MASK
|
||||||
idToWrite |= unix.CAN_EFF_FLAG
|
idToWrite |= unix.CAN_EFF_FLAG
|
||||||
case gotelem.RTR:
|
case gotelem.CanRTRFrame:
|
||||||
idToWrite |= unix.CAN_RTR_FLAG
|
idToWrite |= unix.CAN_RTR_FLAG
|
||||||
default:
|
case gotelem.CanErrFrame:
|
||||||
return errors.New("you can't send error frames")
|
return errors.New("you can't send error frames")
|
||||||
|
default:
|
||||||
|
return errors.New("unknown frame type")
|
||||||
}
|
}
|
||||||
|
|
||||||
binary.LittleEndian.PutUint32(buf[:4], idToWrite)
|
binary.LittleEndian.PutUint32(buf[:4], idToWrite)
|
||||||
|
@ -186,10 +188,10 @@ func (sck *CanSocket) Recv() (*gotelem.Frame, error) {
|
||||||
var k gotelem.Kind
|
var k gotelem.Kind
|
||||||
if id&unix.CAN_EFF_FLAG != 0 {
|
if id&unix.CAN_EFF_FLAG != 0 {
|
||||||
// extended id frame
|
// extended id frame
|
||||||
k = gotelem.EFF
|
k = gotelem.CanEFFFrame
|
||||||
} else {
|
} else {
|
||||||
// it's a normal can frame
|
// it's a normal can frame
|
||||||
k = gotelem.SFF
|
k = gotelem.CanSFFFrame
|
||||||
}
|
}
|
||||||
|
|
||||||
dataLength := uint8(buf[4])
|
dataLength := uint8(buf[4])
|
||||||
|
|
|
@ -45,7 +45,7 @@ func TestCanSocket(t *testing.T) {
|
||||||
// make a packet.
|
// make a packet.
|
||||||
testFrame := &gotelem.Frame{
|
testFrame := &gotelem.Frame{
|
||||||
Id: 0x123,
|
Id: 0x123,
|
||||||
Kind: gotelem.SFF,
|
Kind: gotelem.CanSFFFrame,
|
||||||
Data: []byte{0, 1, 2, 3, 4, 5, 6, 7},
|
Data: []byte{0, 1, 2, 3, 4, 5, 6, 7},
|
||||||
}
|
}
|
||||||
err := sock.Send(testFrame)
|
err := sock.Send(testFrame)
|
||||||
|
@ -63,7 +63,7 @@ func TestCanSocket(t *testing.T) {
|
||||||
|
|
||||||
testFrame := &gotelem.Frame{
|
testFrame := &gotelem.Frame{
|
||||||
Id: 0x234,
|
Id: 0x234,
|
||||||
Kind: gotelem.SFF,
|
Kind: gotelem.CanSFFFrame,
|
||||||
Data: []byte{0, 1, 2, 3, 4, 5, 6, 7},
|
Data: []byte{0, 1, 2, 3, 4, 5, 6, 7},
|
||||||
}
|
}
|
||||||
_ = sock.Send(testFrame)
|
_ = sock.Send(testFrame)
|
||||||
|
@ -81,4 +81,5 @@ func TestCanSocket(t *testing.T) {
|
||||||
|
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// TODO: test filtering.
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue