cryptnox-sdk-esp32 1.0.0
ESP32 SDK for Cryptnox Hardware Wallet
Loading...
Searching...
No Matches
main.cpp File Reference
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include "driver/spi_master.h"
#include "esp_log.h"
#include "esp_wifi.h"
#include "esp_netif.h"
#include "esp_event.h"
#include "nvs_flash.h"
#include "pn532.h"
#include "CryptnoxWallet.h"
#include "Pn532NfcTransport.h"
#include "ESP32Logger.h"
#include "esp32_crypto_provider.h"
#include "ESP32Platform.h"
#include "config.h"
Include dependency graph for main.cpp:

Go to the source code of this file.

Macros

#define SPI_MOSI   11
#define SPI_MISO   13
#define SPI_SCLK   12
#define SPI_MAX_TRANSFER_SZ   4096
#define SPI_PIN_UNUSED   (-1)
#define NFC_CS   10
#define WIFI_CONNECTED_BIT   BIT0
#define WIFI_FAIL_BIT   BIT1

Functions

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.
static void wifi_start (void)
 Initialise Wi-Fi station mode and block until connected or timeout.
static void run_sign_loop (CryptnoxWallet &wallet)
 Main application loop: connect, sign a test hash, and disconnect.
void app_main (void)
 ESP-IDF application entry point.

Variables

static const char *const TAG = "sign"
static const uint32_t LOOP_DELAY_MS = 1000U
static const uint32_t WIFI_TIMEOUT_MS = 10000U
static const int WIFI_MAX_RETRY = 5
static EventGroupHandle_t s_wifi_event_group
static int s_retry_num = 0

Macro Definition Documentation

◆ NFC_CS

#define NFC_CS   10

Definition at line 60 of file main.cpp.

◆ SPI_MAX_TRANSFER_SZ

#define SPI_MAX_TRANSFER_SZ   4096

Definition at line 58 of file main.cpp.

◆ SPI_MISO

#define SPI_MISO   13

Definition at line 56 of file main.cpp.

◆ SPI_MOSI

#define SPI_MOSI   11

Definition at line 55 of file main.cpp.

◆ SPI_PIN_UNUSED

#define SPI_PIN_UNUSED   (-1)

Definition at line 59 of file main.cpp.

◆ SPI_SCLK

#define SPI_SCLK   12

Definition at line 57 of file main.cpp.

◆ WIFI_CONNECTED_BIT

#define WIFI_CONNECTED_BIT   BIT0

Definition at line 67 of file main.cpp.

◆ WIFI_FAIL_BIT

#define WIFI_FAIL_BIT   BIT1

Definition at line 68 of file main.cpp.

Function Documentation

◆ app_main()

void app_main ( void )

ESP-IDF application entry point.

Initialises NVS, starts Wi-Fi for full TRNG entropy, brings up the SPI bus and PN532 reader, then enters run_sign_loop.

Definition at line 241 of file main.cpp.

References ESP32Logger::begin(), NFC_CS, pn532_config_t::pin_cs, pn532_init(), run_sign_loop(), pn532_config_t::skip_bus_init, pn532_config_t::spi_host, SPI_MAX_TRANSFER_SZ, SPI_MISO, SPI_MOSI, SPI_PIN_UNUSED, SPI_SCLK, TAG, and wifi_start().

◆ run_sign_loop()

void run_sign_loop ( CryptnoxWallet & wallet)
static

Main application loop: connect, sign a test hash, and disconnect.

Each iteration establishes the secure channel, signs a 32-byte test hash on the secp256k1 curve via CryptnoxWallet::sign, then disconnects. Halts permanently on CW_SIGN_PIN_INCORRECT to protect the card's retry counter.

Parameters
[in]walletInitialised wallet instance.
Examples
Sign/main/main.cpp.

Definition at line 169 of file main.cpp.

References LOOP_DELAY_MS, and TAG.

Referenced by app_main().

◆ wifi_event_handler()

void wifi_event_handler ( void * arg,
esp_event_base_t event_base,
int32_t event_id,
void * event_data )
static

FreeRTOS event handler driving the Wi-Fi station state machine.

Handles WIFI_EVENT_STA_START (triggers connection), WIFI_EVENT_STA_DISCONNECTED (retries up to WIFI_MAX_RETRY times), and IP_EVENT_STA_GOT_IP (signals success via the event group).

Parameters
[in]argUnused.
[in]event_baseEvent base (WIFI_EVENT or IP_EVENT).
[in]event_idEvent identifier within event_base.
[in]event_dataEvent-specific data (unused).

Definition at line 85 of file main.cpp.

References s_retry_num, s_wifi_event_group, WIFI_CONNECTED_BIT, WIFI_FAIL_BIT, and WIFI_MAX_RETRY.

◆ wifi_start()

void wifi_start ( void )
static

Initialise Wi-Fi station mode and block until connected or timeout.

Starts the ESP Wi-Fi stack, registers wifi_event_handler, configures the SSID and password from WIFI_SSID / WIFI_PASSWORD in config.h, then waits up to WIFI_TIMEOUT_MS for an IP address. The radio must be active before crypto operations so the hardware TRNG operates with full entropy (SEC-001).

Note
If the connection fails the function logs a warning and returns normally; the firmware continues with reduced TRNG entropy.

Definition at line 120 of file main.cpp.

References s_retry_num, s_wifi_event_group, TAG, WIFI_CONNECTED_BIT, wifi_event_handler(), WIFI_FAIL_BIT, WIFI_PASSWORD, WIFI_SSID, and WIFI_TIMEOUT_MS.

Variable Documentation

◆ LOOP_DELAY_MS

const uint32_t LOOP_DELAY_MS = 1000U
static

Definition at line 63 of file main.cpp.

◆ s_retry_num

int s_retry_num = 0
static

Definition at line 71 of file main.cpp.

◆ s_wifi_event_group

EventGroupHandle_t s_wifi_event_group
static

Definition at line 70 of file main.cpp.

◆ TAG

const char* const TAG = "sign"
static

Definition at line 62 of file main.cpp.

◆ WIFI_MAX_RETRY

const int WIFI_MAX_RETRY = 5
static

Definition at line 65 of file main.cpp.

◆ WIFI_TIMEOUT_MS

const uint32_t WIFI_TIMEOUT_MS = 10000U
static

Definition at line 64 of file main.cpp.