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

Wi-Fi station bring-up (init/scan/connect/RSSI) and SNTP time sync. More...

#include "net.h"
#include "civil_time.h"
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <sys/time.h>
#include <inttypes.h>
#include "CW_Utils.h"
#include "freertos/FreeRTOS.h"
#include "freertos/event_groups.h"
#include "esp_wifi.h"
#include "esp_event.h"
#include "esp_netif.h"
#include "esp_netif_sntp.h"
#include "esp_app_desc.h"
#include "esp_timer.h"
#include "wdt.h"
#include "esp_log.h"
Include dependency graph for net.cpp:

Go to the source code of this file.

Macros

#define WIFI_CONNECTED_BIT   BIT0
#define WIFI_FAIL_BIT   BIT1
#define WIFI_MAX_RETRY   2
#define WIFI_TIMEOUT_MS   15000
#define REJOIN_FIRST_S   2U
#define REJOIN_MAX_S   30U
#define BUILD_FLOOR_SLACK_S   86400

Functions

static void rejoin_cb (void *arg)
 Background re-join: one association attempt, from the timer task.
static void rejoin_schedule (void)
 Schedule the next re-join, doubling the wait up to REJOIN_MAX_S.
static void rejoin_cancel (void)
 Stop the background re-join and reset its backoff.
static void wifi_event_handler (void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data)
 WiFi/IP event handler driving the connect retry state machine.
void net_wifi_init (void)
 Bring up the WiFi driver in station mode (idempotent).
bool net_ap_start (const char *ssid, const char *pass)
 Raise a WPA2 SoftAP as the radio's only interface, for phone-based configuration.
void net_wifi_disconnect (void)
 Drop the station association, without the retry loop pulling it back.
void net_ap_stop (void)
 Drop the SoftAP, return the radio to station-only, and re-join.
uint16_t net_wifi_scan (net_wifi_ap_t *out, uint16_t max)
 Scan for nearby access points (blocking).
bool net_wifi_connect (const char *ssid, const char *password)
 Connect to a WiFi network and block until an IP is obtained (up to 30 s). May be called repeatedly to switch networks.
void net_wifi_keep_trying (void)
 Hold on to the network last configured, even though it is down now.
bool net_wifi_online (void)
 true while the station holds an IP address.
bool net_wifi_reconnecting (void)
 true while a kept network is down and the re-join is running.
bool net_wifi_rssi (int8_t *rssi_out)
 Read the RSSI of the currently associated access point.
static int64_t build_time_floor (void)
 Earliest system time this firmware will accept, as a Unix epoch.
static void on_time_sync (struct timeval *tv)
 Every SNTP update, foreground or background, lands here first.
static bool sntp_start (void)
 Subscribe the SNTP client, without waiting for it.
void net_time_background (void)
 Subscribe SNTP without waiting, so the clock is set whenever the network comes back.
bool net_time_sync (uint32_t timeout_ms)
 Block until the system clock has been set via SNTP and sanity-checked.

Variables

static const char *const TAG = "net"
static EventGroupHandle_t s_wifi_event_group = NULL
static int s_retry_num = 0
static bool s_wifi_inited = false
static bool s_sntp_running = false
static volatile bool s_keep = false
static esp_timer_handle_t s_rejoin_timer = NULL
static uint32_t s_rejoin_s = 0U
static volatile bool s_ap_up = false
static esp_netif_t * s_ap_netif = NULL

Detailed Description

Wi-Fi station bring-up (init/scan/connect/RSSI) and SNTP time sync.

Definition in file net.cpp.

Macro Definition Documentation

◆ BUILD_FLOOR_SLACK_S

#define BUILD_FLOOR_SLACK_S   86400

Definition at line 465 of file net.cpp.

Referenced by build_time_floor().

◆ REJOIN_FIRST_S

#define REJOIN_FIRST_S   2U

Definition at line 56 of file net.cpp.

Referenced by rejoin_schedule().

◆ REJOIN_MAX_S

#define REJOIN_MAX_S   30U

Definition at line 57 of file net.cpp.

Referenced by rejoin_schedule().

◆ WIFI_CONNECTED_BIT

#define WIFI_CONNECTED_BIT   BIT0

Definition at line 42 of file net.cpp.

Referenced by net_wifi_connect(), net_wifi_online(), and wifi_event_handler().

◆ WIFI_FAIL_BIT

