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 ST Expansion SW Team

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.

Revision:
4:eaf6c49a86e4
Parent:
2:760e36ba9c23
Child:
6:739e3211749d
--- a/Record.h	Fri Nov 27 15:09:55 2015 +0000
+++ b/Record.h	Tue Dec 01 08:30:27 2015 +0000
@@ -9,196 +9,89 @@
 #define NDEFLIB_RECORD_H_
 #include <stdint.h>
 
+#include "RecordHeader.h"
+
 namespace NDefLib {
 
+/**
+ * Base class for a NDefRecord
+ * @see NFC Data Exchange Format (NDEF) Technical Specification NDEF 1.0
+ */
 class Record {
 public:
-	class RecordHeader{
-
-		public:
-			typedef enum TypeNameFormat{
-				Empty=0x00,
-				NFC_well_known=0x01,
-				Mime_media_type=0x02,
-				Absolute_URI=0x03,
-				NFC_external=0x04,
-				Unknown=0x05,
-				Unchanged=0x06,
-				Reserved=0x07
-			}TypeNameFormat_t;
-
-			RecordHeader():headerFlags(0),
-					typeLenght(0),playloadLenght(0){}
-
-			void setMB(bool value){
-				if(value)
-					headerFlags |= 0x80;
-				else
-					headerFlags &= 0x7F;
-			}
-
-			bool getMB() const {
-				return (headerFlags & 0x80)!=0;
-			}
-
-			void setME(bool value){
-				if(value)
-					headerFlags |= 0x40;
-				else
-					headerFlags &= 0xBF;
-			}
-
-			bool getME() const {
-				return (headerFlags & 0x40)!=0;
-			}
-
-			void setCF(bool value){
-				if(value)
-					headerFlags |= 0x20;
-				else
-					headerFlags &= 0xDF;
-			}
-
-			bool getCF() const {
-				return (headerFlags & 0x20)!=0;
-			}
-
-			void setSR(bool value){
-				if(value)
-					headerFlags |= 0x10;
-				else
-					headerFlags &= 0xCF;
-			}
-
-			bool getSR() const {
-				return (headerFlags & 0x10)!=0;
-			}
-
-			void setIL(bool value){
-				if(value)
-					headerFlags |= 0x08;
-				else
-					headerFlags &= 0xAF;
-			}
-
-			bool getIL() const {
-				return (headerFlags & 0x08)!=0;
-			}
-
-			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
-			}
 
-			TypeNameFormat_t getFNT() const {
-				return (TypeNameFormat_t)(headerFlags & 0x07);
-			}
-
-			void setPlayloadLenght(uint32_t lenght){
-				playloadLenght=lenght;
-				setSR(playloadLenght<=255);
-			}
-
-			uint32_t getPlayloadLenght()const{
-				return playloadLenght;
-			}
-
-			void setTypeLength(uint8_t size){
-				typeLenght=size;
-			}
-
-			uint8_t getTypeLenght()const{
-				return typeLenght;
-			}
-
-			uint16_t getRecordLength()const{
-				return (getSR() ? 3 : 6) + typeLenght+playloadLenght;
-			}
-
-			uint8_t writeHeader(uint8_t *outBuffer)const{
-
-				uint32_t index=0;
+	/**
+	 * enum used for identify the record type
+	 */
+	typedef enum {
+		TYPE_UNKNOWN,        //!< UNKNOWN record
+		TYPE_TEXT,           //!< TEXT
+		TYPE_AAR,            //!< Android Archive record
+		TYPE_MIME,           //!< generic MIME type
+		TYPE_URI,            //!< generic URI
+		TYPE_URI_MAIL,       //!< Email URI record
+		TYPE_URI_SMS,        //!< SMS URI record
+		TYPE_URI_GEOLOCATION,//!< position URI record
+		TYPE_MIME_VCARD      //!< VCard record
+	} RecordType_t;
 
-				outBuffer[index++]= headerFlags;
-				outBuffer[index++]= typeLenght;
-				if(getSR()){
-					outBuffer[index++]=(uint8_t) playloadLenght;
-				}else{
-					outBuffer[index++]=(uint8_t) ((playloadLenght & 0xFF000000)>>24);
-					outBuffer[index++]=(uint8_t) ((playloadLenght & 0x00FF0000)>>16);
-					outBuffer[index++]=(uint8_t) ((playloadLenght & 0x0000FF00)>>8);
-					outBuffer[index++]=(uint8_t)  (playloadLenght & 0x000000FF);
-				}//if-else
-				return index;
-			}//writeHeader
+	Record() {
+	}
 
-			uint16_t loadHeader(const uint8_t *const buffer){
-				uint32_t index=0;
-				headerFlags=buffer[index++];
-				typeLenght=buffer[index++];
-				if(getSR()){
-					playloadLenght=buffer[index++];
-				}else{
-					playloadLenght = (((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 typeLenght;
-			uint32_t playloadLenght;
-
-		};
-
-	typedef enum {
-		TYPE_UNKNOWN,
-		TYPE_TEXT,
-		TYPE_AAR,
-		TYPE_MIME,
-		TYPE_URI,
-		TYPE_URI_MAIL,
-		TYPE_SMS,
-		TYPE_GEOLOCATION,
-		TYPE_MIME_VCARD
-	}RecordType_t;
-
-	Record(){}
-
-	void setAsFirstRecord(){
+	/**
+	 * set the record as the first record in the message
+	 */
+	void setAsFirstRecord() {
 		mRecordHeader.setMB(true);
 	}
 
-	void setAsLastRecord(){
+	/**
+	 * set the record as the last record in the message
+	 */
+	void setAsLastRecord() {
 		mRecordHeader.setME(true);
 	}
 
-	bool isLastRecord()const{
+	/**
+	 * @return true if it is the last record in the message
+	 */
+	bool isLastRecord() const {
 		return mRecordHeader.getME();
 	}
 
-	void setAsMiddleRecord(){
+	/**
+	 * set the record as generic
+	 */
+	void setAsMiddleRecord() {
 		mRecordHeader.setMB(false);
 		mRecordHeader.setME(false);
 	}
 
-	virtual RecordType_t getType()const{
+	/**
+	 * get tag type
+	 * @return tag type if not overwrite it return TYPE_UNKNOWN
+	 */
+	virtual RecordType_t getType() const {
 		return TYPE_UNKNOWN;
-	}//getType
+	} //getType
 
-	virtual uint16_t getByteLenght(){
+	/**
+	 * number of byte needed for store this record
+	 * @return size header + size record content
+	 */
+	virtual uint16_t getByteLength() {
 		return mRecordHeader.getRecordLength();
 	}
 
+	/**
+	 * write the record content into a buffer
+	 * @param[out] buffer buffer where write the record content, the buffer size must be almost getByteLength() bytes
+	 * @return number of write bytes
+	 */
 	virtual uint16_t write(uint8_t *buffer)=0;
 
-	virtual ~Record(){};
+	virtual ~Record() {
+	};
 
 protected:
 	RecordHeader mRecordHeader;