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

📄 mac.h

📁 无线传感器星型网络的节点的基站程序,采用802.15.4协议
💻 H
📖 第 1 页 / 共 3 页
字号:
//----------------------------------------------------//
//-------Institute Of  Computing Technology-----------//
//------------Chinese Academic  Science---------------//
//-----中国科学院计算技术研究所先进测试技术实验室-----//
//----------------------------------------------------//

/**
 * @copyright:nbicc_lpp
 * @this is mac layer header
 * @author liulingyi      (lingyiliu@ict.ac.cn)
 *         zhouqiang      (zhouhongren@163.com)
 *              
 * @data:2005.11.07
 * @version:0.0.1
 * @updata:$
 *
 */


#ifndef _MAC_H_
#define _MAC_H_




#include "phy.h"
#include "generic.h"

//--------------------------------------------------//
//-----------帧控制字段设置的一些预定义常数---------//
//--------------------------------------------------//
#define aMaxPHYPacketSize 127
//帧类型字段bit[2:0]
#define MAC_FRAME_BEACON                (0x00)
#define MAC_FRAME_DATA                  (0x01)
#define MAC_FRAME_ACK                   (0x02)
#define MAC_FRAME_CMD                   (0x03)
//该帧的传送是否采用MAC层的安全策略bit[3]
#define MAC_SECURITY_YES            (0x08)
#define MAC_SECURITY_NO             (0x00)
//用在间接通信中,表示还有数据没传完,需要继续发data request命令帧bit[4]
#define MAC_FRAME_PENDING_YES       (0x10)
#define MAC_FRAME_PENDING_NO        (0x00)
//对当前帧的传送,是否需要收方的应答bit[5]
#define MAC_ACK_YES                 (0x20)
#define MAC_ACK_NO                  (0x00)
//是否为同一个PAN内的传送刘bit[6]
#define MAC_INTRA_PAN_YES           (0x40)
#define MAC_INTRA_PAN_NO            (0x00)
//源地址模式定义字段bit[11:10]
#define MAC_DST_NO_ADDR             (0x00)
#define MAC_DST_SHORT_ADDR          (0x08)
#define MAC_DST_LONG_ADDR           (0x0c)
#define MAC_DST_ADDR_RESERVED       (0x04)
//目标地址模式定义字段bit[15:14]
#define MAC_SRC_NO_ADDR             (0x00)
#define MAC_SRC_SHORT_ADDR          (0x80)
#define MAC_SRC_LONG_ADDR           (0xc0)
#define MAC_SRC_ADDR_RESERVED       (0x40)
//九种命令帧类型信息字段
#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)

#define ED_SCAN             (0x00)
#define ACTIVE_PASSIVE_SCAN     (0x01)
//#define PASSIVE_SCAN   (0x02)
#define ORPHAN_SCAN    (0x03)

//通过条件编译确定本设备是否为协调者
#ifdef I_AM_COORDINATOR
    #define IS_COORDINATOR      (0x01)      // 1 is yes, 0 if no.
    //#define IS_FFD                       (0x02)
#else
    #define IS_COORDINATOR      (0x00)
#endif
//通过条件编译确定是否采用MAC层的安全策略
#ifdef I_AM_SECURITY_CAPABLE
#define MAC_OPT_SECURITY            MAC_SECURITY_YES
#define MAC_OPT_ACL_SIZE         0x10
#else
#define MAC_OPT_SECURITY            MAC_SECURITY_NO
#define MAC_OPT_ACL_SIZE         0x00
#endif
//通过条件编译确定本设备是否为FFD设备
#ifdef I_AM_RFD
   #define IS_FFD              (0x00)      // 0x02 if yes, 0x00 if RFD
   #define IS_RFD              TRUE
#else
    #define IS_FFD              (0x02)  
   #define  IS_RFD              FALSE	
#endif

#ifdef  I_AM_FFD
   #define  IS_FFD              (0x02)
#else
   #define  IS_FFD              (0x00)   
#endif

//通过条件编译确定本设备的供电模式
#ifdef I_AM_MAINS_POWERED
   #define IS_MAINS_POWERED    (0x04)     
#else
#define IS_MAINS_POWERED    (0x00)
#endif 
   //通过条件编译确定本设备射频空闲时候,接收是否打开
   #ifdef MY_RX_IS_ALWAYS_ON_WHEN_IDLE
   #define IS_RX_ALWAYS_ON     (0x08)
   #else
   #define IS_RX_ALWAYS_ON     (0x00)
   #endif
   //通过条件编译确定本设备的MAC安全是否使能
   #ifdef I_AM_SECURITY_CAPABLE
   #define IS_SECURITY_ENABLED (0x40)
