/**************************************************************************************************
  Filename:       zcl_samplesw.c
  Revised:        $Date: 2015-08-19 17:11:00 -0700 (Wed, 19 Aug 2015) $
  Revision:       $Revision: 44460 $

  Description:    Zigbee Cluster Library - sample switch application.


  Copyright 2006-2013 Texas Instruments Incorporated. All rights reserved.

  IMPORTANT: Your use of this Software is limited to those specific rights
  granted under the terms of a software license agreement between the user
  who downloaded the software, his/her employer (which must be your employer)
  and Texas Instruments Incorporated (the "License").  You may not use this
  Software unless you agree to abide by the terms of the License. The License
  limits your use, and you acknowledge, that the Software may not be modified,
  copied or distributed unless embedded on a Texas Instruments microcontroller
  or used solely and exclusively in conjunction with a Texas Instruments radio
  frequency transceiver, which is integrated into your product.  Other than for
  the foregoing purpose, you may not use, reproduce, copy, prepare derivative
  works of, modify, distribute, perform, display or sell this Software and/or
  its documentation for any purpose.

  YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE
  PROVIDED “AS IS” WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,
  INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE,
  NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL
  TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT,
  NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER
  LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
  INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE
  OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT
  OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
  (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.

  Should you have any questions regarding your right to use this Software,
  contact Texas Instruments Incorporated at www.TI.com.
**************************************************************************************************/

/*********************************************************************
  This application implements a ZigBee On/Off Switch, based on Z-Stack 3.0.

  This application is based on the common sample-application user interface. Please see the main
  comment in zcl_sampleapp_ui.c. The rest of this comment describes only the content specific for
  this sample applicetion.
  
  Application-specific UI peripherals being used:

  - none (LED1 is currently unused by this application).

  Application-specific menu system:

    <TOGGLE LIGHT> Send an On, Off or Toggle command targeting appropriate devices from the binding table.
      Pressing / releasing [OK] will have the following functionality, depending on the value of the 
      zclSampleSw_OnOffSwitchActions attribute:
      - OnOffSwitchActions == 0: pressing [OK] will send ON command, releasing it will send OFF command;
      - OnOffSwitchActions == 1: pressing [OK] will send OFF command, releasing it will send ON command;
      - OnOffSwitchActions == 2: pressing [OK] will send TOGGLE command, releasing it will not send any command.

*********************************************************************/

#if ! defined ZCL_ON_OFF
#error ZCL_ON_OFF must be defined for this project.
#endif

/*********************************************************************
 * INCLUDES
 */
#include "ZComDef.h"
#include "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_samplesw.h"
#include "zcl_diagnostic.h"

#include "onboard.h"

/* HAL */
#include "hal_lcd.h"
#include "hal_led.h"
#include "hal_key.h"
#include "hal_adc.h"

#if defined (OTA_CLIENT) && (OTA_CLIENT == TRUE)
#include "zcl_ota.h"
#include "hal_ota.h"
#endif

#include "bdb.h"
#include "bdb_interface.h"

#include <stdio.h>

/*********************************************************************
 * MACROS
 */

#define APP_TITLE "TI Sample Switch"

/** @brief Debug mode
 *      open debug mode by defining  "DEBUG",
 *      close debug mode by defining "xDEBUG"
 */
#define HDEBUG
#ifdef  HDEBUG
  #define LOG(...) printf(__VA_ARGS__)
#else
  #define LOG(...)
#endif

/*********************************************************************
 * TYPEDEFS
 */

/*********************************************************************
 * GLOBAL VARIABLES
 */
byte zclSampleSw_TaskID;

uint8 zclSampleSwSeqNum;

uint8 zclSampleSw_OnOffSwitchType = ON_OFF_SWITCH_TYPE_MOMENTARY;

uint8 zclSampleSw_OnOffSwitchActions;

/*********************************************************************
 * GLOBAL FUNCTIONS
 */

/*********************************************************************
 * LOCAL VARIABLES
 */
afAddrType_t zclSampleSw_DstAddr;

// Endpoint to allow SYS_APP_MSGs
static endPointDesc_t sampleSw_TestEp =
{
  SAMPLESW_ENDPOINT,                  // endpoint
  0,
  &zclSampleSw_TaskID,
  (SimpleDescriptionFormat_t *)&zclSampleSw_SimpleDesc,  // No Simple description for this test endpoint
  (afNetworkLatencyReq_t)0            // No Network Latency req
};

//static uint8 aProcessCmd[] = { 1, 0, 0, 0 }; // used for reset command, { length + cmd0 + cmd1 + data }

devStates_t zclSampleSw_NwkState = DEV_INIT;

#if defined (OTA_CLIENT) && (OTA_CLIENT == TRUE)
#define DEVICE_POLL_RATE                 8000   // Poll rate for end device
#endif

#define SAMPLESW_TOGGLE_TEST_EVT   0x1000
/*********************************************************************
 * LOCAL FUNCTIONS
 */

