⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 zaps.c

📁 zigbee精简协议栈代码
💻 C
📖 第 1 页 / 共 2 页
字号:
#include "zAPS.h"

// Various APS frame header flags.
#define APS_FRAME_TYPE_MASK         0x03
#define APS_FRAME_DATA              0x00
#define APS_FRAME_COMMAND           0x01
#define APS_FRAME_ACKNOWLEDGE       0x02

#define APS_DELIVERY_MODE_MASK      (0x0C)
#define APS_DELIVERY_DIRECT         (0x00 << 2)
#define APS_DELIVERY_INDIRECT       (0x01 << 2)
#define APS_DELIVERY_BROADCAST      (0x02 << 2)
#define APS_DELIVERY_RESERVED       (0x03 << 2)

#define APS_INDIRECT_ADDRESS_MODE_MASK          0x10
#define APS_INDIRECT_ADDRESS_MODE_TO_COORD      0x10
#define APS_INDIRECT_ADDRESS_MODE_FROM_COORD    0x00
#define APS_CLEAR_INDIRECT_ADDRESS_MODE         0xEF

#define APS_DESTINATION_EP_PRESENT      0
#define APS_SOURCE_EP_PRESENT           1

#define APS_SECURITY_MASK           (0x20)
#define APS_SECURITY                (0x20)

#define APS_ACK_REQ_MASK            (0x40)
#define APS_ACK_REQ                 (0x40)

#define BROADCAST_EP                0xff

#define APSIsAcknowledgeFrame() ((apsCurrentFrame.frameCON.Val & APS_FRAME_TYPE_MASK) == APS_FRAME_ACKNOWLEDGE)

#define APSIsCommandFrame()     ((apsCurrentFrame.frameCON.Val & APS_FRAME_TYPE_MASK) == APS_FRAME_COMMAND)

#define APSIsDataFrame()        ((apsCurrentFrame.frameCON.Val & APS_FRAME_TYPE_MASK) == APS_FRAME_DATA)

#define APSAckIsRequested()     (apsCurrentFrame.frameCON.bits.ackRequested)

APS_HEADER      apsCurrentFrame;                // Current APS frame header

EP              epList[NUM_DEFINED_ENDPOINTS];  // Array of endpoints

EP              *pCurrentEP;                    // Pointer to current endpoint for faster access.

MESSAGE_INFO    currentMessageInfo;             // One generic message packet.

static TRANS_ID apsTransId;						// Ascending transaction id for APS frames.

//z内部函数
static BOOL APSGetHeader(void);

static void APSPutHeader(NODE_INFO *dest, BYTE frameCON, SHORT_ADDR dest_nwk_addr);

static BOOL SetEPDataReady(BYTE ep);

static void APSFlushEPs(void);

//内部函数(宏定义)
#define APSPut(a)                   NWKPut(a)

#define APSPutArray(v, n)           NWKPutArray(v, n)

// Calls MAC module to check ack status of frame associated with currnt ep.
#define APSIsConfirmed()            NWKFrameIsAcked(pCurrentEP->hFrame)

// Calls MAC module to check timeout status of frame associated with currnt ep.
#define APSIsTimedOut()             NWKFrameIsTimedOut(pCurrentEP->hFrame)

//Calls MAC module to remove frame entry for current ep.
#define APSRemoveFrame()            NWKFrameRemove(pCurrentEP->hFrame)

//send a frame of the current ep.
#define APSSend()               pCurrentEP->hFrame = NWKFlush()

//return the number of bytes remaining to be fetched for this EP.
#define APSGetDataLen()             (NWKPayloadLength)

/*********************************************************************
 * Function:        void APSInit(void)
 *
 * PreCondition:    None
 *
 * Input:           None
 *
 * Output:          None
 *
 * Side Effects:    None
 *
 * Overview:        Initializes APS variables.
 *
 * Note:            None
 ********************************************************************/
void APSInit(void)
{
    BYTE i;

    // Zero out ep list.
    memset((void*)epList, 0x00, (size_t)(sizeof(epList)));

    // Clear out all aps flags.
    apsCurrentFrame.Flags.Val = 0x00;

    // Disable all endpoints.
    for (i=0; i<NUM_DEFINED_ENDPOINTS; i++)
    {
        epList[i].flags.Val = 0x00;
		
    }

    NWKInit();
}

/*********************************************************************
 * Macro:           void APSDisable(void)
 *
 * PreCondition:    None
 *
 * Input:           None
 *
 * Output:          None
 *
 * Side Effects:    None
 *
 * Overview:        Forgets about all EP transactions and prepares for
 *                  stack disable mode.
 *
 * Note:            None
 ********************************************************************/
void APSDisable(void)
{
	APSFlushEPs(); 
	apsCurrentFrame.Flags.bits.bIsInProcess = FALSE;
	NWKDisable();
}	

/*********************************************************************
 * Function:        void APSFlushEPs(void)
 *
 * PreCondition:    None
 *
 * Input:           None
 *
 * Output:          None
 *
 * Side Effects:    None
 *
 * Overview:        Clears out all EPs from any outstanding transmit/
 *                  receive operations.
 *
 * Note:            None
 ********************************************************************/
