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@6:739e3211749d, 2015-12-03 (annotated)
- Committer:
- giovannivisentini
- Date:
- Thu Dec 03 14:56:01 2015 +0000
- Revision:
- 6:739e3211749d
- Parent:
- 4:eaf6c49a86e4
- Child:
- 7:1ebd6049fa57
Add License
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 | 6:739e3211749d | 4 | * @author ST / CentralLabs |
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 | 0:04b82ae7aa43 | 41 | #include <vector> |
giovannivisentini | 0:04b82ae7aa43 | 42 | |
giovannivisentini | 0:04b82ae7aa43 | 43 | #include "Record.h" |
giovannivisentini | 0:04b82ae7aa43 | 44 | |
giovannivisentini | 0:04b82ae7aa43 | 45 | namespace NDefLib { |
giovannivisentini | 0:04b82ae7aa43 | 46 | |
giovannivisentini | 4:eaf6c49a86e4 | 47 | /** |
giovannivisentini | 4:eaf6c49a86e4 | 48 | * class containing a set of NDefRecord |
giovannivisentini | 4:eaf6c49a86e4 | 49 | */ |
giovannivisentini | 0:04b82ae7aa43 | 50 | class Message { |
giovannivisentini | 0:04b82ae7aa43 | 51 | public: |
giovannivisentini | 4:eaf6c49a86e4 | 52 | |
giovannivisentini | 4:eaf6c49a86e4 | 53 | Message() { } |
giovannivisentini | 0:04b82ae7aa43 | 54 | |
giovannivisentini | 4:eaf6c49a86e4 | 55 | /** |
giovannivisentini | 4:eaf6c49a86e4 | 56 | * add a ndef record to this message |
giovannivisentini | 4:eaf6c49a86e4 | 57 | * @param r record to add |
giovannivisentini | 4:eaf6c49a86e4 | 58 | */ |
giovannivisentini | 4:eaf6c49a86e4 | 59 | void addRecord(Record *r) { |
giovannivisentini | 0:04b82ae7aa43 | 60 | mRecords.push_back(r); |
giovannivisentini | 0:04b82ae7aa43 | 61 | } |
giovannivisentini | 0:04b82ae7aa43 | 62 | |
giovannivisentini | 4:eaf6c49a86e4 | 63 | /** |
giovannivisentini | 4:eaf6c49a86e4 | 64 | * add all the record in the list to this message |
giovannivisentini | 4:eaf6c49a86e4 | 65 | * @param addList list of record to add |
giovannivisentini | 4:eaf6c49a86e4 | 66 | */ |
giovannivisentini | 4:eaf6c49a86e4 | 67 | void addRecords(const std::vector<Record*> &addList) { |
giovannivisentini | 4:eaf6c49a86e4 | 68 | mRecords.insert(mRecords.end(), addList.begin(), addList.end()); |
giovannivisentini | 1:a0eeb478a45a | 69 | } |
giovannivisentini | 1:a0eeb478a45a | 70 | |
giovannivisentini | 4:eaf6c49a86e4 | 71 | /** |
giovannivisentini | 4:eaf6c49a86e4 | 72 | * get the specific record contained by this message, or NULL if itn't a valid index |
giovannivisentini | 4:eaf6c49a86e4 | 73 | * @param index record index |
giovannivisentini | 4:eaf6c49a86e4 | 74 | * @return if present a record otherwise NULL |
giovannivisentini | 4:eaf6c49a86e4 | 75 | */ |
giovannivisentini | 4:eaf6c49a86e4 | 76 | Record* operator[](const uint32_t index){ |
giovannivisentini | 4:eaf6c49a86e4 | 77 | if (index > mRecords.size()) |
giovannivisentini | 1:a0eeb478a45a | 78 | return NULL; |
giovannivisentini | 1:a0eeb478a45a | 79 | return mRecords[index]; |
giovannivisentini | 1:a0eeb478a45a | 80 | } |
giovannivisentini | 1:a0eeb478a45a | 81 | |
giovannivisentini | 4:eaf6c49a86e4 | 82 | /** |
giovannivisentini | 4:eaf6c49a86e4 | 83 | * get the number of records in this message |
giovannivisentini | 4:eaf6c49a86e4 | 84 | * @return number of record in this message |
giovannivisentini | 4:eaf6c49a86e4 | 85 | */ |
giovannivisentini | 4:eaf6c49a86e4 | 86 | uint32_t getNRecords() const { |
giovannivisentini | 1:a0eeb478a45a | 87 | return mRecords.size(); |
giovannivisentini | 1:a0eeb478a45a | 88 | } |
giovannivisentini | 1:a0eeb478a45a | 89 | |
giovannivisentini | 4:eaf6c49a86e4 | 90 | /** |
giovannivisentini | 4:eaf6c49a86e4 | 91 | * length in byte needed for write this message |
giovannivisentini | 4:eaf6c49a86e4 | 92 | * @return number of byte needed for with this message |
giovannivisentini | 4:eaf6c49a86e4 | 93 | */ |
giovannivisentini | 4:eaf6c49a86e4 | 94 | uint16_t getByteLength() const; |
giovannivisentini | 4:eaf6c49a86e4 | 95 | |
giovannivisentini | 4:eaf6c49a86e4 | 96 | /** |
giovannivisentini | 4:eaf6c49a86e4 | 97 | * write this message in the buffer |
giovannivisentini | 4:eaf6c49a86e4 | 98 | * @par the first 2 byte are the NDEF message length |
giovannivisentini | 4:eaf6c49a86e4 | 99 | * @param[out] buffer buffer where write this message |
giovannivisentini | 4:eaf6c49a86e4 | 100 | * @return number of bytes wrote |
giovannivisentini | 4:eaf6c49a86e4 | 101 | */ |
giovannivisentini | 4:eaf6c49a86e4 | 102 | uint16_t write(uint8_t *buffer) const; |
giovannivisentini | 0:04b82ae7aa43 | 103 | |
giovannivisentini | 4:eaf6c49a86e4 | 104 | /** |
giovannivisentini | 4:eaf6c49a86e4 | 105 | * try to rebuild all the record from a message buffer |
giovannivisentini | 4:eaf6c49a86e4 | 106 | * @par I assume that you already read the NDEF length, so the buffer doesn't contain it |
giovannivisentini | 4:eaf6c49a86e4 | 107 | * @param buffer buffer containing the message record |
giovannivisentini | 4:eaf6c49a86e4 | 108 | * @param bufferLength buffer length |
giovannivisentini | 4:eaf6c49a86e4 | 109 | * @param message message where the function will add the read record |
giovannivisentini | 4:eaf6c49a86e4 | 110 | */ |
giovannivisentini | 4:eaf6c49a86e4 | 111 | static void parseMessage(const uint8_t * const buffer, |
giovannivisentini | 4:eaf6c49a86e4 | 112 | const uint8_t bufferLength, Message *message); |
giovannivisentini | 0:04b82ae7aa43 | 113 | |
giovannivisentini | 4:eaf6c49a86e4 | 114 | virtual ~Message() { |
giovannivisentini | 4:eaf6c49a86e4 | 115 | } |
giovannivisentini | 4:eaf6c49a86e4 | 116 | |
giovannivisentini | 0:04b82ae7aa43 | 117 | private: |
giovannivisentini | 4:eaf6c49a86e4 | 118 | /** |
giovannivisentini | 4:eaf6c49a86e4 | 119 | * list of record contained by this message |
giovannivisentini | 4:eaf6c49a86e4 | 120 | */ |
giovannivisentini | 0:04b82ae7aa43 | 121 | std::vector<Record*> mRecords; |
giovannivisentini | 0:04b82ae7aa43 | 122 | }; |
giovannivisentini | 0:04b82ae7aa43 | 123 | |
giovannivisentini | 0:04b82ae7aa43 | 124 | } /* namespace NDefLib */ |
giovannivisentini | 0:04b82ae7aa43 | 125 | |
giovannivisentini | 0:04b82ae7aa43 | 126 | #endif /* NDEFLIB_MESSAGE_H_ */ |