// P2P, hhh, 2018.10.6
static void zclSampleSw_AF_P2P(uint16 destNwkAddr, uint16 cid, uint8 len, uint8 *data);
// Broadcast
static void zclSampleSw_AF_Broadcast(uint16 destNwkAddr, uint16 cid, uint8 len, uint8 *data);
// Groupcast
static void zclSampleSw_AF_Groupcast(uint16 groupId, uint16 cid, uint8 len, uint8 *data);
// Process incoming message
static void zclSampleSw_AF_RxProc(afIncomingMSGPacket_t *MSGpkt);

static void zclSampleSw_HandleKeys( byte shift, byte keys );
static void zclSampleSw_BasicResetCB( void );

static void zclSampleSw_ProcessCommissioningStatus(bdbCommissioningModeMsg_t *bdbCommissioningModeMsg);


// Functions to process ZCL Foundation incoming Command/Response messages
static void zclSampleSw_ProcessIncomingMsg( zclIncomingMsg_t *msg );
#ifdef ZCL_READ
static uint8 zclSampleSw_ProcessInReadRspCmd( zclIncomingMsg_t *pInMsg );
#endif
#ifdef ZCL_WRITE
static uint8 zclSampleSw_ProcessInWriteRspCmd( zclIncomingMsg_t *pInMsg );
#endif
static uint8 zclSampleSw_ProcessInDefaultRspCmd( zclIncomingMsg_t *pInMsg );
#ifdef ZCL_DISCOVER
static uint8 zclSampleSw_ProcessInDiscCmdsRspCmd( zclIncomingMsg_t *pInMsg );
static uint8 zclSampleSw_ProcessInDiscAttrsRspCmd( zclIncomingMsg_t *pInMsg );
static uint8 zclSampleSw_ProcessInDiscAttrsExtRspCmd( zclIncomingMsg_t *pInMsg );
#endif

#if defined (OTA_CLIENT) && (OTA_CLIENT == TRUE)
static void zclSampleSw_ProcessOTAMsgs( zclOTA_CallbackMsg_t* pMsg );
#endif

// Uart
#define ZCLSAMPLESW_UART_BUF_LEN        128
static uint8 uartBuf[ZCLSAMPLESW_UART_BUF_LEN];
static void zclSampleSw_InitUart(void);
static void zclSampleSw_UartCB(uint8 port, uint8 event);

#ifdef ZDO_COORDINATOR
  // On/Off test
  static uint16 zclSampleSw_OnOffTestAddr;
  static void zclSampleSw_processZDOMgs(zdoIncomingMsg_t *pMsg);
  static void zclSampleSw_OnOffTest(void);
#else
  static void zclSampleSw_DeviceAnnce( void );
  static void zclSampleSw_OnOffCB( uint8 cmd );
#endif

/*********************************************************************
 * CONSTANTS
 */

/*********************************************************************
 * REFERENCED EXTERNALS
 */
extern int16 zdpExternalStateTaskID;

/*********************************************************************
 * ZCL General Profile Callback table
 */
static zclGeneral_AppCallbacks_t zclSampleSw_CmdCallbacks =
{
  zclSampleSw_BasicResetCB,               // Basic Cluster Reset command
  NULL,                                   // Identify Trigger Effect command

#ifdef ZDO_COORDINATOR
  NULL,
#else
  zclSampleSw_OnOffCB,    // On/Off cluster commands
#endif
  
  NULL,                                   // On/Off cluster enhanced command Off with Effect
  NULL,                                   // On/Off cluster enhanced command On with Recall Global Scene
  NULL,                                   // On/Off cluster enhanced command On with Timed Off
#ifdef ZCL_LEVEL_CTRL
  NULL,                                   // Level Control Move to Level command
  NULL,                                   // Level Control Move command
  NULL,                                   // Level Control Step command
  NULL,                                   // Level Control Stop command
#endif
#ifdef ZCL_GROUPS
  NULL,                                   // Group Response commands
#endif
#ifdef ZCL_SCENES
  NULL,                                   // Scene Store Request command
  NULL,                                   // Scene Recall Request command
  NULL,                                   // Scene Response command
#endif
#ifdef ZCL_ALARMS
  NULL,                                   // Alarm (Response) commands
#endif
#ifdef SE_UK_EXT
  NULL,                                   // Get Event Log command
  NULL,                                   // Publish Event Log command
#endif
  NULL,                                   // RSSI Location command
  NULL                                    // RSSI Location Response command
};

/*********************************************************************
 * @fn          zclSampleSw_Init
 *
 * @brief       Initialization function for the zclGeneral layer.
 *
 * @param       none
 *
 * @return      none
 */
