Skip to content

Codex/tuya app env self ca - #43

Open
xiongzh2000 wants to merge 9 commits into
tuya:masterfrom
xiongzh2000:codex/tuya-app-env-self-ca
Open

xiongzh2000 wants to merge 9 commits into
tuya:masterfrom
xiongzh2000:codex/tuya-app-env-self-ca

Conversation

@xiongzh2000

@xiongzh2000 xiongzh2000 commented Sep 30, 2026 •

Copy link
Copy Markdown

背景与目标

支持同一份设备固件随涂鸦 App 配网进入对应云环境,避免激活与后续 MQTT 使用不同环境,以及重启后丢失 App 选择的路由。参考 TuyaOpen 的 mqtt_bind.c、tuya_iot.c、iotdns.c:设备保存原始注册 key,原样传给 IoT DNS,由 DNS 返回地址和证书,不在固件里维护环境映射表。

协议规则(以当前最终 diff 为准)

  • BLE authToken 固定为 [region:2][token:8][secret:4];只用中间 8 字节发起激活,尾部 4 字节原样保存为 registration_key。
  • QR/MQTT 激活的 data.env 原样保存为注册 key;仅字段缺省时使用 pro,接受 1~4 个可打印字节,拒绝错误类型、空值、超长及非法字符。
  • pr_0、da_0、普通四字节 secret、私有云 key 等均不映射成 PRE/PROD。config.env/client->env 不被注册 key 改写;注册 key 存在时,实际业务路由以 DNS 结果为准。

主要改动

  1. BLE 与 QR/MQTT 共用 Self 路由激活逻辑:用原始 key 查询 httpsSelfUrl、mqttsSelfUrl(need_ca=true),用 DNS 返回的 HTTPS 地址和 CA 激活,不采用激活消息中的 httpsUrl。
  2. 客户端保留注册 key,后续连接/重连重新查询 Self 地址;MQTT、ATOP、session token、OTA、版本与 schema 请求统一使用对应 Self 路由及 DNS CA。
  3. DNS 地址缺失、路径/端口异常或 CA 无效时失败,不静默回退其他环境。两种 App 激活入口要求 MQTT TLS 及可信引导 CA/证书包;不支持的明文配置在激活前拒绝。
  4. 激活成功后的暂时 DNS/MQTT 故障保留已返回客户端的凭据,应用可保存绑定信息并重试连接;不应因此重新激活。减少激活请求敏感内容的日志输出。
  5. 补齐中英文 API/Token 说明、持久化恢复示例、模块上下文和 changelog,并新增本地 DNS/MQTT/TLS mock 回归覆盖。

兼容性与接入要求

  • 已绑定的旧设备若存储记录没有注册 key,初始化配置保持该字段全零,并恢复原 env,继续旧路由;不清绑定、不要求重新配网,不自动补 pro。
  • 新激活设备由应用将 devid、secret_key、local_key、region、env、registration_key 一起安全保存,重启时填入 iot_client_config_t。SDK 不代管 NVS/文件存储。
  • DNS 地址及 Self CA 无需持久化;每次启动配置运行时引导 CA/证书包即可。损坏的非空 key 应报错,不能清空后回退线上。
  • 公共结构新增字段,集成方需重新编译。BLE 的固定 14 字节格式未放宽;QR 的三字节 pro 与旧绑定的空 key 是不同语义。
  • 不修改芯片 Wi-Fi/BLE 适配层、ESP32 工程、本地依赖覆盖或子模块指针;不包含音乐、音频及设备解绑业务改动。

本轮 CR 补充修复

最新提交 07497a8 修复 QR/MQTT 对 pr_0/da_0 的枚举解析错误,允许三字节 pro,统一 DNS 激活入口,并补齐注册 key 持久化说明和重启恢复测试。前面提交包含迭代过程,审核协议语义请以当前最终 diff 为准,而非早期 commit 文案中的 pre/pro/prod 映射描述。

验证与限制

  • 在 07497a8 上完成完整主机编译,串行 CTest 18/18 套件通过;包括 BLE、RTC、MQTT、DP、OTA,以及 DNS 32/32、onboarding 25/25 用例。
  • 新增回归先确认旧实现失败,再验证修复通过;覆盖原始 key、缺省 pro、非法 key、DNS 决定激活地址、明文配置提前拒绝、凭据恢复后的 Self 路由。
  • 中英文文档一致性检查通过,检查器测试 15/15 通过。完整文档站构建因本地缺少 node_modules/tsc 未完成。
  • 本轮未重新烧录或做真机配网验证;远端 CI 已由本次推送触发,实时结果见 Checks。

