espressif / esp-zigbee-sdk

Espressif Zigbee SDK
Apache License 2.0
178 stars 31 forks source link

heap_caps_free assertion in HA_ON_OFF_LIGHT example with extra clusters after update from 1.3.2 to 1.4.0 (TZ-1078) #411

Closed TJvV-DT closed 3 months ago

TJvV-DT commented 3 months ago

Answers checklist.

IDF version.

v5.1.3-474-gd06c758489

esp-zigbee-lib version.

1.4.0 and 1.4.1

esp-zboss-lib version.

1.4.0 and 1.4.1

Espressif SoC revision.

ESP32-C6

What is the expected behavior?

I expect the device to come online and join my ZB network as it did with SDK v1.3.2.

I (99) esp_image: segment 0: paddr=00010020 vaddr=4205802g the size in the binaESP-ROM:esp32c6-20220919
Build:Sep 19 2022
rst:0x15 (USB_UART_HPSYS),boot:0xc (SPI_FAST_FLASH_BOOT)
Saved PC:0x40022bc4
0x40022bc4: uart_serial_tx_one_char in ROM

SPIWP:0xee
mode:DIO, clock div:2
load:0x4086c410,len:0xd8c
load:0x4086e610,len:0x2e04
load:0x40875728,len:0x17e0
entry 0x4086c410
I (23) boot: ESP-IDF v5.1.3-474-gd06c758489 2nd stage bootloader
I (23) boot: compile time Aug 22 2024 10:55:32
I (24) boot: chip revision: v0.0
I (27) boot.esp32c6: SPI Speed      : 80MHz
I (32) boot.esp32c6: SPI Mode       : DIO
I (37) boot.esp32c6: SPI Flash Size : 2MB
I (41) boot: Enabling RNG early entropy source...
I (47) boot: Partition Table:
I (50) boot: ## Label            Usage          Type ST Offset   Length
I (58) boot:  0 nvs              WiFi data        01 02 00009000 00006000
I (65) boot:  1 phy_init         RF data          01 01 0000f000 00001000
I (72) boot:  2 factory          factory app      00 00 00010000 000e1000
I (80) boot:  3 zb_storage       Unknown data     01 81 000f1000 00004000
I (87) boot:  4 zb_fct           Unknown data     01 81 000f5000 00000400
I (95) boot: End of partition table
I (99) esp_image: segment 0: paddr=00010020 vaddr=42058020 size=0fb10h ( 64272) map
I (120) esp_image: segment 1: paddr=0001fb38 vaddr=40800000 size=004e0h (  1248) load
I (122) esp_image: segment 2: paddr=00020020 vaddr=42000020 size=57e84h (360068) map
I (197) esp_image: segment 3: paddr=00077eac vaddr=408004e0 size=0fca8h ( 64680) load
I (216) boot: Loaded app from partition at offset 0x10000
I (216) boot: Disabling RNG early entropy source...
I (227) cpu_start: Unicore app
I (228) cpu_start: Pro cpu up.
W (236) clk: esp_perip_clk_init() has not been implemented yet
I (243) cpu_start: Pro cpu start user code
I (243) cpu_start: cpu freq: 160000000 Hz
I (243) cpu_start: Application information:
I (246) cpu_start: Project name:     light_bulb
I (251) cpu_start: App version:      1
I (256) cpu_start: Compile time:     Aug 22 2024 10:55:24
I (262) cpu_start: ELF file SHA256:  54acd62c7e80607d...
I (268) cpu_start: ESP-IDF:          v5.1.3-474-gd06c758489
I (274) cpu_start: Min chip rev:     v0.0
I (279) cpu_start: Max chip rev:     v0.99
I (283) cpu_start: Chip rev:         v0.0
I (288) heap_init: Initializing. RAM available for dynamic allocation:
I (295) heap_init: At 40814840 len 00067DD0 (415 KiB): D/IRAM
I (302) heap_init: At 4087C610 len 00002F54 (11 KiB): STACK/DIRAM
I (309) heap_init: At 50000000 len 00003FE8 (15 KiB): RTCRAM
I (316) spi_flash: detected chip: generic
I (320) spi_flash: flash io: dio
W (323) spi_flash: Detected size(8192k) larger than the size in the binary image header(2048k). Using the size in the binary image header.
I (337) sleep: Configure to isolate all GPIO pins in sleep state
I (343) sleep: Enable automatic switching of GPIO sleep configuration
I (350) coexist: coex firmware version: d96c1e51f
I (356) coexist: coexist rom version 5b8dcfa
I (361) app_start: Starting scheduler on CPU0
I (366) main_task: Started on CPU0
I (366) main_task: Calling app_main()
I (376) phy_init: phy_version 270,339aa07,Apr  3 2024,16:36:11
W (376) phy_init: failed to load RF calibration data (0x1102), falling back to full calibration
I (426) phy: libbtbb version: f97b181, Apr  3 2024, 16:36:26
I (426) TEST: No cluster list supplied, creating own
I (426) TEST: Creating attributes
I (436) TEST: Add attribute: id 0, type 2b, access 3: 0
I (436) TEST: Add client cluster
I (446) TEST: Add server cluster
I (446) TEST: Add endpoint
I (446) main_task: Returned from app_main()
I (556) ESP_ZB_ON_OFF_LIGHT: ZDO signal: ZDO Config Ready (0x17), status: ESP_FAIL
I (556) ESP_ZB_ON_OFF_LIGHT: Initialize Zigbee stack
I (556) gpio: GPIO[8]| InputEn: 0| OutputEn: 1| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
I (556) ESP_ZB_ON_OFF_LIGHT: Deferred driver initialization successful
I (576) ESP_ZB_ON_OFF_LIGHT: Device started up in  factory-reset mode
I (576) ESP_ZB_ON_OFF_LIGHT: Start network steering
I (2836) ESP_ZB_ON_OFF_LIGHT: Network steering was not successful (status: ESP_FAIL)
I (6096) ESP_ZB_ON_OFF_LIGHT: Network steering was not successful (status: ESP_FAIL)
I (9356) ESP_ZB_ON_OFF_LIGHT: Network steering was not successful (status: ESP_FAIL)
I (13396) ESP_ZB_ON_OFF_LIGHT: Joined network successfully (Extended PAN ID: 74:ee:d0:f7:5d:e2:f7:58, PAN ID: 0xb961, Channel:20, Short Address: 0x3684)

