This application provides a set of demos with X-NUCLEO-NFC01A1 expansion board.

Dependencies:   NDefLib X_NUCLEO_NFC01A1 mbed

Fork of X-MBED-NFC1 by Giovanni Visentini

This application provides a set of demos with X-NUCLEO-NFC01A1 expansion board.

The available demos are:

  • SAMPLE_WRITE_URL: write a tag with the ST home page URL
  • SAMPLE_COUNT_CLICK: create a custom tag to count and report the user button clicks.
  • SAMPLE_WRITE_AND_CHANGE_ALL: write a tag with all the supported records and update the tag contents when the user button is pressed.
  • SAMPLE_LOCK_TAG_CONTENT: use the M24SR component API to set the NFC tag as read-only.

To enable the different demos comment/uncomment the SAMPLE_* macros provided in main.cpp .

Revision:
1:6d202b62ed68
Parent:
0:674813bd5ec9
Child:
2:0648c1561eb2
--- a/Type4NfcTagSTM24SR.h	Thu Nov 19 08:50:18 2015 +0000
+++ b/Type4NfcTagSTM24SR.h	Fri Nov 27 15:10:25 2015 +0000
@@ -10,22 +10,193 @@
 #include <stdint.h>
 #include "m24sr/m24sr_class.h"
 
-#include "Type4NfcTag.h"
+#include "NDefLib/Type4NfcTag.h"
 
