Skip to content

Commit

Permalink
added support for extended isotp ids and isotp padding
Browse files Browse the repository at this point in the history
  • Loading branch information
ericevenchick committed Jan 25, 2019
1 parent 5b58573 commit aea71f3
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 3 deletions.
20 changes: 20 additions & 0 deletions pkg/socketcan/byteorder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package socketcan

import (
"unsafe"
"encoding/binary"
)

func getEndianness() binary.ByteOrder {
buf := [2]byte{}
*(*uint16)(unsafe.Pointer(&buf[0])) = uint16(0xABCD)

switch buf {
case [2]byte{0xCD, 0xAB}:
return binary.LittleEndian
case [2]byte{0xAB, 0xCD}:
return binary.BigEndian
default:
panic("Could not determine native endianness.")
}
}
52 changes: 49 additions & 3 deletions pkg/socketcan/interface_isotp.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,29 @@ package socketcan

import (
"fmt"
"encoding/binary"
"bytes"

"golang.org/x/sys/unix"
)

type CanIsotpOptions struct {
flags uint32
frame_txtime uint32
ext_address uint8
txpad_content uint8
rxpad_content uint8
rx_ext_address uint8
}


const SYS_SETSOCKOPT = 54
const SOL_CAN_BASE = 100
const SOL_CAN_ISOTP = 106
const CAN_ISOTP_OPTS = 1
const CAN_EFF_FLAG = 0x80000000

const CAN_ISOTP_EXTEND_ADDR = 0x002
const CAN_ISOTP_TX_PADDING = 0x004
const CAN_ISOTP_RX_PADDING = 0x008

Expand All @@ -24,6 +42,14 @@ func NewIsotpInterface(ifName string, rxID uint32, txID uint32) (Interface, erro
return canIf, err
}

// set extended ID flags if required
if rxID > 0x7FF {
rxID |= CAN_EFF_FLAG
}
if txID > 0x7FF {
txID |= CAN_EFF_FLAG
}

addr := &unix.SockaddrCAN{Ifindex: ifIndex, RxID: rxID, TxID: txID}
if err = unix.Bind(fd, addr); err != nil {
return canIf, err
Expand All @@ -41,6 +67,14 @@ func (i Interface) Rebind(rxID uint32, txID uint32) error {
return err
}

// set extended ID flags if required
if rxID > 0x7FF {
rxID |= CAN_EFF_FLAG
}
if txID > 0x7FF {
txID |= CAN_EFF_FLAG
}

addr := &unix.SockaddrCAN{Ifindex: ifIndex, RxID: rxID, TxID: txID}
if err = unix.Bind(i.SocketFd, addr); err != nil {
return err
Expand Down Expand Up @@ -72,7 +106,19 @@ func (i Interface) RecvBuf() ([]byte, error) {
return data[:len], nil
}

func (i Interface) SetTxPadding(on bool) error {
// TODO
return nil
func (i Interface) SetTxPadding(on bool, value uint8) error {
var buf bytes.Buffer
opts := CanIsotpOptions{}
if on {
opts.flags = CAN_ISOTP_TX_PADDING
}
opts.txpad_content = value

err := binary.Write(&buf, getEndianness(), opts)
if err != nil {
return err
}

err = unix.SetsockoptString(i.SocketFd, SOL_CAN_ISOTP, CAN_ISOTP_OPTS, buf.String())
return err
}
32 changes: 32 additions & 0 deletions test/isotp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,35 @@ func TestIsotpWrongFunctions(t *testing.T) {

closeRawInterface(t, dev)
}

func TestIsotpTxPadding(t *testing.T) {
txData := make([]byte, 10)
for i := 0; i < 10; i++ {
txData[i] = byte(i)
}

txDev := openIsotpInterface(t, 0x100, 0x200)
rxDev := openIsotpInterface(t, 0x200, 0x100)
txDev.SetTxPadding(true, 0xAA)
rxDev.SetTxPadding(true, 0xBB)

go func() {
time.Sleep(50 * time.Millisecond)
err := txDev.SendBuf(txData)
if err != nil {
t.Errorf("error sending frame: %v", err)
}
}()

rxData, err := rxDev.RecvBuf()
if err != nil {
t.Errorf("error receiving frame: %v", err)
}

if !bytes.Equal(rxData, txData) {
t.Error("sent and received data does not match")
}

closeIsotpInterface(t, txDev)
closeIsotpInterface(t, rxDev)
}

0 comments on commit aea71f3

Please sign in to comment.