Permalink
Browse files

Adicionar biblioteca SmartCard para acesso aos cartões RFID

  • Loading branch information...
eduardoweiland committed Jan 23, 2018
1 parent 73e326f commit 6ce647faa7de3954815deb50a8fa4df86c5fbb24
Showing with 165 additions and 0 deletions.
  1. +1 −0 .gitignore
  2. +129 −0 lib/SmartCard/SmartCard.cpp
  3. +34 −0 lib/SmartCard/SmartCard.h
  4. +1 −0 platformio.ini
@@ -2,3 +2,4 @@
.piolibdeps
.vscode
.vscode/c_cpp_properties.json
.vscode/launch.json
@@ -0,0 +1,129 @@
#include <SPI.h>
#include <string.h>

#include "SmartCard.h"

SmartCard::SmartCard(const uint8_t *key, const int chipSelectPin, const int resetPin)
{
memcpy(this->key.keyByte, key, MFRC522::MF_KEY_SIZE);
this->chipSelectPin = chipSelectPin;
this->resetPin = resetPin;
}

void SmartCard::begin()
{
SPI.begin();
this->mfrc522.PCD_Init(this->chipSelectPin, this->resetPin);
}

bool SmartCard::isCardAvailable()
{
// Verifica se existe um cartão na frente do leitor
if (!this->mfrc522.PICC_IsNewCardPresent()) {
return false;
}

// Verifica se é possível ler o UID do cartão
return this->mfrc522.PICC_ReadCardSerial();
}

bool SmartCard::read(const uint8_t blockAddr, uint8_t *buffer, uint8_t *bufferSize)
{
if (!this->isCardAvailable()) {
return false;
}

if (!this->authenticate(blockAddr)) {
return false;
}

bool success = this->readBlock(blockAddr, buffer, bufferSize);

this->deauthenticate();

return success;
}

bool SmartCard::readBlock(const uint8_t blockAddr, uint8_t *buffer, uint8_t *bufferSize)
{
MFRC522::StatusCode status;

status = this->mfrc522.MIFARE_Read(blockAddr, buffer, bufferSize);

if (status != MFRC522::STATUS_OK) {
Serial.print(F("MIFARE_Read failed: "));
Serial.println(this->mfrc522.GetStatusCodeName(status));

return false;
}

// Limpa CRC adicionado pela biblioteca no buffer, não é mais necessário
// (CRC já é validado dentro do MIFARE_Read, que retorna MFRC522::STATUS_CRC_WRONG em caso de erro)
buffer[16] = '\0';
buffer[17] = '\0';

return true;
}

bool SmartCard::write(const uint8_t blockAddr, const uint8_t *buffer, const uint8_t bufferSize)
{
if (!this->isCardAvailable()) {
return false;
}

if (!this->authenticate(blockAddr)) {
return false;
}

bool success = this->writeBlock(blockAddr, buffer, bufferSize);

this->deauthenticate();

return success;
}

bool SmartCard::writeBlock(const uint8_t blockAddr, const uint8_t *buffer, const uint8_t bufferSize)
{
MFRC522::StatusCode status;

status = this->mfrc522.MIFARE_Write(blockAddr, (uint8_t *)buffer, (uint8_t)bufferSize);

if (status != MFRC522::STATUS_OK) {
Serial.print(F("MIFARE_Write failed: "));
Serial.println(this->mfrc522.GetStatusCodeName(status));

return false;
}

return true;
}

bool SmartCard::authenticate(const uint8_t blockAddr)
{
MFRC522::StatusCode status;

status = this->mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, blockAddr, &(this->key), &(this->mfrc522.uid));

if (status != MFRC522::STATUS_OK) {
Serial.print(F("PCD_Authenticate failed: "));
Serial.println(this->mfrc522.GetStatusCodeName(status));

return false;
}

return true;
}

void SmartCard::deauthenticate()
{
MFRC522::StatusCode status;

status = this->mfrc522.PICC_HaltA();

if (status != MFRC522::STATUS_OK) {
Serial.print(F("PICC_HaltA failed: "));
Serial.println(this->mfrc522.GetStatusCodeName(status));
}

this->mfrc522.PCD_StopCrypto1();
}
@@ -0,0 +1,34 @@
#ifndef PLACARDUINO_SMART_CARD_H
#define PLACARDUINO_SMART_CARD_H

#include <Arduino.h>
#include <MFRC522.h>

class SmartCard
{
public:
SmartCard(const byte *key, const int chipSelectPin = SS, const int resetPin = UINT8_MAX);

void begin();
bool isCardAvailable();

bool read(const uint8_t blockAddr, uint8_t *buffer, uint8_t *bufferSize);
bool write(const uint8_t blockAddr, const uint8_t *buffer, const uint8_t bufferSize);

protected:
MFRC522 mfrc522;

bool authenticate(const byte blockAddr);
void deauthenticate();

private:
MFRC522::MIFARE_Key key;

int chipSelectPin;
int resetPin;

bool readBlock(const uint8_t blockAddr, uint8_t *buffer, uint8_t *bufferSize);
bool writeBlock(const uint8_t blockAddr, const uint8_t *buffer, const uint8_t bufferSize);
};

#endif // PLACARDUINO_SMART_CARD_H
@@ -17,3 +17,4 @@ lib_deps =
Wire@1.0
LiquidCrystal_I2C@1.1.2
ToneLibrary@1.7.1
MFRC522@1.3.6

0 comments on commit 6ce647f

Please sign in to comment.