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.
Diff: RecordHeader.h
- Revision:
- 4:eaf6c49a86e4
- Child:
- 6:739e3211749d
diff -r 64bb6d5224dc -r eaf6c49a86e4 RecordHeader.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/RecordHeader.h Tue Dec 01 08:30:27 2015 +0000 @@ -0,0 +1,255 @@ +/* + * RecordHeader.h + * + * Created on: Nov 30, 2015 + * Author: giovanni visentini + */ + +#ifndef NDEFLIB_RECORDHEADER_H_ +#define NDEFLIB_RECORDHEADER_H_ + +#include <stdint.h> + +namespace NDefLib { + +/** + * Record header class + * @see NFC Data Exchange Format (NDEF) Technical Specification NDEF 1.0 + */ +class RecordHeader { + +public: + + /** + * record type format + */ + typedef enum TypeNameFormat { + Empty = 0x00, //!< Empty + NFC_well_known = 0x01, //!< NFC_well_known + Mime_media_type = 0x02,//!< Mime_media_type + Absolute_URI = 0x03, //!< Absolute_URI + NFC_external = 0x04, //!< NFC_external + Unknown = 0x05, //!< Unknown + Unchanged = 0x06, //!< Unchanged + Reserved = 0x07 //!< Reserved + } TypeNameFormat_t; + + + RecordHeader() : + headerFlags(0), typeLength(0), playloadLength(0) { + } + + /** + * set the message begin flag + * @param value true if the record is the first of the message + */ + void setMB(bool value) { + if (value) + headerFlags |= 0x80; + else + headerFlags &= 0x7F; + }//setMB + + /** + * get the message being flag + * @return true if it is the first record in the message + */ + bool getMB() const { + return (headerFlags & 0x80) != 0; + }//getMB + + /** + * set the message end flag + * @param value true if it is the last record in the message + */ + void setME(bool value) { + if (value) + headerFlags |= 0x40; + else + headerFlags &= 0xBF; + }//setME + + /** + * get the message end flag + * @return true if it is the last record in the message + */ + bool getME() const { + return (headerFlags & 0x40) != 0; + }//getME + + /** + * set the Chunk flag + * @param value true if the record is in the first record chunk or a middle record + * chunk of a chunked payload + */ + void setCF(bool value) { + if (value) + headerFlags |= 0x20; + else + headerFlags &= 0xDF; + }//getCF + + /** + * get the Chunk flag value + * @return true if the record is in the first record chunk or a middle record + * chunk of a chunked payload + */ + bool getCF() const { + return (headerFlags & 0x20) != 0; + }//getCF + + /** + * set the short record flag value + * @param value true if the record size can be encoded with 8 bits + */ + void setSR(bool value) { + if (value) + headerFlags |= 0x10; + else + headerFlags &= 0xCF; + }//setSR + + /** + * get the Short record flag + * @return true if we are using the short range header format + */ + bool getSR() const { + return (headerFlags & 0x10) != 0; + }//getSR + + /** + * set the ID length flag + * @param value true if we will use the id length value in the record + */ + void setIL(bool value) { + if (value) + headerFlags |= 0x08; + else + headerFlags &= 0xAF; + }//setIL + + /** + * get the ID length flag + * @param value true if we will use the id length value in the record + */ + bool getIL() const { + return (headerFlags & 0x08) != 0; + }//getIL + + /** + * set the type name format field + * @param value record type name format + */ + void setFNT(const TypeNameFormat_t value) { + uint8_t temp = (uint8_t) value; + temp &= 0x07; //keep the first 3 bits + headerFlags &= 0xF8; //clean the fist 3 bits + headerFlags |= temp; //set the fist 3 bits + }//setFNT + + /** + * get the record type name + * @return type name format of the record + */ + TypeNameFormat_t getFNT() const { + return (TypeNameFormat_t) (headerFlags & 0x07); + } + + /** + * set the record playload length + * @par this function will update the SR field as needed + * @param length playload length + */ + void setPlayloadLength(uint32_t length) { + playloadLength = length; + setSR(playloadLength <= 255); + } + + /** + * get the playload length + * @return playload length + */ + uint32_t getPlayloadLength() const { + return playloadLength; + } + + /** + * set the type length + * @param size + */ + void setTypeLength(uint8_t size) { + typeLength = size; + } + + /** + * get the type length + * @return + */ + uint8_t getTypeLength() const { + return typeLength; + } + + /** + * get the number of byte needed for store this record + * @return 3 or 6 + */ + uint16_t getRecordLength() const { + return (getSR() ? 3 : 6) + typeLength + playloadLength; + } + + /** + * store the header information in the buffer + * @param[out] outBuffer buffer where write the header + * @return number of write bytes + */ + uint8_t writeHeader(uint8_t *outBuffer) const { + + uint32_t index = 0; + + outBuffer[index++] = headerFlags; + outBuffer[index++] = typeLength; + if (getSR()) { + outBuffer[index++] = (uint8_t) playloadLength; + } else { + outBuffer[index++] = (uint8_t) ((playloadLength & 0xFF000000) + >> 24); + outBuffer[index++] = (uint8_t) ((playloadLength & 0x00FF0000) + >> 16); + outBuffer[index++] = (uint8_t) ((playloadLength & 0x0000FF00) + >> 8); + outBuffer[index++] = (uint8_t) (playloadLength & 0x000000FF); + } //if-else + return index; + } //writeHeader + + /** + * load an header from a buffer + * @param buffer buffer where read the header + * @return number of read bytes + */ + uint16_t loadHeader(const uint8_t * const buffer) { + uint32_t index = 0; + headerFlags = buffer[index++]; + typeLength = buffer[index++]; + if (getSR()) { + playloadLength = buffer[index++]; + } else { + playloadLength = (((uint32_t) buffer[index + 0]) << 24) + | (((uint32_t) buffer[index + 1]) << 16) + | (((uint32_t) buffer[index + 2]) << 8) + | ((uint32_t) buffer[index + 3]); + index += 4; + } //if-else + return index; + } //loadHeader + + +private: + uint8_t headerFlags; + uint8_t typeLength; + uint32_t playloadLength; +}; + +} /* namespace NDefLib */ + +#endif /* NDEFLIB_RECORDHEADER_H_ */