#define WIFI_FAIL_BIT   BIT1

Definition at line 43 of file net.cpp.

Referenced by net_wifi_connect(), and wifi_event_handler().

◆ WIFI_MAX_RETRY

#define WIFI_MAX_RETRY   2

Definition at line 46 of file net.cpp.

Referenced by net_wifi_disconnect(), rejoin_cb(), and wifi_event_handler().

◆ WIFI_TIMEOUT_MS

#define WIFI_TIMEOUT_MS   15000

Definition at line 50 of file net.cpp.

Referenced by net_wifi_connect().

Function Documentation

◆ build_time_floor()

int64_t build_time_floor ( void )
static

Earliest system time this firmware will accept, as a Unix epoch.

Derived from the firmware's own build timestamp: a clock at or after the build instant cannot make an already-expired certificate look valid, and pushing the clock forward is harmless for the same reason.

Returns
Epoch-second floor, or 0 (no floor) if the build stamp is unparseable — fail-open, so a toolchain that deviates from the standard DATE format cannot brick the terminal. The host self-check (fuzz/test_civil_time.cpp) asserts our own stamp parses.

Definition at line 479 of file net.cpp.

References BUILD_FLOOR_SLACK_S, civil_parse_build_stamp(), and TAG.

Referenced by net_time_sync(), and on_time_sync().

◆ net_ap_start()

bool net_ap_start ( const char * ssid,
const char * pass )

Raise a WPA2 SoftAP as the radio's only interface, for phone-based configuration.

AP-only, not APSTA, and the station association is dropped on the way in. The config portal's HTTP server binds every interface (esp_http_server offers no bind address), so an APSTA terminal answers the payout forms on the venue LAN as well as on the AP — which is exactly where the AP passphrase guards nothing. Taking the station down makes the AP the only door there is.

The station is borrowed back where it is genuinely needed and only for as long as that takes: net_wifi_scan flips to APSTA for the scan itself, and net_wifi_connect for the join the operator asked for. net_ap_stop re-associates afterwards.

One radio, one channel: when the station associates, the SoftAP is dragged onto the station's channel and any joined phone is dropped. That is expected — the setup page warns before it submits Wi-Fi credentials, and the device screen, not the phone, reports the outcome.

Parameters
[in]ssidAP SSID.
[in]passWPA2 passphrase; must be at least 8 characters.
Returns
true once the AP interface is configured and up.

Definition at line 215 of file net.cpp.

References net_wifi_disconnect(), net_wifi_init(), s_ap_netif, s_ap_up, and TAG.

Referenced by prov_start().

◆ net_ap_stop()

void net_ap_stop ( void )

Drop the SoftAP, return the radio to station-only, and re-join.

The association net_ap_start displaced is put back if it is not already up: the driver still holds the credentials, so only the association went away. Without it, closing the config page would leave a working terminal offline until somebody rebooted it.

Definition at line 286 of file net.cpp.

References s_ap_netif, s_ap_up, s_keep, s_retry_num, and TAG.

Referenced by prov_start(), and prov_stop().

◆ net_time_background()

void net_time_background ( void )

Subscribe SNTP without waiting, so the clock is set whenever the network comes back.

For a boot that could not sync in the foreground. lwIP retries on its own; the build-time floor net_time_sync enforces is applied to every update, so a back-dated packet clears the clock rather than setting it. Idempotent.

Definition at line 529 of file net.cpp.

References s_sntp_running, sntp_start(), and TAG.

Referenced by pos_boot().

◆ net_time_sync()

bool net_time_sync ( uint32_t timeout_ms)

Block until the system clock has been set via SNTP and sanity-checked.

Must be called after net_wifi_connect() and before any HTTPS request: without real time, TLS certificate validity-period checks are meaningless. Those checks are only actually compiled in when CONFIG_MBEDTLS_HAVE_TIME_DATE is set (see sdkconfig.defaults) — HAVE_TIME alone is not enough. SNTP keeps running in the background for periodic resyncs.

Callable repeatedly: each call re-subscribes and waits for a fresh packet, so it doubles as a probe for whether the network just joined reaches the internet.

SNTP is unauthenticated, so a synced time earlier than the firmware's own build timestamp is rejected: back-dating is what an attacker needs to make an expired certificate look valid again. Moving the clock forward is harmless and is not restricted. A rejection returns false like any other failure, so the caller's existing "no network time" path applies.

