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.

WEBENCH® Tools/CC2530: Z-stack 1.2.2 sleep issue for battery based end device

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

Tool/software: WEBENCH® Design Tools

Hi,

we are using Z-stack 1.2.2 with cc2530.

we developed one end device code with z-stack 1.2.2 but there is some sleep mode power issue.

we used POWER_SEVING Preprocessor for "osal_pwrmgr_device(PWRMGR_BATTERY);" function.

but still device does not goes to sleep mode.

code working = first initialize required gpio and interrupt. then start OSAL timer event for sent data at specific time interval. after sent data to cordinator end device should be goes to sleep mode. by using "osal_pwrmgr_device(PWRMGR_BATTERY);" but it's not work. can you explain me or suggest me proper solution for this issue.

i attached my code file below. could you suggest me how can i put my device in sleep mode(PM2).

thanks in advance.
-Jignesh
zcl_sampletemperaturesensor.h

/**************************************************************************************************
  Filename:       zcl_sampletemperaturesensor.c
  Revised:        $Date: 2014-10-24 16:04:46 -0700 (Fri, 24 Oct 2014) $
  Revision:       $Revision: 40796 $

  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
 */
#define LED     P2_0  //hitesh
#define ON      1
#define OFF     0


// how often to report temperature
#define SAMPLETEMPERATURESENSOR_REPORT_INTERVAL   10000   //hitesh

/*********************************************************************
 * CONSTANTS
 */
#define MAX_PAYLOAD_BYTE 32
#define MIN_PAYLOAD_BYTE 05 //change from 11 to 5

#define SPIKE_SYNC_VALUE 0xDF
#define COMMAND_SET  0x01
#define COMMAND_GET  0x02
#define COMMAND_SET_ALL  0x03
#define COMMAND_RESTART  0x04
#define COMMAND_ACK_ROUTER      0x05
#define COMMAND_GET_SWITCH  0x06
#define COMMAND_ACK_COORDINATOR  0x07
#define COMMAND_GET_DOOR_SENSOR  0x08
#define COMMAND_GET_MOTION_SENSOR 0x09
#define COMMAND_SET_LOCK_SWITCH 0x0A
#define COMMAND_SEND_BOOT_STATUS  0x0B
#define COMMAND_GET_COORDINATOR_ADDR  0x0C
#define COMMAND_SEND_BOARDCAST_ROUTER  0x0D
#define COMMAND_ACK_DOOR  0x0E
#define COMMAND_ACK_MOTION 0x0F
#define COMMAND_REJOIN  0x10
#define COMMAND_NWKCNT  0x11
#define COMMAND_PRIORITY_SET  0x12
#define COMMAND_GET_SINGLE 0x13

#define COMMAND_GET_PANID   0x1A
#define COMMAND_SET_PANID   0x1B

#define COMMAND_GET_TEMPRATURE          0x16 //--Jay
#define COMMAND_GET_BATVOLT             0x17 //--Jay

#define COMMAND_DEVICE_RESET      0x99 //-Jay       reset the router    
#define COMMAND_DEVICE_CONFIG     0x34 //-Jay       configure router/End device
#define COMMAND_GET_DEVICE_NETID  0x35 //-Jay       get Network Addres of Router/End device which connected via UART to the Coordinator
#define COMMAND_GET_NETID         0x36 //-Jay       get Network Address for requested Device IEEE MAC which is in Network

#define DEVICE_TYPE                 0x0A01 //- (0x0A01)

#define CONFIG          0x19A     //Memory address of config variable used for auto configuration of device
#define SLEEP           0x193

#define RISING_EDGE   0
#define FALLING_EDGE  1

#define DEBOUNCE_VALUE  25

/* CPU port interrupt */
#define DOOR_PORT_0_IF P0IF
#define CONFIG_PORT_2_IF P2IF

/* SW_6 is at P0.1 */
#define DOOR_PORT   P0
#define DOOR_BIT    BV(5)
#define DOOR_SEL    P0SEL
#define DOOR_DIR    P0DIR

/* edge interrupt */
#define DOOR_EDGEBIT  BV(0)
#define DOOR_EDGE     FALLING_EDGE

