-
Notifications
You must be signed in to change notification settings - Fork 7
/
ntagadapter.cpp
104 lines (90 loc) · 2.12 KB
/
ntagadapter.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
#include "ntagadapter.h"
bool NtagAdapter::begin()
{
_ntag->begin();
_ntag->getUid(uid, sizeof(uid));
}
bool NtagAdapter::readerPresent(unsigned long timeout)
{
unsigned long startTime=millis();
do
{
if(_ntag->isReaderPresent())
{
return true;
}
}while(millis()<startTime+timeout);
return false;
}
bool NtagAdapter::waitUntilRfDone(unsigned int uiTimeOut)
{
if(uiTimeOut>0)
{
unsigned long ulStartTime=millis();
while(millis() < ulStartTime+uiTimeOut)
{
if(!(_ntag->isRfBusy()))
{
return true;
}
}
}
return !(_ntag->isRfBusy());
}
bool NtagAdapter::rfBusy(){
return _ntag->isRfBusy();
}
// Decode the NDEF data length from the Mifare TLV
// Leading null TLVs (0x0) are skipped
// Assuming T & L of TLV will be in the first block
// messageLength and messageStartIndex written to the parameters
// success or failure status is returned
//
// { 0x3, LENGTH }
bool NtagAdapter::decodeTlv(byte *data, int &messageLength, int &messageStartIndex)
{
int i = getNdefStartIndex(data);
if (i < 0 || data[i] != 0x3)
{
Serial.println(F("Error. Can't decode message length."));
return false;
}
else
{
messageLength = data[i+1];
messageStartIndex = i + 2;
}
return true;
}
// skip null tlvs (0x0) before the real message
// technically unlimited null tlvs, but we assume
// T & L of TLV in the first block we read
int NtagAdapter::getNdefStartIndex(byte *data)
{
for (int i = 0; i < 16; i++)
{
if (data[i] == 0x0)
{
// do nothing, skip
}
else if (data[i] == MESSAGE_TYPE_NDEF)
{
return i;
}
else
{
Serial.print("Unknown TLV ");Serial.println(data[i], HEX);
return -2;
}
}
return -1;
}
byte NtagAdapter::getUidLength()
{
return UID_LENGTH;
}
bool NtagAdapter::getUid(byte *uidin, unsigned int uidLength)
{
memcpy(uidin, uid, UID_LENGTH < uidLength ? UID_LENGTH : uidLength);
return true;
}