cryptnox-pos 1.0.0
Standalone USDC payment terminal firmware (ESP32 + Cryptnox smart card)
Loading...
Searching...
No Matches
hardening.cpp File Reference

Anomaly handling (§3.3): local fail-closed + persisted counter and a RAM ring buffer for on-site inspection. Never bricks. More...

#include "hardening.h"
#include <stdio.h>
#include "nvs.h"
#include "esp_log.h"
Include dependency graph for hardening.cpp:

Go to the source code of this file.

Classes

struct  anom_entry_t

Macros

#define NS_HARDEN   "harden"
#define K_ANOMALY   "anomaly_ct"
#define ANOM_RING   8U

Functions

static uint32_t nvs_load_ctr (void)
 Read the persisted anomaly counter from NVS.
static void nvs_store_ctr (uint32_t v)
 Persist the anomaly counter to NVS.
static void ensure_loaded (void)
 Lazily seed both counter copies from NVS on first use.
void pos_handle_anomaly (const char *where)
 Report an anomaly: bump a self-checked persisted counter, log it, and keep a local ring buffer for on-site inspection.
uint32_t pos_anomaly_count (void)
 Persisted total anomaly count (for the optional maintenance view).

Variables

static const char *const TAG = "harden"
static anom_entry_t s_ring [ANOM_RING]
static uint32_t s_ring_head = 0U
static volatile uint32_t s_ctr = 0U
static volatile uint32_t s_ctr_echo = 0U
static bool s_loaded = false

Detailed Description

Anomaly handling (§3.3): local fail-closed + persisted counter and a RAM ring buffer for on-site inspection. Never bricks.

Definition in file hardening.cpp.

Macro Definition Documentation

◆ ANOM_RING

#define ANOM_RING   8U

Definition at line 25 of file hardening.cpp.

Referenced by pos_handle_anomaly().

◆ K_ANOMALY

#define K_ANOMALY   "anomaly_ct"

Definition at line 21 of file hardening.cpp.

Referenced by nvs_load_ctr(), and nvs_store_ctr().

◆ NS_HARDEN

#define NS_HARDEN   "harden"

Definition at line 20 of file hardening.cpp.

Referenced by nvs_load_ctr(), and nvs_store_ctr().

Function Documentation

◆ ensure_loaded()

void ensure_loaded ( void )
static

Lazily seed both counter copies from NVS on first use.

Idempotent: later calls are no-ops, so every public entry point can call it without caring whether NVS has been read yet.

Definition at line 89 of file hardening.cpp.

References nvs_load_ctr(), s_ctr, s_ctr_echo, and s_loaded.

Referenced by pos_anomaly_count(), and pos_handle_anomaly().

◆ nvs_load_ctr()

uint32_t nvs_load_ctr ( void )
static

Read the persisted anomaly counter from NVS.

Returns
The stored count, or 0 when nothing is stored yet or the entry could not be read (never fails — a lost history must not brick).

Definition at line 44 of file hardening.cpp.

References K_ANOMALY, NS_HARDEN, and TAG.

Referenced by ensure_loaded().

◆ nvs_store_ctr()

void nvs_store_ctr ( uint32_t v)
static

Persist the anomaly counter to NVS.

Parameters
[in]vCounter value to store. Write failures are logged and ignored.

Definition at line 71 of file hardening.cpp.

References K_ANOMALY, NS_HARDEN, and TAG.

Referenced by pos_handle_anomaly().

◆ pos_anomaly_count()

uint32_t pos_anomaly_count ( void )

Persisted total anomaly count (for the optional maintenance view).

Definition at line 125 of file hardening.cpp.

References ensure_loaded(), and s_ctr.

◆ pos_handle_anomaly()

void pos_handle_anomaly ( const char * where)

Report an anomaly: bump a self-checked persisted counter, log it, and keep a local ring buffer for on-site inspection.

Fails the current transaction closed (the caller's gate returns FALSE32); never bricks — a keyless POS holds no secret to protect (§3.3). Firmware only (NVS + esp_log); defined in hardening.cpp.

Definition at line 99 of file hardening.cpp.

References ANOM_RING, anom_entry_t::at_count, ensure_loaded(), nvs_store_ctr(), s_ctr, s_ctr_echo, s_ring, s_ring_head, TAG, and anom_entry_t::where.

Referenced by app_main(), settle_inflight(), sign_and_broadcast(), and sign_and_broadcast_tron().

Variable Documentation

◆ s_ctr

volatile uint32_t s_ctr = 0U
static

Definition at line 34 of file hardening.cpp.

Referenced by ensure_loaded(), pos_anomaly_count(), and pos_handle_anomaly().

◆ s_ctr_echo

volatile uint32_t s_ctr_echo = 0U
static

Definition at line 35 of file hardening.cpp.

Referenced by ensure_loaded(), and pos_handle_anomaly().

◆ s_loaded

bool s_loaded = false
static

Definition at line 36 of file hardening.cpp.

Referenced by ensure_loaded().

◆ s_ring

anom_entry_t s_ring[ANOM_RING]
static

Definition at line 30 of file hardening.cpp.

Referenced by pos_handle_anomaly().

◆ s_ring_head

uint32_t s_ring_head = 0U
static

Definition at line 31 of file hardening.cpp.

Referenced by pos_handle_anomaly().

◆ TAG

const char* const TAG = "harden"
static

Definition at line 18 of file hardening.cpp.