What is the actual behavior?

Instead, the application crashes with assert failed: heap_caps_free heap_caps.c:387 (heap != NULL && "free() target pointer is outside heap areas")

I (99) esp_image: segment 0: paddr=00010020 vaddr=4205802ESP-ROM:esp32c6-20220919
Build:Sep 19 2022
rst:0x15 (USB_UART_HPSYS),boot:0xc (SPI_FAST_FLASH_BOOT)
Saved PC:0x4204c13a
0x4204c13a: nvs::PageManager::load(nvs::Partition*, unsigned long, unsigned long) at C:/esp/v5.1/esp-idf/components/nvs_flash/src/nvs_pagemanager.cpp:47

SPIWP:0xee
mode:DIO, clock div:2
load:0x4086c410,len:0xd8c
load:0x4086e610,len:0x2e04
load:0x40875728,len:0x17e0
entry 0x4086c410
I (23) boot: ESP-IDF v5.1.3-474-gd06c758489 2nd stage bootloader
I (23) boot: compile time Aug 22 2024 10:58:17
I (24) boot: chip revision: v0.0
I (27) boot.esp32c6: SPI Speed      : 80MHz
I (32) boot.esp32c6: SPI Mode       : DIO
I (37) boot.esp32c6: SPI Flash Size : 2MB
I (41) boot: Enabling RNG early entropy source...
I (47) boot: Partition Table:
I (50) boot: ## Label            Usage          Type ST Offset   Length
I (58) boot:  0 nvs              WiFi data        01 02 00009000 00006000
I (65) boot:  1 phy_init         RF data          01 01 0000f000 00001000
I (72) boot:  2 factory          factory app      00 00 00010000 000e1000
I (80) boot:  3 zb_storage       Unknown data     01 81 000f1000 00004000
I (87) boot:  4 zb_fct           Unknown data     01 81 000f5000 00000400
I (95) boot: End of partition table
I (99) esp_image: segment 0: paddr=00010020 vaddr=42058020 size=0fae0h ( 64224) map
I (120) esp_image: segment 1: paddr=0001fb08 vaddr=40800000 size=00510h (  1296) load
I (122) esp_image: segment 2: paddr=00020020 vaddr=42000020 size=57a8ch (359052) map
I (197) esp_image: segment 3: paddr=00077ab4 vaddr=40800510 size=0e1d4h ( 57812) load
I (211) esp_image: segment 4: paddr=00085c90 vaddr=4080e6f0 size=01a78h (  6776) load
I (216) boot: Loaded app from partition at offset 0x10000
I (217) boot: Disabling RNG early entropy source...
I (231) cpu_start: Unicore app
I (231) cpu_start: Pro cpu up.
W (240) clk: esp_perip_clk_init() has not been implemented yet
I (246) cpu_start: Pro cpu start user code
I (247) cpu_start: cpu freq: 160000000 Hz
I (247) cpu_start: Application information:
I (249) cpu_start: Project name:     light_bulb
I (255) cpu_start: App version:      1
I (259) cpu_start: Compile time:     Aug 22 2024 10:58:08
I (265) cpu_start: ELF file SHA256:  46c672fe53a3a47f...
I (271) cpu_start: ESP-IDF:          v5.1.3-474-gd06c758489
I (277) cpu_start: Min chip rev:     v0.0
I (282) cpu_start: Max chip rev:     v0.99
I (287) cpu_start: Chip rev:         v0.0
I (292) heap_init: Initializing. RAM available for dynamic allocation:
I (299) heap_init: At 408148C0 len 00067D50 (415 KiB): D/IRAM
I (305) heap_init: At 4087C610 len 00002F54 (11 KiB): STACK/DIRAM
I (312) heap_init: At 50000000 len 00003FE8 (15 KiB): RTCRAM
I (319) spi_flash: detected chip: generic
I (323) spi_flash: flash io: dio
W (327) spi_flash: Detected size(8192k) larger than the size in the binary image header(2048k). Using the size in the binary image header.
I (340) sleep: Configure to isolate all GPIO pins in sleep state
I (347) sleep: Enable automatic switching of GPIO sleep configuration
I (354) coexist: coex firmware version: d96c1e51f
I (360) coexist: coexist rom version 5b8dcfa
I (365) app_start: Starting scheduler on CPU0
I (369) main_task: Started on CPU0
I (369) main_task: Calling app_main()
I (379) phy_init: phy_version 270,339aa07,Apr  3 2024,16:36:11
W (379) phy_init: failed to load RF calibration data (0x1102), falling back to full calibration
I (429) phy: libbtbb version: f97b181, Apr  3 2024, 16:36:26
I (429) TEST: No cluster list supplied, creating own
I (429) TEST: Creating attributes
I (439) TEST: Add attribute: id 0, type 2b, access 3: 0
I (439) TEST: Add client cluster
I (449) TEST: Add server cluster
I (449) TEST: Add endpoint

