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.

RecordType/RecordURI.h

Committer:
giovannivisentini
Date:
2015-12-01
Revision:
4:eaf6c49a86e4
Parent:
1:a0eeb478a45a
Child:
5:f2b7efcc5b6e

File content as of revision 4:eaf6c49a86e4:

/*
 * RecordUri.h
 *
 *  Created on: Nov 6, 2015
 *      Author: giovanni visentini
 */

#ifndef NDEFLIB_RECORDTYPE_RECORDURI_H_
#define NDEFLIB_RECORDTYPE_RECORDURI_H_
#include <map>
#include <string>
#include <NDefLib/Record.h>

namespace NDefLib {

/**
 * base class for an uri record
 */
class RecordURI: public Record {

public:

	/**
	 * load a record uri from a buffer
	 * @param header record header already read
	 * @param buffer buffer where read the tag playload
	 * @return record or NULL if it was not possible build it
 	 * @par free the pointer return by this function
	 */
	static RecordURI* parse(const RecordHeader &header,
			const uint8_t *buffer);

	/**
	 * if you want encode an know uri you can use this define for
	 * avoid to encode the uri type as string
	 */
	typedef enum {
		UNKNOWN = 0X00,     //!< UNKNOWN
		HTTP_WWW = 0X01,    //!< HTTP_WWW
		HTTPS_WWW = 0X02,   //!< HTTPS_WWW
		HTTP = 0X03,        //!< HTTP
		HTTPS = 0X04,       //!< HTTPS
		TEL = 0x05,         //!< TEL
		MAIL = 0X06,        //!< MAIL
		FTP_ANONIMUS = 0X07,//!< FTP_ANONIMUS
		FTP_FTP = 0X08,     //!< FTP_FTP
		FTPS = 0X09,        //!< FTPS
		SFTP = 0X0A,        //!< SFTP
		SMB = 0X0B,         //!< SMB
		NFS = 0X0C,         //!< NFS
		FTP = 0X0d,         //!< FTP
		DAV = 0X0E,         //!< DAV
		NEWS = 0X0F,        //!< NEWS
		TELNET = 0X10,      //!< TELNET
		IMAP = 0X11,        //!< IMAP
		RTSP = 0X12,        //!< RTSP
		URN = 0X13,         //!< URN
		POP = 0X14,         //!< POP
		SIP = 0X15,         //!< SIP
		SIPS = 0X016,       //!< SIPS
		TFTP = 0X017,       //!< TFTP
		BTSPP = 0x018,      //!< BTSPP
		BTL2CAP = 0x019,    //!< BTL2CAP
		BTGOEP = 0X01A,     //!< BTGOEP
		TCPOBEX = 0X1B,     //!< TCPOBEX
		IRDAOBEX = 0X1C,    //!< IRDAOBEX
		FILE = 0X1D,        //!< FILE
		URN_EPC_ID = 0X1E,  //!< URN_EPC_ID
		URN_EPC_TAG = 0X1F, //!< URN_EPC_TAG
		URN_EPC_PAT = 0X20, //!< URN_EPC_PAT
		URN_EPC_RAW = 0X21, //!< URN_EPC_RAW
		URN_EPC = 0X22,     //!< URN_EPC
		URN_NFC = 0X23      //!< URN_NFC
	} knowUriId_t;

	/**
	 * build a know uri
	 * @param uriId uri type
	 * @param uriContent uri content
	 * @par we will remove from the content the prefix encoded as part of the know uri
	 */
	RecordURI(knowUriId_t uriId, const std::string &uriContent="");

	/**
	 * build an unknown uri type
	 * @param uriType string with the uri type
	 * @param uriContent uri content
	 */
	RecordURI(const std::string &uriType, const std::string &uriContent="");

	virtual uint16_t write(uint8_t *buffer);

	virtual RecordType_t getType() const {
		return TYPE_URI;
	} //getType

	knowUriId_t getUriId() const {
		return mUriTypeId;
	}

	virtual uint16_t getByteLength() {
		updateContentAndHeader();
		return mRecordHeader.getRecordLength();
	}

	std::string& getContent() {
		updateContentAndHeader();
		return mContent;
	}

	void setContent(const std::string &uri){
		if(mUriTypeId!=UNKNOWN)
			storeRemoveingPrefix(sKnowUriPrefix[mUriTypeId],uri);
		else
			mContent=uri;
		updateContentAndHeader();
	}

	const std::string& getUriType() const {
		return mTypeString;
	}

	virtual ~RecordURI() {
	};

protected:

	/**
	 * record id to write for be recognize as an uri record
	 */
	static const uint8_t sNDEFUriIdCode;

	/**
	 * a subclass must implement this function for store the tag content inside the mContent
	 * variable, in this way this class will handle to write the tag
	 */
	virtual void updateContent(){};

	/**
	 * the subclass have to store in this variable the content to write in the tag.
	 * this class will ask to update the content thought the updateContent call
	 */
	std::string mContent;

private:

	/**
	 * set the record header flags
	 */
	void setRecordHeader();

	/**
	 * update the tag content and update the header with the new content size
	 */
	void updateContentAndHeader(){
		updateContent();
		updateRecordHeader();
	}

	/**
	 * set the correct size of the plaload
	 */
	void updateRecordHeader(){
		//+1 = size of the uriTypeId
		mRecordHeader.setPlayloadLength(1 + mTypeString.size() + mContent.size());
	}

	void storeRemoveingPrefix(const std::string &prefix,const std::string &content){
		//check that the content doens't contain the prefix
		if (content.compare(0, prefix.size(), prefix) == 0) {
			mContent = std::string(content, prefix.size());
		} else
			mContent = content;
	}

	/**
	 * uri type used by this record
	 */
	const knowUriId_t mUriTypeId;

	/**
	 * in case of unknown uri type, it store the used define uri type
	 */
	const std::string mTypeString;


	/**
	 * array of know prefix for the know uri type
	 */
	static const std::string sKnowUriPrefix[];
};

} /* namespace NDefLib */

#endif /* NDEFLIB_RECORDTYPE_RECORDURI_H_ */