建议集成验收:分别用线上、预发、日常 App 配网,确认 DNS/激活/MQTT 路由一致;保存完整绑定记录后断电重启验证;升级旧版本已绑定设备验证无需重新配网;断网恢复验证凭据保留且不跨环境回退。

xiong-luze and others added 9 commits September 24, 2026 15:05
Decode Tuya App registration keys from BLE tokens and MQTT activation messages so the SDK activates against the App-selected cloud. Preserve legacy token and configured-environment behavior for existing callers. Document the wire format and the returned client environment.

Verified: 18/18 CTest cases, i18n parity check, ESP32-S3 firmware build.

Co-Authored-By: Codex <codex@openai.com>
Direct BLE authTokens carry a four-byte App registration key, not a fixed environment enum. Resolve both Self endpoints before activation and reject incomplete or malformed DNS answers so activation cannot silently fall back to a legacy host. Preserve the QR/MQTT activation path.

Tests: iot_on_boarding_test 15/15; targeted CTest 1/1.

Co-Authored-By: Codex <noreply@openai.com>
ATOP uses a fixed /d.json path, so a different DNS path cannot be honored. Reject it before activation, and make on-boarding tests verify the exact on-wire App env plus absence of activation requests when DNS endpoints are invalid.

Tests: iot_on_boarding_test 17/17; targeted CTest 1/1.

Co-Authored-By: Codex <noreply@openai.com>
Reject Self HTTPS port 80 and non-443 ports without a CA or certificate bundle, because the HTTP transport would otherwise use plaintext. Reject JSON-special bytes in the eight-byte activation token before manual JSON formatting, while preserving opaque punctuation in the registration key. Stop logging the activation POST body and token prefix.

Tests: iot_on_boarding_test 20/20; serial CTest 18/18.

Co-Authored-By: Codex <noreply@openai.com>
Carry the opaque key through activated clients and re-resolve Self endpoints on reconnect. Fail closed for missing or unsafe endpoints so post-activation network faults preserve credentials without routing traffic to legacy hosts.

Tests: 5/5 affected CTest; 18/18 prior full suite.

Co-Authored-By: Codex <codex@openai.com>
Token onboarding always carries a registration key, which requires MQTT over TLS. Validate the transport before DNS or cloud activation so an unsupported configuration cannot consume an App token and discard issued credentials.

Tests: 1/1 onboarding CTest; 18/18 full CTest.

Co-Authored-By: Codex <codex@openai.com>
Use a trusted bootstrap CA for App-selected IoT DNS, retain the CA returned for Self endpoints, and apply it consistently to activation, MQTT, ATOP, OTA, version, and schema requests. Without authenticated DNS and its CA, token onboarding must fail closed rather than fall back to legacy TLS trust or unverified connections.

Verified: full host CTest suite passes (18/18).

Co-Authored-By: Codex <codex@openai.com>
The Tuya provisioning specification defines authToken as region(2) + token(8) + secret(4), and sends the secret unchanged as HTTPDNS env. Clarify that this opaque BLE secret is not a pre/pro/prod enum and does not override config.env; distinguish it from data.env in the separate QR/MQTT activation flow. Rename the QR activation env parser and assert the BLE secret leaves config.env unchanged.

Verified: iot_on_boarding_test CTest suite passes (1/1); docs i18n parity passes.

Co-Authored-By: Codex <codex@openai.com>
Match TuyaOpen MQTT activation: retain data.env as an opaque registration key, default to pro only when the field is absent, and reuse BLE Self DNS/CA discovery for activation. Reject unsupported plaintext QR configuration before activation. Accept bounded 1-4 byte keys without changing fixed-width BLE tokens or legacy clients with no key.

Document application-owned persistence of credentials, region, legacy env and the raw registration key, including upgrades of already-bound devices. Add QR/default/invalid-key and restart regression coverage.

Verified: complete host build and 18/18 serial CTest suites passed; i18n parity passed and 15/15 parity tests passed. Documentation-site build could not run because tsc/node_modules are missing.

Co-Authored-By: OpenAI Codex <codex@openai.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants