123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707 |
- #include "ZComDef.h"
- #include "OSAL.h"
- #include "zcl.h"
- #include "zcl_general.h"
- #include "zcl_pi.h"
- #if defined ( INTER_PAN )
- #include "stub_aps.h"
- #endif
- typedef struct zclPICBRec
- {
- struct zclPICBRec *next;
- uint8 endpoint;
- zclPI_AppCallbacks_t *CBs;
- } zclPICBRec_t;
- static zclPICBRec_t *zclPICBs = (zclPICBRec_t *)NULL;
- static uint8 zclPIPluginRegisted = FALSE;
- static ZStatus_t zclPI_HdlIncoming( zclIncoming_t *pInHdlrMsg );
- static ZStatus_t zclPI_HdlInSpecificCommands( zclIncoming_t *pInMsg );
- static zclPI_AppCallbacks_t *zclPI_FindCallbacks( uint8 endpoint );
- static ZStatus_t zclPI_ProcessIn_GenericTunneServer( zclIncoming_t *pInMsg,
- zclPI_AppCallbacks_t *pCBs );
- static ZStatus_t zclPI_ProcessIn_GenericTunneClient( zclIncoming_t *pInMsg,
- zclPI_AppCallbacks_t *pCBs );
- static ZStatus_t zclPI_ProcessIn_BACnetTunnelCmds( zclIncoming_t *pInMsg,
- zclPI_AppCallbacks_t *pCBs );
- static ZStatus_t zclPI_ProcessIn_11073TunnelCmds( zclIncoming_t *pInMsg,
- zclPI_AppCallbacks_t *pCBs );
- ZStatus_t zclPI_RegisterCmdCallbacks( uint8 endpoint, zclPI_AppCallbacks_t *callbacks )
- {
- zclPICBRec_t *pNewItem;
- zclPICBRec_t *pLoop;
-
- if ( !zclPIPluginRegisted )
- {
- zcl_registerPlugin( ZCL_CLUSTER_ID_PI_GENERIC_TUNNEL,
- ZCL_CLUSTER_ID_PI_11073_PROTOCOL_TUNNEL,
- zclPI_HdlIncoming );
- zclPIPluginRegisted = TRUE;
- }
-
- pNewItem = osal_mem_alloc( sizeof( zclPICBRec_t ) );
- if ( pNewItem == NULL )
- return (ZMemError);
- pNewItem->next = (zclPICBRec_t *)NULL;
- pNewItem->endpoint = endpoint;
- pNewItem->CBs = callbacks;
-
- if ( zclPICBs == NULL )
- {
- zclPICBs = pNewItem;
- }
- else
- {
-
- pLoop = zclPICBs;
- while ( pLoop->next != NULL )
- pLoop = pLoop->next;
-
- pLoop->next = pNewItem;
- }
- return ( ZSuccess );
- }
- ZStatus_t zclPI_Send_MatchProtocolAddrCmd( uint8 srcEP, afAddrType_t *dstAddr,
- uint8 len, uint8 *protocolAddr,
- uint8 disableDefaultRsp, uint8 seqNum )
- {
- uint8 *buf;
- ZStatus_t stat;
- buf = osal_mem_alloc( len+1 );
- if ( buf )
- {
- buf[0] = len;
- osal_memcpy( &(buf[1]), protocolAddr, len );
- stat = zcl_SendCommand( srcEP, dstAddr, ZCL_CLUSTER_ID_PI_GENERIC_TUNNEL,
- COMMAND_PI_GENERIC_TUNNEL_MATCH_PROTOCOL_ADDR, TRUE,
- ZCL_FRAME_CLIENT_SERVER_DIR, disableDefaultRsp, 0, seqNum,
- (len+1), buf );
- osal_mem_free( buf );
- }
- else
- {
- stat = ZMemError;
- }
-
- return ( stat );
- }
- ZStatus_t zclPI_Send_MatchProtocolAddrRsp( uint8 srcEP, afAddrType_t *dstAddr,
- uint8 *ieeeAddr, uint8 len, uint8 *protocolAddr,
- uint8 disableDefaultRsp, uint8 seqNum )
- {
- uint8 *buf;
- uint8 msgLen = Z_EXTADDR_LEN + 1 + len;
- ZStatus_t stat;
- buf = osal_mem_alloc( msgLen );
- if ( buf )
- {
-
- osal_cpyExtAddr( buf, ieeeAddr );
-
- buf[8] = len;
- osal_memcpy( &(buf[9]), protocolAddr, len );
- stat = zcl_SendCommand( srcEP, dstAddr, ZCL_CLUSTER_ID_PI_GENERIC_TUNNEL,
- COMMAND_PI_GENERIC_TUNNEL_MATCH_PROTOCOL_ADDR_RSP, TRUE,
- ZCL_FRAME_SERVER_CLIENT_DIR, disableDefaultRsp, 0, seqNum,
- msgLen, buf );
- osal_mem_free( buf );
- }
- else
- {
- stat = ZMemError;
- }
-
- return ( stat );
- }
- ZStatus_t zclPI_Send_AdvertiseProtocolAddrCmd( uint8 srcEP, afAddrType_t *dstAddr,
- uint8 len, uint8 *protocolAddr,
- uint8 disableDefaultRsp, uint8 seqNum )
- {
- uint8 *buf;
- ZStatus_t stat;
- buf = osal_mem_alloc( len+1 );
- if ( buf )
- {
- buf[0] = len;
- osal_memcpy( &(buf[1]), protocolAddr, len );
- stat = zcl_SendCommand( srcEP, dstAddr, ZCL_CLUSTER_ID_PI_GENERIC_TUNNEL,
- COMMAND_PI_GENERIC_TUNNEL_ADVERTISE_PROTOCOL_ADDR, TRUE,
- ZCL_FRAME_SERVER_CLIENT_DIR, disableDefaultRsp, 0, seqNum,
- (len+1), buf );
- osal_mem_free( buf );
- }
- else
- {
- stat = ZMemError;
- }
-
- return ( stat );
- }
- ZStatus_t zclPI_Send_11073TransferAPDUCmd( uint8 srcEP, afAddrType_t *dstAddr,
- uint16 len, uint8 *apdu, uint8 seqNum )
- {
- uint8 *buf;
- ZStatus_t stat;
- buf = osal_mem_alloc( len+2 );
- if ( buf )
- {
- buf[0] = LO_UINT16( len );
- buf[1] = HI_UINT16( len );
- osal_memcpy( &(buf[2]), apdu, len );
-
-
- stat = zcl_SendCommand( srcEP, dstAddr, ZCL_CLUSTER_ID_PI_11073_PROTOCOL_TUNNEL,
- COMMAND_PI_11073_TUNNEL_TRANSFER_APDU, TRUE,
- ZCL_FRAME_CLIENT_SERVER_DIR, TRUE, 0, seqNum, (len+2), buf );
- osal_mem_free( buf );
- }
- else
- {
- stat = ZMemError;
- }
-
- return ( stat );
- }
- ZStatus_t zclPI_Send_11073ConnectReq( uint8 srcEP, afAddrType_t *dstAddr,
- uint8 connectCtrl, uint16 idleTimeout,
- uint8 *managerAddr, uint8 managerEP,
- uint8 disableDefaultRsp, uint8 seqNum )
- {
- uint8 *buf;
- uint8 msgLen = 1 + 2 + Z_EXTADDR_LEN + 1;
- ZStatus_t stat;
- buf = osal_mem_alloc( msgLen );
- if ( buf )
- {
- buf[0] = connectCtrl;
- buf[1] = LO_UINT16( idleTimeout );
- buf[2] = HI_UINT16( idleTimeout );
- osal_memcpy( &(buf[3]), managerAddr, Z_EXTADDR_LEN );
- buf[11] = managerEP;
- stat = zcl_SendCommand( srcEP, dstAddr, ZCL_CLUSTER_ID_PI_11073_PROTOCOL_TUNNEL,
- COMMAND_PI_11073_TUNNEL_CONNECT_REQ, TRUE,
- ZCL_FRAME_CLIENT_SERVER_DIR, disableDefaultRsp, 0, seqNum,
- msgLen, buf );
- osal_mem_free( buf );
- }
- else
- {
- stat = ZMemError;
- }
-
- return ( stat );
- }
- static zclPI_AppCallbacks_t *zclPI_FindCallbacks( uint8 endpoint )
- {
- zclPICBRec_t *pCBs;
-
- pCBs = zclPICBs;
- while ( pCBs )
- {
- if ( pCBs->endpoint == endpoint )
- return ( pCBs->CBs );
- }
- return ( (zclPI_AppCallbacks_t *)NULL );
- }
- static ZStatus_t zclPI_HdlIncoming( zclIncoming_t *pInMsg )
- {
- ZStatus_t stat = ZSuccess;
- #if defined ( INTER_PAN )
- if ( StubAPS_InterPan( pInMsg->msg->srcAddr.panId, pInMsg->msg->srcAddr.endPoint ) )
- return ( stat );
- #endif
- if ( zcl_ClusterCmd( pInMsg->hdr.fc.type ) )
- {
-
- if ( pInMsg->hdr.fc.manuSpecific == 0 )
- {
- stat = zclPI_HdlInSpecificCommands( pInMsg );
- }
- else
- {
-
- stat = ZFailure;
- }
- }
- else
- {
-
- stat = ZFailure;
- }
- return ( stat );
- }
- static ZStatus_t zclPI_HdlInSpecificCommands( zclIncoming_t *pInMsg )
- {
- ZStatus_t stat;
- zclPI_AppCallbacks_t *pCBs;
-
- pCBs = zclPI_FindCallbacks( pInMsg->msg->endPoint );
- if ( pCBs == NULL )
- return ( ZFailure );
- switch ( pInMsg->msg->clusterId )
- {
- case ZCL_CLUSTER_ID_PI_GENERIC_TUNNEL:
- if ( zcl_ServerCmd( pInMsg->hdr.fc.direction ) )
- stat = zclPI_ProcessIn_GenericTunneServer( pInMsg, pCBs );
- else
- stat = zclPI_ProcessIn_GenericTunneClient( pInMsg, pCBs );
- break;
- case ZCL_CLUSTER_ID_PI_BACNET_PROTOCOL_TUNNEL:
- stat = zclPI_ProcessIn_BACnetTunnelCmds( pInMsg, pCBs );
- break;
- case ZCL_CLUSTER_ID_PI_11073_PROTOCOL_TUNNEL:
- stat = zclPI_ProcessIn_11073TunnelCmds( pInMsg, pCBs );
- break;
- default:
- stat = ZFailure;
- break;
- }
- return ( stat );
- }
- static ZStatus_t zclPI_ProcessIn_GenericTunneServer( zclIncoming_t *pInMsg,
- zclPI_AppCallbacks_t *pCBs )
- {
- if ( pInMsg->hdr.commandID != COMMAND_PI_GENERIC_TUNNEL_MATCH_PROTOCOL_ADDR )
- return (ZFailure);
- if ( pCBs->pfnPI_MatchProtocolAddr )
- {
- zclPIMatchProtocolAddr_t cmd;
-
- cmd.srcAddr = &(pInMsg->msg->srcAddr);
- cmd.seqNum = pInMsg->hdr.transSeqNum;
- cmd.len = pInMsg->pData[0];
- cmd.protocolAddr = &(pInMsg->pData[1]);
-
- pCBs->pfnPI_MatchProtocolAddr( &cmd );
- }
- return ( ZSuccess );
- }
- static ZStatus_t zclPI_ProcessIn_GenericTunneClient( zclIncoming_t *pInMsg,
- zclPI_AppCallbacks_t *pCBs )
- {
- ZStatus_t stat = ZSuccess;
- switch ( pInMsg->hdr.commandID )
- {
- case COMMAND_PI_GENERIC_TUNNEL_MATCH_PROTOCOL_ADDR_RSP:
- if ( pCBs->pfnPI_MatchProtocolAddrRsp )
- {
- zclPIMatchProtocolAddrRsp_t cmd;
-
- cmd.srcAddr = &(pInMsg->msg->srcAddr);
- cmd.ieeeAddr = pInMsg->pData;
- cmd.len = pInMsg->pData[8];
- cmd.protocolAddr = &(pInMsg->pData[9]);
- pCBs->pfnPI_MatchProtocolAddrRsp( &cmd );
- }
- break;
- case COMMAND_PI_GENERIC_TUNNEL_ADVERTISE_PROTOCOL_ADDR:
- if ( pCBs->pfnPI_AdvertiseProtocolAddr )
- {
- zclPIAdvertiseProtocolAddr_t cmd;
-
- cmd.srcAddr = &(pInMsg->msg->srcAddr);
- cmd.len = pInMsg->pData[0];
- cmd.protocolAddr = &(pInMsg->pData[1]);
-
- pCBs->pfnPI_AdvertiseProtocolAddr( &cmd );
- }
- break;
- default:
-
- stat = ZFailure;
- break;
- }
- return ( stat );
- }
- static ZStatus_t zclPI_ProcessIn_BACnetTunnelCmds( zclIncoming_t *pInMsg,
- zclPI_AppCallbacks_t *pCBs )
- {
- if ( pInMsg->hdr.commandID != COMMAND_PI_BACNET_TUNNEL_TRANSFER_NPDU )
- return (ZFailure);
- if ( pCBs->pfnPI_BACnetTransferNPDU )
- {
- zclBACnetTransferNPDU_t cmd;
-
- cmd.srcAddr = &(pInMsg->msg->srcAddr);
- cmd.len = pInMsg->pDataLen;
- cmd.npdu = pInMsg->pData;
-
- pCBs->pfnPI_BACnetTransferNPDU( &cmd );
- }
-
- return ( ZSuccess );
- }
- static ZStatus_t zclPI_ProcessIn_11073TunnelCmds( zclIncoming_t *pInMsg,
- zclPI_AppCallbacks_t *pCBs )
- {
- ZStatus_t stat = ZSuccess;
- switch ( pInMsg->hdr.commandID )
- {
- case COMMAND_PI_11073_TUNNEL_TRANSFER_APDU:
- if ( pCBs->pfnPI_11073TransferAPDU )
- {
- zcl11073TransferAPDU_t cmd;
-
- cmd.srcAddr = &(pInMsg->msg->srcAddr);
- cmd.len = BUILD_UINT16( pInMsg->pData[0], pInMsg->pData[1] );
- cmd.apdu = &(pInMsg->pData[2]);
- pCBs->pfnPI_11073TransferAPDU( &cmd );
- }
- break;
- case COMMAND_PI_11073_TUNNEL_CONNECT_REQ:
- if ( pCBs->pfnPI_11073ConnectReq )
- {
- zcl11073ConnectReq_t cmd;
-
- cmd.srcAddr = &(pInMsg->msg->srcAddr);
- cmd.seqNum = pInMsg->hdr.transSeqNum;
- cmd.connectCtrl = pInMsg->pData[0];
- cmd.idleTimeout = BUILD_UINT16( pInMsg->pData[1], pInMsg->pData[2] );
- cmd.managerAddr = &(pInMsg->pData[3]);
- cmd.managerEP = pInMsg->pData[11];
- pCBs->pfnPI_11073ConnectReq( &cmd );
- }
- break;
- case COMMAND_PI_11073_TUNNEL_DISCONNECT_REQ:
- if ( pCBs->pfnPI_11073DisconnectReq )
- {
- zcl11073DisconnectReq_t cmd;
-
- cmd.srcAddr = &(pInMsg->msg->srcAddr);
- cmd.seqNum = pInMsg->hdr.transSeqNum;
- cmd.managerAddr = pInMsg->pData;
-
- pCBs->pfnPI_11073DisconnectReq( &cmd );
- }
- break;
- case COMMAND_PI_11073_TUNNEL_CONNECT_STATUS_NOTI:
- if ( pCBs->pfnPI_11073ConnectStatusNoti )
- {
- zcl11073ConnectStatusNoti_t cmd;
-
- cmd.srcAddr = &(pInMsg->msg->srcAddr);
- cmd.connectStatus = pInMsg->pData[0];
-
- pCBs->pfnPI_11073ConnectStatusNoti( &cmd );
- }
- break;
- default:
-
- stat = ZFailure;
- break;
- }
- return ( stat );
- }
|