void zclSampleSw_Init( byte task_id )
{
  zclSampleSw_TaskID = task_id;

  // Set destination address to indirect
  zclSampleSw_DstAddr.addrMode = (afAddrMode_t)AddrNotPresent;
  zclSampleSw_DstAddr.endPoint = 0;
  zclSampleSw_DstAddr.addr.shortAddr = 0;

  // Register the Simple Descriptor for this application
  bdb_RegisterSimpleDescriptor( &zclSampleSw_SimpleDesc );

  // Register the ZCL General Cluster Library callback functions
  zclGeneral_RegisterCmdCallbacks( SAMPLESW_ENDPOINT, &zclSampleSw_CmdCallbacks );

  zclSampleSw_ResetAttributesToDefaultValues();
  
  // Register the application's attribute list
  zcl_registerAttrList( SAMPLESW_ENDPOINT, zclSampleSw_NumAttributes, zclSampleSw_Attrs );

  // Register the Application to receive the unprocessed Foundation command/response messages
  zcl_registerForMsg( zclSampleSw_TaskID );
  
  // Register for all key events - This app will handle all key events
  RegisterForKeys( zclSampleSw_TaskID );
  
  bdb_RegisterCommissioningStatusCB( zclSampleSw_ProcessCommissioningStatus );

  // Register for a test endpoint
  afRegister( &sampleSw_TestEp );
  
#ifdef ZCL_DIAGNOSTIC
  // Register the application's callback function to read/write attribute data.
  // This is only required when the attribute data format is unknown to ZCL.
  zcl_registerReadWriteCB( SAMPLESW_ENDPOINT, zclDiagnostic_ReadWriteAttrCB, NULL );

  if ( zclDiagnostic_InitStats() == ZSuccess )
  {
    // Here the user could start the timer to save Diagnostics to NV
  }
#endif

#if defined (OTA_CLIENT) && (OTA_CLIENT == TRUE)
  // Register for callback events from the ZCL OTA
  zclOTA_Register(zclSampleSw_TaskID);
#endif

  zdpExternalStateTaskID = zclSampleSw_TaskID;
  
#ifdef ZDO_COORDINATOR
  
  ZDO_RegisterForZDOMsg ( zclSampleSw_TaskID, Device_annce );
  
  bdb_StartCommissioning( BDB_COMMISSIONING_MODE_NWK_FORMATION |
                          BDB_COMMISSIONING_MODE_FINDING_BINDING );
  
  NLME_PermitJoiningRequest(255);
#else
  bdb_StartCommissioning( BDB_COMMISSIONING_MODE_NWK_STEERING |
                          BDB_COMMISSIONING_MODE_FINDING_BINDING );
  
  zclSampleSw_DeviceAnnce();
#endif
  
  // Init Uart
  zclSampleSw_InitUart();
  
  // hhh, 2018.10.6
  uartBuf[0] = 0xFC;
  uartBuf[1] = 6;
  uartBuf[2] = 0xFF;
  uartBuf[3] = 0xFF;
  uartBuf[4] = 9;
  uartBuf[5] = 1;
  HalUARTWrite(HAL_UART_PORT_0 ,  uartBuf , uartBuf[1]);
}

/*********************************************************************
 * @fn          zclSample_event_loop
 *
 * @brief       Event Loop Processor for zclGeneral.
 *
 * @param       none
 *
 * @return      none
 */
uint16 zclSampleSw_event_loop( uint8 task_id, uint16 events )
{
  afIncomingMSGPacket_t *MSGpkt;
  (void)task_id;  // Intentionally unreferenced parameter

  //Send toggle every 500ms
  if( events & SAMPLESW_TOGGLE_TEST_EVT )
  {
    osal_start_timerEx(zclSampleSw_TaskID,SAMPLESW_TOGGLE_TEST_EVT,500);
    zclGeneral_SendOnOff_CmdToggle( SAMPLESW_ENDPOINT, &zclSampleSw_DstAddr, FALSE, 0 );
    
    // return unprocessed events
    return (events ^ SAMPLESW_TOGGLE_TEST_EVT);
  }
  
  
  if ( events & SYS_EVENT_MSG )
  {
    while ( (MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( zclSampleSw_TaskID )) )
    {
      LOG("osal_msg_receive: %d\n", MSGpkt->hdr.event); 
      switch ( MSGpkt->hdr.event )
      {
        case AF_INCOMING_MSG_CMD:
          LOG("AF_INCOMING_MSG_CMD\n");
          zclSampleSw_AF_RxProc( MSGpkt );
          break;
        
        case ZCL_INCOMING_MSG:
          LOG("ZCL_INCOMING_MSG\n");
          // Incoming ZCL Foundation command/response messages
          zclSampleSw_ProcessIncomingMsg( (zclIncomingMsg_t *)MSGpkt );
          break;

        case KEY_CHANGE:
          zclSampleSw_HandleKeys( ((keyChange_t *)MSGpkt)->state, ((keyChange_t *)MSGpkt)->keys );
          break;

        case ZDO_STATE_CHANGE:
          
          break;

#if defined (OTA_CLIENT) && (OTA_CLIENT == TRUE)
        case ZCL_OTA_CALLBACK_IND:
          zclSampleSw_ProcessOTAMsgs( (zclOTA_CallbackMsg_t*)MSGpkt  );
          break;
#endif

    #ifdef ZDO_COORDINATOR
       case ZDO_CB_MSG:
          LOG("ZDO_CB_MSG\n");
          zclSampleSw_processZDOMgs( (zdoIncomingMsg_t *)MSGpkt );
          break; 
    #endif
          
        default:
          LOG("default msg\n");
          break;
      }

      // Release the memory
      osal_msg_deallocate( (uint8 *)MSGpkt );
    }

    // return unprocessed events
    return (events ^ SYS_EVENT_MSG);
  }

#if ZG_BUILD_ENDDEVICE_TYPE    
  if ( events & SAMPLEAPP_END_DEVICE_REJOIN_EVT )
  {
    bdb_ZedAttemptRecoverNwk();
    return ( events ^ SAMPLEAPP_END_DEVICE_REJOIN_EVT );
  }
#endif
  
#ifdef ZDO_COORDINATOR
  if ( events & SAMPLEAPP_ONOFF_TEST_EVT )
  {
    // hhh modified, 2018.10.6
    //zclSampleSw_OnOffTest();
    //osal_start_timerEx(zclSampleSw_TaskID, SAMPLEAPP_ONOFF_TEST_EVT, SAMPLEAPP_ONOFF_TEST_PERIOD);
    return ( events ^ SAMPLEAPP_ONOFF_TEST_EVT );
  }
#else
  // Rejoin
  if ( events & SAMPLEAPP_REJOIN_EVT )
  {
   bdb_StartCommissioning(BDB_COMMISSIONING_MODE_NWK_STEERING |
                      BDB_COMMISSIONING_MODE_FINDING_BINDING );
    
    return ( events ^ SAMPLEAPP_REJOIN_EVT );
  }
#endif
  
  // Discard unknown events
  return 0;
}

