cryptnox-sdk-arduino 1.0.0
Arduino library for Cryptnox Hardware Wallet
Loading...
Searching...
No Matches
PN532Adapter Class Reference

CW_NfcTransport implementation over the Adafruit_PN532 driver. More...

#include <PN532Adapter.h>

Inheritance diagram for PN532Adapter:
[legend]
Collaboration diagram for PN532Adapter:
[legend]

Public Member Functions

 PN532Adapter (CW_Logger &logger, uint8_t ssPin, SPIClass *theSPI=&SPI)
 Construct an adapter wired over hardware SPI.
 PN532Adapter (CW_Logger &logger, uint8_t clk, uint8_t miso, uint8_t mosi, uint8_t ss)
 Construct an adapter wired over bit-banged software SPI.
 PN532Adapter (CW_Logger &logger, uint8_t irqPin, uint8_t resetPin, TwoWire *wire=&Wire)
 Construct an adapter wired over I2C.
 PN532Adapter (CW_Logger &logger, uint8_t resetPin, HardwareSerial *uartSerial)
 Construct an adapter wired over UART.
 ~PN532Adapter () override
 Release the heap-allocated Adafruit_PN532 instance.
 PN532Adapter (const PN532Adapter &)=delete
PN532Adapteroperator= (const PN532Adapter &)=delete
CW_NfcTransport interface
bool begin () override
 Initialise the PN532 and probe its 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 currently selected card.
bool sendAPDULarge (const uint8_t *apdu, uint8_t apduLen, uint8_t *response, uint16_t &responseLen) override
 APDU exchange that can return more than 255 bytes.
bool inListPassiveTarget () override
 Poll for a card in the field.
void resetReader () override
 Reset the PN532 into a clean idle state.
bool printFirmwareVersion () override
 Pretty-print the PN532 firmware version + supported features.
Public Member Functions inherited from CW_NfcTransport
virtual ~CW_NfcTransport ()

Private Attributes

CW_Logger_logger
 Logger reference (non-owning).
PN532Interface _interface
 Wiring variant the active constructor selected.
Adafruit_PN532 * _nfc
 Owned Adafruit_PN532 driver instance.

Detailed Description

CW_NfcTransport implementation over the Adafruit_PN532 driver.

Concrete transport the host integration constructs once per reader and passes by reference to CryptnoxWallet. The class owns the underlying Adafruit_PN532 instance (heap-allocated by the constructor matching the wiring, released by the destructor) — callers do not need to manage its lifetime.

Forwards each CW_NfcTransport call to the equivalent Adafruit_PN532 method, plus a bounds check on APDU length (HIGH-05) and an optional hex-dump of the response when CW_DEBUG_LOGGING is set.

Example — SPI hardware (Arduino UNO R4 default SPI pins)
PN532Adapter nfc(logger, 10); // SS on D10
nfc.begin();
PN532Adapter nfc(serialAdapter, PN532_SS, &SPI)
CW_Logger implementation wrapping Arduino's HardwareSerial.
PN532Adapter(CW_Logger &logger, uint8_t ssPin, SPIClass *theSPI=&SPI)
Construct an adapter wired over hardware SPI.
Example — I2C (PN532 with IRQ + RESET)
PN532Adapter nfc(logger, 2, 3); // IRQ=D2, RESET=D3
Note
Non-copyable: the wrapped Adafruit_PN532 is owned uniquely.
Warning
The PN532 module has a hard 255-byte limit on ISO-DEP APDUs. sendAPDU rejects anything longer (HIGH-05) — chunking must be done in the protocol layer.
Examples
BasicUsage.ino, Connect.ino, Sign.ino, UsdcSigning.ino, and VerifyPin.ino.

Definition at line 86 of file PN532Adapter.h.

Constructor & Destructor Documentation

◆ PN532Adapter() [1/5]

PN532Adapter::PN532Adapter ( CW_Logger & logger,
uint8_t ssPin,
SPIClass * theSPI = &SPI )