/* SW_6 interrupts */
#define DOOR_IEN      IEN1  /* CPU interrupt mask register */
#define DOOR_IENBIT   BV(5) /* Mask bit for all of Port_0 */
#define DOOR_ICTL     P0IEN /* Port Interrupt Control register */
#define DOOR_ICTLBIT  BV(5) /* P0IEN - P0.5 enable/disable bit */
#define DOOR_PXIFG    P0IFG /* Interrupt flag at source */

/* Joy stick move at P2.0 */
#define CONFIG_PORT   P2
#define CONFIG_BIT    BV(2)
#define CONFIG_SEL    P2SEL
#define CONFIG_DIR    P2DIR

#define DOOR_STS      P0_5
#define CONF_STS      P2_2

/* edge interrupt */
#define CONFIG_EDGEBIT  BV(3)
#define CONFIG_EDGE     RISING_EDGE

/* Joy move interrupts */
#define CONFIG_IEN      IEN2  /* CPU interrupt mask register */
#define CONFIG_IENBIT   BV(1) /* Mask bit for all of Port_2 */
#define CONFIG_ICTL     P2IEN /* Port Interrupt Control register */
#define CONFIG_ICTLBIT  BV(2) /* P2IENL - P2.0<->P2.3 enable/disable bit */
#define CONFIG_PXIFG    P2IFG /* Interrupt flag at source */

/*********************************************************************
 * TYPEDEFS5
 */

/*********************************************************************
 * GLOBAL VARIABLES
 */
byte zclSampleTemperatureSensor_TaskID;

uint8 zclSampleTemperatureSensorSeqNum;

static byte gPermitDuration = 0x00;

byte GenericApp_TaskID;   // Task ID for internal task/event processing
// This variable will be received when
// GenericApp_Init() is called.

devStates_t GenericApp_NwkState;

byte GenericApp_TransID;  // This is the unique message ID (counter)

afAddrType_t GenericApp_DstAddr;

uint8 buff[MAX_PAYLOAD_BYTE];
uint16 length=0,sample=100, count;
uint16 rxTail,rxHead,auto_config_time=0,temp_NwkAddr=0;
uint8 rxBuf[128];
bool config;
uint8 DoorStatus;
uint16 BAT_VOLT;
bool configInt,BOOT = 1;

const cId_t GenericApp_ClusterList[GENERICAPP_MAX_CLUSTERS] =
{
  GENERICAPP_CLUSTERID
};

const SimpleDescriptionFormat_t GenericApp_SimpleDesc =
{
  GENERICAPP_ENDPOINT,              //  int Endpoint;
  GENERICAPP_PROFID,                //  uint16 AppProfId[2];
  GENERICAPP_DEVICEID,              //  uint16 AppDeviceId[2];
  GENERICAPP_DEVICE_VERSION,        //  int   AppDevVer:4;
  GENERICAPP_FLAGS,                 //  int   AppFlags:4;
  GENERICAPP_MAX_CLUSTERS,          //  byte  AppNumInClusters;
  (cId_t *)GenericApp_ClusterList,  //  byte *pAppInClusterList;
  GENERICAPP_MAX_CLUSTERS,          //  byte  AppNumInClusters;
  (cId_t *)GenericApp_ClusterList   //  byte *pAppInClusterList;
};

// This is the Endpoint/Interface description.  It is defined here, but
// filled-in in GenericApp_Init().  Another way to go would be to fill
// in the structure here and make it a "const" (in code space).  The
// way it's defined in this sample app it is define in RAM.
endPointDesc_t GenericApp_epDesc;
/*********************************************************************
 * GLOBAL FUNCTIONS
 */
void microSecondDelay(int microSecs); //hitesh
void milliSecondDelay(int milliSecs);  
void SPIKEHalKeyInitEndDevice( void );
static void SPIKE_UART_Init(void);
void DoorConfigInit( void );
void DoorConfig (void);
void ProcessInterrupt(void);  
void DoorConfigPoll(void);
static void SPIKE_UART_CallBack(uint8 port, uint8 event);
static void SPIKE_send_coordinator(uint8 * data,uint16 data_len);
/*********************************************************************
 * LOCAL VARIABLES
 */
afAddrType_t zclSampleTemperatureSensor_DstAddr;

