to colorize a colorful pixel with a simple touch using nfc technology

Dependencies:   Chainable_RGB_LED mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers NfcTag.cpp Source File

NfcTag.cpp

00001 #include <NfcTag.h>
00002 #include <string.h>
00003 #include <PN532_debug.h>
00004 
00005 NfcTag::NfcTag()
00006 {
00007     _uid = 0;
00008     _uidLength = 0;
00009     _tagType = "Unknown";
00010     _ndefMessage = (NdefMessage*)NULL;
00011 }
00012 
00013 NfcTag::NfcTag(uint8_t *uid, unsigned int uidLength)
00014 {
00015     _uid = uid;
00016     _uidLength = uidLength;
00017     _tagType = "Unknown";
00018     _ndefMessage = (NdefMessage*)NULL;
00019 }
00020 
00021 NfcTag::NfcTag(uint8_t *uid, unsigned int  uidLength, string tagType)
00022 {
00023     _uid = uid;
00024     _uidLength = uidLength;
00025     _tagType = tagType;
00026     _ndefMessage = (NdefMessage*)NULL;
00027 }
00028 
00029 NfcTag::NfcTag(uint8_t *uid, unsigned int  uidLength, string tagType, NdefMessage& ndefMessage)
00030 {
00031     _uid = uid;
00032     _uidLength = uidLength;
00033     _tagType = tagType;
00034     _ndefMessage = new NdefMessage(ndefMessage);
00035 }
00036 
00037 // I don't like this version, but it will use less memory
00038 NfcTag::NfcTag(uint8_t *uid, unsigned int uidLength, string tagType, const uint8_t *ndefData, const int ndefDataLength)
00039 {
00040     _uid = uid;
00041     _uidLength = uidLength;
00042     _tagType = tagType;
00043     _ndefMessage = new NdefMessage(ndefData, ndefDataLength);
00044 }
00045 
00046 NfcTag::~NfcTag()
00047 {
00048     delete _ndefMessage;
00049 }
00050 
00051 NfcTag& NfcTag::operator=(const NfcTag& rhs)
00052 {
00053     if (this != &rhs)
00054     {
00055         delete _ndefMessage;
00056         _uid = rhs._uid;
00057         _uidLength = rhs._uidLength;
00058         _tagType = rhs._tagType;
00059         // TODO do I need a copy here?
00060         _ndefMessage = rhs._ndefMessage;
00061     }
00062     return *this;
00063 }
00064 
00065 uint8_t NfcTag::getUidLength()
00066 {
00067     return _uidLength;
00068 }
00069 
00070 void NfcTag::getUid(uint8_t *uid, unsigned int uidLength)
00071 {
00072     memcpy(_uid, uid, uidLength);
00073 }
00074 
00075 string NfcTag::getUidString()
00076 {
00077     string uidString = "";
00078 #if 0
00079     for (int i = 0; i < _uidLength; i++)
00080     {
00081         if (i > 0)
00082         {
00083             uidString += " ";
00084         }
00085 
00086         if (_uid[i] < 0xF)
00087         {
00088             uidString += "0";
00089         }
00090 
00091         uidString += string((unsigned int)_uid[i], 16);
00092     }
00093     uidString.toUpperCase();
00094 #endif
00095     return uidString;
00096 }
00097 
00098 string NfcTag::getTagType()
00099 {
00100     return _tagType;
00101 }
00102 
00103 bool NfcTag::hasNdefMessage()
00104 {
00105     return (_ndefMessage != NULL);
00106 }
00107 
00108 NdefMessage NfcTag::getNdefMessage()
00109 {
00110     return *_ndefMessage;
00111 }
00112 
00113 void NfcTag::print()
00114 {
00115     DMSG("NFC Tag - ");
00116     DMSG_INT(_tagType);
00117     DMSG("UID - ");
00118     DMSG(getUidString().c_str());
00119     if (_ndefMessage == NULL)
00120     {
00121         DMSG("\nNo NDEF Message");
00122     }
00123     else
00124     {
00125         _ndefMessage->print();
00126     }
00127 }