From 365c9bc6c2dc3532f64e0e724d24f505fbbe3f87 Mon Sep 17 00:00:00 2001 From: PhoenixZHC-laptop <2081577684@qq.com> Date: Sun, 30 Aug 2026 01:44:08 +0800 Subject: [PATCH] docs(development): add audio and provisioning experience notes --- docs/development/experience-notes.md | 2 + docs/development/experience-notes.zh_CN.md | 2 + docs/experiences/INDEX.md | 5 + docs/experiences/INDEX.zh_CN.md | 5 + .../network-audio-streaming-and-memory.md | 119 +++++++++++++++ ...etwork-audio-streaming-and-memory.zh_CN.md | 97 ++++++++++++ ...softap-provisioning-and-resource-budget.md | 139 ++++++++++++++++++ ...-provisioning-and-resource-budget.zh_CN.md | 114 ++++++++++++++ 8 files changed, 483 insertions(+) create mode 100644 docs/experiences/phoenixzhc/network-audio-streaming-and-memory.md create mode 100644 docs/experiences/phoenixzhc/network-audio-streaming-and-memory.zh_CN.md create mode 100644 docs/experiences/phoenixzhc/softap-provisioning-and-resource-budget.md create mode 100644 docs/experiences/phoenixzhc/softap-provisioning-and-resource-budget.zh_CN.md diff --git a/docs/development/experience-notes.md b/docs/development/experience-notes.md index f4a1866..77c1b6a 100644 --- a/docs/development/experience-notes.md +++ b/docs/development/experience-notes.md @@ -50,3 +50,5 @@ stored entries. The index below lists each entry once it is added. - **Audio Compression Trade-offs on ESP32-C3** (Shinku-Chen) — how a voice-playback codec was chosen on limited flash (IMA-ADPCM vs Opus vs MP3), with measured capacity and decoder cost. See [`../experiences/shinku-chen/audio-compression-trade-offs.md`](../experiences/shinku-chen/audio-compression-trade-offs.md). - **Post-Release Follow-up for the AI Passport Publishing Flow** (Shinku-Chen) — confirm the publish destination, include the data partition in a release, and the consent gates for the post-release tracks. See [`../experiences/shinku-chen/post-release-follow-up.md`](../experiences/shinku-chen/post-release-follow-up.md). - **Display Refresh and Deep-sleep on ESP32-C3 (No PSRAM)** (Shinku-Chen) — direct panel refresh of a single image rect, RTC-GPIO deep-sleep wakeup, and the LVGL object-type misuse crash signature. See [`../experiences/shinku-chen/display-refresh-and-deep-sleep.md`](../experiences/shinku-chen/display-refresh-and-deep-sleep.md). +- **Network Audio Streaming and Memory Budgeting on AI Passport** (PhoenixZHC) — bounded HTTP audio streaming, ES8311/I2S ownership, and joint memory budgeting for decoding, JSON, DMA, and LVGL. See [`../experiences/phoenixzhc/network-audio-streaming-and-memory.md`](../experiences/phoenixzhc/network-audio-streaming-and-memory.md). +- **SoftAP Provisioning and Resource Budgets on AI Passport** (PhoenixZHC) — DHCP state, captive-portal compatibility, bounded forms and uploads, and no-PSRAM resource planning. See [`../experiences/phoenixzhc/softap-provisioning-and-resource-budget.md`](../experiences/phoenixzhc/softap-provisioning-and-resource-budget.md). diff --git a/docs/development/experience-notes.zh_CN.md b/docs/development/experience-notes.zh_CN.md index 4b3f913..c3eba0b 100644 --- a/docs/development/experience-notes.zh_CN.md +++ b/docs/development/experience-notes.zh_CN.md @@ -38,3 +38,5 @@ fork 上自行创建或变更的 `docs/` 文档。每条经验一个 `.md` 及 - **ESP32-C3 上音频压缩方式的权衡**(Shinku-Chen)— 在有限 Flash 上如何为语音播放应用选编解码(IMA-ADPCM vs Opus vs MP3),含实测容量与解码器成本。见 [`../experiences/shinku-chen/audio-compression-trade-offs.zh_CN.md`](../experiences/shinku-chen/audio-compression-trade-offs.zh_CN.md)。 - **发布后收尾:AI Passport 发布流程的衔接**(Shinku-Chen)— 确认发布目的地、发布时包含数据分区、以及发布后收尾各轨道的同意门槛。见 [`../experiences/shinku-chen/post-release-follow-up.zh_CN.md`](../experiences/shinku-chen/post-release-follow-up.zh_CN.md)。 - **ESP32-C3(无 PSRAM)上的显示刷新与深睡**(Shinku-Chen)— 直接刷新单个图片矩形、RTC GPIO 深睡唤醒,以及 LVGL 对象类型误用的崩溃特征。见 [`../experiences/shinku-chen/display-refresh-and-deep-sleep.zh_CN.md`](../experiences/shinku-chen/display-refresh-and-deep-sleep.zh_CN.md)。 +- **AI Passport 网络音频流与内存预算经验**(PhoenixZHC)— 有边界的 HTTP 音频流、ES8311/I2S 资源归属,以及解码、JSON、DMA 与 LVGL 的统一内存预算。见 [`../experiences/phoenixzhc/network-audio-streaming-and-memory.zh_CN.md`](../experiences/phoenixzhc/network-audio-streaming-and-memory.zh_CN.md)。 +- **AI Passport SoftAP 配网与资源预算经验**(PhoenixZHC)— DHCP 状态、弹窗认证兼容、表单与上传边界,以及无 PSRAM 条件下的资源规划。见 [`../experiences/phoenixzhc/softap-provisioning-and-resource-budget.zh_CN.md`](../experiences/phoenixzhc/softap-provisioning-and-resource-budget.zh_CN.md)。 diff --git a/docs/experiences/INDEX.md b/docs/experiences/INDEX.md index dcb8d73..8f22951 100644 --- a/docs/experiences/INDEX.md +++ b/docs/experiences/INDEX.md @@ -25,3 +25,8 @@ rather than merged into an existing one. - [Audio Compression Trade-offs on ESP32-C3](shinku-chen/audio-compression-trade-offs.md) — how a voice-playback codec was chosen on limited flash (IMA-ADPCM vs Opus vs MP3), with measured capacity and decoder cost. - [Post-Release Follow-up for the AI Passport Publishing Flow](shinku-chen/post-release-follow-up.md) — confirm the publish destination, include the data partition in a release, and the consent gates for the post-release tracks. - [Display Refresh and Deep-sleep on ESP32-C3 (No PSRAM)](shinku-chen/display-refresh-and-deep-sleep.md) — direct panel refresh of a single image rect, RTC-GPIO deep-sleep wakeup, and the LVGL object-type misuse crash signature. + +### PhoenixZHC + +- [Network Audio Streaming and Memory Budgeting on AI Passport](phoenixzhc/network-audio-streaming-and-memory.md) — bounded HTTP audio streaming, ES8311/I2S ownership, and joint memory budgeting for decoding, JSON, DMA, and LVGL. +- [SoftAP Provisioning and Resource Budgets on AI Passport](phoenixzhc/softap-provisioning-and-resource-budget.md) — DHCP state, captive-portal compatibility, bounded forms and uploads, and no-PSRAM resource planning. diff --git a/docs/experiences/INDEX.zh_CN.md b/docs/experiences/INDEX.zh_CN.md index 27930ed..0041cbf 100644 --- a/docs/experiences/INDEX.zh_CN.md +++ b/docs/experiences/INDEX.zh_CN.md @@ -19,3 +19,8 @@ - [ESP32-C3 上音频压缩方式的权衡](shinku-chen/audio-compression-trade-offs.zh_CN.md) — 在有限 Flash 上如何为语音播放应用选编解码(IMA-ADPCM vs Opus vs MP3),含实测容量与解码器成本。 - [发布后收尾:AI Passport 发布流程的衔接](shinku-chen/post-release-follow-up.zh_CN.md) — 确认发布目的地、发布时包含数据分区、以及发布后收尾各轨道的同意门槛。 - [ESP32-C3(无 PSRAM)上的显示刷新与深睡](shinku-chen/display-refresh-and-deep-sleep.zh_CN.md) — 直接刷新单个图片矩形、RTC GPIO 深睡唤醒,以及 LVGL 对象类型误用的崩溃特征。 + +### PhoenixZHC + +- [AI Passport 网络音频流与内存预算经验](phoenixzhc/network-audio-streaming-and-memory.zh_CN.md) — 有边界的 HTTP 音频流、ES8311/I2S 资源归属,以及解码、JSON、DMA 与 LVGL 的统一内存预算。 +- [AI Passport SoftAP 配网与资源预算经验](phoenixzhc/softap-provisioning-and-resource-budget.zh_CN.md) — DHCP 状态、弹窗认证兼容、表单与上传边界,以及无 PSRAM 条件下的资源规划。 diff --git a/docs/experiences/phoenixzhc/network-audio-streaming-and-memory.md b/docs/experiences/phoenixzhc/network-audio-streaming-and-memory.md new file mode 100644 index 0000000..4b19924 --- /dev/null +++ b/docs/experiences/phoenixzhc/network-audio-streaming-and-memory.md @@ -0,0 +1,119 @@ +

