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:
Davidroid
Date:
Wed Jul 12 12:33:42 2017 +0000
Revision:
20:31f727872290
Parent:
19:13d84b136a62
Updated to fit ARM mbed coding style.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
giovannivisentini 6:739e3211749d 1 /**
giovannivisentini 6:739e3211749d 2 ******************************************************************************
giovannivisentini 6:739e3211749d 3 * @file RecordMAIL.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 12:ed4d9b8d1410 7 * @brief RecordMail 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 #include "RecordMail.h"
giovannivisentini 0:04b82ae7aa43 38
giovannivisentini 0:04b82ae7aa43 39 namespace NDefLib {
giovannivisentini 0:04b82ae7aa43 40
giovannivisentini 1:a0eeb478a45a 41 const std::string RecordMail::sSubjectTag("?subject=");
giovannivisentini 1:a0eeb478a45a 42 const std::string RecordMail::sBodyTag("&body=");
giovannivisentini 1:a0eeb478a45a 43
giovannivisentini 19:13d84b136a62 44 void RecordMail::update_content(){
Davidroid 20:31f727872290 45 if (!mContentIsChange) {
giovannivisentini 4:eaf6c49a86e4 46 return;
Davidroid 20:31f727872290 47 }
giovannivisentini 4:eaf6c49a86e4 48
giovannivisentini 4:eaf6c49a86e4 49 mContent.reserve(mToAddress.size() + sSubjectTag.size() + mSubject.size() + sBodyTag.size() + mBody.size());
giovannivisentini 4:eaf6c49a86e4 50 mContent = mToAddress;
giovannivisentini 4:eaf6c49a86e4 51 mContent += sSubjectTag;
giovannivisentini 4:eaf6c49a86e4 52 mContent += mSubject;
giovannivisentini 4:eaf6c49a86e4 53 mContent += sBodyTag;
giovannivisentini 4:eaf6c49a86e4 54 mContent += mBody;
giovannivisentini 4:eaf6c49a86e4 55
giovannivisentini 4:eaf6c49a86e4 56 mContentIsChange=false;
giovannivisentini 4:eaf6c49a86e4 57 }
giovannivisentini 4:eaf6c49a86e4 58
Davidroid 20:31f727872290 59 RecordMail* RecordMail::parse(const RecordHeader &header, const uint8_t* buffer) {
giovannivisentini 1:a0eeb478a45a 60 //not a uri tag or a mail tag
Davidroid 20:31f727872290 61 if (buffer[0] != RecordURI::sNDEFUriIdCode || buffer[1] != RecordURI::MAIL) {
giovannivisentini 1:a0eeb478a45a 62 return NULL;
giovannivisentini 1:a0eeb478a45a 63 }
Davidroid 20:31f727872290 64 const std::string uriContent((const char*) (buffer + 2), header.get_payload_length() - 1);
giovannivisentini 0:04b82ae7aa43 65
giovannivisentini 1:a0eeb478a45a 66 std::size_t subjectStart = uriContent.find(sSubjectTag);
Davidroid 20:31f727872290 67 if (subjectStart == std::string::npos) { //subject not present
giovannivisentini 1:a0eeb478a45a 68 return NULL;
Davidroid 20:31f727872290 69 }
giovannivisentini 1:a0eeb478a45a 70 const std::size_t destSize = subjectStart;
giovannivisentini 4:eaf6c49a86e4 71 subjectStart += sSubjectTag.size(); //skip the tag string
giovannivisentini 1:a0eeb478a45a 72
giovannivisentini 1:a0eeb478a45a 73 std::size_t bodyStart = uriContent.find(sBodyTag);
Davidroid 20:31f727872290 74 if (bodyStart == std::string::npos) { //body not present
giovannivisentini 1:a0eeb478a45a 75 return NULL;
Davidroid 20:31f727872290 76 }
giovannivisentini 4:eaf6c49a86e4 77 const std::size_t subjectLenght = bodyStart - subjectStart;
giovannivisentini 4:eaf6c49a86e4 78 bodyStart += sBodyTag.size(); // skip the tag string
giovannivisentini 1:a0eeb478a45a 79
giovannivisentini 4:eaf6c49a86e4 80 const std::size_t bodyLenght = uriContent.size() - bodyStart;
giovannivisentini 1:a0eeb478a45a 81
giovannivisentini 4:eaf6c49a86e4 82 return new RecordMail(uriContent.substr(0, destSize),
Davidroid 20:31f727872290 83 uriContent.substr(subjectStart, subjectLenght),
Davidroid 20:31f727872290 84 uriContent.substr(bodyStart, bodyLenght));
giovannivisentini 1:a0eeb478a45a 85
giovannivisentini 1:a0eeb478a45a 86 }
giovannivisentini 0:04b82ae7aa43 87
giovannivisentini 0:04b82ae7aa43 88 } /* namespace NDefLib */
Davidroid 20:31f727872290 89
Davidroid 20:31f727872290 90
Davidroid 20:31f727872290 91 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/