cryptnox-sdk-esp32 1.0.0
ESP32 SDK for Cryptnox Hardware Wallet
Loading...
Searching...
No Matches
pn532_adapter.cpp
Go to the documentation of this file.
1/*
2 * SPDX-License-Identifier: LGPL-3.0-or-later
3 * Copyright (c) 2026 Cryptnox SA
4 */
5
14
15#include "pn532_adapter.h"
16#include "esp_log.h"
17#include <string.h>
18
19static const char *TAG = "pn532_adapter";
20
21/* Bit positions used to extract firmware version fields from the packed uint32_t. */
22static const uint32_t FW_IC_SHIFT = 24U;
23static const uint32_t FW_VER_SHIFT = 16U;
24static const uint32_t FW_REV_SHIFT = 8U;
25static const uint32_t FW_BYTE_MASK = 0xFFU;
26
27PN532Adapter::PN532Adapter(const pn532_config_t &config, CW_Logger &logger)
28 : _config(config), _dev{}, _logger(logger), _initialized(false)
29{
30}
31
33{
34 esp_err_t ret = ESP_FAIL;
35 bool result = false;
36
37 ret = pn532_init(&_dev, &_config);
38 if (ret != ESP_OK) {
39 ESP_LOGE(TAG, "pn532_init failed: %s", esp_err_to_name(ret));
40 } else {
41 _initialized = true;
42 result = true;
43 }
44
45 return result;
46}
47
49{
50 uint32_t uid = 0U;
51 bool result = false;
52
53 if (_initialized) {
55 result = (uid != 0U);
56 }
57
58 return result;
59}
60
61bool PN532Adapter::sendAPDU(const uint8_t *apdu, uint8_t apduLen,
62 uint8_t *response, uint8_t &responseLen)
63{
64 bool result = false;
65
66 if (_initialized) {
67 uint16_t len = static_cast<uint16_t>(responseLen);
68 result = pn532_send_apdu(&_dev, apdu, apduLen, response, &len);
69 responseLen = static_cast<uint8_t>((len > static_cast<uint16_t>(UINT8_MAX))
70 ? static_cast<uint16_t>(UINT8_MAX) : len);
71 }
72
73 return result;
74}
75
76bool PN532Adapter::sendAPDULarge(const uint8_t *apdu, uint8_t apduLen,
77 uint8_t *response, uint16_t &responseLen)
78{
79 bool result = false;
80
81 if (_initialized) {
82 result = pn532_send_apdu(&_dev, apdu, apduLen, response, &responseLen);
83 }
84
85 return result;
86}
87
89{
90 if (_initialized) {
92 }
93}
94
96{
97 uint32_t version = 0U;
98 bool result = false;
99
100 if (_initialized) {
102 if (version == 0U) {
103 _logger.println("PN532 firmware query failed");
104 } else {
105 _logger.print("PN5");
106 _logger.print(static_cast<uint8_t>((version >> FW_IC_SHIFT) & FW_BYTE_MASK), HEX);
107 _logger.print(" Firmware v");
108 _logger.print(static_cast<uint8_t>((version >> FW_VER_SHIFT) & FW_BYTE_MASK), DEC);
109 _logger.print(".");
110 _logger.println(static_cast<uint8_t>((version >> FW_REV_SHIFT) & FW_BYTE_MASK), DEC);
111 result = true;
112 }
113 }
114
115 return result;
116}
void resetReader() override
Release the currently selected NFC target.
pn532_t _dev
PN532 device state (owned by this adapter).
bool printFirmwareVersion() override
Query and log the PN532 firmware version.
bool sendAPDU(const uint8_t *apdu, uint8_t apduLen, uint8_t *response, uint8_t &responseLen) override
Exchange one ISO-DEP APDU with the selected card (short response).
PN532Adapter(const pn532_config_t &config, CW_Logger &logger)
Construct the adapter with the given PN532 configuration.
pn532_config_t _config
Stored configuration forwarded to pn532_init.
bool inListPassiveTarget() override
Scan for a passive ISO 14443-A card.
bool begin() override
Initialise the PN532 driver and configure the SAM.
bool sendAPDULarge(const uint8_t *apdu, uint8_t apduLen, uint8_t *response, uint16_t &responseLen) override
Exchange one ISO-DEP APDU with the selected card (large response).
CW_Logger & _logger
Logger reference for printFirmwareVersion.
bool _initialized
true after a successful pn532_init call.
static const char *const TAG
esp_err_t pn532_init(pn532_t *dev, const pn532_config_t *config)
Initialise the PN532 and bring it to a ready state.
Definition pn532.c:657
uint32_t pn532_read_passive_target_id(pn532_t *dev, uint8_t cardbaudrate)
Scan for a passive ISO 14443-A card and return its UID.
Definition pn532.c:775
bool pn532_send_apdu(pn532_t *dev, const uint8_t *apdu, uint8_t apdu_len, uint8_t *response, uint16_t *response_len)
Exchange a single ISO-DEP APDU with the currently selected card.
Definition pn532.c:805
uint32_t pn532_get_firmware_version(pn532_t *dev)
Query the PN532 firmware version.
Definition pn532.c:718
#define PN532_MIFARE_ISO14443A
Baud-rate selector for ISO 14443-A (Mifare) cards passed to pn532_read_passive_target_id.
Definition pn532.h:54
bool pn532_release_target(pn532_t *dev)
Release the currently selected NFC target.
Definition pn532.c:875
static const uint32_t FW_BYTE_MASK
static const uint32_t FW_IC_SHIFT
static const uint32_t FW_REV_SHIFT
static const uint32_t FW_VER_SHIFT
Self-contained CW_NfcTransport adapter that owns its pn532_t handle.
Compile-time configuration passed to pn532_init.
Definition pn532.h:111