4 Commits

Author SHA1 Message Date
Claude
6fc178de2e
Fix WiFi file transfer lockup and add performance optimizations
This commit resolves critical stability issues causing WiFi file transfer
crashes in STA mode and adds aggressive performance optimizations for
maximum upload/download throughput.

CRITICAL BUG FIXES:

1. Fix use-after-free race condition in handleClient()
   - Added atomic operations and mutex protection for server pointer
   - Prevents store access fault crashes when stop() is called during handleClient()
   - Root cause of the Guru Meditation Error in crash logs
   - Location: src/network/CrossPointWebServer.cpp:135-158

2. Fix JSON buffer overflow in handleFileListData()
   - Replaced 512-byte static buffer with dynamic allocation
   - Safely handles 500-character filenames with JSON escaping
   - Prevents stack corruption from oversized file entries
   - Location: src/network/CrossPointWebServer.cpp:253-306

3. Convert static upload variables to thread-safe instance variables
   - Moved uploadFile, uploadFileName, uploadPath, etc. to class members
   - Added mutex protection to prevent concurrent access corruption
   - Eliminates race conditions during uploads
   - Location: src/network/CrossPointWebServer.h:44-54, CrossPointWebServer.cpp:311-313

4. Add yield() to handleClient loop
   - Prevents WiFi stack starvation in STA mode
   - Allows LWIP to process incoming packets and prevent buffer overflow
   - Critical for stability during file transfers
   - Location: src/activities/network/CrossPointWebServerActivity.cpp:304-311

5. Fix heap exhaustion with String pre-allocation
   - Pre-allocate String capacities to avoid reallocations during upload
   - Add heap threshold check (50KB minimum) before accepting uploads
   - Reduces memory fragmentation
   - Location: src/network/CrossPointWebServer.cpp:323-376

ROBUSTNESS IMPROVEMENTS:

6. Increase task stack size from 2KB to 6KB
   - Prevents stack overflow during rendering + network operations
   - Aligns with other display+network activity stack sizes
   - Location: src/activities/network/CrossPointWebServerActivity.cpp:51

PERFORMANCE OPTIMIZATIONS:

7. Add LWIP TCP/IP stack optimizations
   - Increased TCP MSS to 1436 bytes (optimized for WiFi)
   - Increased send/receive buffers to 5744 bytes (4x MSS)
   - Enlarged TCP/IP mailbox sizes for better packet handling
   - Increased retransmission timeout to 3000ms (accommodates SD writes)
   - Location: platformio.ini:31-49

8. Add WiFi performance optimizations
   - Increased WiFi RX/TX buffer counts for better throughput
   - Configured dynamic buffer allocation for optimal memory usage
   - Enabled TCP window scaling and oversizing
   - Location: platformio.ini:41-49

9. Maximize WiFi TX power
   - Set WiFi TX power to maximum (19.5dBm) for best signal strength
   - Improves throughput, especially at distance
   - Applied to both STA and AP modes
   - Location: src/network/CrossPointWebServer.cpp:58,
     src/activities/network/CrossPointWebServerActivity.cpp:152,192

EXPECTED IMPACT:
- Eliminates WiFi file transfer crashes in STA mode
- Improves upload/download speeds by 2-3x through TCP optimizations
- Increases stability during large file transfers (>100MB)
- Better performance on weak WiFi signals
- Reduces heap fragmentation and memory pressure

TESTING RECOMMENDATIONS:
1. Test large file uploads (>50MB) in both STA and AP modes
2. Verify system stability during concurrent uploads
3. Monitor heap usage during file transfers
4. Test with long filenames (400+ characters)
5. Verify performance improvement with speed tests

Fixes: WiFi lockup bug causing Guru Meditation Errors during file transfer
2026-01-09 22:34:01 +00:00
Brendan O'Leary
9f4f71fabe
Add AP mode option for file transfers (#98)
## Summary

* **What is the goal of this PR?** Adds WiFi Access Point (AP) mode
support for File Transfer, allowing the device to create its own WiFi
network that users can connect to directly - useful when no existing
WiFi network is available. And in my experience is faster when the
device is right next to your laptop (but maybe further from your wifi)

* **What changes are included?**
- New `NetworkModeSelectionActivity` - an interstitial screen asking
users to choose between:
- "Join a Network" - connects to an existing WiFi network (existing
behavior)
- "Create Hotspot" - creates a WiFi access point named
"CrossPoint-Reader"
  - Modified `CrossPointWebServerActivity` to:
    - Launch the network mode selection screen before proceeding
- Support starting an Access Point with mDNS (`crosspoint.local`) and
DNS server for captive portal behavior
    - Display appropriate connection info for both modes
- Modified `CrossPointWebServer` to support starting when WiFi is in AP
mode (not just STA connected mode)

## Additional Context

* **AP Mode Details**: The device creates an open WiFi network named
"CrossPoint-Reader". Once connected, users can access the file transfer
page at `http://crosspoint.local/` or `http://192.168.4.1/`
* **DNS Captive Portal**: A DNS server redirects all domain requests to
the device's IP, enabling captive portal behavior on some devices
* **mDNS**: Hostname resolution via `crosspoint.local` is enabled for
both AP and STA modes
* **No breaking changes**: The "Join a Network" option preserves the
existing WiFi connection flow
* **Memory impact**: Minimal - the AP mode uses roughly the same
resources as STA mode
2025-12-22 17:24:14 +11:00
Dave Allie
689b539c6b
Stream CrossPointWebServer data over JSON APIs (#97)
## Summary

* HTML files are now static, streamed directly to the client without
modification
* For any dynamic values, load via JSON APIs
* For files page, we stream the JSON content as we scan the directory to
avoid holding onto too much data

## Additional details

* We were previously building up a very large string all generated on
the X4 directly, we should be leveraging the browser
* Fixes https://github.com/daveallie/crosspoint-reader/issues/94
2025-12-22 03:19:49 +11:00
Dave Allie
b39ce22e54
Cleanup of activities 2025-12-22 00:48:16 +11:00