/*********************************************************************
 * @fn      zclSampleSw_HandleKeys
 *
 * @brief   Handles all key events for this device.
 *
 * @param   shift - true if in shift/alt.
 * @param   keys - bit field for key events. Valid entries:
 *                 HAL_KEY_SW_5
 *                 HAL_KEY_SW_4
 *                 HAL_KEY_SW_2
 *                 HAL_KEY_SW_1
 *
 * @return  none
 */
static void zclSampleSw_HandleKeys( byte shift, byte keys )
{ 
  if(keys & HAL_KEY_SW_6)
  {
  }
}
  
/*********************************************************************
 * @fn      zclSampleSw_ProcessCommissioningStatus
 *
 * @brief   Callback in which the status of the commissioning process are reported
 *
 * @param   bdbCommissioningModeMsg - Context message of the status of a commissioning process
 *
 * @return  none
 */
static void zclSampleSw_ProcessCommissioningStatus(bdbCommissioningModeMsg_t *bdbCommissioningModeMsg)
{
  switch(bdbCommissioningModeMsg->bdbCommissioningMode)
  {
    case BDB_COMMISSIONING_FORMATION:
      if(bdbCommissioningModeMsg->bdbCommissioningStatus == BDB_COMMISSIONING_SUCCESS)
      {
        //After formation, perform nwk steering again plus the remaining commissioning modes that has not been processed yet
        bdb_StartCommissioning(BDB_COMMISSIONING_MODE_NWK_STEERING | bdbCommissioningModeMsg->bdbRemainingCommissioningModes);
      }
      else
      {
        //Want to try other channels?
        //try with bdb_setChannelAttribute
      }
    break;
    case BDB_COMMISSIONING_NWK_STEERING:
      if(bdbCommissioningModeMsg->bdbCommissioningStatus == BDB_COMMISSIONING_SUCCESS)
      {
        //YOUR JOB:
        //We are on the nwk, what now?
      }
      else
      {
        #ifdef ZDO_COORDINATOR
        #else
        osal_start_timerEx(zclSampleSw_TaskID, 
                           SAMPLEAPP_REJOIN_EVT, 
                           SAMPLEAPP_REJOIN_PERIOD);
        #endif
      }
    break;
    case BDB_COMMISSIONING_FINDING_BINDING:
      if(bdbCommissioningModeMsg->bdbCommissioningStatus == BDB_COMMISSIONING_SUCCESS)
      {
        //YOUR JOB:
      }
      else
      {
        //YOUR JOB:
        //retry?, wait for user interaction?
      }
    break;
    case BDB_COMMISSIONING_INITIALIZATION:
      //Initialization notification can only be successful. Failure on initialization 
      //only happens for ZED and is notified as BDB_COMMISSIONING_PARENT_LOST notification
      
      //YOUR JOB:
      //We are on a network, what now?
      
    break;
#if ZG_BUILD_ENDDEVICE_TYPE    
    case BDB_COMMISSIONING_PARENT_LOST:
      if(bdbCommissioningModeMsg->bdbCommissioningStatus == BDB_COMMISSIONING_NETWORK_RESTORED)
      {
        //We did recover from losing parent
      }
      else
      {
        //Parent not found, attempt to rejoin again after a fixed delay
        osal_start_timerEx(zclSampleSw_TaskID, SAMPLEAPP_END_DEVICE_REJOIN_EVT, SAMPLEAPP_END_DEVICE_REJOIN_DELAY);
      }
    break;
#endif 
  }
}

