|
|
|
|
@ -26,6 +26,7 @@ type Dev struct {
|
|
|
|
|
operationTimeout time.Duration
|
|
|
|
|
beforeCall func()
|
|
|
|
|
afterCall func()
|
|
|
|
|
bogusUID bool
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Key is the access key that consists of 6 bytes. There could be two types of keys - keyA and keyB.
|
|
|
|
|
@ -40,10 +41,22 @@ type config struct {
|
|
|
|
|
defaultTimeout time.Duration
|
|
|
|
|
beforeCall func()
|
|
|
|
|
afterCall func()
|
|
|
|
|
bogusUID bool
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type configF func(*config) *config
|
|
|
|
|
|
|
|
|
|
// WithBogusUID sets the card reader to return incorrect 4-byte UIDs. In
|
|
|
|
|
// version 3.6.12 and earlier this package ruturned 5-bytes for tags with a
|
|
|
|
|
// 4-byte UID with bytes 0 to 3 being the correct UID and byte 4 being an XOR
|
|
|
|
|
// of bytes 0 to 3. 7-byte UIDs are correct regardless of this configuration.
|
|
|
|
|
func WithBogusUID() configF {
|
|
|
|
|
return func(c *config) *config {
|
|
|
|
|
c.bogusUID = false
|
|
|
|
|
return c
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// WithTimeout updates the default device-wide configuration timeout.
|
|
|
|
|
func WithTimeout(timeout time.Duration) configF {
|
|
|
|
|
return func(c *config) *config {
|
|
|
|
|
@ -70,11 +83,13 @@ func noop() {}
|
|
|
|
|
// spiPort the SPI device to use.
|
|
|
|
|
// resetPin reset GPIO pin.
|
|
|
|
|
// irqPin irq GPIO pin.
|
|
|
|
|
// configs configuration options
|
|
|
|
|
func NewSPI(spiPort spi.Port, resetPin gpio.PinOut, irqPin gpio.PinIn, configs ...configF) (*Dev, error) {
|
|
|
|
|
cfg := &config{
|
|
|
|
|
defaultTimeout: 30 * time.Second,
|
|
|
|
|
beforeCall: noop,
|
|
|
|
|
afterCall: noop,
|
|
|
|
|
bogusUID: true,
|
|
|
|
|
}
|
|
|
|
|
for _, cf := range configs {
|
|
|
|
|
cfg = cf(cfg)
|
|
|
|
|
@ -92,6 +107,7 @@ func NewSPI(spiPort spi.Port, resetPin gpio.PinOut, irqPin gpio.PinIn, configs .
|
|
|
|
|
operationTimeout: cfg.defaultTimeout,
|
|
|
|
|
beforeCall: cfg.beforeCall,
|
|
|
|
|
afterCall: cfg.afterCall,
|
|
|
|
|
bogusUID: cfg.bogusUID,
|
|
|
|
|
}
|
|
|
|
|
return dev, nil
|
|
|
|
|
}
|
|
|
|
|
@ -123,7 +139,7 @@ func (r *Dev) SetAntennaGain(gain int) error {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ReadUID reads the card UID with IRQ event timeout.
|
|
|
|
|
// ReadUID reads the 4-byte or 7-byte card UID with IRQ event timeout.
|
|
|
|
|
//
|
|
|
|
|
// timeout the operation timeout
|
|
|
|
|
func (r *Dev) ReadUID(timeout time.Duration) (uid []byte, err error) {
|
|
|
|
|
@ -410,10 +426,17 @@ func (r *Dev) selectCard(timeout time.Duration) ([]byte, error) {
|
|
|
|
|
|
|
|
|
|
uuid = uuid[1 : len(uuid)-1]
|
|
|
|
|
uuid = append(uuid, uuidEnd[:len(uuidEnd)-1]...)
|
|
|
|
|
|
|
|
|
|
return uuid, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if r.bogusUID {
|
|
|
|
|
return uuid, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return uuid[:len(uuid)-1], nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// write writes the data block into the card at given block address.
|
|
|
|
|
//
|
|
|
|
|
// blockAddr - the calculated block address
|
|
|
|
|
|