This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

CC1352P: Cannot get correct sequence number from APS ack

Part Number: CC1352P
Other Parts Discussed in Thread: Z-STACK

Hi all,

We edit "zed_switch" and try to get APS ack when device send toggle command. We can get sequence umber when sending command(0x0B, we also get this number from sniffer). But, transaction ID is different when we get APS ACK. Do we miss something?

SDK : simplelink_cc13x2_sdk_2_30_00_45

pic.1 send toggle

pic.2 sniffer

pic.3 APS ack

  • Can you help to verify this issue? It’s similar to what I had reported in Z-Stack 3.0.1.
  • Can you help us to check this issue?
  • Hi Kimi,

    I was not able to replicate the issue, the operation is as expected on my setup with the same SDK version.  Where do you define the pInd pointer as type zstackmsg_afDataConfirmInd_t if not in zstackmsg_CmdIDs_AF_DATA_CONFIRM_IND?

    Regards, Ryan

  • In zstackmsg_afDataConfirmInd_t, we only have req.transID and it doesn’t match zclFrameCounter that we set in zcl send command. How do we check the APS ack is from my specific zcl send command?
  • Hi Ryan,

    The definition is showing pic.1.

    I add zcl_OnOff_Options(pic.2) and register it in the end of the zclSampleSw_Init(pic.3). Do I miss something? Thanks

    pic.1

    pic.2

    pic.3

      

  • I moved my pInd initialization to the same location as yourself and still got matching sequence numbers. Are there any other changes to the example and can you provide the sniffer log? It is worth noting however that zstackmsg_CmdIDs_AF_DATA_CONFIRM_IND occurs due to the On/Off Default Response (enabled when the disableDefaultRsp parameter of zclGeneral_SendOnOff_CmdToggle is set to FALSE), not the APS ACK  either when the stack generates an AF data confirm for a frame (for frames that don't use APS ACK) or specifically only if an APS ACK indication arrives (if APS ACK is enabled). When comparing packets you may notice that the transaction sequence number is only present on the former instead of the latter. APS ACKs can be checked through AF_DATA_CONFIRM_CMD with timeout logic to check for a valid response.

    Regards,
    Ryan

  • Hi Ryan,

    I have two questions :

    Q1. Even follow your description, I cannot receive correct transID(it does not match with sequence number )

    Q2. My purpose is receive APS ACK in the Stack. How do I get APS_ACK packet(which callback) in the Stack?

    my file and sniffer are showing below(search "kimi edit")

    /**************************************************************************************************
      Filename:       zcl_samplesw.c
      Revised:        $Date: 2015-08-19 17:11:00 -0700 (Wed, 19 Aug 2015) $
      Revision:       $Revision: 44460 $
    
      Description:    Zigbee Cluster Library - sample switch application.
    
    
      Copyright 2006-2013 Texas Instruments Incorporated. All rights reserved.
    
      IMPORTANT: Your use of this Software is limited to those specific rights
      granted under the terms of a software license agreement between the user
      who downloaded the software, his/her employer (which must be your employer)
      and Texas Instruments Incorporated (the "License").  You may not use this
      Software unless you agree to abide by the terms of the License. The License
      limits your use, and you acknowledge, that the Software may not be modified,
      copied or distributed unless embedded on a Texas Instruments microcontroller
      or used solely and exclusively in conjunction with a Texas Instruments radio
      frequency transceiver, which is integrated into your product.  Other than for
      the foregoing purpose, you may not use, reproduce, copy, prepare derivative
      works of, modify, distribute, perform, display or sell this Software and/or
      its documentation for any purpose.
    
      YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE
      PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,
      INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE,
      NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL
      TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT,
      NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER
      LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
      INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE
      OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT
      OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
      (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
    
      Should you have any questions regarding your right to use this Software,
      contact Texas Instruments Incorporated at www.TI.com.
    **************************************************************************************************/
    
    /*********************************************************************
      This application implements a ZigBee On/Off Switch, based on Z-Stack 3.0.
    
      This application is based on the common sample-application user interface. Please see the main
      comment in zcl_sampleapp_ui.c. The rest of this comment describes only the content specific for
      this sample application.
      
      Application-specific UI peripherals being used:
    
      - none (LED1 is currently unused by this application).
    
      Application-specific menu system:
    
        <TOGGLE LIGHT> Send an On, Off or Toggle command targeting appropriate devices from the binding table.
          Pressing / releasing [OK] will have the following functionality, depending on the value of the 
          zclSampleSw_OnOffSwitchActions attribute:
          - OnOffSwitchActions == 0: pressing [OK] will send ON command, releasing it will send OFF command;
          - OnOffSwitchActions == 1: pressing [OK] will send OFF command, releasing it will send ON command;
          - OnOffSwitchActions == 2: pressing [OK] will send TOGGLE command, releasing it will not send any command.
    
    *********************************************************************/
    
    #if ! defined ZCL_ON_OFF
    #error ZCL_ON_OFF must be defined for this project.
    #endif
    
    /*********************************************************************
     * INCLUDES
     */
    #include "zcomdef.h"
    #include "zcl.h"
    #include "zcl_general.h"
    #include "zcl_ha.h"
    #include "on_board.h"
    #include "zcl_samplesw.h"
    #include "zcl_diagnostic.h"
    #include <string.h>
    
    #include "zcl_sampleapps_ui.h"
    #include "bdb_interface.h"
    
    #if defined (OTA_CLIENT_CC26XX)
    #include "zcl_ota.h"
    #include "ota_client_app.h"
    #include "bsp_spi.h"
    #endif
    
    #include "board_led.h"
    #include "board_key.h"
    #include "nvintf.h"
    #include "zstackmsg.h"
    #include "zcl_port.h"
    #include "znwk_config.h"
    #include <ti/sysbios/knl/Semaphore.h>
    #include "zstackapi.h"
    #include "zcl_sampleapps_ui.h"
    #include "util_timer.h"
    
    #if !defined (DISABLE_GREENPOWER_BASIC_PROXY) && (ZG_BUILD_RTR_TYPE)
    #include "gp_common.h"
    #endif
    
    #if defined ( BDB_TL_INITIATOR )
    #include "touchlink_initiator_app.h"
    #elif defined ( BDB_TL_TARGET )
    #include "touchlink_target_app.h"
    #endif
    
    #ifdef PER_TEST
    #include "per_test.h"
    #endif
    
    /*********************************************************************
     * MACROS
     */
    #define UI_STATE_TOGGLE_LIGHT 1 //UI_STATE_BACK_FROM_APP_MENU is item #0, so app item numbers should start from 1
    
    #define APP_TITLE "TI Sample Switch"
    
    /*********************************************************************
     * TYPEDEFS
     */
    
    /*********************************************************************
     * GLOBAL VARIABLES
     */
    
    
    uint8 zclSampleSwSeqNum;
    
    uint8 zclSampleSw_OnOffSwitchType = ON_OFF_SWITCH_TYPE_MOMENTARY;
    
    uint8 zclSampleSw_OnOffSwitchActions;
    
    /*********************************************************************
     * GLOBAL FUNCTIONS
     */
    
    /*********************************************************************
     * LOCAL VARIABLES
     */
    // Semaphore used to post events to the application thread
    static ICall_Semaphore sem;
    static ICall_EntityID  zclSampleSw_Entity;
    static endPointDesc_t  zclSampleSwEpDesc = {0};
    
    #if ZG_BUILD_ENDDEVICE_TYPE
    static Clock_Handle EndDeviceRejoinClkHandle;
    static Clock_Struct EndDeviceRejoinClkStruct;
    #endif
    
    // Passed in function pointers to the NV driver
    static NVINTF_nvFuncts_t *pfnZdlNV = NULL;
    
    // Key press parameters
    static uint8_t keys;
    
    // Task pending events
    static uint16_t events = 0;
    
    afAddrType_t zclSampleSw_DstAddr;
    
    
    #define SAMPLESW_TOGGLE_TEST_EVT   0x1000
    
    //kimi edit
    static zclOptionRec_t zcl_OnOff_Options[] =
    {
      {
        ZCL_CLUSTER_ID_GEN_ON_OFF,
        ( AF_ACK_REQUEST ),
      },
    };
    
    uint8_t beforeSend;
    uint8_t afterSend;
    uint8_t test1;
    //kimi edit
    /*********************************************************************
     * LOCAL FUNCTIONS
     */
    static void zclSampleSw_initialization(void);
    static void zclSampleSw_process_loop(void);
    static void zclSampleSw_initParameters(void);
    static void zclSampleSw_processZStackMsgs(zstackmsg_genericReq_t *pMsg);
    static void SetupZStackCallbacks(void);
    static void zclSampleSw_processAfIncomingMsgInd(zstack_afIncomingMsgInd_t *pInMsg);
    static void zclSampleSw_initializeClocks(void);
    #if ZG_BUILD_ENDDEVICE_TYPE
    static void zclSampleSw_processEndDeviceRejoinTimeoutCallback(UArg a0);
    #endif
    static void zclSampleSw_processKey(uint8 keysPressed);
    static void zclSampleSw_changeKeyCallback(uint8_t keysPressed);
    static void zclSampleSw_Init( void );
    
    static void zclSampleSw_BasicResetCB( void );
    
    static void zclSampleSw_ProcessCommissioningStatus(bdbCommissioningModeMsg_t *bdbCommissioningModeMsg);
    
    void zclSampleSw_UiActionToggleLight(uint16 keys);
    void zclSampleSw_UiRefresh(uint8 uiCurrentState, char * line[3]);
    
    // Functions to process ZCL Foundation incoming Command/Response messages
    static uint8 zclSampleSw_ProcessIncomingMsg( zclIncoming_t *msg );
    #ifdef ZCL_READ
    static uint8 zclSampleSw_ProcessInReadRspCmd( zclIncoming_t *pInMsg );
    #endif
    #ifdef ZCL_WRITE
    static uint8 zclSampleSw_ProcessInWriteRspCmd( zclIncoming_t *pInMsg );
    #endif
    static uint8 zclSampleSw_ProcessInDefaultRspCmd( zclIncoming_t *pInMsg );
    #ifdef ZCL_DISCOVER
    static uint8 zclSampleSw_ProcessInDiscCmdsRspCmd( zclIncoming_t *pInMsg );
    static uint8 zclSampleSw_ProcessInDiscAttrsRspCmd( zclIncoming_t *pInMsg );
    static uint8 zclSampleSw_ProcessInDiscAttrsExtRspCmd( zclIncoming_t *pInMsg );
    #endif
    
    
    /*********************************************************************
     * CONSTANTS
     */
      const uiState_t zclSampleSw_UiStatesMain[] = 
      {
        /*  UI_STATE_BACK_FROM_APP_MENU  */   {UI_STATE_DEFAULT_MOVE,       UI_STATE_TOGGLE_LIGHT,  UI_KEY_SW_5_PRESSED, &UI_ActionBackFromAppMenu}, //do not change this line, except for the second item, which should point to the last entry in this menu
        /*  UI_STATE_TOGGLE_LIGHT        */   {UI_STATE_BACK_FROM_APP_MENU, UI_STATE_DEFAULT_MOVE,  UI_KEY_SW_5_PRESSED, &zclSampleSw_UiActionToggleLight},
      };
      
    /*********************************************************************
     * REFERENCED EXTERNALS
     */
    extern int16 zdpExternalStateTaskID;
    
    /*********************************************************************
     * ZCL General Profile Callback table
     */
    static zclGeneral_AppCallbacks_t zclSampleSw_CmdCallbacks =
    {
      zclSampleSw_BasicResetCB,               // Basic Cluster Reset command
      NULL,                                   // Identify Trigger Effect command
      NULL,                                   // On/Off cluster commands
      NULL,                                   // On/Off cluster enhanced command Off with Effect
      NULL,                                   // On/Off cluster enhanced command On with Recall Global Scene
      NULL,                                   // On/Off cluster enhanced command On with Timed Off
    #ifdef ZCL_LEVEL_CTRL
      NULL,                                   // Level Control Move to Level command
      NULL,                                   // Level Control Move command
      NULL,                                   // Level Control Step command
      NULL,                                   // Level Control Stop command
    #endif
    #ifdef ZCL_GROUPS
      NULL,                                   // Group Response commands
    #endif
    #ifdef ZCL_SCENES
      NULL,                                   // Scene Store Request command
      NULL,                                   // Scene Recall Request command
      NULL,                                   // Scene Response command
    #endif
    #ifdef ZCL_ALARMS
      NULL,                                   // Alarm (Response) commands
    #endif
    #ifdef SE_UK_EXT
      NULL,                                   // Get Event Log command
      NULL,                                   // Publish Event Log command
    #endif
      NULL,                                   // RSSI Location command
      NULL                                    // RSSI Location Response command
    };
    
    
    /*********************************************************************
     * @fn          zclSampleSw_task
     *
     * @brief       Application task entry point for the ZStack Sample Switch
     *              Application
     *
     * @param       pfnNV - pointer to the NV functions
     *
     * @return      none
     */
    
    void zclSampleSw_task(NVINTF_nvFuncts_t *pfnNV)
    {
      // Save and register the function pointers to the NV drivers
      pfnZdlNV = pfnNV;
      zclport_registerNV(pfnZdlNV, ZCL_PORT_SCENE_TABLE_NV_ID);
    
      // Initialize application
      zclSampleSw_initialization();
    
      // No return from task process
      zclSampleSw_process_loop();
    }
    
    
    
    
    /*******************************************************************************
     * @fn          zclSampleSw_initialization
     *
     * @brief       Initialize the application
     *
     * @param       none
     *
     * @return      none
     */
    static void zclSampleSw_initialization(void)
    {
        uint8 key;
    
        /* Initialize user clocks */
        zclSampleSw_initializeClocks();
    
        /* Initialize keys */
        key = Board_Key_initialize(zclSampleSw_changeKeyCallback);
    
    
        /* Initialize the LEDS */
        Board_Led_initialize();
    
        // Register the current thread as an ICall dispatcher application
        // so that the application can send and receive messages.
        ICall_registerApp(&zclSampleSw_Entity, &sem);
    
        if(key == KEY_RIGHT)
        {
            Zstackapi_bdbResetLocalActionReq(zclSampleSw_Entity);
    
    #if defined (OTA_CLIENT_CC26XX)
            //Return to FN image
            OTA_loadExtImage(ST_FULL_FACTORY_IMAGE);
    #endif
        }
        
        //Initialize stack
        zclSampleSw_Init();
    
        
    #if defined (OTA_CLIENT_CC26XX)
        OTAClient_SetEndpoint(SAMPLESW_ENDPOINT);
        OTA_Client_Init ( sem, zclSampleSw_Entity);
    #endif
    
        
    }
    
    
    
    /*********************************************************************
     * @fn          zclSampleSw_Init
     *
     * @brief       Initialization function for the zclGeneral layer.
     *
     * @param       none
     *
     * @return      none
     */
    static void zclSampleSw_Init( void )
    {
    
      // Set destination address to indirect
      zclSampleSw_DstAddr.addrMode = (afAddrMode_t)AddrNotPresent;
      zclSampleSw_DstAddr.endPoint = 0;
      zclSampleSw_DstAddr.addr.shortAddr = 0;
    
      //Register Endpoint
      zclSampleSwEpDesc.endPoint = SAMPLESW_ENDPOINT;
      zclSampleSwEpDesc.simpleDesc = &zclSampleSw_SimpleDesc;
      zclport_registerEndpoint(zclSampleSw_Entity, &zclSampleSwEpDesc);
    
      // Register the ZCL General Cluster Library callback functions
      zclGeneral_RegisterCmdCallbacks( SAMPLESW_ENDPOINT, &zclSampleSw_CmdCallbacks );
    
      // Register the application's attribute list and reset to default values
      zclSampleSw_ResetAttributesToDefaultValues();
      zcl_registerAttrList( SAMPLESW_ENDPOINT, zclSampleSw_NumAttributes, zclSampleSw_Attrs );
    
      // Register the Application to receive the unprocessed Foundation command/response messages
      zclport_registerZclHandleExternal(zclSampleSw_ProcessIncomingMsg);
    
      //Write the bdb initialization parameters
      zclSampleSw_initParameters();
    
      //Setup ZDO callbacks
      SetupZStackCallbacks();
    
    #if !defined (DISABLE_GREENPOWER_BASIC_PROXY) && (ZG_BUILD_RTR_TYPE)
      gp_endpointInit(zclSampleSw_Entity);
    #endif
    
    #ifdef ZCL_DIAGNOSTIC
      // Register the application's callback function to read/write attribute data.
      // This is only required when the attribute data format is unknown to ZCL.
      zcl_registerReadWriteCB( SAMPLESW_ENDPOINT, zclDiagnostic_ReadWriteAttrCB, NULL );
    
      if ( zclDiagnostic_InitStats() == ZSuccess )
      {
        // Here the user could start the timer to save Diagnostics to NV
      }
    #endif
    
    
    #ifdef ZCL_DIAGNOSTIC
      // Register the application's callback function to read/write attribute data.
      // This is only required when the attribute data format is unknown to ZCL.
      zcl_registerReadWriteCB( SAMPLESW_ENDPOINT, zclDiagnostic_ReadWriteAttrCB, NULL );
    
      if ( zclDiagnostic_InitStats() == ZSuccess )
      {
        // Here the user could start the timer to save Diagnostics to NV
      }
    #endif
    
    #ifdef PER_TEST
      UI_Init(zclSampleSw_Entity, &events, sem, SAMPLEAPP_UI_AUTO_REFRESH_EVT, SAMPLEAPP_UI_INPUT_EVT, &zclSampleSw_IdentifyTime, APP_TITLE, &PERTest_UiRefresh, PERTest_UiStatesMain, NULL);
    #else
      UI_Init(zclSampleSw_Entity, &events, sem, SAMPLEAPP_UI_AUTO_REFRESH_EVT, SAMPLEAPP_UI_INPUT_EVT, &zclSampleSw_IdentifyTime, APP_TITLE, &zclSampleSw_UiRefresh, zclSampleSw_UiStatesMain, NULL);
    #endif
    
    #if defined ( BDB_TL_INITIATOR )
        touchLinkInitiatorApp_Init(zclSampleSw_Entity);
    #elif defined ( BDB_TL_TARGET )
        touchLinkTargetApp_Init(zclSampleSw_Entity);  
    #endif
      
    #if !defined (DISABLE_GREENPOWER_BASIC_PROXY) && (ZG_BUILD_RTR_TYPE)
      app_Green_Power_Init(zclSampleSw_Entity, &events, sem, SAMPLEAPP_PROCESS_GP_DATA_SEND_EVT, 
                           SAMPLEAPP_PROCESS_GP_EXPIRE_DUPLICATE_EVT, SAMPLEAPP_PROCESS_GP_TEMP_MASTER_EVT);
    #endif
    
    #ifdef PER_TEST
      per_interface_init(sem, zclSampleSw_Entity, SAMPLESW_ENDPOINT );
    #endif
    //kimi edit
     zcl_registerClusterOptionList(SAMPLESW_ENDPOINT, 1, zcl_OnOff_Options);
    //kimi edit
    }
    
    static void zclSampleSw_initParameters(void)
    {
        zstack_bdbSetAttributesReq_t zstack_bdbSetAttrReq;
    
        zstack_bdbSetAttrReq.bdbCommissioningGroupID              = BDB_DEFAULT_COMMISSIONING_GROUP_ID;
        zstack_bdbSetAttrReq.bdbPrimaryChannelSet                 = BDB_DEFAULT_PRIMARY_CHANNEL_SET;
        zstack_bdbSetAttrReq.bdbScanDuration                      = BDB_DEFAULT_SCAN_DURATION;
        zstack_bdbSetAttrReq.bdbSecondaryChannelSet               = BDB_DEFAULT_SECONDARY_CHANNEL_SET;
        zstack_bdbSetAttrReq.has_bdbCommissioningGroupID          = TRUE;
        zstack_bdbSetAttrReq.has_bdbPrimaryChannelSet             = TRUE;
        zstack_bdbSetAttrReq.has_bdbScanDuration                  = TRUE;
        zstack_bdbSetAttrReq.has_bdbSecondaryChannelSet           = TRUE;
    #if (ZG_BUILD_COORDINATOR_TYPE)
        zstack_bdbSetAttrReq.has_bdbJoinUsesInstallCodeKey        = TRUE;
        zstack_bdbSetAttrReq.has_bdbTrustCenterNodeJoinTimeout    = TRUE;
        zstack_bdbSetAttrReq.has_bdbTrustCenterRequireKeyExchange = TRUE;
        zstack_bdbSetAttrReq.bdbJoinUsesInstallCodeKey            = BDB_DEFAULT_JOIN_USES_INSTALL_CODE_KEY;
        zstack_bdbSetAttrReq.bdbTrustCenterNodeJoinTimeout        = BDB_DEFAULT_TC_NODE_JOIN_TIMEOUT;
        zstack_bdbSetAttrReq.bdbTrustCenterRequireKeyExchange     = BDB_DEFAULT_TC_REQUIRE_KEY_EXCHANGE;
    #endif
    #if (ZG_BUILD_JOINING_TYPE)
        zstack_bdbSetAttrReq.has_bdbTCLinkKeyExchangeAttemptsMax  = TRUE;
        zstack_bdbSetAttrReq.has_bdbTCLinkKeyExchangeMethod       = TRUE;
        zstack_bdbSetAttrReq.bdbTCLinkKeyExchangeAttemptsMax      = BDB_DEFAULT_TC_LINK_KEY_EXCHANGE_ATTEMPS_MAX;
        zstack_bdbSetAttrReq.bdbTCLinkKeyExchangeMethod           = BDB_DEFAULT_TC_LINK_KEY_EXCHANGE_METHOD;
    #endif
    
        Zstackapi_bdbSetAttributesReq(zclSampleSw_Entity, &zstack_bdbSetAttrReq);
    }
    
    /*******************************************************************************
     * @fn      zclSampleSw_initializeClocks
     *
     * @brief   Initialize Clocks
     *
     * @param   none
     *
     * @return  none
     */
    static void zclSampleSw_initializeClocks(void)
    {
    #if ZG_BUILD_ENDDEVICE_TYPE
        // Initialize the timers needed for this application
        EndDeviceRejoinClkHandle = Timer_construct(
        &EndDeviceRejoinClkStruct,
        zclSampleSw_processEndDeviceRejoinTimeoutCallback,
        SAMPLEAPP_END_DEVICE_REJOIN_DELAY,
        0, false, 0);
    #endif
    
    }
    
    #if ZG_BUILD_ENDDEVICE_TYPE
    /*******************************************************************************
     * @fn      zclSampleSw_processEndDeviceRejoinTimeoutCallback
     *
     * @brief   Timeout handler function
     *
     * @param   a0 - ignored
     *
     * @return  none
     */
    static void zclSampleSw_processEndDeviceRejoinTimeoutCallback(UArg a0)
    {
        (void)a0; // Parameter is not used
    
        events |= SAMPLEAPP_END_DEVICE_REJOIN_EVT;
    
        // Wake up the application thread when it waits for clock event
        Semaphore_post(sem);
    }
    #endif
    
    /*******************************************************************************
     * @fn      SetupZStackCallbacks
     *
     * @brief   Setup the Zstack Callbacks wanted
     *
     * @param   none
     *
     * @return  none
     */
    static void SetupZStackCallbacks(void)
    {
        zstack_devZDOCBReq_t zdoCBReq = {0};
    
        // Register for Callbacks, turn on:
        //  Device State Change,
        //  ZDO Match Descriptor Response,
        zdoCBReq.has_devStateChange = true;
        zdoCBReq.devStateChange = true;
    
    #if defined(OTA_CLIENT_CC26XX)
        zdoCBReq.has_matchDescRsp = true;
        zdoCBReq.matchDescRsp = true;
        zdoCBReq.has_ieeeAddrRsp = true;
        zdoCBReq.ieeeAddrRsp = true;
    #endif
    
        (void)Zstackapi_DevZDOCBReq(zclSampleSw_Entity, &zdoCBReq);
    }
    
    
    
    /*******************************************************************************
     * @fn      zclSampleSw_process_loop
     *
     * @brief   Application task processing start.
     *
     * @param   none
     *
     * @return  void
     */
    static void zclSampleSw_process_loop(void)
    {
        /* Forever loop */
        for(;;)
        {
            ICall_ServiceEnum stackid;
            ICall_EntityID dest;
            zstackmsg_genericReq_t *pMsg = NULL;
    
            /* Wait for response message */
            if(ICall_wait(ICALL_TIMEOUT_FOREVER) == ICALL_ERRNO_SUCCESS)
            {
                /* Retrieve the response message */
                if(ICall_fetchServiceMsg(&stackid, &dest, (void **)&pMsg)
                   == ICALL_ERRNO_SUCCESS)
                {
                    if( (stackid == ICALL_SERVICE_CLASS_ZSTACK)
                        && (dest == zclSampleSw_Entity) )
                    {
                        if(pMsg)
                        {
                            zclSampleSw_processZStackMsgs(pMsg);
    #ifdef PER_TEST
                            per_interface_processZStackMsg(pMsg);
    #endif
                            // Free any separately allocated memory
                            Zstackapi_freeIndMsg(pMsg);
                        }
                    }
    
                    if(pMsg)
                    {
                        ICall_freeMsg(pMsg);
                    }
                }
    
    #ifdef PER_TEST
                per_interface_process();
    #endif
    #if defined (OTA_CLIENT_CC26XX)
                zclOTA_event_loop(); 
    #endif
    
    
                if(events & SAMPLEAPP_KEY_EVT)
                {
                    // Process Key Presses
                    zclSampleSw_processKey(keys);
                    keys = 0;
                    events &= ~SAMPLEAPP_KEY_EVT;
                }
    
                if(events & SAMPLEAPP_UI_AUTO_REFRESH_EVT)
                {
                    UI_Refresh();
                    events &= ~SAMPLEAPP_UI_AUTO_REFRESH_EVT;
                }
    
                if(events & SAMPLEAPP_UI_INPUT_EVT)
                {
                    zclSampleAppsUI_ProcessUART();
                    events &= ~SAMPLEAPP_UI_INPUT_EVT;
                }
    
    #if !defined (DISABLE_GREENPOWER_BASIC_PROXY) && (ZG_BUILD_RTR_TYPE)
                if(events & SAMPLEAPP_PROCESS_GP_DATA_SEND_EVT)
                {
                    zcl_gpSendNotification();
                    events &= ~SAMPLEAPP_PROCESS_GP_DATA_SEND_EVT;
                }
    
                if(events & SAMPLEAPP_PROCESS_GP_EXPIRE_DUPLICATE_EVT)
                {
                    gp_expireDuplicateFiltering();
                    events &= ~SAMPLEAPP_PROCESS_GP_EXPIRE_DUPLICATE_EVT;
                }
    #endif
    
    #if ZG_BUILD_ENDDEVICE_TYPE
                if ( events & SAMPLEAPP_END_DEVICE_REJOIN_EVT )
                {
                  zstack_bdbZedAttemptRecoverNwkRsp_t zstack_bdbZedAttemptRecoverNwkRsp;
    
                  Zstackapi_bdbZedAttemptRecoverNwkReq(zclSampleSw_Entity,&zstack_bdbZedAttemptRecoverNwkRsp);
    
                  events &= ~SAMPLEAPP_END_DEVICE_REJOIN_EVT;
                }
    #endif
    
    
            }
        }
    }
    
    
    
    
    /*******************************************************************************
     * @fn      zclSampleSw_processZStackMsgs
     *
     * @brief   Process event from Stack
     *
     * @param   pMsg - pointer to incoming ZStack message to process
     *
     * @return  void
     */
    static void zclSampleSw_processZStackMsgs(zstackmsg_genericReq_t *pMsg)
    {
          switch(pMsg->hdr.event)
          {
              case zstackmsg_CmdIDs_BDB_NOTIFICATION:
                  {
                      zstackmsg_bdbNotificationInd_t *pInd;
                      pInd = (zstackmsg_bdbNotificationInd_t*)pMsg;
                      zclSampleSw_ProcessCommissioningStatus(&(pInd->Req));
                  }
                  break;
                  
              case zstackmsg_CmdIDs_BDB_IDENTIFY_TIME_CB:
                  {
                      zstackmsg_bdbIdentifyTimeoutInd_t *pInd;
                      pInd = (zstackmsg_bdbIdentifyTimeoutInd_t*) pMsg;
                      uiProcessIdentifyTimeChange(&(pInd->EndPoint));
                  }
                  break;
                  
              case zstackmsg_CmdIDs_BDB_BIND_NOTIFICATION_CB:
                  {
                      zstackmsg_bdbBindNotificationInd_t *pInd;
                      pInd = (zstackmsg_bdbBindNotificationInd_t*) pMsg;
                      uiProcessBindNotification(&(pInd->Req));
                  }
                  break;
      
              case zstackmsg_CmdIDs_AF_INCOMING_MSG_IND:
                  {
                      // Process incoming data messages
                      zstackmsg_afIncomingMsgInd_t *pInd;
                      pInd = (zstackmsg_afIncomingMsgInd_t *)pMsg;
                      zclSampleSw_processAfIncomingMsgInd( &(pInd->req) );
                  }
                  break;
    //kimi edit
              case zstackmsg_CmdIDs_AF_DATA_CONFIRM_IND:
                  {
                      zstackmsg_afDataConfirmInd_t *pInd;
                      pInd = (zstackmsg_afDataConfirmInd_t *)pMsg;
                      test1 = pInd->req.transID;
                      test1 = pInd->req.endpoint;
                  }
                  break;
    //kimi edit
              case zstackmsg_CmdIDs_DEV_PERMIT_JOIN_IND:
                  {
                      zstackmsg_devPermitJoinInd_t *pInd;
                      pInd = (zstackmsg_devPermitJoinInd_t*)pMsg;
                      uiProcessPermitJoin(&(pInd->Req));
                  }
                  break;
      
                  
    #if (ZG_BUILD_JOINING_TYPE)
              case zstackmsg_CmdIDs_BDB_CBKE_TC_LINK_KEY_EXCHANGE_IND:
              {
                zstack_bdbCBKETCLinkKeyExchangeAttemptReq_t zstack_bdbCBKETCLinkKeyExchangeAttemptReq;
                /* Z3.0 has not defined CBKE yet, so lets attempt default TC Link Key exchange procedure
                 * by reporting CBKE failure.
                 */
    
                zstack_bdbCBKETCLinkKeyExchangeAttemptReq.didSuccess = FALSE;
      
                Zstackapi_bdbCBKETCLinkKeyExchangeAttemptReq(zclSampleSw_Entity,
                                                             &zstack_bdbCBKETCLinkKeyExchangeAttemptReq);
              }
              break;
      
              case zstackmsg_CmdIDs_BDB_FILTER_NWK_DESCRIPTOR_IND:
      
               /*   User logic to remove networks that do not want to join
                *   Networks to be removed can be released with Zstackapi_bdbNwkDescFreeReq
                */
             
                Zstackapi_bdbFilterNwkDescComplete(zclSampleSw_Entity);
              break;
                  
    #endif
              case zstackmsg_CmdIDs_DEV_STATE_CHANGE_IND:
              {
                  // The ZStack Thread is indicating a State change
                  zstackmsg_devStateChangeInd_t *pInd =
                      (zstackmsg_devStateChangeInd_t *)pMsg;
                        UI_DeviceStateUpdated(&(pInd->req));
              }
              break;
      
      
      
              /*
               * These are messages/indications from ZStack that this
               * application doesn't process.  These message can be
               * processed by your application, remove from this list and
               * process them here in this switch statement.
               */
      
    #if !defined (DISABLE_GREENPOWER_BASIC_PROXY) && (ZG_BUILD_RTR_TYPE)
              case zstackmsg_CmdIDs_GP_DATA_IND:
              {
                  zstackmsg_gpDataInd_t *pInd;
                  pInd = (zstackmsg_gpDataInd_t*)pMsg;
                  gp_processDataIndMsg( &(pInd->Req) );
              }
              break;
    
              case zstackmsg_CmdIDs_GP_SECURITY_REQ:
              {
                  zstackmsg_gpSecReq_t *pInd;
                  pInd = (zstackmsg_gpSecReq_t*)pMsg;
                  gp_processSecRecMsg( &(pInd->Req) );
              }
              break;
    
              case zstackmsg_CmdIDs_GP_CHECK_ANNCE:
              {
                  zstackmsg_gpCheckAnnounce_t *pInd;
                  pInd = (zstackmsg_gpCheckAnnounce_t*)pMsg;
                  gp_processCheckAnnceMsg( &(pInd->Req) );
              }
              break;
    
    #ifdef BOARD_DISPLAY_USE_UART
              case zstackmsg_CmdIDs_GP_COMMISSIONING_MODE_IND:
              {
                zstackmsg_gpCommissioningModeInd_t *pInd;
                pInd = (zstackmsg_gpCommissioningModeInd_t*)pMsg;
                UI_SetGPPCommissioningMode( &(pInd->Req) );
              }
              break;
    #endif
    #endif
               
    #ifdef BDB_TL_TARGET
              case zstackmsg_CmdIDs_BDB_TOUCHLINK_TARGET_ENABLE_IND:
              {
                zstackmsg_bdbTouchLinkTargetEnableInd_t *pInd = 
                  (zstackmsg_bdbTouchLinkTargetEnableInd_t*)pMsg;
              
                uiProcessTouchlinkTargetEnable(pInd->Enable);
              }
              break;
    #endif
    
    #if defined (OTA_CLIENT_CC26XX)
              case zstackmsg_CmdIDs_ZDO_MATCH_DESC_RSP:
              {
                zstackmsg_zdoMatchDescRspInd_t  *pInd =
                   (zstackmsg_zdoMatchDescRspInd_t *) pMsg;
    
                ZDO_MatchDescRsp_t              *pRsp;
    
                /*
                 * Parse the Match Descriptor Response and give it
                 * to the ZCL EZMode module t+o process
                 */
                pRsp = (ZDO_MatchDescRsp_t *) ICall_allocMsg(
        		       sizeof(ZDO_MatchDescRsp_t) + pInd->rsp.n_matchList);
                if(pRsp)
                {
                  pRsp->status = pInd->rsp.status;
                  pRsp->nwkAddr = pInd->rsp.nwkAddrOfInterest;
                  pRsp->cnt = pInd->rsp.n_matchList;
                  memcpy(pRsp->epList, pInd->rsp.pMatchList, pInd->rsp.n_matchList);
                  
                  OTA_ProcessMatchDescRsp ( pRsp );
                  ICall_freeMsg(pRsp);
                }
              }
              break;
    #endif
    
              case zstackmsg_CmdIDs_BDB_TC_LINK_KEY_EXCHANGE_NOTIFICATION_IND:
    //kimi edit
    //          case zstackmsg_CmdIDs_AF_DATA_CONFIRM_IND:
    //kimi edit
              case zstackmsg_CmdIDs_ZDO_DEVICE_ANNOUNCE:
              case zstackmsg_CmdIDs_ZDO_NWK_ADDR_RSP:
              case zstackmsg_CmdIDs_ZDO_IEEE_ADDR_RSP:
              case zstackmsg_CmdIDs_ZDO_NODE_DESC_RSP:
              case zstackmsg_CmdIDs_ZDO_POWER_DESC_RSP:
              case zstackmsg_CmdIDs_ZDO_SIMPLE_DESC_RSP:
              case zstackmsg_CmdIDs_ZDO_ACTIVE_EP_RSP:
              case zstackmsg_CmdIDs_ZDO_COMPLEX_DESC_RSP:
              case zstackmsg_CmdIDs_ZDO_USER_DESC_RSP:
              case zstackmsg_CmdIDs_ZDO_USER_DESC_SET_RSP:
              case zstackmsg_CmdIDs_ZDO_SERVER_DISC_RSP:
              case zstackmsg_CmdIDs_ZDO_END_DEVICE_BIND_RSP:
              case zstackmsg_CmdIDs_ZDO_BIND_RSP:
              case zstackmsg_CmdIDs_ZDO_UNBIND_RSP:
              case zstackmsg_CmdIDs_ZDO_MGMT_NWK_DISC_RSP:
              case zstackmsg_CmdIDs_ZDO_MGMT_LQI_RSP:
              case zstackmsg_CmdIDs_ZDO_MGMT_RTG_RSP:
              case zstackmsg_CmdIDs_ZDO_MGMT_BIND_RSP:
              case zstackmsg_CmdIDs_ZDO_MGMT_LEAVE_RSP:
              case zstackmsg_CmdIDs_ZDO_MGMT_DIRECT_JOIN_RSP:
              case zstackmsg_CmdIDs_ZDO_MGMT_PERMIT_JOIN_RSP:
              case zstackmsg_CmdIDs_ZDO_MGMT_NWK_UPDATE_NOTIFY:
              case zstackmsg_CmdIDs_ZDO_SRC_RTG_IND:
              case zstackmsg_CmdIDs_ZDO_CONCENTRATOR_IND:
              case zstackmsg_CmdIDs_ZDO_LEAVE_CNF:
              case zstackmsg_CmdIDs_ZDO_LEAVE_IND:
              case zstackmsg_CmdIDs_SYS_RESET_IND:
              case zstackmsg_CmdIDs_AF_REFLECT_ERROR_IND:
              case zstackmsg_CmdIDs_ZDO_TC_DEVICE_IND:
                  break;
      
              default:
                  break;
          }
    
    }
    
    
    
    /*******************************************************************************
     *
     * @fn          zclSampleSw_processAfIncomingMsgInd
     *
     * @brief       Process AF Incoming Message Indication message
     *
     * @param       pInMsg - pointer to incoming message
     *
     * @return      none
     *
     */
    static void zclSampleSw_processAfIncomingMsgInd(zstack_afIncomingMsgInd_t *pInMsg)
    {
        afIncomingMSGPacket_t afMsg;
    
        /*
         * All incoming messages are passed to the ZCL message processor,
         * first convert to a structure that ZCL can process.
         */
        afMsg.groupId = pInMsg->groupID;
        afMsg.clusterId = pInMsg->clusterId;
        afMsg.srcAddr.endPoint = pInMsg->srcAddr.endpoint;
        afMsg.srcAddr.panId = pInMsg->srcAddr.panID;
        afMsg.srcAddr.addrMode = (afAddrMode_t)pInMsg->srcAddr.addrMode;
        if( (afMsg.srcAddr.addrMode == afAddr16Bit)
            || (afMsg.srcAddr.addrMode == afAddrGroup)
            || (afMsg.srcAddr.addrMode == afAddrBroadcast) )
        {
            afMsg.srcAddr.addr.shortAddr = pInMsg->srcAddr.addr.shortAddr;
        }
        else if(afMsg.srcAddr.addrMode == afAddr64Bit)
        {
            memcpy(afMsg.srcAddr.addr.extAddr, &(pInMsg->srcAddr.addr.extAddr), 8);
        }
        afMsg.macDestAddr = pInMsg->macDestAddr;
        afMsg.endPoint = pInMsg->endpoint;
        afMsg.wasBroadcast = pInMsg->wasBroadcast;
        afMsg.LinkQuality = pInMsg->linkQuality;
        afMsg.correlation = pInMsg->correlation;
        afMsg.rssi = pInMsg->rssi;
        afMsg.SecurityUse = pInMsg->securityUse;
        afMsg.timestamp = pInMsg->timestamp;
        afMsg.nwkSeqNum = pInMsg->nwkSeqNum;
        afMsg.macSrcAddr = pInMsg->macSrcAddr;
        afMsg.radius = pInMsg->radius;
        afMsg.cmd.TransSeqNumber = pInMsg->transSeqNum;
        afMsg.cmd.DataLength = pInMsg->n_payload;
        afMsg.cmd.Data = pInMsg->pPayload;
    
        zcl_ProcessMessageMSG(&afMsg);
    }
    
    
    /*********************************************************************
     * @fn      zclSampleSw_ProcessCommissioningStatus
     *
     * @brief   Callback in which the status of the commissioning process are reported
     *
     * @param   bdbCommissioningModeMsg - Context message of the status of a commissioning process
     *
     * @return  none
     */
    static void zclSampleSw_ProcessCommissioningStatus(bdbCommissioningModeMsg_t *bdbCommissioningModeMsg)
    {
      switch(bdbCommissioningModeMsg->bdbCommissioningMode)
      {
        case BDB_COMMISSIONING_FORMATION:
          if(bdbCommissioningModeMsg->bdbCommissioningStatus == BDB_COMMISSIONING_SUCCESS)
          {
            zstack_bdbStartCommissioningReq_t zstack_bdbStartCommissioningReq;
            
            //After formation, perform nwk steering again plus the remaining commissioning modes that has not been process yet        
            zstack_bdbStartCommissioningReq.commissioning_mode = BDB_COMMISSIONING_MODE_NWK_STEERING | bdbCommissioningModeMsg->bdbRemainingCommissioningModes;
            Zstackapi_bdbStartCommissioningReq(zclSampleSw_Entity,&zstack_bdbStartCommissioningReq);
          }
          else
          {
            //Want to try other channels?
            //try with bdb_setChannelAttribute
          }
        break;
        case BDB_COMMISSIONING_NWK_STEERING:
          if(bdbCommissioningModeMsg->bdbCommissioningStatus == BDB_COMMISSIONING_SUCCESS)
          {
            //YOUR JOB:
            //We are on the nwk, what now?
          }
          else
          {
            //See the possible errors for nwk steering procedure
            //No suitable networks found
            //Want to try other channels?
            //try with bdb_setChannelAttribute
          }
        break;
        case BDB_COMMISSIONING_FINDING_BINDING:
          if(bdbCommissioningModeMsg->bdbCommissioningStatus == BDB_COMMISSIONING_SUCCESS)
          {
            //YOUR JOB:
          }
          else
          {
            //YOUR JOB:
            //retry?, wait for user interaction?
          }
        break;
        case BDB_COMMISSIONING_INITIALIZATION:
          //Initialization notification can only be successful. Failure on initialization 
          //only happens for ZED and is notified as BDB_COMMISSIONING_PARENT_LOST notification
          
          //YOUR JOB:
          //We are on a network, what now?
          
        break;
    #if ZG_BUILD_ENDDEVICE_TYPE    
        case BDB_COMMISSIONING_PARENT_LOST:
          if(bdbCommissioningModeMsg->bdbCommissioningStatus == BDB_COMMISSIONING_NETWORK_RESTORED)
          {
            //We did recover from losing parent
          }
          else
          {
            //Parent not found, attempt to rejoin again after a fixed delay
            Timer_setTimeout( EndDeviceRejoinClkHandle, SAMPLEAPP_END_DEVICE_REJOIN_DELAY );
            Timer_start(&EndDeviceRejoinClkStruct);
          }
        break;
    #endif 
      }
      
      UI_UpdateComissioningStatus(bdbCommissioningModeMsg);
    }
    
    /*********************************************************************
     * @fn      zclSampleSw_BasicResetCB
     *
     * @brief   Callback from the ZCL General Cluster Library
     *          to set all the Basic Cluster attributes to  default values.
     *
     * @param   none
     *
     * @return  none
     */
    static void zclSampleSw_BasicResetCB( void )
    {
      zclSampleSw_ResetAttributesToDefaultValues();
    
      // update the display
      UI_Refresh( ); 
    }
    
    
    /******************************************************************************
     *
     *  Functions for processing ZCL Foundation incoming Command/Response messages
     *
     *****************************************************************************/
      
    /*********************************************************************
     * @fn      zclSampleSw_ProcessIncomingMsg
     *
     * @brief   Process ZCL Foundation incoming message
     *
     * @param   pInMsg - pointer to the received message
     *
     * @return  uint8 - TRUE if got handled
     */
    static uint8 zclSampleSw_ProcessIncomingMsg( zclIncoming_t *pInMsg )
    {
      uint8 handled = FALSE;
    
      switch ( pInMsg->hdr.commandID )
      {
    #ifdef ZCL_READ
        case ZCL_CMD_READ_RSP:
          zclSampleSw_ProcessInReadRspCmd( pInMsg );
          handled = TRUE;
          break;
    #endif
    #ifdef ZCL_WRITE
        case ZCL_CMD_WRITE_RSP:
          zclSampleSw_ProcessInWriteRspCmd( pInMsg );
          handled = TRUE;
          break;
    #endif
    #ifdef ZCL_REPORT
        // See ZCL Test Applicaiton (zcl_testapp.c) for sample code on Attribute Reporting
        case ZCL_CMD_CONFIG_REPORT:
          //zclSampleSw_ProcessInConfigReportCmd( pInMsg );
          break;
    
        case ZCL_CMD_CONFIG_REPORT_RSP:
          //zclSampleSw_ProcessInConfigReportRspCmd( pInMsg );
          break;
    
        case ZCL_CMD_READ_REPORT_CFG:
          //zclSampleSw_ProcessInReadReportCfgCmd( pInMsg );
          break;
    
        case ZCL_CMD_READ_REPORT_CFG_RSP:
          //zclSampleSw_ProcessInReadReportCfgRspCmd( pInMsg );
          break;
    
        case ZCL_CMD_REPORT:
          //zclSampleSw_ProcessInReportCmd( pInMsg );
          break;
    #endif
        case ZCL_CMD_DEFAULT_RSP:
          zclSampleSw_ProcessInDefaultRspCmd( pInMsg );
          handled = TRUE;
          break;
    #ifdef ZCL_DISCOVER
        case ZCL_CMD_DISCOVER_CMDS_RECEIVED_RSP:
          zclSampleSw_ProcessInDiscCmdsRspCmd( pInMsg );
          handled = TRUE;
          break;
    
        case ZCL_CMD_DISCOVER_CMDS_GEN_RSP:
          zclSampleSw_ProcessInDiscCmdsRspCmd( pInMsg );
          handled = TRUE;
          break;
    
        case ZCL_CMD_DISCOVER_ATTRS_RSP:
          zclSampleSw_ProcessInDiscAttrsRspCmd( pInMsg );
          handled = TRUE;
          break;
    
        case ZCL_CMD_DISCOVER_ATTRS_EXT_RSP:
          zclSampleSw_ProcessInDiscAttrsExtRspCmd( pInMsg );
          handled = TRUE;
          break;
    #endif
        default:
          break;
      }
    
      return handled;
    }
    
    #ifdef ZCL_READ
    /*********************************************************************
     * @fn      zclSampleSw_ProcessInReadRspCmd
     *
     * @brief   Process the "Profile" Read Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleSw_ProcessInReadRspCmd( zclIncoming_t *pInMsg )
    {
      zclReadRspCmd_t *readRspCmd;
      uint8 i;
    
      readRspCmd = (zclReadRspCmd_t *)pInMsg->attrCmd;
      for (i = 0; i < readRspCmd->numAttr; i++)
      {
        // Notify the originator of the results of the original read attributes
        // attempt and, for each successfull request, the value of the requested
        // attribute
      }
    
      return TRUE;
    }
    #endif // ZCL_READ
    
    #ifdef ZCL_WRITE
    /*********************************************************************
     * @fn      zclSampleSw_ProcessInWriteRspCmd
     *
     * @brief   Process the "Profile" Write Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleSw_ProcessInWriteRspCmd( zclIncoming_t *pInMsg )
    {
      zclWriteRspCmd_t *writeRspCmd;
      uint8 i;
    
      writeRspCmd = (zclWriteRspCmd_t *)pInMsg->attrCmd;
      for (i = 0; i < writeRspCmd->numAttr; i++)
      {
        // Notify the device of the results of the its original write attributes
        // command.
      }
    
      return TRUE;
    }
    #endif // ZCL_WRITE
    
    /*********************************************************************
     * @fn      zclSampleSw_ProcessInDefaultRspCmd
     *
     * @brief   Process the "Profile" Default Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleSw_ProcessInDefaultRspCmd( zclIncoming_t *pInMsg )
    {
      // zclDefaultRspCmd_t *defaultRspCmd = (zclDefaultRspCmd_t *)pInMsg->attrCmd;
      // Device is notified of the Default Response command.
      (void)pInMsg;
      return TRUE;
    }
    
    #ifdef ZCL_DISCOVER
    /*********************************************************************
     * @fn      zclSampleSw_ProcessInDiscCmdsRspCmd
     *
     * @brief   Process the Discover Commands Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleSw_ProcessInDiscCmdsRspCmd( zclIncoming_t *pInMsg )
    {
      zclDiscoverCmdsCmdRsp_t *discoverRspCmd;
      uint8 i;
    
      discoverRspCmd = (zclDiscoverCmdsCmdRsp_t *)pInMsg->attrCmd;
      for ( i = 0; i < discoverRspCmd->numCmd; i++ )
      {
        // Device is notified of the result of its attribute discovery command.
      }
    
      return TRUE;
    }
    
    /*********************************************************************
     * @fn      zclSampleSw_ProcessInDiscAttrsRspCmd
     *
     * @brief   Process the "Profile" Discover Attributes Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleSw_ProcessInDiscAttrsRspCmd( zclIncoming_t *pInMsg )
    {
      zclDiscoverAttrsRspCmd_t *discoverRspCmd;
      uint8 i;
    
      discoverRspCmd = (zclDiscoverAttrsRspCmd_t *)pInMsg->attrCmd;
      for ( i = 0; i < discoverRspCmd->numAttr; i++ )
      {
        // Device is notified of the result of its attribute discovery command.
      }
    
      return TRUE;
    }
    
    /*********************************************************************
     * @fn      zclSampleSw_ProcessInDiscAttrsExtRspCmd
     *
     * @brief   Process the "Profile" Discover Attributes Extended Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleSw_ProcessInDiscAttrsExtRspCmd( zclIncoming_t *pInMsg )
    {
      zclDiscoverAttrsExtRsp_t *discoverRspCmd;
      uint8 i;
    
      discoverRspCmd = (zclDiscoverAttrsExtRsp_t *)pInMsg->attrCmd;
      for ( i = 0; i < discoverRspCmd->numAttr; i++ )
      {
        // Device is notified of the result of its attribute discovery command.
      }
    
      return TRUE;
    }
    #endif // ZCL_DISCOVER
    
    
    
    /****************************************************************************
    ****************************************************************************/
    
    void zclSampleSw_UiActionToggleLight(uint16 keys)
    {
      if (zclSampleSw_OnOffSwitchActions == ON_OFF_SWITCH_ACTIONS_TOGGLE)
      {
        if (keys & UI_KEY_SW_5_PRESSED)
        {
          zstack_bdbGetZCLFrameCounterRsp_t Rsp;
    
          Zstackapi_bdbGetZCLFrameCounterReq(zclSampleSw_Entity, &Rsp);    
          
          zclGeneral_SendOnOff_CmdToggle( SAMPLESW_ENDPOINT, &zclSampleSw_DstAddr, FALSE, Rsp.zclFrameCounter );
        }
      }
      else
      {
        // Other zclSampleSw_OnOffSwitchActions are not supported by this UI
      }
    }
    
    
    void zclSampleSw_UiRefresh(uint8 gui_state, char * line[3])
    {
      line[2] = "< TOGGLE LIGHT >";
    }
    
    
    /*********************************************************************
     * @fn      zclSampleSw_changeKeyCallback
     *
     * @brief   Key event handler function
     *
     * @param   keysPressed - ignored
     *
     * @return  none
     */
    static void zclSampleSw_changeKeyCallback(uint8_t keysPressed)
    {
        keys = keysPressed;
    
        events |= SAMPLEAPP_KEY_EVT;
    
        // Wake up the application thread when it waits for clock event
        Semaphore_post(sem);
    }
    
    
    
    static void zclSampleSw_processKey(uint8 keysPressed)
    {
        //Button 1
        if(keysPressed == KEY_LEFT)
        {
            zstack_bdbStartCommissioningReq_t zstack_bdbStartCommissioningReq;
    
            if(ZG_BUILD_COORDINATOR_TYPE && ZG_DEVICE_COORDINATOR_TYPE)
            {
                zstack_bdbStartCommissioningReq.commissioning_mode = BDB_COMMISSIONING_MODE_NWK_FORMATION | BDB_COMMISSIONING_MODE_NWK_STEERING | BDB_COMMISSIONING_MODE_FINDING_BINDING;
                Zstackapi_bdbStartCommissioningReq(zclSampleSw_Entity,&zstack_bdbStartCommissioningReq);
            }
            else if (ZG_BUILD_JOINING_TYPE && ZG_DEVICE_JOINING_TYPE)
            {
                zstack_bdbStartCommissioningReq.commissioning_mode = BDB_COMMISSIONING_MODE_NWK_STEERING | BDB_COMMISSIONING_MODE_FINDING_BINDING;
                Zstackapi_bdbStartCommissioningReq(zclSampleSw_Entity,&zstack_bdbStartCommissioningReq);
            }
        }
        //Button 2
        if(keysPressed == KEY_RIGHT)
        {
            zstack_bdbGetZCLFrameCounterRsp_t Rsp;
    
            Zstackapi_bdbGetZCLFrameCounterReq(zclSampleSw_Entity, &Rsp);
            zclGeneral_SendOnOff_CmdToggle( SAMPLESW_ENDPOINT, &zclSampleSw_DstAddr, FALSE, Rsp.zclFrameCounter );
        }
    
    }
    
    
    
    

    20181105_PANID_B822.zip

  • Hi Kimi,

    I'm observing a similar issue when using IAR regardless of C/C++ optimization settings, I will pass this information along to the Software Development Team. Can you evaluate the solution on CCS and confirm that the issue is not apparent on this IDE?

    Regards,
    Ryan
  • Kimi will do the same test with CCS and reply the result to you. How about another question "Q2. My purpose is receive APS ACK in the Stack. How do I get APS_ACK packet(which callback) in the Stack?" According to your description "zstackmsg_CmdIDs_AF_DATA_CONFIRM_IND occurs due to the On/Off Default Response (enabled when the disableDefaultRsp parameter of zclGeneral_SendOnOff_CmdToggle is set to FALSE), not the APS ACK",  zstackmsg_CmdIDs_AF_DATA_CONFIRM_IND doesn't mean to get APS ack so please elaborate how application code can get and parse APS ACK after sending AF_DataRequest.

  • Hi Ryan,

    Actually, the last test is using CCS. I upload the test video for your reference(include sniffer and file)

    20181106_PANID_B822_CCS.zip

    /**************************************************************************************************
      Filename:       zcl_samplesw.c
      Revised:        $Date: 2015-08-19 17:11:00 -0700 (Wed, 19 Aug 2015) $
      Revision:       $Revision: 44460 $
    
      Description:    Zigbee Cluster Library - sample switch application.
    
    
      Copyright 2006-2013 Texas Instruments Incorporated. All rights reserved.
    
      IMPORTANT: Your use of this Software is limited to those specific rights
      granted under the terms of a software license agreement between the user
      who downloaded the software, his/her employer (which must be your employer)
      and Texas Instruments Incorporated (the "License").  You may not use this
      Software unless you agree to abide by the terms of the License. The License
      limits your use, and you acknowledge, that the Software may not be modified,
      copied or distributed unless embedded on a Texas Instruments microcontroller
      or used solely and exclusively in conjunction with a Texas Instruments radio
      frequency transceiver, which is integrated into your product.  Other than for
      the foregoing purpose, you may not use, reproduce, copy, prepare derivative
      works of, modify, distribute, perform, display or sell this Software and/or
      its documentation for any purpose.
    
      YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE
      PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,
      INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE,
      NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL
      TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT,
      NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER
      LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
      INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE
      OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT
      OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
      (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
    
      Should you have any questions regarding your right to use this Software,
      contact Texas Instruments Incorporated at www.TI.com.
    **************************************************************************************************/
    
    /*********************************************************************
      This application implements a ZigBee On/Off Switch, based on Z-Stack 3.0.
    
      This application is based on the common sample-application user interface. Please see the main
      comment in zcl_sampleapp_ui.c. The rest of this comment describes only the content specific for
      this sample application.
      
      Application-specific UI peripherals being used:
    
      - none (LED1 is currently unused by this application).
    
      Application-specific menu system:
    
        <TOGGLE LIGHT> Send an On, Off or Toggle command targeting appropriate devices from the binding table.
          Pressing / releasing [OK] will have the following functionality, depending on the value of the 
          zclSampleSw_OnOffSwitchActions attribute:
          - OnOffSwitchActions == 0: pressing [OK] will send ON command, releasing it will send OFF command;
          - OnOffSwitchActions == 1: pressing [OK] will send OFF command, releasing it will send ON command;
          - OnOffSwitchActions == 2: pressing [OK] will send TOGGLE command, releasing it will not send any command.
    
    *********************************************************************/
    
    #if ! defined ZCL_ON_OFF
    #error ZCL_ON_OFF must be defined for this project.
    #endif
    
    /*********************************************************************
     * INCLUDES
     */
    #include "zcomdef.h"
    #include "zcl.h"
    #include "zcl_general.h"
    #include "zcl_ha.h"
    #include "on_board.h"
    #include "zcl_samplesw.h"
    #include "zcl_diagnostic.h"
    #include <string.h>
    
    #include "zcl_sampleapps_ui.h"
    #include "bdb_interface.h"
    
    #if defined (OTA_CLIENT_CC26XX)
    #include "zcl_ota.h"
    #include "ota_client_app.h"
    #include "bsp_spi.h"
    #endif
    
    #include "board_led.h"
    #include "board_key.h"
    #include "nvintf.h"
    #include "zstackmsg.h"
    #include "zcl_port.h"
    #include "znwk_config.h"
    #include <ti/sysbios/knl/Semaphore.h>
    #include "zstackapi.h"
    #include "zcl_sampleapps_ui.h"
    #include "util_timer.h"
    
    #if !defined (DISABLE_GREENPOWER_BASIC_PROXY) && (ZG_BUILD_RTR_TYPE)
    #include "gp_common.h"
    #endif
    
    #if defined ( BDB_TL_INITIATOR )
    #include "touchlink_initiator_app.h"
    #elif defined ( BDB_TL_TARGET )
    #include "touchlink_target_app.h"
    #endif
    
    #ifdef PER_TEST
    #include "per_test.h"
    #endif
    
    /*********************************************************************
     * MACROS
     */
    #define UI_STATE_TOGGLE_LIGHT 1 //UI_STATE_BACK_FROM_APP_MENU is item #0, so app item numbers should start from 1
    
    #define APP_TITLE "TI Sample Switch"
    
    /*********************************************************************
     * TYPEDEFS
     */
    
    /*********************************************************************
     * GLOBAL VARIABLES
     */
    
    
    uint8 zclSampleSwSeqNum;
    
    uint8 zclSampleSw_OnOffSwitchType = ON_OFF_SWITCH_TYPE_MOMENTARY;
    
    uint8 zclSampleSw_OnOffSwitchActions;
    
    /*********************************************************************
     * GLOBAL FUNCTIONS
     */
    
    /*********************************************************************
     * LOCAL VARIABLES
     */
    // Semaphore used to post events to the application thread
    static ICall_Semaphore sem;
    static ICall_EntityID  zclSampleSw_Entity;
    static endPointDesc_t  zclSampleSwEpDesc = {0};
    
    #if ZG_BUILD_ENDDEVICE_TYPE
    static Clock_Handle EndDeviceRejoinClkHandle;
    static Clock_Struct EndDeviceRejoinClkStruct;
    #endif
    
    // Passed in function pointers to the NV driver
    static NVINTF_nvFuncts_t *pfnZdlNV = NULL;
    
    // Key press parameters
    static uint8_t keys;
    
    // Task pending events
    static uint16_t events = 0;
    
    afAddrType_t zclSampleSw_DstAddr;
    
    
    #define SAMPLESW_TOGGLE_TEST_EVT   0x1000
    
    //kimi edit
    static zclOptionRec_t zcl_OnOff_Options[] =
    {
      {
        ZCL_CLUSTER_ID_GEN_ON_OFF,
        ( AF_ACK_REQUEST ),
      },
    };
    
    uint8_t beforeSend;
    uint8_t afterSend;
    //kimi edit
    /*********************************************************************
     * LOCAL FUNCTIONS
     */
    static void zclSampleSw_initialization(void);
    static void zclSampleSw_process_loop(void);
    static void zclSampleSw_initParameters(void);
    static void zclSampleSw_processZStackMsgs(zstackmsg_genericReq_t *pMsg);
    static void SetupZStackCallbacks(void);
    static void zclSampleSw_processAfIncomingMsgInd(zstack_afIncomingMsgInd_t *pInMsg);
    static void zclSampleSw_initializeClocks(void);
    #if ZG_BUILD_ENDDEVICE_TYPE
    static void zclSampleSw_processEndDeviceRejoinTimeoutCallback(UArg a0);
    #endif
    static void zclSampleSw_processKey(uint8 keysPressed);
    static void zclSampleSw_changeKeyCallback(uint8_t keysPressed);
    static void zclSampleSw_Init( void );
    
    static void zclSampleSw_BasicResetCB( void );
    
    static void zclSampleSw_ProcessCommissioningStatus(bdbCommissioningModeMsg_t *bdbCommissioningModeMsg);
    
    void zclSampleSw_UiActionToggleLight(uint16 keys);
    void zclSampleSw_UiRefresh(uint8 uiCurrentState, char * line[3]);
    
    // Functions to process ZCL Foundation incoming Command/Response messages
    static uint8 zclSampleSw_ProcessIncomingMsg( zclIncoming_t *msg );
    #ifdef ZCL_READ
    static uint8 zclSampleSw_ProcessInReadRspCmd( zclIncoming_t *pInMsg );
    #endif
    #ifdef ZCL_WRITE
    static uint8 zclSampleSw_ProcessInWriteRspCmd( zclIncoming_t *pInMsg );
    #endif
    static uint8 zclSampleSw_ProcessInDefaultRspCmd( zclIncoming_t *pInMsg );
    #ifdef ZCL_DISCOVER
    static uint8 zclSampleSw_ProcessInDiscCmdsRspCmd( zclIncoming_t *pInMsg );
    static uint8 zclSampleSw_ProcessInDiscAttrsRspCmd( zclIncoming_t *pInMsg );
    static uint8 zclSampleSw_ProcessInDiscAttrsExtRspCmd( zclIncoming_t *pInMsg );
    #endif
    
    
    /*********************************************************************
     * CONSTANTS
     */
      const uiState_t zclSampleSw_UiStatesMain[] = 
      {
        /*  UI_STATE_BACK_FROM_APP_MENU  */   {UI_STATE_DEFAULT_MOVE,       UI_STATE_TOGGLE_LIGHT,  UI_KEY_SW_5_PRESSED, &UI_ActionBackFromAppMenu}, //do not change this line, except for the second item, which should point to the last entry in this menu
        /*  UI_STATE_TOGGLE_LIGHT        */   {UI_STATE_BACK_FROM_APP_MENU, UI_STATE_DEFAULT_MOVE,  UI_KEY_SW_5_PRESSED, &zclSampleSw_UiActionToggleLight},
      };
      
    /*********************************************************************
     * REFERENCED EXTERNALS
     */
    extern int16 zdpExternalStateTaskID;
    
    /*********************************************************************
     * ZCL General Profile Callback table
     */
    static zclGeneral_AppCallbacks_t zclSampleSw_CmdCallbacks =
    {
      zclSampleSw_BasicResetCB,               // Basic Cluster Reset command
      NULL,                                   // Identify Trigger Effect command
      NULL,                                   // On/Off cluster commands
      NULL,                                   // On/Off cluster enhanced command Off with Effect
      NULL,                                   // On/Off cluster enhanced command On with Recall Global Scene
      NULL,                                   // On/Off cluster enhanced command On with Timed Off
    #ifdef ZCL_LEVEL_CTRL
      NULL,                                   // Level Control Move to Level command
      NULL,                                   // Level Control Move command
      NULL,                                   // Level Control Step command
      NULL,                                   // Level Control Stop command
    #endif
    #ifdef ZCL_GROUPS
      NULL,                                   // Group Response commands
    #endif
    #ifdef ZCL_SCENES
      NULL,                                   // Scene Store Request command
      NULL,                                   // Scene Recall Request command
      NULL,                                   // Scene Response command
    #endif
    #ifdef ZCL_ALARMS
      NULL,                                   // Alarm (Response) commands
    #endif
    #ifdef SE_UK_EXT
      NULL,                                   // Get Event Log command
      NULL,                                   // Publish Event Log command
    #endif
      NULL,                                   // RSSI Location command
      NULL                                    // RSSI Location Response command
    };
    
    
    /*********************************************************************
     * @fn          zclSampleSw_task
     *
     * @brief       Application task entry point for the ZStack Sample Switch
     *              Application
     *
     * @param       pfnNV - pointer to the NV functions
     *
     * @return      none
     */
    
    void zclSampleSw_task(NVINTF_nvFuncts_t *pfnNV)
    {
      // Save and register the function pointers to the NV drivers
      pfnZdlNV = pfnNV;
      zclport_registerNV(pfnZdlNV, ZCL_PORT_SCENE_TABLE_NV_ID);
    
      // Initialize application
      zclSampleSw_initialization();
    
      // No return from task process
      zclSampleSw_process_loop();
    }
    
    
    
    
    /*******************************************************************************
     * @fn          zclSampleSw_initialization
     *
     * @brief       Initialize the application
     *
     * @param       none
     *
     * @return      none
     */
    static void zclSampleSw_initialization(void)
    {
        uint8 key;
    
        /* Initialize user clocks */
        zclSampleSw_initializeClocks();
    
        /* Initialize keys */
        key = Board_Key_initialize(zclSampleSw_changeKeyCallback);
    
    
        /* Initialize the LEDS */
        Board_Led_initialize();
    
        // Register the current thread as an ICall dispatcher application
        // so that the application can send and receive messages.
        ICall_registerApp(&zclSampleSw_Entity, &sem);
    
        if(key == KEY_RIGHT)
        {
            Zstackapi_bdbResetLocalActionReq(zclSampleSw_Entity);
    
    #if defined (OTA_CLIENT_CC26XX)
            //Return to FN image
            OTA_loadExtImage(ST_FULL_FACTORY_IMAGE);
    #endif
        }
        
        //Initialize stack
        zclSampleSw_Init();
    
        
    #if defined (OTA_CLIENT_CC26XX)
        OTAClient_SetEndpoint(SAMPLESW_ENDPOINT);
        OTA_Client_Init ( sem, zclSampleSw_Entity);
    #endif
    
        
    }
    
    
    
    /*********************************************************************
     * @fn          zclSampleSw_Init
     *
     * @brief       Initialization function for the zclGeneral layer.
     *
     * @param       none
     *
     * @return      none
     */
    static void zclSampleSw_Init( void )
    {
    
      // Set destination address to indirect
      zclSampleSw_DstAddr.addrMode = (afAddrMode_t)AddrNotPresent;
      zclSampleSw_DstAddr.endPoint = 0;
      zclSampleSw_DstAddr.addr.shortAddr = 0;
    
      //Register Endpoint
      zclSampleSwEpDesc.endPoint = SAMPLESW_ENDPOINT;
      zclSampleSwEpDesc.simpleDesc = &zclSampleSw_SimpleDesc;
      zclport_registerEndpoint(zclSampleSw_Entity, &zclSampleSwEpDesc);
    
      // Register the ZCL General Cluster Library callback functions
      zclGeneral_RegisterCmdCallbacks( SAMPLESW_ENDPOINT, &zclSampleSw_CmdCallbacks );
    
      // Register the application's attribute list and reset to default values
      zclSampleSw_ResetAttributesToDefaultValues();
      zcl_registerAttrList( SAMPLESW_ENDPOINT, zclSampleSw_NumAttributes, zclSampleSw_Attrs );
    
      // Register the Application to receive the unprocessed Foundation command/response messages
      zclport_registerZclHandleExternal(zclSampleSw_ProcessIncomingMsg);
    
      //Write the bdb initialization parameters
      zclSampleSw_initParameters();
    
      //Setup ZDO callbacks
      SetupZStackCallbacks();
    
    #if !defined (DISABLE_GREENPOWER_BASIC_PROXY) && (ZG_BUILD_RTR_TYPE)
      gp_endpointInit(zclSampleSw_Entity);
    #endif
    
    #ifdef ZCL_DIAGNOSTIC
      // Register the application's callback function to read/write attribute data.
      // This is only required when the attribute data format is unknown to ZCL.
      zcl_registerReadWriteCB( SAMPLESW_ENDPOINT, zclDiagnostic_ReadWriteAttrCB, NULL );
    
      if ( zclDiagnostic_InitStats() == ZSuccess )
      {
        // Here the user could start the timer to save Diagnostics to NV
      }
    #endif
    
    
    #ifdef ZCL_DIAGNOSTIC
      // Register the application's callback function to read/write attribute data.
      // This is only required when the attribute data format is unknown to ZCL.
      zcl_registerReadWriteCB( SAMPLESW_ENDPOINT, zclDiagnostic_ReadWriteAttrCB, NULL );
    
      if ( zclDiagnostic_InitStats() == ZSuccess )
      {
        // Here the user could start the timer to save Diagnostics to NV
      }
    #endif
    
    #ifdef PER_TEST
      UI_Init(zclSampleSw_Entity, &events, sem, SAMPLEAPP_UI_AUTO_REFRESH_EVT, SAMPLEAPP_UI_INPUT_EVT, &zclSampleSw_IdentifyTime, APP_TITLE, &PERTest_UiRefresh, PERTest_UiStatesMain, NULL);
    #else
      UI_Init(zclSampleSw_Entity, &events, sem, SAMPLEAPP_UI_AUTO_REFRESH_EVT, SAMPLEAPP_UI_INPUT_EVT, &zclSampleSw_IdentifyTime, APP_TITLE, &zclSampleSw_UiRefresh, zclSampleSw_UiStatesMain, NULL);
    #endif
    
    #if defined ( BDB_TL_INITIATOR )
        touchLinkInitiatorApp_Init(zclSampleSw_Entity);
    #elif defined ( BDB_TL_TARGET )
        touchLinkTargetApp_Init(zclSampleSw_Entity);  
    #endif
      
    #if !defined (DISABLE_GREENPOWER_BASIC_PROXY) && (ZG_BUILD_RTR_TYPE)
      app_Green_Power_Init(zclSampleSw_Entity, &events, sem, SAMPLEAPP_PROCESS_GP_DATA_SEND_EVT, 
                           SAMPLEAPP_PROCESS_GP_EXPIRE_DUPLICATE_EVT, SAMPLEAPP_PROCESS_GP_TEMP_MASTER_EVT);
    #endif
    
    #ifdef PER_TEST
      per_interface_init(sem, zclSampleSw_Entity, SAMPLESW_ENDPOINT );
    #endif
    //kimi edit
     zcl_registerClusterOptionList(SAMPLESW_ENDPOINT, 1, zcl_OnOff_Options);
    //kimi edit
    }
    
    static void zclSampleSw_initParameters(void)
    {
        zstack_bdbSetAttributesReq_t zstack_bdbSetAttrReq;
    
        zstack_bdbSetAttrReq.bdbCommissioningGroupID              = BDB_DEFAULT_COMMISSIONING_GROUP_ID;
        zstack_bdbSetAttrReq.bdbPrimaryChannelSet                 = BDB_DEFAULT_PRIMARY_CHANNEL_SET;
        zstack_bdbSetAttrReq.bdbScanDuration                      = BDB_DEFAULT_SCAN_DURATION;
        zstack_bdbSetAttrReq.bdbSecondaryChannelSet               = BDB_DEFAULT_SECONDARY_CHANNEL_SET;
        zstack_bdbSetAttrReq.has_bdbCommissioningGroupID          = TRUE;
        zstack_bdbSetAttrReq.has_bdbPrimaryChannelSet             = TRUE;
        zstack_bdbSetAttrReq.has_bdbScanDuration                  = TRUE;
        zstack_bdbSetAttrReq.has_bdbSecondaryChannelSet           = TRUE;
    #if (ZG_BUILD_COORDINATOR_TYPE)
        zstack_bdbSetAttrReq.has_bdbJoinUsesInstallCodeKey        = TRUE;
        zstack_bdbSetAttrReq.has_bdbTrustCenterNodeJoinTimeout    = TRUE;
        zstack_bdbSetAttrReq.has_bdbTrustCenterRequireKeyExchange = TRUE;
        zstack_bdbSetAttrReq.bdbJoinUsesInstallCodeKey            = BDB_DEFAULT_JOIN_USES_INSTALL_CODE_KEY;
        zstack_bdbSetAttrReq.bdbTrustCenterNodeJoinTimeout        = BDB_DEFAULT_TC_NODE_JOIN_TIMEOUT;
        zstack_bdbSetAttrReq.bdbTrustCenterRequireKeyExchange     = BDB_DEFAULT_TC_REQUIRE_KEY_EXCHANGE;
    #endif
    #if (ZG_BUILD_JOINING_TYPE)
        zstack_bdbSetAttrReq.has_bdbTCLinkKeyExchangeAttemptsMax  = TRUE;
        zstack_bdbSetAttrReq.has_bdbTCLinkKeyExchangeMethod       = TRUE;
        zstack_bdbSetAttrReq.bdbTCLinkKeyExchangeAttemptsMax      = BDB_DEFAULT_TC_LINK_KEY_EXCHANGE_ATTEMPS_MAX;
        zstack_bdbSetAttrReq.bdbTCLinkKeyExchangeMethod           = BDB_DEFAULT_TC_LINK_KEY_EXCHANGE_METHOD;
    #endif
    
        Zstackapi_bdbSetAttributesReq(zclSampleSw_Entity, &zstack_bdbSetAttrReq);
    }
    
    /*******************************************************************************
     * @fn      zclSampleSw_initializeClocks
     *
     * @brief   Initialize Clocks
     *
     * @param   none
     *
     * @return  none
     */
    static void zclSampleSw_initializeClocks(void)
    {
    #if ZG_BUILD_ENDDEVICE_TYPE
        // Initialize the timers needed for this application
        EndDeviceRejoinClkHandle = Timer_construct(
        &EndDeviceRejoinClkStruct,
        zclSampleSw_processEndDeviceRejoinTimeoutCallback,
        SAMPLEAPP_END_DEVICE_REJOIN_DELAY,
        0, false, 0);
    #endif
    
    }
    
    #if ZG_BUILD_ENDDEVICE_TYPE
    /*******************************************************************************
     * @fn      zclSampleSw_processEndDeviceRejoinTimeoutCallback
     *
     * @brief   Timeout handler function
     *
     * @param   a0 - ignored
     *
     * @return  none
     */
    static void zclSampleSw_processEndDeviceRejoinTimeoutCallback(UArg a0)
    {
        (void)a0; // Parameter is not used
    
        events |= SAMPLEAPP_END_DEVICE_REJOIN_EVT;
    
        // Wake up the application thread when it waits for clock event
        Semaphore_post(sem);
    }
    #endif
    
    /*******************************************************************************
     * @fn      SetupZStackCallbacks
     *
     * @brief   Setup the Zstack Callbacks wanted
     *
     * @param   none
     *
     * @return  none
     */
    static void SetupZStackCallbacks(void)
    {
        zstack_devZDOCBReq_t zdoCBReq = {0};
    
        // Register for Callbacks, turn on:
        //  Device State Change,
        //  ZDO Match Descriptor Response,
        zdoCBReq.has_devStateChange = true;
        zdoCBReq.devStateChange = true;
    
    #if defined(OTA_CLIENT_CC26XX)
        zdoCBReq.has_matchDescRsp = true;
        zdoCBReq.matchDescRsp = true;
        zdoCBReq.has_ieeeAddrRsp = true;
        zdoCBReq.ieeeAddrRsp = true;
    #endif
    
        (void)Zstackapi_DevZDOCBReq(zclSampleSw_Entity, &zdoCBReq);
    }
    
    
    
    /*******************************************************************************
     * @fn      zclSampleSw_process_loop
     *
     * @brief   Application task processing start.
     *
     * @param   none
     *
     * @return  void
     */
    static void zclSampleSw_process_loop(void)
    {
        /* Forever loop */
        for(;;)
        {
            ICall_ServiceEnum stackid;
            ICall_EntityID dest;
            zstackmsg_genericReq_t *pMsg = NULL;
    
            /* Wait for response message */
            if(ICall_wait(ICALL_TIMEOUT_FOREVER) == ICALL_ERRNO_SUCCESS)
            {
                /* Retrieve the response message */
                if(ICall_fetchServiceMsg(&stackid, &dest, (void **)&pMsg)
                   == ICALL_ERRNO_SUCCESS)
                {
                    if( (stackid == ICALL_SERVICE_CLASS_ZSTACK)
                        && (dest == zclSampleSw_Entity) )
                    {
                        if(pMsg)
                        {
                            zclSampleSw_processZStackMsgs(pMsg);
    #ifdef PER_TEST
                            per_interface_processZStackMsg(pMsg);
    #endif
                            // Free any separately allocated memory
                            Zstackapi_freeIndMsg(pMsg);
                        }
                    }
    
                    if(pMsg)
                    {
                        ICall_freeMsg(pMsg);
                    }
                }
    
    #ifdef PER_TEST
                per_interface_process();
    #endif
    #if defined (OTA_CLIENT_CC26XX)
                zclOTA_event_loop(); 
    #endif
    
    
                if(events & SAMPLEAPP_KEY_EVT)
                {
                    // Process Key Presses
                    zclSampleSw_processKey(keys);
                    keys = 0;
                    events &= ~SAMPLEAPP_KEY_EVT;
                }
    
                if(events & SAMPLEAPP_UI_AUTO_REFRESH_EVT)
                {
                    UI_Refresh();
                    events &= ~SAMPLEAPP_UI_AUTO_REFRESH_EVT;
                }
    
                if(events & SAMPLEAPP_UI_INPUT_EVT)
                {
                    zclSampleAppsUI_ProcessUART();
                    events &= ~SAMPLEAPP_UI_INPUT_EVT;
                }
    
    #if !defined (DISABLE_GREENPOWER_BASIC_PROXY) && (ZG_BUILD_RTR_TYPE)
                if(events & SAMPLEAPP_PROCESS_GP_DATA_SEND_EVT)
                {
                    zcl_gpSendNotification();
                    events &= ~SAMPLEAPP_PROCESS_GP_DATA_SEND_EVT;
                }
    
                if(events & SAMPLEAPP_PROCESS_GP_EXPIRE_DUPLICATE_EVT)
                {
                    gp_expireDuplicateFiltering();
                    events &= ~SAMPLEAPP_PROCESS_GP_EXPIRE_DUPLICATE_EVT;
                }
    #endif
    
    #if ZG_BUILD_ENDDEVICE_TYPE
                if ( events & SAMPLEAPP_END_DEVICE_REJOIN_EVT )
                {
                  zstack_bdbZedAttemptRecoverNwkRsp_t zstack_bdbZedAttemptRecoverNwkRsp;
    
                  Zstackapi_bdbZedAttemptRecoverNwkReq(zclSampleSw_Entity,&zstack_bdbZedAttemptRecoverNwkRsp);
    
                  events &= ~SAMPLEAPP_END_DEVICE_REJOIN_EVT;
                }
    #endif
    
    
            }
        }
    }
    
    
    
    
    /*******************************************************************************
     * @fn      zclSampleSw_processZStackMsgs
     *
     * @brief   Process event from Stack
     *
     * @param   pMsg - pointer to incoming ZStack message to process
     *
     * @return  void
     */
    static void zclSampleSw_processZStackMsgs(zstackmsg_genericReq_t *pMsg)
    {
        static uint8_t test2 = 0;
          switch(pMsg->hdr.event)
          {
              case zstackmsg_CmdIDs_BDB_NOTIFICATION:
                  {
                      zstackmsg_bdbNotificationInd_t *pInd;
                      pInd = (zstackmsg_bdbNotificationInd_t*)pMsg;
                      zclSampleSw_ProcessCommissioningStatus(&(pInd->Req));
                  }
                  break;
                  
              case zstackmsg_CmdIDs_BDB_IDENTIFY_TIME_CB:
                  {
                      zstackmsg_bdbIdentifyTimeoutInd_t *pInd;
                      pInd = (zstackmsg_bdbIdentifyTimeoutInd_t*) pMsg;
                      uiProcessIdentifyTimeChange(&(pInd->EndPoint));
                  }
                  break;
                  
              case zstackmsg_CmdIDs_BDB_BIND_NOTIFICATION_CB:
                  {
                      zstackmsg_bdbBindNotificationInd_t *pInd;
                      pInd = (zstackmsg_bdbBindNotificationInd_t*) pMsg;
                      uiProcessBindNotification(&(pInd->Req));
                  }
                  break;
      
              case zstackmsg_CmdIDs_AF_INCOMING_MSG_IND:
                  {
                      // Process incoming data messages
                      zstackmsg_afIncomingMsgInd_t *pInd;
                      pInd = (zstackmsg_afIncomingMsgInd_t *)pMsg;
                      zclSampleSw_processAfIncomingMsgInd( &(pInd->req) );
                  }
                  break;
    //kimi edit
              case zstackmsg_CmdIDs_AF_DATA_CONFIRM_IND:
                  {
                      zstackmsg_afDataConfirmInd_t *pInd;
                      pInd = (zstackmsg_afDataConfirmInd_t *)pMsg;
                      test2 = pInd->req.transID;
                      test2 = pInd->req.endpoint;
                  }
                  break;
    //kimi edit
              case zstackmsg_CmdIDs_DEV_PERMIT_JOIN_IND:
                  {
                      zstackmsg_devPermitJoinInd_t *pInd;
                      pInd = (zstackmsg_devPermitJoinInd_t*)pMsg;
                      uiProcessPermitJoin(&(pInd->Req));
                  }
                  break;
      
                  
    #if (ZG_BUILD_JOINING_TYPE)
              case zstackmsg_CmdIDs_BDB_CBKE_TC_LINK_KEY_EXCHANGE_IND:
              {
                zstack_bdbCBKETCLinkKeyExchangeAttemptReq_t zstack_bdbCBKETCLinkKeyExchangeAttemptReq;
                /* Z3.0 has not defined CBKE yet, so lets attempt default TC Link Key exchange procedure
                 * by reporting CBKE failure.
                 */
    
                zstack_bdbCBKETCLinkKeyExchangeAttemptReq.didSuccess = FALSE;
      
                Zstackapi_bdbCBKETCLinkKeyExchangeAttemptReq(zclSampleSw_Entity,
                                                             &zstack_bdbCBKETCLinkKeyExchangeAttemptReq);
              }
              break;
      
              case zstackmsg_CmdIDs_BDB_FILTER_NWK_DESCRIPTOR_IND:
      
               /*   User logic to remove networks that do not want to join
                *   Networks to be removed can be released with Zstackapi_bdbNwkDescFreeReq
                */
             
                Zstackapi_bdbFilterNwkDescComplete(zclSampleSw_Entity);
              break;
                  
    #endif
              case zstackmsg_CmdIDs_DEV_STATE_CHANGE_IND:
              {
                  // The ZStack Thread is indicating a State change
                  zstackmsg_devStateChangeInd_t *pInd =
                      (zstackmsg_devStateChangeInd_t *)pMsg;
                        UI_DeviceStateUpdated(&(pInd->req));
              }
              break;
      
      
      
              /*
               * These are messages/indications from ZStack that this
               * application doesn't process.  These message can be
               * processed by your application, remove from this list and
               * process them here in this switch statement.
               */
      
    #if !defined (DISABLE_GREENPOWER_BASIC_PROXY) && (ZG_BUILD_RTR_TYPE)
              case zstackmsg_CmdIDs_GP_DATA_IND:
              {
                  zstackmsg_gpDataInd_t *pInd;
                  pInd = (zstackmsg_gpDataInd_t*)pMsg;
                  gp_processDataIndMsg( &(pInd->Req) );
              }
              break;
    
              case zstackmsg_CmdIDs_GP_SECURITY_REQ:
              {
                  zstackmsg_gpSecReq_t *pInd;
                  pInd = (zstackmsg_gpSecReq_t*)pMsg;
                  gp_processSecRecMsg( &(pInd->Req) );
              }
              break;
    
              case zstackmsg_CmdIDs_GP_CHECK_ANNCE:
              {
                  zstackmsg_gpCheckAnnounce_t *pInd;
                  pInd = (zstackmsg_gpCheckAnnounce_t*)pMsg;
                  gp_processCheckAnnceMsg( &(pInd->Req) );
              }
              break;
    
    #ifdef BOARD_DISPLAY_USE_UART
              case zstackmsg_CmdIDs_GP_COMMISSIONING_MODE_IND:
              {
                zstackmsg_gpCommissioningModeInd_t *pInd;
                pInd = (zstackmsg_gpCommissioningModeInd_t*)pMsg;
                UI_SetGPPCommissioningMode( &(pInd->Req) );
              }
              break;
    #endif
    #endif
               
    #ifdef BDB_TL_TARGET
              case zstackmsg_CmdIDs_BDB_TOUCHLINK_TARGET_ENABLE_IND:
              {
                zstackmsg_bdbTouchLinkTargetEnableInd_t *pInd = 
                  (zstackmsg_bdbTouchLinkTargetEnableInd_t*)pMsg;
              
                uiProcessTouchlinkTargetEnable(pInd->Enable);
              }
              break;
    #endif
    
    #if defined (OTA_CLIENT_CC26XX)
              case zstackmsg_CmdIDs_ZDO_MATCH_DESC_RSP:
              {
                zstackmsg_zdoMatchDescRspInd_t  *pInd =
                   (zstackmsg_zdoMatchDescRspInd_t *) pMsg;
    
                ZDO_MatchDescRsp_t              *pRsp;
    
                /*
                 * Parse the Match Descriptor Response and give it
                 * to the ZCL EZMode module t+o process
                 */
                pRsp = (ZDO_MatchDescRsp_t *) ICall_allocMsg(
        		       sizeof(ZDO_MatchDescRsp_t) + pInd->rsp.n_matchList);
                if(pRsp)
                {
                  pRsp->status = pInd->rsp.status;
                  pRsp->nwkAddr = pInd->rsp.nwkAddrOfInterest;
                  pRsp->cnt = pInd->rsp.n_matchList;
                  memcpy(pRsp->epList, pInd->rsp.pMatchList, pInd->rsp.n_matchList);
                  
                  OTA_ProcessMatchDescRsp ( pRsp );
                  ICall_freeMsg(pRsp);
                }
              }
              break;
    #endif
    
              case zstackmsg_CmdIDs_BDB_TC_LINK_KEY_EXCHANGE_NOTIFICATION_IND:
    //kimi edit
    //          case zstackmsg_CmdIDs_AF_DATA_CONFIRM_IND:
    //kimi edit
              case zstackmsg_CmdIDs_ZDO_DEVICE_ANNOUNCE:
              case zstackmsg_CmdIDs_ZDO_NWK_ADDR_RSP:
              case zstackmsg_CmdIDs_ZDO_IEEE_ADDR_RSP:
              case zstackmsg_CmdIDs_ZDO_NODE_DESC_RSP:
              case zstackmsg_CmdIDs_ZDO_POWER_DESC_RSP:
              case zstackmsg_CmdIDs_ZDO_SIMPLE_DESC_RSP:
              case zstackmsg_CmdIDs_ZDO_ACTIVE_EP_RSP:
              case zstackmsg_CmdIDs_ZDO_COMPLEX_DESC_RSP:
              case zstackmsg_CmdIDs_ZDO_USER_DESC_RSP:
              case zstackmsg_CmdIDs_ZDO_USER_DESC_SET_RSP:
              case zstackmsg_CmdIDs_ZDO_SERVER_DISC_RSP:
              case zstackmsg_CmdIDs_ZDO_END_DEVICE_BIND_RSP:
              case zstackmsg_CmdIDs_ZDO_BIND_RSP:
              case zstackmsg_CmdIDs_ZDO_UNBIND_RSP:
              case zstackmsg_CmdIDs_ZDO_MGMT_NWK_DISC_RSP:
              case zstackmsg_CmdIDs_ZDO_MGMT_LQI_RSP:
              case zstackmsg_CmdIDs_ZDO_MGMT_RTG_RSP:
              case zstackmsg_CmdIDs_ZDO_MGMT_BIND_RSP:
              case zstackmsg_CmdIDs_ZDO_MGMT_LEAVE_RSP:
              case zstackmsg_CmdIDs_ZDO_MGMT_DIRECT_JOIN_RSP:
              case zstackmsg_CmdIDs_ZDO_MGMT_PERMIT_JOIN_RSP:
              case zstackmsg_CmdIDs_ZDO_MGMT_NWK_UPDATE_NOTIFY:
              case zstackmsg_CmdIDs_ZDO_SRC_RTG_IND:
              case zstackmsg_CmdIDs_ZDO_CONCENTRATOR_IND:
              case zstackmsg_CmdIDs_ZDO_LEAVE_CNF:
              case zstackmsg_CmdIDs_ZDO_LEAVE_IND:
              case zstackmsg_CmdIDs_SYS_RESET_IND:
              case zstackmsg_CmdIDs_AF_REFLECT_ERROR_IND:
              case zstackmsg_CmdIDs_ZDO_TC_DEVICE_IND:
                  break;
      
              default:
                  break;
          }
    
    }
    
    
    
    /*******************************************************************************
     *
     * @fn          zclSampleSw_processAfIncomingMsgInd
     *
     * @brief       Process AF Incoming Message Indication message
     *
     * @param       pInMsg - pointer to incoming message
     *
     * @return      none
     *
     */
    static void zclSampleSw_processAfIncomingMsgInd(zstack_afIncomingMsgInd_t *pInMsg)
    {
        afIncomingMSGPacket_t afMsg;
    
        /*
         * All incoming messages are passed to the ZCL message processor,
         * first convert to a structure that ZCL can process.
         */
        afMsg.groupId = pInMsg->groupID;
        afMsg.clusterId = pInMsg->clusterId;
        afMsg.srcAddr.endPoint = pInMsg->srcAddr.endpoint;
        afMsg.srcAddr.panId = pInMsg->srcAddr.panID;
        afMsg.srcAddr.addrMode = (afAddrMode_t)pInMsg->srcAddr.addrMode;
        if( (afMsg.srcAddr.addrMode == afAddr16Bit)
            || (afMsg.srcAddr.addrMode == afAddrGroup)
            || (afMsg.srcAddr.addrMode == afAddrBroadcast) )
        {
            afMsg.srcAddr.addr.shortAddr = pInMsg->srcAddr.addr.shortAddr;
        }
        else if(afMsg.srcAddr.addrMode == afAddr64Bit)
        {
            memcpy(afMsg.srcAddr.addr.extAddr, &(pInMsg->srcAddr.addr.extAddr), 8);
        }
        afMsg.macDestAddr = pInMsg->macDestAddr;
        afMsg.endPoint = pInMsg->endpoint;
        afMsg.wasBroadcast = pInMsg->wasBroadcast;
        afMsg.LinkQuality = pInMsg->linkQuality;
        afMsg.correlation = pInMsg->correlation;
        afMsg.rssi = pInMsg->rssi;
        afMsg.SecurityUse = pInMsg->securityUse;
        afMsg.timestamp = pInMsg->timestamp;
        afMsg.nwkSeqNum = pInMsg->nwkSeqNum;
        afMsg.macSrcAddr = pInMsg->macSrcAddr;
        afMsg.radius = pInMsg->radius;
        afMsg.cmd.TransSeqNumber = pInMsg->transSeqNum;
        afMsg.cmd.DataLength = pInMsg->n_payload;
        afMsg.cmd.Data = pInMsg->pPayload;
    
        zcl_ProcessMessageMSG(&afMsg);
    }
    
    
    /*********************************************************************
     * @fn      zclSampleSw_ProcessCommissioningStatus
     *
     * @brief   Callback in which the status of the commissioning process are reported
     *
     * @param   bdbCommissioningModeMsg - Context message of the status of a commissioning process
     *
     * @return  none
     */
    static void zclSampleSw_ProcessCommissioningStatus(bdbCommissioningModeMsg_t *bdbCommissioningModeMsg)
    {
      switch(bdbCommissioningModeMsg->bdbCommissioningMode)
      {
        case BDB_COMMISSIONING_FORMATION:
          if(bdbCommissioningModeMsg->bdbCommissioningStatus == BDB_COMMISSIONING_SUCCESS)
          {
            zstack_bdbStartCommissioningReq_t zstack_bdbStartCommissioningReq;
            
            //After formation, perform nwk steering again plus the remaining commissioning modes that has not been process yet        
            zstack_bdbStartCommissioningReq.commissioning_mode = BDB_COMMISSIONING_MODE_NWK_STEERING | bdbCommissioningModeMsg->bdbRemainingCommissioningModes;
            Zstackapi_bdbStartCommissioningReq(zclSampleSw_Entity,&zstack_bdbStartCommissioningReq);
          }
          else
          {
            //Want to try other channels?
            //try with bdb_setChannelAttribute
          }
        break;
        case BDB_COMMISSIONING_NWK_STEERING:
          if(bdbCommissioningModeMsg->bdbCommissioningStatus == BDB_COMMISSIONING_SUCCESS)
          {
            //YOUR JOB:
            //We are on the nwk, what now?
          }
          else
          {
            //See the possible errors for nwk steering procedure
            //No suitable networks found
            //Want to try other channels?
            //try with bdb_setChannelAttribute
          }
        break;
        case BDB_COMMISSIONING_FINDING_BINDING:
          if(bdbCommissioningModeMsg->bdbCommissioningStatus == BDB_COMMISSIONING_SUCCESS)
          {
            //YOUR JOB:
          }
          else
          {
            //YOUR JOB:
            //retry?, wait for user interaction?
          }
        break;
        case BDB_COMMISSIONING_INITIALIZATION:
          //Initialization notification can only be successful. Failure on initialization 
          //only happens for ZED and is notified as BDB_COMMISSIONING_PARENT_LOST notification
          
          //YOUR JOB:
          //We are on a network, what now?
          
        break;
    #if ZG_BUILD_ENDDEVICE_TYPE    
        case BDB_COMMISSIONING_PARENT_LOST:
          if(bdbCommissioningModeMsg->bdbCommissioningStatus == BDB_COMMISSIONING_NETWORK_RESTORED)
          {
            //We did recover from losing parent
          }
          else
          {
            //Parent not found, attempt to rejoin again after a fixed delay
            Timer_setTimeout( EndDeviceRejoinClkHandle, SAMPLEAPP_END_DEVICE_REJOIN_DELAY );
            Timer_start(&EndDeviceRejoinClkStruct);
          }
        break;
    #endif 
      }
      
      UI_UpdateComissioningStatus(bdbCommissioningModeMsg);
    }
    
    /*********************************************************************
     * @fn      zclSampleSw_BasicResetCB
     *
     * @brief   Callback from the ZCL General Cluster Library
     *          to set all the Basic Cluster attributes to  default values.
     *
     * @param   none
     *
     * @return  none
     */
    static void zclSampleSw_BasicResetCB( void )
    {
      zclSampleSw_ResetAttributesToDefaultValues();
    
      // update the display
      UI_Refresh( ); 
    }
    
    
    /******************************************************************************
     *
     *  Functions for processing ZCL Foundation incoming Command/Response messages
     *
     *****************************************************************************/
      
    /*********************************************************************
     * @fn      zclSampleSw_ProcessIncomingMsg
     *
     * @brief   Process ZCL Foundation incoming message
     *
     * @param   pInMsg - pointer to the received message
     *
     * @return  uint8 - TRUE if got handled
     */
    static uint8 zclSampleSw_ProcessIncomingMsg( zclIncoming_t *pInMsg )
    {
      uint8 handled = FALSE;
    
      switch ( pInMsg->hdr.commandID )
      {
    #ifdef ZCL_READ
        case ZCL_CMD_READ_RSP:
          zclSampleSw_ProcessInReadRspCmd( pInMsg );
          handled = TRUE;
          break;
    #endif
    #ifdef ZCL_WRITE
        case ZCL_CMD_WRITE_RSP:
          zclSampleSw_ProcessInWriteRspCmd( pInMsg );
          handled = TRUE;
          break;
    #endif
    #ifdef ZCL_REPORT
        // See ZCL Test Applicaiton (zcl_testapp.c) for sample code on Attribute Reporting
        case ZCL_CMD_CONFIG_REPORT:
          //zclSampleSw_ProcessInConfigReportCmd( pInMsg );
          break;
    
        case ZCL_CMD_CONFIG_REPORT_RSP:
          //zclSampleSw_ProcessInConfigReportRspCmd( pInMsg );
          break;
    
        case ZCL_CMD_READ_REPORT_CFG:
          //zclSampleSw_ProcessInReadReportCfgCmd( pInMsg );
          break;
    
        case ZCL_CMD_READ_REPORT_CFG_RSP:
          //zclSampleSw_ProcessInReadReportCfgRspCmd( pInMsg );
          break;
    
        case ZCL_CMD_REPORT:
          //zclSampleSw_ProcessInReportCmd( pInMsg );
          break;
    #endif
        case ZCL_CMD_DEFAULT_RSP:
          zclSampleSw_ProcessInDefaultRspCmd( pInMsg );
          handled = TRUE;
          break;
    #ifdef ZCL_DISCOVER
        case ZCL_CMD_DISCOVER_CMDS_RECEIVED_RSP:
          zclSampleSw_ProcessInDiscCmdsRspCmd( pInMsg );
          handled = TRUE;
          break;
    
        case ZCL_CMD_DISCOVER_CMDS_GEN_RSP:
          zclSampleSw_ProcessInDiscCmdsRspCmd( pInMsg );
          handled = TRUE;
          break;
    
        case ZCL_CMD_DISCOVER_ATTRS_RSP:
          zclSampleSw_ProcessInDiscAttrsRspCmd( pInMsg );
          handled = TRUE;
          break;
    
        case ZCL_CMD_DISCOVER_ATTRS_EXT_RSP:
          zclSampleSw_ProcessInDiscAttrsExtRspCmd( pInMsg );
          handled = TRUE;
          break;
    #endif
        default:
          break;
      }
    
      return handled;
    }
    
    #ifdef ZCL_READ
    /*********************************************************************
     * @fn      zclSampleSw_ProcessInReadRspCmd
     *
     * @brief   Process the "Profile" Read Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleSw_ProcessInReadRspCmd( zclIncoming_t *pInMsg )
    {
      zclReadRspCmd_t *readRspCmd;
      uint8 i;
    
      readRspCmd = (zclReadRspCmd_t *)pInMsg->attrCmd;
      for (i = 0; i < readRspCmd->numAttr; i++)
      {
        // Notify the originator of the results of the original read attributes
        // attempt and, for each successfull request, the value of the requested
        // attribute
      }
    
      return TRUE;
    }
    #endif // ZCL_READ
    
    #ifdef ZCL_WRITE
    /*********************************************************************
     * @fn      zclSampleSw_ProcessInWriteRspCmd
     *
     * @brief   Process the "Profile" Write Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleSw_ProcessInWriteRspCmd( zclIncoming_t *pInMsg )
    {
      zclWriteRspCmd_t *writeRspCmd;
      uint8 i;
    
      writeRspCmd = (zclWriteRspCmd_t *)pInMsg->attrCmd;
      for (i = 0; i < writeRspCmd->numAttr; i++)
      {
        // Notify the device of the results of the its original write attributes
        // command.
      }
    
      return TRUE;
    }
    #endif // ZCL_WRITE
    
    /*********************************************************************
     * @fn      zclSampleSw_ProcessInDefaultRspCmd
     *
     * @brief   Process the "Profile" Default Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleSw_ProcessInDefaultRspCmd( zclIncoming_t *pInMsg )
    {
      // zclDefaultRspCmd_t *defaultRspCmd = (zclDefaultRspCmd_t *)pInMsg->attrCmd;
      // Device is notified of the Default Response command.
      (void)pInMsg;
      return TRUE;
    }
    
    #ifdef ZCL_DISCOVER
    /*********************************************************************
     * @fn      zclSampleSw_ProcessInDiscCmdsRspCmd
     *
     * @brief   Process the Discover Commands Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleSw_ProcessInDiscCmdsRspCmd( zclIncoming_t *pInMsg )
    {
      zclDiscoverCmdsCmdRsp_t *discoverRspCmd;
      uint8 i;
    
      discoverRspCmd = (zclDiscoverCmdsCmdRsp_t *)pInMsg->attrCmd;
      for ( i = 0; i < discoverRspCmd->numCmd; i++ )
      {
        // Device is notified of the result of its attribute discovery command.
      }
    
      return TRUE;
    }
    
    /*********************************************************************
     * @fn      zclSampleSw_ProcessInDiscAttrsRspCmd
     *
     * @brief   Process the "Profile" Discover Attributes Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleSw_ProcessInDiscAttrsRspCmd( zclIncoming_t *pInMsg )
    {
      zclDiscoverAttrsRspCmd_t *discoverRspCmd;
      uint8 i;
    
      discoverRspCmd = (zclDiscoverAttrsRspCmd_t *)pInMsg->attrCmd;
      for ( i = 0; i < discoverRspCmd->numAttr; i++ )
      {
        // Device is notified of the result of its attribute discovery command.
      }
    
      return TRUE;
    }
    
    /*********************************************************************
     * @fn      zclSampleSw_ProcessInDiscAttrsExtRspCmd
     *
     * @brief   Process the "Profile" Discover Attributes Extended Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleSw_ProcessInDiscAttrsExtRspCmd( zclIncoming_t *pInMsg )
    {
      zclDiscoverAttrsExtRsp_t *discoverRspCmd;
      uint8 i;
    
      discoverRspCmd = (zclDiscoverAttrsExtRsp_t *)pInMsg->attrCmd;
      for ( i = 0; i < discoverRspCmd->numAttr; i++ )
      {
        // Device is notified of the result of its attribute discovery command.
      }
    
      return TRUE;
    }
    #endif // ZCL_DISCOVER
    
    
    
    /****************************************************************************
    ****************************************************************************/
    
    void zclSampleSw_UiActionToggleLight(uint16 keys)
    {
      if (zclSampleSw_OnOffSwitchActions == ON_OFF_SWITCH_ACTIONS_TOGGLE)
      {
        if (keys & UI_KEY_SW_5_PRESSED)
        {
          zstack_bdbGetZCLFrameCounterRsp_t Rsp;
    
          Zstackapi_bdbGetZCLFrameCounterReq(zclSampleSw_Entity, &Rsp);    
          
          zclGeneral_SendOnOff_CmdToggle( SAMPLESW_ENDPOINT, &zclSampleSw_DstAddr, FALSE, Rsp.zclFrameCounter );
        }
      }
      else
      {
        // Other zclSampleSw_OnOffSwitchActions are not supported by this UI
      }
    }
    
    
    void zclSampleSw_UiRefresh(uint8 gui_state, char * line[3])
    {
      line[2] = "< TOGGLE LIGHT >";
    }
    
    
    /*********************************************************************
     * @fn      zclSampleSw_changeKeyCallback
     *
     * @brief   Key event handler function
     *
     * @param   keysPressed - ignored
     *
     * @return  none
     */
    static void zclSampleSw_changeKeyCallback(uint8_t keysPressed)
    {
        keys = keysPressed;
    
        events |= SAMPLEAPP_KEY_EVT;
    
        // Wake up the application thread when it waits for clock event
        Semaphore_post(sem);
    }
    
    
    
    static void zclSampleSw_processKey(uint8 keysPressed)
    {
        static uint8_t test1 = 0;
        //Button 1
        if(keysPressed == KEY_LEFT)
        {
            zstack_bdbStartCommissioningReq_t zstack_bdbStartCommissioningReq;
    
            if(ZG_BUILD_COORDINATOR_TYPE && ZG_DEVICE_COORDINATOR_TYPE)
            {
                zstack_bdbStartCommissioningReq.commissioning_mode = BDB_COMMISSIONING_MODE_NWK_FORMATION | BDB_COMMISSIONING_MODE_NWK_STEERING | BDB_COMMISSIONING_MODE_FINDING_BINDING;
                Zstackapi_bdbStartCommissioningReq(zclSampleSw_Entity,&zstack_bdbStartCommissioningReq);
            }
            else if (ZG_BUILD_JOINING_TYPE && ZG_DEVICE_JOINING_TYPE)
            {
                zstack_bdbStartCommissioningReq.commissioning_mode = BDB_COMMISSIONING_MODE_NWK_STEERING | BDB_COMMISSIONING_MODE_FINDING_BINDING;
                Zstackapi_bdbStartCommissioningReq(zclSampleSw_Entity,&zstack_bdbStartCommissioningReq);
            }
        }
        //Button 2
        if(keysPressed == KEY_RIGHT)
        {
            zstack_bdbGetZCLFrameCounterRsp_t Rsp;
    
            Zstackapi_bdbGetZCLFrameCounterReq(zclSampleSw_Entity, &Rsp);
    
            test1 = Rsp.zclFrameCounter;
    
            zclGeneral_SendOnOff_CmdToggle( SAMPLESW_ENDPOINT, &zclSampleSw_DstAddr, FALSE, Rsp.zclFrameCounter );
        }
    
    }
    
    
    
    

  • Hi Kimi,

    This sequence numbers are not the same stuff zclFrameCounter is the transaction sequence number of the ZCL header and in other hand transID on data confirm is the APS counter of the APS header. From your capture you can look at toggle command at packet 417 and APS ACK at packet 421, and both of them have APS counter value in the APS header equal to 13 which correct and this is the value that transID holds.

    I hope this helps.
  • Actually, we are totally confused by Ryan's and your descriptions. Let's come back to our original request. We just need to check if we get APS ack after we send a ZCL command so can you elaborate with code to show us how to do that?
  • Hi YK,

    Are you not able to get APS ACK?

    step 1: have an options struct

    static zclOptionRec_t zclOptions[] =
    {
    {
    ZCL_CLUSTER_ID_GEN_ON_OFF,
    ( AF_ACK_REQUEST ),
    },
    };


    step 2: Register cluster options with zcl_registerClusterOptionList(SAMPLESW_ENDPOINT,1,zclOptions);

    step 3: On each APS ACK you will receive a confirm that is processed by processAfDataConfirm() and sends a message to application with zstackmsg_CmdIDs_AF_DATA_CONFIRM_IND. You can implement your function to process the data confirm.
  • If you read Ryan's previous reply, he said zstackmsg_CmdIDs_AF_DATA_CONFIRM_IND doen't mean APS ack. So, whose reply is true? If you are correct and we can process APS ack in zstackmsg_CmdIDs_AF_DATA_CONFIRM_IND, how can we match transID to the whatever (seqNum or APS_Counter) we send with zcl command?
  • Hi Jose Maria Alvarez,

    Please check my video(I have another reply with new sniffer and video). You can see in last toggle command. The APS_Counter in sniffer is 29 but I get transID in "case zstackmsg_CmdIDs_DEV_PERMIT_JOIN_IND" is 28
  • and Can you elaborate how we check APS ack after we send a zcl command?

  • Hi Kimi, YK,

    I apologize for the confusion I caused with my previous statement, it has since been edited and Jose will provide a thorough explanation and code example shortly.

    Regards,
    Ryan

  • Hi guys,

    One again we need to be clear that ZCL frame counter and APS transaction ID are not the same. In the current implementation you need to have track of APS_Counter before you send an APS layer frame like ZDO or ZCL command in order to validate with a zstackmsg_CmdIDs_AF_DATA_CONFIRM_IND message on application. All APS layer commands will generate an AF data confirm but the difference between using APS ACK flag is that with APS ACK enabled frames you will get an zstackmsg_CmdIDs_AF_DATA_CONFIRM_IND only when the APS ACK indication arrives, otherwise of APS ACK flag is disabled you will get an zstackmsg_CmdIDs_AF_DATA_CONFIRM_IND whenever the stack sends out an APS layer frame successfully.

    I used the next example on zcl_samplesw application to validate:

    - Add a zcl options struct

    static zclOptionRec_t zclOptions[] =
    {
      {
      ZCL_CLUSTER_ID_GEN_ON_OFF,
      ( AF_ACK_REQUEST ),
      },
    };

    - add a global variable to save APS counter

    uint8 savedAPSCount;

    - register cluster options in zclSampleSw_Init()

    zcl_registerClusterOptionList(SAMPLESW_ENDPOINT,1,zclOptions);

    - Save aps counter before sending the toggle command

    savedAPSCount = APS_Counter;  // Save counter
    zclGeneral_SendOnOff_CmdToggle( SAMPLESW_ENDPOINT, &zclSampleSw_DstAddr, FALSE, Rsp.zclFrameCounter );

    - Add zstackmsg_CmdIDs_AF_DATA_CONFIRM_IND processing to zclSampleSw_processZStackMsgs()

    case zstackmsg_CmdIDs_AF_DATA_CONFIRM_IND:
    {
      zstackmsg_afDataConfirmInd_t *pInd =
      (zstackmsg_afDataConfirmInd_t *) pMsg;
      if(savedAPSCount == pInd->req.transID)
      {
        // Do something
      }
    }
    break;

    You can implement your handler for zstackmsg_CmdIDs_AF_DATA_CONFIRM_IND according to your application needs. Please try it like that and tell me if you need something else to be clarified. 

  • Hi Jose,

    I follow your example. I can get the same APS_Counter(53) but I see another problem. The APS_Counter does not match the sniffer(54). Do I miss anything?

    pic.1 before send

    pic.2 after send

    pic.3 toggle command

    pic.4 APS ACK

  • Can you do the same test and show us your sniffer screenshot that APS_counter in sniffer is matched to savedAPSCount in your code?
  • This is perfectly fine because the spec doesn't specify and mandate this values to be equal since Zigbee spec only care about for Over-The-Air frames and the spec only mandates this counters to be incremental not sequential.

    This behavior is not an issue.

  • Don’t you think such behavior is confusing even you don’t think it’s an issue?

  • No, because as I said this values have no relation according to the spec, so completely different values can be used.

  • When I see something that is set in my code but not matching with sniffer, I am worried about it and that’s why I ask this transID thing in Z-Stack 3.0.x and also here. I still don’t think this is a good practice to see different APS_count in application and sniffer.