Files
nfc-pn532-warlord/firmware/components/nfc_engine/session_capture.c
drjones 2da1515f8d feat: resilient boot + full polish pass
Firmware:
- nfc_engine: add nfc_engine_try_init() (non-fatal, sets s_pn532_ready),
  nfc_engine_try_reattach() (soft re-init, skips bus re-init),
  nfc_engine_is_ready() accessor
- main: replace ESP_ERROR_CHECK(nfc_engine_init) with nfc_engine_try_init;
  device boots and serves web UI even with no PN532 connected
- app_net: add reconnect_task — every 5s retries nfc_engine_try_reattach()
  and broadcasts {"channel":"pn532","payload":{"connected":true}} over WS
- app_net: scan_loop_task skips polling when !nfc_engine_is_ready()
- app_net/api_status: always emit pn532Connected bool; null-guard pn532 fw object
- find_sector_hit / program_classic_snapshot_locked: null-guard cJSON array items
- session_capture: abort if xSemaphoreCreateMutex() returns NULL

Web:
- NfcWsContext: track pn532Connected state from WS pn532 channel + status fetch on connect
- App.tsx: live HeaderBadge (LIVE/NO RF/WAIT) replacing static text
- Dashboard: READY/SEARCHING pill with fw version when available
- api.ts: add pn532Connected to Status type
- toast.tsx: fix ID collision (Date.now + Math.random)
- Capture: surface status fetch errors
- ReadAnalyze: add error feedback for readUl when no data returned
- WriteClone: busy state on both write buttons
- RawConsole: toast when frame returns error not response
- Emulate: validate hex before send (non-empty, even length, hex chars only)
- Brute: warn and skip invalid custom key lines

Made-with: Cursor
2026-04-09 15:49:18 -07:00

161 lines
3.0 KiB
C

#include "nfc_engine/session_capture.h"
#include "freertos/FreeRTOS.h"
#include "freertos/semphr.h"
#include "esp_log.h"
#include <string.h>
#define SESSION_CAPTURE_BYTES (48 * 1024)
static char s_buf[SESSION_CAPTURE_BYTES];
static size_t s_len;
static uint32_t s_lines;
static bool s_full;
static bool s_deep;
static SemaphoreHandle_t s_mu;
void session_capture_init(void)
{
s_mu = xSemaphoreCreateMutex();
if (!s_mu) {
ESP_LOGE("session_capture", "mutex create failed — aborting");
abort();
}
session_capture_clear();
s_deep = false;
}
void session_capture_clear(void)
{
if (s_mu) {
xSemaphoreTake(s_mu, portMAX_DELAY);
}
s_len = 0;
s_lines = 0;
s_full = false;
s_buf[0] = 0;
if (s_mu) {
xSemaphoreGive(s_mu);
}
}
bool session_capture_is_full(void)
{
bool full = false;
if (s_mu) {
xSemaphoreTake(s_mu, portMAX_DELAY);
}
full = s_full;
if (s_mu) {
xSemaphoreGive(s_mu);
}
return full;
}
bool session_capture_deep_enabled(void)
{
bool deep = false;
if (s_mu) {
xSemaphoreTake(s_mu, portMAX_DELAY);
}
deep = s_deep;
if (s_mu) {
xSemaphoreGive(s_mu);
}
return deep;
}
void session_capture_set_deep(bool on)
{
if (s_mu) {
xSemaphoreTake(s_mu, portMAX_DELAY);
}
s_deep = on;
if (s_mu) {
xSemaphoreGive(s_mu);
}
}
size_t session_capture_max(void) { return sizeof(s_buf) - 2; }
void session_capture_get_status(size_t *used_bytes, uint32_t *line_count, bool *full)
{
if (s_mu) {
xSemaphoreTake(s_mu, portMAX_DELAY);
}
if (used_bytes) {
*used_bytes = s_len;
}
if (line_count) {
*line_count = s_lines;
}
if (full) {
*full = s_full;
}
if (s_mu) {
xSemaphoreGive(s_mu);
}
}
bool session_capture_append_line(const char *line)
{
if (!line) {
return false;
}
size_t l = strlen(line);
size_t need = l + 1; /* newline */
if (s_mu) {
xSemaphoreTake(s_mu, portMAX_DELAY);
}
if (s_full || s_len + need >= sizeof(s_buf)) {
s_full = true;
if (s_mu) {
xSemaphoreGive(s_mu);
}
return false;
}
memcpy(s_buf + s_len, line, l);
s_len += l;
s_buf[s_len++] = '\n';
s_buf[s_len] = 0;
s_lines++;
if (s_len + 2 >= sizeof(s_buf)) {
s_full = true;
}
if (s_mu) {
xSemaphoreGive(s_mu);
}
return true;
}
size_t session_capture_export_size(void)
{
if (s_mu) {
xSemaphoreTake(s_mu, portMAX_DELAY);
}
size_t n = s_len;
if (s_mu) {
xSemaphoreGive(s_mu);
}
return n;
}
void session_capture_copy_to(char *dst, size_t cap, size_t *out_len)
{
if (s_mu) {
xSemaphoreTake(s_mu, portMAX_DELAY);
}
size_t n = s_len;
if (n > cap) {
n = cap;
}
if (dst && n) {
memcpy(dst, s_buf, n);
}
if (out_len) {
*out_len = n;
}
if (s_mu) {
xSemaphoreGive(s_mu);
}
}