assert failed: heap_caps_free heap_caps.c:387 (heap != NULL && "free() target pointer is outside heap areas")
Core  0 register dump:
MEPC    : 0x4080063e  RA      : 0x40808c2a  SP      : 0x4087dfc0  GP      : 0x4080eef0
Stack dump detected
0x4080063e: panic_abort at C:/esp/v5.1/esp-idf/components/esp_system/panic.c:466
0x40808c2a: __ubsan_include at C:/esp/v5.1/esp-idf/components/esp_system/ubsan.c:313

TP      : 0x4086ef58  T0      : 0x37363534  T1      : 0x7271706f  T2      : 0x33323130
S0/FP   : 0x0000005a  S1      : 0x00000001  A0      : 0x4087dffc  A1      : 0x4080f309
A2      : 0x00000001  A3      : 0x00000029  A4      : 0x00000001  A5      : 0x40814000
A6      : 0x00000008  A7      : 0x76757473  S2      : 0x00000009  S3      : 0x4087e13d  
S4      : 0x4080f308  S5      : 0x4081c7c8  S6      : 0x00000002  S7      : 0x00000000
S8      : 0x4081cbcf  S9      : 0x00000000  S10     : 0x00000002  S11     : 0x00000001
T3      : 0x6e6d6c6b  T4      : 0x6a696867  T5      : 0x66656463  T6      : 0x62613938
MSTATUS : 0x00001881  MTVEC   : 0x40800001  MCAUSE  : 0x00000007  MTVAL   : 0x00000000
0x40800001: _vector_table at ??:?