/*#if defined (SPIKE_END_DEVICE)
  SPIKEHalKeyInitEndDevice();   
#endif*/

#ifdef ZCL_EZMODE
//SPIKEHalKeyInitEndDevice();  
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 )//007
{   
  zclSampleTemperatureSensor_TaskID = task_id;

  // Set destination address to indirect
  zclSampleTemperatureSensor_DstAddr.addrMode = (afAddrMode_t)Addr16Bit;
  zclSampleTemperatureSensor_DstAddr.endPoint = 0;
  zclSampleTemperatureSensor_DstAddr.addr.shortAddr = 0x0000;

  // 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);

  // Fill out the endpoint description.
  sampleTemperatureSensor_TestEp.endPoint = GENERICAPP_ENDPOINT;
  sampleTemperatureSensor_TestEp.task_id = &zclSampleTemperatureSensor_TaskID;
  sampleTemperatureSensor_TestEp.simpleDesc
    = (SimpleDescriptionFormat_t *)&GenericApp_SimpleDesc;
  sampleTemperatureSensor_TestEp.latencyReq = noLatencyReqs;
  // Register for a test endpoint
  afRegister( &sampleTemperatureSensor_TestEp );
  
  SPIKEHalKeyInitEndDevice();

#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 )
  {
  //  SPIKEHalKeyInitEndDevice();   
    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
   // 3();
  }

  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 ) )
    {
      zAddrType_t tmpAddr;

      tmpAddr.addrMode = Addr16Bit;
      tmpAddr.addr.shortAddr = NLME_GetShortAddr();

      // toggle permit join
      gPermitDuration = gPermitDuration ? 0 : 0xff;

      // Trust Center significance is always true
      ZDP_MgmtPermitJoinReq( &tmpAddr, gPermitDuration, TRUE, FALSE );
    }
  }

  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*/
}
void SPIKEHalKeyInitEndDevice( void ) //hitesh
{ 
  P0SEL &= 0x00;                              //Set all pins as input inorder to power saving
  P0DIR &= 0x00;
  P1SEL &= 0x00;
  P1DIR &= 0x00;
  P2SEL &= 0xF8;
  P2DIR &= 0xF0;
  
  /*All the ports are pulled down*/
  P2INP |= (0xE0);
  
  /*LED pin tri-state mode*/
  P2INP |= BV(0);
  P2SEL &= ~BV(0);
  P2DIR |= BV(0);
  LED =0;
  P1INP |= BV(0);
  P1SEL &= ~BV(0);
  P1DIR |= BV(0);
  P1_0 = 0;  
  SPIKE_UART_Init(); //hitesh
  DoorConfigInit();
  DoorConfig();
  /*int i;
  for(i=0;i<3;i++){
    LED = 1;  
    milliSecondDelay(200);
    LED = 0; 
    milliSecondDelay(200);
  }*/
   //zclSampleTemperatureSensor_SendTemp();
   //osal_start_reload_timer( zclSampleTemperatureSensor_TaskID, SAMPLETEMPERATURESENSOR_TEMP_SEND_EVT, SAMPLETEMPERATURESENSOR_REPORT_INTERVAL );
   LED = 0;
   osal_pwrmgr_device(PWRMGR_BATTERY); 
}

static void SPIKE_UART_Init(void)
{
  P0SEL |= BV(2);
  P0SEL |= BV(3);
  
  P0DIR &= ~(BV(2));
  P0DIR |= BV(3);
  
  halUARTCfg_t uartConfig;
  /* UART Configuration */
  uartConfig.configured = TRUE;
  uartConfig.flowControl = HAL_UART_FLOW_OFF;
  uartConfig.baudRate    = HAL_UART_BR_115200;
  uartConfig.flowControlThreshold = MT_UART_THRESHOLD;
  uartConfig.rx.maxBufSize = MT_UART_TX_BUFF_MAX;
  uartConfig.tx.maxBufSize = MT_UART_RX_BUFF_MAX;
  uartConfig.idleTimeout = MT_UART_IDLE_TIMEOUT;
  uartConfig.intEnable = TRUE;
  uartConfig.callBackFunc = SPIKE_UART_CallBack;
  HalUARTOpen(HAL_UART_PORT_0, &uartConfig);
}

