Fix 4-byte tag UIDs

4-byte UIDs incorrectly included a 5th csum byte. 7-byte UIDs were unaffected.
pull/22/head
Bracken Dawson 5 years ago
parent 47f3fec45d
commit df6f1e5307
No known key found for this signature in database
GPG Key ID: 8CB332D5A897919D

@ -26,6 +26,7 @@ type Dev struct {
operationTimeout time.Duration operationTimeout time.Duration
beforeCall func() beforeCall func()
afterCall 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. // 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 defaultTimeout time.Duration
beforeCall func() beforeCall func()
afterCall func() afterCall func()
bogusUID bool
} }
type configF func(*config) *config type configF func(*config) *config
// WithFixedUID sets the card reader to return correct 4-byte UIDs. Without
// this setting 4-byte UIDs will be 5-bytes long 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 WithFixedUID() configF {
return func(c *config) *config {
c.bogusUID = false
return c
}
}
// WithTimeout updates the default device-wide configuration timeout. // WithTimeout updates the default device-wide configuration timeout.
func WithTimeout(timeout time.Duration) configF { func WithTimeout(timeout time.Duration) configF {
return func(c *config) *config { return func(c *config) *config {
@ -66,15 +79,20 @@ func WithSync() configF {
func noop() {} func noop() {}
// NewSPI creates and initializes the RFID card reader attached to SPI. // NewSPI creates and initializes the RFID card reader attached to SPI.
// It is recommended to use the WithFixedUID configuration to return correct
// card UIDs. Incorrect UIDs are returned for compatibility but will be removed
// from a future version of this package.
// //
// spiPort the SPI device to use. // spiPort the SPI device to use.
// resetPin reset GPIO pin. // resetPin reset GPIO pin.
// irqPin irq GPIO pin. // irqPin irq GPIO pin.
// configs configuration options
func NewSPI(spiPort spi.Port, resetPin gpio.PinOut, irqPin gpio.PinIn, configs ...configF) (*Dev, error) { func NewSPI(spiPort spi.Port, resetPin gpio.PinOut, irqPin gpio.PinIn, configs ...configF) (*Dev, error) {
cfg := &config{ cfg := &config{
defaultTimeout: 30 * time.Second, defaultTimeout: 30 * time.Second,
beforeCall: noop, beforeCall: noop,
afterCall: noop, afterCall: noop,
bogusUID: true,
} }
for _, cf := range configs { for _, cf := range configs {
cfg = cf(cfg) cfg = cf(cfg)
@ -92,6 +110,7 @@ func NewSPI(spiPort spi.Port, resetPin gpio.PinOut, irqPin gpio.PinIn, configs .
operationTimeout: cfg.defaultTimeout, operationTimeout: cfg.defaultTimeout,
beforeCall: cfg.beforeCall, beforeCall: cfg.beforeCall,
afterCall: cfg.afterCall, afterCall: cfg.afterCall,
bogusUID: cfg.bogusUID,
} }
return dev, nil return dev, nil
} }
@ -123,7 +142,9 @@ func (r *Dev) SetAntennaGain(gain int) error {
return nil 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.
// 4-byte UIDs will include their checksum byte unless Dev was created using
// WithFixedUID.
// //
// timeout the operation timeout // timeout the operation timeout
func (r *Dev) ReadUID(timeout time.Duration) (uid []byte, err error) { func (r *Dev) ReadUID(timeout time.Duration) (uid []byte, err error) {
@ -410,10 +431,17 @@ func (r *Dev) selectCard(timeout time.Duration) ([]byte, error) {
uuid = uuid[1 : len(uuid)-1] uuid = uuid[1 : len(uuid)-1]
uuid = append(uuid, uuidEnd[:len(uuidEnd)-1]...) uuid = append(uuid, uuidEnd[:len(uuidEnd)-1]...)
return uuid, nil
} }
if r.bogusUID {
return uuid, nil return uuid, nil
} }
return uuid[:len(uuid)-1], nil
}
// write writes the data block into the card at given block address. // write writes the data block into the card at given block address.
// //
// blockAddr - the calculated block address // blockAddr - the calculated block address

Loading…
Cancel
Save