GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — [Feature Request] Document NVRAM backup/restore commands in ZBOSS NCP Serial Protocol · Issue #818 · espressif/esp-zigbee-sdk

[Feature Request] Document NVRAM backup/restore commands in ZBOSS NCP Serial Protocol #818

New issue
Copy link
New issue
Copy link
Open
Open
[Feature Request] Document NVRAM backup/restore commands in ZBOSS NCP Serial Protocol#818
Copy link

Description

@tostmann
tostmann
opened on May 25, 2026
Issue body actions

Summary

Request to extend the ZBOSS NCP Serial Protocol with documented commands for backing up and restoring the network identity of an ESP-Zigbee NCP coordinator (PAN, ExtPAN, channel, NWK key, frame counter, neighbor table, raw NVRAM).

Without these commands, an ESP-Zigbee NCP coordinator cannot survive a flash erase, hardware swap, or NCP firmware downgrade/upgrade without losing all paired devices.

Why this matters

zigbee-herdsman's adapter:zboss is currently the only host-side stack that lets ESP-Zigbee NCP firmwares (ESP32-C6/H2/C5) run as Zigbee coordinators under Zigbee2MQTT and (in progress) Home Assistant ZHA. The adapter is labeled "experimental, not recommended for production" by Z2M — the single biggest missing feature is backup/restore. Compare against the other production adapters:

Adapter Backup Restore
ember (Sonoff/SMLight) yes yes
zstack (TI) yes yes
deconz (ConBee) yes yes
zboss (Espressif) no — throw NOT_SUPPORTED no — throw NOT_SUPPORTED

Reference: zigbee-herdsman/src/adapter/zboss/adapter/zbossAdapter.ts lines ~209, 282–284 (throw new Error("NOT SUPPORTED") for backup/restore/addInstallCode/reset).

Backup is functionality users implicitly expect from any production coordinator — restoring after coordinator hardware swap, after corrupted NVS, after firmware migration. Without it the ESP-Zigbee NCP path is structurally a non-starter for many users.

Why a host-only fix doesn't work

The state lives in the NCP's zb_storage partition (16 KB, ZBOSS-managed) plus several NVS keys. The current Serial Protocol exposes individual NIB getters via dedicated commands, but:

  1. No way to read the NWK outgoing frame counter from the host other than a vendor command — and it's mandatory for restore (otherwise replay counters reject the coord's first frames after restore).
  2. No way to read the neighbor table from the host without app-side iteration of esp_zb_nwk_get_next_neighbor().
  3. esp_zb_set_extended_pan_id() doesn't set nwkExtendedPANID — it sets apsUseExtendedPANID. The NIB-level setter esp_zb_nwk_set_extended_pan_id() works, but Set Extended PAN ID not working as coordinator (TZ-1193) #445 confirms "only takes effect in factoryreset state."
  4. No public NWK operating-channel setter exists — neither in esp_zigbee_nwk.h nor in zboss_api.h. Only scan-mask APIs consumed by BDB are writable.
  5. Setter side-effect: writing PAN + ExtPAN + key marks the chip joined=true, so bdb_start_top_level_commissioning(NETWORK_FORMATION) no-ops afterward — the chip stays on channel 0xFF after a structured-restore-only path.

Empirically, the only path that preserves end-device pairings across a coordinator wipe is byte-for-byte overwriting zb_storage. ZBOSS loads from disk on boot and is genuinely in the "I am this network" state — channel and all. This requires a documented serial command to read and write the partition — not just better setters.

Proposed commands

Below are four commands as implemented and live-verified in tostmann/esp-coordinator (firmware v1.2.32) plus a matching zigbee-herdsman adapter patch in tostmann/zigbee2mqtt-esp32. Command IDs are our extension-range placeholders — Espressif's call to assign canonical IDs.

GET_NETWORK_BACKUP (proposed 0x0099)

Reads nvs (24 KB) + zb_storage (16 KB) as one logical 40 KB image, chunked. ~128 B per chunk to fit ZBOSS packet MTU.

Request:
  uint32_t offset;

Response:
  uint8_t  status;
  uint32_t total_size;
  uint32_t chunk_length;
  uint8_t  data[chunk_length];

RESTORE_NETWORK (proposed 0x009A)

Writes raw partition data, chunked. Final chunk triggers a deferred esp_restart() after USB-Serial-JTAG phy detach (~800 ms) so the host sees a real port close.

Request:
  uint32_t offset;
  uint32_t total_size;
  uint8_t  data[];

