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