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.cpp@21:72c86cbd49be, 2017-08-21 (annotated)
- Committer:
- giovannivisentini
- Date:
- Mon Aug 21 12:02:31 2017 +0000
- Revision:
- 21:72c86cbd49be
- Parent:
- 20:31f727872290
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.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 | 6:739e3211749d | 7 | * @brief RecordText implementation |
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 | 0:04b82ae7aa43 | 38 | #include <cstring> |
giovannivisentini | 0:04b82ae7aa43 | 39 | #include <NDefLib/RecordType/RecordText.h> |
giovannivisentini | 0:04b82ae7aa43 | 40 | |
giovannivisentini | 0:04b82ae7aa43 | 41 | namespace NDefLib { |
giovannivisentini | 0:04b82ae7aa43 | 42 | |
giovannivisentini | 4:eaf6c49a86e4 | 43 | const uint8_t RecordText::NDEFTextIdCode = 'T'; |
giovannivisentini | 0:04b82ae7aa43 | 44 | |
giovannivisentini | 19:13d84b136a62 | 45 | void RecordText::set_record_header() { |
giovannivisentini | 19:13d84b136a62 | 46 | mRecordHeader.set_FNT(RecordHeader::NFC_well_known); |
giovannivisentini | 19:13d84b136a62 | 47 | mRecordHeader.set_type_length(sizeof(NDEFTextIdCode)); |
giovannivisentini | 19:13d84b136a62 | 48 | update_playload_length(); |
giovannivisentini | 0:04b82ae7aa43 | 49 | } |
giovannivisentini | 0:04b82ae7aa43 | 50 | |
giovannivisentini | 4:eaf6c49a86e4 | 51 | RecordText::RecordText(const std::string &text) : |
Davidroid | 20:31f727872290 | 52 | mEncode(UTF8), mLanguage("en"), mText(text), mTextStatus (get_text_status(mEncode, mLanguage)) { |
giovannivisentini | 19:13d84b136a62 | 53 | set_record_header(); |
giovannivisentini | 0:04b82ae7aa43 | 54 | } |
giovannivisentini | 0:04b82ae7aa43 | 55 | |
giovannivisentini | 4:eaf6c49a86e4 | 56 | RecordText::RecordText(const TextEncoding encoding, const std::string &language, |
Davidroid | 20:31f727872290 | 57 | const std::string &text) : |
Davidroid | 20:31f727872290 | 58 | mEncode(encoding), mLanguage(language), mText(text), mTextStatus(get_text_status(mEncode, mLanguage)) { |
giovannivisentini | 19:13d84b136a62 | 59 | set_record_header(); |
giovannivisentini | 0:04b82ae7aa43 | 60 | } |
giovannivisentini | 0:04b82ae7aa43 | 61 | |
giovannivisentini | 4:eaf6c49a86e4 | 62 | uint16_t RecordText::write(uint8_t *buffer) { |
giovannivisentini | 4:eaf6c49a86e4 | 63 | int16_t offset = 0; |
giovannivisentini | 19:13d84b136a62 | 64 | offset += mRecordHeader.write_header(buffer); |
giovannivisentini | 0:04b82ae7aa43 | 65 | |
giovannivisentini | 4:eaf6c49a86e4 | 66 | buffer[offset++] = NDEFTextIdCode; |
giovannivisentini | 4:eaf6c49a86e4 | 67 | buffer[offset++] = mTextStatus; |
giovannivisentini | 0:04b82ae7aa43 | 68 | |
giovannivisentini | 4:eaf6c49a86e4 | 69 | std::memcpy(buffer + offset, mLanguage.c_str(), mLanguage.size()); |
giovannivisentini | 4:eaf6c49a86e4 | 70 | offset += mLanguage.size(); |
giovannivisentini | 4:eaf6c49a86e4 | 71 | std::memcpy(buffer + offset, mText.c_str(), mText.size()); |
giovannivisentini | 4:eaf6c49a86e4 | 72 | offset += mText.size(); |
Davidroid | 20:31f727872290 | 73 | |
giovannivisentini | 0:04b82ae7aa43 | 74 | return offset; |
giovannivisentini | 0:04b82ae7aa43 | 75 | } |
giovannivisentini | 0:04b82ae7aa43 | 76 | |
Davidroid | 20:31f727872290 | 77 | RecordText* RecordText::parse(const RecordHeader &header, const uint8_t * const buffer) { |
giovannivisentini | 4:eaf6c49a86e4 | 78 | uint32_t index = 0; |
Davidroid | 20:31f727872290 | 79 | if (header.get_FNT() == RecordHeader::NFC_well_known && buffer[index++] == NDEFTextIdCode) { |
giovannivisentini | 0:04b82ae7aa43 | 80 | const uint8_t textStatus = buffer[index++]; |
giovannivisentini | 19:13d84b136a62 | 81 | const TextEncoding enc = get_encoding(textStatus); |
giovannivisentini | 19:13d84b136a62 | 82 | const uint8_t langSize = get_language_length(textStatus); |
giovannivisentini | 0:04b82ae7aa43 | 83 | //-1 is the textStatus |
giovannivisentini | 19:13d84b136a62 | 84 | const uint8_t textSize = header.get_payload_length() - langSize - 1; |
giovannivisentini | 0:04b82ae7aa43 | 85 | |
giovannivisentini | 4:eaf6c49a86e4 | 86 | return new RecordText(enc, |
Davidroid | 20:31f727872290 | 87 | std::string((const char*) (buffer + index), langSize), |
Davidroid | 20:31f727872290 | 88 | std::string((const char*) (buffer + index + langSize), |
Davidroid | 20:31f727872290 | 89 | textSize)); |
Davidroid | 20:31f727872290 | 90 | } else { |
giovannivisentini | 0:04b82ae7aa43 | 91 | return NULL; |
Davidroid | 20:31f727872290 | 92 | } |
giovannivisentini | 0:04b82ae7aa43 | 93 | } |
giovannivisentini | 0:04b82ae7aa43 | 94 | |
giovannivisentini | 0:04b82ae7aa43 | 95 | } /* namespace NDefLib */ |
Davidroid | 20:31f727872290 | 96 | |
Davidroid | 20:31f727872290 | 97 | |
Davidroid | 20:31f727872290 | 98 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ |