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
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
Diff: m24sr/m24sr_class.cpp
- Revision:
- 19:0b65a5813059
- Parent:
- 18:10def2fefa8a
- Child:
- 20:aad5727cb8c6
--- a/m24sr/m24sr_class.cpp Fri Jan 22 09:04:51 2016 +0000
+++ b/m24sr/m24sr_class.cpp Thu Jan 28 14:01:18 2016 +0000
@@ -51,6 +51,8 @@
#endif
#define GPIO_PIN_SET (1)
+#define M24SR_MAX_BYTE_OPERATION_LENGHT (246)
+
/**
* default password, also used to enable super user mode throught the I2C channel
*/
@@ -71,10 +73,9 @@
/** value returned by the NFC chip when a command is successfully completed */
#define NFC_COMMAND_SUCCESS 0x9000
-
+//TODO remove errchek from the async function
/** call the fCall funtion and check that the return status is NFC_SUCCESS,
* otherwise return the error status*/
- //printf("call status: %x\n\r",status);
#define errchk(fCall) {\
const NFC_StatusTypeDef status = (fCall); \
if((status!=NFC_SUCCESS)) \
@@ -236,7 +237,7 @@
* @param FWTbyte : FWT value
* @return NFC_SUCCESS if no errors
*/
-NFC_StatusTypeDef M24SR::M24SR_FWTExtension(uint8_t FWTbyte) {
+NFC_StatusTypeDef M24SR::M24SR_SendFWTExtension(uint8_t FWTbyte) {
uint8_t pBuffer[M24SR_STATUSRESPONSE_NBBYTE];
NFC_StatusTypeDef status;
uint8_t NthByte = 0;
@@ -252,20 +253,44 @@
pBuffer[NthByte++] = GETMSB(uCRC16);
/* send the request */
- errchk(M24SR_IO_SendI2Ccommand(NthByte, pBuffer));
- errchk(M24SR_IO_IsAnswerReady());
- /* read the response */
- errchk(
- M24SR_IO_ReceiveI2Cresponse (M24SR_STATUSRESPONSE_NBBYTE , pBuffer ));
+ status = M24SR_IO_SendI2Ccommand(NthByte, pBuffer);
+ if(status != NFC_SUCCESS)
+ return status;
+
+ mLastCommandSend=UPDATE;
+
+ if(mCommunicationType==M24SR::SYNC){
+ status = M24SR_IO_PollI2C();
+ if(status == NFC_SUCCESS)
+ return M24SR_ReceiveUpdateBinary();
+ else{
+ mLastCommandSend = NONE;
+ getCallback()->onUpdatedBinary(this,status,mLastCommandData.offset,
+ mLastCommandData.data,mLastCommandData.length);
+ return status;
+ }//if-else
+ }//if
- status = M24SR_IsCorrectCRC16Residue(pBuffer, M24SR_STATUSRESPONSE_NBBYTE);
+ return NFC_SUCCESS;
+}
- if(status == NFC_COMMAND_SUCCESS)
- return NFC_SUCCESS;
- else
- return status;
+/**
+ * @brief This function initialize the M24SR device
+ * @retval None
+ */
+NFC_StatusTypeDef M24SR::M24SR_Init(M24SR_InitTypeDef *notUsed) {
+ (void) notUsed;
+ //force to open a i2c session
+ errchk(M24SR_KillSession())
+ //leave the gpo always up
+ errchk(M24SR_ManageI2CGPO(DEFAULT_GPO_STATUS))
+ //close the session
+ errchk(M24SR_Deselect())
+ mGpoEventInterrupt.enable_irq();
+ return NFC_SUCCESS;
}
+
/**
* @brief This function sends the KillSession command to the M24SR device
* @param None
@@ -273,15 +298,21 @@
*/
NFC_StatusTypeDef M24SR::M24SR_KillSession(void) {
uint8_t commandBuffer[] = M24SR_KILLSESSION_COMMAND;
-
- errchk(M24SR_IO_SendI2Ccommand(sizeof(commandBuffer), commandBuffer));
+ NFC_StatusTypeDef status;
+ status = M24SR_IO_SendI2Ccommand(sizeof(commandBuffer), commandBuffer);
+ if(status!=NFC_SUCCESS){
+ mCallback->onSessionOpen(this,status);
+ return status;
+ }
/* Insure no access will be done just after open session */
/* The only way here is to poll I2C to know when M24SR is ready */
/* GPO can not be use with KillSession command */
- errchk(M24SR_IO_PollI2C());
+ status = M24SR_IO_PollI2C();
- return NFC_SUCCESS;
+ getCallback()->onSessionOpen(this,status);
+ return status;
+
}
/**
@@ -290,31 +321,76 @@
*/
NFC_StatusTypeDef M24SR::M24SR_Deselect(void) {
uint8_t pBuffer[] = M24SR_DESELECTREQUEST_COMMAND;
-
+ NFC_StatusTypeDef status;
/* send the request */
- errchk(M24SR_IO_SendI2Ccommand(sizeof(pBuffer), pBuffer));
+ status = M24SR_IO_SendI2Ccommand(sizeof(pBuffer), pBuffer);
+ if(status!=NFC_SUCCESS){
+ getCallback()->onDeselect(this,status);
+ }
+
+ mLastCommandSend=DESELECT;
- errchk(M24SR_IO_IsAnswerReady());
- /* flush the M24SR buffer */
- errchk(M24SR_IO_ReceiveI2Cresponse(sizeof(pBuffer), pBuffer));
+ if(mCommunicationType==M24SR::SYNC){
+ status = M24SR_IO_PollI2C();
+ if(status == NFC_SUCCESS)
+ return M24SR_ReceiveDeselect();
+ else{
+ mLastCommandSend = NONE;
+ getCallback()->onSelectedApplication(this,status);
+ return status;
+ }//if-else
+
+ }
return NFC_SUCCESS;
}
+NFC_StatusTypeDef M24SR::M24SR_ReceiveDeselect(void){
+ uint8_t pBuffer[4];
+ NFC_StatusTypeDef status;
+ status = M24SR_IO_ReceiveI2Cresponse(sizeof(pBuffer), pBuffer);
+ getCallback()->onDeselect(this,status);
+ return status;
+}
+
+
+
+/**
+ * @brief This function sends the GetSession command to the M24SR device
+ * @retval NFC_SUCCESS the function is successful.
+ * @retval Status (SW1&SW2) if operation does not complete.
+ */
+NFC_StatusTypeDef M24SR::M24SR_GetSession(void) {
+ uint8_t commandBuffer[] = M24SR_OPENSESSION_COMMAND;
+
+ NFC_StatusTypeDef status;
+ status = M24SR_IO_SendI2Ccommand(sizeof(commandBuffer), commandBuffer);
+ if(status!=NFC_SUCCESS){
+ getCallback()->onSessionOpen(this,status);
+ return status;
+ }
+
+ /* Insure no access will be done just after open session */
+ /* The only way here is to poll I2C to know when M24SR is ready */
+ /* GPO can not be use with KillSession command */
+ status = M24SR_IO_PollI2C();
+
+ getCallback()->onSessionOpen(this,status);
+ return status;
+}
+
/**
* @brief This function sends the SelectApplication command
* @return NFC_SUCCESS if no errors
*/
-NFC_StatusTypeDef M24SR::M24SR_SelectApplication(void) {
+NFC_StatusTypeDef M24SR::M24SR_SendSelectApplication(void) {
C_APDU command;
+ NFC_StatusTypeDef status;
uint8_t *pBuffer = uM24SRbuffer;
- uint8_t pDataIn[M24SR_STATUSRESPONSE_NBBYTE];
uint8_t pDataOut[] = M24SR_SELECTAPPLICATION_COMMAND;
uint8_t uLe = 0x00;
- NFC_StatusTypeDef status;
- uint16_t uP1P2 =0x0400,
- NbByte;
+ uint16_t uP1P2 =0x0400, NbByte;
/* build the command */
command.Header.CLA = C_APDU_CLA_DEFAULT;
@@ -328,73 +404,64 @@
command.Body.pData = pDataOut;
/* copy the number of byte to read */
command.Body.LE = uLe;
- /* build the I²C command */
+ /* build the I2C command */
M24SR_BuildIBlockCommand( M24SR_CMDSTRUCT_SELECTAPPLICATION, &command,
uDIDbyte, &NbByte, pBuffer);
/* send the request */
- errchk(M24SR_IO_SendI2Ccommand(NbByte, pBuffer));
- errchk(M24SR_IO_IsAnswerReady());
- /* read the response */
- errchk(M24SR_IO_ReceiveI2Cresponse(sizeof(pDataIn), pDataIn));
-
+ status = M24SR_IO_SendI2Ccommand(NbByte, pBuffer);
+ if(status != NFC_SUCCESS){
+ getCallback()->onSelectedApplication(this,status);
+ return status;
+ }
+
+ mLastCommandSend=SELECT_APPLICATION;
+
+ if(mCommunicationType==M24SR::SYNC){
+ status = M24SR_IO_PollI2C();
+ if(status == NFC_SUCCESS)
+ return M24SR_ReceiveSelectApplication();
+ else{
+ mLastCommandSend = NONE;
+ getCallback()->onSelectedApplication(this,status);
+ return status;
+ }//if-else
+ }//if
+
+ return NFC_SUCCESS;
+}
+
+NFC_StatusTypeDef M24SR::M24SR_ReceiveSelectApplication(void) {
+
+ uint8_t pDataIn[M24SR_STATUSRESPONSE_NBBYTE];
+ NFC_StatusTypeDef status;
+
+ mLastCommandSend = NONE;
+
+ status = M24SR_IO_ReceiveI2Cresponse(sizeof(pDataIn), pDataIn);
+ if(status!=NFC_SUCCESS){
+ getCallback()->onSelectedApplication(this,status);
+ return status;
+ }//else
status= M24SR_IsCorrectCRC16Residue(pDataIn, sizeof(pDataIn));
- //printf("M24SR_IsCorrectCRC16Residue %x\r\n",status);
-
- if(status == NFC_COMMAND_SUCCESS)
- return NFC_SUCCESS;
- else
- return status;
+ getCallback()->onSelectedApplication(this,status);
+ return status;
}
-/**
- * @brief This function initialize the M24SR device
- * @retval None
- */
-NFC_StatusTypeDef M24SR::M24SR_Init(M24SR_InitTypeDef *notUsed) {
- (void) notUsed;
- //until we don't set the gpo usage, just do a polling on the
- //i2c read
- M24SR_IO_SetI2CSynchroMode(M24SR_WAITINGTIME_POLLING);
- //force to open a i2c session
- errchk(M24SR_KillSession())
- //ask to set the gpo low when the answer is ready, so we can use
- //the interrupt for polling
- errchk(M24SR_ManageI2CGPO(I2C_ANSWER_READY))
- //close the session
- errchk(M24SR_Deselect())
- return NFC_SUCCESS;
-}
NFC_StatusTypeDef M24SR::M24SR_ReadID(uint8_t *nfc_id) {
if (!nfc_id) {
return NFC_ERROR;
}
- errchk(M24SR_SelectApplication())
-
- return M24SR_ReadBinary(0x0011, 1, nfc_id);
-}
+ //enable the callback for change the gpo
+ mComponentCallback = &mReadIDCallback;
+ mReadIDCallback.readIdOn(nfc_id);
-/**
- * @brief This function sends the GetSession command to the M24SR device
- * @retval NFC_SUCCESS the function is successful.
- * @retval Status (SW1&SW2) if operation does not complete.
- */
-NFC_StatusTypeDef M24SR::M24SR_GetSession(void) {
- uint8_t commandBuffer[] = M24SR_OPENSESSION_COMMAND;
-
- errchk(M24SR_IO_SendI2Ccommand(sizeof(commandBuffer), commandBuffer));
-
- /* Insure no access will be done just after open session */
- /* The only way here is to poll I2C to know when M24SR is ready */
- /* GPO can not be use with GetSession command */
- errchk(M24SR_IO_PollI2C());
-
- return NFC_SUCCESS;
-
+ //start the readID procedure
+ return M24SR_SendSelectApplication();
}
/**
@@ -403,13 +470,11 @@
* @retval M24SR_ERROR_I2CTIMEOUT I2C timeout occurred.
* @retval Status (SW1&SW2) if operation does not complete for another reason.
*/
-NFC_StatusTypeDef M24SR::M24SR_SelectCCfile(void) {
+NFC_StatusTypeDef M24SR::M24SR_SendSelectCCfile(void) {
C_APDU command;
-
+ NFC_StatusTypeDef status;
uint8_t *pBuffer = uM24SRbuffer;
- uint8_t pDataIn[M24SR_STATUSRESPONSE_NBBYTE];
uint8_t pDataOut[] = CC_FILE_ID_BYTES;
- NFC_StatusTypeDef status;
uint16_t uP1P2 =0x000C,
NbByte;
@@ -427,17 +492,45 @@
&NbByte, pBuffer);
/* send the request */
- errchk(M24SR_IO_SendI2Ccommand(NbByte, pBuffer));
- errchk(M24SR_IO_IsAnswerReady());
- /* read the response */
- errchk(M24SR_IO_ReceiveI2Cresponse(sizeof(pDataIn), pDataIn));
+ status = M24SR_IO_SendI2Ccommand(NbByte, pBuffer);
+ if(status!=NFC_SUCCESS){
+ getCallback()->onSelectedCCFile(this,status);
+ return status;
+ }//else
+
+
+ mLastCommandSend=SELECT_CC_FILE;
+
+ if(mCommunicationType==M24SR::SYNC){
+ status = M24SR_IO_PollI2C();
+ if(status==NFC_SUCCESS)
+ return M24SR_ReceiveSelectCCfile();
+ else{
+ mLastCommandSend = NONE;
+ getCallback()->onSelectedCCFile(this,status);
+ return status;
+ }//if-else
+ }//if
- status = M24SR_IsCorrectCRC16Residue(pDataIn, sizeof(pDataIn));
+ return NFC_SUCCESS;
+}
+
+NFC_StatusTypeDef M24SR::M24SR_ReceiveSelectCCfile(void){
+
+ uint8_t pDataIn[M24SR_STATUSRESPONSE_NBBYTE];
+ NFC_StatusTypeDef status;
+
+ mLastCommandSend = NONE;
- if(status == NFC_COMMAND_SUCCESS)
- return NFC_SUCCESS;
- else
+ status = M24SR_IO_ReceiveI2Cresponse(sizeof(pDataIn), pDataIn);
+ if(status!=NFC_SUCCESS){
+ getCallback()->onSelectedCCFile(this,status);
return status;
+ }//else
+ status= M24SR_IsCorrectCRC16Residue(pDataIn, sizeof(pDataIn));
+ getCallback()->onSelectedCCFile(this,status);
+ return status;
+
}
/**
@@ -445,15 +538,13 @@
* @retval Status (SW1&SW2) Status of the operation to complete.
* @retval M24SR_ERROR_I2CTIMEOUT I2C timeout occurred.
*/
-NFC_StatusTypeDef M24SR::M24SR_SelectSystemfile(void) {
+NFC_StatusTypeDef M24SR::M24SR_SendSelectSystemfile(void) {
C_APDU command;
uint8_t *pBuffer = uM24SRbuffer;
- uint8_t pDataIn[M24SR_STATUSRESPONSE_NBBYTE];
uint8_t pDataOut[] = SYSTEM_FILE_ID_BYTES;
NFC_StatusTypeDef status;
- uint16_t uP1P2 =0x000C,
- NbByte;
+ uint16_t uP1P2 =0x000C, NbByte;
/* build the command */
command.Header.CLA = C_APDU_CLA_DEFAULT;
@@ -469,17 +560,44 @@
&NbByte, pBuffer);
/* send the request */
- errchk(M24SR_IO_SendI2Ccommand(NbByte, pBuffer));
- errchk(M24SR_IO_IsAnswerReady());
- /* read the response */
- errchk(M24SR_IO_ReceiveI2Cresponse(sizeof(pDataIn), pDataIn));
+ status = M24SR_IO_SendI2Ccommand(NbByte, pBuffer);
+ if(status!=NFC_SUCCESS){
+ getCallback()->onSelectedSystemFile(this,status);
+ return status;
+ }//else
+
+ mLastCommandSend=SELECT_SYSTEM_FILE;
+
+ if(mCommunicationType==M24SR::SYNC){
+ status = M24SR_IO_PollI2C();
+ if(status == NFC_SUCCESS)
+ return M24SR_ReceiveSelectSystemfile();
+ else{
+ mLastCommandSend = NONE;
+ getCallback()->onSelectedSystemFile(this,status);
+ return status;
+ }//if-else
+ }//if
- status = M24SR_IsCorrectCRC16Residue(pDataIn, sizeof(pDataIn));
+ return NFC_SUCCESS;
+}
+
+NFC_StatusTypeDef M24SR::M24SR_ReceiveSelectSystemfile(){
+
+ uint8_t pDataIn[M24SR_STATUSRESPONSE_NBBYTE];
+ NFC_StatusTypeDef status;
+
+ mLastCommandSend = NONE;
- if(status == NFC_COMMAND_SUCCESS)
- return NFC_SUCCESS;
- else
+ status = M24SR_IO_ReceiveI2Cresponse(sizeof(pDataIn), pDataIn);
+ if(status!=NFC_SUCCESS){
+ getCallback()->onSelectedSystemFile(this,status);
return status;
+ }//else
+ status= M24SR_IsCorrectCRC16Residue(pDataIn, sizeof(pDataIn));
+ getCallback()->onSelectedSystemFile(this,status);
+ return status;
+
}
/**
@@ -487,12 +605,11 @@
* @retval Status (SW1&SW2) Status of the operation to complete.
* @retval M24SR_ERROR_I2CTIMEOUT I2C timeout occurred.
*/
-NFC_StatusTypeDef M24SR::M24SR_SelectNDEFfile(uint16_t NDEFfileId) {
+NFC_StatusTypeDef M24SR::M24SR_SendSelectNDEFfile(uint16_t NDEFfileId) {
C_APDU command;
+ NFC_StatusTypeDef status;
uint8_t *pBuffer = uM24SRbuffer;
- uint8_t pDataIn[M24SR_STATUSRESPONSE_NBBYTE];
uint8_t pDataOut[] = { GETMSB(NDEFfileId), GETLSB(NDEFfileId) };
- NFC_StatusTypeDef status;
uint16_t uP1P2 = 0x000C, NbByte;
/* build the command */
@@ -505,21 +622,48 @@
command.Body.LC = sizeof(pDataOut);
command.Body.pData = pDataOut;
/* copy the offset */
- /* build the I²C command */
+ /* build the I2C command */
M24SR_BuildIBlockCommand(M24SR_CMDSTRUCT_SELECTNDEFFILE, &command, uDIDbyte,
&NbByte, pBuffer);
/* send the request */
- errchk(M24SR_IO_SendI2Ccommand(NbByte, pBuffer));
- errchk(M24SR_IO_IsAnswerReady());
- /* read the response */
- errchk(M24SR_IO_ReceiveI2Cresponse(sizeof(pDataIn), pDataIn));
+ status = M24SR_IO_SendI2Ccommand(NbByte, pBuffer);
+ if(status!=NFC_SUCCESS){
+
+ }
+
+ mLastCommandSend=SELECT_NDEF_FILE;
+
+ if(mCommunicationType==M24SR::SYNC){
+ status = M24SR_IO_PollI2C();
+ if(status==NFC_SUCCESS)
+ return M24SR_ReceiveSelectNDEFfile();
+ else{
+ mLastCommandSend = NONE;
+ getCallback()->onSelectedNDEFFile(this,status);
+ return status;
+ }
+ }
- status = M24SR_IsCorrectCRC16Residue(pDataIn, sizeof(pDataIn));
- if(status == NFC_COMMAND_SUCCESS)
- return NFC_SUCCESS;
- else
+ return NFC_SUCCESS;
+
+}
+
+NFC_StatusTypeDef M24SR::M24SR_ReceiveSelectNDEFfile(){
+
+ uint8_t pDataIn[M24SR_STATUSRESPONSE_NBBYTE];
+ NFC_StatusTypeDef status;
+
+ mLastCommandSend = NONE;
+
+ status = M24SR_IO_ReceiveI2Cresponse(sizeof(pDataIn), pDataIn);
+ if(status!=NFC_SUCCESS){
+ getCallback()->onSelectedNDEFFile(this,status);
return status;
+ }//else
+ status= M24SR_IsCorrectCRC16Residue(pDataIn, sizeof(pDataIn));
+ getCallback()->onSelectedNDEFFile(this,status);
+ return status;
}
@@ -531,12 +675,16 @@
* @retval Status (SW1&SW2) Status of the operation to complete.
* @retval M24SR_ERROR_I2CTIMEOUT I2C timeout occurred.
*/
-NFC_StatusTypeDef M24SR::M24SR_ReadBinary(uint16_t Offset, uint8_t NbByteToRead,
+NFC_StatusTypeDef M24SR::M24SR_SendReadBinary(uint16_t Offset, uint8_t NbByteToRead,
uint8_t *pBufferRead) {
+ //clamp the buffer to the max size
+ if(NbByteToRead>M24SR_MAX_BYTE_OPERATION_LENGHT)
+ NbByteToRead=M24SR_MAX_BYTE_OPERATION_LENGHT;
+
C_APDU command;
uint8_t *pBuffer = uM24SRbuffer;
+ uint16_t NbByte;
NFC_StatusTypeDef status;
- uint16_t NbByte;
/* build the command */
command.Header.CLA = C_APDU_CLA_DEFAULT;
@@ -550,18 +698,53 @@
M24SR_BuildIBlockCommand(M24SR_CMDSTRUCT_READBINARY, &command, uDIDbyte,
&NbByte, pBuffer);
- errchk(M24SR_IO_SendI2Ccommand(NbByte, pBuffer));
- errchk(M24SR_IO_IsAnswerReady());
- errchk(
- M24SR_IO_ReceiveI2Cresponse (NbByteToRead + M24SR_STATUSRESPONSE_NBBYTE , pBuffer ));
+ status = M24SR_IO_SendI2Ccommand(NbByte, pBuffer);
+ if(status!=NFC_SUCCESS){
+ getCallback()->onReadByte(this,status,Offset,pBufferRead,NbByteToRead);
+ return status;
+ }
+
+ mLastCommandSend=READ;
+ mLastCommandData.data=pBufferRead;
+ mLastCommandData.length=NbByteToRead;
+
+ if(mCommunicationType==M24SR::SYNC){
+ status = M24SR_IO_PollI2C();
+ if(status==NFC_SUCCESS){
+ return M24SR_ReceiveReadBinary();
+ }else{
+ mLastCommandSend = NONE;
+ getCallback()->onReadByte(this,status,Offset,pBufferRead,NbByteToRead);
+ return status;
+ }//if-else
+ }//if
+
+ return NFC_SUCCESS;
+}
- status = M24SR_IsCorrectCRC16Residue(pBuffer,
- NbByteToRead + M24SR_STATUSRESPONSE_NBBYTE);
- if(status != NFC_COMMAND_SUCCESS)
+NFC_StatusTypeDef M24SR::M24SR_ReceiveReadBinary(){
+
+ NFC_StatusTypeDef status;
+ const uint16_t length = mLastCommandData.length;
+ const uint16_t offset = mLastCommandData.offset;
+ uint8_t *data = mLastCommandData.data;
+
+ mLastCommandSend=NONE;
+
+ status = M24SR_IO_ReceiveI2Cresponse (length + M24SR_STATUSRESPONSE_NBBYTE , uM24SRbuffer );
+ if(status!=NFC_SUCCESS){
+ getCallback()->onReadByte(this,status,offset,data,length);
return status;
- /* retrieve the data without SW1 & SW2 as provided as return value of the function */
- memcpy(pBufferRead, &pBuffer[1], NbByteToRead);
- return NFC_SUCCESS;
+ }
+ status = M24SR_IsCorrectCRC16Residue(uM24SRbuffer, length + M24SR_STATUSRESPONSE_NBBYTE);
+ if(status != NFC_SUCCESS)
+ getCallback()->onReadByte(this,status,offset,data,length);
+ else{
+ /* retrieve the data without SW1 & SW2 as provided as return value of the function */
+ memcpy(mLastCommandData.data, &uM24SRbuffer[1], length);
+ getCallback()->onReadByte(this,status,offset,data,length);
+ }
+ return status;
}
@@ -573,12 +756,16 @@
* @retval Status (SW1&SW2) Status of the operation to complete.
* @retval M24SR_ERROR_I2CTIMEOUT I2C timeout occurred.
*/
-NFC_StatusTypeDef M24SR::M24SR_STReadBinary(uint16_t Offset,
+NFC_StatusTypeDef M24SR::M24SR_SendSTReadBinary(uint16_t Offset,
uint8_t NbByteToRead, uint8_t *pBufferRead) {
+ //clamp the buffer to the max size
+ if(NbByteToRead>M24SR_MAX_BYTE_OPERATION_LENGHT)
+ NbByteToRead=M24SR_MAX_BYTE_OPERATION_LENGHT;
+
C_APDU command;
uint8_t *pBuffer = uM24SRbuffer;
+ uint16_t NbByte;
NFC_StatusTypeDef status;
- uint16_t NbByte;
/* build the command */
command.Header.CLA = C_APDU_CLA_ST;
@@ -592,18 +779,27 @@
M24SR_BuildIBlockCommand(M24SR_CMDSTRUCT_READBINARY, &command, uDIDbyte,
&NbByte, pBuffer);
- errchk(M24SR_IO_SendI2Ccommand(NbByte, pBuffer));
- errchk(M24SR_IO_IsAnswerReady());
- errchk(
- M24SR_IO_ReceiveI2Cresponse (NbByteToRead + M24SR_STATUSRESPONSE_NBBYTE , pBuffer ));
+ status = M24SR_IO_SendI2Ccommand(NbByte, pBuffer);
+ if(status!=NFC_SUCCESS){
+ getCallback()->onReadByte(this,status,Offset,pBufferRead,NbByteToRead);
+ return status;
+ }
+
+ mLastCommandSend=READ;
+ mLastCommandData.data=pBufferRead;
+ mLastCommandData.length=NbByteToRead;
- status = M24SR_IsCorrectCRC16Residue(pBuffer,
- NbByteToRead + M24SR_STATUSRESPONSE_NBBYTE);
+ if(mCommunicationType==M24SR::SYNC){
+ status = M24SR_IO_PollI2C();
+ if(status==NFC_SUCCESS){
+ return M24SR_ReceiveReadBinary();
+ }else{
+ mLastCommandSend = NONE;
+ getCallback()->onReadByte(this,status,Offset,pBufferRead,NbByteToRead);
+ return status;
+ }//if-else
+ }//if
- if(status != NFC_COMMAND_SUCCESS)
- return status;
- /* retrieve the data without SW1 & SW2 as provided as return value of the function */
- memcpy(pBufferRead, &pBuffer[1], NbByteToRead);
return NFC_SUCCESS;
}
@@ -615,13 +811,15 @@
* @retval Status (SW1&SW2) Status of the operation to complete.
* @retval M24SR_ERROR_I2CTIMEOUT I2C timeout occurred.
*/
-NFC_StatusTypeDef M24SR::M24SR_UpdateBinary(uint16_t Offset,
+NFC_StatusTypeDef M24SR::M24SR_SendUpdateBinary(uint16_t Offset,
uint8_t NbByteToWrite, uint8_t *pDataToWrite) {
- //TODO check the length of the data to write
+ //clamp the buffer to the max size
+ if(NbByteToWrite>M24SR_MAX_BYTE_OPERATION_LENGHT)
+ NbByteToWrite=M24SR_MAX_BYTE_OPERATION_LENGHT;
+
C_APDU command;
-
+ NFC_StatusTypeDef status;
uint8_t *pBuffer = uM24SRbuffer;
- NFC_StatusTypeDef status;
uint16_t NbByte;
/* build the command */
@@ -638,32 +836,67 @@
M24SR_BuildIBlockCommand(M24SR_CMDSTRUCT_UPDATEBINARY, &command, uDIDbyte,
&NbByte, pBuffer);
- errchk(M24SR_IO_SendI2Ccommand(NbByte, pBuffer));
- errchk(M24SR_IO_IsAnswerReady());
- errchk(
- M24SR_IO_ReceiveI2Cresponse (M24SR_STATUSRESPONSE_NBBYTE , pBuffer ));
- /* if the response is a Watiting frame extenstion request */
+ status = M24SR_IO_SendI2Ccommand(NbByte, pBuffer);
+ if(status!=NFC_SUCCESS){
+ getCallback()->onUpdatedBinary(this,status,Offset,pDataToWrite,NbByteToWrite);
+ return status;
+ }
+
+ mLastCommandSend=UPDATE;
+ mLastCommandData.data=pDataToWrite;
+ mLastCommandData.length=NbByteToWrite;
+ mLastCommandData.offset=Offset;
+
+ if(mCommunicationType==M24SR::SYNC){
+ status = M24SR_IO_PollI2C();
+ if(status==NFC_SUCCESS){
+ return M24SR_ReceiveUpdateBinary();
+ }else{
+ mLastCommandSend = NONE;
+ getCallback()->onUpdatedBinary(this,status,Offset,pDataToWrite,NbByteToWrite);
+ return status;
+ }//if-else
+ }
- if (IsSBlock(pBuffer) == NFC_SUCCESS) {
+ return NFC_SUCCESS;
+}
+
+NFC_StatusTypeDef M24SR::M24SR_ReceiveUpdateBinary() {
+
+ uint8_t respBuffer[M24SR_STATUSRESPONSE_NBBYTE];
+ NFC_StatusTypeDef status;
+ const uint16_t length = mLastCommandData.length;
+ uint8_t *data = mLastCommandData.data;
+ const uint16_t offset = mLastCommandData.offset;
+
+ mLastCommandSend=NONE;
+
+ status= M24SR_IO_ReceiveI2Cresponse (sizeof(respBuffer) , respBuffer);
+ if(status != NFC_SUCCESS){
+ getCallback()->onUpdatedBinary(this,status,offset,data,length);
+ return status;
+ }
+
+ if (IsSBlock(respBuffer) == NFC_SUCCESS) {
/*check the CRC */
- status =M24SR_IsCorrectCRC16Residue(pBuffer,
- M24SR_WATINGTIMEEXTRESPONSE_NBBYTE);
+ status =M24SR_IsCorrectCRC16Residue(respBuffer,
+ M24SR_WATINGTIMEEXTRESPONSE_NBBYTE);
// TODO: why if we check ==NFC_Commandsuccess it fail?
if (status != NFC_IO_ERROR_CRC) {
/* send the FrameExension response*/
- errchk(M24SR_FWTExtension(pBuffer[M24SR_OFFSET_PCB + 1]));
- }else
- return status;
- } else {
- status = M24SR_IsCorrectCRC16Residue(pBuffer,
+ status = M24SR_SendFWTExtension(respBuffer[M24SR_OFFSET_PCB + 1]);
+ if(status!=NFC_SUCCESS){ //something get wrong -> abort the update
+ getCallback()->onUpdatedBinary(this,status,offset,data,length);
+ }//status
+ }//if
+ } else { //isSBlock
+ status = M24SR_IsCorrectCRC16Residue(respBuffer,
M24SR_STATUSRESPONSE_NBBYTE);
- if(status!=NFC_COMMAND_SUCCESS)
- return status;
- }
+ getCallback()->onUpdatedBinary(this,status,offset,data,length);
+ }//if else
+ return status;
+}//M24SR_ReceiveUpdateBinary
- return NFC_SUCCESS;
-
-}
/**
* @brief This function sends the Verify command
@@ -673,19 +906,16 @@
* @retval Status (SW1&SW2) Status of the operation to complete.
* @retval M24SR_ERROR_I2CTIMEOUT I2C timeout occurred.
*/
-NFC_StatusTypeDef M24SR::M24SR_Verify(uint16_t uPwdId, uint8_t NbPwdByte,
+NFC_StatusTypeDef M24SR::M24SR_SendVerify(uint16_t uPwdId, uint8_t NbPwdByte,
const uint8_t *pPwd) {
C_APDU command;
-
+ NFC_StatusTypeDef status;
uint8_t *pBuffer = uM24SRbuffer;
- NFC_StatusTypeDef status = NFC_SUCCESS;
uint16_t NbByte;
/*check the parameters */
- if (uPwdId > 0x0003) {
- return NFC_IO_ERROR_PARAMETER;
- }
- if ((NbPwdByte != 0x00) && (NbPwdByte != 0x10)) {
+ if ((uPwdId > 0x0003)|| ((NbPwdByte != 0x00) && (NbPwdByte != 0x10))) {
+ getCallback()->onVerifyed(this,NFC_IO_ERROR_PARAMETER);
return NFC_IO_ERROR_PARAMETER;
}
@@ -712,19 +942,41 @@
}
/* send the request */
- errchk(M24SR_IO_SendI2Ccommand(NbByte, pBuffer));
- /* wait for answer ready */
- errchk(M24SR_IO_IsAnswerReady());
- /* read the response */
- errchk(
- M24SR_IO_ReceiveI2Cresponse (M24SR_STATUSRESPONSE_NBBYTE , pBuffer ));
+ status = M24SR_IO_SendI2Ccommand(NbByte, pBuffer);
+ if(status!=NFC_SUCCESS){
+ getCallback()->onVerifyed(this,status);
+ return status;
+ }
+ mLastCommandSend=VERIFY;
+ if(mCommunicationType==M24SR::SYNC){
+ status = M24SR_IO_PollI2C();
+ if(status == NFC_SUCCESS)
+ return M24SR_ReceiveVerify();
+ else{
+ mLastCommandSend = NONE;
+ getCallback()->onVerifyed(this,status);
+ return status;
+ }
+ }
- status = M24SR_IsCorrectCRC16Residue(pBuffer, M24SR_STATUSRESPONSE_NBBYTE);
- if(status == NFC_COMMAND_SUCCESS)
- return NFC_SUCCESS;
- else
+ return NFC_SUCCESS;
+}
+
+NFC_StatusTypeDef M24SR::M24SR_ReceiveVerify(){
+ NFC_StatusTypeDef status;
+ uint8_t respBuffer[M24SR_STATUSRESPONSE_NBBYTE];
+ mLastCommandSend=NONE;
+
+ status=M24SR_IO_ReceiveI2Cresponse (sizeof(respBuffer),respBuffer);
+
+ if(status !=NFC_SUCCESS){
+ getCallback()->onVerifyed(this,status);
return status;
+ }
+ status = M24SR_IsCorrectCRC16Residue(respBuffer, M24SR_STATUSRESPONSE_NBBYTE);
+ getCallback()->onVerifyed(this,status);
+ return status;
}
/**
@@ -734,15 +986,17 @@
* @retval Status (SW1&SW2) Satus of the operation to complete.
* @retval M24SR_ERROR_I2CTIMEOUT I2C timeout occurred.
*/
-NFC_StatusTypeDef M24SR::M24SR_ChangeReferenceData(uint16_t uPwdId,
+NFC_StatusTypeDef M24SR::M24SR_SendChangeReferenceData(uint16_t uPwdId,
uint8_t *pPwd) {
C_APDU command;
+ NFC_StatusTypeDef status;
uint8_t *pBuffer = uM24SRbuffer;
- NFC_StatusTypeDef status;
uint16_t NbByte;
/*check the parameters */
if (uPwdId > 0x0003) {
+ getCallback()->onChangeReferenceData(this,NFC_IO_ERROR_PARAMETER,
+ constToPasswordType(uPwdId), pPwd);
return NFC_IO_ERROR_PARAMETER;
}
@@ -761,18 +1015,52 @@
&NbByte, pBuffer);
/* send the request */
- errchk(M24SR_IO_SendI2Ccommand(NbByte, pBuffer));
- errchk(M24SR_IO_IsAnswerReady());
- /* read the response */
- errchk(
- M24SR_IO_ReceiveI2Cresponse (M24SR_STATUSRESPONSE_NBBYTE , pBuffer ));
+ status = M24SR_IO_SendI2Ccommand(NbByte, pBuffer);
+ if(status != NFC_SUCCESS){
+ getCallback()->onChangeReferenceData(this,status,
+ constToPasswordType(uPwdId), pPwd);
+ return status;
+ }
+
+ mLastCommandSend = CHANGE_REFERENCE_DATA;
+ mLastCommandData.data = pPwd;
+ //use the offset filed for store the pwd id;
+ mLastCommandData.offset = (uint8_t)uPwdId;
- status = M24SR_IsCorrectCRC16Residue(pBuffer, M24SR_STATUSRESPONSE_NBBYTE);
+ if(mCommunicationType==M24SR::SYNC){
+ status = M24SR_IO_PollI2C();
+ if(status == NFC_SUCCESS){
+ return M24SR_ReceiveChangeReferenceData();
+ }else{
+ mLastCommandSend = NONE;
+ getCallback()->onChangeReferenceData(this,status,
+ constToPasswordType(uPwdId), pPwd);
+ return status;
+ }//if-else
+ }//if
+
+ return NFC_SUCCESS;
+}
- if(status == NFC_COMMAND_SUCCESS)
- return NFC_SUCCESS;
- else
+NFC_StatusTypeDef M24SR::M24SR_ReceiveChangeReferenceData(){
+ NFC_StatusTypeDef status;
+ //TODO the size is 3?
+ uint8_t rensponseBuffer[M24SR_STATUSRESPONSE_NBBYTE];
+
+ PasswordType_t type = constToPasswordType(mLastCommandData.offset);
+ uint8_t *data = mLastCommandData.data;
+
+ status =M24SR_IO_ReceiveI2Cresponse (M24SR_STATUSRESPONSE_NBBYTE , rensponseBuffer );
+ if(status!=NFC_SUCCESS){
+ getCallback()->onChangeReferenceData(this,status,type,data);
return status;
+ }//else
+
+ status = M24SR_IsCorrectCRC16Residue(rensponseBuffer, M24SR_STATUSRESPONSE_NBBYTE);
+
+ status = (status == NFC_COMMAND_SUCCESS) ? NFC_SUCCESS : status;
+ getCallback()->onChangeReferenceData(this,status,type,data);
+ return status;
}
/**
@@ -781,15 +1069,17 @@
* @retval Status (SW1&SW2) Status of the operation to complete.
* @retval M24SR_ERROR_I2CTIMEOUT I2C timeout occurred.
*/
-NFC_StatusTypeDef M24SR::M24SR_EnableVerificationRequirement(
+NFC_StatusTypeDef M24SR::M24SR_SendEnableVerificationRequirement(
uint16_t uReadOrWrite) {
C_APDU command;
+ NFC_StatusTypeDef status;
uint8_t *pBuffer = uM24SRbuffer;
- NFC_StatusTypeDef status = NFC_SUCCESS;
uint16_t NbByte;
/*check the parameters */
if ((uReadOrWrite != 0x0001) && (uReadOrWrite != 0x0002)) {
+ getCallback()->onEnableVerificationRequirement(this,NFC_IO_ERROR_PARAMETER,
+ constToPasswordType(uReadOrWrite));
return NFC_IO_ERROR_PARAMETER;
}
@@ -804,35 +1094,70 @@
uDIDbyte, &NbByte, pBuffer);
/* send the request */
- errchk(M24SR_IO_SendI2Ccommand(NbByte, pBuffer));
- /* The right access to be updated in EEPROM need at least 6ms */
- errchk(M24SR_IO_IsAnswerReady());
- /* read the response */
- errchk(
- M24SR_IO_ReceiveI2Cresponse (M24SR_STATUSRESPONSE_NBBYTE , pBuffer ));
+ status = M24SR_IO_SendI2Ccommand(NbByte, pBuffer);
+ if(status != NFC_SUCCESS){
+ getCallback()->onEnableVerificationRequirement(this,status,
+ constToPasswordType(uReadOrWrite));
+ return status;
+ }//if
+
+ mLastCommandSend = ENABLE_VERIFICATION_REQUIREMENT;
+ //use the offset filed for store the pwd id;
+ mLastCommandData.offset = (uint8_t)uReadOrWrite;
+
+ if(mCommunicationType==M24SR::SYNC){
+ status = M24SR_IO_PollI2C();
+ if(status == NFC_SUCCESS){
+ return M24SR_ReceiveEnableVerificationRequirement();
+ }else{
+ mLastCommandSend = NONE;
+ getCallback()->onEnableVerificationRequirement(this,status,
+ constToPasswordType(uReadOrWrite));
+ return status;
+ }//if-else
+ }//if
- status = M24SR_IsCorrectCRC16Residue(pBuffer, M24SR_STATUSRESPONSE_NBBYTE);
- if(status == NFC_COMMAND_SUCCESS)
- return NFC_SUCCESS;
- else
+ return NFC_SUCCESS;
+}
+
+NFC_StatusTypeDef M24SR::M24SR_ReceiveEnableVerificationRequirement(){
+ NFC_StatusTypeDef status;
+ //TODO the size is 3?
+ uint8_t rensponseBuffer[M24SR_STATUSRESPONSE_NBBYTE];
+
+ const PasswordType_t type = constToPasswordType(mLastCommandData.offset);
+
+ status =M24SR_IO_ReceiveI2Cresponse (M24SR_STATUSRESPONSE_NBBYTE , rensponseBuffer );
+ if(status!=NFC_SUCCESS){
+ getCallback()->onEnableVerificationRequirement(this,status,type);
return status;
+ }//else
+
+ status = M24SR_IsCorrectCRC16Residue(rensponseBuffer, M24SR_STATUSRESPONSE_NBBYTE);
+
+ status = (status == NFC_COMMAND_SUCCESS) ? NFC_SUCCESS : status;
+ getCallback()->onEnableVerificationRequirement(this,status,type);
+ return status;
}
+
/**
* @brief This function sends the DisableVerificationRequirement command
* @param uReadOrWrite enable the read or write protection ( 0x0001 : Read or 0x0002 : Write )
* @retval Status (SW1&SW2) Status of the operation to complete.
* @retval M24SR_ERROR_I2CTIMEOUT I2C timeout occurred.
*/
-NFC_StatusTypeDef M24SR::M24SR_DisableVerificationRequirement(
+NFC_StatusTypeDef M24SR::M24SR_SendDisableVerificationRequirement(
uint16_t uReadOrWrite) {
C_APDU command;
+ NFC_StatusTypeDef status;
uint8_t *pBuffer = uM24SRbuffer;
- NFC_StatusTypeDef status = NFC_SUCCESS;
uint16_t NbByte;
/*check the parameters */
if ((uReadOrWrite != 0x0001) && (uReadOrWrite != 0x0002)) {
+ getCallback()->onDisableVerificationRequirement(this,NFC_IO_ERROR_PARAMETER,
+ constToPasswordType(uReadOrWrite));
return NFC_IO_ERROR_PARAMETER;
}
@@ -847,37 +1172,71 @@
uDIDbyte, &NbByte, pBuffer);
/* send the request */
- errchk(M24SR_IO_SendI2Ccommand(NbByte, pBuffer));
- /* The right access to be updated in EEPROM need at least 6ms */
- errchk(M24SR_IO_IsAnswerReady());
- /* read the response */
- errchk(
- M24SR_IO_ReceiveI2Cresponse (M24SR_STATUSRESPONSE_NBBYTE , pBuffer ));
+ status = M24SR_IO_SendI2Ccommand(NbByte, pBuffer);
+ if(status != NFC_SUCCESS){
+ getCallback()->onDisableVerificationRequirement(this,status,
+ constToPasswordType(uReadOrWrite));
+ return status;
+ }
+
+ mLastCommandSend = DISABLE_VERIFICATION_REQUIREMENT;
+ //use the offset filed for store the pwd id;
+ mLastCommandData.offset = (uint8_t)uReadOrWrite;
- status = M24SR_IsCorrectCRC16Residue(pBuffer, M24SR_STATUSRESPONSE_NBBYTE);
+ if(mCommunicationType==M24SR::SYNC){
+ status = M24SR_IO_PollI2C();
+ if(status==NFC_SUCCESS){
+ return M24SR_ReceiveDisableVerificationRequirement();
+ }else{
+ mLastCommandSend = NONE;
+ getCallback()->onDisableVerificationRequirement(this,status,
+ constToPasswordType(uReadOrWrite));
+ return status;
+ }//if-else
+ }
- if(status == NFC_COMMAND_SUCCESS)
- return NFC_SUCCESS;
- else
- return status;
+ return NFC_SUCCESS;
}
+NFC_StatusTypeDef M24SR::M24SR_ReceiveDisableVerificationRequirement(){
+ NFC_StatusTypeDef status;
+ //TODO the size is 3?
+ uint8_t rensponseBuffer[M24SR_STATUSRESPONSE_NBBYTE];
+
+ PasswordType_t type = constToPasswordType(mLastCommandData.offset);
+
+ status =M24SR_IO_ReceiveI2Cresponse (M24SR_STATUSRESPONSE_NBBYTE , rensponseBuffer );
+ if(status!=NFC_SUCCESS){
+ getCallback()->onDisableVerificationRequirement(this,status,type);
+ return status;
+ }//else
+
+ status = M24SR_IsCorrectCRC16Residue(rensponseBuffer, M24SR_STATUSRESPONSE_NBBYTE);
+
+ status = (status == NFC_COMMAND_SUCCESS) ? NFC_SUCCESS : status;
+ getCallback()->onDisableVerificationRequirement(this,status,type);
+ return status;
+}
+
+
/**
* @brief This function sends the EnablePermananentState command
* @param uReadOrWrite enable the read or write protection ( 0x0001 : Read or 0x0002 : Write )
* @retval Status (SW1&SW2) Status of the operation to complete.
* @retval M24SR_ERROR_I2CTIMEOUT I2C timeout occurred.
*/
-NFC_StatusTypeDef M24SR::M24SR_EnablePermanentState(uint16_t uReadOrWrite) {
+NFC_StatusTypeDef M24SR::M24SR_SendEnablePermanentState(uint16_t uReadOrWrite) {
C_APDU command;
+ NFC_StatusTypeDef status;
uint8_t *pBuffer = uM24SRbuffer;
- NFC_StatusTypeDef status = NFC_SUCCESS;
uint16_t NbByte;
/*check the parameters */
if ((uReadOrWrite != 0x0001) && (uReadOrWrite != 0x0002)) {
+ getCallback()->onEnablePermanentState(this,NFC_IO_ERROR_PARAMETER,
+ constToPasswordType(uReadOrWrite));
return NFC_IO_ERROR_PARAMETER;
}
@@ -892,34 +1251,69 @@
&NbByte, pBuffer);
/* send the request */
- errchk(M24SR_IO_SendI2Ccommand(NbByte, pBuffer));
- errchk(M24SR_IO_IsAnswerReady());
- /* read the response */
- errchk(
- M24SR_IO_ReceiveI2Cresponse (M24SR_STATUSRESPONSE_NBBYTE , pBuffer ));
+ status = M24SR_IO_SendI2Ccommand(NbByte, pBuffer);
+ if(status != NFC_SUCCESS){
+ getCallback()->onEnablePermanentState(this,status,
+ constToPasswordType(uReadOrWrite));
+ return status;
+ }
+
+ mLastCommandSend = ENABLE_PERMANET_STATE;
+ //use the offset filed for store the pwd id;
+ mLastCommandData.offset = (uint8_t)uReadOrWrite;
+
+ if(mCommunicationType==M24SR::SYNC){
+ status = M24SR_IO_PollI2C();
+ if(status==NFC_SUCCESS){
+ return M24SR_ReceiveEnablePermanentState();
+ }else{
+ mLastCommandSend = NONE;
+ getCallback()->onEnablePermanentState(this,status,
+ constToPasswordType(uReadOrWrite));
+ return status;
+ }//if-else
+ }
- status = M24SR_IsCorrectCRC16Residue(pBuffer, M24SR_STATUSRESPONSE_NBBYTE);
- if(status == NFC_COMMAND_SUCCESS)
- return NFC_SUCCESS;
- else
+ return NFC_SUCCESS;
+}
+
+NFC_StatusTypeDef M24SR::M24SR_ReceiveEnablePermanentState(){
+ NFC_StatusTypeDef status;
+ //TODO the size is 3?
+ uint8_t rensponseBuffer[M24SR_STATUSRESPONSE_NBBYTE];
+
+ PasswordType_t type = constToPasswordType(mLastCommandData.offset);
+
+ status =M24SR_IO_ReceiveI2Cresponse (M24SR_STATUSRESPONSE_NBBYTE , rensponseBuffer );
+ if(status!=NFC_SUCCESS){
+ getCallback()->onEnablePermanentState(this,status,type);
return status;
+ }//else
+
+ status = M24SR_IsCorrectCRC16Residue(rensponseBuffer, M24SR_STATUSRESPONSE_NBBYTE);
+
+ status = (status == NFC_COMMAND_SUCCESS) ? NFC_SUCCESS : status;
+ getCallback()->onEnablePermanentState(this,status,type);
+ return status;
}
+
/**
* @brief This function sends the DisablePermanentState command
* @param uReadOrWrite enable the read or write protection ( 0x0001 : Read or 0x0002 : Write )
* @retval Status (SW1&SW2) Status of the operation to complete.
* @retval M24SR_ERROR_I2CTIMEOUT I2C timeout occurred.
*/
-NFC_StatusTypeDef M24SR::M24SR_DisablePermanentState(uint16_t uReadOrWrite) {
+NFC_StatusTypeDef M24SR::M24SR_SendDisablePermanentState(uint16_t uReadOrWrite) {
C_APDU command;
-
+ NFC_StatusTypeDef status;
uint8_t *pBuffer = uM24SRbuffer;
- NFC_StatusTypeDef status = NFC_SUCCESS;
uint16_t NbByte;
/*check the parameters */
if ((uReadOrWrite != 0x0001) && (uReadOrWrite != 0x0002)) {
+ getCallback()->onDisablePermanentState(this,NFC_IO_ERROR_PARAMETER,
+ constToPasswordType(uReadOrWrite));
return NFC_IO_ERROR_PARAMETER;
}
@@ -934,18 +1328,50 @@
uDIDbyte, &NbByte, pBuffer);
/* send the request */
- errchk(M24SR_IO_SendI2Ccommand(NbByte, pBuffer));
- errchk(M24SR_IO_IsAnswerReady());
- /* read the response */
- errchk(
- M24SR_IO_ReceiveI2Cresponse (M24SR_STATUSRESPONSE_NBBYTE , pBuffer ));
+ status = M24SR_IO_SendI2Ccommand(NbByte, pBuffer);
+ if(status != NFC_SUCCESS){
+ getCallback()->onEnablePermanentState(this,status,
+ constToPasswordType(uReadOrWrite));
+ return status;
+ }
+
+ mLastCommandSend = ENABLE_PERMANET_STATE;
+ //use the offset filed for store the pwd id;
+ mLastCommandData.offset = (uint8_t)uReadOrWrite;
- status = M24SR_IsCorrectCRC16Residue(pBuffer, M24SR_STATUSRESPONSE_NBBYTE);
+ if(mCommunicationType==M24SR::SYNC){
+ status = M24SR_IO_PollI2C();
+ if(status==NFC_SUCCESS){
+ return M24SR_ReceiveDisablePermanentState();
+ }else{
+ mLastCommandSend = NONE;
+ getCallback()->onDisablePermanentState(this,status,
+ constToPasswordType(uReadOrWrite));
+ return status;
+ }//if-else
+ }
+
+ return NFC_SUCCESS;
+}
- if(status == NFC_COMMAND_SUCCESS)
- return NFC_SUCCESS;
- else
+NFC_StatusTypeDef M24SR::M24SR_ReceiveDisablePermanentState(){
+ NFC_StatusTypeDef status;
+ //TODO the size is 3?
+ uint8_t rensponseBuffer[M24SR_STATUSRESPONSE_NBBYTE];
+
+ PasswordType_t type = constToPasswordType(mLastCommandData.offset);
+
+ status =M24SR_IO_ReceiveI2Cresponse (M24SR_STATUSRESPONSE_NBBYTE , rensponseBuffer );
+ if(status!=NFC_SUCCESS){
+ getCallback()->onDisablePermanentState(this,status,type);
return status;
+ }//else
+
+ status = M24SR_IsCorrectCRC16Residue(rensponseBuffer, M24SR_STATUSRESPONSE_NBBYTE);
+
+ status = (status == NFC_COMMAND_SUCCESS) ? NFC_SUCCESS : status;
+ getCallback()->onDisablePermanentState(this,status,type);
+ return status;
}
/**
@@ -977,7 +1403,7 @@
/* send the request */
errchk(M24SR_IO_SendI2Ccommand(NbByte, pBuffer));
- errchk(M24SR_IO_IsAnswerReady());
+ errchk(M24SR_IO_PollI2C());
/* read the response */
errchk(
M24SR_IO_ReceiveI2Cresponse (M24SR_STATUSRESPONSE_NBBYTE , pBuffer ));
@@ -1028,7 +1454,7 @@
/* send the request */
errchk(M24SR_IO_SendI2Ccommand(NbByte, pBuffer));
- errchk(M24SR_IO_IsAnswerReady());
+ errchk(M24SR_IO_PollI2C());
/* read the response */
errchk(
M24SR_IO_ReceiveI2Cresponse (M24SR_STATUSRESPONSE_NBBYTE , pBuffer ));
@@ -1042,55 +1468,32 @@
}
-NFC_StatusTypeDef M24SR::M24SR_ManageI2CGPO(M24SR_GPO_MGMT GPO_I2Cconfig) {
- uint8_t GPO_config = 0;
+NFC_StatusTypeDef M24SR::M24SR_ManageI2CGPO(NFC_GPO_MGMT GPO_I2Cconfig) {
if (GPO_I2Cconfig > STATE_CONTROL) {
return NFC_IO_ERROR_PARAMETER;
}
- errchk(M24SR_SelectApplication());
-
- errchk(M24SR_SelectSystemfile());
+ //enable the callback for change the gpo
+ mComponentCallback = &mManageGPOCallback;
+ mManageGPOCallback.setNewGpoConfig(true,GPO_I2Cconfig);
- errchk(M24SR_ReadBinary(0x0004, 0x01, &GPO_config))
- errchk(M24SR_Verify(I2C_PWD, 0x10, DEFAULT_PASSWORD));
-
- /* Update only GPO purpose for I2C */
- GPO_config = (GPO_config & 0xF0) | GPO_I2Cconfig;
-
- errchk(M24SR_UpdateBinary(0x0004, 0x01, &(GPO_config)));
-
- /* if we use the gpo for know when the asnwer is ready,
- we can use the interrupt for do the polling, otherwise just wait on the
- i2c read */
- if (GPO_I2Cconfig == I2C_ANSWER_READY)
- M24SR_IO_SetI2CSynchroMode(M24SR_INTERRUPT_GPO);
- else
- M24SR_IO_SetI2CSynchroMode(M24SR_WAITINGTIME_POLLING);
-
- return NFC_SUCCESS;
+ //start the manageGPO procedure
+ return M24SR_SendSelectApplication();
}
-NFC_StatusTypeDef M24SR::M24SR_ManageRFGPO(M24SR_GPO_MGMT GPO_RFconfig) {
- uint8_t GPO_config;
+NFC_StatusTypeDef M24SR::M24SR_ManageRFGPO(NFC_GPO_MGMT GPO_I2Cconfig) {
- if (GPO_RFconfig > STATE_CONTROL) {
+ if (GPO_I2Cconfig > STATE_CONTROL) {
return NFC_IO_ERROR_PARAMETER;
}
- errchk(M24SR_SelectApplication())
- errchk(M24SR_SelectSystemfile())
- errchk(M24SR_ReadBinary(0x0004, 0x01, &GPO_config))
+ //enable the callback for change the gpo
+ mComponentCallback = &mManageGPOCallback;
+ mManageGPOCallback.setNewGpoConfig(false,GPO_I2Cconfig);
- /* Update only GPO purpose for I2C */
- GPO_config = (GPO_config & 0x0F) | (GPO_RFconfig << 4);
-
- errchk(M24SR_SelectSystemfile())
- errchk(M24SR_Verify(I2C_PWD, 0x10, DEFAULT_PASSWORD))
- errchk(M24SR_UpdateBinary(0x0004, 0x01, &(GPO_config)))
-
- return NFC_SUCCESS;
+ //start the manageGPO procedure
+ return M24SR_SendSelectApplication();
}
NFC_StatusTypeDef M24SR::M24SR_RFConfig(uint8_t OnOffChoice) {
@@ -1134,100 +1537,107 @@
return NFC_IO_ERROR_I2CTIMEOUT;
}
-NFC_StatusTypeDef M24SR::M24SR_IO_IsAnswerReady(void) {
-
- switch (syncMode) {
- case M24SR_WAITINGTIME_POLLING:
- return M24SR_IO_PollI2C();
-
- case M24SR_INTERRUPT_GPO:
+NFC_StatusTypeDef M24SR::ManageEvent(void){
- while (!interruptIsFired ){
- __WFE();
- }//while
- interruptIsFired = false;
+ switch(mLastCommandSend){
+ case SELECT_APPLICATION:
+ return M24SR_ReceiveSelectApplication();
+ case SELECT_CC_FILE:
+ return M24SR_ReceiveSelectCCfile();
+ case SELECT_NDEF_FILE:
+ return M24SR_ReceiveSelectNDEFfile();
+ case SELECT_SYSTEM_FILE:
+ return M24SR_ReceiveSelectSystemfile();
+ case READ:
+ return M24SR_ReceiveReadBinary();
+ case UPDATE:
+ return M24SR_ReceiveUpdateBinary();
+ case VERIFY:
+ return M24SR_ReceiveVerify();
+ case DESELECT:
+ return M24SR_ReceiveDeselect();
+ case CHANGE_REFERENCE_DATA:
+ return M24SR_ReceiveChangeReferenceData();
+ case ENABLE_VERIFICATION_REQUIREMENT:
+ return M24SR_ReceiveEnableVerificationRequirement();
+ case DISABLE_VERIFICATION_REQUIREMENT:
+ return M24SR_ReceiveDisableVerificationRequirement();
- return NFC_SUCCESS;
-
- default:
- return NFC_IO_ERROR_I2CTIMEOUT;
- }
-
-}
+ default:
+ return NFC_SUCCESS;
+ }//switch
+}//manageInterrupt
////////////////////////hight level utility function //////////////////////////
NFC_StatusTypeDef M24SR::enableReadPassword(const uint8_t* pCurrentWritePassword,
- const uint8_t* pNewPassword) {
- errchk(Verify(M24SR::WritePwd, 0x10, pCurrentWritePassword));
- /* Set new password */
- errchk(ChangeReferenceData(M24SR::ReadPwd, pNewPassword));
- return EnableVerificationRequirement(M24SR::ReadPwd);
+ const uint8_t* pNewPassword) {
+
+ //enable the callback for change the gpo
+ mComponentCallback = &mChangePasswordRequestStatusCallback;
+ mChangePasswordRequestStatusCallback.setTask(ReadPwd,pNewPassword);
+
+ return Verify(M24SR::WritePwd, 0x10, pCurrentWritePassword);
}
NFC_StatusTypeDef M24SR::disableReadPassword(const uint8_t* pCurrentWritePassword) {
- errchk(Verify(M24SR::WritePwd, 0x10, pCurrentWritePassword));
- return DisableVerificationRequirement(M24SR::ReadPwd);
+ mComponentCallback = &mChangePasswordRequestStatusCallback;
+ mChangePasswordRequestStatusCallback.setTask(ReadPwd,NULL);
+
+ return Verify(M24SR::WritePwd, 0x10, pCurrentWritePassword);
}
NFC_StatusTypeDef M24SR::enableWritePassword(const uint8_t* pCurrentWritePassword,
uint8_t* pNewPassword) {
- /* check we have the good password */
- errchk(Verify(M24SR::WritePwd, 0x10, pCurrentWritePassword));
- /* Set new password */
- errchk(ChangeReferenceData(M24SR::WritePwd, pNewPassword));
- return EnableVerificationRequirement(M24SR::WritePwd);
+ //enable the callback for change the gpo
+ mComponentCallback = &mChangePasswordRequestStatusCallback;
+ mChangePasswordRequestStatusCallback.setTask(WritePwd,pNewPassword);
+
+ return Verify(M24SR::WritePwd, 0x10, pCurrentWritePassword);
}
NFC_StatusTypeDef M24SR::disableWritePassword(const uint8_t* pCurrentWritePassword) {
- errchk(Verify(M24SR::WritePwd, 0x10, pCurrentWritePassword));
- return(DisableVerificationRequirement(M24SR::WritePwd));
+ mComponentCallback = &mChangePasswordRequestStatusCallback;
+ mChangePasswordRequestStatusCallback.setTask(WritePwd,NULL);
+
+ return Verify(M24SR::WritePwd, 0x10, pCurrentWritePassword);
}
NFC_StatusTypeDef M24SR::disableAllPassword(const uint8_t* pSuperUserPassword){
- errchk(Verify(M24SR::I2CPwd, 0x10, pSuperUserPassword));
-
- errchk(DisablePermanentState(M24SR::ReadPwd));
- errchk(DisablePermanentState(M24SR::WritePwd));
-
- errchk(DisableVerificationRequirement(M24SR::ReadPwd));
- errchk(DisableVerificationRequirement(M24SR::WritePwd));
-
- /* reset password */
- errchk(ChangeReferenceData(M24SR::ReadPwd, M24SR::DEFAULT_PASSWORD));
- return(ChangeReferenceData(M24SR::WritePwd, M24SR::DEFAULT_PASSWORD));
+ mComponentCallback = &mChangePasswordRequestStatusCallback;
+ return Verify(M24SR::I2CPwd, 0x10, pSuperUserPassword);
}
NFC_StatusTypeDef M24SR::enableReadOnly(const uint8_t* pCurrentWritePassword){
- errchk(Verify(M24SR::WritePwd, 0x10, pCurrentWritePassword));
- /* lock write to have read only */
- return EnablePermanentState(M24SR::WritePwd);
+ mComponentCallback = &mChangeAccessStateCallback;
+ //disable write = read only
+ mChangeAccessStateCallback.changeAccessState(ChangeAccessStateCallback::WRITE,false);
+
+ return Verify(M24SR::WritePwd, 0x10, pCurrentWritePassword);
}
NFC_StatusTypeDef M24SR::disableReadOnly() {
-
- errchk(Verify(M24SR::I2CPwd, 0x10, M24SR::DEFAULT_PASSWORD));
- /* disable write protection to disable read only mode */
- errchk(DisablePermanentState(M24SR::WritePwd));
- return(DisableVerificationRequirement(M24SR::WritePwd));
+ mComponentCallback = &mChangeAccessStateCallback;
+ mChangeAccessStateCallback.changeAccessState(ChangeAccessStateCallback::WRITE,true);
+ return Verify(M24SR::I2CPwd, 0x10, M24SR::DEFAULT_PASSWORD);
}
NFC_StatusTypeDef M24SR::enableWriteOnly(const uint8_t* pCurrentWritePassword) {
- errchk(Verify(M24SR::WritePwd, 0x10, pCurrentWritePassword));
- /* disable read access and keep write */
- return(EnablePermanentState(M24SR::ReadPwd));
+ mComponentCallback = &mChangeAccessStateCallback;
+ //disable read = enable write only
+ mChangeAccessStateCallback.changeAccessState(ChangeAccessStateCallback::READ,false);
+
+ return Verify(M24SR::WritePwd, 0x10, pCurrentWritePassword);
}
NFC_StatusTypeDef M24SR::disableWriteOnly() {
-
- errchk(Verify(M24SR::I2CPwd, 0x10, M24SR::DEFAULT_PASSWORD));
- /* disable write only -> enable write acces */
- errchk(DisablePermanentState(M24SR::ReadPwd))
- return DisableVerificationRequirement(M24SR::ReadPwd);
+ mComponentCallback = &mChangeAccessStateCallback;
+ mChangeAccessStateCallback.changeAccessState(ChangeAccessStateCallback::READ,true);
+ return Verify(M24SR::I2CPwd, 0x10, M24SR::DEFAULT_PASSWORD);
}
/******************* (C) COPYRIGHT 2013 STMicroelectronics *****END OF FILE****/

X-NUCLEO-NFC01A1 Dynamic NFC Tag