Parameters
[in]timeout_msMaximum time to wait for the sync.
Returns
true once the clock is set and passes the lower-bound check, false on init error, timeout, or a back-dated clock.

Definition at line 536 of file net.cpp.

References build_time_floor(), s_sntp_running, sntp_start(), TAG, and wdt_feed().

Referenced by app_main(), run_wizard(), and sync_time().

◆ net_wifi_connect()

bool net_wifi_connect ( const char * ssid,
const char * password )

Connect to a WiFi network and block until an IP is obtained (up to 30 s). May be called repeatedly to switch networks.

Parameters
[in]ssidNetwork SSID.
[in]passwordPassphrase (empty string for an open network).
Returns
true on success, false on timeout or repeated association failure.

Definition at line 376 of file net.cpp.

References net_wifi_init(), rejoin_cancel(), s_ap_up, s_keep, s_retry_num, s_wifi_event_group, TAG, wdt_feed(), WIFI_CONNECTED_BIT, WIFI_FAIL_BIT, and WIFI_TIMEOUT_MS.

Referenced by app_main(), run_wizard(), wifi_picker(), and wifi_try_saved().

◆ net_wifi_disconnect()

void net_wifi_disconnect ( void )

Drop the station association, without the retry loop pulling it back.

For a join that worked but is not being kept: the setup portal's HTTP server binds every interface, so an associated station puts the setup forms on the venue LAN as well as on the SoftAP. Leaving a test association up while that portal is still running hands the forms to everyone holding the venue PSK. With a SoftAP up this also returns the radio to AP-only, so the station interface goes down with the association rather than lingering idle. Idempotent, and safe before any connect.

Definition at line 268 of file net.cpp.

References rejoin_cancel(), s_ap_up, s_keep, s_retry_num, s_wifi_inited, TAG, and WIFI_MAX_RETRY.

Referenced by app_main(), net_ap_start(), and run_wizard().

◆ net_wifi_init()

void net_wifi_init ( void )

Bring up the WiFi driver in station mode (idempotent).

Initialises netif, the event loop and the WiFi driver and starts the STA. Call once before scanning or connecting. Safe to call repeatedly.

Definition at line 175 of file net.cpp.

References rejoin_cb(), s_rejoin_timer, s_retry_num, s_wifi_event_group, s_wifi_inited, and wifi_event_handler().

Referenced by net_ap_start(), net_wifi_connect(), net_wifi_scan(), pos_boot(), and prov_start().

◆ net_wifi_keep_trying()

void net_wifi_keep_trying ( void )

Hold on to the network last configured, even though it is down now.

A join that succeeds already does this: from then on a drop past the immediate retries hands over to a background re-join (2 s doubling to every 30 s, for as long as it takes). This is for the unit that boots while its router is still coming up — the saved network was tried and failed, and should be tried again in the background rather than never. No-op while the SoftAP is up, and undone by net_wifi_disconnect and net_wifi_connect.

Definition at line 430 of file net.cpp.

References rejoin_schedule(), s_ap_up, s_keep, and s_wifi_inited.

Referenced by app_main(), and pos_boot().

◆ net_wifi_online()

bool net_wifi_online ( void )

true while the station holds an IP address.

Definition at line 437 of file net.cpp.

References s_wifi_event_group, and WIFI_CONNECTED_BIT.

Referenced by app_main(), and net_wifi_reconnecting().

◆ net_wifi_reconnecting()

bool net_wifi_reconnecting ( void )

true while a kept network is down and the re-join is running.

Definition at line 443 of file net.cpp.

References net_wifi_online(), and s_keep.

Referenced by signal_refresh().

◆ net_wifi_rssi()

bool net_wifi_rssi ( int8_t * rssi_out)

Read the RSSI of the currently associated access point.

Parameters
[out]rssi_outSignal strength in dBm (closer to 0 = stronger); untouched when not associated.
Returns
true if associated and rssi_out was written, false otherwise.

Definition at line 448 of file net.cpp.

Referenced by build_settings(), and signal_refresh().

◆ net_wifi_scan()

uint16_t net_wifi_scan ( net_wifi_ap_t * out,
uint16_t max )

Scan for nearby access points (blocking).

Parameters
[out]outArray to fill with de-duplicated APs.
[in]maxCapacity of out.
Returns
number of APs written (0 on error or none found).

Definition at line 322 of file net.cpp.