static void SPIKE_UART_CallBack(uint8 port, uint8 event)
{
  uint16 length;
  uint8 payload[MAX_PAYLOAD_BYTE];
  switch(event) 
  {        
  case HAL_UART_RX_FULL:
  case HAL_UART_RX_ABOUT_FULL:
  case HAL_UART_RX_TIMEOUT:
    {
      length = Hal_UART_RxBufLen(HAL_UART_PORT_0);
      if(length < MAX_PAYLOAD_BYTE)
      {
        HalUARTRead(HAL_UART_PORT_0, payload, length);
        
        if(payload[0] != SPIKE_SYNC_VALUE)
        {
          break;
        } 
        
        else if(payload [9] == COMMAND_ACK_COORDINATOR )
        {
          HalUARTWrite(HAL_UART_PORT_0, payload, length);
        }
        
        else if(payload [9] == COMMAND_SEND_BOARDCAST_ROUTER)
        {
          //   Send_Broadcast_Cmd(payload,length);
        }
        
        else if(payload [9] == COMMAND_GET_PANID)
        {
          
        }
        
        else if(payload [9] == COMMAND_DEVICE_CONFIG)
        {
          osal_pwrmgr_device(PWRMGR_ALWAYS_ON); 
          zgConfigPANID = (uint16)payload[11]<<8 | (uint16)payload[12];
          osal_nv_write(ZCD_NV_PANID, 0, osal_nv_item_len( ZCD_NV_PANID ), &zgConfigPANID);
          milliSecondDelay(100);
          
          buff[0]=SPIKE_SYNC_VALUE;
          osal_memcpy(&buff[1], NLME_GetExtAddr(), 8);
          length +=8;
          buff[9]=payload[9];
          buff[10]= 0x02;
          
          buff[11] =0x0A;
          buff[12] =0x01;
          
          length=13;
          HalUARTWrite(HAL_UART_PORT_0, buff, length);
        }
        
        else if(payload [9] == COMMAND_DEVICE_RESET)
        {
          
          zgWriteStartupOptions( ZG_STARTUP_SET, ZCD_STARTOPT_DEFAULT_NETWORK_STATE );
          milliSecondDelay(100);
          SystemResetSoft(); 
          
        }
        else if(payload [9] == COMMAND_GET_DEVICE_NETID)
        {
          buff[0]=0xDF;
          osal_memcpy(&buff[1], NLME_GetExtAddr(), 8);
          length +=8;
          buff[9]=COMMAND_GET_DEVICE_NETID;
          buff[10]=0x02;
          buff[11]=NLME_GetShortAddr()>>8;
          buff[12]=NLME_GetShortAddr();
          
          length=13;
          HalUARTWrite(HAL_UART_PORT_0, buff, length);
        }    
        else
        {
          HalUARTWrite(HAL_UART_PORT_0, payload, length);
          //SPIKEPayloadProcess(payload,length);
        }
      }
    }
    break;
  }
}

void DoorConfigInit( void )
{
  //    P0INP |= BV(5); 
  //    DOOR_SEL &= ~(DOOR_BIT);    /* Set pin function to GPIO */
  //    DOOR_DIR &= ~(DOOR_BIT);    /* Set pin direction to Input */
  
  // P2INP |= BV(7);              /*Port 2 set in -BV(2)-tri-state mode  -BV(7)-Internally Pull Down  */
  CONFIG_SEL &= ~(CONFIG_BIT); /* Set pin function to GPIO */
  CONFIG_DIR &= ~(CONFIG_BIT); /* Set pin direction to Input */
}  

