From 0ce2eb60384fd60864731711009a37f92170caba Mon Sep 17 00:00:00 2001 From: Damien Date: Mon, 4 Sep 2023 07:35:36 +1100 Subject: [PATCH] feat(usb): prefer external usb context --- acr122usb/deviceid.go | 4 +--- acr122usb/driver.go | 23 +++++++++++------------ cmd/common/drivers.go | 5 +++-- cmd/nfc-list/main.go | 7 ++++++- cmd/nfc-poll/main.go | 7 ++++++- cmd/nfc-scan-device/main.go | 7 ++++++- 6 files changed, 33 insertions(+), 20 deletions(-) diff --git a/acr122usb/deviceid.go b/acr122usb/deviceid.go index 1fff294..4871240 100644 --- a/acr122usb/deviceid.go +++ b/acr122usb/deviceid.go @@ -34,9 +34,7 @@ func (d *acr122DeviceID) String() string { // Open opens an acr122 usb device (libnfc: acr122_usb_open) func (d *acr122DeviceID) Open(logger *zap.SugaredLogger) (gonfc.Device, error) { - c := usbctx() - - devs, err := c.OpenDevices(func(desc *gousb.DeviceDesc) bool { + devs, err := d.driver.usb.OpenDevices(func(desc *gousb.DeviceDesc) bool { return slices.Equal(desc.Path, d.desc.Path) }) if len(devs) == 0 { diff --git a/acr122usb/driver.go b/acr122usb/driver.go index 9ea20f2..3e5d8f0 100644 --- a/acr122usb/driver.go +++ b/acr122usb/driver.go @@ -1,22 +1,22 @@ package acr122usb import ( - "sync" - "github.com/google/gousb" "github.com/maitredede/gonfc" "go.uber.org/zap" ) -var ( - usbctx = sync.OnceValue(initCtx) -) - -func initCtx() *gousb.Context { - return gousb.NewContext() +type Acr122USBDriver struct { + usb *gousb.Context } -type Acr122USBDriver struct { +func NewDriver(usb *gousb.Context) *Acr122USBDriver { + if usb == nil { + panic("gousb context required") + } + return &Acr122USBDriver{ + usb: usb, + } } var _ gonfc.Driver = (*Acr122USBDriver)(nil) @@ -24,6 +24,7 @@ var _ gonfc.Driver = (*Acr122USBDriver)(nil) func (Acr122USBDriver) Manufacturer() string { return "libusb" } + func (Acr122USBDriver) Product() string { return "acr122" } @@ -37,11 +38,9 @@ func (Acr122USBDriver) Conflicts(otherDriver gonfc.Driver) bool { func (d *Acr122USBDriver) LookupDevices(logger *zap.SugaredLogger) ([]gonfc.DeviceID, error) { - c := usbctx() - result := make([]gonfc.DeviceID, 0) - devs, err := c.OpenDevices(func(desc *gousb.DeviceDesc) bool { + devs, err := d.usb.OpenDevices(func(desc *gousb.DeviceDesc) bool { ok := false var deviceInfo Acr122UsbSupportedDevice for _, d := range UsbSupportedDevices { diff --git a/cmd/common/drivers.go b/cmd/common/drivers.go index 72dd563..52df4e0 100644 --- a/cmd/common/drivers.go +++ b/cmd/common/drivers.go @@ -3,6 +3,7 @@ package common import ( "flag" + "github.com/google/gousb" "github.com/maitredede/gonfc" "github.com/maitredede/gonfc/acr122usb" "github.com/maitredede/gonfc/periphio" @@ -20,9 +21,9 @@ func init() { flag.BoolVar(&disablePeriphioI2C, "disable-periphio-i2c", false, "Disable periph.io I2C") } -func RegisterAllDrivers(logger *zap.SugaredLogger) []gonfc.Driver { +func RegisterAllDrivers(logger *zap.SugaredLogger, usb *gousb.Context) []gonfc.Driver { drvs := []gonfc.Driver{ - &acr122usb.Acr122USBDriver{}, + acr122usb.NewDriver(usb), } if !disablePigpioI2C { // Remote raspberry pi with pigpiod installed diff --git a/cmd/nfc-list/main.go b/cmd/nfc-list/main.go index d7982dd..24e482c 100644 --- a/cmd/nfc-list/main.go +++ b/cmd/nfc-list/main.go @@ -3,6 +3,7 @@ package main import ( goflag "flag" + "github.com/google/gousb" "github.com/maitredede/gonfc" "github.com/maitredede/gonfc/cmd/common" flag "github.com/spf13/pflag" @@ -24,13 +25,17 @@ func main() { logger = log.Sugar() logger.Infof("gonfc version of nfc-list") + //gousb + usb := gousb.NewContext() + defer usb.Close() + //periphio _, err := host.Init() if err != nil { logger.Fatal(err) } - drvs := common.RegisterAllDrivers(logger) + drvs := common.RegisterAllDrivers(logger, usb) devices := make([]gonfc.DeviceID, 0) for _, d := range drvs { diff --git a/cmd/nfc-poll/main.go b/cmd/nfc-poll/main.go index 30319d5..64206b7 100644 --- a/cmd/nfc-poll/main.go +++ b/cmd/nfc-poll/main.go @@ -4,6 +4,7 @@ import ( goflag "flag" "time" + "github.com/google/gousb" "github.com/maitredede/gonfc" "github.com/maitredede/gonfc/cmd/common" flag "github.com/spf13/pflag" @@ -25,13 +26,17 @@ func main() { logger = log.Sugar() logger.Infof("gonfc version of nfc-poll") + //gousb + usb := gousb.NewContext() + defer usb.Close() + //periphio _, err := host.Init() if err != nil { logger.Fatal(err) } - drvs := common.RegisterAllDrivers(logger) + drvs := common.RegisterAllDrivers(logger, usb) devices := make([]gonfc.DeviceID, 0) for _, d := range drvs { diff --git a/cmd/nfc-scan-device/main.go b/cmd/nfc-scan-device/main.go index 5cae0bb..a1a6b4c 100644 --- a/cmd/nfc-scan-device/main.go +++ b/cmd/nfc-scan-device/main.go @@ -3,6 +3,7 @@ package main import ( goflag "flag" + "github.com/google/gousb" "github.com/maitredede/gonfc" "github.com/maitredede/gonfc/cmd/common" flag "github.com/spf13/pflag" @@ -24,13 +25,17 @@ func main() { logger := log.Sugar() logger.Infof("gonfc version of nfc-scan-device") + //gousb + usb := gousb.NewContext() + defer usb.Close() + //periphio _, err := host.Init() if err != nil { logger.Fatal(err) } - drvs := common.RegisterAllDrivers(logger) + drvs := common.RegisterAllDrivers(logger, usb) devices := make([]gonfc.DeviceID, 0) for _, d := range drvs {