Response:
  uint8_t  status;

GET_STRUCTURED_BACKUP (proposed 0x009B)

Returns the semantically meaningful fields in a TLV-encoded image. Small (~770 B for a 2-device network), human-inspectable, format-independent of firmware version. JSON-compatible with the Zigbee Alliance "Universal NWK Backup" shape that zigbee-herdsman already understands.

Request: (empty)

Response:
  uint8_t   status;
  uint32_t  magic;          // 'ZBSB' = 0x4253425A
  uint8_t   format_version; // 1
  uint8_t   flags;
  uint16_t  payload_len;
  uint8_t   tlvs[payload_len];

TLV tags (LE encoding where applicable):

Tag Name Length Source
0x01 PAN_ID 2 esp_zb_get_pan_id()
0x02 EXT_PAN_ID 8 esp_zb_get_extended_pan_id()
0x03 CHANNEL 1 esp_zb_get_current_channel()
0x04 NWK_UPDATE_ID 1 esp_zb_nwk_get_update_id()
0x05 COORD_IEEE 8 esp_zb_get_long_address()
0x06 NWK_KEY 16 esp_zb_secur_primary_network_key_get()
0x07 NWK_KEY_SEQ 1 key sequence number
0x08 NWK_FRAME_COUNTER 4 esp_zb_nwk_get_frame_counter()
0x10 DEVICE_TABLE N×16 iterate esp_zb_nwk_get_next_neighbor()

Each DEVICE_TABLE record (16 B): uint64_t ieee; uint16_t short_addr; uint8_t flags; uint8_t reserved[5];.

RESTORE_STRUCTURED_BACKUP (proposed 0x009C)

Persists a TLV blob to NVS, schedules esp_restart(). On next boot, the bootstrap applies setters before zboss_start_no_autostart().

Same wire shape as the GET_STRUCTURED_BACKUP response payload, with a uint8_t status response.

Known limitation today: applies only partially because of the SDK setter issues listed above. Useful for "form a new network with these defaults" and for cross-adapter migration; not sufficient as a standalone restore until #445 and the missing channel setter are addressed. The raw GET_NETWORK_BACKUP/RESTORE_NETWORK pair is what actually preserves paired devices today.

Reference implementation

  • Firmware: tostmann/esp-coordinator — handlers in main/commands_impl.h, NVS bootstrap in main/zb_ncp.cpp::apply_pending_restore(), TLV layout in main/backup_structured.h.
  • Host stack: tostmann/zigbee2mqtt-esp32 — wire schemas + adapter integration via postinstall (scripts/patch_zboss.js).
  • Live-verified end-to-end 2026-05-24 on ESP32-C6: backup → esptool erase_flash → reflash factory.bin → start Z2M with backup JSON → end-devices reconnect at full link quality without re-pair.

Related issues

  • Set Extended PAN ID not working as coordinator (TZ-1193) #445 — esp_zb_set_extended_pan_id only effective in factoryreset state
  • ZDO simple description response receive but ZIGBEE stack reports request timeout (TZ-1300) #485 — libzboss_stack.zczr.a parser dropping Tuya Simple_Desc_rsp responses
  • Assertion failed in zb_address.c when adding more than 30 Router devices (TZ-2017) #717, Assertion failed in zb_address.c when adding more than 30 Router devices (TZ-2016) #714 — ~30-router stack assertion in libzboss_stack.zczr.a zb_address.c:395
  • OTA payload structs missing __attribute__((packed)) — breaks Zigbee2MQTT OTA compatibility #813 — OTA pass-through on the device side

Ask

  1. Assign canonical command IDs for backup/restore (or confirm 0x0099–0x009C are acceptable in the vendor-extension range).
  2. Document them in the ZBOSS NCP Serial Protocol PDF so zigbee-herdsman upstream has a spec source to target — currently the spec is silent on backup/restore, which is what blocks the adapter from removing its throw NOT_SUPPORTED paths.
  3. Long-term: address the setter gaps in Set Extended PAN ID not working as coordinator (TZ-1193) #445 and add a public esp_zb_nwk_set_current_channel() so the structured-restore path becomes self-sufficient and the raw-partition fallback is no longer needed.

If Espressif prefers a different on-wire shape (different TLV tags, different chunk size, different command groupings, different ID assignments), happy to adapt the reference implementation. The goal is a documented serial protocol the herdsman side can implement upstream.

cc @xieqinan

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402792 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.