-
Notifications
You must be signed in to change notification settings - Fork 71
/
RFID.h
493 lines (427 loc) · 13.1 KB
/
RFID.h
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
//- -----------------------------------------------------------------------------------------------------------------------
// AskSin++
// 2018-05-03 papa Creative Commons - http://creativecommons.org/licenses/by-nc-sa/3.0/de/
// 2019-01-14 jp112sdl Creative Commons - http://creativecommons.org/licenses/by-nc-sa/3.0/de/
//- -----------------------------------------------------------------------------------------------------------------------
#ifndef __RFID_H__
#define __RFID_H__
#include "MultiChannelDevice.h"
#include "Register.h"
#ifdef USE_MFRC522_I2C
#include <Wire.h>
#include <MFRC522_I2C.h>
#endif
#ifdef USE_WIEGAND
#include <Wiegand.h> // https://github.com/monkeyboard/Wiegand-Protocol-Library-for-Arduino
#endif
#ifdef USE_MFRC522_SPI
#include <MFRC522.h>
#endif
#ifdef USE_RDM6300
#include <SoftwareSerial.h>
#endif
#define ID_ADDR_SIZE 8
namespace as {
DEFREGISTER(RFIDReg1,CREG_AES_ACTIVE,0xe0,0xe1,0xe2,0xe3,0xe4,0xe5,0xe6,0xe7)
class RFIDList1 : public RegList1<RFIDReg1> {
public:
RFIDList1 (uint16_t addr) : RegList1<RFIDReg1>(addr) {}
void defaults () {
clear();
}
};
class ChipIdMsg : public Message {
public:
bool free(uint8_t*addr) {
bool f = true;
for (uint8_t n = 0; n < ID_ADDR_SIZE; n++) {
if (addr[n] != 0x00) {
f = false;
break;
}
}
return f;
}
void init(uint8_t msgcnt, uint8_t ch, uint8_t*addr) {
char hexstr[ID_ADDR_SIZE * 2];
if (free(addr)) {
for (uint8_t n = 0; n < (ID_ADDR_SIZE * 2); n++)
hexstr[n] = 0x20;
} else {
//convert address to hex-string - from https://stackoverflow.com/questions/6357031/how-do-you-convert-a-byte-array-to-a-hexadecimal-string-in-c
unsigned char * pin = addr;
const char * hex = "0123456789ABCDEF";
char * pout = hexstr;
uint8_t i = 0;
for(; i < ID_ADDR_SIZE-1; ++i){
*pout++ = hex[(*pin>>4)&0xF];
*pout++ = hex[(*pin++)&0xF];
}
*pout++ = hex[(*pin>>4)&0xF];
*pout++ = hex[(*pin)&0xF];
*pout = 0;
}
//DPRINT("hexstr=");DPRINTLN(hexstr);
Message::init(0x1a, msgcnt, AS_MESSAGE_SENSOR_DATA, BIDI , ch , hexstr[0]);
for (uint8_t i = 1; i < (ID_ADDR_SIZE * 2); i++) {
pload[i-1] = hexstr[i];
}
}
};
template<class HALTYPE,int PEERCOUNT,class List0Type=List0>
class RFIDChannel : public Channel<HALTYPE,RFIDList1,EmptyList,DefList4,PEERCOUNT,List0Type>, Alarm {
// ChipIdMsg chipIdMsg;
enum { none=0, released, longpressed, longreleased };
uint8_t state, matches, repeatcnt;
public:
typedef Channel<HALTYPE,RFIDList1,EmptyList,DefList4,PEERCOUNT,List0Type> BaseChannel;
RFIDChannel () : BaseChannel(), Alarm(0), state(0), matches(0),repeatcnt(0) {}
virtual ~RFIDChannel () {}
virtual void trigger (__attribute__((unused)) AlarmClock& clock) {
state = 0;
this->changed(true);
}
uint8_t status () const {
return state;
}
uint8_t flags () const {
return 0;
}
void start () {
matches <<= 1;
}
bool check (uint8_t* addr) {
if( free() == false && isID(addr) == true ) {
matches |= 0b00000001;
return true;
}
return false;
}
void sendChipID() {
uint8_t buf[ID_ADDR_SIZE];
RFIDList1 l = this->getList1();
for( uint8_t n=0; n< ID_ADDR_SIZE; ++n ) {
buf[n] = l.readRegister(0xe0+n);
}
ChipIdMsg& chipIdMsg = (ChipIdMsg&)this->device().message();
chipIdMsg.init(this->device().nextcount(), this->number(), buf);
_delay_ms(400); //need some small delay for ccu for appropriate message processing
this->device().sendPeerEvent(chipIdMsg, *this);
}
void finish () {
uint8_t s = none;
// 3 or 6 matches are longpress and longlongpress
if( (matches & 0b00111111) == 0b00000111 || (matches & 0b00111111) == 0b00111111 ) {
s = longpressed;
DPRINTLN(F("longpressed"));
this->device().buzzer().on();
// clear longlong
matches &= 0b11000111;
}
// check for long release
else if( (matches & 0b00001111) == 0b00001110 ) {
s = longreleased;
DPRINTLN(F("longreleased"));
this->device().buzzer().off();
}
// check for release
else if( (matches & 0b00000011) == 0b00000010 ) {
s = released;
DPRINTLN(F("released"));
this->device().buzzer().on(millis2ticks(100));
}
if( s != none ) {
RemoteEventMsg& msg = (RemoteEventMsg&)this->device().message();
msg.init(this->device().nextcount(),this->number(),repeatcnt,(s==longreleased || s==longpressed),this->device().battery().low());
if( s == released || s == longreleased) {
// send the message to every peer
this->device().sendPeerEvent(msg,*this);
repeatcnt++;
}
else if (s == longpressed) {
// broadcast the message
this->device().broadcastPeerEvent(msg,*this);
}
}
}
bool match (uint8_t* addr) {
start();
bool res = check(addr);
finish();
return res;
}
bool isID (uint8_t* buf) {
RFIDList1 l = this->getList1();
for( uint8_t n=0; n< ID_ADDR_SIZE; ++n ) {
if( l.readRegister(0xe0+n) != buf[n] ) {
return false;
}
}
return true;
}
void storeID (uint8_t* buf) {
if( learn() == true ) {
for( uint8_t n=0; n < ID_ADDR_SIZE; ++n ) {
this->getList1().writeRegister(0xe0+n,buf[n]);
}
state = 0;
sendChipID();
this->changed(true);
sysclock.cancel(*this);
}
}
bool free () {
return {
this->getList1().readRegister(0xe0) == 0x00 &&
this->getList1().readRegister(0xe1) == 0x00 &&
this->getList1().readRegister(0xe2) == 0x00 &&
this->getList1().readRegister(0xe3) == 0x00 &&
this->getList1().readRegister(0xe4) == 0x00 &&
this->getList1().readRegister(0xe5) == 0x00 &&
this->getList1().readRegister(0xe6) == 0x00 &&
this->getList1().readRegister(0xe7) == 0x00
};
}
bool learn () const {
return state == 200;
}
bool process (const ActionSetMsg& msg) {
state = msg.value();
this->changed(true);
if( state != 0 ) {
sysclock.cancel(*this);
set(seconds2ticks(60));
sysclock.add(*this);
}
return true;
}
bool process (const ActionCommandMsg& msg) {
if ( (msg.len() == ID_ADDR_SIZE) || (msg.len() == 1 && msg.value(0) == 0xcc) ) {
for( uint8_t n=0; n < ID_ADDR_SIZE; ++n ) {
uint8_t val = msg.len() == 1 ? 0x00:msg.value(n);
this->getList1().writeRegister(0xe0+n,val);
}
state = 0;
this->device().getHal().buzzer.on(millis2ticks(300), millis2ticks(200),2);
sendChipID();
this->changed(true);
}
if (msg.value(0) == 0xba) {
if (msg.len() == 3)
this->device().getHal().buzzer.on(decis2ticks(msg.value(1)),decis2ticks(msg.value(2)), -1);
if (msg.len() == 4)
this->device().getHal().buzzer.on(decis2ticks(msg.value(1)),decis2ticks(msg.value(2)),msg.value(3));
}
if (msg.len() == 1 && msg.value(0) == 0xb1) {
this->device().getHal().buzzer.on();
}
if (msg.len() == 1 && msg.value(0) == 0xb0) {
this->device().getHal().buzzer.off(true);
}
if (msg.len() == 1 && msg.value(0) == 0xfe) {
sendChipID();
}
if (msg.len() == 2 && msg.value(0) == 0xff) {
this->device().getHal().standbyLedInvert(msg.value(1) == 0x01);
}
return true;
}
bool process (__attribute__((unused)) const RemoteEventMsg& msg) {return false; }
bool process (__attribute__((unused)) const SensorEventMsg& msg) {return false; }
};
#ifdef USE_WIEGAND
template <class RFIDDev,class RFIDChannel,WIEGAND& rdrDev,int LED_GREEN,int LED_RED>
#endif
#if (defined(USE_MFRC522_I2C) || defined(USE_MFRC522_SPI))
template <class RFIDDev,class RFIDChannel,MFRC522& rdrDev,int LED_GREEN,int LED_RED>
#endif
#ifdef USE_RDM6300
template <class RFIDDev,class RFIDChannel,SoftwareSerial& rdrDev,int LED_GREEN,int LED_RED>
#endif
class RFIDScanner : public Alarm {
RFIDDev& dev;
DualStatusLed<LED_GREEN,LED_RED> led;
uint8_t cnt;
public:
RFIDScanner (RFIDDev& d) : Alarm(millis2ticks(500)), dev(d), cnt(0) {
led.init();
}
virtual ~RFIDScanner () {}
RFIDChannel* learning () {
for( uint8_t i=0; i<dev.rfidCount(); ++i ) {
RFIDChannel& rc = dev.rfidChannel(i);
if( rc.learn() == true ) {
return &rc;
}
}
return 0;
}
RFIDChannel* matches (uint8_t* addr) {
for( uint8_t i=0; i<dev.rfidCount(); ++i ) {
RFIDChannel& rc = dev.rfidChannel(i);
if( rc.match(addr) == true ) {
return &rc;
}
}
return 0;
}
RFIDChannel* find (uint8_t* addr) {
for( uint8_t i=0; i<dev.rfidCount(); ++i ) {
RFIDChannel& rc = dev.rfidChannel(i);
if( rc.isID(addr) == true ) {
return &rc;
}
}
return 0;
}
void DADDR(uint8_t * addr) {
for (uint8_t i = 0; i < ID_ADDR_SIZE; i++)
DHEX(addr[i]);
DPRINTLN(F(""));
}
void start () {
for( uint8_t i=0; i<dev.rfidCount(); ++i ) {
RFIDChannel& rc = dev.rfidChannel(i);
rc.start();
}
}
void finish () {
for( uint8_t i=0; i<dev.rfidCount(); ++i ) {
RFIDChannel& rc = dev.rfidChannel(i);
rc.finish();
}
}
bool check (uint8_t* addr) {
bool res = false;
for( uint8_t i=0; i<dev.rfidCount(); ++i ) {
RFIDChannel& rc = dev.rfidChannel(i);
res |= rc.check(addr);
}
return res;
}
bool getRfidAddress(uint8_t *addr) {
#ifdef USE_WIEGAND
if (rdrDev.available()) {
memset(addr,0x00, ID_ADDR_SIZE);
unsigned long wgAddr = rdrDev.getCode();
byte addrArr[8];
for (uint8_t i = 0; i < ID_ADDR_SIZE; i++)
addrArr[i] = wgAddr >> (i*8) & 0xff;
memcpy(addr, addrArr, ID_ADDR_SIZE);
//DADDR(addr);
return true;
}
return false;
#endif
#if (defined(USE_MFRC522_I2C) || defined(USE_MFRC522_SPI))
if (!rdrDev.PICC_IsNewCardPresent())
if (!rdrDev.PICC_IsNewCardPresent())
return false;
if (!rdrDev.PICC_ReadCardSerial()) return false;
memset(addr,0x00,ID_ADDR_SIZE);
memcpy(addr,rdrDev.uid.uidByte,rdrDev.uid.size);
//DADDR(addr);
return true;
#endif
#ifdef USE_RDM6300
while (rdrDev.available() > 0) {
char d = rdrDev.read();
static uint8_t bytecount = 0;
static uint8_t addrval = 0;
static bool decode = false;
switch (d) {
case 0x02:
bytecount = 0;
memset(addr,0x00,ID_ADDR_SIZE);
decode = true;
break;
case 0x03:
decode = false;
while (rdrDev.available()) rdrDev.read(); //empty rx buffer
return true;
break;
default:
if (decode == true) {
uint8_t val = (d > 57) ? d -= 55 : d -= 48;
if (bytecount % 2 == 0) {
addrval = val << 4;
} else {
addrval |= val;
addr[bytecount/2] = addrval;
//DPRINT("[");DDEC(bytecount/2);DPRINT("]=");DHEXLN(addrval);
addrval = 0;
}
bytecount++;
}
break;
}
}
return false;
#endif
}
bool readRfid(uint8_t *addr) {
uint8_t iD[ID_ADDR_SIZE];
static uint8_t last_addr[ID_ADDR_SIZE];
bool success = false;
memset(addr,0,ID_ADDR_SIZE);
if (getRfidAddress(iD)) {
memcpy(addr,iD,ID_ADDR_SIZE);
if (memcmp(addr, last_addr, ID_ADDR_SIZE) != 0) {
dev.buzzer().on(millis2ticks(100));
}
success = true;
}
memcpy(last_addr,addr,ID_ADDR_SIZE);
return success;
}
void scan () {
uint8_t addr[ID_ADDR_SIZE];
start();
bool readID = readRfid(addr);
if( check(addr) == true ) {
led.ledOn(millis2ticks(500),0);
} else {
if (readID == true )
dev.buzzer().on(millis2ticks(40),millis2ticks(40),3);
}
finish();
}
bool learn (RFIDChannel* lc) {
uint8_t addr[ID_ADDR_SIZE];
while( readRfid(addr) == true ) {
if( find(addr) == 0 ) {
lc->storeID(addr);
dev.buzzer().on(millis2ticks(40), millis2ticks(50),10);
return true;
}
}
return false;
}
void trigger (AlarmClock& clock) {
// reactivate
set(millis2ticks(500));
clock.add(*this);
++cnt;
// check if we have a learning channel
RFIDChannel* lc = learning();
if( lc != 0 ) {
uint8_t cycle = cnt & 0x01;
led.ledOn(cycle == 0 ? tick : 0, cycle == 0 ? 0 : tick);
dev.buzzer().on(millis2ticks(40));
// if we have learned a new ID
if( learn(lc) == true ) {
clock.cancel(*this);
set(seconds2ticks(5));
led.ledOff();
led.ledOn(tick);
clock.add(*this);
}
}
else {
// scan the bus now
scan();
}
}
};
}
#endif