-class Type4NfcTagSTM24SR: public Type4NfcTag {
+class Type4NfcTagSTM24SR: public NDefLib::Type4NfcTag {
 
 public:
-	Type4NfcTagSTM24SR(M24SR &device):mDevice(device),mSessionIsOpen(false),
-	mMaxReadBytes(0xFF),mMaxWriteBytes(0xFF){}
+	Type4NfcTagSTM24SR(M24SR &device) :
+			mDevice(device), mSessionIsOpen(false), mMaxReadBytes(0xFF), mMaxWriteBytes(
+					0xFF) {
+	}
 
-	virtual bool openSession(bool force=false);
+	virtual bool openSession(bool force = false);
 	virtual bool closeSession();
 
-	virtual bool read(void*){return false;};
-	virtual ~Type4NfcTagSTM24SR(){};
+	virtual ~Type4NfcTagSTM24SR() {
+		if (mSessionIsOpen)
+			closeSession();
+	}
+	;
+
+	bool enableReadPassword(const uint8_t* pCurrentWritePassword,
+			const uint8_t* pNewPassword) {
+		if (!mSessionIsOpen)
+			return false;
+
+		if (mDevice.Verify(M24SR::WritePwd, 0x10, pCurrentWritePassword)
+				== NFC_SUCCESS) {
+			/* Set new password */
+			if (mDevice.ChangeReferenceData(M24SR::ReadPwd, pNewPassword)
+					== NFC_SUCCESS)
+				return mDevice.EnableVerificationRequirement(M24SR::ReadPwd)
+						== NFC_SUCCESS;
+		} //else
+		/* M24SR already lock but password not known */
+		return false;
+
+	}
+
+	bool disableReadPassword(const uint8_t* pCurrentWritePassword) {
+		if (!mSessionIsOpen)
+			return false;
+
+		if (mDevice.Verify(M24SR::WritePwd, 0x10, pCurrentWritePassword)
+				== NFC_SUCCESS) {
+			/* Set new password */
+			return mDevice.DisableVerificationRequirement(M24SR::ReadPwd)
+					== NFC_SUCCESS;
+		} else {
+			/* M24SR already lock but password not known */
+			return false;
+		}
+	}
+
+	bool enableWritePassword(const uint8_t* pCurrentWritePassword,
+			uint8_t* pNewPassword) {
+		if (!mSessionIsOpen)
+			return false;
+
+		/* check we have the good password */
+		if (mDevice.Verify(M24SR::WritePwd, 0x10, pCurrentWritePassword)
+				== NFC_SUCCESS) {
+			/* Set new password */
+			if (mDevice.ChangeReferenceData(M24SR::WritePwd, pNewPassword)
+					== NFC_SUCCESS)
+				;
+			return mDevice.EnableVerificationRequirement(M24SR::WritePwd)
+					== NFC_SUCCESS;
+		}
+		return false;
+	}
+
+	bool disableWritePassword(const uint8_t* pCurrentWritePassword) {
+		if (!mSessionIsOpen)
+			return false;
+
+		if (mDevice.Verify(M24SR::WritePwd, 0x10, pCurrentWritePassword)
+				== NFC_SUCCESS)
+			return mDevice.DisableVerificationRequirement(M24SR::WritePwd)
+					== NFC_SUCCESS;
+		return false;
+	}
+
+	/**
+	 * @brief  This fonction desactivate the need of read and write password for next access
+	 * @param	pSuperUserPassword : I2C super user password to overwrite read and write password
+	 * @retval SUCCESS : M24SR access is now free (no password needed)
+	 * @retval ERROR : operation does not complete
+	 */
+	bool disableAllPassword(const uint8_t* pSuperUserPassword) {
+		if (!mSessionIsOpen)
+			return false;
+
+		if (mDevice.Verify(M24SR::WritePwd, 0x10, pSuperUserPassword)
+				== NFC_SUCCESS) {
+			mDevice.DisablePermanentState(M24SR::ReadPwd);
+			mDevice.DisablePermanentState(M24SR::WritePwd);
+
+			mDevice.DisableVerificationRequirement(M24SR::ReadPwd);
+			mDevice.DisableVerificationRequirement(M24SR::WritePwd);
+
+			/* reset password */
+			mDevice.ChangeReferenceData(M24SR::ReadPwd, pSuperUserPassword);
+			mDevice.ChangeReferenceData(M24SR::WritePwd, pSuperUserPassword);
+			return true;
+		}
+		return false;
+	}
+
+	/**
+	 * @brief  This fonction enable read only mode
+	 * @param	pCurrentWritePassword : Write password is needed to have right to enable read only mode
+	 * @retval SUCCESS : M24SR access is now forbidden in write mode
+	 * @retval ERROR : operation does not complete
+	 */
+	bool enableReadOnly(const uint8_t* pCurrentWritePassword) {
+		if (!mSessionIsOpen)
+			return false;
+
+		if (mDevice.Verify(M24SR::WritePwd, 0x10, pCurrentWritePassword)
+				== NFC_SUCCESS) {
+			/* lock write to have read only */
+			return mDevice.EnablePermanentState(M24SR::WritePwd) == NFC_SUCCESS;
+		}
+		return false;
+	}
+
+	/**
+	 * @brief  This fonction disable read only mode
+	 * @param	pCurrentWritePassword : Write password is needed to have right to disable read only mode
+	 * @retval SUCCESS : M24SR write access is now allowed
+	 * @retval ERROR : operation does not complete
+	 */
+	bool disableReadOnly() {
+		if (!mSessionIsOpen)
+			return false;
+
+		if (mDevice.Verify(M24SR::I2CPwd, 0x10, M24SR::DEFAULT_PASSWORD)
+				== NFC_SUCCESS) {
+			/* disable write protection to disable read only mode */
+			if (mDevice.DisablePermanentState(M24SR::WritePwd) == NFC_SUCCESS)
+				return mDevice.DisableVerificationRequirement(M24SR::WritePwd)
+						== NFC_SUCCESS;
+		} //if
+		return false;
+	}
+
+	/**
+	 * @brief  This fonction enable write only mode
+	 * @param	pCurrentWritePassword : Write password is needed to have right to enable write only mode
+	 * @retval SUCCESS : M24SR access is now forbidden in read mode
+	 * @retval ERROR : operation does not complete
+	 */
+	bool enableWriteOnly(const uint8_t* pCurrentWritePassword) {
+		if (!mSessionIsOpen)
+			return false;
+
+		if (mDevice.Verify(M24SR::WritePwd, 0x10, pCurrentWritePassword)
+				== NFC_SUCCESS) {
+			/* disable read access and keep write */
+			return mDevice.EnablePermanentState(M24SR::ReadPwd) == NFC_SUCCESS;
+		}
+		return false;
+	}
+
+	/**
+	 * @brief  This fonction disable write only mode
+	 * @param	pCurrentWritePassword : Write password is needed to have right to disable write only mode
+	 * @retval SUCCESS : M24SR read access is now allowed
+	 * @retval ERROR : operation does not complete
+	 */
+	bool disableWriteOnly() {
+		if (!mSessionIsOpen)
+			return false;
+
+		if (mDevice.Verify(M24SR::I2CPwd, 0x10, M24SR::DEFAULT_PASSWORD)
+				== NFC_SUCCESS) {
+			/* disable write only -> enable write acces */
+			if (mDevice.DisablePermanentState(M24SR::ReadPwd) == NFC_SUCCESS)
+				return mDevice.DisableVerificationRequirement(M24SR::ReadPwd)
+						== NFC_SUCCESS;
+		}
+		return false;
+	}
 
 protected:
-	virtual bool writeByte(uint8_t *buffer,uint16_t lenght);
+	virtual bool writeByte(uint8_t *buffer, const uint16_t lenght);
+	virtual bool readByte(const uint16_t byteOffset, const uint16_t byteLength,
+			uint8_t *buffer);
 
 private:
 
@@ -36,4 +207,3 @@
 };
 
 #endif /* TYPE4NFCTAGSTM24SR_H_ */
-