MHARTID : 0x00000000

Failed to run gdb_panic_server.py script: Command '['riscv32-esp-elf-gdb', '--batch', '-n', 'C:\\werk\\dynatron\\LfitSens\\git\\HA_on_off_light\\HA_on_off_light\\build\\light_bulb.elf', '-ex', 'target remote | "C:\\esp\\.espressif\\python_env\\idf5.1_py3.11_env\\Scripts\\python.exe" -m esp_idf_panic_decoder --target esp32c6 "C:\\Users\\thijs\\AppData\\Local\\Temp\\tmp1i8klu9n"', '-ex', 'bt']' returned non-zero exit status 1.
b"error starting child process '| C:\\esp\\.espressif\\python_env\\idf5.1_py3.11_env\\Scripts\\python.exe -m esp_idf_panic_decoder --target esp32c6 C:\\Users\\thijs\\AppData\\Local\\Temp\\tmp1i8klu9n': CreateProcess: No such file or directory\r\nNo stack.\r\n"

Stack memory:
4087dfc0: 0xff010401 0x00000000 0x42058e38 0x4080d722 0x4080f924 0x42058e38 0x4080fc74 0x42058db6
4087dfe0: 0x4080f934 0x4087dff4 0x4080f938 0x42058d60 0x4080f308 0x00373833 0x0000010e 0x65737361
4087e000: 0x66207472 0x656c6961 0x68203a64 0x5f706165 0x73706163 0x6572665f 0x65682065 0x635f7061
4087e020: 0x2e737061 0x38333a63 0x68282037 0x20706165 0x4e203d21 0x204c4c55 0x22202626 0x65657266
4087e040: 0x74202928 0x65677261 0x6f702074 0x65746e69 0x73692072 0x74756f20 0x65646973 0x61656820
4087e060: 0x72612070 0x22736165 0x00000029 0x40810f40 0x4205d000 0x4087e0f8 0x00000000 0x4204d13e
4087e080: 0x00000000 0x00000000 0x40816790 0x4080ccac 0x00000002 0x00000000 0x4081cbcf 0x4080ccac
4087e0a0: 0x00000001 0x4081c7c8 0x4081cb98 0x40022498 0x4081ccac 0x40880000 0x408148c0 0x4080ccac
4087e0c0: 0x00000002 0x00000000 0x00000000 0x40000000 0x40880000 0x4081cc3c 0x4081cb98 0x00000002
4087e0e0: 0x0000ffff 0x4081c710 0x00004081 0x40800c0c 0x00000040 0x00000000 0x4081c710 0x4080d748
4087e100: 0x00000040 0x00000007 0x4081c6a4 0x420081da 0x00000040 0x4081c784 0x4081c6f8 0x420085ee
4087e120: 0x4081cbc0 0x4081c79c 0x4081c380 0x42009b9a 0x00001800 0x00000002 0x4081c7d8 0x4200b87c
4087e140: 0x4081c380 0x0000ffff 0x4081cc50 0x00000003 0x01010002 0x4081cbc0 0x40810004 0x4081cc50
4087e160: 0x4081c710 0x00000001 0x4081c784 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000
4087e180: 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x4081c380 0x420098bc
4087e1a0: 0x00000000 0x00000000 0x0001040a 0x00000000 0x00000000 0x00000000 0x4081c380 0x42007a8e
4087e1c0: 0x00000008 0x00000000 0x00000000 0x00000000 0x00000002 0x00000000 0x00000006 0x00000bb8
4087e1e0: 0x00000000 0x00000000 0x00000000 0x4080b120 0x00000000 0x00000000 0x00000000 0x00000000
4087e200: 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5
4087e220: 0xa5a5a5a5 0xa5a5a5a5 0x00000154 0x4087dae0 0x4087c624 0x408103d0 0x408103d0 0x4087e22c
4087e240: 0x408103c8 0x00000014 0x0000ffff 0xffffffff 0x4087e22c 0x00000000 0x00000005 0x4087d228
4087e260: 0x6267695a 0x6d5f6565 0x006e6961 0x00010000 0x00000000 0x4087e220 0x00000005 0x00000000
4087e280: 0x00000000 0x00000000 0x00000000 0x40815298 0x40815300 0x40815368 0x00000000 0x00000000
4087e2a0: 0x00000001 0x00000000 0x00000000 0x00000000 0x42054a28 0x00000000 0x00000000 0x00000000
4087e2c0: 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000
4087e2e0: 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000
4087e300: 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000
4087e320: 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000
4087e340: 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000
4087e360: 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x40000000
4087e380: 0x00000014 0x4267695a 0x6c416565 0x6e61696c 0x39306563 0x00000000 0x0000000c 0x00ffffff
4087e3a0: 0x00000000 0x40870000 0x00000280 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000

