29#include "freertos/FreeRTOS.h"
30#include "freertos/task.h"
31#include "freertos/event_groups.h"
32#include "driver/spi_master.h"
39#include "CryptnoxWallet.h"
50#define SPI_MAX_TRANSFER_SZ 4096
51#define SPI_PIN_UNUSED (-1)
54static const char *
const TAG =
"connect";
59#define WIFI_CONNECTED_BIT BIT0
60#define WIFI_FAIL_BIT BIT1
78 int32_t event_id,
void *event_data)
82 if ((event_base == WIFI_EVENT) && (event_id == WIFI_EVENT_STA_START)) {
84 }
else if ((event_base == WIFI_EVENT) &&
85 (event_id == WIFI_EVENT_STA_DISCONNECTED)) {
92 }
else if ((event_base == IP_EVENT) && (event_id == IP_EVENT_STA_GOT_IP)) {
116 ESP_ERROR_CHECK(esp_netif_init());
117 ESP_ERROR_CHECK(esp_event_loop_create_default());
118 (void)esp_netif_create_default_wifi_sta();
119 wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
120 ESP_ERROR_CHECK(esp_wifi_init(&cfg));
121 esp_event_handler_instance_t h_any;
122 esp_event_handler_instance_t h_ip;
123 ESP_ERROR_CHECK(esp_event_handler_instance_register(
125 ESP_ERROR_CHECK(esp_event_handler_instance_register(
127 wifi_config_t wifi_cfg;
128 (void)memset(&wifi_cfg, 0,
sizeof(wifi_cfg));
129 (void)strncpy((
char *)wifi_cfg.sta.ssid,
WIFI_SSID,
130 sizeof(wifi_cfg.sta.ssid) - 1U);
132 sizeof(wifi_cfg.sta.password) - 1U);
133 wifi_cfg.sta.threshold.authmode = WIFI_AUTH_WPA2_PSK;
134 ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
135 ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_cfg));
136 ESP_ERROR_CHECK(esp_wifi_start());
142 ESP_LOGI(
TAG,
"WiFi connected");
144 ESP_LOGW(
TAG,
"WiFi connect failed — TRNG entropy may be reduced");
146 (void)esp_event_handler_instance_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, h_ip);
147 (void)esp_event_handler_instance_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, h_any);
163 CW_SecureSession session{};
164 bool connected = wallet.connect(session);
167 ESP_LOGI(
TAG,
"Card connected, secure channel established");
170 bool infoOk = wallet.getCardInfo(session, &info);
172 ESP_LOGI(
TAG,
"Owner name : %s", info.name);
173 ESP_LOGI(
TAG,
"Owner email: %s", info.email);
175 ESP_LOGW(
TAG,
"getCardInfo failed");
178 ESP_LOGW(
TAG,
"Card not detected or secure channel failed");
181 wallet.disconnect(session);
194 esp_err_t nvs_ret = nvs_flash_init();
195 if ((nvs_ret == ESP_ERR_NVS_NO_FREE_PAGES) ||
196 (nvs_ret == ESP_ERR_NVS_NEW_VERSION_FOUND)) {
197 ESP_ERROR_CHECK(nvs_flash_erase());
198 nvs_ret = nvs_flash_init();
200 ESP_ERROR_CHECK(nvs_ret);
203 spi_bus_config_t buscfg = {};
210 ESP_ERROR_CHECK(spi_bus_initialize(SPI2_HOST, &buscfg, SPI_DMA_CH_AUTO));
217 esp_err_t nfc_ret =
pn532_init(&nfc, &nfc_cfg);
219 if (nfc_ret == ESP_OK) {
221 (void)logger.
begin(115200UL);
226 CryptnoxWallet wallet(nfcTransport, logger, cryptoProvider, platform);
228 if (wallet.begin()) {
232 ESP_LOGE(
TAG,
"Wallet init failed");
235 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.
static void run_connect_loop(CryptnoxWallet &wallet)
Main application loop: connect, fetch card info, and disconnect.
CW_Logger implementation that writes to ESP32 UART0 via printf.
CW_NfcTransport adapter wrapping the ESP-IDF PN532 NFC driver.
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.
bool printFirmwareVersion() override
Query and log the PN532 firmware version.
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.