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 RecordURI.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 RecordURI 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 1:a0eeb478a45a 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 1:a0eeb478a45a 36 */
giovannivisentini 1:a0eeb478a45a 37
giovannivisentini 1:a0eeb478a45a 38 #include <cstring>
giovannivisentini 1:a0eeb478a45a 39
giovannivisentini 1:a0eeb478a45a 40 #include "RecordURI.h"
giovannivisentini 1:a0eeb478a45a 41 #include "RecordMail.h"
giovannivisentini 1:a0eeb478a45a 42 #include "RecordSMS.h"
giovannivisentini 1:a0eeb478a45a 43 #include "RecordGeo.h"
giovannivisentini 1:a0eeb478a45a 44
giovannivisentini 1:a0eeb478a45a 45 namespace NDefLib {
giovannivisentini 1:a0eeb478a45a 46
giovannivisentini 1:a0eeb478a45a 47 const uint8_t RecordURI::sNDEFUriIdCode = 'U';
giovannivisentini 1:a0eeb478a45a 48
giovannivisentini 4:eaf6c49a86e4 49 const std::string RecordURI::sKnowUriPrefix[] = { "", "http://www.",
Davidroid 20:31f727872290 50 "https://www.", "http://", "https://", "tel:", "mailto:",
Davidroid 20:31f727872290 51 "ftp://anonymous:anonymous@", "ftp://ftp.", "ftps://", "sftp://",
Davidroid 20:31f727872290 52 "smb://", "nfs://", "ftp://", "dav://", "news:", "telnet://", "imap:",
Davidroid 20:31f727872290 53 "rtsp://", "urn:", "pop:", "sip:", "sips:", "tftp:", "btspp://",
Davidroid 20:31f727872290 54 "btl2cap://", "btgoep://", "tcpobex://", "irdaobex://", "file://",
Davidroid 20:31f727872290 55 "urn:epc:id:", "urn:epc:tag", "urn:epc:pat:", "urn:epc:raw:",
Davidroid 20:31f727872290 56 "urn:epc:", "urn:nfc:" };
giovannivisentini 1:a0eeb478a45a 57
giovannivisentini 19:13d84b136a62 58 void RecordURI::set_record_header() {
giovannivisentini 19:13d84b136a62 59 mRecordHeader.set_FNT(RecordHeader::NFC_well_known);
giovannivisentini 19:13d84b136a62 60 mRecordHeader.set_type_length(sizeof(sNDEFUriIdCode));
giovannivisentini 1:a0eeb478a45a 61 }
giovannivisentini 1:a0eeb478a45a 62
giovannivisentini 4:eaf6c49a86e4 63 RecordURI::RecordURI(knowUriId_t uriId, const std::string &uriContent) :
Davidroid 20:31f727872290 64 mUriTypeId(uriId), mTypeString("") {
giovannivisentini 19:13d84b136a62 65 set_content(uriContent);
giovannivisentini 19:13d84b136a62 66 set_record_header();
giovannivisentini 1:a0eeb478a45a 67 }
giovannivisentini 1:a0eeb478a45a 68
giovannivisentini 4:eaf6c49a86e4 69 RecordURI::RecordURI(const std::string &uriType, const std::string &uriContent) :
Davidroid 20:31f727872290 70 mContent(uriContent),mUriTypeId(UNKNOWN), mTypeString(uriType) {
giovannivisentini 19:13d84b136a62 71 update_record_header();
giovannivisentini 19:13d84b136a62 72 set_record_header();
giovannivisentini 4:eaf6c49a86e4 73 }
giovannivisentini 4:eaf6c49a86e4 74
giovannivisentini 4:eaf6c49a86e4 75 uint16_t RecordURI::write(uint8_t *buffer) {
giovannivisentini 4:eaf6c49a86e4 76 uint16_t offset = 0;
giovannivisentini 19:13d84b136a62 77 update_content();
giovannivisentini 4:eaf6c49a86e4 78
giovannivisentini 19:13d84b136a62 79 offset += mRecordHeader.write_header(buffer);
giovannivisentini 1:a0eeb478a45a 80
giovannivisentini 4:eaf6c49a86e4 81 buffer[offset++] = sNDEFUriIdCode;
giovannivisentini 4:eaf6c49a86e4 82 buffer[offset++] = (uint8_t) mUriTypeId;
giovannivisentini 1:a0eeb478a45a 83
giovannivisentini 4:eaf6c49a86e4 84 if (mUriTypeId == UNKNOWN) {
giovannivisentini 4:eaf6c49a86e4 85 std::memcpy(buffer + offset, mTypeString.c_str(), mTypeString.size());
giovannivisentini 4:eaf6c49a86e4 86 offset += mTypeString.size();
Davidroid 20:31f727872290 87 }
giovannivisentini 1:a0eeb478a45a 88
giovannivisentini 4:eaf6c49a86e4 89 std::memcpy(buffer + offset, mContent.c_str(), mContent.size());
giovannivisentini 4:eaf6c49a86e4 90 offset += mContent.size();
giovannivisentini 1:a0eeb478a45a 91 return offset;
giovannivisentini 1:a0eeb478a45a 92 }
giovannivisentini 1:a0eeb478a45a 93
giovannivisentini 4:eaf6c49a86e4 94 RecordURI* RecordURI::parse(const RecordHeader &header,
Davidroid 20:31f727872290 95 const uint8_t *buffer) {
giovannivisentini 4:eaf6c49a86e4 96 uint16_t offset = 0;
giovannivisentini 1:a0eeb478a45a 97
Davidroid 20:31f727872290 98 if (buffer[offset++] != sNDEFUriIdCode) {
giovannivisentini 1:a0eeb478a45a 99 return NULL;
Davidroid 20:31f727872290 100 }
giovannivisentini 4:eaf6c49a86e4 101 knowUriId_t uriType = (knowUriId_t) buffer[offset++];
giovannivisentini 1:a0eeb478a45a 102 //it is a standard type handle by a specific class
giovannivisentini 4:eaf6c49a86e4 103 if (uriType == MAIL) {
giovannivisentini 4:eaf6c49a86e4 104 return RecordMail::parse(header, buffer);
Davidroid 20:31f727872290 105 }
giovannivisentini 1:a0eeb478a45a 106
giovannivisentini 1:a0eeb478a45a 107 //is an standard type without a specific class
giovannivisentini 4:eaf6c49a86e4 108 if (uriType != UNKNOWN) {
Davidroid 20:31f727872290 109 return new RecordURI(uriType, std::string((const char*) buffer + offset, header.get_payload_length() - 1));
Davidroid 20:31f727872290 110 }
giovannivisentini 1:a0eeb478a45a 111
giovannivisentini 1:a0eeb478a45a 112 //is an unknown type with a specific class
giovannivisentini 4:eaf6c49a86e4 113 RecordURI *r = RecordSMS::parse(header, buffer);
Davidroid 20:31f727872290 114 if (r != NULL) {
giovannivisentini 1:a0eeb478a45a 115 return r;
Davidroid 20:31f727872290 116 }
Davidroid 20:31f727872290 117 r = RecordGeo::parse(header, buffer);
Davidroid 20:31f727872290 118 if (r != NULL) {
Davidroid 20:31f727872290 119 return r;
Davidroid 20:31f727872290 120 } //else is an unknown type without a specific class
Davidroid 20:31f727872290 121
giovannivisentini 4:eaf6c49a86e4 122 return new RecordURI(uriType,
Davidroid 20:31f727872290 123 std::string((const char*) buffer + offset, header.get_payload_length() - 1));
giovannivisentini 1:a0eeb478a45a 124 }
giovannivisentini 1:a0eeb478a45a 125
giovannivisentini 1:a0eeb478a45a 126 } /* namespace NDefLib */
Davidroid 20:31f727872290 127
Davidroid 20:31f727872290 128
Davidroid 20:31f727872290 129 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/