Steps to reproduce.

To reproduce:

  1. Setup ZigBee network that allows anyone to join
  2. Create new project based on HA_ON_OFF_LIGHT example
  3. Configure for ESP32C6
  4. Add custom cluster as both server and client
  5. Run idf.py fullclean build erase-flash flash monitor
  6. Stop monitor when output looks stable
  7. Change esp-zboss-lib and esp-zigbee-lib versions in idf_component.yml
  8. Run idf.py fullclean build erase-flash flash monitor

Code:

/*
 * SPDX-FileCopyrightText: 2021-2024 Espressif Systems (Shanghai) CO LTD
 *
 * SPDX-License-Identifier: LicenseRef-Included
 *
 * Zigbee HA_on_off_light Example
 *
 * This example code is in the Public Domain (or CC0 licensed, at your option.)
 *
 * Unless required by applicable law or agreed to in writing, this
 * software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
 * CONDITIONS OF ANY KIND, either express or implied.
 */

#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_check.h"
#include "esp_log.h"
#include "nvs_flash.h"
#include "ha/esp_zigbee_ha_standard.h"
#include "esp_zb_light.h"

#if !defined ZB_ED_ROLE
#error Define ZB_ED_ROLE in idf.py menuconfig to compile light (End Device) source code.
#endif

static const char *TAG = "ESP_ZB_ON_OFF_LIGHT";

/********************* Custom cluster **************************/
#define TEST_LOG_LEVEL(__lvl__, ...) ESP_LOG_LEVEL(__lvl__, "TEST", ##__VA_ARGS__)
#define TEST_LOG(...) TEST_LOG_LEVEL(ESP_LOG_INFO, ##__VA_ARGS__)

#define MY_ENDPOINT UINT8_C(1)
#define MY_CLUSTER_ID UINT16_C(0xFC00)
#define MY_PROFILE_ID UINT16_C(0x0104U)
#define MY_DEVICE_ID UINT16_C(0xfff2)
#define MY_DEVICE_VERSION UINT8_C(1)