static void APSFlushEPs(void)
{
    BYTE i;

    for ( i = 0; i < sizeof(epList)/sizeof(epList[0]); i++ )
    {
        epList[i].flags.bits.bIsDataReady = FALSE;
    }

    // Clear out all aps flags.
    apsCurrentFrame.Flags.Val = 0x00;
}

/*********************************************************************
 * Function:        BOOL APSTask(void)
 *
 *
 * PreCondition:    None
 *
 * Input:           None
 *
 * Output:          TRUE if there is no outstanding states to execute
 *                  FALSE otherwise
 *
 * Side Effects:    None
 *
 * Overview:        This is a main APS task. It must be called
 *                  as many times as possible to keep APS module
 *                  running.
 *
 * Note:            None
 ********************************************************************/
BOOL APSTask(void)
{
	 NWKTask();

     BOOL    destroyFrame = TRUE;
     BYTE    frameCon;
	
    if ( NWKIsGetReady() )
    {
        // Proceed only if we have not yet prcessed this frame.
        if ( apsCurrentFrame.Flags.bits.bIsInProcess )
            return TRUE;

        // APS layer only handles NWK DATA.  Make sure nothing else
        // got through to us.
        if ( !MACIsData() )	//NWKIsData()
        {
            return TRUE;
        }

        // Make sure that this is a valid APS frame.
        if ( APSGetHeader() )
        {
            // Handle APS command frames.
            if (APSIsCommandFrame())
            {
                // No APS commands are defined in this version
            }
            // Handle APS acknowledge frames.
            else if (APSIsAcknowledgeFrame())
            {
                // No APS acknoledge are defined in this version
            }
            // Handle APS data frames.
            else if (APSIsDataFrame())
            {
                if ( apsCurrentFrame.deliveryMode == APS_DELIVERY_DIRECT )
                {
					          
                    if ( SetEPDataReady(apsCurrentFrame.destEP) )
                    {
					   	ConsolePutString(" ep receive  direct data\n");
                        //du 当前版本不支持应用层ACK
                        if (APSAckIsRequested())
                        {
                        }
                        return TRUE;
                    }
                    else
                    {
                        ConsolePutString("Unknown EP data received.\r\n");
                    }
                }
				//du 当前版本不支持应用层广播
				//du 当前版本不支持应用层间接发送
            }
        }

        if (destroyFrame)
        { 
			APSDiscardRx();
        }
    }

    return TRUE;
}

/*********************************************************************
 * Function:        EP_HANDLE APSOpenEP(BYTE endPoint )
 *
 * PreCondition:    APSInit() is called
 *
 * Input:           endPoint    - endpoint number to enable
 *
 * Output:          handle to EP if there was one available EP
 *
 * Side Effects:    None
 *
 * Overview:        Enables the selected endpoint to receive data, and
 *                  returns a handle to the endpoint information.  Also
 *                  updates pCurrentEP to point to the newly enabled
 *                  endpoint.
 *
 * Note:            None
 ********************************************************************/
EP_HANDLE APSOpenEP(BYTE endPoint)
{
    BYTE                    i;
    //NODE_SIMPLE_DESCRIPTOR  simpleDescriptor;

    // Setup pointer to EP list.
    pCurrentEP = (EP*)&epList[0];

    // Find the matching endpoint information
    for ( i=0; i < NUM_DEFINED_ENDPOINTS; i++, pCurrentEP++)
    {
        //ProfileGetSimpleDesc( &simpleDescriptor, i );
        //if ( simpleDescriptor.Endpoint == endPoint )
		if (i == endPoint)
        {
            pCurrentEP->flags.Val = 0x00;
            pCurrentEP->flags.bits.bIsInUse = TRUE;

            return i;
        }
    }

    // No EP entry found.
    return EP_HANDLE_INVALID;
}

/*********************************************************************
 * Function:        TRANS_ID APSDE_DATA_request( MESSAGE_INFO *pMessageInfo )
 *
 * PreCondition:    APSInit() is called
 *
 * Input:           pMessageInfo    - pointer to a message information
 *                                      structure
 *
 * Output:          If successful, the transaction ID of the message is
 *                  returned.  Otherwise, TRANS_ID_INVALID is returned.
 *
 * Side Effects:    None
 *
 * Overview:        This function is used to send a KVP, MSG or other frame.
 *
 * Note:            None
 ********************************************************************/
TRANS_ID APSDE_DATA_request( MESSAGE_INFO *pMessageInfo )
{
    BYTE apsFlags;

    apsCurrentFrame.srcEP = pMessageInfo->srcEP;
    apsCurrentFrame.clusterID = pMessageInfo->clusterID;
    apsCurrentFrame.profileID.Val = pMessageInfo->profileID;
	//du 当前版本不支持应用层广播    
	if (pMessageInfo->destAddress.shortAddr.Val == 0xffff)
		return TRANS_ID_INVALID;

	//du 查找目标节点路由
	struct Route_Table_Entry *route = lookup_route(pMessageInfo->destAddress.shortAddr.Val );

	if (route == NULL || !route->flag.bit.is_route_valid)
	{
		//du 路由查找		
		send_rreq( pMessageInfo->destAddress.shortAddr.Val);
			

		

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -