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@12:ed4d9b8d1410, 2016-01-14 (annotated)
- Committer:
- giovannivisentini
- Date:
- Thu Jan 14 07:54:44 2016 +0000
- Revision:
- 12:ed4d9b8d1410
- Parent:
- 11:eaf42739791e
- Child:
- 14:ba0c186ae6d6
Update documentation 3
Who changed what in which revision?
User | Revision | Line number | New 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>© 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 | 12:ed4d9b8d1410 | 46 | * Abstract class used to write/read NDef messages to/from a 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 | 12:ed4d9b8d1410 | 54 | * Open the communication session with the nfc tag. |
giovannivisentini | 12:ed4d9b8d1410 | 55 | * @par This method should be called at the end of an overriding implementation, just before returning. |
giovannivisentini | 12:ed4d9b8d1410 | 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 | 12:ed4d9b8d1410 | 65 | * Close the communication with the nfc tag. |
giovannivisentini | 12:ed4d9b8d1410 | 66 | * @par This method should be called at the end of an overriding implementation, just before returning. |
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 | 12:ed4d9b8d1410 | 75 | * Write a message in the nfc tag. |
giovannivisentini | 12:ed4d9b8d1410 | 76 | * @par This call will delete the previous message. |
giovannivisentini | 12:ed4d9b8d1410 | 77 | * @param msg Message to write. |
giovannivisentini | 8:473f6e0b03df | 78 | * @return true if success |
giovannivisentini | 8:473f6e0b03df | 79 | */ |
giovannivisentini | 8:473f6e0b03df | 80 | bool write(const Message &msg) { |
giovannivisentini | 8:473f6e0b03df | 81 | if(!mSessionIsOpen) |
giovannivisentini | 8:473f6e0b03df | 82 | return false; |
giovannivisentini | 8:473f6e0b03df | 83 | |
giovannivisentini | 8:473f6e0b03df | 84 | const uint16_t length = msg.getByteLength(); |
giovannivisentini | 8:473f6e0b03df | 85 | uint8_t *buffer = new uint8_t[length]; |
giovannivisentini | 12:ed4d9b8d1410 | 86 | if(buffer==NULL) //impossible to allocate the buffer |
giovannivisentini | 8:473f6e0b03df | 87 | return false; |
giovannivisentini | 8:473f6e0b03df | 88 | msg.write(buffer); |
giovannivisentini | 8:473f6e0b03df | 89 | bool retValue = writeByte(buffer, length); |
giovannivisentini | 8:473f6e0b03df | 90 | delete[] buffer; |
giovannivisentini | 8:473f6e0b03df | 91 | return retValue; |
giovannivisentini | 8:473f6e0b03df | 92 | } |
giovannivisentini | 8:473f6e0b03df | 93 | |
giovannivisentini | 8:473f6e0b03df | 94 | /** |
giovannivisentini | 12:ed4d9b8d1410 | 95 | * Read a message from the tag. |
giovannivisentini | 12:ed4d9b8d1410 | 96 | * @param[in,out] msg Message object the read records are added to. |
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 | 12:ed4d9b8d1410 | 111 | //read all the message contents |
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 | 12:ed4d9b8d1410 | 123 | * Returns true if a communication with the nfc tag is open. |
giovannivisentini | 12:ed4d9b8d1410 | 124 | * @return true if a communication with the nfc tag is open |
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 | 12:ed4d9b8d1410 | 134 | * Write a sequence of bytes to the NDEF file. |
giovannivisentini | 12:ed4d9b8d1410 | 135 | * @param buffer Buffer to write. |
giovannivisentini | 12:ed4d9b8d1410 | 136 | * @param length Number of bytes to write. |
giovannivisentini | 12:ed4d9b8d1410 | 137 | * @param offset Write offset in bytes. |
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 | 12:ed4d9b8d1410 | 143 | * Read a sequence of bytes from the NDEF file. |
giovannivisentini | 12:ed4d9b8d1410 | 144 | * @param byteOffset Read offsetin bytes. |
giovannivisentini | 12:ed4d9b8d1410 | 145 | * @param byteLength Number of bytes to read. |
giovannivisentini | 12:ed4d9b8d1410 | 146 | * @param[out] buffer Buffer to store the read data into. |
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 | 12:ed4d9b8d1410 | 153 | |
giovannivisentini | 8:473f6e0b03df | 154 | /** |
giovannivisentini | 12:ed4d9b8d1410 | 155 | * Provides the status of a communication channel with the tag. |
giovannivisentini | 8:473f6e0b03df | 156 | */ |
giovannivisentini | 8:473f6e0b03df | 157 | bool mSessionIsOpen; |
giovannivisentini | 8:473f6e0b03df | 158 | |
giovannivisentini | 8:473f6e0b03df | 159 | /** |
giovannivisentini | 12:ed4d9b8d1410 | 160 | * Read the NDEF message length. |
giovannivisentini | 8:473f6e0b03df | 161 | * @return NDEF message length |
giovannivisentini | 8:473f6e0b03df | 162 | */ |
giovannivisentini | 8:473f6e0b03df | 163 | uint16_t getMessageLength() { |
giovannivisentini | 8:473f6e0b03df | 164 | uint8_t lenghtByte[2]; |
giovannivisentini | 8:473f6e0b03df | 165 | if (readByte(0, 2, lenghtByte)) |
giovannivisentini | 8:473f6e0b03df | 166 | return (((uint16_t) lenghtByte[0]) << 8 | lenghtByte[1]); |
giovannivisentini | 8:473f6e0b03df | 167 | return 0; |
giovannivisentini | 8:473f6e0b03df | 168 | } //getMessageLength |
giovannivisentini | 8:473f6e0b03df | 169 | |
giovannivisentini | 8:473f6e0b03df | 170 | }; |
giovannivisentini | 8:473f6e0b03df | 171 | |
giovannivisentini | 8:473f6e0b03df | 172 | } /* namespace NDefLib */ |
giovannivisentini | 8:473f6e0b03df | 173 | |
giovannivisentini | 8:473f6e0b03df | 174 | #endif /* NDefNFCTAG_H_ */ |