Skip to content

Commit

Permalink
Issue #60, fix onewire not present, and hanging when it is. (#61)
Browse files Browse the repository at this point in the history
  • Loading branch information
gsexton authored Sep 23, 2024
1 parent b69b28c commit 7540d26
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 0 deletions.
1 change: 1 addition & 0 deletions host_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ package host
import (
// Make sure required drivers are registered.
_ "periph.io/x/host/v3/gpioioctl"
_ "periph.io/x/host/v3/netlink"
_ "periph.io/x/host/v3/sysfs"
)
15 changes: 15 additions & 0 deletions netlink/netlink_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package netlink

import (
"fmt"
"path/filepath"
"syscall"
)

Expand Down Expand Up @@ -52,3 +53,17 @@ func (s *connSocket) close() error {
s.fd = 0
return syscall.Close(fd)
}

// isOneWireAvailable checks to see if the Linux onewire bus drivers are loaded.
// It does this by looking for entries in the /sys/bus pseudo-filesystem.
//
// On a Raspberry Pi SBC, the onewire bus is enabled by creating entries in the
// kernel config.txt file which is located in /boot, or /boot/firmware depending
// on OS/kernel levels.
func isOneWireAvailable() bool {
items, err := filepath.Glob("/sys/bus/w1/devices/*")
if err != nil || len(items) == 0 {
return false
}
return true
}
4 changes: 4 additions & 0 deletions netlink/netlink_other.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,7 @@ func (*connSocket) recv(_ []byte) (int, error) {
func (*connSocket) close() error {
return errors.New("not implemented")
}

func isOneWireAvailable() bool {
return false
}
3 changes: 3 additions & 0 deletions netlink/onewire.go
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,9 @@ func (d *driver1W) After() []string {
}

func (d *driver1W) Init() (bool, error) {
if !isOneWireAvailable() {
return false, errors.New("no onewire buses found")
}
s, err := newW1Socket()
if err != nil {
return false, fmt.Errorf("netlink-onewire: failed to open socket: %v", err)
Expand Down

0 comments on commit 7540d26

Please sign in to comment.