Construct an adapter wired over hardware SPI.

Uses the MCU's SPI peripheral; SCK/MOSI/MISO are fixed by the SPI block (D13/D11/D12 on the UNO R4). Only the slave-select pin is configurable.

Parameters
[in]loggerLogger sink for debug and error output.
[in]ssPinGPIO connected to the PN532 SS / NSS pin.
[in]theSPIOptional SPI instance (defaults to the global SPI).

Definition at line 22 of file PN532Adapter.cpp.

References _interface, _logger, and _nfc.

Referenced by operator=(), and PN532Adapter().

◆ PN532Adapter() [2/5]

PN532Adapter::PN532Adapter ( CW_Logger & logger,
uint8_t clk,
uint8_t miso,
uint8_t mosi,
uint8_t ss )

Construct an adapter wired over bit-banged software SPI.

Use this when the hardware SPI peripheral is already taken by another device. Any four GPIOs work; throughput is significantly lower than hardware SPI.

Parameters
[in]loggerLogger sink.
[in]clkGPIO for SPI clock.
[in]misoGPIO for MISO (PN532 → MCU).
[in]mosiGPIO for MOSI (MCU → PN532).
[in]ssGPIO for slave-select.

Definition at line 27 of file PN532Adapter.cpp.

References _interface, _logger, and _nfc.

◆ PN532Adapter() [3/5]

PN532Adapter::PN532Adapter ( CW_Logger & logger,
uint8_t irqPin,
uint8_t resetPin,
TwoWire * wire = &Wire )

Construct an adapter wired over I2C.

Requires the PN532 SEL0/SEL1 jumpers to be set to I2C and external pull-ups on SDA/SCL (the PN532 breakouts from Adafruit include them).

Parameters
[in]loggerLogger sink.
[in]irqPinGPIO connected to the PN532 IRQ line.
[in]resetPinGPIO connected to the PN532 RSTPDN line.
[in]wireOptional TwoWire instance (defaults to the global Wire).

Definition at line 32 of file PN532Adapter.cpp.

References _interface, _logger, and _nfc.

◆ PN532Adapter() [4/5]

PN532Adapter::PN532Adapter ( CW_Logger & logger,
uint8_t resetPin,
HardwareSerial * uartSerial )

Construct an adapter wired over UART.

The PN532 must be configured at 115200 baud by its SEL jumpers. UART is the slowest mode but tolerates long cable runs better than SPI/I2C.

Parameters
[in]loggerLogger sink.
[in]resetPinGPIO connected to the PN532 RSTPDN line.
[in]uartSerialHardwareSerial connected to the PN532 TX/RX lines.

Definition at line 37 of file PN532Adapter.cpp.

References _interface, _logger, and _nfc.

◆ ~PN532Adapter()

PN532Adapter::~PN532Adapter ( )
override

Release the heap-allocated Adafruit_PN532 instance.

Definition at line 41 of file PN532Adapter.cpp.

References _nfc.

◆ PN532Adapter() [5/5]

PN532Adapter::PN532Adapter ( const PN532Adapter & )
delete

References PN532Adapter().

Member Function Documentation

◆ begin()

bool PN532Adapter::begin ( )
overridevirtual

Initialise the PN532 and probe its firmware version.

Calls Adafruit_PN532::begin() then issues getFirmwareVersion() as a liveness check.

Returns
true if the reader responded to getFirmwareVersion(), false on a wiring fault or a dead module.

Implements CW_NfcTransport.

Definition at line 48 of file PN532Adapter.cpp.

References _nfc.

◆ inListPassiveTarget()

bool PN532Adapter::inListPassiveTarget ( )
overridevirtual

Poll for a card in the field.

Single shot — returns immediately after one polling cycle. Callers typically invoke this in a loop with a small delay().

Returns
true if a card was selected, false otherwise.

Implements CW_NfcTransport.

Definition at line 121 of file PN532Adapter.cpp.

References _nfc.

