CUSTOMIZED FOR WATER MONITOR
RecordText.cpp@0:51d7bdf88981, 2017-12-07 (annotated)
- Committer:
- DuyLionTran
- Date:
- Thu Dec 07 17:07:22 2017 +0000
- Revision:
- 0:51d7bdf88981
CUSTOMIZED FOR WATER MONITOR
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
DuyLionTran | 0:51d7bdf88981 | 1 | /** |
DuyLionTran | 0:51d7bdf88981 | 2 | ****************************************************************************** |
DuyLionTran | 0:51d7bdf88981 | 3 | * @file RecordText.cpp |
DuyLionTran | 0:51d7bdf88981 | 4 | * @author ST / Central Labs |
DuyLionTran | 0:51d7bdf88981 | 5 | * @version V2.0.0 |
DuyLionTran | 0:51d7bdf88981 | 6 | * @date 28 Apr 2017 |
DuyLionTran | 0:51d7bdf88981 | 7 | * @brief RecordText implementation |
DuyLionTran | 0:51d7bdf88981 | 8 | ****************************************************************************** |
DuyLionTran | 0:51d7bdf88981 | 9 | * @attention |
DuyLionTran | 0:51d7bdf88981 | 10 | * |
DuyLionTran | 0:51d7bdf88981 | 11 | * <h2><center>© COPYRIGHT(c) 2015 STMicroelectronics</center></h2> |
DuyLionTran | 0:51d7bdf88981 | 12 | * |
DuyLionTran | 0:51d7bdf88981 | 13 | * Redistribution and use in source and binary forms, with or without modification, |
DuyLionTran | 0:51d7bdf88981 | 14 | * are permitted provided that the following conditions are met: |
DuyLionTran | 0:51d7bdf88981 | 15 | * 1. Redistributions of source code must retain the above copyright notice, |
DuyLionTran | 0:51d7bdf88981 | 16 | * this list of conditions and the following disclaimer. |
DuyLionTran | 0:51d7bdf88981 | 17 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
DuyLionTran | 0:51d7bdf88981 | 18 | * this list of conditions and the following disclaimer in the documentation |
DuyLionTran | 0:51d7bdf88981 | 19 | * and/or other materials provided with the distribution. |
DuyLionTran | 0:51d7bdf88981 | 20 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
DuyLionTran | 0:51d7bdf88981 | 21 | * may be used to endorse or promote products derived from this software |
DuyLionTran | 0:51d7bdf88981 | 22 | * without specific prior written permission. |
DuyLionTran | 0:51d7bdf88981 | 23 | * |
DuyLionTran | 0:51d7bdf88981 | 24 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
DuyLionTran | 0:51d7bdf88981 | 25 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
DuyLionTran | 0:51d7bdf88981 | 26 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
DuyLionTran | 0:51d7bdf88981 | 27 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
DuyLionTran | 0:51d7bdf88981 | 28 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
DuyLionTran | 0:51d7bdf88981 | 29 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
DuyLionTran | 0:51d7bdf88981 | 30 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
DuyLionTran | 0:51d7bdf88981 | 31 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
DuyLionTran | 0:51d7bdf88981 | 32 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
DuyLionTran | 0:51d7bdf88981 | 33 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
DuyLionTran | 0:51d7bdf88981 | 34 | * |
DuyLionTran | 0:51d7bdf88981 | 35 | ****************************************************************************** |
DuyLionTran | 0:51d7bdf88981 | 36 | */ |
DuyLionTran | 0:51d7bdf88981 | 37 | |
DuyLionTran | 0:51d7bdf88981 | 38 | #include <cstring> |
DuyLionTran | 0:51d7bdf88981 | 39 | #include <NDefLib/RecordType/RecordText.h> |
DuyLionTran | 0:51d7bdf88981 | 40 | |
DuyLionTran | 0:51d7bdf88981 | 41 | namespace NDefLib { |
DuyLionTran | 0:51d7bdf88981 | 42 | |
DuyLionTran | 0:51d7bdf88981 | 43 | const uint8_t RecordText::NDEFTextIdCode = 'T'; |
DuyLionTran | 0:51d7bdf88981 | 44 | |
DuyLionTran | 0:51d7bdf88981 | 45 | void RecordText::set_record_header() { |
DuyLionTran | 0:51d7bdf88981 | 46 | mRecordHeader.set_FNT(RecordHeader::NFC_well_known); |
DuyLionTran | 0:51d7bdf88981 | 47 | mRecordHeader.set_type_length(sizeof(NDEFTextIdCode)); |
DuyLionTran | 0:51d7bdf88981 | 48 | update_playload_length(); |
DuyLionTran | 0:51d7bdf88981 | 49 | } |
DuyLionTran | 0:51d7bdf88981 | 50 | |
DuyLionTran | 0:51d7bdf88981 | 51 | RecordText::RecordText(const std::string &text) : |
DuyLionTran | 0:51d7bdf88981 | 52 | mEncode(UTF8), mLanguage("en"), mText(text), mTextStatus (get_text_status(mEncode, mLanguage)) { |
DuyLionTran | 0:51d7bdf88981 | 53 | set_record_header(); |
DuyLionTran | 0:51d7bdf88981 | 54 | } |
DuyLionTran | 0:51d7bdf88981 | 55 | |
DuyLionTran | 0:51d7bdf88981 | 56 | RecordText::RecordText(const TextEncoding encoding, const std::string &language, |
DuyLionTran | 0:51d7bdf88981 | 57 | const std::string &text) : |
DuyLionTran | 0:51d7bdf88981 | 58 | mEncode(encoding), mLanguage(language), mText(text), mTextStatus(get_text_status(mEncode, mLanguage)) { |
DuyLionTran | 0:51d7bdf88981 | 59 | set_record_header(); |
DuyLionTran | 0:51d7bdf88981 | 60 | } |
DuyLionTran | 0:51d7bdf88981 | 61 | |
DuyLionTran | 0:51d7bdf88981 | 62 | uint16_t RecordText::write(uint8_t *buffer) { |
DuyLionTran | 0:51d7bdf88981 | 63 | int16_t offset = 0; |
DuyLionTran | 0:51d7bdf88981 | 64 | offset += mRecordHeader.write_header(buffer); |
DuyLionTran | 0:51d7bdf88981 | 65 | |
DuyLionTran | 0:51d7bdf88981 | 66 | buffer[offset++] = NDEFTextIdCode; |
DuyLionTran | 0:51d7bdf88981 | 67 | buffer[offset++] = mTextStatus; |
DuyLionTran | 0:51d7bdf88981 | 68 | |
DuyLionTran | 0:51d7bdf88981 | 69 | std::memcpy(buffer + offset, mLanguage.c_str(), mLanguage.size()); |
DuyLionTran | 0:51d7bdf88981 | 70 | offset += mLanguage.size(); |
DuyLionTran | 0:51d7bdf88981 | 71 | std::memcpy(buffer + offset, mText.c_str(), mText.size()); |
DuyLionTran | 0:51d7bdf88981 | 72 | offset += mText.size(); |
DuyLionTran | 0:51d7bdf88981 | 73 | |
DuyLionTran | 0:51d7bdf88981 | 74 | return offset; |
DuyLionTran | 0:51d7bdf88981 | 75 | } |
DuyLionTran | 0:51d7bdf88981 | 76 | |
DuyLionTran | 0:51d7bdf88981 | 77 | RecordText* RecordText::parse(const RecordHeader &header, const uint8_t * const buffer) { |
DuyLionTran | 0:51d7bdf88981 | 78 | uint32_t index = 0; |
DuyLionTran | 0:51d7bdf88981 | 79 | if (header.get_FNT() == RecordHeader::NFC_well_known && buffer[index++] == NDEFTextIdCode) { |
DuyLionTran | 0:51d7bdf88981 | 80 | const uint8_t textStatus = buffer[index++]; |
DuyLionTran | 0:51d7bdf88981 | 81 | const TextEncoding enc = get_encoding(textStatus); |
DuyLionTran | 0:51d7bdf88981 | 82 | const uint8_t langSize = get_language_length(textStatus); |
DuyLionTran | 0:51d7bdf88981 | 83 | //-1 is the textStatus |
DuyLionTran | 0:51d7bdf88981 | 84 | const uint8_t textSize = header.get_payload_length() - langSize - 1; |
DuyLionTran | 0:51d7bdf88981 | 85 | |
DuyLionTran | 0:51d7bdf88981 | 86 | return new RecordText(enc, |
DuyLionTran | 0:51d7bdf88981 | 87 | std::string((const char*) (buffer + index), langSize), |
DuyLionTran | 0:51d7bdf88981 | 88 | std::string((const char*) (buffer + index + langSize), |
DuyLionTran | 0:51d7bdf88981 | 89 | textSize)); |
DuyLionTran | 0:51d7bdf88981 | 90 | } else { |
DuyLionTran | 0:51d7bdf88981 | 91 | return NULL; |
DuyLionTran | 0:51d7bdf88981 | 92 | } |
DuyLionTran | 0:51d7bdf88981 | 93 | } |
DuyLionTran | 0:51d7bdf88981 | 94 | |
DuyLionTran | 0:51d7bdf88981 | 95 | } /* namespace NDefLib */ |
DuyLionTran | 0:51d7bdf88981 | 96 | |
DuyLionTran | 0:51d7bdf88981 | 97 | |
DuyLionTran | 0:51d7bdf88981 | 98 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ |