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 ST Expansion SW Team

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.

Committer:
giovannivisentini
Date:
Mon Jan 11 13:04:33 2016 +0000
Revision:
11:eaf42739791e
Parent:
9:689c1f56f359
Child:
12:ed4d9b8d1410
update comments 2

Who changed what in which revision?

UserRevisionLine numberNew contents of line
giovannivisentini 8:473f6e0b03df 1 /**
giovannivisentini 8:473f6e0b03df 2 ******************************************************************************
giovannivisentini 8:473f6e0b03df 3 * @file Type4NfcTag.h
giovannivisentini 8:473f6e0b03df 4 * @author ST / Central Labs
giovannivisentini 8:473f6e0b03df 5 * @version V1.0.0
giovannivisentini 8:473f6e0b03df 6 * @date 1 Nov 2015
giovannivisentini 8:473f6e0b03df 7 * @brief Generic interface that a device must implement for use the NDefLib
giovannivisentini 8:473f6e0b03df 8 ******************************************************************************
giovannivisentini 8:473f6e0b03df 9 * @attention
giovannivisentini 8:473f6e0b03df 10 *
giovannivisentini 8:473f6e0b03df 11 * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
giovannivisentini 8:473f6e0b03df 12 *
giovannivisentini 8:473f6e0b03df 13 * Redistribution and use in source and binary forms, with or without modification,
giovannivisentini 8:473f6e0b03df 14 * are permitted provided that the following conditions are met:
giovannivisentini 8:473f6e0b03df 15 * 1. Redistributions of source code must retain the above copyright notice,
giovannivisentini 8:473f6e0b03df 16 * this list of conditions and the following disclaimer.
giovannivisentini 8:473f6e0b03df 17 * 2. Redistributions in binary form must reproduce the above copyright notice,
giovannivisentini 8:473f6e0b03df 18 * this list of conditions and the following disclaimer in the documentation
giovannivisentini 8:473f6e0b03df 19 * and/or other materials provided with the distribution.
giovannivisentini 8:473f6e0b03df 20 * 3. Neither the name of STMicroelectronics nor the names of its contributors
giovannivisentini 8:473f6e0b03df 21 * may be used to endorse or promote products derived from this software
giovannivisentini 8:473f6e0b03df 22 * without specific prior written permission.
giovannivisentini 8:473f6e0b03df 23 *
giovannivisentini 8:473f6e0b03df 24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
giovannivisentini 8:473f6e0b03df 25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
giovannivisentini 8:473f6e0b03df 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
giovannivisentini 8:473f6e0b03df 27 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
giovannivisentini 8:473f6e0b03df 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
giovannivisentini 8:473f6e0b03df 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
giovannivisentini 8:473f6e0b03df 30 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
giovannivisentini 8:473f6e0b03df 31 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
giovannivisentini 8:473f6e0b03df 32 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
giovannivisentini 8:473f6e0b03df 33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
giovannivisentini 8:473f6e0b03df 34 *
giovannivisentini 8:473f6e0b03df 35 ******************************************************************************
giovannivisentini 8:473f6e0b03df 36 */
giovannivisentini 9:689c1f56f359 37
giovannivisentini 8:473f6e0b03df 38 #ifndef NDefNFCTAG_H_
giovannivisentini 8:473f6e0b03df 39 #define NDefNFCTAG_H_
giovannivisentini 8:473f6e0b03df 40
giovannivisentini 8:473f6e0b03df 41 #include "NDefLib/Message.h"
giovannivisentini 8:473f6e0b03df 42
giovannivisentini 8:473f6e0b03df 43 namespace NDefLib {
giovannivisentini 8:473f6e0b03df 44
giovannivisentini 8:473f6e0b03df 45 /**
giovannivisentini 8:473f6e0b03df 46 * Abstract class used for write/read NDef message in an nfc tag
giovannivisentini 8:473f6e0b03df 47 */
giovannivisentini 8:473f6e0b03df 48 class NDefNfcTag {
giovannivisentini 8:473f6e0b03df 49 public:
giovannivisentini 8:473f6e0b03df 50
giovannivisentini 8:473f6e0b03df 51 NDefNfcTag():mSessionIsOpen(false){}
giovannivisentini 8:473f6e0b03df 52
giovannivisentini 8:473f6e0b03df 53 /**
giovannivisentini 8:473f6e0b03df 54 * open the communication with the nfc tag
giovannivisentini 8:473f6e0b03df 55 * \par when override this method call this implementation as last action for set the session opened
giovannivisentini 8:473f6e0b03df 56 * @param force force to open a communication
giovannivisentini 8:473f6e0b03df 57 * @return true if success
giovannivisentini 8:473f6e0b03df 58 */
giovannivisentini 8:473f6e0b03df 59 virtual bool openSession(bool force = false){
giovannivisentini 8:473f6e0b03df 60 mSessionIsOpen=true;
giovannivisentini 8:473f6e0b03df 61 return true;
giovannivisentini 8:473f6e0b03df 62 }
giovannivisentini 8:473f6e0b03df 63
giovannivisentini 8:473f6e0b03df 64 /**
giovannivisentini 8:473f6e0b03df 65 * close the communication with the nfc tag
giovannivisentini 8:473f6e0b03df 66 * \par when override this method call this implementation as last action for set the session closed
giovannivisentini 8:473f6e0b03df 67 * @return true if success
giovannivisentini 8:473f6e0b03df 68 */
giovannivisentini 8:473f6e0b03df 69 virtual bool closeSession(){
giovannivisentini 8:473f6e0b03df 70 mSessionIsOpen=false;
giovannivisentini 8:473f6e0b03df 71 return true;
giovannivisentini 8:473f6e0b03df 72 }
giovannivisentini 8:473f6e0b03df 73
giovannivisentini 8:473f6e0b03df 74 /**
giovannivisentini 8:473f6e0b03df 75 * write a message in the nfc tag, this call will delete the previous message
giovannivisentini 8:473f6e0b03df 76 * @param msg message to write
giovannivisentini 8:473f6e0b03df 77 * @return true if success
giovannivisentini 8:473f6e0b03df 78 */
giovannivisentini 8:473f6e0b03df 79 bool write(const Message &msg) {
giovannivisentini 8:473f6e0b03df 80 if(!mSessionIsOpen)
giovannivisentini 8:473f6e0b03df 81 return false;
giovannivisentini 8:473f6e0b03df 82
giovannivisentini 8:473f6e0b03df 83 const uint16_t length = msg.getByteLength();
giovannivisentini 8:473f6e0b03df 84 uint8_t *buffer = new uint8_t[length];
giovannivisentini 8:473f6e0b03df 85 if(buffer==NULL) //impossible allocate the buffer for write the message
giovannivisentini 8:473f6e0b03df 86 return false;
giovannivisentini 8:473f6e0b03df 87 msg.write(buffer);
giovannivisentini 8:473f6e0b03df 88 bool retValue = writeByte(buffer, length);
giovannivisentini 8:473f6e0b03df 89 delete[] buffer;
giovannivisentini 8:473f6e0b03df 90 return retValue;
giovannivisentini 8:473f6e0b03df 91 }
giovannivisentini 8:473f6e0b03df 92
giovannivisentini 8:473f6e0b03df 93 /**
giovannivisentini 8:473f6e0b03df 94 * read a message from the tag
giovannivisentini 8:473f6e0b03df 95 * @param[in,out] msg message object where we will add the record read by the
giovannivisentini 8:473f6e0b03df 96 * tag
giovannivisentini 8:473f6e0b03df 97 * @return true if success
giovannivisentini 8:473f6e0b03df 98 */
giovannivisentini 8:473f6e0b03df 99 bool read(Message *msg) {
giovannivisentini 8:473f6e0b03df 100 if(!mSessionIsOpen)
giovannivisentini 8:473f6e0b03df 101 return false;
giovannivisentini 8:473f6e0b03df 102
giovannivisentini 8:473f6e0b03df 103 uint16_t length = getMessageLength();
giovannivisentini 8:473f6e0b03df 104 if (length == 0)
giovannivisentini 8:473f6e0b03df 105 return false;
giovannivisentini 8:473f6e0b03df 106 //else
giovannivisentini 8:473f6e0b03df 107 uint8_t *buffer = new uint8_t[length];
giovannivisentini 8:473f6e0b03df 108 if(buffer==NULL)
giovannivisentini 8:473f6e0b03df 109 return false;
giovannivisentini 8:473f6e0b03df 110
giovannivisentini 8:473f6e0b03df 111 //read all the message content
giovannivisentini 8:473f6e0b03df 112 bool retValue = readByte(2, length, buffer);
giovannivisentini 8:473f6e0b03df 113 if (retValue) {
giovannivisentini 8:473f6e0b03df 114 Message::parseMessage(buffer, length, msg);
giovannivisentini 8:473f6e0b03df 115 }
giovannivisentini 8:473f6e0b03df 116 delete[] buffer;
giovannivisentini 8:473f6e0b03df 117 return retValue;
giovannivisentini 8:473f6e0b03df 118 }
giovannivisentini 8:473f6e0b03df 119
giovannivisentini 8:473f6e0b03df 120 virtual ~NDefNfcTag() {}
giovannivisentini 8:473f6e0b03df 121
giovannivisentini 8:473f6e0b03df 122 /**
giovannivisentini 8:473f6e0b03df 123 * true if we have a communication open with the nfc tag
giovannivisentini 8:473f6e0b03df 124 * @return
giovannivisentini 8:473f6e0b03df 125 */
giovannivisentini 8:473f6e0b03df 126 bool isSessionOpen(){
giovannivisentini 8:473f6e0b03df 127 return mSessionIsOpen;
giovannivisentini 8:473f6e0b03df 128 }
giovannivisentini 8:473f6e0b03df 129
giovannivisentini 8:473f6e0b03df 130
giovannivisentini 8:473f6e0b03df 131 protected:
giovannivisentini 8:473f6e0b03df 132
giovannivisentini 8:473f6e0b03df 133 /**
giovannivisentini 8:473f6e0b03df 134 * write a sequence of byte in the NDEF file
giovannivisentini 8:473f6e0b03df 135 * @param buffer buffer to write
giovannivisentini 8:473f6e0b03df 136 * @param length number of byte to write
giovannivisentini 8:473f6e0b03df 137 * @param offset offset where start to write
giovannivisentini 8:473f6e0b03df 138 * @return true if success
giovannivisentini 8:473f6e0b03df 139 */
giovannivisentini 8:473f6e0b03df 140 virtual bool writeByte(const uint8_t *buffer, uint16_t length, uint16_t offset=0)=0;
giovannivisentini 8:473f6e0b03df 141
giovannivisentini 8:473f6e0b03df 142 /**
giovannivisentini 8:473f6e0b03df 143 * read a sequence of byte from the NDEF file
giovannivisentini 8:473f6e0b03df 144 * @param byteOffset offset were start read
giovannivisentini 8:473f6e0b03df 145 * @param byteLength number of byte to read
giovannivisentini 11:eaf42739791e 146 * @param[out] buffer buffer where store the data read
giovannivisentini 8:473f6e0b03df 147 * @return true if success
giovannivisentini 8:473f6e0b03df 148 */
giovannivisentini 8:473f6e0b03df 149 virtual bool readByte(const uint16_t byteOffset, const uint16_t byteLength,
giovannivisentini 8:473f6e0b03df 150 uint8_t *buffer)=0;
giovannivisentini 8:473f6e0b03df 151
giovannivisentini 8:473f6e0b03df 152 private:
giovannivisentini 8:473f6e0b03df 153 /**
giovannivisentini 8:473f6e0b03df 154 * variable to set when we open/close a communication channel with the tag
giovannivisentini 8:473f6e0b03df 155 */
giovannivisentini 8:473f6e0b03df 156 bool mSessionIsOpen;
giovannivisentini 8:473f6e0b03df 157
giovannivisentini 8:473f6e0b03df 158 /**
giovannivisentini 8:473f6e0b03df 159 * read the NDEF message length
giovannivisentini 8:473f6e0b03df 160 * @return NDEF message length
giovannivisentini 8:473f6e0b03df 161 */
giovannivisentini 8:473f6e0b03df 162 uint16_t getMessageLength() {
giovannivisentini 8:473f6e0b03df 163 uint8_t lenghtByte[2];
giovannivisentini 8:473f6e0b03df 164 if (readByte(0, 2, lenghtByte))
giovannivisentini 8:473f6e0b03df 165 return (((uint16_t) lenghtByte[0]) << 8 | lenghtByte[1]);
giovannivisentini 8:473f6e0b03df 166 return 0;
giovannivisentini 8:473f6e0b03df 167 } //getMessageLength
giovannivisentini 8:473f6e0b03df 168
giovannivisentini 8:473f6e0b03df 169 };
giovannivisentini 8:473f6e0b03df 170
giovannivisentini 8:473f6e0b03df 171 } /* namespace NDefLib */
giovannivisentini 8:473f6e0b03df 172
giovannivisentini 8:473f6e0b03df 173 #endif /* NDefNFCTAG_H_ */