-
Notifications
You must be signed in to change notification settings - Fork 399
Expand file tree
/
Copy pathEM4233.c
More file actions
686 lines (548 loc) · 27.8 KB
/
Copy pathEM4233.c
File metadata and controls
686 lines (548 loc) · 27.8 KB
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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
/*
* EM4233.c
*
* Created on: 12-05-2018
* Author: ceres-c & MrMoDDoM
* Notes:
* - In EM4233.h you can find the define EM4233_LOGIN_YES_CARD that has to be uncommented
* to allow any login request without checking the password.
*
* TODO:
* - Check with real tag every command's actual response in addressed/selected State
* (Only EM4233_Read_Single and EM4233_Read_Multiple have been checked up to now)
*/
#ifdef CONFIG_EM4233_SUPPORT
#include "../Random.h"
#include "ISO15693-A.h"
#include "EM4233.h"
static enum {
STATE_READY,
STATE_SELECTED,
STATE_QUIET
} State;
bool loggedIn;
void EM4233AppInit(void) {
State = STATE_READY;
FrameInfo.Flags = NULL;
FrameInfo.Command = NULL;
FrameInfo.Parameters = NULL;
FrameInfo.ParamLen = 0;
FrameInfo.Addressed = false;
FrameInfo.Selected = false;
loggedIn = false;
MemoryReadBlock(&MyAFI, EM4233_MEM_AFI_ADDRESS, 1);
MemoryReadBlock(&Uid, EM4233_MEM_UID_ADDRESS, ActiveConfiguration.UidSize);
}
void EM4233AppReset(void) {
State = STATE_READY;
FrameInfo.Flags = NULL;
FrameInfo.Command = NULL;
FrameInfo.Parameters = NULL;
FrameInfo.ParamLen = 0;
FrameInfo.Addressed = false;
FrameInfo.Selected = false;
loggedIn = false;
MemoryReadBlock(&MyAFI, EM4233_MEM_AFI_ADDRESS, 1);
MemoryReadBlock(&Uid, EM4233_MEM_UID_ADDRESS, ActiveConfiguration.UidSize);
}
void EM4233AppTask(void) {
}
void EM4233AppTick(void) {
}
uint16_t EM4233_Lock_Block(uint8_t *FrameBuf, uint16_t FrameBytes) {
ResponseByteCount = ISO15693_APP_NO_RESPONSE;
uint8_t BlockAddress = *FrameInfo.Parameters;
uint8_t LockStatus = 0;
MemoryReadBlock(&LockStatus, (EM4233_MEM_LSM_ADDRESS + BlockAddress), 1);
if (FrameInfo.ParamLen != 1)
return ISO15693_APP_NO_RESPONSE; /* malformed: not enough or too much data */
if (BlockAddress > EM4233_NUMBER_OF_BLCKS) {
// FrameBuf[ISO15693_ADDR_FLAGS] = ISO15693_RES_FLAG_ERROR;
// FrameBuf[ISO15693_RES_ADDR_PARAM] = ISO15693_RES_ERR_OPT_NOT_SUPP;
ResponseByteCount = ISO15693_APP_NO_RESPONSE; /* real tag does not respond anyway */
return ResponseByteCount; /* malformed: trying to lock a non-existing block */
}
if (LockStatus > ISO15693_MASK_UNLOCKED) { /* LockStatus 0x00 represent unlocked block, greater values are different kind of locks */
ResponseByteCount = ISO15693_APP_NO_RESPONSE; /* real tag does not respond anyway */
} else {
LockStatus |= ISO15693_MASK_USER_LOCK;
MemoryWriteBlock(&LockStatus, (EM4233_MEM_LSM_ADDRESS + BlockAddress), 1); /* write user lock in memory */
// FrameBuf[ISO15693_ADDR_FLAGS] = ISO15693_RES_FLAG_NO_ERROR;
ResponseByteCount = ISO15693_APP_NO_RESPONSE; /* real tag does not respond anyway */
}
return ResponseByteCount;
}
uint16_t EM4233_Write_Single(uint8_t *FrameBuf, uint16_t FrameBytes) {
ResponseByteCount = ISO15693_APP_NO_RESPONSE;
uint8_t BlockAddress = *FrameInfo.Parameters;
uint8_t *Dataptr = FrameInfo.Parameters + 0x01; /* Data to write begins on 2nd byte of the frame received by the reader */
uint8_t LockStatus = 0;
if (FrameInfo.ParamLen != 5)
return ISO15693_APP_NO_RESPONSE; /* malformed: not enough or too much data */
if (BlockAddress > EM4233_NUMBER_OF_BLCKS) {
// FrameBuf[ISO15693_ADDR_FLAGS] = ISO15693_RES_FLAG_ERROR;
// FrameBuf[ISO15693_RES_ADDR_PARAM] = ISO15693_RES_ERR_OPT_NOT_SUPP;
ResponseByteCount = ISO15693_APP_NO_RESPONSE; /* real tag does not respond anyway */
return ResponseByteCount; /* malformed: trying to write in a non-existing block */
}
MemoryReadBlock(&LockStatus, (EM4233_MEM_LSM_ADDRESS + BlockAddress), 1);
if (LockStatus & ISO15693_MASK_FACTORY_LOCK) {
// FrameBuf[ISO15693_ADDR_FLAGS] = ISO15693_RES_FLAG_ERROR;
// FrameBuf[ISO15693_RES_ADDR_PARAM] = ISO15693_RES_ERR_OPT_NOT_SUPP;
ResponseByteCount = ISO15693_APP_NO_RESPONSE; /* real tag does not respond anyway - probably: no factory lock exists? */
} else if (LockStatus & ISO15693_MASK_USER_LOCK) {
// FrameBuf[ISO15693_ADDR_FLAGS] = ISO15693_RES_FLAG_ERROR;
// FrameBuf[ISO15693_RES_ADDR_PARAM] = ISO15693_RES_ERR_BLK_CHG_LKD;
ResponseByteCount = ISO15693_APP_NO_RESPONSE; /* real tag does not respond anyway */
} else {
MemoryWriteBlock(Dataptr, BlockAddress * EM4233_BYTES_PER_BLCK, EM4233_BYTES_PER_BLCK);
FrameBuf[ISO15693_ADDR_FLAGS] = ISO15693_RES_FLAG_NO_ERROR;
ResponseByteCount += 1;
}
return ResponseByteCount;
}
uint16_t EM4233_Read_Single(uint8_t *FrameBuf, uint16_t FrameBytes) {
ResponseByteCount = ISO15693_APP_NO_RESPONSE;
uint8_t FramePtr; /* holds the address where block's data will be put */
uint8_t BlockAddress = FrameInfo.Parameters[0];
uint8_t LockStatus = 0;
if (FrameInfo.ParamLen != 1)
return ISO15693_APP_NO_RESPONSE; /* malformed: not enough or too much data */
if (BlockAddress >= EM4233_NUMBER_OF_BLCKS) { /* check if the reader is requesting a sector out of bound */
if (FrameInfo.Addressed) { /* If the request is addressed */
FrameBuf[ISO15693_ADDR_FLAGS] = ISO15693_RES_FLAG_ERROR;
FrameBuf[ISO15693_RES_ADDR_PARAM] = ISO15693_RES_ERR_GENERIC;
ResponseByteCount += 2; /* Copied this behaviour from real tag, not specified in ISO documents */
}
return ResponseByteCount; /* If not addressed real tag does not respond */
}
FramePtr = 1;
if (FrameBuf[ISO15693_ADDR_FLAGS] & ISO15693_REQ_FLAG_OPTION) { /* request with option flag set */
MemoryReadBlock(&LockStatus, (EM4233_MEM_LSM_ADDRESS + BlockAddress), 1);
if (LockStatus & ISO15693_MASK_FACTORY_LOCK) { /* tests if the n-th bit of the factory bitmask if set to 1 */
FrameBuf[FramePtr] = ISO15693_MASK_FACTORY_LOCK; /* return bit 1 set as 1 (factory locked) */
} else if (LockStatus & ISO15693_MASK_USER_LOCK) { /* tests if the n-th bit of the user bitmask if set to 1 */
FrameBuf[FramePtr] = ISO15693_MASK_USER_LOCK; /* return bit 0 set as 1 (user locked) */
} else
FrameBuf[FramePtr] = ISO15693_MASK_UNLOCKED; /* return lock status 00 (unlocked) */
FramePtr += 1; /* block's data from byte 2 */
ResponseByteCount += 1;
}
MemoryReadBlock(&FrameBuf[FramePtr], BlockAddress * EM4233_BYTES_PER_BLCK, EM4233_BYTES_PER_BLCK);
ResponseByteCount += 4;
FrameBuf[ISO15693_ADDR_FLAGS] = ISO15693_RES_FLAG_NO_ERROR; /* flags */
ResponseByteCount += 1;
return ResponseByteCount;
}
uint16_t EM4233_Read_Multiple(uint8_t *FrameBuf, uint16_t FrameBytes) {
ResponseByteCount = ISO15693_APP_NO_RESPONSE;
uint8_t FramePtr; /* holds the address where block's data will be put */
uint8_t BlockAddress = FrameInfo.Parameters[0];
uint8_t BlocksNumber = FrameInfo.Parameters[1] + 0x01; /* according to ISO standard, we have to read 0x08 blocks if we get 0x07 in request */
if (FrameInfo.ParamLen != 2)
return ISO15693_APP_NO_RESPONSE; /* malformed: not enough or too much data */
if (BlockAddress >= EM4233_NUMBER_OF_BLCKS) { /* the reader is requesting a block out of bound */
if (FrameInfo.Addressed) { /* If the request is addressed */
FrameBuf[ISO15693_ADDR_FLAGS] = ISO15693_RES_FLAG_ERROR;
FrameBuf[ISO15693_RES_ADDR_PARAM] = ISO15693_RES_ERR_GENERIC;
ResponseByteCount += 2; /* Copied this behaviour from real tag, not specified in ISO documents */
}
return ResponseByteCount; /* If not addressed real tag does not respond */
} else if ((BlockAddress + BlocksNumber) >= EM4233_NUMBER_OF_BLCKS) { /* last block is out of bound */
BlocksNumber = EM4233_NUMBER_OF_BLCKS - BlockAddress; /* we read up to latest block, as real tag does */
}
FramePtr = 1; /* start of response data */
if ((FrameBuf[ISO15693_ADDR_FLAGS] & ISO15693_REQ_FLAG_OPTION) == 0) { /* blocks' lock status is not requested */
/* read data straight into frame */
MemoryReadBlock(&FrameBuf[FramePtr], BlockAddress * EM4233_BYTES_PER_BLCK, BlocksNumber * EM4233_BYTES_PER_BLCK);
ResponseByteCount += BlocksNumber * EM4233_BYTES_PER_BLCK;
} else { /* we have to slice blocks' data with lock statuses */
uint8_t DataBuffer[ BlocksNumber * EM4233_BYTES_PER_BLCK ]; /* a temporary vector with blocks' content */
uint8_t LockStatusBuffer[ BlocksNumber ]; /* a temporary vector with blocks' lock status */
/* read all at once to reduce timing issues */
MemoryReadBlock(&DataBuffer, BlockAddress * EM4233_BYTES_PER_BLCK, BlocksNumber * EM4233_BYTES_PER_BLCK);
MemoryReadBlock(&LockStatusBuffer, EM4233_MEM_LSM_ADDRESS + BlockAddress, BlocksNumber);
for (uint8_t block = 0; block < BlocksNumber; block++) { /* we cycle through the blocks */
/* add lock status */
FrameBuf[FramePtr++] = LockStatusBuffer[block]; /* Byte in dump equals to the byte that has to be sent */
/* I.E. We store 0x01 to identify user lock, which is the same as what ISO15693 enforce */
ResponseByteCount += 1;
/* then copy block's data */
FrameBuf[FramePtr++] = DataBuffer[block * EM4233_BYTES_PER_BLCK + 0];
FrameBuf[FramePtr++] = DataBuffer[block * EM4233_BYTES_PER_BLCK + 1];
FrameBuf[FramePtr++] = DataBuffer[block * EM4233_BYTES_PER_BLCK + 2];
FrameBuf[FramePtr++] = DataBuffer[block * EM4233_BYTES_PER_BLCK + 3];
ResponseByteCount += EM4233_BYTES_PER_BLCK;
}
}
FrameBuf[ISO15693_ADDR_FLAGS] = ISO15693_RES_FLAG_NO_ERROR; /* flags */
ResponseByteCount += 1;
return ResponseByteCount;
}
uint16_t EM4233_Write_AFI(uint8_t *FrameBuf, uint16_t FrameBytes) {
ResponseByteCount = ISO15693_APP_NO_RESPONSE;
uint8_t AFI = FrameInfo.Parameters[0];
uint8_t LockStatus = 0;
if (FrameInfo.ParamLen != 1)
return ISO15693_APP_NO_RESPONSE; /* malformed: not enough or too much data */
MemoryReadBlock(&LockStatus, EM4233_MEM_INF_ADDRESS, 1);
if (LockStatus & EM4233_MASK_AFI_STATUS) { /* The AFI is locked */
// FrameBuf[ISO15693_ADDR_FLAGS] = ISO15693_RES_FLAG_ERROR;
// FrameBuf[ISO15693_RES_ADDR_PARAM] = ISO15693_RES_ERR_GENERIC;
// ResponseByteCount += 2;
ResponseByteCount = ISO15693_APP_NO_RESPONSE; /* real tag does not respond anyway */
return ResponseByteCount;
}
MemoryWriteBlock(&AFI, EM4233_MEM_AFI_ADDRESS, 1); /* Actually write new AFI */
MyAFI = AFI; /* And update global variable */
// FrameBuf[ISO15693_ADDR_FLAGS] = ISO15693_RES_FLAG_NO_ERROR; /* flags */
// ResponseByteCount += 1;
ResponseByteCount = ISO15693_APP_NO_RESPONSE; /* real tag does not respond anyway */
return ResponseByteCount;
}
uint16_t EM4233_Lock_AFI(uint8_t *FrameBuf, uint16_t FrameBytes) {
ResponseByteCount = ISO15693_APP_NO_RESPONSE;
uint8_t LockStatus = 0;
if (FrameInfo.ParamLen != 0)
return ISO15693_APP_NO_RESPONSE; /* malformed: not enough or too much data */
MemoryReadBlock(&LockStatus, EM4233_MEM_INF_ADDRESS, 1);
if (LockStatus & EM4233_MASK_AFI_STATUS) { /* The AFI is already locked */
// FrameBuf[ISO15693_ADDR_FLAGS] = ISO15693_RES_FLAG_ERROR;
// FrameBuf[ISO15693_RES_ADDR_PARAM] = ISO15693_RES_ERR_GENERIC;
// ResponseByteCount += 2;
ResponseByteCount = ISO15693_APP_NO_RESPONSE; /* real tag does not respond anyway */
return ResponseByteCount;
}
LockStatus |= EM4233_MASK_AFI_STATUS;
MemoryWriteBlock(&LockStatus, EM4233_MEM_INF_ADDRESS, 1); /* Write in info bits AFI lockdown */
// FrameBuf[ISO15693_ADDR_FLAGS] = ISO15693_RES_FLAG_NO_ERROR; /* flags */
// ResponseByteCount += 1;
ResponseByteCount = ISO15693_APP_NO_RESPONSE; /* real tag does not respond anyway */
return ResponseByteCount;
}
uint16_t EM4233_Write_DSFID(uint8_t *FrameBuf, uint16_t FrameBytes) {
ResponseByteCount = ISO15693_APP_NO_RESPONSE;
uint8_t DSFID = FrameInfo.Parameters[0];
uint8_t LockStatus = 0;
if (FrameInfo.ParamLen != 1)
return ISO15693_APP_NO_RESPONSE; /* malformed: not enough or too much data */
MemoryReadBlock(&LockStatus, EM4233_MEM_INF_ADDRESS, 1);
if (LockStatus & EM4233_MASK_DSFID_STATUS) { /* The DSFID is locked */
// FrameBuf[ISO15693_ADDR_FLAGS] = ISO15693_RES_FLAG_ERROR;
// FrameBuf[ISO15693_RES_ADDR_PARAM] = ISO15693_RES_ERR_GENERIC;
// ResponseByteCount += 2;
ResponseByteCount = ISO15693_APP_NO_RESPONSE; /* real tag does not respond anyway */
return ResponseByteCount;
}
MemoryWriteBlock(&DSFID, EM4233_MEM_DSFID_ADDRESS, 1); /* Actually write new DSFID */
// FrameBuf[ISO15693_ADDR_FLAGS] = ISO15693_RES_FLAG_NO_ERROR; /* flags */
// ResponseByteCount += 1;
ResponseByteCount = ISO15693_APP_NO_RESPONSE; /* real tag does not respond anyway */
return ResponseByteCount;
}
uint16_t EM4233_Lock_DSFID(uint8_t *FrameBuf, uint16_t FrameBytes) {
ResponseByteCount = ISO15693_APP_NO_RESPONSE;
uint8_t LockStatus = 0;
if (FrameInfo.ParamLen != 0)
return ISO15693_APP_NO_RESPONSE; /* malformed: not enough or too much data */
MemoryReadBlock(&LockStatus, EM4233_MEM_INF_ADDRESS, 1);
if (LockStatus & EM4233_MASK_DSFID_STATUS) { /* The DSFID is already locked */
// FrameBuf[ISO15693_ADDR_FLAGS] = ISO15693_RES_FLAG_ERROR;
// FrameBuf[ISO15693_RES_ADDR_PARAM] = ISO15693_RES_ERR_GENERIC;
// ResponseByteCount += 2;
ResponseByteCount = ISO15693_APP_NO_RESPONSE; /* real tag does not respond anyway */
return ResponseByteCount;
}
LockStatus |= EM4233_MASK_DSFID_STATUS;
MemoryWriteBlock(&LockStatus, EM4233_MEM_INF_ADDRESS, 1); /* Write in info bits DSFID lockdown */
// FrameBuf[ISO15693_ADDR_FLAGS] = ISO15693_RES_FLAG_NO_ERROR; /* flags */
// ResponseByteCount += 1;
ResponseByteCount = ISO15693_APP_NO_RESPONSE; /* real tag does not respond anyway */
return ResponseByteCount;
}
uint8_t EM4233_Get_SysInfo(uint8_t *FrameBuf, uint16_t FrameBytes) {
ResponseByteCount = ISO15693_APP_NO_RESPONSE;
uint8_t FramePtr; /* holds the address where block's data will be put */
if (FrameInfo.ParamLen != 0)
return ISO15693_APP_NO_RESPONSE; /* malformed: not enough or too much data */
FramePtr = 1;
/* I've no idea how this request could generate errors ._.
if ( ) {
FrameBuf[ISO15693_ADDR_FLAGS] = ISO15693_RES_FLAG_ERROR;
FrameBuf[ISO15693_RES_ADDR_PARAM] = ISO15693_RES_ERR_GENERIC;
ResponseByteCount += 2;
return ResponseByteCount;
}
*/
/* System info flags */
FrameBuf[FramePtr] = EM4233_SYSINFO_BYTE; /* check EM4233SLIC datasheet for this */
FramePtr += 1; /* Move forward the buffer data pointer */
ResponseByteCount += 1; /* Increment the response count */
/* Then append UID */
ISO15693CopyUid(&FrameBuf[FramePtr], Uid);
FramePtr += ISO15693_GENERIC_UID_SIZE; /* Move forward the buffer data pointer */
ResponseByteCount += ISO15693_GENERIC_UID_SIZE; /* Increment the response count */
/* Append DSFID */
if (EM4233_SYSINFO_BYTE & (1 << 0)) {
MemoryReadBlock(&FrameBuf[FramePtr], EM4233_MEM_DSFID_ADDRESS, 1);
FramePtr += 1; /* Move forward the buffer data pointer */
ResponseByteCount += 1; /* Increment the response count */
}
/* Append AFI */
if (EM4233_SYSINFO_BYTE & (1 << 1)) {
MemoryReadBlock(&FrameBuf[FramePtr], EM4233_MEM_AFI_ADDRESS, 1);
FramePtr += 1; /* Move forward the buffer data pointer */
ResponseByteCount += 1; /* Increment the response count */
}
/* Append VICC memory size */
if (EM4233_SYSINFO_BYTE & (1 << 2)) {
FrameBuf[FramePtr] = EM4233_NUMBER_OF_BLCKS - 0x01;
FramePtr += 1; /* Move forward the buffer data pointer */
ResponseByteCount += 1; /* Increment the response count */
FrameBuf[FramePtr] = EM4233_BYTES_PER_BLCK - 0x01;
FramePtr += 1; /* Move forward the buffer data pointer */
ResponseByteCount += 1; /* Increment the response count */
}
/* Append IC reference */
if (EM4233_SYSINFO_BYTE & (1 << 3)) {
FrameBuf[FramePtr] = EM4233_IC_REFERENCE;
FramePtr += 1; /* Move forward the buffer data pointer */
ResponseByteCount += 1; /* Increment the response count */
}
FrameBuf[ISO15693_ADDR_FLAGS] = ISO15693_RES_FLAG_NO_ERROR; /* flags */
ResponseByteCount += 1;
return ResponseByteCount;
}
uint16_t EM4233_Get_Multi_Block_Sec_Stat(uint8_t *FrameBuf, uint16_t FrameBytes) {
ResponseByteCount = ISO15693_APP_NO_RESPONSE;
uint8_t FramePtr; /* holds the address where block's data will be put */
uint8_t BlockAddress = FrameInfo.Parameters[0];
uint8_t BlocksNumber = FrameInfo.Parameters[1] + 0x01;
if (FrameInfo.ParamLen != 2)
return ISO15693_APP_NO_RESPONSE; /* malformed: not enough or too much data */
if (BlockAddress > EM4233_NUMBER_OF_BLCKS) { /* the reader is requesting a starting block out of bound */
// FrameBuf[ISO15693_ADDR_FLAGS] = ISO15693_RES_FLAG_ERROR;
// FrameBuf[ISO15693_RES_ADDR_PARAM] = ISO15693_RES_ERR_BLK_NOT_AVL;
ResponseByteCount = ISO15693_APP_NO_RESPONSE; /* real tag does not respond anyway */
return ResponseByteCount;
} else if ((BlockAddress + BlocksNumber) >= EM4233_NUMBER_OF_BLCKS) { /* last block is out of bound */
BlocksNumber = EM4233_NUMBER_OF_BLCKS - BlockAddress; /* we read up to latest block, as real tag does */
}
FramePtr = 1; /* start of response data */
/* read all at once to reduce timing issues */
MemoryReadBlock(&FrameBuf[FramePtr], EM4233_MEM_LSM_ADDRESS + BlockAddress, BlocksNumber);
ResponseByteCount += BlocksNumber;
FrameBuf[ISO15693_ADDR_FLAGS] = ISO15693_RES_FLAG_NO_ERROR; /* flags */
ResponseByteCount += 1;
return ResponseByteCount;
}
uint16_t EM4233_Select(uint8_t *FrameBuf, uint16_t FrameBytes, uint8_t *Uid) {
ResponseByteCount = ISO15693_APP_NO_RESPONSE;
/* I've no idea how this request could generate errors ._.
if ( ) {
FrameBuf[ISO15693_ADDR_FLAGS] = ISO15693_RES_FLAG_ERROR;
FrameBuf[ISO15693_RES_ADDR_PARAM] = ISO15693_RES_ERR_GENERIC;
ResponseByteCount += 2;
return ResponseByteCount;
}
*/
bool UidEquals = ISO15693CompareUid(&FrameBuf[ISO15693_REQ_ADDR_PARAM], Uid);
if (!(FrameBuf[ISO15693_ADDR_FLAGS] & ISO15693_REQ_FLAG_ADDRESS) ||
(FrameBuf[ISO15693_ADDR_FLAGS] & ISO15693_REQ_FLAG_SELECT)
) {
/* tag should remain silent if Select is performed without address flag or with select flag */
return ISO15693_APP_NO_RESPONSE;
} else if (!UidEquals) {
/* tag should remain silent and reset if Select is performed against another UID,
* whether our the tag is selected or not
*/
State = STATE_READY;
return ISO15693_APP_NO_RESPONSE;
} else if (State != STATE_SELECTED && UidEquals) {
State = STATE_SELECTED;
FrameBuf[ISO15693_ADDR_FLAGS] = ISO15693_RES_FLAG_NO_ERROR;
ResponseByteCount += 1;
return ResponseByteCount;
}
/* This should never happen (TM), I've added it to shut the compiler up */
State = STATE_READY;
return ISO15693_APP_NO_RESPONSE;
}
uint16_t EM4233_Reset_To_Ready(uint8_t *FrameBuf, uint16_t FrameBytes) {
ResponseByteCount = ISO15693_APP_NO_RESPONSE;
/* I've no idea how this request could generate errors ._.
if ( ) {
FrameBuf[ISO15693_ADDR_FLAGS] = ISO15693_RES_FLAG_ERROR;
FrameBuf[ISO15693_RES_ADDR_PARAM] = ISO15693_RES_ERR_GENERIC;
ResponseByteCount += 2;
return ResponseByteCount;
}
*/
FrameInfo.Flags = NULL;
FrameInfo.Command = NULL;
FrameInfo.Parameters = NULL;
FrameInfo.ParamLen = 0;
FrameInfo.Addressed = false;
FrameInfo.Selected = false;
State = STATE_READY;
FrameBuf[ISO15693_ADDR_FLAGS] = ISO15693_RES_FLAG_NO_ERROR;
ResponseByteCount += 1;
return ResponseByteCount;
}
uint16_t EM4233_Login(uint8_t *FrameBuf, uint16_t FrameBytes) {
ResponseByteCount = ISO15693_APP_NO_RESPONSE;
uint8_t Password[4] = { 0 };
if (FrameInfo.ParamLen != 4 || !FrameInfo.Addressed || !(FrameInfo.Selected && State == STATE_SELECTED))
/* Malformed: not enough or too much data. Also this command only works in addressed mode */
return ISO15693_APP_NO_RESPONSE;
MemoryReadBlock(&Password, EM4233_MEM_PSW_ADDRESS, 4);
#ifdef EM4233_LOGIN_YES_CARD
/* Accept any password from reader as correct one */
loggedIn = true;
MemoryWriteBlock(FrameInfo.Parameters, EM4233_MEM_PSW_ADDRESS, 4); /* Store password in memory for retrival */
#else
/* Check if the password is actually the right one */
if (memcmp(Password, FrameInfo.Parameters, 4) != 0) { /* Incorrect password */
loggedIn = false;
// FrameBuf[ISO15693_ADDR_FLAGS] = ISO15693_RES_FLAG_ERROR;
// FrameBuf[ISO15693_RES_ADDR_PARAM] = ISO15693_RES_ERR_GENERIC;
ResponseByteCount = ISO15693_APP_NO_RESPONSE;
return ResponseByteCount;
}
#endif
loggedIn = true;
FrameBuf[ISO15693_ADDR_FLAGS] = ISO15693_RES_FLAG_NO_ERROR; /* flags */
ResponseByteCount += 1;
return ResponseByteCount;
}
uint16_t EM4233_Auth1(uint8_t *FrameBuf, uint16_t FrameBytes) {
ResponseByteCount = ISO15693_APP_NO_RESPONSE;
// uint8_t KeyNo = *FrameInfo.Parameters; /* Right now this parameter is unused, but it will be useful */
if (FrameInfo.ParamLen != 1) /* Malformed: not enough or too much data */
return ISO15693_APP_NO_RESPONSE;
FrameBuf[ISO15693_ADDR_FLAGS] = ISO15693_RES_FLAG_NO_ERROR;
#ifdef EM4233_LOGIN_YES_CARD
/* Will be useful for testing purposes, probably removed in final version */
FrameBuf[ISO15693_ADDR_FLAGS + 1] = 0x00;
FrameBuf[ISO15693_ADDR_FLAGS + 2] = 0x00;
FrameBuf[ISO15693_ADDR_FLAGS + 3] = 0x00;
FrameBuf[ISO15693_ADDR_FLAGS + 4] = 0x00;
FrameBuf[ISO15693_ADDR_FLAGS + 5] = 0x00;
FrameBuf[ISO15693_ADDR_FLAGS + 6] = 0x00;
FrameBuf[ISO15693_ADDR_FLAGS + 7] = 0x00;
#else
/* Respond like a real tag */
RandomGetBuffer(FrameBuf + 0x01, 7); /* Random number A1 in EM Marin definitions */
#endif
ResponseByteCount += 8;
return ResponseByteCount;
}
uint16_t EM4233_Auth2(uint8_t *FrameBuf, uint16_t FrameBytes) {
ResponseByteCount = ISO15693_APP_NO_RESPONSE;
// uint8_t A2 = FrameInfo.Parameters;
// uint8_t f = FrameInfo.Parameters + 0x08;
// uint8_t g[3] = { 0 }; /* Names according to EM Marin definitions */
if (FrameInfo.ParamLen != 11) /* Malformed: not enough or too much data */
return ISO15693_APP_NO_RESPONSE;
// else if (!fFunc()) /* Actual f implementation to check if the f bytes we received are correct */
// return ISO15693_APP_NO_RESPONSE;
FrameBuf[ISO15693_ADDR_FLAGS] = ISO15693_RES_FLAG_NO_ERROR;
RandomGetBuffer(FrameBuf + 0x01, 3); /* This should be replaced with actual gFunc() implementation */
ResponseByteCount += 4;
return ResponseByteCount;
}
uint16_t EM4233AppProcess(uint8_t *FrameBuf, uint16_t FrameBytes) {
ResponseByteCount = ISO15693_APP_NO_RESPONSE;
if ((FrameBytes < ISO15693_MIN_FRAME_SIZE) || !ISO15693CheckCRC(FrameBuf, FrameBytes - ISO15693_CRC16_SIZE))
/* malformed frame */
return ResponseByteCount;
if (FrameBuf[ISO15693_REQ_ADDR_CMD] == ISO15693_CMD_SELECT) {
/* Select has its own path before PrepareFrame because we have to change the variable State
* from Select to Ready if "Select" cmd is addressed to another tag.
* It felt weird to add this kind of check in ISO15693PrepareFrame, which should not
* interfere with tag specific variables, such as State in this case.
*/
ResponseByteCount = EM4233_Select(FrameBuf, FrameBytes, Uid);
} else if (!ISO15693PrepareFrame(FrameBuf, FrameBytes, &FrameInfo, State == STATE_SELECTED, Uid, MyAFI))
return ISO15693_APP_NO_RESPONSE;
if (State == STATE_READY || State == STATE_SELECTED) {
switch (*FrameInfo.Command) {
case ISO15693_CMD_INVENTORY:
if (FrameInfo.ParamLen == 0)
return ISO15693_APP_NO_RESPONSE; /* malformed: not enough or too much data */
if (ISO15693AntiColl(FrameBuf, FrameBytes, &FrameInfo, Uid)) {
FrameBuf[ISO15693_ADDR_FLAGS] = ISO15693_RES_FLAG_NO_ERROR;
MemoryReadBlock(&FrameBuf[ISO15693_RES_ADDR_PARAM], EM4233_MEM_DSFID_ADDRESS, 1);
ISO15693CopyUid(&FrameBuf[ISO15693_RES_ADDR_PARAM + 0x01], Uid);
ResponseByteCount += 10;
}
break;
case ISO15693_CMD_STAY_QUIET:
if (FrameInfo.Addressed)
State = STATE_QUIET;
break;
case ISO15693_CMD_READ_SINGLE:
ResponseByteCount = EM4233_Read_Single(FrameBuf, FrameBytes);
break;
case ISO15693_CMD_WRITE_SINGLE:
ResponseByteCount = EM4233_Write_Single(FrameBuf, FrameBytes);
break;
case ISO15693_CMD_LOCK_BLOCK:
ResponseByteCount = EM4233_Lock_Block(FrameBuf, FrameBytes);
break;
case ISO15693_CMD_READ_MULTIPLE:
ResponseByteCount = EM4233_Read_Multiple(FrameBuf, FrameBytes);
break;
case ISO15693_CMD_WRITE_AFI:
ResponseByteCount = EM4233_Write_AFI(FrameBuf, FrameBytes);
break;
case ISO15693_CMD_LOCK_AFI:
ResponseByteCount = EM4233_Lock_AFI(FrameBuf, FrameBytes);
break;
case ISO15693_CMD_WRITE_DSFID:
ResponseByteCount = EM4233_Write_DSFID(FrameBuf, FrameBytes);
break;
case ISO15693_CMD_LOCK_DSFID:
ResponseByteCount = EM4233_Lock_DSFID(FrameBuf, FrameBytes);
break;
case ISO15693_CMD_GET_SYS_INFO:
ResponseByteCount = EM4233_Get_SysInfo(FrameBuf, FrameBytes);
break;
case ISO15693_CMD_GET_BLOCK_SEC:
ResponseByteCount = EM4233_Get_Multi_Block_Sec_Stat(FrameBuf, FrameBytes);
break;
case ISO15693_CMD_RESET_TO_READY:
ResponseByteCount = EM4233_Reset_To_Ready(FrameBuf, FrameBytes);
break;
case EM4233_CMD_LOGIN:
ResponseByteCount = EM4233_Login(FrameBuf, FrameBytes);
break;
case EM4233_CMD_AUTH1:
ResponseByteCount = EM4233_Auth1(FrameBuf, FrameBytes);
break;
case EM4233_CMD_AUTH2:
ResponseByteCount = EM4233_Auth2(FrameBuf, FrameBytes);
break;
default:
if (FrameInfo.Addressed) {
FrameBuf[ISO15693_ADDR_FLAGS] = ISO15693_RES_FLAG_ERROR;
FrameBuf[ISO15693_RES_ADDR_PARAM] = ISO15693_RES_ERR_NOT_SUPP;
ResponseByteCount = 2;
} /* EM4233 respond with error flag only to addressed commands */
break;
}
} else if (State == STATE_QUIET) {
if (*FrameInfo.Command == ISO15693_CMD_RESET_TO_READY) {
ResponseByteCount = EM4233_Reset_To_Ready(FrameBuf, FrameBytes);
}
}
return ResponseByteCount;
}
void EM4233GetUid(ConfigurationUidType Uid) {
MemoryReadBlock(&Uid[0], EM4233_MEM_UID_ADDRESS, ActiveConfiguration.UidSize);
}
void EM4233SetUid(ConfigurationUidType NewUid) {
memcpy(Uid, NewUid, ActiveConfiguration.UidSize);
MemoryWriteBlock(NewUid, EM4233_MEM_UID_ADDRESS, ActiveConfiguration.UidSize);
}
#endif /* CONFIG_EM4233_SUPPORT */