ST / NDefLib

Dependents:   NFC M2M_2016_STM32 MyongjiElec_capstone1 IDW01M1_Cloud_IBM ... more

Fork of NDefLib by ST Expansion SW Team

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers RecordText.cpp Source File

RecordText.cpp

Go to the documentation of this file.
00001 /**
00002  ******************************************************************************
00003  * @file    RecordText.cpp
00004  * @author  ST / Central Labs
00005  * @version V2.0.0
00006  * @date    28 Apr 2017
00007  * @brief   RecordText implementation
00008  ******************************************************************************
00009  * @attention
00010  *
00011  * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
00012  *
00013  * Redistribution and use in source and binary forms, with or without modification,
00014  * are permitted provided that the following conditions are met:
00015  *   1. Redistributions of source code must retain the above copyright notice,
00016  *      this list of conditions and the following disclaimer.
00017  *   2. Redistributions in binary form must reproduce the above copyright notice,
00018  *      this list of conditions and the following disclaimer in the documentation
00019  *      and/or other materials provided with the distribution.
00020  *   3. Neither the name of STMicroelectronics nor the names of its contributors
00021  *      may be used to endorse or promote products derived from this software
00022  *      without specific prior written permission.
00023  *
00024  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00025  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00026  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00027  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00028  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00029  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00030  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00031  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00032  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00033  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00034  *
00035  ******************************************************************************
00036  */
00037 
00038 #include <cstring>
00039 #include <NDefLib/RecordType/RecordText.h>
00040 
00041 namespace NDefLib {
00042 
00043 const uint8_t RecordText::NDEFTextIdCode = 'T';
00044 
00045 void RecordText::set_record_header() {
00046     mRecordHeader.set_FNT(RecordHeader::NFC_well_known);
00047     mRecordHeader.set_type_length(sizeof(NDEFTextIdCode));
00048     update_playload_length();
00049 }
00050 
00051 RecordText::RecordText(const std::string &text) :
00052     mEncode(UTF8), mLanguage("en"), mText(text), mTextStatus (get_text_status(mEncode, mLanguage)) {
00053     set_record_header();
00054 }
00055 
00056 RecordText::RecordText(const TextEncoding encoding, const std::string &language,
00057     const std::string &text) :
00058     mEncode(encoding), mLanguage(language), mText(text), mTextStatus(get_text_status(mEncode, mLanguage)) {
00059     set_record_header();
00060 }
00061 
00062 uint16_t RecordText::write(uint8_t *buffer) {
00063     int16_t offset = 0;
00064     offset += mRecordHeader.write_header(buffer);
00065 
00066     buffer[offset++] = NDEFTextIdCode;
00067     buffer[offset++] = mTextStatus;
00068 
00069     std::memcpy(buffer + offset, mLanguage.c_str(), mLanguage.size());
00070     offset += mLanguage.size();
00071     std::memcpy(buffer + offset, mText.c_str(), mText.size());
00072     offset += mText.size();
00073 
00074     return offset;
00075 }
00076 
00077 RecordText* RecordText::parse(const RecordHeader &header, const uint8_t * const buffer) {
00078     uint32_t index = 0;
00079     if (header.get_FNT() == RecordHeader::NFC_well_known && buffer[index++] == NDEFTextIdCode) {
00080         const uint8_t textStatus = buffer[index++];
00081         const TextEncoding enc = get_encoding(textStatus);
00082         const uint8_t langSize = get_language_length(textStatus);
00083         //-1 is the textStatus
00084         const uint8_t textSize = header.get_payload_length() - langSize - 1;
00085 
00086         return new RecordText(enc,
00087             std::string((const char*) (buffer + index), langSize),
00088             std::string((const char*) (buffer + index + langSize),
00089             textSize));
00090     } else {
00091         return NULL;
00092     }
00093 }
00094 
00095 } /* namespace NDefLib */
00096 
00097 
00098 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/