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

📄 zmac.h

📁 zigbee精简协议栈代码
💻 H
📖 第 1 页 / 共 3 页
字号:
#ifndef _ZMAC_H_
#define _ZMAC_H_

#include "zigbee.h"
#include "zPHY.h"

#define HFRAME_INVALID          (HFRAME)(MAX_MAC_FRAME_STATUS)

// Frame Control flags.
#define MAC_FRAME_TYPE_MASK         (0x03)
#define MAC_FRAME_BEACON            (0x00)
#define MAC_FRAME_DATA              (0x01)
#define MAC_FRAME_ACK               (0x02)
#define MAC_FRAME_CMD               (0x03)

#define MAC_CMD_ASSOCIATE_REQ       (0x01)
#define MAC_CMD_ASSOCIATE_RPLY      (0x02)
#define MAC_CMD_DISASSOCIATE_NOTICE (0x03)
#define MAC_CMD_DATA_REQ            (0x04)
#define MAC_CMD_PAN_ID_CONFLICT     (0x05)
#define MAC_CMD_ORPHAN_NOTICE       (0x06)
#define MAC_CMD_BEACON_REQ          (0x07)
#define MAC_CMD_COORD_REALIGNMENT   (0x08)
#define MAC_CMD_GTS_REQ             (0x09)

// Macros to determine current frame type
#define MACIsData()                 (macCurrentFrame.type == MAC_FRAME_DATA)
#define MACIsCommand()              (macCurrentFrame.type == MAC_FRAME_CMD)
#define MACIsBeacon()               (macCurrentFrame.type == MAC_FRAME_BEACON)
#define MACIsAck()                  (macCurrentFrame.type == MAC_FRAME_ACK)

#define MACIsAssocRequest()         (macCurrentFrame.cmd == MAC_CMD_ASSOCIATE_REQ)
#define MACIsDisassocNotice()       (macCurrentFrame.cmd == MAC_CMD_DISASSOCIATE_NOTICE)
#define MACIsBeaconRequest()        (macCurrentFrame.cmd == MAC_CMD_BEACON_REQ)
#define MACIsDataReq()              (macCurrentFrame.cmd == MAC_CMD_DATA_REQ)
#define MACIsCoordRealign()         (macCurrentFrame.cmd == MAC_CMD_COORD_REALIGNMENT)
#define MACIsOrphanNotice()         (macCurrentFrame.cmd == MAC_CMD_ORPHAN_NOTICE)
#define MACIsAssocResponse()        (macCurrentFrame.cmd == MAC_CMD_ASSOCIATE_RPLY)

/*
 *******************************************
 * Frame Type LSB fields - START
 *******************************************
 */
#define MAC_FRAME_INVALID           (0x07)

#define MAC_SECURITY_YES            (0x08)
#define MAC_SECURITY_NO             (0x00)

#define MAC_FRAME_PENDING_YES       (0x10)
#define MAC_FRAME_PENDING_NO        (0x00)

#define MAC_ACK_YES                 (0x20)
#define MAC_ACK_NO                  (0x00)

#define MAC_INTRA_PAN_YES           (0x40)
#define MAC_INTRA_PAN_NO            (0x00)

#define MAC_BEACON_YES              (0x40)
#define MAC_BEACON_NO               (0x00)

#if defined(I_AM_SECURITY_CAPABLE)
    #define MAC_SECURITY            MAC_SECURITY_YES
#else
    #define MAC_SECURITY            MAC_SECURITY_NO
#endif

/*
 *******************************************
 * Frame Type LSB fields - END
 *******************************************
 */


/*
 *******************************************
 * Frame Type MSB fields - START
 *******************************************
 */
#define MAC_DST_NO_ADDR             (0x00)
#define MAC_DST_SHORT_ADDR          (0x08)
#define MAC_DST_LONG_ADDR           (0x0c)
#define MAC_DST_ADDR_RESERVED       (0x04)

#define MAC_SRC_NO_ADDR             (0x00)
#define MAC_SRC_SHORT_ADDR          (0x80)
#define MAC_SRC_LONG_ADDR           (0xc0)
#define MAC_SRC_ADDR_RESERVED       (0x40)

/*
 *******************************************
 * Frame Type MSB fields - END
 *******************************************
 */

// Constants used to build a an Association Request (CMD frame 0x01)
#if defined(I_AM_COORDINATOR)
    #define IS_COORDINATOR      (0x01)      // 1 is yes, 0 if no.
#else
    #define IS_COORDINATOR      (0x00)
#endif

#if defined(I_AM_FFD)
    #define IS_FFD              (0x02)      // 0x02 if yes, 0x00 if RFD
#else
    #define IS_FFD              (0x00)
#endif

#if defined(I_AM_SECURITY_CAPABLE)
    #define IS_SECURITY_ENABLED (0x40)