+ 简体中文 · English +

+ +# Network Audio Streaming and Memory Budgeting on AI Passport + +This note collects reusable lessons from adding HTTP audio streaming to AI +Passport. It is intentionally application-neutral: the focus is the board's +ESP32-C3, ES8311 audio path, display workload, and no-PSRAM memory limit. + +## Start from the board boundary + +AI Passport has 8 MB flash and no PSRAM. The ST7789P3 display, Wi-Fi/TLS, +decoder, HTTP client, JSON parser, and LVGL therefore compete for internal RAM. +The ES8311 and CW2017 also share I2C0, so application code must reuse the BSP +bus instead of creating another driver instance. + +The authoritative pin and bus definitions are in +[`bsp_pins.h`](../../../components/bsp/include/bsp_pins.h): + +| Signal | AI Passport connection | +| --- | --- | +| ES8311 control | I2C0, SDA GPIO10, SCL GPIO7, 7-bit address `0x18` | +| I2S clocks | MCLK GPIO6, BCLK GPIO5, WS GPIO3 | +| I2S data | DOUT GPIO2, DIN GPIO4 | +| Display | ST7789P3, 240 × 320 | + +Use [`bsp_audio`](../../../components/bsp/src/bsp_audio.c) as the owner of the +codec and I2S channels. PCM reads and writes are blocking operations and belong +in worker tasks, never in LVGL or button callbacks. + +## Keep the streaming pipeline bounded + +A stable playback pipeline has explicit ownership at every stage: + +1. A controller validates the request and starts one playback worker. +2. The worker opens the HTTP stream and accepts both fixed and unknown content + lengths. +3. A bounded input buffer feeds the decoder incrementally; the complete file is + never accumulated in RAM. +4. Decoded PCM is converted to the format opened by the BSP and written to I2S. +5. One cleanup path closes HTTP, decoder, audio, and UI state on success, + cancellation, timeout, and decode failure. + +For MP3 with a Helix-style decoder, one measured starting point was a 24 KiB +compressed-input buffer, a 2304-sample PCM buffer, and an 8 KiB playback-task +stack. These are measurements from one firmware, not board defaults. Record +minimum free heap, largest free block, and task stack high-water marks, then +shrink or grow the buffers from evidence. + +Do not assume every decoded frame matches the target output format. Open the +codec using the source sample rate, convert stereo to mono when the application +needs mono, and calculate progress from decoded PCM samples. Byte-based progress +is misleading for variable-bit-rate streams and unavailable for chunked HTTP. + +## Treat memory as one system budget + +The common failure is not simply “the audio buffer is too small.” A firmware may +run out of one large contiguous block while total free heap still looks healthy. +Before each expensive phase, log both free heap and largest free block. + +Budget these consumers together: + +- TLS and HTTP receive buffers; +- decoder input, PCM output, and decoder state; +- I2S DMA descriptors and DMA buffers; +- LVGL draw buffers, image decoders, and screen objects; +- JSON documents and temporary response strings; +- worker-task stacks. + +On the upstream BSP, LVGL uses a 20-row draw buffer, about 9.6 KiB at RGB565. +That is a useful baseline because enlarging the display buffer or I2S DMA ring +directly reduces headroom for networking and decoding. Change one buffer family +at a time and repeat playback while the UI is actively refreshing. + +## Size JSON by the response, not the request + +One real crash pattern came from parsing a response with a fixed 4096-byte JSON +document while the actual payload required about 6971 bytes. The request itself +was small, so request-size testing did not reveal the problem. + +Safer rules are: + +- reject an oversized HTTP response before parsing when its length is known; +- when the length is unknown, accumulate only up to an explicit cap; +- use a parser capacity derived from the accepted response limit; +- check deserialization errors and stop before reading missing fields; +- keep large metadata responses out of the playback worker when possible. + +An optional relay service can normalize large or unstable upstream APIs into a +small device contract, but it should not hide firmware limits. Keep device-side +caps, timeouts, and error handling even when a relay is used. + +## Concurrency and UI rules + +Allow only one owner of the audio device. A new request should either be rejected +or cancel and join the existing worker before another worker opens I2S. Button +callbacks should post commands; they must not perform DNS, HTTP, decode, or PCM +writes. + +Reuse the playback screen instead of recreating a full object tree on every +track. Update progress at a modest rate and modify only the changed labels or +bar. This reduces heap fragmentation and prevents display work from starving +the decoder. + +## Verification checklist + +- Play fixed-length and chunked streams, including a stream without + `Content-Length`. +- Exercise mono/stereo, supported sample rates, malformed frames, slow network, + server disconnect, cancellation, and repeated track changes. +- Refresh the display during playback and verify that audio does not underrun. +- Log free heap, largest free block, and stack high-water marks before connect, + after TLS, after decoder creation, during steady playback, and after cleanup. +- Repeat start/stop cycles and confirm memory returns to a stable baseline. + +The reusable lesson is to make every buffer and owner explicit. On AI Passport, +streaming is reliable when network, decoder, I2S, display, and JSON memory are +designed as one bounded pipeline rather than tuned independently. diff --git a/docs/experiences/phoenixzhc/network-audio-streaming-and-memory.zh_CN.md b/docs/experiences/phoenixzhc/network-audio-streaming-and-memory.zh_CN.md new file mode 100644 index 0000000..6fcce4d --- /dev/null +++ b/docs/experiences/phoenixzhc/network-audio-streaming-and-memory.zh_CN.md @@ -0,0 +1,97 @@ +

