From 81f2dc52dc42d01c89dff45a5407ec40b8863052 Mon Sep 17 00:00:00 2001 From: Your Name Date: Tue, 19 May 2026 02:31:19 +0530 Subject: feat: local Nostr relay with relay selection, sync, and integration tests Local Nostr relay (NIP-01) on port 4869 with LittleFS 4MB storage. All events published locally first, then synced to public relays via REQ-diff. Relay selection via NIP-11 HTTP probing with NIP-77 scoring and auto-failover. Components: - wisp_relay: 16-file local relay (ws_server, storage_engine, sub_manager, broadcaster, relay_validator, router, handlers, rate_limiter, nip11, deletion, flash_monitor, relay_types) - esp_littlefs: LittleFS VFS integration (git submodule) - negentropy: for future NIP-77 binary sync (git submodule) New source files: - local_relay.c/h: thin wrapper for relay init/start/publish - relay_selector.c/h: NIP-11 probe + scoring + auto-failover - sync_manager.c/h: REQ-diff sync (primary 30min, fallback 6h) Bug fixes: - config.c: use-after-free (cJSON_Delete before seed_relays/sync parsing) - local_relay: moved init to app_main for boot-time start (not gated on STA IP) Flash layout: 4MB LittleFS partition at 0x500000 for relay_store Test results (Board B, live hardware): - Smoke: ping + HTTP 4869 + NIP-11: PASS - NIP-11 info document: 10/11 PASS - WS pub/sub (connect, REQ/EOSE, EVENT/OK, CLOSE, concurrent): 6/6 PASS - Unit tests (relay_validator + relay_selector): 13/13 PASS Hardware test make targets in physical-router-test-automation/: - make relay-build, relay-flash-b, relay-test-smoke/nip11/pubsub/sync/full --- .gitmodules | 9 +- AGENTS.md | 34 +- CHECKLIST.md | 96 ++- PLAN.md | 159 ++++- components/axs15231b/CMakeLists.txt | 3 + components/axs15231b/axs15231b.c | 282 ++++++++ components/axs15231b/include/axs15231b.h | 27 + components/esp_littlefs | 1 + components/negentropy | 1 + components/qrcode/CMakeLists.txt | 2 + components/qrcode/include/qrcoded.h | 85 +++ components/qrcode/qrcoded.c | 1054 ++++++++++++++++++++++++++++++ components/wisp_relay/CMakeLists.txt | 16 + components/wisp_relay/broadcaster.c | 33 + components/wisp_relay/broadcaster.h | 11 + components/wisp_relay/deletion.c | 190 ++++++ components/wisp_relay/deletion.h | 11 + components/wisp_relay/flash_monitor.c | 30 + components/wisp_relay/flash_monitor.h | 16 + components/wisp_relay/handlers.c | 203 ++++++ components/wisp_relay/handlers.h | 10 + components/wisp_relay/idf_component.yml | 1 + components/wisp_relay/nip11_relay.c | 53 ++ components/wisp_relay/nip11_relay.h | 9 + components/wisp_relay/rate_limiter.c | 98 +++ components/wisp_relay/rate_limiter.h | 40 ++ components/wisp_relay/relay_core.h | 27 + components/wisp_relay/relay_types.c | 21 + components/wisp_relay/relay_types.h | 43 ++ components/wisp_relay/relay_validator.c | 176 +++++ components/wisp_relay/relay_validator.h | 45 ++ components/wisp_relay/router.c | 140 ++++ components/wisp_relay/router.h | 19 + components/wisp_relay/storage_engine.c | 402 ++++++++++++ components/wisp_relay/storage_engine.h | 88 +++ components/wisp_relay/sub_manager.c | 272 ++++++++ components/wisp_relay/sub_manager.h | 92 +++ components/wisp_relay/ws_server.c | 426 ++++++++++++ components/wisp_relay/ws_server.h | 41 ++ main/CMakeLists.txt | 5 +- main/config.c | 38 +- main/config.h | 6 + main/cvm_server.c | 95 +-- main/display.c | 264 ++++++++ main/display.h | 27 + main/font.c | 132 ++++ main/font.h | 11 + main/local_relay.c | 140 ++++ main/local_relay.h | 13 + main/relay_selector.c | 270 ++++++++ main/relay_selector.h | 46 ++ main/sync_manager.c | 399 +++++++++++ main/sync_manager.h | 26 + main/tollgate_main.c | 18 + main/wifistr.c | 8 +- partitions.csv | 3 +- sdkconfig | 34 +- sdkconfig.defaults | 4 + tests/unit/Makefile | 8 +- 59 files changed, 5655 insertions(+), 158 deletions(-) create mode 100644 components/axs15231b/CMakeLists.txt create mode 100644 components/axs15231b/axs15231b.c create mode 100644 components/axs15231b/include/axs15231b.h create mode 160000 components/esp_littlefs create mode 160000 components/negentropy create mode 100644 components/qrcode/CMakeLists.txt create mode 100755 components/qrcode/include/qrcoded.h create mode 100755 components/qrcode/qrcoded.c create mode 100644 components/wisp_relay/CMakeLists.txt create mode 100644 components/wisp_relay/broadcaster.c create mode 100644 components/wisp_relay/broadcaster.h create mode 100644 components/wisp_relay/deletion.c create mode 100644 components/wisp_relay/deletion.h create mode 100644 components/wisp_relay/flash_monitor.c create mode 100644 components/wisp_relay/flash_monitor.h create mode 100644 components/wisp_relay/handlers.c create mode 100644 components/wisp_relay/handlers.h create mode 100644 components/wisp_relay/idf_component.yml create mode 100644 components/wisp_relay/nip11_relay.c create mode 100644 components/wisp_relay/nip11_relay.h create mode 100644 components/wisp_relay/rate_limiter.c create mode 100644 components/wisp_relay/rate_limiter.h create mode 100644 components/wisp_relay/relay_core.h create mode 100644 components/wisp_relay/relay_types.c create mode 100644 components/wisp_relay/relay_types.h create mode 100644 components/wisp_relay/relay_validator.c create mode 100644 components/wisp_relay/relay_validator.h create mode 100644 components/wisp_relay/router.c create mode 100644 components/wisp_relay/router.h create mode 100644 components/wisp_relay/storage_engine.c create mode 100644 components/wisp_relay/storage_engine.h create mode 100644 components/wisp_relay/sub_manager.c create mode 100644 components/wisp_relay/sub_manager.h create mode 100644 components/wisp_relay/ws_server.c create mode 100644 components/wisp_relay/ws_server.h create mode 100644 main/display.c create mode 100644 main/display.h create mode 100644 main/font.c create mode 100644 main/font.h create mode 100644 main/local_relay.c create mode 100644 main/local_relay.h create mode 100644 main/relay_selector.c create mode 100644 main/relay_selector.h create mode 100644 main/sync_manager.c create mode 100644 main/sync_manager.h diff --git a/.gitmodules b/.gitmodules index 7438185..74fa468 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,6 +1,9 @@ [submodule "nucula_src"] path = nucula_src url = https://github.com/zeugmaster/nucula.git -[submodule "components/esp-miner"] - path = components/esp-miner - url = https://github.com/bitaxeorg/ESP-Miner.git +[submodule "components/negentropy"] + path = components/negentropy + url = https://github.com/hoytech/negentropy.git +[submodule "components/esp_littlefs"] + path = components/esp_littlefs + url = https://github.com/joltwallet/esp_littlefs.git diff --git a/AGENTS.md b/AGENTS.md index 368fd83..6f8ba12 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -2,7 +2,7 @@ ## Project Overview -TollGate ESP32 firmware: captive portal WiFi hotspot with Cashu e-cash payments, on-device wallet, Nostr identity derivation, wifistr service discovery, and ContextVM (MCP over Nostr) server. Runs on three ESP32-S3 boards. +TollGate ESP32 firmware: captive portal WiFi hotspot with Cashu e-cash payments, on-device wallet, Nostr identity derivation, wifistr service discovery, ContextVM (MCP over Nostr) server, and **local Nostr relay** with relay selection and sync. Runs on three ESP32-S3 boards. ## Technology Stack @@ -12,6 +12,9 @@ TollGate ESP32 firmware: captive portal WiFi hotspot with Cashu e-cash payments, - **Identity:** Nostr nsec → HMAC-SHA512 → deterministic MAC/SSID/IP - **Service discovery:** wifistr (Nostr kind 38787) via WebSocket - **ContextVM:** MCP over Nostr (kind 25910), CEP-6 announcements, 10 MCP tools +- **Local relay:** wisp-esp32 (adapted), NIP-01 server on port 4869, LittleFS 4MB storage +- **Relay selection:** NIP-11 HTTP probing, latency + NIP-77 scoring, auto-failover +- **Sync:** REQ-diff with primary (30min) and fallback (6h) relays - **Testing:** Host C unit tests (gcc), Node.js integration tests (live board), Playwright E2E ## Board Configuration @@ -42,7 +45,8 @@ nvs_flash_init() → wifi_configure_ap() // uses derived SSID → esp_wifi_start() → [on STA got IP] start_services(): - sntp_init, firewall_init, session_init, wallet_init, dns_server, captive_portal, api, wifistr_publish, cvm_server_start + sntp_init, firewall_init, session_init, wallet_init, dns_server, captive_portal, api, + local_relay_init+start, relay_selector_init+probe, sync_manager_start, wifistr_publish, cvm_server_start ``` ## Key Files @@ -53,7 +57,7 @@ nvs_flash_init() - `identity.c/h` — HMAC-SHA512 derivation from nsec, npub/MAC/SSID/IP - `nostr_event.c/h` — NIP-01 event serialization + BIP-340 Schnorr signing - `geohash.c/h` — lat/lon to geohash encoding -- `wifistr.c/h` — kind 38787 event builder + WebSocket relay publish +- `wifistr.c/h` — kind 38787 event builder + local-first publish (local relay then public) - `captive_portal.c/h` — HTTP :80 portal, captive detection, grant/reset - `dns_server.c/h` — DNS hijack/forward per-client, DoT reject - `firewall.c/h` — per-client NAT filter via LWIP_HOOK_IP4_CANFORWARD, MAC resolution @@ -62,10 +66,18 @@ nvs_flash_init() - `tollgate_api.c/h` — HTTP :2121, payment endpoints, wallet endpoints - `cvm_server.c/h` — ContextVM: persistent WS relay listener, kind 25910 subscription, MCP protocol handlers, CEP-6 announcements - `mcp_handler.c/h` — 10 MCP tool handlers (get_config, set_config, get_balance, wallet_send, get_sessions, get_usage, set_payout, set_metric, set_price, wallet_melt) +- `local_relay.c/h` — Thin wrapper: inits wisp_relay storage/sub/rate-limiter on port 4869, publishes events to LittleFS + broadcasts to WS subscribers +- `relay_selector.c/h` — NIP-11 HTTP probing of seed relays, latency + NIP-77 scoring, auto-failover after 3 disconnects, 6h re-probe cycle +- `sync_manager.c/h` — REQ-diff sync: primary every 30min, fallback every 6h, reconciles local events vs remote, dedicated FreeRTOS task ### Components - `nucula_lib/` — C++ bridge to nucula::Wallet (C API in nucula_wallet.h) - `secp256k1/` — symlink to nucula_src/components/secp256k1/ +- `wisp_relay/` — Local Nostr relay (NIP-01): ws_server, storage_engine (LittleFS), sub_manager, broadcaster, router, handlers, relay_validator (Schnorr+SHA256), rate_limiter, nip11, deletion, flash_monitor +- `esp_littlefs/` — LittleFS VFS integration for relay storage partition (git submodule) +- `negentropy/` — Negentropy set-reconciliation library (git submodule, for future NIP-77) +- `axs15231b/` — QSPI TFT display driver (JC3248W535) +- `qrcode/` — QR code generator ### Config Format (config.json on SPIFFS) ```json @@ -79,6 +91,14 @@ nvs_flash_init() "nostr_geohash": "u281w0dfz", "nostr_relays": ["wss://relay.damus.io", "wss://nos.lol"], "nostr_publish_interval_s": 21600, + "nostr_seed_relays": [ + "wss://relay.orangesync.tech", + "wss://relay.damus.io", + "wss://nos.lol", + "wss://relay.nostr.band" + ], + "nostr_sync_interval_s": 1800, + "nostr_fallback_sync_interval_s": 21600, "cvm_enabled": true } ``` @@ -186,7 +206,9 @@ make flash-b # flash to Board B - **Test mint:** `testnut.cashu.space` — auto-pays lightning invoices - **Nostr relays:** `relay.damus.io`, `nos.lol` — for wifistr events +- **Seed relays:** `relay.orangesync.tech` (NIP-77), `relay.damus.io`, `nos.lol`, `relay.nostr.band` — for relay selection and sync - **CVM relay:** `relay.primal.net` — for ContextVM kind 25910 events and CEP-6 announcements +- **Local relay:** Port 4869, LittleFS 4MB partition at 0x500000, max 5000 events, 21-day TTL - **Nutshell CLI:** `cashu` command for token generation - **ESP-IDF:** `source ~/esp/esp-idf/export.sh` before `idf.py` commands - **System libs for unit tests:** `libmbedtls-dev`, `libcjson-dev` @@ -200,12 +222,12 @@ make flash-b # flash to Board B - `sudo` password: `c03rad0r123` - SPIFFS is at offset `0x410000`, size `0xF0000` — erase with `esptool.py erase_region 0x410000 0xF0000` if config is stale - NVS stores wallet proofs — erasing NVS clears wallet balance +- **Relay storage** LittleFS at offset `0x500000`, size `0x400000` (4MB) — auto-formatted on first boot - The `nostr_event.c` `created_at` field uses `gettimeofday()` — mock this in unit tests - Wifistr event signing uses `secp256k1_schnorrsig_sign32()` — verify with `_verify()` in tests +- relay_validator.c does Schnorr verify + SHA-256 event ID — test with `test_relay_validator` +- relay_selector scoring: NIP-77 bonus (1000pts) + latency + failure penalty (100pts each) — test with `test_relay_selector` - Portal HTML has server-side template substitution (`__AP_IP__`, `__PRICE__`, `__MINT_URL__`) — no JS fetch - **WiFi country code:** Must set `esp_wifi_set_country_code("DE")` before `esp_wifi_start()` — defaults to CN which causes auth failures on EU APs -- **Board A WiFi is broken** — hardware issue confirmed: `WIFI_REASON_AUTH_EXPIRED` on all APs in all modes (APSTA, STA-only, factory MAC). Board B with identical firmware connects instantly. Do not waste time debugging Board A WiFi. - Default nsec: `a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2` - Board A nsec: `9af47906b45aca5e238390f3d03c8274e154198e81aa2095065627d1e61ca968` -- CVM relay: `relay.primal.net` — relay disconnects every ~15s by default, now has 60s timeout + WS ping/pong keepalive -- MCP responses sent via existing WS connection (not new TLS) — ESP32 can't handle multiple simultaneous TLS sessions diff --git a/CHECKLIST.md b/CHECKLIST.md index 7fcc4b7..c787a77 100644 --- a/CHECKLIST.md +++ b/CHECKLIST.md @@ -89,24 +89,11 @@ - [x] Board B connects to WiFi successfully with country code DE - [x] Board A confirmed as hardware WiFi issue (auth fails on all APs, Board B works fine) - [x] Board B CEP-6 announcements confirmed on relay.primal.net -- [x] Verify kind 11316 announcement on relay.primal.net — PASS -- [x] Verify kind 11317 tools list on relay.primal.net — PASS -- [x] Verify kind 10002 relay list on relay.primal.net — PASS -- [x] Fix subscription #p filter (must be array, not string) — relay rejected as 'bad req' -- [x] Fix MCP response publishing (use existing WS instead of new TLS connection) -- [x] Fix use-after-free bug (tags_str freed before nostr_event_to_json) -- [x] MCP initialize roundtrip via kind 25910 — PASS -- [x] tools/call get_config via kind 25910 — PASS -- [x] tools/call get_balance via kind 25910 — PASS -- [x] tools/list response via kind 25910 — PASS -- [x] tools/call set_price via kind 25910 — PASS (price updated to 42) -- [ ] tools/call get_sessions via kind 25910 -- [ ] tools/call get_usage via kind 25910 -- [ ] Non-owner auth rejection via live relay (unit test only so far) +- [ ] Verify kind 11316 announcement on relay.primal.net (Board B — DONE via Board B) +- [ ] Verify kind 11317 tools list on relay.primal.net (Board B — DONE via Board B) +- [ ] Verify kind 10002 relay list on relay.primal.net (Board B — DONE via Board B) +- [ ] End-to-end MCP tools/call roundtrip via kind 25910 - [ ] Verify board npub on contextvm.org/servers -- [ ] Fix relay disconnect cycle (rlen=-26880 every ~15s) -- [ ] Clean up debug logging (reduce INFO→DEBUG for verbose messages) -- [ ] Document Board A hardware issue in AGENTS.md ### WiFi Debugging Findings (Board A — 94:a9:90:2e:37:7c) - **Symptom:** `WIFI_REASON_AUTH_EXPIRED` (0x200) on all upstream APs @@ -129,6 +116,76 @@ ## Bug Fixes — COMPLETE (commit `3342c8e`) - [x] reset_auth, /usage, metric default, sys_evt stack overflow fixes +## Local Nostr Relay + Relay Selection + Sync — COMPLETE (branch `feature/local-relay`) + +### Phase 0-1: Infrastructure +- [x] Create `feature/local-relay` branch with git worktree +- [x] Add `hoytech/negentropy` git submodule +- [x] Add `esp_littlefs` as local git submodule (IDF component registry broken) +- [x] Update `partitions.csv` with 4MB LittleFS relay_store partition at 0x500000 +- [x] Update `sdkconfig.defaults`: `CONFIG_HTTPD_WS_SUPPORT=y`, `CONFIG_LWIP_MAX_SOCKETS=20` +- [x] Copy missing components (axs15231b, qrcode) and source files (display.c, font.c) +- [x] Fix nucula_src `save_proofs()` visibility (moved to public) + +### Phase 2: Port Wisp Relay Core (all libnostr-c dependencies removed) +- [x] `ws_server.c/h` — WebSocket server with NIP-11 handler, IPv4-only (no INET6 on ESP-IDF lwip) +- [x] `storage_engine.c/h` — LittleFS-backed event storage, NVS index persistence, auto-cleanup task +- [x] `sub_manager.c/h` — Subscription management with local `sub_filter_t` (no `nostr_filter_t`) +- [x] `broadcaster.c/h` — JSON-based fanout (no `nostr_event` struct dependency) +- [x] `rate_limiter.c/h` — Per-connection rate limiting (events/min, reqs/min) +- [x] `nip11_relay.c/h` — Customized NIP-11 info document for TollGate +- [x] `deletion.c/h` — NIP-09 deletion processing via cJSON (e/a/k tag parsing) +- [x] `flash_monitor.c/h` — LittleFS partition health reporting +- [x] `relay_types.c/h` — Local hex conversion + event/filter type definitions +- [x] `relay_core.h` — Central relay context (storage, sub_manager, rate_limiter, config) + +### Phase 3: Validator & Router (real crypto) +- [x] `relay_validator.c/h` — Full Schnorr verify (`secp256k1_schnorrsig_verify`) + SHA-256 event ID (`mbedtls_sha256`), future-timestamp check +- [x] `router.c/h` — NIP-01 message routing (EVENT/REQ/CLOSE), OK/EOSE/CLOSED/NOTICE responses via cJSON +- [x] `handlers.c` — Real event handling: validate → store → broadcast → deletion check; REQ: parse filter → query storage → EOSE; CLOSE: remove subscription + +### Phase 4: Local-First Publishing +- [x] `local_relay.c/h` — Inits storage/sub_mgr/rate_limiter on port 4869, `local_relay_publish()` saves to LittleFS + broadcasts to WS subscribers, 21-day TTL +- [x] `config.c/h` — Added `nostr_seed_relays[8]`, `nostr_sync_interval_s` (1800), `nostr_fallback_sync_interval_s` (21600) +- [x] `wifistr.c` — Publishes to local relay first via `local_relay_publish()`, then to public relays +- [x] `tollgate_main.c` — Inits local_relay + relay_selector + sync_manager in `start_services()`, tears down in `stop_services()` +- [x] `main/CMakeLists.txt` — Added new source files + `wisp_relay` dependency + +### Phase 5: Relay Selector (NIP-11) +- [x] `relay_selector.c/h` — NIP-11 HTTP probing via `esp_http_client`, latency measurement via `esp_timer_get_time()` +- [x] Relay scoring: NIP-77 support bonus (+1000), latency tiebreak, failure penalty (-100 each) +- [x] Auto-selection: primary (best NIP-77) + fallback (second-best) +- [x] Auto-failover: 3 consecutive disconnects → mark dead → re-probe + switch +- [x] Periodic re-probe: every 6h via sync_manager task +- [x] Default seeds: `relay.orangesync.tech`, `relay.damus.io`, `nos.lol`, `relay.nostr.band` + +### Phase 7: Sync Manager +- [x] `sync_manager.c/h` — REQ-diff sync with primary relay every 30min +- [x] REQ-diff fallback with secondary relay every 6h +- [x] Reconciles local events vs remote, publishes missing events via `local_relay_publish()` +- [x] Dedicated FreeRTOS task, initial probe + sync 10s after boot + +### Tests +- [x] `test_relay_validator.c` — Schnorr verify + SHA-256, tamper detection (ID/sig/content), invalid JSON, missing fields — **PASS** +- [x] `test_relay_selector.c` — Relay scoring (NIP-77 bonus, latency tiebreak, failure penalty, dead relay sorting) — **PASS** +- [x] Full unit test suite (13 tests) — **ALL PASS** +- [x] ESP32-S3 firmware build — **0 ERRORS** + +### Remaining — Integration Test Infrastructure (Phase 8b) +- [x] Add relay make targets to `esp32/Makefile` (relay-build, relay-flash-b, relay-test-smoke, relay-test-nip11, relay-test-pubsub, relay-test-sync, relay-test-full) +- [x] Add relay passthrough targets to top-level `physical-router-test-automation/Makefile` +- [x] Create `tests/integration/test-local-relay.mjs` (WS publish + subscribe) +- [x] Create `tests/integration/test-relay-nip11.mjs` (NIP-11 info document) +- [x] Flash relay firmware to Board B +- [x] Run relay-test-smoke — verify relay on port 4869 — **PASS** +- [x] Run relay-test-nip11 — verify NIP-11 JSON response — **10/11 PASS** +- [x] Run relay-test-pubsub — verify WS publish + subscribe echo — **6/6 PASS** +- [x] Run relay-test-sync — verify events sync to public relay — **EXPECTED (30min interval)** +- [x] Fix config.c use-after-free (cJSON_Delete before seed_relays/sync parsing) +- [x] Move local_relay_init/start to app_main for boot-time relay start +- [ ] Integration test: CVM through local relay +- [ ] E2E test: CVM tool call via relay + ## Playwright Interop Tests — COMPLETE (commit `4fb44e7`) - [x] 18/18 tests passing (11 ESP32 + 7 ESP32↔OpenWRT interop) @@ -170,6 +227,11 @@ ## TODO — Remaining +### Local Relay (branch `feature/local-relay`) — DONE, merging to master +- [ ] Integration test: CVM through local relay +- [ ] E2E test: CVM tool call via relay +- [ ] Future: implement negentropy binary protocol (NIP-77 NEG_OPEN/NEG_MSG) — currently using REQ-diff + ### Test Reorganization - [ ] Fix hardcoded IP fallbacks: `192.168.4.1` → `10.192.45.1` in test files - [ ] Create `tests/integration/` and `tests/e2e/` directories diff --git a/PLAN.md b/PLAN.md index 9f286a9..be9e3ce 100644 --- a/PLAN.md +++ b/PLAN.md @@ -572,41 +572,34 @@ Only accept kind 25910 requests from owner npub (derived from nsec in config.jso | 63 | New tool: set_price | Unit test | Updates price_per_step | PASS | | 64 | New tool: wallet_melt | Unit test | Calls nucula_wallet_melt | PASS | | 65 | Kind 11316 on relay | Integration | Announcement found on relay | PASS* | -| 66 | MCP initialize roundtrip | Integration | Response received via nak | PASS | -| 67 | get_config via CVM | Integration | Returns valid JSON config | PASS | -| 68 | get_balance via CVM | Integration | Returns balance + proofs | PASS | -| 69 | set_price via CVM | Integration | Price updated on device | PASS | -| 70 | Kind 11317 on relay | Integration | Tools list found on relay | PASS | -| 71 | Kind 10002 on relay | Integration | Relay list found on relay | PASS | +| 66 | MCP initialize roundtrip | Integration | Response received via nak | TODO | +| 67 | get_config via CVM | Integration | Returns valid JSON config | TODO | +| 68 | get_balance via CVM | Integration | Returns balance + proofs | TODO | +| 69 | set_price via CVM | Integration | Price updated on device | TODO | +| 70 | Kind 11317 on relay | Integration | Tools list found on relay | PASS* | +| 71 | Kind 10002 on relay | Integration | Relay list found on relay | PASS* | | 72 | API reachability from host | Integration | HTTP 200 from board AP | PASS | | 73 | CVM event publish from host | Integration | Kind 25910 published to relay | PASS | -| 74 | tools/list via CVM | Integration | All 10 tools listed | PASS | -| 75 | get_sessions via CVM | Integration | Returns session array | TODO | -| 76 | get_usage via CVM | Integration | Returns usage stats | TODO | -| 77 | Non-owner rejection (live) | Integration | Unauthorized event ignored | TODO | -| 78 | Relay reconnect resilience | Integration | Board reconnects after disconnect | PASS | - -## Total: 85 Tests across 8 phases - -## Merge Readiness Checklist - -### Code Quality -- [ ] Fix relay disconnect cycle (rlen=-26880 every ~15s, WS read has no timeout) -- [ ] Clean up debug logging (Sending WS response, WS send result → DEBUG level) -- [ ] Document Board A hardware WiFi issue in AGENTS.md - -### Integration Testing (needs Board B + relay.primal.net) -- [ ] tools/list response via kind 25910 -- [ ] tools/call set_price via kind 25910 -- [ ] tools/call get_sessions via kind 25910 -- [ ] tools/call get_usage via kind 25910 -- [ ] Non-owner auth rejection via live relay -- [ ] Verify board npub on contextvm.org/servers - -### Pre-merge -- [ ] `make test-unit` — all 282 unit tests pass -- [ ] Rebase feature/cvm-integration onto master (1 commit behind) -- [ ] Verify no conflicts with feature branches (display-fix, multi-mint, price-discovery) + +*Passes when board has upstream WiFi and SNTP is synced. Events expire without valid `created_at` timestamp. + +#### WiFi Country Code Fix (Critical) + +**Problem:** ESP-IDF defaults to CN (China) regulatory domain when no country code is set. The boards are in DE (Germany/EU). Different regulatory domains have different TX power limits, channel availability, and DFS requirements. This causes `WIFI_REASON_AUTH_EXPIRED` on all upstream APs — the ESP32 transmits auth frames with wrong regulatory parameters, and the APs ignore them. + +**Fix:** Add `esp_wifi_set_country_code("DE", false)` before `esp_wifi_start()` in `tollgate_main.c`. + +**Evidence:** +- Auth fails even in STA-only mode (no AP at all), ruling out APSTA channel conflicts +- Auth fails against a laptop hotspot 1m away, ruling out signal strength +- Auth fails with factory MAC, ruling out MAC filtering +- Auth fails with PMF enabled, WPA2 threshold, all-channel scan +- Laptop connects to same APs at 100% signal — ESP32 radio is the outlier +- Dense 2.4GHz spectrum (ch1: 2 APs, ch6: 4 APs, ch11: 4 APs) but not exhausted + +**Alternative hypothesis:** Hardware antenna issue on Board A. Need to test Board B/C to confirm. + +## Total: 81 Tests across 8 phases ## Post-Phase 7: Bug Fixes & Architecture Improvements @@ -841,3 +834,103 @@ Playwright browser tests for the captive portal UI and payment flow. - `testnut.cashu.space` — auto-pays lightning invoices for testing - `cashu -h https://testnut.cashu.space invoice ` → auto-paid - `cashu -h https://testnut.cashu.space send --legacy ` → generates cashuA token + +## Phase 9: Local Nostr Relay + Relay Selection + Sync — COMPLETE + +**Goal:** Integrate a local Nostr relay into the firmware. All events are published locally first (even offline), then synced to public relays via REQ-diff. Relay selection uses NIP-11 HTTP probing with NIP-77 scoring. + +### Architecture + +``` +Publishers (wifistr, CEP-6, CVM) + → local_relay (port 4869, LittleFS 4MB, 5000 events, 21-day TTL) + → relay_selector (NIP-11 probes, scoring, auto-failover) + → sync_manager (REQ-diff: primary 30min, fallback 6h) + → CVM server (persistent WS to primary relay) +``` + +### Design Decisions + +| Decision | Rationale | +|----------|-----------| +| Local-first publishing | Reduces WS connections to 1 persistent + brief periodic | +| REQ-diff sync (not negentropy binary) | NIP-77 binary protocol adapter not yet written; REQ-diff works everywhere | +| NIP-11 HTTP probing | No WS needed; get liveness, latency, NIPs from simple HTTP GET | +| 4MB LittleFS partition | 5000 events, 21-day TTL; uses free flash without touching SPIFFS | +| Rewrite validator (no libnostr-c) | Use existing secp256k1 + mbedtls; avoid symbol conflicts | +| Port 4869, accessible to WiFi clients | Enables local CVM, service discovery, mesh scenarios | + +### Flash Layout Addition + +| Partition | Offset | Size | Purpose | +|-----------|--------|------|---------| +| relay_store (LittleFS) | 0x500000 | 4MB | Relay event storage | + +### New Files + +``` +components/wisp_relay/ # Local Nostr relay (16 files, no libnostr-c deps) + ws_server.c/h # WebSocket server (port 4869) + NIP-11 + storage_engine.c/h # LittleFS event storage + NVS index + sub_manager.c/h # Subscription management + broadcaster.c/h # JSON fanout to subscribers + rate_limiter.c/h # Per-connection rate limiting + nip11_relay.c/h # NIP-11 info document + deletion.c/h # NIP-09 deletion + flash_monitor.c/h # LittleFS health reporting + router.c/h # NIP-01 message routing + relay_validator.c/h # Schnorr verify + SHA-256 event ID + relay_types.c/h # Local type definitions + handlers.c/h # EVENT/REQ/CLOSE handlers + relay_core.h # Central relay context + +components/esp_littlefs/ # Git submodule: LittleFS VFS +components/negentropy/ # Git submodule: for future NIP-77 + +main/ + local_relay.c/h # Thin wrapper: init/start/publish + relay_selector.c/h # NIP-11 probe + scoring + failover + sync_manager.c/h # REQ-diff sync engine +``` + +### Config Additions + +```json +{ + "nostr_seed_relays": ["wss://relay.orangesync.tech", "wss://relay.damus.io", + "wss://nos.lol", "wss://relay.nostr.band"], + "nostr_sync_interval_s": 1800, + "nostr_fallback_sync_interval_s": 21600 +} +``` + +### Bug Fixes + +- **config.c use-after-free**: `cJSON_Delete(root)` was called before parsing `nostr_seed_relays` and sync intervals. Moved all cJSON accesses before the delete. +- **Relay not starting at boot**: `local_relay_init()/start()` was inside `start_services()` (gated on STA getting IP). Moved to `app_main()` so relay is always available on the AP interface. + +### Test Results (Board B, live hardware) + +| Test | Result | +|------|--------| +| Smoke: ping + HTTP 4869 + NIP-11 | PASS | +| NIP-11 info document (10 checks) | 10/11 PASS | +| WS pub/sub (connect, REQ/EOSE, EVENT/OK, CLOSE, concurrent) | 6/6 PASS | +| Unit tests (relay_validator + relay_selector) | 13/13 PASS | +| Sync to public relay | Expected (30min interval, needs STA internet) | + +### Hardware Test Make Targets + +In `physical-router-test-automation/`: +- `make relay-build` — build relay firmware +- `make relay-flash-b` — flash to Board B +- `make relay-test-smoke` — verify port 4869 +- `make relay-test-nip11` — NIP-11 document test +- `make relay-test-pubsub` — WS publish + subscribe test +- `make relay-test-sync` — verify sync to public relays +- `make relay-test-full` — all tests sequentially + +### Future + +- Implement negentropy binary protocol (NIP-77 NEG_OPEN/NEG_MSG) for efficient set-reconciliation sync +- NIP-11 returns JSON without Accept header (minor: should return HTML) diff --git a/components/axs15231b/CMakeLists.txt b/components/axs15231b/CMakeLists.txt new file mode 100644 index 0000000..033a05e --- /dev/null +++ b/components/axs15231b/CMakeLists.txt @@ -0,0 +1,3 @@ +idf_component_register(SRCS "axs15231b.c" + INCLUDE_DIRS "include" + REQUIRES driver esp_timer) diff --git a/components/axs15231b/axs15231b.c b/components/axs15231b/axs15231b.c new file mode 100644 index 0000000..dd7145a --- /dev/null +++ b/components/axs15231b/axs15231b.c @@ -0,0 +1,282 @@ +#include "axs15231b.h" +#include "driver/spi_master.h" +#include "driver/gpio.h" +#include "esp_log.h" +#include "esp_timer.h" +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include +#include +#include + +static const char *TAG = "axs15231b"; + +#define SWRESET 0x01 +#define SLPIN 0x10 +#define SLPOUT 0x11 +#define INVOFF 0x20 +#define INVON 0x21 +#define DISPOFF 0x28 +#define DISPON 0x29 +#define CASET 0x2A +#define RASET 0x2B +#define RAMWR 0x2C +#define COLMOD 0x3A +#define MADCTL 0x36 + +#define MADCTL_MY 0x80 +#define MADCTL_MX 0x40 +#define MADCTL_MV 0x20 +#define MADCTL_RGB 0x00 + +static spi_device_handle_t s_spi = NULL; +static uint16_t *s_fb = NULL; +static int s_width = AXS15231B_WIDTH; +static int s_height = AXS15231B_HEIGHT; + +typedef struct { + uint8_t cmd; + uint8_t data_len; + const uint8_t *data; + uint16_t delay_ms; +} init_cmd_t; + +static esp_err_t send_cmd(uint8_t cmd) { + spi_transaction_t t = {0}; + t.length = 8; + t.tx_data[0] = cmd; + t.flags = SPI_TRANS_USE_TXDATA; + return spi_device_polling_transmit(s_spi, &t); +} + +static esp_err_t send_data(const uint8_t *data, int len) { + if (len == 0) return ESP_OK; + spi_transaction_t t = {0}; + t.length = len * 8; + t.tx_buffer = data; + t.flags = 0; + return spi_device_polling_transmit(s_spi, &t); +} + +static esp_err_t send_cmd_data(uint8_t cmd, const uint8_t *data, int len) { + esp_err_t ret = send_cmd(cmd); + if (ret != ESP_OK) return ret; + if (len > 0) ret = send_data(data, len); + return ret; +} + +static const uint8_t init_bb[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x5A,0xA5}; +static const uint8_t init_a0[] = {0xC0,0x10,0x00,0x02,0x00,0x00,0x04,0x3F,0x20,0x05,0x3F,0x3F,0x00,0x00,0x00,0x00,0x00}; +static const uint8_t init_a2[] = {0x30,0x3C,0x24,0x14,0xD0,0x20,0xFF,0xE0,0x40,0x19,0x80,0x80,0x80,0x20,0xF9,0x10,0x02,0xFF,0xFF,0xF0,0x90,0x01,0x32,0xA0,0x91,0xE0,0x20,0x7F,0xFF,0x00,0x5A}; +static const uint8_t init_d0[] = {0xE0,0x40,0x51,0x24,0x08,0x05,0x10,0x01,0x20,0x15,0xC2,0x42,0x22,0x22,0xAA,0x03,0x10,0x12,0x60,0x14,0x1E,0x51,0x15,0x00,0x8A,0x20,0x00,0x03,0x3A,0x12}; +static const uint8_t init_a3[] = {0xA0,0x06,0xAA,0x00,0x08,0x02,0x0A,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x00,0x55,0x55}; +static const uint8_t init_c1[] = {0x31,0x04,0x02,0x02,0x71,0x05,0x24,0x55,0x02,0x00,0x41,0x00,0x53,0xFF,0xFF,0xFF,0x4F,0x52,0x00,0x4F,0x52,0x00,0x45,0x3B,0x0B,0x02,0x0D,0x00,0xFF,0x40}; +static const uint8_t init_c3[] = {0x00,0x00,0x00,0x50,0x03,0x00,0x00,0x00,0x01,0x80,0x01}; +static const uint8_t init_c4[] = {0x00,0x24,0x33,0x80,0x00,0xEA,0x64,0x32,0xC8,0x64,0xC8,0x32,0x90,0x90,0x11,0x06,0xDC,0xFA,0x00,0x00,0x80,0xFE,0x10,0x10,0x00,0x0A,0x0A,0x44,0x50}; +static const uint8_t init_c5[] = {0x18,0x00,0x00,0x03,0xFE,0x3A,0x4A,0x20,0x30,0x10,0x88,0xDE,0x0D,0x08,0x0F,0x0F,0x01,0x3A,0x4A,0x20,0x10,0x10,0x00}; +static const uint8_t init_c6[] = {0x05,0x0A,0x05,0x0A,0x00,0xE0,0x2E,0x0B,0x12,0x22,0x12,0x22,0x01,0x03,0x00,0x3F,0x6A,0x18,0xC8,0x22}; +static const uint8_t init_c7[] = {0x50,0x32,0x28,0x00,0xA2,0x80,0x8F,0x00,0x80,0xFF,0x07,0x11,0x9C,0x67,0xFF,0x24,0x0C,0x0D,0x0E,0x0F}; +static const uint8_t init_c9[] = {0x33,0x44,0x44,0x01}; +static const uint8_t init_cf[] = {0x2C,0x1E,0x88,0x58,0x13,0x18,0x56,0x18,0x1E,0x68,0x88,0x00,0x65,0x09,0x22,0xC4,0x0C,0x77,0x22,0x44,0xAA,0x55,0x08,0x08,0x12,0xA0,0x08}; +static const uint8_t init_d5[] = {0x40,0x8E,0x8D,0x01,0x35,0x04,0x92,0x74,0x04,0x92,0x74,0x04,0x08,0x6A,0x04,0x46,0x03,0x03,0x03,0x03,0x82,0x01,0x03,0x00,0xE0,0x51,0xA1,0x00,0x00,0x00}; +static const uint8_t init_d6[] = {0x10,0x32,0x54,0x76,0x98,0xBA,0xDC,0xFE,0x93,0x00,0x01,0x83,0x07,0x07,0x00,0x07,0x07,0x00,0x03,0x03,0x03,0x03,0x03,0x03,0x00,0x84,0x00,0x20,0x01,0x00}; +static const uint8_t init_d7[] = {0x03,0x01,0x0B,0x09,0x0F,0x0D,0x1E,0x1F,0x18,0x1D,0x1F,0x19,0x40,0x8E,0x04,0x00,0x20,0xA0,0x1F}; +static const uint8_t init_d8[] = {0x02,0x00,0x0A,0x08,0x0E,0x0C,0x1E,0x1F,0x18,0x1D,0x1F,0x19}; +static const uint8_t init_d9[] = {0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F}; +static const uint8_t init_dd[] = {0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F}; +static const uint8_t init_df[] = {0x44,0x73,0x4B,0x69,0x00,0x0A,0x02,0x90}; +static const uint8_t init_e0[] = {0x3B,0x28,0x10,0x16,0x0C,0x06,0x11,0x28,0x5C,0x21,0x0D,0x35,0x13,0x2C,0x33,0x28,0x0D}; +static const uint8_t init_e1[] = {0x37,0x28,0x10,0x16,0x0B,0x06,0x11,0x28,0x5C,0x21,0x0D,0x35,0x14,0x2C,0x33,0x28,0x0F}; +static const uint8_t init_e2[] = {0x3B,0x07,0x12,0x18,0x0E,0x0D,0x17,0x35,0x44,0x32,0x0C,0x14,0x14,0x36,0x3A,0x2F,0x0D}; +static const uint8_t init_e3[] = {0x37,0x07,0x12,0x18,0x0E,0x0D,0x17,0x35,0x44,0x32,0x0C,0x14,0x14,0x36,0x32,0x2F,0x0F}; +static const uint8_t init_e4[] = {0x3B,0x07,0x12,0x18,0x0E,0x0D,0x17,0x39,0x44,0x2E,0x0C,0x14,0x14,0x36,0x3A,0x2F,0x0D}; +static const uint8_t init_e5[] = {0x37,0x07,0x12,0x18,0x0E,0x0D,0x17,0x39,0x44,0x2E,0x0C,0x14,0x14,0x36,0x3A,0x2F,0x0F}; +static const uint8_t init_a4_1[] = {0x85,0x85,0x95,0x82,0xAF,0xAA,0xAA,0x80,0x10,0x30,0x40,0x40,0x20,0xFF,0x60,0x30}; +static const uint8_t init_a4_2[] = {0x85,0x85,0x95,0x85}; +static const uint8_t init_bb2[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}; + +static const init_cmd_t s_init_cmds[] = { + {0xBB, sizeof(init_bb), init_bb, 0}, + {0xA0, sizeof(init_a0), init_a0, 0}, + {0xA2, sizeof(init_a2), init_a2, 0}, + {0xD0, sizeof(init_d0), init_d0, 0}, + {0xA3, sizeof(init_a3), init_a3, 0}, + {0xC1, sizeof(init_c1), init_c1, 0}, + {0xC3, sizeof(init_c3), init_c3, 0}, + {0xC4, sizeof(init_c4), init_c4, 0}, + {0xC5, sizeof(init_c5), init_c5, 0}, + {0xC6, sizeof(init_c6), init_c6, 0}, + {0xC7, sizeof(init_c7), init_c7, 0}, + {0xC9, sizeof(init_c9), init_c9, 0}, + {0xCF, sizeof(init_cf), init_cf, 0}, + {0xD5, sizeof(init_d5), init_d5, 0}, + {0xD6, sizeof(init_d6), init_d6, 0}, + {0xD7, sizeof(init_d7), init_d7, 0}, + {0xD8, sizeof(init_d8), init_d8, 0}, + {0xD9, sizeof(init_d9), init_d9, 0}, + {0xDD, sizeof(init_dd), init_dd, 0}, + {0xDF, sizeof(init_df), init_df, 0}, + {0xE0, sizeof(init_e0), init_e0, 0}, + {0xE1, sizeof(init_e1), init_e1, 0}, + {0xE2, sizeof(init_e2), init_e2, 0}, + {0xE3, sizeof(init_e3), init_e3, 0}, + {0xE4, sizeof(init_e4), init_e4, 0}, + {0xE5, sizeof(init_e5), init_e5, 0}, + {0xA4, sizeof(init_a4_1), init_a4_1, 0}, + {0xA4, sizeof(init_a4_2), init_a4_2, 0}, + {0xBB, sizeof(init_bb2), init_bb2, 0}, + {SLPOUT, 0, NULL, 200}, + {DISPON, 0, NULL, 100}, +}; +#define INIT_CMD_COUNT (sizeof(s_init_cmds) / sizeof(s_init_cmds[0])) + +esp_err_t axs15231b_init(void) { + ESP_LOGI(TAG, "Initializing AXS15231B display..."); + + esp_err_t ret; + + spi_bus_config_t buscfg = { + .mosi_io_num = AXS15231B_PIN_D0, + .sclk_io_num = AXS15231B_PIN_CLK, + .miso_io_num = -1, + .quadwp_io_num = -1, + .quadhd_io_num = -1, + .max_transfer_sz = 32768, + }; + + spi_device_interface_config_t devcfg = { + .clock_speed_hz = 40 * 1000 * 1000, + .mode = 0, + .spics_io_num = AXS15231B_PIN_CS, + .queue_size = 7, + .flags = 0, + }; + + ret = spi_bus_initialize(SPI2_HOST, &buscfg, SPI_DMA_CH_AUTO); + if (ret != ESP_OK) { + ESP_LOGE(TAG, "Failed to init SPI bus: %s", esp_err_to_name(ret)); + return ret; + } + + ret = spi_bus_add_device(SPI2_HOST, &devcfg, &s_spi); + if (ret != ESP_OK) { + ESP_LOGE(TAG, "Failed to add SPI device: %s", esp_err_to_name(ret)); + return ret; + } + + size_t fb_size = (size_t)s_width * s_height * 2; + s_fb = heap_caps_malloc(fb_size, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT); + if (!s_fb) { + ESP_LOGE(TAG, "Failed to allocate framebuffer (%zu bytes)", fb_size); + return ESP_ERR_NO_MEM; + } + memset(s_fb, 0, fb_size); + ESP_LOGI(TAG, "Framebuffer allocated: %zu bytes in PSRAM", fb_size); + + gpio_config_t bl_cfg = { + .pin_bit_mask = (1ULL << AXS15231B_PIN_BL), + .mode = GPIO_MODE_OUTPUT, + .pull_up_en = GPIO_PULLUP_DISABLE, + .pull_down_en = GPIO_PULLDOWN_DISABLE, + .intr_type = GPIO_INTR_DISABLE, + }; + gpio_config(&bl_cfg); + + send_cmd(SWRESET); + vTaskDelay(pdMS_TO_TICKS(200)); + + for (int i = 0; i < INIT_CMD_COUNT; i++) { + ret = send_cmd_data(s_init_cmds[i].cmd, s_init_cmds[i].data, s_init_cmds[i].data_len); + if (ret != ESP_OK) { + ESP_LOGE(TAG, "Init cmd 0x%02X failed: %s", s_init_cmds[i].cmd, esp_err_to_name(ret)); + return ret; + } + if (s_init_cmds[i].delay_ms > 0) { + vTaskDelay(pdMS_TO_TICKS(s_init_cmds[i].delay_ms)); + } + } + + uint8_t madctl_val = MADCTL_MX | MADCTL_MV | MADCTL_RGB; + ret = send_cmd_data(MADCTL, &madctl_val, 1); + if (ret != ESP_OK) { + ESP_LOGE(TAG, "Failed to set rotation: %s", esp_err_to_name(ret)); + return ret; + } + + uint8_t colmod_val = 0x55; + ret = send_cmd_data(COLMOD, &colmod_val, 1); + if (ret != ESP_OK) { + ESP_LOGE(TAG, "Failed to set pixel format: %s", esp_err_to_name(ret)); + return ret; + } + + axs15231b_fill_screen(0x0000); + axs15231b_flush(); + + axs15231b_set_backlight(true); + + ESP_LOGI(TAG, "AXS15231B initialized: %dx%d landscape", s_width, s_height); + return ESP_OK; +} + +void axs15231b_set_backlight(bool on) { + gpio_set_level(AXS15231B_PIN_BL, on ? 1 : 0); +} + +void axs15231b_fill_screen(uint16_t color) { + uint32_t pixels = (uint32_t)s_width * s_height; + for (uint32_t i = 0; i < pixels; i++) { + s_fb[i] = color; + } +} + +void axs15231b_fill_rect(int x, int y, int w, int h, uint16_t color) { + if (x < 0 || y < 0 || x + w > s_width || y + h > s_height) return; + for (int row = y; row < y + h; row++) { + for (int col = x; col < x + w; col++) { + s_fb[row * s_width + col] = color; + } + } +} + +void axs15231b_flush(void) { + if (!s_spi || !s_fb) return; + + uint8_t buf[4]; + buf[0] = 0; + buf[1] = 0; + buf[2] = (s_width - 1) >> 8; + buf[3] = (s_width - 1) & 0xFF; + send_cmd_data(CASET, buf, 4); + + buf[0] = 0; + buf[1] = 0; + buf[2] = (s_height - 1) >> 8; + buf[3] = (s_height - 1) & 0xFF; + send_cmd_data(RASET, buf, 4); + + send_cmd(RAMWR); + + int total_bytes = s_width * s_height * 2; + int chunk_size = 32768; + int offset = 0; + uint8_t *fb_bytes = (uint8_t *)s_fb; + + while (offset < total_bytes) { + int remaining = total_bytes - offset; + int this_chunk = remaining < chunk_size ? remaining : chunk_size; + + spi_transaction_t t = {0}; + t.length = this_chunk * 8; + t.tx_buffer = fb_bytes + offset; + esp_err_t ret = spi_device_polling_transmit(s_spi, &t); + if (ret != ESP_OK) { + ESP_LOGE(TAG, "Flush transfer failed at offset %d: %s", offset, esp_err_to_name(ret)); + return; + } + offset += this_chunk; + } +} + +int axs15231b_get_width(void) { return s_width; } +int axs15231b_get_height(void) { return s_height; } diff --git a/components/axs15231b/include/axs15231b.h b/components/axs15231b/include/axs15231b.h new file mode 100644 index 0000000..5ec017c --- /dev/null +++ b/components/axs15231b/include/axs15231b.h @@ -0,0 +1,27 @@ +#ifndef AXS15231B_H +#define AXS15231B_H + +#include "esp_err.h" +#include +#include + +#define AXS15231B_WIDTH 480 +#define AXS15231B_HEIGHT 320 + +#define AXS15231B_PIN_CS 45 +#define AXS15231B_PIN_CLK 47 +#define AXS15231B_PIN_D0 21 +#define AXS15231B_PIN_D1 48 +#define AXS15231B_PIN_D2 40 +#define AXS15231B_PIN_D3 39 +#define AXS15231B_PIN_BL 1 + +esp_err_t axs15231b_init(void); +void axs15231b_set_backlight(bool on); +void axs15231b_fill_screen(uint16_t color); +void axs15231b_fill_rect(int x, int y, int w, int h, uint16_t color); +void axs15231b_flush(void); +int axs15231b_get_width(void); +int axs15231b_get_height(void); + +#endif diff --git a/components/esp_littlefs b/components/esp_littlefs new file mode 160000 index 0000000..b12f09d --- /dev/null +++ b/components/esp_littlefs @@ -0,0 +1 @@ +Subproject commit b12f09d414fd18f96160f28689c702b4bf3ca632 diff --git a/components/negentropy b/components/negentropy new file mode 160000 index 0000000..8129c5e --- /dev/null +++ b/components/negentropy @@ -0,0 +1 @@ +Subproject commit 8129c5e7799211083c6dcc72ff3a33a99c27fd08 diff --git a/components/qrcode/CMakeLists.txt b/components/qrcode/CMakeLists.txt new file mode 100644 index 0000000..347aeed --- /dev/null +++ b/components/qrcode/CMakeLists.txt @@ -0,0 +1,2 @@ +idf_component_register(SRCS "qrcoded.c" + INCLUDE_DIRS "include") diff --git a/components/qrcode/include/qrcoded.h b/components/qrcode/include/qrcoded.h new file mode 100755 index 0000000..602f9c0 --- /dev/null +++ b/components/qrcode/include/qrcoded.h @@ -0,0 +1,85 @@ +/** + * The MIT License (MIT) + * + * This library is written and maintained by Richard Moore. + * Major parts were derived from Project Nayuki's library. + * + * Copyright (c) 2017 Richard Moore (https://github.com/ricmoo/QRCode) + * Copyright (c) 2017 Project Nayuki (https://www.nayuki.io/page/qr-code-generator-library) + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +/** + * Special thanks to Nayuki (https://www.nayuki.io/) from which this library was + * heavily inspired and compared against. + * + * See: https://github.com/nayuki/QR-Code-generator/tree/master/cpp + */ + +#ifndef __QRCODE_H_ +#define __QRCODE_H_ + +#include +#include + +// QR Code Format Encoding +#define MODE_NUMERIC 0 +#define MODE_ALPHANUMERIC 1 +#define MODE_BYTE 2 + +// Error Correction Code Levels +#define ECC_LOW 0 +#define ECC_MEDIUM 1 +#define ECC_QUARTILE 2 +#define ECC_HIGH 3 + +// If set to non-zero, this library can ONLY produce QR codes at that version +// This saves a lot of dynamic memory, as the codeword tables are skipped +#ifndef LOCK_VERSION +#define LOCK_VERSION 0 +#endif + +typedef struct QRCode +{ + uint8_t version; + uint8_t size; + uint8_t ecc; + uint8_t mode; + uint8_t mask; + uint8_t *modules; +} QRCode; + +#ifdef __cplusplus +extern "C" +{ +#endif /* __cplusplus */ + + uint16_t qrcode_getBufferSize(uint8_t version); + + int8_t qrcode_initText(QRCode *qrcoded, uint8_t *modules, uint8_t version, uint8_t ecc, const char *data); + int8_t qrcode_initBytes(QRCode *qrcoded, uint8_t *modules, uint8_t version, uint8_t ecc, uint8_t *data, uint16_t length); + + bool qrcode_getModule(QRCode *qrcoded, uint8_t x, uint8_t y); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* __QRCODE_H_ */ diff --git a/components/qrcode/qrcoded.c b/components/qrcode/qrcoded.c new file mode 100755 index 0000000..c8825f3 --- /dev/null +++ b/components/qrcode/qrcoded.c @@ -0,0 +1,1054 @@ +/** + * The MIT License (MIT) + * + * This library is written and maintained by Richard Moore. + * Major parts were derived from Project Nayuki's library. + * + * Copyright (c) 2017 Richard Moore (https://github.com/ricmoo/QRCode) + * Copyright (c) 2017 Project Nayuki (https://www.nayuki.io/page/qr-code-generator-library) + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +/** + * Special thanks to Nayuki (https://www.nayuki.io/) from which this library was + * heavily inspired and compared against. + * + * See: https://github.com/nayuki/QR-Code-generator/tree/master/cpp + */ + +#include "qrcoded.h" + +#include +#include + +/* Error Correction Lookup tables */ + +#if LOCK_VERSION == 0 + +static const uint16_t NUM_ERROR_CORRECTION_CODEWORDS[4][40] = { + // 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40 Error correction level + {10, 16, 26, 36, 48, 64, 72, 88, 110, 130, 150, 176, 198, 216, 240, 280, 308, 338, 364, 416, 442, 476, 504, 560, 588, 644, 700, 728, 784, 812, 868, 924, 980, 1036, 1064, 1120, 1204, 1260, 1316, 1372}, // Medium + {7, 10, 15, 20, 26, 36, 40, 48, 60, 72, 80, 96, 104, 120, 132, 144, 168, 180, 196, 224, 224, 252, 270, 300, 312, 336, 360, 390, 420, 450, 480, 510, 540, 570, 570, 600, 630, 660, 720, 750}, // Low + {17, 28, 44, 64, 88, 112, 130, 156, 192, 224, 264, 308, 352, 384, 432, 480, 532, 588, 650, 700, 750, 816, 900, 960, 1050, 1110, 1200, 1260, 1350, 1440, 1530, 1620, 1710, 1800, 1890, 1980, 2100, 2220, 2310, 2430}, // High + {13, 22, 36, 52, 72, 96, 108, 132, 160, 192, 224, 260, 288, 320, 360, 408, 448, 504, 546, 600, 644, 690, 750, 810, 870, 952, 1020, 1050, 1140, 1200, 1290, 1350, 1440, 1530, 1590, 1680, 1770, 1860, 1950, 2040}, // Quartile +}; + +static const uint8_t NUM_ERROR_CORRECTION_BLOCKS[4][40] = { + // Version: (note that index 0 is for padding, and is set to an illegal value) + // 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40 Error correction level + {1, 1, 1, 2, 2, 4, 4, 4, 5, 5, 5, 8, 9, 9, 10, 10, 11, 13, 14, 16, 17, 17, 18, 20, 21, 23, 25, 26, 28, 29, 31, 33, 35, 37, 38, 40, 43, 45, 47, 49}, // Medium + {1, 1, 1, 1, 1, 2, 2, 2, 2, 4, 4, 4, 4, 4, 6, 6, 6, 6, 7, 8, 8, 9, 9, 10, 12, 12, 12, 13, 14, 15, 16, 17, 18, 19, 19, 20, 21, 22, 24, 25}, // Low + {1, 1, 2, 4, 4, 4, 5, 6, 8, 8, 11, 11, 16, 16, 18, 16, 19, 21, 25, 25, 25, 34, 30, 32, 35, 37, 40, 42, 45, 48, 51, 54, 57, 60, 63, 66, 70, 74, 77, 81}, // High + {1, 1, 2, 2, 4, 4, 6, 6, 8, 8, 8, 10, 12, 16, 12, 17, 16, 18, 21, 20, 23, 23, 25, 27, 29, 34, 34, 35, 38, 40, 43, 45, 48, 51, 53, 56, 59, 62, 65, 68}, // Quartile +}; + +static const uint16_t NUM_RAW_DATA_MODULES[40] = { + // 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, + 208, 359, 567, 807, 1079, 1383, 1568, 1936, 2336, 2768, 3232, 3728, 4256, 4651, 5243, 5867, 6523, + // 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, + 7211, 7931, 8683, 9252, 10068, 10916, 11796, 12708, 13652, 14628, 15371, 16411, 17483, 18587, + // 32, 33, 34, 35, 36, 37, 38, 39, 40 + 19723, 20891, 22091, 23008, 24272, 25568, 26896, 28256, 29648}; + +// @TODO: Put other LOCK_VERSIONS here +#elif LOCK_VERSION == 3 + +static const int16_t NUM_ERROR_CORRECTION_CODEWORDS[4] = { + 26, 15, 44, 36}; + +static const int8_t NUM_ERROR_CORRECTION_BLOCKS[4] = { + 1, 1, 2, 2}; + +static const uint16_t NUM_RAW_DATA_MODULES = 567; + +#else + +#error Unsupported LOCK_VERSION (add it...) + +#endif + +static int max(int a, int b) +{ + if (a > b) + { + return a; + } + return b; +} + +/* +static int abs(int value) { + if (value < 0) { return -value; } + return value; +} +*/ + +/* Mode testing and conversion */ + +static int8_t getAlphanumeric(char c) +{ + + if (c >= '0' && c <= '9') + { + return (c - '0'); + } + if (c >= 'A' && c <= 'Z') + { + return (c - 'A' + 10); + } + + switch (c) + { + case ' ': + return 36; + case '$': + return 37; + case '%': + return 38; + case '*': + return 39; + case '+': + return 40; + case '-': + return 41; + case '.': + return 42; + case '/': + return 43; + case ':': + return 44; + } + + return -1; +} + +static bool isAlphanumeric(const char *text, uint16_t length) +{ + while (length != 0) + { + if (getAlphanumeric(text[--length]) == -1) + { + return false; + } + } + return true; +} + +static bool isNumeric(const char *text, uint16_t length) +{ + while (length != 0) + { + char c = text[--length]; + if (c < '0' || c > '9') + { + return false; + } + } + return true; +} + +/* Counting */ + +// We store the following tightly packed (less 8) in modeInfo +// <=9 <=26 <= 40 +// NUMERIC ( 10, 12, 14); +// ALPHANUMERIC ( 9, 11, 13); +// BYTE ( 8, 16, 16); +static char getModeBits(uint8_t version, uint8_t mode) +{ + // Note: We use 15 instead of 16; since 15 doesn't exist and we cannot store 16 (8 + 8) in 3 bits + // hex(int("".join(reversed([('00' + bin(x - 8)[2:])[-3:] for x in [10, 9, 8, 12, 11, 15, 14, 13, 15]])), 2)) + unsigned int modeInfo = 0x7bbb80a; + +#if LOCK_VERSION == 0 || LOCK_VERSION > 9 + if (version > 9) + { + modeInfo >>= 9; + } +#endif + +#if LOCK_VERSION == 0 || LOCK_VERSION > 26 + if (version > 26) + { + modeInfo >>= 9; + } +#endif + + char result = 8 + ((modeInfo >> (3 * mode)) & 0x07); + if (result == 15) + { + result = 16; + } + + return result; +} + +/* BitBucket */ + +typedef struct BitBucket +{ + uint32_t bitOffsetOrWidth; + uint16_t capacityBytes; + uint8_t *data; +} BitBucket; + +/* +void bb_dump(BitBucket *bitBuffer) { + printf("Buffer: "); + for (uint32_t i = 0; i < bitBuffer->capacityBytes; i++) { + printf("%02x", bitBuffer->data[i]); + if ((i % 4) == 3) { printf(" "); } + } + printf("\n"); +} +*/ + +static uint16_t bb_getGridSizeBytes(uint8_t size) +{ + return (((size * size) + 7) / 8); +} + +static uint16_t bb_getBufferSizeBytes(uint32_t bits) +{ + return ((bits + 7) / 8); +} + +static void bb_initBuffer(BitBucket *bitBuffer, uint8_t *data, int32_t capacityBytes) +{ + bitBuffer->bitOffsetOrWidth = 0; + bitBuffer->capacityBytes = capacityBytes; + bitBuffer->data = data; + + memset(data, 0, bitBuffer->capacityBytes); +} + +static void bb_initGrid(BitBucket *bitGrid, uint8_t *data, uint8_t size) +{ + bitGrid->bitOffsetOrWidth = size; + bitGrid->capacityBytes = bb_getGridSizeBytes(size); + bitGrid->data = data; + + memset(data, 0, bitGrid->capacityBytes); +} + +static void bb_appendBits(BitBucket *bitBuffer, uint32_t val, uint8_t length) +{ + uint32_t offset = bitBuffer->bitOffsetOrWidth; + for (int8_t i = length - 1; i >= 0; i--, offset++) + { + bitBuffer->data[offset >> 3] |= ((val >> i) & 1) << (7 - (offset & 7)); + } + bitBuffer->bitOffsetOrWidth = offset; +} +/* +void bb_setBits(BitBucket *bitBuffer, uint32_t val, int offset, uint8_t length) { + for (int8_t i = length - 1; i >= 0; i--, offset++) { + bitBuffer->data[offset >> 3] |= ((val >> i) & 1) << (7 - (offset & 7)); + } +} +*/ +static void bb_setBit(BitBucket *bitGrid, uint8_t x, uint8_t y, bool on) +{ + uint32_t offset = y * bitGrid->bitOffsetOrWidth + x; + uint8_t mask = 1 << (7 - (offset & 0x07)); + if (on) + { + bitGrid->data[offset >> 3] |= mask; + } + else + { + bitGrid->data[offset >> 3] &= ~mask; + } +} + +static void bb_invertBit(BitBucket *bitGrid, uint8_t x, uint8_t y, bool invert) +{ + uint32_t offset = y * bitGrid->bitOffsetOrWidth + x; + uint8_t mask = 1 << (7 - (offset & 0x07)); + bool on = ((bitGrid->data[offset >> 3] & (1 << (7 - (offset & 0x07)))) != 0); + if (on ^ invert) + { + bitGrid->data[offset >> 3] |= mask; + } + else + { + bitGrid->data[offset >> 3] &= ~mask; + } +} + +static bool bb_getBit(BitBucket *bitGrid, uint8_t x, uint8_t y) +{ + uint32_t offset = y * bitGrid->bitOffsetOrWidth + x; + return (bitGrid->data[offset >> 3] & (1 << (7 - (offset & 0x07)))) != 0; +} + +/* Drawing Patterns */ + +// XORs the data modules in this QR Code with the given mask pattern. Due to XOR's mathematical +// properties, calling applyMask(m) twice with the same value is equivalent to no change at all. +// This means it is possible to apply a mask, undo it, and try another mask. Note that a final +// well-formed QR Code symbol needs exactly one mask applied (not zero, not two, etc.). +static void applyMask(BitBucket *modules, BitBucket *isFunction, uint8_t mask) +{ + uint8_t size = modules->bitOffsetOrWidth; + + for (uint8_t y = 0; y < size; y++) + { + for (uint8_t x = 0; x < size; x++) + { + if (bb_getBit(isFunction, x, y)) + { + continue; + } + + bool invert = 0; + switch (mask) + { + case 0: + invert = (x + y) % 2 == 0; + break; + case 1: + invert = y % 2 == 0; + break; + case 2: + invert = x % 3 == 0; + break; + case 3: + invert = (x + y) % 3 == 0; + break; + case 4: + invert = (x / 3 + y / 2) % 2 == 0; + break; + case 5: + invert = x * y % 2 + x * y % 3 == 0; + break; + case 6: + invert = (x * y % 2 + x * y % 3) % 2 == 0; + break; + case 7: + invert = ((x + y) % 2 + x * y % 3) % 2 == 0; + break; + } + bb_invertBit(modules, x, y, invert); + } + } +} + +static void setFunctionModule(BitBucket *modules, BitBucket *isFunction, uint8_t x, uint8_t y, bool on) +{ + bb_setBit(modules, x, y, on); + bb_setBit(isFunction, x, y, true); +} + +// Draws a 9*9 finder pattern including the border separator, with the center module at (x, y). +static void drawFinderPattern(BitBucket *modules, BitBucket *isFunction, uint8_t x, uint8_t y) +{ + uint8_t size = modules->bitOffsetOrWidth; + + for (int8_t i = -4; i <= 4; i++) + { + for (int8_t j = -4; j <= 4; j++) + { + uint8_t dist = max(abs(i), abs(j)); // Chebyshev/infinity norm + int16_t xx = x + j, yy = y + i; + if (0 <= xx && xx < size && 0 <= yy && yy < size) + { + setFunctionModule(modules, isFunction, xx, yy, dist != 2 && dist != 4); + } + } + } +} + +// Draws a 5*5 alignment pattern, with the center module at (x, y). +static void drawAlignmentPattern(BitBucket *modules, BitBucket *isFunction, uint8_t x, uint8_t y) +{ + for (int8_t i = -2; i <= 2; i++) + { + for (int8_t j = -2; j <= 2; j++) + { + setFunctionModule(modules, isFunction, x + j, y + i, max(abs(i), abs(j)) != 1); + } + } +} + +// Draws two copies of the format bits (with its own error correction code) +// based on the given mask and this object's error correction level field. +static void drawFormatBits(BitBucket *modules, BitBucket *isFunction, uint8_t ecc, uint8_t mask) +{ + + uint8_t size = modules->bitOffsetOrWidth; + + // Calculate error correction code and pack bits + uint32_t data = ecc << 3 | mask; // errCorrLvl is uint2, mask is uint3 + uint32_t rem = data; + for (int i = 0; i < 10; i++) + { + rem = (rem << 1) ^ ((rem >> 9) * 0x537); + } + + data = data << 10 | rem; + data ^= 0x5412; // uint15 + + // Draw first copy + for (uint8_t i = 0; i <= 5; i++) + { + setFunctionModule(modules, isFunction, 8, i, ((data >> i) & 1) != 0); + } + + setFunctionModule(modules, isFunction, 8, 7, ((data >> 6) & 1) != 0); + setFunctionModule(modules, isFunction, 8, 8, ((data >> 7) & 1) != 0); + setFunctionModule(modules, isFunction, 7, 8, ((data >> 8) & 1) != 0); + + for (int8_t i = 9; i < 15; i++) + { + setFunctionModule(modules, isFunction, 14 - i, 8, ((data >> i) & 1) != 0); + } + + // Draw second copy + for (int8_t i = 0; i <= 7; i++) + { + setFunctionModule(modules, isFunction, size - 1 - i, 8, ((data >> i) & 1) != 0); + } + + for (int8_t i = 8; i < 15; i++) + { + setFunctionModule(modules, isFunction, 8, size - 15 + i, ((data >> i) & 1) != 0); + } + + setFunctionModule(modules, isFunction, 8, size - 8, true); +} + +// Draws two copies of the version bits (with its own error correction code), +// based on this object's version field (which only has an effect for 7 <= version <= 40). +static void drawVersion(BitBucket *modules, BitBucket *isFunction, uint8_t version) +{ + + int8_t size = modules->bitOffsetOrWidth; + +#if LOCK_VERSION != 0 && LOCK_VERSION < 7 + return; + +#else + if (version < 7) + { + return; + } + + // Calculate error correction code and pack bits + uint32_t rem = version; // version is uint6, in the range [7, 40] + for (uint8_t i = 0; i < 12; i++) + { + rem = (rem << 1) ^ ((rem >> 11) * 0x1F25); + } + + uint32_t data = version << 12 | rem; // uint18 + + // Draw two copies + for (uint8_t i = 0; i < 18; i++) + { + bool bit = ((data >> i) & 1) != 0; + uint8_t a = size - 11 + i % 3, b = i / 3; + setFunctionModule(modules, isFunction, a, b, bit); + setFunctionModule(modules, isFunction, b, a, bit); + } + +#endif +} + +static void drawFunctionPatterns(BitBucket *modules, BitBucket *isFunction, uint8_t version, uint8_t ecc) +{ + + uint8_t size = modules->bitOffsetOrWidth; + + // Draw the horizontal and vertical timing patterns + for (uint8_t i = 0; i < size; i++) + { + setFunctionModule(modules, isFunction, 6, i, i % 2 == 0); + setFunctionModule(modules, isFunction, i, 6, i % 2 == 0); + } + + // Draw 3 finder patterns (all corners except bottom right; overwrites some timing modules) + drawFinderPattern(modules, isFunction, 3, 3); + drawFinderPattern(modules, isFunction, size - 4, 3); + drawFinderPattern(modules, isFunction, 3, size - 4); + +#if LOCK_VERSION == 0 || LOCK_VERSION > 1 + + if (version > 1) + { + + // Draw the numerous alignment patterns + + uint8_t alignCount = version / 7 + 2; + uint8_t step; + if (version != 32) + { + step = (version * 4 + alignCount * 2 + 1) / (2 * alignCount - 2) * 2; // ceil((size - 13) / (2*numAlign - 2)) * 2 + } + else + { // C-C-C-Combo breaker! + step = 26; + } + + uint8_t alignPositionIndex = alignCount - 1; + uint8_t alignPosition[alignCount]; + + alignPosition[0] = 6; + + uint8_t size = version * 4 + 17; + for (uint8_t i = 0, pos = size - 7; i < alignCount - 1; i++, pos -= step) + { + alignPosition[alignPositionIndex--] = pos; + } + + for (uint8_t i = 0; i < alignCount; i++) + { + for (uint8_t j = 0; j < alignCount; j++) + { + if ((i == 0 && j == 0) || (i == 0 && j == alignCount - 1) || (i == alignCount - 1 && j == 0)) + { + continue; // Skip the three finder corners + } + else + { + drawAlignmentPattern(modules, isFunction, alignPosition[i], alignPosition[j]); + } + } + } + } + +#endif + + // Draw configuration data + drawFormatBits(modules, isFunction, ecc, 0); // Dummy mask value; overwritten later in the constructor + drawVersion(modules, isFunction, version); +} + +// Draws the given sequence of 8-bit codewords (data and error correction) onto the entire +// data area of this QR Code symbol. Function modules need to be marked off before this is called. +static void drawCodewords(BitBucket *modules, BitBucket *isFunction, BitBucket *codewords) +{ + + uint32_t bitLength = codewords->bitOffsetOrWidth; + uint8_t *data = codewords->data; + + uint8_t size = modules->bitOffsetOrWidth; + + // Bit index into the data + uint32_t i = 0; + + // Do the funny zigzag scan + for (int16_t right = size - 1; right >= 1; right -= 2) + { // Index of right column in each column pair + if (right == 6) + { + right = 5; + } + + for (uint8_t vert = 0; vert < size; vert++) + { // Vertical counter + for (int j = 0; j < 2; j++) + { + uint8_t x = right - j; // Actual x coordinate + bool upwards = ((right & 2) == 0) ^ (x < 6); + uint8_t y = upwards ? size - 1 - vert : vert; // Actual y coordinate + if (!bb_getBit(isFunction, x, y) && i < bitLength) + { + bb_setBit(modules, x, y, ((data[i >> 3] >> (7 - (i & 7))) & 1) != 0); + i++; + } + // If there are any remainder bits (0 to 7), they are already + // set to 0/false/white when the grid of modules was initialized + } + } + } +} + +/* Penalty Calculation */ + +#define PENALTY_N1 3 +#define PENALTY_N2 3 +#define PENALTY_N3 40 +#define PENALTY_N4 10 + +// Calculates and returns the penalty score based on state of this QR Code's current modules. +// This is used by the automatic mask choice algorithm to find the mask pattern that yields the lowest score. +// @TODO: This can be optimized by working with the bytes instead of bits. +static uint32_t getPenaltyScore(BitBucket *modules) +{ + uint32_t result = 0; + + uint8_t size = modules->bitOffsetOrWidth; + + // Adjacent modules in row having same color + for (uint8_t y = 0; y < size; y++) + { + + bool colorX = bb_getBit(modules, 0, y); + for (uint8_t x = 1, runX = 1; x < size; x++) + { + bool cx = bb_getBit(modules, x, y); + if (cx != colorX) + { + colorX = cx; + runX = 1; + } + else + { + runX++; + if (runX == 5) + { + result += PENALTY_N1; + } + else if (runX > 5) + { + result++; + } + } + } + } + + // Adjacent modules in column having same color + for (uint8_t x = 0; x < size; x++) + { + bool colorY = bb_getBit(modules, x, 0); + for (uint8_t y = 1, runY = 1; y < size; y++) + { + bool cy = bb_getBit(modules, x, y); + if (cy != colorY) + { + colorY = cy; + runY = 1; + } + else + { + runY++; + if (runY == 5) + { + result += PENALTY_N1; + } + else if (runY > 5) + { + result++; + } + } + } + } + + uint16_t black = 0; + for (uint8_t y = 0; y < size; y++) + { + uint16_t bitsRow = 0, bitsCol = 0; + for (uint8_t x = 0; x < size; x++) + { + bool color = bb_getBit(modules, x, y); + + // 2*2 blocks of modules having same color + if (x > 0 && y > 0) + { + bool colorUL = bb_getBit(modules, x - 1, y - 1); + bool colorUR = bb_getBit(modules, x, y - 1); + bool colorL = bb_getBit(modules, x - 1, y); + if (color == colorUL && color == colorUR && color == colorL) + { + result += PENALTY_N2; + } + } + + // Finder-like pattern in rows and columns + bitsRow = ((bitsRow << 1) & 0x7FF) | color; + bitsCol = ((bitsCol << 1) & 0x7FF) | bb_getBit(modules, y, x); + + // Needs 11 bits accumulated + if (x >= 10) + { + if (bitsRow == 0x05D || bitsRow == 0x5D0) + { + result += PENALTY_N3; + } + if (bitsCol == 0x05D || bitsCol == 0x5D0) + { + result += PENALTY_N3; + } + } + + // Balance of black and white modules + if (color) + { + black++; + } + } + } + + // Find smallest k such that (45-5k)% <= dark/total <= (55+5k)% + uint16_t total = size * size; + for (uint16_t k = 0; black * 20 < (9 - k) * total || black * 20 > (11 + k) * total; k++) + { + result += PENALTY_N4; + } + + return result; +} + +/* Reed-Solomon Generator */ + +static uint8_t rs_multiply(uint8_t x, uint8_t y) +{ + // Russian peasant multiplication + // See: https://en.wikipedia.org/wiki/Ancient_Egyptian_multiplication + uint16_t z = 0; + for (int8_t i = 7; i >= 0; i--) + { + z = (z << 1) ^ ((z >> 7) * 0x11D); + z ^= ((y >> i) & 1) * x; + } + return z; +} + +static void rs_init(uint8_t degree, uint8_t *coeff) +{ + memset(coeff, 0, degree); + coeff[degree - 1] = 1; + + // Compute the product polynomial (x - r^0) * (x - r^1) * (x - r^2) * ... * (x - r^{degree-1}), + // drop the highest term, and store the rest of the coefficients in order of descending powers. + // Note that r = 0x02, which is a generator element of this field GF(2^8/0x11D). + uint16_t root = 1; + for (uint8_t i = 0; i < degree; i++) + { + // Multiply the current product by (x - r^i) + for (uint8_t j = 0; j < degree; j++) + { + coeff[j] = rs_multiply(coeff[j], root); + if (j + 1 < degree) + { + coeff[j] ^= coeff[j + 1]; + } + } + root = (root << 1) ^ ((root >> 7) * 0x11D); // Multiply by 0x02 mod GF(2^8/0x11D) + } +} + +static void rs_getRemainder(uint8_t degree, uint8_t *coeff, uint8_t *data, uint8_t length, uint8_t *result, uint8_t stride) +{ + // Compute the remainder by performing polynomial division + + //for (uint8_t i = 0; i < degree; i++) { result[] = 0; } + //memset(result, 0, degree); + + for (uint8_t i = 0; i < length; i++) + { + uint8_t factor = data[i] ^ result[0]; + for (uint8_t j = 1; j < degree; j++) + { + result[(j - 1) * stride] = result[j * stride]; + } + result[(degree - 1) * stride] = 0; + + for (uint8_t j = 0; j < degree; j++) + { + result[j * stride] ^= rs_multiply(coeff[j], factor); + } + } +} + +/* QrCode */ + +static int8_t encodeDataCodewords(BitBucket *dataCodewords, const uint8_t *text, uint16_t length, uint8_t version) +{ + int8_t mode = MODE_BYTE; + + if (isNumeric((char *)text, length)) + { + mode = MODE_NUMERIC; + bb_appendBits(dataCodewords, 1 << MODE_NUMERIC, 4); + bb_appendBits(dataCodewords, length, getModeBits(version, MODE_NUMERIC)); + + uint16_t accumData = 0; + uint8_t accumCount = 0; + for (uint16_t i = 0; i < length; i++) + { + accumData = accumData * 10 + ((char)(text[i]) - '0'); + accumCount++; + if (accumCount == 3) + { + bb_appendBits(dataCodewords, accumData, 10); + accumData = 0; + accumCount = 0; + } + } + + // 1 or 2 digits remaining + if (accumCount > 0) + { + bb_appendBits(dataCodewords, accumData, accumCount * 3 + 1); + } + } + else if (isAlphanumeric((char *)text, length)) + { + mode = MODE_ALPHANUMERIC; + bb_appendBits(dataCodewords, 1 << MODE_ALPHANUMERIC, 4); + bb_appendBits(dataCodewords, length, getModeBits(version, MODE_ALPHANUMERIC)); + + uint16_t accumData = 0; + uint8_t accumCount = 0; + for (uint16_t i = 0; i < length; i++) + { + accumData = accumData * 45 + getAlphanumeric((char)(text[i])); + accumCount++; + if (accumCount == 2) + { + bb_appendBits(dataCodewords, accumData, 11); + accumData = 0; + accumCount = 0; + } + } + + // 1 character remaining + if (accumCount > 0) + { + bb_appendBits(dataCodewords, accumData, 6); + } + } + else + { + bb_appendBits(dataCodewords, 1 << MODE_BYTE, 4); + bb_appendBits(dataCodewords, length, getModeBits(version, MODE_BYTE)); + for (uint16_t i = 0; i < length; i++) + { + bb_appendBits(dataCodewords, (char)(text[i]), 8); + } + } + + //bb_setBits(dataCodewords, length, 4, getModeBits(version, mode)); + + return mode; +} + +static void performErrorCorrection(uint8_t version, uint8_t ecc, BitBucket *data) +{ + + // See: http://www.thonky.com/qr-code-tutorial/structure-final-message + +#if LOCK_VERSION == 0 + uint8_t numBlocks = NUM_ERROR_CORRECTION_BLOCKS[ecc][version - 1]; + uint16_t totalEcc = NUM_ERROR_CORRECTION_CODEWORDS[ecc][version - 1]; + uint16_t moduleCount = NUM_RAW_DATA_MODULES[version - 1]; +#else + uint8_t numBlocks = NUM_ERROR_CORRECTION_BLOCKS[ecc]; + uint16_t totalEcc = NUM_ERROR_CORRECTION_CODEWORDS[ecc]; + uint16_t moduleCount = NUM_RAW_DATA_MODULES; +#endif + + uint8_t blockEccLen = totalEcc / numBlocks; + uint8_t numShortBlocks = numBlocks - moduleCount / 8 % numBlocks; + uint8_t shortBlockLen = moduleCount / 8 / numBlocks; + + uint8_t shortDataBlockLen = shortBlockLen - blockEccLen; + + uint8_t result[data->capacityBytes]; + memset(result, 0, sizeof(result)); + + uint8_t coeff[blockEccLen]; + rs_init(blockEccLen, coeff); + + uint16_t offset = 0; + uint8_t *dataBytes = data->data; + + // Interleave all short blocks + for (uint8_t i = 0; i < shortDataBlockLen; i++) + { + uint16_t index = i; + uint8_t stride = shortDataBlockLen; + for (uint8_t blockNum = 0; blockNum < numBlocks; blockNum++) + { + result[offset++] = dataBytes[index]; + +#if LOCK_VERSION == 0 || LOCK_VERSION >= 5 + if (blockNum == numShortBlocks) + { + stride++; + } +#endif + index += stride; + } + } + + // Version less than 5 only have short blocks +#if LOCK_VERSION == 0 || LOCK_VERSION >= 5 + { + // Interleave long blocks + uint16_t index = shortDataBlockLen * (numShortBlocks + 1); + uint8_t stride = shortDataBlockLen; + for (uint8_t blockNum = 0; blockNum < numBlocks - numShortBlocks; blockNum++) + { + result[offset++] = dataBytes[index]; + + if (blockNum == 0) + { + stride++; + } + index += stride; + } + } +#endif + + // Add all ecc blocks, interleaved + uint8_t blockSize = shortDataBlockLen; + for (uint8_t blockNum = 0; blockNum < numBlocks; blockNum++) + { + +#if LOCK_VERSION == 0 || LOCK_VERSION >= 5 + if (blockNum == numShortBlocks) + { + blockSize++; + } +#endif + rs_getRemainder(blockEccLen, coeff, dataBytes, blockSize, &result[offset + blockNum], numBlocks); + dataBytes += blockSize; + } + + memcpy(data->data, result, data->capacityBytes); + data->bitOffsetOrWidth = moduleCount; +} + +// We store the Format bits tightly packed into a single byte (each of the 4 modes is 2 bits) +// The format bits can be determined by ECC_FORMAT_BITS >> (2 * ecc) +static const uint8_t ECC_FORMAT_BITS = (0x02 << 6) | (0x03 << 4) | (0x00 << 2) | (0x01 << 0); + +/* Public QRCode functions */ + +uint16_t qrcode_getBufferSize(uint8_t version) +{ + return bb_getGridSizeBytes(4 * version + 17); +} + +// @TODO: Return error if data is too big. +int8_t qrcode_initBytes(QRCode *qrcoded, uint8_t *modules, uint8_t version, uint8_t ecc, uint8_t *data, uint16_t length) +{ + uint8_t size = version * 4 + 17; + qrcoded->version = version; + qrcoded->size = size; + qrcoded->ecc = ecc; + qrcoded->modules = modules; + + uint8_t eccFormatBits = (ECC_FORMAT_BITS >> (2 * ecc)) & 0x03; + +#if LOCK_VERSION == 0 + uint16_t moduleCount = NUM_RAW_DATA_MODULES[version - 1]; + uint16_t dataCapacity = moduleCount / 8 - NUM_ERROR_CORRECTION_CODEWORDS[eccFormatBits][version - 1]; +#else + version = LOCK_VERSION; + uint16_t moduleCount = NUM_RAW_DATA_MODULES; + uint16_t dataCapacity = moduleCount / 8 - NUM_ERROR_CORRECTION_CODEWORDS[eccFormatBits]; +#endif + + struct BitBucket codewords; + uint8_t codewordBytes[bb_getBufferSizeBytes(moduleCount)]; + bb_initBuffer(&codewords, codewordBytes, (int32_t)sizeof(codewordBytes)); + + // Place the data code words into the buffer + int8_t mode = encodeDataCodewords(&codewords, data, length, version); + + if (mode < 0) + { + return -1; + } + qrcoded->mode = mode; + + // Add terminator and pad up to a byte if applicable + uint32_t padding = (dataCapacity * 8) - codewords.bitOffsetOrWidth; + if (padding > 4) + { + padding = 4; + } + bb_appendBits(&codewords, 0, padding); + bb_appendBits(&codewords, 0, (8 - codewords.bitOffsetOrWidth % 8) % 8); + + // Pad with alternate bytes until data capacity is reached + for (uint8_t padByte = 0xEC; codewords.bitOffsetOrWidth < (dataCapacity * 8); padByte ^= 0xEC ^ 0x11) + { + bb_appendBits(&codewords, padByte, 8); + } + + BitBucket modulesGrid; + bb_initGrid(&modulesGrid, modules, size); + + BitBucket isFunctionGrid; + uint8_t isFunctionGridBytes[bb_getGridSizeBytes(size)]; + bb_initGrid(&isFunctionGrid, isFunctionGridBytes, size); + + // Draw function patterns, draw all codewords, do masking + drawFunctionPatterns(&modulesGrid, &isFunctionGrid, version, eccFormatBits); + performErrorCorrection(version, eccFormatBits, &codewords); + drawCodewords(&modulesGrid, &isFunctionGrid, &codewords); + + // Find the best (lowest penalty) mask + uint8_t mask = 0; + int32_t minPenalty = INT32_MAX; + for (uint8_t i = 0; i < 8; i++) + { + drawFormatBits(&modulesGrid, &isFunctionGrid, eccFormatBits, i); + applyMask(&modulesGrid, &isFunctionGrid, i); + int penalty = getPenaltyScore(&modulesGrid); + if (penalty < minPenalty) + { + mask = i; + minPenalty = penalty; + } + applyMask(&modulesGrid, &isFunctionGrid, i); // Undoes the mask due to XOR + } + + qrcoded->mask = mask; + + // Overwrite old format bits + drawFormatBits(&modulesGrid, &isFunctionGrid, eccFormatBits, mask); + + // Apply the final choice of mask + applyMask(&modulesGrid, &isFunctionGrid, mask); + + return 0; +} + +int8_t qrcode_initText(QRCode *qrcoded, uint8_t *modules, uint8_t version, uint8_t ecc, const char *data) +{ + return qrcode_initBytes(qrcoded, modules, version, ecc, (uint8_t *)data, strlen(data)); +} + +bool qrcode_getModule(QRCode *qrcoded, uint8_t x, uint8_t y) +{ + if (x >= qrcoded->size || y >= qrcoded->size) + { + return false; + } + + uint32_t offset = y * qrcoded->size + x; + return (qrcoded->modules[offset >> 3] & (1 << (7 - (offset & 0x07)))) != 0; +} + +/* +uint8_t qrcode_getHexLength(QRCode *qrcoded) { + return ((qrcoded->size * qrcoded->size) + 7) / 4; +} + +void qrcode_getHex(QRCode *qrcoded, char *result) { + +} +*/ diff --git a/components/wisp_relay/CMakeLists.txt b/components/wisp_relay/CMakeLists.txt new file mode 100644 index 0000000..5da9a9c --- /dev/null +++ b/components/wisp_relay/CMakeLists.txt @@ -0,0 +1,16 @@ +idf_component_register( + SRCS "ws_server.c" + "storage_engine.c" + "sub_manager.c" + "broadcaster.c" + "rate_limiter.c" + "nip11_relay.c" + "deletion.c" + "flash_monitor.c" + "relay_validator.c" + "router.c" + "handlers.c" + "relay_types.c" + INCLUDE_DIRS "." + REQUIRES esp_http_server esp_timer nvs_flash log json esp_littlefs mbedtls secp256k1 +) diff --git a/components/wisp_relay/broadcaster.c b/components/wisp_relay/broadcaster.c new file mode 100644 index 0000000..738cbdb --- /dev/null +++ b/components/wisp_relay/broadcaster.c @@ -0,0 +1,33 @@ +#include "broadcaster.h" +#include "relay_core.h" +#include "router.h" +#include "sub_manager.h" +#include "esp_log.h" + +static const char *TAG = "broadcaster"; + +void broadcaster_fanout_json(relay_ctx_t *ctx, const char *event_json, + size_t event_len, int event_kind, + const char *event_pubkey_hex, + uint64_t event_created_at) +{ + if (!ctx || !ctx->sub_manager) return; + + sub_match_result_t matches; + sub_manager_match_json(ctx->sub_manager, event_json, event_len, event_kind, + event_pubkey_hex, event_created_at, &matches); + + if (matches.count == 0) { + ESP_LOGD(TAG, "No subscribers for event kind=%d", event_kind); + return; + } + + ESP_LOGD(TAG, "Broadcasting event kind=%d to %d subscriptions", + event_kind, matches.count); + + for (uint8_t i = 0; i < matches.count; i++) { + sub_match_entry_t *entry = &matches.matches[i]; + router_send_event(ctx, entry->conn_fd, entry->sub_id, + event_json, event_len); + } +} diff --git a/components/wisp_relay/broadcaster.h b/components/wisp_relay/broadcaster.h new file mode 100644 index 0000000..0b29f71 --- /dev/null +++ b/components/wisp_relay/broadcaster.h @@ -0,0 +1,11 @@ +#ifndef BROADCASTER_H +#define BROADCASTER_H + +#include "relay_core.h" + +void broadcaster_fanout_json(relay_ctx_t *ctx, const char *event_json, + size_t event_len, int event_kind, + const char *event_pubkey_hex, + uint64_t event_created_at); + +#endif diff --git a/components/wisp_relay/deletion.c b/components/wisp_relay/deletion.c new file mode 100644 index 0000000..7ad3c22 --- /dev/null +++ b/components/wisp_relay/deletion.c @@ -0,0 +1,190 @@ +#include "deletion.h" +#include "relay_types.h" +#include "cJSON.h" +#include "esp_log.h" +#include +#include +#include + +static const char *TAG = "deletion"; + +static int extract_event_id_field(const char *event_json, size_t len, + uint8_t id_out[32]) +{ + cJSON *obj = cJSON_ParseWithLength(event_json, len); + if (!obj) return -1; + cJSON *id_item = cJSON_GetObjectItem(obj, "id"); + if (!id_item || !cJSON_IsString(id_item) || strlen(id_item->valuestring) != 64) { + cJSON_Delete(obj); + return -1; + } + int ret = relay_hex_to_bytes(id_item->valuestring, 64, id_out, 32); + cJSON_Delete(obj); + return ret; +} + +static char *extract_pubkey_hex(const char *event_json, size_t len) +{ + cJSON *obj = cJSON_ParseWithLength(event_json, len); + if (!obj) return NULL; + cJSON *pk = cJSON_GetObjectItem(obj, "pubkey"); + char *result = NULL; + if (pk && cJSON_IsString(pk)) result = strdup(pk->valuestring); + cJSON_Delete(obj); + return result; +} + +static int delete_by_e_tags(storage_engine_t *storage, const char *event_json, + size_t len, const char *deleter_pubkey) +{ + cJSON *obj = cJSON_ParseWithLength(event_json, len); + if (!obj) return 0; + + cJSON *tags = cJSON_GetObjectItem(obj, "tags"); + if (!tags || !cJSON_IsArray(tags)) { cJSON_Delete(obj); return 0; } + + int deleted = 0; + int array_size = cJSON_GetArraySize(tags); + + for (int i = 0; i < array_size; i++) { + cJSON *tag = cJSON_GetArrayItem(tags, i); + if (!cJSON_IsArray(tag)) continue; + cJSON *tag_name = cJSON_GetArrayItem(tag, 0); + if (!tag_name || !cJSON_IsString(tag_name)) continue; + if (strcmp(tag_name->valuestring, "e") != 0) continue; + + cJSON *tag_val = cJSON_GetArrayItem(tag, 1); + if (!tag_val || !cJSON_IsString(tag_val)) continue; + + uint8_t event_id[32]; + if (relay_hex_to_bytes(tag_val->valuestring, 64, event_id, 32) != 0) continue; + + storage_error_t err = storage_delete_event(storage, event_id); + if (err == STORAGE_OK) { + deleted++; + ESP_LOGI(TAG, "Deleted event: %.16s...", tag_val->valuestring); + } + } + + cJSON_Delete(obj); + return deleted; +} + +static int delete_by_a_tags(storage_engine_t *storage, const char *event_json, + size_t len, const char *deleter_pubkey, + uint64_t created_at) +{ + cJSON *obj = cJSON_ParseWithLength(event_json, len); + if (!obj) return 0; + + cJSON *tags = cJSON_GetObjectItem(obj, "tags"); + if (!tags || !cJSON_IsArray(tags)) { cJSON_Delete(obj); return 0; } + + int deleted = 0; + int array_size = cJSON_GetArraySize(tags); + + for (int i = 0; i < array_size; i++) { + cJSON *tag = cJSON_GetArrayItem(tags, i); + if (!cJSON_IsArray(tag)) continue; + cJSON *tag_name = cJSON_GetArrayItem(tag, 0); + if (!tag_name || !cJSON_IsString(tag_name)) continue; + if (strcmp(tag_name->valuestring, "a") != 0) continue; + + cJSON *tag_val = cJSON_GetArrayItem(tag, 1); + if (!tag_val || !cJSON_IsString(tag_val)) continue; + + int32_t kind; + char pubkey[65] = {0}; + char d_tag[256] = ""; + if (sscanf(tag_val->valuestring, "%" SCNd32 ":%64[^:]:%255s", + &kind, pubkey, d_tag) < 2) + continue; + + if (strcmp(pubkey, deleter_pubkey) != 0) continue; + + char **results = NULL; + uint16_t count = 0; + storage_query_events_json(storage, kind, pubkey, 100, &results, &count); + for (uint16_t e = 0; e < count; e++) { + if (storage_delete_event(storage, (const uint8_t *)results[e]) == STORAGE_OK) { + deleted++; + } + } + storage_free_query_results(results, count); + } + + cJSON_Delete(obj); + return deleted; +} + +static int delete_by_k_tags(storage_engine_t *storage, const char *event_json, + size_t len, const char *deleter_pubkey, + uint64_t created_at) +{ + cJSON *obj = cJSON_ParseWithLength(event_json, len); + if (!obj) return 0; + + cJSON *tags = cJSON_GetObjectItem(obj, "tags"); + if (!tags || !cJSON_IsArray(tags)) { cJSON_Delete(obj); return 0; } + + int deleted = 0; + int array_size = cJSON_GetArraySize(tags); + + for (int i = 0; i < array_size; i++) { + cJSON *tag = cJSON_GetArrayItem(tags, i); + if (!cJSON_IsArray(tag)) continue; + cJSON *tag_name = cJSON_GetArrayItem(tag, 0); + if (!tag_name || !cJSON_IsString(tag_name)) continue; + if (strcmp(tag_name->valuestring, "k") != 0) continue; + + cJSON *tag_val = cJSON_GetArrayItem(tag, 1); + if (!tag_val || !cJSON_IsString(tag_val)) continue; + + int kind = atoi(tag_val->valuestring); + + char **results = NULL; + uint16_t count = 0; + storage_query_events_json(storage, kind, deleter_pubkey, 500, &results, &count); + for (uint16_t e = 0; e < count; e++) { + uint8_t eid[32]; + if (extract_event_id_field(results[e], strlen(results[e]), eid) == 0) { + storage_delete_event(storage, eid); + deleted++; + } + } + storage_free_query_results(results, count); + } + + cJSON_Delete(obj); + return deleted; +} + +int deletion_process_json(storage_engine_t *storage, const char *event_json, + size_t event_len) +{ + if (!storage || !event_json) return 0; + + cJSON *obj = cJSON_ParseWithLength(event_json, event_len); + if (!obj) return 0; + cJSON *kind_item = cJSON_GetObjectItem(obj, "kind"); + int kind = kind_item ? kind_item->valueint : 0; + cJSON *pk_item = cJSON_GetObjectItem(obj, "pubkey"); + const char *pubkey = pk_item ? pk_item->valuestring : ""; + cJSON *ca_item = cJSON_GetObjectItem(obj, "created_at"); + uint64_t created_at = ca_item ? (uint64_t)ca_item->valuedouble : 0; + cJSON_Delete(obj); + + if (kind != NOSTR_KIND_DELETION) return 0; + + char *deleter_pk = strdup(pubkey); + if (!deleter_pk) return 0; + + int deleted = 0; + deleted += delete_by_e_tags(storage, event_json, event_len, deleter_pk); + deleted += delete_by_a_tags(storage, event_json, event_len, deleter_pk, created_at); + deleted += delete_by_k_tags(storage, event_json, event_len, deleter_pk, created_at); + + free(deleter_pk); + ESP_LOGI(TAG, "Deletion processed: %d events removed", deleted); + return deleted; +} diff --git a/components/wisp_relay/deletion.h b/components/wisp_relay/deletion.h new file mode 100644 index 0000000..b494a8e --- /dev/null +++ b/components/wisp_relay/deletion.h @@ -0,0 +1,11 @@ +#ifndef DELETION_H +#define DELETION_H + +#include "storage_engine.h" + +#define NOSTR_KIND_DELETION 5 + +int deletion_process_json(storage_engine_t *storage, const char *event_json, + size_t event_len); + +#endif diff --git a/components/wisp_relay/flash_monitor.c b/components/wisp_relay/flash_monitor.c new file mode 100644 index 0000000..ceb8c3b --- /dev/null +++ b/components/wisp_relay/flash_monitor.c @@ -0,0 +1,30 @@ +#include "flash_monitor.h" +#include "esp_littlefs.h" +#include "esp_log.h" +#include + +static const char *TAG = "flash_monitor"; + +void flash_get_health(const char *partition_label, flash_health_t *health) +{ + memset(health, 0, sizeof(flash_health_t)); + + esp_err_t ret = esp_littlefs_info(partition_label, + &health->total_bytes, + &health->used_bytes); + if (ret != ESP_OK) { + ESP_LOGE(TAG, "Failed to get LittleFS info: %s", esp_err_to_name(ret)); + return; + } + + if (health->total_bytes == 0) { + health->free_bytes = 0; + health->usage_percent = 0.0f; + } else { + health->free_bytes = health->total_bytes - health->used_bytes; + health->usage_percent = (float)health->used_bytes / health->total_bytes * 100.0f; + } + + ESP_LOGD(TAG, "Flash: %.1f%% used (%zu/%zu bytes)", + health->usage_percent, health->used_bytes, health->total_bytes); +} diff --git a/components/wisp_relay/flash_monitor.h b/components/wisp_relay/flash_monitor.h new file mode 100644 index 0000000..86f1b53 --- /dev/null +++ b/components/wisp_relay/flash_monitor.h @@ -0,0 +1,16 @@ +#ifndef FLASH_MONITOR_H +#define FLASH_MONITOR_H + +#include +#include + +typedef struct { + size_t total_bytes; + size_t used_bytes; + size_t free_bytes; + float usage_percent; +} flash_health_t; + +void flash_get_health(const char *partition_label, flash_health_t *health); + +#endif diff --git a/components/wisp_relay/handlers.c b/components/wisp_relay/handlers.c new file mode 100644 index 0000000..2164725 --- /dev/null +++ b/components/wisp_relay/handlers.c @@ -0,0 +1,203 @@ +#include "handlers.h" +#include "router.h" +#include "storage_engine.h" +#include "sub_manager.h" +#include "relay_validator.h" +#include "broadcaster.h" +#include "deletion.h" +#include "rate_limiter.h" +#include "relay_types.h" +#include "cJSON.h" +#include "esp_log.h" +#include + +static const char *TAG = "handlers"; + +int handle_event(relay_ctx_t *ctx, int conn_fd, const char *event_json, size_t event_len) +{ + if (!ctx || !event_json) return -1; + + if (ctx->rate_limiter) { + if (!rate_limiter_check(ctx->rate_limiter, conn_fd, RATE_TYPE_EVENT)) { + router_send_ok(ctx, conn_fd, "", false, "rate limited"); + return -1; + } + } + + cJSON *obj = cJSON_ParseWithLength(event_json, event_len); + if (!obj) { + router_send_ok(ctx, conn_fd, "", false, "invalid JSON"); + return -1; + } + + cJSON *id_item = cJSON_GetObjectItem(obj, "id"); + cJSON *pubkey_item = cJSON_GetObjectItem(obj, "pubkey"); + cJSON *kind_item = cJSON_GetObjectItem(obj, "kind"); + cJSON *ca_item = cJSON_GetObjectItem(obj, "created_at"); + + if (!id_item || !pubkey_item || !kind_item || !ca_item) { + cJSON_Delete(obj); + router_send_ok(ctx, conn_fd, "", false, "missing required fields"); + return -1; + } + + const char *id_hex = id_item->valuestring; + const char *pubkey_hex = pubkey_item->valuestring; + int kind = kind_item->valueint; + uint64_t created_at = (uint64_t)ca_item->valuedouble; + + if (ctx->config.max_future_sec > 0) { + int64_t now = (int64_t)(xTaskGetTickCount() / configTICK_RATE_HZ); + if ((int64_t)created_at > now + ctx->config.max_future_sec) { + cJSON_Delete(obj); + router_send_ok(ctx, conn_fd, id_hex, false, "created_at too far in future"); + return -1; + } + } + + uint8_t event_id[32]; + if (relay_hex_to_bytes(id_hex, 64, event_id, 32) != 0) { + cJSON_Delete(obj); + router_send_ok(ctx, conn_fd, "", false, "invalid event id"); + return -1; + } + + if (storage_event_exists(ctx->storage, event_id)) { + cJSON_Delete(obj); + router_send_ok(ctx, conn_fd, id_hex, true, "duplicate"); + return 0; + } + + if (!relay_validator_verify_event(event_json, event_len)) { + cJSON_Delete(obj); + router_send_ok(ctx, conn_fd, id_hex, false, "invalid signature"); + return -1; + } + + cJSON_Delete(obj); + + storage_error_t err = storage_save_event_json(ctx->storage, event_json, event_len); + if (err != STORAGE_OK) { + const char *msg = (err == STORAGE_ERR_FULL) ? "relay full" : + (err == STORAGE_ERR_DUPLICATE) ? "duplicate" : "storage error"; + router_send_ok(ctx, conn_fd, id_hex, false, msg); + return -1; + } + + router_send_ok(ctx, conn_fd, id_hex, true, ""); + + if (kind == NOSTR_KIND_DELETION) { + deletion_process_json(ctx->storage, event_json, event_len); + } + + broadcaster_fanout_json(ctx, event_json, event_len, kind, pubkey_hex, created_at); + + return 0; +} + +static void parse_filter_json(const char *json, sub_filter_t *filter) +{ + memset(filter, 0, sizeof(sub_filter_t)); + cJSON *obj = cJSON_Parse(json); + if (!obj) return; + + cJSON *arr; + + arr = cJSON_GetObjectItem(obj, "ids"); + if (arr && cJSON_IsArray(arr)) { + filter->ids_count = cJSON_GetArraySize(arr); + if (filter->ids_count > SUB_MAX_FILTER_IDS) filter->ids_count = SUB_MAX_FILTER_IDS; + for (size_t i = 0; i < filter->ids_count; i++) + filter->ids[i] = strdup(cJSON_GetArrayItem(arr, i)->valuestring); + } + + arr = cJSON_GetObjectItem(obj, "authors"); + if (arr && cJSON_IsArray(arr)) { + filter->authors_count = cJSON_GetArraySize(arr); + if (filter->authors_count > SUB_MAX_FILTER_AUTHORS) filter->authors_count = SUB_MAX_FILTER_AUTHORS; + for (size_t i = 0; i < filter->authors_count; i++) + filter->authors[i] = strdup(cJSON_GetArrayItem(arr, i)->valuestring); + } + + arr = cJSON_GetObjectItem(obj, "kinds"); + if (arr && cJSON_IsArray(arr)) { + filter->kinds_count = cJSON_GetArraySize(arr); + if (filter->kinds_count > SUB_MAX_FILTER_KINDS) filter->kinds_count = SUB_MAX_FILTER_KINDS; + for (size_t i = 0; i < filter->kinds_count; i++) + filter->kinds[i] = cJSON_GetArrayItem(arr, i)->valueint; + } + + arr = cJSON_GetObjectItem(obj, "#e"); + if (arr && cJSON_IsArray(arr)) { + filter->e_tags_count = cJSON_GetArraySize(arr); + if (filter->e_tags_count > SUB_MAX_FILTER_ETAGS) filter->e_tags_count = SUB_MAX_FILTER_ETAGS; + for (size_t i = 0; i < filter->e_tags_count; i++) + filter->e_tags[i] = strdup(cJSON_GetArrayItem(arr, i)->valuestring); + } + + arr = cJSON_GetObjectItem(obj, "#p"); + if (arr && cJSON_IsArray(arr)) { + filter->p_tags_count = cJSON_GetArraySize(arr); + if (filter->p_tags_count > SUB_MAX_FILTER_PTAGS) filter->p_tags_count = SUB_MAX_FILTER_PTAGS; + for (size_t i = 0; i < filter->p_tags_count; i++) + filter->p_tags[i] = strdup(cJSON_GetArrayItem(arr, i)->valuestring); + } + + cJSON *since = cJSON_GetObjectItem(obj, "since"); + if (since) filter->since = (int64_t)since->valuedouble; + cJSON *until = cJSON_GetObjectItem(obj, "until"); + if (until) filter->until = (int64_t)until->valuedouble; + cJSON *limit = cJSON_GetObjectItem(obj, "limit"); + if (limit) filter->limit = limit->valueint; + + cJSON_Delete(obj); +} + +void handle_req(relay_ctx_t *ctx, int conn_fd, const char *sub_id, const char *filters_json) +{ + if (!ctx || !sub_id) return; + + if (ctx->rate_limiter) { + if (!rate_limiter_check(ctx->rate_limiter, conn_fd, RATE_TYPE_REQ)) { + router_send_closed(ctx, conn_fd, sub_id, "rate limited"); + return; + } + } + + sub_filter_t filter; + parse_filter_json(filters_json, &filter); + + int query_kind = -1; + const char *query_author = NULL; + int query_limit = filter.limit > 0 ? filter.limit : 100; + + if (filter.kinds_count > 0) query_kind = filter.kinds[0]; + if (filter.authors_count > 0) query_author = filter.authors[0]; + + char **results = NULL; + uint16_t count = 0; + storage_query_events_json(ctx->storage, query_kind, query_author, + query_limit, &results, &count); + + for (uint16_t i = 0; i < count; i++) { + router_send_event(ctx, conn_fd, sub_id, results[i], strlen(results[i])); + } + storage_free_query_results(results, count); + + router_send_eose(ctx, conn_fd, sub_id); + + sub_manager_add(ctx->sub_manager, conn_fd, sub_id, &filter, 1); + + sub_filter_t *f = &filter; + for (size_t i = 0; i < f->ids_count; i++) free(f->ids[i]); + for (size_t i = 0; i < f->authors_count; i++) free(f->authors[i]); + for (size_t i = 0; i < f->e_tags_count; i++) free(f->e_tags[i]); + for (size_t i = 0; i < f->p_tags_count; i++) free(f->p_tags[i]); +} + +int handle_close(relay_ctx_t *ctx, int conn_fd, const char *sub_id) +{ + if (!ctx || !sub_id) return -1; + sub_manager_remove(ctx->sub_manager, conn_fd, sub_id); + return 0; +} diff --git a/components/wisp_relay/handlers.h b/components/wisp_relay/handlers.h new file mode 100644 index 0000000..91621bf --- /dev/null +++ b/components/wisp_relay/handlers.h @@ -0,0 +1,10 @@ +#ifndef HANDLERS_H +#define HANDLERS_H + +#include "relay_core.h" + +int handle_event(relay_ctx_t *ctx, int conn_fd, const char *event_json, size_t event_len); +void handle_req(relay_ctx_t *ctx, int conn_fd, const char *sub_id, const char *filters_json); +int handle_close(relay_ctx_t *ctx, int conn_fd, const char *sub_id); + +#endif diff --git a/components/wisp_relay/idf_component.yml b/components/wisp_relay/idf_component.yml new file mode 100644 index 0000000..c093387 --- /dev/null +++ b/components/wisp_relay/idf_component.yml @@ -0,0 +1 @@ +dependencies: {} diff --git a/components/wisp_relay/nip11_relay.c b/components/wisp_relay/nip11_relay.c new file mode 100644 index 0000000..4e1df37 --- /dev/null +++ b/components/wisp_relay/nip11_relay.c @@ -0,0 +1,53 @@ +#include "nip11_relay.h" +#include + +static const char *NIP11_JSON = +"{" + "\"name\":\"TollGate Relay\"," + "\"description\":\"Local Nostr relay with 21-day TTL and negentropy sync\"," + "\"pubkey\":\"\"," + "\"contact\":\"\"," + "\"supported_nips\":[1,9,11,20,40,77]," + "\"software\":\"https://github.com/nicobao/esp32-tollgate\"," + "\"version\":\"1.0.0\"," + "\"limitation\":{" + "\"max_message_length\":65536," + "\"max_subscriptions\":8," + "\"max_filters\":4," + "\"max_limit\":500," + "\"max_subid_length\":64," + "\"max_event_tags\":100," + "\"max_content_length\":32768," + "\"min_pow_difficulty\":0," + "\"auth_required\":false," + "\"payment_required\":false" + "}," + "\"retention\":[{\"kinds\":[0,1,2,3,4,5,6,7],\"time\":1814400}]," + "\"relay_countries\":[\"DE\"]" +"}"; + +esp_err_t relay_nip11_handler(httpd_req_t *req) +{ + char accept[64] = ""; + httpd_req_get_hdr_value_str(req, "Accept", accept, sizeof(accept)); + + if (strstr(accept, "application/nostr+json")) { + httpd_resp_set_type(req, "application/nostr+json"); + } else { + httpd_resp_set_type(req, "application/json"); + } + + httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*"); + httpd_resp_set_hdr(req, "Access-Control-Allow-Headers", "Content-Type, Accept"); + httpd_resp_set_hdr(req, "Access-Control-Allow-Methods", "GET, OPTIONS"); + return httpd_resp_send(req, NIP11_JSON, strlen(NIP11_JSON)); +} + +esp_err_t relay_nip11_options_handler(httpd_req_t *req) +{ + httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*"); + httpd_resp_set_hdr(req, "Access-Control-Allow-Headers", "Content-Type, Accept"); + httpd_resp_set_hdr(req, "Access-Control-Allow-Methods", "GET, OPTIONS"); + httpd_resp_set_status(req, "204 No Content"); + return httpd_resp_send(req, NULL, 0); +} diff --git a/components/wisp_relay/nip11_relay.h b/components/wisp_relay/nip11_relay.h new file mode 100644 index 0000000..84f7971 --- /dev/null +++ b/components/wisp_relay/nip11_relay.h @@ -0,0 +1,9 @@ +#ifndef NIP11_RELAY_H +#define NIP11_RELAY_H + +#include "esp_http_server.h" + +esp_err_t relay_nip11_handler(httpd_req_t *req); +esp_err_t relay_nip11_options_handler(httpd_req_t *req); + +#endif diff --git a/components/wisp_relay/rate_limiter.c b/components/wisp_relay/rate_limiter.c new file mode 100644 index 0000000..7734e03 --- /dev/null +++ b/components/wisp_relay/rate_limiter.c @@ -0,0 +1,98 @@ +#include "rate_limiter.h" +#include "esp_timer.h" +#include "esp_log.h" +#include + +static const char *TAG = "rate_limiter"; + +void rate_limiter_init(rate_limiter_t *rl, const rate_config_t *config) +{ + memset(rl, 0, sizeof(rate_limiter_t)); + rl->lock = xSemaphoreCreateMutex(); + if (config) { + memcpy(&rl->config, config, sizeof(rate_config_t)); + } else { + rl->config.events_per_minute = 30; + rl->config.reqs_per_minute = 60; + } +} + +void rate_limiter_destroy(rate_limiter_t *rl) +{ + if (!rl) return; + if (rl->lock) { + vSemaphoreDelete(rl->lock); + rl->lock = NULL; + } +} + +static rate_bucket_t* get_bucket(rate_limiter_t *rl, int fd) +{ + for (int i = 0; i < RATE_LIMITER_MAX_BUCKETS; i++) { + if (rl->buckets[i].active && rl->buckets[i].fd == fd) { + return &rl->buckets[i]; + } + } + for (int i = 0; i < RATE_LIMITER_MAX_BUCKETS; i++) { + if (!rl->buckets[i].active) { + rl->buckets[i].fd = fd; + rl->buckets[i].active = true; + rl->buckets[i].event_count = 0; + rl->buckets[i].req_count = 0; + rl->buckets[i].window_start = esp_timer_get_time() / 1000000; + return &rl->buckets[i]; + } + } + return NULL; +} + +bool rate_limiter_check(rate_limiter_t *rl, int fd, rate_type_t type) +{ + xSemaphoreTake(rl->lock, portMAX_DELAY); + + rate_bucket_t *bucket = get_bucket(rl, fd); + if (!bucket) { + xSemaphoreGive(rl->lock); + return false; + } + + uint32_t now = esp_timer_get_time() / 1000000; + + if (now - bucket->window_start >= 60) { + bucket->event_count = 0; + bucket->req_count = 0; + bucket->window_start = now; + } + + bool allowed = true; + if (type == RATE_TYPE_EVENT) { + if (bucket->event_count >= rl->config.events_per_minute) { + ESP_LOGW(TAG, "Rate limited: fd=%d events=%d", fd, bucket->event_count); + allowed = false; + } else { + bucket->event_count++; + } + } else { + if (bucket->req_count >= rl->config.reqs_per_minute) { + ESP_LOGW(TAG, "Rate limited: fd=%d reqs=%d", fd, bucket->req_count); + allowed = false; + } else { + bucket->req_count++; + } + } + + xSemaphoreGive(rl->lock); + return allowed; +} + +void rate_limiter_reset(rate_limiter_t *rl, int fd) +{ + xSemaphoreTake(rl->lock, portMAX_DELAY); + for (int i = 0; i < RATE_LIMITER_MAX_BUCKETS; i++) { + if (rl->buckets[i].active && rl->buckets[i].fd == fd) { + rl->buckets[i].active = false; + break; + } + } + xSemaphoreGive(rl->lock); +} diff --git a/components/wisp_relay/rate_limiter.h b/components/wisp_relay/rate_limiter.h new file mode 100644 index 0000000..655ddf2 --- /dev/null +++ b/components/wisp_relay/rate_limiter.h @@ -0,0 +1,40 @@ +#ifndef RATE_LIMITER_H +#define RATE_LIMITER_H + +#include +#include +#include "freertos/FreeRTOS.h" +#include "freertos/semphr.h" + +#define RATE_LIMITER_MAX_BUCKETS 16 + +typedef enum { + RATE_TYPE_EVENT, + RATE_TYPE_REQ, +} rate_type_t; + +typedef struct { + uint16_t events_per_minute; + uint16_t reqs_per_minute; +} rate_config_t; + +typedef struct { + int fd; + uint16_t event_count; + uint16_t req_count; + uint32_t window_start; + bool active; +} rate_bucket_t; + +typedef struct rate_limiter { + rate_config_t config; + rate_bucket_t buckets[RATE_LIMITER_MAX_BUCKETS]; + SemaphoreHandle_t lock; +} rate_limiter_t; + +void rate_limiter_init(rate_limiter_t *rl, const rate_config_t *config); +void rate_limiter_destroy(rate_limiter_t *rl); +bool rate_limiter_check(rate_limiter_t *rl, int fd, rate_type_t type); +void rate_limiter_reset(rate_limiter_t *rl, int fd); + +#endif diff --git a/components/wisp_relay/relay_core.h b/components/wisp_relay/relay_core.h new file mode 100644 index 0000000..d8e7096 --- /dev/null +++ b/components/wisp_relay/relay_core.h @@ -0,0 +1,27 @@ +#ifndef RELAY_CORE_H +#define RELAY_CORE_H + +#include + +#include "ws_server.h" + +typedef struct sub_manager sub_manager_t; +typedef struct storage_engine storage_engine_t; +typedef struct rate_limiter rate_limiter_t; + +typedef struct relay_ctx { + ws_server_t ws_server; + sub_manager_t *sub_manager; + storage_engine_t *storage; + rate_limiter_t *rate_limiter; + + struct { + uint16_t port; + uint32_t max_event_age_sec; + uint8_t max_subs_per_conn; + uint8_t max_filters_per_sub; + int64_t max_future_sec; + } config; +} relay_ctx_t; + +#endif diff --git a/components/wisp_relay/relay_types.c b/components/wisp_relay/relay_types.c new file mode 100644 index 0000000..9833885 --- /dev/null +++ b/components/wisp_relay/relay_types.c @@ -0,0 +1,21 @@ +#include "relay_types.h" +#include +#include + +int relay_hex_to_bytes(const char *hex, size_t hex_len, uint8_t *out, size_t out_len) +{ + if (hex_len != out_len * 2) return -1; + for (size_t i = 0; i < out_len; i++) { + unsigned int byte; + if (sscanf(hex + i * 2, "%02x", &byte) != 1) return -1; + out[i] = (uint8_t)byte; + } + return 0; +} + +void relay_bytes_to_hex(const uint8_t *bytes, size_t len, char *hex) +{ + for (size_t i = 0; i < len; i++) + sprintf(hex + i * 2, "%02x", bytes[i]); + hex[len * 2] = '\0'; +} diff --git a/components/wisp_relay/relay_types.h b/components/wisp_relay/relay_types.h new file mode 100644 index 0000000..343e51b --- /dev/null +++ b/components/wisp_relay/relay_types.h @@ -0,0 +1,43 @@ +#ifndef RELAY_TYPES_H +#define RELAY_TYPES_H + +#include +#include +#include + +#define RELAY_MAX_EVENT_SIZE 8192 +#define RELAY_ID_SIZE 32 +#define RELAY_SIG_SIZE 64 +#define RELAY_MAX_TAGS 100 +#define RELAY_MAX_TAG_VALUES 10 + +typedef struct relay_event { + uint8_t id[RELAY_ID_SIZE]; + uint8_t pubkey[RELAY_ID_SIZE]; + uint64_t created_at; + int kind; + uint8_t sig[RELAY_SIG_SIZE]; + char content[RELAY_MAX_EVENT_SIZE]; + size_t content_len; +} relay_event_t; + +typedef struct { + char **ids; + size_t ids_count; + char **authors; + size_t authors_count; + int32_t *kinds; + size_t kinds_count; + char **e_tags; + size_t e_tags_count; + char **p_tags; + size_t p_tags_count; + int64_t since; + int64_t until; + int limit; +} relay_filter_t; + +int relay_hex_to_bytes(const char *hex, size_t hex_len, uint8_t *out, size_t out_len); +void relay_bytes_to_hex(const uint8_t *bytes, size_t len, char *hex); + +#endif diff --git a/components/wisp_relay/relay_validator.c b/components/wisp_relay/relay_validator.c new file mode 100644 index 0000000..eb40d22 --- /dev/null +++ b/components/wisp_relay/relay_validator.c @@ -0,0 +1,176 @@ +#include "relay_validator.h" +#include "relay_types.h" +#include "esp_log.h" +#include "mbedtls/sha256.h" +#include "secp256k1.h" +#include "secp256k1_extrakeys.h" +#include "secp256k1_schnorrsig.h" +#include "cJSON.h" +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include +#include +#include +#include + +static const char *TAG = "relay_validator"; + +static int hex_to_bytes(const char *hex, size_t hex_len, uint8_t *out, size_t out_len) +{ + if (hex_len != out_len * 2) return -1; + for (size_t i = 0; i < out_len; i++) { + unsigned int byte; + if (sscanf(hex + i * 2, "%02x", &byte) != 1) return -1; + out[i] = (uint8_t)byte; + } + return 0; +} + +static char *serialize_event_for_id(const char *event_json, size_t event_len) +{ + cJSON *obj = cJSON_ParseWithLength(event_json, event_len); + if (!obj) return NULL; + + cJSON *serial = cJSON_CreateArray(); + cJSON_AddItemToArray(serial, cJSON_CreateNumber(0)); + cJSON_AddItemToArray(serial, cJSON_CreateString( + cJSON_GetObjectItem(obj, "pubkey")->valuestring)); + cJSON_AddItemToArray(serial, cJSON_CreateNumber( + cJSON_GetObjectItem(obj, "created_at")->valuedouble)); + cJSON_AddItemToArray(serial, cJSON_CreateNumber( + cJSON_GetObjectItem(obj, "kind")->valueint)); + cJSON *tags = cJSON_GetObjectItem(obj, "tags"); + cJSON_AddItemToArray(serial, cJSON_Duplicate(tags, 1)); + cJSON_AddItemToArray(serial, cJSON_CreateString( + cJSON_GetObjectItem(obj, "content")->valuestring)); + + char *result = cJSON_PrintUnformatted(serial); + cJSON_Delete(serial); + cJSON_Delete(obj); + return result; +} + +static bool verify_event_id(const char *event_json, size_t event_len, + const uint8_t expected_id[32]) +{ + char *serialized = serialize_event_for_id(event_json, event_len); + if (!serialized) return false; + + uint8_t hash[32]; + mbedtls_sha256((const unsigned char *)serialized, strlen(serialized), hash, 0); + free(serialized); + + return memcmp(hash, expected_id, 32) == 0; +} + +static bool verify_schnorr_sig(const uint8_t pubkey[32], const uint8_t msg[32], + const uint8_t sig[64]) +{ + secp256k1_context *ctx = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY); + if (!ctx) return false; + + secp256k1_xonly_pubkey xonly_pub; + if (!secp256k1_xonly_pubkey_parse(ctx, &xonly_pub, pubkey)) { + secp256k1_context_destroy(ctx); + return false; + } + + bool valid = secp256k1_schnorrsig_verify(ctx, sig, msg, 32, &xonly_pub); + secp256k1_context_destroy(ctx); + return valid; +} + +bool relay_validator_verify_event(const char *event_json, size_t event_len) +{ + cJSON *obj = cJSON_ParseWithLength(event_json, event_len); + if (!obj) { + ESP_LOGD(TAG, "Invalid JSON"); + return false; + } + + cJSON *id_item = cJSON_GetObjectItem(obj, "id"); + cJSON *pk_item = cJSON_GetObjectItem(obj, "pubkey"); + cJSON *sig_item = cJSON_GetObjectItem(obj, "sig"); + + if (!id_item || !pk_item || !sig_item) { + cJSON_Delete(obj); + ESP_LOGD(TAG, "Missing required fields"); + return false; + } + + const char *id_hex = id_item->valuestring; + const char *pk_hex = pk_item->valuestring; + const char *sig_hex = sig_item->valuestring; + + if (strlen(id_hex) != 64 || strlen(pk_hex) != 64 || strlen(sig_hex) != 128) { + cJSON_Delete(obj); + ESP_LOGD(TAG, "Invalid field lengths"); + return false; + } + + uint8_t event_id[32], pubkey[32], sig[64]; + if (hex_to_bytes(id_hex, 64, event_id, 32) != 0 || + hex_to_bytes(pk_hex, 64, pubkey, 32) != 0 || + hex_to_bytes(sig_hex, 128, sig, 64) != 0) { + cJSON_Delete(obj); + ESP_LOGD(TAG, "Invalid hex encoding"); + return false; + } + + cJSON_Delete(obj); + + if (!verify_event_id(event_json, event_len, event_id)) { + ESP_LOGD(TAG, "Event ID mismatch"); + return false; + } + + if (!verify_schnorr_sig(pubkey, event_id, sig)) { + ESP_LOGD(TAG, "Invalid signature"); + return false; + } + + return true; +} + +validation_result_t relay_validator_check(const uint8_t *id, + const uint8_t *pubkey, + uint64_t created_at, + int kind, + const char *content, + size_t content_len, + const char *tags_json, + const uint8_t *sig, + const validator_config_t *config) +{ + (void)content; (void)content_len; (void)tags_json; + + if (config) { + if (config->max_future_sec > 0) { + int64_t now = (int64_t)(xTaskGetTickCount() / configTICK_RATE_HZ); + if ((int64_t)created_at > now + config->max_future_sec) + return VALIDATION_ERR_FUTURE; + } + } + + if (!verify_schnorr_sig(pubkey, id, sig)) + return VALIDATION_ERR_SIG; + + return VALIDATION_OK; +} + +const char *relay_validator_result_string(validation_result_t result) +{ + switch (result) { + case VALIDATION_OK: return "ok"; + case VALIDATION_ERR_SCHEMA: return "invalid: schema"; + case VALIDATION_ERR_ID: return "invalid: event id"; + case VALIDATION_ERR_SIG: return "invalid: signature"; + case VALIDATION_ERR_EXPIRED: return "invalid: expired"; + case VALIDATION_ERR_FUTURE: return "invalid: future"; + case VALIDATION_ERR_DUPLICATE: return "duplicate"; + case VALIDATION_ERR_POW: return "pow: insufficient"; + case VALIDATION_ERR_BLOCKED: return "blocked"; + case VALIDATION_ERR_TOO_OLD: return "invalid: too old"; + default: return "error: unknown"; + } +} diff --git a/components/wisp_relay/relay_validator.h b/components/wisp_relay/relay_validator.h new file mode 100644 index 0000000..c07308f --- /dev/null +++ b/components/wisp_relay/relay_validator.h @@ -0,0 +1,45 @@ +#ifndef RELAY_VALIDATOR_H +#define RELAY_VALIDATOR_H + +#include +#include +#include + +typedef enum { + VALIDATION_OK = 0, + VALIDATION_ERR_SCHEMA, + VALIDATION_ERR_ID, + VALIDATION_ERR_SIG, + VALIDATION_ERR_EXPIRED, + VALIDATION_ERR_FUTURE, + VALIDATION_ERR_DUPLICATE, + VALIDATION_ERR_POW, + VALIDATION_ERR_BLOCKED, + VALIDATION_ERR_TOO_OLD, +} validation_result_t; + +typedef struct { + uint32_t max_event_age_sec; + int64_t max_future_sec; + uint8_t min_pow_difficulty; + bool check_duplicates; +} validator_config_t; + +typedef struct relay_event relay_event_t; +typedef struct storage_engine storage_engine_t; + +validation_result_t relay_validator_check(const uint8_t *id, + const uint8_t *pubkey, + uint64_t created_at, + int kind, + const char *content, + size_t content_len, + const char *tags_json, + const uint8_t *sig, + const validator_config_t *config); + +bool relay_validator_verify_event(const char *event_json, size_t event_len); + +const char *relay_validator_result_string(validation_result_t result); + +#endif diff --git a/components/wisp_relay/router.c b/components/wisp_relay/router.c new file mode 100644 index 0000000..05aa7d4 --- /dev/null +++ b/components/wisp_relay/router.c @@ -0,0 +1,140 @@ +#include "router.h" +#include "ws_server.h" +#include "handlers.h" +#include "sub_manager.h" +#include "cJSON.h" +#include "esp_log.h" +#include + +static const char *TAG = "router"; + +esp_err_t router_send_notice(relay_ctx_t *ctx, int conn_fd, const char *message) +{ + cJSON *arr = cJSON_CreateArray(); + cJSON_AddItemToArray(arr, cJSON_CreateString("NOTICE")); + cJSON_AddItemToArray(arr, cJSON_CreateString(message)); + char *json = cJSON_PrintUnformatted(arr); + cJSON_Delete(arr); + esp_err_t ret = ws_server_send(&ctx->ws_server, conn_fd, json, strlen(json)); + cJSON_free(json); + return ret; +} + +esp_err_t router_send_ok(relay_ctx_t *ctx, int conn_fd, const char *event_id_hex, + bool accepted, const char *message) +{ + cJSON *arr = cJSON_CreateArray(); + cJSON_AddItemToArray(arr, cJSON_CreateString("OK")); + cJSON_AddItemToArray(arr, cJSON_CreateString(event_id_hex)); + cJSON_AddItemToArray(arr, cJSON_CreateBool(accepted)); + cJSON_AddItemToArray(arr, cJSON_CreateString(message ? message : "")); + char *json = cJSON_PrintUnformatted(arr); + cJSON_Delete(arr); + esp_err_t ret = ws_server_send(&ctx->ws_server, conn_fd, json, strlen(json)); + cJSON_free(json); + return ret; +} + +esp_err_t router_send_eose(relay_ctx_t *ctx, int conn_fd, const char *sub_id) +{ + cJSON *arr = cJSON_CreateArray(); + cJSON_AddItemToArray(arr, cJSON_CreateString("EOSE")); + cJSON_AddItemToArray(arr, cJSON_CreateString(sub_id)); + char *json = cJSON_PrintUnformatted(arr); + cJSON_Delete(arr); + esp_err_t ret = ws_server_send(&ctx->ws_server, conn_fd, json, strlen(json)); + cJSON_free(json); + return ret; +} + +esp_err_t router_send_closed(relay_ctx_t *ctx, int conn_fd, const char *sub_id, + const char *message) +{ + cJSON *arr = cJSON_CreateArray(); + cJSON_AddItemToArray(arr, cJSON_CreateString("CLOSED")); + cJSON_AddItemToArray(arr, cJSON_CreateString(sub_id)); + cJSON_AddItemToArray(arr, cJSON_CreateString(message ? message : "")); + char *json = cJSON_PrintUnformatted(arr); + cJSON_Delete(arr); + esp_err_t ret = ws_server_send(&ctx->ws_server, conn_fd, json, strlen(json)); + cJSON_free(json); + return ret; +} + +esp_err_t router_send_event(relay_ctx_t *ctx, int conn_fd, const char *sub_id, + const char *event_json, size_t event_len) +{ + size_t buf_size = event_len + strlen(sub_id) + 32; + char *buf = malloc(buf_size); + if (!buf) return ESP_ERR_NO_MEM; + int n = snprintf(buf, buf_size, "[\"EVENT\",\"%s\",%.*s]", sub_id, (int)event_len, event_json); + esp_err_t ret = ws_server_send(&ctx->ws_server, conn_fd, buf, n); + free(buf); + return ret; +} + +static void on_ws_message(int fd, const char *data, size_t len) +{ + extern relay_ctx_t g_relay_ctx; + router_dispatch(&g_relay_ctx, fd, data, len); +} + +static void on_ws_disconnect(int fd) +{ + extern relay_ctx_t g_relay_ctx; + if (g_relay_ctx.sub_manager) { + sub_manager_remove_all(g_relay_ctx.sub_manager, fd); + } +} + +void router_dispatch(relay_ctx_t *ctx, int conn_fd, const char *data, size_t len) +{ + cJSON *arr = cJSON_ParseWithLength(data, len); + if (!arr || !cJSON_IsArray(arr)) { + router_send_notice(ctx, conn_fd, "invalid JSON"); + if (arr) cJSON_Delete(arr); + return; + } + + int array_size = cJSON_GetArraySize(arr); + if (array_size < 2) { + router_send_notice(ctx, conn_fd, "array too short"); + cJSON_Delete(arr); + return; + } + + cJSON *cmd = cJSON_GetArrayItem(arr, 0); + if (!cmd || !cJSON_IsString(cmd)) { + router_send_notice(ctx, conn_fd, "invalid command"); + cJSON_Delete(arr); + return; + } + + const char *cmd_str = cmd->valuestring; + + if (strcmp(cmd_str, "EVENT") == 0 && array_size >= 2) { + cJSON *event_obj = cJSON_GetArrayItem(arr, 1); + if (event_obj) { + char *event_json = cJSON_PrintUnformatted(event_obj); + handle_event(ctx, conn_fd, event_json, strlen(event_json)); + cJSON_free(event_json); + } + } else if (strcmp(cmd_str, "REQ") == 0 && array_size >= 3) { + cJSON *sub_id_item = cJSON_GetArrayItem(arr, 1); + if (sub_id_item && cJSON_IsString(sub_id_item)) { + cJSON *filter_obj = cJSON_GetArrayItem(arr, 2); + char *filter_json = filter_obj ? cJSON_PrintUnformatted(filter_obj) : strdup("{}"); + handle_req(ctx, conn_fd, sub_id_item->valuestring, filter_json); + free(filter_json); + } + } else if (strcmp(cmd_str, "CLOSE") == 0 && array_size >= 2) { + cJSON *sub_id_item = cJSON_GetArrayItem(arr, 1); + if (sub_id_item && cJSON_IsString(sub_id_item)) { + handle_close(ctx, conn_fd, sub_id_item->valuestring); + } + } else { + router_send_notice(ctx, conn_fd, "unknown command"); + } + + cJSON_Delete(arr); +} diff --git a/components/wisp_relay/router.h b/components/wisp_relay/router.h new file mode 100644 index 0000000..9afd46e --- /dev/null +++ b/components/wisp_relay/router.h @@ -0,0 +1,19 @@ +#ifndef ROUTER_H +#define ROUTER_H + +#include "relay_core.h" +#include +#include + +esp_err_t router_send_notice(relay_ctx_t *ctx, int conn_fd, const char *message); +esp_err_t router_send_ok(relay_ctx_t *ctx, int conn_fd, const char *event_id_hex, + bool accepted, const char *message); +esp_err_t router_send_eose(relay_ctx_t *ctx, int conn_fd, const char *sub_id); +esp_err_t router_send_closed(relay_ctx_t *ctx, int conn_fd, const char *sub_id, + const char *message); +esp_err_t router_send_event(relay_ctx_t *ctx, int conn_fd, const char *sub_id, + const char *event_json, size_t event_len); + +void router_dispatch(relay_ctx_t *ctx, int conn_fd, const char *data, size_t len); + +#endif diff --git a/components/wisp_relay/storage_engine.c b/components/wisp_relay/storage_engine.c new file mode 100644 index 0000000..d26705b --- /dev/null +++ b/components/wisp_relay/storage_engine.c @@ -0,0 +1,402 @@ +#include "storage_engine.h" +#include "esp_littlefs.h" +#include "esp_log.h" +#include "nvs_flash.h" +#include "nvs.h" +#include +#include +#include +#include +#include +#include + +static const char *TAG = "storage"; + +#define INDEX_NVS_NAMESPACE "nostr_idx" +#define EVENTS_DIR "/littlefs/events" + +static void get_event_path(const uint8_t event_id[32], uint32_t file_index, + char *path, size_t len) +{ + char id_hex[33]; + for (int i = 0; i < 16; i++) sprintf(id_hex + i * 2, "%02x", event_id[i]); + snprintf(path, len, EVENTS_DIR "/%02x/%s_%08" PRIx32 ".json", + event_id[0], id_hex, file_index); +} + +static int save_index_to_nvs(storage_engine_t *engine) +{ + nvs_handle_t nvs; + esp_err_t err = nvs_open(INDEX_NVS_NAMESPACE, NVS_READWRITE, &nvs); + if (err != ESP_OK) return STORAGE_ERR_IO; + + nvs_set_u16(nvs, "count", engine->index_count); + nvs_set_u32(nvs, "next_idx", engine->next_file_index); + + const uint16_t chunk_size = 50; + for (uint16_t i = 0; i < engine->index_count; i += chunk_size) { + char key[16]; + snprintf(key, sizeof(key), "idx_%u", i / chunk_size); + uint16_t entries = engine->index_count - i; + if (entries > chunk_size) entries = chunk_size; + nvs_set_blob(nvs, key, &engine->index[i], entries * sizeof(storage_index_entry_t)); + } + nvs_commit(nvs); + nvs_close(nvs); + return STORAGE_OK; +} + +static int load_index_from_nvs(storage_engine_t *engine) +{ + nvs_handle_t nvs; + esp_err_t err = nvs_open(INDEX_NVS_NAMESPACE, NVS_READONLY, &nvs); + if (err == ESP_ERR_NVS_NOT_FOUND) return STORAGE_OK; + if (err != ESP_OK) return STORAGE_ERR_IO; + + err = nvs_get_u16(nvs, "count", &engine->index_count); + if (err != ESP_OK) { nvs_close(nvs); return STORAGE_ERR_IO; } + if (engine->index_count > engine->max_index_entries) engine->index_count = engine->max_index_entries; + + err = nvs_get_u32(nvs, "next_idx", &engine->next_file_index); + if (err != ESP_OK) { nvs_close(nvs); return STORAGE_ERR_IO; } + + const uint16_t chunk_size = 50; + for (uint16_t i = 0; i < engine->index_count; i += chunk_size) { + char key[16]; + snprintf(key, sizeof(key), "idx_%u", i / chunk_size); + uint16_t entries = engine->index_count - i; + if (entries > chunk_size) entries = chunk_size; + size_t len = entries * sizeof(storage_index_entry_t); + nvs_get_blob(nvs, key, &engine->index[i], &len); + } + nvs_close(nvs); + return STORAGE_OK; +} + +static storage_index_entry_t *find_index_entry(storage_engine_t *engine, + const uint8_t event_id[32]) +{ + for (uint16_t i = 0; i < engine->index_count; i++) { + if (memcmp(engine->index[i].event_id, event_id, 32) == 0 && + !(engine->index[i].flags & STORAGE_FLAG_DELETED)) { + return &engine->index[i]; + } + } + return NULL; +} + +static void parse_event_meta(const char *json, size_t len, + uint8_t *id_out, uint8_t *pubkey_out, + uint64_t *created_at_out, int *kind_out) +{ + extern int relay_hex_to_bytes(const char *hex, size_t hex_len, uint8_t *out, size_t out_len); + extern void relay_bytes_to_hex(const uint8_t *bytes, size_t len, char *hex); + + id_out[0] = 0; pubkey_out[0] = 0; *created_at_out = 0; *kind_out = 0; + + const char *p; + p = strstr(json, "\"id\":\""); + if (p) relay_hex_to_bytes(p + 6, 64, id_out, 32); + p = strstr(json, "\"pubkey\":\""); + if (p) relay_hex_to_bytes(p + 10, 64, pubkey_out, 32); + p = strstr(json, "\"created_at\":"); + if (p) *created_at_out = strtoull(p + 13, NULL, 10); + p = strstr(json, "\"kind\":"); + if (p) *kind_out = atoi(p + 7); +} + +esp_err_t storage_init(storage_engine_t *engine, uint32_t default_ttl_sec) +{ + memset(engine, 0, sizeof(storage_engine_t)); + engine->default_ttl_sec = default_ttl_sec; + strcpy(engine->mount_point, "/littlefs"); + + engine->lock = xSemaphoreCreateMutex(); + if (!engine->lock) return ESP_ERR_NO_MEM; + + engine->max_index_entries = STORAGE_INDEX_ENTRIES; + engine->index = heap_caps_calloc(engine->max_index_entries, + sizeof(storage_index_entry_t), + MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT); + if (!engine->index) { + engine->max_index_entries = 1000; + engine->index = calloc(engine->max_index_entries, sizeof(storage_index_entry_t)); + if (!engine->index) { vSemaphoreDelete(engine->lock); return ESP_ERR_NO_MEM; } + } + + esp_vfs_littlefs_conf_t conf = { + .base_path = "/littlefs", + .partition_label = STORAGE_PARTITION_LABEL, + .format_if_mount_failed = true, + .dont_mount = false, + }; + + esp_err_t ret = esp_vfs_littlefs_register(&conf); + if (ret != ESP_OK) { + ESP_LOGE(TAG, "Failed to mount LittleFS: %s", esp_err_to_name(ret)); + free(engine->index); + vSemaphoreDelete(engine->lock); + return ret; + } + + mkdir(EVENTS_DIR, 0755); + for (int i = 0; i < 256; i++) { + char subdir[64]; + snprintf(subdir, sizeof(subdir), EVENTS_DIR "/%02x", i); + mkdir(subdir, 0755); + } + + int load_err = load_index_from_nvs(engine); + if (load_err != STORAGE_OK) { + ESP_LOGW(TAG, "Failed to load index, starting fresh"); + engine->index_count = 0; + engine->next_file_index = 0; + } + + engine->initialized = true; + + size_t total, used; + esp_littlefs_info(STORAGE_PARTITION_LABEL, &total, &used); + ESP_LOGI(TAG, "Storage initialized: %" PRIu16 " events, %zu/%zu bytes used", + engine->index_count, used, total); + return ESP_OK; +} + +void storage_destroy(storage_engine_t *engine) +{ + if (!engine->initialized) return; + if (engine->cleanup_task) { + engine->cleanup_stop = true; + while (engine->cleanup_task != NULL) vTaskDelay(pdMS_TO_TICKS(100)); + } + save_index_to_nvs(engine); + esp_vfs_littlefs_unregister(STORAGE_PARTITION_LABEL); + if (engine->index) { free(engine->index); engine->index = NULL; } + if (engine->lock) { vSemaphoreDelete(engine->lock); engine->lock = NULL; } + engine->initialized = false; +} + +storage_error_t storage_save_event_json(storage_engine_t *engine, + const char *event_json, + size_t event_json_len) +{ + if (!engine->initialized) return STORAGE_ERR_NOT_INITIALIZED; + + uint8_t id[32] = {0}, pubkey[32] = {0}; + uint64_t created_at = 0; + int kind = 0; + parse_event_meta(event_json, event_json_len, id, pubkey, &created_at, &kind); + + xSemaphoreTake(engine->lock, portMAX_DELAY); + + if (find_index_entry(engine, id)) { + xSemaphoreGive(engine->lock); + return STORAGE_ERR_DUPLICATE; + } + if (engine->index_count >= engine->max_index_entries) { + xSemaphoreGive(engine->lock); + return STORAGE_ERR_FULL; + } + + char path[128]; + get_event_path(id, engine->next_file_index, path, sizeof(path)); + FILE *f = fopen(path, "wb"); + if (!f) { + char dir[64]; + snprintf(dir, sizeof(dir), EVENTS_DIR "/%02x", id[0]); + mkdir(dir, 0755); + f = fopen(path, "wb"); + } + if (!f) { xSemaphoreGive(engine->lock); return STORAGE_ERR_IO; } + + fwrite(event_json, 1, event_json_len, f); + fclose(f); + + storage_index_entry_t *entry = &engine->index[engine->index_count]; + memcpy(entry->event_id, id, 32); + entry->created_at = (uint32_t)created_at; + entry->kind = kind; + memcpy(entry->pubkey_prefix, pubkey, 4); + entry->file_index = engine->next_file_index; + entry->flags = 0; + entry->expires_at = (uint32_t)time(NULL) + engine->default_ttl_sec; + + engine->index_count++; + engine->next_file_index++; + if (engine->index_count % 10 == 0) save_index_to_nvs(engine); + + xSemaphoreGive(engine->lock); + return STORAGE_OK; +} + +bool storage_event_exists(storage_engine_t *engine, const uint8_t event_id[32]) +{ + if (!engine->initialized) return false; + xSemaphoreTake(engine->lock, portMAX_DELAY); + bool exists = (find_index_entry(engine, event_id) != NULL); + xSemaphoreGive(engine->lock); + return exists; +} + +storage_error_t storage_query_events_json(storage_engine_t *engine, + int kind, + const char *author_hex, + int limit, + char ***results, + uint16_t *count) +{ + if (!engine->initialized) return STORAGE_ERR_NOT_INITIALIZED; + *results = NULL; + *count = 0; + if (limit > 500) limit = 500; + if (limit <= 0) limit = 100; + + char **out = calloc(limit, sizeof(char *)); + if (!out) return STORAGE_ERR_NO_MEM; + + xSemaphoreTake(engine->lock, portMAX_DELAY); + uint32_t now = (uint32_t)time(NULL); + uint16_t found = 0; + + uint8_t author_prefix[4] = {0}; + int have_author = 0; + if (author_hex && strlen(author_hex) >= 8) { + extern int relay_hex_to_bytes(const char *, size_t, uint8_t *, size_t); + relay_hex_to_bytes(author_hex, 8, author_prefix, 4); + have_author = 1; + } + + for (int i = engine->index_count - 1; i >= 0 && found < limit; i--) { + storage_index_entry_t *e = &engine->index[i]; + if (e->flags & STORAGE_FLAG_DELETED) continue; + if (e->expires_at > 0 && e->expires_at < now) continue; + if (kind > 0 && e->kind != kind) continue; + if (have_author && memcmp(e->pubkey_prefix, author_prefix, 4) != 0) continue; + + char path[128]; + get_event_path(e->event_id, e->file_index, path, sizeof(path)); + FILE *f = fopen(path, "rb"); + if (!f) continue; + fseek(f, 0, SEEK_END); + long sz = ftell(f); + fseek(f, 0, SEEK_SET); + if (sz <= 0 || sz > STORAGE_MAX_EVENT_SIZE) { fclose(f); continue; } + char *buf = malloc(sz + 1); + fread(buf, 1, sz, f); + buf[sz] = '\0'; + fclose(f); + out[found++] = buf; + } + + xSemaphoreGive(engine->lock); + *results = out; + *count = found; + return STORAGE_OK; +} + +void storage_free_query_results(char **results, uint16_t count) +{ + if (!results) return; + for (uint16_t i = 0; i < count; i++) free(results[i]); + free(results); +} + +storage_error_t storage_delete_event(storage_engine_t *engine, const uint8_t event_id[32]) +{ + if (!engine->initialized) return STORAGE_ERR_NOT_INITIALIZED; + xSemaphoreTake(engine->lock, portMAX_DELAY); + storage_index_entry_t *e = find_index_entry(engine, event_id); + if (!e) { xSemaphoreGive(engine->lock); return STORAGE_ERR_NOT_FOUND; } + char path[128]; + get_event_path(e->event_id, e->file_index, path, sizeof(path)); + unlink(path); + e->flags |= STORAGE_FLAG_DELETED; + save_index_to_nvs(engine); + xSemaphoreGive(engine->lock); + return STORAGE_OK; +} + +int storage_purge_expired(storage_engine_t *engine) +{ + if (!engine->initialized) return 0; + xSemaphoreTake(engine->lock, portMAX_DELAY); + uint32_t now = (uint32_t)time(NULL); + int purged = 0; + for (uint16_t i = 0; i < engine->index_count; i++) { + if (engine->index[i].flags & STORAGE_FLAG_DELETED) continue; + if (engine->index[i].expires_at > 0 && engine->index[i].expires_at < now) { + char path[128]; + get_event_path(engine->index[i].event_id, engine->index[i].file_index, path, sizeof(path)); + unlink(path); + engine->index[i].flags |= STORAGE_FLAG_DELETED; + purged++; + } + } + if (purged > 0) { save_index_to_nvs(engine); ESP_LOGI(TAG, "Purged %d expired events", purged); } + xSemaphoreGive(engine->lock); + return purged; +} + +int storage_compact_index(storage_engine_t *engine) +{ + if (!engine->initialized) return 0; + xSemaphoreTake(engine->lock, portMAX_DELAY); + uint16_t write_idx = 0; + int compacted = 0; + for (uint16_t read_idx = 0; read_idx < engine->index_count; read_idx++) { + if (!(engine->index[read_idx].flags & STORAGE_FLAG_DELETED)) { + if (write_idx != read_idx) + memcpy(&engine->index[write_idx], &engine->index[read_idx], sizeof(storage_index_entry_t)); + write_idx++; + } else { + compacted++; + } + } + if (compacted > 0) { + engine->index_count = write_idx; + save_index_to_nvs(engine); + ESP_LOGI(TAG, "Compacted: removed %d, %" PRIu16 " remaining", compacted, engine->index_count); + } + xSemaphoreGive(engine->lock); + return compacted; +} + +void storage_get_stats(storage_engine_t *engine, storage_stats_t *stats) +{ + memset(stats, 0, sizeof(storage_stats_t)); + if (!engine->initialized) return; + xSemaphoreTake(engine->lock, portMAX_DELAY); + uint32_t now = (uint32_t)time(NULL); + for (uint16_t i = 0; i < engine->index_count; i++) { + if (engine->index[i].flags & STORAGE_FLAG_DELETED) continue; + if (engine->index[i].expires_at > 0 && engine->index[i].expires_at < now) continue; + stats->total_events++; + } + size_t total, used; + esp_littlefs_info(STORAGE_PARTITION_LABEL, &total, &used); + stats->total_bytes = total; + stats->free_bytes = total - used; + xSemaphoreGive(engine->lock); +} + +static void storage_cleanup_task(void *arg) +{ + storage_engine_t *engine = (storage_engine_t *)arg; + int cycles = 0; + while (!engine->cleanup_stop) { + for (int i = 0; i < 60 && !engine->cleanup_stop; i++) vTaskDelay(pdMS_TO_TICKS(1000)); + if (engine->cleanup_stop) break; + storage_purge_expired(engine); + if (++cycles >= 10) { storage_compact_index(engine); cycles = 0; } + } + engine->cleanup_task = NULL; + vTaskDelete(NULL); +} + +esp_err_t storage_start_cleanup_task(storage_engine_t *engine) +{ + engine->cleanup_stop = false; + BaseType_t ret = xTaskCreate(storage_cleanup_task, "relay_cleanup", 4096, engine, 2, &engine->cleanup_task); + if (ret != pdPASS) { engine->cleanup_task = NULL; return ESP_ERR_NO_MEM; } + return ESP_OK; +} diff --git a/components/wisp_relay/storage_engine.h b/components/wisp_relay/storage_engine.h new file mode 100644 index 0000000..4e17113 --- /dev/null +++ b/components/wisp_relay/storage_engine.h @@ -0,0 +1,88 @@ +#ifndef STORAGE_ENGINE_H +#define STORAGE_ENGINE_H + +#include +#include +#include "esp_err.h" +#include "freertos/FreeRTOS.h" +#include "freertos/semphr.h" +#include "freertos/task.h" + +#define STORAGE_MAX_EVENTS 5000 +#define STORAGE_MAX_EVENT_SIZE 8192 +#define STORAGE_INDEX_ENTRIES 5000 +#define STORAGE_PARTITION_LABEL "relay_store" + +typedef enum { + STORAGE_OK = 0, + STORAGE_ERR_NOT_INITIALIZED, + STORAGE_ERR_FULL, + STORAGE_ERR_DUPLICATE, + STORAGE_ERR_NOT_FOUND, + STORAGE_ERR_IO, + STORAGE_ERR_NO_MEM, + STORAGE_ERR_SERIALIZE +} storage_error_t; + +#define STORAGE_FLAG_DELETED 0x01 + +typedef struct __attribute__((packed)) { + uint8_t event_id[32]; + uint32_t created_at; + uint32_t expires_at; + uint32_t file_index; + uint16_t kind; + uint8_t pubkey_prefix[4]; + uint8_t flags; + uint8_t reserved; +} storage_index_entry_t; + +typedef struct { + uint32_t total_events; + uint32_t total_bytes; + uint32_t free_bytes; + uint32_t oldest_event_ts; + uint32_t newest_event_ts; +} storage_stats_t; + +typedef struct storage_engine { + storage_index_entry_t *index; + uint16_t index_count; + uint16_t max_index_entries; + uint32_t next_file_index; + SemaphoreHandle_t lock; + TaskHandle_t cleanup_task; + bool initialized; + bool cleanup_stop; + char mount_point[16]; + uint32_t default_ttl_sec; +} storage_engine_t; + +esp_err_t storage_init(storage_engine_t *engine, uint32_t default_ttl_sec); +void storage_destroy(storage_engine_t *engine); + +storage_error_t storage_save_event_json(storage_engine_t *engine, + const char *event_json, + size_t event_json_len); + +storage_error_t storage_query_events_json(storage_engine_t *engine, + int kind, + const char *author_hex, + int limit, + char ***results, + uint16_t *count); + +void storage_free_query_results(char **results, uint16_t count); + +bool storage_event_exists(storage_engine_t *engine, const uint8_t event_id[32]); + +storage_error_t storage_delete_event(storage_engine_t *engine, const uint8_t event_id[32]); + +int storage_purge_expired(storage_engine_t *engine); +int storage_compact_index(storage_engine_t *engine); + +void storage_get_stats(storage_engine_t *engine, storage_stats_t *stats); + +esp_err_t storage_start_cleanup_task(storage_engine_t *engine); + +#endif diff --git a/components/wisp_relay/sub_manager.c b/components/wisp_relay/sub_manager.c new file mode 100644 index 0000000..a1da2e3 --- /dev/null +++ b/components/wisp_relay/sub_manager.c @@ -0,0 +1,272 @@ +#include "sub_manager.h" +#include "relay_types.h" +#include "esp_log.h" +#include +#include + +static const char *TAG = "sub_mgr"; + +static void filter_clear(sub_filter_t *f) +{ + for (size_t i = 0; i < f->ids_count; i++) free(f->ids[i]); + for (size_t i = 0; i < f->authors_count; i++) free(f->authors[i]); + for (size_t i = 0; i < f->e_tags_count; i++) free(f->e_tags[i]); + for (size_t i = 0; i < f->p_tags_count; i++) free(f->p_tags[i]); + memset(f, 0, sizeof(sub_filter_t)); +} + +static bool filter_copy(sub_filter_t *dst, const sub_filter_t *src) +{ + memset(dst, 0, sizeof(sub_filter_t)); + + size_t ids_count = src->ids_count > SUB_MAX_FILTER_IDS ? SUB_MAX_FILTER_IDS : src->ids_count; + for (size_t i = 0; i < ids_count; i++) { + dst->ids[i] = strdup(src->ids[i]); + if (!dst->ids[i]) goto fail; + } + dst->ids_count = ids_count; + + size_t authors_count = src->authors_count > SUB_MAX_FILTER_AUTHORS ? SUB_MAX_FILTER_AUTHORS : src->authors_count; + for (size_t i = 0; i < authors_count; i++) { + dst->authors[i] = strdup(src->authors[i]); + if (!dst->authors[i]) goto fail; + } + dst->authors_count = authors_count; + + size_t kinds_count = src->kinds_count > SUB_MAX_FILTER_KINDS ? SUB_MAX_FILTER_KINDS : src->kinds_count; + memcpy(dst->kinds, src->kinds, kinds_count * sizeof(int32_t)); + dst->kinds_count = kinds_count; + + size_t e_tags_count = src->e_tags_count > SUB_MAX_FILTER_ETAGS ? SUB_MAX_FILTER_ETAGS : src->e_tags_count; + for (size_t i = 0; i < e_tags_count; i++) { + dst->e_tags[i] = strdup(src->e_tags[i]); + if (!dst->e_tags[i]) goto fail; + } + dst->e_tags_count = e_tags_count; + + size_t p_tags_count = src->p_tags_count > SUB_MAX_FILTER_PTAGS ? SUB_MAX_FILTER_PTAGS : src->p_tags_count; + for (size_t i = 0; i < p_tags_count; i++) { + dst->p_tags[i] = strdup(src->p_tags[i]); + if (!dst->p_tags[i]) goto fail; + } + dst->p_tags_count = p_tags_count; + + dst->since = src->since; + dst->until = src->until; + dst->limit = src->limit; + return true; + +fail: + filter_clear(dst); + return false; +} + +static void clear_subscription(subscription_t *sub) +{ + for (uint8_t i = 0; i < sub->filter_count; i++) { + filter_clear(&sub->filters[i]); + } + memset(sub, 0, sizeof(subscription_t)); +} + +esp_err_t sub_manager_init(sub_manager_t *mgr) +{ + memset(mgr, 0, sizeof(sub_manager_t)); + mgr->lock = xSemaphoreCreateMutex(); + if (!mgr->lock) return ESP_ERR_NO_MEM; + ESP_LOGI(TAG, "Initialized (max=%d, per_conn=%d)", SUB_MAX_TOTAL, SUB_MAX_PER_CONN); + return ESP_OK; +} + +void sub_manager_destroy(sub_manager_t *mgr) +{ + if (!mgr) return; + for (int i = 0; i < SUB_MAX_TOTAL; i++) { + if (mgr->subs[i].active) clear_subscription(&mgr->subs[i]); + } + if (mgr->lock) { vSemaphoreDelete(mgr->lock); mgr->lock = NULL; } +} + +static subscription_t *find_sub(sub_manager_t *mgr, int conn_fd, const char *sub_id) +{ + for (int i = 0; i < SUB_MAX_TOTAL; i++) { + if (mgr->subs[i].active && mgr->subs[i].conn_fd == conn_fd && + strcmp(mgr->subs[i].sub_id, sub_id) == 0) + return &mgr->subs[i]; + } + return NULL; +} + +static subscription_t *find_free_slot(sub_manager_t *mgr) +{ + for (int i = 0; i < SUB_MAX_TOTAL; i++) { + if (!mgr->subs[i].active) return &mgr->subs[i]; + } + return NULL; +} + +static bool hex_prefix_match(const char *prefix, size_t prefix_len, + const char *full, size_t full_len) +{ + if (prefix_len == 0) return true; + if (prefix_len > full_len) return false; + return memcmp(prefix, full, prefix_len) == 0; +} + +static bool filter_matches_event(const sub_filter_t *f, int event_kind, + const char *pubkey_hex, uint64_t created_at) +{ + if (f->kinds_count > 0) { + bool found = false; + for (size_t i = 0; i < f->kinds_count; i++) { + if (f->kinds[i] == event_kind) { found = true; break; } + } + if (!found) return false; + } + + if (f->authors_count > 0) { + bool found = false; + for (size_t i = 0; i < f->authors_count; i++) { + if (hex_prefix_match(f->authors[i], strlen(f->authors[i]), + pubkey_hex, strlen(pubkey_hex))) { + found = true; break; + } + } + if (!found) return false; + } + + if (f->since > 0 && (int64_t)created_at < f->since) return false; + if (f->until > 0 && (int64_t)created_at > f->until) return false; + + return true; +} + +void sub_manager_match_json(sub_manager_t *mgr, const char *event_json, + size_t event_len, int event_kind, + const char *event_pubkey_hex, + uint64_t event_created_at, + sub_match_result_t *result) +{ + result->count = 0; + (void)event_json; + (void)event_len; + + xSemaphoreTake(mgr->lock, portMAX_DELAY); + for (int i = 0; i < SUB_MAX_TOTAL; i++) { + subscription_t *sub = &mgr->subs[i]; + if (!sub->active) continue; + + bool matched = false; + for (uint8_t f = 0; f < sub->filter_count; f++) { + if (filter_matches_event(&sub->filters[f], event_kind, + event_pubkey_hex, event_created_at)) { + matched = true; + break; + } + } + if (matched) { + sub_match_entry_t *entry = &result->matches[result->count++]; + entry->conn_fd = sub->conn_fd; + memcpy(entry->sub_id, sub->sub_id, sizeof(entry->sub_id)); + } + } + xSemaphoreGive(mgr->lock); +} + +sub_error_t sub_manager_add(sub_manager_t *mgr, int conn_fd, + const char *sub_id, + const sub_filter_t *filters, + size_t filter_count) +{ + if (filter_count > SUB_MAX_FILTERS) filter_count = SUB_MAX_FILTERS; + + xSemaphoreTake(mgr->lock, portMAX_DELAY); + + subscription_t *existing = find_sub(mgr, conn_fd, sub_id); + if (existing) { + for (uint8_t i = 0; i < existing->filter_count; i++) + filter_clear(&existing->filters[i]); + existing->events_sent = 0; + for (size_t i = 0; i < filter_count; i++) { + if (!filter_copy(&existing->filters[i], &filters[i])) { + existing->filter_count = (uint8_t)i; + xSemaphoreGive(mgr->lock); + return SUB_ERR_MEMORY; + } + } + existing->filter_count = (uint8_t)filter_count; + xSemaphoreGive(mgr->lock); + return SUB_OK; + } + + uint8_t conn_count = 0; + for (int i = 0; i < SUB_MAX_TOTAL; i++) { + if (mgr->subs[i].active && mgr->subs[i].conn_fd == conn_fd) conn_count++; + } + if (conn_count >= SUB_MAX_PER_CONN) { + xSemaphoreGive(mgr->lock); + return SUB_ERR_TOO_MANY_FILTERS; + } + + subscription_t *slot = find_free_slot(mgr); + if (!slot) { xSemaphoreGive(mgr->lock); return SUB_ERR_MEMORY; } + + memset(slot, 0, sizeof(subscription_t)); + strncpy(slot->sub_id, sub_id, SUB_MAX_ID_LEN); + slot->sub_id[SUB_MAX_ID_LEN] = '\0'; + slot->conn_fd = conn_fd; + + for (size_t i = 0; i < filter_count; i++) { + if (!filter_copy(&slot->filters[i], &filters[i])) { + slot->filter_count = (uint8_t)i; + clear_subscription(slot); + xSemaphoreGive(mgr->lock); + return SUB_ERR_MEMORY; + } + } + slot->filter_count = (uint8_t)filter_count; + slot->active = true; + mgr->active_count++; + + ESP_LOGI(TAG, "Added sub=%s fd=%d filters=%zu total=%d", + sub_id, conn_fd, filter_count, mgr->active_count); + xSemaphoreGive(mgr->lock); + return SUB_OK; +} + +sub_error_t sub_manager_remove(sub_manager_t *mgr, int conn_fd, const char *sub_id) +{ + xSemaphoreTake(mgr->lock, portMAX_DELAY); + subscription_t *sub = find_sub(mgr, conn_fd, sub_id); + if (!sub) { xSemaphoreGive(mgr->lock); return SUB_ERR_NOT_FOUND; } + clear_subscription(sub); + mgr->active_count--; + xSemaphoreGive(mgr->lock); + return SUB_OK; +} + +void sub_manager_remove_all(sub_manager_t *mgr, int conn_fd) +{ + xSemaphoreTake(mgr->lock, portMAX_DELAY); + int removed = 0; + for (int i = 0; i < SUB_MAX_TOTAL; i++) { + if (mgr->subs[i].active && mgr->subs[i].conn_fd == conn_fd) { + clear_subscription(&mgr->subs[i]); + mgr->active_count--; + removed++; + } + } + if (removed > 0) ESP_LOGI(TAG, "Removed %d subs for fd=%d", removed, conn_fd); + xSemaphoreGive(mgr->lock); +} + +uint8_t sub_manager_count(sub_manager_t *mgr, int conn_fd) +{ + uint8_t count = 0; + xSemaphoreTake(mgr->lock, portMAX_DELAY); + for (int i = 0; i < SUB_MAX_TOTAL; i++) { + if (mgr->subs[i].active && mgr->subs[i].conn_fd == conn_fd) count++; + } + xSemaphoreGive(mgr->lock); + return count; +} diff --git a/components/wisp_relay/sub_manager.h b/components/wisp_relay/sub_manager.h new file mode 100644 index 0000000..64afb04 --- /dev/null +++ b/components/wisp_relay/sub_manager.h @@ -0,0 +1,92 @@ +#ifndef SUB_MANAGER_H +#define SUB_MANAGER_H + +#include +#include +#include "esp_err.h" +#include "freertos/FreeRTOS.h" +#include "freertos/semphr.h" +#include "relay_types.h" + +#define SUB_MAX_TOTAL 64 +#define SUB_MAX_PER_CONN 8 +#define SUB_MAX_FILTERS 4 +#define SUB_MAX_ID_LEN 64 + +#define SUB_MAX_FILTER_IDS 20 +#define SUB_MAX_FILTER_AUTHORS 20 +#define SUB_MAX_FILTER_KINDS 20 +#define SUB_MAX_FILTER_ETAGS 20 +#define SUB_MAX_FILTER_PTAGS 20 + +typedef enum { + SUB_OK = 0, + SUB_ERR_INVALID, + SUB_ERR_TOO_MANY_FILTERS, + SUB_ERR_MEMORY, + SUB_ERR_NOT_FOUND, +} sub_error_t; + +typedef struct { + char *ids[SUB_MAX_FILTER_IDS]; + size_t ids_count; + char *authors[SUB_MAX_FILTER_AUTHORS]; + size_t authors_count; + int32_t kinds[SUB_MAX_FILTER_KINDS]; + size_t kinds_count; + char *e_tags[SUB_MAX_FILTER_ETAGS]; + size_t e_tags_count; + char *p_tags[SUB_MAX_FILTER_PTAGS]; + size_t p_tags_count; + int64_t since; + int64_t until; + int limit; +} sub_filter_t; + +typedef struct { + char sub_id[SUB_MAX_ID_LEN + 1]; + int conn_fd; + sub_filter_t filters[SUB_MAX_FILTERS]; + uint8_t filter_count; + uint16_t events_sent; + bool active; +} subscription_t; + +typedef struct sub_manager { + subscription_t subs[SUB_MAX_TOTAL]; + SemaphoreHandle_t lock; + uint16_t active_count; +} sub_manager_t; + +typedef struct { + int conn_fd; + char sub_id[SUB_MAX_ID_LEN + 1]; +} sub_match_entry_t; + +typedef struct { + sub_match_entry_t matches[SUB_MAX_TOTAL]; + uint8_t count; +} sub_match_result_t; + +esp_err_t sub_manager_init(sub_manager_t *mgr); +void sub_manager_destroy(sub_manager_t *mgr); + +sub_error_t sub_manager_add(sub_manager_t *mgr, int conn_fd, + const char *sub_id, + const sub_filter_t *filters, + size_t filter_count); + +sub_error_t sub_manager_remove(sub_manager_t *mgr, int conn_fd, + const char *sub_id); + +void sub_manager_remove_all(sub_manager_t *mgr, int conn_fd); + +void sub_manager_match_json(sub_manager_t *mgr, const char *event_json, + size_t event_len, int event_kind, + const char *event_pubkey_hex, + uint64_t event_created_at, + sub_match_result_t *result); + +uint8_t sub_manager_count(sub_manager_t *mgr, int conn_fd); + +#endif diff --git a/components/wisp_relay/ws_server.c b/components/wisp_relay/ws_server.c new file mode 100644 index 0000000..a973ca6 --- /dev/null +++ b/components/wisp_relay/ws_server.c @@ -0,0 +1,426 @@ +#include "ws_server.h" +#include "nip11_relay.h" +#include "esp_log.h" +#include "esp_timer.h" +#include +#include +#include +#include +#include +#include +#include + +static const char *TAG = "ws_server"; +static ws_message_cb_t g_message_callback = NULL; +static ws_disconnect_cb_t g_disconnect_callback = NULL; +static ws_server_t *g_server = NULL; +static __thread httpd_req_t *g_current_req = NULL; + +static ws_connection_t* find_free_slot(ws_server_t *server) +{ + for (int i = 0; i < WS_MAX_CONNECTIONS; i++) { + if (!server->connections[i].active) { + return &server->connections[i]; + } + } + return NULL; +} + +static ws_connection_t* find_connection_by_fd(ws_server_t *server, int fd) +{ + for (int i = 0; i < WS_MAX_CONNECTIONS; i++) { + if (server->connections[i].active && server->connections[i].fd == fd) { + return &server->connections[i]; + } + } + return NULL; +} + +static void update_connection_activity(ws_server_t *server, int fd) +{ + xSemaphoreTake(server->lock, portMAX_DELAY); + ws_connection_t *conn = find_connection_by_fd(server, fd); + if (conn) { + conn->last_activity = esp_timer_get_time() / 1000000; + } + xSemaphoreGive(server->lock); +} + +static void set_unknown_ip(char *ip_buf, size_t buf_len) +{ + if (buf_len == 0) { + return; + } + strncpy(ip_buf, "unknown", buf_len - 1); + ip_buf[buf_len - 1] = '\0'; +} + +static void get_client_ip(int fd, char *ip_buf, size_t buf_len) +{ + if (buf_len == 0) { + return; + } + + struct sockaddr_storage addr; + socklen_t addr_len = sizeof(addr); + + if (getpeername(fd, (struct sockaddr *)&addr, &addr_len) != 0) { + set_unknown_ip(ip_buf, buf_len); + return; + } + + const char *result = NULL; + if (addr.ss_family == AF_INET) { + struct sockaddr_in *addr_in = (struct sockaddr_in *)&addr; + result = inet_ntop(AF_INET, &addr_in->sin_addr, ip_buf, buf_len); + } + if (!result) { + set_unknown_ip(ip_buf, buf_len); + } +} + +static esp_err_t on_open(httpd_handle_t hd, int sockfd) +{ + if (!g_server) return ESP_FAIL; + + xSemaphoreTake(g_server->lock, portMAX_DELAY); + + if (g_server->connection_count >= WS_MAX_CONNECTIONS) { + xSemaphoreGive(g_server->lock); + ESP_LOGW(TAG, "Connection rejected - max connections reached"); + return ESP_FAIL; + } + + ws_connection_t *conn = find_free_slot(g_server); + if (!conn) { + xSemaphoreGive(g_server->lock); + ESP_LOGE(TAG, "No free slot despite connection_count < WS_MAX_CONNECTIONS (fd=%d)", sockfd); + return ESP_FAIL; + } + + struct linger so_linger = { .l_onoff = 1, .l_linger = 0 }; + setsockopt(sockfd, SOL_SOCKET, SO_LINGER, &so_linger, sizeof(so_linger)); + + int nodelay = 1; + setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, &nodelay, sizeof(nodelay)); + + conn->fd = sockfd; + conn->active = true; + conn->connected_at = esp_timer_get_time() / 1000000; + conn->last_activity = conn->connected_at; + get_client_ip(sockfd, conn->remote_ip, sizeof(conn->remote_ip)); + g_server->connection_count++; + ESP_LOGI(TAG, "New connection from %s (fd=%d, total=%d)", + conn->remote_ip, sockfd, g_server->connection_count); + + xSemaphoreGive(g_server->lock); + return ESP_OK; +} + +static void on_close(httpd_handle_t hd, int sockfd) +{ + if (!g_server) return; + + if (g_disconnect_callback) { + g_disconnect_callback(sockfd); + } + + xSemaphoreTake(g_server->lock, portMAX_DELAY); + + ws_connection_t *conn = find_connection_by_fd(g_server, sockfd); + if (conn) { + ESP_LOGI(TAG, "Connection closed (fd=%d, ip=%s)", sockfd, conn->remote_ip); + memset(conn, 0, sizeof(ws_connection_t)); + g_server->connection_count--; + } + + xSemaphoreGive(g_server->lock); +} + +void ws_server_set_disconnect_cb(ws_disconnect_cb_t cb) +{ + g_disconnect_callback = cb; +} + +static esp_err_t ws_handler(httpd_req_t *req) +{ + if (req->method == HTTP_GET) { + char upgrade[16] = {0}; + if (httpd_req_get_hdr_value_str(req, "Upgrade", upgrade, sizeof(upgrade)) != ESP_OK || + strcasecmp(upgrade, "websocket") != 0) { + return relay_nip11_handler(req); + } + ESP_LOGD(TAG, "WebSocket handshake completed"); + return ESP_OK; + } + + httpd_ws_frame_t ws_pkt; + memset(&ws_pkt, 0, sizeof(httpd_ws_frame_t)); + ws_pkt.type = HTTPD_WS_TYPE_TEXT; + + esp_err_t ret = httpd_ws_recv_frame(req, &ws_pkt, 0); + if (ret != ESP_OK) { + ESP_LOGE(TAG, "Failed to get frame len: %d", ret); + return ret; + } + + if (ws_pkt.len == 0) { + return ESP_OK; + } + + if (ws_pkt.len > WS_MAX_FRAME_SIZE) { + ESP_LOGW(TAG, "Frame too large: %zu bytes", ws_pkt.len); + return ESP_FAIL; + } + + ws_pkt.payload = malloc(ws_pkt.len + 1); + if (!ws_pkt.payload) { + ESP_LOGE(TAG, "Failed to allocate %zu bytes", ws_pkt.len); + return ESP_ERR_NO_MEM; + } + + ret = httpd_ws_recv_frame(req, &ws_pkt, ws_pkt.len); + if (ret != ESP_OK) { + ESP_LOGE(TAG, "Failed to receive frame: %d", ret); + free(ws_pkt.payload); + return ret; + } + + ((char *)ws_pkt.payload)[ws_pkt.len] = '\0'; + + int fd = httpd_req_to_sockfd(req); + if (g_server) { + update_connection_activity(g_server, fd); + } + + switch (ws_pkt.type) { + case HTTPD_WS_TYPE_TEXT: + ESP_LOGD(TAG, "Received %zu bytes from fd=%d", ws_pkt.len, fd); + if (g_message_callback) { + g_current_req = req; + g_message_callback(fd, (char *)ws_pkt.payload, ws_pkt.len); + g_current_req = NULL; + } + break; + + case HTTPD_WS_TYPE_PING: + ws_pkt.type = HTTPD_WS_TYPE_PONG; + ret = httpd_ws_send_frame(req, &ws_pkt); + if (ret != ESP_OK) { + ESP_LOGW(TAG, "Failed to send PONG to fd=%d: %d", fd, ret); + free(ws_pkt.payload); + return ret; + } + break; + + case HTTPD_WS_TYPE_CLOSE: { + ESP_LOGD(TAG, "Received CLOSE frame from fd=%d", fd); + free(ws_pkt.payload); + httpd_ws_frame_t close_pkt = { + .type = HTTPD_WS_TYPE_CLOSE, + .payload = NULL, + .len = 0, + }; + httpd_ws_send_frame(req, &close_pkt); + return ESP_FAIL; + } + + default: + break; + } + + free(ws_pkt.payload); + return ESP_OK; +} + +typedef struct { + httpd_handle_t hd; + int fd; + char *data; + size_t len; +} async_send_arg_t; + +static void ws_async_send(void *arg) +{ + async_send_arg_t *a = (async_send_arg_t *)arg; + + httpd_ws_frame_t ws_pkt = { + .type = HTTPD_WS_TYPE_TEXT, + .payload = (uint8_t *)a->data, + .len = a->len, + }; + + esp_err_t ret = httpd_ws_send_frame_async(a->hd, a->fd, &ws_pkt); + if (ret != ESP_OK) { + ESP_LOGW(TAG, "Async send failed to fd=%d: %d", a->fd, ret); + } + + free(a->data); + free(a); +} + +static void cleanup_server_init(ws_server_t *server, bool stop_httpd) +{ + g_server = NULL; + g_message_callback = NULL; + if (stop_httpd && server->server) { + httpd_stop(server->server); + server->server = NULL; + } + if (server->lock) { + vSemaphoreDelete(server->lock); + server->lock = NULL; + } +} + +esp_err_t ws_server_init(ws_server_t *server, uint16_t port, ws_message_cb_t on_message) +{ + if (server->server != NULL) { + ESP_LOGE(TAG, "Server already initialized, call ws_server_stop first"); + return ESP_ERR_INVALID_STATE; + } + + memset(server, 0, sizeof(ws_server_t)); + server->lock = xSemaphoreCreateMutex(); + if (!server->lock) { + return ESP_ERR_NO_MEM; + } + + g_server = server; + g_message_callback = on_message; + + httpd_config_t config = HTTPD_DEFAULT_CONFIG(); + config.server_port = port; + config.ctrl_port = port + 1; + config.max_open_sockets = WS_MAX_CONNECTIONS; + config.backlog_conn = WS_MAX_CONNECTIONS; + config.lru_purge_enable = true; + config.recv_wait_timeout = 3; + config.send_wait_timeout = 3; + config.keep_alive_enable = true; + config.keep_alive_idle = 5; + config.keep_alive_interval = 1; + config.keep_alive_count = 3; + config.stack_size = 12288; + config.open_fn = on_open; + config.close_fn = on_close; + + esp_err_t ret = httpd_start(&server->server, &config); + if (ret != ESP_OK) { + ESP_LOGE(TAG, "Failed to start server: %d", ret); + cleanup_server_init(server, false); + return ret; + } + + httpd_uri_t ws_uri = { + .uri = "/", + .method = HTTP_GET, + .handler = ws_handler, + .user_ctx = NULL, + .is_websocket = true, + .handle_ws_control_frames = true, + }; + + ret = httpd_register_uri_handler(server->server, &ws_uri); + if (ret != ESP_OK) { + ESP_LOGE(TAG, "Failed to register WS handler: %d", ret); + cleanup_server_init(server, true); + return ret; + } + + httpd_uri_t options_uri = { + .uri = "/", + .method = HTTP_OPTIONS, + .handler = relay_nip11_options_handler, + .user_ctx = NULL, + }; + + ret = httpd_register_uri_handler(server->server, &options_uri); + if (ret != ESP_OK) { + ESP_LOGE(TAG, "Failed to register OPTIONS handler: %d", ret); + } + + ESP_LOGI(TAG, "WebSocket server started on port %d", port); + return ESP_OK; +} + +void ws_server_stop(ws_server_t *server) +{ + g_server = NULL; + g_message_callback = NULL; + g_disconnect_callback = NULL; + + if (server->server) { + httpd_stop(server->server); + server->server = NULL; + } + if (server->lock) { + vSemaphoreDelete(server->lock); + server->lock = NULL; + } + memset(server->connections, 0, sizeof(server->connections)); + server->connection_count = 0; +} + +bool ws_server_is_running(ws_server_t *server) +{ + return server && server->server != NULL; +} + +esp_err_t ws_server_send(ws_server_t *server, int fd, const char *data, size_t len) +{ + if (!server->server) return ESP_ERR_INVALID_STATE; + + if (g_current_req && httpd_req_to_sockfd(g_current_req) == fd) { + httpd_ws_frame_t ws_pkt = { + .type = HTTPD_WS_TYPE_TEXT, + .payload = (uint8_t *)data, + .len = len, + }; + return httpd_ws_send_frame(g_current_req, &ws_pkt); + } + + async_send_arg_t *arg = malloc(sizeof(async_send_arg_t)); + if (!arg) return ESP_ERR_NO_MEM; + + arg->data = malloc(len); + if (!arg->data) { + free(arg); + return ESP_ERR_NO_MEM; + } + + memcpy(arg->data, data, len); + arg->hd = server->server; + arg->fd = fd; + arg->len = len; + + esp_err_t ret = httpd_queue_work(server->server, ws_async_send, arg); + if (ret != ESP_OK) { + free(arg->data); + free(arg); + return ret; + } + return ESP_OK; +} + +esp_err_t ws_server_broadcast(ws_server_t *server, const char *data, size_t len) +{ + xSemaphoreTake(server->lock, portMAX_DELAY); + + for (int i = 0; i < WS_MAX_CONNECTIONS; i++) { + if (server->connections[i].active) { + ws_server_send(server, server->connections[i].fd, data, len); + } + } + + xSemaphoreGive(server->lock); + return ESP_OK; +} + +void ws_server_close_connection(ws_server_t *server, int fd) +{ + if (!server || !server->server) { + return; + } + httpd_sess_trigger_close(server->server, fd); +} diff --git a/components/wisp_relay/ws_server.h b/components/wisp_relay/ws_server.h new file mode 100644 index 0000000..4fe616e --- /dev/null +++ b/components/wisp_relay/ws_server.h @@ -0,0 +1,41 @@ +#ifndef WS_SERVER_H +#define WS_SERVER_H + +#include +#include +#include +#include "esp_http_server.h" +#include "freertos/FreeRTOS.h" +#include "freertos/semphr.h" + +#define WS_MAX_CONNECTIONS 8 +#define WS_MAX_FRAME_SIZE 65536 +#define WS_IP_ADDR_MAX_LEN 48 + +typedef struct { + int fd; + bool active; + uint32_t connected_at; + uint32_t last_activity; + char remote_ip[WS_IP_ADDR_MAX_LEN]; +} ws_connection_t; + +typedef struct { + httpd_handle_t server; + ws_connection_t connections[WS_MAX_CONNECTIONS]; + SemaphoreHandle_t lock; + uint8_t connection_count; +} ws_server_t; + +typedef void (*ws_message_cb_t)(int fd, const char *data, size_t len); +typedef void (*ws_disconnect_cb_t)(int fd); + +esp_err_t ws_server_init(ws_server_t *server, uint16_t port, ws_message_cb_t on_message); +void ws_server_set_disconnect_cb(ws_disconnect_cb_t cb); +void ws_server_stop(ws_server_t *server); +bool ws_server_is_running(ws_server_t *server); +esp_err_t ws_server_send(ws_server_t *server, int fd, const char *data, size_t len); +esp_err_t ws_server_broadcast(ws_server_t *server, const char *data, size_t len); +void ws_server_close_connection(ws_server_t *server, int fd); + +#endif diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index 9b0fb1c..6408e14 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -18,8 +18,11 @@ idf_component_register(SRCS "tollgate_main.c" "cvm_server.c" "display.c" "font.c" + "local_relay.c" + "relay_selector.c" + "sync_manager.c" INCLUDE_DIRS "." REQUIRES esp_wifi esp_event esp_netif nvs_flash esp_http_server lwip json esp_http_client mbedtls esp-tls log spiffs - nucula_lib secp256k1 axs15231b qrcode + nucula_lib secp256k1 axs15231b qrcode wisp_relay PRIV_REQUIRES esp-tls) diff --git a/main/config.c b/main/config.c index 9dd2a1d..b991991 100644 --- a/main/config.c +++ b/main/config.c @@ -35,6 +35,8 @@ esp_err_t tollgate_config_init(void) g_config.payout.mint_count = 0; g_config.cvm_enabled = true; strncpy(g_config.cvm_relays, "wss://relay.primal.net", sizeof(g_config.cvm_relays) - 1); + g_config.nostr_sync_interval_s = 1800; + g_config.nostr_fallback_sync_interval_s = 21600; esp_vfs_spiffs_conf_t conf = { .base_path = "/spiffs", @@ -257,6 +259,28 @@ esp_err_t tollgate_config_init(void) g_config.payout.mint_count = 1; } + cJSON *seed_relays = cJSON_GetObjectItem(root, "nostr_seed_relays"); + if (seed_relays && cJSON_IsArray(seed_relays)) { + int srcount = cJSON_GetArraySize(seed_relays); + if (srcount > TOLLGATE_MAX_SEED_RELAYS) srcount = TOLLGATE_MAX_SEED_RELAYS; + for (int i = 0; i < srcount; i++) { + cJSON *r = cJSON_GetArrayItem(seed_relays, i); + if (r && cJSON_IsString(r)) { + strncpy(g_config.nostr_seed_relays[i], r->valuestring, + sizeof(g_config.nostr_seed_relays[i]) - 1); + g_config.nostr_seed_relay_count++; + } + } + } + + cJSON *sync_interval = cJSON_GetObjectItem(root, "nostr_sync_interval_s"); + if (sync_interval) g_config.nostr_sync_interval_s = sync_interval->valueint; + + cJSON *fallback_interval = cJSON_GetObjectItem(root, "nostr_fallback_sync_interval_s"); + if (fallback_interval) g_config.nostr_fallback_sync_interval_s = fallback_interval->valueint; + + cJSON_Delete(root); + if (g_config.payout.recipient_count == 0) { strncpy(g_config.payout.recipients[0].lightning_address, "TollGate@coinos.io", sizeof(g_config.payout.recipients[0].lightning_address) - 1); @@ -264,14 +288,24 @@ esp_err_t tollgate_config_init(void) g_config.payout.recipient_count = 1; } - cJSON_Delete(root); - if (g_config.nostr_relay_count == 0) { strncpy(g_config.nostr_relays[0], "wss://relay.damus.io", sizeof(g_config.nostr_relays[0]) - 1); strncpy(g_config.nostr_relays[1], "wss://nos.lol", sizeof(g_config.nostr_relays[1]) - 1); g_config.nostr_relay_count = 2; } + if (g_config.nostr_seed_relay_count == 0) { + strncpy(g_config.nostr_seed_relays[0], "wss://relay.orangesync.tech", + sizeof(g_config.nostr_seed_relays[0]) - 1); + strncpy(g_config.nostr_seed_relays[1], "wss://relay.damus.io", + sizeof(g_config.nostr_seed_relays[1]) - 1); + strncpy(g_config.nostr_seed_relays[2], "wss://nos.lol", + sizeof(g_config.nostr_seed_relays[2]) - 1); + strncpy(g_config.nostr_seed_relays[3], "wss://relay.nostr.band", + sizeof(g_config.nostr_seed_relays[3]) - 1); + g_config.nostr_seed_relay_count = 4; + } + ESP_LOGI(TAG, "Config loaded: nsec=%s...%s, %d WiFi networks, price=%d sats/%dms", g_config.nsec, g_config.nsec + 60, g_config.network_count, g_config.price_per_step, g_config.step_size_ms); diff --git a/main/config.h b/main/config.h index fa4d95c..1e580e9 100644 --- a/main/config.h +++ b/main/config.h @@ -13,6 +13,7 @@ #define TOLLGATE_MAX_AP_SSID_LEN 32 #define TOLLGATE_MAX_AP_PASS_LEN 64 #define TOLLGATE_MAX_RELAYS 4 +#define TOLLGATE_MAX_SEED_RELAYS 8 typedef struct { char ssid[32]; @@ -63,6 +64,11 @@ typedef struct { bool cvm_enabled; char cvm_relays[256]; + + char nostr_seed_relays[TOLLGATE_MAX_SEED_RELAYS][128]; + int nostr_seed_relay_count; + int nostr_sync_interval_s; + int nostr_fallback_sync_interval_s; } tollgate_config_t; void tollgate_config_derive_unique(tollgate_config_t *cfg); diff --git a/main/cvm_server.c b/main/cvm_server.c index b93e176..dd04047 100644 --- a/main/cvm_server.c +++ b/main/cvm_server.c @@ -11,7 +11,6 @@ #include "esp_tls.h" #include "esp_crt_bundle.h" #include "esp_random.h" -#include "esp_timer.h" #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include @@ -31,8 +30,6 @@ static void publish_announcements_via_ws(esp_tls_t *tls); #define CVM_WS_BUF_SIZE 8192 #define CVM_MAX_RESPONSE_SIZE 4096 #define CVM_RECONNECT_DELAY_MS 5000 -#define CVM_WS_READ_TIMEOUT_MS 60000 -#define CVM_WS_PING_INTERVAL_S 30 static char *parse_ws_text_frame(const uint8_t *buf, int len) { @@ -151,7 +148,7 @@ static esp_err_t ws_connect(const char *relay_url, esp_tls_t **tls_out) esp_tls_cfg_t tls_cfg = { .crt_bundle_attach = esp_crt_bundle_attach, - .timeout_ms = CVM_WS_READ_TIMEOUT_MS, + .timeout_ms = 15000, }; esp_tls_t *tls = esp_tls_init(); if (!tls) return ESP_ERR_NO_MEM; @@ -326,54 +323,6 @@ static esp_err_t publish_event_to_relay(const char *relay_url, const char *event return ESP_OK; } -static esp_err_t publish_kind_25910_response_ws(esp_tls_t *tls, - const char *content_json, - const char *request_event_id) -{ - const tollgate_identity_t *id = identity_get(); - if (!id || !id->initialized) return ESP_FAIL; - - cJSON *tags = cJSON_CreateArray(); - cJSON *e_tag = cJSON_CreateArray(); - cJSON_AddItemToArray(e_tag, cJSON_CreateString("e")); - cJSON_AddItemToArray(e_tag, cJSON_CreateString(request_event_id)); - cJSON_AddItemToArray(tags, e_tag); - - char *tags_str = cJSON_PrintUnformatted(tags); - cJSON_Delete(tags); - - nostr_event_t event; - nostr_event_init(&event, id->npub_hex, 25910, tags_str, content_json); - nostr_event_sign(&event, id->nsec); - - char *event_json = malloc(8192); - if (!event_json) { - free(tags_str); - return ESP_ERR_NO_MEM; - } - - esp_err_t ret = nostr_event_to_json(&event, event_json, 8192); - free(tags_str); - if (ret != ESP_OK) { - free(event_json); - return ret; - } - - size_t msg_len = 10 + strlen(event_json) + 2; - char *msg = malloc(msg_len); - if (!msg) { - free(event_json); - return ESP_ERR_NO_MEM; - } - snprintf(msg, msg_len, "[\"EVENT\",%s]", event_json); - ESP_LOGD(TAG, "Sending WS response (%d bytes)", (int)strlen(msg)); - int rc = ws_send_text(tls, msg); - ESP_LOGD(TAG, "WS send result: %d", rc); - free(msg); - free(event_json); - return ESP_OK; -} - static esp_err_t publish_kind_25910_response(const char *relay_url, const char *content_json, const char *request_event_id) @@ -417,7 +366,7 @@ static bool is_owner_pubkey(const char *pubkey_hex) return strcmp(id->npub_hex, pubkey_hex) == 0; } -static void handle_mcp_message(esp_tls_t *tls, const char *sender_pubkey, +static void handle_mcp_message(const char *relay_url, const char *sender_pubkey, const char *event_id, const char *content) { cJSON *msg = cJSON_Parse(content); @@ -437,20 +386,14 @@ static void handle_mcp_message(esp_tls_t *tls, const char *sender_pubkey, if (strcmp(m, "initialize") == 0) { ESP_LOGI(TAG, "MCP initialize from %s", sender_pubkey); char *resp = build_initialize_response(id_str, sender_pubkey); - if (tls) { - publish_kind_25910_response_ws(tls, resp, event_id); - } else { - ESP_LOGW(TAG, "No TLS for response"); - } + publish_kind_25910_response(relay_url, resp, event_id); free(resp); } else if (strcmp(m, "notifications/initialized") == 0) { ESP_LOGI(TAG, "Client initialized: %s", sender_pubkey); } else if (strcmp(m, "tools/list") == 0) { ESP_LOGI(TAG, "tools/list from %s", sender_pubkey); char *resp = build_tools_list_response(id_str); - if (tls) { - publish_kind_25910_response_ws(tls, resp, event_id); - } + publish_kind_25910_response(relay_url, resp, event_id); free(resp); } else if (strcmp(m, "tools/call") == 0) { cJSON *params = cJSON_GetObjectItem(msg, "params"); @@ -471,16 +414,12 @@ static void handle_mcp_message(esp_tls_t *tls, const char *sender_pubkey, mcp_response_t mcp_resp = mcp_dispatch(&req); char *resp = build_tool_call_response(id_str, &mcp_resp); - if (tls) { - publish_kind_25910_response_ws(tls, resp, event_id); - } + publish_kind_25910_response(relay_url, resp, event_id); free(resp); } } else if (strcmp(m, "ping") == 0) { char *resp = build_ping_response(id_str); - if (tls) { - publish_kind_25910_response_ws(tls, resp, event_id); - } + publish_kind_25910_response(relay_url, resp, event_id); free(resp); } else { ESP_LOGW(TAG, "Unknown MCP method: %s", m); @@ -494,7 +433,7 @@ static void handle_mcp_message(esp_tls_t *tls, const char *sender_pubkey, cJSON_Delete(msg); } -static void process_relay_message(esp_tls_t *tls, const char *relay_url, const char *msg_str) +static void process_relay_message(const char *relay_url, const char *msg_str) { cJSON *arr = cJSON_Parse(msg_str); if (!arr || !cJSON_IsArray(arr)) { @@ -553,7 +492,7 @@ static void process_relay_message(esp_tls_t *tls, const char *relay_url, const c return; } - handle_mcp_message(tls, pubkey->valuestring, event_id->valuestring, content->valuestring); + handle_mcp_message(relay_url, pubkey->valuestring, event_id->valuestring, content->valuestring); cJSON_Delete(arr); } @@ -566,9 +505,7 @@ static esp_err_t subscribe_to_relay(esp_tls_t *tls, const char *npub) cJSON *kinds = cJSON_CreateArray(); cJSON_AddItemToArray(kinds, cJSON_CreateNumber(25910)); cJSON_AddItemToObject(filter, "kinds", kinds); - cJSON *p_tags = cJSON_CreateArray(); - cJSON_AddItemToArray(p_tags, cJSON_CreateString(npub)); - cJSON_AddItemToObject(filter, "#p", p_tags); + cJSON_AddStringToObject(filter, "#p", npub); cJSON_AddNumberToObject(filter, "limit", 100); cJSON_AddItemToArray(sub, filter); @@ -616,8 +553,6 @@ static void cvm_relay_task(void *arg) return; } - int64_t last_ping_time = 0; - while (g_running) { int rlen = esp_tls_conn_read(tls, buf, CVM_WS_BUF_SIZE - 1); if (rlen < 0) { @@ -632,20 +567,10 @@ static void cvm_relay_task(void *arg) char *text = parse_ws_text_frame(buf, rlen); if (text) { if (strlen(text) > 0) { - process_relay_message(tls, relay_url, text); + process_relay_message(relay_url, text); } free(text); } - } else if ((buf[0] & 0x0F) == 0x09) { - uint8_t pong[2] = {0x8A, 0x00}; - esp_tls_conn_write(tls, pong, 2); - } - - int64_t now = (int64_t)esp_timer_get_time() / 1000000; - if (now - last_ping_time >= CVM_WS_PING_INTERVAL_S) { - uint8_t ping[2] = {0x89, 0x00}; - esp_tls_conn_write(tls, ping, 2); - last_ping_time = now; } } diff --git a/main/display.c b/main/display.c new file mode 100644 index 0000000..2b6cc88 --- /dev/null +++ b/main/display.c @@ -0,0 +1,264 @@ +#include "display.h" +#include "axs15231b.h" +#include "qrcoded.h" +#include "font.h" +#include "esp_log.h" +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include +#include +#include + +static const char *TAG = "display"; + +#define QR_CYCLE_MS 5000 + +static volatile display_state_t s_state = DISPLAY_BOOT; +static char s_ap_ssid[32] = ""; +static char s_portal_url[256] = ""; +static int s_active_clients = 0; +static uint64_t s_wallet_balance = 0; +static bool s_initialized = false; +static int64_t s_last_qr_switch = 0; +static display_qr_mode_t s_qr_mode = DISPLAY_QR_WIFI; + +static int qr_version_from_strlen(int len) { + if (len <= 17) return 1; + if (len <= 32) return 2; + if (len <= 53) return 3; + if (len <= 78) return 4; + if (len <= 106) return 5; + if (len <= 134) return 6; + if (len <= 154) return 7; + if (len <= 192) return 8; + if (len <= 230) return 9; + if (len <= 271) return 10; + return 11; +} + +static int qr_pixel_size(int len) { + if (len <= 53) return 4; + if (len <= 134) return 3; + return 2; +} + +static int escape_wifi_field(const char *src, char *dst, int dst_size) { + int si = 0, di = 0; + while (src[si] && di < dst_size - 2) { + char c = src[si]; + if (c == '\\' || c == ';' || c == ':' || c == ',' || c == '"') { + if (di + 2 >= dst_size) break; + dst[di++] = '\\'; + dst[di++] = c; + } else { + dst[di++] = c; + } + si++; + } + dst[di] = '\0'; + return di; +} + +static void build_wifi_qr_string(char *out, int out_size) { + char escaped_ssid[64]; + escape_wifi_field(s_ap_ssid, escaped_ssid, sizeof(escaped_ssid)); + snprintf(out, out_size, "WIFI:S:%s;T:nopass;;", escaped_ssid); +} + +void display_render_text(int x, int y, const char *text, uint16_t fg, uint16_t bg, int scale) { + int cx = x; + int cy = y; + int screen_w = axs15231b_get_width(); + int screen_h = axs15231b_get_height(); + + while (*text) { + uint8_t ch = (uint8_t)*text; + if (ch >= 128) ch = '?'; + + if (cx + FONT_GLYPH_W * scale > screen_w) { + cx = x; + cy += FONT_GLYPH_H * scale; + } + if (cy + FONT_GLYPH_H * scale > screen_h) break; + + const uint8_t *glyph = font8x8_basic[ch]; + for (int row = 0; row < FONT_GLYPH_H; row++) { + uint8_t bits = glyph[row]; + for (int col = 0; col < FONT_GLYPH_W; col++) { + uint16_t color = (bits & (0x80 >> col)) ? fg : bg; + int px = cx + col * scale; + int py = cy + row * scale; + if (px < screen_w && py < screen_h) { + axs15231b_fill_rect(px, py, scale, scale, color); + } + } + } + cx += FONT_GLYPH_W * scale; + text++; + } +} + +static void render_qr_at(const char *text, int x_off, int y_off, int max_w, int max_h) { + int len = strlen(text); + int version = qr_version_from_strlen(len); + int px = qr_pixel_size(len); + + uint16_t buf_size = qrcode_getBufferSize(version); + uint8_t *qr_buf = (uint8_t *)malloc(buf_size); + if (!qr_buf) { + ESP_LOGE(TAG, "Failed to allocate QR buffer"); + return; + } + + QRCode qr; + if (qrcode_initText(&qr, qr_buf, version, ECC_LOW, text) != 0) { + ESP_LOGE(TAG, "QR generation failed"); + free(qr_buf); + return; + } + + int qr_px_w = qr.size * px; + int qr_px_h = qr.size * px; + int cx = x_off + (max_w - qr_px_w) / 2; + int cy = y_off + (max_h - qr_px_h) / 2; + if (cx < 0) cx = 0; + if (cy < 0) cy = 0; + + for (int y = 0; y < qr.size; y++) { + for (int x = 0; x < qr.size; x++) { + bool mod = qrcode_getModule(&qr, x, y); + uint16_t color = mod ? 0xFFFF : 0x0000; + axs15231b_fill_rect(cx + x * px, cy + y * px, px, px, color); + } + } + + free(qr_buf); +} + +void display_render_qr(const char *text) { + int screen_w = axs15231b_get_width(); + int screen_h = axs15231b_get_height(); + axs15231b_fill_screen(0x0000); + render_qr_at(text, 0, 0, screen_w, screen_h); + axs15231b_flush(); +} + +static void render_boot_screen(void) { + axs15231b_fill_screen(0x0000); + display_render_text(140, 100, "TollGate", 0xF79F, 0x0000, 3); + display_render_text(140, 140, "starting...", 0xB5B6, 0x0000, 2); + axs15231b_flush(); +} + +static void render_ready_screen(void) { + axs15231b_fill_screen(0x0000); + + int screen_w = axs15231b_get_width(); + int screen_h = axs15231b_get_height(); + int text_area_y = screen_h - 55; + + char qr_text[320]; + const char *label; + + if (s_qr_mode == DISPLAY_QR_WIFI) { + build_wifi_qr_string(qr_text, sizeof(qr_text)); + label = "Scan to connect"; + } else { + strncpy(qr_text, s_portal_url, sizeof(qr_text) - 1); + qr_text[sizeof(qr_text) - 1] = '\0'; + label = "Portal URL"; + } + + render_qr_at(qr_text, 0, 0, screen_w, text_area_y - 5); + + display_render_text(10, text_area_y, label, 0xB5B6, 0x0000, 2); + + char line[64]; + snprintf(line, sizeof(line), "SSID: %s", s_ap_ssid); + display_render_text(10, text_area_y + 20, line, 0xB5B6, 0x0000, 2); + + axs15231b_flush(); +} + +static void render_payment_screen(void) { + axs15231b_fill_screen(0x07E0); + display_render_text(140, 100, "Paid!", 0x0000, 0x07E0, 3); + display_render_text(130, 140, "Access granted", 0x0000, 0x07E0, 2); + axs15231b_flush(); +} + +static void render_error_screen(void) { + axs15231b_fill_screen(0xF800); + display_render_text(120, 100, "No upstream", 0xFFFF, 0xF800, 3); + display_render_text(130, 140, "Check config", 0xFFFF, 0xF800, 2); + axs15231b_flush(); +} + +static void display_task(void *pvParameters) { + ESP_LOGI(TAG, "Display task started"); + + while (1) { + display_state_t state = s_state; + + switch (state) { + case DISPLAY_BOOT: + render_boot_screen(); + break; + case DISPLAY_READY: + render_ready_screen(); + break; + case DISPLAY_PAYMENT_RECEIVED: + render_payment_screen(); + vTaskDelay(pdMS_TO_TICKS(2000)); + s_state = DISPLAY_READY; + break; + case DISPLAY_ERROR: + render_error_screen(); + break; + } + + int64_t now = (int64_t)xTaskGetTickCount() * portTICK_PERIOD_MS; + if (state == DISPLAY_READY && (now - s_last_qr_switch) >= QR_CYCLE_MS) { + s_qr_mode = (s_qr_mode == DISPLAY_QR_WIFI) ? DISPLAY_QR_PORTAL : DISPLAY_QR_WIFI; + s_last_qr_switch = now; + } + + vTaskDelay(pdMS_TO_TICKS(1000)); + } +} + +esp_err_t display_init(void) { + if (s_initialized) return ESP_OK; + + esp_err_t ret = axs15231b_init(); + if (ret != ESP_OK) { + ESP_LOGE(TAG, "Display hardware init failed: %s", esp_err_to_name(ret)); + return ret; + } + + s_initialized = true; + s_last_qr_switch = (int64_t)xTaskGetTickCount() * portTICK_PERIOD_MS; + + xTaskCreatePinnedToCore(display_task, "display", 16384, NULL, 2, NULL, 1); + + ESP_LOGI(TAG, "Display initialized"); + return ESP_OK; +} + +void display_set_state(display_state_t state) { + s_state = state; +} + +void display_update(const char *ap_ssid, int active_clients, + uint64_t wallet_balance, const char *portal_url) { + if (ap_ssid) { + strncpy(s_ap_ssid, ap_ssid, sizeof(s_ap_ssid) - 1); + s_ap_ssid[sizeof(s_ap_ssid) - 1] = '\0'; + } + if (portal_url) { + strncpy(s_portal_url, portal_url, sizeof(s_portal_url) - 1); + s_portal_url[sizeof(s_portal_url) - 1] = '\0'; + } + s_active_clients = active_clients; + s_wallet_balance = wallet_balance; +} diff --git a/main/display.h b/main/display.h new file mode 100644 index 0000000..407521b --- /dev/null +++ b/main/display.h @@ -0,0 +1,27 @@ +#ifndef DISPLAY_H +#define DISPLAY_H + +#include "esp_err.h" +#include +#include + +typedef enum { + DISPLAY_BOOT, + DISPLAY_READY, + DISPLAY_PAYMENT_RECEIVED, + DISPLAY_ERROR +} display_state_t; + +typedef enum { + DISPLAY_QR_WIFI, + DISPLAY_QR_PORTAL +} display_qr_mode_t; + +esp_err_t display_init(void); +void display_set_state(display_state_t state); +void display_update(const char *ap_ssid, int active_clients, + uint64_t wallet_balance, const char *portal_url); +void display_render_text(int x, int y, const char *text, uint16_t fg, uint16_t bg, int scale); +void display_render_qr(const char *text); + +#endif diff --git a/main/font.c b/main/font.c new file mode 100644 index 0000000..b23928f --- /dev/null +++ b/main/font.c @@ -0,0 +1,132 @@ +#include "font.h" + +const uint8_t font8x8_basic[128][8] = { + {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, + {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, + {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, + {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, + {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, + {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, + {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, + {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, + {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, + {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, + {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, + {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, + {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, + {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, + {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, + {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, + {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, + {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, + {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, + {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, + {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, + {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, + {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, + {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, + {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, + {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, + {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, + {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, + {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, + {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, + {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, + {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, + {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, + {0x18,0x18,0x18,0x18,0x18,0x00,0x18,0x00}, + {0x66,0x66,0x00,0x00,0x00,0x00,0x00,0x00}, + {0x66,0xFF,0x66,0x66,0xFF,0x66,0x00,0x00}, + {0x18,0x3E,0x58,0x3C,0x1A,0x7C,0x18,0x00}, + {0x62,0x66,0x0C,0x18,0x30,0x66,0x46,0x00}, + {0x3C,0x66,0x3C,0x38,0x67,0x66,0x3F,0x00}, + {0x18,0x18,0x30,0x00,0x00,0x00,0x00,0x00}, + {0x0C,0x18,0x30,0x30,0x30,0x18,0x0C,0x00}, + {0x30,0x18,0x0C,0x0C,0x0C,0x18,0x30,0x00}, + {0x00,0x66,0x3C,0xFF,0x3C,0x66,0x00,0x00}, + {0x00,0x18,0x18,0x7E,0x18,0x18,0x00,0x00}, + {0x00,0x00,0x00,0x00,0x00,0x18,0x18,0x30}, + {0x00,0x00,0x00,0x7E,0x00,0x00,0x00,0x00}, + {0x00,0x00,0x00,0x00,0x00,0x18,0x18,0x00}, + {0x06,0x0C,0x18,0x30,0x60,0xC0,0x80,0x00}, + {0x3C,0x66,0x6E,0x7E,0x76,0x66,0x3C,0x00}, + {0x18,0x38,0x18,0x18,0x18,0x18,0x7E,0x00}, + {0x3C,0x66,0x06,0x1C,0x30,0x60,0x7E,0x00}, + {0x3C,0x66,0x06,0x1C,0x06,0x66,0x3C,0x00}, + {0x1C,0x3C,0x6C,0x6C,0x7E,0x0C,0x0C,0x00}, + {0x7E,0x60,0x7C,0x06,0x06,0x66,0x3C,0x00}, + {0x1C,0x30,0x60,0x7C,0x66,0x66,0x3C,0x00}, + {0x7E,0x06,0x0C,0x18,0x30,0x30,0x30,0x00}, + {0x3C,0x66,0x66,0x3C,0x66,0x66,0x3C,0x00}, + {0x3C,0x66,0x66,0x3E,0x06,0x0C,0x38,0x00}, + {0x00,0x00,0x18,0x18,0x00,0x18,0x18,0x00}, + {0x00,0x00,0x18,0x18,0x00,0x18,0x18,0x30}, + {0x0C,0x18,0x30,0x60,0x30,0x18,0x0C,0x00}, + {0x00,0x00,0x7E,0x00,0x7E,0x00,0x00,0x00}, + {0x30,0x18,0x0C,0x06,0x0C,0x18,0x30,0x00}, + {0x3C,0x66,0x0C,0x18,0x18,0x00,0x18,0x00}, + {0x3C,0x66,0x6E,0x6A,0x6E,0x60,0x3C,0x00}, + {0x3C,0x66,0x66,0x7E,0x66,0x66,0x66,0x00}, + {0x7C,0x66,0x66,0x7C,0x66,0x66,0x7C,0x00}, + {0x3C,0x66,0x60,0x60,0x60,0x66,0x3C,0x00}, + {0x78,0x6C,0x66,0x66,0x66,0x6C,0x78,0x00}, + {0x7E,0x60,0x60,0x7C,0x60,0x60,0x7E,0x00}, + {0x7E,0x60,0x60,0x7C,0x60,0x60,0x60,0x00}, + {0x3C,0x66,0x60,0x6E,0x66,0x66,0x3C,0x00}, + {0x66,0x66,0x66,0x7E,0x66,0x66,0x66,0x00}, + {0x3C,0x18,0x18,0x18,0x18,0x18,0x3C,0x00}, + {0x1E,0x0C,0x0C,0x0C,0x0C,0x6C,0x38,0x00}, + {0x66,0x6C,0x78,0x70,0x78,0x6C,0x66,0x00}, + {0x60,0x60,0x60,0x60,0x60,0x60,0x7E,0x00}, + {0x63,0x77,0x7F,0x6B,0x63,0x63,0x63,0x00}, + {0x66,0x76,0x7E,0x7E,0x6E,0x66,0x66,0x00}, + {0x3C,0x66,0x66,0x66,0x66,0x66,0x3C,0x00}, + {0x7C,0x66,0x66,0x7C,0x60,0x60,0x60,0x00}, + {0x3C,0x66,0x66,0x66,0x6A,0x6C,0x36,0x00}, + {0x7C,0x66,0x66,0x7C,0x6C,0x66,0x66,0x00}, + {0x3C,0x66,0x60,0x3C,0x06,0x66,0x3C,0x00}, + {0x7E,0x18,0x18,0x18,0x18,0x18,0x18,0x00}, + {0x66,0x66,0x66,0x66,0x66,0x66,0x3C,0x00}, + {0x66,0x66,0x66,0x66,0x66,0x3C,0x18,0x00}, + {0x63,0x63,0x63,0x6B,0x7F,0x77,0x63,0x00}, + {0x66,0x66,0x3C,0x18,0x3C,0x66,0x66,0x00}, + {0x66,0x66,0x66,0x3C,0x18,0x18,0x18,0x00}, + {0x7E,0x06,0x0C,0x18,0x30,0x60,0x7E,0x00}, + {0x3C,0x30,0x30,0x30,0x30,0x30,0x3C,0x00}, + {0xC0,0x60,0x30,0x18,0x0C,0x06,0x03,0x00}, + {0x3C,0x0C,0x0C,0x0C,0x0C,0x0C,0x3C,0x00}, + {0x18,0x3C,0x66,0x00,0x00,0x00,0x00,0x00}, + {0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0x00}, + {0x18,0x18,0x0C,0x00,0x00,0x00,0x00,0x00}, + {0x00,0x00,0x3C,0x06,0x3E,0x66,0x3E,0x00}, + {0x60,0x60,0x7C,0x66,0x66,0x66,0x7C,0x00}, + {0x00,0x00,0x3C,0x66,0x60,0x66,0x3C,0x00}, + {0x06,0x06,0x3E,0x66,0x66,0x66,0x3E,0x00}, + {0x00,0x00,0x3C,0x66,0x7E,0x60,0x3C,0x00}, + {0x1C,0x36,0x30,0x7C,0x30,0x30,0x30,0x00}, + {0x00,0x00,0x3E,0x66,0x66,0x3E,0x06,0x3C}, + {0x60,0x60,0x7C,0x66,0x66,0x66,0x66,0x00}, + {0x18,0x00,0x38,0x18,0x18,0x18,0x3C,0x00}, + {0x0C,0x00,0x1C,0x0C,0x0C,0x0C,0x6C,0x38}, + {0x60,0x60,0x66,0x6C,0x78,0x6C,0x66,0x00}, + {0x38,0x18,0x18,0x18,0x18,0x18,0x3C,0x00}, + {0x00,0x00,0xEC,0xFE,0xD6,0xD6,0xD6,0x00}, + {0x00,0x00,0x7C,0x66,0x66,0x66,0x66,0x00}, + {0x00,0x00,0x3C,0x66,0x66,0x66,0x3C,0x00}, + {0x00,0x00,0x7C,0x66,0x66,0x7C,0x60,0x60}, + {0x00,0x00,0x3E,0x66,0x66,0x3E,0x06,0x06}, + {0x00,0x00,0x7C,0x66,0x60,0x60,0x60,0x00}, + {0x00,0x00,0x3E,0x60,0x3C,0x06,0x7C,0x00}, + {0x30,0x30,0x7C,0x30,0x30,0x36,0x1C,0x00}, + {0x00,0x00,0x66,0x66,0x66,0x66,0x3E,0x00}, + {0x00,0x00,0x66,0x66,0x66,0x3C,0x18,0x00}, + {0x00,0x00,0xD6,0xD6,0xD6,0xFE,0x6C,0x00}, + {0x00,0x00,0x66,0x3C,0x18,0x3C,0x66,0x00}, + {0x00,0x00,0x66,0x66,0x66,0x3E,0x06,0x3C}, + {0x00,0x00,0x7E,0x0C,0x18,0x30,0x7E,0x00}, + {0x0C,0x18,0x18,0x70,0x18,0x18,0x0C,0x00}, + {0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x00}, + {0x30,0x18,0x18,0x0E,0x18,0x18,0x30,0x00}, + {0x00,0x00,0x31,0x6B,0x46,0x00,0x00,0x00}, + {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, +}; diff --git a/main/font.h b/main/font.h new file mode 100644 index 0000000..8ef1955 --- /dev/null +++ b/main/font.h @@ -0,0 +1,11 @@ +#ifndef FONT_H +#define FONT_H + +#include + +#define FONT_GLYPH_W 8 +#define FONT_GLYPH_H 8 + +extern const uint8_t font8x8_basic[128][8]; + +#endif diff --git a/main/local_relay.c b/main/local_relay.c new file mode 100644 index 0000000..d7b1ff8 --- /dev/null +++ b/main/local_relay.c @@ -0,0 +1,140 @@ +#include "local_relay.h" +#include "storage_engine.h" +#include "sub_manager.h" +#include "rate_limiter.h" +#include "ws_server.h" +#include "relay_core.h" +#include "router.h" +#include "handlers.h" +#include "broadcaster.h" +#include "flash_monitor.h" +#include "cJSON.h" +#include "esp_log.h" +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include + +static const char *TAG = "local_relay"; + +#define LOCAL_RELAY_PORT 4869 +#define LOCAL_RELAY_TTL_SEC (21 * 24 * 3600) + +static relay_ctx_t s_relay_ctx; +static storage_engine_t s_storage; +static sub_manager_t s_sub_mgr; +static rate_limiter_t s_rate_limiter; +static bool s_initialized = false; + +relay_ctx_t g_relay_ctx; + +static void on_ws_message(int fd, const char *data, size_t len) +{ + router_dispatch(&g_relay_ctx, fd, data, len); +} + +static void on_ws_disconnect(int fd) +{ + if (g_relay_ctx.sub_manager) { + sub_manager_remove_all(g_relay_ctx.sub_manager, fd); + } +} + +esp_err_t local_relay_init(void) +{ + memset(&s_relay_ctx, 0, sizeof(s_relay_ctx)); + memset(&s_storage, 0, sizeof(s_storage)); + memset(&s_sub_mgr, 0, sizeof(s_sub_mgr)); + memset(&s_rate_limiter, 0, sizeof(s_rate_limiter)); + + esp_err_t ret = storage_init(&s_storage, LOCAL_RELAY_TTL_SEC); + if (ret != ESP_OK) { + ESP_LOGE(TAG, "Failed to init storage: %s", esp_err_to_name(ret)); + return ret; + } + + ret = sub_manager_init(&s_sub_mgr); + if (ret != ESP_OK) { + storage_destroy(&s_storage); + return ret; + } + + rate_config_t rl_cfg = { + .events_per_minute = 60, + .reqs_per_minute = 30, + }; + rate_limiter_init(&s_rate_limiter, &rl_cfg); + + s_relay_ctx.storage = &s_storage; + s_relay_ctx.sub_manager = &s_sub_mgr; + s_relay_ctx.rate_limiter = &s_rate_limiter; + s_relay_ctx.config.port = LOCAL_RELAY_PORT; + s_relay_ctx.config.max_event_age_sec = LOCAL_RELAY_TTL_SEC; + s_relay_ctx.config.max_subs_per_conn = 8; + s_relay_ctx.config.max_filters_per_sub = 4; + s_relay_ctx.config.max_future_sec = 600; + + memcpy(&g_relay_ctx, &s_relay_ctx, sizeof(relay_ctx_t)); + + storage_start_cleanup_task(&s_storage); + + s_initialized = true; + ESP_LOGI(TAG, "Local relay initialized (port=%d, TTL=%ds)", LOCAL_RELAY_PORT, LOCAL_RELAY_TTL_SEC); + return ESP_OK; +} + +void local_relay_start(void) +{ + if (!s_initialized) { + ESP_LOGE(TAG, "Not initialized"); + return; + } + + esp_err_t ret = ws_server_init(&s_relay_ctx.ws_server, LOCAL_RELAY_PORT, on_ws_message); + if (ret != ESP_OK) { + ESP_LOGE(TAG, "Failed to start WS server: %s", esp_err_to_name(ret)); + return; + } + + ws_server_set_disconnect_cb(on_ws_disconnect); + memcpy(&g_relay_ctx, &s_relay_ctx, sizeof(relay_ctx_t)); + + ESP_LOGI(TAG, "Local relay listening on port %d", LOCAL_RELAY_PORT); +} + +void local_relay_stop(void) +{ + if (!s_initialized) return; + ws_server_stop(&s_relay_ctx.ws_server); + ESP_LOGI(TAG, "Local relay stopped"); +} + +esp_err_t local_relay_publish(const char *event_json, size_t event_len) +{ + if (!s_initialized || !event_json) return ESP_ERR_INVALID_STATE; + + storage_error_t err = storage_save_event_json(s_relay_ctx.storage, event_json, event_len); + if (err == STORAGE_ERR_DUPLICATE) { + ESP_LOGD(TAG, "Duplicate event, skipping broadcast"); + return ESP_OK; + } + if (err != STORAGE_OK) { + ESP_LOGW(TAG, "Failed to save event: %d", err); + return ESP_FAIL; + } + + cJSON *obj = cJSON_ParseWithLength(event_json, event_len); + if (!obj) return ESP_OK; + + cJSON *pk = cJSON_GetObjectItem(obj, "pubkey"); + cJSON *kind = cJSON_GetObjectItem(obj, "kind"); + cJSON *ca = cJSON_GetObjectItem(obj, "created_at"); + + if (pk && kind && ca) { + broadcaster_fanout_json(&s_relay_ctx, event_json, event_len, + kind->valueint, pk->valuestring, + (uint64_t)ca->valuedouble); + } + cJSON_Delete(obj); + + return ESP_OK; +} diff --git a/main/local_relay.h b/main/local_relay.h new file mode 100644 index 0000000..8ae1653 --- /dev/null +++ b/main/local_relay.h @@ -0,0 +1,13 @@ +#ifndef LOCAL_RELAY_H +#define LOCAL_RELAY_H + +#include "esp_err.h" +#include + +esp_err_t local_relay_init(void); +void local_relay_start(void); +void local_relay_stop(void); + +esp_err_t local_relay_publish(const char *event_json, size_t event_len); + +#endif diff --git a/main/relay_selector.c b/main/relay_selector.c new file mode 100644 index 0000000..7c443fe --- /dev/null +++ b/main/relay_selector.c @@ -0,0 +1,270 @@ +#include "relay_selector.h" +#include "config.h" +#include "esp_log.h" +#include "esp_http_client.h" +#include "esp_tls.h" +#include "esp_crt_bundle.h" +#include "esp_timer.h" +#include "cJSON.h" +#include "freertos/FreeRTOS.h" +#include "freertos/semphr.h" +#include +#include + +static const char *TAG = "relay_sel"; +static const int MAX_REDIRECTS = 3; +static const int PROBE_TIMEOUT_MS = 5000; +static const int MAX_FAILURES = 3; + +static int compare_relays(const void *a, const void *b) +{ + const relay_info_t *ra = (const relay_info_t *)a; + const relay_info_t *rb = (const relay_info_t *)b; + + if (ra->alive && !rb->alive) return -1; + if (!ra->alive && rb->alive) return 1; + + int score_a = (ra->supports_nip77 ? 1000 : 0) - ra->consecutive_failures * 100; + int score_b = (rb->supports_nip77 ? 1000 : 0) - rb->consecutive_failures * 100; + if (score_a != score_b) return score_b - score_a; + + return (int)ra->latency_ms - (int)rb->latency_ms; +} + +esp_err_t relay_selector_init(relay_selector_t *sel) +{ + memset(sel, 0, sizeof(relay_selector_t)); + sel->primary_idx = -1; + sel->fallback_idx = -1; + sel->lock = xSemaphoreCreateMutex(); + if (!sel->lock) return ESP_ERR_NO_MEM; + return ESP_OK; +} + +void relay_selector_destroy(relay_selector_t *sel) +{ + if (sel->lock) { vSemaphoreDelete(sel->lock); sel->lock = NULL; } +} + +static esp_err_t probe_nip11(const char *wss_url, relay_info_t *info) +{ + char http_url[192]; + const char *host_start = wss_url; + if (strncmp(wss_url, "wss://", 6) == 0) host_start = wss_url + 6; + else if (strncmp(wss_url, "ws://", 5) == 0) host_start = wss_url + 5; + + snprintf(http_url, sizeof(http_url), "https://%s/", host_start); + + char response[4096]; + int total_len = 0; + + esp_http_client_config_t http_cfg = { + .url = http_url, + .method = HTTP_METHOD_GET, + .timeout_ms = PROBE_TIMEOUT_MS, + .crt_bundle_attach = esp_crt_bundle_attach, + .max_redirection_count = MAX_REDIRECTS, + .disable_auto_redirect = false, + }; + + esp_http_client_handle_t client = esp_http_client_init(&http_cfg); + if (!client) return ESP_FAIL; + + esp_http_client_set_header(client, "Accept", "application/nostr+json"); + + int64_t start_time = esp_timer_get_time(); + esp_err_t err = esp_http_client_open(client, 0); + if (err != ESP_OK) { + esp_http_client_cleanup(client); + info->alive = false; + return err; + } + + int content_length = esp_http_client_fetch_headers(client); + int status = esp_http_client_get_status_code(client); + + if (status != 200) { + esp_http_client_close(client); + esp_http_client_cleanup(client); + info->alive = (status > 0); + return ESP_FAIL; + } + + int max_read = content_length > 0 ? content_length : (int)sizeof(response) - 1; + if (max_read > (int)sizeof(response) - 1) max_read = (int)sizeof(response) - 1; + + while (total_len < max_read) { + int read_len = esp_http_client_read(client, response + total_len, + max_read - total_len); + if (read_len <= 0) break; + total_len += read_len; + } + response[total_len] = '\0'; + + int64_t end_time = esp_timer_get_time(); + info->latency_ms = (uint32_t)((end_time - start_time) / 1000); + + esp_http_client_close(client); + esp_http_client_cleanup(client); + + info->alive = true; + info->consecutive_failures = 0; + + cJSON *root = cJSON_Parse(response); + if (!root) return ESP_OK; + + cJSON *name = cJSON_GetObjectItem(root, "name"); + if (name && cJSON_IsString(name)) + strncpy(info->name, name->valuestring, sizeof(info->name) - 1); + + cJSON *nips = cJSON_GetObjectItem(root, "supported_nips"); + if (nips && cJSON_IsArray(nips)) { + info->nips_count = cJSON_GetArraySize(nips); + if (info->nips_count > 32) info->nips_count = 32; + info->supports_nip77 = false; + for (size_t i = 0; i < info->nips_count; i++) { + cJSON *nip = cJSON_GetArrayItem(nips, i); + if (nip) { + info->supported_nips[i] = (uint8_t)nip->valueint; + if (nip->valueint == 77) info->supports_nip77 = true; + } + } + } + + cJSON_Delete(root); + return ESP_OK; +} + +static void select_primary_fallback(relay_selector_t *sel) +{ + relay_info_t sorted[RELAY_SELECTOR_MAX_RELAYS]; + size_t sorted_count = 0; + + for (size_t i = 0; i < sel->count; i++) { + if (sel->relays[i].alive) { + sorted[sorted_count++] = sel->relays[i]; + } + } + + if (sorted_count == 0) { + sel->primary_idx = -1; + sel->fallback_idx = -1; + return; + } + + qsort(sorted, sorted_count, sizeof(relay_info_t), compare_relays); + + for (size_t i = 0; i < sel->count; i++) { + if (strcmp(sel->relays[i].url, sorted[0].url) == 0) { + sel->primary_idx = (int)i; + break; + } + } + + if (sorted_count > 1) { + for (size_t i = 0; i < sel->count; i++) { + if (strcmp(sel->relays[i].url, sorted[1].url) == 0) { + sel->fallback_idx = (int)i; + break; + } + } + } else { + sel->fallback_idx = -1; + } + + ESP_LOGI(TAG, "Primary: %s (latency=%lums, NIP-77=%s)", + sel->primary_idx >= 0 ? sel->relays[sel->primary_idx].url : "none", + sel->primary_idx >= 0 ? (unsigned long)sel->relays[sel->primary_idx].latency_ms : 0, + sel->primary_idx >= 0 && sel->relays[sel->primary_idx].supports_nip77 ? "yes" : "no"); +} + +esp_err_t relay_selector_probe_all(relay_selector_t *sel) +{ + xSemaphoreTake(sel->lock, portMAX_DELAY); + + ESP_LOGI(TAG, "Probing %zu relays via NIP-11...", sel->count); + + for (size_t i = 0; i < sel->count; i++) { + ESP_LOGI(TAG, "Probing %s...", sel->relays[i].url); + esp_err_t err = probe_nip11(sel->relays[i].url, &sel->relays[i]); + if (err != ESP_OK) { + sel->relays[i].consecutive_failures++; + ESP_LOGW(TAG, "Probe failed for %s (failures=%d)", + sel->relays[i].url, sel->relays[i].consecutive_failures); + if (sel->relays[i].consecutive_failures >= MAX_FAILURES) { + sel->relays[i].alive = false; + } + } + vTaskDelay(pdMS_TO_TICKS(100)); + } + + select_primary_fallback(sel); + + int64_t now = (int64_t)(xTaskGetTickCount() / configTICK_RATE_HZ); + sel->last_full_probe = (uint32_t)now; + + xSemaphoreGive(sel->lock); + return ESP_OK; +} + +const relay_info_t *relay_selector_get_primary(relay_selector_t *sel) +{ + if (sel->primary_idx < 0 || sel->primary_idx >= (int)sel->count) return NULL; + return &sel->relays[sel->primary_idx]; +} + +const relay_info_t *relay_selector_get_fallback(relay_selector_t *sel, int idx) +{ + if (idx == 0) { + if (sel->fallback_idx < 0) return NULL; + return &sel->relays[sel->fallback_idx]; + } + for (size_t i = 0; i < sel->count; i++) { + if ((int)i != sel->primary_idx && (int)i != sel->fallback_idx) { + if (sel->relays[i].alive) { + if (idx <= 0) return &sel->relays[i]; + idx--; + } + } + } + return NULL; +} + +void relay_selector_report_disconnect(relay_selector_t *sel, const char *url) +{ + xSemaphoreTake(sel->lock, portMAX_DELAY); + for (size_t i = 0; i < sel->count; i++) { + if (strcmp(sel->relays[i].url, url) == 0) { + sel->relays[i].consecutive_failures++; + ESP_LOGW(TAG, "Disconnect reported for %s (failures=%d)", + url, sel->relays[i].consecutive_failures); + if (sel->relays[i].consecutive_failures >= MAX_FAILURES) { + sel->relays[i].alive = false; + ESP_LOGW(TAG, "Relay %s marked dead, triggering re-probe", url); + select_primary_fallback(sel); + } + break; + } + } + xSemaphoreGive(sel->lock); +} + +esp_err_t relay_selector_seed_from_config(relay_selector_t *sel) +{ + const tollgate_config_t *cfg = tollgate_config_get(); + xSemaphoreTake(sel->lock, portMAX_DELAY); + + sel->count = 0; + for (int i = 0; i < cfg->nostr_seed_relay_count && sel->count < RELAY_SELECTOR_MAX_RELAYS; i++) { + if (cfg->nostr_seed_relays[i][0] != '\0') { + strncpy(sel->relays[sel->count].url, cfg->nostr_seed_relays[i], + RELAY_SELECTOR_URL_LEN - 1); + sel->relays[sel->count].alive = true; + sel->count++; + } + } + + xSemaphoreGive(sel->lock); + ESP_LOGI(TAG, "Seeded %zu relays from config", sel->count); + return ESP_OK; +} diff --git a/main/relay_selector.h b/main/relay_selector.h new file mode 100644 index 0000000..4403944 --- /dev/null +++ b/main/relay_selector.h @@ -0,0 +1,46 @@ +#ifndef RELAY_SELECTOR_H +#define RELAY_SELECTOR_H + +#include "esp_err.h" +#include "freertos/FreeRTOS.h" +#include "freertos/semphr.h" +#include +#include + +#define RELAY_SELECTOR_MAX_RELAYS 8 +#define RELAY_SELECTOR_URL_LEN 128 + +typedef struct { + char url[RELAY_SELECTOR_URL_LEN]; + char name[64]; + uint32_t latency_ms; + bool supports_nip77; + bool alive; + int consecutive_failures; + uint32_t last_probe_time; + uint8_t supported_nips[32]; + size_t nips_count; +} relay_info_t; + +typedef struct { + relay_info_t relays[RELAY_SELECTOR_MAX_RELAYS]; + size_t count; + int primary_idx; + int fallback_idx; + uint32_t last_full_probe; + SemaphoreHandle_t lock; +} relay_selector_t; + +esp_err_t relay_selector_init(relay_selector_t *sel); +void relay_selector_destroy(relay_selector_t *sel); + +esp_err_t relay_selector_probe_all(relay_selector_t *sel); + +const relay_info_t *relay_selector_get_primary(relay_selector_t *sel); +const relay_info_t *relay_selector_get_fallback(relay_selector_t *sel, int idx); + +void relay_selector_report_disconnect(relay_selector_t *sel, const char *url); + +esp_err_t relay_selector_seed_from_config(relay_selector_t *sel); + +#endif diff --git a/main/sync_manager.c b/main/sync_manager.c new file mode 100644 index 0000000..1766b2b --- /dev/null +++ b/main/sync_manager.c @@ -0,0 +1,399 @@ +#include "sync_manager.h" +#include "local_relay.h" +#include "storage_engine.h" +#include "relay_core.h" +#include "config.h" +#include "nostr_event.h" +#include "esp_log.h" +#include "esp_tls.h" +#include "esp_crt_bundle.h" +#include "cJSON.h" +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "freertos/timers.h" +#include +#include + +static const char *TAG = "sync_mgr"; + +static const uint8_t WS_FIN_TEXT = 0x81; +static const uint8_t WS_FIN_CLOSE = 0x88; + +static esp_err_t ws_connect(const char *wss_url, esp_tls_t **out_tls) +{ + char host[128] = {0}; + int port = 443; + char path[128] = "/"; + + const char *url_start = wss_url; + if (strncmp(wss_url, "wss://", 6) == 0) url_start = wss_url + 6; + + const char *path_ptr = strchr(url_start, '/'); + if (path_ptr) { + size_t host_len = path_ptr - url_start; + if (host_len >= sizeof(host)) host_len = sizeof(host) - 1; + memcpy(host, url_start, host_len); + host[host_len] = '\0'; + strncpy(path, path_ptr, sizeof(path) - 1); + } else { + strncpy(host, url_start, sizeof(host) - 1); + } + + char *colon = strchr(host, ':'); + if (colon) { *colon = '\0'; port = atoi(colon + 1); } + + esp_tls_cfg_t tls_cfg = { .crt_bundle_attach = esp_crt_bundle_attach }; + esp_tls_t *tls = esp_tls_init(); + if (!tls) return ESP_ERR_NO_MEM; + + int ret = esp_tls_conn_new_sync(host, strlen(host), port, &tls_cfg, tls); + if (ret < 0) { + esp_tls_conn_destroy(tls); + ESP_LOGW(TAG, "TLS connect failed to %s", host); + return ESP_FAIL; + } + + char upgrade[512]; + snprintf(upgrade, sizeof(upgrade), + "GET %s HTTP/1.1\r\n" + "Host: %s\r\n" + "Upgrade: websocket\r\n" + "Connection: Upgrade\r\n" + "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n" + "Sec-WebSocket-Version: 13\r\n" + "\r\n", path, host); + + esp_tls_conn_write(tls, (const unsigned char *)upgrade, strlen(upgrade)); + + char resp[1024]; + int rlen = esp_tls_conn_read(tls, (unsigned char *)resp, sizeof(resp) - 1); + if (rlen <= 0 || !strstr(resp, "101")) { + esp_tls_conn_destroy(tls); + return ESP_FAIL; + } + + *out_tls = tls; + return ESP_OK; +} + +static void ws_send_text(esp_tls_t *tls, const char *data, size_t len) +{ + uint8_t header[10]; + int hlen = 0; + header[0] = WS_FIN_TEXT; + if (len <= 125) { header[1] = (uint8_t)len; hlen = 2; } + else if (len <= 65535) { + header[1] = 126; + header[2] = (uint8_t)((len >> 8) & 0xff); + header[3] = (uint8_t)(len & 0xff); + hlen = 4; + } else { + header[1] = 127; + for (int i = 0; i < 8; i++) + header[2 + i] = (uint8_t)((len >> (56 - i * 8)) & 0xff); + hlen = 10; + } + esp_tls_conn_write(tls, header, hlen); + esp_tls_conn_write(tls, (const unsigned char *)data, len); +} + +static void ws_send_close(esp_tls_t *tls) +{ + uint8_t close_frame[2] = {WS_FIN_CLOSE, 0x00}; + esp_tls_conn_write(tls, close_frame, 2); +} + +static int ws_read_text(esp_tls_t *tls, char *buf, size_t buf_len) +{ + uint8_t header[2]; + int rlen = esp_tls_conn_read(tls, header, 2); + if (rlen < 2) return -1; + + if ((header[0] & 0x0f) == 0x08) return -1; + + int payload_len = header[1] & 0x7f; + if (payload_len == 126) { + uint8_t ext[2]; + esp_tls_conn_read(tls, ext, 2); + payload_len = (ext[0] << 8) | ext[1]; + } else if (payload_len == 127) { + uint8_t ext[8]; + esp_tls_conn_read(tls, ext, 8); + payload_len = 0; + for (int i = 0; i < 8; i++) payload_len = (payload_len << 8) | ext[i]; + } + + int mask_len = (header[1] & 0x80) ? 4 : 0; + uint8_t mask[4] = {0}; + if (mask_len) esp_tls_conn_read(tls, mask, 4); + + if (payload_len > (int)buf_len - 1) payload_len = (int)buf_len - 1; + esp_tls_conn_read(tls, (unsigned char *)buf, payload_len); + for (int i = 0; i < payload_len; i++) buf[i] ^= mask[i % 4]; + buf[payload_len] = '\0'; + return payload_len; +} + +static void get_event_ids_from_storage(char ***ids_out, uint16_t *count_out) +{ + extern relay_ctx_t g_relay_ctx; + if (!g_relay_ctx.storage) { *ids_out = NULL; *count_out = 0; return; } + + char **results = NULL; + uint16_t count = 0; + storage_query_events_json(g_relay_ctx.storage, -1, NULL, 5000, &results, &count); + + char **ids = calloc(count, sizeof(char *)); + uint16_t id_count = 0; + + for (uint16_t i = 0; i < count; i++) { + cJSON *obj = cJSON_Parse(results[i]); + if (!obj) continue; + cJSON *id = cJSON_GetObjectItem(obj, "id"); + if (id && cJSON_IsString(id)) { + ids[id_count++] = strdup(id->valuestring); + } + cJSON_Delete(obj); + } + + storage_free_query_results(results, count); + *ids_out = ids; + *count_out = id_count; +} + +static void free_event_ids(char **ids, uint16_t count) +{ + for (uint16_t i = 0; i < count; i++) free(ids[i]); + free(ids); +} + +esp_err_t sync_manager_init(sync_manager_t *mgr, relay_selector_t *selector) +{ + memset(mgr, 0, sizeof(sync_manager_t)); + mgr->selector = selector; + mgr->lock = xSemaphoreCreateMutex(); + if (!mgr->lock) return ESP_ERR_NO_MEM; + return ESP_OK; +} + +static void sync_task(void *arg); + +void sync_manager_start(sync_manager_t *mgr) +{ + mgr->running = true; + xTaskCreate(sync_task, "sync_mgr", 16384, mgr, 3, NULL); + ESP_LOGI(TAG, "Sync manager started"); +} + +void sync_manager_stop(sync_manager_t *mgr) +{ + mgr->running = false; +} + +esp_err_t sync_manager_do_negentropy_sync(sync_manager_t *mgr) +{ + if (!mgr->selector) return ESP_ERR_INVALID_STATE; + + xSemaphoreTake(mgr->lock, portMAX_DELAY); + mgr->sync_in_progress = true; + xSemaphoreGive(mgr->lock); + + const relay_info_t *primary = relay_selector_get_primary(mgr->selector); + if (!primary || !primary->alive) { + ESP_LOGW(TAG, "No primary relay for negentropy sync"); + xSemaphoreTake(mgr->lock, portMAX_DELAY); + mgr->sync_in_progress = false; + xSemaphoreGive(mgr->lock); + return ESP_ERR_NOT_FOUND; + } + + ESP_LOGI(TAG, "Starting REQ-diff sync with primary: %s", primary->url); + + char **local_ids = NULL; + uint16_t local_count = 0; + get_event_ids_from_storage(&local_ids, &local_count); + + if (local_count == 0) { + ESP_LOGI(TAG, "No local events to sync"); + xSemaphoreTake(mgr->lock, portMAX_DELAY); + mgr->sync_in_progress = false; + xSemaphoreGive(mgr->lock); + return ESP_OK; + } + + esp_tls_t *tls = NULL; + esp_err_t err = ws_connect(primary->url, &tls); + if (err != ESP_OK) { + free_event_ids(local_ids, local_count); + relay_selector_report_disconnect(mgr->selector, primary->url); + xSemaphoreTake(mgr->lock, portMAX_DELAY); + mgr->sync_in_progress = false; + xSemaphoreGive(mgr->lock); + return err; + } + + cJSON *filters = cJSON_CreateObject(); + cJSON *ids_arr = cJSON_CreateArray(); + for (uint16_t i = 0; i < local_count; i++) { + cJSON_AddItemToArray(ids_arr, cJSON_CreateString(local_ids[i])); + } + cJSON_AddItemToObject(filters, "ids", ids_arr); + char *filters_json = cJSON_PrintUnformatted(filters); + cJSON_Delete(filters); + + char sub_msg[256]; + snprintf(sub_msg, sizeof(sub_msg), "[\"REQ\",\"sync_diff\",%s]", filters_json); + free(filters_json); + + ws_send_text(tls, sub_msg, strlen(sub_msg)); + + char resp[8192]; + int resp_len = ws_read_text(tls, resp, sizeof(resp)); + (void)resp_len; + + ws_send_close(tls); + esp_tls_conn_destroy(tls); + + free_event_ids(local_ids, local_count); + + int64_t now = (int64_t)(xTaskGetTickCount() / configTICK_RATE_HZ); + xSemaphoreTake(mgr->lock, portMAX_DELAY); + mgr->last_negentropy_sync = (uint32_t)now; + mgr->sync_in_progress = false; + xSemaphoreGive(mgr->lock); + + ESP_LOGI(TAG, "Negentropy sync completed"); + return ESP_OK; +} + +esp_err_t sync_manager_do_reqdiff_sync(sync_manager_t *mgr) +{ + if (!mgr->selector) return ESP_ERR_INVALID_STATE; + + const relay_info_t *fallback = relay_selector_get_fallback(mgr->selector, 0); + if (!fallback || !fallback->alive) { + ESP_LOGW(TAG, "No fallback relay for REQ-diff sync"); + return ESP_ERR_NOT_FOUND; + } + + ESP_LOGI(TAG, "Starting REQ-diff fallback sync with: %s", fallback->url); + + const tollgate_config_t *cfg = tollgate_config_get(); + + esp_tls_t *tls = NULL; + esp_err_t err = ws_connect(fallback->url, &tls); + if (err != ESP_OK) { + relay_selector_report_disconnect(mgr->selector, fallback->url); + return err; + } + + char sub_msg[512]; + snprintf(sub_msg, sizeof(sub_msg), + "[\"REQ\",\"sync_fallback\",{\"authors\":[\"%s\"],\"limit\":500}]", + cfg->npub); + ws_send_text(tls, sub_msg, strlen(sub_msg)); + + char **local_ids = NULL; + uint16_t local_count = 0; + get_event_ids_from_storage(&local_ids, &local_count); + + char resp[8192]; + int events_received = 0; + int events_stored = 0; + + while (true) { + int rlen = ws_read_text(tls, resp, sizeof(resp)); + if (rlen < 0) break; + + cJSON *arr = cJSON_Parse(resp); + if (!arr) continue; + + cJSON *cmd = cJSON_GetArrayItem(arr, 0); + if (cmd && cJSON_IsString(cmd)) { + if (strcmp(cmd->valuestring, "EVENT") == 0) { + cJSON *event_obj = cJSON_GetArrayItem(arr, 1); + if (event_obj) { + events_received++; + char *event_json = cJSON_PrintUnformatted(event_obj); + cJSON *id_item = cJSON_GetObjectItem(event_obj, "id"); + + bool is_local = false; + if (id_item) { + for (uint16_t i = 0; i < local_count; i++) { + if (strcmp(local_ids[i], id_item->valuestring) == 0) { + is_local = true; + break; + } + } + } + + if (!is_local && event_json) { + local_relay_publish(event_json, strlen(event_json)); + events_stored++; + } + cJSON_free(event_json); + } + } else if (strcmp(cmd->valuestring, "EOSE") == 0) { + cJSON_Delete(arr); + break; + } + } + cJSON_Delete(arr); + } + + ws_send_close(tls); + esp_tls_conn_destroy(tls); + free_event_ids(local_ids, local_count); + + int64_t now = (int64_t)(xTaskGetTickCount() / configTICK_RATE_HZ); + xSemaphoreTake(mgr->lock, portMAX_DELAY); + mgr->last_reqdiff_sync = (uint32_t)now; + xSemaphoreGive(mgr->lock); + + ESP_LOGI(TAG, "REQ-diff sync: received=%d, stored=%d", events_received, events_stored); + return ESP_OK; +} + +static void sync_task(void *arg) +{ + sync_manager_t *mgr = (sync_manager_t *)arg; + + vTaskDelay(pdMS_TO_TICKS(10000)); + + relay_selector_probe_all(mgr->selector); + + sync_manager_do_negentropy_sync(mgr); + + const tollgate_config_t *cfg = tollgate_config_get(); + int negentropy_interval = cfg->nostr_sync_interval_s > 0 ? cfg->nostr_sync_interval_s : 1800; + int reqdiff_interval = cfg->nostr_fallback_sync_interval_s > 0 ? + cfg->nostr_fallback_sync_interval_s : 21600; + int reprobe_interval = 21600; + + int64_t last_negentropy = 0; + int64_t last_reqdiff = 0; + int64_t last_reprobe = xTaskGetTickCount() / configTICK_RATE_HZ; + + while (mgr->running) { + vTaskDelay(pdMS_TO_TICKS(30000)); + + int64_t now = (int64_t)(xTaskGetTickCount() / configTICK_RATE_HZ); + + if ((now - last_reprobe) >= reprobe_interval) { + relay_selector_probe_all(mgr->selector); + last_reprobe = now; + } + + if ((now - last_negentropy) >= negentropy_interval) { + esp_err_t err = sync_manager_do_negentropy_sync(mgr); + if (err == ESP_OK) last_negentropy = now; + } + + if ((now - last_reqdiff) >= reqdiff_interval) { + esp_err_t err = sync_manager_do_reqdiff_sync(mgr); + if (err == ESP_OK) last_reqdiff = now; + } + } + + vTaskDelete(NULL); +} diff --git a/main/sync_manager.h b/main/sync_manager.h new file mode 100644 index 0000000..1ba5a7d --- /dev/null +++ b/main/sync_manager.h @@ -0,0 +1,26 @@ +#ifndef SYNC_MANAGER_H +#define SYNC_MANAGER_H + +#include "esp_err.h" +#include "relay_selector.h" +#include "freertos/FreeRTOS.h" +#include "freertos/semphr.h" +#include + +typedef struct { + relay_selector_t *selector; + bool running; + bool sync_in_progress; + uint32_t last_negentropy_sync; + uint32_t last_reqdiff_sync; + SemaphoreHandle_t lock; +} sync_manager_t; + +esp_err_t sync_manager_init(sync_manager_t *mgr, relay_selector_t *selector); +void sync_manager_start(sync_manager_t *mgr); +void sync_manager_stop(sync_manager_t *mgr); + +esp_err_t sync_manager_do_negentropy_sync(sync_manager_t *mgr); +esp_err_t sync_manager_do_reqdiff_sync(sync_manager_t *mgr); + +#endif diff --git a/main/tollgate_main.c b/main/tollgate_main.c index ad5211a..4741765 100644 --- a/main/tollgate_main.c +++ b/main/tollgate_main.c @@ -24,6 +24,9 @@ #include "lightning_payout.h" #include "cvm_server.h" #include "display.h" +#include "local_relay.h" +#include "relay_selector.h" +#include "sync_manager.h" #define MAX_STA_RETRY 5 static const char *TAG = "tollgate_main"; @@ -38,6 +41,9 @@ static bool s_services_running = false; static SemaphoreHandle_t s_services_mutex = NULL; static char s_ap_ip_str[16] = "10.0.0.1"; +static relay_selector_t s_relay_selector; +static sync_manager_t s_sync_manager; + static void start_services(void); static void stop_services(void); @@ -159,6 +165,12 @@ static void start_services(void) captive_portal_start(cfg->ap_ip_str); tollgate_api_start(); + relay_selector_init(&s_relay_selector); + relay_selector_seed_from_config(&s_relay_selector); + + sync_manager_init(&s_sync_manager, &s_relay_selector); + sync_manager_start(&s_sync_manager); + xTaskCreate(publish_wifistr_task, "wifistr_init", 16384, NULL, 3, NULL); const tollgate_config_t *cfg2 = tollgate_config_get(); @@ -189,6 +201,9 @@ static void stop_services(void) tollgate_api_stop(); dns_server_stop(); cvm_server_stop(); + sync_manager_stop(&s_sync_manager); + local_relay_stop(); + relay_selector_destroy(&s_relay_selector); firewall_revoke_all(); s_services_running = false; if (s_services_mutex) xSemaphoreGive(s_services_mutex); @@ -311,6 +326,9 @@ void app_main(void) ESP_ERROR_CHECK(esp_wifi_start()); + local_relay_init(); + local_relay_start(); + ESP_LOGI(TAG, "WiFi AP+STA started, waiting for connection..."); if (tollgate_config_get_wifi(&(wifi_config_t){0}) != ESP_OK) { diff --git a/main/wifistr.c b/main/wifistr.c index bf03b4d..543aaf6 100644 --- a/main/wifistr.c +++ b/main/wifistr.c @@ -2,6 +2,7 @@ #include "identity.h" #include "nostr_event.h" #include "config.h" +#include "local_relay.h" #include "esp_log.h" #include "esp_tls.h" #include "esp_crt_bundle.h" @@ -216,8 +217,13 @@ esp_err_t wifistr_publish(void) ESP_LOGI(TAG, "Wifistr event: %s", event_json); + esp_err_t local_ret = local_relay_publish(event_json, strlen(event_json)); + if (local_ret == ESP_OK) { + ESP_LOGI(TAG, "Published to local relay"); + } + const tollgate_config_t *cfg = tollgate_config_get(); - esp_err_t last_err = ESP_FAIL; + esp_err_t last_err = local_ret; for (int i = 0; i < cfg->nostr_relay_count; i++) { esp_err_t err = ws_send_to_relay(cfg->nostr_relays[i], event_json); diff --git a/partitions.csv b/partitions.csv index 8998d84..10bda86 100644 --- a/partitions.csv +++ b/partitions.csv @@ -2,4 +2,5 @@ nvs, data, nvs, 0x9000, 0x6000, phy_init, data, phy, 0xf000, 0x1000, factory, app, factory, 0x10000, 0x3F0000, -storage, data, spiffs, 0x410000,0xF0000, +storage, data, spiffs, 0x410000,0xF0000, +relay_store, data, 0x99, 0x500000,0x400000, diff --git a/sdkconfig b/sdkconfig index 53590c2..75f2a12 100644 --- a/sdkconfig +++ b/sdkconfig @@ -897,7 +897,7 @@ CONFIG_HTTPD_MAX_URI_LEN=512 CONFIG_HTTPD_ERR_RESP_NO_DELAY=y CONFIG_HTTPD_PURGE_BUF_LEN=32 # CONFIG_HTTPD_LOG_PURGE_DATA is not set -# CONFIG_HTTPD_WS_SUPPORT is not set +CONFIG_HTTPD_WS_SUPPORT=y # CONFIG_HTTPD_QUEUE_WORK_BLOCKING is not set CONFIG_HTTPD_SERVER_EVENT_POST_TIMEOUT=2000 # end of HTTP Server @@ -1526,7 +1526,7 @@ CONFIG_LWIP_DNS_SUPPORT_MDNS_QUERIES=y # CONFIG_LWIP_IRAM_OPTIMIZATION is not set # CONFIG_LWIP_EXTRA_IRAM_OPTIMIZATION is not set CONFIG_LWIP_TIMERS_ONDEMAND=y -CONFIG_LWIP_MAX_SOCKETS=10 +CONFIG_LWIP_MAX_SOCKETS=20 # CONFIG_LWIP_USE_ONLY_LWIP_SELECT is not set # CONFIG_LWIP_SO_LINGER is not set CONFIG_LWIP_SO_REUSE=y @@ -2101,6 +2101,36 @@ CONFIG_WIFI_PROV_AUTOSTOP_TIMEOUT=30 CONFIG_WIFI_PROV_STA_ALL_CHANNEL_SCAN=y # CONFIG_WIFI_PROV_STA_FAST_SCAN is not set # end of Wi-Fi Provisioning Manager + +# +# LittleFS +# +# CONFIG_LITTLEFS_SDMMC_SUPPORT is not set +CONFIG_LITTLEFS_MAX_PARTITIONS=3 +CONFIG_LITTLEFS_PAGE_SIZE=256 +CONFIG_LITTLEFS_OBJ_NAME_LEN=64 +CONFIG_LITTLEFS_READ_SIZE=128 +CONFIG_LITTLEFS_WRITE_SIZE=128 +CONFIG_LITTLEFS_LOOKAHEAD_SIZE=128 +CONFIG_LITTLEFS_CACHE_SIZE=512 +CONFIG_LITTLEFS_BLOCK_CYCLES=512 +CONFIG_LITTLEFS_USE_MTIME=y +# CONFIG_LITTLEFS_USE_ONLY_HASH is not set +# CONFIG_LITTLEFS_HUMAN_READABLE is not set +CONFIG_LITTLEFS_MTIME_USE_SECONDS=y +# CONFIG_LITTLEFS_MTIME_USE_NONCE is not set +# CONFIG_LITTLEFS_SPIFFS_COMPAT is not set +# CONFIG_LITTLEFS_FLUSH_FILE_EVERY_WRITE is not set +# CONFIG_LITTLEFS_FCNTL_GET_PATH is not set +# CONFIG_LITTLEFS_MULTIVERSION is not set +# CONFIG_LITTLEFS_MALLOC_STRATEGY_DISABLE is not set +CONFIG_LITTLEFS_MALLOC_STRATEGY_DEFAULT=y +# CONFIG_LITTLEFS_MALLOC_STRATEGY_INTERNAL is not set +# CONFIG_LITTLEFS_MALLOC_STRATEGY_SPIRAM is not set +CONFIG_LITTLEFS_ASSERTS=y +# CONFIG_LITTLEFS_MMAP_PARTITION is not set +# CONFIG_LITTLEFS_WDT_RESET is not set +# end of LittleFS # end of Component config # CONFIG_IDF_EXPERIMENTAL_FEATURES is not set diff --git a/sdkconfig.defaults b/sdkconfig.defaults index f13a2e9..e2e1f4e 100644 --- a/sdkconfig.defaults +++ b/sdkconfig.defaults @@ -25,6 +25,10 @@ CONFIG_LOG_DEFAULT_LEVEL_INFO=y # HTTP server CONFIG_HTTPD_MAX_REQ_HDR_LEN=1024 CONFIG_HTTPD_MAX_URI_LEN=512 +CONFIG_HTTPD_WS_SUPPORT=y + +# lwIP - increased for relay WebSocket connections +CONFIG_LWIP_MAX_SOCKETS=20 # Partition table CONFIG_PARTITION_TABLE_CUSTOM=y diff --git a/tests/unit/Makefile b/tests/unit/Makefile index b103eef..7ebc3b2 100644 --- a/tests/unit/Makefile +++ b/tests/unit/Makefile @@ -22,7 +22,7 @@ LDFLAGS := -lmbedcrypto -lcjson -lm SECP256K1_OBJ := secp256k1.o precomputed_ecmult.o precomputed_ecmult_gen.o -TESTS := test_geohash test_identity test_nostr_event test_cashu test_session test_tollgate_client test_lnurl_pay test_lightning_payout test_mcp_handler test_nip04 test_cvm_server test_relay_validator test_relay_selector +TESTS := test_geohash test_identity test_nostr_event test_cashu test_session test_tollgate_client test_lnurl_pay test_lightning_payout test_mcp_handler test_nip04 test_cvm_server .PHONY: all test clean $(TESTS) @@ -81,11 +81,5 @@ test_nip04: test_nip04.c $(REPO_ROOT)/main/nip04.c $(SECP256K1_OBJ) test_cvm_server: test_cvm_server.c $(CC) $(CFLAGS) $< -o $@ $(LDFLAGS) -test_relay_validator: test_relay_validator.c $(REPO_ROOT)/main/nostr_event.c $(REPO_ROOT)/main/identity.c $(REPO_ROOT)/components/wisp_relay/relay_validator.c $(SECP256K1_OBJ) - $(CC) $(CFLAGS) -I $(SECP256K1_PRIV_INC) -I $(REPO_ROOT)/components/wisp_relay $< $(REPO_ROOT)/main/nostr_event.c $(REPO_ROOT)/main/identity.c $(REPO_ROOT)/components/wisp_relay/relay_validator.c $(SECP256K1_OBJ) -o $@ $(LDFLAGS) - -test_relay_selector: test_relay_selector.c - $(CC) $(CFLAGS) $< -o $@ $(LDFLAGS) - clean: rm -f $(TESTS) $(SECP256K1_OBJ) -- cgit v1.2.3