#else
   #define IS_SECURITY_ENABLED (0x00)
#endif
   //通过条件编译确定本设备是否希望协调设备给他分配短地址(pg125)
   #ifdef MAC_USE_SHORT_ADDR
   #define IS_SHORT_ADDRESS    (0x80)
#else
   #define IS_SHORT_ADDRESS    (0x00)
#endif
   //关联网络时候使用的本设备的有用信息
#define MAC_CAP_INFO      (IS_COORDINATOR        |     \  
                             IS_FFD                 |       \
                             IS_MAINS_POWERED       |       \
                             IS_RX_ALWAYS_ON        |       \
                             IS_SECURITY_ENABLED    |       \
IS_SHORT_ADDRESS);
//MAC层的枚举常数,反映MAC层各原语的状态值.(pg110)
#define SUCCESS                 0
#define BEACON_LOSS             0xE0
#define CHANNEL_ACCESS_FAILURE  0xE1
#define DENIED                  0xE2
#define DISABLE_TRX_FAILURE     0xE3
#define FAILED_SECURITY_CHECK   0xE4
#define FRAME_TOO_LONG          0xE5
#define INVALID_GTS             0xE6
#define INVALID_HANDLE          0xE7
#define INVALID_PARAMETER       0xE8
#define NO_ACK                  0xE9
#define NO_BEACON               0xEA
#define NO_DATA                 0xEB
#define NO_SHORT_ADDRESS        0xEC
#define OUT_OF_CAP              0xED
#define PAN_ID_CONFLICT         0xEE
#define REALIGNMENT             0xEF
#define TRANSACTION_EXPIRED     0xF0
#define TRANSACTION_OVERFLOW    0xF1
#define TX_ACTIVE               0xF2
#define UNAVAILABLE_KEY         0xF3
#define UNSUPPORTED_ATTRIBUTE   0xF4
#define RX_DEFERRED             0xF5

// 反映关联的状态值(pg126)
#define PAN_AT_CAPACITY         0x01
#define PAN_ACCESS_DENIED       0x02

//反映取消关联的原因状态值  (pg127)
#define COORD_WISH_TO_LEAVE     0x01
#define DIVICE_TO_LEAVE         0x02

//反映MAC层特征的一些常数
#define aBaseSlotDuration       20
#define aNumSuperframeSlots     8
#define aBaseSuperframeDuration (aBaseSlotDuration * aNumSuperframeSlots)
#define aMaxBE                  5
#define aMaxBeaconOverhead      75
#define aMaxBeaconPayloadLength (aMaxPHYPacketSize - aMaxBeaconOverhead)
#define aGTSDescPersistenceTime 4
#define aMaxFrameOverhead       25
#define aMaxFrameResponseTime   1220
#define aMaxFrameRetries        2
#define aMaxLostBeacons         4
#define aMaxMACFrameSize        (aMaxPHYPacketSize - aMaxFrameOverhead)
#define aMaxSIFSFrameSize       18
#define aMinCAPLength           440
#define aMinLIFSPeriod          40
#define aMinSIFSPeriod          12
#define aResponseWaitTime       (10 * aBaseSuperframeDuration)
//#define aUnitBackoffPeriod
//其他的涉及MAC层的常数定义
#define MAX_PENDING_LIST_SIZE   2



//反映安全策略的一些参数,包括MAC层的和PHY层的
// MAC ACL and security information bases
#ifdef I_AM_SECURITY_CAPABLE
#define MAC_SECURITY_NONE               0x00
#define MAC_SECURITY_AES_CTR            0x01
#define MAC_SECURITY_AES_CCM128         0x02
#define MAC_SECURITY_AES_CCM64          0x03
#define MAC_SECURITY_AES_CCM32          0x04
#define MAC_SECURITY_AES_CBC_MAC128     0x05
#define MAC_SECURITY_AES_CBC_MAC64      0x06
#define MAC_SECURITY_AES_CBC_MAC32      0x07
#define MAC_HIGHEST_SECURITY_MODE       MAC_SECURITY_AES_CBC_MAC32

#define MAC_UNSECURED_MODE              0x00
#define MAC_ACL_MODE                    0x01
#define MAC_SECURED_MODE                0x02

#define TX_OPTION_ACK_NEED        0x01;
#define TX_OPTION_GTS_TRANSMIT  0x02;
#define TX_OPTION_INDIRECT            0x04;
#define TX_OPTION_SECURITY           0x08;

