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:
0:04b82ae7aa43
Child:
1:a0eeb478a45a
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Record.h	Thu Nov 19 08:49:47 2015 +0000
@@ -0,0 +1,189 @@
+/*
+ * Record.h
+ *
+ *  Created on: Nov 6, 2015
+ *      Author: giovanni visentini
+ */
+
+#ifndef NDEFLIB_RECORD_H_
+#define NDEFLIB_RECORD_H_
+#include <stdint.h>
+
+namespace NDefLib {
+
+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(){
+				return playloadLenght;
+			}
+
+			void setTypeLength(uint8_t size){
+				typeLenght=size;
+			}
+
+			uint16_t getRecordLength()const{
+				return (getSR() ? 3 : 6) + typeLenght+playloadLenght;
+			}
+
+			uint8_t writeHeader(uint8_t *outBuffer)const{
+
+				uint32_t index=0;
+
+				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
+
+			uint16_t loadHeader(uint8_t *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;
+
+		};
+
+	Record(){}
+
+	void setAsFirstRecord(){
+		mRecordHeader.setMB(true);
+	}
+
+	void setAsLastRecord(){
+		mRecordHeader.setME(true);
+	}
+
+	bool isLastRecord()const{
+		return mRecordHeader.getME();
+	}
+
+	void setAsMiddleRecord(){
+		mRecordHeader.setMB(false);
+		mRecordHeader.setME(false);
+	}
+
+	virtual uint16_t getByteLenght(){
+		return mRecordHeader.getRecordLength();
+	}
+
+	virtual uint16_t write(uint8_t *buffer)=0;
+
+	virtual ~Record(){};
+
+protected:
+	RecordHeader mRecordHeader;
+};
+
+} /* namespace NDefLib */
+
+#endif /* NDEFLIB_RECORD_H_ */