|
cryptnox-pos 1.0.0
Standalone USDC payment terminal firmware (ESP32 + Cryptnox smart card)
|
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"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 |
Wi-Fi station bring-up (init/scan/connect/RSSI) and SNTP time sync.
Definition in file net.cpp.
| #define BUILD_FLOOR_SLACK_S 86400 |
Definition at line 465 of file net.cpp.
Referenced by build_time_floor().
| #define REJOIN_FIRST_S 2U |
Definition at line 56 of file net.cpp.
Referenced by rejoin_schedule().
| #define REJOIN_MAX_S 30U |
Definition at line 57 of file net.cpp.
Referenced by rejoin_schedule().
| #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().
| #define WIFI_FAIL_BIT BIT1 |
Definition at line 43 of file net.cpp.
Referenced by net_wifi_connect(), and wifi_event_handler().
| #define WIFI_MAX_RETRY 2 |
Definition at line 46 of file net.cpp.
Referenced by net_wifi_disconnect(), rejoin_cb(), and wifi_event_handler().
| #define WIFI_TIMEOUT_MS 15000 |
Definition at line 50 of file net.cpp.
Referenced by net_wifi_connect().
|
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.
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().
| 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.
| [in] | ssid | AP SSID. |
| [in] | pass | WPA2 passphrase; must be at least 8 characters. |
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().
| 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().
| 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().
| 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.
| [in] | timeout_ms | Maximum time to wait for the sync. |
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().
| 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.
| [in] | ssid | Network SSID. |
| [in] | password | Passphrase (empty string for an open network). |
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().
| 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().
| 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().
| 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().
| 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().
| 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().
| bool net_wifi_rssi | ( | int8_t * | rssi_out | ) |
Read the RSSI of the currently associated access point.
| [out] | rssi_out | Signal strength in dBm (closer to 0 = stronger); untouched when not associated. |
rssi_out was written, false otherwise. Definition at line 448 of file net.cpp.
Referenced by build_settings(), and signal_refresh().
| uint16_t net_wifi_scan | ( | net_wifi_ap_t * | out, |
| uint16_t | max ) |
Scan for nearby access points (blocking).
| [out] | out | Array to fill with de-duplicated APs. |
| [in] | max | Capacity of out. |
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().
|
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().
|
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().
|
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().
|
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().
|
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().
|
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.
| [in] | arg | Unused. |
| [in] | event_base | Event base (WIFI_EVENT or IP_EVENT). |
| [in] | event_id | Event identifier within event_base. |
| [in] | event_data | Unused. |
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().
|
static |
Definition at line 213 of file net.cpp.
Referenced by net_ap_start(), and net_ap_stop().
|
static |
Definition at line 80 of file net.cpp.
Referenced by net_ap_start(), net_ap_stop(), net_wifi_connect(), net_wifi_disconnect(), net_wifi_keep_trying(), net_wifi_scan(), rejoin_cb(), and rejoin_schedule().
|
static |
Definition at line 72 of file net.cpp.
Referenced by net_ap_stop(), net_wifi_connect(), net_wifi_disconnect(), net_wifi_keep_trying(), net_wifi_reconnecting(), rejoin_cb(), and rejoin_schedule().
|
static |
Definition at line 74 of file net.cpp.
Referenced by rejoin_cancel(), and rejoin_schedule().
|
static |
Definition at line 73 of file net.cpp.
Referenced by net_wifi_init(), rejoin_cancel(), and rejoin_schedule().
|
static |
Definition at line 64 of file net.cpp.
Referenced by net_ap_stop(), net_wifi_connect(), net_wifi_disconnect(), net_wifi_init(), rejoin_cb(), and wifi_event_handler().
|
static |
Definition at line 66 of file net.cpp.
Referenced by net_time_background(), and net_time_sync().
|
static |
Definition at line 63 of file net.cpp.
Referenced by net_wifi_connect(), net_wifi_init(), net_wifi_online(), and wifi_event_handler().
|
static |
Definition at line 65 of file net.cpp.
Referenced by net_wifi_disconnect(), net_wifi_init(), and net_wifi_keep_trying().