Skip to content

Commit

Permalink
Remove block's buffer_size arg from mifare read/write functions (#64)
Browse files Browse the repository at this point in the history
Since it's always 16 bytes
  • Loading branch information
abobija authored Sep 23, 2024
1 parent 0658ee5 commit 161323b
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 32 deletions.
4 changes: 2 additions & 2 deletions examples/multiple_scanners/main/multiple_scanners.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ static const char *TAG = "rc522-multiple-scanners-example";
#define RC522_SPI_SCANNER_1_GPIO_SDA (22)
#define RC522_SPI_SCANNER_2_GPIO_SDA (26)

// {{ Reader configurations
// {{ Scanner configurations

static rc522_spi_config_t scanner_1_config = {
.host_id = VSPI_HOST,
Expand Down Expand Up @@ -49,7 +49,7 @@ static rc522_spi_config_t scanner_2_config = {
static rc522_driver_handle_t driver_1;
static rc522_driver_handle_t driver_2;

// Readers
// Scanners

static rc522_handle_t scanner_1;
static rc522_handle_t scanner_2;
Expand Down
28 changes: 11 additions & 17 deletions examples/read_write/main/read_write.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ static rc522_spi_config_t driver_config = {
static rc522_driver_handle_t driver;
static rc522_handle_t scanner;

static void dump_block(uint8_t buffer[16])
static void dump_block(uint8_t buffer[RC522_MIFARE_BLOCK_SIZE])
{
for (uint8_t i = 0; i < 16; i++) {
for (uint8_t i = 0; i < RC522_MIFARE_BLOCK_SIZE; i++) {
esp_log_write(ESP_LOG_INFO, TAG, "%02" RC522_X " ", buffer[i]);
}

Expand All @@ -57,47 +57,41 @@ static esp_err_t read_write(rc522_handle_t scanner, rc522_picc_t *picc)

ESP_RETURN_ON_ERROR(rc522_mifare_auth(scanner, picc, block_address, &key), TAG, "auth fail");

uint8_t read_buffer[16];
uint8_t write_buffer[16];
uint8_t read_buffer[RC522_MIFARE_BLOCK_SIZE];
uint8_t write_buffer[RC522_MIFARE_BLOCK_SIZE];

// Read
ESP_LOGI(TAG, "Reading data from the block %d", block_address);
ESP_RETURN_ON_ERROR(rc522_mifare_read(scanner, picc, block_address, read_buffer, sizeof(read_buffer)),
TAG,
"read fail");
ESP_RETURN_ON_ERROR(rc522_mifare_read(scanner, picc, block_address, read_buffer), TAG, "read fail");
ESP_LOGI(TAG, "Current data:");
dump_block(read_buffer);
// ~Read

// Write
strncpy((char *)write_buffer, data_to_write, 16);
strncpy((char *)write_buffer, data_to_write, RC522_MIFARE_BLOCK_SIZE);

// Set random values for the last two bytes in the write buffer
// so we are using new data on each call of read_write function
int r = rand();
write_buffer[16 - 2] = ((r >> 8) & 0xFF);
write_buffer[16 - 1] = ((r >> 0) & 0xFF);
write_buffer[RC522_MIFARE_BLOCK_SIZE - 2] = ((r >> 8) & 0xFF);
write_buffer[RC522_MIFARE_BLOCK_SIZE - 1] = ((r >> 0) & 0xFF);

ESP_LOGI(TAG, "Writing data (%s) to the block %d:", data_to_write, block_address);
dump_block(write_buffer);
ESP_RETURN_ON_ERROR(rc522_mifare_write(scanner, picc, block_address, write_buffer, sizeof(write_buffer)),
TAG,
"write fail");
ESP_RETURN_ON_ERROR(rc522_mifare_write(scanner, picc, block_address, write_buffer), TAG, "write fail");
// ~Write

// Read again
ESP_LOGI(TAG, "Write done. Verifying...");
ESP_RETURN_ON_ERROR(rc522_mifare_read(scanner, picc, block_address, read_buffer, sizeof(read_buffer)),
TAG,
"read fail");
ESP_RETURN_ON_ERROR(rc522_mifare_read(scanner, picc, block_address, read_buffer), TAG, "read fail");
ESP_LOGI(TAG, "New data in the block %d:", block_address);
dump_block(read_buffer);
// ~Read again

// Validate
bool rw_missmatch = false;
uint8_t i;
for (i = 0; i < 16; i++) {
for (i = 0; i < RC522_MIFARE_BLOCK_SIZE; i++) {
if (write_buffer[i] != read_buffer[i]) {
rw_missmatch = true;
break;
Expand Down
2 changes: 1 addition & 1 deletion idf_component.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: "3.1.6"
version: "3.2.0"
description: "Library for communication with RFID / NFC cards using MFRC522 module"
url: "https://github.com/abobija/esp-idf-rc522"
repository: "https://github.com/abobija/esp-idf-rc522.git"
Expand Down
10 changes: 4 additions & 6 deletions include/picc/rc522_mifare.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,10 @@ esp_err_t rc522_mifare_auth(
* @param rc522 RC522 handle
* @param picc PICC that is currently selected
* @param block_address Address of the block to read
* @param[out] out_buffer Buffer to store the retrived data in
* @param buffer_size Size of the @c out_buffer (should be at least @c RC522_MIFARE_BLOCK_SIZE)
* @param[out] out_buffer Buffer of exactly @c RC522_MIFARE_BLOCK_SIZE bytes to store the retrived data in
*/
esp_err_t rc522_mifare_read(const rc522_handle_t rc522, const rc522_picc_t *picc, uint8_t block_address,
uint8_t *out_buffer, uint8_t buffer_size);
uint8_t out_buffer[RC522_MIFARE_BLOCK_SIZE]);

/**
* @brief Write to a block at the given @c block_address on the PICC.
Expand All @@ -127,11 +126,10 @@ esp_err_t rc522_mifare_read(const rc522_handle_t rc522, const rc522_picc_t *picc
* @param rc522 RC522 handle
* @param picc PICC that is currently selected
* @param block_address Address of the block to write
* @param[in] buffer Buffer containing the data to write
* @param buffer_size Size of the @c buffer (should be at least @c RC522_MIFARE_BLOCK_SIZE)
* @param[in] buffer Buffer of exactly @c RC522_MIFARE_BLOCK_SIZE that contains the data to write
*/
esp_err_t rc522_mifare_write(const rc522_handle_t rc522, const rc522_picc_t *picc, uint8_t block_address,
const uint8_t *buffer, uint8_t buffer_size);
const uint8_t buffer[RC522_MIFARE_BLOCK_SIZE]);

// }}

Expand Down
10 changes: 4 additions & 6 deletions src/picc/rc522_mifare.c
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,11 @@ esp_err_t rc522_mifare_auth(
}

esp_err_t rc522_mifare_read(const rc522_handle_t rc522, const rc522_picc_t *picc, uint8_t block_address,
uint8_t *out_buffer, uint8_t buffer_size)
uint8_t out_buffer[RC522_MIFARE_BLOCK_SIZE])
{
RC522_CHECK(rc522 == NULL);
RC522_CHECK(picc == NULL);
RC522_CHECK(out_buffer == NULL);
RC522_CHECK(buffer_size < RC522_MIFARE_BLOCK_SIZE);

RC522_LOGD("MIFARE READ (block_address=%02" RC522_X ")", block_address);

Expand Down Expand Up @@ -334,12 +333,11 @@ static esp_err_t rc522_mifare_confirm_sector_trailer_write_permission_config(uin
}

esp_err_t rc522_mifare_write(const rc522_handle_t rc522, const rc522_picc_t *picc, uint8_t block_address,
const uint8_t *buffer, uint8_t buffer_size)
const uint8_t buffer[RC522_MIFARE_BLOCK_SIZE])
{
RC522_CHECK(rc522 == NULL);
RC522_CHECK(picc == NULL);
RC522_CHECK(buffer == NULL);
RC522_CHECK(buffer_size < RC522_MIFARE_BLOCK_SIZE);
RC522_CHECK(!rc522_mifare_type_is_classic_compatible(picc->type));
RC522_CHECK(rc522_mifare_confirm_sector_trailer_write_permission_config(block_address) != ESP_OK);

Expand Down Expand Up @@ -513,7 +511,7 @@ esp_err_t rc522_mifare_read_sector_trailer_block(const rc522_handle_t rc522, con
block.address = (sector_desc->block_0_address + sector_desc->number_of_blocks - 1);
block.type = RC522_MIFARE_BLOCK_TRAILER;

RC522_RETURN_ON_ERROR(rc522_mifare_read(rc522, picc, block.address, block.bytes, sizeof(block.bytes)));
RC522_RETURN_ON_ERROR(rc522_mifare_read(rc522, picc, block.address, block.bytes));
block.error = rc522_mifare_parse_sector_trailer(block.bytes, sizeof(block.bytes), &block.trailer_info);
memcpy(&block.access_bits, &block.trailer_info.access_bits[3], sizeof(rc522_mifare_access_bits_t));

Expand Down Expand Up @@ -541,7 +539,7 @@ esp_err_t rc522_mifare_read_sector_block(const rc522_handle_t rc522, const rc522

block.address = (sector_desc->block_0_address + block_offset);

RC522_RETURN_ON_ERROR(rc522_mifare_read(rc522, picc, block.address, block.bytes, sizeof(block.bytes)));
RC522_RETURN_ON_ERROR(rc522_mifare_read(rc522, picc, block.address, block.bytes));

uint8_t group = 0;
RC522_RETURN_ON_ERROR(rc522_mifare_get_sector_block_group_index(sector_desc, block_offset, &group));
Expand Down

0 comments on commit 161323b

Please sign in to comment.