typedef struct
{
    uint16_t id;
    esp_zb_zcl_attr_type_t type;
    uint8_t access;
    void *value;
} Zcl_attribute;

static int32_t attr_val;

Zcl_attribute my_cluster_attributes[] = {
    {
        .id = 0,
        .type = ESP_ZB_ZCL_ATTR_TYPE_S32,
        .access = ESP_ZB_ZCL_ATTR_ACCESS_READ_WRITE,
        .value = &attr_val,
    },
};

esp_zb_attribute_list_t *create_attributes_list()
{
    TEST_LOG("Creating attributes");

    esp_zb_attribute_list_t *attributes_list = esp_zb_zcl_attr_list_create(MY_CLUSTER_ID);
    const size_t num_attrs = sizeof(my_cluster_attributes) / sizeof(my_cluster_attributes[0]);

    const Zcl_attribute *attr = &my_cluster_attributes[0];
    for (size_t count = 0; count < num_attrs; count++, attr++)
    {
        const esp_err_t err = esp_zb_custom_cluster_add_custom_attr(attributes_list, attr->id, attr->type, attr->access, attr->value);

        TEST_LOG("Add attribute: id %x, type %x, access %x: %x", attr->id, attr->type, attr->access, err);
        if (err != ESP_OK)
        {
            TEST_LOG_LEVEL(ESP_LOG_ERROR, "Failed to add attribute %x: %x", attr->id, err);
            return NULL;
        }
    }

    return attributes_list;
}

esp_err_t configure_custom_clusters(esp_zb_ep_list_t *esp_zb_ep_list, esp_zb_cluster_list_t *esp_zb_cluster_list)
{
    if (esp_zb_ep_list == NULL)
    {
        TEST_LOG_LEVEL(ESP_LOG_ERROR, "Invalid arguments");
        return ESP_ERR_INVALID_ARG;
    }

    if (esp_zb_cluster_list == NULL)
    {
        TEST_LOG("No cluster list supplied, creating own");
        esp_zb_cluster_list = esp_zb_zcl_cluster_list_create();
    }

    esp_zb_attribute_list_t *attributes_list = create_attributes_list();
    if (attributes_list == NULL)
    {
        return ESP_ERR_INVALID_STATE;
    }

    esp_err_t err = ESP_FAIL;

    TEST_LOG("Add client cluster");
    err = esp_zb_cluster_list_add_custom_cluster(esp_zb_cluster_list, attributes_list, ESP_ZB_ZCL_CLUSTER_CLIENT_ROLE);
    if (err != ESP_OK)
    {
        TEST_LOG_LEVEL(ESP_LOG_ERROR, "Failed to add client cluster: %x", err);
        return err;
    }

    TEST_LOG("Add server cluster");
    err = esp_zb_cluster_list_add_custom_cluster(esp_zb_cluster_list, attributes_list, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE);
    if (err != ESP_OK)
    {
        TEST_LOG_LEVEL(ESP_LOG_ERROR, "Failed to add server cluster: %x", err);
        return err;
    }

    /* add created endpoint (cluster_list) to endpoint list */
    TEST_LOG("Add endpoint");
    const esp_zb_endpoint_config_t cfg = {
        .endpoint = MY_ENDPOINT,
        .app_profile_id = MY_PROFILE_ID,
        .app_device_id = MY_DEVICE_ID,
        .app_device_version = MY_DEVICE_VERSION,
    };
    err = esp_zb_ep_list_add_ep(esp_zb_ep_list, esp_zb_cluster_list, cfg);
    if (err != ESP_OK)
    {
        TEST_LOG_LEVEL(ESP_LOG_ERROR, "Failed to add endpoint: %x", err);
        return err;
    }

    return err;
}

/********************* Define functions **************************/
static esp_err_t deferred_driver_init(void)
{
    light_driver_init(LIGHT_DEFAULT_OFF);
    return ESP_OK;
}

