30#include "freertos/FreeRTOS.h"
31#include "freertos/task.h"
32#include "freertos/event_groups.h"
33#include "driver/spi_master.h"
40#include "CryptnoxWallet.h"
51#define SPI_MAX_TRANSFER_SZ 4096
52#define SPI_PIN_UNUSED (-1)
55static const char *
const TAG =
"verify_pin";
60#define WIFI_CONNECTED_BIT BIT0
61#define WIFI_FAIL_BIT BIT1
79 int32_t event_id,
void *event_data)
83 if ((event_base == WIFI_EVENT) && (event_id == WIFI_EVENT_STA_START)) {
85 }
else if ((event_base == WIFI_EVENT) &&
86 (event_id == WIFI_EVENT_STA_DISCONNECTED)) {
93 }
else if ((event_base == IP_EVENT) && (event_id == IP_EVENT_STA_GOT_IP)) {
117 ESP_ERROR_CHECK(esp_netif_init());
118 ESP_ERROR_CHECK(esp_event_loop_create_default());
119 (void)esp_netif_create_default_wifi_sta();
120 wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
121 ESP_ERROR_CHECK(esp_wifi_init(&cfg));
122 esp_event_handler_instance_t h_any;
123 esp_event_handler_instance_t h_ip;
124 ESP_ERROR_CHECK(esp_event_handler_instance_register(
126 ESP_ERROR_CHECK(esp_event_handler_instance_register(
128 wifi_config_t wifi_cfg;
129 (void)memset(&wifi_cfg, 0,
sizeof(wifi_cfg));
130 (void)strncpy((
char *)wifi_cfg.sta.ssid,
WIFI_SSID,
131 sizeof(wifi_cfg.sta.ssid) - 1U);
133 sizeof(wifi_cfg.sta.password) - 1U);
134 wifi_cfg.sta.threshold.authmode = WIFI_AUTH_WPA2_PSK;
135 ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
136 ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_cfg));
137 ESP_ERROR_CHECK(esp_wifi_start());
143 ESP_LOGI(
TAG,
"WiFi connected");
145 ESP_LOGW(
TAG,
"WiFi connect failed — TRNG entropy may be reduced");
147 (void)esp_event_handler_instance_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, h_ip);
148 (void)esp_event_handler_instance_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, h_any);
164 static const uint8_t DEMO_PIN[CW_MAX_PIN_LENGTH] = {
165 '0',
'0',
'0',
'0',
'0',
'0',
'0',
'0',
'0'
168 bool keep_running =
true;
169 while (keep_running) {
170 CW_SecureSession session{};
171 bool connected = wallet.connect(session);
174 ESP_LOGW(
TAG,
"Card not detected");
178 bool pinOk = wallet.verifyPin(session,
180 static_cast<uint8_t
>(CW_MAX_PIN_LENGTH));
182 ESP_LOGI(
TAG,
"PIN accepted");
187 ESP_LOGE(
TAG,
"PIN rejected — halting to protect retry counter");
188 keep_running =
false;
192 wallet.disconnect(session);
212 esp_err_t nvs_ret = nvs_flash_init();
213 if ((nvs_ret == ESP_ERR_NVS_NO_FREE_PAGES) ||
214 (nvs_ret == ESP_ERR_NVS_NEW_VERSION_FOUND)) {
215 ESP_ERROR_CHECK(nvs_flash_erase());
216 nvs_ret = nvs_flash_init();
218 ESP_ERROR_CHECK(nvs_ret);
221 spi_bus_config_t buscfg = {};
228 ESP_ERROR_CHECK(spi_bus_initialize(SPI2_HOST, &buscfg, SPI_DMA_CH_AUTO));
235 esp_err_t nfc_ret =
pn532_init(&nfc, &nfc_cfg);
237 if (nfc_ret == ESP_OK) {
239 (void)logger.
begin(115200UL);
244 CryptnoxWallet wallet(nfcTransport, logger, cryptoProvider, platform);
246 if (wallet.begin()) {
249 ESP_LOGE(
TAG,
"Wallet init failed");
252 ESP_LOGE(
TAG,
"PN532 init failed");
void app_main(void)
ESP-IDF application entry point.
static EventGroupHandle_t s_wifi_event_group
#define SPI_MAX_TRANSFER_SZ
static const uint32_t LOOP_DELAY_MS
static void wifi_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data)
FreeRTOS event handler driving the Wi-Fi station state machine.
#define WIFI_CONNECTED_BIT
static void wifi_start(void)
Initialise Wi-Fi station mode and block until connected or timeout.
CW_Logger implementation that writes to ESP32 UART0 via printf.
CW_NfcTransport adapter wrapping the ESP-IDF PN532 NFC driver.
static void run_verify_pin_loop(CryptnoxWallet &wallet)
Main application loop: connect, verify PIN, and disconnect.
CW_CryptoProvider backed by mbedTLS and the ESP32 hardware TRNG.
CW_Logger backed by ESP32 UART0.
bool begin(unsigned long baudRate=115200UL) override
Initialise UART0 at the given baud rate.
CW_NfcTransport implementation backed by the ESP-IDF PN532 driver.
CW_CryptoProvider implementation for ESP32 using mbedTLS and the hardware TRNG.
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.
Low-level PN532 NFC controller driver for ESP-IDF (SPI and I²C).
Compile-time configuration passed to pn532_init.
spi_host_device_t spi_host
Opaque-like runtime state for a single PN532 instance.