-
Notifications
You must be signed in to change notification settings - Fork 144
/
NfcTag.cpp
123 lines (107 loc) · 2.4 KB
/
NfcTag.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
#include <NfcTag.h>
NfcTag::NfcTag()
{
_uid = 0;
_uidLength = 0;
_tagType = "Unknown";
_ndefMessage = (NdefMessage*)NULL;
}
NfcTag::NfcTag(byte *uid, unsigned int uidLength)
{
_uid = uid;
_uidLength = uidLength;
_tagType = "Unknown";
_ndefMessage = (NdefMessage*)NULL;
}
NfcTag::NfcTag(byte *uid, unsigned int uidLength, String tagType)
{
_uid = uid;
_uidLength = uidLength;
_tagType = tagType;
_ndefMessage = (NdefMessage*)NULL;
}
NfcTag::NfcTag(byte *uid, unsigned int uidLength, String tagType, NdefMessage& ndefMessage)
{
_uid = uid;
_uidLength = uidLength;
_tagType = tagType;
_ndefMessage = new NdefMessage(ndefMessage);
}
// I don't like this version, but it will use less memory
NfcTag::NfcTag(byte *uid, unsigned int uidLength, String tagType, const byte *ndefData, const int ndefDataLength)
{
_uid = uid;
_uidLength = uidLength;
_tagType = tagType;
_ndefMessage = new NdefMessage(ndefData, ndefDataLength);
}
NfcTag::~NfcTag()
{
delete _ndefMessage;
}
NfcTag& NfcTag::operator=(const NfcTag& rhs)
{
if (this != &rhs)
{
delete _ndefMessage;
_uid = rhs._uid;
_uidLength = rhs._uidLength;
_tagType = rhs._tagType;
// TODO do I need a copy here?
_ndefMessage = rhs._ndefMessage;
}
return *this;
}
uint8_t NfcTag::getUidLength()
{
return _uidLength;
}
void NfcTag::getUid(byte *uid, unsigned int uidLength)
{
memcpy(uid, _uid, _uidLength < uidLength ? _uidLength : uidLength);
}
String NfcTag::getUidString()
{
String uidString = "";
for (unsigned int i = 0; i < _uidLength; i++)
{
if (i > 0)
{
uidString += " ";
}
if (_uid[i] < 0xF)
{
uidString += "0";
}
uidString += String((unsigned int)_uid[i], (unsigned char)HEX);
}
uidString.toUpperCase();
return uidString;
}
String NfcTag::getTagType()
{
return _tagType;
}
boolean NfcTag::hasNdefMessage()
{
return (_ndefMessage != NULL);
}
NdefMessage NfcTag::getNdefMessage()
{
return *_ndefMessage;
}
#ifdef NDEF_USE_SERIAL
void NfcTag::print()
{
Serial.print(F("NFC Tag - "));Serial.println(_tagType);
Serial.print(F("UID "));Serial.println(getUidString());
if (_ndefMessage == NULL)
{
Serial.println(F("\nNo NDEF Message"));
}
else
{
_ndefMessage->print();
}
}
#endif