static void bdb_start_top_level_commissioning_cb(uint8_t mode_mask)
{
    ESP_RETURN_ON_FALSE(esp_zb_bdb_start_top_level_commissioning(mode_mask) == ESP_OK, , TAG, "Failed to start Zigbee commissioning");
}

void esp_zb_app_signal_handler(esp_zb_app_signal_t *signal_struct)
{
    uint32_t *p_sg_p = signal_struct->p_app_signal;
    esp_err_t err_status = signal_struct->esp_err_status;
    esp_zb_app_signal_type_t sig_type = *p_sg_p;
    switch (sig_type)
    {
    case ESP_ZB_ZDO_SIGNAL_SKIP_STARTUP:
        ESP_LOGI(TAG, "Initialize Zigbee stack");
        esp_zb_bdb_start_top_level_commissioning(ESP_ZB_BDB_MODE_INITIALIZATION);
        break;
    case ESP_ZB_BDB_SIGNAL_DEVICE_FIRST_START:
    case ESP_ZB_BDB_SIGNAL_DEVICE_REBOOT:
        if (err_status == ESP_OK)
        {
            ESP_LOGI(TAG, "Deferred driver initialization %s", deferred_driver_init() ? "failed" : "successful");
            ESP_LOGI(TAG, "Device started up in %s factory-reset mode", esp_zb_bdb_is_factory_new() ? "" : "non");
            if (esp_zb_bdb_is_factory_new())
            {
                ESP_LOGI(TAG, "Start network steering");
                esp_zb_bdb_start_top_level_commissioning(ESP_ZB_BDB_MODE_NETWORK_STEERING);
            }
            else
            {
                ESP_LOGI(TAG, "Device rebooted");
            }
        }
        else
        {
            /* commissioning failed */
            ESP_LOGW(TAG, "Failed to initialize Zigbee stack (status: %s)", esp_err_to_name(err_status));
        }
        break;
    case ESP_ZB_BDB_SIGNAL_STEERING:
        if (err_status == ESP_OK)
        {
            esp_zb_ieee_addr_t extended_pan_id;
            esp_zb_get_extended_pan_id(extended_pan_id);
            ESP_LOGI(TAG, "Joined network successfully (Extended PAN ID: %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x, PAN ID: 0x%04hx, Channel:%d, Short Address: 0x%04hx)",
                     extended_pan_id[7], extended_pan_id[6], extended_pan_id[5], extended_pan_id[4],
                     extended_pan_id[3], extended_pan_id[2], extended_pan_id[1], extended_pan_id[0],
                     esp_zb_get_pan_id(), esp_zb_get_current_channel(), esp_zb_get_short_address());
        }
        else
        {
            ESP_LOGI(TAG, "Network steering was not successful (status: %s)", esp_err_to_name(err_status));
            esp_zb_scheduler_alarm((esp_zb_callback_t)bdb_start_top_level_commissioning_cb, ESP_ZB_BDB_MODE_NETWORK_STEERING, 1000);
        }
        break;
    default:
        ESP_LOGI(TAG, "ZDO signal: %s (0x%x), status: %s", esp_zb_zdo_signal_to_string(sig_type), sig_type,
                 esp_err_to_name(err_status));
        break;
    }
}

static esp_err_t zb_attribute_handler(const esp_zb_zcl_set_attr_value_message_t *message)
{
    esp_err_t ret = ESP_OK;
    bool light_state = 0;

    ESP_RETURN_ON_FALSE(message, ESP_FAIL, TAG, "Empty message");
    ESP_RETURN_ON_FALSE(message->info.status == ESP_ZB_ZCL_STATUS_SUCCESS, ESP_ERR_INVALID_ARG, TAG, "Received message: error status(%d)",
                        message->info.status);
    ESP_LOGI(TAG, "Received message: endpoint(%d), cluster(0x%x), attribute(0x%x), data size(%d)", message->info.dst_endpoint, message->info.cluster,
             message->attribute.id, message->attribute.data.size);
    if (message->info.dst_endpoint == HA_ESP_LIGHT_ENDPOINT)
    {
        if (message->info.cluster == ESP_ZB_ZCL_CLUSTER_ID_ON_OFF)
        {
            if (message->attribute.id == ESP_ZB_ZCL_ATTR_ON_OFF_ON_OFF_ID && message->attribute.data.type == ESP_ZB_ZCL_ATTR_TYPE_BOOL)
            {
                light_state = message->attribute.data.value ? *(bool *)message->attribute.data.value : light_state;
                ESP_LOGI(TAG, "Light sets to %s", light_state ? "On" : "Off");
                light_driver_set_power(light_state);
            }
        }
    }
    return ret;
}

