diff --git a/pkg/socketcan/frame.go b/pkg/socketcan/frame.go index 327d44e..0b2a3f5 100644 --- a/pkg/socketcan/frame.go +++ b/pkg/socketcan/frame.go @@ -60,7 +60,7 @@ const ( // The interceptor is called if and only if the frame transmission/receival is a success. type FrameInterceptor func(fr can.Frame) -// frame represents a SocketCAN frame. +// Frame represents a SocketCAN frame. // // The format specified in the Linux SocketCAN kernel module: // @@ -72,7 +72,7 @@ type FrameInterceptor func(fr can.Frame) // __u8 __res1; /* reserved / padding */ // __u8 data[8] __attribute__((aligned(8))); // }; -type frame struct { +type Frame struct { // idAndFlags is the combined CAN ID and flags. idAndFlags uint32 // dataLengthCode is the frame payload length in bytes. @@ -83,21 +83,21 @@ type frame struct { data [8]byte } -func (f *frame) unmarshalBinary(b []byte) { +func (f *Frame) UnmarshalBinary(b []byte) { _ = b[lengthOfFrame-1] // bounds check f.idAndFlags = binary.LittleEndian.Uint32(b[indexOfID : indexOfID+lengthOfID]) f.dataLengthCode = b[indexOfDataLengthCode] copy(f.data[:], b[indexOfData:lengthOfFrame]) } -func (f *frame) marshalBinary(b []byte) { +func (f *Frame) MarshalBinary(b []byte) { _ = b[lengthOfFrame-1] // bounds check binary.LittleEndian.PutUint32(b[indexOfID:indexOfID+lengthOfID], f.idAndFlags) b[indexOfDataLengthCode] = f.dataLengthCode copy(b[indexOfData:], f.data[:]) } -func (f *frame) decodeFrame() can.Frame { +func (f *Frame) DecodeFrame() can.Frame { return can.Frame{ ID: f.id(), Length: f.dataLengthCode, @@ -107,7 +107,7 @@ func (f *frame) decodeFrame() can.Frame { } } -func (f *frame) encodeFrame(cf can.Frame) { +func (f *Frame) EncodeFrame(cf can.Frame) { f.idAndFlags = cf.ID if cf.IsRemote { f.idAndFlags |= idFlagRemote @@ -119,26 +119,26 @@ func (f *frame) encodeFrame(cf can.Frame) { f.data = cf.Data } -func (f *frame) isExtended() bool { +func (f *Frame) isExtended() bool { return f.idAndFlags&idFlagExtended > 0 } -func (f *frame) isRemote() bool { +func (f *Frame) isRemote() bool { return f.idAndFlags&idFlagRemote > 0 } -func (f *frame) isError() bool { +func (f *Frame) IsError() bool { return f.idAndFlags&idFlagError > 0 } -func (f *frame) id() uint32 { +func (f *Frame) id() uint32 { if f.isExtended() { return f.idAndFlags & idMaskExtended } return f.idAndFlags & idMaskStandard } -func (f *frame) decodeErrorFrame() ErrorFrame { +func (f *Frame) DecodeErrorFrame() ErrorFrame { return ErrorFrame{ ErrorClass: f.errorClass(), LostArbitrationBit: f.lostArbitrationBit(), @@ -150,31 +150,31 @@ func (f *frame) decodeErrorFrame() ErrorFrame { } } -func (f *frame) errorClass() ErrorClass { +func (f *Frame) errorClass() ErrorClass { return ErrorClass(f.idAndFlags &^ idFlagError) } -func (f *frame) lostArbitrationBit() uint8 { +func (f *Frame) lostArbitrationBit() uint8 { return f.data[indexOfLostArbitrationBit] } -func (f *frame) controllerError() ControllerError { +func (f *Frame) controllerError() ControllerError { return ControllerError(f.data[indexOfControllerError]) } -func (f *frame) protocolError() ProtocolViolationError { +func (f *Frame) protocolError() ProtocolViolationError { return ProtocolViolationError(f.data[indexOfProtocolError]) } -func (f *frame) protocolErrorLocation() ProtocolViolationErrorLocation { +func (f *Frame) protocolErrorLocation() ProtocolViolationErrorLocation { return ProtocolViolationErrorLocation(f.data[indexOfProtocolViolationErrorLocation]) } -func (f *frame) transceiverError() TransceiverError { +func (f *Frame) transceiverError() TransceiverError { return TransceiverError(f.data[indexOfTransceiverError]) } -func (f *frame) controllerSpecificInformation() [LengthOfControllerSpecificInformation]byte { +func (f *Frame) controllerSpecificInformation() [LengthOfControllerSpecificInformation]byte { var ret [LengthOfControllerSpecificInformation]byte start := indexOfControllerSpecificInformation end := start + LengthOfControllerSpecificInformation diff --git a/pkg/socketcan/frame_test.go b/pkg/socketcan/frame_test.go index c85d3da..4a213cd 100644 --- a/pkg/socketcan/frame_test.go +++ b/pkg/socketcan/frame_test.go @@ -14,10 +14,10 @@ func TestFrame_MarshalUnmarshalBinary_Property_Idempotent(t *testing.T) { return data } g := func(data [lengthOfFrame]byte) [lengthOfFrame]byte { - var f frame - f.unmarshalBinary(data[:]) + var f Frame + f.UnmarshalBinary(data[:]) var newData [lengthOfFrame]byte - f.marshalBinary(newData[:]) + f.MarshalBinary(newData[:]) return newData } assert.NilError(t, quick.CheckEqual(f, g, nil)) @@ -27,7 +27,7 @@ func TestFrame_EncodeDecode(t *testing.T) { for _, tt := range []struct { msg string frame can.Frame - socketCANFrame frame + socketCANFrame Frame }{ { msg: "data", @@ -36,7 +36,7 @@ func TestFrame_EncodeDecode(t *testing.T) { Length: 8, Data: can.Data{1, 2, 3, 4, 5, 6, 7, 8}, }, - socketCANFrame: frame{ + socketCANFrame: Frame{ idAndFlags: 0x00000001, dataLengthCode: 8, data: [8]byte{1, 2, 3, 4, 5, 6, 7, 8}, @@ -48,7 +48,7 @@ func TestFrame_EncodeDecode(t *testing.T) { ID: 0x00000001, IsExtended: true, }, - socketCANFrame: frame{ + socketCANFrame: Frame{ idAndFlags: 0x80000001, }, }, @@ -58,7 +58,7 @@ func TestFrame_EncodeDecode(t *testing.T) { ID: 0x00000001, IsRemote: true, }, - socketCANFrame: frame{ + socketCANFrame: Frame{ idAndFlags: 0x40000001, }, }, @@ -69,38 +69,38 @@ func TestFrame_EncodeDecode(t *testing.T) { IsExtended: true, IsRemote: true, }, - socketCANFrame: frame{ + socketCANFrame: Frame{ idAndFlags: 0xc0000001, }, }, } { t.Run(tt.msg, func(t *testing.T) { t.Run("encode", func(t *testing.T) { - var actual frame - actual.encodeFrame(tt.frame) + var actual Frame + actual.EncodeFrame(tt.frame) assert.Equal(t, tt.socketCANFrame, actual) }) t.Run("decode", func(t *testing.T) { - assert.Equal(t, tt.frame, tt.socketCANFrame.decodeFrame()) + assert.Equal(t, tt.frame, tt.socketCANFrame.DecodeFrame()) }) }) } } func TestFrame_IsError(t *testing.T) { - assert.Assert(t, (&frame{idAndFlags: 0x20000001}).isError()) - assert.Assert(t, !(&frame{idAndFlags: 0x00000001}).isError()) + assert.Assert(t, (&Frame{idAndFlags: 0x20000001}).IsError()) + assert.Assert(t, !(&Frame{idAndFlags: 0x00000001}).IsError()) } func TestFrame_DecodeErrorFrame(t *testing.T) { for _, tt := range []struct { msg string - f frame + f Frame expected ErrorFrame }{ { msg: "lost arbitration", - f: frame{ + f: Frame{ idAndFlags: 0x20000002, dataLengthCode: 8, data: [8]byte{ @@ -114,7 +114,7 @@ func TestFrame_DecodeErrorFrame(t *testing.T) { }, { msg: "controller", - f: frame{ + f: Frame{ idAndFlags: 0x20000004, dataLengthCode: 8, data: [8]byte{ @@ -129,7 +129,7 @@ func TestFrame_DecodeErrorFrame(t *testing.T) { }, { msg: "protocol violation", - f: frame{ + f: Frame{ idAndFlags: 0x20000008, dataLengthCode: 8, data: [8]byte{ @@ -147,7 +147,7 @@ func TestFrame_DecodeErrorFrame(t *testing.T) { }, { msg: "transceiver", - f: frame{ + f: Frame{ idAndFlags: 0x20000010, dataLengthCode: 8, data: [8]byte{ @@ -165,7 +165,7 @@ func TestFrame_DecodeErrorFrame(t *testing.T) { }, { msg: "controller-specific information", - f: frame{ + f: Frame{ idAndFlags: 0x20000001, dataLengthCode: 8, data: [8]byte{ @@ -186,7 +186,7 @@ func TestFrame_DecodeErrorFrame(t *testing.T) { }, } { t.Run(tt.msg, func(t *testing.T) { - assert.Equal(t, tt.expected, tt.f.decodeErrorFrame()) + assert.Equal(t, tt.expected, tt.f.DecodeErrorFrame()) }) } } diff --git a/pkg/socketcan/receiver.go b/pkg/socketcan/receiver.go index aa5d52c..24c268c 100644 --- a/pkg/socketcan/receiver.go +++ b/pkg/socketcan/receiver.go @@ -17,7 +17,7 @@ type Receiver struct { opts receiverOpts rc io.ReadCloser sc *bufio.Scanner - frame frame + frame Frame } func NewReceiver(rc io.ReadCloser, opt ...ReceiverOption) *Receiver { @@ -44,26 +44,26 @@ func scanFrames(data []byte, _ bool) (int, []byte, error) { func (r *Receiver) Receive() bool { ok := r.sc.Scan() - r.frame = frame{} + r.frame = Frame{} if ok { - r.frame.unmarshalBinary(r.sc.Bytes()) + r.frame.UnmarshalBinary(r.sc.Bytes()) if r.opts.frameInterceptor != nil { - r.opts.frameInterceptor(r.frame.decodeFrame()) + r.opts.frameInterceptor(r.frame.DecodeFrame()) } } return ok } func (r *Receiver) HasErrorFrame() bool { - return r.frame.isError() + return r.frame.IsError() } func (r *Receiver) Frame() can.Frame { - return r.frame.decodeFrame() + return r.frame.DecodeFrame() } func (r *Receiver) ErrorFrame() ErrorFrame { - return r.frame.decodeErrorFrame() + return r.frame.DecodeErrorFrame() } func (r *Receiver) Err() error { diff --git a/pkg/socketcan/transmitter.go b/pkg/socketcan/transmitter.go index 2d61e1b..6efc002 100644 --- a/pkg/socketcan/transmitter.go +++ b/pkg/socketcan/transmitter.go @@ -43,10 +43,10 @@ func (t *Transmitter) TransmitMessage(ctx context.Context, m can.Message) error // TransmitFrame transmits a CAN frame. func (t *Transmitter) TransmitFrame(ctx context.Context, f can.Frame) error { - var scf frame - scf.encodeFrame(f) + var scf Frame + scf.EncodeFrame(f) data := make([]byte, lengthOfFrame) - scf.marshalBinary(data) + scf.MarshalBinary(data) if deadline, ok := ctx.Deadline(); ok { if err := t.conn.SetWriteDeadline(deadline); err != nil { return fmt.Errorf("transmit frame: %w", err)