Skip to content

Commit

Permalink
Add inCommunicateThru() and add check status for mifareclassic_WriteD…
Browse files Browse the repository at this point in the history
…ataBlock()
  • Loading branch information
jazchen authored and Pillar1989 committed Feb 10, 2022
1 parent 3c20075 commit 40fa741
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
61 changes: 60 additions & 1 deletion PN532/PN532.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,19 @@ uint8_t PN532::mifareclassic_WriteDataBlock (uint8_t blockNumber, uint8_t *data)
}

/* Read the response packet */
return (0 < HAL(readResponse)(pn532_packetbuffer, sizeof(pn532_packetbuffer)));
if (0 > HAL(readResponse)(pn532_packetbuffer, sizeof(pn532_packetbuffer))) {
return 0;
}

/* Check status */
if (pn532_packetbuffer[0] != 0x00) {
DMSG("Status code indicates an error: ");
DMSG_HEX(pn532_packetbuffer[0]);
DMSG("\n");
return 0;
}

return 1;
}

/**************************************************************************/
Expand Down Expand Up @@ -858,6 +870,53 @@ bool PN532::inDataExchange(uint8_t *send, uint8_t sendLength, uint8_t *response,
return true;
}

/**************************************************************************/
/*!
This command is used to support basic data exchanges
between the PN532 and a target.
@param send Pointer to the command buffer
@param sendLength Command length in bytes
@param response Pointer to response data
@param responseLength Pointer to the response data length
*/
/**************************************************************************/
bool PN532::inCommunicateThru(uint8_t *send, uint8_t sendLength, uint8_t *response, uint8_t *responseLength)
{
pn532_packetbuffer[0] = PN532_COMMAND_INCOMMUNICATETHRU;

if (HAL(writeCommand)(pn532_packetbuffer, 1, send, sendLength)) {
return false;
}

int16_t status = HAL(readResponse)(response, *responseLength, 1000);
if (status < 0) {
return false;
}

// check status code
if (response[0] != 0x0) {
DMSG("Status code indicates an error : 0x");
DMSG_HEX(pn532_packetbuffer[0]);
DMSG("\n");
return false;
}

uint8_t length = status;
length -= 1;

if (length > *responseLength) {
length = *responseLength; // silent truncation...
}

for (uint8_t i = 0; i < length; i++) {
response[i] = response[i + 1];
}
*responseLength = length;

return true;
}

/**************************************************************************/
/*!
@brief 'InLists' a passive target. PN532 acting as reader/initiator,
Expand Down
1 change: 1 addition & 0 deletions PN532/PN532.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ class PN532
bool startPassiveTargetIDDetection(uint8_t cardbaudrate);
bool readPassiveTargetID(uint8_t cardbaudrate, uint8_t *uid, uint8_t *uidLength, uint16_t timeout = 1000, bool inlist = false);
bool inDataExchange(uint8_t *send, uint8_t sendLength, uint8_t *response, uint8_t *responseLength);
bool inCommunicateThru(uint8_t *send, uint8_t sendLength, uint8_t *response, uint8_t *responseLength);

// Mifare Classic functions
bool mifareclassic_IsFirstBlock (uint32_t uiBlock);
Expand Down

0 comments on commit 40fa741

Please sign in to comment.