void DoorConfig (void)
{
  //      /* Rising/Falling edge configuratinn */
  //      PICTL &= ~(DOOR_EDGEBIT);    /* Clear the edge bit */
  //      /* For falling edge, the bit must be set. */
  //    #if (DOOR_EDGE == FALLING_EDGE)
  //      PICTL |= DOOR_EDGEBIT;
  //    #endif
  //      /* Interrupt configuration:
  //       * - Enable interrupt
  //       * - Enable CPU interrupt generation at the port
  //       * - Clear any pending interrupt
  //       */
  //      DOOR_ICTL |= DOOR_ICTLBIT;
  //      DOOR_IEN |= DOOR_IENBIT;
  //      DOOR_PXIFG = ~(DOOR_BIT);
  /* Rising/Falling edge configuratinn */
  PICTL &= ~(CONFIG_EDGEBIT);    /* Clear the edge bit */
  /* For falling edge, the bit must be set. */
#if (CONFIG_EDGE == FALLING_EDGE)
  PICTL |= CONFIG_EDGEBIT;
#endif
  /* Interrupt configuration:
  * - Enable interrupt generation at the port
  * - Enable CPU interrupt
  * - Clear any pending interrupt
  */
  CONFIG_ICTL |= CONFIG_ICTLBIT;
  CONFIG_IEN |= CONFIG_IENBIT;
  CONFIG_PXIFG = ~(CONFIG_BIT);
  
  osal_stop_timerEx(GenericApp_TaskID, SENSE_CONFIG_EVT);  /* Cancel polling if active */
  
  if(P2_2)
  {
    Onboard_wait(1);
    if(P2_2)
    {
      configInt = TRUE;
      osal_stop_timerEx(GenericApp_TaskID, SEND_BATVOLT_EVT); 
      osal_start_timerEx (GenericApp_TaskID, SENSE_CONFIG_EVT, 1000);
    }
  }
}

/*HAL_ISR_FUNCTION( halKeyPort0Isr, P0INT_VECTOR )
{
HAL_ENTER_ISR();
if (DOOR_PXIFG & DOOR_BIT)
{
ProcessInterrupt();
    }
//Clear the CPU interrupt flag for Port_0 PxIFG has to be cleared before PxIF
DOOR_PXIFG = 0;
DOOR_PORT_0_IF = 0;
CLEAR_SLEEP_MODE();
HAL_EXIT_ISR();
  }*/

HAL_ISR_FUNCTION( halKeyPort2Isr, P2INT_VECTOR )
{
  HAL_ENTER_ISR();    
  if (CONFIG_PXIFG & CONFIG_BIT)
  {
    ProcessInterrupt();
  }
  /*
  Clear the CPU interrupt flag for Port_2
  PxIFG has to be cleared before PxIF
  Notes: P2_1 and P2_2 are debug lines.
  */
  CONFIG_PXIFG = 0;
  CONFIG_PORT_2_IF = 0;
 // CLEAR_SLEEP_MODE();
  HAL_EXIT_ISR();
}  

void ProcessInterrupt(void)
{
  bool valid=FALSE;
  /*
  if (DOOR_PXIFG & DOOR_BIT)  // Interrupt Flag has been set
  {
  DOOR_PXIFG = ~(DOOR_BIT); // Clear Interrupt Flag
  doorInt = TRUE;
  valid = TRUE;
}*/
  
  if (CONFIG_PXIFG & CONFIG_BIT)  /* Interrupt Flag has been set */
  {
    CONFIG_PXIFG = ~(CONFIG_BIT); /* Clear Interrupt Flag */
    configInt = TRUE;
    valid = TRUE;
  }
  
  if (valid)
  {
    osal_start_timerEx (GenericApp_TaskID, SENSE_CONFIG_EVT, SENSE_CONFIG_TIMEOUT);
  }
} 

