Skip to content

Commit

Permalink
Account for already exported pins and unexportable pins
Browse files Browse the repository at this point in the history
  • Loading branch information
zankich committed Dec 23, 2014
1 parent cde1919 commit 2591924
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 6 deletions.
23 changes: 17 additions & 6 deletions sysfs/digital_pin.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os"
"strconv"
"syscall"
)

const (
Expand Down Expand Up @@ -64,17 +65,27 @@ func (d *digitalPin) Read() (n int, err error) {

// Export exports the pin for use by the operating system
func (d *digitalPin) Export() error {
_, err := writeFile(GPIOPATH+"/export", []byte(d.pin))
return err
if _, err := writeFile(GPIOPATH+"/export", []byte(d.pin)); err != nil {
// If EBUSY then the pin has already been exported
if err.(*os.PathError).Err != syscall.EBUSY {
return err
}
}
return nil
}

// Unexport unexports the pin and releases the pin from the operating system
func (d *digitalPin) Unexport() error {
_, err := writeFile(GPIOPATH+"/unexport", []byte(d.pin))
return err
if _, err := writeFile(GPIOPATH+"/unexport", []byte(d.pin)); err != nil {
// If EINVAL then the pin is reserved in the system and can't be unexported
if err.(*os.PathError).Err != syscall.EINVAL {
return err
}
}
return nil
}

func writeFile(path string, data []byte) (i int, err error) {
var writeFile = func(path string, data []byte) (i int, err error) {
file, err := OpenFile(path, os.O_WRONLY, 0644)
defer file.Close()
if err != nil {
Expand All @@ -84,7 +95,7 @@ func writeFile(path string, data []byte) (i int, err error) {
return file.Write(data)
}

func readFile(path string) ([]byte, error) {
var readFile = func(path string) ([]byte, error) {
file, err := OpenFile(path, os.O_RDONLY, 0644)
defer file.Close()
if err != nil {
Expand Down
31 changes: 31 additions & 0 deletions sysfs/digital_pin_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package sysfs

import (
"errors"
"os"
"syscall"
"testing"

"github.com/hybridgroup/gobot"
Expand Down Expand Up @@ -45,4 +48,32 @@ func TestDigitalPin(t *testing.T) {
data, err = pin2.Read()
gobot.Refute(t, err, nil)
gobot.Assert(t, data, 0)

writeFile = func(string, []byte) (int, error) {
return 0, &os.PathError{Err: syscall.EINVAL}
}

err = pin.Unexport()
gobot.Assert(t, err, nil)

writeFile = func(string, []byte) (int, error) {
return 0, &os.PathError{Err: errors.New("write error")}
}

err = pin.Unexport()
gobot.Assert(t, err.(*os.PathError).Err, errors.New("write error"))

writeFile = func(string, []byte) (int, error) {
return 0, &os.PathError{Err: syscall.EBUSY}
}

err = pin.Export()
gobot.Assert(t, err, nil)

writeFile = func(string, []byte) (int, error) {
return 0, &os.PathError{Err: errors.New("write error")}
}

err = pin.Export()
gobot.Assert(t, err.(*os.PathError).Err, errors.New("write error"))
}

0 comments on commit 2591924

Please sign in to comment.