CUSTOMIZED FOR WATER MONITOR

Committer:
DuyLionTran
Date:
Thu Dec 07 17:07:22 2017 +0000
Revision:
0:51d7bdf88981
CUSTOMIZED FOR WATER MONITOR

Who changed what in which revision?

UserRevisionLine numberNew contents of line
DuyLionTran 0:51d7bdf88981 1 /**
DuyLionTran 0:51d7bdf88981 2 ******************************************************************************
DuyLionTran 0:51d7bdf88981 3 * @file RecordText.h
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 {@link Record} containing a simple text
DuyLionTran 0:51d7bdf88981 8 ******************************************************************************
DuyLionTran 0:51d7bdf88981 9 * @attention
DuyLionTran 0:51d7bdf88981 10 *
DuyLionTran 0:51d7bdf88981 11 * <h2><center>&copy; 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
DuyLionTran 0:51d7bdf88981 39 #ifndef NDEFLIB_RECORDTYPE_RECORDTEXT_H_
DuyLionTran 0:51d7bdf88981 40 #define NDEFLIB_RECORDTYPE_RECORDTEXT_H_
DuyLionTran 0:51d7bdf88981 41 #include <string>
DuyLionTran 0:51d7bdf88981 42 #include <NDefLib/Record.h>
DuyLionTran 0:51d7bdf88981 43
DuyLionTran 0:51d7bdf88981 44 namespace NDefLib {
DuyLionTran 0:51d7bdf88981 45
DuyLionTran 0:51d7bdf88981 46 /**
DuyLionTran 0:51d7bdf88981 47 * {@link Record} containing a simple text.
DuyLionTran 0:51d7bdf88981 48 * @par The text is copied inside the class.
DuyLionTran 0:51d7bdf88981 49 */
DuyLionTran 0:51d7bdf88981 50 class RecordText: public Record {
DuyLionTran 0:51d7bdf88981 51 public:
DuyLionTran 0:51d7bdf88981 52
DuyLionTran 0:51d7bdf88981 53 /**
DuyLionTran 0:51d7bdf88981 54 * Read a recordText from a buffer.
DuyLionTran 0:51d7bdf88981 55 * @param header Record header.
DuyLionTran 0:51d7bdf88981 56 * @param buffer Buffer to read the record content from.
DuyLionTran 0:51d7bdf88981 57 * @return a record of type Text or NULL if it was not possible build this type of record
DuyLionTran 0:51d7bdf88981 58 * @par User is in charge of freeing the pointer returned by this function.
DuyLionTran 0:51d7bdf88981 59 */
DuyLionTran 0:51d7bdf88981 60 static RecordText* parse(const RecordHeader &header,
DuyLionTran 0:51d7bdf88981 61 const uint8_t * const buffer);
DuyLionTran 0:51d7bdf88981 62
DuyLionTran 0:51d7bdf88981 63 /**
DuyLionTran 0:51d7bdf88981 64 * Set the type of encoding used to store the text data
DuyLionTran 0:51d7bdf88981 65 */
DuyLionTran 0:51d7bdf88981 66 typedef enum {
DuyLionTran 0:51d7bdf88981 67 UTF8, //!< UTF8
DuyLionTran 0:51d7bdf88981 68 UTF16,//!< UTF16
DuyLionTran 0:51d7bdf88981 69 } TextEncoding;
DuyLionTran 0:51d7bdf88981 70
DuyLionTran 0:51d7bdf88981 71 /**
DuyLionTran 0:51d7bdf88981 72 * Build a utf8, English text record.
DuyLionTran 0:51d7bdf88981 73 * @param text Text to store in the record.
DuyLionTran 0:51d7bdf88981 74 */
DuyLionTran 0:51d7bdf88981 75 explicit RecordText(const std::string &text="");
DuyLionTran 0:51d7bdf88981 76
DuyLionTran 0:51d7bdf88981 77 /**
DuyLionTran 0:51d7bdf88981 78 * Build a text record.
DuyLionTran 0:51d7bdf88981 79 * @param encoding Rype used to store the message.
DuyLionTran 0:51d7bdf88981 80 * @param language Language used in the text.
DuyLionTran 0:51d7bdf88981 81 * @param Text record text.
DuyLionTran 0:51d7bdf88981 82 */
DuyLionTran 0:51d7bdf88981 83 RecordText(const TextEncoding encoding, const std::string &language,
DuyLionTran 0:51d7bdf88981 84 const std::string &text);
DuyLionTran 0:51d7bdf88981 85
DuyLionTran 0:51d7bdf88981 86 /**
DuyLionTran 0:51d7bdf88981 87 * Get the record type.
DuyLionTran 0:51d7bdf88981 88 * @return TYPE_TEXT
DuyLionTran 0:51d7bdf88981 89 */
DuyLionTran 0:51d7bdf88981 90 virtual RecordType_t get_type() const {
DuyLionTran 0:51d7bdf88981 91 return TYPE_TEXT;
DuyLionTran 0:51d7bdf88981 92 } //getType
DuyLionTran 0:51d7bdf88981 93
DuyLionTran 0:51d7bdf88981 94 /**
DuyLionTran 0:51d7bdf88981 95 * Get the text inside this record.
DuyLionTran 0:51d7bdf88981 96 * @return the text content
DuyLionTran 0:51d7bdf88981 97 */
DuyLionTran 0:51d7bdf88981 98 const std::string& get_text() const {
DuyLionTran 0:51d7bdf88981 99 return mText;
DuyLionTran 0:51d7bdf88981 100 }
DuyLionTran 0:51d7bdf88981 101
DuyLionTran 0:51d7bdf88981 102 /**
DuyLionTran 0:51d7bdf88981 103 * Change the text content.
DuyLionTran 0:51d7bdf88981 104 * @param text New text to store.
DuyLionTran 0:51d7bdf88981 105 */
DuyLionTran 0:51d7bdf88981 106 void set_text (const std::string &text){
DuyLionTran 0:51d7bdf88981 107 mText = text;
DuyLionTran 0:51d7bdf88981 108 update_playload_length();
DuyLionTran 0:51d7bdf88981 109 }
DuyLionTran 0:51d7bdf88981 110
DuyLionTran 0:51d7bdf88981 111 /**
DuyLionTran 0:51d7bdf88981 112 * Get the language used in the text.
DuyLionTran 0:51d7bdf88981 113 * @return Language used in the text.
DuyLionTran 0:51d7bdf88981 114 */
DuyLionTran 0:51d7bdf88981 115 const std::string& get_language()const{
DuyLionTran 0:51d7bdf88981 116 return mLanguage;
DuyLionTran 0:51d7bdf88981 117 }
DuyLionTran 0:51d7bdf88981 118
DuyLionTran 0:51d7bdf88981 119 /**
DuyLionTran 0:51d7bdf88981 120 * Get the encoding used to store the text.
DuyLionTran 0:51d7bdf88981 121 * @return get the encoding used to store the text
DuyLionTran 0:51d7bdf88981 122 */
DuyLionTran 0:51d7bdf88981 123 TextEncoding get_encoding()const{
DuyLionTran 0:51d7bdf88981 124 return mEncode;
DuyLionTran 0:51d7bdf88981 125 }
DuyLionTran 0:51d7bdf88981 126
DuyLionTran 0:51d7bdf88981 127 /**
DuyLionTran 0:51d7bdf88981 128 * Compare 2 objects.
DuyLionTran 0:51d7bdf88981 129 * @return true if the objects have the same encoding,language and text
DuyLionTran 0:51d7bdf88981 130 */
DuyLionTran 0:51d7bdf88981 131 bool operator==(const RecordText &other)const{
DuyLionTran 0:51d7bdf88981 132 return mTextStatus == other.mTextStatus &&
DuyLionTran 0:51d7bdf88981 133 mLanguage == other.mLanguage &&
DuyLionTran 0:51d7bdf88981 134 mText == other.mText;
DuyLionTran 0:51d7bdf88981 135
DuyLionTran 0:51d7bdf88981 136 }
DuyLionTran 0:51d7bdf88981 137
DuyLionTran 0:51d7bdf88981 138 virtual uint16_t write(uint8_t *buffer);
DuyLionTran 0:51d7bdf88981 139 virtual ~RecordText() { };
DuyLionTran 0:51d7bdf88981 140
DuyLionTran 0:51d7bdf88981 141 private:
DuyLionTran 0:51d7bdf88981 142
DuyLionTran 0:51d7bdf88981 143 void set_record_header();
DuyLionTran 0:51d7bdf88981 144
DuyLionTran 0:51d7bdf88981 145 void update_playload_length(){
DuyLionTran 0:51d7bdf88981 146 mRecordHeader.set_payload_length(1 + mLanguage.size() + mText.size());
DuyLionTran 0:51d7bdf88981 147 }
DuyLionTran 0:51d7bdf88981 148
DuyLionTran 0:51d7bdf88981 149 /**
DuyLionTran 0:51d7bdf88981 150 * encode the text encoding type and language size in a single byte
DuyLionTran 0:51d7bdf88981 151 * @param enc encoding used by this record
DuyLionTran 0:51d7bdf88981 152 * @param language language used by this record
DuyLionTran 0:51d7bdf88981 153 * @return text encoding and language size in a single byte
DuyLionTran 0:51d7bdf88981 154 */
DuyLionTran 0:51d7bdf88981 155 static uint8_t get_text_status(TextEncoding enc,
DuyLionTran 0:51d7bdf88981 156 const std::string &language) {
DuyLionTran 0:51d7bdf88981 157 uint8_t status = language.size();
DuyLionTran 0:51d7bdf88981 158 if (enc == UTF16) {
DuyLionTran 0:51d7bdf88981 159 status &= 0x80; // set to 1 the bit 7
DuyLionTran 0:51d7bdf88981 160 } //if
DuyLionTran 0:51d7bdf88981 161 return status;
DuyLionTran 0:51d7bdf88981 162 } //getTextStatus
DuyLionTran 0:51d7bdf88981 163
DuyLionTran 0:51d7bdf88981 164 /**
DuyLionTran 0:51d7bdf88981 165 * extract the encoding information from a status byte
DuyLionTran 0:51d7bdf88981 166 * @param textStatus status byte
DuyLionTran 0:51d7bdf88981 167 * @return encoding used by this record
DuyLionTran 0:51d7bdf88981 168 */
DuyLionTran 0:51d7bdf88981 169 static TextEncoding get_encoding(const uint8_t textStatus) {
DuyLionTran 0:51d7bdf88981 170 if ((textStatus & 0x80) == 0)
DuyLionTran 0:51d7bdf88981 171 return UTF8;
DuyLionTran 0:51d7bdf88981 172 else
DuyLionTran 0:51d7bdf88981 173 return UTF16;
DuyLionTran 0:51d7bdf88981 174 }
DuyLionTran 0:51d7bdf88981 175
DuyLionTran 0:51d7bdf88981 176 /**
DuyLionTran 0:51d7bdf88981 177 * extract language string length from a status byte
DuyLionTran 0:51d7bdf88981 178 * @param textStatus status byte
DuyLionTran 0:51d7bdf88981 179 * @return number of bytes needed to store the language name used by this record
DuyLionTran 0:51d7bdf88981 180 */
DuyLionTran 0:51d7bdf88981 181 static uint8_t get_language_length(const uint8_t textStatus) {
DuyLionTran 0:51d7bdf88981 182 //take the fist 5 bits
DuyLionTran 0:51d7bdf88981 183 return textStatus & 0x1F;
DuyLionTran 0:51d7bdf88981 184 }
DuyLionTran 0:51d7bdf88981 185
DuyLionTran 0:51d7bdf88981 186 const TextEncoding mEncode;
DuyLionTran 0:51d7bdf88981 187 const std::string mLanguage;
DuyLionTran 0:51d7bdf88981 188 std::string mText;
DuyLionTran 0:51d7bdf88981 189
DuyLionTran 0:51d7bdf88981 190 const uint8_t mTextStatus;
DuyLionTran 0:51d7bdf88981 191
DuyLionTran 0:51d7bdf88981 192 static const uint8_t NDEFTextIdCode;
DuyLionTran 0:51d7bdf88981 193
DuyLionTran 0:51d7bdf88981 194 };
DuyLionTran 0:51d7bdf88981 195
DuyLionTran 0:51d7bdf88981 196 } /* namespace NDefLib */
DuyLionTran 0:51d7bdf88981 197
DuyLionTran 0:51d7bdf88981 198 #endif /* NDEFLIB_RECORDTYPE_RECORDTEXT_H_ */