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.

CC2530: how to change sample projects in zstack as I need.

Part Number: CC2530
Other Parts Discussed in Thread: Z-STACK, , Z-STACK-ARCHIVE

hello.

I am beginner in zigbee. I want to write some at-command for a zigbee module in cc2530 to communicate with it. I want to do this work by IAR for 8051 and change an sample project in z-stack. can anybody help me about it?

how can I change zigbee stack protocol for my work? for example if I want to blink a LED where should I change? I read developer guide in TI site completely but i do not understand how can I change the sample projects as I need. my zigbee stack protocol is 3.

can anybody give me a link or pdf to explain all section in sample apps and how to change them? thanks for your help.

  • Can you elaborate which part you don’t understand?
  • I don't understand how can I change this program as I need. I mean which sections need to change for my program and where can I write my code for my program. I need to find something like while(1) loop in another micro controllers that I can write my main code to run in zigbee module always. can you help me about it?

    if you have some video that change in sample programs to blink a LED always or send and recieve data from UART pins pleassssssssssssssssssssssse send it to me. I actually confused and I  really don't know what can I do.   

  • You can use API HalLedBlink to blink LED at anywhere you want blink LED. For others, you can refer to Z-Stack Developer’s Guide.pdf in Z-Stack document folder.
  • when I set my pins in hal-board-cfg I can't blink my LED by using HalLedBlink in HalLed.c and it doesn't work. can you tell me where is the source code of sample applications? I mean which section do main work in sample application and if I want to change it which section I should change?
    please don't tell me texas documents beacause I read them about 2 month and I can't solve my problems.
  • If you use CC Debugger to trace and debug Z-Stack example, it will stop at main function right after you download it.
  • yes. you are right but when I call HalLedBlink in this section, it doesn't work and when the code arrive to this line osal_start_system(); // No Return from here, don't return as it commented. if you can send me a complete example that show me how to change this sample code to work
    I don't have a develop kit and when I define my pins and connect cc2530 pins to LEDs or switches the sample code doesn't work.
  • Using SampleSwitch as example, all of application related code should be implemented inside zcl_samplesw.c. For example, you can do led blink in zclSampleSw_HandleKeys when you detect a key is pressed.
  • thanks for your answer but I have another question. when I run the program step by step zcl_samplesw never run. I get a break point in this file but running program doesn't stop in break point. What does it mean? why do this happen?
  • If you set breakpoint inside zclSampleSw_Init, doesn’t it hit?
  • hello.

    when I set break point in zclSampleSW_Init, the code is stop in break point once.

    1. can you tell me when I run a zigbee sample app which sections run respectively?

    2. if I need to manage packets between zigbee modules in a network, which sections I need to change? I mean I want to write a program that the coordinator send packets to network and each zigbee module can receive this packet and if it is for him response to it.   

  • 1. When Zigbee application is running, osal_run_system will check if there is any task to do and send tasks in queue to different event loop according to taskID registered in osalInitTasks. For example, zclSampleSw_event_loop will process the event sent to application with zclSampleSw_TaskID.

    2. On sender side, you can use AF_DataRequest to send packets in your application. For receiver side, it will receive the message on zcl_ProcessMessageMSG.

  • 1. how can I create an event that OSAL response to it? for example if I want to blink an led in normal mode and when a key pressed, another LED blinking, can you tell me which sections I have to change and how?
    2.Is sender most be a coordinator or it can be a router or end device?
    3. where is the AF_request and zcl_ProcessMessageMSG in sample light or sample switch?
    4.how can I use this Item?
    5. If I want to send a message from a zigbee module to another for turn a light on, how I can do this?
  • 1. You can refer to SAMPLEAPP_END_DEVICE_REJOIN_EVT in zcl_samplesw.h/zcl_samplesw.c to know how to create an event.
    2. Router and end device can also send messages.
    3. Both SampleLight and SampleSwitch can use AF_DataRequest (in AF.c) and zcl_ProcessMessageMSG (in zcl.c).
    4&5. You can test SampleLight and SampleSwitch examples to know more details.
  • 1.for create event I have to call zclSampleLight_event_loop(taskid, event). am I right?
    2.if the answer of 1 is yes, where do I call it? and how can I define task ID and event?
    3.I can't find AF.c and zcl.c. can you please tell me where can I find them?
    5.What do you mean from test it? how do I test it?
    6.can you introduce me a site that have some example to explain sample app of zstack step by step?
  • 1&2. No. You should define your own event ID like SAMPLEAPP_END_DEVICE_REJOIN_EVT in zcl_samplesw.h and call osal_start_timerEx to start the event. Then, add event processing in zclSampleSw_event_loop.

    3.

    5&6. The only guide/document is Z-Stack 3.0 Sample Application User's Guide.pdf under Z-Stack Documents folder.

  • I define my event ID like this.

    and call osal_start_timerEx  here

    and then write my event processing here

    but it never run zclSampleSw_event_loop and my code doesn't work.

    what is my wrong here?

  • I suppose you don't even define ZCL_DIAGNOSTIC in your project so it won't go into the code you add. By the way, It should be "osal_start_timerEx(zclSampleSw_TaskID,TURNON_LIGHT_EVT,500);" instead of "osal_start_timerEx();".
  • I add "#define zclDiagnostic_InitStats() SUCCESS" to zcl_samplelight/sw.c but the if condition is not TRUE and if block doesn't run. I mean when I run the program step by step it jumps from if.
    I mean this if:
    if ( zclDiagnostic_InitStats() == ZSuccess )
    {
    // Here the user could start the timer to save Diagnostics to NV
    osal_start_timerEx(zclSampleLight_TaskID,TURNON_LIGHT_EVT,500);
    }
  • Why don’t you move osal_start_timerEx out of the “if...” statement?
  • it doesn't go to zclSampleLight_event_loop. after start timer I should call this method? if no what do I do?
  • Check return value of osal_start_timerEx to see if TURNON_LIGHT_EVT event is scheduled successfully.
  • 1.how can I check the return value of osal_start_timerEx?

    2. I change the code like this:

    when I run my code the LED1 only turn on and don't blinking. Am I doing this work correctly?

  • Why do you put zclSampleLight_event_loop under the line of osal_start_timerEx?
  • because it doesn't run and I don't know how to check the return value of osal_start_timerEx. so I call it. what do I do for run this method without calling it?
  • Use the following code to get returned value and check the ret value.

    uint8 ret;
    ret=osal_start_timerEx(...);
  • I write this code:
    ret = osal_start_timerEx(zclSampleLight_TaskID,TURNON_LIGHT_EVT,500);
    if(ret){
    HalLedSet(HAL_LED_2,HAL_LED_MODE_ON);}
    and when I run the program it jumps if statement. do this mean TURNON_LIGHT_EVT event is scheduled not successfully?
  • osal_start_timerEx returning 0 means success.
  • so why the zclSampleLight_event_loop doesn't run? when it doesn't run if statement that means ret=0 but the zclSampleLight_event_loop doesn't run.
  • Do you run IAR Debugger to check if ret is equal to 0 ?
  • yes. I use this code to check ret value. I set a break point and then debug the code. I see if statement doesn't run and this mean ret value is equal to  0.

  • Can you show me where you put TURNON_LIGHT_EVT in zclSampleLight_event_loop?
  • I define the event in zcl_samplelight.h:

    and then define event processor in zclSampleLight_event_loop(...) in zcl_samplelight.c:

    I try to debug this code without if statement but it doesn't run. and when I call the zclSampleLight_event_loop(..) like something I explain in last comments the if statement is running. I mean the condition of if is true.

  • I do the same test on my CC2530DK and I can see it hits breakpoint inside TURNON_LIGHT_EVT event.

    I attached my zcl_samplelight.h/zcl_samplelight.c for your reference.

    5078.zcl_samplelight.h

    /**************************************************************************************************
      Filename:       zcl_sampleLight.c
      Revised:        $Date: 2014-10-24 16:04:46 -0700 (Fri, 24 Oct 2014) $
      Revision:       $Revision: 40796 $
    
    
      Description:    Zigbee Cluster Library - sample light application.
    
    
      Copyright 2006-2014 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 Light, based on Z-Stack 3.0. It can be configured as an
      On/Off light or as a dimmable light, by undefining or defining ZCL_LEVEL_CTRL, respectively.
    
      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 applicetion.
      
      Application-specific UI peripherals being used:
    
      - LEDs:
        LED1 reflect the current light state (On / Off accordingly).
    
      Application-specific menu system:
    
        <TOGGLE LIGHT> Toggle the local light and display its status and level
          Press OK to toggle the local light on and off.
          This screen shows the following information
            Line1: (only populated if ZCL_LEVEL_CTRL is defined)
              LEVEL XXX - xxx is the current level of the light if the light state is ON, or the target level
                of the light when the light state is off. The target level is the level that the light will be
                set to when it is switched from off to on using the on or the toggle commands.
            Line2:
              LIGHT OFF / ON: shows the current state of the light.
          Note when ZCL_LEVEL_CTRL is enabled:
            - If the light state is ON and the light level is X, and then the light receives the OFF or TOGGLE 
              commands: The level will decrease gradually until it reaches 1, and only then the light state will
              be changed to OFF. The level then will be restored to X, with the state staying OFF. At this stage
              the light is not lighting, and the level represent the target level for the next ON or TOGGLE 
              commands.
            - If the light state is OFF and the light level is X, and then the light receives the ON or TOGGLE
              commands; The level will be set to 1, the light state will be set to ON, and then the level will
              increase gradually until it reaches level X.
            - Any level-setting command will affect the level directly, and may also affect the on/off state,
              depending on the command's arguments.       
    
    *********************************************************************/
    
    /*********************************************************************
     * INCLUDES
     */
    #include "ZComDef.h"
    #include "OSAL.h"
    #include "AF.h"
    #include "ZDApp.h"
    #include "ZDObject.h"
    #include "MT_SYS.h"
    
    #include "nwk_util.h"
    
    #include "zcl.h"
    #include "zcl_general.h"
    #include "zcl_ha.h"
    #include "zcl_diagnostic.h"
    
    #include "zcl_samplelight.h"
       
    #include "bdb.h"
    #include "bdb_interface.h"
    
     //GP_UPDATE
    #include "gp_interface.h"
       
    #include "onboard.h"
    
    /* HAL */
    #include "hal_lcd.h"
    #include "hal_led.h"
    #include "hal_key.h"
    
    #include "NLMEDE.h"
    
    // Added to include TouchLink initiator functionality 
    #if defined ( BDB_TL_INITIATOR )
      #include "bdb_touchlink_initiator.h"
    #endif // BDB_TL_INITIATOR
    
    #if defined ( BDB_TL_TARGET )
      #include "bdb_touchlink_target.h"
    #endif // BDB_TL_TARGET
    
    #if defined ( BDB_TL_INITIATOR ) || defined ( BDB_TL_TARGET )
      #include "bdb_touchlink.h"
    #endif
    
    #include "zcl_sampleapps_ui.h"
    
    /*********************************************************************
     * 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 Light"
    
    /*********************************************************************
     * TYPEDEFS
     */
    
    /*********************************************************************
     * GLOBAL VARIABLES
     */
    byte zclSampleLight_TaskID;
    uint8 zclSampleLightSeqNum;
    
    /*********************************************************************
     * GLOBAL FUNCTIONS
     */
    
    /*********************************************************************
     * LOCAL VARIABLES
     */
    afAddrType_t zclSampleLight_DstAddr;
    
    // Test Endpoint to allow SYS_APP_MSGs
    static endPointDesc_t sampleLight_TestEp =
    {
      SAMPLELIGHT_ENDPOINT,
      0,
      &zclSampleLight_TaskID,
      (SimpleDescriptionFormat_t *)NULL,  // No Simple description for this test endpoint
      (afNetworkLatencyReq_t)0            // No Network Latency req
    };
    
    #ifdef ZCL_LEVEL_CTRL
    uint8 zclSampleLight_WithOnOff;       // set to TRUE if state machine should set light on/off
    uint8 zclSampleLight_NewLevel;        // new level when done moving
    uint8 zclSampleLight_LevelChangeCmd; // current level change was triggered by an on/off command
    bool  zclSampleLight_NewLevelUp;      // is direction to new level up or down?
    int32 zclSampleLight_CurrentLevel32;  // current level, fixed point (e.g. 192.456)
    int32 zclSampleLight_Rate32;          // rate in units, fixed point (e.g. 16.123)
    uint8 zclSampleLight_LevelLastLevel;  // to save the Current Level before the light was turned OFF
    #endif
    
    /*********************************************************************
     * LOCAL FUNCTIONS
     */
    static void zclSampleLight_HandleKeys( byte shift, byte keys );
    static void zclSampleLight_BasicResetCB( void );
    static void zclSampleLight_OnOffCB( uint8 cmd );
    //GP_UPDATE
    #if (ZG_BUILD_RTR_TYPE)
    static void gp_CommissioningMode(bool isEntering);
    static uint8 gp_ChangeChannelReq(void);
    #endif
    
    
    static void zclSampleLight_ProcessCommissioningStatus(bdbCommissioningModeMsg_t *bdbCommissioningModeMsg);
    
    
    #ifdef ZCL_LEVEL_CTRL
    static void zclSampleLight_LevelControlMoveToLevelCB( zclLCMoveToLevel_t *pCmd );
    static void zclSampleLight_LevelControlMoveCB( zclLCMove_t *pCmd );
    static void zclSampleLight_LevelControlStepCB( zclLCStep_t *pCmd );
    static void zclSampleLight_LevelControlStopCB( void );
    static void zclSampleLight_DefaultMove( uint8 OnOff );
    static uint32 zclSampleLight_TimeRateHelper( uint8 newLevel );
    static uint16 zclSampleLight_GetTime ( uint8 level, uint16 time );
    static void zclSampleLight_MoveBasedOnRate( uint8 newLevel, uint32 rate );
    static void zclSampleLight_MoveBasedOnTime( uint8 newLevel, uint16 time );
    static void zclSampleLight_AdjustLightLevel( void );
    #endif
    
    // Functions to process ZCL Foundation incoming Command/Response messages
    static void zclSampleLight_ProcessIncomingMsg( zclIncomingMsg_t *msg );
    #ifdef ZCL_READ
    static uint8 zclSampleLight_ProcessInReadRspCmd( zclIncomingMsg_t *pInMsg );
    #endif
    #ifdef ZCL_WRITE
    static uint8 zclSampleLight_ProcessInWriteRspCmd( zclIncomingMsg_t *pInMsg );
    #endif
    static uint8 zclSampleLight_ProcessInDefaultRspCmd( zclIncomingMsg_t *pInMsg );
    #ifdef ZCL_DISCOVER
    static uint8 zclSampleLight_ProcessInDiscCmdsRspCmd( zclIncomingMsg_t *pInMsg );
    static uint8 zclSampleLight_ProcessInDiscAttrsRspCmd( zclIncomingMsg_t *pInMsg );
    static uint8 zclSampleLight_ProcessInDiscAttrsExtRspCmd( zclIncomingMsg_t *pInMsg );
    #endif
    
    static void zclSampleApp_BatteryWarningCB( uint8 voltLevel);
    
    void zclSampleLight_UiActionToggleLight(uint16 keys);
    void zclSampleLight_UiUpdateLcd(uint8 uiCurrentState, char * line[3]);
    void zclSampleLight_UpdateLedState(void);
    
    /*********************************************************************
     * CONSTANTS
     */
    const uiState_t zclSampleLight_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, &zclSampleLight_UiActionToggleLight},
    };
    
    #define LEVEL_CHANGED_BY_LEVEL_CMD  0
    #define LEVEL_CHANGED_BY_ON_CMD     1
    #define LEVEL_CHANGED_BY_OFF_CMD    2
    
    /*********************************************************************
     * STATUS STRINGS
     */
    #ifdef LCD_SUPPORTED
      const char sLightOn[]      = "   LIGHT ON     ";
      const char sLightOff[]     = "   LIGHT OFF    ";
      #ifdef ZCL_LEVEL_CTRL
        char sLightLevel[]        = "   LEVEL ###    "; // displays level 1-254
      #endif
    #endif
    
    /*********************************************************************
     * REFERENCED EXTERNALS
     */
    extern int16 zdpExternalStateTaskID;
    
    /*********************************************************************
     * ZCL General Profile Callback table
     */
    static zclGeneral_AppCallbacks_t zclSampleLight_CmdCallbacks =
    {
      zclSampleLight_BasicResetCB,            // Basic Cluster Reset command
      NULL,                                   // Identify Trigger Effect command
      zclSampleLight_OnOffCB,                 // 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
      zclSampleLight_LevelControlMoveToLevelCB, // Level Control Move to Level command
      zclSampleLight_LevelControlMoveCB,        // Level Control Move command
      zclSampleLight_LevelControlStepCB,        // Level Control Step command
      zclSampleLight_LevelControlStopCB,        // 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          zclSampleLight_Init
     *
     * @brief       Initialization function for the zclGeneral layer.
     *
     * @param       none
     *
     * @return      none
     */
    void zclSampleLight_Init( byte task_id )
    {
      zclSampleLight_TaskID = task_id;
    
      // Set destination address to indirect
      zclSampleLight_DstAddr.addrMode = (afAddrMode_t)AddrNotPresent;
      zclSampleLight_DstAddr.endPoint = 0;
      zclSampleLight_DstAddr.addr.shortAddr = 0;
    
      // Register the Simple Descriptor for this application
      bdb_RegisterSimpleDescriptor( &zclSampleLight_SimpleDesc );
    
      // Register the ZCL General Cluster Library callback functions
      zclGeneral_RegisterCmdCallbacks( SAMPLELIGHT_ENDPOINT, &zclSampleLight_CmdCallbacks );
    
      // Register the application's attribute list
      zclSampleLight_ResetAttributesToDefaultValues();
      zcl_registerAttrList( SAMPLELIGHT_ENDPOINT, zclSampleLight_NumAttributes, zclSampleLight_Attrs );
    
    #ifdef ZCL_LEVEL_CTRL
      zclSampleLight_LevelLastLevel = zclSampleLight_LevelCurrentLevel;
    #endif
    
      // Register the Application to receive the unprocessed Foundation command/response messages
      zcl_registerForMsg( zclSampleLight_TaskID );
    
    #ifdef ZCL_DISCOVER
      // Register the application's command list
      zcl_registerCmdList( SAMPLELIGHT_ENDPOINT, zclCmdsArraySize, zclSampleLight_Cmds );
    #endif
    
      // Register low voltage NV memory protection application callback
      RegisterVoltageWarningCB( zclSampleApp_BatteryWarningCB );
    
      // Register for all key events - This app will handle all key events
      RegisterForKeys( zclSampleLight_TaskID );
      
      bdb_RegisterCommissioningStatusCB( zclSampleLight_ProcessCommissioningStatus );
      
      // Register for a test endpoint
      afRegister( &sampleLight_TestEp );
    
    #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( SAMPLELIGHT_ENDPOINT, zclDiagnostic_ReadWriteAttrCB, NULL );
    
      if ( zclDiagnostic_InitStats() == ZSuccess )
      {
        // Here the user could start the timer to save Diagnostics to NV
      }
    #endif
      
    //GP_UPDATE  
    #if (ZG_BUILD_RTR_TYPE)  
      gp_RegisterCommissioningModeCB(gp_CommissioningMode);
      gp_RegisterGPChangeChannelReqCB(gp_ChangeChannelReq);
    #endif
      
      zdpExternalStateTaskID = zclSampleLight_TaskID;
    
      UI_Init(zclSampleLight_TaskID, SAMPLEAPP_LCD_AUTO_UPDATE_EVT, SAMPLEAPP_KEY_AUTO_REPEAT_EVT, &zclSampleLight_IdentifyTime, APP_TITLE, &zclSampleLight_UiUpdateLcd, zclSampleLight_UiStatesMain);
    
      UI_UpdateLcd();
      osal_start_timerEx(zclSampleLight_TaskID,TURNON_LIGHT_EVT,500);
    }
    
    /*********************************************************************
     * @fn          zclSample_event_loop
     *
     * @brief       Event Loop Processor for zclGeneral.
     *
     * @param       none
     *
     * @return      none
     */
    uint16 zclSampleLight_event_loop( uint8 task_id, uint16 events )
    {
      afIncomingMSGPacket_t *MSGpkt;
    
      (void)task_id;  // Intentionally unreferenced parameter
    
      if ( events & SYS_EVENT_MSG )
      {
        while ( (MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( zclSampleLight_TaskID )) )
        {
          switch ( MSGpkt->hdr.event )
          {
            case ZCL_INCOMING_MSG:
              // Incoming ZCL Foundation command/response messages
              zclSampleLight_ProcessIncomingMsg( (zclIncomingMsg_t *)MSGpkt );
              break;
    
            case KEY_CHANGE:
              zclSampleLight_HandleKeys( ((keyChange_t *)MSGpkt)->state, ((keyChange_t *)MSGpkt)->keys );
              break;
    
            case ZDO_STATE_CHANGE:
              UI_DeviceStateUpdated((devStates_t)(MSGpkt->hdr.status));
              break;
    
            default:
              break;
          }
    
          // Release the memory
          osal_msg_deallocate( (uint8 *)MSGpkt );
        }
    
        // return unprocessed events
        return (events ^ SYS_EVENT_MSG);
      }
    
    #ifdef ZCL_LEVEL_CTRL
      if ( events & SAMPLELIGHT_LEVEL_CTRL_EVT )
      {
        zclSampleLight_AdjustLightLevel();
        return ( events ^ SAMPLELIGHT_LEVEL_CTRL_EVT );
      }
    #endif
    
    #if ZG_BUILD_ENDDEVICE_TYPE    
      if ( events & SAMPLEAPP_END_DEVICE_REJOIN_EVT )
      {
        bdb_ZedAttemptRecoverNwk();
        return ( events ^ SAMPLEAPP_END_DEVICE_REJOIN_EVT );
      }
    #endif
    
      if ( events & SAMPLEAPP_LCD_AUTO_UPDATE_EVT )
      {
        UI_UpdateLcd();
        return ( events ^ SAMPLEAPP_LCD_AUTO_UPDATE_EVT );
      }
    
      if ( events & SAMPLEAPP_KEY_AUTO_REPEAT_EVT )
      {
        UI_MainStateMachine(UI_KEY_AUTO_PRESSED);
        return ( events ^ SAMPLEAPP_KEY_AUTO_REPEAT_EVT );
      }
      if ( events & TURNON_LIGHT_EVT )
      {
        HalLedBlink(HAL_LED_1,10, 50, 500); 
        return ( events ^ TURNON_LIGHT_EVT );
      }
    
      // Discard unknown events
      return 0;
    }
    
    
    /*********************************************************************
     * @fn      zclSampleLight_HandleKeys
     *
     * @brief   Handles all key events for this device.
     *
     * @param   shift - true if in shift/alt.
     * @param   keys - bit field for key events. Valid entries:
     *                 HAL_KEY_SW_5
     *                 HAL_KEY_SW_4
     *                 HAL_KEY_SW_2
     *                 HAL_KEY_SW_1
     *
     * @return  none
     */
    static void zclSampleLight_HandleKeys( byte shift, byte keys )
    {
      UI_MainStateMachine(keys);
    }
    
    //GP_UPDATE
    #if (ZG_BUILD_RTR_TYPE)
    /*********************************************************************
     * @fn      gp_CommissioningMode
     *
     * @brief   Callback that notifies the application that gp Proxy is entering 
     *          into commissioning mode
     *
     * @param   isEntering - 
     *
     * @return  
     */
    static void gp_CommissioningMode(bool isEntering)
    {
      if(isEntering)
      {
        //Led on indicating enter commissioning mode
      }
      else
      {
        //Led off indicating enter commissioning mode
      }
    }
    
    
    
    //GP_UPDATE
    /*********************************************************************
     * @fn      gp_ChangeChannelReq
     *
     * @brief   Callback function to notify the application about a GP commissioning 
     * request that will change the current channel for at most 
     * gpBirectionalCommissioningChangeChannelTimeout ms
     *
     * @param   channel - Channel in which the commissioning will take place
     *
     * @return  TRUE to allow change channel, FALSE to do not allow
     */
    static uint8 gp_ChangeChannelReq(void)
    {
      bool allowChangeChannel = TRUE;
      
      //Check application state to decide if allow change channel or not
      
      return allowChangeChannel;
    }
    
    #endif
    
    
    /*********************************************************************
     * @fn      zclSampleLight_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 zclSampleLight_ProcessCommissioningStatus(bdbCommissioningModeMsg_t *bdbCommissioningModeMsg)
    {
      switch(bdbCommissioningModeMsg->bdbCommissioningMode)
      {
        case BDB_COMMISSIONING_FORMATION:
          if(bdbCommissioningModeMsg->bdbCommissioningStatus == BDB_COMMISSIONING_SUCCESS)
          {
            //After formation, perform nwk steering again plus the remaining commissioning modes that has not been process yet
            bdb_StartCommissioning(BDB_COMMISSIONING_MODE_NWK_STEERING | bdbCommissioningModeMsg->bdbRemainingCommissioningModes);
          }
          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
            osal_start_timerEx(zclSampleLight_TaskID, SAMPLEAPP_END_DEVICE_REJOIN_EVT, SAMPLEAPP_END_DEVICE_REJOIN_DELAY);
          }
        break;
    #endif 
      }
      
      UI_UpdateComissioningStatus(bdbCommissioningModeMsg);
    }
    
    /*********************************************************************
     * @fn      zclSampleLight_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 zclSampleLight_BasicResetCB( void )
    {
      //Reset every attribute in all supported cluster to their default value.
    
      zclSampleLight_ResetAttributesToDefaultValues();
    
      zclSampleLight_UpdateLedState();
    
      // update the display
      UI_UpdateLcd( ); 
    }
    
    /*********************************************************************
     * @fn      zclSampleLight_OnOffCB
     *
     * @brief   Callback from the ZCL General Cluster Library when
     *          it received an On/Off Command for this application.
     *
     * @param   cmd - COMMAND_ON, COMMAND_OFF or COMMAND_TOGGLE
     *
     * @return  none
     */
    static void zclSampleLight_OnOffCB( uint8 cmd )
    {
      afIncomingMSGPacket_t *pPtr = zcl_getRawAFMsg();
    
      uint8 OnOff;
    
      zclSampleLight_DstAddr.addr.shortAddr = pPtr->srcAddr.addr.shortAddr;
    
    
      // Turn on the light
      if ( cmd == COMMAND_ON )
      {
        OnOff = LIGHT_ON;
      }
      // Turn off the light
      else if ( cmd == COMMAND_OFF )
      {
        OnOff = LIGHT_OFF;
      }
      // Toggle the light
      else if ( cmd == COMMAND_TOGGLE )
      {
    #ifdef ZCL_LEVEL_CTRL
        if (zclSampleLight_LevelRemainingTime > 0) 
        {
          if (zclSampleLight_NewLevelUp)
          {
            OnOff = LIGHT_OFF;
          }
          else
          {
            OnOff = LIGHT_ON;
          }
        }
        else
        {
          if (zclSampleLight_OnOff == LIGHT_ON)
          {
            OnOff = LIGHT_OFF;
          }
          else
          {
            OnOff = LIGHT_ON;
          }
        }
    #else
        if (zclSampleLight_OnOff == LIGHT_ON)
        {
          OnOff = LIGHT_OFF;
        }
        else
        {
          OnOff = LIGHT_ON;
        }
    #endif
      }
    
    #ifdef ZCL_LEVEL_CTRL
      zclSampleLight_LevelChangeCmd = (OnOff == LIGHT_ON ? LEVEL_CHANGED_BY_ON_CMD : LEVEL_CHANGED_BY_OFF_CMD);
    
      zclSampleLight_DefaultMove(OnOff);
    #else
      zclSampleLight_OnOff = OnOff;
    #endif
    
      zclSampleLight_UpdateLedState();
      
      // update the display
      UI_UpdateLcd( );
    }
    
    #ifdef ZCL_LEVEL_CTRL
    /*********************************************************************
     * @fn      zclSampleLight_TimeRateHelper
     *
     * @brief   Calculate time based on rate, and startup level state machine
     *
     * @param   newLevel - new level for current level
     *
     * @return  diff (directly), zclSampleLight_CurrentLevel32 and zclSampleLight_NewLevel, zclSampleLight_NewLevelUp
     */
    static uint32 zclSampleLight_TimeRateHelper( uint8 newLevel )
    {
      uint32 diff;
      uint32 newLevel32;
    
      // remember current and new level
      zclSampleLight_NewLevel = newLevel;
      zclSampleLight_CurrentLevel32 = (uint32)1000 * zclSampleLight_LevelCurrentLevel;
    
      // calculate diff
      newLevel32 = (uint32)1000 * newLevel;
      if ( zclSampleLight_LevelCurrentLevel > newLevel )
      {
        diff = zclSampleLight_CurrentLevel32 - newLevel32;
        zclSampleLight_NewLevelUp = FALSE;  // moving down
      }
      else
      {
        diff = newLevel32 - zclSampleLight_CurrentLevel32;
        zclSampleLight_NewLevelUp = TRUE;   // moving up
      }
    
      return ( diff );
    }
    
    /*********************************************************************
     * @fn      zclSampleLight_MoveBasedOnRate
     *
     * @brief   Calculate time based on rate, and startup level state machine
     *
     * @param   newLevel - new level for current level
     * @param   rate16   - fixed point rate (e.g. 16.123)
     *
     * @return  none
     */
    static void zclSampleLight_MoveBasedOnRate( uint8 newLevel, uint32 rate )
    {
      uint32 diff;
    
      // determine how much time (in 10ths of seconds) based on the difference and rate
      zclSampleLight_Rate32 = rate;
      diff = zclSampleLight_TimeRateHelper( newLevel );
      zclSampleLight_LevelRemainingTime = diff / rate;
      if ( !zclSampleLight_LevelRemainingTime )
      {
        zclSampleLight_LevelRemainingTime = 1;
      }
    
      osal_start_timerEx( zclSampleLight_TaskID, SAMPLELIGHT_LEVEL_CTRL_EVT, 100 );
    }
    
    /*********************************************************************
     * @fn      zclSampleLight_MoveBasedOnTime
     *
     * @brief   Calculate rate based on time, and startup level state machine
     *
     * @param   newLevel  - new level for current level
     * @param   time      - in 10ths of seconds
     *
     * @return  none
     */
    static void zclSampleLight_MoveBasedOnTime( uint8 newLevel, uint16 time )
    {
      uint16 diff;
    
      // determine rate (in units) based on difference and time
      diff = zclSampleLight_TimeRateHelper( newLevel );
      zclSampleLight_LevelRemainingTime = zclSampleLight_GetTime( newLevel, time );
      zclSampleLight_Rate32 = diff / time;
    
      osal_start_timerEx( zclSampleLight_TaskID, SAMPLELIGHT_LEVEL_CTRL_EVT, 100 );
    }
    
    /*********************************************************************
     * @fn      zclSampleLight_GetTime
     *
     * @brief   Determine amount of time that MoveXXX will take to complete.
     *
     * @param   level = new level to move to
     *          time  = 0xffff=default, or 0x0000-n amount of time in tenths of seconds.
     *
     * @return  none
     */
    static uint16 zclSampleLight_GetTime( uint8 newLevel, uint16 time )
    {
      // there is a hiearchy of the amount of time to use for transistioning
      // check each one in turn. If none of defaults are set, then use fastest
      // time possible.
      if ( time == 0xFFFF )
      {
        // use On or Off Transition Time if set (not 0xffff)
        if ( zclSampleLight_LevelCurrentLevel > newLevel )
        {
          time = zclSampleLight_LevelOffTransitionTime;
        }
        else
        {
          time = zclSampleLight_LevelOnTransitionTime;
        }
    
        // else use OnOffTransitionTime if set (not 0xffff)
        if ( time == 0xFFFF )
        {
          time = zclSampleLight_LevelOnOffTransitionTime;
        }
    
        // else as fast as possible
        if ( time == 0xFFFF )
        {
          time = 1;
        }
      }
    
      if ( time == 0 )
      {
        time = 1; // as fast as possible
      }
    
      return ( time );
    }
    
    /*********************************************************************
     * @fn      zclSampleLight_DefaultMove
     *
     * @brief   We were turned on/off. Use default time to move to on or off.
     *
     * @param   zclSampleLight_OnOff - must be set prior to calling this function.
     *
     * @return  none
     */
    static void zclSampleLight_DefaultMove( uint8 OnOff )
    {
      uint8  newLevel;
      uint32 rate;      // fixed point decimal (3 places, eg. 16.345)
      uint16 time;
    
      // if moving to on position, move to on level
      if ( OnOff )
      {
        if (zclSampleLight_OnOff == LIGHT_OFF)
        {
          zclSampleLight_LevelCurrentLevel = ATTR_LEVEL_MIN_LEVEL;
        }
        
        if ( zclSampleLight_LevelOnLevel == ATTR_LEVEL_ON_LEVEL_NO_EFFECT )
        {
          // The last Level (before going OFF) should be used)
          newLevel = zclSampleLight_LevelLastLevel;
        }
        else
        {
          newLevel = zclSampleLight_LevelOnLevel;
        }
    
        time = zclSampleLight_LevelOnTransitionTime;
    
      }
      else
      {
        newLevel = ATTR_LEVEL_MIN_LEVEL;
    
        time = zclSampleLight_LevelOffTransitionTime;
      }
    
      // else use OnOffTransitionTime if set (not 0xffff)
      if ( time == 0xFFFF )
      {
        time = zclSampleLight_LevelOnOffTransitionTime;
      }
    
      // else as fast as possible
      if ( time == 0xFFFF )
      {
        time = 1;
      }
    
      // calculate rate based on time (int 10ths) for full transition (1-254)
      rate = 255000 / time;    // units per tick, fixed point, 3 decimal places (e.g. 8500 = 8.5 units per tick)
    
      // start up state machine.
      zclSampleLight_WithOnOff = TRUE;
      zclSampleLight_MoveBasedOnRate( newLevel, rate );
    }
    
    /*********************************************************************
     * @fn      zclSampleLight_AdjustLightLevel
     *
     * @brief   Called each 10th of a second while state machine running
     *
     * @param   none
     *
     * @return  none
     */
    static void zclSampleLight_AdjustLightLevel( void )
    {
      // one tick (10th of a second) less
      if ( zclSampleLight_LevelRemainingTime )
      {
        --zclSampleLight_LevelRemainingTime;
      }
    
      // no time left, done
      if ( zclSampleLight_LevelRemainingTime == 0)
      {
        zclSampleLight_LevelCurrentLevel = zclSampleLight_NewLevel;
      }
    
      // still time left, keep increment/decrementing
      else
      {
        if ( zclSampleLight_NewLevelUp )
        {
          zclSampleLight_CurrentLevel32 += zclSampleLight_Rate32;
        }
        else
        {
          zclSampleLight_CurrentLevel32 -= zclSampleLight_Rate32;
        }
        zclSampleLight_LevelCurrentLevel = (uint8)( zclSampleLight_CurrentLevel32 / 1000 );
      }
    
      if (( zclSampleLight_LevelChangeCmd == LEVEL_CHANGED_BY_LEVEL_CMD ) && ( zclSampleLight_LevelOnLevel == ATTR_LEVEL_ON_LEVEL_NO_EFFECT ))
      {
        zclSampleLight_LevelLastLevel = zclSampleLight_LevelCurrentLevel;
      }
    
      // also affect on/off
      if ( zclSampleLight_WithOnOff )
      {
        if ( zclSampleLight_LevelCurrentLevel > ATTR_LEVEL_MIN_LEVEL )
        {
          zclSampleLight_OnOff = LIGHT_ON;
        }
        else
        {
          if (zclSampleLight_LevelChangeCmd != LEVEL_CHANGED_BY_ON_CMD)
          {
            zclSampleLight_OnOff = LIGHT_OFF;
          }
          else
          {
            zclSampleLight_OnOff = LIGHT_ON;
          }
          
          if (( zclSampleLight_LevelChangeCmd != LEVEL_CHANGED_BY_LEVEL_CMD ) && ( zclSampleLight_LevelOnLevel == ATTR_LEVEL_ON_LEVEL_NO_EFFECT ))
          {
            zclSampleLight_LevelCurrentLevel = zclSampleLight_LevelLastLevel;
          }
        }
      }
    
      zclSampleLight_UpdateLedState();
      
      // display light level as we go
      UI_UpdateLcd( );
    
      // keep ticking away
      if ( zclSampleLight_LevelRemainingTime )
      {
        osal_start_timerEx( zclSampleLight_TaskID, SAMPLELIGHT_LEVEL_CTRL_EVT, 100 );
      }
    }
    
    /*********************************************************************
     * @fn      zclSampleLight_LevelControlMoveToLevelCB
     *
     * @brief   Callback from the ZCL General Cluster Library when
     *          it received a LevelControlMoveToLevel Command for this application.
     *
     * @param   pCmd - ZigBee command parameters
     *
     * @return  none
     */
    static void zclSampleLight_LevelControlMoveToLevelCB( zclLCMoveToLevel_t *pCmd )
    {
      zclSampleLight_LevelChangeCmd = LEVEL_CHANGED_BY_LEVEL_CMD;
    
      zclSampleLight_WithOnOff = pCmd->withOnOff;
      zclSampleLight_MoveBasedOnTime( pCmd->level, pCmd->transitionTime );
    }
    
    /*********************************************************************
     * @fn      zclSampleLight_LevelControlMoveCB
     *
     * @brief   Callback from the ZCL General Cluster Library when
     *          it received a LevelControlMove Command for this application.
     *
     * @param   pCmd - ZigBee command parameters
     *
     * @return  none
     */
    static void zclSampleLight_LevelControlMoveCB( zclLCMove_t *pCmd )
    {
      uint8 newLevel;
      uint32 rate;
    
      // convert rate from units per second to units per tick (10ths of seconds)
      // and move at that right up or down
      zclSampleLight_WithOnOff = pCmd->withOnOff;
    
      if ( pCmd->moveMode == LEVEL_MOVE_UP )
      {
        newLevel = ATTR_LEVEL_MAX_LEVEL;  // fully on
      }
      else
      {
        newLevel = ATTR_LEVEL_MIN_LEVEL; // fully off
      }
    
      zclSampleLight_LevelChangeCmd = LEVEL_CHANGED_BY_LEVEL_CMD;
    
      rate = (uint32)100 * pCmd->rate;
      zclSampleLight_MoveBasedOnRate( newLevel, rate );
    }
    
    /*********************************************************************
     * @fn      zclSampleLight_LevelControlStepCB
     *
     * @brief   Callback from the ZCL General Cluster Library when
     *          it received an On/Off Command for this application.
     *
     * @param   pCmd - ZigBee command parameters
     *
     * @return  none
     */
    static void zclSampleLight_LevelControlStepCB( zclLCStep_t *pCmd )
    {
      uint8 newLevel;
    
      // determine new level, but don't exceed boundaries
      if ( pCmd->stepMode == LEVEL_MOVE_UP )
      {
        if ( (uint16)zclSampleLight_LevelCurrentLevel + pCmd->amount > ATTR_LEVEL_MAX_LEVEL )
        {
          newLevel = ATTR_LEVEL_MAX_LEVEL;
        }
        else
        {
          newLevel = zclSampleLight_LevelCurrentLevel + pCmd->amount;
        }
      }
      else
      {
        if ( pCmd->amount >= zclSampleLight_LevelCurrentLevel )
        {
          newLevel = ATTR_LEVEL_MIN_LEVEL;
        }
        else
        {
          newLevel = zclSampleLight_LevelCurrentLevel - pCmd->amount;
        }
      }
      
      zclSampleLight_LevelChangeCmd = LEVEL_CHANGED_BY_LEVEL_CMD;
    
      // move to the new level
      zclSampleLight_WithOnOff = pCmd->withOnOff;
      zclSampleLight_MoveBasedOnTime( newLevel, pCmd->transitionTime );
    }
    
    /*********************************************************************
     * @fn      zclSampleLight_LevelControlStopCB
     *
     * @brief   Callback from the ZCL General Cluster Library when
     *          it received an Level Control Stop Command for this application.
     *
     * @param   pCmd - ZigBee command parameters
     *
     * @return  none
     */
    static void zclSampleLight_LevelControlStopCB( void )
    {
      // stop immediately
      osal_stop_timerEx( zclSampleLight_TaskID, SAMPLELIGHT_LEVEL_CTRL_EVT );
      zclSampleLight_LevelRemainingTime = 0;
    }
    #endif
    
    /*********************************************************************
     * @fn      zclSampleApp_BatteryWarningCB
     *
     * @brief   Called to handle battery-low situation.
     *
     * @param   voltLevel - level of severity
     *
     * @return  none
     */
    void zclSampleApp_BatteryWarningCB( uint8 voltLevel )
    {
      if ( voltLevel == VOLT_LEVEL_CAUTIOUS )
      {
        // Send warning message to the gateway and blink LED
      }
      else if ( voltLevel == VOLT_LEVEL_BAD )
      {
        // Shut down the system
      }
    }
    
    /******************************************************************************
     *
     *  Functions for processing ZCL Foundation incoming Command/Response messages
     *
     *****************************************************************************/
    
    /*********************************************************************
     * @fn      zclSampleLight_ProcessIncomingMsg
     *
     * @brief   Process ZCL Foundation incoming message
     *
     * @param   pInMsg - pointer to the received message
     *
     * @return  none
     */
    static void zclSampleLight_ProcessIncomingMsg( zclIncomingMsg_t *pInMsg )
    {
      switch ( pInMsg->zclHdr.commandID )
      {
    #ifdef ZCL_READ
        case ZCL_CMD_READ_RSP:
          zclSampleLight_ProcessInReadRspCmd( pInMsg );
          break;
    #endif
    #ifdef ZCL_WRITE
        case ZCL_CMD_WRITE_RSP:
          zclSampleLight_ProcessInWriteRspCmd( pInMsg );
          break;
    #endif
        case ZCL_CMD_CONFIG_REPORT:
        case ZCL_CMD_CONFIG_REPORT_RSP:
        case ZCL_CMD_READ_REPORT_CFG:
        case ZCL_CMD_READ_REPORT_CFG_RSP:
        case ZCL_CMD_REPORT:
          //bdb_ProcessIncomingReportingMsg( pInMsg );
          break;
    
        case ZCL_CMD_DEFAULT_RSP:
          zclSampleLight_ProcessInDefaultRspCmd( pInMsg );
          break;
    #ifdef ZCL_DISCOVER
        case ZCL_CMD_DISCOVER_CMDS_RECEIVED_RSP:
          zclSampleLight_ProcessInDiscCmdsRspCmd( pInMsg );
          break;
    
        case ZCL_CMD_DISCOVER_CMDS_GEN_RSP:
          zclSampleLight_ProcessInDiscCmdsRspCmd( pInMsg );
          break;
    
        case ZCL_CMD_DISCOVER_ATTRS_RSP:
          zclSampleLight_ProcessInDiscAttrsRspCmd( pInMsg );
          break;
    
        case ZCL_CMD_DISCOVER_ATTRS_EXT_RSP:
          zclSampleLight_ProcessInDiscAttrsExtRspCmd( pInMsg );
          break;
    #endif
        default:
          break;
      }
    
      if ( pInMsg->attrCmd )
        osal_mem_free( pInMsg->attrCmd );
    }
    
    #ifdef ZCL_READ
    /*********************************************************************
     * @fn      zclSampleLight_ProcessInReadRspCmd
     *
     * @brief   Process the "Profile" Read Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleLight_ProcessInReadRspCmd( zclIncomingMsg_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      zclSampleLight_ProcessInWriteRspCmd
     *
     * @brief   Process the "Profile" Write Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleLight_ProcessInWriteRspCmd( zclIncomingMsg_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      zclSampleLight_ProcessInDefaultRspCmd
     *
     * @brief   Process the "Profile" Default Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleLight_ProcessInDefaultRspCmd( zclIncomingMsg_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      zclSampleLight_ProcessInDiscCmdsRspCmd
     *
     * @brief   Process the Discover Commands Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleLight_ProcessInDiscCmdsRspCmd( zclIncomingMsg_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      zclSampleLight_ProcessInDiscAttrsRspCmd
     *
     * @brief   Process the "Profile" Discover Attributes Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleLight_ProcessInDiscAttrsRspCmd( zclIncomingMsg_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      zclSampleLight_ProcessInDiscAttrsExtRspCmd
     *
     * @brief   Process the "Profile" Discover Attributes Extended Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleLight_ProcessInDiscAttrsExtRspCmd( zclIncomingMsg_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 zclSampleLight_UiActionToggleLight(uint16 keys)
    {
      zclSampleLight_OnOffCB(COMMAND_TOGGLE);
    }
    
    void zclSampleLight_UpdateLedState(void)
    {
      // set the LED1 based on light (on or off)
      if ( zclSampleLight_OnOff == LIGHT_ON )
      {
        HalLedSet ( UI_LED_APP, HAL_LED_MODE_ON );
      }
      else
      {
        HalLedSet ( UI_LED_APP, HAL_LED_MODE_OFF );
      }
    }
    
    void zclSampleLight_UiUpdateLcd(uint8 UiState, char * line[3])
    {
    #ifdef LCD_SUPPORTED
    #ifdef ZCL_LEVEL_CTRL
      zclHA_uint8toa( zclSampleLight_LevelCurrentLevel, &sLightLevel[9] );
      line[0] = (char *)sLightLevel;
    #endif // ZCL_LEVEL_CTRL
      line[1] = (char *)(zclSampleLight_OnOff ? sLightOn : sLightOff);
      line[2] = "< TOGGLE LIGHT >";
    #endif
    }
    
    /****************************************************************************
    ****************************************************************************/
    
    
    

  • is CC2530F256 a CC2530DK device?
  • CC2530DK is TI CC2530 EVB which has CC2530F256 on it.
  • my program doesn't stop in break point. can you send me a picture of your cc2530 device and all of your app?
  • For CC2530DK, you can refer to

    Also, my hex file is attached for you reference.

    SampleLight.zip

  • thank you.
    you don't change any thing in samplelight example? you just add define event, timer and event handling?
  • 1.why this if statement isn't true?
    if ( events & TURNON_LIGHT_EVT )
    the timer return value is 0. but this if statement isn't true.
    2.and if I write a code after return (events ^ SYS_EVENT_MSG); it doesn't run. why?
  • Yes, I don’t change any thing in samplelight other than defining event, timer and event handling. I have no idea why your code doesn’t work.
    a
  • I think I understand why my code doesn't work. my event ID id 0x0008 and events is 0x0001 and event id & events =0. so the if statement isn't true. but I don't know why your code run but mine not.
    Now I have another questions.
    1. why my LED doesn't blink. it just turn on and don't blinking.
    2. when I change LED mode to toggle and run the code step by step the led is toggled but when run the code without breakpoints it just turned on. why?
    3. generally when we want to write an event we have to define event Id in zcl_appname.h and then start timer in zclAppName_Init and then write event handling in zclAppnamet_event_loop with HAL drivers. am I right?
    4.if I want to when a send a hello packet for all zigbee module in network , what code I should write? can you explain me step by step?
    5.if I want to receive a data from UART and do something about it, what I should do? can you guide me step by step? for example if user write PANID the module write back its PANID.
    6. can you explain me what do the sample apps of zigbee do?for which work I should choose which one?
    thanks for your help.
  • 1&2. Can you show me your code and elaborate your issue with the code?
    3. If you mean to create an event running in application event loop, yes.
    4. I suggest you to download and install Z-Stack mesh 1.0.0 which contains GenericApp example showing you how to send and receive “Hello World!” message.
    5. You can enable MT interface and use MT command to do it. However, if you run Z-Stack 3.0.2 on CC2530, it might not have enough RAM/Flash to enable MT interface and you have to refer to www.ti.com/.../swra635.pdf to revise example to make it.
    6. I think Z-Stack 3.0 Sample UART Guide.pdf already describes those examples.
  • 1&2. for blinking LED I write same code as we discuss about it. I add
    #define TURNON_LIGHT_EVT 0xffff
    to zclsamplelight.c. and then add
    osal_start_timerEx(zclSampleLight_TaskID,TURNON_LIGHT_EVT,500);
    to zclsamplelightInit and add
    if ( events & TURNON_LIGHT_EVT )
    {

    HalLedBlink(HAL_LED_1, 10, 50,500);
    return ( events ^ TURNON_LIGHT_EVT );
    }
    to event loop. but my led isn't blinking even I run it step by step.
    for toggle led I change HalLedBlink(HAL_LED_1, 10, 50,500); with HalLedSet(HAL_LED_1, HAL_LED_MODE_TOGGLE);
    when I run the code step by step the LED was toggled but if I remove breakpoint from line if and run code, the led just turn on and don't toggle.
    3. expect create an event running in application event loop, what events we can create and how?
    4&6. can you tell me the links of them?
  • You cannot definite TURNON_LIGHT_EVT as 0xffff. Event should be bit flag like 0x0001, 0x0002, 0x0004, 0x0008,... and cannot be duplicate.
  • 1. but when I define event ID 0x0008 or another one my if statement isn't true. when I define it 0xffff the if statement run.
    2. can you tell me the links of Z-Stack 3.0 Sample UART Guide.pdf and l Z-Stack mesh 1.0.0?
    3. How can I enable MT interface and use MT command to work with UART interface?
  • 1. I don’t know why your code doesn’t work. Do you try my attached zcl_samplelight.c and zcl_samplswitch.h?
    2. There’s no Z-Stack 3.0 Sample UART Guide.pdf. It’s typo and it’s Z-Stack 3.0 Sample User’s Guide.pdf in Z-Stack document folder. Z-Stack mesh 1.0.0 can be downloaded from Z-STACK-ARCHIVE
    3. Define ZTOOL_P1, MT_TASK, and other MT_xxx defines you need in your project predefined symbols to enable MT interface.
  • hello. thanks for your answer.

    1. yes. I try to run my program with your files. but it doesn't work. I attach my zcl_samplelight.c and .h and I want to ask you to run this files in your cc2530 and show me is the event processing run correctly?

    2. can you please tell me the ways that I can write an event in zigbee network and handle it?

    0636.zcl_samplelight.h

    /**************************************************************************************************
      Filename:       zcl_sampleLight.c
      Revised:        $Date: 2014-10-24 16:04:46 -0700 (Fri, 24 Oct 2014) $
      Revision:       $Revision: 40796 $
    
    
      Description:    Zigbee Cluster Library - sample light application.
    
    
      Copyright 2006-2014 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 Light, based on Z-Stack 3.0. It can be configured as an
      On/Off light or as a dimmable light, by undefining or defining ZCL_LEVEL_CTRL, respectively.
    
      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 applicetion.
      
      Application-specific UI peripherals being used:
    
      - LEDs:
        LED1 reflect the current light state (On / Off accordingly).
    
      Application-specific menu system:
    
        <TOGGLE LIGHT> Toggle the local light and display its status and level
          Press OK to toggle the local light on and off.
          This screen shows the following information
            Line1: (only populated if ZCL_LEVEL_CTRL is defined)
              LEVEL XXX - xxx is the current level of the light if the light state is ON, or the target level
                of the light when the light state is off. The target level is the level that the light will be
                set to when it is switched from off to on using the on or the toggle commands.
            Line2:
              LIGHT OFF / ON: shows the current state of the light.
          Note when ZCL_LEVEL_CTRL is enabled:
            - If the light state is ON and the light level is X, and then the light receives the OFF or TOGGLE 
              commands: The level will decrease gradually until it reaches 1, and only then the light state will
              be changed to OFF. The level then will be restored to X, with the state staying OFF. At this stage
              the light is not lighting, and the level represent the target level for the next ON or TOGGLE 
              commands.
            - If the light state is OFF and the light level is X, and then the light receives the ON or TOGGLE
              commands; The level will be set to 1, the light state will be set to ON, and then the level will
              increase gradually until it reaches level X.
            - Any level-setting command will affect the level directly, and may also affect the on/off state,
              depending on the command's arguments.       
    
    *********************************************************************/
    
    /*********************************************************************
     * INCLUDES
     */
    #include "ZComDef.h"
    #include "OSAL.h"
    #include "AF.h"
    #include "ZDApp.h"
    #include "ZDObject.h"
    #include "MT_SYS.h"
    
    #include "nwk_util.h"
    
    #include "zcl.h"
    #include "zcl_general.h"
    #include "zcl_ha.h"
    #include "zcl_diagnostic.h"
    
    #include "zcl_samplelight.h"
       
    #include "bdb.h"
    #include "bdb_interface.h"
    
     //GP_UPDATE
    #include "gp_interface.h"
       
    #include "onboard.h"
    
    /* HAL */
    #include "hal_lcd.h"
    #include "hal_led.h"
    #include "hal_key.h"
    
    #include "NLMEDE.h"
    
    // Added to include TouchLink initiator functionality 
    #if defined ( BDB_TL_INITIATOR )
      #include "bdb_touchlink_initiator.h"
    #endif // BDB_TL_INITIATOR
    
    #if defined ( BDB_TL_TARGET )
      #include "bdb_touchlink_target.h"
    #endif // BDB_TL_TARGET
    
    #if defined ( BDB_TL_INITIATOR ) || defined ( BDB_TL_TARGET )
      #include "bdb_touchlink.h"
    #endif
    
    #include "zcl_sampleapps_ui.h"
    
    /*********************************************************************
     * 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 Light"
    
    /*********************************************************************
     * TYPEDEFS
     */
    
    /*********************************************************************
     * GLOBAL VARIABLES
     */
    byte zclSampleLight_TaskID;
    uint8 zclSampleLightSeqNum;
    uint8 ret;
    
    /*********************************************************************
     * GLOBAL FUNCTIONS
     */
    
    /*********************************************************************
     * LOCAL VARIABLES
     */
    afAddrType_t zclSampleLight_DstAddr;
    
    // Test Endpoint to allow SYS_APP_MSGs
    static endPointDesc_t sampleLight_TestEp =
    {
      SAMPLELIGHT_ENDPOINT,
      0,
      &zclSampleLight_TaskID,
      (SimpleDescriptionFormat_t *)NULL,  // No Simple description for this test endpoint
      (afNetworkLatencyReq_t)0            // No Network Latency req
    };
    
    #ifdef ZCL_LEVEL_CTRL
    uint8 zclSampleLight_WithOnOff;       // set to TRUE if state machine should set light on/off
    uint8 zclSampleLight_NewLevel;        // new level when done moving
    uint8 zclSampleLight_LevelChangeCmd; // current level change was triggered by an on/off command
    bool  zclSampleLight_NewLevelUp;      // is direction to new level up or down?
    int32 zclSampleLight_CurrentLevel32;  // current level, fixed point (e.g. 192.456)
    int32 zclSampleLight_Rate32;          // rate in units, fixed point (e.g. 16.123)
    uint8 zclSampleLight_LevelLastLevel;  // to save the Current Level before the light was turned OFF
    #endif
    
    /*********************************************************************
     * LOCAL FUNCTIONS
     */
    static void zclSampleLight_HandleKeys( byte shift, byte keys );
    static void zclSampleLight_BasicResetCB( void );
    static void zclSampleLight_OnOffCB( uint8 cmd );
    //GP_UPDATE
    #if (ZG_BUILD_RTR_TYPE)
    static void gp_CommissioningMode(bool isEntering);
    static uint8 gp_ChangeChannelReq(void);
    #endif
    
    
    static void zclSampleLight_ProcessCommissioningStatus(bdbCommissioningModeMsg_t *bdbCommissioningModeMsg);
    
    
    #ifdef ZCL_LEVEL_CTRL
    static void zclSampleLight_LevelControlMoveToLevelCB( zclLCMoveToLevel_t *pCmd );
    static void zclSampleLight_LevelControlMoveCB( zclLCMove_t *pCmd );
    static void zclSampleLight_LevelControlStepCB( zclLCStep_t *pCmd );
    static void zclSampleLight_LevelControlStopCB( void );
    static void zclSampleLight_DefaultMove( uint8 OnOff );
    static uint32 zclSampleLight_TimeRateHelper( uint8 newLevel );
    static uint16 zclSampleLight_GetTime ( uint8 level, uint16 time );
    static void zclSampleLight_MoveBasedOnRate( uint8 newLevel, uint32 rate );
    static void zclSampleLight_MoveBasedOnTime( uint8 newLevel, uint16 time );
    static void zclSampleLight_AdjustLightLevel( void );
    #endif
    
    // Functions to process ZCL Foundation incoming Command/Response messages
    static void zclSampleLight_ProcessIncomingMsg( zclIncomingMsg_t *msg );
    #ifdef ZCL_READ
    static uint8 zclSampleLight_ProcessInReadRspCmd( zclIncomingMsg_t *pInMsg );
    #endif
    #ifdef ZCL_WRITE
    static uint8 zclSampleLight_ProcessInWriteRspCmd( zclIncomingMsg_t *pInMsg );
    #endif
    static uint8 zclSampleLight_ProcessInDefaultRspCmd( zclIncomingMsg_t *pInMsg );
    #ifdef ZCL_DISCOVER
    static uint8 zclSampleLight_ProcessInDiscCmdsRspCmd( zclIncomingMsg_t *pInMsg );
    static uint8 zclSampleLight_ProcessInDiscAttrsRspCmd( zclIncomingMsg_t *pInMsg );
    static uint8 zclSampleLight_ProcessInDiscAttrsExtRspCmd( zclIncomingMsg_t *pInMsg );
    #endif
    
    void zclSampleLight_UiActionToggleLight(uint16 keys);
    void zclSampleLight_UiUpdateLcd(uint8 uiCurrentState, char * line[3]);
    void zclSampleLight_UpdateLedState(void);
    
    /*********************************************************************
     * CONSTANTS
     */
    const uiState_t zclSampleLight_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, &zclSampleLight_UiActionToggleLight},
    };
    
    #define LEVEL_CHANGED_BY_LEVEL_CMD  0
    #define LEVEL_CHANGED_BY_ON_CMD     1
    #define LEVEL_CHANGED_BY_OFF_CMD    2
    
    /*********************************************************************
     * STATUS STRINGS
     */
    #ifdef LCD_SUPPORTED
      const char sLightOn[]      = "   LIGHT ON     ";
      const char sLightOff[]     = "   LIGHT OFF    ";
      #ifdef ZCL_LEVEL_CTRL
        char sLightLevel[]        = "   LEVEL ###    "; // displays level 1-254
      #endif
    #endif
    
    /*********************************************************************
     * REFERENCED EXTERNALS
     */
    extern int16 zdpExternalStateTaskID;
    
    /*********************************************************************
     * ZCL General Profile Callback table
     */
    static zclGeneral_AppCallbacks_t zclSampleLight_CmdCallbacks =
    {
      zclSampleLight_BasicResetCB,            // Basic Cluster Reset command
      NULL,                                   // Identify Trigger Effect command
      zclSampleLight_OnOffCB,                 // 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
      zclSampleLight_LevelControlMoveToLevelCB, // Level Control Move to Level command
      zclSampleLight_LevelControlMoveCB,        // Level Control Move command
      zclSampleLight_LevelControlStepCB,        // Level Control Step command
      zclSampleLight_LevelControlStopCB,        // 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          zclSampleLight_Init
     *
     * @brief       Initialization function for the zclGeneral layer.
     *
     * @param       none
     *
     * @return      none
     */
    void zclSampleLight_Init( byte task_id )
    {
      zclSampleLight_TaskID = task_id;
    
      // Set destination address to indirect
      zclSampleLight_DstAddr.addrMode = (afAddrMode_t)AddrNotPresent;
      zclSampleLight_DstAddr.endPoint = 0;
      zclSampleLight_DstAddr.addr.shortAddr = 0;
    
      // Register the Simple Descriptor for this application
      bdb_RegisterSimpleDescriptor( &zclSampleLight_SimpleDesc );
    
      // Register the ZCL General Cluster Library callback functions
      zclGeneral_RegisterCmdCallbacks( SAMPLELIGHT_ENDPOINT, &zclSampleLight_CmdCallbacks );
    
      // Register the application's attribute list
      zclSampleLight_ResetAttributesToDefaultValues();
      zcl_registerAttrList( SAMPLELIGHT_ENDPOINT, zclSampleLight_NumAttributes, zclSampleLight_Attrs );
    
    #ifdef ZCL_LEVEL_CTRL
      zclSampleLight_LevelLastLevel = zclSampleLight_LevelCurrentLevel;
    #endif
    
      // Register the Application to receive the unprocessed Foundation command/response messages
      zcl_registerForMsg( zclSampleLight_TaskID );
    
    #ifdef ZCL_DISCOVER
      // Register the application's command list
      zcl_registerCmdList( SAMPLELIGHT_ENDPOINT, zclCmdsArraySize, zclSampleLight_Cmds );
    #endif
    
      // Register for all key events - This app will handle all key events
      RegisterForKeys( zclSampleLight_TaskID );
      
      bdb_RegisterCommissioningStatusCB( zclSampleLight_ProcessCommissioningStatus );
      
      // Register for a test endpoint
      afRegister( &sampleLight_TestEp );
    
      
    #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( SAMPLELIGHT_ENDPOINT, zclDiagnostic_ReadWriteAttrCB, NULL );
      if ( zclDiagnostic_InitStats() == ZSuccess )
      {
        // Here the user could start the timer to save Diagnostics to NV  
      }
    #endif
      
    //GP_UPDATE  
    #if (ZG_BUILD_RTR_TYPE)  
      gp_RegisterCommissioningModeCB(gp_CommissioningMode);
      gp_RegisterGPChangeChannelReqCB(gp_ChangeChannelReq);
    #endif
      
      zdpExternalStateTaskID = zclSampleLight_TaskID;
    
      UI_Init(zclSampleLight_TaskID, SAMPLEAPP_LCD_AUTO_UPDATE_EVT, SAMPLEAPP_KEY_AUTO_REPEAT_EVT, &zclSampleLight_IdentifyTime, APP_TITLE, &zclSampleLight_UiUpdateLcd, zclSampleLight_UiStatesMain);
    
      UI_UpdateLcd();
      osal_start_timerEx(zclSampleLight_TaskID,TURNON_LIGHT_EVT,500);
    
    }
    
    /*********************************************************************
     * @fn          zclSample_event_loop
     *
     * @brief       Event Loop Processor for zclGeneral.
     *
     * @param       none
     *
     * @return      none
     */
    uint16 zclSampleLight_event_loop( uint8 task_id, uint16 events )
    {
      afIncomingMSGPacket_t *MSGpkt;
    
      (void)task_id;  // Intentionally unreferenced parameter
    
    
      if ( events & SYS_EVENT_MSG )
      {
    
        while ( (MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( zclSampleLight_TaskID )) )
        {
          switch ( MSGpkt->hdr.event )
          {
            case ZCL_INCOMING_MSG:
              // Incoming ZCL Foundation command/response messages
              zclSampleLight_ProcessIncomingMsg( (zclIncomingMsg_t *)MSGpkt );
              break;
    
            case KEY_CHANGE:
              zclSampleLight_HandleKeys( ((keyChange_t *)MSGpkt)->state, ((keyChange_t *)MSGpkt)->keys );
              break;
    
            case ZDO_STATE_CHANGE:
              UI_DeviceStateUpdated((devStates_t)(MSGpkt->hdr.status));
              break;
    
            default:
              break;
          }
    
          // Release the memory
          osal_msg_deallocate( (uint8 *)MSGpkt );
        }
    
        // return unprocessed events
        return (events ^ SYS_EVENT_MSG);
      }
    
    #ifdef ZCL_LEVEL_CTRL
      if ( events & SAMPLELIGHT_LEVEL_CTRL_EVT )
      {
        zclSampleLight_AdjustLightLevel();
        return ( events ^ SAMPLELIGHT_LEVEL_CTRL_EVT );
      }
    #endif
    
    #if ZG_BUILD_ENDDEVICE_TYPE    
      if ( events & SAMPLEAPP_END_DEVICE_REJOIN_EVT )
      {
        bdb_ZedAttemptRecoverNwk();
        return ( events ^ SAMPLEAPP_END_DEVICE_REJOIN_EVT );
      }
    #endif
    
      if ( events & SAMPLEAPP_LCD_AUTO_UPDATE_EVT )
      {
        UI_UpdateLcd();
        return ( events ^ SAMPLEAPP_LCD_AUTO_UPDATE_EVT );
      }
    
      if ( events & SAMPLEAPP_KEY_AUTO_REPEAT_EVT )
      {
        UI_MainStateMachine(UI_KEY_AUTO_PRESSED);
        return ( events ^ SAMPLEAPP_KEY_AUTO_REPEAT_EVT );
      }
      if ( events & TURNON_LIGHT_EVT )
      {
         
         HalLedBlink(HAL_LED_1, 10, 50,500);
         return ( events ^ TURNON_LIGHT_EVT );
      }
      // Discard unknown events
      return 0;
    }
    
    
    /*********************************************************************
     * @fn      zclSampleLight_HandleKeys
     *
     * @brief   Handles all key events for this device.
     *
     * @param   shift - true if in shift/alt.
     * @param   keys - bit field for key events. Valid entries:
     *                 HAL_KEY_SW_5
     *                 HAL_KEY_SW_4
     *                 HAL_KEY_SW_2
     *                 HAL_KEY_SW_1
     *
     * @return  none
     */
    static void zclSampleLight_HandleKeys( byte shift, byte keys )
    {
      UI_MainStateMachine(keys);
    }
    
    //GP_UPDATE
    #if (ZG_BUILD_RTR_TYPE)
    /*********************************************************************
     * @fn      gp_CommissioningMode
     *
     * @brief   Callback that notifies the application that gp Proxy is entering 
     *          into commissioning mode
     *
     * @param   isEntering - 
     *
     * @return  
     */
    static void gp_CommissioningMode(bool isEntering)
    {
      if(isEntering)
      {
        //Led on indicating enter commissioning mode
      }
      else
      {
        //Led off indicating enter commissioning mode
      }
    }
    
    
    
    //GP_UPDATE
    /*********************************************************************
     * @fn      gp_ChangeChannelReq
     *
     * @brief   Callback function to notify the application about a GP commissioning 
     * request that will change the current channel for at most 
     * gpBirectionalCommissioningChangeChannelTimeout ms
     *
     * @param   channel - Channel in which the commissioning will take place
     *
     * @return  TRUE to allow change channel, FALSE to do not allow
     */
    static uint8 gp_ChangeChannelReq(void)
    {
      bool allowChangeChannel = TRUE;
      
      //Check application state to decide if allow change channel or not
      
      return allowChangeChannel;
    }
    
    #endif
    
    
    /*********************************************************************
     * @fn      zclSampleLight_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 zclSampleLight_ProcessCommissioningStatus(bdbCommissioningModeMsg_t *bdbCommissioningModeMsg)
    {
      switch(bdbCommissioningModeMsg->bdbCommissioningMode)
      {
        case BDB_COMMISSIONING_FORMATION:
          if(bdbCommissioningModeMsg->bdbCommissioningStatus == BDB_COMMISSIONING_SUCCESS)
          {
            //After formation, perform nwk steering again plus the remaining commissioning modes that has not been process yet
            bdb_StartCommissioning(BDB_COMMISSIONING_MODE_NWK_STEERING | bdbCommissioningModeMsg->bdbRemainingCommissioningModes);
          }
          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
            osal_start_timerEx(zclSampleLight_TaskID, SAMPLEAPP_END_DEVICE_REJOIN_EVT, SAMPLEAPP_END_DEVICE_REJOIN_DELAY);
          }
        break;
    #endif 
      }
      
      UI_UpdateComissioningStatus(bdbCommissioningModeMsg);
    }
    
    /*********************************************************************
     * @fn      zclSampleLight_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 zclSampleLight_BasicResetCB( void )
    {
      //Reset every attribute in all supported cluster to their default value.
    
      zclSampleLight_ResetAttributesToDefaultValues();
    
      zclSampleLight_UpdateLedState();
    
      // update the display
      UI_UpdateLcd( ); 
    }
    
    /*********************************************************************
     * @fn      zclSampleLight_OnOffCB
     *
     * @brief   Callback from the ZCL General Cluster Library when
     *          it received an On/Off Command for this application.
     *
     * @param   cmd - COMMAND_ON, COMMAND_OFF or COMMAND_TOGGLE
     *
     * @return  none
     */
    static void zclSampleLight_OnOffCB( uint8 cmd )
    {
      afIncomingMSGPacket_t *pPtr = zcl_getRawAFMsg();
    
      uint8 OnOff;
    
      zclSampleLight_DstAddr.addr.shortAddr = pPtr->srcAddr.addr.shortAddr;
    
    
      // Turn on the light
      if ( cmd == COMMAND_ON )
      {
        OnOff = LIGHT_ON;
      }
      // Turn off the light
      else if ( cmd == COMMAND_OFF )
      {
        OnOff = LIGHT_OFF;
      }
      // Toggle the light
      else if ( cmd == COMMAND_TOGGLE )
      {
    #ifdef ZCL_LEVEL_CTRL
        if (zclSampleLight_LevelRemainingTime > 0) 
        {
          if (zclSampleLight_NewLevelUp)
          {
            OnOff = LIGHT_OFF;
          }
          else
          {
            OnOff = LIGHT_ON;
          }
        }
        else
        {
          if (zclSampleLight_OnOff == LIGHT_ON)
          {
            OnOff = LIGHT_OFF;
          }
          else
          {
            OnOff = LIGHT_ON;
          }
        }
    #else
        if (zclSampleLight_OnOff == LIGHT_ON)
        {
          OnOff = LIGHT_OFF;
        }
        else
        {
          OnOff = LIGHT_ON;
        }
    #endif
      }
    
    #ifdef ZCL_LEVEL_CTRL
      zclSampleLight_LevelChangeCmd = (OnOff == LIGHT_ON ? LEVEL_CHANGED_BY_ON_CMD : LEVEL_CHANGED_BY_OFF_CMD);
    
      zclSampleLight_DefaultMove(OnOff);
    #else
      zclSampleLight_OnOff = OnOff;
    #endif
    
      zclSampleLight_UpdateLedState();
      
      // update the display
      UI_UpdateLcd( );
    }
    
    #ifdef ZCL_LEVEL_CTRL
    /*********************************************************************
     * @fn      zclSampleLight_TimeRateHelper
     *
     * @brief   Calculate time based on rate, and startup level state machine
     *
     * @param   newLevel - new level for current level
     *
     * @return  diff (directly), zclSampleLight_CurrentLevel32 and zclSampleLight_NewLevel, zclSampleLight_NewLevelUp
     */
    static uint32 zclSampleLight_TimeRateHelper( uint8 newLevel )
    {
      uint32 diff;
      uint32 newLevel32;
    
      // remember current and new level
      zclSampleLight_NewLevel = newLevel;
      zclSampleLight_CurrentLevel32 = (uint32)1000 * zclSampleLight_LevelCurrentLevel;
    
      // calculate diff
      newLevel32 = (uint32)1000 * newLevel;
      if ( zclSampleLight_LevelCurrentLevel > newLevel )
      {
        diff = zclSampleLight_CurrentLevel32 - newLevel32;
        zclSampleLight_NewLevelUp = FALSE;  // moving down
      }
      else
      {
        diff = newLevel32 - zclSampleLight_CurrentLevel32;
        zclSampleLight_NewLevelUp = TRUE;   // moving up
      }
    
      return ( diff );
    }
    
    /*********************************************************************
     * @fn      zclSampleLight_MoveBasedOnRate
     *
     * @brief   Calculate time based on rate, and startup level state machine
     *
     * @param   newLevel - new level for current level
     * @param   rate16   - fixed point rate (e.g. 16.123)
     *
     * @return  none
     */
    static void zclSampleLight_MoveBasedOnRate( uint8 newLevel, uint32 rate )
    {
      uint32 diff;
    
      // determine how much time (in 10ths of seconds) based on the difference and rate
      zclSampleLight_Rate32 = rate;
      diff = zclSampleLight_TimeRateHelper( newLevel );
      zclSampleLight_LevelRemainingTime = diff / rate;
      if ( !zclSampleLight_LevelRemainingTime )
      {
        zclSampleLight_LevelRemainingTime = 1;
      }
    
      osal_start_timerEx( zclSampleLight_TaskID, SAMPLELIGHT_LEVEL_CTRL_EVT, 100 );
    }
    
    /*********************************************************************
     * @fn      zclSampleLight_MoveBasedOnTime
     *
     * @brief   Calculate rate based on time, and startup level state machine
     *
     * @param   newLevel  - new level for current level
     * @param   time      - in 10ths of seconds
     *
     * @return  none
     */
    static void zclSampleLight_MoveBasedOnTime( uint8 newLevel, uint16 time )
    {
      uint16 diff;
    
      // determine rate (in units) based on difference and time
      diff = zclSampleLight_TimeRateHelper( newLevel );
      zclSampleLight_LevelRemainingTime = zclSampleLight_GetTime( newLevel, time );
      zclSampleLight_Rate32 = diff / time;
    
      osal_start_timerEx( zclSampleLight_TaskID, SAMPLELIGHT_LEVEL_CTRL_EVT, 100 );
    }
    
    /*********************************************************************
     * @fn      zclSampleLight_GetTime
     *
     * @brief   Determine amount of time that MoveXXX will take to complete.
     *
     * @param   level = new level to move to
     *          time  = 0xffff=default, or 0x0000-n amount of time in tenths of seconds.
     *
     * @return  none
     */
    static uint16 zclSampleLight_GetTime( uint8 newLevel, uint16 time )
    {
      // there is a hiearchy of the amount of time to use for transistioning
      // check each one in turn. If none of defaults are set, then use fastest
      // time possible.
      if ( time == 0xFFFF )
      {
        // use On or Off Transition Time if set (not 0xffff)
        if ( zclSampleLight_LevelCurrentLevel > newLevel )
        {
          time = zclSampleLight_LevelOffTransitionTime;
        }
        else
        {
          time = zclSampleLight_LevelOnTransitionTime;
        }
    
        // else use OnOffTransitionTime if set (not 0xffff)
        if ( time == 0xFFFF )
        {
          time = zclSampleLight_LevelOnOffTransitionTime;
        }
    
        // else as fast as possible
        if ( time == 0xFFFF )
        {
          time = 1;
        }
      }
    
      if ( time == 0 )
      {
        time = 1; // as fast as possible
      }
    
      return ( time );
    }
    
    /*********************************************************************
     * @fn      zclSampleLight_DefaultMove
     *
     * @brief   We were turned on/off. Use default time to move to on or off.
     *
     * @param   zclSampleLight_OnOff - must be set prior to calling this function.
     *
     * @return  none
     */
    static void zclSampleLight_DefaultMove( uint8 OnOff )
    {
      uint8  newLevel;
      uint32 rate;      // fixed point decimal (3 places, eg. 16.345)
      uint16 time;
    
      // if moving to on position, move to on level
      if ( OnOff )
      {
        if (zclSampleLight_OnOff == LIGHT_OFF)
        {
          zclSampleLight_LevelCurrentLevel = ATTR_LEVEL_MIN_LEVEL;
        }
        
        if ( zclSampleLight_LevelOnLevel == ATTR_LEVEL_ON_LEVEL_NO_EFFECT )
        {
          // The last Level (before going OFF) should be used)
          newLevel = zclSampleLight_LevelLastLevel;
        }
        else
        {
          newLevel = zclSampleLight_LevelOnLevel;
        }
    
        time = zclSampleLight_LevelOnTransitionTime;
    
      }
      else
      {
        newLevel = ATTR_LEVEL_MIN_LEVEL;
    
        time = zclSampleLight_LevelOffTransitionTime;
      }
    
      // else use OnOffTransitionTime if set (not 0xffff)
      if ( time == 0xFFFF )
      {
        time = zclSampleLight_LevelOnOffTransitionTime;
      }
    
      // else as fast as possible
      if ( time == 0xFFFF )
      {
        time = 1;
      }
    
      // calculate rate based on time (int 10ths) for full transition (1-254)
      rate = 255000 / time;    // units per tick, fixed point, 3 decimal places (e.g. 8500 = 8.5 units per tick)
    
      // start up state machine.
      zclSampleLight_WithOnOff = TRUE;
      zclSampleLight_MoveBasedOnRate( newLevel, rate );
    }
    
    /*********************************************************************
     * @fn      zclSampleLight_AdjustLightLevel
     *
     * @brief   Called each 10th of a second while state machine running
     *
     * @param   none
     *
     * @return  none
     */
    static void zclSampleLight_AdjustLightLevel( void )
    {
      // one tick (10th of a second) less
      if ( zclSampleLight_LevelRemainingTime )
      {
        --zclSampleLight_LevelRemainingTime;
      }
    
      // no time left, done
      if ( zclSampleLight_LevelRemainingTime == 0)
      {
        zclSampleLight_LevelCurrentLevel = zclSampleLight_NewLevel;
      }
    
      // still time left, keep increment/decrementing
      else
      {
        if ( zclSampleLight_NewLevelUp )
        {
          zclSampleLight_CurrentLevel32 += zclSampleLight_Rate32;
        }
        else
        {
          zclSampleLight_CurrentLevel32 -= zclSampleLight_Rate32;
        }
        zclSampleLight_LevelCurrentLevel = (uint8)( zclSampleLight_CurrentLevel32 / 1000 );
      }
    
      if (( zclSampleLight_LevelChangeCmd == LEVEL_CHANGED_BY_LEVEL_CMD ) && ( zclSampleLight_LevelOnLevel == ATTR_LEVEL_ON_LEVEL_NO_EFFECT ))
      {
        zclSampleLight_LevelLastLevel = zclSampleLight_LevelCurrentLevel;
      }
    
      // also affect on/off
      if ( zclSampleLight_WithOnOff )
      {
        if ( zclSampleLight_LevelCurrentLevel > ATTR_LEVEL_MIN_LEVEL )
        {
          zclSampleLight_OnOff = LIGHT_ON;
        }
        else
        {
          if (zclSampleLight_LevelChangeCmd != LEVEL_CHANGED_BY_ON_CMD)
          {
            zclSampleLight_OnOff = LIGHT_OFF;
          }
          else
          {
            zclSampleLight_OnOff = LIGHT_ON;
          }
          
          if (( zclSampleLight_LevelChangeCmd != LEVEL_CHANGED_BY_LEVEL_CMD ) && ( zclSampleLight_LevelOnLevel == ATTR_LEVEL_ON_LEVEL_NO_EFFECT ))
          {
            zclSampleLight_LevelCurrentLevel = zclSampleLight_LevelLastLevel;
          }
        }
      }
    
      zclSampleLight_UpdateLedState();
      
      // display light level as we go
      UI_UpdateLcd( );
    
      // keep ticking away
      if ( zclSampleLight_LevelRemainingTime )
      {
        osal_start_timerEx( zclSampleLight_TaskID, SAMPLELIGHT_LEVEL_CTRL_EVT, 100 );
      }
    }
    
    /*********************************************************************
     * @fn      zclSampleLight_LevelControlMoveToLevelCB
     *
     * @brief   Callback from the ZCL General Cluster Library when
     *          it received a LevelControlMoveToLevel Command for this application.
     *
     * @param   pCmd - ZigBee command parameters
     *
     * @return  none
     */
    static void zclSampleLight_LevelControlMoveToLevelCB( zclLCMoveToLevel_t *pCmd )
    {
      zclSampleLight_LevelChangeCmd = LEVEL_CHANGED_BY_LEVEL_CMD;
    
      zclSampleLight_WithOnOff = pCmd->withOnOff;
      zclSampleLight_MoveBasedOnTime( pCmd->level, pCmd->transitionTime );
    }
    
    /*********************************************************************
     * @fn      zclSampleLight_LevelControlMoveCB
     *
     * @brief   Callback from the ZCL General Cluster Library when
     *          it received a LevelControlMove Command for this application.
     *
     * @param   pCmd - ZigBee command parameters
     *
     * @return  none
     */
    static void zclSampleLight_LevelControlMoveCB( zclLCMove_t *pCmd )
    {
      uint8 newLevel;
      uint32 rate;
    
      // convert rate from units per second to units per tick (10ths of seconds)
      // and move at that right up or down
      zclSampleLight_WithOnOff = pCmd->withOnOff;
    
      if ( pCmd->moveMode == LEVEL_MOVE_UP )
      {
        newLevel = ATTR_LEVEL_MAX_LEVEL;  // fully on
      }
      else
      {
        newLevel = ATTR_LEVEL_MIN_LEVEL; // fully off
      }
    
      zclSampleLight_LevelChangeCmd = LEVEL_CHANGED_BY_LEVEL_CMD;
    
      rate = (uint32)100 * pCmd->rate;
      zclSampleLight_MoveBasedOnRate( newLevel, rate );
    }
    
    /*********************************************************************
     * @fn      zclSampleLight_LevelControlStepCB
     *
     * @brief   Callback from the ZCL General Cluster Library when
     *          it received an On/Off Command for this application.
     *
     * @param   pCmd - ZigBee command parameters
     *
     * @return  none
     */
    static void zclSampleLight_LevelControlStepCB( zclLCStep_t *pCmd )
    {
      uint8 newLevel;
    
      // determine new level, but don't exceed boundaries
      if ( pCmd->stepMode == LEVEL_MOVE_UP )
      {
        if ( (uint16)zclSampleLight_LevelCurrentLevel + pCmd->amount > ATTR_LEVEL_MAX_LEVEL )
        {
          newLevel = ATTR_LEVEL_MAX_LEVEL;
        }
        else
        {
          newLevel = zclSampleLight_LevelCurrentLevel + pCmd->amount;
        }
      }
      else
      {
        if ( pCmd->amount >= zclSampleLight_LevelCurrentLevel )
        {
          newLevel = ATTR_LEVEL_MIN_LEVEL;
        }
        else
        {
          newLevel = zclSampleLight_LevelCurrentLevel - pCmd->amount;
        }
      }
      
      zclSampleLight_LevelChangeCmd = LEVEL_CHANGED_BY_LEVEL_CMD;
    
      // move to the new level
      zclSampleLight_WithOnOff = pCmd->withOnOff;
      zclSampleLight_MoveBasedOnTime( newLevel, pCmd->transitionTime );
    }
    
    /*********************************************************************
     * @fn      zclSampleLight_LevelControlStopCB
     *
     * @brief   Callback from the ZCL General Cluster Library when
     *          it received an Level Control Stop Command for this application.
     *
     * @param   pCmd - ZigBee command parameters
     *
     * @return  none
     */
    static void zclSampleLight_LevelControlStopCB( void )
    {
      // stop immediately
      osal_stop_timerEx( zclSampleLight_TaskID, SAMPLELIGHT_LEVEL_CTRL_EVT );
      zclSampleLight_LevelRemainingTime = 0;
    }
    #endif
    
    /******************************************************************************
     *
     *  Functions for processing ZCL Foundation incoming Command/Response messages
     *
     *****************************************************************************/
    
    /*********************************************************************
     * @fn      zclSampleLight_ProcessIncomingMsg
     *
     * @brief   Process ZCL Foundation incoming message
     *
     * @param   pInMsg - pointer to the received message
     *
     * @return  none
     */
    static void zclSampleLight_ProcessIncomingMsg( zclIncomingMsg_t *pInMsg )
    {
      switch ( pInMsg->zclHdr.commandID )
      {
    #ifdef ZCL_READ
        case ZCL_CMD_READ_RSP:
          zclSampleLight_ProcessInReadRspCmd( pInMsg );
          break;
    #endif
    #ifdef ZCL_WRITE
        case ZCL_CMD_WRITE_RSP:
          zclSampleLight_ProcessInWriteRspCmd( pInMsg );
          break;
    #endif
        case ZCL_CMD_CONFIG_REPORT:
        case ZCL_CMD_CONFIG_REPORT_RSP:
        case ZCL_CMD_READ_REPORT_CFG:
        case ZCL_CMD_READ_REPORT_CFG_RSP:
        case ZCL_CMD_REPORT:
          //bdb_ProcessIncomingReportingMsg( pInMsg );
          break;
    
        case ZCL_CMD_DEFAULT_RSP:
          zclSampleLight_ProcessInDefaultRspCmd( pInMsg );
          break;
    #ifdef ZCL_DISCOVER
        case ZCL_CMD_DISCOVER_CMDS_RECEIVED_RSP:
          zclSampleLight_ProcessInDiscCmdsRspCmd( pInMsg );
          break;
    
        case ZCL_CMD_DISCOVER_CMDS_GEN_RSP:
          zclSampleLight_ProcessInDiscCmdsRspCmd( pInMsg );
          break;
    
        case ZCL_CMD_DISCOVER_ATTRS_RSP:
          zclSampleLight_ProcessInDiscAttrsRspCmd( pInMsg );
          break;
    
        case ZCL_CMD_DISCOVER_ATTRS_EXT_RSP:
          zclSampleLight_ProcessInDiscAttrsExtRspCmd( pInMsg );
          break;
    #endif
        default:
          break;
      }
    
      if ( pInMsg->attrCmd )
        osal_mem_free( pInMsg->attrCmd );
    }
    
    #ifdef ZCL_READ
    /*********************************************************************
     * @fn      zclSampleLight_ProcessInReadRspCmd
     *
     * @brief   Process the "Profile" Read Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleLight_ProcessInReadRspCmd( zclIncomingMsg_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      zclSampleLight_ProcessInWriteRspCmd
     *
     * @brief   Process the "Profile" Write Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleLight_ProcessInWriteRspCmd( zclIncomingMsg_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      zclSampleLight_ProcessInDefaultRspCmd
     *
     * @brief   Process the "Profile" Default Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleLight_ProcessInDefaultRspCmd( zclIncomingMsg_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      zclSampleLight_ProcessInDiscCmdsRspCmd
     *
     * @brief   Process the Discover Commands Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleLight_ProcessInDiscCmdsRspCmd( zclIncomingMsg_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      zclSampleLight_ProcessInDiscAttrsRspCmd
     *
     * @brief   Process the "Profile" Discover Attributes Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleLight_ProcessInDiscAttrsRspCmd( zclIncomingMsg_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      zclSampleLight_ProcessInDiscAttrsExtRspCmd
     *
     * @brief   Process the "Profile" Discover Attributes Extended Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleLight_ProcessInDiscAttrsExtRspCmd( zclIncomingMsg_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 zclSampleLight_UiActionToggleLight(uint16 keys)
    {
      zclSampleLight_OnOffCB(COMMAND_TOGGLE);
    }
    
    void zclSampleLight_UpdateLedState(void)
    {
      // set the LED1 based on light (on or off)
      if ( zclSampleLight_OnOff == LIGHT_ON )
      {
        HalLedSet ( UI_LED_APP, HAL_LED_MODE_ON );
      }
      else
      {
        HalLedSet ( UI_LED_APP, HAL_LED_MODE_OFF );
      }
    }
    
    void zclSampleLight_UiUpdateLcd(uint8 UiState, char * line[3])
    {
    #ifdef LCD_SUPPORTED
    #ifdef ZCL_LEVEL_CTRL
      zclHA_uint8toa( zclSampleLight_LevelCurrentLevel, &sLightLevel[9] );
      line[0] = (char *)sLightLevel;
    #endif // ZCL_LEVEL_CTRL
      line[1] = (char *)(zclSampleLight_OnOff ? sLightOn : sLightOff);
      line[2] = "< TOGGLE LIGHT >";
    #endif
    }
    
    /****************************************************************************
    ****************************************************************************/
    
    
    

  • 1. Yes, it runs correctly.
    2. I have show you the example with TURNON_LIGHT_EVT.
  • thank you.
    1. can you send me a picture from your hardware?
    2. in last commands I ask you "generally when we want to write an event we have to define event Id in zcl_appname.h and then start timer in zclAppName_Init and then write event handling in zclAppnamet_event_loop with HAL drivers. am I right?" and you answer me "3. If you mean to create an event running in application event loop, yes.".
    I want to know is there another way to create an event and handle it? if yes which ways does exist?

    3. can you please explain to me an example like get a command from user and send it to another zigbee module and then get back the answer to user. for example the user enter light and zigbee module send a command to another zigbee module to notify the lights status. I mean LED1 is on, 2 is off,etc. and then the first zigbee module show light's status to user. I want to ask you explain me the program step by step. show me which codes I have to write and which hardware connection I have to implement. pleeeeeeeeeeeaseeeeeeeeeee explain the whole code of this. thank you
  • 1. I told you it's TI CC2530DK. Anyway, I attach the picture of my kit.

    2. No. What I told you is the only way to create an event in Z-Stack

    3. I cannot do this for you. You have to have patient to trace code and compare the source code with developer's guide.