Skip to content
This repository has been archived by the owner on Sep 20, 2023. It is now read-only.

Commit

Permalink
sysfs: Improve coverage for thermal
Browse files Browse the repository at this point in the history
- Correct minor typos.
- Improve sysfs-thermal error wrapping.
- No significant functional change.
  • Loading branch information
maruel committed Aug 25, 2017
1 parent 252733a commit 687bb43
Show file tree
Hide file tree
Showing 11 changed files with 314 additions and 33 deletions.
6 changes: 3 additions & 3 deletions conn/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@
//
// Most connection-type specific subpackages include subpackages:
//
// - XXXreg: registry as that is populated by the host drivers and that can be
// XXXreg: registry as that is populated by the host drivers and that can be
// leveraged by applications.
//
// - XXXtest: fake implementation that can be leveraged when writting device
// XXXtest: fake implementation that can be leveraged when writing device
// driver unit test.
//
// - XXXsmoketest: smoke test that tests against real hardware to ensure the
// XXXsmoketest: smoke test that tests against real hardware to ensure the
// whole stack work correctly, including the OS supplied drivers.
package conn
2 changes: 1 addition & 1 deletion devices/bmxx80/bmp180.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
//
// It must be called with d.mu lock held.
func (d *Dev) sense180(env *devices.Environment) error {
// Request temperature convertion and read measurement.
// Request temperature conversion and read measurement.
if err := d.writeCommands([]byte{0xF4, 0x20 | 0x0E}); err != nil {
return d.wrap(err)
}
Expand Down
2 changes: 1 addition & 1 deletion host/rpi/rpi.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func Present() bool {
//
// P1 is also known as J8 on A+, B+, 2 and later.
var (
// Rapsberry Pi A and B, 26 pin header:
// Raspberry Pi A and B, 26 pin header:
P1_1 pin.Pin = pin.V3_3 // max 30mA
P1_2 pin.Pin = pin.V5 // (filtered)
P1_3 gpio.PinIO = bcm283x.GPIO2 // High, I2C1_SDA
Expand Down
6 changes: 6 additions & 0 deletions host/sysfs/gpio_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,12 @@ func TestPin_readInt(t *testing.T) {
}
}

func TestGPIODriver(t *testing.T) {
if len((&driverGPIO{}).Prerequisites()) != 0 {
t.Fatal("unexpected GPIO prerequisites")
}
}

//

type fakeGPIOFile struct {
Expand Down
2 changes: 1 addition & 1 deletion host/sysfs/i2c_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func TestNewI2C(t *testing.T) {

func TestI2C_faked(t *testing.T) {
// Create a fake I2C to test methods.
bus := I2C{f: ioctlClose(0), busNumber: 24}
bus := I2C{f: &ioctlClose{}, busNumber: 24}
if s := bus.String(); s != "I2C24" {
t.Fatal(s)
}
Expand Down
6 changes: 6 additions & 0 deletions host/sysfs/led_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,9 @@ func TestLED_not_supported(t *testing.T) {
t.Fatal(pull)
}
}

func TestLEDDriver(t *testing.T) {
if len((&driverLED{}).Prerequisites()) != 0 {
t.Fatal("unexpected LED prerequisites")
}
}
16 changes: 11 additions & 5 deletions host/sysfs/spi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func TestNewSPI(t *testing.T) {
}

func TestSPI_IO(t *testing.T) {
port := SPI{f: ioctlClose(0), busNumber: 24}
port := SPI{f: &ioctlClose{}, busNumber: 24}
c, err := port.Connect(1, spi.Mode3, 8)
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -108,7 +108,7 @@ func TestSPI_IO(t *testing.T) {
}

func TestSPI_IO_not_initialized(t *testing.T) {
port := SPI{f: ioctlClose(0), busNumber: 24}
port := SPI{f: &ioctlClose{}, busNumber: 24}
if _, err := port.txInternal([]byte{0}, []byte{0}); err == nil {
t.Fatal("not initialized")
}
Expand All @@ -118,7 +118,7 @@ func TestSPI_IO_not_initialized(t *testing.T) {
}

func TestSPI_pins(t *testing.T) {
port := SPI{f: ioctlClose(0), busNumber: 24}
port := SPI{f: &ioctlClose{}, busNumber: 24}
if p := port.CLK(); p != gpio.INVALID {
t.Fatal(p)
}
Expand All @@ -134,7 +134,7 @@ func TestSPI_pins(t *testing.T) {
}

func TestSPI_other(t *testing.T) {
port := SPI{f: ioctlClose(0), busNumber: 24}
port := SPI{f: &ioctlClose{}, busNumber: 24}
if s := port.String(); s != "SPI24.0" {
t.Fatal(s)
}
Expand All @@ -151,7 +151,7 @@ func TestSPI_other(t *testing.T) {

func TestSPI_Connect(t *testing.T) {
// Create a fake SPI to test methods.
port := SPI{f: ioctlClose(0), busNumber: 24}
port := SPI{f: &ioctlClose{}, busNumber: 24}
if _, err := port.Connect(-1, spi.Mode0, 8); err == nil {
t.Fatal("invalid speed")
}
Expand Down Expand Up @@ -191,6 +191,12 @@ func TestSPIIOCTX(t *testing.T) {
}
}

func TestSPIDriver(t *testing.T) {
if len((&driverSPI{}).Prerequisites()) != 0 {
t.Fatal("unexpected SPI prerequisites")
}
}

//

func init() {
Expand Down
8 changes: 6 additions & 2 deletions host/sysfs/sysfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,19 @@ import (
"periph.io/x/periph/host/fs"
)

var ioctlOpen = func(path string, flag int) (ioctlCloser, error) {
var ioctlOpen = ioctlOpenDefault

func ioctlOpenDefault(path string, flag int) (ioctlCloser, error) {
f, err := fs.Open(path, flag)
if err != nil {
return nil, err
}
return f, nil
}

var fileIOOpen = func(path string, flag int) (fileIO, error) {
var fileIOOpen = fileIOOpenDefault

func fileIOOpenDefault(path string, flag int) (fileIO, error) {
f, err := fs.Open(path, flag)
if err != nil {
return nil, err
Expand Down
53 changes: 49 additions & 4 deletions host/sysfs/sysfs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,63 @@

package sysfs

import "periph.io/x/periph/host/fs"
import (
"errors"
"io"

"periph.io/x/periph/host/fs"
)

func init() {
fs.Inhibit()
reset()
}

type ioctlClose int
func reset() {
fileIOOpen = fileIOOpenDefault
ioctlOpen = ioctlOpenDefault
// Soon.
//fileIOOpen = fileIOOpenPanic
//ioctlOpen = ioctlOpenPanic
}

func (i ioctlClose) Ioctl(op uint, data uintptr) error {
func ioctlOpenPanic(path string, flag int) (ioctlCloser, error) {
panic("don't forget to override fileIOOpen")
}

func fileIOOpenPanic(path string, flag int) (fileIO, error) {
panic("don't forget to override fileIOOpen")
}

type ioctlClose struct{}

func (i *ioctlClose) Ioctl(op uint, data uintptr) error {
return nil
}

func (i ioctlClose) Close() error {
func (i *ioctlClose) Close() error {
return nil
}

type file struct {
ioctlClose
}

func (f *file) Fd() uintptr {
return 0xFFFFFFFF
}

func (f *file) Read(p []byte) (int, error) {
return 0, errors.New("not implemented")
}

func (f *file) Seek(offset int64, whence int) (int64, error) {
if offset == 0 && whence == io.SeekStart {
return 0, nil
}
return 0, errors.New("not implemented")
}

func (f *file) Write(p []byte) (int, error) {
return 0, errors.New("not implemented")
}
20 changes: 10 additions & 10 deletions host/sysfs/thermal_sensor.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (

"periph.io/x/periph"
"periph.io/x/periph/devices"
"periph.io/x/periph/host/fs"
)

// ThermalSensors is all the sensors discovered on this host via sysfs.
Expand Down Expand Up @@ -63,13 +62,13 @@ func (t *ThermalSensor) Type() string {
if t.nameType == "" {
f, err := fileIOOpen(t.root+"type", os.O_RDONLY)
if err != nil {
return err.Error()
return fmt.Sprintf("sysfs-thermal: %v", err)
}
defer f.Close()
var buf [256]byte
n, err := f.Read(buf[:])
if err != nil {
return err.Error()
return fmt.Sprintf("sysfs-thermal: %v", err)
}
if n < 2 {
t.nameType = "<unknown>"
Expand All @@ -90,14 +89,14 @@ func (t *ThermalSensor) Sense(env *devices.Environment) error {
var buf [24]byte
n, err := seekRead(t.f, buf[:])
if err != nil {
return err
return fmt.Errorf("sysfs-thermal: %v", err)
}
if n < 2 {
return errors.New("sysfs-thermal: failed to read temperature")
}
i, err := strconv.Atoi(string(buf[:n-1]))
if err != nil {
return err
return fmt.Errorf("sysfs-thermal: %v", err)
}
if i < 100 {
i *= 1000
Expand All @@ -109,7 +108,7 @@ func (t *ThermalSensor) Sense(env *devices.Environment) error {
// SenseContinuous implements devices.Environmental.
func (t *ThermalSensor) SenseContinuous(interval time.Duration) (<-chan devices.Environment, error) {
// TODO(maruel): Manually poll in a loop via time.NewTicker.
return nil, errors.New("not implemented")
return nil, errors.New("sysfs-thermal: not implemented")
}

//
Expand All @@ -120,11 +119,12 @@ func (t *ThermalSensor) open() error {
if t.f != nil {
return nil
}
f, err := fs.Open(t.root+"temp", os.O_RDONLY)
if err == nil {
t.f = f
f, err := fileIOOpen(t.root+"temp", os.O_RDONLY)
if err != nil {
return fmt.Errorf("sysfs-thermal: %v", err)
}
return err
t.f = f
return nil
}

// driverThermalSensor implements periph.Driver.
Expand Down
Loading

0 comments on commit 687bb43

Please sign in to comment.