// MAC Security flags definitions.
// Note that the bits are shifted compared to the actual security flags //defined by IEEE 802.15.4,
// please see the CC2420 datasheet for details.
#define MAC_CC2420_CTR_FLAGS            0x42
#define MAC_CC2420_CCM_FLAGS            0x09
#endif


//--------------------------------------------------//
//---下面为Zigbee中MAC层用到的专用的数据类型定义----//
//--------------------------------------------------//

//MAC层安全策略相关的类型定义
#if  ((MAC_OPT_SECURITY) || (MAC_OPT_ACL_SIZE>0))
typedef BYTE KEY[16];
typedef struct _SECURITY_MATERIAL {
    KEY pSymmetricKey;
    DWORD frameCounter;
    BYTE keySequenceCounter;
#if MAC_OPT_SEQUENTIAL_FRESHNESS
    DWORD externalFrameCounter;
    BYTE externalKeySequenceCounter;
#endif
} SECURITY_MATERIAL;

// Access Control List
typedef struct _ACL_ENTRY {
    QWORD extendedAddress;
    WORD shortAddress;
    WORD panId;
    BYTE securityMaterialLength;
    SECURITY_MATERIAL* pSecurityMaterial;
    BYTE securitySuite;
} ACL_ENTRY;
typedef ACL_ENTRY ACL_ENTRY_SET[MAC_OPT_ACL_SIZE];
   #endif
  
   //8字节长地址类型定义

//PAN地址类型定义
typedef SHORT_ADDR  PAN_ADDR;
//MAC层的PIB存储结构定义
typedef struct  _MAC_PIB{
    BYTE macAckWaitDuration;
    bool macAssociationPermit;
    bool macAutoRequest;
    bool macBattLifeExt;
    BYTE macBattLifeExtPeriods;
    BYTE* pMacBeaconPayload;
    BYTE macBeaconPayloadLength;
    uint8_t macBeaconOrder;
    DWORD macBeaconTxTime;
    BYTE macBSN;
    LONG_ADDR macCoordExtendedAddress;
    SHORT_ADDR macCoordShortAddress;
    BYTE macDSN;
    bool macGTSPermit;
    BYTE macMaxCsmaBackoffs;
    BYTE macMinBE;
    SHORT_ADDR macPANId;
    bool macPromiscuousMode;
    bool macRxOnWhenIdle;
    SHORT_ADDR macShortAddress;
    LONG_ADDR   macExtAddress;
    uint8_t macSuperframeOrder;
    WORD macTransactionPersistenceTime;

#if MAC_OPT_ACL_SIZE>0
    // ACL attributes
    ACL_ENTRY_SET* ppMacACLEntryDescriptorSet;
    BYTE macACLEntryDescriptorSetSize;
#endif

#if MAC_OPT_SECURITY
    // Security attributes
    bool macDefaultSecurity;
    BYTE macDefaultSecurityMaterialLength;
    SECURITY_MATERIAL* pMacDefaultSecurityMaterial;
    BYTE macDefaultSecuritySuite;
#endif

#if ((MAC_OPT_SECURITY) || (MAC_OPT_ACL_SIZE>0))
    BYTE macSecurityMode;
#endif
} MAC_PIB;

//给MAC PIB中的属性一个ID编号,从而用于访问
typedef enum {
    MAC_ACK_WAIT_DURATION = 0x40,
    MAC_ASSOCIATION_PERMIT,
    MAC_AUTO_REQUEST,
    MAC_BATT_LIFE_EXT,
    MAC_BATT_LIFE_EXT_PERIODS,
    MAC_BEACON_PAYLOAD,
    MAC_BEACON_PAYLOAD_LENGTH,
    MAC_BEACON_ORDER,
    MAC_BEACON_TX_TIME,
    MAC_BSN,
    MAC_COORD_EXTENDED_ADDRESS,
    MAC_COORD_SHORT_ADDRESS,
    MAC_DSN,
    MAC_GTS_PERMIT,
    MAC_MAX_CSMA_BACKOFFS,
    MAC_MIN_BE,
    MAC_PAN_ID,
    MAC_PROMISCUOUS_MODE,
    MAC_RX_ON_WHEN_IDLE,
    MAC_SHORT_ADDRESS,
    MAC_SUPERFRAME_ORDER,
    MAC_TRANSACTION_PERSISTENCE_TIME,
    MAC_ACL_ENTRY_DESCRIPTOR_SET = 0x70,

⌨️ 快捷键说明

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