#else
    #define IS_SECURITY_ENABLED (0x00)
#endif



#define SUPERFRAME_SPEC_LSB         (0xFF)
#define SUPERFRAME_SPEC_MSB         (0xC0)
#define GTS_FIELD_VAL               (0x00)

typedef BYTE HFRAME;

// Association State Machine states.
typedef enum _SM_ASSOCIATION
{
    SM_SEND_ASSOCIATE_REQ,
    SM_WAIT_FOR_TX_COMPLETE,
    SM_WAIT_FOR_ACK,
    SM_SEND_DATA_REQ,
    SM_DATA_REQ_ACK_WAIT,
    SM_WAIT_FOR_ASSOC_RESP,
    SM_SEND_DISASSOCIATE_REQ,
    SM_SEND_ORPHAN_NOTICE,
    SM_WAIT_FOR_COORD_ALIGNMENT
} SM_ASSOCIATION;

typedef enum _MAC_ASSOCIATE_STATUS
{
    MAC_ASSOCIATE_SUCCESS   = 0,
    MAC_ASSOCIATE_PAN_FULL  = 0x01,
    MAC_ASSOCIATE_DENIED    = 0x02
} MAC_ASSOCIATE_STATUS;

typedef enum _MAC_DISASSOCIATION_REASON_CODES
{
    MAC_COORDINATOR_FORCED_LEAVE = 0x01,
    MAC_DEVICE_LEAVE = 0x02
} MAC_DISASSOCIATION_REASON_CODES;

// A 802.15.4 Node address information.
typedef struct _NODE_INFO
{
    BYTE            addrMode;
    LONG_ADDR       longAddr;
    SHORT_ADDR      shortAddr;
    PAN_ADDR        panID;
} NODE_INFO;

extern NODE_INFO macInfo;

extern NODE_INFO macCoordInfo;

extern NODE_INFO macDestInfo;

// MAC module state
typedef union _MAC_STATE
{
    struct
    {
        unsigned int bIsAssociated      : 1;        // '1' if this node is associated
        unsigned int bIsTxBusy          : 1;        // '1' if transmission is in progress
        unsigned int bPermitAssociation : 1;        // '1' if other nodes are allowed to associat
        unsigned int bIsEnabled         : 1;        // '1' if PHY is enabled.
#if defined(I_AM_COORDINATOR)
        unsigned int bUseTxRAM : 1;                 // '1' if current tx packet should be put in stage
                                                    // buffer.
#elif defined(I_AM_END_DEVICE)
        unsigned int bIsPollDone        : 1;        // '1 if a frame of type data has been received
                                                    // after calling MACPoll()
#endif
    } bits;
    BYTE Val;
} MAC_STATE;

extern MAC_STATE macState;

// MAC Frame header information in decoded manner.
typedef struct _MAC_HEADER
{
    NODE_INFO dst;
    NODE_INFO src;
    BYTE type;
    BYTE cmd;
    BYTE macDSN;
    BYTE frameLength;
    union
    {
        struct
        {
            unsigned int : 3;                   // Frame type field.  Copied to 'type' member for faster access.
            unsigned int SecurityEnabled : 1;
            unsigned int FramePending : 1;
            unsigned int AckRequest : 1;
            unsigned int IntraPAN : 1;
            unsigned int : 1;                   // Bit 7 is reserved
        } bits;

        BYTE Val;
    } frameCONLSB;
    BYTE_VAL frameCONMSB;

    union
    {
        struct
        {
            unsigned int AltPANCoord : 1;
            unsigned int DeviceType: 1;
            unsigned int PowerSource:1;
            unsigned int ReceiveOnWhenIdle:1;
            unsigned int :2;
            unsigned int SecurityCapability:1;
            unsigned int AllocateAddress:1;
        } bits;
        BYTE Val;
    } capInfo;

    union
    {
        struct
        {
            unsigned int bIsGetReady:1;
            unsigned int bToBeQueued:1;
        } bits;
        BYTE Val;
    } Flags;

} MAC_HEADER;

extern MAC_HEADER macCurrentFrame;

// PAN descriptor in decoded manner - this is used by end device to determine whom to associate
typedef struct _PAN_DESC
{
    SHORT_ADDR  CoordPANId;
    union
    {
        SHORT_ADDR shortAddr;
        LONG_ADDR longAddr;
    } CoordAddress;

    BYTE        LogicalChannel;
    WORD_VAL    SuperFrameSpec;
    BYTE        LinkQuality;
    DWORD_VAL   TimeStamp;
    BYTE        ACLEntry;

    union
    {
        struct
        {
            unsigned int CoordAddrMode:1;     // 0 = 16-bit, 1 = 64-bit
            unsigned int GTSPermit:1;
            unsigned int SecurityInUse:1;
            unsigned int SecurityFailure:1;
        } bits;
        BYTE Val;
    } Flags;

} PAN_DESC;

