Skip to content

Commit

Permalink
Merge branch 'fix-linux'
Browse files Browse the repository at this point in the history
  • Loading branch information
brutella committed Dec 15, 2018
2 parents 214cee1 + 3278841 commit 2fd43cd
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 33 deletions.
7 changes: 7 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,16 @@ VERSION=$(shell git describe --exact-match --tags 2>/dev/null)
BUILD_DIR=build
PACKAGE_RPI=candump-$(VERSION)_linux_armhf

# unset GOPATH to us Go modules
unexport GOPATH

test:
$(GOTEST) -v ./...

clean:
$(GOCLEAN)
rm -rf $(BUILD_DIR)

package-rpi: build-rpi
tar -cvzf $(PACKAGE_RPI).tar.gz -C $(BUILD_DIR) $(PACKAGE_RPI)

Expand Down
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/brutella/can

require golang.org/x/sys v0.0.0-20181213200352-4d1cda033e06
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
golang.org/x/sys v0.0.0-20181213200352-4d1cda033e06 h1:0oC8rFnE+74kEmuHZ46F6KHsMr5Gx2gUQPuNz28iQZM=
golang.org/x/sys v0.0.0-20181213200352-4d1cda033e06/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
33 changes: 0 additions & 33 deletions readwritecloser.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
package can

import (
"fmt"
"io"
"net"
"os"
"syscall"
)

// The Reader interface extends the `io.Reader` interface by method
Expand All @@ -31,39 +27,10 @@ type ReadWriteCloser interface {
io.Closer
}

// Socket protocols
const (
Raw uint8 = 1 // CAN_RAW
Bcm uint8 = 2 // CAN_BCM
TP16 uint8 = 3
TP20 uint8 = 4
MCNet uint8 = 5
ISOTp uint8 = 6
)

type readWriteCloser struct {
rwc io.ReadWriteCloser
}

// NewReadWriteCloserForInterface returns a ReadWriteCloser for a network interface.
func NewReadWriteCloserForInterface(i *net.Interface) (ReadWriteCloser, error) {
proto := Raw
s, err := syscall.Socket(AF_CAN, syscall.SOCK_RAW, int(proto) /* 0? */)
if err != nil {
return nil, err
}

addr := NewSockaddr(uint16(proto) /* can.AF_CAN? */, i.Index /* 0 for all interfaces? */)

if err := syscall.Bind(s, addr); err != nil {
return nil, err
}

f := os.NewFile(uintptr(s), fmt.Sprintf("fd %d", s))

return &readWriteCloser{f}, nil
}

// NewReadWriteCloser returns a ReadWriteCloser for an `io.ReadWriteCloser`.
func NewReadWriteCloser(rwc io.ReadWriteCloser) ReadWriteCloser {
return &readWriteCloser{rwc}
Expand Down
10 changes: 10 additions & 0 deletions readwritecloser_darwin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package can

import (
"fmt"
"net"
)

func NewReadWriteCloserForInterface(i *net.Interface) (ReadWriteCloser, error) {
return nil, fmt.Errorf("Binding to can interface no supported on Darwin")
}
21 changes: 21 additions & 0 deletions readwritecloser_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package can

import (
"fmt"
"golang.org/x/sys/unix"
"net"
"os"
"syscall"
)

func NewReadWriteCloserForInterface(i *net.Interface) (ReadWriteCloser, error) {
s, _ := syscall.Socket(syscall.AF_CAN, syscall.SOCK_RAW, unix.CAN_RAW)
addr := &unix.SockaddrCAN{Ifindex: i.Index}
if err := unix.Bind(s, addr); err != nil {
return nil, err
}

f := os.NewFile(uintptr(s), fmt.Sprintf("fd %d", s))

return &readWriteCloser{f}, nil
}

0 comments on commit 2fd43cd

Please sign in to comment.