/*********************************************************************
 * @fn      zclSampleSw_BasicResetCB
 *
 * @brief   Callback from the ZCL General Cluster Library
 *          to set all the Basic Cluster attributes to  default values.
 *
 * @param   none
 *
 * @return  none
 */
static void zclSampleSw_BasicResetCB( void )
{
  zclSampleSw_ResetAttributesToDefaultValues();
}

/******************************************************************************
 *
 *  Functions for processing ZCL Foundation incoming Command/Response messages
 *
 *****************************************************************************/

/*********************************************************************
 * @fn      zclSampleSw_ProcessIncomingMsg
 *
 * @brief   Process ZCL Foundation incoming message
 *
 * @param   pInMsg - pointer to the received message
 *
 * @return  none
 */
static void zclSampleSw_ProcessIncomingMsg( zclIncomingMsg_t *pInMsg )
{
  switch ( pInMsg->zclHdr.commandID )
  {
#ifdef ZCL_READ
    case ZCL_CMD_READ_RSP:
      zclSampleSw_ProcessInReadRspCmd( pInMsg );
      break;
#endif
#ifdef ZCL_WRITE
    case ZCL_CMD_WRITE_RSP:
      zclSampleSw_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:
      //zclSampleSw_ProcessInConfigReportCmd( pInMsg );
      break;

    case ZCL_CMD_CONFIG_REPORT_RSP:
      //zclSampleSw_ProcessInConfigReportRspCmd( pInMsg );
      break;

    case ZCL_CMD_READ_REPORT_CFG:
      //zclSampleSw_ProcessInReadReportCfgCmd( pInMsg );
      break;

    case ZCL_CMD_READ_REPORT_CFG_RSP:
      //zclSampleSw_ProcessInReadReportCfgRspCmd( pInMsg );
      break;

    case ZCL_CMD_REPORT:
      //zclSampleSw_ProcessInReportCmd( pInMsg );
      break;
#endif
    case ZCL_CMD_DEFAULT_RSP:
      zclSampleSw_ProcessInDefaultRspCmd( pInMsg );
      break;
#ifdef ZCL_DISCOVER
    case ZCL_CMD_DISCOVER_CMDS_RECEIVED_RSP:
      zclSampleSw_ProcessInDiscCmdsRspCmd( pInMsg );
      break;

    case ZCL_CMD_DISCOVER_CMDS_GEN_RSP:
      zclSampleSw_ProcessInDiscCmdsRspCmd( pInMsg );
      break;

    case ZCL_CMD_DISCOVER_ATTRS_RSP:
      zclSampleSw_ProcessInDiscAttrsRspCmd( pInMsg );
      break;

    case ZCL_CMD_DISCOVER_ATTRS_EXT_RSP:
      zclSampleSw_ProcessInDiscAttrsExtRspCmd( pInMsg );
      break;
#endif
    default:
      break;
  }

  if ( pInMsg->attrCmd )
    osal_mem_free( pInMsg->attrCmd );
}

#ifdef ZCL_READ
/*********************************************************************
 * @fn      zclSampleSw_ProcessInReadRspCmd
 *
 * @brief   Process the "Profile" Read Response Command
 *
 * @param   pInMsg - incoming message to process
 *
 * @return  none
 */