+ 简体中文 · English +

+ +# AI Passport 网络音频流与内存预算经验 + +本文总结在 AI Passport 上实现 HTTP 音频流时可复用的经验。内容不绑定具体应用,重点是 +ESP32-C3、ES8311 音频链路、显示刷新和无 PSRAM 条件下的内存限制。 + +## 先确认硬件边界 + +AI Passport 使用 8 MB Flash,没有 PSRAM。ST7789P3 显示、Wi-Fi/TLS、解码器、 +HTTP 客户端、JSON 解析器和 LVGL 都会争用内部 RAM。ES8311 与 CW2017 还共用 +I2C0,因此应用必须复用 BSP 的总线,不能再创建一套同端口驱动。 + +引脚和总线定义以 [`bsp_pins.h`](../../../components/bsp/include/bsp_pins.h) 为准: + +| 信号 | AI Passport 连接 | +| --- | --- | +| ES8311 控制 | I2C0,SDA GPIO10,SCL GPIO7,7 位地址 `0x18` | +| I2S 时钟 | MCLK GPIO6,BCLK GPIO5,WS GPIO3 | +| I2S 数据 | DOUT GPIO2,DIN GPIO4 | +| 显示屏 | ST7789P3,240 × 320 | + +让 [`bsp_audio`](../../../components/bsp/src/bsp_audio.c) 统一持有 codec 和 I2S +通道。PCM 读写是阻塞操作,必须放到工作任务中,不能放进 LVGL 或按键回调。 + +## 把流式链路限制在明确边界内 + +稳定的播放链路需要让每一层的资源归属都清楚: + +1. 控制器校验请求,并只启动一个播放工作任务。 +2. 工作任务打开 HTTP 流,同时支持固定长度和未知长度响应。 +3. 有上限的输入缓冲增量喂给解码器,绝不把完整文件堆进 RAM。 +4. 解码后的 PCM 转成 BSP 已打开的格式,再写入 I2S。 +5. 成功、取消、超时和解码失败都走同一条清理路径,关闭 HTTP、解码器、音频和 UI 状态。 + +使用 Helix 类 MP3 解码器时,一份固件的实测起点是:24 KiB 压缩输入缓冲、 +2304 个采样的 PCM 缓冲和 8 KiB 播放任务栈。这些是实测参考,不是硬件默认值。 +应记录最小剩余堆、最大连续空闲块和任务栈高水位,再依据数据调整。 + +不要假设所有音频帧都等于目标输出格式。应按音源采样率打开 codec;应用需要单声道时, +明确执行双声道转单声道;播放进度按已解码 PCM 采样数计算。对于可变码率或分块传输, +按网络字节数算进度并不可靠。 + +## 把内存当成一份总预算 + +常见故障不只是“音频缓冲太小”。即使总空闲堆看起来不少,也可能已经没有足够大的连续内存块。 +每个高开销阶段前后,都应同时记录空闲堆和最大连续空闲块。 + +以下资源必须一起核算: + +- TLS 和 HTTP 接收缓冲; +- 解码输入、PCM 输出和解码器状态; +- I2S DMA 描述符和 DMA 缓冲; +- LVGL 绘制缓冲、图片解码器和界面对象; +- JSON 文档和临时响应字符串; +- 工作任务栈。 + +上游 BSP 的 LVGL 使用 20 行绘制缓冲,RGB565 下约 9.6 KiB。这是一个有用基线: +放大显示缓冲或 I2S DMA 环都会直接压缩网络和解码空间。一次只改一类缓冲,并在界面持续刷新时 +重复播放测试。 + +## JSON 容量要按响应计算 + +一个真实死机问题是:固定 4096 字节的 JSON 文档去解析实际约需 6971 字节的响应。 +请求本身很小,因此只测试请求大小发现不了问题。 + +更稳妥的规则是: + +- 已知响应长度时,解析前拒绝超限数据; +- 长度未知时,只允许累计到明确上限; +- JSON 解析容量按允许的最大响应推导; +- 检查反序列化错误,失败后不能继续读取字段; +- 大型元数据响应尽量不要放在播放工作任务中处理。 + +可选的中转服务可以把庞大或不稳定的上游接口收敛成小型设备协议,但不能用它掩盖固件限制。 +即使使用中转,设备端仍要保留大小上限、超时和错误处理。 + +## 并发和界面规则 + +音频设备只能有一个持有者。新请求要么被拒绝,要么先取消并等待旧任务退出,之后才能再次打开 +I2S。按键回调只负责投递命令,不能在回调中执行 DNS、HTTP、解码或 PCM 写入。 + +播放界面应复用,不要每次切换都重建整棵对象树。降低进度刷新频率,只更新变化的文字和进度条, +可以减少堆碎片,也能避免显示任务挤占解码时间。 + +## 验证清单 + +- 测试固定长度、分块传输和没有 `Content-Length` 的音频流。 +- 覆盖单双声道、支持的采样率、损坏帧、慢网络、服务端断开、取消和连续切换。 +- 播放时持续刷新界面,确认音频不欠载。 +- 在连接前、TLS 建立后、解码器创建后、稳定播放时和清理后,记录空闲堆、最大连续块和栈高水位。 +- 重复启停,确认内存回到稳定基线。 + +最可复用的结论是:明确每个缓冲的上限和每项资源的唯一持有者。在 AI Passport 上,只有把 +网络、解码、I2S、显示和 JSON 当成一条受限流水线统一设计,流式播放才会稳定。 diff --git a/docs/experiences/phoenixzhc/softap-provisioning-and-resource-budget.md b/docs/experiences/phoenixzhc/softap-provisioning-and-resource-budget.md new file mode 100644 index 0000000..18d65cd --- /dev/null +++ b/docs/experiences/phoenixzhc/softap-provisioning-and-resource-budget.md @@ -0,0 +1,139 @@ +

