Firmware library for the X-NUCLEO-NFC01A1 Dynamic NFC Tag board.

Dependencies:   M24SR

Dependents:   NFC M2M_2016_STM32 MyongjiElec_capstone1 IDW01M1_Cloud_IBM ... more

Fork of X_NUCLEO_NFC01A1 by ST Expansion SW Team

X-NUCLEO-NFC01A1 Dynamic NFC Tag Expansion Board Firmware Package

Introduction

This firmware package includes Components Device Drivers, Board Support Package and example applications for STMicroelectronics X-NUCLEO-NFC01A1 Dynamic NFC Tag Expansion Board based on M24SR.

Firmware Library

Class X_NUCLEO_NFC01A1 is intended to represent the Dynamic NFC Tag Expansion Board with the same name.
It provides an API to access to the M24SR component and to the three onboard LEDs.
It is intentionally implemented as a singleton because only one X_NUCLEO_NFC01A1 at a time might be deployed in a HW component stack.
The library also provides an implementation of the NDEF library API for M24SR, providing an simple way to read/write NDEF formatted messages from/to the M24SR dynamic NFC tag.

Example applications

1. Hello World
2. Asynchronous Hello World

Revision:
21:ccc4f3fed4b3
Parent:
19:0b65a5813059
Child:
23:d07138541feb
--- a/Interfaces/Nfc_class.h	Thu Jan 28 16:38:30 2016 +0000
+++ b/Interfaces/Nfc_class.h	Fri Jan 29 15:06:30 2016 +0000
@@ -120,8 +120,9 @@
 			(void)nfc; (void)status; (void)writeByte; (void)nWriteByte; (void)offset;
 		}
 