void DoorConfigPoll(void)
{
  /*if(doorInt == TRUE)
  {
  doorInt = FALSE; 
  if((PICTL & BV(0)) && !DOOR_STS)                  //Check weather interrupt is set to Falling edge detect or not
  { 
  PICTL &= ~(DOOR_EDGEBIT);   
  DoorStatus=0x01;                            //that means Door is opened   
  sendDoorStatus();
}
      else if(!(PICTL & BV(0)) && DOOR_STS)             //change Rising Edge to Falling Edge Detect
  {               
  PICTL |= (DOOR_EDGEBIT); 
  DoorStatus=0x00;                            //that means Door is closed
  sendDoorStatus();
}
}*/
  
  if(configInt == TRUE)
  {
    configInt = FALSE;
    if((PICTL & BV(3)) && !CONF_STS)               //Check weather interrupt is set to Falling edge detect or not
    { 
     // LED = OFF;                                   //LED off indicating exiting configuration mode
      PICTL &= ~(CONFIG_EDGEBIT); 
      P0SEL &= ~(0x0C);
      P0DIR &= ~(0x0C);    
      U0UCR |= BV(7);                             //Flush the UART
      HalUARTSuspend();                           //Disable UART Receiver
      URX0IE=0;                                   //Disable UART interrupt
     // osal_pwrmgr_device(PWRMGR_BATTERY);
      osal_stop_timerEx(GenericApp_TaskID, GENERICAPP_SEND_MSG_EVT); 
     // SEND_data();
     // sendBatVolt();
      //osal_start_reload_timer (GenericApp_TaskID, SEND_BATVOLT_EVT, SEND_BATVOLT_TIMEOUT);
    }
    else if(!(PICTL & BV(3)) && CONF_STS)          //change Rising Edge to Falling Edge Detect
    {    
      
     // LED = ON;                                    //LED on indicating entering to configuration mode
      PICTL |= (CONFIG_EDGEBIT); 
      P0SEL |= 0x0C;
      P0DIR |= 0x08;
      SPIKE_UART_Init();
     // osal_pwrmgr_device(PWRMGR_ALWAYS_ON);
      //osal_stop_timerEx(GenericApp_TaskID, SEND_BATVOLT_EVT); 
     // osal_start_timerEx(GenericApp_TaskID, GENERICAPP_SEND_MSG_EVT, 100);
      //LED = ON;
    }
  }
  
  
}

void microSecondDelay(int microSecs)//hitesh
{
  
  while(microSecs--)
  {
    /* 32 NOPs == 1 usecs */
    asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop");
    asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop");
    asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop");
    asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop");
    asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop");
    asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop");
    asm("nop"); asm("nop");
  }
}

void milliSecondDelay(int milliSecs) //hitesh
{
  while(milliSecs--)
  {
    Onboard_wait(1000);
  }
}
/*********************************************************************
 * @fn      zclSampleTemperatureSensor_SendTemp
 *
 * @brief   Called to send current temperature information to the thermostat
 *
 * @param   none
 *
 * @return  none
 */
static void zclSampleTemperatureSensor_SendTemp( void )//hitesh
{
  
#ifdef ZCL_REPORT
  LED = 0;
  osal_pwrmgr_device(PWRMGR_ALWAYS_ON); 
  buff[0]=SPIKE_SYNC_VALUE;
  buff[1]=NLME_GetShortAddr()>>8;
  buff[2]=NLME_GetShortAddr();
  buff[3]=COMMAND_GET_TEMPRATURE;
  buff[4]=0x01;
  buff[5]=0x23;  ///temperature
  buff[6]=0x45;  ///temperature
  buff[7]=0x67;       //sign
  buff[8]=0x89;   //humidity
  
 /* int i;
  //LED =~ LED;
  for(i=0;i<3;i++){
    LED = 1;  
    milliSecondDelay(200);
    LED = 0; 
    milliSecondDelay(200);
  }*/
  
  SPIKE_send_coordinator(buff,9); 
  osal_pwrmgr_device(PWRMGR_BATTERY);
  /*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 *)(&buff);   //007
    
    // Set destination address to indirect
    zclSampleTemperatureSensor_DstAddr.addrMode = (afAddrMode_t)Addr16Bit;
    zclSampleTemperatureSensor_DstAddr.endPoint = 1;
    zclSampleTemperatureSensor_DstAddr.addr.shortAddr = 0x0000;


    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
}


static void SPIKE_send_coordinator(uint8 * data,uint16 data_len)
{ 
  GenericApp_DstAddr.addrMode = (afAddrMode_t)Addr16Bit;
  GenericApp_DstAddr.endPoint = GENERICAPP_ENDPOINT;
  GenericApp_DstAddr.addr.shortAddr = 0x0000;
  
  if (AF_DataRequest( &GenericApp_DstAddr, &sampleTemperatureSensor_TestEp,
                     GENERICAPP_CLUSTERID,
                     data_len ,
                     data,
                     &GenericApp_TransID,
                     (AF_DISCV_ROUTE  | AF_ACK_REQUEST), AF_DEFAULT_RADIUS ) == afStatus_SUCCESS ) //hitesh
  {
    
  }
}

/*********************************************************************
 * @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
  }*/
}
#endif // ZCL_EZMODE

/****************************************************************************
****************************************************************************/