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 Aug 21 12:02:31 2017 +0000
Revision:
21:72c86cbd49be
Parent:
20:31f727872290
change on_message_write callback; the message parameter was unused and it pointed to an invalid object

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 19:13d84b136a62 5 * @version V2.0.0
giovannivisentini 19:13d84b136a62 6 * @date 28 Apr 2017
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 19:13d84b136a62 48 uint16_t Message::get_byte_length() const {
giovannivisentini 4:eaf6c49a86e4 49 uint16_t lenght = 2; //length size
giovannivisentini 0:04b82ae7aa43 50
Davidroid 20:31f727872290 51 if (mRecords.size() == 0) {
giovannivisentini 19:13d84b136a62 52 return lenght + EmptyRecord().get_byte_length();
Davidroid 20:31f727872290 53 }
giovannivisentini 0:04b82ae7aa43 54
giovannivisentini 0:04b82ae7aa43 55 std::vector<Record*>::const_iterator it = mRecords.begin();
giovannivisentini 0:04b82ae7aa43 56 const std::vector<Record*>::const_iterator end = mRecords.end();
giovannivisentini 0:04b82ae7aa43 57
giovannivisentini 4:eaf6c49a86e4 58 for (; it != end; ++it) {
giovannivisentini 19:13d84b136a62 59 lenght += (*it)->get_byte_length();
giovannivisentini 4:eaf6c49a86e4 60 } //for
giovannivisentini 0:04b82ae7aa43 61
giovannivisentini 0:04b82ae7aa43 62 return lenght;
giovannivisentini 4:eaf6c49a86e4 63 } //getByteLenght
giovannivisentini 0:04b82ae7aa43 64
giovannivisentini 1:a0eeb478a45a 65 uint16_t Message::write(uint8_t *buffer) const {
giovannivisentini 0:04b82ae7aa43 66
giovannivisentini 19:13d84b136a62 67 const uint16_t length = get_byte_length() - 2;
giovannivisentini 4:eaf6c49a86e4 68 uint16_t offset = 0;
giovannivisentini 4:eaf6c49a86e4 69 buffer[offset++] = (uint8_t) ((length & 0xFF00) >> 8);
giovannivisentini 4:eaf6c49a86e4 70 buffer[offset++] = (uint8_t) ((length & 0x00FF));
giovannivisentini 0:04b82ae7aa43 71
giovannivisentini 0:04b82ae7aa43 72 const uint32_t nRecord = mRecords.size();
giovannivisentini 0:04b82ae7aa43 73
giovannivisentini 4:eaf6c49a86e4 74 if (mRecords.size() == 0) {
giovannivisentini 4:eaf6c49a86e4 75 offset += EmptyRecord().write(buffer + offset);
giovannivisentini 0:04b82ae7aa43 76 return offset;
giovannivisentini 4:eaf6c49a86e4 77 } //else
giovannivisentini 0:04b82ae7aa43 78
giovannivisentini 4:eaf6c49a86e4 79 for (uint32_t i = 0; i < nRecord; i++) {
giovannivisentini 0:04b82ae7aa43 80 Record *r = mRecords[i];
giovannivisentini 0:04b82ae7aa43 81
giovannivisentini 19:13d84b136a62 82 r->set_as_middle_record();
Davidroid 20:31f727872290 83 if (i == 0) {
giovannivisentini 19:13d84b136a62 84 r->set_as_first_record();
Davidroid 20:31f727872290 85 }
Davidroid 20:31f727872290 86 if (i == nRecord - 1) {
giovannivisentini 19:13d84b136a62 87 r->set_as_last_record();
Davidroid 20:31f727872290 88 }
giovannivisentini 0:04b82ae7aa43 89
giovannivisentini 4:eaf6c49a86e4 90 offset += r->write(buffer + offset);
giovannivisentini 4:eaf6c49a86e4 91 } //for
giovannivisentini 0:04b82ae7aa43 92
giovannivisentini 0:04b82ae7aa43 93 return offset;
giovannivisentini 4:eaf6c49a86e4 94 } //write
giovannivisentini 0:04b82ae7aa43 95
Davidroid 20:31f727872290 96 void Message::parse_message(const uint8_t * const rawNdefFile, const uint16_t length, Message *msg) {
giovannivisentini 4:eaf6c49a86e4 97 uint16_t offset = 0;
giovannivisentini 1:a0eeb478a45a 98 Record *r;
giovannivisentini 0:04b82ae7aa43 99
giovannivisentini 4:eaf6c49a86e4 100 RecordHeader header;
giovannivisentini 4:eaf6c49a86e4 101 do {
giovannivisentini 19:13d84b136a62 102 const uint8_t headerLenght = header.load_header(rawNdefFile + offset);
giovannivisentini 4:eaf6c49a86e4 103 r = RecordText::parse(header, rawNdefFile + offset + headerLenght);
Davidroid 20:31f727872290 104 if (r == NULL) {
giovannivisentini 4:eaf6c49a86e4 105 r = RecordAAR::parse(header, rawNdefFile + offset + headerLenght);
Davidroid 20:31f727872290 106 }
Davidroid 20:31f727872290 107 if (r == NULL) {
giovannivisentini 4:eaf6c49a86e4 108 r = RecordMimeType::parse(header,
Davidroid 20:31f727872290 109 rawNdefFile + offset + headerLenght);
Davidroid 20:31f727872290 110 }
Davidroid 20:31f727872290 111 if (r == NULL) {
giovannivisentini 4:eaf6c49a86e4 112 r = RecordURI::parse(header, rawNdefFile + offset + headerLenght);
Davidroid 20:31f727872290 113 }
giovannivisentini 0:04b82ae7aa43 114
giovannivisentini 19:13d84b136a62 115 offset += header.get_record_length();
giovannivisentini 19:13d84b136a62 116 msg->add_record(r);
giovannivisentini 4:eaf6c49a86e4 117 } while (offset < length);
giovannivisentini 0:04b82ae7aa43 118 }
giovannivisentini 0:04b82ae7aa43 119
giovannivisentini 19:13d84b136a62 120 void Message::remove_and_delete_all_record(Message &msg){
giovannivisentini 19:13d84b136a62 121 const uint32_t nRecords =msg.get_N_records();
Davidroid 20:31f727872290 122 for (uint32_t i =0 ;i<nRecords ;i++) {
giovannivisentini 17:46899fa3d9f2 123 NDefLib::Record *r = msg[i];
giovannivisentini 17:46899fa3d9f2 124 delete r;
giovannivisentini 17:46899fa3d9f2 125 }//for
giovannivisentini 17:46899fa3d9f2 126 msg.mRecords.clear();
giovannivisentini 17:46899fa3d9f2 127 }//removeAndDeleteAllRecord
giovannivisentini 17:46899fa3d9f2 128
Davidroid 20:31f727872290 129 } /* namespace NDefLib */
giovannivisentini 17:46899fa3d9f2 130
Davidroid 20:31f727872290 131
Davidroid 20:31f727872290 132 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/