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.

temperature sensor in cc2530dk kit

Other Parts Discussed in Thread: Z-STACK, CC2530EM, CC2530

Warning[Pe177]: variable "zclSampleTemperatureSensor_MeasuredValue" was declared but never referenced E:\PROJECT\13$11\Z-Stack Home 1.2.1\Projects\zstack\HomeAutomation\SampleTemperatureSensor\Source\zcl_sampletemperaturesensor.c 452
Linking
Error[e46]: Undefined external "zclSampleTemperatureSensor_MeasuredValue" referred in zcl_sampletemperaturesensor ( E:\PROJECT\13$11\Z-Stack Home
1.2.1\Projects\zstack\HomeAutomation\SampleTemperatureSensor\CC2530DB\EndDeviceEB\Obj\zcl_sampletemperaturesensor.r51 )
Error while running Linker
Total number of errors: 1
Total number of warnings: 3

i used the adc to get data from internal temperature sensor and transmit it via zigbee and display it in samplethermostat code dumped board

  • 7041.zcl_sampletemperaturesensor.c
    /**************************************************************************************************
      Filename:       zcl_sampletemperaturesensor.c
      Revised:        $Date: 2013-10-18 11:49:27 -0700 (Fri, 18 Oct 2013) $
      Revision:       $Revision: 35718 $
    
      Description:    Zigbee Cluster Library - sample device application.
    
    
      Copyright 2013 Texas Instruments Incorporated. All rights reserved.
    
      IMPORTANT: Your use of this Software is limited to those specific rights
      granted under the terms of a software license agreement between the user
      who downloaded the software, his/her employer (which must be your employer)
      and Texas Instruments Incorporated (the "License").  You may not use this
      Software unless you agree to abide by the terms of the License. The License
      limits your use, and you acknowledge, that the Software may not be modified,
      copied or distributed unless embedded on a Texas Instruments microcontroller
      or used solely and exclusively in conjunction with a Texas Instruments radio
      frequency transceiver, which is integrated into your product.  Other than for
      the foregoing purpose, you may not use, reproduce, copy, prepare derivative
      works of, modify, distribute, perform, display or sell this Software and/or
      its documentation for any purpose.
    
      YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE
      PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,
      INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE,
      NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL
      TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT,
      NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER
      LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
      INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE
      OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT
      OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
      (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
    
      Should you have any questions regarding your right to use this Software,
      contact Texas Instruments Incorporated at www.TI.com.
    **************************************************************************************************/
    
    /*********************************************************************
      This device will act as a temperature sensor. It updates the current
      temperature on the thermostat when the user sends the desired
      temperature using SW1.
    
      SCREEN MODES
      ----------------------------------------
      Main:
        - SW1: Send current temperature
        - SW2: Invoke EZMode
        - SW3: Adjust temperature
        - SW5: Go to Help screen
    
      Temperature:
        - SW1: Increase temperature
        - SW3: Decrease temperature
        - SW5: Enter temperature
      ----------------------------------------
    *********************************************************************/
    
    /*********************************************************************
     * INCLUDES
     */
    #include "ZComDef.h"
    #include "OSAL.h"
    #include "AF.h"
    #include "ZDApp.h"
    #include "ZDObject.h"
    #include "ZDProfile.h"
    #include "MT_SYS.h"
    
    #include "zcl.h"
    #include "zcl_general.h"
    #include "zcl_ha.h"
    #include "zcl_ezmode.h"
    #include "zcl_ms.h"
    #include "hal_adc.h"
    
    #include "zcl_sampletemperaturesensor.h"
    
    #include "onboard.h"
    
    /* HAL */
    #include "hal_lcd.h"
    #include "hal_led.h"
    #include "hal_key.h"
    
    
    /*********************************************************************
     * MACROS
     */
    
    // how often to report temperature
    #define SAMPLETEMPERATURESENSOR_REPORT_INTERVAL   10000
    
    /*********************************************************************
     * CONSTANTS
     */
    
    /*********************************************************************
     * TYPEDEFS
     */
    
    /*********************************************************************
     * GLOBAL VARIABLES
     */
    byte zclSampleTemperatureSensor_TaskID;
    
    uint8 zclSampleTemperatureSensorSeqNum;
    
    static byte gPermitDuration = 0x00;
    
    /*void initTempSensor(void){
       DISABLE_ALL_INTERRUPTS();            //??????
       InitClock();                         //????????32M
       *((BYTE __xdata*) 0x624B) = 0x01;    //???????
       *((BYTE __xdata*) 0x61BD) = 0x01;    //???????ADC????
    }*/
    
    /*********************************************************************
     * GLOBAL FUNCTIONS
     */
    
    /*********************************************************************
     * LOCAL VARIABLES
     */
    afAddrType_t zclSampleTemperatureSensor_DstAddr;
    
    #ifdef ZCL_EZMODE
    static void zclSampleTemperatureSensor_ProcessZDOMsgs( zdoIncomingMsg_t *pMsg );
    static void zclSampleTemperatureSensor_EZModeCB( zlcEZMode_State_t state, zclEZMode_CBData_t *pData );
    
    static const zclEZMode_RegisterData_t zclSampleTemperatureSensor_RegisterEZModeData =
    {
      &zclSampleTemperatureSensor_TaskID,
      SAMPLETEMPERATURESENSOR_EZMODE_NEXTSTATE_EVT,
      SAMPLETEMPERATURESENSOR_EZMODE_TIMEOUT_EVT,
      &zclSampleTemperatureSensorSeqNum,
      zclSampleTemperatureSensor_EZModeCB
    };
    
    // NOT ZCL_EZMODE, Use EndDeviceBind
    #else
    
    static cId_t bindingOutClusters[] =
    {
      ZCL_CLUSTER_ID_MS_TEMPERATURE_MEASUREMENT
    };
    #define ZCLSAMPLETEMPERATURESENSOR_BINDINGLIST        1
    #endif
    
    devStates_t zclSampleTemperatureSensor_NwkState = DEV_INIT;
    
    uint8 giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;   // display main screen mode first
    
    static uint8 aProcessCmd[] = { 1, 0, 0, 0 }; // used for reset command, { length + cmd0 + cmd1 + data }
    
    // Test Endpoint to allow SYS_APP_MSGs
    static endPointDesc_t sampleTemperatureSensor_TestEp =
    {
      20,                                 // Test endpoint
      &zclSampleTemperatureSensor_TaskID,
      (SimpleDescriptionFormat_t *)NULL,  // No Simple description for this test endpoint
      (afNetworkLatencyReq_t)0            // No Network Latency req
    };
    
    
    /*********************************************************************
     * LOCAL FUNCTIONS
     */
    static void zclSampleTemperatureSensor_HandleKeys( byte shift, byte keys );
    static void zclSampleTemperatureSensor_BasicResetCB( void );
    static void zclSampleTemperatureSensor_IdentifyCB( zclIdentify_t *pCmd );
    static void zclSampleTemperatureSensor_IdentifyQueryRspCB( zclIdentifyQueryRsp_t *pRsp );
    static void zclSampleTemperatureSensor_ProcessIdentifyTimeChange( void );
    void getTemperature(void);
    // app display functions
    void zclSampleTemperatureSensor_LcdDisplayUpdate(void);
    void zclSampleTemperatureSensor_LcdDisplayMainMode(void);
    void zclSampleTemperatureSensor_LcdDisplayTempMode(void);
    void zclSampleTemperatureSensor_LcdDisplayHelpMode(void);
    //int16 getTemperature(void);
    
    static void zclSampleTemperatureSensor_SendTemp(void);
    
    // Functions to process ZCL Foundation incoming Command/Response messages
    static void zclSampleTemperatureSensor_ProcessIncomingMsg( zclIncomingMsg_t *msg );
    #ifdef ZCL_READ
    static uint8 zclSampleTemperatureSensor_ProcessInReadRspCmd( zclIncomingMsg_t *pInMsg );
    #endif
    #ifdef ZCL_WRITE
    static uint8 zclSampleTemperatureSensor_ProcessInWriteRspCmd( zclIncomingMsg_t *pInMsg );
    #endif
    static uint8 zclSampleTemperatureSensor_ProcessInDefaultRspCmd( zclIncomingMsg_t *pInMsg );
    #ifdef ZCL_DISCOVER
    static uint8 zclSampleTemperatureSensor_ProcessInDiscCmdsRspCmd( zclIncomingMsg_t *pInMsg );
    static uint8 zclSampleTemperatureSensor_ProcessInDiscAttrsRspCmd( zclIncomingMsg_t *pInMsg );
    static uint8 zclSampleTemperatureSensor_ProcessInDiscAttrsExtRspCmd( zclIncomingMsg_t *pInMsg );
    #endif // ZCL_DISCOVER
    
    /*********************************************************************
     * STATUS STRINGS
     */
    #ifdef LCD_SUPPORTED
    const char sClearLine[]    = " ";
    const char sDeviceName[]   = "  Temp Sensor";
    const char sSwTempUp[]     = "SW1: Raise Temp";
    const char sSwEZMode[]     = "SW2: EZ-Mode";
    const char sSwTempDown[]   = "SW3: Lower Temp";
    const char sSwHelp[]       = "SW5: Help";
    #endif
    
    /*********************************************************************
     * ZCL General Profile Callback table
     */
    static zclGeneral_AppCallbacks_t zclSampleTemperatureSensor_CmdCallbacks =
    {
      zclSampleTemperatureSensor_BasicResetCB,        // Basic Cluster Reset command
      zclSampleTemperatureSensor_IdentifyCB,          // Identify command
    #ifdef ZCL_EZMODE
      NULL,                                           // Identify EZ-Mode Invoke command
      NULL,                                           // Identify Update Commission State command
    #endif
      NULL,                                           // Identify Trigger Effect command
      zclSampleTemperatureSensor_IdentifyQueryRspCB,  // Identify Query Response command
      NULL,             				                      // On/Off cluster command
      NULL,                                           // On/Off cluster enhanced command Off with Effect
      NULL,                                           // On/Off cluster enhanced command On with Recall Global Scene
      NULL,                                           // On/Off cluster enhanced command On with Timed Off
    #ifdef ZCL_LEVEL_CTRL
      NULL,                                           // Level Control Move to Level command
      NULL,                                           // Level Control Move command
      NULL,                                           // Level Control Step command
      NULL,                                           // Level Control Stop command
    #endif
    #ifdef ZCL_GROUPS
      NULL,                                           // Group Response commands
    #endif
    #ifdef ZCL_SCENES
      NULL,                                           // Scene Store Request command
      NULL,                                           // Scene Recall Request command
      NULL,                                           // Scene Response command
    #endif
    #ifdef ZCL_ALARMS
      NULL,                                           // Alarm (Response) commands
    #endif
    #ifdef SE_UK_EXT
      NULL,                                           // Get Event Log command
      NULL,                                           // Publish Event Log command
    #endif
      NULL,                                           // RSSI Location command
      NULL                                            // RSSI Location Response command
    };
    
    /*********************************************************************
     * @fn          zclSampleTemperatureSensor_Init
     *
     * @brief       Initialization function for the zclGeneral layer.
     *
     * @param       none
     *
     * @return      none
     */
    void zclSampleTemperatureSensor_Init( byte task_id )
    {
      zclSampleTemperatureSensor_TaskID = task_id;
    
      // Set destination address to indirect
      zclSampleTemperatureSensor_DstAddr.addrMode = (afAddrMode_t)AddrNotPresent;
      zclSampleTemperatureSensor_DstAddr.endPoint = 0;
      zclSampleTemperatureSensor_DstAddr.addr.shortAddr = 0;
    
      // This app is part of the Home Automation Profile
      zclHA_Init( &zclSampleTemperatureSensor_SimpleDesc );
    
      // Register the ZCL General Cluster Library callback functions
      zclGeneral_RegisterCmdCallbacks( SAMPLETEMPERATURESENSOR_ENDPOINT, &zclSampleTemperatureSensor_CmdCallbacks );
    
      // Register the application's attribute list
      zcl_registerAttrList( SAMPLETEMPERATURESENSOR_ENDPOINT, SAMPLETEMPERATURESENSOR_MAX_ATTRIBUTES, zclSampleTemperatureSensor_Attrs );
    
      // Register the Application to receive the unprocessed Foundation command/response messages
      zcl_registerForMsg( zclSampleTemperatureSensor_TaskID );
    
    #ifdef ZCL_EZMODE
      // Register EZ-Mode
      zcl_RegisterEZMode( &zclSampleTemperatureSensor_RegisterEZModeData );
    
      // Register with the ZDO to receive Match Descriptor Responses
      ZDO_RegisterForZDOMsg(task_id, Match_Desc_rsp);
    #endif
    
      // Register for all key events - This app will handle all key events
      RegisterForKeys( zclSampleTemperatureSensor_TaskID );
    
      // Register for a test endpoint
      afRegister( &sampleTemperatureSensor_TestEp );
    
    #ifdef LCD_SUPPORTED
      // display the device name
      HalLcdWriteString( (char *)sDeviceName, HAL_LCD_LINE_3 );
    #endif
    }
    
    /*********************************************************************
     * @fn          zclSample_event_loop
     *
     * @brief       Event Loop Processor for zclGeneral.
     *
     * @param       none
     *
     * @return      none
     */
    uint16 zclSampleTemperatureSensor_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( zclSampleTemperatureSensor_TaskID )) )
        {
          switch ( MSGpkt->hdr.event )
          {
    #ifdef ZCL_EZMODE
            case ZDO_CB_MSG:
              zclSampleTemperatureSensor_ProcessZDOMsgs( (zdoIncomingMsg_t *)MSGpkt );
              break;
    #endif
    
            case ZCL_INCOMING_MSG:
              // Incoming ZCL Foundation command/response messages
              zclSampleTemperatureSensor_ProcessIncomingMsg( (zclIncomingMsg_t *)MSGpkt );
              break;
    
            case KEY_CHANGE:
              zclSampleTemperatureSensor_HandleKeys( ((keyChange_t *)MSGpkt)->state, ((keyChange_t *)MSGpkt)->keys );
              break;
    
            case ZDO_STATE_CHANGE:
              zclSampleTemperatureSensor_NwkState = (devStates_t)(MSGpkt->hdr.status);
    
    
              // now on the network
              if ( (zclSampleTemperatureSensor_NwkState == DEV_ZB_COORD) ||
                   (zclSampleTemperatureSensor_NwkState == DEV_ROUTER)   ||
                   (zclSampleTemperatureSensor_NwkState == DEV_END_DEVICE) )
              {
    #ifndef HOLD_AUTO_START
                giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;
                zclSampleTemperatureSensor_LcdDisplayUpdate();
    #endif
    #ifdef ZCL_EZMODE
                zcl_EZModeAction( EZMODE_ACTION_NETWORK_STARTED, NULL );
    #endif // ZCL_EZMODE
              }
              break;
    
            default:
              break;
          }
    
          // Release the memory
          osal_msg_deallocate( (uint8 *)MSGpkt );
        }
    
        // return unprocessed events
        return (events ^ SYS_EVENT_MSG);
      }
    
      if ( events & SAMPLETEMPERATURESENSOR_IDENTIFY_TIMEOUT_EVT )
      {
        if ( zclSampleTemperatureSensor_IdentifyTime > 0 )
          zclSampleTemperatureSensor_IdentifyTime--;
        zclSampleTemperatureSensor_ProcessIdentifyTimeChange();
    
        return ( events ^ SAMPLETEMPERATURESENSOR_IDENTIFY_TIMEOUT_EVT );
      }
    
    #ifdef ZCL_EZMODE
      // going on to next state
      if ( events & SAMPLETEMPERATURESENSOR_EZMODE_NEXTSTATE_EVT )
      {
        zcl_EZModeAction ( EZMODE_ACTION_PROCESS, NULL );   // going on to next state
        return ( events ^ SAMPLETEMPERATURESENSOR_EZMODE_NEXTSTATE_EVT );
      }
    
      // the overall EZMode timer expired, so we timed out
      if ( events & SAMPLETEMPERATURESENSOR_EZMODE_TIMEOUT_EVT )
      {
        zcl_EZModeAction ( EZMODE_ACTION_TIMED_OUT, NULL ); // EZ-Mode timed out
        return ( events ^ SAMPLETEMPERATURESENSOR_EZMODE_TIMEOUT_EVT );
      }
    #endif // ZLC_EZMODE
    
      if ( events & SAMPLETEMPERATURESENSOR_MAIN_SCREEN_EVT )
      {
        giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;
        zclSampleTemperatureSensor_LcdDisplayUpdate();
    
        return ( events ^ SAMPLETEMPERATURESENSOR_MAIN_SCREEN_EVT );
      }
    
      if ( events & SAMPLETEMPERATURESENSOR_TEMP_SEND_EVT )
      {
        zclSampleTemperatureSensor_SendTemp();
    
        // report current temperature reading every 10 seconds
        osal_start_timerEx( zclSampleTemperatureSensor_TaskID, SAMPLETEMPERATURESENSOR_TEMP_SEND_EVT, SAMPLETEMPERATURESENSOR_REPORT_INTERVAL );
    
        return ( events ^ SAMPLETEMPERATURESENSOR_TEMP_SEND_EVT );
      }
    
      // Discard unknown events
      return 0;
    }
    
    int16 getTemperature(void)
    {
      int16 AvgTemp;
      uint8   i;
      uint16  AdcValue;
      uint16  value;
      uint16  value1;
    //char i;
            char TempValue[10]; 
            //initTempSensor(); 
      AdcValue = 0;
      for( i = 0; i < 4; i++ )
      {
    ADCIF = 0;
        ADCCON3 = (HAL_ADC_REF_125V | HAL_ADC_RESOLUTION_14 | HAL_ADC_CHN_TEMP);    // ??1.25V????,12????,AD??:?????
            while ( !ADCIF );                                              //?????ADC
                     
        value =  ADCL >> 2;                        
        value |= (((UINT16)ADCH) << 6);    
        AdcValue += value;                          
      }
      value = AdcValue >> 2;                        //????4,?????
      value1 =   (((value) >> 4) - 335);
                 //??AD?,????????
      while(1)
            {
              AvgTemp = 0;
              for(i = 0 ; i < 64 ; i++)
              {
                AvgTemp += value1;              
                AvgTemp >>= 1;                              //??????2.
              }
              
            }
      return AvgTemp;
    }
    
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_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_3
     *                 HAL_KEY_SW_2
     *                 HAL_KEY_SW_1
     *
     * @return  none
     */
    static void zclSampleTemperatureSensor_HandleKeys( byte shift, byte keys )
    {
      if ( keys & HAL_KEY_SW_1 )
      {
        // increase temperature
        giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;
    
        if ( zclSampleTemperatureSensor_MeasuredValue < zclSampleTemperatureSensor_MaxMeasuredValue )
        {
          zclSampleTemperatureSensor_MeasuredValue = zclSampleTemperatureSensor_MeasuredValue + 100;  // considering using whole number value
        }
        else if ( zclSampleTemperatureSensor_MeasuredValue >= zclSampleTemperatureSensor_MaxMeasuredValue )
        {
          zclSampleTemperatureSensor_MeasuredValue = zclSampleTemperatureSensor_MaxMeasuredValue;
        }
    
        // Send temperature information
        zclSampleTemperatureSensor_SendTemp();
      }
    
      if ( keys & HAL_KEY_SW_2 )
      {
        if ( ( giTemperatureSensorScreenMode == TEMPSENSE_MAINMODE ) ||
            ( giTemperatureSensorScreenMode == TEMPSENSE_HELPMODE ) )
        {
          giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;
    
    #ifdef ZCL_EZMODE
          zclEZMode_InvokeData_t ezModeData;
          static uint16 clusterIDs[] = { ZCL_CLUSTER_ID_MS_TEMPERATURE_MEASUREMENT };   // only bind on the Temperature Measurement cluster
    
          // Invoke EZ-Mode
          ezModeData.endpoint = SAMPLETEMPERATURESENSOR_ENDPOINT; // endpoint on which to invoke EZ-Mode
          if ( ( zclSampleTemperatureSensor_NwkState == DEV_ZB_COORD ) ||
               ( zclSampleTemperatureSensor_NwkState == DEV_ROUTER )   ||
               ( zclSampleTemperatureSensor_NwkState == DEV_END_DEVICE ) )
          {
            ezModeData.onNetwork = TRUE;      // node is already on the network
          }
          else
          {
            ezModeData.onNetwork = FALSE;     // node is not yet on the network
          }
          ezModeData.initiator = TRUE;        // Temperature Sensor is an initiator
          ezModeData.numActiveInClusters = 1;
          ezModeData.pActiveInClusterIDs = clusterIDs;
          ezModeData.numActiveOutClusters = 0;   // active output cluster
          ezModeData.pActiveOutClusterIDs = NULL;
          zcl_InvokeEZMode( &ezModeData );
    
    #ifdef LCD_SUPPORTED
          HalLcdWriteString( "EZMode", HAL_LCD_LINE_2 );
    #endif
    
          // NOT ZCL_EZMODE, Use EndDeviceBind
    #else
          {
            zAddrType_t dstAddr;
            dstAddr.addrMode = Addr16Bit;
            dstAddr.addr.shortAddr = 0;   // Coordinator makes the EDB match
    
            // Initiate an End Device Bind Request, this bind request will
            // only use a cluster list that is important to binding.
            HalLedSet ( HAL_LED_4, HAL_LED_MODE_OFF );
            ZDP_EndDeviceBindReq( &dstAddr, NLME_GetShortAddr(),
                                  SAMPLETEMPERATURESENSOR_ENDPOINT,
                                  ZCL_HA_PROFILE_ID,
                                  0, NULL,
                                  ZCLSAMPLETEMPERATURESENSOR_BINDINGLIST, bindingOutClusters,
                                  FALSE );
          }
    #endif // ZCL_EZMODE
        }
      }
    
      if ( keys & HAL_KEY_SW_3 )
      {
        giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;
    
        // decrease the temperature
        if ( zclSampleTemperatureSensor_MeasuredValue > zclSampleTemperatureSensor_MinMeasuredValue )
        {
          zclSampleTemperatureSensor_MeasuredValue = zclSampleTemperatureSensor_MeasuredValue - 100;  // considering using whole number value
        }
        else if ( zclSampleTemperatureSensor_MeasuredValue >= zclSampleTemperatureSensor_MinMeasuredValue )
        {
          zclSampleTemperatureSensor_MeasuredValue = zclSampleTemperatureSensor_MinMeasuredValue;
        }
    
        // Send temperature information
        zclSampleTemperatureSensor_SendTemp();
      }
    
      if ( keys & HAL_KEY_SW_4 )
      {
        giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;
    
        if ( ( zclSampleTemperatureSensor_NwkState == DEV_ZB_COORD ) ||
             ( zclSampleTemperatureSensor_NwkState == DEV_ROUTER ) )
        {
          // toggle permit join
          gPermitDuration = gPermitDuration ? 0 : 0xff;
          NLME_PermitJoiningRequest( gPermitDuration );
        }
      }
    
      if ( shift && ( keys & HAL_KEY_SW_5 ) )
      {
        zclSampleTemperatureSensor_BasicResetCB();
      }
      else if ( keys & HAL_KEY_SW_5 )
      {
        if ( giTemperatureSensorScreenMode == TEMPSENSE_MAINMODE )
        {
          giTemperatureSensorScreenMode = TEMPSENSE_HELPMODE;
        }
        else if ( giTemperatureSensorScreenMode == TEMPSENSE_HELPMODE )
        {
    #ifdef LCD_SUPPORTED
          HalLcdWriteString( (char *)sClearLine, HAL_LCD_LINE_2 );
    #endif
          giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;
        }
      }
    
      // update display
      zclSampleTemperatureSensor_LcdDisplayUpdate();
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_LcdDisplayUpdate
     *
     * @brief   Called to update the LCD display.
     *
     * @param   none
     *
     * @return  none
     */
    void zclSampleTemperatureSensor_LcdDisplayUpdate( void )
    {
      // turn on red LED for temperatures >= 24.00C
      if ( zclSampleTemperatureSensor_MeasuredValue >= 2400 )
      {
        HalLedSet ( HAL_LED_1, HAL_LED_MODE_OFF );
        HalLedSet ( HAL_LED_2, HAL_LED_MODE_ON );
      }
      // turn on green LED for temperatures <= 20.00C
      else if ( zclSampleTemperatureSensor_MeasuredValue <= 2000 )
      {
        HalLedSet ( HAL_LED_1, HAL_LED_MODE_ON );
        HalLedSet ( HAL_LED_2, HAL_LED_MODE_OFF );
      }
      // turn on both red and green LEDs for temperatures between 20.00C and 24.00C
      else
      {
        HalLedSet ( HAL_LED_1, HAL_LED_MODE_ON );
        HalLedSet ( HAL_LED_2, HAL_LED_MODE_ON );
      }
    
      if ( giTemperatureSensorScreenMode == TEMPSENSE_HELPMODE )
      {
        zclSampleTemperatureSensor_LcdDisplayHelpMode();
      }
      else
      {
        zclSampleTemperatureSensor_LcdDisplayMainMode();
      }
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_LcdDisplayMainMode
     *
     * @brief   Called to display the main screen on the LCD.
     *
     * @param   none
     *
     * @return  none
     */
    void zclSampleTemperatureSensor_LcdDisplayMainMode( void )
    {
      char sDisplayTemp[16];
    
      if ( zclSampleTemperatureSensor_NwkState == DEV_ZB_COORD )
      {
        zclHA_LcdStatusLine1( 0 );
      }
      else if ( zclSampleTemperatureSensor_NwkState == DEV_ROUTER )
      {
        zclHA_LcdStatusLine1( 1 );
      }
      else if ( zclSampleTemperatureSensor_NwkState == DEV_END_DEVICE )
      {
        zclHA_LcdStatusLine1( 2 );
      }
    
      // display current temperature
      osal_memcpy(sDisplayTemp, "TEMP: ", 6);
      _ltoa( ( zclSampleTemperatureSensor_MeasuredValue / 100 ), (void *)(&sDisplayTemp[6]), 10 );   // convert temperature to whole number
      osal_memcpy( &sDisplayTemp[8], "C", 2 );
    #ifdef LCD_SUPPORTED
      HalLcdWriteString( (char *)sDisplayTemp, HAL_LCD_LINE_2 );
    #endif
    
    #ifdef LCD_SUPPORTED
      if ( ( zclSampleTemperatureSensor_NwkState == DEV_ZB_COORD ) ||
           ( zclSampleTemperatureSensor_NwkState == DEV_ROUTER ) )
      {
        // display help key with permit join status
        if ( gPermitDuration )
        {
          HalLcdWriteString( "SW5: Help      *", HAL_LCD_LINE_3 );
        }
        else
        {
          HalLcdWriteString( "SW5: Help       ", HAL_LCD_LINE_3 );
        }
      }
      else
      {
        // display help key
        HalLcdWriteString( (char *)sSwHelp, HAL_LCD_LINE_3 );
      }
    #endif
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_LcdDisplayHelpMode
     *
     * @brief   Called to display the SW options on the LCD.
     *
     * @param   none
     *
     * @return  none
     */
    void zclSampleTemperatureSensor_LcdDisplayHelpMode( void )
    {
    #ifdef LCD_SUPPORTED
      HalLcdWriteString( (char *)sSwTempUp, HAL_LCD_LINE_1 );
      HalLcdWriteString( (char *)sSwEZMode, HAL_LCD_LINE_2 );
      HalLcdWriteString( (char *)sSwTempDown, HAL_LCD_LINE_3 );
    #endif
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_SendTemp
     *
     * @brief   Called to send current temperature information to the thermostat
     *
     * @param   none
     *
     * @return  none
     */
    static void zclSampleTemperatureSensor_SendTemp( void )
    {
    #ifdef ZCL_REPORT
      zclReportCmd_t *pReportCmd;
      zclSampleTemperatureSensor_MeasuredValue = getTemperature();
      pReportCmd = osal_mem_alloc( sizeof(zclReportCmd_t) + sizeof(zclReport_t) );
      if ( pReportCmd != NULL )
      {
        pReportCmd->numAttr = 1;
        pReportCmd->attrList[0].attrID = ATTRID_MS_TEMPERATURE_MEASURED_VALUE;
        pReportCmd->attrList[0].dataType = ZCL_DATATYPE_INT16;
        pReportCmd->attrList[0].attrData = (void *)(&zclSampleTemperatureSensor_MeasuredValue);
    
        zcl_SendReportCmd( SAMPLETEMPERATURESENSOR_ENDPOINT, &zclSampleTemperatureSensor_DstAddr,
                           ZCL_CLUSTER_ID_MS_TEMPERATURE_MEASUREMENT,
                           pReportCmd, ZCL_FRAME_SERVER_CLIENT_DIR, TRUE, zclSampleTemperatureSensorSeqNum++ );
      }
    
      osal_mem_free( pReportCmd );
    #endif  // ZCL_REPORT
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_ProcessIdentifyTimeChange
     *
     * @brief   Called to process any change to the IdentifyTime attribute.
     *
     * @param   none
     *
     * @return  none
     */
    static void zclSampleTemperatureSensor_ProcessIdentifyTimeChange( void )
    {
      if ( zclSampleTemperatureSensor_IdentifyTime > 0 )
      {
        osal_start_timerEx( zclSampleTemperatureSensor_TaskID, SAMPLETEMPERATURESENSOR_IDENTIFY_TIMEOUT_EVT, 1000 );
        HalLedBlink ( HAL_LED_4, 0xFF, HAL_LED_DEFAULT_DUTY_CYCLE, HAL_LED_DEFAULT_FLASH_TIME );
      }
      else
      {
        if ( zclSampleTemperatureSensor_OnOff )
        {
          HalLedSet ( HAL_LED_4, HAL_LED_MODE_ON );
        }
        else
        {
          HalLedSet ( HAL_LED_4, HAL_LED_MODE_OFF );
        }
    
        osal_stop_timerEx( zclSampleTemperatureSensor_TaskID, SAMPLETEMPERATURESENSOR_IDENTIFY_TIMEOUT_EVT );
      }
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_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 zclSampleTemperatureSensor_BasicResetCB( void )
    {
      // Put device back to factory default settings
      zgWriteStartupOptions( ZG_STARTUP_SET, 3 );   // bit set both default configuration and default network
    
      // restart device
      MT_SysCommandProcessing( aProcessCmd );
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_IdentifyCB
     *
     * @brief   Callback from the ZCL General Cluster Library when
     *          it received an Identity Command for this application.
     *
     * @param   srcAddr - source address and endpoint of the response message
     * @param   identifyTime - the number of seconds to identify yourself
     *
     * @return  none
     */
    static void zclSampleTemperatureSensor_IdentifyCB( zclIdentify_t *pCmd )
    {
      zclSampleTemperatureSensor_IdentifyTime = pCmd->identifyTime;
      zclSampleTemperatureSensor_ProcessIdentifyTimeChange();
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_IdentifyQueryRspCB
     *
     * @brief   Callback from the ZCL General Cluster Library when
     *          it received an Identity Query Response Command for this application.
     *
     * @param   srcAddr - requestor's address
     * @param   timeout - number of seconds to identify yourself (valid for query response)
     *
     * @return  none
     */
    static void zclSampleTemperatureSensor_IdentifyQueryRspCB( zclIdentifyQueryRsp_t *pRsp )
    {
      (void)pRsp;
    #ifdef ZCL_EZMODE
      {
        zclEZMode_ActionData_t data;
        data.pIdentifyQueryRsp = pRsp;
        zcl_EZModeAction ( EZMODE_ACTION_IDENTIFY_QUERY_RSP, &data );
      }
    #endif
    }
    
    /******************************************************************************
     *
     *  Functions for processing ZCL Foundation incoming Command/Response messages
     *
     *****************************************************************************/
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_ProcessIncomingMsg
     *
     * @brief   Process ZCL Foundation incoming message
     *
     * @param   pInMsg - pointer to the received message
     *
     * @return  none
     */
    static void zclSampleTemperatureSensor_ProcessIncomingMsg( zclIncomingMsg_t *pInMsg)
    {
      switch ( pInMsg->zclHdr.commandID )
      {
    #ifdef ZCL_READ
        case ZCL_CMD_READ_RSP:
          zclSampleTemperatureSensor_ProcessInReadRspCmd( pInMsg );
          break;
    #endif
    #ifdef ZCL_WRITE
        case ZCL_CMD_WRITE_RSP:
          zclSampleTemperatureSensor_ProcessInWriteRspCmd( pInMsg );
          break;
    #endif
    #ifdef ZCL_REPORT
        // See ZCL Test Applicaiton (zcl_testapp.c) for sample code on Attribute Reporting
        case ZCL_CMD_CONFIG_REPORT:
          //zclSampleTemperatureSensor_ProcessInConfigReportCmd( pInMsg );
          break;
    
        case ZCL_CMD_CONFIG_REPORT_RSP:
          //zclSampleTemperatureSensor_ProcessInConfigReportRspCmd( pInMsg );
          break;
    
        case ZCL_CMD_READ_REPORT_CFG:
          //zclSampleTemperatureSensor_ProcessInReadReportCfgCmd( pInMsg );
          break;
    
        case ZCL_CMD_READ_REPORT_CFG_RSP:
          //zclSampleTemperatureSensor_ProcessInReadReportCfgRspCmd( pInMsg );
          break;
    
        case ZCL_CMD_REPORT:
          //zclSampleTemperatureSensor_ProcessInReportCmd( pInMsg );
          break;
    #endif
        case ZCL_CMD_DEFAULT_RSP:
          zclSampleTemperatureSensor_ProcessInDefaultRspCmd( pInMsg );
          break;
    #ifdef ZCL_DISCOVER
        case ZCL_CMD_DISCOVER_CMDS_RECEIVED_RSP:
          zclSampleTemperatureSensor_ProcessInDiscCmdsRspCmd( pInMsg );
          break;
    
        case ZCL_CMD_DISCOVER_CMDS_GEN_RSP:
          zclSampleTemperatureSensor_ProcessInDiscCmdsRspCmd( pInMsg );
          break;
    
        case ZCL_CMD_DISCOVER_ATTRS_RSP:
          zclSampleTemperatureSensor_ProcessInDiscAttrsRspCmd( pInMsg );
          break;
    
        case ZCL_CMD_DISCOVER_ATTRS_EXT_RSP:
          zclSampleTemperatureSensor_ProcessInDiscAttrsExtRspCmd( pInMsg );
          break;
    #endif
        default:
          break;
      }
    
      if ( pInMsg->attrCmd )
      {
        osal_mem_free( pInMsg->attrCmd );
      }
    }
    
    #ifdef ZCL_READ
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_ProcessInReadRspCmd
     *
     * @brief   Process the "Profile" Read Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleTemperatureSensor_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      zclSampleTemperatureSensor_ProcessInWriteRspCmd
     *
     * @brief   Process the "Profile" Write Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleTemperatureSensor_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      zclSampleTemperatureSensor_ProcessInDefaultRspCmd
     *
     * @brief   Process the "Profile" Default Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleTemperatureSensor_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      zclSampleTemperatureSensor_ProcessInDiscCmdsRspCmd
     *
     * @brief   Process the Discover Commands Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleTemperatureSensor_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      zclSampleTemperatureSensor_ProcessInDiscAttrsRspCmd
     *
     * @brief   Process the "Profile" Discover Attributes Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleTemperatureSensor_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      zclSampleTemperatureSensor_ProcessInDiscAttrsExtRspCmd
     *
     * @brief   Process the "Profile" Discover Attributes Extended Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleTemperatureSensor_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
    
    #ifdef ZCL_EZMODE
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_ProcessZDOMsgs
     *
     * @brief   Called when this node receives a ZDO/ZDP response.
     *
     * @param   none
     *
     * @return  status
     */
    static void zclSampleTemperatureSensor_ProcessZDOMsgs( zdoIncomingMsg_t *pMsg )
    {
      zclEZMode_ActionData_t data;
      ZDO_MatchDescRsp_t *pMatchDescRsp;
    
      // Let EZ-Mode know of the Match Descriptor Response
      if ( pMsg->clusterID == Match_Desc_rsp )
      {
        pMatchDescRsp = ZDO_ParseEPListRsp( pMsg );
        data.pMatchDescRsp = pMatchDescRsp;
        zcl_EZModeAction( EZMODE_ACTION_MATCH_DESC_RSP, &data );
        osal_mem_free( pMatchDescRsp );
      }
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_EZModeCB
     *
     * @brief   The Application is informed of events. This can be used to show on the UI what is
    *           going on during EZ-Mode steering/finding/binding.
     *
     * @param   state - an
     *
     * @return  none
     */
    static void zclSampleTemperatureSensor_EZModeCB( zlcEZMode_State_t state, zclEZMode_CBData_t *pData )
    {
    #ifdef LCD_SUPPORTED
      char szLine[20];
      char *pStr;
      uint8 err;
    #endif
    
      // time to go into identify mode
      if ( state == EZMODE_STATE_IDENTIFYING )
      {
        zclSampleTemperatureSensor_IdentifyTime = ( EZMODE_TIME / 1000 );  // convert to seconds
        zclSampleTemperatureSensor_ProcessIdentifyTimeChange();
      }
    
      // autoclosing, show what happened (success, cancelled, etc...)
      if( state == EZMODE_STATE_AUTOCLOSE )
      {
    #ifdef LCD_SUPPORTED
        pStr = NULL;
        err = pData->sAutoClose.err;
        if ( err == EZMODE_ERR_SUCCESS )
        {
          pStr = "EZMode: Success";
        }
        else if ( err == EZMODE_ERR_NOMATCH )
        {
          pStr = "EZMode: NoMatch"; // not a match made in heaven
        }
        if ( pStr )
        {
          if ( giTemperatureSensorScreenMode == TEMPSENSE_MAINMODE )
          {
            HalLcdWriteString ( pStr, HAL_LCD_LINE_2 );
          }
        }
    #endif
      }
    
      // finished, either show DstAddr/EP, or nothing (depending on success or not)
      if( state == EZMODE_STATE_FINISH )
      {
        // turn off identify mode
        zclSampleTemperatureSensor_IdentifyTime = 0;
        zclSampleTemperatureSensor_ProcessIdentifyTimeChange();
    
    #ifdef LCD_SUPPORTED
        // if successful, inform user which nwkaddr/ep we bound to
        pStr = NULL;
        err = pData->sFinish.err;
        if( err == EZMODE_ERR_SUCCESS )
        {
          // "EZDst:1234 EP:34"
          osal_memcpy( szLine, "EZDst:", 6 );
          zclHA_uint16toa( pData->sFinish.nwkaddr, &szLine[6] );
          osal_memcpy( &szLine[10], " EP:", 4 );
          _ltoa( pData->sFinish.ep, (void *)(&szLine[14]), 16 );  // _ltoa NULL terminates
          pStr = szLine;
        }
        else if ( err == EZMODE_ERR_BAD_PARAMETER )
        {
          pStr = "EZMode: BadParm";
        }
        else if ( err == EZMODE_ERR_CANCELLED )
        {
          pStr = "EZMode: Cancel";
        }
        else
        {
          pStr = "EZMode: TimeOut";
        }
        if ( pStr )
        {
          if ( giTemperatureSensorScreenMode == TEMPSENSE_MAINMODE )
          {
            HalLcdWriteString ( pStr, HAL_LCD_LINE_2 );
          }
        }
    #endif  // LCD_SUPPORTED
    
        // show main UI screen 3 seconds after joining network
        osal_start_timerEx( zclSampleTemperatureSensor_TaskID, SAMPLETEMPERATURESENSOR_MAIN_SCREEN_EVT, 3000 );
    
        // report current temperature reading 15 seconds after joinging the network
        osal_start_timerEx( zclSampleTemperatureSensor_TaskID, SAMPLETEMPERATURESENSOR_TEMP_SEND_EVT, SAMPLETEMPERATURESENSOR_REPORT_INTERVAL );
      }
    }
    #endif // ZCL_EZMODE
    
    /****************************************************************************
    ****************************************************************************/
    
    
    

    Error[Pe147]: declaration is incompatible with "__banked_func __xdata_reentrant void getTemperature(void)" (declared at line 175) E:\PROJECT\13$11\Z-Stack Home 1.2.1\Projects\zstack\HomeAutomation\SampleTemperatureSensor\Source\zcl_sampletemperaturesensor.c 418
    Warning[Pe111]: statement is unreachable E:\PROJECT\13$11\Z-Stack Home 1.2.1\Projects\zstack\HomeAutomation\SampleTemperatureSensor\Source\zcl_sampletemperaturesensor.c 452
    Warning[Pe177]: variable "TempValue" was declared but never referenced E:\PROJECT\13$11\Z-Stack Home 1.2.1\Projects\zstack\HomeAutomation\SampleTemperatureSensor\Source\zcl_sampletemperaturesensor.c 426
    Error while running C/C++ Compiler

    anyone can help with these errors ??

  • Variable zclSampleTemperatureSensor_MeasuredValue is defined in zcl_sampletemperaturesensor_data.c.

  • sir i didnt get what are trying to say ??

  • Since this variable is declare in zcl_sampletemperaturesensor_data.c, do you include zcl_sampletemperaturesensor_data.c in your project? By the way, this is very basic thing in programming world and I suggest you do some study in C programmin.

  • sir i did it too ... 

    8637.zcl_sampletemperaturesensor.c
    /**************************************************************************************************
      Filename:       zcl_sampletemperaturesensor.c
      Revised:        $Date: 2013-10-18 11:49:27 -0700 (Fri, 18 Oct 2013) $
      Revision:       $Revision: 35718 $
    
      Description:    Zigbee Cluster Library - sample device application.
    
    
      Copyright 2013 Texas Instruments Incorporated. All rights reserved.
    
      IMPORTANT: Your use of this Software is limited to those specific rights
      granted under the terms of a software license agreement between the user
      who downloaded the software, his/her employer (which must be your employer)
      and Texas Instruments Incorporated (the "License").  You may not use this
      Software unless you agree to abide by the terms of the License. The License
      limits your use, and you acknowledge, that the Software may not be modified,
      copied or distributed unless embedded on a Texas Instruments microcontroller
      or used solely and exclusively in conjunction with a Texas Instruments radio
      frequency transceiver, which is integrated into your product.  Other than for
      the foregoing purpose, you may not use, reproduce, copy, prepare derivative
      works of, modify, distribute, perform, display or sell this Software and/or
      its documentation for any purpose.
    
      YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE
      PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,
      INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE,
      NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL
      TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT,
      NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER
      LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
      INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE
      OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT
      OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
      (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
    
      Should you have any questions regarding your right to use this Software,
      contact Texas Instruments Incorporated at www.TI.com.
    **************************************************************************************************/
    
    /*********************************************************************
      This device will act as a temperature sensor. It updates the current
      temperature on the thermostat when the user sends the desired
      temperature using SW1.
    
      SCREEN MODES
      ----------------------------------------
      Main:
        - SW1: Send current temperature
        - SW2: Invoke EZMode
        - SW3: Adjust temperature
        - SW5: Go to Help screen
    
      Temperature:
        - SW1: Increase temperature
        - SW3: Decrease temperature
        - SW5: Enter temperature
      ----------------------------------------
    *********************************************************************/
    
    /*********************************************************************
     * INCLUDES
     */
    #include "ZComDef.h"
    #include "OSAL.h"
    #include "AF.h"
    #include "ZDApp.h"
    #include "ZDObject.h"
    #include "ZDProfile.h"
    #include "MT_SYS.h"
    
    #include "zcl.h"
    #include "zcl_general.h"
    #include "zcl_ha.h"
    #include "zcl_ezmode.h"
    #include "zcl_ms.h"
    #include "hal_adc.h"
    
    #include "zcl_sampletemperaturesensor.h"
    
    #include "onboard.h"
    
    /* HAL */
    #include "hal_lcd.h"
    #include "hal_led.h"
    #include "hal_key.h"
    #include "zcl_sampletemperaturesensor_data.c"
    
    /*********************************************************************
     * MACROS
     */
    
    // how often to report temperature
    #define SAMPLETEMPERATURESENSOR_REPORT_INTERVAL   10000
    
    /*********************************************************************
     * CONSTANTS
     */
    
    /*********************************************************************
     * TYPEDEFS
     */
    
    /*********************************************************************
     * GLOBAL VARIABLES
     */
    byte zclSampleTemperatureSensor_TaskID;
    
    uint8 zclSampleTemperatureSensorSeqNum;
    
    static byte gPermitDuration = 0x00;
    
    /*void initTempSensor(void){
       DISABLE_ALL_INTERRUPTS();            //??????
       InitClock();                         //????????32M
       *((BYTE __xdata*) 0x624B) = 0x01;    //???????
       *((BYTE __xdata*) 0x61BD) = 0x01;    //???????ADC????
    }*/
    
    /*********************************************************************
     * GLOBAL FUNCTIONS
     */
    
    /*********************************************************************
     * LOCAL VARIABLES
     */
    afAddrType_t zclSampleTemperatureSensor_DstAddr;
    
    #ifdef ZCL_EZMODE
    static void zclSampleTemperatureSensor_ProcessZDOMsgs( zdoIncomingMsg_t *pMsg );
    static void zclSampleTemperatureSensor_EZModeCB( zlcEZMode_State_t state, zclEZMode_CBData_t *pData );
    
    static const zclEZMode_RegisterData_t zclSampleTemperatureSensor_RegisterEZModeData =
    {
      &zclSampleTemperatureSensor_TaskID,
      SAMPLETEMPERATURESENSOR_EZMODE_NEXTSTATE_EVT,
      SAMPLETEMPERATURESENSOR_EZMODE_TIMEOUT_EVT,
      &zclSampleTemperatureSensorSeqNum,
      zclSampleTemperatureSensor_EZModeCB
    };
    
    // NOT ZCL_EZMODE, Use EndDeviceBind
    #else
    
    static cId_t bindingOutClusters[] =
    {
      ZCL_CLUSTER_ID_MS_TEMPERATURE_MEASUREMENT
    };
    #define ZCLSAMPLETEMPERATURESENSOR_BINDINGLIST        1
    #endif
    
    devStates_t zclSampleTemperatureSensor_NwkState = DEV_INIT;
    
    uint8 giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;   // display main screen mode first
    
    static uint8 aProcessCmd[] = { 1, 0, 0, 0 }; // used for reset command, { length + cmd0 + cmd1 + data }
    
    // Test Endpoint to allow SYS_APP_MSGs
    static endPointDesc_t sampleTemperatureSensor_TestEp =
    {
      20,                                 // Test endpoint
      &zclSampleTemperatureSensor_TaskID,
      (SimpleDescriptionFormat_t *)NULL,  // No Simple description for this test endpoint
      (afNetworkLatencyReq_t)0            // No Network Latency req
    };
    
    
    /*********************************************************************
     * LOCAL FUNCTIONS
     */
    static void zclSampleTemperatureSensor_HandleKeys( byte shift, byte keys );
    static void zclSampleTemperatureSensor_BasicResetCB( void );
    static void zclSampleTemperatureSensor_IdentifyCB( zclIdentify_t *pCmd );
    static void zclSampleTemperatureSensor_IdentifyQueryRspCB( zclIdentifyQueryRsp_t *pRsp );
    static void zclSampleTemperatureSensor_ProcessIdentifyTimeChange( void );
    void getTemperature(void);
    // app display functions
    void zclSampleTemperatureSensor_LcdDisplayUpdate(void);
    void zclSampleTemperatureSensor_LcdDisplayMainMode(void);
    void zclSampleTemperatureSensor_LcdDisplayTempMode(void);
    void zclSampleTemperatureSensor_LcdDisplayHelpMode(void);
    int16 getTemperature(void);
    
    static void zclSampleTemperatureSensor_SendTemp(void);
    
    // Functions to process ZCL Foundation incoming Command/Response messages
    static void zclSampleTemperatureSensor_ProcessIncomingMsg( zclIncomingMsg_t *msg );
    #ifdef ZCL_READ
    static uint8 zclSampleTemperatureSensor_ProcessInReadRspCmd( zclIncomingMsg_t *pInMsg );
    #endif
    #ifdef ZCL_WRITE
    static uint8 zclSampleTemperatureSensor_ProcessInWriteRspCmd( zclIncomingMsg_t *pInMsg );
    #endif
    static uint8 zclSampleTemperatureSensor_ProcessInDefaultRspCmd( zclIncomingMsg_t *pInMsg );
    #ifdef ZCL_DISCOVER
    static uint8 zclSampleTemperatureSensor_ProcessInDiscCmdsRspCmd( zclIncomingMsg_t *pInMsg );
    static uint8 zclSampleTemperatureSensor_ProcessInDiscAttrsRspCmd( zclIncomingMsg_t *pInMsg );
    static uint8 zclSampleTemperatureSensor_ProcessInDiscAttrsExtRspCmd( zclIncomingMsg_t *pInMsg );
    #endif // ZCL_DISCOVER
    
    /*********************************************************************
     * STATUS STRINGS
     */
    #ifdef LCD_SUPPORTED
    const char sClearLine[]    = " ";
    const char sDeviceName[]   = "  Temp Sensor";
    const char sSwTempUp[]     = "SW1: Raise Temp";
    const char sSwEZMode[]     = "SW2: EZ-Mode";
    const char sSwTempDown[]   = "SW3: Lower Temp";
    const char sSwHelp[]       = "SW5: Help";
    #endif
    
    /*********************************************************************
     * ZCL General Profile Callback table
     */
    static zclGeneral_AppCallbacks_t zclSampleTemperatureSensor_CmdCallbacks =
    {
      zclSampleTemperatureSensor_BasicResetCB,        // Basic Cluster Reset command
      zclSampleTemperatureSensor_IdentifyCB,          // Identify command
    #ifdef ZCL_EZMODE
      NULL,                                           // Identify EZ-Mode Invoke command
      NULL,                                           // Identify Update Commission State command
    #endif
      NULL,                                           // Identify Trigger Effect command
      zclSampleTemperatureSensor_IdentifyQueryRspCB,  // Identify Query Response command
      NULL,             				                      // On/Off cluster command
      NULL,                                           // On/Off cluster enhanced command Off with Effect
      NULL,                                           // On/Off cluster enhanced command On with Recall Global Scene
      NULL,                                           // On/Off cluster enhanced command On with Timed Off
    #ifdef ZCL_LEVEL_CTRL
      NULL,                                           // Level Control Move to Level command
      NULL,                                           // Level Control Move command
      NULL,                                           // Level Control Step command
      NULL,                                           // Level Control Stop command
    #endif
    #ifdef ZCL_GROUPS
      NULL,                                           // Group Response commands
    #endif
    #ifdef ZCL_SCENES
      NULL,                                           // Scene Store Request command
      NULL,                                           // Scene Recall Request command
      NULL,                                           // Scene Response command
    #endif
    #ifdef ZCL_ALARMS
      NULL,                                           // Alarm (Response) commands
    #endif
    #ifdef SE_UK_EXT
      NULL,                                           // Get Event Log command
      NULL,                                           // Publish Event Log command
    #endif
      NULL,                                           // RSSI Location command
      NULL                                            // RSSI Location Response command
    };
    
    /*********************************************************************
     * @fn          zclSampleTemperatureSensor_Init
     *
     * @brief       Initialization function for the zclGeneral layer.
     *
     * @param       none
     *
     * @return      none
     */
    void zclSampleTemperatureSensor_Init( byte task_id )
    {
      zclSampleTemperatureSensor_TaskID = task_id;
    
      // Set destination address to indirect
      zclSampleTemperatureSensor_DstAddr.addrMode = (afAddrMode_t)AddrNotPresent;
      zclSampleTemperatureSensor_DstAddr.endPoint = 0;
      zclSampleTemperatureSensor_DstAddr.addr.shortAddr = 0;
    
      // This app is part of the Home Automation Profile
      zclHA_Init( &zclSampleTemperatureSensor_SimpleDesc );
    
      // Register the ZCL General Cluster Library callback functions
      zclGeneral_RegisterCmdCallbacks( SAMPLETEMPERATURESENSOR_ENDPOINT, &zclSampleTemperatureSensor_CmdCallbacks );
    
      // Register the application's attribute list
      zcl_registerAttrList( SAMPLETEMPERATURESENSOR_ENDPOINT, SAMPLETEMPERATURESENSOR_MAX_ATTRIBUTES, zclSampleTemperatureSensor_Attrs );
    
      // Register the Application to receive the unprocessed Foundation command/response messages
      zcl_registerForMsg( zclSampleTemperatureSensor_TaskID );
    
    #ifdef ZCL_EZMODE
      // Register EZ-Mode
      zcl_RegisterEZMode( &zclSampleTemperatureSensor_RegisterEZModeData );
    
      // Register with the ZDO to receive Match Descriptor Responses
      ZDO_RegisterForZDOMsg(task_id, Match_Desc_rsp);
    #endif
    
      // Register for all key events - This app will handle all key events
      RegisterForKeys( zclSampleTemperatureSensor_TaskID );
    
      // Register for a test endpoint
      afRegister( &sampleTemperatureSensor_TestEp );
    
    #ifdef LCD_SUPPORTED
      // display the device name
      HalLcdWriteString( (char *)sDeviceName, HAL_LCD_LINE_3 );
    #endif
    }
    
    /*********************************************************************
     * @fn          zclSample_event_loop
     *
     * @brief       Event Loop Processor for zclGeneral.
     *
     * @param       none
     *
     * @return      none
     */
    uint16 zclSampleTemperatureSensor_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( zclSampleTemperatureSensor_TaskID )) )
        {
          switch ( MSGpkt->hdr.event )
          {
    #ifdef ZCL_EZMODE
            case ZDO_CB_MSG:
              zclSampleTemperatureSensor_ProcessZDOMsgs( (zdoIncomingMsg_t *)MSGpkt );
              break;
    #endif
    
            case ZCL_INCOMING_MSG:
              // Incoming ZCL Foundation command/response messages
              zclSampleTemperatureSensor_ProcessIncomingMsg( (zclIncomingMsg_t *)MSGpkt );
              break;
    
            case KEY_CHANGE:
              zclSampleTemperatureSensor_HandleKeys( ((keyChange_t *)MSGpkt)->state, ((keyChange_t *)MSGpkt)->keys );
              break;
    
            case ZDO_STATE_CHANGE:
              zclSampleTemperatureSensor_NwkState = (devStates_t)(MSGpkt->hdr.status);
    
    
              // now on the network
              if ( (zclSampleTemperatureSensor_NwkState == DEV_ZB_COORD) ||
                   (zclSampleTemperatureSensor_NwkState == DEV_ROUTER)   ||
                   (zclSampleTemperatureSensor_NwkState == DEV_END_DEVICE) )
              {
    #ifndef HOLD_AUTO_START
                giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;
                zclSampleTemperatureSensor_LcdDisplayUpdate();
    #endif
    #ifdef ZCL_EZMODE
                zcl_EZModeAction( EZMODE_ACTION_NETWORK_STARTED, NULL );
    #endif // ZCL_EZMODE
              }
              break;
    
            default:
              break;
          }
    
          // Release the memory
          osal_msg_deallocate( (uint8 *)MSGpkt );
        }
    
        // return unprocessed events
        return (events ^ SYS_EVENT_MSG);
      }
    
      if ( events & SAMPLETEMPERATURESENSOR_IDENTIFY_TIMEOUT_EVT )
      {
        if ( zclSampleTemperatureSensor_IdentifyTime > 0 )
          zclSampleTemperatureSensor_IdentifyTime--;
        zclSampleTemperatureSensor_ProcessIdentifyTimeChange();
    
        return ( events ^ SAMPLETEMPERATURESENSOR_IDENTIFY_TIMEOUT_EVT );
      }
    
    #ifdef ZCL_EZMODE
      // going on to next state
      if ( events & SAMPLETEMPERATURESENSOR_EZMODE_NEXTSTATE_EVT )
      {
        zcl_EZModeAction ( EZMODE_ACTION_PROCESS, NULL );   // going on to next state
        return ( events ^ SAMPLETEMPERATURESENSOR_EZMODE_NEXTSTATE_EVT );
      }
    
      // the overall EZMode timer expired, so we timed out
      if ( events & SAMPLETEMPERATURESENSOR_EZMODE_TIMEOUT_EVT )
      {
        zcl_EZModeAction ( EZMODE_ACTION_TIMED_OUT, NULL ); // EZ-Mode timed out
        return ( events ^ SAMPLETEMPERATURESENSOR_EZMODE_TIMEOUT_EVT );
      }
    #endif // ZLC_EZMODE
    
      if ( events & SAMPLETEMPERATURESENSOR_MAIN_SCREEN_EVT )
      {
        giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;
        zclSampleTemperatureSensor_LcdDisplayUpdate();
    
        return ( events ^ SAMPLETEMPERATURESENSOR_MAIN_SCREEN_EVT );
      }
    
      if ( events & SAMPLETEMPERATURESENSOR_TEMP_SEND_EVT )
      {
        zclSampleTemperatureSensor_SendTemp();
    
        // report current temperature reading every 10 seconds
        osal_start_timerEx( zclSampleTemperatureSensor_TaskID, SAMPLETEMPERATURESENSOR_TEMP_SEND_EVT, SAMPLETEMPERATURESENSOR_REPORT_INTERVAL );
    
        return ( events ^ SAMPLETEMPERATURESENSOR_TEMP_SEND_EVT );
      }
    
      // Discard unknown events
      return 0;
    }
    
    int16 getTemperature(void)
    {
      int16 AvgTemp;
      uint8   i;
      uint16  AdcValue;
      uint16  value;
      uint16  value1;
    //char i;
            char TempValue[10]; 
            //initTempSensor(); 
      AdcValue = 0;
      for( i = 0; i < 4; i++ )
      {
    ADCIF = 0;
        ADCCON3 = (HAL_ADC_REF_125V | HAL_ADC_RESOLUTION_14 | HAL_ADC_CHN_TEMP);    // ??1.25V????,12????,AD??:?????
            while ( !ADCIF );                                              //?????ADC
                     
        value =  ADCL >> 2;                        
        value |= (((UINT16)ADCH) << 6);    
        AdcValue += value;                          
      }
      value = AdcValue >> 2;                        //????4,?????
      value1 =   (((value) >> 4) - 335);
                 //??AD?,????????
      while(1)
            {
              AvgTemp = 0;
              for(i = 0 ; i < 64 ; i++)
              {
                AvgTemp += value1;              
                AvgTemp >>= 1;                              //??????2.
              }
              
            }
      //zclSampleTemperatureSensor_MeasuredValue = AvgTemp ; 
      return AvgTemp;
    }
    
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_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_3
     *                 HAL_KEY_SW_2
     *                 HAL_KEY_SW_1
     *
     * @return  none
     */
    static void zclSampleTemperatureSensor_HandleKeys( byte shift, byte keys )
    {
      if ( keys & HAL_KEY_SW_1 )
      {
        // increase temperature
        giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;
    
        if ( zclSampleTemperatureSensor_MeasuredValue < zclSampleTemperatureSensor_MaxMeasuredValue )
        {
          zclSampleTemperatureSensor_MeasuredValue = zclSampleTemperatureSensor_MeasuredValue + 100;  // considering using whole number value
        }
        else if ( zclSampleTemperatureSensor_MeasuredValue >= zclSampleTemperatureSensor_MaxMeasuredValue )
        {
          zclSampleTemperatureSensor_MeasuredValue = zclSampleTemperatureSensor_MaxMeasuredValue;
        }
    
        // Send temperature information
        zclSampleTemperatureSensor_SendTemp();
      }
    
      if ( keys & HAL_KEY_SW_2 )
      {
        if ( ( giTemperatureSensorScreenMode == TEMPSENSE_MAINMODE ) ||
            ( giTemperatureSensorScreenMode == TEMPSENSE_HELPMODE ) )
        {
          giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;
    
    #ifdef ZCL_EZMODE
          zclEZMode_InvokeData_t ezModeData;
          static uint16 clusterIDs[] = { ZCL_CLUSTER_ID_MS_TEMPERATURE_MEASUREMENT };   // only bind on the Temperature Measurement cluster
    
          // Invoke EZ-Mode
          ezModeData.endpoint = SAMPLETEMPERATURESENSOR_ENDPOINT; // endpoint on which to invoke EZ-Mode
          if ( ( zclSampleTemperatureSensor_NwkState == DEV_ZB_COORD ) ||
               ( zclSampleTemperatureSensor_NwkState == DEV_ROUTER )   ||
               ( zclSampleTemperatureSensor_NwkState == DEV_END_DEVICE ) )
          {
            ezModeData.onNetwork = TRUE;      // node is already on the network
          }
          else
          {
            ezModeData.onNetwork = FALSE;     // node is not yet on the network
          }
          ezModeData.initiator = TRUE;        // Temperature Sensor is an initiator
          ezModeData.numActiveInClusters = 1;
          ezModeData.pActiveInClusterIDs = clusterIDs;
          ezModeData.numActiveOutClusters = 0;   // active output cluster
          ezModeData.pActiveOutClusterIDs = NULL;
          zcl_InvokeEZMode( &ezModeData );
    
    #ifdef LCD_SUPPORTED
          HalLcdWriteString( "EZMode", HAL_LCD_LINE_2 );
    #endif
    
          // NOT ZCL_EZMODE, Use EndDeviceBind
    #else
          {
            zAddrType_t dstAddr;
            dstAddr.addrMode = Addr16Bit;
            dstAddr.addr.shortAddr = 0;   // Coordinator makes the EDB match
    
            // Initiate an End Device Bind Request, this bind request will
            // only use a cluster list that is important to binding.
            HalLedSet ( HAL_LED_4, HAL_LED_MODE_OFF );
            ZDP_EndDeviceBindReq( &dstAddr, NLME_GetShortAddr(),
                                  SAMPLETEMPERATURESENSOR_ENDPOINT,
                                  ZCL_HA_PROFILE_ID,
                                  0, NULL,
                                  ZCLSAMPLETEMPERATURESENSOR_BINDINGLIST, bindingOutClusters,
                                  FALSE );
          }
    #endif // ZCL_EZMODE
        }
      }
    
      if ( keys & HAL_KEY_SW_3 )
      {
        giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;
    
        // decrease the temperature
        if ( zclSampleTemperatureSensor_MeasuredValue > zclSampleTemperatureSensor_MinMeasuredValue )
        {
          zclSampleTemperatureSensor_MeasuredValue = zclSampleTemperatureSensor_MeasuredValue - 100;  // considering using whole number value
        }
        else if ( zclSampleTemperatureSensor_MeasuredValue >= zclSampleTemperatureSensor_MinMeasuredValue )
        {
          zclSampleTemperatureSensor_MeasuredValue = zclSampleTemperatureSensor_MinMeasuredValue;
        }
    
        // Send temperature information
        zclSampleTemperatureSensor_SendTemp();
      }
    
      if ( keys & HAL_KEY_SW_4 )
      {
        giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;
    
        if ( ( zclSampleTemperatureSensor_NwkState == DEV_ZB_COORD ) ||
             ( zclSampleTemperatureSensor_NwkState == DEV_ROUTER ) )
        {
          // toggle permit join
          gPermitDuration = gPermitDuration ? 0 : 0xff;
          NLME_PermitJoiningRequest( gPermitDuration );
        }
      }
    
      if ( shift && ( keys & HAL_KEY_SW_5 ) )
      {
        zclSampleTemperatureSensor_BasicResetCB();
      }
      else if ( keys & HAL_KEY_SW_5 )
      {
        if ( giTemperatureSensorScreenMode == TEMPSENSE_MAINMODE )
        {
          giTemperatureSensorScreenMode = TEMPSENSE_HELPMODE;
        }
        else if ( giTemperatureSensorScreenMode == TEMPSENSE_HELPMODE )
        {
    #ifdef LCD_SUPPORTED
          HalLcdWriteString( (char *)sClearLine, HAL_LCD_LINE_2 );
    #endif
          giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;
        }
      }
    
      // update display
      zclSampleTemperatureSensor_LcdDisplayUpdate();
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_LcdDisplayUpdate
     *
     * @brief   Called to update the LCD display.
     *
     * @param   none
     *
     * @return  none
     */
    void zclSampleTemperatureSensor_LcdDisplayUpdate( void )
    {
      // turn on red LED for temperatures >= 24.00C
      if ( zclSampleTemperatureSensor_MeasuredValue >= 2400 )
      {
        HalLedSet ( HAL_LED_1, HAL_LED_MODE_OFF );
        HalLedSet ( HAL_LED_2, HAL_LED_MODE_ON );
      }
      // turn on green LED for temperatures <= 20.00C
      else if ( zclSampleTemperatureSensor_MeasuredValue <= 2000 )
      {
        HalLedSet ( HAL_LED_1, HAL_LED_MODE_ON );
        HalLedSet ( HAL_LED_2, HAL_LED_MODE_OFF );
      }
      // turn on both red and green LEDs for temperatures between 20.00C and 24.00C
      else
      {
        HalLedSet ( HAL_LED_1, HAL_LED_MODE_ON );
        HalLedSet ( HAL_LED_2, HAL_LED_MODE_ON );
      }
    
      if ( giTemperatureSensorScreenMode == TEMPSENSE_HELPMODE )
      {
        zclSampleTemperatureSensor_LcdDisplayHelpMode();
      }
      else
      {
        zclSampleTemperatureSensor_LcdDisplayMainMode();
      }
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_LcdDisplayMainMode
     *
     * @brief   Called to display the main screen on the LCD.
     *
     * @param   none
     *
     * @return  none
     */
    void zclSampleTemperatureSensor_LcdDisplayMainMode( void )
    {
      char sDisplayTemp[16];
    
      if ( zclSampleTemperatureSensor_NwkState == DEV_ZB_COORD )
      {
        zclHA_LcdStatusLine1( 0 );
      }
      else if ( zclSampleTemperatureSensor_NwkState == DEV_ROUTER )
      {
        zclHA_LcdStatusLine1( 1 );
      }
      else if ( zclSampleTemperatureSensor_NwkState == DEV_END_DEVICE )
      {
        zclHA_LcdStatusLine1( 2 );
      }
    
      // display current temperature
      osal_memcpy(sDisplayTemp, "TEMP: ", 6);
      _ltoa( ( zclSampleTemperatureSensor_MeasuredValue / 100 ), (void *)(&sDisplayTemp[6]), 10 );   // convert temperature to whole number
      osal_memcpy( &sDisplayTemp[8], "C", 2 );
    #ifdef LCD_SUPPORTED
      HalLcdWriteString( (char *)sDisplayTemp, HAL_LCD_LINE_2 );
    #endif
    
    #ifdef LCD_SUPPORTED
      if ( ( zclSampleTemperatureSensor_NwkState == DEV_ZB_COORD ) ||
           ( zclSampleTemperatureSensor_NwkState == DEV_ROUTER ) )
      {
        // display help key with permit join status
        if ( gPermitDuration )
        {
          HalLcdWriteString( "SW5: Help      *", HAL_LCD_LINE_3 );
        }
        else
        {
          HalLcdWriteString( "SW5: Help       ", HAL_LCD_LINE_3 );
        }
      }
      else
      {
        // display help key
        HalLcdWriteString( (char *)sSwHelp, HAL_LCD_LINE_3 );
      }
    #endif
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_LcdDisplayHelpMode
     *
     * @brief   Called to display the SW options on the LCD.
     *
     * @param   none
     *
     * @return  none
     */
    void zclSampleTemperatureSensor_LcdDisplayHelpMode( void )
    {
    #ifdef LCD_SUPPORTED
      HalLcdWriteString( (char *)sSwTempUp, HAL_LCD_LINE_1 );
      HalLcdWriteString( (char *)sSwEZMode, HAL_LCD_LINE_2 );
      HalLcdWriteString( (char *)sSwTempDown, HAL_LCD_LINE_3 );
    #endif
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_SendTemp
     *
     * @brief   Called to send current temperature information to the thermostat
     *
     * @param   none
     *
     * @return  none
     */
    static void zclSampleTemperatureSensor_SendTemp( void )
    {
    #ifdef ZCL_REPORT
      zclReportCmd_t *pReportCmd;
      zclSampleTemperatureSensor_MeasuredValue = getTemperature();
      pReportCmd = osal_mem_alloc( sizeof(zclReportCmd_t) + sizeof(zclReport_t) );
      if ( pReportCmd != NULL )
      {
        pReportCmd->numAttr = 1;
        pReportCmd->attrList[0].attrID = ATTRID_MS_TEMPERATURE_MEASURED_VALUE;
        pReportCmd->attrList[0].dataType = ZCL_DATATYPE_INT16;
        pReportCmd->attrList[0].attrData = (void *)(&zclSampleTemperatureSensor_MeasuredValue);
    
        zcl_SendReportCmd( SAMPLETEMPERATURESENSOR_ENDPOINT, &zclSampleTemperatureSensor_DstAddr,
                           ZCL_CLUSTER_ID_MS_TEMPERATURE_MEASUREMENT,
                           pReportCmd, ZCL_FRAME_SERVER_CLIENT_DIR, TRUE, zclSampleTemperatureSensorSeqNum++ );
      }
    
      osal_mem_free( pReportCmd );
    #endif  // ZCL_REPORT
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_ProcessIdentifyTimeChange
     *
     * @brief   Called to process any change to the IdentifyTime attribute.
     *
     * @param   none
     *
     * @return  none
     */
    static void zclSampleTemperatureSensor_ProcessIdentifyTimeChange( void )
    {
      if ( zclSampleTemperatureSensor_IdentifyTime > 0 )
      {
        osal_start_timerEx( zclSampleTemperatureSensor_TaskID, SAMPLETEMPERATURESENSOR_IDENTIFY_TIMEOUT_EVT, 1000 );
        HalLedBlink ( HAL_LED_4, 0xFF, HAL_LED_DEFAULT_DUTY_CYCLE, HAL_LED_DEFAULT_FLASH_TIME );
      }
      else
      {
        if ( zclSampleTemperatureSensor_OnOff )
        {
          HalLedSet ( HAL_LED_4, HAL_LED_MODE_ON );
        }
        else
        {
          HalLedSet ( HAL_LED_4, HAL_LED_MODE_OFF );
        }
    
        osal_stop_timerEx( zclSampleTemperatureSensor_TaskID, SAMPLETEMPERATURESENSOR_IDENTIFY_TIMEOUT_EVT );
      }
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_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 zclSampleTemperatureSensor_BasicResetCB( void )
    {
      // Put device back to factory default settings
      zgWriteStartupOptions( ZG_STARTUP_SET, 3 );   // bit set both default configuration and default network
    
      // restart device
      MT_SysCommandProcessing( aProcessCmd );
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_IdentifyCB
     *
     * @brief   Callback from the ZCL General Cluster Library when
     *          it received an Identity Command for this application.
     *
     * @param   srcAddr - source address and endpoint of the response message
     * @param   identifyTime - the number of seconds to identify yourself
     *
     * @return  none
     */
    static void zclSampleTemperatureSensor_IdentifyCB( zclIdentify_t *pCmd )
    {
      zclSampleTemperatureSensor_IdentifyTime = pCmd->identifyTime;
      zclSampleTemperatureSensor_ProcessIdentifyTimeChange();
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_IdentifyQueryRspCB
     *
     * @brief   Callback from the ZCL General Cluster Library when
     *          it received an Identity Query Response Command for this application.
     *
     * @param   srcAddr - requestor's address
     * @param   timeout - number of seconds to identify yourself (valid for query response)
     *
     * @return  none
     */
    static void zclSampleTemperatureSensor_IdentifyQueryRspCB( zclIdentifyQueryRsp_t *pRsp )
    {
      (void)pRsp;
    #ifdef ZCL_EZMODE
      {
        zclEZMode_ActionData_t data;
        data.pIdentifyQueryRsp = pRsp;
        zcl_EZModeAction ( EZMODE_ACTION_IDENTIFY_QUERY_RSP, &data );
      }
    #endif
    }
    
    /******************************************************************************
     *
     *  Functions for processing ZCL Foundation incoming Command/Response messages
     *
     *****************************************************************************/
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_ProcessIncomingMsg
     *
     * @brief   Process ZCL Foundation incoming message
     *
     * @param   pInMsg - pointer to the received message
     *
     * @return  none
     */
    static void zclSampleTemperatureSensor_ProcessIncomingMsg( zclIncomingMsg_t *pInMsg)
    {
      switch ( pInMsg->zclHdr.commandID )
      {
    #ifdef ZCL_READ
        case ZCL_CMD_READ_RSP:
          zclSampleTemperatureSensor_ProcessInReadRspCmd( pInMsg );
          break;
    #endif
    #ifdef ZCL_WRITE
        case ZCL_CMD_WRITE_RSP:
          zclSampleTemperatureSensor_ProcessInWriteRspCmd( pInMsg );
          break;
    #endif
    #ifdef ZCL_REPORT
        // See ZCL Test Applicaiton (zcl_testapp.c) for sample code on Attribute Reporting
        case ZCL_CMD_CONFIG_REPORT:
          //zclSampleTemperatureSensor_ProcessInConfigReportCmd( pInMsg );
          break;
    
        case ZCL_CMD_CONFIG_REPORT_RSP:
          //zclSampleTemperatureSensor_ProcessInConfigReportRspCmd( pInMsg );
          break;
    
        case ZCL_CMD_READ_REPORT_CFG:
          //zclSampleTemperatureSensor_ProcessInReadReportCfgCmd( pInMsg );
          break;
    
        case ZCL_CMD_READ_REPORT_CFG_RSP:
          //zclSampleTemperatureSensor_ProcessInReadReportCfgRspCmd( pInMsg );
          break;
    
        case ZCL_CMD_REPORT:
          //zclSampleTemperatureSensor_ProcessInReportCmd( pInMsg );
          break;
    #endif
        case ZCL_CMD_DEFAULT_RSP:
          zclSampleTemperatureSensor_ProcessInDefaultRspCmd( pInMsg );
          break;
    #ifdef ZCL_DISCOVER
        case ZCL_CMD_DISCOVER_CMDS_RECEIVED_RSP:
          zclSampleTemperatureSensor_ProcessInDiscCmdsRspCmd( pInMsg );
          break;
    
        case ZCL_CMD_DISCOVER_CMDS_GEN_RSP:
          zclSampleTemperatureSensor_ProcessInDiscCmdsRspCmd( pInMsg );
          break;
    
        case ZCL_CMD_DISCOVER_ATTRS_RSP:
          zclSampleTemperatureSensor_ProcessInDiscAttrsRspCmd( pInMsg );
          break;
    
        case ZCL_CMD_DISCOVER_ATTRS_EXT_RSP:
          zclSampleTemperatureSensor_ProcessInDiscAttrsExtRspCmd( pInMsg );
          break;
    #endif
        default:
          break;
      }
    
      if ( pInMsg->attrCmd )
      {
        osal_mem_free( pInMsg->attrCmd );
      }
    }
    
    #ifdef ZCL_READ
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_ProcessInReadRspCmd
     *
     * @brief   Process the "Profile" Read Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleTemperatureSensor_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      zclSampleTemperatureSensor_ProcessInWriteRspCmd
     *
     * @brief   Process the "Profile" Write Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleTemperatureSensor_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      zclSampleTemperatureSensor_ProcessInDefaultRspCmd
     *
     * @brief   Process the "Profile" Default Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleTemperatureSensor_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      zclSampleTemperatureSensor_ProcessInDiscCmdsRspCmd
     *
     * @brief   Process the Discover Commands Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleTemperatureSensor_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      zclSampleTemperatureSensor_ProcessInDiscAttrsRspCmd
     *
     * @brief   Process the "Profile" Discover Attributes Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleTemperatureSensor_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      zclSampleTemperatureSensor_ProcessInDiscAttrsExtRspCmd
     *
     * @brief   Process the "Profile" Discover Attributes Extended Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleTemperatureSensor_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
    
    #ifdef ZCL_EZMODE
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_ProcessZDOMsgs
     *
     * @brief   Called when this node receives a ZDO/ZDP response.
     *
     * @param   none
     *
     * @return  status
     */
    static void zclSampleTemperatureSensor_ProcessZDOMsgs( zdoIncomingMsg_t *pMsg )
    {
      zclEZMode_ActionData_t data;
      ZDO_MatchDescRsp_t *pMatchDescRsp;
    
      // Let EZ-Mode know of the Match Descriptor Response
      if ( pMsg->clusterID == Match_Desc_rsp )
      {
        pMatchDescRsp = ZDO_ParseEPListRsp( pMsg );
        data.pMatchDescRsp = pMatchDescRsp;
        zcl_EZModeAction( EZMODE_ACTION_MATCH_DESC_RSP, &data );
        osal_mem_free( pMatchDescRsp );
      }
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_EZModeCB
     *
     * @brief   The Application is informed of events. This can be used to show on the UI what is
    *           going on during EZ-Mode steering/finding/binding.
     *
     * @param   state - an
     *
     * @return  none
     */
    static void zclSampleTemperatureSensor_EZModeCB( zlcEZMode_State_t state, zclEZMode_CBData_t *pData )
    {
    #ifdef LCD_SUPPORTED
      char szLine[20];
      char *pStr;
      uint8 err;
    #endif
    
      // time to go into identify mode
      if ( state == EZMODE_STATE_IDENTIFYING )
      {
        zclSampleTemperatureSensor_IdentifyTime = ( EZMODE_TIME / 1000 );  // convert to seconds
        zclSampleTemperatureSensor_ProcessIdentifyTimeChange();
      }
    
      // autoclosing, show what happened (success, cancelled, etc...)
      if( state == EZMODE_STATE_AUTOCLOSE )
      {
    #ifdef LCD_SUPPORTED
        pStr = NULL;
        err = pData->sAutoClose.err;
        if ( err == EZMODE_ERR_SUCCESS )
        {
          pStr = "EZMode: Success";
        }
        else if ( err == EZMODE_ERR_NOMATCH )
        {
          pStr = "EZMode: NoMatch"; // not a match made in heaven
        }
        if ( pStr )
        {
          if ( giTemperatureSensorScreenMode == TEMPSENSE_MAINMODE )
          {
            HalLcdWriteString ( pStr, HAL_LCD_LINE_2 );
          }
        }
    #endif
      }
    
      // finished, either show DstAddr/EP, or nothing (depending on success or not)
      if( state == EZMODE_STATE_FINISH )
      {
        // turn off identify mode
        zclSampleTemperatureSensor_IdentifyTime = 0;
        zclSampleTemperatureSensor_ProcessIdentifyTimeChange();
    
    #ifdef LCD_SUPPORTED
        // if successful, inform user which nwkaddr/ep we bound to
        pStr = NULL;
        err = pData->sFinish.err;
        if( err == EZMODE_ERR_SUCCESS )
        {
          // "EZDst:1234 EP:34"
          osal_memcpy( szLine, "EZDst:", 6 );
          zclHA_uint16toa( pData->sFinish.nwkaddr, &szLine[6] );
          osal_memcpy( &szLine[10], " EP:", 4 );
          _ltoa( pData->sFinish.ep, (void *)(&szLine[14]), 16 );  // _ltoa NULL terminates
          pStr = szLine;
        }
        else if ( err == EZMODE_ERR_BAD_PARAMETER )
        {
          pStr = "EZMode: BadParm";
        }
        else if ( err == EZMODE_ERR_CANCELLED )
        {
          pStr = "EZMode: Cancel";
        }
        else
        {
          pStr = "EZMode: TimeOut";
        }
        if ( pStr )
        {
          if ( giTemperatureSensorScreenMode == TEMPSENSE_MAINMODE )
          {
            HalLcdWriteString ( pStr, HAL_LCD_LINE_2 );
          }
        }
    #endif  // LCD_SUPPORTED
    
        // show main UI screen 3 seconds after joining network
        osal_start_timerEx( zclSampleTemperatureSensor_TaskID, SAMPLETEMPERATURESENSOR_MAIN_SCREEN_EVT, 3000 );
    
        // report current temperature reading 15 seconds after joinging the network
        osal_start_timerEx( zclSampleTemperatureSensor_TaskID, SAMPLETEMPERATURESENSOR_TEMP_SEND_EVT, SAMPLETEMPERATURESENSOR_REPORT_INTERVAL );
      }
    }
    #endif // ZCL_EZMODE
    
    /****************************************************************************
    ****************************************************************************/
    
    
    

    Error[Pe147]: declaration is incompatible with "__banked_func __xdata_reentrant void getTemperature(void)" (declared at line 175) E:\PROJECT\14$11\Z-Stack Home 1.2.1\Projects\zstack\HomeAutomation\SampleTemperatureSensor\Source\zcl_sampletemperaturesensor.c 181

  • You told me that you don't have latest IAR 8.30 to build Z-Stack Home 1.2.1. Could you tell me the IAR version you use and Do you build SampleTemperatureSensor project?

  • yesterday i got 8.30 and started using the sampletemperaturesensor in one node and samplethermostat in another node ..

    the sampletemperaturesensor program is giving values from17 to 27by pressing button and not by sensing temperature via its input sensor so i used the a simple adc pgm along with it 2 get temperature from the internal sensor and send it ...at that time 1ly i was held up with this error

  • i cleared the above said error sir and nw it gives one more error like

    Error[e27]: Entry "zclSampleTemperatureSensor_Attrs" in module zcl_sampletemperaturesensor ( E:\PROJECT\14$11\Z-Stack Home 1.2.1\Projects\zstack\
    HomeAutomation\SampleTemperatureSensor\CC2530DB\EndDeviceEB\Obj\zcl_sampletemperaturesensor.r51 ) redefined in module
    zcl_sampletemperaturesensor_data ( E:\PROJECT\14$11\Z-Stack Home 1.2.1\Projects\zstack\HomeAutomation\SampleTemperatureSensor\CC2530DB\
    EndDeviceEB\Obj\zcl_sampletemperaturesensor_data.r51 )
    Error while running Linker

  • Do you modify anything on SampleTemperatureSensor? If you don't, you shouldn't have this problem. It seems that you define zclSampleTemperatureSensor_Attrs again in your zcl_sampletemperaturesensor.c

  • yes sir i modified it i added adc pgm to acquire data from the internal sensor and called that function in the lace where it will sending the data to the coordinator (dumped with samplethermostat ) pgm ..

    i hav to finish this task 2day sir ... pl help out  

  • To fix the compile error, you can remove declare of  zclSampleTemperatureSensor_Attrs in your zcl_sampletemperaturesensor.c

  • actually it is not declared in zcl_sampletemperaturesensor.c ..... i am sending u the modifies c file 

    2475.zcl_sampletemperaturesensor.c
    /**************************************************************************************************
      Filename:       zcl_sampletemperaturesensor.c
      Revised:        $Date: 2013-10-18 11:49:27 -0700 (Fri, 18 Oct 2013) $
      Revision:       $Revision: 35718 $
    
      Description:    Zigbee Cluster Library - sample device application.
    
    
      Copyright 2013 Texas Instruments Incorporated. All rights reserved.
    
      IMPORTANT: Your use of this Software is limited to those specific rights
      granted under the terms of a software license agreement between the user
      who downloaded the software, his/her employer (which must be your employer)
      and Texas Instruments Incorporated (the "License").  You may not use this
      Software unless you agree to abide by the terms of the License. The License
      limits your use, and you acknowledge, that the Software may not be modified,
      copied or distributed unless embedded on a Texas Instruments microcontroller
      or used solely and exclusively in conjunction with a Texas Instruments radio
      frequency transceiver, which is integrated into your product.  Other than for
      the foregoing purpose, you may not use, reproduce, copy, prepare derivative
      works of, modify, distribute, perform, display or sell this Software and/or
      its documentation for any purpose.
    
      YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE
      PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,
      INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE,
      NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL
      TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT,
      NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER
      LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
      INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE
      OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT
      OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
      (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
    
      Should you have any questions regarding your right to use this Software,
      contact Texas Instruments Incorporated at www.TI.com.
    **************************************************************************************************/
    
    /*********************************************************************
      This device will act as a temperature sensor. It updates the current
      temperature on the thermostat when the user sends the desired
      temperature using SW1.
    
      SCREEN MODES
      ----------------------------------------
      Main:
        - SW1: Send current temperature
        - SW2: Invoke EZMode
        - SW3: Adjust temperature
        - SW5: Go to Help screen
    
      Temperature:
        - SW1: Increase temperature
        - SW3: Decrease temperature
        - SW5: Enter temperature
      ----------------------------------------
    *********************************************************************/
    
    /*********************************************************************
     * INCLUDES
     */
    #include "ZComDef.h"
    #include "OSAL.h"
    #include "AF.h"
    #include "ZDApp.h"
    #include "ZDObject.h"
    #include "ZDProfile.h"
    #include "MT_SYS.h"
    
    #include "zcl.h"
    #include "zcl_general.h"
    #include "zcl_ha.h"
    #include "zcl_ezmode.h"
    #include "zcl_ms.h"
    #include "hal_adc.h"
    
    #include "zcl_sampletemperaturesensor.h"
    
    #include "onboard.h"
    
    /* HAL */
    #include "hal_lcd.h"
    #include "hal_led.h"
    #include "hal_key.h"
    #include "zcl_sampletemperaturesensor_data.c"
    
    /*********************************************************************
     * MACROS
     */
    
    // how often to report temperature
    #define SAMPLETEMPERATURESENSOR_REPORT_INTERVAL   10000
    
    /*********************************************************************
     * CONSTANTS
     */
    
    /*********************************************************************
     * TYPEDEFS
     */
    /*#ifdef __IAR_SYSTEMS_ICC__
    #define  CODE   __code
    #define  XDATA  __xdata
    */
    
    /*********************************************************************
     * GLOBAL VARIABLES
     */
    byte zclSampleTemperatureSensor_TaskID;
    
    uint8 zclSampleTemperatureSensorSeqNum;
    
    static byte gPermitDuration = 0x00;
    
    /*void initTempSensor(void){
       DISABLE_ALL_INTERRUPTS();            //??????
       InitClock();                         //????????32M
       *((BYTE XDATA*) 0x624B) = 0x01;    //???????
       *((BYTE XDATA*) 0x61BD) = 0x01;    //???????ADC????
    }*/
    
    /*********************************************************************
     * GLOBAL FUNCTIONS
     */
    
    /*********************************************************************
     * LOCAL VARIABLES
     */
    afAddrType_t zclSampleTemperatureSensor_DstAddr;
    
    #ifdef ZCL_EZMODE
    static void zclSampleTemperatureSensor_ProcessZDOMsgs( zdoIncomingMsg_t *pMsg );
    static void zclSampleTemperatureSensor_EZModeCB( zlcEZMode_State_t state, zclEZMode_CBData_t *pData );
    
    static const zclEZMode_RegisterData_t zclSampleTemperatureSensor_RegisterEZModeData =
    {
      &zclSampleTemperatureSensor_TaskID,
      SAMPLETEMPERATURESENSOR_EZMODE_NEXTSTATE_EVT,
      SAMPLETEMPERATURESENSOR_EZMODE_TIMEOUT_EVT,
      &zclSampleTemperatureSensorSeqNum,
      zclSampleTemperatureSensor_EZModeCB
    };
    
    // NOT ZCL_EZMODE, Use EndDeviceBind
    #else
    
    static cId_t bindingOutClusters[] =
    {
      ZCL_CLUSTER_ID_MS_TEMPERATURE_MEASUREMENT
    };
    #define ZCLSAMPLETEMPERATURESENSOR_BINDINGLIST        1
    #endif
    
    devStates_t zclSampleTemperatureSensor_NwkState = DEV_INIT;
    
    uint8 giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;   // display main screen mode first
    
    static uint8 aProcessCmd[] = { 1, 0, 0, 0 }; // used for reset command, { length + cmd0 + cmd1 + data }
    
    // Test Endpoint to allow SYS_APP_MSGs
    static endPointDesc_t sampleTemperatureSensor_TestEp =
    {
      20,                                 // Test endpoint
      &zclSampleTemperatureSensor_TaskID,
      (SimpleDescriptionFormat_t *)NULL,  // No Simple description for this test endpoint
      (afNetworkLatencyReq_t)0            // No Network Latency req
    };
    
    
    /*********************************************************************
     * LOCAL FUNCTIONS
     */
    static void zclSampleTemperatureSensor_HandleKeys( byte shift, byte keys );
    static void zclSampleTemperatureSensor_BasicResetCB( void );
    static void zclSampleTemperatureSensor_IdentifyCB( zclIdentify_t *pCmd );
    static void zclSampleTemperatureSensor_IdentifyQueryRspCB( zclIdentifyQueryRsp_t *pRsp );
    static void zclSampleTemperatureSensor_ProcessIdentifyTimeChange( void );
    //void getTemperature(void);
    // app display functions
    void zclSampleTemperatureSensor_LcdDisplayUpdate(void);
    void zclSampleTemperatureSensor_LcdDisplayMainMode(void);
    void zclSampleTemperatureSensor_LcdDisplayTempMode(void);
    void zclSampleTemperatureSensor_LcdDisplayHelpMode(void);
    int16 getTemperature(void);
    
    static void zclSampleTemperatureSensor_SendTemp(void);
    
    // Functions to process ZCL Foundation incoming Command/Response messages
    static void zclSampleTemperatureSensor_ProcessIncomingMsg( zclIncomingMsg_t *msg );
    #ifdef ZCL_READ
    static uint8 zclSampleTemperatureSensor_ProcessInReadRspCmd( zclIncomingMsg_t *pInMsg );
    #endif
    #ifdef ZCL_WRITE
    static uint8 zclSampleTemperatureSensor_ProcessInWriteRspCmd( zclIncomingMsg_t *pInMsg );
    #endif
    static uint8 zclSampleTemperatureSensor_ProcessInDefaultRspCmd( zclIncomingMsg_t *pInMsg );
    #ifdef ZCL_DISCOVER
    static uint8 zclSampleTemperatureSensor_ProcessInDiscCmdsRspCmd( zclIncomingMsg_t *pInMsg );
    static uint8 zclSampleTemperatureSensor_ProcessInDiscAttrsRspCmd( zclIncomingMsg_t *pInMsg );
    static uint8 zclSampleTemperatureSensor_ProcessInDiscAttrsExtRspCmd( zclIncomingMsg_t *pInMsg );
    #endif // ZCL_DISCOVER
    
    /*********************************************************************
     * STATUS STRINGS
     */
    #ifdef LCD_SUPPORTED
    const char sClearLine[]    = " ";
    const char sDeviceName[]   = "  Temp Sensor";
    const char sSwTempUp[]     = "SW1: Raise Temp";
    const char sSwEZMode[]     = "SW2: EZ-Mode";
    const char sSwTempDown[]   = "SW3: Lower Temp";
    const char sSwHelp[]       = "SW5: Help";
    #endif
    
    /*********************************************************************
     * ZCL General Profile Callback table
     */
    static zclGeneral_AppCallbacks_t zclSampleTemperatureSensor_CmdCallbacks =
    {
      zclSampleTemperatureSensor_BasicResetCB,        // Basic Cluster Reset command
      zclSampleTemperatureSensor_IdentifyCB,          // Identify command
    #ifdef ZCL_EZMODE
      NULL,                                           // Identify EZ-Mode Invoke command
      NULL,                                           // Identify Update Commission State command
    #endif
      NULL,                                           // Identify Trigger Effect command
      zclSampleTemperatureSensor_IdentifyQueryRspCB,  // Identify Query Response command
      NULL,             				                      // On/Off cluster command
      NULL,                                           // On/Off cluster enhanced command Off with Effect
      NULL,                                           // On/Off cluster enhanced command On with Recall Global Scene
      NULL,                                           // On/Off cluster enhanced command On with Timed Off
    #ifdef ZCL_LEVEL_CTRL
      NULL,                                           // Level Control Move to Level command
      NULL,                                           // Level Control Move command
      NULL,                                           // Level Control Step command
      NULL,                                           // Level Control Stop command
    #endif
    #ifdef ZCL_GROUPS
      NULL,                                           // Group Response commands
    #endif
    #ifdef ZCL_SCENES
      NULL,                                           // Scene Store Request command
      NULL,                                           // Scene Recall Request command
      NULL,                                           // Scene Response command
    #endif
    #ifdef ZCL_ALARMS
      NULL,                                           // Alarm (Response) commands
    #endif
    #ifdef SE_UK_EXT
      NULL,                                           // Get Event Log command
      NULL,                                           // Publish Event Log command
    #endif
      NULL,                                           // RSSI Location command
      NULL                                            // RSSI Location Response command
    };
    
    /*********************************************************************
     * @fn          zclSampleTemperatureSensor_Init
     *
     * @brief       Initialization function for the zclGeneral layer.
     *
     * @param       none
     *
     * @return      none
     */
    void zclSampleTemperatureSensor_Init( byte task_id )
    {
      zclSampleTemperatureSensor_TaskID = task_id;
    
      // Set destination address to indirect
      zclSampleTemperatureSensor_DstAddr.addrMode = (afAddrMode_t)AddrNotPresent;
      zclSampleTemperatureSensor_DstAddr.endPoint = 0;
      zclSampleTemperatureSensor_DstAddr.addr.shortAddr = 0;
    
      // This app is part of the Home Automation Profile
      zclHA_Init( &zclSampleTemperatureSensor_SimpleDesc );
    
      // Register the ZCL General Cluster Library callback functions
      zclGeneral_RegisterCmdCallbacks( SAMPLETEMPERATURESENSOR_ENDPOINT, &zclSampleTemperatureSensor_CmdCallbacks );
    
      // Register the application's attribute list
      zcl_registerAttrList( SAMPLETEMPERATURESENSOR_ENDPOINT, SAMPLETEMPERATURESENSOR_MAX_ATTRIBUTES, zclSampleTemperatureSensor_Attrs );
    
      // Register the Application to receive the unprocessed Foundation command/response messages
      zcl_registerForMsg( zclSampleTemperatureSensor_TaskID );
    
    #ifdef ZCL_EZMODE
      // Register EZ-Mode
      zcl_RegisterEZMode( &zclSampleTemperatureSensor_RegisterEZModeData );
    
      // Register with the ZDO to receive Match Descriptor Responses
      ZDO_RegisterForZDOMsg(task_id, Match_Desc_rsp);
    #endif
    
      // Register for all key events - This app will handle all key events
      RegisterForKeys( zclSampleTemperatureSensor_TaskID );
    
      // Register for a test endpoint
      afRegister( &sampleTemperatureSensor_TestEp );
    
    #ifdef LCD_SUPPORTED
      // display the device name
      HalLcdWriteString( (char *)sDeviceName, HAL_LCD_LINE_3 );
    #endif
    }
    
    /*********************************************************************
     * @fn          zclSample_event_loop
     *
     * @brief       Event Loop Processor for zclGeneral.
     *
     * @param       none
     *
     * @return      none
     */
    uint16 zclSampleTemperatureSensor_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( zclSampleTemperatureSensor_TaskID )) )
        {
          switch ( MSGpkt->hdr.event )
          {
    #ifdef ZCL_EZMODE
            case ZDO_CB_MSG:
              zclSampleTemperatureSensor_ProcessZDOMsgs( (zdoIncomingMsg_t *)MSGpkt );
              break;
    #endif
    
            case ZCL_INCOMING_MSG:
              // Incoming ZCL Foundation command/response messages
              zclSampleTemperatureSensor_ProcessIncomingMsg( (zclIncomingMsg_t *)MSGpkt );
              break;
    
            case KEY_CHANGE:
              zclSampleTemperatureSensor_HandleKeys( ((keyChange_t *)MSGpkt)->state, ((keyChange_t *)MSGpkt)->keys );
              break;
    
            case ZDO_STATE_CHANGE:
              zclSampleTemperatureSensor_NwkState = (devStates_t)(MSGpkt->hdr.status);
    
    
              // now on the network
              if ( (zclSampleTemperatureSensor_NwkState == DEV_ZB_COORD) ||
                   (zclSampleTemperatureSensor_NwkState == DEV_ROUTER)   ||
                   (zclSampleTemperatureSensor_NwkState == DEV_END_DEVICE) )
              {
    #ifndef HOLD_AUTO_START
                giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;
                zclSampleTemperatureSensor_LcdDisplayUpdate();
    #endif
    #ifdef ZCL_EZMODE
                zcl_EZModeAction( EZMODE_ACTION_NETWORK_STARTED, NULL );
    #endif // ZCL_EZMODE
              }
              break;
    
            default:
              break;
          }
    
          // Release the memory
          osal_msg_deallocate( (uint8 *)MSGpkt );
        }
    
        // return unprocessed events
        return (events ^ SYS_EVENT_MSG);
      }
    
      if ( events & SAMPLETEMPERATURESENSOR_IDENTIFY_TIMEOUT_EVT )
      {
        if ( zclSampleTemperatureSensor_IdentifyTime > 0 )
          zclSampleTemperatureSensor_IdentifyTime--;
        zclSampleTemperatureSensor_ProcessIdentifyTimeChange();
    
        return ( events ^ SAMPLETEMPERATURESENSOR_IDENTIFY_TIMEOUT_EVT );
      }
    
    #ifdef ZCL_EZMODE
      // going on to next state
      if ( events & SAMPLETEMPERATURESENSOR_EZMODE_NEXTSTATE_EVT )
      {
        zcl_EZModeAction ( EZMODE_ACTION_PROCESS, NULL );   // going on to next state
        return ( events ^ SAMPLETEMPERATURESENSOR_EZMODE_NEXTSTATE_EVT );
      }
    
      // the overall EZMode timer expired, so we timed out
      if ( events & SAMPLETEMPERATURESENSOR_EZMODE_TIMEOUT_EVT )
      {
        zcl_EZModeAction ( EZMODE_ACTION_TIMED_OUT, NULL ); // EZ-Mode timed out
        return ( events ^ SAMPLETEMPERATURESENSOR_EZMODE_TIMEOUT_EVT );
      }
    #endif // ZLC_EZMODE
    
      if ( events & SAMPLETEMPERATURESENSOR_MAIN_SCREEN_EVT )
      {
        giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;
        zclSampleTemperatureSensor_LcdDisplayUpdate();
    
        return ( events ^ SAMPLETEMPERATURESENSOR_MAIN_SCREEN_EVT );
      }
    
      if ( events & SAMPLETEMPERATURESENSOR_TEMP_SEND_EVT )
      {
        zclSampleTemperatureSensor_SendTemp();
    
        // report current temperature reading every 10 seconds
        osal_start_timerEx( zclSampleTemperatureSensor_TaskID, SAMPLETEMPERATURESENSOR_TEMP_SEND_EVT, SAMPLETEMPERATURESENSOR_REPORT_INTERVAL );
    
        return ( events ^ SAMPLETEMPERATURESENSOR_TEMP_SEND_EVT );
      }
    
      // Discard unknown events
      return 0;
    }
    
    int16 getTemperature(void)
    {
      int16 AvgTemp;
      uint8   i;
      uint16  AdcValue;
      uint16  value;
      uint16  value1;
    //char i;
            char TempValue[10]; 
            //initTempSensor(); 
      AdcValue = 0;
      for( i = 0; i < 4; i++ )
      {
    ADCIF = 0;
        ADCCON3 = (HAL_ADC_REF_125V | HAL_ADC_RESOLUTION_14 | HAL_ADC_CHN_TEMP);    // ??1.25V????,12????,AD??:?????
            while ( !ADCIF );                                              //?????ADC
                     
        value =  ADCL >> 2;                        
        value |= (((UINT16)ADCH) << 6);    
        AdcValue += value;                          
      }
      value = AdcValue >> 2;                        //????4,?????
      value1 =   (((value) >> 4) - 335);
                 //??AD?,????????
      while(1)
            {
              AvgTemp = 0;
              for(i = 0 ; i < 64 ; i++)
              {
                AvgTemp += value1;              
                AvgTemp >>= 1;                              //??????2.
              }
              
            }
      //zclSampleTemperatureSensor_MeasuredValue = AvgTemp ; 
      return AvgTemp;
    }
    
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_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_3
     *                 HAL_KEY_SW_2
     *                 HAL_KEY_SW_1
     *
     * @return  none
     */
    static void zclSampleTemperatureSensor_HandleKeys( byte shift, byte keys )
    {
      if ( keys & HAL_KEY_SW_1 )
      {
        // increase temperature
        giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;
    
        if ( zclSampleTemperatureSensor_MeasuredValue < zclSampleTemperatureSensor_MaxMeasuredValue )
        {
          zclSampleTemperatureSensor_MeasuredValue = zclSampleTemperatureSensor_MeasuredValue + 100;  // considering using whole number value
        }
        else if ( zclSampleTemperatureSensor_MeasuredValue >= zclSampleTemperatureSensor_MaxMeasuredValue )
        {
          zclSampleTemperatureSensor_MeasuredValue = zclSampleTemperatureSensor_MaxMeasuredValue;
        }
    
        // Send temperature information
        zclSampleTemperatureSensor_SendTemp();
      }
    
      if ( keys & HAL_KEY_SW_2 )
      {
        if ( ( giTemperatureSensorScreenMode == TEMPSENSE_MAINMODE ) ||
            ( giTemperatureSensorScreenMode == TEMPSENSE_HELPMODE ) )
        {
          giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;
    
    #ifdef ZCL_EZMODE
          zclEZMode_InvokeData_t ezModeData;
          static uint16 clusterIDs[] = { ZCL_CLUSTER_ID_MS_TEMPERATURE_MEASUREMENT };   // only bind on the Temperature Measurement cluster
    
          // Invoke EZ-Mode
          ezModeData.endpoint = SAMPLETEMPERATURESENSOR_ENDPOINT; // endpoint on which to invoke EZ-Mode
          if ( ( zclSampleTemperatureSensor_NwkState == DEV_ZB_COORD ) ||
               ( zclSampleTemperatureSensor_NwkState == DEV_ROUTER )   ||
               ( zclSampleTemperatureSensor_NwkState == DEV_END_DEVICE ) )
          {
            ezModeData.onNetwork = TRUE;      // node is already on the network
          }
          else
          {
            ezModeData.onNetwork = FALSE;     // node is not yet on the network
          }
          ezModeData.initiator = TRUE;        // Temperature Sensor is an initiator
          ezModeData.numActiveInClusters = 1;
          ezModeData.pActiveInClusterIDs = clusterIDs;
          ezModeData.numActiveOutClusters = 0;   // active output cluster
          ezModeData.pActiveOutClusterIDs = NULL;
          zcl_InvokeEZMode( &ezModeData );
    
    #ifdef LCD_SUPPORTED
          HalLcdWriteString( "EZMode", HAL_LCD_LINE_2 );
    #endif
    
          // NOT ZCL_EZMODE, Use EndDeviceBind
    #else
          {
            zAddrType_t dstAddr;
            dstAddr.addrMode = Addr16Bit;
            dstAddr.addr.shortAddr = 0;   // Coordinator makes the EDB match
    
            // Initiate an End Device Bind Request, this bind request will
            // only use a cluster list that is important to binding.
            HalLedSet ( HAL_LED_4, HAL_LED_MODE_OFF );
            ZDP_EndDeviceBindReq( &dstAddr, NLME_GetShortAddr(),
                                  SAMPLETEMPERATURESENSOR_ENDPOINT,
                                  ZCL_HA_PROFILE_ID,
                                  0, NULL,
                                  ZCLSAMPLETEMPERATURESENSOR_BINDINGLIST, bindingOutClusters,
                                  FALSE );
          }
    #endif // ZCL_EZMODE
        }
      }
    
      if ( keys & HAL_KEY_SW_3 )
      {
        giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;
    
        // decrease the temperature
        if ( zclSampleTemperatureSensor_MeasuredValue > zclSampleTemperatureSensor_MinMeasuredValue )
        {
          zclSampleTemperatureSensor_MeasuredValue = zclSampleTemperatureSensor_MeasuredValue - 100;  // considering using whole number value
        }
        else if ( zclSampleTemperatureSensor_MeasuredValue >= zclSampleTemperatureSensor_MinMeasuredValue )
        {
          zclSampleTemperatureSensor_MeasuredValue = zclSampleTemperatureSensor_MinMeasuredValue;
        }
    
        // Send temperature information
        zclSampleTemperatureSensor_SendTemp();
      }
    
      if ( keys & HAL_KEY_SW_4 )
      {
        giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;
    
        if ( ( zclSampleTemperatureSensor_NwkState == DEV_ZB_COORD ) ||
             ( zclSampleTemperatureSensor_NwkState == DEV_ROUTER ) )
        {
          // toggle permit join
          gPermitDuration = gPermitDuration ? 0 : 0xff;
          NLME_PermitJoiningRequest( gPermitDuration );
        }
      }
    
      if ( shift && ( keys & HAL_KEY_SW_5 ) )
      {
        zclSampleTemperatureSensor_BasicResetCB();
      }
      else if ( keys & HAL_KEY_SW_5 )
      {
        if ( giTemperatureSensorScreenMode == TEMPSENSE_MAINMODE )
        {
          giTemperatureSensorScreenMode = TEMPSENSE_HELPMODE;
        }
        else if ( giTemperatureSensorScreenMode == TEMPSENSE_HELPMODE )
        {
    #ifdef LCD_SUPPORTED
          HalLcdWriteString( (char *)sClearLine, HAL_LCD_LINE_2 );
    #endif
          giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;
        }
      }
    
      // update display
      zclSampleTemperatureSensor_LcdDisplayUpdate();
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_LcdDisplayUpdate
     *
     * @brief   Called to update the LCD display.
     *
     * @param   none
     *
     * @return  none
     */
    void zclSampleTemperatureSensor_LcdDisplayUpdate( void )
    {
      // turn on red LED for temperatures >= 24.00C
      if ( zclSampleTemperatureSensor_MeasuredValue >= 2400 )
      {
        HalLedSet ( HAL_LED_1, HAL_LED_MODE_OFF );
        HalLedSet ( HAL_LED_2, HAL_LED_MODE_ON );
      }
      // turn on green LED for temperatures <= 20.00C
      else if ( zclSampleTemperatureSensor_MeasuredValue <= 2000 )
      {
        HalLedSet ( HAL_LED_1, HAL_LED_MODE_ON );
        HalLedSet ( HAL_LED_2, HAL_LED_MODE_OFF );
      }
      // turn on both red and green LEDs for temperatures between 20.00C and 24.00C
      else
      {
        HalLedSet ( HAL_LED_1, HAL_LED_MODE_ON );
        HalLedSet ( HAL_LED_2, HAL_LED_MODE_ON );
      }
    
      if ( giTemperatureSensorScreenMode == TEMPSENSE_HELPMODE )
      {
        zclSampleTemperatureSensor_LcdDisplayHelpMode();
      }
      else
      {
        zclSampleTemperatureSensor_LcdDisplayMainMode();
      }
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_LcdDisplayMainMode
     *
     * @brief   Called to display the main screen on the LCD.
     *
     * @param   none
     *
     * @return  none
     */
    void zclSampleTemperatureSensor_LcdDisplayMainMode( void )
    {
      char sDisplayTemp[16];
    
      if ( zclSampleTemperatureSensor_NwkState == DEV_ZB_COORD )
      {
        zclHA_LcdStatusLine1( 0 );
      }
      else if ( zclSampleTemperatureSensor_NwkState == DEV_ROUTER )
      {
        zclHA_LcdStatusLine1( 1 );
      }
      else if ( zclSampleTemperatureSensor_NwkState == DEV_END_DEVICE )
      {
        zclHA_LcdStatusLine1( 2 );
      }
    
      // display current temperature
      osal_memcpy(sDisplayTemp, "TEMP: ", 6);
      _ltoa( ( zclSampleTemperatureSensor_MeasuredValue / 100 ), (void *)(&sDisplayTemp[6]), 10 );   // convert temperature to whole number
      osal_memcpy( &sDisplayTemp[8], "C", 2 );
    #ifdef LCD_SUPPORTED
      HalLcdWriteString( (char *)sDisplayTemp, HAL_LCD_LINE_2 );
    #endif
    
    #ifdef LCD_SUPPORTED
      if ( ( zclSampleTemperatureSensor_NwkState == DEV_ZB_COORD ) ||
           ( zclSampleTemperatureSensor_NwkState == DEV_ROUTER ) )
      {
        // display help key with permit join status
        if ( gPermitDuration )
        {
          HalLcdWriteString( "SW5: Help      *", HAL_LCD_LINE_3 );
        }
        else
        {
          HalLcdWriteString( "SW5: Help       ", HAL_LCD_LINE_3 );
        }
      }
      else
      {
        // display help key
        HalLcdWriteString( (char *)sSwHelp, HAL_LCD_LINE_3 );
      }
    #endif
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_LcdDisplayHelpMode
     *
     * @brief   Called to display the SW options on the LCD.
     *
     * @param   none
     *
     * @return  none
     */
    void zclSampleTemperatureSensor_LcdDisplayHelpMode( void )
    {
    #ifdef LCD_SUPPORTED
      HalLcdWriteString( (char *)sSwTempUp, HAL_LCD_LINE_1 );
      HalLcdWriteString( (char *)sSwEZMode, HAL_LCD_LINE_2 );
      HalLcdWriteString( (char *)sSwTempDown, HAL_LCD_LINE_3 );
    #endif
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_SendTemp
     *
     * @brief   Called to send current temperature information to the thermostat
     *
     * @param   none
     *
     * @return  none
     */
    static void zclSampleTemperatureSensor_SendTemp( void )
    {
    #ifdef ZCL_REPORT
      zclReportCmd_t *pReportCmd;
      zclSampleTemperatureSensor_MeasuredValue = getTemperature();
      pReportCmd = osal_mem_alloc( sizeof(zclReportCmd_t) + sizeof(zclReport_t) );
      if ( pReportCmd != NULL )
      {
        pReportCmd->numAttr = 1;
        pReportCmd->attrList[0].attrID = ATTRID_MS_TEMPERATURE_MEASURED_VALUE;
        pReportCmd->attrList[0].dataType = ZCL_DATATYPE_INT16;
        pReportCmd->attrList[0].attrData = (void *)(&zclSampleTemperatureSensor_MeasuredValue);
    
        zcl_SendReportCmd( SAMPLETEMPERATURESENSOR_ENDPOINT, &zclSampleTemperatureSensor_DstAddr,
                           ZCL_CLUSTER_ID_MS_TEMPERATURE_MEASUREMENT,
                           pReportCmd, ZCL_FRAME_SERVER_CLIENT_DIR, TRUE, zclSampleTemperatureSensorSeqNum++ );
      }
    
      osal_mem_free( pReportCmd );
    #endif  // ZCL_REPORT
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_ProcessIdentifyTimeChange
     *
     * @brief   Called to process any change to the IdentifyTime attribute.
     *
     * @param   none
     *
     * @return  none
     */
    static void zclSampleTemperatureSensor_ProcessIdentifyTimeChange( void )
    {
      if ( zclSampleTemperatureSensor_IdentifyTime > 0 )
      {
        osal_start_timerEx( zclSampleTemperatureSensor_TaskID, SAMPLETEMPERATURESENSOR_IDENTIFY_TIMEOUT_EVT, 1000 );
        HalLedBlink ( HAL_LED_4, 0xFF, HAL_LED_DEFAULT_DUTY_CYCLE, HAL_LED_DEFAULT_FLASH_TIME );
      }
      else
      {
        if ( zclSampleTemperatureSensor_OnOff )
        {
          HalLedSet ( HAL_LED_4, HAL_LED_MODE_ON );
        }
        else
        {
          HalLedSet ( HAL_LED_4, HAL_LED_MODE_OFF );
        }
    
        osal_stop_timerEx( zclSampleTemperatureSensor_TaskID, SAMPLETEMPERATURESENSOR_IDENTIFY_TIMEOUT_EVT );
      }
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_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 zclSampleTemperatureSensor_BasicResetCB( void )
    {
      // Put device back to factory default settings
      zgWriteStartupOptions( ZG_STARTUP_SET, 3 );   // bit set both default configuration and default network
    
      // restart device
      MT_SysCommandProcessing( aProcessCmd );
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_IdentifyCB
     *
     * @brief   Callback from the ZCL General Cluster Library when
     *          it received an Identity Command for this application.
     *
     * @param   srcAddr - source address and endpoint of the response message
     * @param   identifyTime - the number of seconds to identify yourself
     *
     * @return  none
     */
    static void zclSampleTemperatureSensor_IdentifyCB( zclIdentify_t *pCmd )
    {
      zclSampleTemperatureSensor_IdentifyTime = pCmd->identifyTime;
      zclSampleTemperatureSensor_ProcessIdentifyTimeChange();
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_IdentifyQueryRspCB
     *
     * @brief   Callback from the ZCL General Cluster Library when
     *          it received an Identity Query Response Command for this application.
     *
     * @param   srcAddr - requestor's address
     * @param   timeout - number of seconds to identify yourself (valid for query response)
     *
     * @return  none
     */
    static void zclSampleTemperatureSensor_IdentifyQueryRspCB( zclIdentifyQueryRsp_t *pRsp )
    {
      (void)pRsp;
    #ifdef ZCL_EZMODE
      {
        zclEZMode_ActionData_t data;
        data.pIdentifyQueryRsp = pRsp;
        zcl_EZModeAction ( EZMODE_ACTION_IDENTIFY_QUERY_RSP, &data );
      }
    #endif
    }
    
    /******************************************************************************
     *
     *  Functions for processing ZCL Foundation incoming Command/Response messages
     *
     *****************************************************************************/
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_ProcessIncomingMsg
     *
     * @brief   Process ZCL Foundation incoming message
     *
     * @param   pInMsg - pointer to the received message
     *
     * @return  none
     */
    static void zclSampleTemperatureSensor_ProcessIncomingMsg( zclIncomingMsg_t *pInMsg)
    {
      switch ( pInMsg->zclHdr.commandID )
      {
    #ifdef ZCL_READ
        case ZCL_CMD_READ_RSP:
          zclSampleTemperatureSensor_ProcessInReadRspCmd( pInMsg );
          break;
    #endif
    #ifdef ZCL_WRITE
        case ZCL_CMD_WRITE_RSP:
          zclSampleTemperatureSensor_ProcessInWriteRspCmd( pInMsg );
          break;
    #endif
    #ifdef ZCL_REPORT
        // See ZCL Test Applicaiton (zcl_testapp.c) for sample code on Attribute Reporting
        case ZCL_CMD_CONFIG_REPORT:
          //zclSampleTemperatureSensor_ProcessInConfigReportCmd( pInMsg );
          break;
    
        case ZCL_CMD_CONFIG_REPORT_RSP:
          //zclSampleTemperatureSensor_ProcessInConfigReportRspCmd( pInMsg );
          break;
    
        case ZCL_CMD_READ_REPORT_CFG:
          //zclSampleTemperatureSensor_ProcessInReadReportCfgCmd( pInMsg );
          break;
    
        case ZCL_CMD_READ_REPORT_CFG_RSP:
          //zclSampleTemperatureSensor_ProcessInReadReportCfgRspCmd( pInMsg );
          break;
    
        case ZCL_CMD_REPORT:
          //zclSampleTemperatureSensor_ProcessInReportCmd( pInMsg );
          break;
    #endif
        case ZCL_CMD_DEFAULT_RSP:
          zclSampleTemperatureSensor_ProcessInDefaultRspCmd( pInMsg );
          break;
    #ifdef ZCL_DISCOVER
        case ZCL_CMD_DISCOVER_CMDS_RECEIVED_RSP:
          zclSampleTemperatureSensor_ProcessInDiscCmdsRspCmd( pInMsg );
          break;
    
        case ZCL_CMD_DISCOVER_CMDS_GEN_RSP:
          zclSampleTemperatureSensor_ProcessInDiscCmdsRspCmd( pInMsg );
          break;
    
        case ZCL_CMD_DISCOVER_ATTRS_RSP:
          zclSampleTemperatureSensor_ProcessInDiscAttrsRspCmd( pInMsg );
          break;
    
        case ZCL_CMD_DISCOVER_ATTRS_EXT_RSP:
          zclSampleTemperatureSensor_ProcessInDiscAttrsExtRspCmd( pInMsg );
          break;
    #endif
        default:
          break;
      }
    
      if ( pInMsg->attrCmd )
      {
        osal_mem_free( pInMsg->attrCmd );
      }
    }
    
    #ifdef ZCL_READ
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_ProcessInReadRspCmd
     *
     * @brief   Process the "Profile" Read Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleTemperatureSensor_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      zclSampleTemperatureSensor_ProcessInWriteRspCmd
     *
     * @brief   Process the "Profile" Write Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleTemperatureSensor_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      zclSampleTemperatureSensor_ProcessInDefaultRspCmd
     *
     * @brief   Process the "Profile" Default Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleTemperatureSensor_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      zclSampleTemperatureSensor_ProcessInDiscCmdsRspCmd
     *
     * @brief   Process the Discover Commands Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleTemperatureSensor_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      zclSampleTemperatureSensor_ProcessInDiscAttrsRspCmd
     *
     * @brief   Process the "Profile" Discover Attributes Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleTemperatureSensor_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      zclSampleTemperatureSensor_ProcessInDiscAttrsExtRspCmd
     *
     * @brief   Process the "Profile" Discover Attributes Extended Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleTemperatureSensor_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
    
    #ifdef ZCL_EZMODE
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_ProcessZDOMsgs
     *
     * @brief   Called when this node receives a ZDO/ZDP response.
     *
     * @param   none
     *
     * @return  status
     */
    static void zclSampleTemperatureSensor_ProcessZDOMsgs( zdoIncomingMsg_t *pMsg )
    {
      zclEZMode_ActionData_t data;
      ZDO_MatchDescRsp_t *pMatchDescRsp;
    
      // Let EZ-Mode know of the Match Descriptor Response
      if ( pMsg->clusterID == Match_Desc_rsp )
      {
        pMatchDescRsp = ZDO_ParseEPListRsp( pMsg );
        data.pMatchDescRsp = pMatchDescRsp;
        zcl_EZModeAction( EZMODE_ACTION_MATCH_DESC_RSP, &data );
        osal_mem_free( pMatchDescRsp );
      }
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_EZModeCB
     *
     * @brief   The Application is informed of events. This can be used to show on the UI what is
    *           going on during EZ-Mode steering/finding/binding.
     *
     * @param   state - an
     *
     * @return  none
     */
    static void zclSampleTemperatureSensor_EZModeCB( zlcEZMode_State_t state, zclEZMode_CBData_t *pData )
    {
    #ifdef LCD_SUPPORTED
      char szLine[20];
      char *pStr;
      uint8 err;
    #endif
    
      // time to go into identify mode
      if ( state == EZMODE_STATE_IDENTIFYING )
      {
        zclSampleTemperatureSensor_IdentifyTime = ( EZMODE_TIME / 1000 );  // convert to seconds
        zclSampleTemperatureSensor_ProcessIdentifyTimeChange();
      }
    
      // autoclosing, show what happened (success, cancelled, etc...)
      if( state == EZMODE_STATE_AUTOCLOSE )
      {
    #ifdef LCD_SUPPORTED
        pStr = NULL;
        err = pData->sAutoClose.err;
        if ( err == EZMODE_ERR_SUCCESS )
        {
          pStr = "EZMode: Success";
        }
        else if ( err == EZMODE_ERR_NOMATCH )
        {
          pStr = "EZMode: NoMatch"; // not a match made in heaven
        }
        if ( pStr )
        {
          if ( giTemperatureSensorScreenMode == TEMPSENSE_MAINMODE )
          {
            HalLcdWriteString ( pStr, HAL_LCD_LINE_2 );
          }
        }
    #endif
      }
    
      // finished, either show DstAddr/EP, or nothing (depending on success or not)
      if( state == EZMODE_STATE_FINISH )
      {
        // turn off identify mode
        zclSampleTemperatureSensor_IdentifyTime = 0;
        zclSampleTemperatureSensor_ProcessIdentifyTimeChange();
    
    #ifdef LCD_SUPPORTED
        // if successful, inform user which nwkaddr/ep we bound to
        pStr = NULL;
        err = pData->sFinish.err;
        if( err == EZMODE_ERR_SUCCESS )
        {
          // "EZDst:1234 EP:34"
          osal_memcpy( szLine, "EZDst:", 6 );
          zclHA_uint16toa( pData->sFinish.nwkaddr, &szLine[6] );
          osal_memcpy( &szLine[10], " EP:", 4 );
          _ltoa( pData->sFinish.ep, (void *)(&szLine[14]), 16 );  // _ltoa NULL terminates
          pStr = szLine;
        }
        else if ( err == EZMODE_ERR_BAD_PARAMETER )
        {
          pStr = "EZMode: BadParm";
        }
        else if ( err == EZMODE_ERR_CANCELLED )
        {
          pStr = "EZMode: Cancel";
        }
        else
        {
          pStr = "EZMode: TimeOut";
        }
        if ( pStr )
        {
          if ( giTemperatureSensorScreenMode == TEMPSENSE_MAINMODE )
          {
            HalLcdWriteString ( pStr, HAL_LCD_LINE_2 );
          }
        }
    #endif  // LCD_SUPPORTED
    
        // show main UI screen 3 seconds after joining network
        osal_start_timerEx( zclSampleTemperatureSensor_TaskID, SAMPLETEMPERATURESENSOR_MAIN_SCREEN_EVT, 3000 );
    
        // report current temperature reading 15 seconds after joinging the network
        osal_start_timerEx( zclSampleTemperatureSensor_TaskID, SAMPLETEMPERATURESENSOR_TEMP_SEND_EVT, SAMPLETEMPERATURESENSOR_REPORT_INTERVAL );
      }
    }
    #endif // ZCL_EZMODE
    
    /****************************************************************************
    ****************************************************************************/
    
    
    

  • any replies pl !!! 

  • i cleared all the errors and the code is runing without any errors but after dumping in the board it shows only as "0" not even any value !!! where should i be wrong 

    5554.zcl_sampletemperaturesensor.c
    /**************************************************************************************************
      Filename:       zcl_sampletemperaturesensor.c
      Revised:        $Date: 2013-10-18 11:49:27 -0700 (Fri, 18 Oct 2013) $
      Revision:       $Revision: 35718 $
    
      Description:    Zigbee Cluster Library - sample device application.
    
    
      Copyright 2013 Texas Instruments Incorporated. All rights reserved.
    
      IMPORTANT: Your use of this Software is limited to those specific rights
      granted under the terms of a software license agreement between the user
      who downloaded the software, his/her employer (which must be your employer)
      and Texas Instruments Incorporated (the "License").  You may not use this
      Software unless you agree to abide by the terms of the License. The License
      limits your use, and you acknowledge, that the Software may not be modified,
      copied or distributed unless embedded on a Texas Instruments microcontroller
      or used solely and exclusively in conjunction with a Texas Instruments radio
      frequency transceiver, which is integrated into your product.  Other than for
      the foregoing purpose, you may not use, reproduce, copy, prepare derivative
      works of, modify, distribute, perform, display or sell this Software and/or
      its documentation for any purpose.
    
      YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE
      PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,
      INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE,
      NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL
      TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT,
      NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER
      LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
      INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE
      OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT
      OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
      (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
    
      Should you have any questions regarding your right to use this Software,
      contact Texas Instruments Incorporated at www.TI.com.
    **************************************************************************************************/
    
    /*********************************************************************
      This device will act as a temperature sensor. It updates the current
      temperature on the thermostat when the user sends the desired
      temperature using SW1.
    
      SCREEN MODES
      ----------------------------------------
      Main:
        - SW1: Send current temperature
        - SW2: Invoke EZMode
        - SW3: Adjust temperature
        - SW5: Go to Help screen
    
      Temperature:
        - SW1: Increase temperature
        - SW3: Decrease temperature
        - SW5: Enter temperature
      ----------------------------------------
    *********************************************************************/
    
    /*********************************************************************
     * INCLUDES
     */
    #include "ZComDef.h"
    #include "OSAL.h"
    #include "AF.h"
    #include "ZDApp.h"
    #include "ZDObject.h"
    #include "ZDProfile.h"
    #include "MT_SYS.h"
    
    #include "zcl.h"
    #include "zcl_general.h"
    #include "zcl_ha.h"
    #include "zcl_ezmode.h"
    #include "zcl_ms.h"
    #include "hal_adc.h"
    
    #include "zcl_sampletemperaturesensor.h"
    
    #include "onboard.h"
    
    /* HAL */
    #include "hal_lcd.h"
    #include "hal_led.h"
    #include "hal_key.h"
    //#include "zcl_sampletemperaturesensor_data.c"
    
    /*********************************************************************
     * MACROS
     */
    
    // how often to report temperature
    #define SAMPLETEMPERATURESENSOR_REPORT_INTERVAL   10000
    
    /*********************************************************************
     * CONSTANTS
     */
    
    /*********************************************************************
     * TYPEDEFS
     */
    /*#ifdef __IAR_SYSTEMS_ICC__
    #define  CODE   __code
    #define  XDATA  __xdata
    */
    
    /*********************************************************************
     * GLOBAL VARIABLES
     */
    byte zclSampleTemperatureSensor_TaskID;
    
    uint8 zclSampleTemperatureSensorSeqNum;
    
    static byte gPermitDuration = 0x00;
    int16 zclSampleTemperatureSensor_MeasuredValue;
    /*void initTempSensor(void){
       DISABLE_ALL_INTERRUPTS();            //??????
       InitClock();                         //????????32M
       *((BYTE XDATA*) 0x624B) = 0x01;    //???????
       *((BYTE XDATA*) 0x61BD) = 0x01;    //???????ADC????
    }*/
    
    /*********************************************************************
     * GLOBAL FUNCTIONS
     */
    
    /*********************************************************************
     * LOCAL VARIABLES
     */
    afAddrType_t zclSampleTemperatureSensor_DstAddr;
    
    #ifdef ZCL_EZMODE
    static void zclSampleTemperatureSensor_ProcessZDOMsgs( zdoIncomingMsg_t *pMsg );
    static void zclSampleTemperatureSensor_EZModeCB( zlcEZMode_State_t state, zclEZMode_CBData_t *pData );
    
    static const zclEZMode_RegisterData_t zclSampleTemperatureSensor_RegisterEZModeData =
    {
      &zclSampleTemperatureSensor_TaskID,
      SAMPLETEMPERATURESENSOR_EZMODE_NEXTSTATE_EVT,
      SAMPLETEMPERATURESENSOR_EZMODE_TIMEOUT_EVT,
      &zclSampleTemperatureSensorSeqNum,
      zclSampleTemperatureSensor_EZModeCB
    };
    
    // NOT ZCL_EZMODE, Use EndDeviceBind
    #else
    
    static cId_t bindingOutClusters[] =
    {
      ZCL_CLUSTER_ID_MS_TEMPERATURE_MEASUREMENT
    };
    #define ZCLSAMPLETEMPERATURESENSOR_BINDINGLIST        1
    #endif
    
    devStates_t zclSampleTemperatureSensor_NwkState = DEV_INIT;
    
    uint8 giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;   // display main screen mode first
    
    static uint8 aProcessCmd[] = { 1, 0, 0, 0 }; // used for reset command, { length + cmd0 + cmd1 + data }
    
    // Test Endpoint to allow SYS_APP_MSGs
    static endPointDesc_t sampleTemperatureSensor_TestEp =
    {
      20,                                 // Test endpoint
      &zclSampleTemperatureSensor_TaskID,
      (SimpleDescriptionFormat_t *)NULL,  // No Simple description for this test endpoint
      (afNetworkLatencyReq_t)0            // No Network Latency req
    };
    
    
    /*********************************************************************
     * LOCAL FUNCTIONS
     */
    static void zclSampleTemperatureSensor_HandleKeys( byte shift, byte keys );
    static void zclSampleTemperatureSensor_BasicResetCB( void );
    static void zclSampleTemperatureSensor_IdentifyCB( zclIdentify_t *pCmd );
    static void zclSampleTemperatureSensor_IdentifyQueryRspCB( zclIdentifyQueryRsp_t *pRsp );
    static void zclSampleTemperatureSensor_ProcessIdentifyTimeChange( void );
    //void getTemperature(void);
    // app display functions
    void zclSampleTemperatureSensor_LcdDisplayUpdate(void);
    void zclSampleTemperatureSensor_LcdDisplayMainMode(void);
    void zclSampleTemperatureSensor_LcdDisplayTempMode(void);
    void zclSampleTemperatureSensor_LcdDisplayHelpMode(void);
    int16 getTemperature(void);
    
    static void zclSampleTemperatureSensor_SendTemp(void);
    
    // Functions to process ZCL Foundation incoming Command/Response messages
    static void zclSampleTemperatureSensor_ProcessIncomingMsg( zclIncomingMsg_t *msg );
    #ifdef ZCL_READ
    static uint8 zclSampleTemperatureSensor_ProcessInReadRspCmd( zclIncomingMsg_t *pInMsg );
    #endif
    #ifdef ZCL_WRITE
    static uint8 zclSampleTemperatureSensor_ProcessInWriteRspCmd( zclIncomingMsg_t *pInMsg );
    #endif
    static uint8 zclSampleTemperatureSensor_ProcessInDefaultRspCmd( zclIncomingMsg_t *pInMsg );
    #ifdef ZCL_DISCOVER
    static uint8 zclSampleTemperatureSensor_ProcessInDiscCmdsRspCmd( zclIncomingMsg_t *pInMsg );
    static uint8 zclSampleTemperatureSensor_ProcessInDiscAttrsRspCmd( zclIncomingMsg_t *pInMsg );
    static uint8 zclSampleTemperatureSensor_ProcessInDiscAttrsExtRspCmd( zclIncomingMsg_t *pInMsg );
    #endif // ZCL_DISCOVER
    
    /*********************************************************************
     * STATUS STRINGS
     */
    #ifdef LCD_SUPPORTED
    const char sClearLine[]    = " ";
    const char sDeviceName[]   = "  Temp Sensor";
    const char sSwTempUp[]     = "SW1: Raise Temp";
    const char sSwEZMode[]     = "SW2: EZ-Mode";
    const char sSwTempDown[]   = "SW3: Lower Temp";
    const char sSwHelp[]       = "SW5: Help";
    #endif
    
    /*********************************************************************
     * ZCL General Profile Callback table
     */
    static zclGeneral_AppCallbacks_t zclSampleTemperatureSensor_CmdCallbacks =
    {
      zclSampleTemperatureSensor_BasicResetCB,        // Basic Cluster Reset command
      zclSampleTemperatureSensor_IdentifyCB,          // Identify command
    #ifdef ZCL_EZMODE
      NULL,                                           // Identify EZ-Mode Invoke command
      NULL,                                           // Identify Update Commission State command
    #endif
      NULL,                                           // Identify Trigger Effect command
      zclSampleTemperatureSensor_IdentifyQueryRspCB,  // Identify Query Response command
      NULL,             				                      // On/Off cluster command
      NULL,                                           // On/Off cluster enhanced command Off with Effect
      NULL,                                           // On/Off cluster enhanced command On with Recall Global Scene
      NULL,                                           // On/Off cluster enhanced command On with Timed Off
    #ifdef ZCL_LEVEL_CTRL
      NULL,                                           // Level Control Move to Level command
      NULL,                                           // Level Control Move command
      NULL,                                           // Level Control Step command
      NULL,                                           // Level Control Stop command
    #endif
    #ifdef ZCL_GROUPS
      NULL,                                           // Group Response commands
    #endif
    #ifdef ZCL_SCENES
      NULL,                                           // Scene Store Request command
      NULL,                                           // Scene Recall Request command
      NULL,                                           // Scene Response command
    #endif
    #ifdef ZCL_ALARMS
      NULL,                                           // Alarm (Response) commands
    #endif
    #ifdef SE_UK_EXT
      NULL,                                           // Get Event Log command
      NULL,                                           // Publish Event Log command
    #endif
      NULL,                                           // RSSI Location command
      NULL                                            // RSSI Location Response command
    };
    
    /*********************************************************************
     * @fn          zclSampleTemperatureSensor_Init
     *
     * @brief       Initialization function for the zclGeneral layer.
     *
     * @param       none
     *
     * @return      none
     */
    void zclSampleTemperatureSensor_Init( byte task_id )
    {
      zclSampleTemperatureSensor_TaskID = task_id;
    
      // Set destination address to indirect
      zclSampleTemperatureSensor_DstAddr.addrMode = (afAddrMode_t)AddrNotPresent;
      zclSampleTemperatureSensor_DstAddr.endPoint = 0;
      zclSampleTemperatureSensor_DstAddr.addr.shortAddr = 0;
    
      // This app is part of the Home Automation Profile
      zclHA_Init( &zclSampleTemperatureSensor_SimpleDesc );
    
      // Register the ZCL General Cluster Library callback functions
      zclGeneral_RegisterCmdCallbacks( SAMPLETEMPERATURESENSOR_ENDPOINT, &zclSampleTemperatureSensor_CmdCallbacks );
    
      // Register the application's attribute list
      zcl_registerAttrList( SAMPLETEMPERATURESENSOR_ENDPOINT, SAMPLETEMPERATURESENSOR_MAX_ATTRIBUTES, zclSampleTemperatureSensor_Attrs );
    
      // Register the Application to receive the unprocessed Foundation command/response messages
      zcl_registerForMsg( zclSampleTemperatureSensor_TaskID );
    
    #ifdef ZCL_EZMODE
      // Register EZ-Mode
      zcl_RegisterEZMode( &zclSampleTemperatureSensor_RegisterEZModeData );
    
      // Register with the ZDO to receive Match Descriptor Responses
      ZDO_RegisterForZDOMsg(task_id, Match_Desc_rsp);
    #endif
    
      // Register for all key events - This app will handle all key events
      RegisterForKeys( zclSampleTemperatureSensor_TaskID );
    
      // Register for a test endpoint
      afRegister( &sampleTemperatureSensor_TestEp );
    
    #ifdef LCD_SUPPORTED
      // display the device name
      HalLcdWriteString( (char *)sDeviceName, HAL_LCD_LINE_3 );
    #endif
    }
    
    /*********************************************************************
     * @fn          zclSample_event_loop
     *
     * @brief       Event Loop Processor for zclGeneral.
     *
     * @param       none
     *
     * @return      none
     */
    uint16 zclSampleTemperatureSensor_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( zclSampleTemperatureSensor_TaskID )) )
        {
          switch ( MSGpkt->hdr.event )
          {
    #ifdef ZCL_EZMODE
            case ZDO_CB_MSG:
              zclSampleTemperatureSensor_ProcessZDOMsgs( (zdoIncomingMsg_t *)MSGpkt );
              break;
    #endif
    
            case ZCL_INCOMING_MSG:
              // Incoming ZCL Foundation command/response messages
              zclSampleTemperatureSensor_ProcessIncomingMsg( (zclIncomingMsg_t *)MSGpkt );
              break;
    
            case KEY_CHANGE:
              zclSampleTemperatureSensor_HandleKeys( ((keyChange_t *)MSGpkt)->state, ((keyChange_t *)MSGpkt)->keys );
              break;
    
            case ZDO_STATE_CHANGE:
              zclSampleTemperatureSensor_NwkState = (devStates_t)(MSGpkt->hdr.status);
    
    
              // now on the network
              if ( (zclSampleTemperatureSensor_NwkState == DEV_ZB_COORD) ||
                   (zclSampleTemperatureSensor_NwkState == DEV_ROUTER)   ||
                   (zclSampleTemperatureSensor_NwkState == DEV_END_DEVICE) )
              {
    #ifndef HOLD_AUTO_START
                giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;
                zclSampleTemperatureSensor_LcdDisplayUpdate();
    #endif
    #ifdef ZCL_EZMODE
                zcl_EZModeAction( EZMODE_ACTION_NETWORK_STARTED, NULL );
    #endif // ZCL_EZMODE
              }
              break;
    
            default:
              break;
          }
    
          // Release the memory
          osal_msg_deallocate( (uint8 *)MSGpkt );
        }
    
        // return unprocessed events
        return (events ^ SYS_EVENT_MSG);
      }
    
      if ( events & SAMPLETEMPERATURESENSOR_IDENTIFY_TIMEOUT_EVT )
      {
        if ( zclSampleTemperatureSensor_IdentifyTime > 0 )
          zclSampleTemperatureSensor_IdentifyTime--;
        zclSampleTemperatureSensor_ProcessIdentifyTimeChange();
    
        return ( events ^ SAMPLETEMPERATURESENSOR_IDENTIFY_TIMEOUT_EVT );
      }
    
    #ifdef ZCL_EZMODE
      // going on to next state
      if ( events & SAMPLETEMPERATURESENSOR_EZMODE_NEXTSTATE_EVT )
      {
        zcl_EZModeAction ( EZMODE_ACTION_PROCESS, NULL );   // going on to next state
        return ( events ^ SAMPLETEMPERATURESENSOR_EZMODE_NEXTSTATE_EVT );
      }
    
      // the overall EZMode timer expired, so we timed out
      if ( events & SAMPLETEMPERATURESENSOR_EZMODE_TIMEOUT_EVT )
      {
        zcl_EZModeAction ( EZMODE_ACTION_TIMED_OUT, NULL ); // EZ-Mode timed out
        return ( events ^ SAMPLETEMPERATURESENSOR_EZMODE_TIMEOUT_EVT );
      }
    #endif // ZLC_EZMODE
    
      if ( events & SAMPLETEMPERATURESENSOR_MAIN_SCREEN_EVT )
      {
        giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;
        zclSampleTemperatureSensor_LcdDisplayUpdate();
    
        return ( events ^ SAMPLETEMPERATURESENSOR_MAIN_SCREEN_EVT );
      }
    
      if ( events & SAMPLETEMPERATURESENSOR_TEMP_SEND_EVT )
      {
        zclSampleTemperatureSensor_SendTemp();
    
        // report current temperature reading every 10 seconds
        osal_start_timerEx( zclSampleTemperatureSensor_TaskID, SAMPLETEMPERATURESENSOR_TEMP_SEND_EVT, SAMPLETEMPERATURESENSOR_REPORT_INTERVAL );
    
        return ( events ^ SAMPLETEMPERATURESENSOR_TEMP_SEND_EVT );
      }
    
      // Discard unknown events
      return 0;
    }
    
    int16 getTemperature(void)
    {
      int16 AvgTemp;
      uint8   i;
      uint16  AdcValue;
      uint16  value;
      uint16  value1;
    //char i;
            //char TempValue[10]; 
            //initTempSensor(); 
      AdcValue = 0;
      for( i = 0; i < 4; i++ )
      {
    ADCIF = 0;
        ADCCON3 = (HAL_ADC_REF_125V | HAL_ADC_RESOLUTION_14 | HAL_ADC_CHN_TEMP);    // ??1.25V????,12????,AD??:?????
            while ( !ADCIF );                                              //?????ADC
                     
        value =  ADCL >> 2;                        
        value |= (((UINT16)ADCH) << 6);    
        AdcValue += value;                          
      }
      value = AdcValue >> 2;                        //????4,?????
      value1 =   (((value) >> 4) - 335);
                 //??AD?,????????
      while(1)
            {
              AvgTemp = 0;
              for(i = 0 ; i < 64 ; i++)
              {
                AvgTemp += value1;              
                AvgTemp >>= 1;                              //??????2.
              }
              
            }
      //zclSampleTemperatureSensor_MeasuredValue = AvgTemp ; 
      return AvgTemp;
    }
    
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_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_3
     *                 HAL_KEY_SW_2
     *                 HAL_KEY_SW_1
     *
     * @return  none
     */
    static void zclSampleTemperatureSensor_HandleKeys( byte shift, byte keys )
    {
      if ( keys & HAL_KEY_SW_1 )
      {
        // increase temperature
        giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;
    
        if ( zclSampleTemperatureSensor_MeasuredValue < zclSampleTemperatureSensor_MaxMeasuredValue )
        {
          zclSampleTemperatureSensor_MeasuredValue = zclSampleTemperatureSensor_MeasuredValue + 100;  // considering using whole number value
        }
        else if ( zclSampleTemperatureSensor_MeasuredValue >= zclSampleTemperatureSensor_MaxMeasuredValue )
        {
          zclSampleTemperatureSensor_MeasuredValue = zclSampleTemperatureSensor_MaxMeasuredValue;
        }
    
        // Send temperature information
        zclSampleTemperatureSensor_SendTemp();
      }
    
      if ( keys & HAL_KEY_SW_2 )
      {
        if ( ( giTemperatureSensorScreenMode == TEMPSENSE_MAINMODE ) ||
            ( giTemperatureSensorScreenMode == TEMPSENSE_HELPMODE ) )
        {
          giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;
    
    #ifdef ZCL_EZMODE
          zclEZMode_InvokeData_t ezModeData;
          static uint16 clusterIDs[] = { ZCL_CLUSTER_ID_MS_TEMPERATURE_MEASUREMENT };   // only bind on the Temperature Measurement cluster
    
          // Invoke EZ-Mode
          ezModeData.endpoint = SAMPLETEMPERATURESENSOR_ENDPOINT; // endpoint on which to invoke EZ-Mode
          if ( ( zclSampleTemperatureSensor_NwkState == DEV_ZB_COORD ) ||
               ( zclSampleTemperatureSensor_NwkState == DEV_ROUTER )   ||
               ( zclSampleTemperatureSensor_NwkState == DEV_END_DEVICE ) )
          {
            ezModeData.onNetwork = TRUE;      // node is already on the network
          }
          else
          {
            ezModeData.onNetwork = FALSE;     // node is not yet on the network
          }
          ezModeData.initiator = TRUE;        // Temperature Sensor is an initiator
          ezModeData.numActiveInClusters = 1;
          ezModeData.pActiveInClusterIDs = clusterIDs;
          ezModeData.numActiveOutClusters = 0;   // active output cluster
          ezModeData.pActiveOutClusterIDs = NULL;
          zcl_InvokeEZMode( &ezModeData );
    
    #ifdef LCD_SUPPORTED
          HalLcdWriteString( "EZMode", HAL_LCD_LINE_2 );
    #endif
    
          // NOT ZCL_EZMODE, Use EndDeviceBind
    #else
          {
            zAddrType_t dstAddr;
            dstAddr.addrMode = Addr16Bit;
            dstAddr.addr.shortAddr = 0;   // Coordinator makes the EDB match
    
            // Initiate an End Device Bind Request, this bind request will
            // only use a cluster list that is important to binding.
            HalLedSet ( HAL_LED_4, HAL_LED_MODE_OFF );
            ZDP_EndDeviceBindReq( &dstAddr, NLME_GetShortAddr(),
                                  SAMPLETEMPERATURESENSOR_ENDPOINT,
                                  ZCL_HA_PROFILE_ID,
                                  0, NULL,
                                  ZCLSAMPLETEMPERATURESENSOR_BINDINGLIST, bindingOutClusters,
                                  FALSE );
          }
    #endif // ZCL_EZMODE
        }
      }
    
      if ( keys & HAL_KEY_SW_3 )
      {
        giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;
    
        // decrease the temperature
        if ( zclSampleTemperatureSensor_MeasuredValue > zclSampleTemperatureSensor_MinMeasuredValue )
        {
          zclSampleTemperatureSensor_MeasuredValue = zclSampleTemperatureSensor_MeasuredValue - 100;  // considering using whole number value
        }
        else if ( zclSampleTemperatureSensor_MeasuredValue >= zclSampleTemperatureSensor_MinMeasuredValue )
        {
          zclSampleTemperatureSensor_MeasuredValue = zclSampleTemperatureSensor_MinMeasuredValue;
        }
    
        // Send temperature information
        zclSampleTemperatureSensor_SendTemp();
      }
    
      if ( keys & HAL_KEY_SW_4 )
      {
        giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;
    
        if ( ( zclSampleTemperatureSensor_NwkState == DEV_ZB_COORD ) ||
             ( zclSampleTemperatureSensor_NwkState == DEV_ROUTER ) )
        {
          // toggle permit join
          gPermitDuration = gPermitDuration ? 0 : 0xff;
          NLME_PermitJoiningRequest( gPermitDuration );
        }
      }
    
      if ( shift && ( keys & HAL_KEY_SW_5 ) )
      {
        zclSampleTemperatureSensor_BasicResetCB();
      }
      else if ( keys & HAL_KEY_SW_5 )
      {
        if ( giTemperatureSensorScreenMode == TEMPSENSE_MAINMODE )
        {
          giTemperatureSensorScreenMode = TEMPSENSE_HELPMODE;
        }
        else if ( giTemperatureSensorScreenMode == TEMPSENSE_HELPMODE )
        {
    #ifdef LCD_SUPPORTED
          HalLcdWriteString( (char *)sClearLine, HAL_LCD_LINE_2 );
    #endif
          giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;
        }
      }
    
      // update display
      zclSampleTemperatureSensor_LcdDisplayUpdate();
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_LcdDisplayUpdate
     *
     * @brief   Called to update the LCD display.
     *
     * @param   none
     *
     * @return  none
     */
    void zclSampleTemperatureSensor_LcdDisplayUpdate( void )
    {
      // turn on red LED for temperatures >= 24.00C
      if ( zclSampleTemperatureSensor_MeasuredValue >= 2400 )
      {
        HalLedSet ( HAL_LED_1, HAL_LED_MODE_OFF );
        HalLedSet ( HAL_LED_2, HAL_LED_MODE_ON );
      }
      // turn on green LED for temperatures <= 20.00C
      else if ( zclSampleTemperatureSensor_MeasuredValue <= 2000 )
      {
        HalLedSet ( HAL_LED_1, HAL_LED_MODE_ON );
        HalLedSet ( HAL_LED_2, HAL_LED_MODE_OFF );
      }
      // turn on both red and green LEDs for temperatures between 20.00C and 24.00C
      else
      {
        HalLedSet ( HAL_LED_1, HAL_LED_MODE_ON );
        HalLedSet ( HAL_LED_2, HAL_LED_MODE_ON );
      }
    
      if ( giTemperatureSensorScreenMode == TEMPSENSE_HELPMODE )
      {
        zclSampleTemperatureSensor_LcdDisplayHelpMode();
      }
      else
      {
        zclSampleTemperatureSensor_LcdDisplayMainMode();
      }
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_LcdDisplayMainMode
     *
     * @brief   Called to display the main screen on the LCD.
     *
     * @param   none
     *
     * @return  none
     */
    void zclSampleTemperatureSensor_LcdDisplayMainMode( void )
    {
      char sDisplayTemp[16];
    
      if ( zclSampleTemperatureSensor_NwkState == DEV_ZB_COORD )
      {
        zclHA_LcdStatusLine1( 0 );
      }
      else if ( zclSampleTemperatureSensor_NwkState == DEV_ROUTER )
      {
        zclHA_LcdStatusLine1( 1 );
      }
      else if ( zclSampleTemperatureSensor_NwkState == DEV_END_DEVICE )
      {
        zclHA_LcdStatusLine1( 2 );
      }
    
      // display current temperature
      osal_memcpy(sDisplayTemp, "TEMP: ", 6);
      _ltoa( ( zclSampleTemperatureSensor_MeasuredValue / 100 ), (void *)(&sDisplayTemp[6]), 10 );   // convert temperature to whole number
      osal_memcpy( &sDisplayTemp[8], "C", 2 );
    #ifdef LCD_SUPPORTED
      HalLcdWriteString( (char *)sDisplayTemp, HAL_LCD_LINE_2 );
    #endif
    
    #ifdef LCD_SUPPORTED
      if ( ( zclSampleTemperatureSensor_NwkState == DEV_ZB_COORD ) ||
           ( zclSampleTemperatureSensor_NwkState == DEV_ROUTER ) )
      {
        // display help key with permit join status
        if ( gPermitDuration )
        {
          HalLcdWriteString( "SW5: Help      *", HAL_LCD_LINE_3 );
        }
        else
        {
          HalLcdWriteString( "SW5: Help       ", HAL_LCD_LINE_3 );
        }
      }
      else
      {
        // display help key
        HalLcdWriteString( (char *)sSwHelp, HAL_LCD_LINE_3 );
      }
    #endif
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_LcdDisplayHelpMode
     *
     * @brief   Called to display the SW options on the LCD.
     *
     * @param   none
     *
     * @return  none
     */
    void zclSampleTemperatureSensor_LcdDisplayHelpMode( void )
    {
    #ifdef LCD_SUPPORTED
      HalLcdWriteString( (char *)sSwTempUp, HAL_LCD_LINE_1 );
      HalLcdWriteString( (char *)sSwEZMode, HAL_LCD_LINE_2 );
      HalLcdWriteString( (char *)sSwTempDown, HAL_LCD_LINE_3 );
    #endif
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_SendTemp
     *
     * @brief   Called to send current temperature information to the thermostat
     *
     * @param   none
     *
     * @return  none
     */
    static void zclSampleTemperatureSensor_SendTemp( void )
    {
    #ifdef ZCL_REPORT
      zclReportCmd_t *pReportCmd;
      zclSampleTemperatureSensor_MeasuredValue = getTemperature();
      pReportCmd = osal_mem_alloc( sizeof(zclReportCmd_t) + sizeof(zclReport_t) );
      if ( pReportCmd != NULL )
      {
        pReportCmd->numAttr = 1;
        pReportCmd->attrList[0].attrID = ATTRID_MS_TEMPERATURE_MEASURED_VALUE;
        pReportCmd->attrList[0].dataType = ZCL_DATATYPE_INT16;
        pReportCmd->attrList[0].attrData = (void *)(&zclSampleTemperatureSensor_MeasuredValue);
    
        zcl_SendReportCmd( SAMPLETEMPERATURESENSOR_ENDPOINT, &zclSampleTemperatureSensor_DstAddr,
                           ZCL_CLUSTER_ID_MS_TEMPERATURE_MEASUREMENT,
                           pReportCmd, ZCL_FRAME_SERVER_CLIENT_DIR, TRUE, zclSampleTemperatureSensorSeqNum++ );
      }
    
      osal_mem_free( pReportCmd );
    #endif  // ZCL_REPORT
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_ProcessIdentifyTimeChange
     *
     * @brief   Called to process any change to the IdentifyTime attribute.
     *
     * @param   none
     *
     * @return  none
     */
    static void zclSampleTemperatureSensor_ProcessIdentifyTimeChange( void )
    {
      if ( zclSampleTemperatureSensor_IdentifyTime > 0 )
      {
        osal_start_timerEx( zclSampleTemperatureSensor_TaskID, SAMPLETEMPERATURESENSOR_IDENTIFY_TIMEOUT_EVT, 1000 );
        HalLedBlink ( HAL_LED_4, 0xFF, HAL_LED_DEFAULT_DUTY_CYCLE, HAL_LED_DEFAULT_FLASH_TIME );
      }
      else
      {
        if ( zclSampleTemperatureSensor_OnOff )
        {
          HalLedSet ( HAL_LED_4, HAL_LED_MODE_ON );
        }
        else
        {
          HalLedSet ( HAL_LED_4, HAL_LED_MODE_OFF );
        }
    
        osal_stop_timerEx( zclSampleTemperatureSensor_TaskID, SAMPLETEMPERATURESENSOR_IDENTIFY_TIMEOUT_EVT );
      }
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_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 zclSampleTemperatureSensor_BasicResetCB( void )
    {
      // Put device back to factory default settings
      zgWriteStartupOptions( ZG_STARTUP_SET, 3 );   // bit set both default configuration and default network
    
      // restart device
      MT_SysCommandProcessing( aProcessCmd );
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_IdentifyCB
     *
     * @brief   Callback from the ZCL General Cluster Library when
     *          it received an Identity Command for this application.
     *
     * @param   srcAddr - source address and endpoint of the response message
     * @param   identifyTime - the number of seconds to identify yourself
     *
     * @return  none
     */
    static void zclSampleTemperatureSensor_IdentifyCB( zclIdentify_t *pCmd )
    {
      zclSampleTemperatureSensor_IdentifyTime = pCmd->identifyTime;
      zclSampleTemperatureSensor_ProcessIdentifyTimeChange();
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_IdentifyQueryRspCB
     *
     * @brief   Callback from the ZCL General Cluster Library when
     *          it received an Identity Query Response Command for this application.
     *
     * @param   srcAddr - requestor's address
     * @param   timeout - number of seconds to identify yourself (valid for query response)
     *
     * @return  none
     */
    static void zclSampleTemperatureSensor_IdentifyQueryRspCB( zclIdentifyQueryRsp_t *pRsp )
    {
      (void)pRsp;
    #ifdef ZCL_EZMODE
      {
        zclEZMode_ActionData_t data;
        data.pIdentifyQueryRsp = pRsp;
        zcl_EZModeAction ( EZMODE_ACTION_IDENTIFY_QUERY_RSP, &data );
      }
    #endif
    }
    
    /******************************************************************************
     *
     *  Functions for processing ZCL Foundation incoming Command/Response messages
     *
     *****************************************************************************/
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_ProcessIncomingMsg
     *
     * @brief   Process ZCL Foundation incoming message
     *
     * @param   pInMsg - pointer to the received message
     *
     * @return  none
     */
    static void zclSampleTemperatureSensor_ProcessIncomingMsg( zclIncomingMsg_t *pInMsg)
    {
      switch ( pInMsg->zclHdr.commandID )
      {
    #ifdef ZCL_READ
        case ZCL_CMD_READ_RSP:
          zclSampleTemperatureSensor_ProcessInReadRspCmd( pInMsg );
          break;
    #endif
    #ifdef ZCL_WRITE
        case ZCL_CMD_WRITE_RSP:
          zclSampleTemperatureSensor_ProcessInWriteRspCmd( pInMsg );
          break;
    #endif
    #ifdef ZCL_REPORT
        // See ZCL Test Applicaiton (zcl_testapp.c) for sample code on Attribute Reporting
        case ZCL_CMD_CONFIG_REPORT:
          //zclSampleTemperatureSensor_ProcessInConfigReportCmd( pInMsg );
          break;
    
        case ZCL_CMD_CONFIG_REPORT_RSP:
          //zclSampleTemperatureSensor_ProcessInConfigReportRspCmd( pInMsg );
          break;
    
        case ZCL_CMD_READ_REPORT_CFG:
          //zclSampleTemperatureSensor_ProcessInReadReportCfgCmd( pInMsg );
          break;
    
        case ZCL_CMD_READ_REPORT_CFG_RSP:
          //zclSampleTemperatureSensor_ProcessInReadReportCfgRspCmd( pInMsg );
          break;
    
        case ZCL_CMD_REPORT:
          //zclSampleTemperatureSensor_ProcessInReportCmd( pInMsg );
          break;
    #endif
        case ZCL_CMD_DEFAULT_RSP:
          zclSampleTemperatureSensor_ProcessInDefaultRspCmd( pInMsg );
          break;
    #ifdef ZCL_DISCOVER
        case ZCL_CMD_DISCOVER_CMDS_RECEIVED_RSP:
          zclSampleTemperatureSensor_ProcessInDiscCmdsRspCmd( pInMsg );
          break;
    
        case ZCL_CMD_DISCOVER_CMDS_GEN_RSP:
          zclSampleTemperatureSensor_ProcessInDiscCmdsRspCmd( pInMsg );
          break;
    
        case ZCL_CMD_DISCOVER_ATTRS_RSP:
          zclSampleTemperatureSensor_ProcessInDiscAttrsRspCmd( pInMsg );
          break;
    
        case ZCL_CMD_DISCOVER_ATTRS_EXT_RSP:
          zclSampleTemperatureSensor_ProcessInDiscAttrsExtRspCmd( pInMsg );
          break;
    #endif
        default:
          break;
      }
    
      if ( pInMsg->attrCmd )
      {
        osal_mem_free( pInMsg->attrCmd );
      }
    }
    
    #ifdef ZCL_READ
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_ProcessInReadRspCmd
     *
     * @brief   Process the "Profile" Read Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleTemperatureSensor_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      zclSampleTemperatureSensor_ProcessInWriteRspCmd
     *
     * @brief   Process the "Profile" Write Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleTemperatureSensor_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      zclSampleTemperatureSensor_ProcessInDefaultRspCmd
     *
     * @brief   Process the "Profile" Default Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleTemperatureSensor_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      zclSampleTemperatureSensor_ProcessInDiscCmdsRspCmd
     *
     * @brief   Process the Discover Commands Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleTemperatureSensor_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      zclSampleTemperatureSensor_ProcessInDiscAttrsRspCmd
     *
     * @brief   Process the "Profile" Discover Attributes Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleTemperatureSensor_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      zclSampleTemperatureSensor_ProcessInDiscAttrsExtRspCmd
     *
     * @brief   Process the "Profile" Discover Attributes Extended Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleTemperatureSensor_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
    
    #ifdef ZCL_EZMODE
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_ProcessZDOMsgs
     *
     * @brief   Called when this node receives a ZDO/ZDP response.
     *
     * @param   none
     *
     * @return  status
     */
    static void zclSampleTemperatureSensor_ProcessZDOMsgs( zdoIncomingMsg_t *pMsg )
    {
      zclEZMode_ActionData_t data;
      ZDO_MatchDescRsp_t *pMatchDescRsp;
    
      // Let EZ-Mode know of the Match Descriptor Response
      if ( pMsg->clusterID == Match_Desc_rsp )
      {
        pMatchDescRsp = ZDO_ParseEPListRsp( pMsg );
        data.pMatchDescRsp = pMatchDescRsp;
        zcl_EZModeAction( EZMODE_ACTION_MATCH_DESC_RSP, &data );
        osal_mem_free( pMatchDescRsp );
      }
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_EZModeCB
     *
     * @brief   The Application is informed of events. This can be used to show on the UI what is
    *           going on during EZ-Mode steering/finding/binding.
     *
     * @param   state - an
     *
     * @return  none
     */
    static void zclSampleTemperatureSensor_EZModeCB( zlcEZMode_State_t state, zclEZMode_CBData_t *pData )
    {
    #ifdef LCD_SUPPORTED
      char szLine[20];
      char *pStr;
      uint8 err;
    #endif
    
      // time to go into identify mode
      if ( state == EZMODE_STATE_IDENTIFYING )
      {
        zclSampleTemperatureSensor_IdentifyTime = ( EZMODE_TIME / 1000 );  // convert to seconds
        zclSampleTemperatureSensor_ProcessIdentifyTimeChange();
      }
    
      // autoclosing, show what happened (success, cancelled, etc...)
      if( state == EZMODE_STATE_AUTOCLOSE )
      {
    #ifdef LCD_SUPPORTED
        pStr = NULL;
        err = pData->sAutoClose.err;
        if ( err == EZMODE_ERR_SUCCESS )
        {
          pStr = "EZMode: Success";
        }
        else if ( err == EZMODE_ERR_NOMATCH )
        {
          pStr = "EZMode: NoMatch"; // not a match made in heaven
        }
        if ( pStr )
        {
          if ( giTemperatureSensorScreenMode == TEMPSENSE_MAINMODE )
          {
            HalLcdWriteString ( pStr, HAL_LCD_LINE_2 );
          }
        }
    #endif
      }
    
      // finished, either show DstAddr/EP, or nothing (depending on success or not)
      if( state == EZMODE_STATE_FINISH )
      {
        // turn off identify mode
        zclSampleTemperatureSensor_IdentifyTime = 0;
        zclSampleTemperatureSensor_ProcessIdentifyTimeChange();
    
    #ifdef LCD_SUPPORTED
        // if successful, inform user which nwkaddr/ep we bound to
        pStr = NULL;
        err = pData->sFinish.err;
        if( err == EZMODE_ERR_SUCCESS )
        {
          // "EZDst:1234 EP:34"
          osal_memcpy( szLine, "EZDst:", 6 );
          zclHA_uint16toa( pData->sFinish.nwkaddr, &szLine[6] );
          osal_memcpy( &szLine[10], " EP:", 4 );
          _ltoa( pData->sFinish.ep, (void *)(&szLine[14]), 16 );  // _ltoa NULL terminates
          pStr = szLine;
        }
        else if ( err == EZMODE_ERR_BAD_PARAMETER )
        {
          pStr = "EZMode: BadParm";
        }
        else if ( err == EZMODE_ERR_CANCELLED )
        {
          pStr = "EZMode: Cancel";
        }
        else
        {
          pStr = "EZMode: TimeOut";
        }
        if ( pStr )
        {
          if ( giTemperatureSensorScreenMode == TEMPSENSE_MAINMODE )
          {
            HalLcdWriteString ( pStr, HAL_LCD_LINE_2 );
          }
        }
    #endif  // LCD_SUPPORTED
    
        // show main UI screen 3 seconds after joining network
        osal_start_timerEx( zclSampleTemperatureSensor_TaskID, SAMPLETEMPERATURESENSOR_MAIN_SCREEN_EVT, 3000 );
    
        // report current temperature reading 15 seconds after joinging the network
        osal_start_timerEx( zclSampleTemperatureSensor_TaskID, SAMPLETEMPERATURESENSOR_TEMP_SEND_EVT, SAMPLETEMPERATURESENSOR_REPORT_INTERVAL );
      }
    }
    #endif // ZCL_EZMODE
    
    /****************************************************************************
    ****************************************************************************/
    
    
    

  • You have to remove "int16 zclSampleTemperatureSensor_MeasuredValue;" in zcl_sampletemperaturesensor.c to fix compile error. What do you mean it show only as "0"? I don't understand your question.

  • sir the 0 i said s the output in the lcd display of my node sir ... y isnt that code not giving the temperature values as output 

  • You return ADC reading in your getTemperature() function and it is not temperature. You have to transform it to temperature first.

  • the return value will print the temperature value only sir ..... i tested that code without using stack and all and it s wrking fine in simpleapp also sir ... i added that function as an event is it a rite idea 

  • I suggest you set a breakpoint at zclSampleTemperatureSensor_MeasuredValue = getTemperature(); in zclSampleTemperatureSensor_SendTemp function to check what zclSampleTemperatureSensor_MeasuredValue value is.

  • how can i check that sir ??? 

  • i tried sir but it s the device is not responding ... later i tried to just created an event like this

    if (events & SAMPLETEMPERATURESENSOR_ADC)
    {
    zclSampleTemperatureSensor_MeasuredValue = aadc ;
    osal_start_timerEx( zclSampleTemperatureSensor_TaskID, SAMPLETEMPERATURESENSOR_ADC, SAMPLETEMPERATURESENSOR_REPORT_INTERVAL );
    return ( events ^ SAMPLETEMPERATURESENSOR_ADC );
    }

    and i initialized that aadc as int16 aadc = 30 ;

    but the code is executed without error but in lcd it s not displaying the value 30 . 

    6266.zcl_sampletemperaturesensor.c
    /**************************************************************************************************
      Filename:       zcl_sampletemperaturesensor.c
      Revised:        $Date: 2013-10-18 11:49:27 -0700 (Fri, 18 Oct 2013) $
      Revision:       $Revision: 35718 $
    
      Description:    Zigbee Cluster Library - sample device application.
    
    
      Copyright 2013 Texas Instruments Incorporated. All rights reserved.
    
      IMPORTANT: Your use of this Software is limited to those specific rights
      granted under the terms of a software license agreement between the user
      who downloaded the software, his/her employer (which must be your employer)
      and Texas Instruments Incorporated (the "License").  You may not use this
      Software unless you agree to abide by the terms of the License. The License
      limits your use, and you acknowledge, that the Software may not be modified,
      copied or distributed unless embedded on a Texas Instruments microcontroller
      or used solely and exclusively in conjunction with a Texas Instruments radio
      frequency transceiver, which is integrated into your product.  Other than for
      the foregoing purpose, you may not use, reproduce, copy, prepare derivative
      works of, modify, distribute, perform, display or sell this Software and/or
      its documentation for any purpose.
    
      YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE
      PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,
      INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE,
      NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL
      TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT,
      NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER
      LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
      INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE
      OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT
      OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
      (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
    
      Should you have any questions regarding your right to use this Software,
      contact Texas Instruments Incorporated at www.TI.com.
    **************************************************************************************************/
    
    /*********************************************************************
      This device will act as a temperature sensor. It updates the current
      temperature on the thermostat when the user sends the desired
      temperature using SW1.
    
      SCREEN MODES
      ----------------------------------------
      Main:
        - SW1: Send current temperature
        - SW2: Invoke EZMode
        - SW3: Adjust temperature
        - SW5: Go to Help screen
    
      Temperature:
        - SW1: Increase temperature
        - SW3: Decrease temperature
        - SW5: Enter temperature
      ----------------------------------------
    *********************************************************************/
    
    /*********************************************************************
     * INCLUDES
     */
    #include "ZComDef.h"
    #include "OSAL.h"
    #include "AF.h"
    #include "ZDApp.h"
    #include "ZDObject.h"
    #include "ZDProfile.h"
    #include "MT_SYS.h"
    
    #include "zcl.h"
    #include "zcl_general.h"
    #include "zcl_ha.h"
    #include "zcl_ezmode.h"
    #include "zcl_ms.h"
    #include "hal_adc.h"
    
    #include "zcl_sampletemperaturesensor.h"
    
    #include "onboard.h"
    
    /* HAL */
    #include "hal_lcd.h"
    #include "hal_led.h"
    #include "hal_key.h"
    //#include "zcl_sampletemperaturesensor_data.c"
    
    /*********************************************************************
     * MACROS
     */
    
    // how often to report temperature
    #define SAMPLETEMPERATURESENSOR_REPORT_INTERVAL   10000
    
    /*********************************************************************
     * CONSTANTS
     */
    
    /*********************************************************************
     * TYPEDEFS
     */
    /*#ifdef __IAR_SYSTEMS_ICC__
    #define  CODE   __code
    #define  XDATA  __xdata
    */
    
    /*********************************************************************
     * GLOBAL VARIABLES
     */
    byte zclSampleTemperatureSensor_TaskID;
    
    uint8 zclSampleTemperatureSensorSeqNum;
    
    static byte gPermitDuration = 0x00;
    int16 zclSampleTemperatureSensor_MeasuredValue;
    /*void initTempSensor(void){
       DISABLE_ALL_INTERRUPTS();            //??????
       InitClock();                         //????????32M
       *((BYTE XDATA*) 0x624B) = 0x01;    //???????
       *((BYTE XDATA*) 0x61BD) = 0x01;    //???????ADC????
    }*/
    
    /*********************************************************************
     * GLOBAL FUNCTIONS
     */
    
    /*********************************************************************
     * LOCAL VARIABLES
     */
    afAddrType_t zclSampleTemperatureSensor_DstAddr;
    
    #ifdef ZCL_EZMODE
    static void zclSampleTemperatureSensor_ProcessZDOMsgs( zdoIncomingMsg_t *pMsg );
    static void zclSampleTemperatureSensor_EZModeCB( zlcEZMode_State_t state, zclEZMode_CBData_t *pData );
    uint16 aadc = 30;
    
    static const zclEZMode_RegisterData_t zclSampleTemperatureSensor_RegisterEZModeData =
    {
      &zclSampleTemperatureSensor_TaskID,
      SAMPLETEMPERATURESENSOR_EZMODE_NEXTSTATE_EVT,
      SAMPLETEMPERATURESENSOR_EZMODE_TIMEOUT_EVT,
      &zclSampleTemperatureSensorSeqNum,
      zclSampleTemperatureSensor_EZModeCB
    };
    
    // NOT ZCL_EZMODE, Use EndDeviceBind
    #else
    
    static cId_t bindingOutClusters[] =
    {
      ZCL_CLUSTER_ID_MS_TEMPERATURE_MEASUREMENT
    };
    #define ZCLSAMPLETEMPERATURESENSOR_BINDINGLIST        1
    #endif
    
    devStates_t zclSampleTemperatureSensor_NwkState = DEV_INIT;
    
    uint8 giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;   // display main screen mode first
    
    static uint8 aProcessCmd[] = { 1, 0, 0, 0 }; // used for reset command, { length + cmd0 + cmd1 + data }
    
    // Test Endpoint to allow SYS_APP_MSGs
    static endPointDesc_t sampleTemperatureSensor_TestEp =
    {
      20,                                 // Test endpoint
      &zclSampleTemperatureSensor_TaskID,
      (SimpleDescriptionFormat_t *)NULL,  // No Simple description for this test endpoint
      (afNetworkLatencyReq_t)0            // No Network Latency req
    };
    
    
    /*********************************************************************
     * LOCAL FUNCTIONS
     */
    static void zclSampleTemperatureSensor_HandleKeys( byte shift, byte keys );
    static void zclSampleTemperatureSensor_BasicResetCB( void );
    static void zclSampleTemperatureSensor_IdentifyCB( zclIdentify_t *pCmd );
    static void zclSampleTemperatureSensor_IdentifyQueryRspCB( zclIdentifyQueryRsp_t *pRsp );
    static void zclSampleTemperatureSensor_ProcessIdentifyTimeChange( void );
    //void getTemperature(void);
    // app display functions
    void zclSampleTemperatureSensor_LcdDisplayUpdate(void);
    void zclSampleTemperatureSensor_LcdDisplayMainMode(void);
    void zclSampleTemperatureSensor_LcdDisplayTempMode(void);
    void zclSampleTemperatureSensor_LcdDisplayHelpMode(void);
    //int16 getTemperature(void);
    //uint16 aadc = 30;
    
    static void zclSampleTemperatureSensor_SendTemp(void);
    
    // Functions to process ZCL Foundation incoming Command/Response messages
    static void zclSampleTemperatureSensor_ProcessIncomingMsg( zclIncomingMsg_t *msg );
    #ifdef ZCL_READ
    static uint8 zclSampleTemperatureSensor_ProcessInReadRspCmd( zclIncomingMsg_t *pInMsg );
    #endif
    #ifdef ZCL_WRITE
    static uint8 zclSampleTemperatureSensor_ProcessInWriteRspCmd( zclIncomingMsg_t *pInMsg );
    #endif
    static uint8 zclSampleTemperatureSensor_ProcessInDefaultRspCmd( zclIncomingMsg_t *pInMsg );
    #ifdef ZCL_DISCOVER
    static uint8 zclSampleTemperatureSensor_ProcessInDiscCmdsRspCmd( zclIncomingMsg_t *pInMsg );
    static uint8 zclSampleTemperatureSensor_ProcessInDiscAttrsRspCmd( zclIncomingMsg_t *pInMsg );
    static uint8 zclSampleTemperatureSensor_ProcessInDiscAttrsExtRspCmd( zclIncomingMsg_t *pInMsg );
    #endif // ZCL_DISCOVER
    
    /*********************************************************************
     * STATUS STRINGS
     */
    #ifdef LCD_SUPPORTED
    const char sClearLine[]    = " ";
    const char sDeviceName[]   = "  Temp Sensor";
    const char sSwTempUp[]     = "SW1: Raise Temp";
    const char sSwEZMode[]     = "SW2: EZ-Mode";
    const char sSwTempDown[]   = "SW3: Lower Temp";
    const char sSwHelp[]       = "SW5: Help";
    #endif
    
    /*********************************************************************
     * ZCL General Profile Callback table
     */
    static zclGeneral_AppCallbacks_t zclSampleTemperatureSensor_CmdCallbacks =
    {
      zclSampleTemperatureSensor_BasicResetCB,        // Basic Cluster Reset command
      zclSampleTemperatureSensor_IdentifyCB,          // Identify command
    #ifdef ZCL_EZMODE
      NULL,                                           // Identify EZ-Mode Invoke command
      NULL,                                           // Identify Update Commission State command
    #endif
      NULL,                                           // Identify Trigger Effect command
      zclSampleTemperatureSensor_IdentifyQueryRspCB,  // Identify Query Response command
      NULL,             				                      // On/Off cluster command
      NULL,                                           // On/Off cluster enhanced command Off with Effect
      NULL,                                           // On/Off cluster enhanced command On with Recall Global Scene
      NULL,                                           // On/Off cluster enhanced command On with Timed Off
    #ifdef ZCL_LEVEL_CTRL
      NULL,                                           // Level Control Move to Level command
      NULL,                                           // Level Control Move command
      NULL,                                           // Level Control Step command
      NULL,                                           // Level Control Stop command
    #endif
    #ifdef ZCL_GROUPS
      NULL,                                           // Group Response commands
    #endif
    #ifdef ZCL_SCENES
      NULL,                                           // Scene Store Request command
      NULL,                                           // Scene Recall Request command
      NULL,                                           // Scene Response command
    #endif
    #ifdef ZCL_ALARMS
      NULL,                                           // Alarm (Response) commands
    #endif
    #ifdef SE_UK_EXT
      NULL,                                           // Get Event Log command
      NULL,                                           // Publish Event Log command
    #endif
      NULL,                                           // RSSI Location command
      NULL                                            // RSSI Location Response command
    };
    
    /*********************************************************************
     * @fn          zclSampleTemperatureSensor_Init
     *
     * @brief       Initialization function for the zclGeneral layer.
     *
     * @param       none
     *
     * @return      none
     */
    void zclSampleTemperatureSensor_Init( byte task_id )
    {
      zclSampleTemperatureSensor_TaskID = task_id;
    
      // Set destination address to indirect
      zclSampleTemperatureSensor_DstAddr.addrMode = (afAddrMode_t)AddrNotPresent;
      zclSampleTemperatureSensor_DstAddr.endPoint = 0;
      zclSampleTemperatureSensor_DstAddr.addr.shortAddr = 0;
    
      // This app is part of the Home Automation Profile
      zclHA_Init( &zclSampleTemperatureSensor_SimpleDesc );
    
      // Register the ZCL General Cluster Library callback functions
      zclGeneral_RegisterCmdCallbacks( SAMPLETEMPERATURESENSOR_ENDPOINT, &zclSampleTemperatureSensor_CmdCallbacks );
    
      // Register the application's attribute list
      zcl_registerAttrList( SAMPLETEMPERATURESENSOR_ENDPOINT, SAMPLETEMPERATURESENSOR_MAX_ATTRIBUTES, zclSampleTemperatureSensor_Attrs );
    
      // Register the Application to receive the unprocessed Foundation command/response messages
      zcl_registerForMsg( zclSampleTemperatureSensor_TaskID );
    
    #ifdef ZCL_EZMODE
      // Register EZ-Mode
      zcl_RegisterEZMode( &zclSampleTemperatureSensor_RegisterEZModeData );
    
      // Register with the ZDO to receive Match Descriptor Responses
      ZDO_RegisterForZDOMsg(task_id, Match_Desc_rsp);
    #endif
    
      // Register for all key events - This app will handle all key events
      RegisterForKeys( zclSampleTemperatureSensor_TaskID );
    
      // Register for a test endpoint
      afRegister( &sampleTemperatureSensor_TestEp );
    
    #ifdef LCD_SUPPORTED
      // display the device name
      HalLcdWriteString( (char *)sDeviceName, HAL_LCD_LINE_3 );
    #endif
    }
    
    /*********************************************************************
     * @fn          zclSample_event_loop
     *
     * @brief       Event Loop Processor for zclGeneral.
     *
     * @param       none
     *
     * @return      none
     */
    uint16 zclSampleTemperatureSensor_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( zclSampleTemperatureSensor_TaskID )) )
        {
          switch ( MSGpkt->hdr.event )
          {
    #ifdef ZCL_EZMODE
            case ZDO_CB_MSG:
              zclSampleTemperatureSensor_ProcessZDOMsgs( (zdoIncomingMsg_t *)MSGpkt );
              break;
    #endif
    
            case ZCL_INCOMING_MSG:
              // Incoming ZCL Foundation command/response messages
              zclSampleTemperatureSensor_ProcessIncomingMsg( (zclIncomingMsg_t *)MSGpkt );
              break;
    
            case KEY_CHANGE:
              zclSampleTemperatureSensor_HandleKeys( ((keyChange_t *)MSGpkt)->state, ((keyChange_t *)MSGpkt)->keys );
              break;
    
            case ZDO_STATE_CHANGE:
              zclSampleTemperatureSensor_NwkState = (devStates_t)(MSGpkt->hdr.status);
    
    
              // now on the network
              if ( (zclSampleTemperatureSensor_NwkState == DEV_ZB_COORD) ||
                   (zclSampleTemperatureSensor_NwkState == DEV_ROUTER)   ||
                   (zclSampleTemperatureSensor_NwkState == DEV_END_DEVICE) )
              {
    #ifndef HOLD_AUTO_START
                giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;
                zclSampleTemperatureSensor_LcdDisplayUpdate();
    #endif
    #ifdef ZCL_EZMODE
                zcl_EZModeAction( EZMODE_ACTION_NETWORK_STARTED, NULL );
    #endif // ZCL_EZMODE
              }
              break;
    
            default:
              break;
          }
    
          // Release the memory
          osal_msg_deallocate( (uint8 *)MSGpkt );
        }
    
        // return unprocessed events
        return (events ^ SYS_EVENT_MSG);
      }
    
      if ( events & SAMPLETEMPERATURESENSOR_IDENTIFY_TIMEOUT_EVT )
      {
        if ( zclSampleTemperatureSensor_IdentifyTime > 0 )
          zclSampleTemperatureSensor_IdentifyTime--;
        zclSampleTemperatureSensor_ProcessIdentifyTimeChange();
    
        return ( events ^ SAMPLETEMPERATURESENSOR_IDENTIFY_TIMEOUT_EVT );
      }
    
    #ifdef ZCL_EZMODE
      // going on to next state
      if ( events & SAMPLETEMPERATURESENSOR_EZMODE_NEXTSTATE_EVT )
      {
        zcl_EZModeAction ( EZMODE_ACTION_PROCESS, NULL );   // going on to next state
        return ( events ^ SAMPLETEMPERATURESENSOR_EZMODE_NEXTSTATE_EVT );
      }
    
      // the overall EZMode timer expired, so we timed out
      if ( events & SAMPLETEMPERATURESENSOR_EZMODE_TIMEOUT_EVT )
      {
        zcl_EZModeAction ( EZMODE_ACTION_TIMED_OUT, NULL ); // EZ-Mode timed out
        return ( events ^ SAMPLETEMPERATURESENSOR_EZMODE_TIMEOUT_EVT );
      }
    #endif // ZLC_EZMODE
    
      if ( events & SAMPLETEMPERATURESENSOR_MAIN_SCREEN_EVT )
      {
        giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;
        zclSampleTemperatureSensor_LcdDisplayUpdate();
    
        return ( events ^ SAMPLETEMPERATURESENSOR_MAIN_SCREEN_EVT );
      }
    
      if (events & SAMPLETEMPERATURESENSOR_ADC)
      {
        zclSampleTemperatureSensor_MeasuredValue = aadc ;
    osal_start_timerEx( zclSampleTemperatureSensor_TaskID, SAMPLETEMPERATURESENSOR_ADC, SAMPLETEMPERATURESENSOR_REPORT_INTERVAL );
        return ( events ^ SAMPLETEMPERATURESENSOR_ADC );
      }
    
      if ( events & SAMPLETEMPERATURESENSOR_TEMP_SEND_EVT )
      {
        zclSampleTemperatureSensor_SendTemp();
    
        // report current temperature reading every 10 seconds
        osal_start_timerEx( zclSampleTemperatureSensor_TaskID, SAMPLETEMPERATURESENSOR_TEMP_SEND_EVT, SAMPLETEMPERATURESENSOR_REPORT_INTERVAL );
    
        return ( events ^ SAMPLETEMPERATURESENSOR_TEMP_SEND_EVT );
      }
    
      // Discard unknown events
      return 0;
    }
    
    /*int16 getTemperature(void)
    {
      int16 AvgTemp;
      uint8   i;
      uint16  AdcValue;
      uint16  value;
      uint16  value1;
    //char i;
            //char TempValue[10]; 
            //initTempSensor(); 
      AdcValue = 0;
      for( i = 0; i < 4; i++ )
      {
    ADCIF = 0;
        ADCCON3 = (HAL_ADC_REF_125V | HAL_ADC_RESOLUTION_14 | HAL_ADC_CHN_TEMP);    // ??1.25V????,12????,AD??:?????
            while ( !ADCIF );                                              //?????ADC
                     
        value =  ADCL >> 2;                        
        value |= (((UINT16)ADCH) << 6);    
        AdcValue += value;                          
      }
      value = AdcValue >> 2;                        //????4,?????
      value1 =   (((value) >> 4) - 335);
                 //??AD?,????????
      //while(1)
           // {
              AvgTemp = 0;
              for(i = 0 ; i < 64 ; i++)
              {
                AvgTemp += value1;              
                AvgTemp >>= 1;                              //??????2.
              }
              
           // }
      //zclSampleTemperatureSensor_MeasuredValue = AvgTemp ; 
      return AvgTemp;
    // * @param   keys - bit field for key events. Valid entries:
     //*                 HAL_KEY_SW_5
     //*                 HAL_KEY_SW_4
     //*                 HAL_KEY_SW_3
    }*/
    
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_HandleKeys
     *
     * @brief   Handles all key events for this device.
     *
     * @param   shift - true if in shift/alt.
     *                 HAL_KEY_SW_2
     *                 HAL_KEY_SW_1
     *
     * @return  none
     */
    static void zclSampleTemperatureSensor_HandleKeys( byte shift, byte keys )
    {
      if ( keys & HAL_KEY_SW_1 )
      {
        // increase temperature
        giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;
    
        if ( zclSampleTemperatureSensor_MeasuredValue < zclSampleTemperatureSensor_MaxMeasuredValue )
        {
          zclSampleTemperatureSensor_MeasuredValue = zclSampleTemperatureSensor_MeasuredValue + 100;  // considering using whole number value
        }
        else if ( zclSampleTemperatureSensor_MeasuredValue >= zclSampleTemperatureSensor_MaxMeasuredValue )
        {
          zclSampleTemperatureSensor_MeasuredValue = zclSampleTemperatureSensor_MaxMeasuredValue;
        }
    
        // Send temperature information
        zclSampleTemperatureSensor_SendTemp();
      }
    
      if ( keys & HAL_KEY_SW_2 )
      {
        if ( ( giTemperatureSensorScreenMode == TEMPSENSE_MAINMODE ) ||
            ( giTemperatureSensorScreenMode == TEMPSENSE_HELPMODE ) )
        {
          giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;
    
    #ifdef ZCL_EZMODE
          zclEZMode_InvokeData_t ezModeData;
          static uint16 clusterIDs[] = { ZCL_CLUSTER_ID_MS_TEMPERATURE_MEASUREMENT };   // only bind on the Temperature Measurement cluster
    
          // Invoke EZ-Mode
          ezModeData.endpoint = SAMPLETEMPERATURESENSOR_ENDPOINT; // endpoint on which to invoke EZ-Mode
          if ( ( zclSampleTemperatureSensor_NwkState == DEV_ZB_COORD ) ||
               ( zclSampleTemperatureSensor_NwkState == DEV_ROUTER )   ||
               ( zclSampleTemperatureSensor_NwkState == DEV_END_DEVICE ) )
          {
            ezModeData.onNetwork = TRUE;      // node is already on the network
          }
          else
          {
            ezModeData.onNetwork = FALSE;     // node is not yet on the network
          }
          ezModeData.initiator = TRUE;        // Temperature Sensor is an initiator
          ezModeData.numActiveInClusters = 1;
          ezModeData.pActiveInClusterIDs = clusterIDs;
          ezModeData.numActiveOutClusters = 0;   // active output cluster
          ezModeData.pActiveOutClusterIDs = NULL;
          zcl_InvokeEZMode( &ezModeData );
    
    #ifdef LCD_SUPPORTED
          HalLcdWriteString( "EZMode", HAL_LCD_LINE_2 );
    #endif
    
          // NOT ZCL_EZMODE, Use EndDeviceBind
    #else
          {
            zAddrType_t dstAddr;
            dstAddr.addrMode = Addr16Bit;
            dstAddr.addr.shortAddr = 0;   // Coordinator makes the EDB match
    
            // Initiate an End Device Bind Request, this bind request will
            // only use a cluster list that is important to binding.
            HalLedSet ( HAL_LED_4, HAL_LED_MODE_OFF );
            ZDP_EndDeviceBindReq( &dstAddr, NLME_GetShortAddr(),
                                  SAMPLETEMPERATURESENSOR_ENDPOINT,
                                  ZCL_HA_PROFILE_ID,
                                  0, NULL,
                                  ZCLSAMPLETEMPERATURESENSOR_BINDINGLIST, bindingOutClusters,
                                  FALSE );
          }
    #endif // ZCL_EZMODE
        }
      }
    
      if ( keys & HAL_KEY_SW_3 )
      {
        giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;
    
        // decrease the temperature
        if ( zclSampleTemperatureSensor_MeasuredValue > zclSampleTemperatureSensor_MinMeasuredValue )
        {
          zclSampleTemperatureSensor_MeasuredValue = zclSampleTemperatureSensor_MeasuredValue - 100;  // considering using whole number value
        }
        else if ( zclSampleTemperatureSensor_MeasuredValue >= zclSampleTemperatureSensor_MinMeasuredValue )
        {
          zclSampleTemperatureSensor_MeasuredValue = zclSampleTemperatureSensor_MinMeasuredValue;
        }
    
        // Send temperature information
        zclSampleTemperatureSensor_SendTemp();
      }
    
      if ( keys & HAL_KEY_SW_4 )
      {
        giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;
    
        if ( ( zclSampleTemperatureSensor_NwkState == DEV_ZB_COORD ) ||
             ( zclSampleTemperatureSensor_NwkState == DEV_ROUTER ) )
        {
          // toggle permit join
          gPermitDuration = gPermitDuration ? 0 : 0xff;
          NLME_PermitJoiningRequest( gPermitDuration );
        }
      }
    
      if ( shift && ( keys & HAL_KEY_SW_5 ) )
      {
        zclSampleTemperatureSensor_BasicResetCB();
      }
      else if ( keys & HAL_KEY_SW_5 )
      {
        if ( giTemperatureSensorScreenMode == TEMPSENSE_MAINMODE )
        {
          giTemperatureSensorScreenMode = TEMPSENSE_HELPMODE;
        }
        else if ( giTemperatureSensorScreenMode == TEMPSENSE_HELPMODE )
        {
    #ifdef LCD_SUPPORTED
          HalLcdWriteString( (char *)sClearLine, HAL_LCD_LINE_2 );
    #endif
          giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;
        }
      }
    
      // update display
      zclSampleTemperatureSensor_LcdDisplayUpdate();
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_LcdDisplayUpdate
     *
     * @brief   Called to update the LCD display.
     *
     * @param   none
     *
     * @return  none
     */
    void zclSampleTemperatureSensor_LcdDisplayUpdate( void )
    {
      // turn on red LED for temperatures >= 24.00C
      if ( zclSampleTemperatureSensor_MeasuredValue >= 2400 )
      {
        HalLedSet ( HAL_LED_1, HAL_LED_MODE_OFF );
        HalLedSet ( HAL_LED_2, HAL_LED_MODE_ON );
      }
      // turn on green LED for temperatures <= 20.00C
      else if ( zclSampleTemperatureSensor_MeasuredValue <= 2000 )
      {
        HalLedSet ( HAL_LED_1, HAL_LED_MODE_ON );
        HalLedSet ( HAL_LED_2, HAL_LED_MODE_OFF );
      }
      // turn on both red and green LEDs for temperatures between 20.00C and 24.00C
      else
      {
        HalLedSet ( HAL_LED_1, HAL_LED_MODE_ON );
        HalLedSet ( HAL_LED_2, HAL_LED_MODE_ON );
      }
    
      if ( giTemperatureSensorScreenMode == TEMPSENSE_HELPMODE )
      {
        zclSampleTemperatureSensor_LcdDisplayHelpMode();
      }
      else
      {
        zclSampleTemperatureSensor_LcdDisplayMainMode();
      }
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_LcdDisplayMainMode
     *
     * @brief   Called to display the main screen on the LCD.
     *
     * @param   none
     *
     * @return  none
     */
    void zclSampleTemperatureSensor_LcdDisplayMainMode( void )
    {
      char sDisplayTemp[16];
    
      if ( zclSampleTemperatureSensor_NwkState == DEV_ZB_COORD )
      {
        zclHA_LcdStatusLine1( 0 );
      }
      else if ( zclSampleTemperatureSensor_NwkState == DEV_ROUTER )
      {
        zclHA_LcdStatusLine1( 1 );
      }
      else if ( zclSampleTemperatureSensor_NwkState == DEV_END_DEVICE )
      {
        zclHA_LcdStatusLine1( 2 );
      }
    
      // display current temperature
      osal_memcpy(sDisplayTemp, "TEMP: ", 6);
      _ltoa( ( zclSampleTemperatureSensor_MeasuredValue / 100 ), (void *)(&sDisplayTemp[6]), 10 );   // convert temperature to whole number
      osal_memcpy( &sDisplayTemp[8], "C", 2 );
    #ifdef LCD_SUPPORTED
      HalLcdWriteString( (char *)sDisplayTemp, HAL_LCD_LINE_2 );
    #endif
    
    #ifdef LCD_SUPPORTED
      if ( ( zclSampleTemperatureSensor_NwkState == DEV_ZB_COORD ) ||
           ( zclSampleTemperatureSensor_NwkState == DEV_ROUTER ) )
      {
        // display help key with permit join status
        if ( gPermitDuration )
        {
          HalLcdWriteString( "SW5: Help      *", HAL_LCD_LINE_3 );
        }
        else
        {
          HalLcdWriteString( "SW5: Help       ", HAL_LCD_LINE_3 );
        }
      }
      else
      {
        // display help key
        HalLcdWriteString( (char *)sSwHelp, HAL_LCD_LINE_3 );
      }
    #endif
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_LcdDisplayHelpMode
     *
     * @brief   Called to display the SW options on the LCD.
     *
     * @param   none
     *
     * @return  none
     */
    void zclSampleTemperatureSensor_LcdDisplayHelpMode( void )
    {
    #ifdef LCD_SUPPORTED
      HalLcdWriteString( (char *)sSwTempUp, HAL_LCD_LINE_1 );
      HalLcdWriteString( (char *)sSwEZMode, HAL_LCD_LINE_2 );
      HalLcdWriteString( (char *)sSwTempDown, HAL_LCD_LINE_3 );
    #endif
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_SendTemp
     *
     * @brief   Called to send current temperature information to the thermostat
     *
     * @param   none
     *
     * @return  none
     */
    static void zclSampleTemperatureSensor_SendTemp( void )
    {
    #ifdef ZCL_REPORT
      zclReportCmd_t *pReportCmd;
     // zclSampleTemperatureSensor_MeasuredValue = getTemperature();
      pReportCmd = osal_mem_alloc( sizeof(zclReportCmd_t) + sizeof(zclReport_t) );
      if ( pReportCmd != NULL )
      {
        pReportCmd->numAttr = 1;
        pReportCmd->attrList[0].attrID = ATTRID_MS_TEMPERATURE_MEASURED_VALUE;
        pReportCmd->attrList[0].dataType = ZCL_DATATYPE_INT16;
        pReportCmd->attrList[0].attrData = (void *)(&zclSampleTemperatureSensor_MeasuredValue);
    
        zcl_SendReportCmd( SAMPLETEMPERATURESENSOR_ENDPOINT, &zclSampleTemperatureSensor_DstAddr,
                           ZCL_CLUSTER_ID_MS_TEMPERATURE_MEASUREMENT,
                           pReportCmd, ZCL_FRAME_SERVER_CLIENT_DIR, TRUE, zclSampleTemperatureSensorSeqNum++ );
      }
    
      osal_mem_free( pReportCmd );
    #endif  // ZCL_REPORT
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_ProcessIdentifyTimeChange
     *
     * @brief   Called to process any change to the IdentifyTime attribute.
     *
     * @param   none
     *
     * @return  none
     */
    static void zclSampleTemperatureSensor_ProcessIdentifyTimeChange( void )
    {
      if ( zclSampleTemperatureSensor_IdentifyTime > 0 )
      {
        osal_start_timerEx( zclSampleTemperatureSensor_TaskID, SAMPLETEMPERATURESENSOR_IDENTIFY_TIMEOUT_EVT, 1000 );
        HalLedBlink ( HAL_LED_4, 0xFF, HAL_LED_DEFAULT_DUTY_CYCLE, HAL_LED_DEFAULT_FLASH_TIME );
      }
      else
      {
        if ( zclSampleTemperatureSensor_OnOff )
        {
          HalLedSet ( HAL_LED_4, HAL_LED_MODE_ON );
        }
        else
        {
          HalLedSet ( HAL_LED_4, HAL_LED_MODE_OFF );
        }
    
        osal_stop_timerEx( zclSampleTemperatureSensor_TaskID, SAMPLETEMPERATURESENSOR_IDENTIFY_TIMEOUT_EVT );
      }
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_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 zclSampleTemperatureSensor_BasicResetCB( void )
    {
      // Put device back to factory default settings
      zgWriteStartupOptions( ZG_STARTUP_SET, 3 );   // bit set both default configuration and default network
    
      // restart device
      MT_SysCommandProcessing( aProcessCmd );
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_IdentifyCB
     *
     * @brief   Callback from the ZCL General Cluster Library when
     *          it received an Identity Command for this application.
     *
     * @param   srcAddr - source address and endpoint of the response message
     * @param   identifyTime - the number of seconds to identify yourself
     *
     * @return  none
     */
    static void zclSampleTemperatureSensor_IdentifyCB( zclIdentify_t *pCmd )
    {
      zclSampleTemperatureSensor_IdentifyTime = pCmd->identifyTime;
      zclSampleTemperatureSensor_ProcessIdentifyTimeChange();
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_IdentifyQueryRspCB
     *
     * @brief   Callback from the ZCL General Cluster Library when
     *          it received an Identity Query Response Command for this application.
     *
     * @param   srcAddr - requestor's address
     * @param   timeout - number of seconds to identify yourself (valid for query response)
     *
     * @return  none
     */
    static void zclSampleTemperatureSensor_IdentifyQueryRspCB( zclIdentifyQueryRsp_t *pRsp )
    {
      (void)pRsp;
    #ifdef ZCL_EZMODE
      {
        zclEZMode_ActionData_t data;
        data.pIdentifyQueryRsp = pRsp;
        zcl_EZModeAction ( EZMODE_ACTION_IDENTIFY_QUERY_RSP, &data );
      }
    #endif
    }
    
    /******************************************************************************
     *
     *  Functions for processing ZCL Foundation incoming Command/Response messages
     *
     *****************************************************************************/
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_ProcessIncomingMsg
     *
     * @brief   Process ZCL Foundation incoming message
     *
     * @param   pInMsg - pointer to the received message
     *
     * @return  none
     */
    static void zclSampleTemperatureSensor_ProcessIncomingMsg( zclIncomingMsg_t *pInMsg)
    {
      switch ( pInMsg->zclHdr.commandID )
      {
    #ifdef ZCL_READ
        case ZCL_CMD_READ_RSP:
          zclSampleTemperatureSensor_ProcessInReadRspCmd( pInMsg );
          break;
    #endif
    #ifdef ZCL_WRITE
        case ZCL_CMD_WRITE_RSP:
          zclSampleTemperatureSensor_ProcessInWriteRspCmd( pInMsg );
          break;
    #endif
    #ifdef ZCL_REPORT
        // See ZCL Test Applicaiton (zcl_testapp.c) for sample code on Attribute Reporting
        case ZCL_CMD_CONFIG_REPORT:
          //zclSampleTemperatureSensor_ProcessInConfigReportCmd( pInMsg );
          break;
    
        case ZCL_CMD_CONFIG_REPORT_RSP:
          //zclSampleTemperatureSensor_ProcessInConfigReportRspCmd( pInMsg );
          break;
    
        case ZCL_CMD_READ_REPORT_CFG:
          //zclSampleTemperatureSensor_ProcessInReadReportCfgCmd( pInMsg );
          break;
    
        case ZCL_CMD_READ_REPORT_CFG_RSP:
          //zclSampleTemperatureSensor_ProcessInReadReportCfgRspCmd( pInMsg );
          break;
    
        case ZCL_CMD_REPORT:
          //zclSampleTemperatureSensor_ProcessInReportCmd( pInMsg );
          break;
    #endif
        case ZCL_CMD_DEFAULT_RSP:
          zclSampleTemperatureSensor_ProcessInDefaultRspCmd( pInMsg );
          break;
    #ifdef ZCL_DISCOVER
        case ZCL_CMD_DISCOVER_CMDS_RECEIVED_RSP:
          zclSampleTemperatureSensor_ProcessInDiscCmdsRspCmd( pInMsg );
          break;
    
        case ZCL_CMD_DISCOVER_CMDS_GEN_RSP:
          zclSampleTemperatureSensor_ProcessInDiscCmdsRspCmd( pInMsg );
          break;
    
        case ZCL_CMD_DISCOVER_ATTRS_RSP:
          zclSampleTemperatureSensor_ProcessInDiscAttrsRspCmd( pInMsg );
          break;
    
        case ZCL_CMD_DISCOVER_ATTRS_EXT_RSP:
          zclSampleTemperatureSensor_ProcessInDiscAttrsExtRspCmd( pInMsg );
          break;
    #endif
        default:
          break;
      }
    
      if ( pInMsg->attrCmd )
      {
        osal_mem_free( pInMsg->attrCmd );
      }
    }
    
    #ifdef ZCL_READ
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_ProcessInReadRspCmd
     *
     * @brief   Process the "Profile" Read Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleTemperatureSensor_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      zclSampleTemperatureSensor_ProcessInWriteRspCmd
     *
     * @brief   Process the "Profile" Write Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleTemperatureSensor_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      zclSampleTemperatureSensor_ProcessInDefaultRspCmd
     *
     * @brief   Process the "Profile" Default Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleTemperatureSensor_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      zclSampleTemperatureSensor_ProcessInDiscCmdsRspCmd
     *
     * @brief   Process the Discover Commands Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleTemperatureSensor_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      zclSampleTemperatureSensor_ProcessInDiscAttrsRspCmd
     *
     * @brief   Process the "Profile" Discover Attributes Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleTemperatureSensor_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      zclSampleTemperatureSensor_ProcessInDiscAttrsExtRspCmd
     *
     * @brief   Process the "Profile" Discover Attributes Extended Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleTemperatureSensor_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
    
    #ifdef ZCL_EZMODE
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_ProcessZDOMsgs
     *
     * @brief   Called when this node receives a ZDO/ZDP response.
     *
     * @param   none
     *
     * @return  status
     */
    static void zclSampleTemperatureSensor_ProcessZDOMsgs( zdoIncomingMsg_t *pMsg )
    {
      zclEZMode_ActionData_t data;
      ZDO_MatchDescRsp_t *pMatchDescRsp;
    
      // Let EZ-Mode know of the Match Descriptor Response
      if ( pMsg->clusterID == Match_Desc_rsp )
      {
        pMatchDescRsp = ZDO_ParseEPListRsp( pMsg );
        data.pMatchDescRsp = pMatchDescRsp;
        zcl_EZModeAction( EZMODE_ACTION_MATCH_DESC_RSP, &data );
        osal_mem_free( pMatchDescRsp );
      }
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_EZModeCB
     *
     * @brief   The Application is informed of events. This can be used to show on the UI what is
    *           going on during EZ-Mode steering/finding/binding.
     *
     * @param   state - an
     *
     * @return  none
     */
    static void zclSampleTemperatureSensor_EZModeCB( zlcEZMode_State_t state, zclEZMode_CBData_t *pData )
    {
    #ifdef LCD_SUPPORTED
      char szLine[20];
      char *pStr;
      uint8 err;
    #endif
    
      // time to go into identify mode
      if ( state == EZMODE_STATE_IDENTIFYING )
      {
        zclSampleTemperatureSensor_IdentifyTime = ( EZMODE_TIME / 1000 );  // convert to seconds
        zclSampleTemperatureSensor_ProcessIdentifyTimeChange();
      }
    
      // autoclosing, show what happened (success, cancelled, etc...)
      if( state == EZMODE_STATE_AUTOCLOSE )
      {
    #ifdef LCD_SUPPORTED
        pStr = NULL;
        err = pData->sAutoClose.err;
        if ( err == EZMODE_ERR_SUCCESS )
        {
          pStr = "EZMode: Success";
        }
        else if ( err == EZMODE_ERR_NOMATCH )
        {
          pStr = "EZMode: NoMatch"; // not a match made in heaven
        }
        if ( pStr )
        {
          if ( giTemperatureSensorScreenMode == TEMPSENSE_MAINMODE )
          {
            HalLcdWriteString ( pStr, HAL_LCD_LINE_2 );
          }
        }
    #endif
      }
    
      // finished, either show DstAddr/EP, or nothing (depending on success or not)
      if( state == EZMODE_STATE_FINISH )
      {
        // turn off identify mode
        zclSampleTemperatureSensor_IdentifyTime = 0;
        zclSampleTemperatureSensor_ProcessIdentifyTimeChange();
    
    #ifdef LCD_SUPPORTED
        // if successful, inform user which nwkaddr/ep we bound to
        pStr = NULL;
        err = pData->sFinish.err;
        if( err == EZMODE_ERR_SUCCESS )
        {
          // "EZDst:1234 EP:34"
          osal_memcpy( szLine, "EZDst:", 6 );
          zclHA_uint16toa( pData->sFinish.nwkaddr, &szLine[6] );
          osal_memcpy( &szLine[10], " EP:", 4 );
          _ltoa( pData->sFinish.ep, (void *)(&szLine[14]), 16 );  // _ltoa NULL terminates
          pStr = szLine;
        }
        else if ( err == EZMODE_ERR_BAD_PARAMETER )
        {
          pStr = "EZMode: BadParm";
        }
        else if ( err == EZMODE_ERR_CANCELLED )
        {
          pStr = "EZMode: Cancel";
        }
        else
        {
          pStr = "EZMode: TimeOut";
        }
        if ( pStr )
        {
          if ( giTemperatureSensorScreenMode == TEMPSENSE_MAINMODE )
          {
            HalLcdWriteString ( pStr, HAL_LCD_LINE_2 );
          }
        }
    #endif  // LCD_SUPPORTED
    
        // show main UI screen 3 seconds after joining network
        osal_start_timerEx( zclSampleTemperatureSensor_TaskID, SAMPLETEMPERATURESENSOR_MAIN_SCREEN_EVT, 3000 );
    
        // report current temperature reading 15 seconds after joinging the network
        osal_start_timerEx( zclSampleTemperatureSensor_TaskID, SAMPLETEMPERATURESENSOR_TEMP_SEND_EVT, SAMPLETEMPERATURESENSOR_REPORT_INTERVAL );
      }
    }
    #endif // ZCL_EZMODE
    
    /****************************************************************************
    ****************************************************************************/
    
    
    

  • even i write the event as 

    if (events & SAMPLETEMPERATURESENSOR_ADC)
    {
    zclSampleTemperatureSensor_MeasuredValue = 30 ;
    osal_start_timerEx( zclSampleTemperatureSensor_TaskID, SAMPLETEMPERATURESENSOR_ADC, SAMPLETEMPERATURESENSOR_REPORT_INTERVAL );
    return ( events ^ SAMPLETEMPERATURESENSOR_ADC );
    }

    i couldnt be able to get the value 30 in lcd  

  • Do you set breakpoint at "zclSampleTemperatureSensor_MeasuredValue = 30 ;" to check if your program goes to this line? If not, how come your LCD would display 30?

  • if (events & SAMPLETEMPERATURESENSOR_ADC)
    {
    zclSampleTemperatureSensor_MeasuredValue = aadc ;
    osal_start_timerEx( zclSampleTemperatureSensor_TaskID, SAMPLETEMPERATURESENSOR_ADC, SAMPLETEMPERATURESENSOR_REPORT_INTERVAL );
    return ( events ^ SAMPLETEMPERATURESENSOR_ADC );
    }

    i set the bp and checked and u r rite the value is not even setting !!! what can i do ??? what am i doing wrong ??


  • int16 aadc = 30;
    int16 zclSampleTemperatureSensor_MeasuredValue = aadc; // 22.00C
    const int16 zclSampleTemperatureSensor_MinMeasuredValue = 1700; // 17.00C
    const uint16 zclSampleTemperatureSensor_MaxMeasuredValue = 2700; // 27.00C

    i tried asigning values like this in zcl_temperaturesensor_data.c  but it s showing an error 

    Error[Pe028]: expression must have a constant value E:\PROJECT\15$11\Z-Stack Home 1.2.1\Projects\zstack\HomeAutomation\SampleTemperatureSensor\Source\zcl_sampletemperaturesensor_data.c 100

  • I don't see you call "osal_start_timerEx( zclSampleTemperatureSensor_TaskID, SAMPLETEMPERATURESENSOR_ADC, SAMPLETEMPERATURESENSOR_REPORT_INTERVAL );" in ZDO_STATE_CHANGE so the SAMPLETEMPERATURESENSOR_ADC event won't be started after it joins network.

  • 4403.zcl_sampletemperaturesensor.c
    /**************************************************************************************************
      Filename:       zcl_sampletemperaturesensor.c
      Revised:        $Date: 2013-10-18 11:49:27 -0700 (Fri, 18 Oct 2013) $
      Revision:       $Revision: 35718 $
    
      Description:    Zigbee Cluster Library - sample device application.
    
    
      Copyright 2013 Texas Instruments Incorporated. All rights reserved.
    
      IMPORTANT: Your use of this Software is limited to those specific rights
      granted under the terms of a software license agreement between the user
      who downloaded the software, his/her employer (which must be your employer)
      and Texas Instruments Incorporated (the "License").  You may not use this
      Software unless you agree to abide by the terms of the License. The License
      limits your use, and you acknowledge, that the Software may not be modified,
      copied or distributed unless embedded on a Texas Instruments microcontroller
      or used solely and exclusively in conjunction with a Texas Instruments radio
      frequency transceiver, which is integrated into your product.  Other than for
      the foregoing purpose, you may not use, reproduce, copy, prepare derivative
      works of, modify, distribute, perform, display or sell this Software and/or
      its documentation for any purpose.
    
      YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE
      PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,
      INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE,
      NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL
      TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT,
      NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER
      LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
      INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE
      OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT
      OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
      (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
    
      Should you have any questions regarding your right to use this Software,
      contact Texas Instruments Incorporated at www.TI.com.
    **************************************************************************************************/
    
    /*********************************************************************
      This device will act as a temperature sensor. It updates the current
      temperature on the thermostat when the user sends the desired
      temperature using SW1.
    
      SCREEN MODES
      ----------------------------------------
      Main:
        - SW1: Send current temperature
        - SW2: Invoke EZMode
        - SW3: Adjust temperature
        - SW5: Go to Help screen
    
      Temperature:
        - SW1: Increase temperature
        - SW3: Decrease temperature
        - SW5: Enter temperature
      ----------------------------------------
    *********************************************************************/
    
    /*********************************************************************
     * INCLUDES
     */
    #include "ZComDef.h"
    #include "OSAL.h"
    #include "AF.h"
    #include "ZDApp.h"
    #include "ZDObject.h"
    #include "ZDProfile.h"
    #include "MT_SYS.h"
    
    #include "zcl.h"
    #include "zcl_general.h"
    #include "zcl_ha.h"
    #include "zcl_ezmode.h"
    #include "zcl_ms.h"
    #include "hal_adc.h"
    
    #include "zcl_sampletemperaturesensor.h"
    
    #include "onboard.h"
    
    /* HAL */
    #include "hal_lcd.h"
    #include "hal_led.h"
    #include "hal_key.h"
    //#include "zcl_sampletemperaturesensor_data.c"
    
    /*********************************************************************
     * MACROS
     */
    
    // how often to report temperature
    #define SAMPLETEMPERATURESENSOR_REPORT_INTERVAL   10000
    
    /*********************************************************************
     * CONSTANTS
     */
    
    /*********************************************************************
     * TYPEDEFS
     */
    /*#ifdef __IAR_SYSTEMS_ICC__
    #define  CODE   __code
    #define  XDATA  __xdata
    */
    
    /*********************************************************************
     * GLOBAL VARIABLES
     */
    byte zclSampleTemperatureSensor_TaskID;
    
    uint8 zclSampleTemperatureSensorSeqNum;
    
    static byte gPermitDuration = 0x00;
    int16 zclSampleTemperatureSensor_MeasuredValue;
    /*void initTempSensor(void){
       DISABLE_ALL_INTERRUPTS();            //??????
       InitClock();                         //????????32M
       *((BYTE XDATA*) 0x624B) = 0x01;    //???????
       *((BYTE XDATA*) 0x61BD) = 0x01;    //???????ADC????
    }*/
    
    /*********************************************************************
     * GLOBAL FUNCTIONS
     */
    
    /*********************************************************************
     * LOCAL VARIABLES
     */
    afAddrType_t zclSampleTemperatureSensor_DstAddr;
    
    #ifdef ZCL_EZMODE
    static void zclSampleTemperatureSensor_ProcessZDOMsgs( zdoIncomingMsg_t *pMsg );
    static void zclSampleTemperatureSensor_EZModeCB( zlcEZMode_State_t state, zclEZMode_CBData_t *pData );
    
    static const zclEZMode_RegisterData_t zclSampleTemperatureSensor_RegisterEZModeData =
    {
      &zclSampleTemperatureSensor_TaskID,
      SAMPLETEMPERATURESENSOR_EZMODE_NEXTSTATE_EVT,
      SAMPLETEMPERATURESENSOR_EZMODE_TIMEOUT_EVT,
      &zclSampleTemperatureSensorSeqNum,
      zclSampleTemperatureSensor_EZModeCB
    };
    
    // NOT ZCL_EZMODE, Use EndDeviceBind
    #else
    
    static cId_t bindingOutClusters[] =
    {
      ZCL_CLUSTER_ID_MS_TEMPERATURE_MEASUREMENT
    };
    #define ZCLSAMPLETEMPERATURESENSOR_BINDINGLIST        1
    #endif
    
    devStates_t zclSampleTemperatureSensor_NwkState = DEV_INIT;
    
    uint8 giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;   // display main screen mode first
    
    static uint8 aProcessCmd[] = { 1, 0, 0, 0 }; // used for reset command, { length + cmd0 + cmd1 + data }
    
    // Test Endpoint to allow SYS_APP_MSGs
    static endPointDesc_t sampleTemperatureSensor_TestEp =
    {
      20,                                 // Test endpoint
      &zclSampleTemperatureSensor_TaskID,
      (SimpleDescriptionFormat_t *)NULL,  // No Simple description for this test endpoint
      (afNetworkLatencyReq_t)0            // No Network Latency req
    };
    
    
    /*********************************************************************
     * LOCAL FUNCTIONS
     */
    static void zclSampleTemperatureSensor_HandleKeys( byte shift, byte keys );
    static void zclSampleTemperatureSensor_BasicResetCB( void );
    static void zclSampleTemperatureSensor_IdentifyCB( zclIdentify_t *pCmd );
    static void zclSampleTemperatureSensor_IdentifyQueryRspCB( zclIdentifyQueryRsp_t *pRsp );
    static void zclSampleTemperatureSensor_ProcessIdentifyTimeChange( void );
    //void getTemperature(void);
    
    // app display functions
    void zclSampleTemperatureSensor_LcdDisplayUpdate(void);
    void zclSampleTemperatureSensor_LcdDisplayMainMode(void);
    void zclSampleTemperatureSensor_LcdDisplayTempMode(void);
    void zclSampleTemperatureSensor_LcdDisplayHelpMode(void);
    //int16 getTemperature(void);
    int16 aadc = 3000;
    
    static void zclSampleTemperatureSensor_SendTemp(void);
    
    // Functions to process ZCL Foundation incoming Command/Response messages
    static void zclSampleTemperatureSensor_ProcessIncomingMsg( zclIncomingMsg_t *msg );
    #ifdef ZCL_READ
    static uint8 zclSampleTemperatureSensor_ProcessInReadRspCmd( zclIncomingMsg_t *pInMsg );
    #endif
    #ifdef ZCL_WRITE
    static uint8 zclSampleTemperatureSensor_ProcessInWriteRspCmd( zclIncomingMsg_t *pInMsg );
    #endif
    static uint8 zclSampleTemperatureSensor_ProcessInDefaultRspCmd( zclIncomingMsg_t *pInMsg );
    #ifdef ZCL_DISCOVER
    static uint8 zclSampleTemperatureSensor_ProcessInDiscCmdsRspCmd( zclIncomingMsg_t *pInMsg );
    static uint8 zclSampleTemperatureSensor_ProcessInDiscAttrsRspCmd( zclIncomingMsg_t *pInMsg );
    static uint8 zclSampleTemperatureSensor_ProcessInDiscAttrsExtRspCmd( zclIncomingMsg_t *pInMsg );
    #endif // ZCL_DISCOVER
    
    /*********************************************************************
     * STATUS STRINGS
     */
    #ifdef LCD_SUPPORTED
    const char sClearLine[]    = " ";
    const char sDeviceName[]   = "  Temp Sensor";
    const char sSwTempUp[]     = "SW1: Raise Temp";
    const char sSwEZMode[]     = "SW2: EZ-Mode";
    const char sSwTempDown[]   = "SW3: Lower Temp";
    const char sSwHelp[]       = "SW5: Help";
    #endif
    
    /*********************************************************************
     * ZCL General Profile Callback table
     */
    static zclGeneral_AppCallbacks_t zclSampleTemperatureSensor_CmdCallbacks =
    {
      zclSampleTemperatureSensor_BasicResetCB,        // Basic Cluster Reset command
      zclSampleTemperatureSensor_IdentifyCB,          // Identify command
    #ifdef ZCL_EZMODE
      NULL,                                           // Identify EZ-Mode Invoke command
      NULL,                                           // Identify Update Commission State command
    #endif
      NULL,                                           // Identify Trigger Effect command
      zclSampleTemperatureSensor_IdentifyQueryRspCB,  // Identify Query Response command
      NULL,             				                      // On/Off cluster command
      NULL,                                           // On/Off cluster enhanced command Off with Effect
      NULL,                                           // On/Off cluster enhanced command On with Recall Global Scene
      NULL,                                           // On/Off cluster enhanced command On with Timed Off
    #ifdef ZCL_LEVEL_CTRL
      NULL,                                           // Level Control Move to Level command
      NULL,                                           // Level Control Move command
      NULL,                                           // Level Control Step command
      NULL,                                           // Level Control Stop command
    #endif
    #ifdef ZCL_GROUPS
      NULL,                                           // Group Response commands
    #endif
    #ifdef ZCL_SCENES
      NULL,                                           // Scene Store Request command
      NULL,                                           // Scene Recall Request command
      NULL,                                           // Scene Response command
    #endif
    #ifdef ZCL_ALARMS
      NULL,                                           // Alarm (Response) commands
    #endif
    #ifdef SE_UK_EXT
      NULL,                                           // Get Event Log command
      NULL,                                           // Publish Event Log command
    #endif
      NULL,                                           // RSSI Location command
      NULL                                            // RSSI Location Response command
    };
    
    /*********************************************************************
     * @fn          zclSampleTemperatureSensor_Init
     *
     * @brief       Initialization function for the zclGeneral layer.
     *
     * @param       none
     *
     * @return      none
     */
    void zclSampleTemperatureSensor_Init( byte task_id )
    {
      zclSampleTemperatureSensor_TaskID = task_id;
    
      // Set destination address to indirect
      zclSampleTemperatureSensor_DstAddr.addrMode = (afAddrMode_t)AddrNotPresent;
      zclSampleTemperatureSensor_DstAddr.endPoint = 0;
      zclSampleTemperatureSensor_DstAddr.addr.shortAddr = 0;
    
      // This app is part of the Home Automation Profile
      zclHA_Init( &zclSampleTemperatureSensor_SimpleDesc );
    
      // Register the ZCL General Cluster Library callback functions
      zclGeneral_RegisterCmdCallbacks( SAMPLETEMPERATURESENSOR_ENDPOINT, &zclSampleTemperatureSensor_CmdCallbacks );
    
      // Register the application's attribute list
      zcl_registerAttrList( SAMPLETEMPERATURESENSOR_ENDPOINT, SAMPLETEMPERATURESENSOR_MAX_ATTRIBUTES, zclSampleTemperatureSensor_Attrs );
    
      // Register the Application to receive the unprocessed Foundation command/response messages
      zcl_registerForMsg( zclSampleTemperatureSensor_TaskID );
    
    #ifdef ZCL_EZMODE
      // Register EZ-Mode
      zcl_RegisterEZMode( &zclSampleTemperatureSensor_RegisterEZModeData );
    
      // Register with the ZDO to receive Match Descriptor Responses
      ZDO_RegisterForZDOMsg(task_id, Match_Desc_rsp);
    #endif
    
      // Register for all key events - This app will handle all key events
      RegisterForKeys( zclSampleTemperatureSensor_TaskID );
    
      // Register for a test endpoint
      afRegister( &sampleTemperatureSensor_TestEp );
    
    #ifdef LCD_SUPPORTED
      // display the device name
      HalLcdWriteString( (char *)sDeviceName, HAL_LCD_LINE_3 );
    #endif
    }
    
    /*********************************************************************
     * @fn          zclSample_event_loop
     *
     * @brief       Event Loop Processor for zclGeneral.
     *
     * @param       none
     *
     * @return      none
     */
    uint16 zclSampleTemperatureSensor_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( zclSampleTemperatureSensor_TaskID )) )
        {
          switch ( MSGpkt->hdr.event )
          {
    #ifdef ZCL_EZMODE
            case ZDO_CB_MSG:
              zclSampleTemperatureSensor_ProcessZDOMsgs( (zdoIncomingMsg_t *)MSGpkt );
              break;
    #endif
    
            case ZCL_INCOMING_MSG:
              // Incoming ZCL Foundation command/response messages
              zclSampleTemperatureSensor_ProcessIncomingMsg( (zclIncomingMsg_t *)MSGpkt );
              break;
    
            case KEY_CHANGE:
              zclSampleTemperatureSensor_HandleKeys( ((keyChange_t *)MSGpkt)->state, ((keyChange_t *)MSGpkt)->keys );
              break;
    
            case ZDO_STATE_CHANGE:
              zclSampleTemperatureSensor_NwkState = (devStates_t)(MSGpkt->hdr.status);
    osal_start_timerEx( zclSampleTemperatureSensor_TaskID, SAMPLETEMPERATURESENSOR_ADC, SAMPLETEMPERATURESENSOR_REPORT_INTERVAL );
    
              // now on the network
              if ( (zclSampleTemperatureSensor_NwkState == DEV_ZB_COORD) ||
                   (zclSampleTemperatureSensor_NwkState == DEV_ROUTER)   ||
                   (zclSampleTemperatureSensor_NwkState == DEV_END_DEVICE) )
              {
    #ifndef HOLD_AUTO_START
                giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;
                zclSampleTemperatureSensor_LcdDisplayUpdate();
    #endif
    #ifdef ZCL_EZMODE
                zcl_EZModeAction( EZMODE_ACTION_NETWORK_STARTED, NULL );
    #endif // ZCL_EZMODE
              }
              break;
    
            default:
              break;
          }
    
          // Release the memory
          osal_msg_deallocate( (uint8 *)MSGpkt );
        }
    
        // return unprocessed events
        return (events ^ SYS_EVENT_MSG);
      }
    
      if ( events & SAMPLETEMPERATURESENSOR_IDENTIFY_TIMEOUT_EVT )
      {
        if ( zclSampleTemperatureSensor_IdentifyTime > 0 )
          zclSampleTemperatureSensor_IdentifyTime--;
        zclSampleTemperatureSensor_ProcessIdentifyTimeChange();
    
        return ( events ^ SAMPLETEMPERATURESENSOR_IDENTIFY_TIMEOUT_EVT );
      }
    
    #ifdef ZCL_EZMODE
      // going on to next state
      if ( events & SAMPLETEMPERATURESENSOR_EZMODE_NEXTSTATE_EVT )
      {
        zcl_EZModeAction ( EZMODE_ACTION_PROCESS, NULL );   // going on to next state
        return ( events ^ SAMPLETEMPERATURESENSOR_EZMODE_NEXTSTATE_EVT );
      }
    
      // the overall EZMode timer expired, so we timed out
      if ( events & SAMPLETEMPERATURESENSOR_EZMODE_TIMEOUT_EVT )
      {
        zcl_EZModeAction ( EZMODE_ACTION_TIMED_OUT, NULL ); // EZ-Mode timed out
        return ( events ^ SAMPLETEMPERATURESENSOR_EZMODE_TIMEOUT_EVT );
      }
    #endif // ZLC_EZMODE
    
      if ( events & SAMPLETEMPERATURESENSOR_MAIN_SCREEN_EVT )
      {
        giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;
        zclSampleTemperatureSensor_LcdDisplayUpdate();
    
        return ( events ^ SAMPLETEMPERATURESENSOR_MAIN_SCREEN_EVT );
      }
    
      if (events & SAMPLETEMPERATURESENSOR_ADC)
      {
        zclSampleTemperatureSensor_MeasuredValue = aadc ;
        osal_start_timerEx( zclSampleTemperatureSensor_TaskID, SAMPLETEMPERATURESENSOR_ADC, SAMPLETEMPERATURESENSOR_REPORT_INTERVAL );
        return ( events ^ SAMPLETEMPERATURESENSOR_ADC );
      }
    
      if ( events & SAMPLETEMPERATURESENSOR_TEMP_SEND_EVT )
      {
        zclSampleTemperatureSensor_SendTemp();
    
        // report current temperature reading every 10 seconds
        osal_start_timerEx( zclSampleTemperatureSensor_TaskID, SAMPLETEMPERATURESENSOR_TEMP_SEND_EVT, SAMPLETEMPERATURESENSOR_REPORT_INTERVAL );
    
        return ( events ^ SAMPLETEMPERATURESENSOR_TEMP_SEND_EVT );
      }
    
      // Discard unknown events
      return 0;
    }
    
    /*int16 getTemperature(void)
    {
      int16 AvgTemp;
      uint8   i;
      uint16  AdcValue;
      uint16  value;
      uint16  value1;
    //char i;
            //char TempValue[10]; 
            //initTempSensor(); 
      AdcValue = 0;
      for( i = 0; i < 4; i++ )
      {
    ADCIF = 0;
        ADCCON3 = (HAL_ADC_REF_125V | HAL_ADC_RESOLUTION_14 | HAL_ADC_CHN_TEMP);    // ??1.25V????,12????,AD??:?????
            while ( !ADCIF );                                              //?????ADC
                     
        value =  ADCL >> 2;                        
        value |= (((UINT16)ADCH) << 6);    
        AdcValue += value;                          
      }
      value = AdcValue >> 2;                        //????4,?????
      value1 =   (((value) >> 4) - 335);
                 //??AD?,????????
      //while(1)
           // {
              AvgTemp = 0;
              for(i = 0 ; i < 64 ; i++)
              {
                AvgTemp += value1;              
                AvgTemp >>= 1;                              //??????2.
              }
              
           // }
      //zclSampleTemperatureSensor_MeasuredValue = AvgTemp ; 
      return AvgTemp;
    // * @param   keys - bit field for key events. Valid entries:
     //*                 HAL_KEY_SW_5
     //*                 HAL_KEY_SW_4
     //*                 HAL_KEY_SW_3
    }*/
    
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_HandleKeys
     *
     * @brief   Handles all key events for this device.
     *
     * @param   shift - true if in shift/alt.
     *                 HAL_KEY_SW_2
     *                 HAL_KEY_SW_1
     *
     * @return  none
     */
    static void zclSampleTemperatureSensor_HandleKeys( byte shift, byte keys )
    {
      if ( keys & HAL_KEY_SW_1 )
      {
        // increase temperature
        giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;
    
        if ( zclSampleTemperatureSensor_MeasuredValue < zclSampleTemperatureSensor_MaxMeasuredValue )
        {
          zclSampleTemperatureSensor_MeasuredValue = zclSampleTemperatureSensor_MeasuredValue + 100;  // considering using whole number value
        }
        else if ( zclSampleTemperatureSensor_MeasuredValue >= zclSampleTemperatureSensor_MaxMeasuredValue )
        {
          zclSampleTemperatureSensor_MeasuredValue = zclSampleTemperatureSensor_MaxMeasuredValue;
        }
    
        // Send temperature information
        zclSampleTemperatureSensor_SendTemp();
      }
    
      if ( keys & HAL_KEY_SW_2 )
      {
        if ( ( giTemperatureSensorScreenMode == TEMPSENSE_MAINMODE ) ||
            ( giTemperatureSensorScreenMode == TEMPSENSE_HELPMODE ) )
        {
          giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;
    
    #ifdef ZCL_EZMODE
          zclEZMode_InvokeData_t ezModeData;
          static uint16 clusterIDs[] = { ZCL_CLUSTER_ID_MS_TEMPERATURE_MEASUREMENT };   // only bind on the Temperature Measurement cluster
    
          // Invoke EZ-Mode
          ezModeData.endpoint = SAMPLETEMPERATURESENSOR_ENDPOINT; // endpoint on which to invoke EZ-Mode
          if ( ( zclSampleTemperatureSensor_NwkState == DEV_ZB_COORD ) ||
               ( zclSampleTemperatureSensor_NwkState == DEV_ROUTER )   ||
               ( zclSampleTemperatureSensor_NwkState == DEV_END_DEVICE ) )
          {
            ezModeData.onNetwork = TRUE;      // node is already on the network
          }
          else
          {
            ezModeData.onNetwork = FALSE;     // node is not yet on the network
          }
          ezModeData.initiator = TRUE;        // Temperature Sensor is an initiator
          ezModeData.numActiveInClusters = 1;
          ezModeData.pActiveInClusterIDs = clusterIDs;
          ezModeData.numActiveOutClusters = 0;   // active output cluster
          ezModeData.pActiveOutClusterIDs = NULL;
          zcl_InvokeEZMode( &ezModeData );
    
    #ifdef LCD_SUPPORTED
          HalLcdWriteString( "EZMode", HAL_LCD_LINE_2 );
    #endif
    
          // NOT ZCL_EZMODE, Use EndDeviceBind
    #else
          {
            zAddrType_t dstAddr;
            dstAddr.addrMode = Addr16Bit;
            dstAddr.addr.shortAddr = 0;   // Coordinator makes the EDB match
    
            // Initiate an End Device Bind Request, this bind request will
            // only use a cluster list that is important to binding.
            HalLedSet ( HAL_LED_4, HAL_LED_MODE_OFF );
            ZDP_EndDeviceBindReq( &dstAddr, NLME_GetShortAddr(),
                                  SAMPLETEMPERATURESENSOR_ENDPOINT,
                                  ZCL_HA_PROFILE_ID,
                                  0, NULL,
                                  ZCLSAMPLETEMPERATURESENSOR_BINDINGLIST, bindingOutClusters,
                                  FALSE );
          }
    #endif // ZCL_EZMODE
        }
      }
    
      if ( keys & HAL_KEY_SW_3 )
      {
        giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;
    
        // decrease the temperature
        if ( zclSampleTemperatureSensor_MeasuredValue > zclSampleTemperatureSensor_MinMeasuredValue )
        {
          zclSampleTemperatureSensor_MeasuredValue = zclSampleTemperatureSensor_MeasuredValue - 100;  // considering using whole number value
        }
        else if ( zclSampleTemperatureSensor_MeasuredValue >= zclSampleTemperatureSensor_MinMeasuredValue )
        {
          zclSampleTemperatureSensor_MeasuredValue = zclSampleTemperatureSensor_MinMeasuredValue;
        }
    
        // Send temperature information
        zclSampleTemperatureSensor_SendTemp();
      }
    
      if ( keys & HAL_KEY_SW_4 )
      {
        giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;
    
        if ( ( zclSampleTemperatureSensor_NwkState == DEV_ZB_COORD ) ||
             ( zclSampleTemperatureSensor_NwkState == DEV_ROUTER ) )
        {
          // toggle permit join
          gPermitDuration = gPermitDuration ? 0 : 0xff;
          NLME_PermitJoiningRequest( gPermitDuration );
        }
      }
    
      if ( shift && ( keys & HAL_KEY_SW_5 ) )
      {
        zclSampleTemperatureSensor_BasicResetCB();
      }
      else if ( keys & HAL_KEY_SW_5 )
      {
        if ( giTemperatureSensorScreenMode == TEMPSENSE_MAINMODE )
        {
          giTemperatureSensorScreenMode = TEMPSENSE_HELPMODE;
        }
        else if ( giTemperatureSensorScreenMode == TEMPSENSE_HELPMODE )
        {
    #ifdef LCD_SUPPORTED
          HalLcdWriteString( (char *)sClearLine, HAL_LCD_LINE_2 );
    #endif
          giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;
        }
      }
    
      // update display
      zclSampleTemperatureSensor_LcdDisplayUpdate();
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_LcdDisplayUpdate
     *
     * @brief   Called to update the LCD display.
     *
     * @param   none
     *
     * @return  none
     */
    void zclSampleTemperatureSensor_LcdDisplayUpdate( void )
    {
      // turn on red LED for temperatures >= 24.00C
      if ( zclSampleTemperatureSensor_MeasuredValue >= 2400 )
      {
        HalLedSet ( HAL_LED_1, HAL_LED_MODE_OFF );
        HalLedSet ( HAL_LED_2, HAL_LED_MODE_ON );
      }
      // turn on green LED for temperatures <= 20.00C
      else if ( zclSampleTemperatureSensor_MeasuredValue <= 2000 )
      {
        HalLedSet ( HAL_LED_1, HAL_LED_MODE_ON );
        HalLedSet ( HAL_LED_2, HAL_LED_MODE_OFF );
      }
      // turn on both red and green LEDs for temperatures between 20.00C and 24.00C
      else
      {
        HalLedSet ( HAL_LED_1, HAL_LED_MODE_ON );
        HalLedSet ( HAL_LED_2, HAL_LED_MODE_ON );
      }
    
      if ( giTemperatureSensorScreenMode == TEMPSENSE_HELPMODE )
      {
        zclSampleTemperatureSensor_LcdDisplayHelpMode();
      }
      else
      {
        zclSampleTemperatureSensor_LcdDisplayMainMode();
      }
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_LcdDisplayMainMode
     *
     * @brief   Called to display the main screen on the LCD.
     *
     * @param   none
     *
     * @return  none
     */
    void zclSampleTemperatureSensor_LcdDisplayMainMode( void )
    {
      char sDisplayTemp[16];
    
      if ( zclSampleTemperatureSensor_NwkState == DEV_ZB_COORD )
      {
        zclHA_LcdStatusLine1( 0 );
      }
      else if ( zclSampleTemperatureSensor_NwkState == DEV_ROUTER )
      {
        zclHA_LcdStatusLine1( 1 );
      }
      else if ( zclSampleTemperatureSensor_NwkState == DEV_END_DEVICE )
      {
        zclHA_LcdStatusLine1( 2 );
      }
    
      // display current temperature
      osal_memcpy(sDisplayTemp, "TEMP: ", 6);
      _ltoa( ( zclSampleTemperatureSensor_MeasuredValue / 100 ), (void *)(&sDisplayTemp[6]), 10 );   // convert temperature to whole number
      osal_memcpy( &sDisplayTemp[8], "C", 2 );
    #ifdef LCD_SUPPORTED
      HalLcdWriteString( (char *)sDisplayTemp, HAL_LCD_LINE_2 );
    #endif
    
    #ifdef LCD_SUPPORTED
      if ( ( zclSampleTemperatureSensor_NwkState == DEV_ZB_COORD ) ||
           ( zclSampleTemperatureSensor_NwkState == DEV_ROUTER ) )
      {
        // display help key with permit join status
        if ( gPermitDuration )
        {
          HalLcdWriteString( "SW5: Help      *", HAL_LCD_LINE_3 );
        }
        else
        {
          HalLcdWriteString( "SW5: Help       ", HAL_LCD_LINE_3 );
        }
      }
      else
      {
        // display help key
        HalLcdWriteString( (char *)sSwHelp, HAL_LCD_LINE_3 );
      }
    #endif
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_LcdDisplayHelpMode
     *
     * @brief   Called to display the SW options on the LCD.
     *
     * @param   none
     *
     * @return  none
     */
    void zclSampleTemperatureSensor_LcdDisplayHelpMode( void )
    {
    #ifdef LCD_SUPPORTED
      HalLcdWriteString( (char *)sSwTempUp, HAL_LCD_LINE_1 );
      HalLcdWriteString( (char *)sSwEZMode, HAL_LCD_LINE_2 );
      HalLcdWriteString( (char *)sSwTempDown, HAL_LCD_LINE_3 );
    #endif
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_SendTemp
     *
     * @brief   Called to send current temperature information to the thermostat
     *
     * @param   none
     *
     * @return  none
     */
    static void zclSampleTemperatureSensor_SendTemp( void )
    {
    #ifdef ZCL_REPORT
      zclReportCmd_t *pReportCmd;
     // zclSampleTemperatureSensor_MeasuredValue = getTemperature();
      pReportCmd = osal_mem_alloc( sizeof(zclReportCmd_t) + sizeof(zclReport_t) );
      if ( pReportCmd != NULL )
      {
        pReportCmd->numAttr = 1;
        pReportCmd->attrList[0].attrID = ATTRID_MS_TEMPERATURE_MEASURED_VALUE;
        pReportCmd->attrList[0].dataType = ZCL_DATATYPE_INT16;
        pReportCmd->attrList[0].attrData = (void *)(&zclSampleTemperatureSensor_MeasuredValue);
    
        zcl_SendReportCmd( SAMPLETEMPERATURESENSOR_ENDPOINT, &zclSampleTemperatureSensor_DstAddr,
                           ZCL_CLUSTER_ID_MS_TEMPERATURE_MEASUREMENT,
                           pReportCmd, ZCL_FRAME_SERVER_CLIENT_DIR, TRUE, zclSampleTemperatureSensorSeqNum++ );
      }
    
      osal_mem_free( pReportCmd );
    #endif  // ZCL_REPORT
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_ProcessIdentifyTimeChange
     *
     * @brief   Called to process any change to the IdentifyTime attribute.
     *
     * @param   none
     *
     * @return  none
     */
    static void zclSampleTemperatureSensor_ProcessIdentifyTimeChange( void )
    {
      if ( zclSampleTemperatureSensor_IdentifyTime > 0 )
      {
        osal_start_timerEx( zclSampleTemperatureSensor_TaskID, SAMPLETEMPERATURESENSOR_IDENTIFY_TIMEOUT_EVT, 1000 );
        HalLedBlink ( HAL_LED_4, 0xFF, HAL_LED_DEFAULT_DUTY_CYCLE, HAL_LED_DEFAULT_FLASH_TIME );
      }
      else
      {
        if ( zclSampleTemperatureSensor_OnOff )
        {
          HalLedSet ( HAL_LED_4, HAL_LED_MODE_ON );
        }
        else
        {
          HalLedSet ( HAL_LED_4, HAL_LED_MODE_OFF );
        }
    
        osal_stop_timerEx( zclSampleTemperatureSensor_TaskID, SAMPLETEMPERATURESENSOR_IDENTIFY_TIMEOUT_EVT );
      }
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_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 zclSampleTemperatureSensor_BasicResetCB( void )
    {
      // Put device back to factory default settings
      zgWriteStartupOptions( ZG_STARTUP_SET, 3 );   // bit set both default configuration and default network
    
      // restart device
      MT_SysCommandProcessing( aProcessCmd );
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_IdentifyCB
     *
     * @brief   Callback from the ZCL General Cluster Library when
     *          it received an Identity Command for this application.
     *
     * @param   srcAddr - source address and endpoint of the response message
     * @param   identifyTime - the number of seconds to identify yourself
     *
     * @return  none
     */
    static void zclSampleTemperatureSensor_IdentifyCB( zclIdentify_t *pCmd )
    {
      zclSampleTemperatureSensor_IdentifyTime = pCmd->identifyTime;
      zclSampleTemperatureSensor_ProcessIdentifyTimeChange();
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_IdentifyQueryRspCB
     *
     * @brief   Callback from the ZCL General Cluster Library when
     *          it received an Identity Query Response Command for this application.
     *
     * @param   srcAddr - requestor's address
     * @param   timeout - number of seconds to identify yourself (valid for query response)
     *
     * @return  none
     */
    static void zclSampleTemperatureSensor_IdentifyQueryRspCB( zclIdentifyQueryRsp_t *pRsp )
    {
      (void)pRsp;
    #ifdef ZCL_EZMODE
      {
        zclEZMode_ActionData_t data;
        data.pIdentifyQueryRsp = pRsp;
        zcl_EZModeAction ( EZMODE_ACTION_IDENTIFY_QUERY_RSP, &data );
      }
    #endif
    }
    
    /******************************************************************************
     *
     *  Functions for processing ZCL Foundation incoming Command/Response messages
     *
     *****************************************************************************/
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_ProcessIncomingMsg
     *
     * @brief   Process ZCL Foundation incoming message
     *
     * @param   pInMsg - pointer to the received message
     *
     * @return  none
     */
    static void zclSampleTemperatureSensor_ProcessIncomingMsg( zclIncomingMsg_t *pInMsg)
    {
      switch ( pInMsg->zclHdr.commandID )
      {
    #ifdef ZCL_READ
        case ZCL_CMD_READ_RSP:
          zclSampleTemperatureSensor_ProcessInReadRspCmd( pInMsg );
          break;
    #endif
    #ifdef ZCL_WRITE
        case ZCL_CMD_WRITE_RSP:
          zclSampleTemperatureSensor_ProcessInWriteRspCmd( pInMsg );
          break;
    #endif
    #ifdef ZCL_REPORT
        // See ZCL Test Applicaiton (zcl_testapp.c) for sample code on Attribute Reporting
        case ZCL_CMD_CONFIG_REPORT:
          //zclSampleTemperatureSensor_ProcessInConfigReportCmd( pInMsg );
          break;
    
        case ZCL_CMD_CONFIG_REPORT_RSP:
          //zclSampleTemperatureSensor_ProcessInConfigReportRspCmd( pInMsg );
          break;
    
        case ZCL_CMD_READ_REPORT_CFG:
          //zclSampleTemperatureSensor_ProcessInReadReportCfgCmd( pInMsg );
          break;
    
        case ZCL_CMD_READ_REPORT_CFG_RSP:
          //zclSampleTemperatureSensor_ProcessInReadReportCfgRspCmd( pInMsg );
          break;
    
        case ZCL_CMD_REPORT:
          //zclSampleTemperatureSensor_ProcessInReportCmd( pInMsg );
          break;
    #endif
        case ZCL_CMD_DEFAULT_RSP:
          zclSampleTemperatureSensor_ProcessInDefaultRspCmd( pInMsg );
          break;
    #ifdef ZCL_DISCOVER
        case ZCL_CMD_DISCOVER_CMDS_RECEIVED_RSP:
          zclSampleTemperatureSensor_ProcessInDiscCmdsRspCmd( pInMsg );
          break;
    
        case ZCL_CMD_DISCOVER_CMDS_GEN_RSP:
          zclSampleTemperatureSensor_ProcessInDiscCmdsRspCmd( pInMsg );
          break;
    
        case ZCL_CMD_DISCOVER_ATTRS_RSP:
          zclSampleTemperatureSensor_ProcessInDiscAttrsRspCmd( pInMsg );
          break;
    
        case ZCL_CMD_DISCOVER_ATTRS_EXT_RSP:
          zclSampleTemperatureSensor_ProcessInDiscAttrsExtRspCmd( pInMsg );
          break;
    #endif
        default:
          break;
      }
    
      if ( pInMsg->attrCmd )
      {
        osal_mem_free( pInMsg->attrCmd );
      }
    }
    
    #ifdef ZCL_READ
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_ProcessInReadRspCmd
     *
     * @brief   Process the "Profile" Read Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleTemperatureSensor_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      zclSampleTemperatureSensor_ProcessInWriteRspCmd
     *
     * @brief   Process the "Profile" Write Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleTemperatureSensor_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      zclSampleTemperatureSensor_ProcessInDefaultRspCmd
     *
     * @brief   Process the "Profile" Default Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleTemperatureSensor_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      zclSampleTemperatureSensor_ProcessInDiscCmdsRspCmd
     *
     * @brief   Process the Discover Commands Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleTemperatureSensor_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      zclSampleTemperatureSensor_ProcessInDiscAttrsRspCmd
     *
     * @brief   Process the "Profile" Discover Attributes Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleTemperatureSensor_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      zclSampleTemperatureSensor_ProcessInDiscAttrsExtRspCmd
     *
     * @brief   Process the "Profile" Discover Attributes Extended Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleTemperatureSensor_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
    
    #ifdef ZCL_EZMODE
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_ProcessZDOMsgs
     *
     * @brief   Called when this node receives a ZDO/ZDP response.
     *
     * @param   none
     *
     * @return  status
     */
    static void zclSampleTemperatureSensor_ProcessZDOMsgs( zdoIncomingMsg_t *pMsg )
    {
      zclEZMode_ActionData_t data;
      ZDO_MatchDescRsp_t *pMatchDescRsp;
    
      // Let EZ-Mode know of the Match Descriptor Response
      if ( pMsg->clusterID == Match_Desc_rsp )
      {
        pMatchDescRsp = ZDO_ParseEPListRsp( pMsg );
        data.pMatchDescRsp = pMatchDescRsp;
        zcl_EZModeAction( EZMODE_ACTION_MATCH_DESC_RSP, &data );
        osal_mem_free( pMatchDescRsp );
      }
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_EZModeCB
     *
     * @brief   The Application is informed of events. This can be used to show on the UI what is
    *           going on during EZ-Mode steering/finding/binding.
     *
     * @param   state - an
     *
     * @return  none
     */
    static void zclSampleTemperatureSensor_EZModeCB( zlcEZMode_State_t state, zclEZMode_CBData_t *pData )
    {
    #ifdef LCD_SUPPORTED
      char szLine[20];
      char *pStr;
      uint8 err;
    #endif
    
      // time to go into identify mode
      if ( state == EZMODE_STATE_IDENTIFYING )
      {
        zclSampleTemperatureSensor_IdentifyTime = ( EZMODE_TIME / 1000 );  // convert to seconds
        zclSampleTemperatureSensor_ProcessIdentifyTimeChange();
      }
    
      // autoclosing, show what happened (success, cancelled, etc...)
      if( state == EZMODE_STATE_AUTOCLOSE )
      {
    #ifdef LCD_SUPPORTED
        pStr = NULL;
        err = pData->sAutoClose.err;
        if ( err == EZMODE_ERR_SUCCESS )
        {
          pStr = "EZMode: Success";
        }
        else if ( err == EZMODE_ERR_NOMATCH )
        {
          pStr = "EZMode: NoMatch"; // not a match made in heaven
        }
        if ( pStr )
        {
          if ( giTemperatureSensorScreenMode == TEMPSENSE_MAINMODE )
          {
            HalLcdWriteString ( pStr, HAL_LCD_LINE_2 );
          }
        }
    #endif
      }
    
      // finished, either show DstAddr/EP, or nothing (depending on success or not)
      if( state == EZMODE_STATE_FINISH )
      {
        // turn off identify mode
        zclSampleTemperatureSensor_IdentifyTime = 0;
        zclSampleTemperatureSensor_ProcessIdentifyTimeChange();
    
    #ifdef LCD_SUPPORTED
        // if successful, inform user which nwkaddr/ep we bound to
        pStr = NULL;
        err = pData->sFinish.err;
        if( err == EZMODE_ERR_SUCCESS )
        {
          // "EZDst:1234 EP:34"
          osal_memcpy( szLine, "EZDst:", 6 );
          zclHA_uint16toa( pData->sFinish.nwkaddr, &szLine[6] );
          osal_memcpy( &szLine[10], " EP:", 4 );
          _ltoa( pData->sFinish.ep, (void *)(&szLine[14]), 16 );  // _ltoa NULL terminates
          pStr = szLine;
        }
        else if ( err == EZMODE_ERR_BAD_PARAMETER )
        {
          pStr = "EZMode: BadParm";
        }
        else if ( err == EZMODE_ERR_CANCELLED )
        {
          pStr = "EZMode: Cancel";
        }
        else
        {
          pStr = "EZMode: TimeOut";
        }
        if ( pStr )
        {
          if ( giTemperatureSensorScreenMode == TEMPSENSE_MAINMODE )
          {
            HalLcdWriteString ( pStr, HAL_LCD_LINE_2 );
          }
        }
    #endif  // LCD_SUPPORTED
    
        // show main UI screen 3 seconds after joining network
        osal_start_timerEx( zclSampleTemperatureSensor_TaskID, SAMPLETEMPERATURESENSOR_MAIN_SCREEN_EVT, 3000 );
    
        // report current temperature reading 15 seconds after joinging the network
        osal_start_timerEx( zclSampleTemperatureSensor_TaskID, SAMPLETEMPERATURESENSOR_TEMP_SEND_EVT, SAMPLETEMPERATURESENSOR_REPORT_INTERVAL );
      }
    }
    #endif // ZCL_EZMODE
    
    /****************************************************************************
    ****************************************************************************/
    
    
    

    i did it asu said but when i check using break point the value of aadc is not assigning to zclSampleTemperatureSensor_MeasuredValue


    if (events & SAMPLETEMPERATURESENSOR_ADC)
    {
    zclSampleTemperatureSensor_MeasuredValue = aadc ;
    osal_start_timerEx( zclSampleTemperatureSensor_TaskID, SAMPLETEMPERATURESENSOR_ADC, SAMPLETEMPERATURESENSOR_REPORT_INTERVAL );
    return ( events ^ SAMPLETEMPERATURESENSOR_ADC );
    }

  • If you set breakpoint at zclSampleTemperatureSensor_MeasuredValue = aadc ;, does the program stop there?

  • i think it s stopping there sir  !!! 

  • No, it doesn't stop on the breakpoint according to your snapshot. Do you join your sensorTemperature into Zigbee network? If not, the event won't be triggered.

  • ya i am not sure how to join sensortemperature to zigbee network sir !!!! i just added the adc pgm where the tempsensor will b enabled bcause i ma using internal temp seensor 1ly sir ...

    but in the last pgm i have not done any temp sensor connection i was just trying to get the value 30 printed on the lcd display sir 

    7144.zcl_sampletemperaturesensor.c
    /**************************************************************************************************
      Filename:       zcl_sampletemperaturesensor.c
      Revised:        $Date: 2013-10-18 11:49:27 -0700 (Fri, 18 Oct 2013) $
      Revision:       $Revision: 35718 $
    
      Description:    Zigbee Cluster Library - sample device application.
    
    
      Copyright 2013 Texas Instruments Incorporated. All rights reserved.
    
      IMPORTANT: Your use of this Software is limited to those specific rights
      granted under the terms of a software license agreement between the user
      who downloaded the software, his/her employer (which must be your employer)
      and Texas Instruments Incorporated (the "License").  You may not use this
      Software unless you agree to abide by the terms of the License. The License
      limits your use, and you acknowledge, that the Software may not be modified,
      copied or distributed unless embedded on a Texas Instruments microcontroller
      or used solely and exclusively in conjunction with a Texas Instruments radio
      frequency transceiver, which is integrated into your product.  Other than for
      the foregoing purpose, you may not use, reproduce, copy, prepare derivative
      works of, modify, distribute, perform, display or sell this Software and/or
      its documentation for any purpose.
    
      YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE
      PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,
      INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE,
      NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL
      TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT,
      NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER
      LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
      INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE
      OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT
      OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
      (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
    
      Should you have any questions regarding your right to use this Software,
      contact Texas Instruments Incorporated at www.TI.com.
    **************************************************************************************************/
    
    /*********************************************************************
      This device will act as a temperature sensor. It updates the current
      temperature on the thermostat when the user sends the desired
      temperature using SW1.
    
      SCREEN MODES
      ----------------------------------------
      Main:
        - SW1: Send current temperature
        - SW2: Invoke EZMode
        - SW3: Adjust temperature
        - SW5: Go to Help screen
    
      Temperature:
        - SW1: Increase temperature
        - SW3: Decrease temperature
        - SW5: Enter temperature
      ----------------------------------------
    *********************************************************************/
    
    /*********************************************************************
     * INCLUDES
     */
    #include "ZComDef.h"
    #include "OSAL.h"
    #include "AF.h"
    #include "ZDApp.h"
    #include "ZDObject.h"
    #include "ZDProfile.h"
    #include "MT_SYS.h"
    
    #include "zcl.h"
    #include "zcl_general.h"
    #include "zcl_ha.h"
    #include "zcl_ezmode.h"
    #include "zcl_ms.h"
    #include "hal_adc.h"
    
    #include "zcl_sampletemperaturesensor.h"
    
    #include "onboard.h"
    
    /* HAL */
    #include "hal_lcd.h"
    #include "hal_led.h"
    #include "hal_key.h"
    //#include "zcl_sampletemperaturesensor_data.c"
    
    /*********************************************************************
     * MACROS
     */
    
    // how often to report temperature
    #define SAMPLETEMPERATURESENSOR_REPORT_INTERVAL   10000
    
    /*********************************************************************
     * CONSTANTS
     */
    
    /*********************************************************************
     * TYPEDEFS
     */
    /*#ifdef __IAR_SYSTEMS_ICC__
    #define  CODE   __code
    #define  XDATA  __xdata
    */
    
    /*********************************************************************
     * GLOBAL VARIABLES
     */
    byte zclSampleTemperatureSensor_TaskID;
    
    uint8 zclSampleTemperatureSensorSeqNum;
    
    static byte gPermitDuration = 0x00;
    int16 zclSampleTemperatureSensor_MeasuredValue;
    /*void initTempSensor(void){
       DISABLE_ALL_INTERRUPTS();            //??????
       InitClock();                         //????????32M
       *((BYTE XDATA*) 0x624B) = 0x01;    //???????
       *((BYTE XDATA*) 0x61BD) = 0x01;    //???????ADC????
    }*/
    
    /*********************************************************************
     * GLOBAL FUNCTIONS
     */
    
    /*********************************************************************
     * LOCAL VARIABLES
     */
    afAddrType_t zclSampleTemperatureSensor_DstAddr;
    
    #ifdef ZCL_EZMODE
    static void zclSampleTemperatureSensor_ProcessZDOMsgs( zdoIncomingMsg_t *pMsg );
    static void zclSampleTemperatureSensor_EZModeCB( zlcEZMode_State_t state, zclEZMode_CBData_t *pData );
    
    static const zclEZMode_RegisterData_t zclSampleTemperatureSensor_RegisterEZModeData =
    {
      &zclSampleTemperatureSensor_TaskID,
      SAMPLETEMPERATURESENSOR_EZMODE_NEXTSTATE_EVT,
      SAMPLETEMPERATURESENSOR_EZMODE_TIMEOUT_EVT,
      &zclSampleTemperatureSensorSeqNum,
      zclSampleTemperatureSensor_EZModeCB
    };
    
    // NOT ZCL_EZMODE, Use EndDeviceBind
    #else
    
    static cId_t bindingOutClusters[] =
    {
      ZCL_CLUSTER_ID_MS_TEMPERATURE_MEASUREMENT
    };
    #define ZCLSAMPLETEMPERATURESENSOR_BINDINGLIST        1
    #endif
    
    devStates_t zclSampleTemperatureSensor_NwkState = DEV_INIT;
    
    uint8 giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;   // display main screen mode first
    
    static uint8 aProcessCmd[] = { 1, 0, 0, 0 }; // used for reset command, { length + cmd0 + cmd1 + data }
    
    // Test Endpoint to allow SYS_APP_MSGs
    static endPointDesc_t sampleTemperatureSensor_TestEp =
    {
      20,                                 // Test endpoint
      &zclSampleTemperatureSensor_TaskID,
      (SimpleDescriptionFormat_t *)NULL,  // No Simple description for this test endpoint
      (afNetworkLatencyReq_t)0            // No Network Latency req
    };
    
    
    /*********************************************************************
     * LOCAL FUNCTIONS
     */
    static void zclSampleTemperatureSensor_HandleKeys( byte shift, byte keys );
    static void zclSampleTemperatureSensor_BasicResetCB( void );
    static void zclSampleTemperatureSensor_IdentifyCB( zclIdentify_t *pCmd );
    static void zclSampleTemperatureSensor_IdentifyQueryRspCB( zclIdentifyQueryRsp_t *pRsp );
    static void zclSampleTemperatureSensor_ProcessIdentifyTimeChange( void );
    //void getTemperature(void);
    
    // app display functions
    void zclSampleTemperatureSensor_LcdDisplayUpdate(void);
    void zclSampleTemperatureSensor_LcdDisplayMainMode(void);
    void zclSampleTemperatureSensor_LcdDisplayTempMode(void);
    void zclSampleTemperatureSensor_LcdDisplayHelpMode(void);
    //int16 getTemperature(void);
    int16 aadc = 3000;
    
    static void zclSampleTemperatureSensor_SendTemp(void);
    
    // Functions to process ZCL Foundation incoming Command/Response messages
    static void zclSampleTemperatureSensor_ProcessIncomingMsg( zclIncomingMsg_t *msg );
    #ifdef ZCL_READ
    static uint8 zclSampleTemperatureSensor_ProcessInReadRspCmd( zclIncomingMsg_t *pInMsg );
    #endif
    #ifdef ZCL_WRITE
    static uint8 zclSampleTemperatureSensor_ProcessInWriteRspCmd( zclIncomingMsg_t *pInMsg );
    #endif
    static uint8 zclSampleTemperatureSensor_ProcessInDefaultRspCmd( zclIncomingMsg_t *pInMsg );
    #ifdef ZCL_DISCOVER
    static uint8 zclSampleTemperatureSensor_ProcessInDiscCmdsRspCmd( zclIncomingMsg_t *pInMsg );
    static uint8 zclSampleTemperatureSensor_ProcessInDiscAttrsRspCmd( zclIncomingMsg_t *pInMsg );
    static uint8 zclSampleTemperatureSensor_ProcessInDiscAttrsExtRspCmd( zclIncomingMsg_t *pInMsg );
    #endif // ZCL_DISCOVER
    
    /*********************************************************************
     * STATUS STRINGS
     */
    #ifdef LCD_SUPPORTED
    const char sClearLine[]    = " ";
    const char sDeviceName[]   = "  Temp Sensor";
    const char sSwTempUp[]     = "SW1: Raise Temp";
    const char sSwEZMode[]     = "SW2: EZ-Mode";
    const char sSwTempDown[]   = "SW3: Lower Temp";
    const char sSwHelp[]       = "SW5: Help";
    #endif
    
    /*********************************************************************
     * ZCL General Profile Callback table
     */
    static zclGeneral_AppCallbacks_t zclSampleTemperatureSensor_CmdCallbacks =
    {
      zclSampleTemperatureSensor_BasicResetCB,        // Basic Cluster Reset command
      zclSampleTemperatureSensor_IdentifyCB,          // Identify command
    #ifdef ZCL_EZMODE
      NULL,                                           // Identify EZ-Mode Invoke command
      NULL,                                           // Identify Update Commission State command
    #endif
      NULL,                                           // Identify Trigger Effect command
      zclSampleTemperatureSensor_IdentifyQueryRspCB,  // Identify Query Response command
      NULL,             				                      // On/Off cluster command
      NULL,                                           // On/Off cluster enhanced command Off with Effect
      NULL,                                           // On/Off cluster enhanced command On with Recall Global Scene
      NULL,                                           // On/Off cluster enhanced command On with Timed Off
    #ifdef ZCL_LEVEL_CTRL
      NULL,                                           // Level Control Move to Level command
      NULL,                                           // Level Control Move command
      NULL,                                           // Level Control Step command
      NULL,                                           // Level Control Stop command
    #endif
    #ifdef ZCL_GROUPS
      NULL,                                           // Group Response commands
    #endif
    #ifdef ZCL_SCENES
      NULL,                                           // Scene Store Request command
      NULL,                                           // Scene Recall Request command
      NULL,                                           // Scene Response command
    #endif
    #ifdef ZCL_ALARMS
      NULL,                                           // Alarm (Response) commands
    #endif
    #ifdef SE_UK_EXT
      NULL,                                           // Get Event Log command
      NULL,                                           // Publish Event Log command
    #endif
      NULL,                                           // RSSI Location command
      NULL                                            // RSSI Location Response command
    };
    
    /*********************************************************************
     * @fn          zclSampleTemperatureSensor_Init
     *
     * @brief       Initialization function for the zclGeneral layer.
     *
     * @param       none
     *
     * @return      none
     */
    void zclSampleTemperatureSensor_Init( byte task_id )
    {
      zclSampleTemperatureSensor_TaskID = task_id;
    
      // Set destination address to indirect
      zclSampleTemperatureSensor_DstAddr.addrMode = (afAddrMode_t)AddrNotPresent;
      zclSampleTemperatureSensor_DstAddr.endPoint = 0;
      zclSampleTemperatureSensor_DstAddr.addr.shortAddr = 0;
    
      // This app is part of the Home Automation Profile
      zclHA_Init( &zclSampleTemperatureSensor_SimpleDesc );
    
      // Register the ZCL General Cluster Library callback functions
      zclGeneral_RegisterCmdCallbacks( SAMPLETEMPERATURESENSOR_ENDPOINT, &zclSampleTemperatureSensor_CmdCallbacks );
    
      // Register the application's attribute list
      zcl_registerAttrList( SAMPLETEMPERATURESENSOR_ENDPOINT, SAMPLETEMPERATURESENSOR_MAX_ATTRIBUTES, zclSampleTemperatureSensor_Attrs );
    
      // Register the Application to receive the unprocessed Foundation command/response messages
      zcl_registerForMsg( zclSampleTemperatureSensor_TaskID );
    
    #ifdef ZCL_EZMODE
      // Register EZ-Mode
      zcl_RegisterEZMode( &zclSampleTemperatureSensor_RegisterEZModeData );
    
      // Register with the ZDO to receive Match Descriptor Responses
      ZDO_RegisterForZDOMsg(task_id, Match_Desc_rsp);
    #endif
    
      // Register for all key events - This app will handle all key events
      RegisterForKeys( zclSampleTemperatureSensor_TaskID );
    
      // Register for a test endpoint
      afRegister( &sampleTemperatureSensor_TestEp );
    
    #ifdef LCD_SUPPORTED
      // display the device name
      HalLcdWriteString( (char *)sDeviceName, HAL_LCD_LINE_3 );
    #endif
    }
    
    /*********************************************************************
     * @fn          zclSample_event_loop
     *
     * @brief       Event Loop Processor for zclGeneral.
     *
     * @param       none
     *
     * @return      none
     */
    uint16 zclSampleTemperatureSensor_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( zclSampleTemperatureSensor_TaskID )) )
        {
          switch ( MSGpkt->hdr.event )
          {
    #ifdef ZCL_EZMODE
            case ZDO_CB_MSG:
              zclSampleTemperatureSensor_ProcessZDOMsgs( (zdoIncomingMsg_t *)MSGpkt );
              break;
    #endif
    
            case ZCL_INCOMING_MSG:
              // Incoming ZCL Foundation command/response messages
              zclSampleTemperatureSensor_ProcessIncomingMsg( (zclIncomingMsg_t *)MSGpkt );
              break;
    
            case KEY_CHANGE:
              zclSampleTemperatureSensor_HandleKeys( ((keyChange_t *)MSGpkt)->state, ((keyChange_t *)MSGpkt)->keys );
              break;
    
            case ZDO_STATE_CHANGE:
              zclSampleTemperatureSensor_NwkState = (devStates_t)(MSGpkt->hdr.status);
    osal_start_timerEx( zclSampleTemperatureSensor_TaskID, SAMPLETEMPERATURESENSOR_ADC, SAMPLETEMPERATURESENSOR_REPORT_INTERVAL );
    
              // now on the network
              if ( (zclSampleTemperatureSensor_NwkState == DEV_ZB_COORD) ||
                   (zclSampleTemperatureSensor_NwkState == DEV_ROUTER)   ||
                   (zclSampleTemperatureSensor_NwkState == DEV_END_DEVICE) )
              {
    #ifndef HOLD_AUTO_START
                giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;
                zclSampleTemperatureSensor_LcdDisplayUpdate();
    #endif
    #ifdef ZCL_EZMODE
                zcl_EZModeAction( EZMODE_ACTION_NETWORK_STARTED, NULL );
    #endif // ZCL_EZMODE
              }
              break;
    
            default:
              break;
          }
    
          // Release the memory
          osal_msg_deallocate( (uint8 *)MSGpkt );
        }
    
        // return unprocessed events
        return (events ^ SYS_EVENT_MSG);
      }
    
      if ( events & SAMPLETEMPERATURESENSOR_IDENTIFY_TIMEOUT_EVT )
      {
        if ( zclSampleTemperatureSensor_IdentifyTime > 0 )
          zclSampleTemperatureSensor_IdentifyTime--;
        zclSampleTemperatureSensor_ProcessIdentifyTimeChange();
    
        return ( events ^ SAMPLETEMPERATURESENSOR_IDENTIFY_TIMEOUT_EVT );
      }
    
    #ifdef ZCL_EZMODE
      // going on to next state
      if ( events & SAMPLETEMPERATURESENSOR_EZMODE_NEXTSTATE_EVT )
      {
        zcl_EZModeAction ( EZMODE_ACTION_PROCESS, NULL );   // going on to next state
        return ( events ^ SAMPLETEMPERATURESENSOR_EZMODE_NEXTSTATE_EVT );
      }
    
      // the overall EZMode timer expired, so we timed out
      if ( events & SAMPLETEMPERATURESENSOR_EZMODE_TIMEOUT_EVT )
      {
        zcl_EZModeAction ( EZMODE_ACTION_TIMED_OUT, NULL ); // EZ-Mode timed out
        return ( events ^ SAMPLETEMPERATURESENSOR_EZMODE_TIMEOUT_EVT );
      }
    #endif // ZLC_EZMODE
    
      if ( events & SAMPLETEMPERATURESENSOR_MAIN_SCREEN_EVT )
      {
        giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;
        zclSampleTemperatureSensor_LcdDisplayUpdate();
    
        return ( events ^ SAMPLETEMPERATURESENSOR_MAIN_SCREEN_EVT );
      }
    
      if (events & SAMPLETEMPERATURESENSOR_ADC)
      {
        zclSampleTemperatureSensor_MeasuredValue = aadc ;
        osal_start_timerEx( zclSampleTemperatureSensor_TaskID, SAMPLETEMPERATURESENSOR_ADC, SAMPLETEMPERATURESENSOR_REPORT_INTERVAL );
        return ( events ^ SAMPLETEMPERATURESENSOR_ADC );
      }
    
      if ( events & SAMPLETEMPERATURESENSOR_TEMP_SEND_EVT )
      {
        zclSampleTemperatureSensor_SendTemp();
    
        // report current temperature reading every 10 seconds
        osal_start_timerEx( zclSampleTemperatureSensor_TaskID, SAMPLETEMPERATURESENSOR_TEMP_SEND_EVT, SAMPLETEMPERATURESENSOR_REPORT_INTERVAL );
    
        return ( events ^ SAMPLETEMPERATURESENSOR_TEMP_SEND_EVT );
      }
    
      // Discard unknown events
      return 0;
    }
    
    /*int16 getTemperature(void)
    {
      int16 AvgTemp;
      uint8   i;
      uint16  AdcValue;
      uint16  value;
      uint16  value1;
    //char i;
            //char TempValue[10]; 
            //initTempSensor(); 
      AdcValue = 0;
      for( i = 0; i < 4; i++ )
      {
    ADCIF = 0;
        ADCCON3 = (HAL_ADC_REF_125V | HAL_ADC_RESOLUTION_14 | HAL_ADC_CHN_TEMP);    // ??1.25V????,12????,AD??:?????
            while ( !ADCIF );                                              //?????ADC
                     
        value =  ADCL >> 2;                        
        value |= (((UINT16)ADCH) << 6);    
        AdcValue += value;                          
      }
      value = AdcValue >> 2;                        //????4,?????
      value1 =   (((value) >> 4) - 335);
                 //??AD?,????????
      //while(1)
           // {
              AvgTemp = 0;
              for(i = 0 ; i < 64 ; i++)
              {
                AvgTemp += value1;              
                AvgTemp >>= 1;                              //??????2.
              }
              
           // }
      //zclSampleTemperatureSensor_MeasuredValue = AvgTemp ; 
      return AvgTemp;
    // * @param   keys - bit field for key events. Valid entries:
     //*                 HAL_KEY_SW_5
     //*                 HAL_KEY_SW_4
     //*                 HAL_KEY_SW_3
    }*/
    
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_HandleKeys
     *
     * @brief   Handles all key events for this device.
     *
     * @param   shift - true if in shift/alt.
     *                 HAL_KEY_SW_2
     *                 HAL_KEY_SW_1
     *
     * @return  none
     */
    static void zclSampleTemperatureSensor_HandleKeys( byte shift, byte keys )
    {
      if ( keys & HAL_KEY_SW_1 )
      {
        // increase temperature
        giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;
    
        if ( zclSampleTemperatureSensor_MeasuredValue < zclSampleTemperatureSensor_MaxMeasuredValue )
        {
          zclSampleTemperatureSensor_MeasuredValue = zclSampleTemperatureSensor_MeasuredValue + 100;  // considering using whole number value
        }
        else if ( zclSampleTemperatureSensor_MeasuredValue >= zclSampleTemperatureSensor_MaxMeasuredValue )
        {
          zclSampleTemperatureSensor_MeasuredValue = zclSampleTemperatureSensor_MaxMeasuredValue;
        }
    
        // Send temperature information
        zclSampleTemperatureSensor_SendTemp();
      }
    
      if ( keys & HAL_KEY_SW_2 )
      {
        if ( ( giTemperatureSensorScreenMode == TEMPSENSE_MAINMODE ) ||
            ( giTemperatureSensorScreenMode == TEMPSENSE_HELPMODE ) )
        {
          giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;
    
    #ifdef ZCL_EZMODE
          zclEZMode_InvokeData_t ezModeData;
          static uint16 clusterIDs[] = { ZCL_CLUSTER_ID_MS_TEMPERATURE_MEASUREMENT };   // only bind on the Temperature Measurement cluster
    
          // Invoke EZ-Mode
          ezModeData.endpoint = SAMPLETEMPERATURESENSOR_ENDPOINT; // endpoint on which to invoke EZ-Mode
          if ( ( zclSampleTemperatureSensor_NwkState == DEV_ZB_COORD ) ||
               ( zclSampleTemperatureSensor_NwkState == DEV_ROUTER )   ||
               ( zclSampleTemperatureSensor_NwkState == DEV_END_DEVICE ) )
          {
            ezModeData.onNetwork = TRUE;      // node is already on the network
          }
          else
          {
            ezModeData.onNetwork = FALSE;     // node is not yet on the network
          }
          ezModeData.initiator = TRUE;        // Temperature Sensor is an initiator
          ezModeData.numActiveInClusters = 1;
          ezModeData.pActiveInClusterIDs = clusterIDs;
          ezModeData.numActiveOutClusters = 0;   // active output cluster
          ezModeData.pActiveOutClusterIDs = NULL;
          zcl_InvokeEZMode( &ezModeData );
    
    #ifdef LCD_SUPPORTED
          HalLcdWriteString( "EZMode", HAL_LCD_LINE_2 );
    #endif
    
          // NOT ZCL_EZMODE, Use EndDeviceBind
    #else
          {
            zAddrType_t dstAddr;
            dstAddr.addrMode = Addr16Bit;
            dstAddr.addr.shortAddr = 0;   // Coordinator makes the EDB match
    
            // Initiate an End Device Bind Request, this bind request will
            // only use a cluster list that is important to binding.
            HalLedSet ( HAL_LED_4, HAL_LED_MODE_OFF );
            ZDP_EndDeviceBindReq( &dstAddr, NLME_GetShortAddr(),
                                  SAMPLETEMPERATURESENSOR_ENDPOINT,
                                  ZCL_HA_PROFILE_ID,
                                  0, NULL,
                                  ZCLSAMPLETEMPERATURESENSOR_BINDINGLIST, bindingOutClusters,
                                  FALSE );
          }
    #endif // ZCL_EZMODE
        }
      }
    
      if ( keys & HAL_KEY_SW_3 )
      {
        giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;
    
        // decrease the temperature
        if ( zclSampleTemperatureSensor_MeasuredValue > zclSampleTemperatureSensor_MinMeasuredValue )
        {
          zclSampleTemperatureSensor_MeasuredValue = zclSampleTemperatureSensor_MeasuredValue - 100;  // considering using whole number value
        }
        else if ( zclSampleTemperatureSensor_MeasuredValue >= zclSampleTemperatureSensor_MinMeasuredValue )
        {
          zclSampleTemperatureSensor_MeasuredValue = zclSampleTemperatureSensor_MinMeasuredValue;
        }
    
        // Send temperature information
        zclSampleTemperatureSensor_SendTemp();
      }
    
      if ( keys & HAL_KEY_SW_4 )
      {
        giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;
    
        if ( ( zclSampleTemperatureSensor_NwkState == DEV_ZB_COORD ) ||
             ( zclSampleTemperatureSensor_NwkState == DEV_ROUTER ) )
        {
          // toggle permit join
          gPermitDuration = gPermitDuration ? 0 : 0xff;
          NLME_PermitJoiningRequest( gPermitDuration );
        }
      }
    
      if ( shift && ( keys & HAL_KEY_SW_5 ) )
      {
        zclSampleTemperatureSensor_BasicResetCB();
      }
      else if ( keys & HAL_KEY_SW_5 )
      {
        if ( giTemperatureSensorScreenMode == TEMPSENSE_MAINMODE )
        {
          giTemperatureSensorScreenMode = TEMPSENSE_HELPMODE;
        }
        else if ( giTemperatureSensorScreenMode == TEMPSENSE_HELPMODE )
        {
    #ifdef LCD_SUPPORTED
          HalLcdWriteString( (char *)sClearLine, HAL_LCD_LINE_2 );
    #endif
          giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;
        }
      }
    
      // update display
      zclSampleTemperatureSensor_LcdDisplayUpdate();
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_LcdDisplayUpdate
     *
     * @brief   Called to update the LCD display.
     *
     * @param   none
     *
     * @return  none
     */
    void zclSampleTemperatureSensor_LcdDisplayUpdate( void )
    {
      // turn on red LED for temperatures >= 24.00C
      if ( zclSampleTemperatureSensor_MeasuredValue >= 2400 )
      {
        HalLedSet ( HAL_LED_1, HAL_LED_MODE_OFF );
        HalLedSet ( HAL_LED_2, HAL_LED_MODE_ON );
      }
      // turn on green LED for temperatures <= 20.00C
      else if ( zclSampleTemperatureSensor_MeasuredValue <= 2000 )
      {
        HalLedSet ( HAL_LED_1, HAL_LED_MODE_ON );
        HalLedSet ( HAL_LED_2, HAL_LED_MODE_OFF );
      }
      // turn on both red and green LEDs for temperatures between 20.00C and 24.00C
      else
      {
        HalLedSet ( HAL_LED_1, HAL_LED_MODE_ON );
        HalLedSet ( HAL_LED_2, HAL_LED_MODE_ON );
      }
    
      if ( giTemperatureSensorScreenMode == TEMPSENSE_HELPMODE )
      {
        zclSampleTemperatureSensor_LcdDisplayHelpMode();
      }
      else
      {
        zclSampleTemperatureSensor_LcdDisplayMainMode();
      }
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_LcdDisplayMainMode
     *
     * @brief   Called to display the main screen on the LCD.
     *
     * @param   none
     *
     * @return  none
     */
    void zclSampleTemperatureSensor_LcdDisplayMainMode( void )
    {
      char sDisplayTemp[16];
    
      if ( zclSampleTemperatureSensor_NwkState == DEV_ZB_COORD )
      {
        zclHA_LcdStatusLine1( 0 );
      }
      else if ( zclSampleTemperatureSensor_NwkState == DEV_ROUTER )
      {
        zclHA_LcdStatusLine1( 1 );
      }
      else if ( zclSampleTemperatureSensor_NwkState == DEV_END_DEVICE )
      {
        zclHA_LcdStatusLine1( 2 );
      }
    
      // display current temperature
      osal_memcpy(sDisplayTemp, "TEMP: ", 6);
      _ltoa( ( zclSampleTemperatureSensor_MeasuredValue / 100 ), (void *)(&sDisplayTemp[6]), 10 );   // convert temperature to whole number
      osal_memcpy( &sDisplayTemp[8], "C", 2 );
    #ifdef LCD_SUPPORTED
      HalLcdWriteString( (char *)sDisplayTemp, HAL_LCD_LINE_2 );
    #endif
    
    #ifdef LCD_SUPPORTED
      if ( ( zclSampleTemperatureSensor_NwkState == DEV_ZB_COORD ) ||
           ( zclSampleTemperatureSensor_NwkState == DEV_ROUTER ) )
      {
        // display help key with permit join status
        if ( gPermitDuration )
        {
          HalLcdWriteString( "SW5: Help      *", HAL_LCD_LINE_3 );
        }
        else
        {
          HalLcdWriteString( "SW5: Help       ", HAL_LCD_LINE_3 );
        }
      }
      else
      {
        // display help key
        HalLcdWriteString( (char *)sSwHelp, HAL_LCD_LINE_3 );
      }
    #endif
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_LcdDisplayHelpMode
     *
     * @brief   Called to display the SW options on the LCD.
     *
     * @param   none
     *
     * @return  none
     */
    void zclSampleTemperatureSensor_LcdDisplayHelpMode( void )
    {
    #ifdef LCD_SUPPORTED
      HalLcdWriteString( (char *)sSwTempUp, HAL_LCD_LINE_1 );
      HalLcdWriteString( (char *)sSwEZMode, HAL_LCD_LINE_2 );
      HalLcdWriteString( (char *)sSwTempDown, HAL_LCD_LINE_3 );
    #endif
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_SendTemp
     *
     * @brief   Called to send current temperature information to the thermostat
     *
     * @param   none
     *
     * @return  none
     */
    static void zclSampleTemperatureSensor_SendTemp( void )
    {
    #ifdef ZCL_REPORT
      zclReportCmd_t *pReportCmd;
     // zclSampleTemperatureSensor_MeasuredValue = getTemperature();
      pReportCmd = osal_mem_alloc( sizeof(zclReportCmd_t) + sizeof(zclReport_t) );
      if ( pReportCmd != NULL )
      {
        pReportCmd->numAttr = 1;
        pReportCmd->attrList[0].attrID = ATTRID_MS_TEMPERATURE_MEASURED_VALUE;
        pReportCmd->attrList[0].dataType = ZCL_DATATYPE_INT16;
        pReportCmd->attrList[0].attrData = (void *)(&zclSampleTemperatureSensor_MeasuredValue);
    
        zcl_SendReportCmd( SAMPLETEMPERATURESENSOR_ENDPOINT, &zclSampleTemperatureSensor_DstAddr,
                           ZCL_CLUSTER_ID_MS_TEMPERATURE_MEASUREMENT,
                           pReportCmd, ZCL_FRAME_SERVER_CLIENT_DIR, TRUE, zclSampleTemperatureSensorSeqNum++ );
      }
    
      osal_mem_free( pReportCmd );
    #endif  // ZCL_REPORT
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_ProcessIdentifyTimeChange
     *
     * @brief   Called to process any change to the IdentifyTime attribute.
     *
     * @param   none
     *
     * @return  none
     */
    static void zclSampleTemperatureSensor_ProcessIdentifyTimeChange( void )
    {
      if ( zclSampleTemperatureSensor_IdentifyTime > 0 )
      {
        osal_start_timerEx( zclSampleTemperatureSensor_TaskID, SAMPLETEMPERATURESENSOR_IDENTIFY_TIMEOUT_EVT, 1000 );
        HalLedBlink ( HAL_LED_4, 0xFF, HAL_LED_DEFAULT_DUTY_CYCLE, HAL_LED_DEFAULT_FLASH_TIME );
      }
      else
      {
        if ( zclSampleTemperatureSensor_OnOff )
        {
          HalLedSet ( HAL_LED_4, HAL_LED_MODE_ON );
        }
        else
        {
          HalLedSet ( HAL_LED_4, HAL_LED_MODE_OFF );
        }
    
        osal_stop_timerEx( zclSampleTemperatureSensor_TaskID, SAMPLETEMPERATURESENSOR_IDENTIFY_TIMEOUT_EVT );
      }
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_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 zclSampleTemperatureSensor_BasicResetCB( void )
    {
      // Put device back to factory default settings
      zgWriteStartupOptions( ZG_STARTUP_SET, 3 );   // bit set both default configuration and default network
    
      // restart device
      MT_SysCommandProcessing( aProcessCmd );
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_IdentifyCB
     *
     * @brief   Callback from the ZCL General Cluster Library when
     *          it received an Identity Command for this application.
     *
     * @param   srcAddr - source address and endpoint of the response message
     * @param   identifyTime - the number of seconds to identify yourself
     *
     * @return  none
     */
    static void zclSampleTemperatureSensor_IdentifyCB( zclIdentify_t *pCmd )
    {
      zclSampleTemperatureSensor_IdentifyTime = pCmd->identifyTime;
      zclSampleTemperatureSensor_ProcessIdentifyTimeChange();
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_IdentifyQueryRspCB
     *
     * @brief   Callback from the ZCL General Cluster Library when
     *          it received an Identity Query Response Command for this application.
     *
     * @param   srcAddr - requestor's address
     * @param   timeout - number of seconds to identify yourself (valid for query response)
     *
     * @return  none
     */
    static void zclSampleTemperatureSensor_IdentifyQueryRspCB( zclIdentifyQueryRsp_t *pRsp )
    {
      (void)pRsp;
    #ifdef ZCL_EZMODE
      {
        zclEZMode_ActionData_t data;
        data.pIdentifyQueryRsp = pRsp;
        zcl_EZModeAction ( EZMODE_ACTION_IDENTIFY_QUERY_RSP, &data );
      }
    #endif
    }
    
    /******************************************************************************
     *
     *  Functions for processing ZCL Foundation incoming Command/Response messages
     *
     *****************************************************************************/
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_ProcessIncomingMsg
     *
     * @brief   Process ZCL Foundation incoming message
     *
     * @param   pInMsg - pointer to the received message
     *
     * @return  none
     */
    static void zclSampleTemperatureSensor_ProcessIncomingMsg( zclIncomingMsg_t *pInMsg)
    {
      switch ( pInMsg->zclHdr.commandID )
      {
    #ifdef ZCL_READ
        case ZCL_CMD_READ_RSP:
          zclSampleTemperatureSensor_ProcessInReadRspCmd( pInMsg );
          break;
    #endif
    #ifdef ZCL_WRITE
        case ZCL_CMD_WRITE_RSP:
          zclSampleTemperatureSensor_ProcessInWriteRspCmd( pInMsg );
          break;
    #endif
    #ifdef ZCL_REPORT
        // See ZCL Test Applicaiton (zcl_testapp.c) for sample code on Attribute Reporting
        case ZCL_CMD_CONFIG_REPORT:
          //zclSampleTemperatureSensor_ProcessInConfigReportCmd( pInMsg );
          break;
    
        case ZCL_CMD_CONFIG_REPORT_RSP:
          //zclSampleTemperatureSensor_ProcessInConfigReportRspCmd( pInMsg );
          break;
    
        case ZCL_CMD_READ_REPORT_CFG:
          //zclSampleTemperatureSensor_ProcessInReadReportCfgCmd( pInMsg );
          break;
    
        case ZCL_CMD_READ_REPORT_CFG_RSP:
          //zclSampleTemperatureSensor_ProcessInReadReportCfgRspCmd( pInMsg );
          break;
    
        case ZCL_CMD_REPORT:
          //zclSampleTemperatureSensor_ProcessInReportCmd( pInMsg );
          break;
    #endif
        case ZCL_CMD_DEFAULT_RSP:
          zclSampleTemperatureSensor_ProcessInDefaultRspCmd( pInMsg );
          break;
    #ifdef ZCL_DISCOVER
        case ZCL_CMD_DISCOVER_CMDS_RECEIVED_RSP:
          zclSampleTemperatureSensor_ProcessInDiscCmdsRspCmd( pInMsg );
          break;
    
        case ZCL_CMD_DISCOVER_CMDS_GEN_RSP:
          zclSampleTemperatureSensor_ProcessInDiscCmdsRspCmd( pInMsg );
          break;
    
        case ZCL_CMD_DISCOVER_ATTRS_RSP:
          zclSampleTemperatureSensor_ProcessInDiscAttrsRspCmd( pInMsg );
          break;
    
        case ZCL_CMD_DISCOVER_ATTRS_EXT_RSP:
          zclSampleTemperatureSensor_ProcessInDiscAttrsExtRspCmd( pInMsg );
          break;
    #endif
        default:
          break;
      }
    
      if ( pInMsg->attrCmd )
      {
        osal_mem_free( pInMsg->attrCmd );
      }
    }
    
    #ifdef ZCL_READ
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_ProcessInReadRspCmd
     *
     * @brief   Process the "Profile" Read Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleTemperatureSensor_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      zclSampleTemperatureSensor_ProcessInWriteRspCmd
     *
     * @brief   Process the "Profile" Write Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleTemperatureSensor_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      zclSampleTemperatureSensor_ProcessInDefaultRspCmd
     *
     * @brief   Process the "Profile" Default Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleTemperatureSensor_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      zclSampleTemperatureSensor_ProcessInDiscCmdsRspCmd
     *
     * @brief   Process the Discover Commands Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleTemperatureSensor_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      zclSampleTemperatureSensor_ProcessInDiscAttrsRspCmd
     *
     * @brief   Process the "Profile" Discover Attributes Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleTemperatureSensor_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      zclSampleTemperatureSensor_ProcessInDiscAttrsExtRspCmd
     *
     * @brief   Process the "Profile" Discover Attributes Extended Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleTemperatureSensor_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
    
    #ifdef ZCL_EZMODE
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_ProcessZDOMsgs
     *
     * @brief   Called when this node receives a ZDO/ZDP response.
     *
     * @param   none
     *
     * @return  status
     */
    static void zclSampleTemperatureSensor_ProcessZDOMsgs( zdoIncomingMsg_t *pMsg )
    {
      zclEZMode_ActionData_t data;
      ZDO_MatchDescRsp_t *pMatchDescRsp;
    
      // Let EZ-Mode know of the Match Descriptor Response
      if ( pMsg->clusterID == Match_Desc_rsp )
      {
        pMatchDescRsp = ZDO_ParseEPListRsp( pMsg );
        data.pMatchDescRsp = pMatchDescRsp;
        zcl_EZModeAction( EZMODE_ACTION_MATCH_DESC_RSP, &data );
        osal_mem_free( pMatchDescRsp );
      }
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_EZModeCB
     *
     * @brief   The Application is informed of events. This can be used to show on the UI what is
    *           going on during EZ-Mode steering/finding/binding.
     *
     * @param   state - an
     *
     * @return  none
     */
    static void zclSampleTemperatureSensor_EZModeCB( zlcEZMode_State_t state, zclEZMode_CBData_t *pData )
    {
    #ifdef LCD_SUPPORTED
      char szLine[20];
      char *pStr;
      uint8 err;
    #endif
    
      // time to go into identify mode
      if ( state == EZMODE_STATE_IDENTIFYING )
      {
        zclSampleTemperatureSensor_IdentifyTime = ( EZMODE_TIME / 1000 );  // convert to seconds
        zclSampleTemperatureSensor_ProcessIdentifyTimeChange();
      }
    
      // autoclosing, show what happened (success, cancelled, etc...)
      if( state == EZMODE_STATE_AUTOCLOSE )
      {
    #ifdef LCD_SUPPORTED
        pStr = NULL;
        err = pData->sAutoClose.err;
        if ( err == EZMODE_ERR_SUCCESS )
        {
          pStr = "EZMode: Success";
        }
        else if ( err == EZMODE_ERR_NOMATCH )
        {
          pStr = "EZMode: NoMatch"; // not a match made in heaven
        }
        if ( pStr )
        {
          if ( giTemperatureSensorScreenMode == TEMPSENSE_MAINMODE )
          {
            HalLcdWriteString ( pStr, HAL_LCD_LINE_2 );
          }
        }
    #endif
      }
    
      // finished, either show DstAddr/EP, or nothing (depending on success or not)
      if( state == EZMODE_STATE_FINISH )
      {
        // turn off identify mode
        zclSampleTemperatureSensor_IdentifyTime = 0;
        zclSampleTemperatureSensor_ProcessIdentifyTimeChange();
    
    #ifdef LCD_SUPPORTED
        // if successful, inform user which nwkaddr/ep we bound to
        pStr = NULL;
        err = pData->sFinish.err;
        if( err == EZMODE_ERR_SUCCESS )
        {
          // "EZDst:1234 EP:34"
          osal_memcpy( szLine, "EZDst:", 6 );
          zclHA_uint16toa( pData->sFinish.nwkaddr, &szLine[6] );
          osal_memcpy( &szLine[10], " EP:", 4 );
          _ltoa( pData->sFinish.ep, (void *)(&szLine[14]), 16 );  // _ltoa NULL terminates
          pStr = szLine;
        }
        else if ( err == EZMODE_ERR_BAD_PARAMETER )
        {
          pStr = "EZMode: BadParm";
        }
        else if ( err == EZMODE_ERR_CANCELLED )
        {
          pStr = "EZMode: Cancel";
        }
        else
        {
          pStr = "EZMode: TimeOut";
        }
        if ( pStr )
        {
          if ( giTemperatureSensorScreenMode == TEMPSENSE_MAINMODE )
          {
            HalLcdWriteString ( pStr, HAL_LCD_LINE_2 );
          }
        }
    #endif  // LCD_SUPPORTED
    
        // show main UI screen 3 seconds after joining network
        osal_start_timerEx( zclSampleTemperatureSensor_TaskID, SAMPLETEMPERATURESENSOR_MAIN_SCREEN_EVT, 3000 );
    
        // report current temperature reading 15 seconds after joinging the network
        osal_start_timerEx( zclSampleTemperatureSensor_TaskID, SAMPLETEMPERATURESENSOR_TEMP_SEND_EVT, SAMPLETEMPERATURESENSOR_REPORT_INTERVAL );
      }
    }
    #endif // ZCL_EZMODE
    
    /****************************************************************************
    ****************************************************************************/
    
    
    

    i commented the temperature sensor part ...and just tried displaying 30 in the lcd ... pl go through it and reply asap

  • If you don't join sampletemperaturesensor to Zigbee network, you have to put "osal_start_timerEx( zclSampleTemperatureSensor_TaskID, SAMPLETEMPERATURESENSOR_ADC, SAMPLETEMPERATURESENSOR_REPORT_INTERVAL );" in zclSampleTemperatureSensor_Init.

  • sir what do u mean by joining sampletemperature sensor to zigbee network and i hav to put that osal function ???

    i didnt quitee get it ... bt nw i am trying just to print a value of 30 in the lct i am not gng n the sensor part sir ... and i intend to do that by adding 

    if (events & SAMPLETEMPERATURESENSOR_ADC)
    {
    zclSampleTemperatureSensor_MeasuredValue = aadc ;
    osal_start_timerEx( zclSampleTemperatureSensor_TaskID, SAMPLETEMPERATURESENSOR_ADC, SAMPLETEMPERATURESENSOR_REPORT_INTERVAL );
    return ( events ^ SAMPLETEMPERATURESENSOR_ADC );
    }

    in event and 

    i added 

    osal_start_timerEx( zclSampleTemperatureSensor_TaskID, SAMPLETEMPERATURESENSOR_ADC, SAMPLETEMPERATURESENSOR_REPORT_INTERVAL ); 

    in zdochange state case

  • any ideas sir ?? 

  • I told you to put "osal_start_timerEx( zclSampleTemperatureSensor_TaskID, SAMPLETEMPERATURESENSOR_ADC, SAMPLETEMPERATURESENSOR_REPORT_INTERVAL );" in zclSampleTemperatureSensor_Init().

  • i tried that to sir ... but the code is not displaying the value of aadc=30 in lcd ... where did i went wrong ??

    5466.OSAL_SampleTemperatureSensor.c
    /**************************************************************************************************
      Filename:       OSAL_SampleTemperatureSensor.c
      Revised:        $Date: 2013-04-22 14:49:05 -0700 (Mon, 22 Apr 2013) $
      Revision:       $Revision: 33994 $
    
      Description:    This file contains all the settings and other functions
                      that the user should set and change.
    
    
      Copyright 2013 Texas Instruments Incorporated. All rights reserved.
    
      IMPORTANT: Your use of this Software is limited to those specific rights
      granted under the terms of a software license agreement between the user
      who downloaded the software, his/her employer (which must be your employer)
      and Texas Instruments Incorporated (the "License").  You may not use this
      Software unless you agree to abide by the terms of the License. The License
      limits your use, and you acknowledge, that the Software may not be modified,
      copied or distributed unless embedded on a Texas Instruments microcontroller
      or used solely and exclusively in conjunction with a Texas Instruments radio
      frequency transceiver, which is integrated into your product.  Other than for
      the foregoing purpose, you may not use, reproduce, copy, prepare derivative
      works of, modify, distribute, perform, display or sell this Software and/or
      its documentation for any purpose.
    
      YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE
      PROVIDED �AS IS� WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,
      INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE,
      NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL
      TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT,
      NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER
      LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
      INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE
      OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT
      OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
      (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
    
      Should you have any questions regarding your right to use this Software,
      contact Texas Instruments Incorporated at www.TI.com.
    **************************************************************************************************/
    
    /*********************************************************************
     * INCLUDES
     */
    
    #include "ZComDef.h"
    #include "hal_drivers.h"
    #include "OSAL.h"
    #include "OSAL_Tasks.h"
    
    #if defined ( MT_TASK )
      #include "MT.h"
      #include "MT_TASK.h"
    #endif
    
    #include "nwk.h"
    #include "APS.h"
    #include "ZDApp.h"
    #if defined ( ZIGBEE_FREQ_AGILITY ) || defined ( ZIGBEE_PANID_CONFLICT )
      #include "ZDNwkMgr.h"
    #endif
    #if defined ( ZIGBEE_FRAGMENTATION )
      #include "aps_frag.h"
    #endif
    
    #include "zcl_sampletemperaturesensor.h"
    
    /*********************************************************************
     * GLOBAL VARIABLES
     */
    
    // The order in this table must be identical to the task initialization calls below in osalInitTask.
    const pTaskEventHandlerFn tasksArr[] = {
      macEventLoop,
      nwk_event_loop,
      Hal_ProcessEvent,
    #if defined( MT_TASK )
      MT_ProcessEvent,
    #endif
      APS_event_loop,
    #if defined ( ZIGBEE_FRAGMENTATION )
      APSF_ProcessEvent,
    #endif
      ZDApp_event_loop,
    #if defined ( ZIGBEE_FREQ_AGILITY ) || defined ( ZIGBEE_PANID_CONFLICT )
      ZDNwkMgr_event_loop,
    #endif
      zcl_event_loop,
      zclSampleTemperatureSensor_event_loop
    };
    
    const uint8 tasksCnt = sizeof( tasksArr ) / sizeof( tasksArr[0] );
    uint16 *tasksEvents;
    
    /*********************************************************************
     * FUNCTIONS
     *********************************************************************/
    
    /*********************************************************************
     * @fn      osalInitTasks
     *
     * @brief   This function invokes the initialization function for each task.
     *
     * @param   void
     *
     * @return  none
     */
    void osalInitTasks( void )
    {
      uint8 taskID = 0;
    
      tasksEvents = (uint16 *)osal_mem_alloc( sizeof( uint16 ) * tasksCnt);
      osal_memset( tasksEvents, 0, (sizeof( uint16 ) * tasksCnt));
    
      macTaskInit( taskID++ );
      nwk_init( taskID++ );
      Hal_Init( taskID++ );
    #if defined( MT_TASK )
      MT_TaskInit( taskID++ );
    #endif
      APS_Init( taskID++ );
    #if defined ( ZIGBEE_FRAGMENTATION )
      APSF_Init( taskID++ );
    #endif
      ZDApp_Init( taskID++ );
    #if defined ( ZIGBEE_FREQ_AGILITY ) || defined ( ZIGBEE_PANID_CONFLICT )
      ZDNwkMgr_Init( taskID++ );
    #endif
      zcl_Init( taskID++ );
      zclSampleTemperatureSensor_Init( taskID );
    }
    
    /*********************************************************************
    *********************************************************************/
    

    4188.zcl_sampletemperaturesensor.c
    /**************************************************************************************************
      Filename:       zcl_sampletemperaturesensor.c
      Revised:        $Date: 2013-10-18 11:49:27 -0700 (Fri, 18 Oct 2013) $
      Revision:       $Revision: 35718 $
    
      Description:    Zigbee Cluster Library - sample device application.
    
    
      Copyright 2013 Texas Instruments Incorporated. All rights reserved.
    
      IMPORTANT: Your use of this Software is limited to those specific rights
      granted under the terms of a software license agreement between the user
      who downloaded the software, his/her employer (which must be your employer)
      and Texas Instruments Incorporated (the "License").  You may not use this
      Software unless you agree to abide by the terms of the License. The License
      limits your use, and you acknowledge, that the Software may not be modified,
      copied or distributed unless embedded on a Texas Instruments microcontroller
      or used solely and exclusively in conjunction with a Texas Instruments radio
      frequency transceiver, which is integrated into your product.  Other than for
      the foregoing purpose, you may not use, reproduce, copy, prepare derivative
      works of, modify, distribute, perform, display or sell this Software and/or
      its documentation for any purpose.
    
      YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE
      PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,
      INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE,
      NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL
      TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT,
      NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER
      LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
      INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE
      OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT
      OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
      (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
    
      Should you have any questions regarding your right to use this Software,
      contact Texas Instruments Incorporated at www.TI.com.
    **************************************************************************************************/
    
    /*********************************************************************
      This device will act as a temperature sensor. It updates the current
      temperature on the thermostat when the user sends the desired
      temperature using SW1.
    
      SCREEN MODES
      ----------------------------------------
      Main:
        - SW1: Send current temperature
        - SW2: Invoke EZMode
        - SW3: Adjust temperature
        - SW5: Go to Help screen
    
      Temperature:
        - SW1: Increase temperature
        - SW3: Decrease temperature
        - SW5: Enter temperature
      ----------------------------------------
    *********************************************************************/
    
    /*********************************************************************
     * INCLUDES
     */
    #include "ZComDef.h"
    #include "OSAL.h"
    #include "AF.h"
    #include "ZDApp.h"
    #include "ZDObject.h"
    #include "ZDProfile.h"
    #include "MT_SYS.h"
    
    #include "zcl.h"
    #include "zcl_general.h"
    #include "zcl_ha.h"
    #include "zcl_ezmode.h"
    #include "zcl_ms.h"
    #include "hal_adc.h"
    
    #include "zcl_sampletemperaturesensor.h"
    
    #include "onboard.h"
    
    /* HAL */
    #include "hal_lcd.h"
    #include "hal_led.h"
    #include "hal_key.h"
    //#include "zcl_sampletemperaturesensor_data.c"
    
    /*********************************************************************
     * MACROS
     */
    
    // how often to report temperature
    #define SAMPLETEMPERATURESENSOR_REPORT_INTERVAL   10000
    
    /*********************************************************************
     * CONSTANTS
     */
    
    /*********************************************************************
     * TYPEDEFS
     */
    /*#ifdef __IAR_SYSTEMS_ICC__
    #define  CODE   __code
    #define  XDATA  __xdata
    */
    
    /*********************************************************************
     * GLOBAL VARIABLES
     */
    byte zclSampleTemperatureSensor_TaskID;
    
    uint8 zclSampleTemperatureSensorSeqNum;
    
    static byte gPermitDuration = 0x00;
    int16 zclSampleTemperatureSensor_MeasuredValue;
    /*void initTempSensor(void){
       DISABLE_ALL_INTERRUPTS();            //??????
       InitClock();                         //????????32M
       *((BYTE XDATA*) 0x624B) = 0x01;    //???????
       *((BYTE XDATA*) 0x61BD) = 0x01;    //???????ADC????
    }*/
    
    /*********************************************************************
     * GLOBAL FUNCTIONS
     */
    
    /*********************************************************************
     * LOCAL VARIABLES
     */
    afAddrType_t zclSampleTemperatureSensor_DstAddr;
    
    #ifdef ZCL_EZMODE
    static void zclSampleTemperatureSensor_ProcessZDOMsgs( zdoIncomingMsg_t *pMsg );
    static void zclSampleTemperatureSensor_EZModeCB( zlcEZMode_State_t state, zclEZMode_CBData_t *pData );
    
    static const zclEZMode_RegisterData_t zclSampleTemperatureSensor_RegisterEZModeData =
    {
      &zclSampleTemperatureSensor_TaskID,
      SAMPLETEMPERATURESENSOR_EZMODE_NEXTSTATE_EVT,
      SAMPLETEMPERATURESENSOR_EZMODE_TIMEOUT_EVT,
      &zclSampleTemperatureSensorSeqNum,
      zclSampleTemperatureSensor_EZModeCB
    };
    
    // NOT ZCL_EZMODE, Use EndDeviceBind
    #else
    
    static cId_t bindingOutClusters[] =
    {
      ZCL_CLUSTER_ID_MS_TEMPERATURE_MEASUREMENT
    };
    #define ZCLSAMPLETEMPERATURESENSOR_BINDINGLIST        1
    #endif
    
    devStates_t zclSampleTemperatureSensor_NwkState = DEV_INIT;
    
    uint8 giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;   // display main screen mode first
    
    static uint8 aProcessCmd[] = { 1, 0, 0, 0 }; // used for reset command, { length + cmd0 + cmd1 + data }
    
    // Test Endpoint to allow SYS_APP_MSGs
    static endPointDesc_t sampleTemperatureSensor_TestEp =
    {
      20,                                 // Test endpoint
      &zclSampleTemperatureSensor_TaskID,
      (SimpleDescriptionFormat_t *)NULL,  // No Simple description for this test endpoint
      (afNetworkLatencyReq_t)0            // No Network Latency req
    };
    
    
    /*********************************************************************
     * LOCAL FUNCTIONS
     */
    static void zclSampleTemperatureSensor_HandleKeys( byte shift, byte keys );
    static void zclSampleTemperatureSensor_BasicResetCB( void );
    static void zclSampleTemperatureSensor_IdentifyCB( zclIdentify_t *pCmd );
    static void zclSampleTemperatureSensor_IdentifyQueryRspCB( zclIdentifyQueryRsp_t *pRsp );
    static void zclSampleTemperatureSensor_ProcessIdentifyTimeChange( void );
    //void getTemperature(void);
    
    // app display functions
    void zclSampleTemperatureSensor_LcdDisplayUpdate(void);
    void zclSampleTemperatureSensor_LcdDisplayMainMode(void);
    void zclSampleTemperatureSensor_LcdDisplayTempMode(void);
    void zclSampleTemperatureSensor_LcdDisplayHelpMode(void);
    //int16 getTemperature(void);
    int16 aadc = 30;
    
    static void zclSampleTemperatureSensor_SendTemp(void);
    
    // Functions to process ZCL Foundation incoming Command/Response messages
    static void zclSampleTemperatureSensor_ProcessIncomingMsg( zclIncomingMsg_t *msg );
    #ifdef ZCL_READ
    static uint8 zclSampleTemperatureSensor_ProcessInReadRspCmd( zclIncomingMsg_t *pInMsg );
    #endif
    #ifdef ZCL_WRITE
    static uint8 zclSampleTemperatureSensor_ProcessInWriteRspCmd( zclIncomingMsg_t *pInMsg );
    #endif
    static uint8 zclSampleTemperatureSensor_ProcessInDefaultRspCmd( zclIncomingMsg_t *pInMsg );
    #ifdef ZCL_DISCOVER
    static uint8 zclSampleTemperatureSensor_ProcessInDiscCmdsRspCmd( zclIncomingMsg_t *pInMsg );
    static uint8 zclSampleTemperatureSensor_ProcessInDiscAttrsRspCmd( zclIncomingMsg_t *pInMsg );
    static uint8 zclSampleTemperatureSensor_ProcessInDiscAttrsExtRspCmd( zclIncomingMsg_t *pInMsg );
    #endif // ZCL_DISCOVER
    
    /*********************************************************************
     * STATUS STRINGS
     */
    #ifdef LCD_SUPPORTED
    const char sClearLine[]    = " ";
    const char sDeviceName[]   = "  Temp Sensor";
    const char sSwTempUp[]     = "SW1: Raise Temp";
    const char sSwEZMode[]     = "SW2: EZ-Mode";
    const char sSwTempDown[]   = "SW3: Lower Temp";
    const char sSwHelp[]       = "SW5: Help";
    #endif
    
    /*********************************************************************
     * ZCL General Profile Callback table
     */
    static zclGeneral_AppCallbacks_t zclSampleTemperatureSensor_CmdCallbacks =
    {
      zclSampleTemperatureSensor_BasicResetCB,        // Basic Cluster Reset command
      zclSampleTemperatureSensor_IdentifyCB,          // Identify command
    #ifdef ZCL_EZMODE
      NULL,                                           // Identify EZ-Mode Invoke command
      NULL,                                           // Identify Update Commission State command
    #endif
      NULL,                                           // Identify Trigger Effect command
      zclSampleTemperatureSensor_IdentifyQueryRspCB,  // Identify Query Response command
      NULL,             				                      // On/Off cluster command
      NULL,                                           // On/Off cluster enhanced command Off with Effect
      NULL,                                           // On/Off cluster enhanced command On with Recall Global Scene
      NULL,                                           // On/Off cluster enhanced command On with Timed Off
    #ifdef ZCL_LEVEL_CTRL
      NULL,                                           // Level Control Move to Level command
      NULL,                                           // Level Control Move command
      NULL,                                           // Level Control Step command
      NULL,                                           // Level Control Stop command
    #endif
    #ifdef ZCL_GROUPS
      NULL,                                           // Group Response commands
    #endif
    #ifdef ZCL_SCENES
      NULL,                                           // Scene Store Request command
      NULL,                                           // Scene Recall Request command
      NULL,                                           // Scene Response command
    #endif
    #ifdef ZCL_ALARMS
      NULL,                                           // Alarm (Response) commands
    #endif
    #ifdef SE_UK_EXT
      NULL,                                           // Get Event Log command
      NULL,                                           // Publish Event Log command
    #endif
      NULL,                                           // RSSI Location command
      NULL                                            // RSSI Location Response command
    };
    
    /*********************************************************************
     * @fn          zclSampleTemperatureSensor_Init
     *
     * @brief       Initialization function for the zclGeneral layer.
     *
     * @param       none
     *
     * @return      none
     */
    void zclSampleTemperatureSensor_Init( byte task_id )
    {
      zclSampleTemperatureSensor_TaskID = task_id;
    
      // Set destination address to indirect
      zclSampleTemperatureSensor_DstAddr.addrMode = (afAddrMode_t)AddrNotPresent;
      zclSampleTemperatureSensor_DstAddr.endPoint = 0;
      zclSampleTemperatureSensor_DstAddr.addr.shortAddr = 0;
    
      // This app is part of the Home Automation Profile
      zclHA_Init( &zclSampleTemperatureSensor_SimpleDesc );
    
      // Register the ZCL General Cluster Library callback functions
      zclGeneral_RegisterCmdCallbacks( SAMPLETEMPERATURESENSOR_ENDPOINT, &zclSampleTemperatureSensor_CmdCallbacks );
    
      osal_start_timerEx( zclSampleTemperatureSensor_TaskID, SAMPLETEMPERATURESENSOR_ADC, SAMPLETEMPERATURESENSOR_REPORT_INTERVAL );
      // Register the application's attribute list
      zcl_registerAttrList( SAMPLETEMPERATURESENSOR_ENDPOINT, SAMPLETEMPERATURESENSOR_MAX_ATTRIBUTES, zclSampleTemperatureSensor_Attrs );
    
      // Register the Application to receive the unprocessed Foundation command/response messages
      zcl_registerForMsg( zclSampleTemperatureSensor_TaskID );
    
    #ifdef ZCL_EZMODE
      // Register EZ-Mode
      zcl_RegisterEZMode( &zclSampleTemperatureSensor_RegisterEZModeData );
    
      // Register with the ZDO to receive Match Descriptor Responses
      ZDO_RegisterForZDOMsg(task_id, Match_Desc_rsp);
    #endif
    
      // Register for all key events - This app will handle all key events
      RegisterForKeys( zclSampleTemperatureSensor_TaskID );
    
      // Register for a test endpoint
      afRegister( &sampleTemperatureSensor_TestEp );
    
    #ifdef LCD_SUPPORTED
      // display the device name
      HalLcdWriteString( (char *)sDeviceName, HAL_LCD_LINE_3 );
    #endif
    }
    
    /*********************************************************************
     * @fn          zclSample_event_loop
     *
     * @brief       Event Loop Processor for zclGeneral.
     *
     * @param       none
     *
     * @return      none
     */
    uint16 zclSampleTemperatureSensor_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( zclSampleTemperatureSensor_TaskID )) )
        {
          switch ( MSGpkt->hdr.event )
          {
    #ifdef ZCL_EZMODE
            case ZDO_CB_MSG:
              zclSampleTemperatureSensor_ProcessZDOMsgs( (zdoIncomingMsg_t *)MSGpkt );
              break;
    #endif
    
            case ZCL_INCOMING_MSG:
              // Incoming ZCL Foundation command/response messages
              zclSampleTemperatureSensor_ProcessIncomingMsg( (zclIncomingMsg_t *)MSGpkt );
              break;
    
            case KEY_CHANGE:
              zclSampleTemperatureSensor_HandleKeys( ((keyChange_t *)MSGpkt)->state, ((keyChange_t *)MSGpkt)->keys );
              break;
    
            case ZDO_STATE_CHANGE:
              zclSampleTemperatureSensor_NwkState = (devStates_t)(MSGpkt->hdr.status);
    osal_start_timerEx( zclSampleTemperatureSensor_TaskID, SAMPLETEMPERATURESENSOR_ADC, SAMPLETEMPERATURESENSOR_REPORT_INTERVAL );
    
              // now on the network
              if ( (zclSampleTemperatureSensor_NwkState == DEV_ZB_COORD) ||
                   (zclSampleTemperatureSensor_NwkState == DEV_ROUTER)   ||
                   (zclSampleTemperatureSensor_NwkState == DEV_END_DEVICE) )
              {
    #ifndef HOLD_AUTO_START
                giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;
                zclSampleTemperatureSensor_LcdDisplayUpdate();
    #endif
    #ifdef ZCL_EZMODE
                zcl_EZModeAction( EZMODE_ACTION_NETWORK_STARTED, NULL );
    #endif // ZCL_EZMODE
              }
              break;
    
            default:
              break;
          }
    
          // Release the memory
          osal_msg_deallocate( (uint8 *)MSGpkt );
        }
    
        // return unprocessed events
        return (events ^ SYS_EVENT_MSG);
      }
    
      if ( events & SAMPLETEMPERATURESENSOR_IDENTIFY_TIMEOUT_EVT )
      {
        if ( zclSampleTemperatureSensor_IdentifyTime > 0 )
          zclSampleTemperatureSensor_IdentifyTime--;
        zclSampleTemperatureSensor_ProcessIdentifyTimeChange();
    
        return ( events ^ SAMPLETEMPERATURESENSOR_IDENTIFY_TIMEOUT_EVT );
      }
    
    #ifdef ZCL_EZMODE
      // going on to next state
      if ( events & SAMPLETEMPERATURESENSOR_EZMODE_NEXTSTATE_EVT )
      {
        zcl_EZModeAction ( EZMODE_ACTION_PROCESS, NULL );   // going on to next state
        return ( events ^ SAMPLETEMPERATURESENSOR_EZMODE_NEXTSTATE_EVT );
      }
    
      // the overall EZMode timer expired, so we timed out
      if ( events & SAMPLETEMPERATURESENSOR_EZMODE_TIMEOUT_EVT )
      {
        zcl_EZModeAction ( EZMODE_ACTION_TIMED_OUT, NULL ); // EZ-Mode timed out
        return ( events ^ SAMPLETEMPERATURESENSOR_EZMODE_TIMEOUT_EVT );
      }
    #endif // ZLC_EZMODE
    
      if ( events & SAMPLETEMPERATURESENSOR_MAIN_SCREEN_EVT )
      {
        giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;
        zclSampleTemperatureSensor_LcdDisplayUpdate();
    
        return ( events ^ SAMPLETEMPERATURESENSOR_MAIN_SCREEN_EVT );
      }
    
      if (events & SAMPLETEMPERATURESENSOR_ADC)
      {
        zclSampleTemperatureSensor_MeasuredValue = aadc ;
        osal_start_timerEx( zclSampleTemperatureSensor_TaskID, SAMPLETEMPERATURESENSOR_ADC, SAMPLETEMPERATURESENSOR_REPORT_INTERVAL );
        return ( events ^ SAMPLETEMPERATURESENSOR_ADC );
      }
    
      if ( events & SAMPLETEMPERATURESENSOR_TEMP_SEND_EVT )
      {
        zclSampleTemperatureSensor_SendTemp();
    
        // report current temperature reading every 10 seconds
        osal_start_timerEx( zclSampleTemperatureSensor_TaskID, SAMPLETEMPERATURESENSOR_TEMP_SEND_EVT, SAMPLETEMPERATURESENSOR_REPORT_INTERVAL );
    
        return ( events ^ SAMPLETEMPERATURESENSOR_TEMP_SEND_EVT );
      }
    
      // Discard unknown events
      return 0;
    }
    
    /*int16 getTemperature(void)
    {
      int16 AvgTemp;
      uint8   i;
      uint16  AdcValue;
      uint16  value;
      uint16  value1;
    //char i;
            //char TempValue[10]; 
            //initTempSensor(); 
      AdcValue = 0;
      for( i = 0; i < 4; i++ )
      {
    ADCIF = 0;
        ADCCON3 = (HAL_ADC_REF_125V | HAL_ADC_RESOLUTION_14 | HAL_ADC_CHN_TEMP);    // ??1.25V????,12????,AD??:?????
            while ( !ADCIF );                                              //?????ADC
                     
        value =  ADCL >> 2;                        
        value |= (((UINT16)ADCH) << 6);    
        AdcValue += value;                          
      }
      value = AdcValue >> 2;                        //????4,?????
      value1 =   (((value) >> 4) - 335);
                 //??AD?,????????
      //while(1)
           // {
              AvgTemp = 0;
              for(i = 0 ; i < 64 ; i++)
              {
                AvgTemp += value1;              
                AvgTemp >>= 1;                              //??????2.
              }
              
           // }
      //zclSampleTemperatureSensor_MeasuredValue = AvgTemp ; 
      return AvgTemp;
    // * @param   keys - bit field for key events. Valid entries:
     //*                 HAL_KEY_SW_5
     //*                 HAL_KEY_SW_4
     //*                 HAL_KEY_SW_3
    }*/
    
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_HandleKeys
     *
     * @brief   Handles all key events for this device.
     *
     * @param   shift - true if in shift/alt.
     *                 HAL_KEY_SW_2
     *                 HAL_KEY_SW_1
     *
     * @return  none
     */
    static void zclSampleTemperatureSensor_HandleKeys( byte shift, byte keys )
    {
      if ( keys & HAL_KEY_SW_1 )
      {
        // increase temperature
        giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;
    
        if ( zclSampleTemperatureSensor_MeasuredValue < zclSampleTemperatureSensor_MaxMeasuredValue )
        {
          zclSampleTemperatureSensor_MeasuredValue = zclSampleTemperatureSensor_MeasuredValue + 100;  // considering using whole number value
        }
        else if ( zclSampleTemperatureSensor_MeasuredValue >= zclSampleTemperatureSensor_MaxMeasuredValue )
        {
          zclSampleTemperatureSensor_MeasuredValue = zclSampleTemperatureSensor_MaxMeasuredValue;
        }
    
        // Send temperature information
        zclSampleTemperatureSensor_SendTemp();
      }
    
      if ( keys & HAL_KEY_SW_2 )
      {
        if ( ( giTemperatureSensorScreenMode == TEMPSENSE_MAINMODE ) ||
            ( giTemperatureSensorScreenMode == TEMPSENSE_HELPMODE ) )
        {
          giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;
    
    #ifdef ZCL_EZMODE
          zclEZMode_InvokeData_t ezModeData;
          static uint16 clusterIDs[] = { ZCL_CLUSTER_ID_MS_TEMPERATURE_MEASUREMENT };   // only bind on the Temperature Measurement cluster
    
          // Invoke EZ-Mode
          ezModeData.endpoint = SAMPLETEMPERATURESENSOR_ENDPOINT; // endpoint on which to invoke EZ-Mode
          if ( ( zclSampleTemperatureSensor_NwkState == DEV_ZB_COORD ) ||
               ( zclSampleTemperatureSensor_NwkState == DEV_ROUTER )   ||
               ( zclSampleTemperatureSensor_NwkState == DEV_END_DEVICE ) )
          {
            ezModeData.onNetwork = TRUE;      // node is already on the network
          }
          else
          {
            ezModeData.onNetwork = FALSE;     // node is not yet on the network
          }
          ezModeData.initiator = TRUE;        // Temperature Sensor is an initiator
          ezModeData.numActiveInClusters = 1;
          ezModeData.pActiveInClusterIDs = clusterIDs;
          ezModeData.numActiveOutClusters = 0;   // active output cluster
          ezModeData.pActiveOutClusterIDs = NULL;
          zcl_InvokeEZMode( &ezModeData );
    
    #ifdef LCD_SUPPORTED
          HalLcdWriteString( "EZMode", HAL_LCD_LINE_2 );
    #endif
    
          // NOT ZCL_EZMODE, Use EndDeviceBind
    #else
          {
            zAddrType_t dstAddr;
            dstAddr.addrMode = Addr16Bit;
            dstAddr.addr.shortAddr = 0;   // Coordinator makes the EDB match
    
            // Initiate an End Device Bind Request, this bind request will
            // only use a cluster list that is important to binding.
            HalLedSet ( HAL_LED_4, HAL_LED_MODE_OFF );
            ZDP_EndDeviceBindReq( &dstAddr, NLME_GetShortAddr(),
                                  SAMPLETEMPERATURESENSOR_ENDPOINT,
                                  ZCL_HA_PROFILE_ID,
                                  0, NULL,
                                  ZCLSAMPLETEMPERATURESENSOR_BINDINGLIST, bindingOutClusters,
                                  FALSE );
          }
    #endif // ZCL_EZMODE
        }
      }
    
      if ( keys & HAL_KEY_SW_3 )
      {
        giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;
    
        // decrease the temperature
        if ( zclSampleTemperatureSensor_MeasuredValue > zclSampleTemperatureSensor_MinMeasuredValue )
        {
          zclSampleTemperatureSensor_MeasuredValue = zclSampleTemperatureSensor_MeasuredValue - 100;  // considering using whole number value
        }
        else if ( zclSampleTemperatureSensor_MeasuredValue >= zclSampleTemperatureSensor_MinMeasuredValue )
        {
          zclSampleTemperatureSensor_MeasuredValue = zclSampleTemperatureSensor_MinMeasuredValue;
        }
    
        // Send temperature information
        zclSampleTemperatureSensor_SendTemp();
      }
    
      if ( keys & HAL_KEY_SW_4 )
      {
        giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;
    
        if ( ( zclSampleTemperatureSensor_NwkState == DEV_ZB_COORD ) ||
             ( zclSampleTemperatureSensor_NwkState == DEV_ROUTER ) )
        {
          // toggle permit join
          gPermitDuration = gPermitDuration ? 0 : 0xff;
          NLME_PermitJoiningRequest( gPermitDuration );
        }
      }
    
      if ( shift && ( keys & HAL_KEY_SW_5 ) )
      {
        zclSampleTemperatureSensor_BasicResetCB();
      }
      else if ( keys & HAL_KEY_SW_5 )
      {
        if ( giTemperatureSensorScreenMode == TEMPSENSE_MAINMODE )
        {
          giTemperatureSensorScreenMode = TEMPSENSE_HELPMODE;
        }
        else if ( giTemperatureSensorScreenMode == TEMPSENSE_HELPMODE )
        {
    #ifdef LCD_SUPPORTED
          HalLcdWriteString( (char *)sClearLine, HAL_LCD_LINE_2 );
    #endif
          giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;
        }
      }
    
      // update display
      zclSampleTemperatureSensor_LcdDisplayUpdate();
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_LcdDisplayUpdate
     *
     * @brief   Called to update the LCD display.
     *
     * @param   none
     *
     * @return  none
     */
    void zclSampleTemperatureSensor_LcdDisplayUpdate( void )
    {
      // turn on red LED for temperatures >= 24.00C
      if ( zclSampleTemperatureSensor_MeasuredValue >= 2400 )
      {
        HalLedSet ( HAL_LED_1, HAL_LED_MODE_OFF );
        HalLedSet ( HAL_LED_2, HAL_LED_MODE_ON );
      }
      // turn on green LED for temperatures <= 20.00C
      else if ( zclSampleTemperatureSensor_MeasuredValue <= 2000 )
      {
        HalLedSet ( HAL_LED_1, HAL_LED_MODE_ON );
        HalLedSet ( HAL_LED_2, HAL_LED_MODE_OFF );
      }
      // turn on both red and green LEDs for temperatures between 20.00C and 24.00C
      else
      {
        HalLedSet ( HAL_LED_1, HAL_LED_MODE_ON );
        HalLedSet ( HAL_LED_2, HAL_LED_MODE_ON );
      }
    
      if ( giTemperatureSensorScreenMode == TEMPSENSE_HELPMODE )
      {
        zclSampleTemperatureSensor_LcdDisplayHelpMode();
      }
      else
      {
        zclSampleTemperatureSensor_LcdDisplayMainMode();
      }
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_LcdDisplayMainMode
     *
     * @brief   Called to display the main screen on the LCD.
     *
     * @param   none
     *
     * @return  none
     */
    void zclSampleTemperatureSensor_LcdDisplayMainMode( void )
    {
      char sDisplayTemp[16];
    
      if ( zclSampleTemperatureSensor_NwkState == DEV_ZB_COORD )
      {
        zclHA_LcdStatusLine1( 0 );
      }
      else if ( zclSampleTemperatureSensor_NwkState == DEV_ROUTER )
      {
        zclHA_LcdStatusLine1( 1 );
      }
      else if ( zclSampleTemperatureSensor_NwkState == DEV_END_DEVICE )
      {
        zclHA_LcdStatusLine1( 2 );
      }
    
      // display current temperature
      osal_memcpy(sDisplayTemp, "TEMP: ", 6);
      _ltoa( ( zclSampleTemperatureSensor_MeasuredValue / 100 ), (void *)(&sDisplayTemp[6]), 10 );   // convert temperature to whole number
      osal_memcpy( &sDisplayTemp[8], "C", 2 );
    #ifdef LCD_SUPPORTED
      HalLcdWriteString( (char *)sDisplayTemp, HAL_LCD_LINE_2 );
    #endif
    
    #ifdef LCD_SUPPORTED
      if ( ( zclSampleTemperatureSensor_NwkState == DEV_ZB_COORD ) ||
           ( zclSampleTemperatureSensor_NwkState == DEV_ROUTER ) )
      {
        // display help key with permit join status
        if ( gPermitDuration )
        {
          HalLcdWriteString( "SW5: Help      *", HAL_LCD_LINE_3 );
        }
        else
        {
          HalLcdWriteString( "SW5: Help       ", HAL_LCD_LINE_3 );
        }
      }
      else
      {
        // display help key
        HalLcdWriteString( (char *)sSwHelp, HAL_LCD_LINE_3 );
      }
    #endif
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_LcdDisplayHelpMode
     *
     * @brief   Called to display the SW options on the LCD.
     *
     * @param   none
     *
     * @return  none
     */
    void zclSampleTemperatureSensor_LcdDisplayHelpMode( void )
    {
    #ifdef LCD_SUPPORTED
      HalLcdWriteString( (char *)sSwTempUp, HAL_LCD_LINE_1 );
      HalLcdWriteString( (char *)sSwEZMode, HAL_LCD_LINE_2 );
      HalLcdWriteString( (char *)sSwTempDown, HAL_LCD_LINE_3 );
    #endif
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_SendTemp
     *
     * @brief   Called to send current temperature information to the thermostat
     *
     * @param   none
     *
     * @return  none
     */
    static void zclSampleTemperatureSensor_SendTemp( void )
    {
    #ifdef ZCL_REPORT
      zclReportCmd_t *pReportCmd;
     // zclSampleTemperatureSensor_MeasuredValue = getTemperature();
      pReportCmd = osal_mem_alloc( sizeof(zclReportCmd_t) + sizeof(zclReport_t) );
      if ( pReportCmd != NULL )
      {
        pReportCmd->numAttr = 1;
        pReportCmd->attrList[0].attrID = ATTRID_MS_TEMPERATURE_MEASURED_VALUE;
        pReportCmd->attrList[0].dataType = ZCL_DATATYPE_INT16;
        pReportCmd->attrList[0].attrData = (void *)(&zclSampleTemperatureSensor_MeasuredValue);
    
        zcl_SendReportCmd( SAMPLETEMPERATURESENSOR_ENDPOINT, &zclSampleTemperatureSensor_DstAddr,
                           ZCL_CLUSTER_ID_MS_TEMPERATURE_MEASUREMENT,
                           pReportCmd, ZCL_FRAME_SERVER_CLIENT_DIR, TRUE, zclSampleTemperatureSensorSeqNum++ );
      }
    
      osal_mem_free( pReportCmd );
    #endif  // ZCL_REPORT
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_ProcessIdentifyTimeChange
     *
     * @brief   Called to process any change to the IdentifyTime attribute.
     *
     * @param   none
     *
     * @return  none
     */
    static void zclSampleTemperatureSensor_ProcessIdentifyTimeChange( void )
    {
      if ( zclSampleTemperatureSensor_IdentifyTime > 0 )
      {
        osal_start_timerEx( zclSampleTemperatureSensor_TaskID, SAMPLETEMPERATURESENSOR_IDENTIFY_TIMEOUT_EVT, 1000 );
        HalLedBlink ( HAL_LED_4, 0xFF, HAL_LED_DEFAULT_DUTY_CYCLE, HAL_LED_DEFAULT_FLASH_TIME );
      }
      else
      {
        if ( zclSampleTemperatureSensor_OnOff )
        {
          HalLedSet ( HAL_LED_4, HAL_LED_MODE_ON );
        }
        else
        {
          HalLedSet ( HAL_LED_4, HAL_LED_MODE_OFF );
        }
    
        osal_stop_timerEx( zclSampleTemperatureSensor_TaskID, SAMPLETEMPERATURESENSOR_IDENTIFY_TIMEOUT_EVT );
      }
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_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 zclSampleTemperatureSensor_BasicResetCB( void )
    {
      // Put device back to factory default settings
      zgWriteStartupOptions( ZG_STARTUP_SET, 3 );   // bit set both default configuration and default network
    
      // restart device
      MT_SysCommandProcessing( aProcessCmd );
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_IdentifyCB
     *
     * @brief   Callback from the ZCL General Cluster Library when
     *          it received an Identity Command for this application.
     *
     * @param   srcAddr - source address and endpoint of the response message
     * @param   identifyTime - the number of seconds to identify yourself
     *
     * @return  none
     */
    static void zclSampleTemperatureSensor_IdentifyCB( zclIdentify_t *pCmd )
    {
      zclSampleTemperatureSensor_IdentifyTime = pCmd->identifyTime;
      zclSampleTemperatureSensor_ProcessIdentifyTimeChange();
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_IdentifyQueryRspCB
     *
     * @brief   Callback from the ZCL General Cluster Library when
     *          it received an Identity Query Response Command for this application.
     *
     * @param   srcAddr - requestor's address
     * @param   timeout - number of seconds to identify yourself (valid for query response)
     *
     * @return  none
     */
    static void zclSampleTemperatureSensor_IdentifyQueryRspCB( zclIdentifyQueryRsp_t *pRsp )
    {
      (void)pRsp;
    #ifdef ZCL_EZMODE
      {
        zclEZMode_ActionData_t data;
        data.pIdentifyQueryRsp = pRsp;
        zcl_EZModeAction ( EZMODE_ACTION_IDENTIFY_QUERY_RSP, &data );
      }
    #endif
    }
    
    /******************************************************************************
     *
     *  Functions for processing ZCL Foundation incoming Command/Response messages
     *
     *****************************************************************************/
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_ProcessIncomingMsg
     *
     * @brief   Process ZCL Foundation incoming message
     *
     * @param   pInMsg - pointer to the received message
     *
     * @return  none
     */
    static void zclSampleTemperatureSensor_ProcessIncomingMsg( zclIncomingMsg_t *pInMsg)
    {
      switch ( pInMsg->zclHdr.commandID )
      {
    #ifdef ZCL_READ
        case ZCL_CMD_READ_RSP:
          zclSampleTemperatureSensor_ProcessInReadRspCmd( pInMsg );
          break;
    #endif
    #ifdef ZCL_WRITE
        case ZCL_CMD_WRITE_RSP:
          zclSampleTemperatureSensor_ProcessInWriteRspCmd( pInMsg );
          break;
    #endif
    #ifdef ZCL_REPORT
        // See ZCL Test Applicaiton (zcl_testapp.c) for sample code on Attribute Reporting
        case ZCL_CMD_CONFIG_REPORT:
          //zclSampleTemperatureSensor_ProcessInConfigReportCmd( pInMsg );
          break;
    
        case ZCL_CMD_CONFIG_REPORT_RSP:
          //zclSampleTemperatureSensor_ProcessInConfigReportRspCmd( pInMsg );
          break;
    
        case ZCL_CMD_READ_REPORT_CFG:
          //zclSampleTemperatureSensor_ProcessInReadReportCfgCmd( pInMsg );
          break;
    
        case ZCL_CMD_READ_REPORT_CFG_RSP:
          //zclSampleTemperatureSensor_ProcessInReadReportCfgRspCmd( pInMsg );
          break;
    
        case ZCL_CMD_REPORT:
          //zclSampleTemperatureSensor_ProcessInReportCmd( pInMsg );
          break;
    #endif
        case ZCL_CMD_DEFAULT_RSP:
          zclSampleTemperatureSensor_ProcessInDefaultRspCmd( pInMsg );
          break;
    #ifdef ZCL_DISCOVER
        case ZCL_CMD_DISCOVER_CMDS_RECEIVED_RSP:
          zclSampleTemperatureSensor_ProcessInDiscCmdsRspCmd( pInMsg );
          break;
    
        case ZCL_CMD_DISCOVER_CMDS_GEN_RSP:
          zclSampleTemperatureSensor_ProcessInDiscCmdsRspCmd( pInMsg );
          break;
    
        case ZCL_CMD_DISCOVER_ATTRS_RSP:
          zclSampleTemperatureSensor_ProcessInDiscAttrsRspCmd( pInMsg );
          break;
    
        case ZCL_CMD_DISCOVER_ATTRS_EXT_RSP:
          zclSampleTemperatureSensor_ProcessInDiscAttrsExtRspCmd( pInMsg );
          break;
    #endif
        default:
          break;
      }
    
      if ( pInMsg->attrCmd )
      {
        osal_mem_free( pInMsg->attrCmd );
      }
    }
    
    #ifdef ZCL_READ
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_ProcessInReadRspCmd
     *
     * @brief   Process the "Profile" Read Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleTemperatureSensor_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      zclSampleTemperatureSensor_ProcessInWriteRspCmd
     *
     * @brief   Process the "Profile" Write Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleTemperatureSensor_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      zclSampleTemperatureSensor_ProcessInDefaultRspCmd
     *
     * @brief   Process the "Profile" Default Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleTemperatureSensor_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      zclSampleTemperatureSensor_ProcessInDiscCmdsRspCmd
     *
     * @brief   Process the Discover Commands Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleTemperatureSensor_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      zclSampleTemperatureSensor_ProcessInDiscAttrsRspCmd
     *
     * @brief   Process the "Profile" Discover Attributes Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleTemperatureSensor_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      zclSampleTemperatureSensor_ProcessInDiscAttrsExtRspCmd
     *
     * @brief   Process the "Profile" Discover Attributes Extended Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleTemperatureSensor_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
    
    #ifdef ZCL_EZMODE
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_ProcessZDOMsgs
     *
     * @brief   Called when this node receives a ZDO/ZDP response.
     *
     * @param   none
     *
     * @return  status
     */
    static void zclSampleTemperatureSensor_ProcessZDOMsgs( zdoIncomingMsg_t *pMsg )
    {
      zclEZMode_ActionData_t data;
      ZDO_MatchDescRsp_t *pMatchDescRsp;
    
      // Let EZ-Mode know of the Match Descriptor Response
      if ( pMsg->clusterID == Match_Desc_rsp )
      {
        pMatchDescRsp = ZDO_ParseEPListRsp( pMsg );
        data.pMatchDescRsp = pMatchDescRsp;
        zcl_EZModeAction( EZMODE_ACTION_MATCH_DESC_RSP, &data );
        osal_mem_free( pMatchDescRsp );
      }
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_EZModeCB
     *
     * @brief   The Application is informed of events. This can be used to show on the UI what is
    *           going on during EZ-Mode steering/finding/binding.
     *
     * @param   state - an
     *
     * @return  none
     */
    static void zclSampleTemperatureSensor_EZModeCB( zlcEZMode_State_t state, zclEZMode_CBData_t *pData )
    {
    #ifdef LCD_SUPPORTED
      char szLine[20];
      char *pStr;
      uint8 err;
    #endif
    
      // time to go into identify mode
      if ( state == EZMODE_STATE_IDENTIFYING )
      {
        zclSampleTemperatureSensor_IdentifyTime = ( EZMODE_TIME / 1000 );  // convert to seconds
        zclSampleTemperatureSensor_ProcessIdentifyTimeChange();
      }
    
      // autoclosing, show what happened (success, cancelled, etc...)
      if( state == EZMODE_STATE_AUTOCLOSE )
      {
    #ifdef LCD_SUPPORTED
        pStr = NULL;
        err = pData->sAutoClose.err;
        if ( err == EZMODE_ERR_SUCCESS )
        {
          pStr = "EZMode: Success";
        }
        else if ( err == EZMODE_ERR_NOMATCH )
        {
          pStr = "EZMode: NoMatch"; // not a match made in heaven
        }
        if ( pStr )
        {
          if ( giTemperatureSensorScreenMode == TEMPSENSE_MAINMODE )
          {
            HalLcdWriteString ( pStr, HAL_LCD_LINE_2 );
          }
        }
    #endif
      }
    
      // finished, either show DstAddr/EP, or nothing (depending on success or not)
      if( state == EZMODE_STATE_FINISH )
      {
        // turn off identify mode
        zclSampleTemperatureSensor_IdentifyTime = 0;
        zclSampleTemperatureSensor_ProcessIdentifyTimeChange();
    
    #ifdef LCD_SUPPORTED
        // if successful, inform user which nwkaddr/ep we bound to
        pStr = NULL;
        err = pData->sFinish.err;
        if( err == EZMODE_ERR_SUCCESS )
        {
          // "EZDst:1234 EP:34"
          osal_memcpy( szLine, "EZDst:", 6 );
          zclHA_uint16toa( pData->sFinish.nwkaddr, &szLine[6] );
          osal_memcpy( &szLine[10], " EP:", 4 );
          _ltoa( pData->sFinish.ep, (void *)(&szLine[14]), 16 );  // _ltoa NULL terminates
          pStr = szLine;
        }
        else if ( err == EZMODE_ERR_BAD_PARAMETER )
        {
          pStr = "EZMode: BadParm";
        }
        else if ( err == EZMODE_ERR_CANCELLED )
        {
          pStr = "EZMode: Cancel";
        }
        else
        {
          pStr = "EZMode: TimeOut";
        }
        if ( pStr )
        {
          if ( giTemperatureSensorScreenMode == TEMPSENSE_MAINMODE )
          {
            HalLcdWriteString ( pStr, HAL_LCD_LINE_2 );
          }
        }
    #endif  // LCD_SUPPORTED
    
        // show main UI screen 3 seconds after joining network
        osal_start_timerEx( zclSampleTemperatureSensor_TaskID, SAMPLETEMPERATURESENSOR_MAIN_SCREEN_EVT, 3000 );
    
        // report current temperature reading 15 seconds after joinging the network
        osal_start_timerEx( zclSampleTemperatureSensor_TaskID, SAMPLETEMPERATURESENSOR_TEMP_SEND_EVT, SAMPLETEMPERATURESENSOR_REPORT_INTERVAL );
      }
    }
    #endif // ZCL_EZMODE
    
    /****************************************************************************
    ****************************************************************************/
    
    
    

    5265.zcl_sampletemperaturesensor.h

    8322.zcl_sampletemperaturesensor_data.c
    /**************************************************************************************************
      Filename:       zcl_sampletemperaturesensor_data.c
      Revised:        $Date: 2014-04-15 13:37:56 -0700 (Tue, 15 Apr 2014) $
      Revision:       $Revision: 38169 $
    
    
      Description:    Zigbee Cluster Library - sample device application.
    
    
      Copyright 2013-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.
    **************************************************************************************************/
    
    /*********************************************************************
     * INCLUDES
     */
    #include "ZComDef.h"
    #include "OSAL.h"
    #include "AF.h"
    #include "ZDConfig.h"
    
    #include "zcl.h"
    #include "zcl_general.h"
    #include "zcl_ha.h"
    #include "zcl_ms.h"
    #include "zcl_ezmode.h"
    
    #include "zcl_sampletemperaturesensor.h"
    
    /*********************************************************************
     * CONSTANTS
     */
    
    #define SAMPLETEMPERATURESENSOR_DEVICE_VERSION     0
    #define SAMPLETEMPERATURESENSOR_FLAGS              0
    
    #define SAMPLETEMPERATURESENSOR_HWVERSION          1
    #define SAMPLETEMPERATURESENSOR_ZCLVERSION         1
    
    /*********************************************************************
     * TYPEDEFS
     */
    
    /*********************************************************************
     * MACROS
     */
    
    /*********************************************************************
     * GLOBAL VARIABLES
     */
    
    // Basic Cluster
    const uint8 zclSampleTemperatureSensor_HWRevision = SAMPLETEMPERATURESENSOR_HWVERSION;
    const uint8 zclSampleTemperatureSensor_ZCLVersion = SAMPLETEMPERATURESENSOR_ZCLVERSION;
    const uint8 zclSampleTemperatureSensor_ManufacturerName[] = { 16, 'T','e','x','a','s','I','n','s','t','r','u','m','e','n','t','s' };
    const uint8 zclSampleTemperatureSensor_ModelId[] = { 16, 'T','I','0','0','0','1',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ' };
    const uint8 zclSampleTemperatureSensor_DateCode[] = { 16, '2','0','0','6','0','8','3','1',' ',' ',' ',' ',' ',' ',' ',' ' };
    const uint8 zclSampleTemperatureSensor_PowerSource = POWER_SOURCE_MAINS_1_PHASE;
    
    uint8 zclSampleTemperatureSensor_LocationDescription[17] = { 16, ' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ' };
    uint8 zclSampleTemperatureSensor_PhysicalEnvironment = 0;
    uint8 zclSampleTemperatureSensor_DeviceEnable = DEVICE_ENABLED;
    
    // Identify Cluster
    uint16 zclSampleTemperatureSensor_IdentifyTime = 0;
    
    // On/Off Cluster
    uint8  zclSampleTemperatureSensor_OnOff = LIGHT_OFF;
    
    // Temperature Sensor Cluster
    
    //int16 aadc = 30;
    //int16 zclSampleTemperatureSensor_MeasuredValue = aadc;  // 22.00C
    const int16 zclSampleTemperatureSensor_MinMeasuredValue = 1700;   // 17.00C
    const uint16 zclSampleTemperatureSensor_MaxMeasuredValue = 2700;  // 27.00C
    
    /*********************************************************************
     * ATTRIBUTE DEFINITIONS - Uses REAL cluster IDs
     */
    CONST zclAttrRec_t zclSampleTemperatureSensor_Attrs[SAMPLETEMPERATURESENSOR_MAX_ATTRIBUTES] =
    {
      // *** General Basic Cluster Attributes ***
      {
        ZCL_CLUSTER_ID_GEN_BASIC,             // Cluster IDs - defined in the foundation (ie. zcl.h)
        {  // Attribute record
          ATTRID_BASIC_HW_VERSION,            // Attribute ID - Found in Cluster Library header (ie. zcl_general.h)
          ZCL_DATATYPE_UINT8,                 // Data Type - found in zcl.h
          ACCESS_CONTROL_READ,                // Variable access control - found in zcl.h
          (void *)&zclSampleTemperatureSensor_HWRevision  // Pointer to attribute variable
        }
      },
      {
        ZCL_CLUSTER_ID_GEN_BASIC,
        { // Attribute record
          ATTRID_BASIC_ZCL_VERSION,
          ZCL_DATATYPE_UINT8,
          ACCESS_CONTROL_READ,
          (void *)&zclSampleTemperatureSensor_ZCLVersion
        }
      },
      {
        ZCL_CLUSTER_ID_GEN_BASIC,
        { // Attribute record
          ATTRID_BASIC_MANUFACTURER_NAME,
          ZCL_DATATYPE_CHAR_STR,
          ACCESS_CONTROL_READ,
          (void *)zclSampleTemperatureSensor_ManufacturerName
        }
      },
      {
        ZCL_CLUSTER_ID_GEN_BASIC,
        { // Attribute record
          ATTRID_BASIC_MODEL_ID,
          ZCL_DATATYPE_CHAR_STR,
          ACCESS_CONTROL_READ,
          (void *)zclSampleTemperatureSensor_ModelId
        }
      },
      {
        ZCL_CLUSTER_ID_GEN_BASIC,
        { // Attribute record
          ATTRID_BASIC_DATE_CODE,
          ZCL_DATATYPE_CHAR_STR,
          ACCESS_CONTROL_READ,
          (void *)zclSampleTemperatureSensor_DateCode
        }
      },
      {
        ZCL_CLUSTER_ID_GEN_BASIC,
        { // Attribute record
          ATTRID_BASIC_POWER_SOURCE,
          ZCL_DATATYPE_UINT8,
          ACCESS_CONTROL_READ,
          (void *)&zclSampleTemperatureSensor_PowerSource
        }
      },
      {
        ZCL_CLUSTER_ID_GEN_BASIC,
        { // Attribute record
          ATTRID_BASIC_LOCATION_DESC,
          ZCL_DATATYPE_CHAR_STR,
          (ACCESS_CONTROL_READ | ACCESS_CONTROL_WRITE),
          (void *)zclSampleTemperatureSensor_LocationDescription
        }
      },
      {
        ZCL_CLUSTER_ID_GEN_BASIC,
        { // Attribute record
          ATTRID_BASIC_PHYSICAL_ENV,
          ZCL_DATATYPE_UINT8,
          (ACCESS_CONTROL_READ | ACCESS_CONTROL_WRITE),
          (void *)&zclSampleTemperatureSensor_PhysicalEnvironment
        }
      },
      {
        ZCL_CLUSTER_ID_GEN_BASIC,
        { // Attribute record
          ATTRID_BASIC_DEVICE_ENABLED,
          ZCL_DATATYPE_BOOLEAN,
          (ACCESS_CONTROL_READ | ACCESS_CONTROL_WRITE),
          (void *)&zclSampleTemperatureSensor_DeviceEnable
        }
      },
    
      // *** Identify Cluster Attribute ***
      {
        ZCL_CLUSTER_ID_GEN_IDENTIFY,
        { // Attribute record
          ATTRID_IDENTIFY_TIME,
          ZCL_DATATYPE_UINT16,
          (ACCESS_CONTROL_READ | ACCESS_CONTROL_WRITE),
          (void *)&zclSampleTemperatureSensor_IdentifyTime
        }
      },
    
      // *** On/Off Cluster Attributes ***
      {
        ZCL_CLUSTER_ID_GEN_ON_OFF,
        { // Attribute record
          ATTRID_ON_OFF,
          ZCL_DATATYPE_BOOLEAN,
          ACCESS_CONTROL_READ,
          (void *)&zclSampleTemperatureSensor_OnOff
        }
      },
    
      // *** Temperature Measurement Attriubtes ***
      {
        ZCL_CLUSTER_ID_MS_TEMPERATURE_MEASUREMENT,
        { // Attribute record
          ATTRID_MS_TEMPERATURE_MEASURED_VALUE,
          ZCL_DATATYPE_INT16,
          ACCESS_CONTROL_READ,
          (void *)&zclSampleTemperatureSensor_MeasuredValue
        }
      },
      {
        ZCL_CLUSTER_ID_MS_TEMPERATURE_MEASUREMENT,
        { // Attribute record
          ATTRID_MS_TEMPERATURE_MIN_MEASURED_VALUE,
          ZCL_DATATYPE_INT16,
          ACCESS_CONTROL_READ,
          (void *)&zclSampleTemperatureSensor_MinMeasuredValue
        }
      },
      {
        ZCL_CLUSTER_ID_MS_TEMPERATURE_MEASUREMENT,
        { // Attribute record
          ATTRID_MS_TEMPERATURE_MAX_MEASURED_VALUE,
          ZCL_DATATYPE_UINT16,
          ACCESS_CONTROL_READ,
          (void *)&zclSampleTemperatureSensor_MaxMeasuredValue
        }
      },
    };
    
    /*********************************************************************
     * SIMPLE DESCRIPTOR
     */
    // This is the Cluster ID List and should be filled with Application
    // specific cluster IDs.
    #define ZCLSAMPLETEMPERATURESENSOR_MAX_INCLUSTERS       3
    const cId_t zclSampleTemperatureSensor_InClusterList[ZCLSAMPLETEMPERATURESENSOR_MAX_INCLUSTERS] =
    {
      ZCL_CLUSTER_ID_GEN_BASIC,
      ZCL_CLUSTER_ID_GEN_IDENTIFY,
      ZCL_CLUSTER_ID_MS_TEMPERATURE_MEASUREMENT
    };
    
    #define ZCLSAMPLETEMPERATURESENSOR_MAX_OUTCLUSTERS       1
    const cId_t zclSampleTemperatureSensor_OutClusterList[ZCLSAMPLETEMPERATURESENSOR_MAX_OUTCLUSTERS] =
    {
      ZCL_CLUSTER_ID_GEN_IDENTIFY
    };
    
    SimpleDescriptionFormat_t zclSampleTemperatureSensor_SimpleDesc =
    {
      SAMPLETEMPERATURESENSOR_ENDPOINT,                  //  int Endpoint;
      ZCL_HA_PROFILE_ID,                     //  uint16 AppProfId[2];
      ZCL_HA_DEVICEID_DIMMABLE_LIGHT,        //  uint16 AppDeviceId[2];
      SAMPLETEMPERATURESENSOR_DEVICE_VERSION,            //  int   AppDevVer:4;
      SAMPLETEMPERATURESENSOR_FLAGS,                     //  int   AppFlags:4;
      ZCLSAMPLETEMPERATURESENSOR_MAX_INCLUSTERS,         //  byte  AppNumInClusters;
      (cId_t *)zclSampleTemperatureSensor_InClusterList, //  byte *pAppInClusterList;
      ZCLSAMPLETEMPERATURESENSOR_MAX_OUTCLUSTERS,        //  byte  AppNumInClusters;
      (cId_t *)zclSampleTemperatureSensor_OutClusterList //  byte *pAppInClusterList;
    };
    
    /*********************************************************************
     * GLOBAL FUNCTIONS
     */
    
    /*********************************************************************
     * LOCAL FUNCTIONS
     */
    
    /****************************************************************************
    ****************************************************************************/
    
    
    

  • Try to add zclSampleTemperatureSensor_LcdDisplayUpdate(); in SAMPLETEMPERATURESENSOR_ADC event

    if (events & SAMPLETEMPERATURESENSOR_ADC)
    {
    zclSampleTemperatureSensor_MeasuredValue = aadc ;

    zclSampleTemperatureSensor_LcdDisplayUpdate();
    osal_start_timerEx( zclSampleTemperatureSensor_TaskID, SAMPLETEMPERATURESENSOR_ADC, SAMPLETEMPERATURESENSOR_REPORT_INTERVAL );
    return ( events ^ SAMPLETEMPERATURESENSOR_ADC );
    }

  • sir i trid it but no use it s not giving me the value 30 in the lcd ... if only i completed this task thn i will b able to call the adc value in that event and make it display the value i get  from the adc ....

     any other idea .... u got what am telling rite sir ???

  • Try to replace your zcl_sampletemperaturesensor.h and zcl_sampletemperaturesensor.c using the attchments

    8130.zcl_sampletemperaturesensor.h

    5488.zcl_sampletemperaturesensor.c
    /**************************************************************************************************
      Filename:       zcl_sampletemperaturesensor.c
      Revised:        $Date: 2013-10-18 11:49:27 -0700 (Fri, 18 Oct 2013) $
      Revision:       $Revision: 35718 $
    
      Description:    Zigbee Cluster Library - sample device application.
    
    
      Copyright 2013 Texas Instruments Incorporated. All rights reserved.
    
      IMPORTANT: Your use of this Software is limited to those specific rights
      granted under the terms of a software license agreement between the user
      who downloaded the software, his/her employer (which must be your employer)
      and Texas Instruments Incorporated (the "License").  You may not use this
      Software unless you agree to abide by the terms of the License. The License
      limits your use, and you acknowledge, that the Software may not be modified,
      copied or distributed unless embedded on a Texas Instruments microcontroller
      or used solely and exclusively in conjunction with a Texas Instruments radio
      frequency transceiver, which is integrated into your product.  Other than for
      the foregoing purpose, you may not use, reproduce, copy, prepare derivative
      works of, modify, distribute, perform, display or sell this Software and/or
      its documentation for any purpose.
    
      YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE
      PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,
      INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE,
      NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL
      TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT,
      NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER
      LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
      INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE
      OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT
      OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
      (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
    
      Should you have any questions regarding your right to use this Software,
      contact Texas Instruments Incorporated at www.TI.com.
    **************************************************************************************************/
    
    /*********************************************************************
      This device will act as a temperature sensor. It updates the current
      temperature on the thermostat when the user sends the desired
      temperature using SW1.
    
      SCREEN MODES
      ----------------------------------------
      Main:
        - SW1: Send current temperature
        - SW2: Invoke EZMode
        - SW3: Adjust temperature
        - SW5: Go to Help screen
    
      Temperature:
        - SW1: Increase temperature
        - SW3: Decrease temperature
        - SW5: Enter temperature
      ----------------------------------------
    *********************************************************************/
    
    /*********************************************************************
     * INCLUDES
     */
    #include "ZComDef.h"
    #include "OSAL.h"
    #include "AF.h"
    #include "ZDApp.h"
    #include "ZDObject.h"
    #include "ZDProfile.h"
    #include "MT_SYS.h"
    
    #include "zcl.h"
    #include "zcl_general.h"
    #include "zcl_ha.h"
    #include "zcl_ezmode.h"
    #include "zcl_ms.h"
    #include "hal_adc.h"
    
    #include "zcl_sampletemperaturesensor.h"
    
    #include "onboard.h"
    
    /* HAL */
    #include "hal_lcd.h"
    #include "hal_led.h"
    #include "hal_key.h"
    //#include "zcl_sampletemperaturesensor_data.c"
    
    /*********************************************************************
     * MACROS
     */
    
    // how often to report temperature
    #define SAMPLETEMPERATURESENSOR_REPORT_INTERVAL   3000
    
    /*********************************************************************
     * CONSTANTS
     */
    
    /*********************************************************************
     * TYPEDEFS
     */
    /*#ifdef __IAR_SYSTEMS_ICC__
    #define  CODE   __code
    #define  XDATA  __xdata
    */
    
    /*********************************************************************
     * GLOBAL VARIABLES
     */
    byte zclSampleTemperatureSensor_TaskID;
    
    uint8 zclSampleTemperatureSensorSeqNum;
    
    static byte gPermitDuration = 0x00;
    /*void initTempSensor(void){
       DISABLE_ALL_INTERRUPTS();            //??????
       InitClock();                         //????????32M
       *((BYTE XDATA*) 0x624B) = 0x01;    //???????
       *((BYTE XDATA*) 0x61BD) = 0x01;    //???????ADC????
    }*/
    
    /*********************************************************************
     * GLOBAL FUNCTIONS
     */
    
    /*********************************************************************
     * LOCAL VARIABLES
     */
    afAddrType_t zclSampleTemperatureSensor_DstAddr;
    
    #ifdef ZCL_EZMODE
    static void zclSampleTemperatureSensor_ProcessZDOMsgs( zdoIncomingMsg_t *pMsg );
    static void zclSampleTemperatureSensor_EZModeCB( zlcEZMode_State_t state, zclEZMode_CBData_t *pData );
    
    static const zclEZMode_RegisterData_t zclSampleTemperatureSensor_RegisterEZModeData =
    {
      &zclSampleTemperatureSensor_TaskID,
      SAMPLETEMPERATURESENSOR_EZMODE_NEXTSTATE_EVT,
      SAMPLETEMPERATURESENSOR_EZMODE_TIMEOUT_EVT,
      &zclSampleTemperatureSensorSeqNum,
      zclSampleTemperatureSensor_EZModeCB
    };
    
    // NOT ZCL_EZMODE, Use EndDeviceBind
    #else
    
    static cId_t bindingOutClusters[] =
    {
      ZCL_CLUSTER_ID_MS_TEMPERATURE_MEASUREMENT
    };
    #define ZCLSAMPLETEMPERATURESENSOR_BINDINGLIST        1
    #endif
    
    devStates_t zclSampleTemperatureSensor_NwkState = DEV_INIT;
    
    uint8 giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;   // display main screen mode first
    
    static uint8 aProcessCmd[] = { 1, 0, 0, 0 }; // used for reset command, { length + cmd0 + cmd1 + data }
    
    // Test Endpoint to allow SYS_APP_MSGs
    static endPointDesc_t sampleTemperatureSensor_TestEp =
    {
      20,                                 // Test endpoint
      &zclSampleTemperatureSensor_TaskID,
      (SimpleDescriptionFormat_t *)NULL,  // No Simple description for this test endpoint
      (afNetworkLatencyReq_t)0            // No Network Latency req
    };
    
    
    /*********************************************************************
     * LOCAL FUNCTIONS
     */
    static void zclSampleTemperatureSensor_HandleKeys( byte shift, byte keys );
    static void zclSampleTemperatureSensor_BasicResetCB( void );
    static void zclSampleTemperatureSensor_IdentifyCB( zclIdentify_t *pCmd );
    static void zclSampleTemperatureSensor_IdentifyQueryRspCB( zclIdentifyQueryRsp_t *pRsp );
    static void zclSampleTemperatureSensor_ProcessIdentifyTimeChange( void );
    //void getTemperature(void);
    // app display functions
    void zclSampleTemperatureSensor_LcdDisplayUpdate(void);
    void zclSampleTemperatureSensor_LcdDisplayMainMode(void);
    void zclSampleTemperatureSensor_LcdDisplayTempMode(void);
    void zclSampleTemperatureSensor_LcdDisplayHelpMode(void);
    int16 getTemperature(void);
    
    static void zclSampleTemperatureSensor_SendTemp(void);
    
    // Functions to process ZCL Foundation incoming Command/Response messages
    static void zclSampleTemperatureSensor_ProcessIncomingMsg( zclIncomingMsg_t *msg );
    #ifdef ZCL_READ
    static uint8 zclSampleTemperatureSensor_ProcessInReadRspCmd( zclIncomingMsg_t *pInMsg );
    #endif
    #ifdef ZCL_WRITE
    static uint8 zclSampleTemperatureSensor_ProcessInWriteRspCmd( zclIncomingMsg_t *pInMsg );
    #endif
    static uint8 zclSampleTemperatureSensor_ProcessInDefaultRspCmd( zclIncomingMsg_t *pInMsg );
    #ifdef ZCL_DISCOVER
    static uint8 zclSampleTemperatureSensor_ProcessInDiscCmdsRspCmd( zclIncomingMsg_t *pInMsg );
    static uint8 zclSampleTemperatureSensor_ProcessInDiscAttrsRspCmd( zclIncomingMsg_t *pInMsg );
    static uint8 zclSampleTemperatureSensor_ProcessInDiscAttrsExtRspCmd( zclIncomingMsg_t *pInMsg );
    #endif // ZCL_DISCOVER
    
    /*********************************************************************
     * STATUS STRINGS
     */
    #ifdef LCD_SUPPORTED
    const char sClearLine[]    = " ";
    const char sDeviceName[]   = "  Temp Sensor";
    const char sSwTempUp[]     = "SW1: Raise Temp";
    const char sSwEZMode[]     = "SW2: EZ-Mode";
    const char sSwTempDown[]   = "SW3: Lower Temp";
    const char sSwHelp[]       = "SW5: Help";
    #endif
    
    /*********************************************************************
     * ZCL General Profile Callback table
     */
    static zclGeneral_AppCallbacks_t zclSampleTemperatureSensor_CmdCallbacks =
    {
      zclSampleTemperatureSensor_BasicResetCB,        // Basic Cluster Reset command
      zclSampleTemperatureSensor_IdentifyCB,          // Identify command
    #ifdef ZCL_EZMODE
      NULL,                                           // Identify EZ-Mode Invoke command
      NULL,                                           // Identify Update Commission State command
    #endif
      NULL,                                           // Identify Trigger Effect command
      zclSampleTemperatureSensor_IdentifyQueryRspCB,  // Identify Query Response command
      NULL,             				                      // On/Off cluster command
      NULL,                                           // On/Off cluster enhanced command Off with Effect
      NULL,                                           // On/Off cluster enhanced command On with Recall Global Scene
      NULL,                                           // On/Off cluster enhanced command On with Timed Off
    #ifdef ZCL_LEVEL_CTRL
      NULL,                                           // Level Control Move to Level command
      NULL,                                           // Level Control Move command
      NULL,                                           // Level Control Step command
      NULL,                                           // Level Control Stop command
    #endif
    #ifdef ZCL_GROUPS
      NULL,                                           // Group Response commands
    #endif
    #ifdef ZCL_SCENES
      NULL,                                           // Scene Store Request command
      NULL,                                           // Scene Recall Request command
      NULL,                                           // Scene Response command
    #endif
    #ifdef ZCL_ALARMS
      NULL,                                           // Alarm (Response) commands
    #endif
    #ifdef SE_UK_EXT
      NULL,                                           // Get Event Log command
      NULL,                                           // Publish Event Log command
    #endif
      NULL,                                           // RSSI Location command
      NULL                                            // RSSI Location Response command
    };
    
    /*********************************************************************
     * @fn          zclSampleTemperatureSensor_Init
     *
     * @brief       Initialization function for the zclGeneral layer.
     *
     * @param       none
     *
     * @return      none
     */
    void zclSampleTemperatureSensor_Init( byte task_id )
    {
      zclSampleTemperatureSensor_TaskID = task_id;
    
      // Set destination address to indirect
      zclSampleTemperatureSensor_DstAddr.addrMode = (afAddrMode_t)AddrNotPresent;
      zclSampleTemperatureSensor_DstAddr.endPoint = 0;
      zclSampleTemperatureSensor_DstAddr.addr.shortAddr = 0;
    
      // This app is part of the Home Automation Profile
      zclHA_Init( &zclSampleTemperatureSensor_SimpleDesc );
    
      // Register the ZCL General Cluster Library callback functions
      zclGeneral_RegisterCmdCallbacks( SAMPLETEMPERATURESENSOR_ENDPOINT, &zclSampleTemperatureSensor_CmdCallbacks );
    
      // Register the application's attribute list
      zcl_registerAttrList( SAMPLETEMPERATURESENSOR_ENDPOINT, SAMPLETEMPERATURESENSOR_MAX_ATTRIBUTES, zclSampleTemperatureSensor_Attrs );
    
      // Register the Application to receive the unprocessed Foundation command/response messages
      zcl_registerForMsg( zclSampleTemperatureSensor_TaskID );
    
    #ifdef ZCL_EZMODE
      // Register EZ-Mode
      zcl_RegisterEZMode( &zclSampleTemperatureSensor_RegisterEZModeData );
    
      // Register with the ZDO to receive Match Descriptor Responses
      ZDO_RegisterForZDOMsg(task_id, Match_Desc_rsp);
    #endif
    
      // Register for all key events - This app will handle all key events
      RegisterForKeys( zclSampleTemperatureSensor_TaskID );
    
      // Register for a test endpoint
      afRegister( &sampleTemperatureSensor_TestEp );
    
    #ifdef LCD_SUPPORTED
      // display the device name
      HalLcdWriteString( (char *)sDeviceName, HAL_LCD_LINE_3 );
    #endif
      osal_start_timerEx( zclSampleTemperatureSensor_TaskID, SAMPLETEMPERATURESENSOR_ADC, SAMPLETEMPERATURESENSOR_REPORT_INTERVAL );
    
    }
    
    /*********************************************************************
     * @fn          zclSample_event_loop
     *
     * @brief       Event Loop Processor for zclGeneral.
     *
     * @param       none
     *
     * @return      none
     */
    uint16 zclSampleTemperatureSensor_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( zclSampleTemperatureSensor_TaskID )) )
        {
          switch ( MSGpkt->hdr.event )
          {
    #ifdef ZCL_EZMODE
            case ZDO_CB_MSG:
              zclSampleTemperatureSensor_ProcessZDOMsgs( (zdoIncomingMsg_t *)MSGpkt );
              break;
    #endif
    
            case ZCL_INCOMING_MSG:
              // Incoming ZCL Foundation command/response messages
              zclSampleTemperatureSensor_ProcessIncomingMsg( (zclIncomingMsg_t *)MSGpkt );
              break;
    
            case KEY_CHANGE:
              zclSampleTemperatureSensor_HandleKeys( ((keyChange_t *)MSGpkt)->state, ((keyChange_t *)MSGpkt)->keys );
              break;
    
            case ZDO_STATE_CHANGE:
              zclSampleTemperatureSensor_NwkState = (devStates_t)(MSGpkt->hdr.status);
    
    
              // now on the network
              if ( (zclSampleTemperatureSensor_NwkState == DEV_ZB_COORD) ||
                   (zclSampleTemperatureSensor_NwkState == DEV_ROUTER)   ||
                   (zclSampleTemperatureSensor_NwkState == DEV_END_DEVICE) )
              {
    #ifndef HOLD_AUTO_START
                giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;
                zclSampleTemperatureSensor_LcdDisplayUpdate();
    #endif
    #ifdef ZCL_EZMODE
                zcl_EZModeAction( EZMODE_ACTION_NETWORK_STARTED, NULL );
    #endif // ZCL_EZMODE
              }
              break;
    
            default:
              break;
          }
    
          // Release the memory
          osal_msg_deallocate( (uint8 *)MSGpkt );
        }
    
        // return unprocessed events
        return (events ^ SYS_EVENT_MSG);
      }
    
      if ( events & SAMPLETEMPERATURESENSOR_IDENTIFY_TIMEOUT_EVT )
      {
        if ( zclSampleTemperatureSensor_IdentifyTime > 0 )
          zclSampleTemperatureSensor_IdentifyTime--;
        zclSampleTemperatureSensor_ProcessIdentifyTimeChange();
    
        return ( events ^ SAMPLETEMPERATURESENSOR_IDENTIFY_TIMEOUT_EVT );
      }
    
    #ifdef ZCL_EZMODE
      // going on to next state
      if ( events & SAMPLETEMPERATURESENSOR_EZMODE_NEXTSTATE_EVT )
      {
        zcl_EZModeAction ( EZMODE_ACTION_PROCESS, NULL );   // going on to next state
        return ( events ^ SAMPLETEMPERATURESENSOR_EZMODE_NEXTSTATE_EVT );
      }
    
      // the overall EZMode timer expired, so we timed out
      if ( events & SAMPLETEMPERATURESENSOR_EZMODE_TIMEOUT_EVT )
      {
        zcl_EZModeAction ( EZMODE_ACTION_TIMED_OUT, NULL ); // EZ-Mode timed out
        return ( events ^ SAMPLETEMPERATURESENSOR_EZMODE_TIMEOUT_EVT );
      }
    #endif // ZLC_EZMODE
    
      if ( events & SAMPLETEMPERATURESENSOR_MAIN_SCREEN_EVT )
      {
        giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;
        zclSampleTemperatureSensor_LcdDisplayUpdate();
    
        return ( events ^ SAMPLETEMPERATURESENSOR_MAIN_SCREEN_EVT );
      }
    
      if (events & SAMPLETEMPERATURESENSOR_ADC)
      {
        zclSampleTemperatureSensor_MeasuredValue = 3000 ;
        zclSampleTemperatureSensor_LcdDisplayUpdate();
        osal_start_timerEx( zclSampleTemperatureSensor_TaskID, SAMPLETEMPERATURESENSOR_ADC, SAMPLETEMPERATURESENSOR_REPORT_INTERVAL );
        return ( events ^ SAMPLETEMPERATURESENSOR_ADC );
      }
    
      if ( events & SAMPLETEMPERATURESENSOR_TEMP_SEND_EVT )
      {
        zclSampleTemperatureSensor_SendTemp();
    
        // report current temperature reading every 10 seconds
        osal_start_timerEx( zclSampleTemperatureSensor_TaskID, SAMPLETEMPERATURESENSOR_TEMP_SEND_EVT, SAMPLETEMPERATURESENSOR_REPORT_INTERVAL );
    
        return ( events ^ SAMPLETEMPERATURESENSOR_TEMP_SEND_EVT );
      }
    
      // Discard unknown events
      return 0;
    }
    
    int16 getTemperature(void)
    {
      int16 AvgTemp;
      uint8   i;
      uint16  AdcValue;
      uint16  value;
      uint16  value1;
    //char i;
            //char TempValue[10]; 
            //initTempSensor(); 
      AdcValue = 0;
      for( i = 0; i < 4; i++ )
      {
    ADCIF = 0;
        ADCCON3 = (HAL_ADC_REF_125V | HAL_ADC_RESOLUTION_14 | HAL_ADC_CHN_TEMP);    // ??1.25V????,12????,AD??:?????
            while ( !ADCIF );                                              //?????ADC
                     
        value =  ADCL >> 2;                        
        value |= (((UINT16)ADCH) << 6);    
        AdcValue += value;                          
      }
      value = AdcValue >> 2;                        //????4,?????
      value1 =   (((value) >> 4) - 335);
                 //??AD?,????????
      while(1)
            {
              AvgTemp = 0;
              for(i = 0 ; i < 64 ; i++)
              {
                AvgTemp += value1;              
                AvgTemp >>= 1;                              //??????2.
              }
              
            }
      //zclSampleTemperatureSensor_MeasuredValue = AvgTemp ; 
      return AvgTemp;
    }
    
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_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_3
     *                 HAL_KEY_SW_2
     *                 HAL_KEY_SW_1
     *
     * @return  none
     */
    static void zclSampleTemperatureSensor_HandleKeys( byte shift, byte keys )
    {
      if ( keys & HAL_KEY_SW_1 )
      {
        // increase temperature
        giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;
    
        if ( zclSampleTemperatureSensor_MeasuredValue < zclSampleTemperatureSensor_MaxMeasuredValue )
        {
          zclSampleTemperatureSensor_MeasuredValue = zclSampleTemperatureSensor_MeasuredValue + 100;  // considering using whole number value
        }
        else if ( zclSampleTemperatureSensor_MeasuredValue >= zclSampleTemperatureSensor_MaxMeasuredValue )
        {
          zclSampleTemperatureSensor_MeasuredValue = zclSampleTemperatureSensor_MaxMeasuredValue;
        }
    
        // Send temperature information
        zclSampleTemperatureSensor_SendTemp();
      }
    
      if ( keys & HAL_KEY_SW_2 )
      {
        if ( ( giTemperatureSensorScreenMode == TEMPSENSE_MAINMODE ) ||
            ( giTemperatureSensorScreenMode == TEMPSENSE_HELPMODE ) )
        {
          giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;
    
    #ifdef ZCL_EZMODE
          zclEZMode_InvokeData_t ezModeData;
          static uint16 clusterIDs[] = { ZCL_CLUSTER_ID_MS_TEMPERATURE_MEASUREMENT };   // only bind on the Temperature Measurement cluster
    
          // Invoke EZ-Mode
          ezModeData.endpoint = SAMPLETEMPERATURESENSOR_ENDPOINT; // endpoint on which to invoke EZ-Mode
          if ( ( zclSampleTemperatureSensor_NwkState == DEV_ZB_COORD ) ||
               ( zclSampleTemperatureSensor_NwkState == DEV_ROUTER )   ||
               ( zclSampleTemperatureSensor_NwkState == DEV_END_DEVICE ) )
          {
            ezModeData.onNetwork = TRUE;      // node is already on the network
          }
          else
          {
            ezModeData.onNetwork = FALSE;     // node is not yet on the network
          }
          ezModeData.initiator = TRUE;        // Temperature Sensor is an initiator
          ezModeData.numActiveInClusters = 1;
          ezModeData.pActiveInClusterIDs = clusterIDs;
          ezModeData.numActiveOutClusters = 0;   // active output cluster
          ezModeData.pActiveOutClusterIDs = NULL;
          zcl_InvokeEZMode( &ezModeData );
    
    #ifdef LCD_SUPPORTED
          HalLcdWriteString( "EZMode", HAL_LCD_LINE_2 );
    #endif
    
          // NOT ZCL_EZMODE, Use EndDeviceBind
    #else
          {
            zAddrType_t dstAddr;
            dstAddr.addrMode = Addr16Bit;
            dstAddr.addr.shortAddr = 0;   // Coordinator makes the EDB match
    
            // Initiate an End Device Bind Request, this bind request will
            // only use a cluster list that is important to binding.
            HalLedSet ( HAL_LED_4, HAL_LED_MODE_OFF );
            ZDP_EndDeviceBindReq( &dstAddr, NLME_GetShortAddr(),
                                  SAMPLETEMPERATURESENSOR_ENDPOINT,
                                  ZCL_HA_PROFILE_ID,
                                  0, NULL,
                                  ZCLSAMPLETEMPERATURESENSOR_BINDINGLIST, bindingOutClusters,
                                  FALSE );
          }
    #endif // ZCL_EZMODE
        }
      }
    
      if ( keys & HAL_KEY_SW_3 )
      {
        giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;
    
        // decrease the temperature
        if ( zclSampleTemperatureSensor_MeasuredValue > zclSampleTemperatureSensor_MinMeasuredValue )
        {
          zclSampleTemperatureSensor_MeasuredValue = zclSampleTemperatureSensor_MeasuredValue - 100;  // considering using whole number value
        }
        else if ( zclSampleTemperatureSensor_MeasuredValue >= zclSampleTemperatureSensor_MinMeasuredValue )
        {
          zclSampleTemperatureSensor_MeasuredValue = zclSampleTemperatureSensor_MinMeasuredValue;
        }
    
        // Send temperature information
        zclSampleTemperatureSensor_SendTemp();
      }
    
      if ( keys & HAL_KEY_SW_4 )
      {
        giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;
    
        if ( ( zclSampleTemperatureSensor_NwkState == DEV_ZB_COORD ) ||
             ( zclSampleTemperatureSensor_NwkState == DEV_ROUTER ) )
        {
          // toggle permit join
          gPermitDuration = gPermitDuration ? 0 : 0xff;
          NLME_PermitJoiningRequest( gPermitDuration );
        }
      }
    
      if ( shift && ( keys & HAL_KEY_SW_5 ) )
      {
        zclSampleTemperatureSensor_BasicResetCB();
      }
      else if ( keys & HAL_KEY_SW_5 )
      {
        if ( giTemperatureSensorScreenMode == TEMPSENSE_MAINMODE )
        {
          giTemperatureSensorScreenMode = TEMPSENSE_HELPMODE;
        }
        else if ( giTemperatureSensorScreenMode == TEMPSENSE_HELPMODE )
        {
    #ifdef LCD_SUPPORTED
          HalLcdWriteString( (char *)sClearLine, HAL_LCD_LINE_2 );
    #endif
          giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;
        }
      }
    
      // update display
      zclSampleTemperatureSensor_LcdDisplayUpdate();
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_LcdDisplayUpdate
     *
     * @brief   Called to update the LCD display.
     *
     * @param   none
     *
     * @return  none
     */
    void zclSampleTemperatureSensor_LcdDisplayUpdate( void )
    {
      // turn on red LED for temperatures >= 24.00C
      if ( zclSampleTemperatureSensor_MeasuredValue >= 2400 )
      {
        HalLedSet ( HAL_LED_1, HAL_LED_MODE_OFF );
        HalLedSet ( HAL_LED_2, HAL_LED_MODE_ON );
      }
      // turn on green LED for temperatures <= 20.00C
      else if ( zclSampleTemperatureSensor_MeasuredValue <= 2000 )
      {
        HalLedSet ( HAL_LED_1, HAL_LED_MODE_ON );
        HalLedSet ( HAL_LED_2, HAL_LED_MODE_OFF );
      }
      // turn on both red and green LEDs for temperatures between 20.00C and 24.00C
      else
      {
        HalLedSet ( HAL_LED_1, HAL_LED_MODE_ON );
        HalLedSet ( HAL_LED_2, HAL_LED_MODE_ON );
      }
    
      if ( giTemperatureSensorScreenMode == TEMPSENSE_HELPMODE )
      {
        zclSampleTemperatureSensor_LcdDisplayHelpMode();
      }
      else
      {
        zclSampleTemperatureSensor_LcdDisplayMainMode();
      }
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_LcdDisplayMainMode
     *
     * @brief   Called to display the main screen on the LCD.
     *
     * @param   none
     *
     * @return  none
     */
    void zclSampleTemperatureSensor_LcdDisplayMainMode( void )
    {
      char sDisplayTemp[16];
    
      if ( zclSampleTemperatureSensor_NwkState == DEV_ZB_COORD )
      {
        zclHA_LcdStatusLine1( 0 );
      }
      else if ( zclSampleTemperatureSensor_NwkState == DEV_ROUTER )
      {
        zclHA_LcdStatusLine1( 1 );
      }
      else if ( zclSampleTemperatureSensor_NwkState == DEV_END_DEVICE )
      {
        zclHA_LcdStatusLine1( 2 );
      }
    
      // display current temperature
      osal_memcpy(sDisplayTemp, "TEMP: ", 6);
      _ltoa( ( zclSampleTemperatureSensor_MeasuredValue / 100 ), (void *)(&sDisplayTemp[6]), 10 );   // convert temperature to whole number
      osal_memcpy( &sDisplayTemp[8], "C", 2 );
    #ifdef LCD_SUPPORTED
      HalLcdWriteString( (char *)sDisplayTemp, HAL_LCD_LINE_2 );
    #endif
    
    #ifdef LCD_SUPPORTED
      if ( ( zclSampleTemperatureSensor_NwkState == DEV_ZB_COORD ) ||
           ( zclSampleTemperatureSensor_NwkState == DEV_ROUTER ) )
      {
        // display help key with permit join status
        if ( gPermitDuration )
        {
          HalLcdWriteString( "SW5: Help      *", HAL_LCD_LINE_3 );
        }
        else
        {
          HalLcdWriteString( "SW5: Help       ", HAL_LCD_LINE_3 );
        }
      }
      else
      {
        // display help key
        HalLcdWriteString( (char *)sSwHelp, HAL_LCD_LINE_3 );
      }
    #endif
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_LcdDisplayHelpMode
     *
     * @brief   Called to display the SW options on the LCD.
     *
     * @param   none
     *
     * @return  none
     */
    void zclSampleTemperatureSensor_LcdDisplayHelpMode( void )
    {
    #ifdef LCD_SUPPORTED
      HalLcdWriteString( (char *)sSwTempUp, HAL_LCD_LINE_1 );
      HalLcdWriteString( (char *)sSwEZMode, HAL_LCD_LINE_2 );
      HalLcdWriteString( (char *)sSwTempDown, HAL_LCD_LINE_3 );
    #endif
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_SendTemp
     *
     * @brief   Called to send current temperature information to the thermostat
     *
     * @param   none
     *
     * @return  none
     */
    static void zclSampleTemperatureSensor_SendTemp( void )
    {
    #ifdef ZCL_REPORT
      zclReportCmd_t *pReportCmd;
      zclSampleTemperatureSensor_MeasuredValue = getTemperature();
      pReportCmd = osal_mem_alloc( sizeof(zclReportCmd_t) + sizeof(zclReport_t) );
      if ( pReportCmd != NULL )
      {
        pReportCmd->numAttr = 1;
        pReportCmd->attrList[0].attrID = ATTRID_MS_TEMPERATURE_MEASURED_VALUE;
        pReportCmd->attrList[0].dataType = ZCL_DATATYPE_INT16;
        pReportCmd->attrList[0].attrData = (void *)(&zclSampleTemperatureSensor_MeasuredValue);
    
        zcl_SendReportCmd( SAMPLETEMPERATURESENSOR_ENDPOINT, &zclSampleTemperatureSensor_DstAddr,
                           ZCL_CLUSTER_ID_MS_TEMPERATURE_MEASUREMENT,
                           pReportCmd, ZCL_FRAME_SERVER_CLIENT_DIR, TRUE, zclSampleTemperatureSensorSeqNum++ );
      }
    
      osal_mem_free( pReportCmd );
    #endif  // ZCL_REPORT
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_ProcessIdentifyTimeChange
     *
     * @brief   Called to process any change to the IdentifyTime attribute.
     *
     * @param   none
     *
     * @return  none
     */
    static void zclSampleTemperatureSensor_ProcessIdentifyTimeChange( void )
    {
      if ( zclSampleTemperatureSensor_IdentifyTime > 0 )
      {
        osal_start_timerEx( zclSampleTemperatureSensor_TaskID, SAMPLETEMPERATURESENSOR_IDENTIFY_TIMEOUT_EVT, 1000 );
        HalLedBlink ( HAL_LED_4, 0xFF, HAL_LED_DEFAULT_DUTY_CYCLE, HAL_LED_DEFAULT_FLASH_TIME );
      }
      else
      {
        if ( zclSampleTemperatureSensor_OnOff )
        {
          HalLedSet ( HAL_LED_4, HAL_LED_MODE_ON );
        }
        else
        {
          HalLedSet ( HAL_LED_4, HAL_LED_MODE_OFF );
        }
    
        osal_stop_timerEx( zclSampleTemperatureSensor_TaskID, SAMPLETEMPERATURESENSOR_IDENTIFY_TIMEOUT_EVT );
      }
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_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 zclSampleTemperatureSensor_BasicResetCB( void )
    {
      // Put device back to factory default settings
      zgWriteStartupOptions( ZG_STARTUP_SET, 3 );   // bit set both default configuration and default network
    
      // restart device
      MT_SysCommandProcessing( aProcessCmd );
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_IdentifyCB
     *
     * @brief   Callback from the ZCL General Cluster Library when
     *          it received an Identity Command for this application.
     *
     * @param   srcAddr - source address and endpoint of the response message
     * @param   identifyTime - the number of seconds to identify yourself
     *
     * @return  none
     */
    static void zclSampleTemperatureSensor_IdentifyCB( zclIdentify_t *pCmd )
    {
      zclSampleTemperatureSensor_IdentifyTime = pCmd->identifyTime;
      zclSampleTemperatureSensor_ProcessIdentifyTimeChange();
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_IdentifyQueryRspCB
     *
     * @brief   Callback from the ZCL General Cluster Library when
     *          it received an Identity Query Response Command for this application.
     *
     * @param   srcAddr - requestor's address
     * @param   timeout - number of seconds to identify yourself (valid for query response)
     *
     * @return  none
     */
    static void zclSampleTemperatureSensor_IdentifyQueryRspCB( zclIdentifyQueryRsp_t *pRsp )
    {
      (void)pRsp;
    #ifdef ZCL_EZMODE
      {
        zclEZMode_ActionData_t data;
        data.pIdentifyQueryRsp = pRsp;
        zcl_EZModeAction ( EZMODE_ACTION_IDENTIFY_QUERY_RSP, &data );
      }
    #endif
    }
    
    /******************************************************************************
     *
     *  Functions for processing ZCL Foundation incoming Command/Response messages
     *
     *****************************************************************************/
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_ProcessIncomingMsg
     *
     * @brief   Process ZCL Foundation incoming message
     *
     * @param   pInMsg - pointer to the received message
     *
     * @return  none
     */
    static void zclSampleTemperatureSensor_ProcessIncomingMsg( zclIncomingMsg_t *pInMsg)
    {
      switch ( pInMsg->zclHdr.commandID )
      {
    #ifdef ZCL_READ
        case ZCL_CMD_READ_RSP:
          zclSampleTemperatureSensor_ProcessInReadRspCmd( pInMsg );
          break;
    #endif
    #ifdef ZCL_WRITE
        case ZCL_CMD_WRITE_RSP:
          zclSampleTemperatureSensor_ProcessInWriteRspCmd( pInMsg );
          break;
    #endif
    #ifdef ZCL_REPORT
        // See ZCL Test Applicaiton (zcl_testapp.c) for sample code on Attribute Reporting
        case ZCL_CMD_CONFIG_REPORT:
          //zclSampleTemperatureSensor_ProcessInConfigReportCmd( pInMsg );
          break;
    
        case ZCL_CMD_CONFIG_REPORT_RSP:
          //zclSampleTemperatureSensor_ProcessInConfigReportRspCmd( pInMsg );
          break;
    
        case ZCL_CMD_READ_REPORT_CFG:
          //zclSampleTemperatureSensor_ProcessInReadReportCfgCmd( pInMsg );
          break;
    
        case ZCL_CMD_READ_REPORT_CFG_RSP:
          //zclSampleTemperatureSensor_ProcessInReadReportCfgRspCmd( pInMsg );
          break;
    
        case ZCL_CMD_REPORT:
          //zclSampleTemperatureSensor_ProcessInReportCmd( pInMsg );
          break;
    #endif
        case ZCL_CMD_DEFAULT_RSP:
          zclSampleTemperatureSensor_ProcessInDefaultRspCmd( pInMsg );
          break;
    #ifdef ZCL_DISCOVER
        case ZCL_CMD_DISCOVER_CMDS_RECEIVED_RSP:
          zclSampleTemperatureSensor_ProcessInDiscCmdsRspCmd( pInMsg );
          break;
    
        case ZCL_CMD_DISCOVER_CMDS_GEN_RSP:
          zclSampleTemperatureSensor_ProcessInDiscCmdsRspCmd( pInMsg );
          break;
    
        case ZCL_CMD_DISCOVER_ATTRS_RSP:
          zclSampleTemperatureSensor_ProcessInDiscAttrsRspCmd( pInMsg );
          break;
    
        case ZCL_CMD_DISCOVER_ATTRS_EXT_RSP:
          zclSampleTemperatureSensor_ProcessInDiscAttrsExtRspCmd( pInMsg );
          break;
    #endif
        default:
          break;
      }
    
      if ( pInMsg->attrCmd )
      {
        osal_mem_free( pInMsg->attrCmd );
      }
    }
    
    #ifdef ZCL_READ
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_ProcessInReadRspCmd
     *
     * @brief   Process the "Profile" Read Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleTemperatureSensor_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      zclSampleTemperatureSensor_ProcessInWriteRspCmd
     *
     * @brief   Process the "Profile" Write Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleTemperatureSensor_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      zclSampleTemperatureSensor_ProcessInDefaultRspCmd
     *
     * @brief   Process the "Profile" Default Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleTemperatureSensor_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      zclSampleTemperatureSensor_ProcessInDiscCmdsRspCmd
     *
     * @brief   Process the Discover Commands Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleTemperatureSensor_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      zclSampleTemperatureSensor_ProcessInDiscAttrsRspCmd
     *
     * @brief   Process the "Profile" Discover Attributes Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleTemperatureSensor_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      zclSampleTemperatureSensor_ProcessInDiscAttrsExtRspCmd
     *
     * @brief   Process the "Profile" Discover Attributes Extended Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleTemperatureSensor_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
    
    #ifdef ZCL_EZMODE
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_ProcessZDOMsgs
     *
     * @brief   Called when this node receives a ZDO/ZDP response.
     *
     * @param   none
     *
     * @return  status
     */
    static void zclSampleTemperatureSensor_ProcessZDOMsgs( zdoIncomingMsg_t *pMsg )
    {
      zclEZMode_ActionData_t data;
      ZDO_MatchDescRsp_t *pMatchDescRsp;
    
      // Let EZ-Mode know of the Match Descriptor Response
      if ( pMsg->clusterID == Match_Desc_rsp )
      {
        pMatchDescRsp = ZDO_ParseEPListRsp( pMsg );
        data.pMatchDescRsp = pMatchDescRsp;
        zcl_EZModeAction( EZMODE_ACTION_MATCH_DESC_RSP, &data );
        osal_mem_free( pMatchDescRsp );
      }
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_EZModeCB
     *
     * @brief   The Application is informed of events. This can be used to show on the UI what is
    *           going on during EZ-Mode steering/finding/binding.
     *
     * @param   state - an
     *
     * @return  none
     */
    static void zclSampleTemperatureSensor_EZModeCB( zlcEZMode_State_t state, zclEZMode_CBData_t *pData )
    {
    #ifdef LCD_SUPPORTED
      char szLine[20];
      char *pStr;
      uint8 err;
    #endif
    
      // time to go into identify mode
      if ( state == EZMODE_STATE_IDENTIFYING )
      {
        zclSampleTemperatureSensor_IdentifyTime = ( EZMODE_TIME / 1000 );  // convert to seconds
        zclSampleTemperatureSensor_ProcessIdentifyTimeChange();
      }
    
      // autoclosing, show what happened (success, cancelled, etc...)
      if( state == EZMODE_STATE_AUTOCLOSE )
      {
    #ifdef LCD_SUPPORTED
        pStr = NULL;
        err = pData->sAutoClose.err;
        if ( err == EZMODE_ERR_SUCCESS )
        {
          pStr = "EZMode: Success";
        }
        else if ( err == EZMODE_ERR_NOMATCH )
        {
          pStr = "EZMode: NoMatch"; // not a match made in heaven
        }
        if ( pStr )
        {
          if ( giTemperatureSensorScreenMode == TEMPSENSE_MAINMODE )
          {
            HalLcdWriteString ( pStr, HAL_LCD_LINE_2 );
          }
        }
    #endif
      }
    
      // finished, either show DstAddr/EP, or nothing (depending on success or not)
      if( state == EZMODE_STATE_FINISH )
      {
        // turn off identify mode
        zclSampleTemperatureSensor_IdentifyTime = 0;
        zclSampleTemperatureSensor_ProcessIdentifyTimeChange();
    
    #ifdef LCD_SUPPORTED
        // if successful, inform user which nwkaddr/ep we bound to
        pStr = NULL;
        err = pData->sFinish.err;
        if( err == EZMODE_ERR_SUCCESS )
        {
          // "EZDst:1234 EP:34"
          osal_memcpy( szLine, "EZDst:", 6 );
          zclHA_uint16toa( pData->sFinish.nwkaddr, &szLine[6] );
          osal_memcpy( &szLine[10], " EP:", 4 );
          _ltoa( pData->sFinish.ep, (void *)(&szLine[14]), 16 );  // _ltoa NULL terminates
          pStr = szLine;
        }
        else if ( err == EZMODE_ERR_BAD_PARAMETER )
        {
          pStr = "EZMode: BadParm";
        }
        else if ( err == EZMODE_ERR_CANCELLED )
        {
          pStr = "EZMode: Cancel";
        }
        else
        {
          pStr = "EZMode: TimeOut";
        }
        if ( pStr )
        {
          if ( giTemperatureSensorScreenMode == TEMPSENSE_MAINMODE )
          {
            HalLcdWriteString ( pStr, HAL_LCD_LINE_2 );
          }
        }
    #endif  // LCD_SUPPORTED
    
        // show main UI screen 3 seconds after joining network
        osal_start_timerEx( zclSampleTemperatureSensor_TaskID, SAMPLETEMPERATURESENSOR_MAIN_SCREEN_EVT, 3000 );
    
        // report current temperature reading 15 seconds after joinging the network
        osal_start_timerEx( zclSampleTemperatureSensor_TaskID, SAMPLETEMPERATURESENSOR_TEMP_SEND_EVT, SAMPLETEMPERATURESENSOR_REPORT_INTERVAL );
      }
    }
    #endif // ZCL_EZMODE
    
    /****************************************************************************
    ****************************************************************************/
    
    
    

  • sir the code is displaying the value in lcd but whn i try to transmit the value 30 in to the coordinator node which contains thermostat code in it ... the value of temp is showing as N/A ?? pl reply asap sir 

  • How do you know your coordinator receives temperature report? Do you check it by Ubiqua Packet Analyzer?

  • the code i modified is just the example pgm which is temperature sensor in ZED and thermostat in coordinator .... so according to the example pgm the data given in ZED 30 will also be diaplayed in the lcd of the coordinator itself ... 

    the files u send is for just printing the value of 30 in the lcd rite , r it suppose to do like displaying the values of temperature sensor .. 

    in the code u send there is and error of 

    Warning[Pe111]: statement is unreachable E:\PROJECT\17$11\30\Z-Stack Home 1.2.1\Projects\zstack\HomeAutomation\SampleTemperatureSensor\Source\zcl_sampletemperaturesensor.c 467
    Linking
    Error[e46]: Undefined external "zclSampleTemperatureSensor_MeasuredValue" referred in zcl_sampletemperaturesensor ( E:\PROJECT\17$11\30\Z-Stack Home
    1.2.1\Projects\zstack\HomeAutomation\SampleTemperatureSensor\CC2530DB\EndDeviceEB\Obj\zcl_sampletemperaturesensor.r51 )
    Error while running Linker which is cleared when i used the initialization of 

    int16 zclSampleTemperatureSensor_MeasuredValue; in temperaturesensor.c

  • I don't know what your problem is. Please refer to Section 3.2.3  "SampleThermostat, SampleTemperatureSensor, SampleHeatingCoolingUnit" in Z-Stack Home Sample Application User's Guide.pdf first.

  • sir actually i saw that sir ... all i wanted to do is to program my ZED where temperaturesensor pgm is dumped ..... the pgm tempsensor in the example is giving a value of 17 to 27 based on the already declared value and it is displayed on the coordinator which is dumpew with thermostat pgm ....i want to use the internal temp sensor of ZED instead of geting the predefined value of the temperature and my ZED should send the tempvalue from its sensor to the coordinator ie thermostat

  • Do you call getTemperature() in your zcl_sampletemperaturesensor.c? There is a while(1) in your getTemperature() and it will get stuck in there.

  • 7801.zcl_sampletemperaturesensor.c
    /**************************************************************************************************
      Filename:       zcl_sampletemperaturesensor.c
      Revised:        $Date: 2013-10-18 11:49:27 -0700 (Fri, 18 Oct 2013) $
      Revision:       $Revision: 35718 $
    
      Description:    Zigbee Cluster Library - sample device application.
    
    
      Copyright 2013 Texas Instruments Incorporated. All rights reserved.
    
      IMPORTANT: Your use of this Software is limited to those specific rights
      granted under the terms of a software license agreement between the user
      who downloaded the software, his/her employer (which must be your employer)
      and Texas Instruments Incorporated (the "License").  You may not use this
      Software unless you agree to abide by the terms of the License. The License
      limits your use, and you acknowledge, that the Software may not be modified,
      copied or distributed unless embedded on a Texas Instruments microcontroller
      or used solely and exclusively in conjunction with a Texas Instruments radio
      frequency transceiver, which is integrated into your product.  Other than for
      the foregoing purpose, you may not use, reproduce, copy, prepare derivative
      works of, modify, distribute, perform, display or sell this Software and/or
      its documentation for any purpose.
    
      YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE
      PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,
      INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE,
      NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL
      TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT,
      NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER
      LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
      INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE
      OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT
      OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
      (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
    
      Should you have any questions regarding your right to use this Software,
      contact Texas Instruments Incorporated at www.TI.com.
    **************************************************************************************************/
    
    /*********************************************************************
      This device will act as a temperature sensor. It updates the current
      temperature on the thermostat when the user sends the desired
      temperature using SW1.
    
      SCREEN MODES
      ----------------------------------------
      Main:
        - SW1: Send current temperature
        - SW2: Invoke EZMode
        - SW3: Adjust temperature
        - SW5: Go to Help screen
    
      Temperature:
        - SW1: Increase temperature
        - SW3: Decrease temperature
        - SW5: Enter temperature
      ----------------------------------------
    *********************************************************************/
    
    /*********************************************************************
     * INCLUDES
     */
    #include "ZComDef.h"
    #include "OSAL.h"
    #include "AF.h"
    #include "ZDApp.h"
    #include "ZDObject.h"
    #include "ZDProfile.h"
    #include "MT_SYS.h"
    
    #include "zcl.h"
    #include "zcl_general.h"
    #include "zcl_ha.h"
    #include "zcl_ezmode.h"
    #include "zcl_ms.h"
    #include "hal_adc.h"
    
    #include "zcl_sampletemperaturesensor.h"
    
    #include "onboard.h"
    
    /* HAL */
    #include "hal_lcd.h"
    #include "hal_led.h"
    #include "hal_key.h"
    //#include "zcl_sampletemperaturesensor_data.c"
    
    /*********************************************************************
     * MACROS
     */
    
    // how often to report temperature
    #define SAMPLETEMPERATURESENSOR_REPORT_INTERVAL   3000
    
    /*********************************************************************
     * CONSTANTS
     */
    
    /*********************************************************************
     * TYPEDEFS
     */
    /*#ifdef __IAR_SYSTEMS_ICC__
    #define  CODE   __code
    #define  XDATA  __xdata
    */
    
    /*********************************************************************
     * GLOBAL VARIABLES
     */
    byte zclSampleTemperatureSensor_TaskID;
    
    uint8 zclSampleTemperatureSensorSeqNum;
    
    static byte gPermitDuration = 0x00;
    /*void initTempSensor(void){
       DISABLE_ALL_INTERRUPTS();            //??????
       InitClock();                         //????????32M
       *((BYTE XDATA*) 0x624B) = 0x01;    //???????
       *((BYTE XDATA*) 0x61BD) = 0x01;    //???????ADC????
    }*/
    
    /*********************************************************************
     * GLOBAL FUNCTIONS
     */
    
    /*********************************************************************
     * LOCAL VARIABLES
     */
    afAddrType_t zclSampleTemperatureSensor_DstAddr;
    
    #ifdef ZCL_EZMODE
    static void zclSampleTemperatureSensor_ProcessZDOMsgs( zdoIncomingMsg_t *pMsg );
    static void zclSampleTemperatureSensor_EZModeCB( zlcEZMode_State_t state, zclEZMode_CBData_t *pData );
    
    static const zclEZMode_RegisterData_t zclSampleTemperatureSensor_RegisterEZModeData =
    {
      &zclSampleTemperatureSensor_TaskID,
      SAMPLETEMPERATURESENSOR_EZMODE_NEXTSTATE_EVT,
      SAMPLETEMPERATURESENSOR_EZMODE_TIMEOUT_EVT,
      &zclSampleTemperatureSensorSeqNum,
      zclSampleTemperatureSensor_EZModeCB
    };
    
    // NOT ZCL_EZMODE, Use EndDeviceBind
    #else
    
    static cId_t bindingOutClusters[] =
    {
      ZCL_CLUSTER_ID_MS_TEMPERATURE_MEASUREMENT
    };
    #define ZCLSAMPLETEMPERATURESENSOR_BINDINGLIST        1
    #endif
    
    devStates_t zclSampleTemperatureSensor_NwkState = DEV_INIT;
    
    uint8 giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;   // display main screen mode first
    
    static uint8 aProcessCmd[] = { 1, 0, 0, 0 }; // used for reset command, { length + cmd0 + cmd1 + data }
    
    // Test Endpoint to allow SYS_APP_MSGs
    static endPointDesc_t sampleTemperatureSensor_TestEp =
    {
      20,                                 // Test endpoint
      &zclSampleTemperatureSensor_TaskID,
      (SimpleDescriptionFormat_t *)NULL,  // No Simple description for this test endpoint
      (afNetworkLatencyReq_t)0            // No Network Latency req
    };
    
    
    /*********************************************************************
     * LOCAL FUNCTIONS
     */
    static void zclSampleTemperatureSensor_HandleKeys( byte shift, byte keys );
    static void zclSampleTemperatureSensor_BasicResetCB( void );
    static void zclSampleTemperatureSensor_IdentifyCB( zclIdentify_t *pCmd );
    static void zclSampleTemperatureSensor_IdentifyQueryRspCB( zclIdentifyQueryRsp_t *pRsp );
    static void zclSampleTemperatureSensor_ProcessIdentifyTimeChange( void );
    //void getTemperature(void);
    // app display functions
    void zclSampleTemperatureSensor_LcdDisplayUpdate(void);
    void zclSampleTemperatureSensor_LcdDisplayMainMode(void);
    void zclSampleTemperatureSensor_LcdDisplayTempMode(void);
    void zclSampleTemperatureSensor_LcdDisplayHelpMode(void);
    //int16 getTemperature(void);
    int16 zclSampleTemperatureSensor_MeasuredValue;
    
    static void zclSampleTemperatureSensor_SendTemp(void);
    
    // Functions to process ZCL Foundation incoming Command/Response messages
    static void zclSampleTemperatureSensor_ProcessIncomingMsg( zclIncomingMsg_t *msg );
    #ifdef ZCL_READ
    static uint8 zclSampleTemperatureSensor_ProcessInReadRspCmd( zclIncomingMsg_t *pInMsg );
    #endif
    #ifdef ZCL_WRITE
    static uint8 zclSampleTemperatureSensor_ProcessInWriteRspCmd( zclIncomingMsg_t *pInMsg );
    #endif
    static uint8 zclSampleTemperatureSensor_ProcessInDefaultRspCmd( zclIncomingMsg_t *pInMsg );
    #ifdef ZCL_DISCOVER
    static uint8 zclSampleTemperatureSensor_ProcessInDiscCmdsRspCmd( zclIncomingMsg_t *pInMsg );
    static uint8 zclSampleTemperatureSensor_ProcessInDiscAttrsRspCmd( zclIncomingMsg_t *pInMsg );
    static uint8 zclSampleTemperatureSensor_ProcessInDiscAttrsExtRspCmd( zclIncomingMsg_t *pInMsg );
    #endif // ZCL_DISCOVER
    
    /*********************************************************************
     * STATUS STRINGS
     */
    #ifdef LCD_SUPPORTED
    const char sClearLine[]    = " ";
    const char sDeviceName[]   = "  Temp Sensor";
    const char sSwTempUp[]     = "SW1: Raise Temp";
    const char sSwEZMode[]     = "SW2: EZ-Mode";
    const char sSwTempDown[]   = "SW3: Lower Temp";
    const char sSwHelp[]       = "SW5: Help";
    #endif
    
    /*********************************************************************
     * ZCL General Profile Callback table
     */
    static zclGeneral_AppCallbacks_t zclSampleTemperatureSensor_CmdCallbacks =
    {
      zclSampleTemperatureSensor_BasicResetCB,        // Basic Cluster Reset command
      zclSampleTemperatureSensor_IdentifyCB,          // Identify command
    #ifdef ZCL_EZMODE
      NULL,                                           // Identify EZ-Mode Invoke command
      NULL,                                           // Identify Update Commission State command
    #endif
      NULL,                                           // Identify Trigger Effect command
      zclSampleTemperatureSensor_IdentifyQueryRspCB,  // Identify Query Response command
      NULL,             				                      // On/Off cluster command
      NULL,                                           // On/Off cluster enhanced command Off with Effect
      NULL,                                           // On/Off cluster enhanced command On with Recall Global Scene
      NULL,                                           // On/Off cluster enhanced command On with Timed Off
    #ifdef ZCL_LEVEL_CTRL
      NULL,                                           // Level Control Move to Level command
      NULL,                                           // Level Control Move command
      NULL,                                           // Level Control Step command
      NULL,                                           // Level Control Stop command
    #endif
    #ifdef ZCL_GROUPS
      NULL,                                           // Group Response commands
    #endif
    #ifdef ZCL_SCENES
      NULL,                                           // Scene Store Request command
      NULL,                                           // Scene Recall Request command
      NULL,                                           // Scene Response command
    #endif
    #ifdef ZCL_ALARMS
      NULL,                                           // Alarm (Response) commands
    #endif
    #ifdef SE_UK_EXT
      NULL,                                           // Get Event Log command
      NULL,                                           // Publish Event Log command
    #endif
      NULL,                                           // RSSI Location command
      NULL                                            // RSSI Location Response command
    };
    
    /*********************************************************************
     * @fn          zclSampleTemperatureSensor_Init
     *
     * @brief       Initialization function for the zclGeneral layer.
     *
     * @param       none
     *
     * @return      none
     */
    void zclSampleTemperatureSensor_Init( byte task_id )
    {
      zclSampleTemperatureSensor_TaskID = task_id;
    
      // Set destination address to indirect
      zclSampleTemperatureSensor_DstAddr.addrMode = (afAddrMode_t)AddrNotPresent;
      zclSampleTemperatureSensor_DstAddr.endPoint = 0;
      zclSampleTemperatureSensor_DstAddr.addr.shortAddr = 0;
    
      // This app is part of the Home Automation Profile
      zclHA_Init( &zclSampleTemperatureSensor_SimpleDesc );
    
      // Register the ZCL General Cluster Library callback functions
      zclGeneral_RegisterCmdCallbacks( SAMPLETEMPERATURESENSOR_ENDPOINT, &zclSampleTemperatureSensor_CmdCallbacks );
    
      // Register the application's attribute list
      zcl_registerAttrList( SAMPLETEMPERATURESENSOR_ENDPOINT, SAMPLETEMPERATURESENSOR_MAX_ATTRIBUTES, zclSampleTemperatureSensor_Attrs );
    
      // Register the Application to receive the unprocessed Foundation command/response messages
      zcl_registerForMsg( zclSampleTemperatureSensor_TaskID );
    
    #ifdef ZCL_EZMODE
      // Register EZ-Mode
      zcl_RegisterEZMode( &zclSampleTemperatureSensor_RegisterEZModeData );
    
      // Register with the ZDO to receive Match Descriptor Responses
      ZDO_RegisterForZDOMsg(task_id, Match_Desc_rsp);
    #endif
    
      // Register for all key events - This app will handle all key events
      RegisterForKeys( zclSampleTemperatureSensor_TaskID );
    
      // Register for a test endpoint
      afRegister( &sampleTemperatureSensor_TestEp );
    
    #ifdef LCD_SUPPORTED
      // display the device name
      HalLcdWriteString( (char *)sDeviceName, HAL_LCD_LINE_3 );
    #endif
      osal_start_timerEx( zclSampleTemperatureSensor_TaskID, SAMPLETEMPERATURESENSOR_ADC, SAMPLETEMPERATURESENSOR_REPORT_INTERVAL );
    
    }
    
    /*********************************************************************
     * @fn          zclSample_event_loop
     *
     * @brief       Event Loop Processor for zclGeneral.
     *
     * @param       none
     *
     * @return      none
     */
    uint16 zclSampleTemperatureSensor_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( zclSampleTemperatureSensor_TaskID )) )
        {
          switch ( MSGpkt->hdr.event )
          {
    #ifdef ZCL_EZMODE
            case ZDO_CB_MSG:
              zclSampleTemperatureSensor_ProcessZDOMsgs( (zdoIncomingMsg_t *)MSGpkt );
              break;
    #endif
    
            case ZCL_INCOMING_MSG:
              // Incoming ZCL Foundation command/response messages
              zclSampleTemperatureSensor_ProcessIncomingMsg( (zclIncomingMsg_t *)MSGpkt );
              break;
    
            case KEY_CHANGE:
              zclSampleTemperatureSensor_HandleKeys( ((keyChange_t *)MSGpkt)->state, ((keyChange_t *)MSGpkt)->keys );
              break;
    
            case ZDO_STATE_CHANGE:
              zclSampleTemperatureSensor_NwkState = (devStates_t)(MSGpkt->hdr.status);
    
    
              // now on the network
              if ( (zclSampleTemperatureSensor_NwkState == DEV_ZB_COORD) ||
                   (zclSampleTemperatureSensor_NwkState == DEV_ROUTER)   ||
                   (zclSampleTemperatureSensor_NwkState == DEV_END_DEVICE) )
              {
    #ifndef HOLD_AUTO_START
                giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;
                zclSampleTemperatureSensor_LcdDisplayUpdate();
    #endif
    #ifdef ZCL_EZMODE
                zcl_EZModeAction( EZMODE_ACTION_NETWORK_STARTED, NULL );
    #endif // ZCL_EZMODE
              }
              break;
    
            default:
              break;
          }
    
          // Release the memory
          osal_msg_deallocate( (uint8 *)MSGpkt );
        }
    
        // return unprocessed events
        return (events ^ SYS_EVENT_MSG);
      }
    
      if ( events & SAMPLETEMPERATURESENSOR_IDENTIFY_TIMEOUT_EVT )
      {
        if ( zclSampleTemperatureSensor_IdentifyTime > 0 )
          zclSampleTemperatureSensor_IdentifyTime--;
        zclSampleTemperatureSensor_ProcessIdentifyTimeChange();
    
        return ( events ^ SAMPLETEMPERATURESENSOR_IDENTIFY_TIMEOUT_EVT );
      }
    
    #ifdef ZCL_EZMODE
      // going on to next state
      if ( events & SAMPLETEMPERATURESENSOR_EZMODE_NEXTSTATE_EVT )
      {
        zcl_EZModeAction ( EZMODE_ACTION_PROCESS, NULL );   // going on to next state
        return ( events ^ SAMPLETEMPERATURESENSOR_EZMODE_NEXTSTATE_EVT );
      }
    
      // the overall EZMode timer expired, so we timed out
      if ( events & SAMPLETEMPERATURESENSOR_EZMODE_TIMEOUT_EVT )
      {
        zcl_EZModeAction ( EZMODE_ACTION_TIMED_OUT, NULL ); // EZ-Mode timed out
        return ( events ^ SAMPLETEMPERATURESENSOR_EZMODE_TIMEOUT_EVT );
      }
    #endif // ZLC_EZMODE
    
      if ( events & SAMPLETEMPERATURESENSOR_MAIN_SCREEN_EVT )
      {
        giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;
        zclSampleTemperatureSensor_LcdDisplayUpdate();
    
        return ( events ^ SAMPLETEMPERATURESENSOR_MAIN_SCREEN_EVT );
      }
    
      if (events & SAMPLETEMPERATURESENSOR_ADC)
      {
       int16 AvgTemp;
      uint8   i;
      uint16  AdcValue;
      uint16  value;
      uint16  value1;
    //char i;
            //char TempValue[10]; 
            //initTempSensor(); 
      AdcValue = 0;
      for( i = 0; i < 4; i++ )
      {
    ADCIF = 0;
        ADCCON3 = (HAL_ADC_REF_125V | HAL_ADC_RESOLUTION_14 | HAL_ADC_CHN_TEMP);    // ??1.25V????,12????,AD??:?????
            while ( !ADCIF );                                              //?????ADC
                     
        value =  ADCL >> 2;                        
        value |= (((UINT16)ADCH) << 6);    
        AdcValue += value;                          
      }
      value = AdcValue >> 2;                        //????4,?????
      value1 =   (((value) >> 4) - 335);
                 //??AD?,????????
     // while(1)
      //      {
             AvgTemp = 0;
             for(i = 0 ; i < 64 ; i++)
              {
                AvgTemp += value1;              
                AvgTemp >>= 1;                              //??????2.
              }
              
        //    }
        zclSampleTemperatureSensor_MeasuredValue = AvgTemp ;
        zclSampleTemperatureSensor_LcdDisplayUpdate();
        osal_start_timerEx( zclSampleTemperatureSensor_TaskID, SAMPLETEMPERATURESENSOR_ADC, SAMPLETEMPERATURESENSOR_REPORT_INTERVAL );
        return ( events ^ SAMPLETEMPERATURESENSOR_ADC );
      }
    
      if ( events & SAMPLETEMPERATURESENSOR_TEMP_SEND_EVT )
      {
        zclSampleTemperatureSensor_SendTemp();
    
        // report current temperature reading every 10 seconds
        osal_start_timerEx( zclSampleTemperatureSensor_TaskID, SAMPLETEMPERATURESENSOR_TEMP_SEND_EVT, SAMPLETEMPERATURESENSOR_REPORT_INTERVAL );
    
        return ( events ^ SAMPLETEMPERATURESENSOR_TEMP_SEND_EVT );
      }
    
      // Discard unknown events
      return 0;
    }
    
    /*int16 getTemperature(void)
    {
      int16 AvgTemp;
      uint8   i;
      uint16  AdcValue;
      uint16  value;
      uint16  value1;
    //char i;
            //char TempValue[10]; 
            //initTempSensor(); 
      AdcValue = 0;
      for( i = 0; i < 4; i++ )
      {
    ADCIF = 0;
        ADCCON3 = (HAL_ADC_REF_125V | HAL_ADC_RESOLUTION_14 | HAL_ADC_CHN_TEMP);    // ??1.25V????,12????,AD??:?????
            while ( !ADCIF );                                              //?????ADC
                     
        value =  ADCL >> 2;                        
        value |= (((UINT16)ADCH) << 6);    
        AdcValue += value;                          
      }
      value = AdcValue >> 2;                        //????4,?????
      value1 =   (((value) >> 4) - 335);
                 //??AD?,????????
      while(1)
            {
              AvgTemp = 0;
              for(i = 0 ; i < 64 ; i++)
              {
                AvgTemp += value1;              
                AvgTemp >>= 1;                              //??????2.
              }
              
            }
      //zclSampleTemperatureSensor_MeasuredValue = AvgTemp ; 
      return AvgTemp;
    }*/
    
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_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_3
     *                 HAL_KEY_SW_2
     *                 HAL_KEY_SW_1
     *
     * @return  none
     */
    static void zclSampleTemperatureSensor_HandleKeys( byte shift, byte keys )
    {
      if ( keys & HAL_KEY_SW_1 )
      {
        // increase temperature
        giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;
    
        if ( zclSampleTemperatureSensor_MeasuredValue < zclSampleTemperatureSensor_MaxMeasuredValue )
        {
          zclSampleTemperatureSensor_MeasuredValue = zclSampleTemperatureSensor_MeasuredValue + 100;  // considering using whole number value
        }
        else if ( zclSampleTemperatureSensor_MeasuredValue >= zclSampleTemperatureSensor_MaxMeasuredValue )
        {
          zclSampleTemperatureSensor_MeasuredValue = zclSampleTemperatureSensor_MaxMeasuredValue;
        }
    
        // Send temperature information
        zclSampleTemperatureSensor_SendTemp();
      }
    
      if ( keys & HAL_KEY_SW_2 )
      {
        if ( ( giTemperatureSensorScreenMode == TEMPSENSE_MAINMODE ) ||
            ( giTemperatureSensorScreenMode == TEMPSENSE_HELPMODE ) )
        {
          giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;
    
    #ifdef ZCL_EZMODE
          zclEZMode_InvokeData_t ezModeData;
          static uint16 clusterIDs[] = { ZCL_CLUSTER_ID_MS_TEMPERATURE_MEASUREMENT };   // only bind on the Temperature Measurement cluster
    
          // Invoke EZ-Mode
          ezModeData.endpoint = SAMPLETEMPERATURESENSOR_ENDPOINT; // endpoint on which to invoke EZ-Mode
          if ( ( zclSampleTemperatureSensor_NwkState == DEV_ZB_COORD ) ||
               ( zclSampleTemperatureSensor_NwkState == DEV_ROUTER )   ||
               ( zclSampleTemperatureSensor_NwkState == DEV_END_DEVICE ) )
          {
            ezModeData.onNetwork = TRUE;      // node is already on the network
          }
          else
          {
            ezModeData.onNetwork = FALSE;     // node is not yet on the network
          }
          ezModeData.initiator = TRUE;        // Temperature Sensor is an initiator
          ezModeData.numActiveInClusters = 1;
          ezModeData.pActiveInClusterIDs = clusterIDs;
          ezModeData.numActiveOutClusters = 0;   // active output cluster
          ezModeData.pActiveOutClusterIDs = NULL;
          zcl_InvokeEZMode( &ezModeData );
    
    #ifdef LCD_SUPPORTED
          HalLcdWriteString( "EZMode", HAL_LCD_LINE_2 );
    #endif
    
          // NOT ZCL_EZMODE, Use EndDeviceBind
    #else
          {
            zAddrType_t dstAddr;
            dstAddr.addrMode = Addr16Bit;
            dstAddr.addr.shortAddr = 0;   // Coordinator makes the EDB match
    
            // Initiate an End Device Bind Request, this bind request will
            // only use a cluster list that is important to binding.
            HalLedSet ( HAL_LED_4, HAL_LED_MODE_OFF );
            ZDP_EndDeviceBindReq( &dstAddr, NLME_GetShortAddr(),
                                  SAMPLETEMPERATURESENSOR_ENDPOINT,
                                  ZCL_HA_PROFILE_ID,
                                  0, NULL,
                                  ZCLSAMPLETEMPERATURESENSOR_BINDINGLIST, bindingOutClusters,
                                  FALSE );
          }
    #endif // ZCL_EZMODE
        }
      }
    
      if ( keys & HAL_KEY_SW_3 )
      {
        giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;
    
        // decrease the temperature
        if ( zclSampleTemperatureSensor_MeasuredValue > zclSampleTemperatureSensor_MinMeasuredValue )
        {
          zclSampleTemperatureSensor_MeasuredValue = zclSampleTemperatureSensor_MeasuredValue - 100;  // considering using whole number value
        }
        else if ( zclSampleTemperatureSensor_MeasuredValue >= zclSampleTemperatureSensor_MinMeasuredValue )
        {
          zclSampleTemperatureSensor_MeasuredValue = zclSampleTemperatureSensor_MinMeasuredValue;
        }
    
        // Send temperature information
        zclSampleTemperatureSensor_SendTemp();
      }
    
      if ( keys & HAL_KEY_SW_4 )
      {
        giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;
    
        if ( ( zclSampleTemperatureSensor_NwkState == DEV_ZB_COORD ) ||
             ( zclSampleTemperatureSensor_NwkState == DEV_ROUTER ) )
        {
          // toggle permit join
          gPermitDuration = gPermitDuration ? 0 : 0xff;
          NLME_PermitJoiningRequest( gPermitDuration );
        }
      }
    
      if ( shift && ( keys & HAL_KEY_SW_5 ) )
      {
        zclSampleTemperatureSensor_BasicResetCB();
      }
      else if ( keys & HAL_KEY_SW_5 )
      {
        if ( giTemperatureSensorScreenMode == TEMPSENSE_MAINMODE )
        {
          giTemperatureSensorScreenMode = TEMPSENSE_HELPMODE;
        }
        else if ( giTemperatureSensorScreenMode == TEMPSENSE_HELPMODE )
        {
    #ifdef LCD_SUPPORTED
          HalLcdWriteString( (char *)sClearLine, HAL_LCD_LINE_2 );
    #endif
          giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;
        }
      }
    
      // update display
      zclSampleTemperatureSensor_LcdDisplayUpdate();
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_LcdDisplayUpdate
     *
     * @brief   Called to update the LCD display.
     *
     * @param   none
     *
     * @return  none
     */
    void zclSampleTemperatureSensor_LcdDisplayUpdate( void )
    {
      // turn on red LED for temperatures >= 24.00C
      if ( zclSampleTemperatureSensor_MeasuredValue >= 2400 )
      {
        HalLedSet ( HAL_LED_1, HAL_LED_MODE_OFF );
        HalLedSet ( HAL_LED_2, HAL_LED_MODE_ON );
      }
      // turn on green LED for temperatures <= 20.00C
      else if ( zclSampleTemperatureSensor_MeasuredValue <= 2000 )
      {
        HalLedSet ( HAL_LED_1, HAL_LED_MODE_ON );
        HalLedSet ( HAL_LED_2, HAL_LED_MODE_OFF );
      }
      // turn on both red and green LEDs for temperatures between 20.00C and 24.00C
      else
      {
        HalLedSet ( HAL_LED_1, HAL_LED_MODE_ON );
        HalLedSet ( HAL_LED_2, HAL_LED_MODE_ON );
      }
    
      if ( giTemperatureSensorScreenMode == TEMPSENSE_HELPMODE )
      {
        zclSampleTemperatureSensor_LcdDisplayHelpMode();
      }
      else
      {
        zclSampleTemperatureSensor_LcdDisplayMainMode();
      }
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_LcdDisplayMainMode
     *
     * @brief   Called to display the main screen on the LCD.
     *
     * @param   none
     *
     * @return  none
     */
    void zclSampleTemperatureSensor_LcdDisplayMainMode( void )
    {
      char sDisplayTemp[16];
    
      if ( zclSampleTemperatureSensor_NwkState == DEV_ZB_COORD )
      {
        zclHA_LcdStatusLine1( 0 );
      }
      else if ( zclSampleTemperatureSensor_NwkState == DEV_ROUTER )
      {
        zclHA_LcdStatusLine1( 1 );
      }
      else if ( zclSampleTemperatureSensor_NwkState == DEV_END_DEVICE )
      {
        zclHA_LcdStatusLine1( 2 );
      }
    
      // display current temperature
      osal_memcpy(sDisplayTemp, "TEMP: ", 6);
      _ltoa( ( zclSampleTemperatureSensor_MeasuredValue / 100 ), (void *)(&sDisplayTemp[6]), 10 );   // convert temperature to whole number
      osal_memcpy( &sDisplayTemp[8], "C", 2 );
    #ifdef LCD_SUPPORTED
      HalLcdWriteString( (char *)sDisplayTemp, HAL_LCD_LINE_2 );
    #endif
    
    #ifdef LCD_SUPPORTED
      if ( ( zclSampleTemperatureSensor_NwkState == DEV_ZB_COORD ) ||
           ( zclSampleTemperatureSensor_NwkState == DEV_ROUTER ) )
      {
        // display help key with permit join status
        if ( gPermitDuration )
        {
          HalLcdWriteString( "SW5: Help      *", HAL_LCD_LINE_3 );
        }
        else
        {
          HalLcdWriteString( "SW5: Help       ", HAL_LCD_LINE_3 );
        }
      }
      else
      {
        // display help key
        HalLcdWriteString( (char *)sSwHelp, HAL_LCD_LINE_3 );
      }
    #endif
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_LcdDisplayHelpMode
     *
     * @brief   Called to display the SW options on the LCD.
     *
     * @param   none
     *
     * @return  none
     */
    void zclSampleTemperatureSensor_LcdDisplayHelpMode( void )
    {
    #ifdef LCD_SUPPORTED
      HalLcdWriteString( (char *)sSwTempUp, HAL_LCD_LINE_1 );
      HalLcdWriteString( (char *)sSwEZMode, HAL_LCD_LINE_2 );
      HalLcdWriteString( (char *)sSwTempDown, HAL_LCD_LINE_3 );
    #endif
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_SendTemp
     *
     * @brief   Called to send current temperature information to the thermostat
     *
     * @param   none
     *
     * @return  none
     */
    static void zclSampleTemperatureSensor_SendTemp( void )
    {
    #ifdef ZCL_REPORT
      zclReportCmd_t *pReportCmd;
      //zclSampleTemperatureSensor_MeasuredValue = getTemperature();
      pReportCmd = osal_mem_alloc( sizeof(zclReportCmd_t) + sizeof(zclReport_t) );
      if ( pReportCmd != NULL )
      {
        pReportCmd->numAttr = 1;
        pReportCmd->attrList[0].attrID = ATTRID_MS_TEMPERATURE_MEASURED_VALUE;
        pReportCmd->attrList[0].dataType = ZCL_DATATYPE_INT16;
        pReportCmd->attrList[0].attrData = (void *)(&zclSampleTemperatureSensor_MeasuredValue);
    
        zcl_SendReportCmd( SAMPLETEMPERATURESENSOR_ENDPOINT, &zclSampleTemperatureSensor_DstAddr,
                           ZCL_CLUSTER_ID_MS_TEMPERATURE_MEASUREMENT,
                           pReportCmd, ZCL_FRAME_SERVER_CLIENT_DIR, TRUE, zclSampleTemperatureSensorSeqNum++ );
      }
    
      osal_mem_free( pReportCmd );
    #endif  // ZCL_REPORT
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_ProcessIdentifyTimeChange
     *
     * @brief   Called to process any change to the IdentifyTime attribute.
     *
     * @param   none
     *
     * @return  none
     */
    static void zclSampleTemperatureSensor_ProcessIdentifyTimeChange( void )
    {
      if ( zclSampleTemperatureSensor_IdentifyTime > 0 )
      {
        osal_start_timerEx( zclSampleTemperatureSensor_TaskID, SAMPLETEMPERATURESENSOR_IDENTIFY_TIMEOUT_EVT, 1000 );
        HalLedBlink ( HAL_LED_4, 0xFF, HAL_LED_DEFAULT_DUTY_CYCLE, HAL_LED_DEFAULT_FLASH_TIME );
      }
      else
      {
        if ( zclSampleTemperatureSensor_OnOff )
        {
          HalLedSet ( HAL_LED_4, HAL_LED_MODE_ON );
        }
        else
        {
          HalLedSet ( HAL_LED_4, HAL_LED_MODE_OFF );
        }
    
        osal_stop_timerEx( zclSampleTemperatureSensor_TaskID, SAMPLETEMPERATURESENSOR_IDENTIFY_TIMEOUT_EVT );
      }
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_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 zclSampleTemperatureSensor_BasicResetCB( void )
    {
      // Put device back to factory default settings
      zgWriteStartupOptions( ZG_STARTUP_SET, 3 );   // bit set both default configuration and default network
    
      // restart device
      MT_SysCommandProcessing( aProcessCmd );
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_IdentifyCB
     *
     * @brief   Callback from the ZCL General Cluster Library when
     *          it received an Identity Command for this application.
     *
     * @param   srcAddr - source address and endpoint of the response message
     * @param   identifyTime - the number of seconds to identify yourself
     *
     * @return  none
     */
    static void zclSampleTemperatureSensor_IdentifyCB( zclIdentify_t *pCmd )
    {
      zclSampleTemperatureSensor_IdentifyTime = pCmd->identifyTime;
      zclSampleTemperatureSensor_ProcessIdentifyTimeChange();
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_IdentifyQueryRspCB
     *
     * @brief   Callback from the ZCL General Cluster Library when
     *          it received an Identity Query Response Command for this application.
     *
     * @param   srcAddr - requestor's address
     * @param   timeout - number of seconds to identify yourself (valid for query response)
     *
     * @return  none
     */
    static void zclSampleTemperatureSensor_IdentifyQueryRspCB( zclIdentifyQueryRsp_t *pRsp )
    {
      (void)pRsp;
    #ifdef ZCL_EZMODE
      {
        zclEZMode_ActionData_t data;
        data.pIdentifyQueryRsp = pRsp;
        zcl_EZModeAction ( EZMODE_ACTION_IDENTIFY_QUERY_RSP, &data );
      }
    #endif
    }
    
    /******************************************************************************
     *
     *  Functions for processing ZCL Foundation incoming Command/Response messages
     *
     *****************************************************************************/
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_ProcessIncomingMsg
     *
     * @brief   Process ZCL Foundation incoming message
     *
     * @param   pInMsg - pointer to the received message
     *
     * @return  none
     */
    static void zclSampleTemperatureSensor_ProcessIncomingMsg( zclIncomingMsg_t *pInMsg)
    {
      switch ( pInMsg->zclHdr.commandID )
      {
    #ifdef ZCL_READ
        case ZCL_CMD_READ_RSP:
          zclSampleTemperatureSensor_ProcessInReadRspCmd( pInMsg );
          break;
    #endif
    #ifdef ZCL_WRITE
        case ZCL_CMD_WRITE_RSP:
          zclSampleTemperatureSensor_ProcessInWriteRspCmd( pInMsg );
          break;
    #endif
    #ifdef ZCL_REPORT
        // See ZCL Test Applicaiton (zcl_testapp.c) for sample code on Attribute Reporting
        case ZCL_CMD_CONFIG_REPORT:
          //zclSampleTemperatureSensor_ProcessInConfigReportCmd( pInMsg );
          break;
    
        case ZCL_CMD_CONFIG_REPORT_RSP:
          //zclSampleTemperatureSensor_ProcessInConfigReportRspCmd( pInMsg );
          break;
    
        case ZCL_CMD_READ_REPORT_CFG:
          //zclSampleTemperatureSensor_ProcessInReadReportCfgCmd( pInMsg );
          break;
    
        case ZCL_CMD_READ_REPORT_CFG_RSP:
          //zclSampleTemperatureSensor_ProcessInReadReportCfgRspCmd( pInMsg );
          break;
    
        case ZCL_CMD_REPORT:
          //zclSampleTemperatureSensor_ProcessInReportCmd( pInMsg );
          break;
    #endif
        case ZCL_CMD_DEFAULT_RSP:
          zclSampleTemperatureSensor_ProcessInDefaultRspCmd( pInMsg );
          break;
    #ifdef ZCL_DISCOVER
        case ZCL_CMD_DISCOVER_CMDS_RECEIVED_RSP:
          zclSampleTemperatureSensor_ProcessInDiscCmdsRspCmd( pInMsg );
          break;
    
        case ZCL_CMD_DISCOVER_CMDS_GEN_RSP:
          zclSampleTemperatureSensor_ProcessInDiscCmdsRspCmd( pInMsg );
          break;
    
        case ZCL_CMD_DISCOVER_ATTRS_RSP:
          zclSampleTemperatureSensor_ProcessInDiscAttrsRspCmd( pInMsg );
          break;
    
        case ZCL_CMD_DISCOVER_ATTRS_EXT_RSP:
          zclSampleTemperatureSensor_ProcessInDiscAttrsExtRspCmd( pInMsg );
          break;
    #endif
        default:
          break;
      }
    
      if ( pInMsg->attrCmd )
      {
        osal_mem_free( pInMsg->attrCmd );
      }
    }
    
    #ifdef ZCL_READ
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_ProcessInReadRspCmd
     *
     * @brief   Process the "Profile" Read Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleTemperatureSensor_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      zclSampleTemperatureSensor_ProcessInWriteRspCmd
     *
     * @brief   Process the "Profile" Write Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleTemperatureSensor_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      zclSampleTemperatureSensor_ProcessInDefaultRspCmd
     *
     * @brief   Process the "Profile" Default Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleTemperatureSensor_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      zclSampleTemperatureSensor_ProcessInDiscCmdsRspCmd
     *
     * @brief   Process the Discover Commands Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleTemperatureSensor_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      zclSampleTemperatureSensor_ProcessInDiscAttrsRspCmd
     *
     * @brief   Process the "Profile" Discover Attributes Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleTemperatureSensor_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      zclSampleTemperatureSensor_ProcessInDiscAttrsExtRspCmd
     *
     * @brief   Process the "Profile" Discover Attributes Extended Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleTemperatureSensor_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
    
    #ifdef ZCL_EZMODE
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_ProcessZDOMsgs
     *
     * @brief   Called when this node receives a ZDO/ZDP response.
     *
     * @param   none
     *
     * @return  status
     */
    static void zclSampleTemperatureSensor_ProcessZDOMsgs( zdoIncomingMsg_t *pMsg )
    {
      zclEZMode_ActionData_t data;
      ZDO_MatchDescRsp_t *pMatchDescRsp;
    
      // Let EZ-Mode know of the Match Descriptor Response
      if ( pMsg->clusterID == Match_Desc_rsp )
      {
        pMatchDescRsp = ZDO_ParseEPListRsp( pMsg );
        data.pMatchDescRsp = pMatchDescRsp;
        zcl_EZModeAction( EZMODE_ACTION_MATCH_DESC_RSP, &data );
        osal_mem_free( pMatchDescRsp );
      }
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_EZModeCB
     *
     * @brief   The Application is informed of events. This can be used to show on the UI what is
    *           going on during EZ-Mode steering/finding/binding.
     *
     * @param   state - an
     *
     * @return  none
     */
    static void zclSampleTemperatureSensor_EZModeCB( zlcEZMode_State_t state, zclEZMode_CBData_t *pData )
    {
    #ifdef LCD_SUPPORTED
      char szLine[20];
      char *pStr;
      uint8 err;
    #endif
    
      // time to go into identify mode
      if ( state == EZMODE_STATE_IDENTIFYING )
      {
        zclSampleTemperatureSensor_IdentifyTime = ( EZMODE_TIME / 1000 );  // convert to seconds
        zclSampleTemperatureSensor_ProcessIdentifyTimeChange();
      }
    
      // autoclosing, show what happened (success, cancelled, etc...)
      if( state == EZMODE_STATE_AUTOCLOSE )
      {
    #ifdef LCD_SUPPORTED
        pStr = NULL;
        err = pData->sAutoClose.err;
        if ( err == EZMODE_ERR_SUCCESS )
        {
          pStr = "EZMode: Success";
        }
        else if ( err == EZMODE_ERR_NOMATCH )
        {
          pStr = "EZMode: NoMatch"; // not a match made in heaven
        }
        if ( pStr )
        {
          if ( giTemperatureSensorScreenMode == TEMPSENSE_MAINMODE )
          {
            HalLcdWriteString ( pStr, HAL_LCD_LINE_2 );
          }
        }
    #endif
      }
    
      // finished, either show DstAddr/EP, or nothing (depending on success or not)
      if( state == EZMODE_STATE_FINISH )
      {
        // turn off identify mode
        zclSampleTemperatureSensor_IdentifyTime = 0;
        zclSampleTemperatureSensor_ProcessIdentifyTimeChange();
    
    #ifdef LCD_SUPPORTED
        // if successful, inform user which nwkaddr/ep we bound to
        pStr = NULL;
        err = pData->sFinish.err;
        if( err == EZMODE_ERR_SUCCESS )
        {
          // "EZDst:1234 EP:34"
          osal_memcpy( szLine, "EZDst:", 6 );
          zclHA_uint16toa( pData->sFinish.nwkaddr, &szLine[6] );
          osal_memcpy( &szLine[10], " EP:", 4 );
          _ltoa( pData->sFinish.ep, (void *)(&szLine[14]), 16 );  // _ltoa NULL terminates
          pStr = szLine;
        }
        else if ( err == EZMODE_ERR_BAD_PARAMETER )
        {
          pStr = "EZMode: BadParm";
        }
        else if ( err == EZMODE_ERR_CANCELLED )
        {
          pStr = "EZMode: Cancel";
        }
        else
        {
          pStr = "EZMode: TimeOut";
        }
        if ( pStr )
        {
          if ( giTemperatureSensorScreenMode == TEMPSENSE_MAINMODE )
          {
            HalLcdWriteString ( pStr, HAL_LCD_LINE_2 );
          }
        }
    #endif  // LCD_SUPPORTED
    
        // show main UI screen 3 seconds after joining network
        osal_start_timerEx( zclSampleTemperatureSensor_TaskID, SAMPLETEMPERATURESENSOR_MAIN_SCREEN_EVT, 3000 );
    
        // report current temperature reading 15 seconds after joinging the network
        osal_start_timerEx( zclSampleTemperatureSensor_TaskID, SAMPLETEMPERATURESENSOR_TEMP_SEND_EVT, SAMPLETEMPERATURESENSOR_REPORT_INTERVAL );
      }
    }
    #endif // ZCL_EZMODE
    
    /****************************************************************************
    ****************************************************************************/
    
    
    

    in this code it is giving a value of 42c in the lcd of zed but the pblm is that i cant figure it out that 

    1) if the ZED is transmitting the data or not

    2) if the zED is transmitting y is the coordinator ode dumped with the thermistor value is not showing the values in its lcd

  • Do you do the following steps on your coordinator and end device?

    1. Power  on  the  SampleThermostat,  SampleTemperatureSensor,  and SampleHeatingCoolingUnit  boards.  Once  powered  up,  LCD  will  show  “Texas Instruments”, the MAC address, and the device name. When the coordinator has formed a network, the screen will appear as  shown  in  Figure  19  and the devices will be ready for
    EZ-Mode pairing.
    2. To  pair  the  devices  using  EZ-Mode,  press  SW2  on  the  SampleThermostat  and SampleHeatingCoolingUnit;  after  which,  EZMODE  will  display  on  the  2nd line  of  the LCD  screen.  Repeat  the  pairing  process  for  the  SampleThermostat  and SampleTemperatureSensor.  If  pairing  is  successful,  2nd line  will  display:  “EZMODE: SUCCESS”. If the device is the EZ-Mode “initiator”, the “target” device’s information will be displayed on the initiator’s 2
    nd line. LED1 will blink throughout this process until EZ-Mode  pairing  has  finished.  Refer  to  Figure  20.  EZMODE  device’s  information  or status will disappear after few seconds.
    3. Once all three devices have been successfully paired, current temperature reading will be sent from the SampleTemperatureSensor to the SampleThermostat every 10 seconds.

  • sir i am only going for the temperaturesensor and thermostat pgming sir ... all i wanted to do is that the tamperature sensor sends its data to the thermostat and the thermostat should display the data comming from the temperaturesensor on its lcd 

    i am not using SampleHeatingCoolingUnit  now .. i planned that for later onwards ... 

  • this is the code for temperaturresensor which give the value 22 first and gives 17 to 27 on button press .. it s an example code and it transfers data to the thermostat ans it will display the values precisely 

    2477.zcl_sampletemperaturesensor.c
    /**************************************************************************************************
      Filename:       zcl_sampletemperaturesensor.c
      Revised:        $Date: 2013-10-18 11:49:27 -0700 (Fri, 18 Oct 2013) $
      Revision:       $Revision: 35718 $
    
      Description:    Zigbee Cluster Library - sample device application.
    
    
      Copyright 2013 Texas Instruments Incorporated. All rights reserved.
    
      IMPORTANT: Your use of this Software is limited to those specific rights
      granted under the terms of a software license agreement between the user
      who downloaded the software, his/her employer (which must be your employer)
      and Texas Instruments Incorporated (the "License").  You may not use this
      Software unless you agree to abide by the terms of the License. The License
      limits your use, and you acknowledge, that the Software may not be modified,
      copied or distributed unless embedded on a Texas Instruments microcontroller
      or used solely and exclusively in conjunction with a Texas Instruments radio
      frequency transceiver, which is integrated into your product.  Other than for
      the foregoing purpose, you may not use, reproduce, copy, prepare derivative
      works of, modify, distribute, perform, display or sell this Software and/or
      its documentation for any purpose.
    
      YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE
      PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,
      INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE,
      NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL
      TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT,
      NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER
      LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
      INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE
      OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT
      OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
      (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
    
      Should you have any questions regarding your right to use this Software,
      contact Texas Instruments Incorporated at www.TI.com.
    **************************************************************************************************/
    
    /*********************************************************************
      This device will act as a temperature sensor. It updates the current
      temperature on the thermostat when the user sends the desired
      temperature using SW1.
    
      SCREEN MODES
      ----------------------------------------
      Main:
        - SW1: Send current temperature
        - SW2: Invoke EZMode
        - SW3: Adjust temperature
        - SW5: Go to Help screen
    
      Temperature:
        - SW1: Increase temperature
        - SW3: Decrease temperature
        - SW5: Enter temperature
      ----------------------------------------
    *********************************************************************/
    
    /*********************************************************************
     * INCLUDES
     */
    #include "ZComDef.h"
    #include "OSAL.h"
    #include "AF.h"
    #include "ZDApp.h"
    #include "ZDObject.h"
    #include "ZDProfile.h"
    #include "MT_SYS.h"
    
    #include "zcl.h"
    #include "zcl_general.h"
    #include "zcl_ha.h"
    #include "zcl_ezmode.h"
    #include "zcl_ms.h"
    
    #include "zcl_sampletemperaturesensor.h"
    
    #include "onboard.h"
    
    /* HAL */
    #include "hal_lcd.h"
    #include "hal_led.h"
    #include "hal_key.h"
    
    
    /*********************************************************************
     * MACROS
     */
    
    // how often to report temperature
    #define SAMPLETEMPERATURESENSOR_REPORT_INTERVAL   10000
    
    /*********************************************************************
     * CONSTANTS
     */
    
    /*********************************************************************
     * TYPEDEFS
     */
    
    /*********************************************************************
     * GLOBAL VARIABLES
     */
    byte zclSampleTemperatureSensor_TaskID;
    
    uint8 zclSampleTemperatureSensorSeqNum;
    
    static byte gPermitDuration = 0x00;
    
    /*********************************************************************
     * GLOBAL FUNCTIONS
     */
    
    /*********************************************************************
     * LOCAL VARIABLES
     */
    afAddrType_t zclSampleTemperatureSensor_DstAddr;
    
    #ifdef ZCL_EZMODE
    static void zclSampleTemperatureSensor_ProcessZDOMsgs( zdoIncomingMsg_t *pMsg );
    static void zclSampleTemperatureSensor_EZModeCB( zlcEZMode_State_t state, zclEZMode_CBData_t *pData );
    
    static const zclEZMode_RegisterData_t zclSampleTemperatureSensor_RegisterEZModeData =
    {
      &zclSampleTemperatureSensor_TaskID,
      SAMPLETEMPERATURESENSOR_EZMODE_NEXTSTATE_EVT,
      SAMPLETEMPERATURESENSOR_EZMODE_TIMEOUT_EVT,
      &zclSampleTemperatureSensorSeqNum,
      zclSampleTemperatureSensor_EZModeCB
    };
    
    // NOT ZCL_EZMODE, Use EndDeviceBind
    #else
    
    static cId_t bindingOutClusters[] =
    {
      ZCL_CLUSTER_ID_MS_TEMPERATURE_MEASUREMENT
    };
    #define ZCLSAMPLETEMPERATURESENSOR_BINDINGLIST        1
    #endif
    
    devStates_t zclSampleTemperatureSensor_NwkState = DEV_INIT;
    
    uint8 giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;   // display main screen mode first
    
    static uint8 aProcessCmd[] = { 1, 0, 0, 0 }; // used for reset command, { length + cmd0 + cmd1 + data }
    
    // Test Endpoint to allow SYS_APP_MSGs
    static endPointDesc_t sampleTemperatureSensor_TestEp =
    {
      20,                                 // Test endpoint
      &zclSampleTemperatureSensor_TaskID,
      (SimpleDescriptionFormat_t *)NULL,  // No Simple description for this test endpoint
      (afNetworkLatencyReq_t)0            // No Network Latency req
    };
    
    
    /*********************************************************************
     * LOCAL FUNCTIONS
     */
    static void zclSampleTemperatureSensor_HandleKeys( byte shift, byte keys );
    static void zclSampleTemperatureSensor_BasicResetCB( void );
    static void zclSampleTemperatureSensor_IdentifyCB( zclIdentify_t *pCmd );
    static void zclSampleTemperatureSensor_IdentifyQueryRspCB( zclIdentifyQueryRsp_t *pRsp );
    static void zclSampleTemperatureSensor_ProcessIdentifyTimeChange( void );
    
    // app display functions
    void zclSampleTemperatureSensor_LcdDisplayUpdate(void);
    void zclSampleTemperatureSensor_LcdDisplayMainMode(void);
    void zclSampleTemperatureSensor_LcdDisplayTempMode(void);
    void zclSampleTemperatureSensor_LcdDisplayHelpMode(void);
    
    static void zclSampleTemperatureSensor_SendTemp(void);
    
    // Functions to process ZCL Foundation incoming Command/Response messages
    static void zclSampleTemperatureSensor_ProcessIncomingMsg( zclIncomingMsg_t *msg );
    #ifdef ZCL_READ
    static uint8 zclSampleTemperatureSensor_ProcessInReadRspCmd( zclIncomingMsg_t *pInMsg );
    #endif
    #ifdef ZCL_WRITE
    static uint8 zclSampleTemperatureSensor_ProcessInWriteRspCmd( zclIncomingMsg_t *pInMsg );
    #endif
    static uint8 zclSampleTemperatureSensor_ProcessInDefaultRspCmd( zclIncomingMsg_t *pInMsg );
    #ifdef ZCL_DISCOVER
    static uint8 zclSampleTemperatureSensor_ProcessInDiscCmdsRspCmd( zclIncomingMsg_t *pInMsg );
    static uint8 zclSampleTemperatureSensor_ProcessInDiscAttrsRspCmd( zclIncomingMsg_t *pInMsg );
    static uint8 zclSampleTemperatureSensor_ProcessInDiscAttrsExtRspCmd( zclIncomingMsg_t *pInMsg );
    #endif // ZCL_DISCOVER
    
    /*********************************************************************
     * STATUS STRINGS
     */
    #ifdef LCD_SUPPORTED
    const char sClearLine[]    = " ";
    const char sDeviceName[]   = "  Temp Sensor";
    const char sSwTempUp[]     = "SW1: Raise Temp";
    const char sSwEZMode[]     = "SW2: EZ-Mode";
    const char sSwTempDown[]   = "SW3: Lower Temp";
    const char sSwHelp[]       = "SW5: Help";
    #endif
    
    /*********************************************************************
     * ZCL General Profile Callback table
     */
    static zclGeneral_AppCallbacks_t zclSampleTemperatureSensor_CmdCallbacks =
    {
      zclSampleTemperatureSensor_BasicResetCB,        // Basic Cluster Reset command
      zclSampleTemperatureSensor_IdentifyCB,          // Identify command
    #ifdef ZCL_EZMODE
      NULL,                                           // Identify EZ-Mode Invoke command
      NULL,                                           // Identify Update Commission State command
    #endif
      NULL,                                           // Identify Trigger Effect command
      zclSampleTemperatureSensor_IdentifyQueryRspCB,  // Identify Query Response command
      NULL,             				                      // On/Off cluster command
      NULL,                                           // On/Off cluster enhanced command Off with Effect
      NULL,                                           // On/Off cluster enhanced command On with Recall Global Scene
      NULL,                                           // On/Off cluster enhanced command On with Timed Off
    #ifdef ZCL_LEVEL_CTRL
      NULL,                                           // Level Control Move to Level command
      NULL,                                           // Level Control Move command
      NULL,                                           // Level Control Step command
      NULL,                                           // Level Control Stop command
    #endif
    #ifdef ZCL_GROUPS
      NULL,                                           // Group Response commands
    #endif
    #ifdef ZCL_SCENES
      NULL,                                           // Scene Store Request command
      NULL,                                           // Scene Recall Request command
      NULL,                                           // Scene Response command
    #endif
    #ifdef ZCL_ALARMS
      NULL,                                           // Alarm (Response) commands
    #endif
    #ifdef SE_UK_EXT
      NULL,                                           // Get Event Log command
      NULL,                                           // Publish Event Log command
    #endif
      NULL,                                           // RSSI Location command
      NULL                                            // RSSI Location Response command
    };
    
    /*********************************************************************
     * @fn          zclSampleTemperatureSensor_Init
     *
     * @brief       Initialization function for the zclGeneral layer.
     *
     * @param       none
     *
     * @return      none
     */
    void zclSampleTemperatureSensor_Init( byte task_id )
    {
      zclSampleTemperatureSensor_TaskID = task_id;
    
      // Set destination address to indirect
      zclSampleTemperatureSensor_DstAddr.addrMode = (afAddrMode_t)AddrNotPresent;
      zclSampleTemperatureSensor_DstAddr.endPoint = 0;
      zclSampleTemperatureSensor_DstAddr.addr.shortAddr = 0;
    
      // This app is part of the Home Automation Profile
      zclHA_Init( &zclSampleTemperatureSensor_SimpleDesc );
    
      // Register the ZCL General Cluster Library callback functions
      zclGeneral_RegisterCmdCallbacks( SAMPLETEMPERATURESENSOR_ENDPOINT, &zclSampleTemperatureSensor_CmdCallbacks );
    
      // Register the application's attribute list
      zcl_registerAttrList( SAMPLETEMPERATURESENSOR_ENDPOINT, SAMPLETEMPERATURESENSOR_MAX_ATTRIBUTES, zclSampleTemperatureSensor_Attrs );
    
      // Register the Application to receive the unprocessed Foundation command/response messages
      zcl_registerForMsg( zclSampleTemperatureSensor_TaskID );
    
    #ifdef ZCL_EZMODE
      // Register EZ-Mode
      zcl_RegisterEZMode( &zclSampleTemperatureSensor_RegisterEZModeData );
    
      // Register with the ZDO to receive Match Descriptor Responses
      ZDO_RegisterForZDOMsg(task_id, Match_Desc_rsp);
    #endif
    
      // Register for all key events - This app will handle all key events
      RegisterForKeys( zclSampleTemperatureSensor_TaskID );
    
      // Register for a test endpoint
      afRegister( &sampleTemperatureSensor_TestEp );
    
    #ifdef LCD_SUPPORTED
      // display the device name
      HalLcdWriteString( (char *)sDeviceName, HAL_LCD_LINE_3 );
    #endif
    }
    
    /*********************************************************************
     * @fn          zclSample_event_loop
     *
     * @brief       Event Loop Processor for zclGeneral.
     *
     * @param       none
     *
     * @return      none
     */
    uint16 zclSampleTemperatureSensor_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( zclSampleTemperatureSensor_TaskID )) )
        {
          switch ( MSGpkt->hdr.event )
          {
    #ifdef ZCL_EZMODE
            case ZDO_CB_MSG:
              zclSampleTemperatureSensor_ProcessZDOMsgs( (zdoIncomingMsg_t *)MSGpkt );
              break;
    #endif
    
            case ZCL_INCOMING_MSG:
              // Incoming ZCL Foundation command/response messages
              zclSampleTemperatureSensor_ProcessIncomingMsg( (zclIncomingMsg_t *)MSGpkt );
              break;
    
            case KEY_CHANGE:
              zclSampleTemperatureSensor_HandleKeys( ((keyChange_t *)MSGpkt)->state, ((keyChange_t *)MSGpkt)->keys );
              break;
    
            case ZDO_STATE_CHANGE:
              zclSampleTemperatureSensor_NwkState = (devStates_t)(MSGpkt->hdr.status);
    
    
              // now on the network
              if ( (zclSampleTemperatureSensor_NwkState == DEV_ZB_COORD) ||
                   (zclSampleTemperatureSensor_NwkState == DEV_ROUTER)   ||
                   (zclSampleTemperatureSensor_NwkState == DEV_END_DEVICE) )
              {
    #ifndef HOLD_AUTO_START
                giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;
                zclSampleTemperatureSensor_LcdDisplayUpdate();
    #endif
    #ifdef ZCL_EZMODE
                zcl_EZModeAction( EZMODE_ACTION_NETWORK_STARTED, NULL );
    #endif // ZCL_EZMODE
              }
              break;
    
            default:
              break;
          }
    
          // Release the memory
          osal_msg_deallocate( (uint8 *)MSGpkt );
        }
    
        // return unprocessed events
        return (events ^ SYS_EVENT_MSG);
      }
    
      if ( events & SAMPLETEMPERATURESENSOR_IDENTIFY_TIMEOUT_EVT )
      {
        if ( zclSampleTemperatureSensor_IdentifyTime > 0 )
          zclSampleTemperatureSensor_IdentifyTime--;
        zclSampleTemperatureSensor_ProcessIdentifyTimeChange();
    
        return ( events ^ SAMPLETEMPERATURESENSOR_IDENTIFY_TIMEOUT_EVT );
      }
    
    #ifdef ZCL_EZMODE
      // going on to next state
      if ( events & SAMPLETEMPERATURESENSOR_EZMODE_NEXTSTATE_EVT )
      {
        zcl_EZModeAction ( EZMODE_ACTION_PROCESS, NULL );   // going on to next state
        return ( events ^ SAMPLETEMPERATURESENSOR_EZMODE_NEXTSTATE_EVT );
      }
    
      // the overall EZMode timer expired, so we timed out
      if ( events & SAMPLETEMPERATURESENSOR_EZMODE_TIMEOUT_EVT )
      {
        zcl_EZModeAction ( EZMODE_ACTION_TIMED_OUT, NULL ); // EZ-Mode timed out
        return ( events ^ SAMPLETEMPERATURESENSOR_EZMODE_TIMEOUT_EVT );
      }
    #endif // ZLC_EZMODE
    
      if ( events & SAMPLETEMPERATURESENSOR_MAIN_SCREEN_EVT )
      {
        giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;
        zclSampleTemperatureSensor_LcdDisplayUpdate();
    
        return ( events ^ SAMPLETEMPERATURESENSOR_MAIN_SCREEN_EVT );
      }
    
      if ( events & SAMPLETEMPERATURESENSOR_TEMP_SEND_EVT )
      {
        zclSampleTemperatureSensor_SendTemp();
    
        // report current temperature reading every 10 seconds
        osal_start_timerEx( zclSampleTemperatureSensor_TaskID, SAMPLETEMPERATURESENSOR_TEMP_SEND_EVT, SAMPLETEMPERATURESENSOR_REPORT_INTERVAL );
    
        return ( events ^ SAMPLETEMPERATURESENSOR_TEMP_SEND_EVT );
      }
    
      // Discard unknown events
      return 0;
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_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_3
     *                 HAL_KEY_SW_2
     *                 HAL_KEY_SW_1
     *
     * @return  none
     */
    static void zclSampleTemperatureSensor_HandleKeys( byte shift, byte keys )
    {
      if ( keys & HAL_KEY_SW_1 )
      {
        // increase temperature
        giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;
    
        if ( zclSampleTemperatureSensor_MeasuredValue < zclSampleTemperatureSensor_MaxMeasuredValue )
        {
          zclSampleTemperatureSensor_MeasuredValue = zclSampleTemperatureSensor_MeasuredValue + 100;  // considering using whole number value
        }
        else if ( zclSampleTemperatureSensor_MeasuredValue >= zclSampleTemperatureSensor_MaxMeasuredValue )
        {
          zclSampleTemperatureSensor_MeasuredValue = zclSampleTemperatureSensor_MaxMeasuredValue;
        }
    
        // Send temperature information
        zclSampleTemperatureSensor_SendTemp();
      }
    
      if ( keys & HAL_KEY_SW_2 )
      {
        if ( ( giTemperatureSensorScreenMode == TEMPSENSE_MAINMODE ) ||
            ( giTemperatureSensorScreenMode == TEMPSENSE_HELPMODE ) )
        {
          giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;
    
    #ifdef ZCL_EZMODE
          zclEZMode_InvokeData_t ezModeData;
          static uint16 clusterIDs[] = { ZCL_CLUSTER_ID_MS_TEMPERATURE_MEASUREMENT };   // only bind on the Temperature Measurement cluster
    
          // Invoke EZ-Mode
          ezModeData.endpoint = SAMPLETEMPERATURESENSOR_ENDPOINT; // endpoint on which to invoke EZ-Mode
          if ( ( zclSampleTemperatureSensor_NwkState == DEV_ZB_COORD ) ||
               ( zclSampleTemperatureSensor_NwkState == DEV_ROUTER )   ||
               ( zclSampleTemperatureSensor_NwkState == DEV_END_DEVICE ) )
          {
            ezModeData.onNetwork = TRUE;      // node is already on the network
          }
          else
          {
            ezModeData.onNetwork = FALSE;     // node is not yet on the network
          }
          ezModeData.initiator = TRUE;        // Temperature Sensor is an initiator
          ezModeData.numActiveInClusters = 1;
          ezModeData.pActiveInClusterIDs = clusterIDs;
          ezModeData.numActiveOutClusters = 0;   // active output cluster
          ezModeData.pActiveOutClusterIDs = NULL;
          zcl_InvokeEZMode( &ezModeData );
    
    #ifdef LCD_SUPPORTED
          HalLcdWriteString( "EZMode", HAL_LCD_LINE_2 );
    #endif
    
          // NOT ZCL_EZMODE, Use EndDeviceBind
    #else
          {
            zAddrType_t dstAddr;
            dstAddr.addrMode = Addr16Bit;
            dstAddr.addr.shortAddr = 0;   // Coordinator makes the EDB match
    
            // Initiate an End Device Bind Request, this bind request will
            // only use a cluster list that is important to binding.
            HalLedSet ( HAL_LED_4, HAL_LED_MODE_OFF );
            ZDP_EndDeviceBindReq( &dstAddr, NLME_GetShortAddr(),
                                  SAMPLETEMPERATURESENSOR_ENDPOINT,
                                  ZCL_HA_PROFILE_ID,
                                  0, NULL,
                                  ZCLSAMPLETEMPERATURESENSOR_BINDINGLIST, bindingOutClusters,
                                  FALSE );
          }
    #endif // ZCL_EZMODE
        }
      }
    
      if ( keys & HAL_KEY_SW_3 )
      {
        giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;
    
        // decrease the temperature
        if ( zclSampleTemperatureSensor_MeasuredValue > zclSampleTemperatureSensor_MinMeasuredValue )
        {
          zclSampleTemperatureSensor_MeasuredValue = zclSampleTemperatureSensor_MeasuredValue - 100;  // considering using whole number value
        }
        else if ( zclSampleTemperatureSensor_MeasuredValue >= zclSampleTemperatureSensor_MinMeasuredValue )
        {
          zclSampleTemperatureSensor_MeasuredValue = zclSampleTemperatureSensor_MinMeasuredValue;
        }
    
        // Send temperature information
        zclSampleTemperatureSensor_SendTemp();
      }
    
      if ( keys & HAL_KEY_SW_4 )
      {
        giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;
    
        if ( ( zclSampleTemperatureSensor_NwkState == DEV_ZB_COORD ) ||
             ( zclSampleTemperatureSensor_NwkState == DEV_ROUTER ) )
        {
          // toggle permit join
          gPermitDuration = gPermitDuration ? 0 : 0xff;
          NLME_PermitJoiningRequest( gPermitDuration );
        }
      }
    
      if ( shift && ( keys & HAL_KEY_SW_5 ) )
      {
        zclSampleTemperatureSensor_BasicResetCB();
      }
      else if ( keys & HAL_KEY_SW_5 )
      {
        if ( giTemperatureSensorScreenMode == TEMPSENSE_MAINMODE )
        {
          giTemperatureSensorScreenMode = TEMPSENSE_HELPMODE;
        }
        else if ( giTemperatureSensorScreenMode == TEMPSENSE_HELPMODE )
        {
    #ifdef LCD_SUPPORTED
          HalLcdWriteString( (char *)sClearLine, HAL_LCD_LINE_2 );
    #endif
          giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;
        }
      }
    
      // update display
      zclSampleTemperatureSensor_LcdDisplayUpdate();
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_LcdDisplayUpdate
     *
     * @brief   Called to update the LCD display.
     *
     * @param   none
     *
     * @return  none
     */
    void zclSampleTemperatureSensor_LcdDisplayUpdate( void )
    {
      // turn on red LED for temperatures >= 24.00C
      if ( zclSampleTemperatureSensor_MeasuredValue >= 2400 )
      {
        HalLedSet ( HAL_LED_1, HAL_LED_MODE_OFF );
        HalLedSet ( HAL_LED_2, HAL_LED_MODE_ON );
      }
      // turn on green LED for temperatures <= 20.00C
      else if ( zclSampleTemperatureSensor_MeasuredValue <= 2000 )
      {
        HalLedSet ( HAL_LED_1, HAL_LED_MODE_ON );
        HalLedSet ( HAL_LED_2, HAL_LED_MODE_OFF );
      }
      // turn on both red and green LEDs for temperatures between 20.00C and 24.00C
      else
      {
        HalLedSet ( HAL_LED_1, HAL_LED_MODE_ON );
        HalLedSet ( HAL_LED_2, HAL_LED_MODE_ON );
      }
    
      if ( giTemperatureSensorScreenMode == TEMPSENSE_HELPMODE )
      {
        zclSampleTemperatureSensor_LcdDisplayHelpMode();
      }
      else
      {
        zclSampleTemperatureSensor_LcdDisplayMainMode();
      }
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_LcdDisplayMainMode
     *
     * @brief   Called to display the main screen on the LCD.
     *
     * @param   none
     *
     * @return  none
     */
    void zclSampleTemperatureSensor_LcdDisplayMainMode( void )
    {
      char sDisplayTemp[16];
    
      if ( zclSampleTemperatureSensor_NwkState == DEV_ZB_COORD )
      {
        zclHA_LcdStatusLine1( 0 );
      }
      else if ( zclSampleTemperatureSensor_NwkState == DEV_ROUTER )
      {
        zclHA_LcdStatusLine1( 1 );
      }
      else if ( zclSampleTemperatureSensor_NwkState == DEV_END_DEVICE )
      {
        zclHA_LcdStatusLine1( 2 );
      }
    
      // display current temperature
      osal_memcpy(sDisplayTemp, "TEMP: ", 6);
      _ltoa( ( zclSampleTemperatureSensor_MeasuredValue / 100 ), (void *)(&sDisplayTemp[6]), 10 );   // convert temperature to whole number
      osal_memcpy( &sDisplayTemp[8], "C", 2 );
    #ifdef LCD_SUPPORTED
      HalLcdWriteString( (char *)sDisplayTemp, HAL_LCD_LINE_2 );
    #endif
    
    #ifdef LCD_SUPPORTED
      if ( ( zclSampleTemperatureSensor_NwkState == DEV_ZB_COORD ) ||
           ( zclSampleTemperatureSensor_NwkState == DEV_ROUTER ) )
      {
        // display help key with permit join status
        if ( gPermitDuration )
        {
          HalLcdWriteString( "SW5: Help      *", HAL_LCD_LINE_3 );
        }
        else
        {
          HalLcdWriteString( "SW5: Help       ", HAL_LCD_LINE_3 );
        }
      }
      else
      {
        // display help key
        HalLcdWriteString( (char *)sSwHelp, HAL_LCD_LINE_3 );
      }
    #endif
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_LcdDisplayHelpMode
     *
     * @brief   Called to display the SW options on the LCD.
     *
     * @param   none
     *
     * @return  none
     */
    void zclSampleTemperatureSensor_LcdDisplayHelpMode( void )
    {
    #ifdef LCD_SUPPORTED
      HalLcdWriteString( (char *)sSwTempUp, HAL_LCD_LINE_1 );
      HalLcdWriteString( (char *)sSwEZMode, HAL_LCD_LINE_2 );
      HalLcdWriteString( (char *)sSwTempDown, HAL_LCD_LINE_3 );
    #endif
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_SendTemp
     *
     * @brief   Called to send current temperature information to the thermostat
     *
     * @param   none
     *
     * @return  none
     */
    static void zclSampleTemperatureSensor_SendTemp( void )
    {
    #ifdef ZCL_REPORT
      zclReportCmd_t *pReportCmd;
    
      pReportCmd = osal_mem_alloc( sizeof(zclReportCmd_t) + sizeof(zclReport_t) );
      if ( pReportCmd != NULL )
      {
        pReportCmd->numAttr = 1;
        pReportCmd->attrList[0].attrID = ATTRID_MS_TEMPERATURE_MEASURED_VALUE;
        pReportCmd->attrList[0].dataType = ZCL_DATATYPE_INT16;
        pReportCmd->attrList[0].attrData = (void *)(&zclSampleTemperatureSensor_MeasuredValue);
    
        zcl_SendReportCmd( SAMPLETEMPERATURESENSOR_ENDPOINT, &zclSampleTemperatureSensor_DstAddr,
                           ZCL_CLUSTER_ID_MS_TEMPERATURE_MEASUREMENT,
                           pReportCmd, ZCL_FRAME_SERVER_CLIENT_DIR, TRUE, zclSampleTemperatureSensorSeqNum++ );
      }
    
      osal_mem_free( pReportCmd );
    #endif  // ZCL_REPORT
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_ProcessIdentifyTimeChange
     *
     * @brief   Called to process any change to the IdentifyTime attribute.
     *
     * @param   none
     *
     * @return  none
     */
    static void zclSampleTemperatureSensor_ProcessIdentifyTimeChange( void )
    {
      if ( zclSampleTemperatureSensor_IdentifyTime > 0 )
      {
        osal_start_timerEx( zclSampleTemperatureSensor_TaskID, SAMPLETEMPERATURESENSOR_IDENTIFY_TIMEOUT_EVT, 1000 );
        HalLedBlink ( HAL_LED_4, 0xFF, HAL_LED_DEFAULT_DUTY_CYCLE, HAL_LED_DEFAULT_FLASH_TIME );
      }
      else
      {
        if ( zclSampleTemperatureSensor_OnOff )
        {
          HalLedSet ( HAL_LED_4, HAL_LED_MODE_ON );
        }
        else
        {
          HalLedSet ( HAL_LED_4, HAL_LED_MODE_OFF );
        }
    
        osal_stop_timerEx( zclSampleTemperatureSensor_TaskID, SAMPLETEMPERATURESENSOR_IDENTIFY_TIMEOUT_EVT );
      }
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_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 zclSampleTemperatureSensor_BasicResetCB( void )
    {
      // Put device back to factory default settings
      zgWriteStartupOptions( ZG_STARTUP_SET, 3 );   // bit set both default configuration and default network
    
      // restart device
      MT_SysCommandProcessing( aProcessCmd );
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_IdentifyCB
     *
     * @brief   Callback from the ZCL General Cluster Library when
     *          it received an Identity Command for this application.
     *
     * @param   srcAddr - source address and endpoint of the response message
     * @param   identifyTime - the number of seconds to identify yourself
     *
     * @return  none
     */
    static void zclSampleTemperatureSensor_IdentifyCB( zclIdentify_t *pCmd )
    {
      zclSampleTemperatureSensor_IdentifyTime = pCmd->identifyTime;
      zclSampleTemperatureSensor_ProcessIdentifyTimeChange();
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_IdentifyQueryRspCB
     *
     * @brief   Callback from the ZCL General Cluster Library when
     *          it received an Identity Query Response Command for this application.
     *
     * @param   srcAddr - requestor's address
     * @param   timeout - number of seconds to identify yourself (valid for query response)
     *
     * @return  none
     */
    static void zclSampleTemperatureSensor_IdentifyQueryRspCB( zclIdentifyQueryRsp_t *pRsp )
    {
      (void)pRsp;
    #ifdef ZCL_EZMODE
      {
        zclEZMode_ActionData_t data;
        data.pIdentifyQueryRsp = pRsp;
        zcl_EZModeAction ( EZMODE_ACTION_IDENTIFY_QUERY_RSP, &data );
      }
    #endif
    }
    
    /******************************************************************************
     *
     *  Functions for processing ZCL Foundation incoming Command/Response messages
     *
     *****************************************************************************/
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_ProcessIncomingMsg
     *
     * @brief   Process ZCL Foundation incoming message
     *
     * @param   pInMsg - pointer to the received message
     *
     * @return  none
     */
    static void zclSampleTemperatureSensor_ProcessIncomingMsg( zclIncomingMsg_t *pInMsg)
    {
      switch ( pInMsg->zclHdr.commandID )
      {
    #ifdef ZCL_READ
        case ZCL_CMD_READ_RSP:
          zclSampleTemperatureSensor_ProcessInReadRspCmd( pInMsg );
          break;
    #endif
    #ifdef ZCL_WRITE
        case ZCL_CMD_WRITE_RSP:
          zclSampleTemperatureSensor_ProcessInWriteRspCmd( pInMsg );
          break;
    #endif
    #ifdef ZCL_REPORT
        // See ZCL Test Applicaiton (zcl_testapp.c) for sample code on Attribute Reporting
        case ZCL_CMD_CONFIG_REPORT:
          //zclSampleTemperatureSensor_ProcessInConfigReportCmd( pInMsg );
          break;
    
        case ZCL_CMD_CONFIG_REPORT_RSP:
          //zclSampleTemperatureSensor_ProcessInConfigReportRspCmd( pInMsg );
          break;
    
        case ZCL_CMD_READ_REPORT_CFG:
          //zclSampleTemperatureSensor_ProcessInReadReportCfgCmd( pInMsg );
          break;
    
        case ZCL_CMD_READ_REPORT_CFG_RSP:
          //zclSampleTemperatureSensor_ProcessInReadReportCfgRspCmd( pInMsg );
          break;
    
        case ZCL_CMD_REPORT:
          //zclSampleTemperatureSensor_ProcessInReportCmd( pInMsg );
          break;
    #endif
        case ZCL_CMD_DEFAULT_RSP:
          zclSampleTemperatureSensor_ProcessInDefaultRspCmd( pInMsg );
          break;
    #ifdef ZCL_DISCOVER
        case ZCL_CMD_DISCOVER_CMDS_RECEIVED_RSP:
          zclSampleTemperatureSensor_ProcessInDiscCmdsRspCmd( pInMsg );
          break;
    
        case ZCL_CMD_DISCOVER_CMDS_GEN_RSP:
          zclSampleTemperatureSensor_ProcessInDiscCmdsRspCmd( pInMsg );
          break;
    
        case ZCL_CMD_DISCOVER_ATTRS_RSP:
          zclSampleTemperatureSensor_ProcessInDiscAttrsRspCmd( pInMsg );
          break;
    
        case ZCL_CMD_DISCOVER_ATTRS_EXT_RSP:
          zclSampleTemperatureSensor_ProcessInDiscAttrsExtRspCmd( pInMsg );
          break;
    #endif
        default:
          break;
      }
    
      if ( pInMsg->attrCmd )
      {
        osal_mem_free( pInMsg->attrCmd );
      }
    }
    
    #ifdef ZCL_READ
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_ProcessInReadRspCmd
     *
     * @brief   Process the "Profile" Read Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleTemperatureSensor_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      zclSampleTemperatureSensor_ProcessInWriteRspCmd
     *
     * @brief   Process the "Profile" Write Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleTemperatureSensor_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      zclSampleTemperatureSensor_ProcessInDefaultRspCmd
     *
     * @brief   Process the "Profile" Default Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleTemperatureSensor_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      zclSampleTemperatureSensor_ProcessInDiscCmdsRspCmd
     *
     * @brief   Process the Discover Commands Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleTemperatureSensor_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      zclSampleTemperatureSensor_ProcessInDiscAttrsRspCmd
     *
     * @brief   Process the "Profile" Discover Attributes Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleTemperatureSensor_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      zclSampleTemperatureSensor_ProcessInDiscAttrsExtRspCmd
     *
     * @brief   Process the "Profile" Discover Attributes Extended Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleTemperatureSensor_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
    
    #ifdef ZCL_EZMODE
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_ProcessZDOMsgs
     *
     * @brief   Called when this node receives a ZDO/ZDP response.
     *
     * @param   none
     *
     * @return  status
     */
    static void zclSampleTemperatureSensor_ProcessZDOMsgs( zdoIncomingMsg_t *pMsg )
    {
      zclEZMode_ActionData_t data;
      ZDO_MatchDescRsp_t *pMatchDescRsp;
    
      // Let EZ-Mode know of the Match Descriptor Response
      if ( pMsg->clusterID == Match_Desc_rsp )
      {
        pMatchDescRsp = ZDO_ParseEPListRsp( pMsg );
        data.pMatchDescRsp = pMatchDescRsp;
        zcl_EZModeAction( EZMODE_ACTION_MATCH_DESC_RSP, &data );
        osal_mem_free( pMatchDescRsp );
      }
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_EZModeCB
     *
     * @brief   The Application is informed of events. This can be used to show on the UI what is
    *           going on during EZ-Mode steering/finding/binding.
     *
     * @param   state - an
     *
     * @return  none
     */
    static void zclSampleTemperatureSensor_EZModeCB( zlcEZMode_State_t state, zclEZMode_CBData_t *pData )
    {
    #ifdef LCD_SUPPORTED
      char szLine[20];
      char *pStr;
      uint8 err;
    #endif
    
      // time to go into identify mode
      if ( state == EZMODE_STATE_IDENTIFYING )
      {
        zclSampleTemperatureSensor_IdentifyTime = ( EZMODE_TIME / 1000 );  // convert to seconds
        zclSampleTemperatureSensor_ProcessIdentifyTimeChange();
      }
    
      // autoclosing, show what happened (success, cancelled, etc...)
      if( state == EZMODE_STATE_AUTOCLOSE )
      {
    #ifdef LCD_SUPPORTED
        pStr = NULL;
        err = pData->sAutoClose.err;
        if ( err == EZMODE_ERR_SUCCESS )
        {
          pStr = "EZMode: Success";
        }
        else if ( err == EZMODE_ERR_NOMATCH )
        {
          pStr = "EZMode: NoMatch"; // not a match made in heaven
        }
        if ( pStr )
        {
          if ( giTemperatureSensorScreenMode == TEMPSENSE_MAINMODE )
          {
            HalLcdWriteString ( pStr, HAL_LCD_LINE_2 );
          }
        }
    #endif
      }
    
      // finished, either show DstAddr/EP, or nothing (depending on success or not)
      if( state == EZMODE_STATE_FINISH )
      {
        // turn off identify mode
        zclSampleTemperatureSensor_IdentifyTime = 0;
        zclSampleTemperatureSensor_ProcessIdentifyTimeChange();
    
    #ifdef LCD_SUPPORTED
        // if successful, inform user which nwkaddr/ep we bound to
        pStr = NULL;
        err = pData->sFinish.err;
        if( err == EZMODE_ERR_SUCCESS )
        {
          // "EZDst:1234 EP:34"
          osal_memcpy( szLine, "EZDst:", 6 );
          zclHA_uint16toa( pData->sFinish.nwkaddr, &szLine[6] );
          osal_memcpy( &szLine[10], " EP:", 4 );
          _ltoa( pData->sFinish.ep, (void *)(&szLine[14]), 16 );  // _ltoa NULL terminates
          pStr = szLine;
        }
        else if ( err == EZMODE_ERR_BAD_PARAMETER )
        {
          pStr = "EZMode: BadParm";
        }
        else if ( err == EZMODE_ERR_CANCELLED )
        {
          pStr = "EZMode: Cancel";
        }
        else
        {
          pStr = "EZMode: TimeOut";
        }
        if ( pStr )
        {
          if ( giTemperatureSensorScreenMode == TEMPSENSE_MAINMODE )
          {
            HalLcdWriteString ( pStr, HAL_LCD_LINE_2 );
          }
        }
    #endif  // LCD_SUPPORTED
    
        // show main UI screen 3 seconds after joining network
        osal_start_timerEx( zclSampleTemperatureSensor_TaskID, SAMPLETEMPERATURESENSOR_MAIN_SCREEN_EVT, 3000 );
    
        // report current temperature reading 15 seconds after joinging the network
        osal_start_timerEx( zclSampleTemperatureSensor_TaskID, SAMPLETEMPERATURESENSOR_TEMP_SEND_EVT, SAMPLETEMPERATURESENSOR_REPORT_INTERVAL );
      }
    }
    #endif // ZCL_EZMODE
    
    /****************************************************************************
    ****************************************************************************/
    
    
    

    the below given is the code i edited and i meant it to get the value of the temperature sensor and and send it to the thermostat for displaying

    0640.zcl_sampletemperaturesensor.c
    /**************************************************************************************************
      Filename:       zcl_sampletemperaturesensor.c
      Revised:        $Date: 2013-10-18 11:49:27 -0700 (Fri, 18 Oct 2013) $
      Revision:       $Revision: 35718 $
    
      Description:    Zigbee Cluster Library - sample device application.
    
    
      Copyright 2013 Texas Instruments Incorporated. All rights reserved.
    
      IMPORTANT: Your use of this Software is limited to those specific rights
      granted under the terms of a software license agreement between the user
      who downloaded the software, his/her employer (which must be your employer)
      and Texas Instruments Incorporated (the "License").  You may not use this
      Software unless you agree to abide by the terms of the License. The License
      limits your use, and you acknowledge, that the Software may not be modified,
      copied or distributed unless embedded on a Texas Instruments microcontroller
      or used solely and exclusively in conjunction with a Texas Instruments radio
      frequency transceiver, which is integrated into your product.  Other than for
      the foregoing purpose, you may not use, reproduce, copy, prepare derivative
      works of, modify, distribute, perform, display or sell this Software and/or
      its documentation for any purpose.
    
      YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE
      PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,
      INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE,
      NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL
      TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT,
      NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER
      LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
      INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE
      OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT
      OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
      (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
    
      Should you have any questions regarding your right to use this Software,
      contact Texas Instruments Incorporated at www.TI.com.
    **************************************************************************************************/
    
    /*********************************************************************
      This device will act as a temperature sensor. It updates the current
      temperature on the thermostat when the user sends the desired
      temperature using SW1.
    
      SCREEN MODES
      ----------------------------------------
      Main:
        - SW1: Send current temperature
        - SW2: Invoke EZMode
        - SW3: Adjust temperature
        - SW5: Go to Help screen
    
      Temperature:
        - SW1: Increase temperature
        - SW3: Decrease temperature
        - SW5: Enter temperature
      ----------------------------------------
    *********************************************************************/
    
    /*********************************************************************
     * INCLUDES
     */
    #include "ZComDef.h"
    #include "OSAL.h"
    #include "AF.h"
    #include "ZDApp.h"
    #include "ZDObject.h"
    #include "ZDProfile.h"
    #include "MT_SYS.h"
    
    #include "zcl.h"
    #include "zcl_general.h"
    #include "zcl_ha.h"
    #include "zcl_ezmode.h"
    #include "zcl_ms.h"
    #include "hal_adc.h"
    
    #include "zcl_sampletemperaturesensor.h"
    
    #include "onboard.h"
    
    /* HAL */
    #include "hal_lcd.h"
    #include "hal_led.h"
    #include "hal_key.h"
    //#include "zcl_sampletemperaturesensor_data.c"
    
    /*********************************************************************
     * MACROS
     */
    
    // how often to report temperature
    #define SAMPLETEMPERATURESENSOR_REPORT_INTERVAL   3000
    
    /*********************************************************************
     * CONSTANTS
     */
    
    /*********************************************************************
     * TYPEDEFS
     */
    /*#ifdef __IAR_SYSTEMS_ICC__
    #define  CODE   __code
    #define  XDATA  __xdata
    */
    
    /*********************************************************************
     * GLOBAL VARIABLES
     */
    byte zclSampleTemperatureSensor_TaskID;
    
    uint8 zclSampleTemperatureSensorSeqNum;
    
    static byte gPermitDuration = 0x00;
    /*void initTempSensor(void){
       DISABLE_ALL_INTERRUPTS();            //??????
       InitClock();                         //????????32M
       *((BYTE XDATA*) 0x624B) = 0x01;    //???????
       *((BYTE XDATA*) 0x61BD) = 0x01;    //???????ADC????
    }*/
    
    /*********************************************************************
     * GLOBAL FUNCTIONS
     */
    
    /*********************************************************************
     * LOCAL VARIABLES
     */
    afAddrType_t zclSampleTemperatureSensor_DstAddr;
    
    #ifdef ZCL_EZMODE
    static void zclSampleTemperatureSensor_ProcessZDOMsgs( zdoIncomingMsg_t *pMsg );
    static void zclSampleTemperatureSensor_EZModeCB( zlcEZMode_State_t state, zclEZMode_CBData_t *pData );
    
    static const zclEZMode_RegisterData_t zclSampleTemperatureSensor_RegisterEZModeData =
    {
      &zclSampleTemperatureSensor_TaskID,
      SAMPLETEMPERATURESENSOR_EZMODE_NEXTSTATE_EVT,
      SAMPLETEMPERATURESENSOR_EZMODE_TIMEOUT_EVT,
      &zclSampleTemperatureSensorSeqNum,
      zclSampleTemperatureSensor_EZModeCB
    };
    
    // NOT ZCL_EZMODE, Use EndDeviceBind
    #else
    
    static cId_t bindingOutClusters[] =
    {
      ZCL_CLUSTER_ID_MS_TEMPERATURE_MEASUREMENT
    };
    #define ZCLSAMPLETEMPERATURESENSOR_BINDINGLIST        1
    #endif
    
    devStates_t zclSampleTemperatureSensor_NwkState = DEV_INIT;
    
    uint8 giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;   // display main screen mode first
    
    static uint8 aProcessCmd[] = { 1, 0, 0, 0 }; // used for reset command, { length + cmd0 + cmd1 + data }
    
    // Test Endpoint to allow SYS_APP_MSGs
    static endPointDesc_t sampleTemperatureSensor_TestEp =
    {
      20,                                 // Test endpoint
      &zclSampleTemperatureSensor_TaskID,
      (SimpleDescriptionFormat_t *)NULL,  // No Simple description for this test endpoint
      (afNetworkLatencyReq_t)0            // No Network Latency req
    };
    
    
    /*********************************************************************
     * LOCAL FUNCTIONS
     */
    static void zclSampleTemperatureSensor_HandleKeys( byte shift, byte keys );
    static void zclSampleTemperatureSensor_BasicResetCB( void );
    static void zclSampleTemperatureSensor_IdentifyCB( zclIdentify_t *pCmd );
    static void zclSampleTemperatureSensor_IdentifyQueryRspCB( zclIdentifyQueryRsp_t *pRsp );
    static void zclSampleTemperatureSensor_ProcessIdentifyTimeChange( void );
    //void getTemperature(void);
    // app display functions
    void zclSampleTemperatureSensor_LcdDisplayUpdate(void);
    void zclSampleTemperatureSensor_LcdDisplayMainMode(void);
    void zclSampleTemperatureSensor_LcdDisplayTempMode(void);
    void zclSampleTemperatureSensor_LcdDisplayHelpMode(void);
    //int16 getTemperature(void);
    int16 zclSampleTemperatureSensor_MeasuredValue;
    
    static void zclSampleTemperatureSensor_SendTemp(void);
    
    // Functions to process ZCL Foundation incoming Command/Response messages
    static void zclSampleTemperatureSensor_ProcessIncomingMsg( zclIncomingMsg_t *msg );
    #ifdef ZCL_READ
    static uint8 zclSampleTemperatureSensor_ProcessInReadRspCmd( zclIncomingMsg_t *pInMsg );
    #endif
    #ifdef ZCL_WRITE
    static uint8 zclSampleTemperatureSensor_ProcessInWriteRspCmd( zclIncomingMsg_t *pInMsg );
    #endif
    static uint8 zclSampleTemperatureSensor_ProcessInDefaultRspCmd( zclIncomingMsg_t *pInMsg );
    #ifdef ZCL_DISCOVER
    static uint8 zclSampleTemperatureSensor_ProcessInDiscCmdsRspCmd( zclIncomingMsg_t *pInMsg );
    static uint8 zclSampleTemperatureSensor_ProcessInDiscAttrsRspCmd( zclIncomingMsg_t *pInMsg );
    static uint8 zclSampleTemperatureSensor_ProcessInDiscAttrsExtRspCmd( zclIncomingMsg_t *pInMsg );
    #endif // ZCL_DISCOVER
    
    /*********************************************************************
     * STATUS STRINGS
     */
    #ifdef LCD_SUPPORTED
    const char sClearLine[]    = " ";
    const char sDeviceName[]   = "  Temp Sensor";
    const char sSwTempUp[]     = "SW1: Raise Temp";
    const char sSwEZMode[]     = "SW2: EZ-Mode";
    const char sSwTempDown[]   = "SW3: Lower Temp";
    const char sSwHelp[]       = "SW5: Help";
    #endif
    
    /*********************************************************************
     * ZCL General Profile Callback table
     */
    static zclGeneral_AppCallbacks_t zclSampleTemperatureSensor_CmdCallbacks =
    {
      zclSampleTemperatureSensor_BasicResetCB,        // Basic Cluster Reset command
      zclSampleTemperatureSensor_IdentifyCB,          // Identify command
    #ifdef ZCL_EZMODE
      NULL,                                           // Identify EZ-Mode Invoke command
      NULL,                                           // Identify Update Commission State command
    #endif
      NULL,                                           // Identify Trigger Effect command
      zclSampleTemperatureSensor_IdentifyQueryRspCB,  // Identify Query Response command
      NULL,             				                      // On/Off cluster command
      NULL,                                           // On/Off cluster enhanced command Off with Effect
      NULL,                                           // On/Off cluster enhanced command On with Recall Global Scene
      NULL,                                           // On/Off cluster enhanced command On with Timed Off
    #ifdef ZCL_LEVEL_CTRL
      NULL,                                           // Level Control Move to Level command
      NULL,                                           // Level Control Move command
      NULL,                                           // Level Control Step command
      NULL,                                           // Level Control Stop command
    #endif
    #ifdef ZCL_GROUPS
      NULL,                                           // Group Response commands
    #endif
    #ifdef ZCL_SCENES
      NULL,                                           // Scene Store Request command
      NULL,                                           // Scene Recall Request command
      NULL,                                           // Scene Response command
    #endif
    #ifdef ZCL_ALARMS
      NULL,                                           // Alarm (Response) commands
    #endif
    #ifdef SE_UK_EXT
      NULL,                                           // Get Event Log command
      NULL,                                           // Publish Event Log command
    #endif
      NULL,                                           // RSSI Location command
      NULL                                            // RSSI Location Response command
    };
    
    /*********************************************************************
     * @fn          zclSampleTemperatureSensor_Init
     *
     * @brief       Initialization function for the zclGeneral layer.
     *
     * @param       none
     *
     * @return      none
     */
    void zclSampleTemperatureSensor_Init( byte task_id )
    {
      zclSampleTemperatureSensor_TaskID = task_id;
    
      // Set destination address to indirect
      zclSampleTemperatureSensor_DstAddr.addrMode = (afAddrMode_t)AddrNotPresent;
      zclSampleTemperatureSensor_DstAddr.endPoint = 0;
      zclSampleTemperatureSensor_DstAddr.addr.shortAddr = 0;
    
      // This app is part of the Home Automation Profile
      zclHA_Init( &zclSampleTemperatureSensor_SimpleDesc );
    
      // Register the ZCL General Cluster Library callback functions
      zclGeneral_RegisterCmdCallbacks( SAMPLETEMPERATURESENSOR_ENDPOINT, &zclSampleTemperatureSensor_CmdCallbacks );
    
      // Register the application's attribute list
      zcl_registerAttrList( SAMPLETEMPERATURESENSOR_ENDPOINT, SAMPLETEMPERATURESENSOR_MAX_ATTRIBUTES, zclSampleTemperatureSensor_Attrs );
    
      // Register the Application to receive the unprocessed Foundation command/response messages
      zcl_registerForMsg( zclSampleTemperatureSensor_TaskID );
    
    #ifdef ZCL_EZMODE
      // Register EZ-Mode
      zcl_RegisterEZMode( &zclSampleTemperatureSensor_RegisterEZModeData );
    
      // Register with the ZDO to receive Match Descriptor Responses
      ZDO_RegisterForZDOMsg(task_id, Match_Desc_rsp);
    #endif
    
      // Register for all key events - This app will handle all key events
      RegisterForKeys( zclSampleTemperatureSensor_TaskID );
    
      // Register for a test endpoint
      afRegister( &sampleTemperatureSensor_TestEp );
    
    #ifdef LCD_SUPPORTED
      // display the device name
      HalLcdWriteString( (char *)sDeviceName, HAL_LCD_LINE_3 );
    #endif
      osal_start_timerEx( zclSampleTemperatureSensor_TaskID, SAMPLETEMPERATURESENSOR_ADC, SAMPLETEMPERATURESENSOR_REPORT_INTERVAL );
    
    }
    
    /*********************************************************************
     * @fn          zclSample_event_loop
     *
     * @brief       Event Loop Processor for zclGeneral.
     *
     * @param       none
     *
     * @return      none
     */
    uint16 zclSampleTemperatureSensor_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( zclSampleTemperatureSensor_TaskID )) )
        {
          switch ( MSGpkt->hdr.event )
          {
    #ifdef ZCL_EZMODE
            case ZDO_CB_MSG:
              zclSampleTemperatureSensor_ProcessZDOMsgs( (zdoIncomingMsg_t *)MSGpkt );
              break;
    #endif
    
            case ZCL_INCOMING_MSG:
              // Incoming ZCL Foundation command/response messages
              zclSampleTemperatureSensor_ProcessIncomingMsg( (zclIncomingMsg_t *)MSGpkt );
              break;
    
            case KEY_CHANGE:
              zclSampleTemperatureSensor_HandleKeys( ((keyChange_t *)MSGpkt)->state, ((keyChange_t *)MSGpkt)->keys );
              break;
    
            case ZDO_STATE_CHANGE:
              zclSampleTemperatureSensor_NwkState = (devStates_t)(MSGpkt->hdr.status);
    
    
              // now on the network
              if ( (zclSampleTemperatureSensor_NwkState == DEV_ZB_COORD) ||
                   (zclSampleTemperatureSensor_NwkState == DEV_ROUTER)   ||
                   (zclSampleTemperatureSensor_NwkState == DEV_END_DEVICE) )
              {
    #ifndef HOLD_AUTO_START
                giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;
                zclSampleTemperatureSensor_LcdDisplayUpdate();
    #endif
    #ifdef ZCL_EZMODE
                zcl_EZModeAction( EZMODE_ACTION_NETWORK_STARTED, NULL );
    #endif // ZCL_EZMODE
              }
              break;
    
            default:
              break;
          }
    
          // Release the memory
          osal_msg_deallocate( (uint8 *)MSGpkt );
        }
    
        // return unprocessed events
        return (events ^ SYS_EVENT_MSG);
      }
    
      if ( events & SAMPLETEMPERATURESENSOR_IDENTIFY_TIMEOUT_EVT )
      {
        if ( zclSampleTemperatureSensor_IdentifyTime > 0 )
          zclSampleTemperatureSensor_IdentifyTime--;
        zclSampleTemperatureSensor_ProcessIdentifyTimeChange();
    
        return ( events ^ SAMPLETEMPERATURESENSOR_IDENTIFY_TIMEOUT_EVT );
      }
    
    #ifdef ZCL_EZMODE
      // going on to next state
      if ( events & SAMPLETEMPERATURESENSOR_EZMODE_NEXTSTATE_EVT )
      {
        zcl_EZModeAction ( EZMODE_ACTION_PROCESS, NULL );   // going on to next state
        return ( events ^ SAMPLETEMPERATURESENSOR_EZMODE_NEXTSTATE_EVT );
      }
    
      // the overall EZMode timer expired, so we timed out
      if ( events & SAMPLETEMPERATURESENSOR_EZMODE_TIMEOUT_EVT )
      {
        zcl_EZModeAction ( EZMODE_ACTION_TIMED_OUT, NULL ); // EZ-Mode timed out
        return ( events ^ SAMPLETEMPERATURESENSOR_EZMODE_TIMEOUT_EVT );
      }
    #endif // ZLC_EZMODE
    
      if ( events & SAMPLETEMPERATURESENSOR_MAIN_SCREEN_EVT )
      {
        giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;
        zclSampleTemperatureSensor_LcdDisplayUpdate();
    
        return ( events ^ SAMPLETEMPERATURESENSOR_MAIN_SCREEN_EVT );
      }
    
      if (events & SAMPLETEMPERATURESENSOR_ADC)
      {
       int16 AvgTemp;
      uint8   i;
      uint16  AdcValue;
      uint16  value;
      uint16  value1;
    //char i;
            //char TempValue[10]; 
            //initTempSensor(); 
      AdcValue = 0;
      for( i = 0; i < 4; i++ )
      {
    ADCIF = 0;
        ADCCON3 = (HAL_ADC_REF_125V | HAL_ADC_RESOLUTION_14 | HAL_ADC_CHN_TEMP);    // ??1.25V????,12????,AD??:?????
            while ( !ADCIF );                                              //?????ADC
                     
        value =  ADCL >> 2;                        
        value |= (((UINT16)ADCH) << 6);    
        AdcValue += value;                          
      }
      value = AdcValue >> 2;                        //????4,?????
      value1 =   (((value) >> 4) - 335);
                 //??AD?,????????
     // while(1)
      //      {
             AvgTemp = 0;
             for(i = 0 ; i < 64 ; i++)
              {
                AvgTemp += value1;              
                AvgTemp >>= 1;                              //??????2.
              }
              
        //    }
        zclSampleTemperatureSensor_MeasuredValue = AvgTemp ;
        zclSampleTemperatureSensor_LcdDisplayUpdate();
        osal_start_timerEx( zclSampleTemperatureSensor_TaskID, SAMPLETEMPERATURESENSOR_ADC, SAMPLETEMPERATURESENSOR_REPORT_INTERVAL );
        return ( events ^ SAMPLETEMPERATURESENSOR_ADC );
      }
    
      if ( events & SAMPLETEMPERATURESENSOR_TEMP_SEND_EVT )
      {
        zclSampleTemperatureSensor_SendTemp();
    
        // report current temperature reading every 10 seconds
        osal_start_timerEx( zclSampleTemperatureSensor_TaskID, SAMPLETEMPERATURESENSOR_TEMP_SEND_EVT, SAMPLETEMPERATURESENSOR_REPORT_INTERVAL );
    
        return ( events ^ SAMPLETEMPERATURESENSOR_TEMP_SEND_EVT );
      }
    
      // Discard unknown events
      return 0;
    }
    
    /*int16 getTemperature(void)
    {
      int16 AvgTemp;
      uint8   i;
      uint16  AdcValue;
      uint16  value;
      uint16  value1;
    //char i;
            //char TempValue[10]; 
            //initTempSensor(); 
      AdcValue = 0;
      for( i = 0; i < 4; i++ )
      {
    ADCIF = 0;
        ADCCON3 = (HAL_ADC_REF_125V | HAL_ADC_RESOLUTION_14 | HAL_ADC_CHN_TEMP);    // ??1.25V????,12????,AD??:?????
            while ( !ADCIF );                                              //?????ADC
                     
        value =  ADCL >> 2;                        
        value |= (((UINT16)ADCH) << 6);    
        AdcValue += value;                          
      }
      value = AdcValue >> 2;                        //????4,?????
      value1 =   (((value) >> 4) - 335);
                 //??AD?,????????
      while(1)
            {
              AvgTemp = 0;
              for(i = 0 ; i < 64 ; i++)
              {
                AvgTemp += value1;              
                AvgTemp >>= 1;                              //??????2.
              }
              
            }
      //zclSampleTemperatureSensor_MeasuredValue = AvgTemp ; 
      return AvgTemp;
    }*/
    
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_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_3
     *                 HAL_KEY_SW_2
     *                 HAL_KEY_SW_1
     *
     * @return  none
     */
    static void zclSampleTemperatureSensor_HandleKeys( byte shift, byte keys )
    {
      if ( keys & HAL_KEY_SW_1 )
      {
        // increase temperature
        giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;
    
        if ( zclSampleTemperatureSensor_MeasuredValue < zclSampleTemperatureSensor_MaxMeasuredValue )
        {
          zclSampleTemperatureSensor_MeasuredValue = zclSampleTemperatureSensor_MeasuredValue + 100;  // considering using whole number value
        }
        else if ( zclSampleTemperatureSensor_MeasuredValue >= zclSampleTemperatureSensor_MaxMeasuredValue )
        {
          zclSampleTemperatureSensor_MeasuredValue = zclSampleTemperatureSensor_MaxMeasuredValue;
        }
    
        // Send temperature information
        zclSampleTemperatureSensor_SendTemp();
      }
    
      if ( keys & HAL_KEY_SW_2 )
      {
        if ( ( giTemperatureSensorScreenMode == TEMPSENSE_MAINMODE ) ||
            ( giTemperatureSensorScreenMode == TEMPSENSE_HELPMODE ) )
        {
          giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;
    
    #ifdef ZCL_EZMODE
          zclEZMode_InvokeData_t ezModeData;
          static uint16 clusterIDs[] = { ZCL_CLUSTER_ID_MS_TEMPERATURE_MEASUREMENT };   // only bind on the Temperature Measurement cluster
    
          // Invoke EZ-Mode
          ezModeData.endpoint = SAMPLETEMPERATURESENSOR_ENDPOINT; // endpoint on which to invoke EZ-Mode
          if ( ( zclSampleTemperatureSensor_NwkState == DEV_ZB_COORD ) ||
               ( zclSampleTemperatureSensor_NwkState == DEV_ROUTER )   ||
               ( zclSampleTemperatureSensor_NwkState == DEV_END_DEVICE ) )
          {
            ezModeData.onNetwork = TRUE;      // node is already on the network
          }
          else
          {
            ezModeData.onNetwork = FALSE;     // node is not yet on the network
          }
          ezModeData.initiator = TRUE;        // Temperature Sensor is an initiator
          ezModeData.numActiveInClusters = 1;
          ezModeData.pActiveInClusterIDs = clusterIDs;
          ezModeData.numActiveOutClusters = 0;   // active output cluster
          ezModeData.pActiveOutClusterIDs = NULL;
          zcl_InvokeEZMode( &ezModeData );
    
    #ifdef LCD_SUPPORTED
          HalLcdWriteString( "EZMode", HAL_LCD_LINE_2 );
    #endif
    
          // NOT ZCL_EZMODE, Use EndDeviceBind
    #else
          {
            zAddrType_t dstAddr;
            dstAddr.addrMode = Addr16Bit;
            dstAddr.addr.shortAddr = 0;   // Coordinator makes the EDB match
    
            // Initiate an End Device Bind Request, this bind request will
            // only use a cluster list that is important to binding.
            HalLedSet ( HAL_LED_4, HAL_LED_MODE_OFF );
            ZDP_EndDeviceBindReq( &dstAddr, NLME_GetShortAddr(),
                                  SAMPLETEMPERATURESENSOR_ENDPOINT,
                                  ZCL_HA_PROFILE_ID,
                                  0, NULL,
                                  ZCLSAMPLETEMPERATURESENSOR_BINDINGLIST, bindingOutClusters,
                                  FALSE );
          }
    #endif // ZCL_EZMODE
        }
      }
    
      if ( keys & HAL_KEY_SW_3 )
      {
        giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;
    
        // decrease the temperature
        if ( zclSampleTemperatureSensor_MeasuredValue > zclSampleTemperatureSensor_MinMeasuredValue )
        {
          zclSampleTemperatureSensor_MeasuredValue = zclSampleTemperatureSensor_MeasuredValue - 100;  // considering using whole number value
        }
        else if ( zclSampleTemperatureSensor_MeasuredValue >= zclSampleTemperatureSensor_MinMeasuredValue )
        {
          zclSampleTemperatureSensor_MeasuredValue = zclSampleTemperatureSensor_MinMeasuredValue;
        }
    
        // Send temperature information
        zclSampleTemperatureSensor_SendTemp();
      }
    
      if ( keys & HAL_KEY_SW_4 )
      {
        giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;
    
        if ( ( zclSampleTemperatureSensor_NwkState == DEV_ZB_COORD ) ||
             ( zclSampleTemperatureSensor_NwkState == DEV_ROUTER ) )
        {
          // toggle permit join
          gPermitDuration = gPermitDuration ? 0 : 0xff;
          NLME_PermitJoiningRequest( gPermitDuration );
        }
      }
    
      if ( shift && ( keys & HAL_KEY_SW_5 ) )
      {
        zclSampleTemperatureSensor_BasicResetCB();
      }
      else if ( keys & HAL_KEY_SW_5 )
      {
        if ( giTemperatureSensorScreenMode == TEMPSENSE_MAINMODE )
        {
          giTemperatureSensorScreenMode = TEMPSENSE_HELPMODE;
        }
        else if ( giTemperatureSensorScreenMode == TEMPSENSE_HELPMODE )
        {
    #ifdef LCD_SUPPORTED
          HalLcdWriteString( (char *)sClearLine, HAL_LCD_LINE_2 );
    #endif
          giTemperatureSensorScreenMode = TEMPSENSE_MAINMODE;
        }
      }
    
      // update display
      zclSampleTemperatureSensor_LcdDisplayUpdate();
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_LcdDisplayUpdate
     *
     * @brief   Called to update the LCD display.
     *
     * @param   none
     *
     * @return  none
     */
    void zclSampleTemperatureSensor_LcdDisplayUpdate( void )
    {
      // turn on red LED for temperatures >= 24.00C
      if ( zclSampleTemperatureSensor_MeasuredValue >= 2400 )
      {
        HalLedSet ( HAL_LED_1, HAL_LED_MODE_OFF );
        HalLedSet ( HAL_LED_2, HAL_LED_MODE_ON );
      }
      // turn on green LED for temperatures <= 20.00C
      else if ( zclSampleTemperatureSensor_MeasuredValue <= 2000 )
      {
        HalLedSet ( HAL_LED_1, HAL_LED_MODE_ON );
        HalLedSet ( HAL_LED_2, HAL_LED_MODE_OFF );
      }
      // turn on both red and green LEDs for temperatures between 20.00C and 24.00C
      else
      {
        HalLedSet ( HAL_LED_1, HAL_LED_MODE_ON );
        HalLedSet ( HAL_LED_2, HAL_LED_MODE_ON );
      }
    
      if ( giTemperatureSensorScreenMode == TEMPSENSE_HELPMODE )
      {
        zclSampleTemperatureSensor_LcdDisplayHelpMode();
      }
      else
      {
        zclSampleTemperatureSensor_LcdDisplayMainMode();
      }
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_LcdDisplayMainMode
     *
     * @brief   Called to display the main screen on the LCD.
     *
     * @param   none
     *
     * @return  none
     */
    void zclSampleTemperatureSensor_LcdDisplayMainMode( void )
    {
      char sDisplayTemp[16];
    
      if ( zclSampleTemperatureSensor_NwkState == DEV_ZB_COORD )
      {
        zclHA_LcdStatusLine1( 0 );
      }
      else if ( zclSampleTemperatureSensor_NwkState == DEV_ROUTER )
      {
        zclHA_LcdStatusLine1( 1 );
      }
      else if ( zclSampleTemperatureSensor_NwkState == DEV_END_DEVICE )
      {
        zclHA_LcdStatusLine1( 2 );
      }
    
      // display current temperature
      osal_memcpy(sDisplayTemp, "TEMP: ", 6);
      _ltoa( ( zclSampleTemperatureSensor_MeasuredValue / 100 ), (void *)(&sDisplayTemp[6]), 10 );   // convert temperature to whole number
      osal_memcpy( &sDisplayTemp[8], "C", 2 );
    #ifdef LCD_SUPPORTED
      HalLcdWriteString( (char *)sDisplayTemp, HAL_LCD_LINE_2 );
    #endif
    
    #ifdef LCD_SUPPORTED
      if ( ( zclSampleTemperatureSensor_NwkState == DEV_ZB_COORD ) ||
           ( zclSampleTemperatureSensor_NwkState == DEV_ROUTER ) )
      {
        // display help key with permit join status
        if ( gPermitDuration )
        {
          HalLcdWriteString( "SW5: Help      *", HAL_LCD_LINE_3 );
        }
        else
        {
          HalLcdWriteString( "SW5: Help       ", HAL_LCD_LINE_3 );
        }
      }
      else
      {
        // display help key
        HalLcdWriteString( (char *)sSwHelp, HAL_LCD_LINE_3 );
      }
    #endif
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_LcdDisplayHelpMode
     *
     * @brief   Called to display the SW options on the LCD.
     *
     * @param   none
     *
     * @return  none
     */
    void zclSampleTemperatureSensor_LcdDisplayHelpMode( void )
    {
    #ifdef LCD_SUPPORTED
      HalLcdWriteString( (char *)sSwTempUp, HAL_LCD_LINE_1 );
      HalLcdWriteString( (char *)sSwEZMode, HAL_LCD_LINE_2 );
      HalLcdWriteString( (char *)sSwTempDown, HAL_LCD_LINE_3 );
    #endif
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_SendTemp
     *
     * @brief   Called to send current temperature information to the thermostat
     *
     * @param   none
     *
     * @return  none
     */
    static void zclSampleTemperatureSensor_SendTemp( void )
    {
    #ifdef ZCL_REPORT
      zclReportCmd_t *pReportCmd;
      //zclSampleTemperatureSensor_MeasuredValue = getTemperature();
      pReportCmd = osal_mem_alloc( sizeof(zclReportCmd_t) + sizeof(zclReport_t) );
      if ( pReportCmd != NULL )
      {
        pReportCmd->numAttr = 1;
        pReportCmd->attrList[0].attrID = ATTRID_MS_TEMPERATURE_MEASURED_VALUE;
        pReportCmd->attrList[0].dataType = ZCL_DATATYPE_INT16;
        pReportCmd->attrList[0].attrData = (void *)(&zclSampleTemperatureSensor_MeasuredValue);
    
        zcl_SendReportCmd( SAMPLETEMPERATURESENSOR_ENDPOINT, &zclSampleTemperatureSensor_DstAddr,
                           ZCL_CLUSTER_ID_MS_TEMPERATURE_MEASUREMENT,
                           pReportCmd, ZCL_FRAME_SERVER_CLIENT_DIR, TRUE, zclSampleTemperatureSensorSeqNum++ );
      }
    
      osal_mem_free( pReportCmd );
    #endif  // ZCL_REPORT
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_ProcessIdentifyTimeChange
     *
     * @brief   Called to process any change to the IdentifyTime attribute.
     *
     * @param   none
     *
     * @return  none
     */
    static void zclSampleTemperatureSensor_ProcessIdentifyTimeChange( void )
    {
      if ( zclSampleTemperatureSensor_IdentifyTime > 0 )
      {
        osal_start_timerEx( zclSampleTemperatureSensor_TaskID, SAMPLETEMPERATURESENSOR_IDENTIFY_TIMEOUT_EVT, 1000 );
        HalLedBlink ( HAL_LED_4, 0xFF, HAL_LED_DEFAULT_DUTY_CYCLE, HAL_LED_DEFAULT_FLASH_TIME );
      }
      else
      {
        if ( zclSampleTemperatureSensor_OnOff )
        {
          HalLedSet ( HAL_LED_4, HAL_LED_MODE_ON );
        }
        else
        {
          HalLedSet ( HAL_LED_4, HAL_LED_MODE_OFF );
        }
    
        osal_stop_timerEx( zclSampleTemperatureSensor_TaskID, SAMPLETEMPERATURESENSOR_IDENTIFY_TIMEOUT_EVT );
      }
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_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 zclSampleTemperatureSensor_BasicResetCB( void )
    {
      // Put device back to factory default settings
      zgWriteStartupOptions( ZG_STARTUP_SET, 3 );   // bit set both default configuration and default network
    
      // restart device
      MT_SysCommandProcessing( aProcessCmd );
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_IdentifyCB
     *
     * @brief   Callback from the ZCL General Cluster Library when
     *          it received an Identity Command for this application.
     *
     * @param   srcAddr - source address and endpoint of the response message
     * @param   identifyTime - the number of seconds to identify yourself
     *
     * @return  none
     */
    static void zclSampleTemperatureSensor_IdentifyCB( zclIdentify_t *pCmd )
    {
      zclSampleTemperatureSensor_IdentifyTime = pCmd->identifyTime;
      zclSampleTemperatureSensor_ProcessIdentifyTimeChange();
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_IdentifyQueryRspCB
     *
     * @brief   Callback from the ZCL General Cluster Library when
     *          it received an Identity Query Response Command for this application.
     *
     * @param   srcAddr - requestor's address
     * @param   timeout - number of seconds to identify yourself (valid for query response)
     *
     * @return  none
     */
    static void zclSampleTemperatureSensor_IdentifyQueryRspCB( zclIdentifyQueryRsp_t *pRsp )
    {
      (void)pRsp;
    #ifdef ZCL_EZMODE
      {
        zclEZMode_ActionData_t data;
        data.pIdentifyQueryRsp = pRsp;
        zcl_EZModeAction ( EZMODE_ACTION_IDENTIFY_QUERY_RSP, &data );
      }
    #endif
    }
    
    /******************************************************************************
     *
     *  Functions for processing ZCL Foundation incoming Command/Response messages
     *
     *****************************************************************************/
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_ProcessIncomingMsg
     *
     * @brief   Process ZCL Foundation incoming message
     *
     * @param   pInMsg - pointer to the received message
     *
     * @return  none
     */
    static void zclSampleTemperatureSensor_ProcessIncomingMsg( zclIncomingMsg_t *pInMsg)
    {
      switch ( pInMsg->zclHdr.commandID )
      {
    #ifdef ZCL_READ
        case ZCL_CMD_READ_RSP:
          zclSampleTemperatureSensor_ProcessInReadRspCmd( pInMsg );
          break;
    #endif
    #ifdef ZCL_WRITE
        case ZCL_CMD_WRITE_RSP:
          zclSampleTemperatureSensor_ProcessInWriteRspCmd( pInMsg );
          break;
    #endif
    #ifdef ZCL_REPORT
        // See ZCL Test Applicaiton (zcl_testapp.c) for sample code on Attribute Reporting
        case ZCL_CMD_CONFIG_REPORT:
          //zclSampleTemperatureSensor_ProcessInConfigReportCmd( pInMsg );
          break;
    
        case ZCL_CMD_CONFIG_REPORT_RSP:
          //zclSampleTemperatureSensor_ProcessInConfigReportRspCmd( pInMsg );
          break;
    
        case ZCL_CMD_READ_REPORT_CFG:
          //zclSampleTemperatureSensor_ProcessInReadReportCfgCmd( pInMsg );
          break;
    
        case ZCL_CMD_READ_REPORT_CFG_RSP:
          //zclSampleTemperatureSensor_ProcessInReadReportCfgRspCmd( pInMsg );
          break;
    
        case ZCL_CMD_REPORT:
          //zclSampleTemperatureSensor_ProcessInReportCmd( pInMsg );
          break;
    #endif
        case ZCL_CMD_DEFAULT_RSP:
          zclSampleTemperatureSensor_ProcessInDefaultRspCmd( pInMsg );
          break;
    #ifdef ZCL_DISCOVER
        case ZCL_CMD_DISCOVER_CMDS_RECEIVED_RSP:
          zclSampleTemperatureSensor_ProcessInDiscCmdsRspCmd( pInMsg );
          break;
    
        case ZCL_CMD_DISCOVER_CMDS_GEN_RSP:
          zclSampleTemperatureSensor_ProcessInDiscCmdsRspCmd( pInMsg );
          break;
    
        case ZCL_CMD_DISCOVER_ATTRS_RSP:
          zclSampleTemperatureSensor_ProcessInDiscAttrsRspCmd( pInMsg );
          break;
    
        case ZCL_CMD_DISCOVER_ATTRS_EXT_RSP:
          zclSampleTemperatureSensor_ProcessInDiscAttrsExtRspCmd( pInMsg );
          break;
    #endif
        default:
          break;
      }
    
      if ( pInMsg->attrCmd )
      {
        osal_mem_free( pInMsg->attrCmd );
      }
    }
    
    #ifdef ZCL_READ
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_ProcessInReadRspCmd
     *
     * @brief   Process the "Profile" Read Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleTemperatureSensor_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      zclSampleTemperatureSensor_ProcessInWriteRspCmd
     *
     * @brief   Process the "Profile" Write Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleTemperatureSensor_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      zclSampleTemperatureSensor_ProcessInDefaultRspCmd
     *
     * @brief   Process the "Profile" Default Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleTemperatureSensor_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      zclSampleTemperatureSensor_ProcessInDiscCmdsRspCmd
     *
     * @brief   Process the Discover Commands Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleTemperatureSensor_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      zclSampleTemperatureSensor_ProcessInDiscAttrsRspCmd
     *
     * @brief   Process the "Profile" Discover Attributes Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleTemperatureSensor_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      zclSampleTemperatureSensor_ProcessInDiscAttrsExtRspCmd
     *
     * @brief   Process the "Profile" Discover Attributes Extended Response Command
     *
     * @param   pInMsg - incoming message to process
     *
     * @return  none
     */
    static uint8 zclSampleTemperatureSensor_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
    
    #ifdef ZCL_EZMODE
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_ProcessZDOMsgs
     *
     * @brief   Called when this node receives a ZDO/ZDP response.
     *
     * @param   none
     *
     * @return  status
     */
    static void zclSampleTemperatureSensor_ProcessZDOMsgs( zdoIncomingMsg_t *pMsg )
    {
      zclEZMode_ActionData_t data;
      ZDO_MatchDescRsp_t *pMatchDescRsp;
    
      // Let EZ-Mode know of the Match Descriptor Response
      if ( pMsg->clusterID == Match_Desc_rsp )
      {
        pMatchDescRsp = ZDO_ParseEPListRsp( pMsg );
        data.pMatchDescRsp = pMatchDescRsp;
        zcl_EZModeAction( EZMODE_ACTION_MATCH_DESC_RSP, &data );
        osal_mem_free( pMatchDescRsp );
      }
    }
    
    /*********************************************************************
     * @fn      zclSampleTemperatureSensor_EZModeCB
     *
     * @brief   The Application is informed of events. This can be used to show on the UI what is
    *           going on during EZ-Mode steering/finding/binding.
     *
     * @param   state - an
     *
     * @return  none
     */
    static void zclSampleTemperatureSensor_EZModeCB( zlcEZMode_State_t state, zclEZMode_CBData_t *pData )
    {
    #ifdef LCD_SUPPORTED
      char szLine[20];
      char *pStr;
      uint8 err;
    #endif
    
      // time to go into identify mode
      if ( state == EZMODE_STATE_IDENTIFYING )
      {
        zclSampleTemperatureSensor_IdentifyTime = ( EZMODE_TIME / 1000 );  // convert to seconds
        zclSampleTemperatureSensor_ProcessIdentifyTimeChange();
      }
    
      // autoclosing, show what happened (success, cancelled, etc...)
      if( state == EZMODE_STATE_AUTOCLOSE )
      {
    #ifdef LCD_SUPPORTED
        pStr = NULL;
        err = pData->sAutoClose.err;
        if ( err == EZMODE_ERR_SUCCESS )
        {
          pStr = "EZMode: Success";
        }
        else if ( err == EZMODE_ERR_NOMATCH )
        {
          pStr = "EZMode: NoMatch"; // not a match made in heaven
        }
        if ( pStr )
        {
          if ( giTemperatureSensorScreenMode == TEMPSENSE_MAINMODE )
          {
            HalLcdWriteString ( pStr, HAL_LCD_LINE_2 );
          }
        }
    #endif
      }
    
      // finished, either show DstAddr/EP, or nothing (depending on success or not)
      if( state == EZMODE_STATE_FINISH )
      {
        // turn off identify mode
        zclSampleTemperatureSensor_IdentifyTime = 0;
        zclSampleTemperatureSensor_ProcessIdentifyTimeChange();
    
    #ifdef LCD_SUPPORTED
        // if successful, inform user which nwkaddr/ep we bound to
        pStr = NULL;
        err = pData->sFinish.err;
        if( err == EZMODE_ERR_SUCCESS )
        {
          // "EZDst:1234 EP:34"
          osal_memcpy( szLine, "EZDst:", 6 );
          zclHA_uint16toa( pData->sFinish.nwkaddr, &szLine[6] );
          osal_memcpy( &szLine[10], " EP:", 4 );
          _ltoa( pData->sFinish.ep, (void *)(&szLine[14]), 16 );  // _ltoa NULL terminates
          pStr = szLine;
        }
        else if ( err == EZMODE_ERR_BAD_PARAMETER )
        {
          pStr = "EZMode: BadParm";
        }
        else if ( err == EZMODE_ERR_CANCELLED )
        {
          pStr = "EZMode: Cancel";
        }
        else
        {
          pStr = "EZMode: TimeOut";
        }
        if ( pStr )
        {
          if ( giTemperatureSensorScreenMode == TEMPSENSE_MAINMODE )
          {
            HalLcdWriteString ( pStr, HAL_LCD_LINE_2 );
          }
        }
    #endif  // LCD_SUPPORTED
    
        // show main UI screen 3 seconds after joining network
        osal_start_timerEx( zclSampleTemperatureSensor_TaskID, SAMPLETEMPERATURESENSOR_MAIN_SCREEN_EVT, 3000 );
    
        // report current temperature reading 15 seconds after joinging the network
        osal_start_timerEx( zclSampleTemperatureSensor_TaskID, SAMPLETEMPERATURESENSOR_TEMP_SEND_EVT, SAMPLETEMPERATURESENSOR_REPORT_INTERVAL );
      }
    }
    #endif // ZCL_EZMODE
    
    /****************************************************************************
    ****************************************************************************/