Skip to content

Commit

Permalink
feat(usb): prefer external usb context
Browse files Browse the repository at this point in the history
  • Loading branch information
maitredede committed Sep 3, 2023
1 parent 21dd135 commit 0ce2eb6
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 20 deletions.
4 changes: 1 addition & 3 deletions acr122usb/deviceid.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
23 changes: 11 additions & 12 deletions acr122usb/driver.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
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)

func (Acr122USBDriver) Manufacturer() string {
return "libusb"
}

func (Acr122USBDriver) Product() string {
return "acr122"
}
Expand All @@ -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 {
Expand Down
5 changes: 3 additions & 2 deletions cmd/common/drivers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down
7 changes: 6 additions & 1 deletion cmd/nfc-list/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 {
Expand Down
7 changes: 6 additions & 1 deletion cmd/nfc-poll/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 {
Expand Down
7 changes: 6 additions & 1 deletion cmd/nfc-scan-device/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 {
Expand Down

0 comments on commit 0ce2eb6

Please sign in to comment.