Utility library to read and write Ndef messages from/to a Type4 NFC tag
Dependents: NFC M2M_2016_STM32 MyongjiElec_capstone1 IDW01M1_Cloud_IBM ... more
Fork of NDefLib by
NDEF NFC library
This library provides an abstract API to create NDEF formatted messages and records and to read/write them from/to a Type4 NFC Tag.
Implementations
At the moment, the NDEF API is implemented by X_NUCLEO_NFC01A1 and X_NUCLEO_NFC02A1 Dynamic NFC Tag libraries respectively driving the X-NUCLEO-NFC01A1 and X-NUCLEO-NFC02A1 boards.
Diff: NDefNfcTag.h
- Revision:
- 8:473f6e0b03df
- Child:
- 9:689c1f56f359
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/NDefNfcTag.h Tue Dec 22 15:34:06 2015 +0000 @@ -0,0 +1,172 @@ +/** + ****************************************************************************** + * @file Type4NfcTag.h + * @author ST / Central Labs + * @version V1.0.0 + * @date 1 Nov 2015 + * @brief Generic interface that a device must implement for use the NDefLib + ****************************************************************************** + * @attention + * + * <h2><center>© COPYRIGHT(c) 2015 STMicroelectronics</center></h2> + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of STMicroelectronics nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************** + */ +#ifndef NDefNFCTAG_H_ +#define NDefNFCTAG_H_ + +#include "NDefLib/Message.h" + +namespace NDefLib { + +/** + * Abstract class used for write/read NDef message in an nfc tag + */ +class NDefNfcTag { +public: + + NDefNfcTag():mSessionIsOpen(false){} + + /** + * open the communication with the nfc tag + * \par when override this method call this implementation as last action for set the session opened + * @param force force to open a communication + * @return true if success + */ + virtual bool openSession(bool force = false){ + mSessionIsOpen=true; + return true; + } + + /** + * close the communication with the nfc tag + * \par when override this method call this implementation as last action for set the session closed + * @return true if success + */ + virtual bool closeSession(){ + mSessionIsOpen=false; + return true; + } + + /** + * write a message in the nfc tag, this call will delete the previous message + * @param msg message to write + * @return true if success + */ + bool write(const Message &msg) { + if(!mSessionIsOpen) + return false; + + const uint16_t length = msg.getByteLength(); + uint8_t *buffer = new uint8_t[length]; + if(buffer==NULL) //impossible allocate the buffer for write the message + return false; + msg.write(buffer); + bool retValue = writeByte(buffer, length); + delete[] buffer; + return retValue; + } + + /** + * read a message from the tag + * @param[in,out] msg message object where we will add the record read by the + * tag + * @return true if success + */ + bool read(Message *msg) { + if(!mSessionIsOpen) + return false; + + uint16_t length = getMessageLength(); + if (length == 0) + return false; + //else + uint8_t *buffer = new uint8_t[length]; + if(buffer==NULL) + return false; + + //read all the message content + bool retValue = readByte(2, length, buffer); + if (retValue) { + Message::parseMessage(buffer, length, msg); + } + delete[] buffer; + return retValue; + } + + virtual ~NDefNfcTag() {} + + /** + * true if we have a communication open with the nfc tag + * @return + */ + bool isSessionOpen(){ + return mSessionIsOpen; + } + + +protected: + + /** + * write a sequence of byte in the NDEF file + * @param buffer buffer to write + * @param length number of byte to write + * @param offset offset where start to write + * @return true if success + */ + virtual bool writeByte(const uint8_t *buffer, uint16_t length, uint16_t offset=0)=0; + + /** + * read a sequence of byte from the NDEF file + * @param byteOffset offset were start read + * @param byteLength number of byte to read + * @param buffer buffer where store the data read + * @return true if success + */ + virtual bool readByte(const uint16_t byteOffset, const uint16_t byteLength, + uint8_t *buffer)=0; + +private: + /** + * variable to set when we open/close a communication channel with the tag + */ + bool mSessionIsOpen; + + /** + * read the NDEF message length + * @return NDEF message length + */ + uint16_t getMessageLength() { + uint8_t lenghtByte[2]; + if (readByte(0, 2, lenghtByte)) + return (((uint16_t) lenghtByte[0]) << 8 | lenghtByte[1]); + return 0; + } //getMessageLength + +}; + +} /* namespace NDefLib */ + +#endif /* NDefNFCTAG_H_ */