-
Notifications
You must be signed in to change notification settings - Fork 0
/
reader.go
183 lines (159 loc) · 4.36 KB
/
reader.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package main
import (
"context"
"encoding/hex"
"log"
"time"
"github.com/clausecker/nfc/v2"
"github.com/pkg/errors"
"github.com/warthog618/gpiod"
)
var modulations = []nfc.Modulation{
{Type: nfc.ISO14443a, BaudRate: nfc.Nbr106},
{Type: nfc.ISO14443b, BaudRate: nfc.Nbr106},
{Type: nfc.Felica, BaudRate: nfc.Nbr212},
{Type: nfc.Felica, BaudRate: nfc.Nbr424},
{Type: nfc.Jewel, BaudRate: nfc.Nbr106},
{Type: nfc.ISO14443biClass, BaudRate: nfc.Nbr106},
}
type Reader interface {
ListenForTags(ctx context.Context) error
Cleanup() error
Reset()
GetTagChannel() <-chan string
}
type TagReader struct {
TagChannel chan string
reader *nfc.Device
ResetPin int
DeviceConnection string
}
func (reader *TagReader) init() error {
dev, err := nfc.Open(reader.DeviceConnection)
if err != nil {
// Reset the reader if there is an error
reader.Reset()
return errors.Wrap(err, "Cannot communicate with the device")
}
reader.reader = &dev
err = reader.reader.InitiatorInit()
if err != nil {
return errors.Wrap(err, "Cannot initialize the reader")
}
return nil
}
func NewTagReader(deviceConnection string, resetPin int) *TagReader {
return &TagReader{DeviceConnection: deviceConnection, TagChannel: make(chan string, 10), ResetPin: resetPin}
}
// Reset performs a hardware reset by pulling the ResetPin low and then releasing.
func (reader *TagReader) Reset() {
log.Println("Resetting the reader..")
//refer to gpiod docs
c, err := gpiod.NewChip("gpiochip0")
pin, err := c.RequestLine(reader.ResetPin, gpiod.AsOutput(0))
if err != nil {
log.Println(err)
return
}
err = pin.SetValue(1)
if err != nil {
log.Println(err)
return
}
time.Sleep(time.Millisecond * 400)
err = pin.SetValue(0)
if err != nil {
log.Println(err)
return
}
time.Sleep(time.Millisecond * 400)
err = pin.SetValue(1)
time.Sleep(time.Millisecond * 100)
if err != nil {
log.Println(err)
return
}
}
func (reader *TagReader) Cleanup() error {
return reader.reader.Close()
}
func (reader *TagReader) GetTagChannel() <-chan string {
return reader.TagChannel
}
func (reader *TagReader) getIdFromTarget(target nfc.Target) (*string, error) {
var UID string
// Transform the target to a specific tag Type and send the UID to the channel
switch target.Modulation() {
case nfc.Modulation{Type: nfc.ISO14443a, BaudRate: nfc.Nbr106}:
var card = target.(*nfc.ISO14443aTarget)
var UIDLen = card.UIDLen
var ID = card.UID
UID = hex.EncodeToString(ID[:UIDLen])
break
case nfc.Modulation{Type: nfc.ISO14443b, BaudRate: nfc.Nbr106}:
var card = target.(*nfc.ISO14443bTarget)
var UIDLen = len(card.ApplicationData)
var ID = card.ApplicationData
UID = hex.EncodeToString(ID[:UIDLen])
break
case nfc.Modulation{Type: nfc.Felica, BaudRate: nfc.Nbr212}:
var card = target.(*nfc.FelicaTarget)
var UIDLen = card.Len
var ID = card.ID
UID = hex.EncodeToString(ID[:UIDLen])
break
case nfc.Modulation{Type: nfc.Felica, BaudRate: nfc.Nbr424}:
var card = target.(*nfc.FelicaTarget)
var UIDLen = card.Len
var ID = card.ID
UID = hex.EncodeToString(ID[:UIDLen])
break
case nfc.Modulation{Type: nfc.Jewel, BaudRate: nfc.Nbr106}:
var card = target.(*nfc.JewelTarget)
var ID = card.ID
var UIDLen = len(ID)
UID = hex.EncodeToString(ID[:UIDLen])
break
case nfc.Modulation{Type: nfc.ISO14443biClass, BaudRate: nfc.Nbr106}:
var card = target.(*nfc.ISO14443biClassTarget)
var ID = card.UID
var UIDLen = len(ID)
UID = hex.EncodeToString(ID[:UIDLen])
break
default:
return nil, errors.New("Unknown modulation")
}
return &UID, nil
}
func (reader *TagReader) ListenForTags(ctx context.Context) error {
//Initialize the reader
err := reader.init()
if err != nil {
return errors.Wrap(err, "Cannot initialize the reader")
}
for {
select {
case <-ctx.Done():
return nil
default:
// Poll for 300ms
tagCount, target, err := reader.reader.InitiatorPollTarget(modulations, 1, 300*time.Millisecond)
if err != nil {
log.Println("Error polling the reader", err)
continue
}
// Check if a tag was detected
if tagCount > 0 {
// Get the UID of the tag based on the modulation type
id, err := reader.getIdFromTarget(target)
if err != nil {
log.Println("Error getting ID from target", err)
continue
}
// Send the UID of the tag to main goroutine
reader.TagChannel <- *id
}
time.Sleep(time.Second * 1)
}
}
}