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 "CW_Utils.h"
#include "config.h"
Include dependency graph for main.cpp:

Go to the source code of this file.

Macros

#define SPI_ENABLED   1
#define I2C_ENABLED   0
#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_basic_usage_loop (CryptnoxWallet &wallet)
 Main application loop: connect, sign a test hash, wipe buffers, disconnect.
void app_main (void)
 ESP-IDF application entry point.

Variables

static const char *const TAG = "basic_usage"
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
static const uint8_t DEFAULT_PIN [] = "000000000"
 Demo PIN — replace with the PIN used during card initialisation.
static const size_t DEFAULT_PIN_LEN = sizeof(DEFAULT_PIN) - 1U

Macro Definition Documentation

◆ I2C_ENABLED

#define I2C_ENABLED   0

Definition at line 50 of file main.cpp.

◆ NFC_CS

#define NFC_CS   10

◆ SPI_ENABLED

#define SPI_ENABLED   1

Definition at line 49 of file main.cpp.

◆ SPI_MAX_TRANSFER_SZ

#define SPI_MAX_TRANSFER_SZ   4096

◆ SPI_MISO

#define SPI_MISO   13

◆ SPI_MOSI

#define SPI_MOSI   11

◆ SPI_PIN_UNUSED

#define SPI_PIN_UNUSED   (-1)

◆ SPI_SCLK

#define SPI_SCLK   12

◆ WIFI_CONNECTED_BIT

#define WIFI_CONNECTED_BIT   BIT0

◆ WIFI_FAIL_BIT

Function Documentation

◆ app_main()

◆ run_basic_usage_loop()

void run_basic_usage_loop ( CryptnoxWallet & wallet)
static

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

Each iteration establishes the secure channel, signs a 32-byte test hash, prints the raw r‖s bytes, securely wipes sensitive buffers, then disconnects. Halts permanently on CW_SIGN_PIN_INCORRECT to protect the card's retry counter.

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

Definition at line 192 of file main.cpp.

References DEFAULT_PIN, DEFAULT_PIN_LEN, 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).
Examples
BasicUsage/main/main.cpp, Connect/main/main.cpp, Sign/main/main.cpp, and VerifyPin/main/main.cpp.

Definition at line 95 of file main.cpp.

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

Referenced by eth_rpc_wifi_connect(), and wifi_start().

◆ 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.
Examples
BasicUsage/main/main.cpp, Connect/main/main.cpp, Sign/main/main.cpp, and VerifyPin/main/main.cpp.

Definition at line 130 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.

Referenced by app_main().

Variable Documentation

◆ DEFAULT_PIN

const uint8_t DEFAULT_PIN[] = "000000000"
static

Demo PIN — replace with the PIN used during card initialisation.

Examples
BasicUsage/main/main.cpp.

Definition at line 175 of file main.cpp.

Referenced by run_basic_usage_loop().

◆ DEFAULT_PIN_LEN

const size_t DEFAULT_PIN_LEN = sizeof(DEFAULT_PIN) - 1U
static
Examples
BasicUsage/main/main.cpp.

Definition at line 176 of file main.cpp.

Referenced by run_basic_usage_loop().

◆ LOOP_DELAY_MS

const uint32_t LOOP_DELAY_MS = 1000U
static

◆ s_retry_num

◆ s_wifi_event_group

EventGroupHandle_t s_wifi_event_group
static

◆ TAG

const char* const TAG = "basic_usage"
static

Definition at line 72 of file main.cpp.

◆ WIFI_MAX_RETRY

const int WIFI_MAX_RETRY = 5
static

Definition at line 75 of file main.cpp.

◆ WIFI_TIMEOUT_MS

const uint32_t WIFI_TIMEOUT_MS = 10000U
static

Definition at line 74 of file main.cpp.