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.
Message.h@17:46899fa3d9f2, 2016-08-31 (annotated)
- Committer:
- giovannivisentini
- Date:
- Wed Aug 31 08:32:13 2016 +0000
- Revision:
- 17:46899fa3d9f2
- Parent:
- 15:01fc5a4b8366
- Child:
- 19:13d84b136a62
add function for remove/delete Record from a Message
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 Message.h |
giovannivisentini | 8:473f6e0b03df | 4 | * @author ST / Central Labs |
giovannivisentini | 6:739e3211749d | 5 | * @version V1.0.0 |
giovannivisentini | 6:739e3211749d | 6 | * @date 6 Nov 2015 |
giovannivisentini | 6:739e3211749d | 7 | * @brief NDef Message class |
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 | #ifndef NDEFLIB_MESSAGE_H_ |
giovannivisentini | 0:04b82ae7aa43 | 39 | #define NDEFLIB_MESSAGE_H_ |
giovannivisentini | 0:04b82ae7aa43 | 40 | |
giovannivisentini | 17:46899fa3d9f2 | 41 | #include <algorithm> |
giovannivisentini | 0:04b82ae7aa43 | 42 | #include <vector> |
giovannivisentini | 0:04b82ae7aa43 | 43 | |
giovannivisentini | 0:04b82ae7aa43 | 44 | #include "Record.h" |
giovannivisentini | 0:04b82ae7aa43 | 45 | |
giovannivisentini | 0:04b82ae7aa43 | 46 | namespace NDefLib { |
giovannivisentini | 0:04b82ae7aa43 | 47 | |
giovannivisentini | 4:eaf6c49a86e4 | 48 | /** |
giovannivisentini | 12:ed4d9b8d1410 | 49 | * Class containing a list of {@link Record} |
giovannivisentini | 4:eaf6c49a86e4 | 50 | */ |
giovannivisentini | 0:04b82ae7aa43 | 51 | class Message { |
giovannivisentini | 0:04b82ae7aa43 | 52 | public: |
giovannivisentini | 4:eaf6c49a86e4 | 53 | |
giovannivisentini | 4:eaf6c49a86e4 | 54 | /** |
giovannivisentini | 12:ed4d9b8d1410 | 55 | * Add a ndef record to this message. |
giovannivisentini | 12:ed4d9b8d1410 | 56 | * @param r Record to add |
giovannivisentini | 4:eaf6c49a86e4 | 57 | */ |
giovannivisentini | 4:eaf6c49a86e4 | 58 | void addRecord(Record *r) { |
giovannivisentini | 0:04b82ae7aa43 | 59 | mRecords.push_back(r); |
giovannivisentini | 0:04b82ae7aa43 | 60 | } |
giovannivisentini | 0:04b82ae7aa43 | 61 | |
giovannivisentini | 4:eaf6c49a86e4 | 62 | /** |
giovannivisentini | 17:46899fa3d9f2 | 63 | * Remove a ndef record to this message |
giovannivisentini | 17:46899fa3d9f2 | 64 | * @param r record to remove |
giovannivisentini | 17:46899fa3d9f2 | 65 | */ |
giovannivisentini | 17:46899fa3d9f2 | 66 | void removeRecord(Record *r){ |
giovannivisentini | 17:46899fa3d9f2 | 67 | mRecords.erase( std::remove( mRecords.begin(), mRecords.end(), r ), |
giovannivisentini | 17:46899fa3d9f2 | 68 | mRecords.end() ); |
giovannivisentini | 17:46899fa3d9f2 | 69 | } |
giovannivisentini | 17:46899fa3d9f2 | 70 | |
giovannivisentini | 17:46899fa3d9f2 | 71 | |
giovannivisentini | 17:46899fa3d9f2 | 72 | /** |
giovannivisentini | 12:ed4d9b8d1410 | 73 | * Add all the records in the list to this message. |
giovannivisentini | 12:ed4d9b8d1410 | 74 | * @param addList List of records to add. |
giovannivisentini | 4:eaf6c49a86e4 | 75 | */ |
giovannivisentini | 4:eaf6c49a86e4 | 76 | void addRecords(const std::vector<Record*> &addList) { |
giovannivisentini | 4:eaf6c49a86e4 | 77 | mRecords.insert(mRecords.end(), addList.begin(), addList.end()); |
giovannivisentini | 1:a0eeb478a45a | 78 | } |
giovannivisentini | 1:a0eeb478a45a | 79 | |
giovannivisentini | 4:eaf6c49a86e4 | 80 | /** |
giovannivisentini | 12:ed4d9b8d1410 | 81 | * Get the specific record contained by this message, NULL if not a valid index. |
giovannivisentini | 12:ed4d9b8d1410 | 82 | * @param index Record index. |
giovannivisentini | 12:ed4d9b8d1410 | 83 | * @return a Record object if present, otherwise NULL |
giovannivisentini | 4:eaf6c49a86e4 | 84 | */ |
giovannivisentini | 15:01fc5a4b8366 | 85 | Record* operator[](const uint32_t index)const{ |
giovannivisentini | 7:1ebd6049fa57 | 86 | if (index >= mRecords.size()) |
giovannivisentini | 1:a0eeb478a45a | 87 | return NULL; |
giovannivisentini | 1:a0eeb478a45a | 88 | return mRecords[index]; |
giovannivisentini | 1:a0eeb478a45a | 89 | } |
giovannivisentini | 1:a0eeb478a45a | 90 | |
giovannivisentini | 4:eaf6c49a86e4 | 91 | /** |
giovannivisentini | 12:ed4d9b8d1410 | 92 | * Get the number of records in this message. |
giovannivisentini | 12:ed4d9b8d1410 | 93 | * @return number of records in this message |
giovannivisentini | 4:eaf6c49a86e4 | 94 | */ |
giovannivisentini | 4:eaf6c49a86e4 | 95 | uint32_t getNRecords() const { |
giovannivisentini | 1:a0eeb478a45a | 96 | return mRecords.size(); |
giovannivisentini | 1:a0eeb478a45a | 97 | } |
giovannivisentini | 1:a0eeb478a45a | 98 | |
giovannivisentini | 4:eaf6c49a86e4 | 99 | /** |
giovannivisentini | 12:ed4d9b8d1410 | 100 | * Length in bytes needed to write this message. |
giovannivisentini | 12:ed4d9b8d1410 | 101 | * @return number of bytes needed to write this message |
giovannivisentini | 4:eaf6c49a86e4 | 102 | */ |
giovannivisentini | 4:eaf6c49a86e4 | 103 | uint16_t getByteLength() const; |
giovannivisentini | 4:eaf6c49a86e4 | 104 | |
giovannivisentini | 4:eaf6c49a86e4 | 105 | /** |
giovannivisentini | 12:ed4d9b8d1410 | 106 | * Write message in the provided buffer |
giovannivisentini | 12:ed4d9b8d1410 | 107 | * @par The first 2 bytes contain the NDEF message length. |
giovannivisentini | 12:ed4d9b8d1410 | 108 | * @param[out] buffer Buffer the message must be written into. |
giovannivisentini | 12:ed4d9b8d1410 | 109 | * @return number of bytes written |
giovannivisentini | 4:eaf6c49a86e4 | 110 | */ |
giovannivisentini | 4:eaf6c49a86e4 | 111 | uint16_t write(uint8_t *buffer) const; |
giovannivisentini | 0:04b82ae7aa43 | 112 | |
giovannivisentini | 4:eaf6c49a86e4 | 113 | /** |
giovannivisentini | 12:ed4d9b8d1410 | 114 | * Create a set of records from a raw buffer adding them to a message object. |
giovannivisentini | 12:ed4d9b8d1410 | 115 | * @par Message buffer must NOT contain the buffer length in the first two bytes. |
giovannivisentini | 12:ed4d9b8d1410 | 116 | * @param buffer Buffer containing the message record. |
giovannivisentini | 12:ed4d9b8d1410 | 117 | * @param bufferLength Buffer length. |
giovannivisentini | 12:ed4d9b8d1410 | 118 | * @param[in,out] Message message that will contain the new records. |
giovannivisentini | 4:eaf6c49a86e4 | 119 | */ |
giovannivisentini | 4:eaf6c49a86e4 | 120 | static void parseMessage(const uint8_t * const buffer, |
giovannivisentini | 12:ed4d9b8d1410 | 121 | const uint16_t bufferLength, Message *message); |
giovannivisentini | 0:04b82ae7aa43 | 122 | |
giovannivisentini | 17:46899fa3d9f2 | 123 | /** |
giovannivisentini | 17:46899fa3d9f2 | 124 | * Remove all the recrods from the mesasge and delete it |
giovannivisentini | 17:46899fa3d9f2 | 125 | * @param msg Message with the records to delete |
giovannivisentini | 17:46899fa3d9f2 | 126 | */ |
giovannivisentini | 17:46899fa3d9f2 | 127 | static void removeAndDeleteAllRecord(Message &msg); |
giovannivisentini | 17:46899fa3d9f2 | 128 | |
giovannivisentini | 4:eaf6c49a86e4 | 129 | virtual ~Message() { |
giovannivisentini | 4:eaf6c49a86e4 | 130 | } |
giovannivisentini | 4:eaf6c49a86e4 | 131 | |
giovannivisentini | 0:04b82ae7aa43 | 132 | private: |
giovannivisentini | 4:eaf6c49a86e4 | 133 | /** |
giovannivisentini | 12:ed4d9b8d1410 | 134 | * List of records contained by this message. |
giovannivisentini | 4:eaf6c49a86e4 | 135 | */ |
giovannivisentini | 0:04b82ae7aa43 | 136 | std::vector<Record*> mRecords; |
giovannivisentini | 0:04b82ae7aa43 | 137 | }; |
giovannivisentini | 0:04b82ae7aa43 | 138 | |
giovannivisentini | 0:04b82ae7aa43 | 139 | } /* namespace NDefLib */ |
giovannivisentini | 0:04b82ae7aa43 | 140 | |
giovannivisentini | 0:04b82ae7aa43 | 141 | #endif /* NDEFLIB_MESSAGE_H_ */ |