+ 简体中文 · English +

+ +# SoftAP Provisioning and Resource Budgets on AI Passport + +This note summarizes reusable experience from implementing SoftAP provisioning +and a local configuration page on AI Passport. The difficult part is not making +an access point appear; it is keeping DHCP, captive-portal traffic, HTTP forms, +uploads, and the rest of the firmware inside the ESP32-C3's no-PSRAM budget. + +## Choose the operating model first + +Two products that both use SoftAP can need very different designs: + +| Model | Recommended behavior | +| --- | --- | +| Short provisioning session | Use APSTA, collect credentials, verify the STA connection, persist only after success, then stop the AP and web service. | +| Long-lived local management | Prefer AP-only unless upstream connectivity is required; expose a stable local URL and budget for repeated reconnects and browser probes. | + +Keeping APSTA active permanently costs more heap and sockets and creates more +radio work. Do not keep it merely because it was convenient during development. + +## Release optional subsystems before starting Wi-Fi + +On one AI Passport-derived firmware, the largest free block before SoftAP was +about 13 KiB. Deferring audio initialization and releasing unused codec/I2S +resources raised the available contiguous block to about 31 KiB. The exact +numbers are firmware-specific, but the lesson is general: lazy initialization is +often more valuable than shaving a few bytes from every network buffer. + +Before provisioning, stop or defer resources that are not needed on that page: +audio tasks, decoder state, large images, temporary JSON documents, and duplicate +screens. Log free heap and largest free block before Wi-Fi start, after AP start, +after HTTP start, and after a phone joins. + +## DHCP and connection state + +Create the default SoftAP network interface and let ESP-IDF own its DHCP server +unless the product needs a custom address plan. A conservative measured starting +point for a one-user configuration page is `max_connection = 1`; increase it +only after measuring heap and socket use. + +Do not confuse Wi-Fi association with DHCP completion: + +- `WIFI_EVENT_AP_STACONNECTED` means the station joined the access point. +- `IP_EVENT_AP_STAIPASSIGNED` means the DHCP server assigned an address. + +Use the second event when the UI needs to report that the client is actually +ready to open the page. Log both events, the assigned address, disconnect reason, +and current heap. This separates authentication problems from DHCP and HTTP +problems. + +## Captive portals are a compatibility feature + +A captive portal usually combines wildcard DNS with HTTP redirects and optional +DHCP captive-portal metadata. Different Android, iOS, and desktop versions send +different probe URLs, so a portal that works on one phone is not yet verified. + +For a short provisioning flow: + +1. Answer DNS queries with the AP address. +2. Serve known connectivity-check paths or redirect them to the local page. +3. Consider DHCP Option 114 where supported, but verify its presence on packets + and real devices before documenting it as deployed. +4. Stop DNS and HTTP cleanly when provisioning completes. + +For a long-lived management AP, automatic pop-up behavior can become annoying. +A printed local URL or QR code plus limited redirects may be a better product +choice. Wildcard DNS also creates background traffic; parse bounds, rate limits, +and socket cleanup still apply. + +## Bound every HTTP input + +Small form handlers can overflow just as easily as file uploads. Before receiving +a form: + +- reject `Content-Length` above the endpoint limit; +- reserve space for the terminating byte; +- loop until the declared body is received or a timeout occurs; +- treat zero, timeout, and disconnect as explicit failures; +- URL-decode only after the complete bounded body is available; +- validate decoded field lengths before storing them. + +Never write Wi-Fi credentials to persistent storage until the station connection +has been tested. Keep the old working credentials until the new pair succeeds, +so a typo does not strand the device. + +For larger files, receive fixed-size chunks instead of allocating the whole body. +One measured implementation used 1024-byte chunks. Write to a temporary target, +validate size and format, synchronize concurrent access, then replace the active +asset only after success. Delete or invalidate the temporary target on every +failure path. + +## Keep the web server deliberately small + +One measured single-client configuration service used three HTTP sockets, a +backlog of two, two receive retries, ten send retries, LRU purge, a 6144-byte +server task stack, and 2880-byte TCP send/receive windows. These values are a +starting reference, not universal defaults. + +Reduce web assets before enlarging transport buffers: + +- gzip static pages at build time; +- send large JSON or file listings in chunks; +- keep thumbnails small and limit how many are decoded at once; +- close failed requests immediately and release their context; +- avoid multiple simultaneous fetches from the page. + +A browser may open several connections even for one visible page. Count sockets +used by DNS, HTTP, STA verification, telemetry, and any remaining audio service +together. + +## Failure signatures + +| Symptom | Check first | +| --- | --- | +| Phone joins but never opens the page | DHCP assignment event, DNS replies, probe URL handling, HTTP socket availability | +| Random reset when a form is submitted | declared length, terminator space, receive loop, task stack, largest free block | +| Upload stops partway | receive timeout, chunk write result, storage space, concurrent reader/writer | +| Works once, then fails after retry | leaked socket, DNS task, event handler, netif, or temporary buffer | +| AP start fails only after other features run | largest free block and optional subsystems still initialized | + +## Verification checklist + +- Test Android, iOS, Windows, and at least one client that does not show a portal + automatically. +- Test wrong credentials, unavailable router, DHCP timeout, AP disconnect, HTTP + timeout, oversized form, interrupted upload, and repeated retries. +- Verify DHCP Option 114 with packet capture or equivalent device evidence when + it is part of the claimed behavior. +- Run repeated enter/exit cycles and confirm tasks, handlers, sockets, and netifs + return to a stable count. +- Record free heap, largest free block, and stack high-water marks at each phase. + +The main lesson is that provisioning is a temporary network product inside the +firmware. Give it explicit start/stop ownership and strict input limits, and test +DHCP and captive-portal behavior separately instead of treating “connected to +Wi-Fi” as proof that the whole flow works. diff --git a/docs/experiences/phoenixzhc/softap-provisioning-and-resource-budget.zh_CN.md b/docs/experiences/phoenixzhc/softap-provisioning-and-resource-budget.zh_CN.md new file mode 100644 index 0000000..0cdda2e --- /dev/null +++ b/docs/experiences/phoenixzhc/softap-provisioning-and-resource-budget.zh_CN.md @@ -0,0 +1,114 @@ +