static esp_err_t zb_action_handler(esp_zb_core_action_callback_id_t callback_id, const void *message)
{
    esp_err_t ret = ESP_OK;
    switch (callback_id)
    {
    case ESP_ZB_CORE_SET_ATTR_VALUE_CB_ID:
        ret = zb_attribute_handler((esp_zb_zcl_set_attr_value_message_t *)message);
        break;
    default:
        ESP_LOGW(TAG, "Receive Zigbee action(0x%x) callback", callback_id);
        break;
    }
    return ret;
}

static void esp_zb_task(void *pvParameters)
{
    /* initialize Zigbee stack */
    esp_zb_cfg_t zb_nwk_cfg = ESP_ZB_ZED_CONFIG();
    esp_zb_init(&zb_nwk_cfg);
    esp_zb_on_off_light_cfg_t light_cfg = ESP_ZB_DEFAULT_ON_OFF_LIGHT_CONFIG();
    esp_zb_ep_list_t *esp_zb_on_off_light_ep = esp_zb_on_off_light_ep_create(HA_ESP_LIGHT_ENDPOINT, &light_cfg);

    configure_custom_clusters(esp_zb_on_off_light_ep, NULL);

    esp_zb_device_register(esp_zb_on_off_light_ep);
    esp_zb_core_action_handler_register(zb_action_handler);
    esp_zb_set_primary_network_channel_set(ESP_ZB_PRIMARY_CHANNEL_MASK);
    ESP_ERROR_CHECK(esp_zb_start(false));
    esp_zb_main_loop_iteration();
}

void app_main(void)
{
    esp_zb_platform_config_t config = {
        .radio_config = ESP_ZB_DEFAULT_RADIO_CONFIG(),
        .host_config = ESP_ZB_DEFAULT_HOST_CONFIG(),
    };
    ESP_ERROR_CHECK(nvs_flash_init());
    ESP_ERROR_CHECK(esp_zb_platform_config(&config));
    xTaskCreate(esp_zb_task, "Zigbee_main", 4096, NULL, 5, NULL);
}

More Information.

No response

xieqinan commented 3 months ago

@TJvV-DT ,

The esp-zigbee-sdk v1.4.0 does not support the ZCL data model reuse. Please refer to comments.

Suggestion

    esp_err_t err = ESP_FAIL;

    TEST_LOG("Add client cluster");
    err = esp_zb_cluster_list_add_custom_cluster(esp_zb_cluster_list, create_attributes_list(), ESP_ZB_ZCL_CLUSTER_CLIENT_ROLE);
    if (err != ESP_OK)
    {
        TEST_LOG_LEVEL(ESP_LOG_ERROR, "Failed to add client cluster: %x", err);
        return err;
    }

    TEST_LOG("Add server cluster");
    err = esp_zb_cluster_list_add_custom_cluster(esp_zb_cluster_list, create_attributes_list(), ESP_ZB_ZCL_CLUSTER_SERVER_ROLE);
    if (err != ESP_OK)
    {
        TEST_LOG_LEVEL(ESP_LOG_ERROR, "Failed to add server cluster: %x", err);
        return err;
    }
TJvV-DT commented 3 months ago

It looks like this works. It would be nice to get clearer error messages when things like this happen.