It failed to run attributes set function in the timer callback handler.

Software version: nRF CONNECT SDK v2.4.2.  Project: Matter light bulb

I have created a soft timer, and defined a function named "autoAdjustPWMLevel" as callback handler, like the pic shows.

Everything is fine but when I want to set the attribute value and call this "Set" function, it run wrong even if it compile and build normally.

chip::app::Clusters::IlluminanceMeasurement::Attributes::MeasuredValue::Set()

  • I am not able to compile your project when I try to compile it similar to the light_bulb. 

    === child image mcuboot -  begin ===
    loading initial cache file E:/ncs/v2.4.2/nrf/samples/matter/Light_bulb-main/build/mcuboot/child_image_preload.cmake
    -- Application: E:/ncs/v2.4.2/bootloader/mcuboot/boot/zephyr
    -- CMake version: 3.20.5
    Loading Zephyr default modules (Zephyr base).
    -- Found Python3: E:/ncs/toolchains/v2.3.0/opt/bin/python.exe (found suitable exact version "3.8.2") found components: Interpreter 
    -- Cache files will be written to: E:/ncs/v2.4.2/zephyr/.cache
    -- Zephyr version: 3.3.99 (E:/ncs/v2.4.2/zephyr)
    -- Found west (found suitable version "0.14.0", minimum required is "0.7.1")
    -- Board: nrf52840dk_nrf52840
    -- Found host-tools: zephyr 0.15.2 (E:/ncs/toolchains/v2.3.0/opt/zephyr-sdk)
    -- Found toolchain: zephyr 0.15.2 (E:/ncs/toolchains/v2.3.0/opt/zephyr-sdk)
    -- Found Dtc: E:/ncs/toolchains/v2.3.0/opt/bin/dtc.exe (found suitable version "1.4.7", minimum required is "1.4.6") 
    -- Found BOARD.dts: E:/ncs/v2.4.2/zephyr/boards/arm/nrf52840dk_nrf52840/nrf52840dk_nrf52840.dts
    -- Found devicetree overlay: E:/ncs/v2.4.2/nrf/samples/matter/Light_bulb-main/child_image/mcuboot/boards/nrf52840dk_nrf52840.overlay
    -- Generated zephyr.dts: E:/ncs/v2.4.2/nrf/samples/matter/Light_bulb-main/build/mcuboot/zephyr/zephyr.dts
    -- Generated devicetree_generated.h: E:/ncs/v2.4.2/nrf/samples/matter/Light_bulb-main/build/mcuboot/zephyr/include/generated/devicetree_generated.h
    -- Including generated dts.cmake file: E:/ncs/v2.4.2/nrf/samples/matter/Light_bulb-main/build/mcuboot/zephyr/dts.cmake
    Parsing E:/ncs/v2.4.2/modules/lib/matter/config/nrfconnect/chip-module/Kconfig.mcuboot.root
    Loaded configuration 'E:/ncs/v2.4.2/zephyr/boards/arm/nrf52840dk_nrf52840/nrf52840dk_nrf52840_defconfig'
    Merged configuration 'E:/ncs/v2.4.2/nrf/samples/matter/Light_bulb-main/child_image/mcuboot/prj.conf'
    Merged configuration 'E:/ncs/v2.4.2/nrf/subsys/partition_manager/ext_flash_mcuboot_secondary.conf'
    Merged configuration 'E:/ncs/v2.4.2/nrf/subsys/partition_manager/partition_manager_enabled.conf'
    Merged configuration 'E:/ncs/v2.4.2/nrf/samples/matter/Light_bulb-main/build/mcuboot/zephyr/misc/generated/extra_kconfig_options.conf'
    Configuration saved to 'E:/ncs/v2.4.2/nrf/samples/matter/Light_bulb-main/build/mcuboot/zephyr/.config'
    Kconfig header saved to 'E:/ncs/v2.4.2/nrf/samples/matter/Light_bulb-main/build/mcuboot/zephyr/include/generated/autoconf.h'
    -- Found GnuLd: e:/ncs/toolchains/v2.3.0/opt/zephyr-sdk/arm-zephyr-eabi/bin/../lib/gcc/arm-zephyr-eabi/12.1.0/../../../../arm-zephyr-eabi/bin/ld.bfd.exe (found version "2.38") 
    -- The C compiler identification is GNU 12.1.0
    -- The CXX compiler identification is GNU 12.1.0
    -- The ASM compiler identification is GNU
    -- Found assembler: E:/ncs/toolchains/v2.3.0/opt/zephyr-sdk/arm-zephyr-eabi/bin/arm-zephyr-eabi-gcc.exe
    CMake Warning at E:/ncs/v2.4.2/nrf/lib/flash_patch/CMakeLists.txt:8 (message):
      
    
            ----------------------------------------------------------
            --- WARNING: To maintain the integrity of secure boot, ---
            --- enable CONFIG_DISABLE_FLASH_PATCH in production.   ---
            ----------------------------------------------------------
    
    
    CMake Warning at E:/ncs/v2.4.2/zephyr/CMakeLists.txt:839 (message):
      No SOURCES given to Zephyr library: lib__libc__common
    
      Excluding target from build.
    
    
    MCUBoot bootloader key file: E:/ncs/v2.4.2/bootloader/mcuboot/root-ec-p256.pem
    -- Configuring done
    -- Generating done
    -- Build files have been written to: E:/ncs/v2.4.2/nrf/samples/matter/Light_bulb-main/build/mcuboot
    === child image mcuboot -  end ===
    
    CMake Warning at E:/ncs/v2.4.2/nrf/modules/mcuboot/CMakeLists.txt:310 (message):
      
    
              ---------------------------------------------------------
              --- WARNING: Using default MCUBoot key, it should not ---
              --- be used for production.                           ---
              ---------------------------------------------------------
              
    
    
    
    CMake Warning at E:/ncs/v2.4.2/nrfxlib/openthread/cmake/extensions.cmake:113 (message):
       
       Current nrf_security configuration does not provide all MBEDTLS options which are required by precompiled OpenThread libraries.
       The minimum of MBEDTLS configuration is stored at: E:/ncs/v2.4.2/nrfxlib/openthread/lib/cortex-m4/hard-float/v1.3/ftd/oberon/nrf_security_mbedtls_configuration.h
    
    Call Stack (most recent call first):
      E:/ncs/v2.4.2/nrfxlib/openthread/CMakeLists.txt:82 (check_openthread_dependencies)
    
    
    CMake Warning at E:/ncs/v2.4.2/zephyr/CMakeLists.txt:839 (message):
      No SOURCES given to Zephyr library: lib__libc__common
    
      Excluding target from build.
    
    
    CMake Warning at E:/ncs/v2.4.2/zephyr/CMakeLists.txt:1865 (message):
      __ASSERT() statements are globally ENABLED
    
    
    -- Found partition manager static configuration: E:/ncs/v2.4.2/nrf/samples/matter/Light_bulb-main/configuration/nrf52840dk_nrf52840/pm_static_dfu.yml
    Partition 'mcuboot' is not included in the dynamic resolving since it is statically defined.
    Partition 'mcuboot_pad' is not included in the dynamic resolving since it is statically defined.
    Partition 'mcuboot_primary' is not included in the dynamic resolving since it is statically defined.
    Partition 'mcuboot_primary_app' is not included in the dynamic resolving since it is statically defined.
    Partition 'settings_storage' is not included in the dynamic resolving since it is statically defined.
    Partition 'mcuboot_secondary' is not included in the dynamic resolving since it is statically defined.
    -- Configuring done
    CMake Error at E:/ncs/v2.4.2/zephyr/cmake/modules/extensions.cmake:424 (add_library):
      Cannot find source file:
    
        //callback-stub.cpp
    Call Stack (most recent call first):
      E:/ncs/v2.4.2/zephyr/cmake/modules/kernel.cmake:210 (zephyr_library_named)
      E:/ncs/v2.4.2/zephyr/cmake/modules/zephyr_default.cmake:124 (include)
      E:/ncs/v2.4.2/zephyr/share/zephyr-package/cmake/ZephyrConfig.cmake:66 (include)
      E:/ncs/v2.4.2/zephyr/share/zephyr-package/cmake/ZephyrConfig.cmake:92 (include_boilerplate)
      CMakeLists.txt:30 (find_package)
    
    
    -- Generating done
    CMake Generate step failed.  Build files cannot be regenerated correctly.
    FATAL ERROR: command exited with status 1: 'E:\ncs\toolchains\v2.3.0\opt\bin\cmake.EXE' -DWEST_PYTHON=E:/ncs/toolchains/v2.3.0/opt/bin/python.exe '-Be:\ncs\v2.4.2\nrf\samples\matter\Light_bulb-main\build' -GNinja -DBOARD=nrf52840dk_nrf52840 -DNCS_TOOLCHAIN_VERSION:STRING=NONE -DBOARD_ROOT:STRING=e:/ncs/v2.4.2/nrf/samples/matter/light_bulb-main -DCONF_FILE:STRING=e:/ncs/v2.4.2/nrf/samples/matter/Light_bulb-main/prj.conf -DDTC_OVERLAY_FILE:STRING=e:/ncs/v2.4.2/nrf/samples/matter/Light_bulb-main/boards/nrf52840dk_nrf52840.overlay '-Se:\ncs\v2.4.2\nrf\samples\matter\Light_bulb-main'
    
     *  The terminal process terminated with exit code: 1. 
     *  Terminal will be reused by tasks, press any key to close it. 

  • Thank you for your answer!

    I can compile normally. It said "cannot find source file: callback-stub.cpp" in your logs.

    OK, let's forget my project. Maybe I didn't describe my problem very well. There is one thing I want to verify is that can the "Set()" function be used in a timer callback function like the pic shows? It seems not.

    7180.light_bulb.zip

  • The documentation does not specify that you cannot use it. If you are able to start your application in the debugger and step into this function, you can verify yourself if it goes low level in actually calling the protocol stack API to set the attribute. If there is no errors returned, then I do not know why the set function is not working as it should.

  • Thanks for your help all the time. I have figured out some problems, but therer are still some problems remaining.

    Now the situation is: I put the function chip::app::Clusters::IlluminanceMeasurement::Attributes::MeasuredValue::Set into a timer,which would update illuminance measurement's measured value per second, it seems to run normally. But when if I want to use the command chip-tool illuminancemeasurement read measured-value 2332 1 to read this attribute, its Matter progress seems to collapse totally.And the same problem will occur when I want to "ble-thread" pair the device to OTBR while this "Set Attribute" timer running.

  • Barry, 

    My colleague who is more experience with Matter said that you should use events in addition to timers as is done in the Adding clusters to Matter application guide (step 3). So their implementation should be more like this (I have not tested or verified this code, just based it on the guide + other samples and the customer's code)

    /*----------- src/app_event.h -----------*/
    
    enum class AppEventType {
         None = 0,
         Button,
         ButtonPushed,
         ButtonReleased,
         Timer,
         UpdateLedState,
         GLAppActivate,
         GLAppDeactivate,
         AdjustPWMLevel
    };
    
    /*----------- src/app_task.cpp -----------*/
    
    // Timers
    
    void GLApp::audoAdjustPWMLevel(k_timer *timer)
    {
        AppEvent event;
        event.Type = AppEventType::AdjustPWMLevel;
        event.Handler = AppTask::AdjustPWMLevelHandler;
        AppTask::Instance().PostEvent(event);
    }
    
    void StartGLAppTimer()
    {
        k_timer_start(&glAppTimer, K_MSEC(500), K_MSEC(500));
    }
    
    void StopGLAppTimer()
    {
        k_timer_stop(&glAppTimer);
    }
    
    void GLApp::initGLAppTimer()
    {
        k_timer_init(&glAppTimer, &GLApp::audoAdjustPWMLevel, nullptr);
        k_timer_user_data_set(&glAppTimer, this);
        k_timer_start(&glAppTimer, K_MSEC(500), K_MSEC(500));
    }
    
    
    int AppTask::Init()
    {
        /*
        ... Original content
        */
    
        k_timer_init(&glAppTimer, &GLApp::audoAdjustPWMLevel, nullptr);
        k_timer_user_data_set(&glAppTimer, this);
        return 0;
    }
    
    // Event handlers
    
    void AppTask::GLAppActivateHandler(const AppEvent &)
    {
        StartGLAppTimer();
    }
    
    void AppTask::GLAppDeactivateHandler(const AppEvent &)
    {
        StopGLAppTimer()
    }
    
    void AppTask::AdjustPWMLevelHandler(const AppEvent &)
    {
        chip::app::Clusters::IlluminanceMeasurement::Attributes::MeasuredValue::Set(1, testValue);
    }

    Not sure if this fixes the issue at hand, but this is suggested anyway to be implemented in your design.

Related