-		virtual void onVerifyed(Nfc *nfc,NFC_StatusTypeDef status){
-			(void)nfc; (void)status;
+		virtual void onVerifyed(Nfc *nfc,NFC_StatusTypeDef status,PasswordType_t uPwdId,
+				const uint8_t *pwd){
+			(void)nfc; (void)status;(void)uPwdId;(void)pwd;
 		}
 
 		virtual void onManageI2CGPO(Nfc *nfc,NFC_StatusTypeDef status,NFC_GPO_MGMT newStatus){
@@ -154,7 +155,7 @@
 		}
 
 		virtual void onReadId(Nfc *nfc, NFC_StatusTypeDef status, uint8_t *id){
-			(void)nfc; (void)status;(void)id;
+					(void)nfc; (void)status;(void)id;
 		}
 
 		virtual void onEnableReadPassword(Nfc *nfc, NFC_StatusTypeDef status,const uint8_t *newPwd){
@@ -196,7 +197,8 @@
 		virtual ~Callback(){};
 	};
 
-	Nfc():mCallback(&defaultCallback){ }
+	Nfc():mCallback(&defaultCallback),
+			mComponentCallback(NULL){ }
 
 	/**
 	 * Open a I2C connection with the tag if an RF connection isn't already open.
@@ -208,7 +210,7 @@
 	 * Force to open an I2C connection , abort the RF connection.
 	 * @return NFC_SUCCESS if the session is acquired
 	 */
-	virtual NFC_StatusTypeDef KillSession(void) = 0;
+	virtual NFC_StatusTypeDef ForceGetSession(void) = 0;
 
 	/**
 	 * Close an I2C connection.
@@ -356,7 +358,7 @@
 	 * Change the function to call when a command ends
 	 * @param commandCallback object containings the callback, if NULL it will use empty callback
 	 */
-	void SetCallBack(Callback *commandCallback){
+	void SetCallback(Callback *commandCallback){
 		if(commandCallback!=NULL)
 			mCallback = commandCallback;
 		else
@@ -369,15 +371,459 @@
 	 */
 	virtual NFC_StatusTypeDef ManageEvent()=0;
 
+
+	///////////////////////HIGHT LEVEL /UTILITY FUNCTION ////////////////////////////////////
+
+	/**
+	 * Enable the request of a password before reading the tag.
+	 * @param  pCurrentWritePassword Current password
+	 * @param  pNewPassword Password to request before reading the tag.
+	 * @return return NFC_SUCCESS if no errors
+	 * @par The password must have a length of 16 chars.
+	 */
+	NFC_StatusTypeDef enableReadPassword(const uint8_t* pCurrentWritePassword,
+				const uint8_t* pNewPassword) {
+
+		//enable the callback for change the gpo
+		mComponentCallback = &mChangePasswordRequestStatusCallback;
+		mChangePasswordRequestStatusCallback.setTask(ReadPwd,pNewPassword);
+
+		return Verify(Nfc::WritePwd, 0x10, pCurrentWritePassword);
+	}
+
+	/**
+	 * Disable the request of a password before reading the tag.
+	 * @param  pCurrentWritePassword Current password
+	 * @return return NFC_SUCCESS if no errors
+	 * @par The password must have a length of 16 chars.
+	 */
+	NFC_StatusTypeDef disableReadPassword(const uint8_t* pCurrentWritePassword) {
+		mComponentCallback = &mChangePasswordRequestStatusCallback;
+		mChangePasswordRequestStatusCallback.setTask(ReadPwd,NULL);
+
+		return Verify(Nfc::WritePwd, 0x10, pCurrentWritePassword);
+	}
+
+	/**
+	 * Enable the request of a password before writing to the tag.
+	 * @param  pCurrentWritePassword Current password
+	 * @param  pNewPassword Password to request before reading the tag.
+	 * @return return NFC_SUCCESS if no errors
+	 * @par The password must have a length of 16 chars.
+	 */
+	NFC_StatusTypeDef enableWritePassword(const uint8_t* pCurrentWritePassword,
+				uint8_t* pNewPassword) {
+		//enable the callback for change the gpo
+		mComponentCallback = &mChangePasswordRequestStatusCallback;
+		mChangePasswordRequestStatusCallback.setTask(WritePwd,pNewPassword);
+
+		return Verify(Nfc::WritePwd, 0x10, pCurrentWritePassword);
+	}
+
+	/**
+	 * Disable the request of a password before writing the tag.
+	 * @param  pCurrentWritePassword Current password.
+	 * @return return NFC_SUCCESS if no errors
+	 * @par The password must have a length of 16 chars.
+	 */
+	NFC_StatusTypeDef disableWritePassword(const uint8_t* pCurrentWritePassword) {
+		mComponentCallback = &mChangePasswordRequestStatusCallback;
+		mChangePasswordRequestStatusCallback.setTask(WritePwd,NULL);
+
+		return Verify(Nfc::WritePwd, 0x10, pCurrentWritePassword);
+	}
+
+	/**
+	 * @brief   This function disables both read and write passwords.
+	 * @param	pSuperUserPassword I2C super user password.
+	 * @return  return NFC_SUCCESS if no errors
+	 * @par The password must have a length of 16 chars.
+	 */
+	NFC_StatusTypeDef disableAllPassword(const uint8_t* pSuperUserPassword){
+		mComponentCallback = &mRemoveAllPasswordCallback;
+		mRemoveAllPasswordCallback.setPwd(pSuperUserPassword);
+		return Verify(Nfc::I2CPwd, 0x10, pSuperUserPassword);
+	}
+
+	/**
+	 * @brief   This function enables read only mode.
+	 * @param	pCurrentWritePassword Write password is needed to enable read only mode.
+	 * @return  return NFC_SUCCESS if no errors
+	 * @par The password must have a length of 16 chars.
+	 */
+	NFC_StatusTypeDef enableReadOnly(const uint8_t* pCurrentWritePassword){
+
+		mComponentCallback = &mChangeAccessStateCallback;
+		//disable write = read only
+		mChangeAccessStateCallback.changeAccessState(ChangeAccessStateCallback::WRITE,false);
+
+		return Verify(Nfc::I2CPwd, 0x10, pCurrentWritePassword);
+	}
+
+	/**
+	 * @brief   This function disables read only mode.
+	 * @param	pCurrentWritePassword Write password is needed to disable read only mode.
+	 * @return  return NFC_SUCCESS if no errors
+	 * @par The password must have a length of 16 chars.
+	 */
+	NFC_StatusTypeDef disableReadOnly(const uint8_t* pCurrentWritePassword) {
+		mComponentCallback = &mChangeAccessStateCallback;
+		mChangeAccessStateCallback.changeAccessState(ChangeAccessStateCallback::WRITE,true);
+		return Verify(Nfc::I2CPwd, 0x10,pCurrentWritePassword);
+	}
+
+
+	/**
+	 * @brief   This function enables write only mode.
+	 * @param	pCurrentWritePassword Write password is needed to enable write only mode.
+	 * @return  return NFC_SUCCESS if no errors
+	 * @par The password must have a length of 16 chars.
+	 */
+	NFC_StatusTypeDef enableWriteOnly(const uint8_t* pCurrentWritePassword) {
+
+		mComponentCallback = &mChangeAccessStateCallback;
+		//disable read = enable write only
+		mChangeAccessStateCallback.changeAccessState(ChangeAccessStateCallback::READ,false);
+
+		return Verify(Nfc::I2CPwd, 0x10, pCurrentWritePassword);
+
+	}
+
+	/**
+	 * @brief   This function disables write only mode.
+	 * @param	pCurrentWritePassword Write password is needed to disable write only mode.
+	 * @return  return NFC_SUCCESS if no errors
+	 * @par The password must have a length of 16 chars.
+	 */
+	NFC_StatusTypeDef disableWriteOnly(const uint8_t* pCurrentWritePassword) {
+		mComponentCallback = &mChangeAccessStateCallback;
+		mChangeAccessStateCallback.changeAccessState(ChangeAccessStateCallback::READ,true);
+		return Verify(Nfc::I2CPwd, 0x10, pCurrentWritePassword);
+	}
+
 	virtual ~Nfc(){};
 
+protected:
+		/** object containing the callback to use*/
+		Callback *mCallback;
+
+		/**
+		 * Object with private callback used for hide height level commands
+		 * that require more than one low level command.
+		 * This object has an height priority comparing the user callback
+		 */
+		Nfc::Callback *mComponentCallback;
+
+		/**
+		 * get the callback object to use
+		 * @return callback object to use
+		 */
+		Nfc::Callback * getCallback(){
+			if(mComponentCallback!=NULL)
+				return mComponentCallback;
+			return mCallback;
+		}//getCallback
+
 private:
 	/** object containing empty callback to use in the default case*/
 	Callback defaultCallback;
 
-protected:
-	/** object containing the callback to use*/
-	Callback *mCallback;
+
+	/**
+	 * This class permit to enable/disable the password request for read/write into the tag
+	 * This class is equivalent to call the method:
+	 * To enable the request:
+	 * <ul>
+	 *   <li> Verify </li>
+	 *   <li> ChangeReferenceData </li>
+	 *   <li> EnablePermanentState </li>
+	 * </ul>
+	 * To disable the request:
+	 * <ul>
+	 *   <li> Verify </li>
+	 *   <li> DisableVerificationRequirement </li>
+	 * </ul>
+	 */
+	class ChangePasswordRequestStatusCallback : public Nfc::Callback{
+
+		public:
+
+			/**
+			 * Build the chain of callback.
+			 * @param parent object where send the command
+			 */
+			ChangePasswordRequestStatusCallback():
+				mNewPwd(NULL),mType(I2CPwd),mEnable(false){}
+
+			/**
+			 * Set the password to enable/disable.
+			 * @param type Type of password to enable/disable.
+			 * @param newPwd Array of 16bytes with the new password, if null the request will be disabled.
+			 */
+			void setTask(PasswordType_t type, const uint8_t *newPwd){
+				mNewPwd=newPwd;
+				mType=type;
+				mEnable=newPwd!=NULL;
+			}//setTask
+
+			virtual void onVerifyed(Nfc *nfc, NFC_StatusTypeDef status,PasswordType_t ,
+					const uint8_t *){
+				if(status!=NFC_SUCCESS)
+					return onFinishCommand(nfc,status);
+				if(mEnable)
+					nfc->ChangeReferenceData(mType,mNewPwd);
+				else
+					nfc->DisableVerificationRequirement(mType);
+			}
+
+			virtual void onDisableVerificationRequirement(Nfc *nfc,
+					NFC_StatusTypeDef status, PasswordType_t ){
+				onFinishCommand(nfc,status);
+			}
+
+			virtual void onChangeReferenceData(Nfc *nfc, NFC_StatusTypeDef status,
+					PasswordType_t type,
+					uint8_t *){
+				if(status==NFC_SUCCESS)
+					nfc->EnablePermanentState(type);
+				else
+					onFinishCommand(nfc,status);
+			}
+
+			virtual void onEnablePermanentState(Nfc *nfc, NFC_StatusTypeDef status,
+					PasswordType_t ){
+				onFinishCommand(nfc,status);
+			}
+
+
+		private:
+
+			/**
+			 * Remove the private callback and call the user callback.
+			 * @param nfc Object where the command is run.
+			 * @param status Command status.
+			 */
+			void onFinishCommand(Nfc *nfc,NFC_StatusTypeDef status){
+				nfc->mComponentCallback=NULL;
+
+				if(mEnable){
+					if(mType==ReadPwd){
+						nfc->getCallback()->onEnableReadPassword(nfc,status,mNewPwd);
+					}else
+						nfc->getCallback()->onEnableWritePassword(nfc,status,mNewPwd);
+				}else{
+					if(mType==ReadPwd){
+						nfc->getCallback()->onDisableReadPassword(nfc,status);
+					}else
+						nfc->getCallback()->onDisableWritePassword(nfc,status);
+				}//if-else enable
+			}//onFinish
+
+			const uint8_t *mNewPwd;
+			Nfc::PasswordType_t mType;
+			bool mEnable;
+
+		};
+
+		friend class ChangePasswordRequestStatusCallback;
+		/**
+		 * Object containing the callback chain needed to change the password request
+		 */
+		ChangePasswordRequestStatusCallback mChangePasswordRequestStatusCallback;
+
+		/**
+		 * This class permit to disable all the password request for read/write into the tag
+		 * This class is equivalent to call the methods:
+		 * <ul>
+		 *   <li> Verify(i2c) </li>
+		 *   <li> DisablePermanentState(Read) </li>
+		 *   <li> DisablePermanentState(write) </li>
+		 *   <li> DisableVerificationRequirement(Read) </li>
+		 *   <li> DisableVerificationRequirement(write) </li>
+		 *   <li> ChangeReferenceData(Read) </li>
+		 *   <li> ChangeReferenceData(write) </li>
+		 * </ul>
+		 */
+		class RemoveAllPasswordCallback : public Nfc::Callback{
+
+			const uint8_t *mI2CPwd;
+
+			public:
+
+				/**
+				 * Build the chain of callback.
+				 * @param parent object where send the command
+				 */
+				RemoveAllPasswordCallback():mI2CPwd(NULL){}
+
+				void setPwd(const uint8_t *pwd){
+					mI2CPwd = pwd;
+				}
+
+				virtual void onVerifyed(Nfc *nfc,NFC_StatusTypeDef status,
+						PasswordType_t,const uint8_t*){
+					if(status!=NFC_SUCCESS)
+						return onFinishCommand(nfc,status);
+					nfc->DisablePermanentState(ReadPwd);
+				}
+
+				virtual void onDisablePermanentState(Nfc *nfc , NFC_StatusTypeDef status,
+						PasswordType_t type){
+					if(status!=NFC_SUCCESS)
+						return onFinishCommand(nfc,status);
+					if(type==ReadPwd)
+						nfc->DisablePermanentState(WritePwd);
+					else
+						nfc->DisableVerificationRequirement(ReadPwd);
+				}
+
+				virtual void onDisableVerificationRequirement(Nfc *nfc ,
+						NFC_StatusTypeDef status,PasswordType_t type){
+					if(status!=NFC_SUCCESS)
+						return onFinishCommand(nfc,status);
+					if(type==ReadPwd)
+						nfc->DisableVerificationRequirement(WritePwd);
+					else
+						nfc->ChangeReferenceData(ReadPwd,mI2CPwd);
+				}
+
+				virtual void onChangeReferenceData(Nfc *nfc ,NFC_StatusTypeDef status,PasswordType_t type,
+						uint8_t *data){
+					if(status!=NFC_SUCCESS)
+						return onFinishCommand(nfc,status);
+					if(type==ReadPwd)
+						nfc->ChangeReferenceData(WritePwd,data);
+					else
+						onFinishCommand(nfc,status);
+				}
+
+			private:
+
+				/**
+				 * Remove the private callback and call the onDisableAllPassword callback.
+				 * @param nfc Object where the command is run.
+				 * @param status Command status.
+				 */
+				void onFinishCommand(Nfc *nfc,NFC_StatusTypeDef status){
+					nfc->mComponentCallback=NULL;
+					mI2CPwd=NULL;
+					nfc->getCallback()->onDisableAllPassword(nfc,status);
+				}//onFinish
+
+		};
+
+		friend class RemoveAllPasswordCallback;
+		/**
+		 * Object containing the callback chain needed remove the password request
+		 */
+		RemoveAllPasswordCallback mRemoveAllPasswordCallback;
+
+		/**
+		 * This class permit set the tag as read/write only
+		 * This class is equivalent to call the methods:
+		 * <ul>
+		 *   <li> Verify(i2c) </li>
+		 *   <li> EnablePermanentState(Read/write) </li>
+		 * </ul>
+		 * or:
+		 * <ul>
+		 *   <li> Verify(i2c) </li>
+		 *   <li> DisablePermanentState</li>
+		 *   <li> DisableVerificationRequirement(Read/write) </li>
+		 * </ul>
+		 */
+		class ChangeAccessStateCallback : public Nfc::Callback{
+
+			public:
+
+				typedef enum{
+					WRITE,
+					READ
+				}AccessType_t;
+
+				/**
+				 * Build the chain of callback.
+				 * @param parent object where send the command
+				 */
+				ChangeAccessStateCallback():mType(WRITE),mEnable(false){}
+
+				/**
+				 * Set the access to enable/disable
+				 * @param type Type to enable or disable
+				 * @param enable true for enable the state, false for disable it
+				 */
+				void changeAccessState(AccessType_t type,bool enable){
+					mType=type;
+					mEnable=enable;
+				}
+
+				virtual void onVerifyed(Nfc *nfc,NFC_StatusTypeDef status,
+						PasswordType_t,const uint8_t*){
+					if(status!=NFC_SUCCESS)
+						return onFinishCommand(nfc,status);
+
+					if(mEnable){
+						nfc->DisablePermanentState(mType==WRITE? WritePwd : ReadPwd);
+					}else
+						nfc->EnablePermanentState(mType==WRITE? WritePwd : ReadPwd);
+
+				}
+
+				virtual void onDisablePermanentState(Nfc *nfc, NFC_StatusTypeDef status,
+						PasswordType_t type ){
+					if(status!=NFC_SUCCESS)
+						return onFinishCommand(nfc,status);
+
+					nfc->DisableVerificationRequirement(type);
+				}
+
+				virtual void onDisableVerificationRequirement(Nfc *nfc , NFC_StatusTypeDef status,
+						PasswordType_t ){
+					onFinishCommand(nfc,status);
+				}
+
+				virtual void onEnablePermanentState(Nfc *nfc ,NFC_StatusTypeDef status,PasswordType_t ){
+					onFinishCommand(nfc,status);
+				}
+
+
+			private:
+
+				/**
+				 * Remove the private callback and call the user callback.
+				 * @param nfc Object where the command is run.
+				 * @param status Command status.
+				 */
+				void onFinishCommand(Nfc *nfc,NFC_StatusTypeDef status){
+					nfc->mComponentCallback=NULL;
+					if(mEnable){
+						if(mType==READ){
+							//enable read = disable write only
+							nfc->getCallback()->onDisableWriteOnly(nfc,status);
+						}else
+							//enable write = disable read only
+							nfc->getCallback()->onDisableReadOnly(nfc,status);
+					}else{
+						if(mType==WRITE){
+							//disable write = enable read only
+							nfc->getCallback()->onEnableReadOnly(nfc,status);
+						}else{
+							//
+							nfc->getCallback()->onEnableWriteOnly(nfc,status);
+						}
+					}//if-else enable
+				}//onFinish
+
+				AccessType_t mType;
+				bool mEnable;
+
+		};
+
+		friend class ChangeAccessStateCallback;
+		/**
+		 * Object containing the callback chain needed to change the access state
+		 */
+		ChangeAccessStateCallback mChangeAccessStateCallback;
 
 };