extern PAN_DESC PANDesc;

extern BYTE PANDescCount;

//发送缓冲队列
typedef struct _MAC_FRAME_STATUS
{
    union
    {
        struct
        {
            unsigned int bIsInUse : 1;
            unsigned int bIsConfirmed:1;
            unsigned int bIsTimedOut:1;
        } bits;
        BYTE Val;
    } Flags;

    BYTE macDSN;
    TICK lastTick;
    BYTE retryCount;

} MAC_FRAME_STATUS;

// Exact length of frame stauts queue would strictly depend on how long is your ack timeout and
// the rate of new frame transmissions.
#define MAX_MAC_FRAME_STATUS    (8)

extern MAC_FRAME_STATUS macFrameStatusQ[MAX_MAC_FRAME_STATUS];
/////////////////////////////////////////////////////////////////////////////////////////////////

/*********************************************************************
 * Function:        void MACInit(void)
 *
 * PreCondition:    macInfo.longAddr is set as required.
 *
 * Input:           None
 *
 * Output:          None
 *
 * Side Effects:    None
 *
 * Overview:        Initializes data variables used by MAC module.
 *
 * Note:            None
 ********************************************************************/
void MACInit(void);

BOOL MACIsIdle(); 

/*********************************************************************
 * Function:        void MACEnable(void)
 *
 * PreCondition:    None
 *
 * Input:           None
 *
 * Output:          None
 *
 * Side Effects:    None
 *
 * Overview:        Enables PHY regulator and registers.
 *
 * Note:            None
 ********************************************************************/
void MACEnable(void);

/*********************************************************************
 * Macro:           void MACDisable(void)
 *
 * PreCondition:    None
 *
 * Input:           None
 *
 * Output:          None
 *
 * Side Effects:    None
 *
 * Overview:        Disbales PHY and marks MAC module as disabled.
 *
 * Note:            None
 ********************************************************************/
#define MACDisable()        {macState.bits.bIsEnabled = FALSE; macCurrentFrame.Flags.bits.bIsGetReady = FALSE; PHYDisable();}

/*********************************************************************
 * Function:        void MACISR(void)
 *
 * PreCondition:    MACInit() is previously called.
 *
 * Input:           None
 *
 * Output:          None
 *
 * Side Effects:    None
 *
 * Overview:        Determines if a frame has completed transmission
 *                  or not.
 *
 * Note:            None
 ********************************************************************/
void MACISR(void);

/*********************************************************************
 * Function:        BOOL MACTask(void)
 *
 * PreCondition:    MACInit() is called
 *
 * Input:           None
 *
 * Output:          TRUE        - If Processing is complete
 *                  FALSE       - If further processing is required
 *
 * Side Effects:    None
 *
 * Overview:        If previous frame is processed, it checks
 *                  for new RX frame.
 *                  Fetches the header and determiens if it is valid
 *                  Automatically sends ACK if remote node has
 *                  requested it.
 *                  Also performs some other automatic processing
 *                  based on the frame type.
 *                  If there is no frame in buffer, it goes through
 *                  DSN queue and calculates timeout for unack'ed
 *                  TX frames.
 *
 * Note:            This function must be called as many times as
 *                  possible to keep handling MAC frames.
 ********************************************************************/
BOOL MACTask(void);

// Callback to application to notify of a frame transmission.
extern void AppMACFrameTransmitted(void);

// Callback to application to notify of a frame reception
extern void AppMACFrameReceived(void);

// Callback to application to notify of an ack timeout
extern void AppMACFrameTimeOutOccurred(void);

/*********************************************************************
 * Macro:           void MACSetFirstChannel(void)
 *
 * PreCondition:    None
 *
 * Input:           None
 *
 * Output:          None
 *
 * Side Effects:    None
 *
 * Overview:        Selects very first channel in current frequency
 *                  band (i.e. channel 11 for 2.4GHz band)
 *
 * Note:            This macro is designed to allow application to
 *                  browse through all channels irrespective of
 *                  frequency band in use.
 ********************************************************************/
#define MACSetFirstChannel()    PHYSetFirstChannel()

/*********************************************************************
 * Function:        BOOL MACSetNextChannel(void)
 *
 * PreCondition:    None
 *
 * Input:           None
 *
 * Output:          TRUE if a valid next channel was selected
 *                  FALES if end of available channels was reached.
 *
 * Side Effects:    None
 *
 * Overview:        Sequences to next available channel specific
 *                  to current frequency band.
 *                  When end of channels is reached, FALSE is returned
 *                  and application call SetFirstChannel() to start

⌨️ 快捷键说明

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