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@4:eaf6c49a86e4, 2015-12-01 (annotated)
- Committer:
- giovannivisentini
- Date:
- Tue Dec 01 08:30:27 2015 +0000
- Revision:
- 4:eaf6c49a86e4
- Parent:
- 1:a0eeb478a45a
- Child:
- 6:739e3211749d
add the possibility to change the record content + add doc
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
giovannivisentini | 0:04b82ae7aa43 | 1 | /* |
giovannivisentini | 0:04b82ae7aa43 | 2 | * Message.h |
giovannivisentini | 0:04b82ae7aa43 | 3 | * |
giovannivisentini | 0:04b82ae7aa43 | 4 | * Created on: Nov 6, 2015 |
giovannivisentini | 0:04b82ae7aa43 | 5 | * Author: giovanni visentini |
giovannivisentini | 0:04b82ae7aa43 | 6 | */ |
giovannivisentini | 0:04b82ae7aa43 | 7 | |
giovannivisentini | 0:04b82ae7aa43 | 8 | #ifndef NDEFLIB_MESSAGE_H_ |
giovannivisentini | 0:04b82ae7aa43 | 9 | #define NDEFLIB_MESSAGE_H_ |
giovannivisentini | 0:04b82ae7aa43 | 10 | |
giovannivisentini | 0:04b82ae7aa43 | 11 | #include <vector> |
giovannivisentini | 0:04b82ae7aa43 | 12 | |
giovannivisentini | 0:04b82ae7aa43 | 13 | #include "Record.h" |
giovannivisentini | 0:04b82ae7aa43 | 14 | |
giovannivisentini | 0:04b82ae7aa43 | 15 | namespace NDefLib { |
giovannivisentini | 0:04b82ae7aa43 | 16 | |
giovannivisentini | 4:eaf6c49a86e4 | 17 | /** |
giovannivisentini | 4:eaf6c49a86e4 | 18 | * class containing a set of NDefRecord |
giovannivisentini | 4:eaf6c49a86e4 | 19 | */ |
giovannivisentini | 0:04b82ae7aa43 | 20 | class Message { |
giovannivisentini | 0:04b82ae7aa43 | 21 | public: |
giovannivisentini | 4:eaf6c49a86e4 | 22 | |
giovannivisentini | 4:eaf6c49a86e4 | 23 | Message() { } |
giovannivisentini | 0:04b82ae7aa43 | 24 | |
giovannivisentini | 4:eaf6c49a86e4 | 25 | /** |
giovannivisentini | 4:eaf6c49a86e4 | 26 | * add a ndef record to this message |
giovannivisentini | 4:eaf6c49a86e4 | 27 | * @param r record to add |
giovannivisentini | 4:eaf6c49a86e4 | 28 | */ |
giovannivisentini | 4:eaf6c49a86e4 | 29 | void addRecord(Record *r) { |
giovannivisentini | 0:04b82ae7aa43 | 30 | mRecords.push_back(r); |
giovannivisentini | 0:04b82ae7aa43 | 31 | } |
giovannivisentini | 0:04b82ae7aa43 | 32 | |
giovannivisentini | 4:eaf6c49a86e4 | 33 | /** |
giovannivisentini | 4:eaf6c49a86e4 | 34 | * add all the record in the list to this message |
giovannivisentini | 4:eaf6c49a86e4 | 35 | * @param addList list of record to add |
giovannivisentini | 4:eaf6c49a86e4 | 36 | */ |
giovannivisentini | 4:eaf6c49a86e4 | 37 | void addRecords(const std::vector<Record*> &addList) { |
giovannivisentini | 4:eaf6c49a86e4 | 38 | mRecords.insert(mRecords.end(), addList.begin(), addList.end()); |
giovannivisentini | 1:a0eeb478a45a | 39 | } |
giovannivisentini | 1:a0eeb478a45a | 40 | |
giovannivisentini | 4:eaf6c49a86e4 | 41 | /** |
giovannivisentini | 4:eaf6c49a86e4 | 42 | * get the specific record contained by this message, or NULL if itn't a valid index |
giovannivisentini | 4:eaf6c49a86e4 | 43 | * @param index record index |
giovannivisentini | 4:eaf6c49a86e4 | 44 | * @return if present a record otherwise NULL |
giovannivisentini | 4:eaf6c49a86e4 | 45 | */ |
giovannivisentini | 4:eaf6c49a86e4 | 46 | Record* operator[](const uint32_t index){ |
giovannivisentini | 4:eaf6c49a86e4 | 47 | if (index > mRecords.size()) |
giovannivisentini | 1:a0eeb478a45a | 48 | return NULL; |
giovannivisentini | 1:a0eeb478a45a | 49 | return mRecords[index]; |
giovannivisentini | 1:a0eeb478a45a | 50 | } |
giovannivisentini | 1:a0eeb478a45a | 51 | |
giovannivisentini | 4:eaf6c49a86e4 | 52 | /** |
giovannivisentini | 4:eaf6c49a86e4 | 53 | * get the number of records in this message |
giovannivisentini | 4:eaf6c49a86e4 | 54 | * @return number of record in this message |
giovannivisentini | 4:eaf6c49a86e4 | 55 | */ |
giovannivisentini | 4:eaf6c49a86e4 | 56 | uint32_t getNRecords() const { |
giovannivisentini | 1:a0eeb478a45a | 57 | return mRecords.size(); |
giovannivisentini | 1:a0eeb478a45a | 58 | } |
giovannivisentini | 1:a0eeb478a45a | 59 | |
giovannivisentini | 4:eaf6c49a86e4 | 60 | /** |
giovannivisentini | 4:eaf6c49a86e4 | 61 | * length in byte needed for write this message |
giovannivisentini | 4:eaf6c49a86e4 | 62 | * @return number of byte needed for with this message |
giovannivisentini | 4:eaf6c49a86e4 | 63 | */ |
giovannivisentini | 4:eaf6c49a86e4 | 64 | uint16_t getByteLength() const; |
giovannivisentini | 4:eaf6c49a86e4 | 65 | |
giovannivisentini | 4:eaf6c49a86e4 | 66 | /** |
giovannivisentini | 4:eaf6c49a86e4 | 67 | * write this message in the buffer |
giovannivisentini | 4:eaf6c49a86e4 | 68 | * @par the first 2 byte are the NDEF message length |
giovannivisentini | 4:eaf6c49a86e4 | 69 | * @param[out] buffer buffer where write this message |
giovannivisentini | 4:eaf6c49a86e4 | 70 | * @return number of bytes wrote |
giovannivisentini | 4:eaf6c49a86e4 | 71 | */ |
giovannivisentini | 4:eaf6c49a86e4 | 72 | uint16_t write(uint8_t *buffer) const; |
giovannivisentini | 0:04b82ae7aa43 | 73 | |
giovannivisentini | 4:eaf6c49a86e4 | 74 | /** |
giovannivisentini | 4:eaf6c49a86e4 | 75 | * try to rebuild all the record from a message buffer |
giovannivisentini | 4:eaf6c49a86e4 | 76 | * @par I assume that you already read the NDEF length, so the buffer doesn't contain it |
giovannivisentini | 4:eaf6c49a86e4 | 77 | * @param buffer buffer containing the message record |
giovannivisentini | 4:eaf6c49a86e4 | 78 | * @param bufferLength buffer length |
giovannivisentini | 4:eaf6c49a86e4 | 79 | * @param message message where the function will add the read record |
giovannivisentini | 4:eaf6c49a86e4 | 80 | */ |
giovannivisentini | 4:eaf6c49a86e4 | 81 | static void parseMessage(const uint8_t * const buffer, |
giovannivisentini | 4:eaf6c49a86e4 | 82 | const uint8_t bufferLength, Message *message); |
giovannivisentini | 0:04b82ae7aa43 | 83 | |
giovannivisentini | 4:eaf6c49a86e4 | 84 | virtual ~Message() { |
giovannivisentini | 4:eaf6c49a86e4 | 85 | } |
giovannivisentini | 4:eaf6c49a86e4 | 86 | |
giovannivisentini | 0:04b82ae7aa43 | 87 | private: |
giovannivisentini | 4:eaf6c49a86e4 | 88 | /** |
giovannivisentini | 4:eaf6c49a86e4 | 89 | * list of record contained by this message |
giovannivisentini | 4:eaf6c49a86e4 | 90 | */ |
giovannivisentini | 0:04b82ae7aa43 | 91 | std::vector<Record*> mRecords; |
giovannivisentini | 0:04b82ae7aa43 | 92 | }; |
giovannivisentini | 0:04b82ae7aa43 | 93 | |
giovannivisentini | 0:04b82ae7aa43 | 94 | } /* namespace NDefLib */ |
giovannivisentini | 0:04b82ae7aa43 | 95 | |
giovannivisentini | 0:04b82ae7aa43 | 96 | #endif /* NDEFLIB_MESSAGE_H_ */ |