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:
Wed Aug 31 08:32:13 2016 +0000
Revision:
17:46899fa3d9f2
Parent:
12:ed4d9b8d1410
Child:
18:cf1dd5c931c2
add function for remove/delete Record from a Message

Who changed what in which revision?

UserRevisionLine numberNew contents of line
giovannivisentini 6:739e3211749d 1 /**
giovannivisentini 6:739e3211749d 2 ******************************************************************************
giovannivisentini 6:739e3211749d 3 * @file Message.cpp
giovannivisentini 8:473f6e0b03df 4 * @author ST / Central Labs
giovannivisentini 6:739e3211749d 5 * @version V1.0.0
giovannivisentini 6:739e3211749d 6 * @date 6 Nov 2015
giovannivisentini 6:739e3211749d 7 * @brief NDef Message class implementation
giovannivisentini 6:739e3211749d 8 ******************************************************************************
giovannivisentini 6:739e3211749d 9 * @attention
giovannivisentini 6:739e3211749d 10 *
giovannivisentini 6:739e3211749d 11 * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
giovannivisentini 0:04b82ae7aa43 12 *
giovannivisentini 6:739e3211749d 13 * Redistribution and use in source and binary forms, with or without modification,
giovannivisentini 6:739e3211749d 14 * are permitted provided that the following conditions are met:
giovannivisentini 6:739e3211749d 15 * 1. Redistributions of source code must retain the above copyright notice,
giovannivisentini 6:739e3211749d 16 * this list of conditions and the following disclaimer.
giovannivisentini 6:739e3211749d 17 * 2. Redistributions in binary form must reproduce the above copyright notice,
giovannivisentini 6:739e3211749d 18 * this list of conditions and the following disclaimer in the documentation
giovannivisentini 6:739e3211749d 19 * and/or other materials provided with the distribution.
giovannivisentini 6:739e3211749d 20 * 3. Neither the name of STMicroelectronics nor the names of its contributors
giovannivisentini 6:739e3211749d 21 * may be used to endorse or promote products derived from this software
giovannivisentini 6:739e3211749d 22 * without specific prior written permission.
giovannivisentini 6:739e3211749d 23 *
giovannivisentini 6:739e3211749d 24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
giovannivisentini 6:739e3211749d 25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
giovannivisentini 6:739e3211749d 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
giovannivisentini 6:739e3211749d 27 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
giovannivisentini 6:739e3211749d 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
giovannivisentini 6:739e3211749d 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
giovannivisentini 6:739e3211749d 30 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
giovannivisentini 6:739e3211749d 31 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
giovannivisentini 6:739e3211749d 32 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
giovannivisentini 6:739e3211749d 33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
giovannivisentini 6:739e3211749d 34 *
giovannivisentini 6:739e3211749d 35 ******************************************************************************
giovannivisentini 0:04b82ae7aa43 36 */
giovannivisentini 0:04b82ae7aa43 37
giovannivisentini 0:04b82ae7aa43 38 #include <cstdlib>
giovannivisentini 0:04b82ae7aa43 39 #include "NDefLib/Message.h"
giovannivisentini 0:04b82ae7aa43 40 #include "RecordType/EmptyRecord.h"
giovannivisentini 0:04b82ae7aa43 41 #include "RecordType/RecordText.h"
giovannivisentini 1:a0eeb478a45a 42 #include "RecordType/RecordAAR.h"
giovannivisentini 1:a0eeb478a45a 43 #include "RecordType/RecordMimeType.h"
giovannivisentini 1:a0eeb478a45a 44 #include "RecordType/RecordURI.h"
giovannivisentini 0:04b82ae7aa43 45
giovannivisentini 0:04b82ae7aa43 46 namespace NDefLib {
giovannivisentini 0:04b82ae7aa43 47
giovannivisentini 4:eaf6c49a86e4 48 uint16_t Message::getByteLength() const {
giovannivisentini 4:eaf6c49a86e4 49 uint16_t lenght = 2; //length size
giovannivisentini 0:04b82ae7aa43 50
giovannivisentini 4:eaf6c49a86e4 51 if (mRecords.size() == 0)
giovannivisentini 4:eaf6c49a86e4 52 return lenght + EmptyRecord().getByteLength();
giovannivisentini 0:04b82ae7aa43 53
giovannivisentini 0:04b82ae7aa43 54 std::vector<Record*>::const_iterator it = mRecords.begin();
giovannivisentini 0:04b82ae7aa43 55 const std::vector<Record*>::const_iterator end = mRecords.end();
giovannivisentini 0:04b82ae7aa43 56
giovannivisentini 4:eaf6c49a86e4 57 for (; it != end; ++it) {
giovannivisentini 4:eaf6c49a86e4 58 lenght += (*it)->getByteLength();
giovannivisentini 4:eaf6c49a86e4 59 } //for
giovannivisentini 0:04b82ae7aa43 60
giovannivisentini 0:04b82ae7aa43 61 return lenght;
giovannivisentini 4:eaf6c49a86e4 62 } //getByteLenght
giovannivisentini 0:04b82ae7aa43 63
giovannivisentini 1:a0eeb478a45a 64 uint16_t Message::write(uint8_t *buffer) const {
giovannivisentini 0:04b82ae7aa43 65
giovannivisentini 4:eaf6c49a86e4 66 const uint16_t length = getByteLength() - 2;
giovannivisentini 4:eaf6c49a86e4 67 uint16_t offset = 0;
giovannivisentini 4:eaf6c49a86e4 68 buffer[offset++] = (uint8_t) ((length & 0xFF00) >> 8);
giovannivisentini 4:eaf6c49a86e4 69 buffer[offset++] = (uint8_t) ((length & 0x00FF));
giovannivisentini 0:04b82ae7aa43 70
giovannivisentini 0:04b82ae7aa43 71 const uint32_t nRecord = mRecords.size();
giovannivisentini 0:04b82ae7aa43 72
giovannivisentini 4:eaf6c49a86e4 73 if (mRecords.size() == 0) {
giovannivisentini 4:eaf6c49a86e4 74 offset += EmptyRecord().write(buffer + offset);
giovannivisentini 0:04b82ae7aa43 75 return offset;
giovannivisentini 4:eaf6c49a86e4 76 } //else
giovannivisentini 0:04b82ae7aa43 77
giovannivisentini 4:eaf6c49a86e4 78 for (uint32_t i = 0; i < nRecord; i++) {
giovannivisentini 0:04b82ae7aa43 79 Record *r = mRecords[i];
giovannivisentini 0:04b82ae7aa43 80
giovannivisentini 0:04b82ae7aa43 81 r->setAsMiddleRecord();
giovannivisentini 4:eaf6c49a86e4 82 if (i == 0)
giovannivisentini 0:04b82ae7aa43 83 r->setAsFirstRecord();
giovannivisentini 4:eaf6c49a86e4 84 if (i == nRecord - 1)
giovannivisentini 0:04b82ae7aa43 85 r->setAsLastRecord();
giovannivisentini 0:04b82ae7aa43 86
giovannivisentini 4:eaf6c49a86e4 87 offset += r->write(buffer + offset);
giovannivisentini 4:eaf6c49a86e4 88 } //for
giovannivisentini 0:04b82ae7aa43 89
giovannivisentini 0:04b82ae7aa43 90 return offset;
giovannivisentini 4:eaf6c49a86e4 91 } //write
giovannivisentini 0:04b82ae7aa43 92
giovannivisentini 4:eaf6c49a86e4 93 void Message::parseMessage(const uint8_t * const rawNdefFile,
giovannivisentini 12:ed4d9b8d1410 94 const uint16_t length, Message *msg) {
giovannivisentini 4:eaf6c49a86e4 95 uint16_t offset = 0;
giovannivisentini 1:a0eeb478a45a 96 Record *r;
giovannivisentini 0:04b82ae7aa43 97
giovannivisentini 4:eaf6c49a86e4 98 RecordHeader header;
giovannivisentini 4:eaf6c49a86e4 99 do {
giovannivisentini 4:eaf6c49a86e4 100 const uint8_t headerLenght = header.loadHeader(rawNdefFile + offset);
giovannivisentini 4:eaf6c49a86e4 101 r = RecordText::parse(header, rawNdefFile + offset + headerLenght);
giovannivisentini 4:eaf6c49a86e4 102 if (r == NULL)
giovannivisentini 4:eaf6c49a86e4 103 r = RecordAAR::parse(header, rawNdefFile + offset + headerLenght);
giovannivisentini 4:eaf6c49a86e4 104 if (r == NULL)
giovannivisentini 4:eaf6c49a86e4 105 r = RecordMimeType::parse(header,
giovannivisentini 4:eaf6c49a86e4 106 rawNdefFile + offset + headerLenght);
giovannivisentini 4:eaf6c49a86e4 107 if (r == NULL)
giovannivisentini 4:eaf6c49a86e4 108 r = RecordURI::parse(header, rawNdefFile + offset + headerLenght);
giovannivisentini 0:04b82ae7aa43 109
giovannivisentini 1:a0eeb478a45a 110 offset += header.getRecordLength();
giovannivisentini 1:a0eeb478a45a 111 msg->addRecord(r);
giovannivisentini 4:eaf6c49a86e4 112 } while (offset < length);
giovannivisentini 0:04b82ae7aa43 113
giovannivisentini 0:04b82ae7aa43 114 }
giovannivisentini 0:04b82ae7aa43 115
giovannivisentini 17:46899fa3d9f2 116 void Message::removeAndDeleteAllRecord(Message &msg){
giovannivisentini 17:46899fa3d9f2 117 const uint32_t nRecords =msg.getNRecords();
giovannivisentini 17:46899fa3d9f2 118 for(int i =0 ;i<nRecords ;i++){
giovannivisentini 17:46899fa3d9f2 119 NDefLib::Record *r = msg[i];
giovannivisentini 17:46899fa3d9f2 120 delete r;
giovannivisentini 17:46899fa3d9f2 121 }//for
giovannivisentini 17:46899fa3d9f2 122 msg.mRecords.clear();
giovannivisentini 17:46899fa3d9f2 123 }//removeAndDeleteAllRecord
giovannivisentini 17:46899fa3d9f2 124
giovannivisentini 17:46899fa3d9f2 125
giovannivisentini 0:04b82ae7aa43 126 } /* namespace NDefLib */