Merge pull request #4957 from andrethomas/development

PN532: Prevent non AlphaNumberic on DATA
This commit is contained in:
Theo Arends 2019-01-17 08:42:13 +01:00 committed by GitHub
commit 8edec390d6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -67,6 +67,7 @@ uint8_t pn532_i2c_disable = 0;
#ifdef USE_PN532_DATA_FUNCTION
uint8_t pn532_i2c_function = 0;
uint8_t pn532_i2c_newdata[16];
uint8_t pn532_i2c_newdata_len = 0;
#endif // USE_PN532_DATA_FUNCTION
const uint8_t PROGMEM pn532_global_timeout = 10;
@ -406,7 +407,13 @@ void PN532_ScanForTag(void)
uint8_t keyuniversal[6] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
if (mifareclassic_AuthenticateBlock (uid, uid_len, 1, 1, keyuniversal)) {
if (mifareclassic_ReadDataBlock(1, card_data)) {
memcpy(&card_datas,&card_data,sizeof(card_data)); // Cast block 1 to a string
for (uint8_t i = 0;i < sizeof(card_data);i++) {
if ((isalpha(card_data[i])) || ((isdigit(card_data[i])))) {
card_datas[i] = char(card_data[i]);
} else {
card_datas[i] = '\0';
}
}
}
if (pn532_i2c_function == 1) { // erase block 1 of card
for (uint8_t i = 0;i<16;i++) {
@ -420,12 +427,23 @@ void PN532_ScanForTag(void)
}
}
if (pn532_i2c_function == 2) {
memcpy(&card_data,&pn532_i2c_newdata,sizeof(card_data));
if (mifareclassic_WriteDataBlock(1, card_data)) {
set_success = true;
snprintf_P(log_data, sizeof(log_data),"I2C: PN532 NFC - Data write successful");
boolean IsAlphaNumeric = true;
for (uint8_t i = 0;i < pn532_i2c_newdata_len;i++) {
if ((!isalpha(pn532_i2c_newdata[i])) || (!isdigit(pn532_i2c_newdata[i]))) {
IsAlphaNumeric = false;
}
}
if (IsAlphaNumeric) {
if (mifareclassic_WriteDataBlock(1, card_data)) {
memcpy(&card_data,&pn532_i2c_newdata,sizeof(card_data));
set_success = true;
snprintf_P(log_data, sizeof(log_data),"I2C: PN532 NFC - Data write successful");
AddLog(LOG_LEVEL_INFO);
memcpy(&card_datas,&card_data,sizeof(card_data)); // Cast block 1 to a string
}
} else {
snprintf_P(log_data, sizeof(log_data),"I2C: PN532 NFC - Data must be alphanumeric");
AddLog(LOG_LEVEL_INFO);
memcpy(&card_datas,&card_data,sizeof(card_data)); // Cast block 1 to a string
}
}
} else {
@ -512,10 +530,10 @@ boolean PN532_Command(void)
return serviced;
}
sprintf(sub_string_tmp,subStr(sub_string, XdrvMailbox.data, ",", 2));
uint8_t dlen = strlen(sub_string_tmp);
if (dlen > 15) { dlen = 15; }
memcpy(&pn532_i2c_newdata,&sub_string_tmp,dlen);
pn532_i2c_newdata[dlen] = 0x00; // Null terminate the string
pn532_i2c_newdata_len = strlen(sub_string_tmp);
if (pn532_i2c_newdata_len > 15) { pn532_i2c_newdata_len = 15; }
memcpy(&pn532_i2c_newdata,&sub_string_tmp,pn532_i2c_newdata_len);
pn532_i2c_newdata[pn532_i2c_newdata_len] = 0x00; // Null terminate the string
pn532_i2c_function = 2;
snprintf_P(log_data, sizeof(log_data),"I2C: PN532 NFC - Next scanned tag data block 1 will be set to '%s'",pn532_i2c_newdata);
AddLog(LOG_LEVEL_INFO);