-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathcard.go
65 lines (50 loc) · 1.15 KB
/
card.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package acr122u
import "bytes"
// Card represents a ACR122U card
type Card interface {
// Reader returns the name of the reader used
Reader() string
// Status returns the card status
Status() (Status, error)
// UID returns the UID for the card
UID() []byte
}
type card struct {
uid []byte
reader string
scard scardCard
}
func newCard(reader string, sc scardCard) *card {
return &card{reader: reader, scard: sc}
}
func (c *card) Reader() string {
return c.reader
}
func (c *card) Status() (Status, error) {
scs, err := c.scard.Status()
if err != nil {
return Status{}, err
}
return newStatus(scs)
}
func (c *card) UID() []byte {
return c.uid
}
// transmit raw command to underlying scardCard
func (c *card) transmit(cmd []byte) ([]byte, error) {
resp, err := c.scard.Transmit(cmd)
if err != nil {
return nil, err
}
if bytes.Equal(resp, rcOperationFailed) {
return nil, ErrOperationFailed
}
if bytes.HasSuffix(resp, rcOperationSuccess) {
return bytes.TrimSuffix(resp, rcOperationSuccess), nil
}
return resp, nil
}
// getUID returns the UID for the card
func (c *card) getUID() ([]byte, error) {
return c.transmit(cmdGetUID)
}