+ 简体中文 · English +

+ +# AI Passport SoftAP 配网与资源预算经验 + +本文总结在 AI Passport 上实现 SoftAP 配网和本地配置页时可复用的经验。难点不是让热点出现, +而是让 DHCP、弹窗认证流量、HTTP 表单、文件上传和其他固件功能共同适应 ESP32-C3 无 +PSRAM 的资源限制。 + +## 先确定工作模式 + +同样使用 SoftAP,两类产品需要完全不同的设计: + +| 模式 | 推荐行为 | +| --- | --- | +| 短时配网 | 使用 APSTA,接收凭据,验证 STA 连接,成功后再保存,最后停止 AP 和网页服务。 | +| 长期开启的本地管理 | 不需要上游网络时优先使用 AP-only;提供稳定的本地地址,并为反复重连和浏览器探测预留资源。 | + +长期保持 APSTA 会占用更多堆、socket 和无线调度时间。不能只因为开发阶段方便就一直保留。 + +## 启动 Wi-Fi 前释放可选子系统 + +一份基于 AI Passport 的固件中,启动 SoftAP 前最大连续空闲块约为 13 KiB。改为延迟初始化 +音频,并释放不用的 codec/I2S 资源后,最大连续块提高到约 31 KiB。具体数值只代表这份固件, +但结论可复用:延迟初始化通常比从每个网络缓冲里零散节省几个字节更有效。 + +进入配网页前,应停止或延迟不需要的资源:音频任务、解码状态、大图片、临时 JSON 文档和重复 +界面。分别在 Wi-Fi 启动前、AP 启动后、HTTP 启动后和手机接入后记录空闲堆与最大连续空闲块。 + +## DHCP 与连接状态 + +创建 ESP-IDF 默认 SoftAP 网络接口;除非产品需要自定义地址规划,否则让框架管理 DHCP 服务。 +只允许一个用户配网时,`max_connection = 1` 是一份保守的实测起点。提高连接数前,应重新测量 +堆和 socket 占用。 + +不要把 Wi-Fi 关联成功误当成 DHCP 完成: + +- `WIFI_EVENT_AP_STACONNECTED` 表示终端已加入热点。 +- `IP_EVENT_AP_STAIPASSIGNED` 表示 DHCP 已分配地址。 + +如果界面要显示“客户端已经可以打开网页”,应以第二个事件为依据。同时记录这两个事件、分配的 +地址、断开原因和当前堆,才能区分认证、DHCP 和 HTTP 问题。 + +## 弹窗认证是一项兼容性功能 + +弹窗认证通常由通配 DNS、HTTP 重定向和可选的 DHCP 门户信息组合实现。不同 Android、iOS +和桌面系统会访问不同探测地址,因此在一台手机上成功不等于功能已经验证完整。 + +短时配网可采用以下流程: + +1. DNS 查询统一返回 AP 地址。 +2. 对已知联网探测路径直接响应或重定向到本地页面。 +3. 平台支持时可考虑 DHCP Option 114,但必须通过抓包和真机确认后,才能宣称已经部署。 +4. 配网完成后完整停止 DNS 和 HTTP 服务。 + +对于长期管理热点,自动弹窗可能反而打扰用户。印在界面上的本地地址或二维码,加少量重定向, +可能更合适。通配 DNS 也会产生后台流量,仍需限制解析长度、请求频率并正确释放 socket。 + +## 限制每一份 HTTP 输入 + +小表单和文件上传一样可能溢出。接收表单前应做到: + +- `Content-Length` 超过接口上限时立即拒绝; +- 为字符串结尾的零字节预留空间; +- 循环接收,直到达到声明长度或超时; +- 把返回零、超时和断开都作为明确失败; +- 完整收到受限长度的正文后再做 URL 解码; +- 写入存储前再次校验解码后的字段长度。 + +在 STA 连接验证成功前,不要覆盖持久化的 Wi-Fi 凭据。保留上一组可用凭据,只有新凭据验证 +成功后才替换,避免一次输入错误让设备无法恢复。 + +大文件应采用固定大小分块接收,不能按完整正文申请内存。一份实测实现使用 1024 字节分块。 +先写临时目标,校验大小和格式,处理并发访问,再在全部成功后替换正式资源;任何失败路径都要 +删除或标记临时目标无效。 + +## 有意控制网页服务规模 + +一份单客户端配置服务的实测参数是:3 个 HTTP socket、backlog 为 2、接收重试 2 次、发送 +重试 10 次、开启 LRU 清理、服务任务栈 6144 字节、TCP 收发窗口 2880 字节。这些是起点参考, +不是通用默认值。 + +增大传输缓冲之前,先缩减网页资源: + +- 构建时 gzip 压缩静态页面; +- 大型 JSON 或文件列表分块发送; +- 缩小缩略图,并限制同时解码的数量; +- 请求失败后立即关闭并释放上下文; +- 避免网页同时发起多个请求。 + +即使只打开一个页面,浏览器也可能建立多个连接。DNS、HTTP、STA 验证、遥测和仍在运行的音频 +服务所用 socket 必须合并计算。 + +## 常见故障特征 + +| 现象 | 优先检查 | +| --- | --- | +| 手机连上热点但不弹出页面 | DHCP 分配事件、DNS 响应、探测路径处理、HTTP socket 是否可用 | +| 提交表单时随机重启 | 声明长度、结尾空间、接收循环、任务栈、最大连续空闲块 | +| 上传中途停止 | 接收超时、分块写入结果、存储空间、并发读写 | +| 第一次成功,重试后失败 | socket、DNS 任务、事件处理器、netif 或临时缓冲是否泄漏 | +| 运行过其他功能后 AP 才启动失败 | 最大连续空闲块、可选子系统是否仍占用资源 | + +## 验证清单 + +- 测试 Android、iOS、Windows,以及至少一种不会自动弹窗的客户端。 +- 覆盖错误凭据、路由器不可达、DHCP 超时、AP 断开、HTTP 超时、超大表单、上传中断和反复重试。 +- 如果文档宣称支持 DHCP Option 114,必须用抓包或等价的真机证据验证。 +- 反复进入和退出配网,确认任务、事件处理器、socket 和 netif 数量回到稳定基线。 +- 在每个阶段记录空闲堆、最大连续空闲块和任务栈高水位。 + +最重要的经验是:把配网当成固件内部一个临时运行的网络产品。它需要明确的启动和停止归属、严格 +的输入上限,并且要分别验证 DHCP 与弹窗认证,不能把“已经连上 Wi-Fi”当成整个流程成功。