123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768 |
- #include "OSAL.h"
- #include "OSAL_Clock.h"
- #include "ZDApp.h"
- #include "ZDObject.h"
- #include "AddrMgr.h"
- #include "se.h"
- #include "rangeext.h"
- #include "zcl_general.h"
- #include "zcl_key_establish.h"
- #include "onboard.h"
- #include "hal_lcd.h"
- #include "hal_led.h"
- #include "hal_key.h"
- #define zcl_MandatoryReportableAttribute( a ) ( a == NULL )
- #define rangeExtNwkState devState
- static uint8 rangeExtTaskID;
- static uint8 rangeExtTransID;
- static afAddrType_t ESPAddr;
- #if SECURE
- static uint8 linkKeyStatus;
- #endif
- static void rangeext_HandleKeys( uint8 shift, uint8 keys );
- #if SECURE
- static uint8 rangeext_KeyEstablish_ReturnLinkKey( uint16 shortAddr );
- #endif
- static void rangeext_ProcessIdentifyTimeChange( void );
- static uint8 rangeext_ValidateAttrDataCB( zclAttrRec_t *pAttr, zclWriteRec_t *pAttrInfo );
- static void rangeext_BasicResetCB( void );
- static void rangeext_IdentifyCB( zclIdentify_t *pCmd );
- static void rangeext_IdentifyQueryRspCB( zclIdentifyQueryRsp_t *pRsp );
- static void rangeext_AlarmCB( zclAlarm_t *pAlarm );
- #ifdef SE_UK_EXT
- static void rangeext_GetEventLogCB( uint8 srcEP, afAddrType_t *srcAddr,
- zclGetEventLog_t *pEventLog, uint8 seqNum );
- static void rangeext_PublishEventLogCB( afAddrType_t *srcAddr, zclPublishEventLog_t *pEventLog );
- #endif
- static void rangeext_ProcessZDOMsgs( zdoIncomingMsg_t *pMsg );
- static void rangeext_ProcessZCLMsg( zclIncomingMsg_t *msg );
- #if defined ( ZCL_READ )
- static uint8 rangeext_ProcessInReadRspCmd( zclIncomingMsg_t *pInMsg );
- #endif
- #if defined ( ZCL_WRITE )
- static uint8 rangeext_ProcessInWriteRspCmd( zclIncomingMsg_t *pInMsg );
- #endif
- static uint8 rangeext_ProcessInDefaultRspCmd( zclIncomingMsg_t *pInMsg );
- #if defined ( ZCL_DISCOVER )
- static uint8 rangeext_ProcessInDiscRspCmd( zclIncomingMsg_t *pInMsg );
- #endif
- static zclGeneral_AppCallbacks_t rangeext_GenCmdCallbacks =
- {
- rangeext_BasicResetCB,
- rangeext_IdentifyCB,
- rangeext_IdentifyQueryRspCB,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- rangeext_AlarmCB,
- #ifdef SE_UK_EXT
- rangeext_GetEventLogCB,
- rangeext_PublishEventLogCB,
- #endif
- NULL,
- NULL,
- };
- void rangeext_Init( uint8 task_id )
- {
- rangeExtTaskID = task_id;
-
- ESPAddr.addrMode = (afAddrMode_t)Addr16Bit;
- ESPAddr.endPoint = RANGEEXT_ENDPOINT;
- ESPAddr.addr.shortAddr = 0;
-
- zclSE_Init( &rangeExtSimpleDesc );
-
- zclGeneral_RegisterCmdCallbacks( RANGEEXT_ENDPOINT, &rangeext_GenCmdCallbacks );
-
- zcl_registerAttrList( RANGEEXT_ENDPOINT, RANGEEXT_MAX_ATTRIBUTES, rangeExtAttrs );
-
- zcl_registerClusterOptionList( RANGEEXT_ENDPOINT, RANGEEXT_MAX_OPTIONS, rangeExtOptions );
-
- zcl_registerValidateAttrData( rangeext_ValidateAttrDataCB );
-
- zcl_registerForMsg( rangeExtTaskID );
-
- RegisterForKeys( rangeExtTaskID );
-
- ZDO_RegisterForZDOMsg(task_id, Match_Desc_rsp);
- }
- uint16 rangeext_event_loop( uint8 task_id, uint16 events )
- {
- afIncomingMSGPacket_t *MSGpkt;
- if ( events & SYS_EVENT_MSG )
- {
- while ( (MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( rangeExtTaskID )) )
- {
- switch ( MSGpkt->hdr.event )
- {
- case ZDO_CB_MSG:
- rangeext_ProcessZDOMsgs( (zdoIncomingMsg_t *)MSGpkt );
- break;
- case ZCL_INCOMING_MSG:
-
- rangeext_ProcessZCLMsg( (zclIncomingMsg_t *)MSGpkt );
- break;
- case KEY_CHANGE:
- rangeext_HandleKeys( ((keyChange_t *)MSGpkt)->state, ((keyChange_t *)MSGpkt)->keys );
- break;
- case ZDO_STATE_CHANGE:
- if (DEV_ROUTER == (devStates_t)(MSGpkt->hdr.status))
- {
- #if SECURE
- {
-
- linkKeyStatus = rangeext_KeyEstablish_ReturnLinkKey(ESPAddr.addr.shortAddr);
- if (linkKeyStatus != ZSuccess)
- {
- cId_t cbkeCluster = ZCL_CLUSTER_ID_GEN_KEY_ESTABLISHMENT;
- zAddrType_t dstAddr;
-
- dstAddr.addrMode = AddrBroadcast;
- dstAddr.addr.shortAddr = NWK_BROADCAST_SHORTADDR;
- ZDP_MatchDescReq( &dstAddr, NWK_BROADCAST_SHORTADDR, ZCL_SE_PROFILE_ID,
- 1, &cbkeCluster, 0, NULL, FALSE );
- }
- }
- #endif
- }
- break;
- #if defined( ZCL_KEY_ESTABLISH )
- case ZCL_KEY_ESTABLISH_IND:
- if ((MSGpkt->hdr.status) == TermKeyStatus_Success)
- {
- ESPAddr.endPoint = RANGEEXT_ENDPOINT;
- }
- break;
- #endif
- default:
- break;
- }
-
- osal_msg_deallocate( (uint8 *)MSGpkt );
- }
-
- return (events ^ SYS_EVENT_MSG);
- }
-
- if ( events & RANGEEXT_KEY_ESTABLISHMENT_REQUEST_EVT )
- {
- zclGeneral_KeyEstablish_InitiateKeyEstablishment(rangeExtTaskID, &ESPAddr, rangeExtTransID);
- return ( events ^ RANGEEXT_KEY_ESTABLISHMENT_REQUEST_EVT );
- }
-
- if ( events & RANGEEXT_IDENTIFY_TIMEOUT_EVT )
- {
- if ( rangeExtIdentifyTime > 0 )
- {
- rangeExtIdentifyTime--;
- }
- rangeext_ProcessIdentifyTimeChange();
- return ( events ^ RANGEEXT_IDENTIFY_TIMEOUT_EVT );
- }
-
- return 0;
- }
- static void rangeext_ProcessZDOMsgs( zdoIncomingMsg_t *pMsg )
- {
- if (pMsg->clusterID == Match_Desc_rsp)
- {
- ZDO_ActiveEndpointRsp_t *pRsp = ZDO_ParseEPListRsp( pMsg );
- if (pRsp)
- {
- if (pRsp->cnt)
- {
-
- ESPAddr.endPoint = pRsp->epList[0];
- ESPAddr.addr.shortAddr = pMsg->srcAddr.addr.shortAddr;
-
- osal_set_event( rangeExtTaskID, RANGEEXT_KEY_ESTABLISHMENT_REQUEST_EVT);
- }
- osal_mem_free(pRsp);
- }
- }
- }
- static void rangeext_ProcessIdentifyTimeChange( void )
- {
- if ( rangeExtIdentifyTime > 0 )
- {
- osal_start_timerEx( rangeExtTaskID, RANGEEXT_IDENTIFY_TIMEOUT_EVT, 1000 );
- HalLedBlink ( HAL_LED_4, 0xFF, HAL_LED_DEFAULT_DUTY_CYCLE, HAL_LED_DEFAULT_FLASH_TIME );
- }
- else
- {
- HalLedSet ( HAL_LED_4, HAL_LED_MODE_OFF );
- osal_stop_timerEx( rangeExtTaskID, RANGEEXT_IDENTIFY_TIMEOUT_EVT );
- }
- }
- #if SECURE
- static uint8 rangeext_KeyEstablish_ReturnLinkKey( uint16 shortAddr )
- {
- uint8 status = ZFailure;
- AddrMgrEntry_t entry;
-
- entry.user = ADDRMGR_USER_DEFAULT;
- entry.nwkAddr = shortAddr;
- if ( AddrMgrEntryLookupNwk( &entry ) )
- {
-
- if ( APSME_IsLinkKeyValid( entry.extAddr ) == TRUE )
- {
- status = ZSuccess;
- }
- }
- else
- {
-
- status = ZInvalidParameter;
- }
- return status;
- }
- #endif
- static void rangeext_HandleKeys( uint8 shift, uint8 keys )
- {
-
- if ( shift )
- {
- if ( keys & HAL_KEY_SW_1 )
- {
- }
- if ( keys & HAL_KEY_SW_2 )
- {
- }
- if ( keys & HAL_KEY_SW_3 )
- {
- }
- if ( keys & HAL_KEY_SW_4 )
- {
- }
- }
- else
- {
- if ( keys & HAL_KEY_SW_1 )
- {
- ZDOInitDevice(0);
- }
- if ( keys & HAL_KEY_SW_2 )
- {
- }
- if ( keys & HAL_KEY_SW_3 )
- {
- }
- if ( keys & HAL_KEY_SW_4 )
- {
- }
- }
- }
- static uint8 rangeext_ValidateAttrDataCB( zclAttrRec_t *pAttr, zclWriteRec_t *pAttrInfo )
- {
- uint8 valid = TRUE;
- switch ( pAttrInfo->dataType )
- {
- case ZCL_DATATYPE_BOOLEAN:
- if ( ( *(pAttrInfo->attrData) != 0 ) && ( *(pAttrInfo->attrData) != 1 ) )
- valid = FALSE;
- break;
- default:
- break;
- }
- return ( valid );
- }
- static void rangeext_BasicResetCB( void )
- {
-
- }
- static void rangeext_IdentifyCB( zclIdentify_t *pCmd )
- {
- rangeExtIdentifyTime = pCmd->identifyTime;
- rangeext_ProcessIdentifyTimeChange();
- }
- static void rangeext_IdentifyQueryRspCB( zclIdentifyQueryRsp_t *pRsp )
- {
-
- }
- static void rangeext_AlarmCB( zclAlarm_t *pAlarm )
- {
-
- }
- #ifdef SE_UK_EXT
- static void rangeext_GetEventLogCB( uint8 srcEP, afAddrType_t *srcAddr,
- zclGetEventLog_t *pEventLog, uint8 seqNum )
- {
-
-
-
-
-
-
- }
- static void rangeext_PublishEventLogCB( afAddrType_t *srcAddr, zclPublishEventLog_t *pEventLog )
- {
-
- }
- #endif
- static void rangeext_ProcessZCLMsg( zclIncomingMsg_t *pInMsg )
- {
- switch ( pInMsg->zclHdr.commandID )
- {
- #if defined ( ZCL_READ )
- case ZCL_CMD_READ_RSP:
- rangeext_ProcessInReadRspCmd( pInMsg );
- break;
- #endif
- #if defined ( ZCL_WRITE )
- case ZCL_CMD_WRITE_RSP:
- rangeext_ProcessInWriteRspCmd( pInMsg );
- break;
- #endif
- case ZCL_CMD_DEFAULT_RSP:
- rangeext_ProcessInDefaultRspCmd( pInMsg );
- break;
- #if defined ( ZCL_DISCOVER )
- case ZCL_CMD_DISCOVER_RSP:
- rangeext_ProcessInDiscRspCmd( pInMsg );
- break;
- #endif
- default:
- break;
- }
- if ( pInMsg->attrCmd != NULL )
- {
-
- osal_mem_free( pInMsg->attrCmd );
- pInMsg->attrCmd = NULL;
- }
- }
- #if defined ( ZCL_READ )
- static uint8 rangeext_ProcessInReadRspCmd( zclIncomingMsg_t *pInMsg )
- {
- zclReadRspCmd_t *readRspCmd;
- uint8 i;
- readRspCmd = (zclReadRspCmd_t *)pInMsg->attrCmd;
- for (i = 0; i < readRspCmd->numAttr; i++)
- {
-
-
-
- }
- return TRUE;
- }
- #endif
- #if defined ( ZCL_WRITE )
- static uint8 rangeext_ProcessInWriteRspCmd( zclIncomingMsg_t *pInMsg )
- {
- zclWriteRspCmd_t *writeRspCmd;
- uint8 i;
- writeRspCmd = (zclWriteRspCmd_t *)pInMsg->attrCmd;
- for (i = 0; i < writeRspCmd->numAttr; i++)
- {
-
-
- }
- return TRUE;
- }
- #endif
- static uint8 rangeext_ProcessInDefaultRspCmd( zclIncomingMsg_t *pInMsg )
- {
-
-
- return TRUE;
- }
- #if defined ( ZCL_DISCOVER )
- static uint8 rangeext_ProcessInDiscRspCmd( zclIncomingMsg_t *pInMsg )
- {
- zclDiscoverRspCmd_t *discoverRspCmd;
- uint8 i;
- discoverRspCmd = (zclDiscoverRspCmd_t *)pInMsg->attrCmd;
- for ( i = 0; i < discoverRspCmd->numAttr; i++ )
- {
-
- }
- return TRUE;
- }
- #endif
|