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.
NDefNfcTag.h
- Committer:
- giovannivisentini
- Date:
- 2016-01-14
- Revision:
- 12:ed4d9b8d1410
- Parent:
- 11:eaf42739791e
- Child:
- 14:ba0c186ae6d6
File content as of revision 12:ed4d9b8d1410:
/** ****************************************************************************** * @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 to write/read NDef messages to/from a nfc tag */ class NDefNfcTag { public: NDefNfcTag():mSessionIsOpen(false){} /** * Open the communication session with the nfc tag. * @par This method should be called at the end of an overriding implementation, just before returning. * @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 This method should be called at the end of an overriding implementation, just before returning. * @return true if success */ virtual bool closeSession(){ mSessionIsOpen=false; return true; } /** * Write a message in the nfc tag. * @par 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 to allocate the buffer 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 the read records are added to. * @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 contents bool retValue = readByte(2, length, buffer); if (retValue) { Message::parseMessage(buffer, length, msg); } delete[] buffer; return retValue; } virtual ~NDefNfcTag() {} /** * Returns true if a communication with the nfc tag is open. * @return true if a communication with the nfc tag is open */ bool isSessionOpen(){ return mSessionIsOpen; } protected: /** * Write a sequence of bytes to the NDEF file. * @param buffer Buffer to write. * @param length Number of bytes to write. * @param offset Write offset in bytes. * @return true if success */ virtual bool writeByte(const uint8_t *buffer, uint16_t length, uint16_t offset=0)=0; /** * Read a sequence of bytes from the NDEF file. * @param byteOffset Read offsetin bytes. * @param byteLength Number of bytes to read. * @param[out] buffer Buffer to store the read data into. * @return true if success */ virtual bool readByte(const uint16_t byteOffset, const uint16_t byteLength, uint8_t *buffer)=0; private: /** * Provides the status of 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_ */