References net_wifi_init(), net_wifi_ap_t::open, net_wifi_ap_t::rssi, s_ap_up, net_wifi_ap_t::ssid, and wdt_feed().

Referenced by app_main(), run_wizard(), and wifi_picker().

◆ on_time_sync()

void on_time_sync ( struct timeval * tv)
static

Every SNTP update, foreground or background, lands here first.

The same floor net_time_sync applies after its wait, applied to the background resyncs too: once SNTP is left subscribed, lwIP sets the clock on its own, and a back-dated packet arriving then would otherwise be adopted with nobody checking. Refused by putting the clock back to "unset" — every TLS request then fails closed, and the sale path says it has no time.

Definition at line 501 of file net.cpp.

References build_time_floor(), and TAG.

Referenced by sntp_start().

◆ rejoin_cancel()

void rejoin_cancel ( void )
static

Stop the background re-join and reset its backoff.

Definition at line 116 of file net.cpp.

References s_rejoin_s, and s_rejoin_timer.

Referenced by net_wifi_connect(), net_wifi_disconnect(), and wifi_event_handler().

◆ rejoin_cb()

void rejoin_cb ( void * arg)
static

Background re-join: one association attempt, from the timer task.

Definition at line 87 of file net.cpp.

References s_ap_up, s_keep, s_retry_num, TAG, and WIFI_MAX_RETRY.

Referenced by net_wifi_init().

◆ rejoin_schedule()

void rejoin_schedule ( void )
static

Schedule the next re-join, doubling the wait up to REJOIN_MAX_S.

Definition at line 99 of file net.cpp.

References REJOIN_FIRST_S, REJOIN_MAX_S, s_ap_up, s_keep, s_rejoin_s, s_rejoin_timer, and TAG.

Referenced by net_wifi_keep_trying(), and wifi_event_handler().

◆ sntp_start()

bool sntp_start ( void )
static

Subscribe the SNTP client, without waiting for it.

Definition at line 514 of file net.cpp.

References on_time_sync(), and TAG.

Referenced by net_time_background(), and net_time_sync().

◆ wifi_event_handler()

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

WiFi/IP event handler driving the connect retry state machine.

Retries the association up to WIFI_MAX_RETRY times, then signals WIFI_FAIL_BIT — and, for an association that is being kept, hands over to the background re-join, which keeps trying with backoff for as long as it takes. Signals WIFI_CONNECTED_BIT once an IP is bound.

Parameters
[in]argUnused.
[in]event_baseEvent base (WIFI_EVENT or IP_EVENT).
[in]event_idEvent identifier within event_base.
[in]event_dataUnused.

Definition at line 135 of file net.cpp.

References rejoin_cancel(), rejoin_schedule(), s_retry_num, s_wifi_event_group, TAG, WIFI_CONNECTED_BIT, WIFI_FAIL_BIT, and WIFI_MAX_RETRY.

Referenced by net_wifi_init().

Variable Documentation

◆ s_ap_netif

esp_netif_t* s_ap_netif = NULL
static

Definition at line 213 of file net.cpp.

Referenced by net_ap_start(), and net_ap_stop().

◆ s_ap_up

volatile bool s_ap_up = false
static

◆ s_keep

volatile bool s_keep = false
static

◆ s_rejoin_s

uint32_t s_rejoin_s = 0U
static

Definition at line 74 of file net.cpp.

Referenced by rejoin_cancel(), and rejoin_schedule().

◆ s_rejoin_timer

esp_timer_handle_t s_rejoin_timer = NULL
static

Definition at line 73 of file net.cpp.

Referenced by net_wifi_init(), rejoin_cancel(), and rejoin_schedule().

◆ s_retry_num

int s_retry_num = 0
static

◆ s_sntp_running

bool s_sntp_running = false
static

Definition at line 66 of file net.cpp.

Referenced by net_time_background(), and net_time_sync().

◆ s_wifi_event_group

EventGroupHandle_t s_wifi_event_group = NULL
static

Definition at line 63 of file net.cpp.

Referenced by net_wifi_connect(), net_wifi_init(), net_wifi_online(), and wifi_event_handler().

◆ s_wifi_inited

bool s_wifi_inited = false
static

Definition at line 65 of file net.cpp.

Referenced by net_wifi_disconnect(), net_wifi_init(), and net_wifi_keep_trying().

◆ TAG

const char* const TAG = "net"
static

Definition at line 40 of file net.cpp.