tinkerboard: protect against pin map races

Signed-off-by: deadprogram <ron@hybridgroup.com>
This commit is contained in:
deadprogram 2017-05-06 15:12:10 +02:00
parent ca7ffed510
commit e9c6cdc39b
1 changed files with 16 additions and 1 deletions

View File

@ -3,6 +3,7 @@ package tinkerboard
import ( import (
"errors" "errors"
"fmt" "fmt"
"sync"
multierror "github.com/hashicorp/go-multierror" multierror "github.com/hashicorp/go-multierror"
"gobot.io/x/gobot" "gobot.io/x/gobot"
@ -22,12 +23,14 @@ type Adaptor struct {
digitalPins map[int]*sysfs.DigitalPin digitalPins map[int]*sysfs.DigitalPin
pwmPins map[int]*sysfs.PWMPin pwmPins map[int]*sysfs.PWMPin
i2cBuses [2]sysfs.I2cDevice i2cBuses [2]sysfs.I2cDevice
mutex *sync.Mutex
} }
// NewAdaptor creates a Tinkerboard Adaptor // NewAdaptor creates a Tinkerboard Adaptor
func NewAdaptor() *Adaptor { func NewAdaptor() *Adaptor {
c := &Adaptor{ c := &Adaptor{
name: gobot.DefaultName("Tinker Board"), name: gobot.DefaultName("Tinker Board"),
mutex: &sync.Mutex{},
} }
c.setPins() c.setPins()
@ -47,6 +50,9 @@ func (c *Adaptor) Connect() (err error) {
// Finalize closes connection to board and pins // Finalize closes connection to board and pins
func (c *Adaptor) Finalize() (err error) { func (c *Adaptor) Finalize() (err error) {
c.mutex.Lock()
defer c.mutex.Unlock()
for _, pin := range c.digitalPins { for _, pin := range c.digitalPins {
if pin != nil { if pin != nil {
if e := pin.Unexport(); e != nil { if e := pin.Unexport(); e != nil {
@ -127,6 +133,9 @@ func (c *Adaptor) ServoWrite(pin string, angle byte) (err error) {
// DigitalPin returns matched digitalPin for specified values // DigitalPin returns matched digitalPin for specified values
func (c *Adaptor) DigitalPin(pin string, dir string) (sysfsPin sysfs.DigitalPinner, err error) { func (c *Adaptor) DigitalPin(pin string, dir string) (sysfsPin sysfs.DigitalPinner, err error) {
c.mutex.Lock()
defer c.mutex.Unlock()
i, err := c.translatePin(pin) i, err := c.translatePin(pin)
if err != nil { if err != nil {
@ -149,6 +158,9 @@ func (c *Adaptor) DigitalPin(pin string, dir string) (sysfsPin sysfs.DigitalPinn
// PWMPin returns matched pwmPin for specified pin number // PWMPin returns matched pwmPin for specified pin number
func (c *Adaptor) PWMPin(pin string) (sysfsPin sysfs.PWMPinner, err error) { func (c *Adaptor) PWMPin(pin string) (sysfsPin sysfs.PWMPinner, err error) {
c.mutex.Lock()
defer c.mutex.Unlock()
i, err := c.translatePwmPin(pin) i, err := c.translatePwmPin(pin)
if err != nil { if err != nil {
return nil, err return nil, err
@ -181,6 +193,9 @@ func (c *Adaptor) PWMPin(pin string) (sysfsPin sysfs.PWMPinner, err error) {
// GetConnection returns a connection to a device on a specified bus. // GetConnection returns a connection to a device on a specified bus.
// Valid bus number is [0..1] which corresponds to /dev/i2c-0 through /dev/i2c-1. // Valid bus number is [0..1] which corresponds to /dev/i2c-0 through /dev/i2c-1.
func (c *Adaptor) GetConnection(address int, bus int) (connection i2c.Connection, err error) { func (c *Adaptor) GetConnection(address int, bus int) (connection i2c.Connection, err error) {
c.mutex.Lock()
defer c.mutex.Unlock()
if (bus < 0) || (bus > 1) { if (bus < 0) || (bus > 1) {
return nil, fmt.Errorf("Bus number %d out of range", bus) return nil, fmt.Errorf("Bus number %d out of range", bus)
} }