forked from carlosefr/magstripelib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMagStripe_ESP32.cpp
291 lines (215 loc) · 7.41 KB
/
MagStripe_ESP32.cpp
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/*
* MagStripe - Read data from a magnetic stripe card.
*
* Copyright (c) 2010 Carlos Rodrigues <[email protected]>
* Copyright (c) 2022 Seth Teichman <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "MagStripe_ESP32.h"
#include <Arduino.h>
// Enough bits to read any of the three tracks...
#define BIT_BUFFER_LEN 800
// Variables used by the interrupt handlers...
static volatile bool next_bit = 0; // next bit to read
static volatile unsigned char bits[BIT_BUFFER_LEN / 8]; // buffer for bits being read
static volatile short num_bits = 0; // number of bits already read
// Manipulate the bit buffer...
static void bits_set(short index, bool bit);
static bool bits_get(short index);
// The interrupt handlers...
static void handle_data(void);
static void handle_clock(void);
portMUX_TYPE dmux = portMUX_INITIALIZER_UNLOCKED;
portMUX_TYPE cmux = portMUX_INITIALIZER_UNLOCKED;
MagStripe::MagStripe(unsigned char cls):
pin_cls(cls),
direction(READ_UNKNOWN)
{}
void MagStripe::begin(unsigned char track)
{
this->track = track;
pinMode(MAGSTRIPE_RDT, INPUT);
pinMode(MAGSTRIPE_RCL, INPUT);
pinMode(this->pin_cls, INPUT);
// Reading is more reliable when using interrupts...
attachInterrupt(digitalPinToInterrupt(MAGSTRIPE_RDT), handle_data, CHANGE); // data pin
attachInterrupt(digitalPinToInterrupt(MAGSTRIPE_RCL), handle_clock, FALLING); // clock pin
}
void MagStripe::stop()
{
detachInterrupt(digitalPinToInterrupt(MAGSTRIPE_RDT));
detachInterrupt(digitalPinToInterrupt(MAGSTRIPE_RCL));
}
bool MagStripe::available()
{
return digitalRead(this->pin_cls) == LOW;
}
short MagStripe::read(char *data, unsigned char size)
{
// Fail if no card present...
if (!this->available()) {
return -1;
}
// Empty the bit buffer...
num_bits = 0;
next_bit = 0;
// Wait while the data is being read by the interrupt routines...
while (this->available()) {}
// Decode the raw bits...
short chars = this->decode_bits(data, size);
this->direction = READ_FORWARD;
// If the data looks bad, reverse and try again...
if (chars < 0) {
this->reverse_bits();
chars = this->decode_bits(data, size);
this->direction = READ_BACKWARD;
}
// The card could not be read successfully...
if (chars < 0) {
this->direction = READ_UNKNOWN;
}
return chars;
}
enum ReadDirection MagStripe::read_direction()
{
return this->direction;
}
void MagStripe::reverse_bits()
{
for (short i = 0; i < num_bits / 2; i++) {
bool b = bits_get(i);
bits_set(i, bits_get(num_bits - i - 1));
bits_set(num_bits - i - 1, b);
}
}
bool MagStripe::verify_parity(unsigned char c)
{
unsigned char parity = 0;
for (unsigned char i = 0; i < 8; i++) {
parity += (c >> i) & 1;
}
// The parity must be odd...
return parity % 2 != 0;
}
bool MagStripe::verify_lrc(short start, short length)
{
unsigned char parity_bit = (this->track == 1 ? 7 : 5);
// Count the number of ones per column (ignoring parity bits)...
for (short i = 0; i < (parity_bit - 1); i++) {
short parity = 0;
for (short j = i; j < length; j += parity_bit) {
parity += bits_get(start + j);
}
// Even parity is what we want...
if (parity % 2 != 0) {
return false;
}
}
return true;
}
short MagStripe::find_sentinel(unsigned char pattern)
{
unsigned char bit_accum = 0;
unsigned char bit_length = (this->track == 1 ? 7 : 5);
for (short i = 0; i < num_bits; i++) {
bit_accum >>= 1; // rotate the bits to the right...
bit_accum |= bits_get(i) << (bit_length - 1); // ...and add the current bit
// Stop when the start sentinel pattern is found...
if (bit_accum == pattern) {
return i - (bit_length - 1);
}
}
// No start sentinel was found...
return -1;
}
short MagStripe::decode_bits(char *data, unsigned char size) {
short bit_count = 0;
unsigned char chars = 0;
unsigned char bit_accum = 0;
unsigned char bit_length = (this->track == 1 ? 7 : 5);
short bit_start = this->find_sentinel(this->track == 1 ? 0x45 : 0x0b);
if (bit_start < 0) { // error, start sentinel not found
return -1;
}
for (short i = bit_start; i < num_bits; i++) {
bit_accum >>= 1; // rotate the bits to the right...
bit_accum |= (bits_get(i) << (bit_length - 1)); // ...and add the current bit
bit_count++;
if (bit_count % bit_length == 0) {
if (chars >= size) { // error, the buffer is too small
return -1;
}
// A null means we reached the end of the data...
if (bit_accum == 0) {
break;
}
// The parity must be odd...
if (!verify_parity(bit_accum)) {
return -1;
}
// Remove the parity bit...
bit_accum &= ~(1 << (bit_length - 1));
// Convert the character to ASCII...
data[chars] = bit_accum + (this->track == 1 ? 0x20 : 0x30);
chars++;
// Reset...
bit_accum = 0;
}
}
// Turn the data into a null-terminated string...
data[chars] = '\0';
if (data[chars - 2] != '?') { // error, the end sentinel is not in the right place
return -1;
}
// Verify the LRC (even parity across columns)...
if (!verify_lrc(bit_start, chars * bit_length)) {
return -1;
}
return chars;
}
static void bits_set(short index, bool bit)
{
volatile unsigned char *b = &bits[index / 8];
unsigned char m = 1 << (index % 8);
*b = bit ? (*b | m) : (*b & ~m);
}
static bool bits_get(short index)
{
return bits[index / 8] & (1 << (index % 8));
}
static void IRAM_ATTR handle_data()
{
portENTER_CRITICAL_ISR(&dmux);
next_bit = !next_bit;
portEXIT_CRITICAL_ISR(&dmux);
}
static void IRAM_ATTR handle_clock()
{
portENTER_CRITICAL_ISR(&cmux);
// Avoid a crash in case there are too many bits (garbage)...
if (num_bits >= BIT_BUFFER_LEN) {
portEXIT_CRITICAL_ISR(&cmux);
return;
}
bits_set(num_bits, next_bit);
num_bits++;
portEXIT_CRITICAL_ISR(&cmux);
}
/* vim: set expandtab ts=4 sw=4: */