static uint8 zclSampleSw_ProcessInReadRspCmd( 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      zclSampleSw_ProcessInWriteRspCmd
 *
 * @brief   Process the "Profile" Write Response Command
 *
 * @param   pInMsg - incoming message to process
 *
 * @return  none
 */
static uint8 zclSampleSw_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      zclSampleSw_ProcessInDefaultRspCmd
 *
 * @brief   Process the "Profile" Default Response Command
 *
 * @param   pInMsg - incoming message to process
 *
 * @return  none
 */
static uint8 zclSampleSw_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      zclSampleSw_ProcessInDiscCmdsRspCmd
 *
 * @brief   Process the Discover Commands Response Command
 *
 * @param   pInMsg - incoming message to process
 *
 * @return  none
 */
static uint8 zclSampleSw_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      zclSampleSw_ProcessInDiscAttrsRspCmd
 *
 * @brief   Process the "Profile" Discover Attributes Response Command
 *
 * @param   pInMsg - incoming message to process
 *
 * @return  none
 */
static uint8 zclSampleSw_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      zclSampleSw_ProcessInDiscAttrsExtRspCmd
 *
 * @brief   Process the "Profile" Discover Attributes Extended Response Command
 *
 * @param   pInMsg - incoming message to process
 *
 * @return  none
 */
static uint8 zclSampleSw_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

#if defined (OTA_CLIENT) && (OTA_CLIENT == TRUE)
/*********************************************************************
 * @fn      zclSampleSw_ProcessOTAMsgs
 *
 * @brief   Called to process callbacks from the ZCL OTA.
 *
 * @param   none
 *
 * @return  none
 */
static void zclSampleSw_ProcessOTAMsgs( zclOTA_CallbackMsg_t* pMsg )
{
  uint8 RxOnIdle;

  switch(pMsg->ota_event)
  {
  case ZCL_OTA_START_CALLBACK:
    if (pMsg->hdr.status == ZSuccess)
    {
      // Speed up the poll rate
      RxOnIdle = TRUE;
      ZMacSetReq( ZMacRxOnIdle, &RxOnIdle );
      NLME_SetPollRate( 2000 );
    }
    break;

  case ZCL_OTA_DL_COMPLETE_CALLBACK:
    if (pMsg->hdr.status == ZSuccess)
    {
      // Reset the CRC Shadow and reboot.  The bootloader will see the
      // CRC shadow has been cleared and switch to the new image
      HalOTAInvRC();
      SystemReset();
    }
    else
    {
      // slow the poll rate back down.
      RxOnIdle = FALSE;
      ZMacSetReq( ZMacRxOnIdle, &RxOnIdle );
      NLME_SetPollRate(DEVICE_POLL_RATE);
    }
    break;

  default:
    break;
  }
}
#endif // defined (OTA_CLIENT) && (OTA_CLIENT == TRUE)


// ----------------------- ZDO_COORDINATOR or Device -------------------
#ifdef ZDO_COORDINATOR  // Coordinator!

/**
 * @fn      zclSampleSw_processZDOMgs
 *
 * @brief   Process ZDO Message
 */
static void zclSampleSw_processZDOMgs(zdoIncomingMsg_t *pMsg)
{
  switch ( pMsg->clusterID )
  {
    case Device_annce:
    {
      zclSampleSw_OnOffTestAddr = pMsg->srcAddr.addr.shortAddr;
      
      HalLcdWriteStringValue("Node:", pMsg->srcAddr.addr.shortAddr, 16, 3);
      HalLcdWriteString("On/Off Test...", 4);
      
      
      osal_start_timerEx(zclSampleSw_TaskID, SAMPLEAPP_ONOFF_TEST_EVT, SAMPLEAPP_ONOFF_TEST_PERIOD);
    }
    break;
    
    default:
    break;
  }
}

/**
 * @fn      zclSampleSw_OnOffTest
 *
 * @brief   Send On/Off Command to device
 */
static void zclSampleSw_OnOffTest(void)
{
    afAddrType_t destAddr;
    static uint8 txID = 0;
    static bool  on   = true;
    
    destAddr.endPoint = SAMPLESW_ENDPOINT;
    destAddr.addrMode = afAddr16Bit;
    destAddr.addr.shortAddr = zclSampleSw_OnOffTestAddr;
    
    if(on)
    {
      HalLcdWriteString("Command: ON", 4);
      
      zclGeneral_SendOnOff_CmdOn(SAMPLESW_ENDPOINT, 
                                 &destAddr,
                                 TRUE,
                                 txID++);
    }
    else
    {
      HalLcdWriteString("Command: OFF", 4);
      
      zclGeneral_SendOnOff_CmdOff(SAMPLESW_ENDPOINT, 
                                  &destAddr,
                                  TRUE,
                                  txID++);
    }
    
    on = !on;
}

#else  // Device!

/**
 * @fn      zclSampleSw_DeviceAnnce
 *
 * @brief   Broadcast "Annce"
 */
static void zclSampleSw_DeviceAnnce( void )
{
    ZDP_DeviceAnnce( NLME_GetShortAddr(), NLME_GetExtAddr(),
                     ZDO_Config_Node_Descriptor.CapabilityFlags, 0 );
}

/**
 * @fn      zclSampleSw_OnOffCB
 *
 * @brief   ZCL Command - On/Off
 */
static void zclSampleSw_OnOffCB( uint8 cmd )
{
    if(cmd == COMMAND_ON)
    {
      HalLcdWriteString("Set ON", 4);
      
      HalLedSet(HAL_LED_ALL, HAL_LED_MODE_ON);
    }
    else if(cmd == COMMAND_OFF)
    {
      HalLcdWriteString("Set OFF", 4);
      
      HalLedSet(HAL_LED_ALL, HAL_LED_MODE_OFF);
    }
}

#endif

/****************************************************************************
****************************************************************************/

/**
 * @fn      zclSampleSw_InitUart
 *
 * @brief   init. and open Uart
 */
static void zclSampleSw_InitUart(void)
{
  halUARTCfg_t uartConfig;

  /* UART Configuration */
  uartConfig.configured           = TRUE;
  uartConfig.baudRate             = HAL_UART_BR_115200;
  uartConfig.flowControl          = FALSE;
  uartConfig.flowControlThreshold = 0;
  uartConfig.rx.maxBufSize        = ZCLSAMPLESW_UART_BUF_LEN;
  uartConfig.tx.maxBufSize        = 0;
  uartConfig.idleTimeout          = 6;
  uartConfig.intEnable            = TRUE;
  uartConfig.callBackFunc         = zclSampleSw_UartCB;

  /* Start UART */
  HalUARTOpen(HAL_UART_PORT_0, &uartConfig);
}

/**
 * @fn      zclSampleSw_UartCB
 *
 * @brief   Uart Callback
 */
static void zclSampleSw_UartCB(uint8 port, uint8 event)
{
  uint16 destNwkAddr = 0;
  uint8 rxLen = Hal_UART_RxBufLen(HAL_UART_PORT_0);
  if(rxLen < 6) return;
  HalUARTRead(HAL_UART_PORT_0  ,  uartBuf , rxLen);

  uartBuf[1] = rxLen;  // Èç¹ûÏûÏ¢²»Ö§³Ö, Ô­·â²»¶¯·µ»Ø.
  switch(uartBuf[0]) {
  case 0xFC:
    uartBuf[2] = 0xFF;
    uartBuf[3] = 0xFF;
    // ²éÑ¯Éè±¸ÀàÐÍ: FC 06 0000 01 00
    if (1==uartBuf[4] && 0==uartBuf[5]) {
      uartBuf[6] = DEVICE_LOGICAL_TYPE;
      uartBuf[1] = 7;
    }
    // ²éÑ¯ PanId:   FC 06 0000 02 00
    else if (2==uartBuf[4] && 0==uartBuf[5]) {
      osal_memcpy(uartBuf+6, &_NIB.nwkPanId, sizeof(uint16));
      uartBuf[1] = 8;
    }
    // ²éÑ¯ÐÅµÀ:     FC 06 0000 03 00 
    else if (3==uartBuf[4] && 0==uartBuf[5]) {
      uartBuf[6] = _NIB.nwkLogicalChannel;
      uartBuf[1] = 7;
    }
    // ²éÑ¯¶ÌµØÖ·:   FC 06 0000 04 00 
    else if (4==uartBuf[4] && 0==uartBuf[5]) {
      osal_memcpy(uartBuf+6, &_NIB.nwkDevAddress, sizeof(uint16));
      uartBuf[1] = 8;
    }
    // ²éÑ¯mac:      FC 06 0000 05 00
    else if (5==uartBuf[4] && 0==uartBuf[5]) {
      osal_memcpy(uartBuf+6, aExtendedAddress, Z_EXTADDR_LEN);
      uartBuf[1] = 6 + Z_EXTADDR_LEN;
    }
    // ²éÑ¯¸¸Éè±¸¶ÌµØÖ·: FC 06 0000 06 00 
    else if (6==uartBuf[4] && 0==uartBuf[5]) {
      osal_memcpy(uartBuf+6, &_NIB.nwkCoordAddress, sizeof(uint16));
      uartBuf[1] = 8;
    }
    // ²éÑ¯¸¸Éè±¸mac:      FC 06 0000 07 00
    else if (7==uartBuf[4] && 0==uartBuf[5]) {
      osal_memcpy(uartBuf+6, &_NIB.nwkCoordExtAddress, Z_EXTADDR_LEN);
      uartBuf[1] = 6 + Z_EXTADDR_LEN;
    }
    // »Ö¸´³ö³§ÉèÖÃ:  FC 07 0000 08 01 03 
    else if (7==uartBuf[1] && 8==uartBuf[4] && 1==uartBuf[5] && 3==uartBuf[6]) {
      // ²Î¼û: https://e2echina.ti.com/question_answer/wireless_connectivity/zigbee/f/104/p/131709/368670
      // Õâ¸öÐèÒªºÃºÃ²âÊÔÏÂ
      bdb_resetLocalAction();
    }
    // ²éÑ¯Éè±¸×´Ì¬:       FC 06 0000 08 00 
    else if (8==uartBuf[4] && 0==uartBuf[5]) {
      uartBuf[6] = devState;
      uartBuf[1] = 7;
    }
    // reboot:        FC 06 0000 09 01
    else if (9==uartBuf[4] && 1==uartBuf[5]) {
      SystemReset();
    }
    // ²éÑ¯À©Õ¹panId:      FC 06 0000 0A 00
    else if (0x0A==uartBuf[4] && 0==uartBuf[5]) {
      osal_memcpy(uartBuf+6, &_NIB.extendedPANID, Z_EXTADDR_LEN);
      uartBuf[1] = 6 + Z_EXTADDR_LEN;
    }
    break;
  case 0xFE:
    // Í¸´«: 
    if (1==uartBuf[4]) {
      uartBuf[4] = 0x81;
      destNwkAddr = (uartBuf[3] << 8) | uartBuf[2]; 
      if ( 0xFF==uartBuf[3] && (0xFF==uartBuf[2] || 0xFD==uartBuf[2] || 0xFC==uartBuf[2])) 
        zclSampleSw_AF_Broadcast(destNwkAddr, CLUSTER_BROADCAST, rxLen, uartBuf);
      else zclSampleSw_AF_P2P(destNwkAddr, CLUSTER_P2P, rxLen, uartBuf);
    }
    // todo: ÓÐÐ©ÃüÁîÊÇÐ­µ÷Æ÷ÌØÓÐµÄ, ÐèÒª´¦Àí
    // ´ò¿ª×éÍø: FE 07 00 00 02 03 %02X
    else if (7==uartBuf[1] && 2==uartBuf[4] ) {
      uartBuf[4] = 0x82;
      NLME_PermitJoiningRequest(uartBuf[6]);
    }
    // Õâ¸öÃüÁîÓÐÐ©Ææ¹Ö. ×îºó02 ÊÇÊ²Ã´ÒâË¼£¬µ¹ÊýµÚ2¸ö ±ØÐëÊÇ0Âð?
    // ²éÑ¯Ð­µ÷Æ÷mac:  FE 07 00 00   00 00 02. ÌØÊâÖ®´¦. ÒÔºó²»ÔÙÓÃÕâ¸ö
    else if (7==uartBuf[1] && 0==uartBuf[4] && 2==uartBuf[6]) {
      // ÐèÒªÖªµÀÒÑÓÐ·µ»ØÊ²Ã´. È»ºóÏàÓ¦¹¹Ôì. 
      // ¿ÉÒÔ·µ»Ø devState, Ò²¿ÉÒÔ·µ»Ø ¸¸Éè±¸macµØÖ·
    }
    break;
  }
  
  HalUARTWrite(HAL_UART_PORT_0 ,  uartBuf , uartBuf[1]);
}

// ---------------- Í¸´«
/**
 * @fn      zclSampleSw_AF_P2P
 *
 * @brief   P2P
 *
 * @param   destNwkAddr - destination address
 *          cid - cluster id
 *          len - data length
 *          data - data
 *
 * @return  none
 */
static void zclSampleSw_AF_P2P(uint16 destNwkAddr,
                               uint16 cid,
                               uint8 len, uint8 *data)
{
  afAddrType_t dstAddr;
  static uint8 transferId = 0;
 
  /* Destination */
  dstAddr.addrMode = afAddr16Bit;
  dstAddr.addr.shortAddr = destNwkAddr;
  dstAddr.endPoint = SAMPLESW_ENDPOINT;
  
  /* Transfer id */
  transferId++;
  
  /* Send */
  AF_DataRequest( &dstAddr, &sampleSw_TestEp,
                  cid,
                  len, data,
                  &transferId,
                  AF_DISCV_ROUTE, AF_DEFAULT_RADIUS );
}

/**
 * @fn      zclSampleSw_AF_Broadcast
 *
 * @brief   Broadcase
 *
 * @param   cid - cluster id
 *          len - data length
 *          data - data
 *
 * @return  none
 */
static void zclSampleSw_AF_Broadcast(uint16 destNwkAddr, uint16 cid,
                                     uint8 len, uint8 *data)
{
  afAddrType_t dstAddr;
  static uint8 transferId = 0;
 
  /* Destination */
  dstAddr.addrMode = afAddrBroadcast;
  dstAddr.addr.shortAddr = destNwkAddr;
  dstAddr.endPoint = SAMPLESW_ENDPOINT;
  
  /* Transfer id */
  transferId++;
  
  /* Send */
  AF_DataRequest( &dstAddr, &sampleSw_TestEp,
                  cid,
                  len, data,
                  &transferId,
                  AF_TX_OPTIONS_NONE, AF_DEFAULT_RADIUS );
}

/**
 * @fn      zclSampleSw_AF_Groupcast
 *
 * @brief   Groupcast
 *
 * @param   groupId - group id
 *          cid - cluster id
 *          len - data length
 *          data - data
 *
 * @return  none
 */
static void zclSampleSw_AF_Groupcast(uint16 groupId,
                                     uint16 cid,
                                     uint8 len, uint8 *data)
{
  afAddrType_t dstAddr;
  static uint8 transferId = 0;
 
  /* Destination */
  dstAddr.addrMode = afAddrGroup;
  dstAddr.addr.shortAddr = groupId;
  dstAddr.endPoint = SAMPLESW_ENDPOINT;
  
  /* Transfer id */
  transferId++;
  
  /* Send */
  AF_DataRequest( &dstAddr, &sampleSw_TestEp,
                  cid,
                  len, data,
                  &transferId,
                  AF_TX_OPTIONS_NONE, AF_DEFAULT_RADIUS );
}

/**
 * @fn      zclSampleSw_AF_RxProc
 *
 * @brief   Process incoming message
 *
 * @param   MSGpkt - incoming message
 *
 * @return  none
 */
static void zclSampleSw_AF_RxProc(afIncomingMSGPacket_t *MSGpkt)
{
  uint8 last = MSGpkt->cmd.Data[MSGpkt->cmd.DataLength-1];
  switch( MSGpkt->clusterId )
  {
  case CLUSTER_P2P:
    HalLcdWriteStringValue( "P2P", last, 16, 3);
    HalLedSet(HAL_LED_1, HAL_LED_MODE_TOGGLE);
  break;
    
  case CLUSTER_BROADCAST:
    HalLcdWriteStringValue( "BROADCAST", last, 16, 4);
    HalLedSet(HAL_LED_2, HAL_LED_MODE_TOGGLE);
  break;
  
  case CLUSTER_GROUPCAST:
    HalLcdWriteStringValue( "GROUPCAST", last, 16, 4);
    HalLedSet(HAL_LED_3, HAL_LED_MODE_TOGGLE);
  break;
  
  default:
  break;
  }
}