◆ operator=()

PN532Adapter & PN532Adapter::operator= ( const PN532Adapter & )
delete

References PN532Adapter().

◆ printFirmwareVersion()

bool PN532Adapter::printFirmwareVersion ( )
overridevirtual

Pretty-print the PN532 firmware version + supported features.

Decodes the raw firmware-version word into IC type (0x32 = PN532), major/minor revision, and a feature flag set (MIFARE / ISO-DEP / FeliCa) and emits a tree-style summary to the logger.

Returns
true if the PN532 responded, false otherwise.

Implements CW_NfcTransport.

Definition at line 129 of file PN532Adapter.cpp.

References _logger, _nfc, F, and HEX.

◆ resetReader()

void PN532Adapter::resetReader ( )
overridevirtual

Reset the PN532 into a clean idle state.

Issues SAMConfig() which both clears any pending card session and arms the Secure Access Module. Called by CryptnoxWallet between retries so a stuck reader can recover without a hard reset.

Implements CW_NfcTransport.

Definition at line 125 of file PN532Adapter.cpp.

References _nfc.

◆ sendAPDU()

bool PN532Adapter::sendAPDU ( const uint8_t * apdu,
uint8_t apduLen,
uint8_t * response,
uint8_t & responseLen )
overridevirtual

Exchange one ISO-DEP APDU with the currently selected card.

Wraps Adafruit_PN532::inDataExchange() and adapts its semantics to the CW_NfcTransport contract: responseLen is in/out (capacity in, actual length out). The uint8_t apduLen already enforces the PN532's 255-byte ISO-DEP frame limit at the type level (HIGH-05).

When CW_DEBUG_LOGGING is set, dumps the response as a hex grid to the logger.

Parameters
[in]apduAPDU bytes (≤ 255).
[in]apduLenLength of apdu in bytes.
[out]responseResponse buffer (caller-allocated).
[in,out]responseLenIn: capacity of response. Out: number of bytes actually written.
Returns
true on a successful exchange, false on a PN532 / card communication failure.

Implements CW_NfcTransport.

Definition at line 53 of file PN532Adapter.cpp.

References _logger, _nfc, F, and HEX.

◆ sendAPDULarge()

bool PN532Adapter::sendAPDULarge ( const uint8_t * apdu,
uint8_t apduLen,
uint8_t * response,
uint16_t & responseLen )
overridevirtual

APDU exchange that can return more than 255 bytes.

Uses Adafruit_PN532's inDataExchange16() (added by the Adafruit_PN532_extended_frame.patch) which parses both PN532 normal and extended frame formats — required by GET_MANUFACTURER_CERTIFICATE pages that exceed the 255-byte normal-frame ceiling on cards whose cert is ~411 bytes.

Parameters
[in]apduAPDU bytes (≤ 255).
[in]apduLenLength of apdu in bytes.
[out]responseResponse buffer (caller-allocated).
[in,out]responseLenIn: capacity of response (up to ~436). Out: number of bytes actually written.
Returns
true on a successful exchange, false on a PN532 / card communication failure or frame parse error.

Reimplemented from CW_NfcTransport.

Definition at line 87 of file PN532Adapter.cpp.

References _logger, _nfc, F, and HEX.

Member Data Documentation

◆ _interface

PN532Interface PN532Adapter::_interface
private

Wiring variant the active constructor selected.

Definition at line 237 of file PN532Adapter.h.

Referenced by PN532Adapter(), PN532Adapter(), PN532Adapter(), and PN532Adapter().

◆ _logger

CW_Logger* PN532Adapter::_logger
private

Logger reference (non-owning).

Definition at line 236 of file PN532Adapter.h.

Referenced by PN532Adapter(), PN532Adapter(), PN532Adapter(), PN532Adapter(), printFirmwareVersion(), sendAPDU(), and sendAPDULarge().

◆ _nfc

Adafruit_PN532* PN532Adapter::_nfc
private

The documentation for this class was generated from the following files: