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

📄 mac_802_15_4.cpp

📁 csma协议
💻 CPP
📖 第 1 页 / 共 5 页
字号:
// Copyright (c) 2001-2008, Scalable Network Technologies, Inc.  All Rights Reserved.//                          6701 Center Drive West//                          Suite 520//                          Los Angeles, CA 90045//                          sales@scalable-networks.com//// This source code is licensed, not sold, and is subject to a written// license agreement.  Among other things, no portion of this source// code may be copied, transmitted, disclosed, displayed, distributed,// translated, used as the basis for a derivative work, or used, in// whole or in part, for any program or purpose other than its intended// use in compliance with the license agreement as part of the QualNet// software.  This source code and certain of the algorithms contained// within it are confidential trade secrets of Scalable Network// Technologies, Inc. and may not be used as the basis for any other// software, hardware, product or service.#include <stdlib.h>#include <stdio.h>#include <string.h>#include "api.h"#include "partition.h"#include "network_ip.h"#include "list.h"#include "mac.h"#include "mac_802_15_4.h"#define DEBUG 0#define DEBUG_TIMER 0//Forward declarationsstaticvoid Mac802_15_4mlme_scan_request(        Node* node,        int interfaceIndex,        UInt8 ScanType,        UInt32 ScanChannels,        UInt8 ScanDuration,        BOOL frUpper,        PhyStatusType status);staticvoid Mac802_15_4mlme_start_request(        Node* node,        int interfaceIndex,        UInt16 PANId,        UInt8 LogicalChannel,        UInt8 BeaconOrder,        UInt8 SuperframeOrder,        BOOL PANCoordinator,        BOOL BatteryLifeExtension,        BOOL CoordRealignment,        BOOL SecurityEnable,        BOOL frUpper,        PhyStatusType status);staticvoid Mac802_15_4mlme_associate_request(        Node* node,        int interfaceIndex,        UInt8 LogicalChannel,        UInt8 CoordAddrMode,        UInt16 CoordPANId,        MACADDR CoordAddress,        UInt8 CapabilityInformation,        BOOL SecurityEnable,        BOOL frUpper,        PhyStatusType status,        M802_15_4_enum mStatus);staticvoid Mac802_15_4mlme_associate_response(        Node* node,        int interfaceIndex,        MACADDR DeviceAddress,        UInt16 AssocShortAddress,        M802_15_4_enum Status,        BOOL SecurityEnable,        BOOL frUpper,        PhyStatusType status);staticvoid Mac802_15_4mlme_orphan_response(        Node* node,        int interfaceIndex,        MACADDR OrphanAddress,        UInt16 ShortAddress,        BOOL AssociatedMember,        BOOL SecurityEnable,        BOOL frUpper,        PhyStatusType status);staticvoid Mac802_15_4mlme_disassociate_request(        Node* node,        int interfaceIndex,        MACADDR DeviceAddress,        UInt8 DisassociateReason,        BOOL SecurityEnable,        BOOL frUpper,        PhyStatusType status);staticvoid Mac802_15_4mlme_sync_request(        Node* node,        int interfaceIndex,        UInt8 LogicalChannel,        BOOL TrackBeacon,        BOOL frUpper,        PhyStatusType status);staticvoid Mac802_15_4mlme_poll_request(        Node* node,        int interfaceIndex,        UInt8 CoordAddrMode,        UInt16 CoordPANId,        MACADDR CoordAddress,        BOOL SecurityEnable,        BOOL autoRequest,        BOOL firstTime,        PhyStatusType status);staticvoid Mac802_15_4mlme_reset_request(        Node* node,        int interfaceIndex,        BOOL SetDefaultPIB,        BOOL frUpper,        PhyStatusType status);staticvoid Mac802_15_4mlme_rx_enable_request(        Node* node,        int interfaceIndex,        BOOL DeferPermit,        UInt32 RxOnTime,        UInt32 RxOnDuration,        BOOL frUpper,        PhyStatusType status);staticvoid Mac802_15_4mcps_data_request(        Node* node,        int interfaceIndex,        UInt8 SrcAddrMode,        UInt16 SrcPANId,        MACADDR SrcAddr,        UInt8 DstAddrMode,        UInt16 DstPANId,        MACADDR DstAddr,        UInt8 msduLength,        Message* msdu,        UInt8 msduHandle,        UInt8 TxOptions,        BOOL frUpper,        PhyStatusType status,        M802_15_4_enum mStatus);staticvoid Mac802_15_4CsmacaResume(Node* node, int interfaceIndex);staticvoid Mac802_15_4TxBcnCmdDataHandler(Node* node, int interfaceIndex);// /**// FUNCTION   :: MAC_VariableHWAddressToTwoByteMacAddress// LAYER      :: MAC// PURPOSE    :: Retrieve  IP address from.MacHWAddress of type//               HW_NODE_ID// PARAMETERS ::// + node : Node* : Pointer to Node structure// + macAddr : MacHWAddress* : Pointer to hardware address structure// RETURN     :: NodeAddress : Ipv4 Address// **/staticMACADDR MAC_VariableHWAddressToTwoByteMacAddress (        Node* node,        MacHWAddress* macAddr){    if(macAddr->hwLength != MAC_NODEID_LINKADDRESS_LENGTH ||       macAddr->hwType == HW_TYPE_UNKNOWN)    {        return 0;    }    if (MAC_IsBroadcastHWAddress(macAddr))    {        return (MACADDR)ANY_DEST;    }    else    {        MACADDR nodeAddress;        memcpy(&nodeAddress, macAddr->byte, MAC_NODEID_LINKADDRESS_LENGTH);        return nodeAddress;    }}// /**// FUNCTION   :: ConvertVariableHWAddressToMacAddr// LAYER      :: MAC// PURPOSE    :: Convert Variable Hardware address to ZigBee MACADDR// PARAMETERS ::// + node : Node* : Pointer to Node structure// + macHWAddr : MacHWAddress* : Pointer to hardware address structure// + macAddr : MACADDR* : Pointer to ZigBee MAC address structure// RETURN     :: Bool :// **/staticBOOL ConvertVariableHWAddressToMacAddr(                    Node *node,                    MacHWAddress* macHWAddr,                    MACADDR* macAddr){    if(macHWAddr->hwLength == MAC_NODEID_LINKADDRESS_LENGTH){        memcpy(macAddr, macHWAddr->byte, macHWAddr->hwLength);        return TRUE;    }    else        return FALSE;}// /**// FUNCTION   :: ConvertMacAddrToVariableHWAddress// LAYER      :: MAC// PURPOSE    :: Convert ZigBee Mac addtess to Variable Hardware address// PARAMETERS ::// + node : Node* : Pointer to Node structure// + macHWAddr : MacHWAddress* : Pointer to hardware address structure// + macAddr : MACADDR* : Pointer to mac 802 address structure// RETURN     :: Bool :// **/staticvoid ConvertMacAddrToVariableHWAddress (                        Node *node,                        MacHWAddress* macHWAddr,                        MACADDR* macAddr){    macHWAddr->hwLength = MAC_NODEID_LINKADDRESS_LENGTH;    macHWAddr->hwType = HW_NODE_ID;    (*macHWAddr).byte = (unsigned char*) MEM_malloc(      sizeof(unsigned char)*MAC_NODEID_LINKADDRESS_LENGTH);    memcpy(macHWAddr->byte, macAddr, MAC_NODEID_LINKADDRESS_LENGTH);}// /**// FUNCTION   :: MAC802_15_4IPv4AddressToHWAddress// LAYER      :: MAC// PURPOSE    :: Convert IPv4 addtess to Hardware address// PARAMETERS ::// + node : Node* : Pointer to Node structure// + ipv4Address : NodeAddress : IPv4 address// + macAddr : MacHWAddress* : Pointer to Hardware address structure// RETURN     :: void :// **/void MAC802_15_4IPv4AddressToHWAddress(                        Node *node,                        NodeAddress ipv4Address,                        MacHWAddress* macAddr){    MACADDR nodeId = (MACADDR)                        MAPPING_GetNodeIdFromInterfaceAddress(                            node,                            ipv4Address);    ConvertMacAddrToVariableHWAddress(node, macAddr, &nodeId);}// /**// FUNCTION   :: Phy802_15_4PLME_CCA_request// LAYER      :: MAC// PURPOSE    :: To initiate Channel clear access request// PARAMETERS ::// + node           : Node*             : Pointer to node// + interfaceIndex : int               : Interface index// RETURN     :: None// **/void Phy802_15_4PLME_CCA_request(Node* node, int interfaceIndex){    BOOL isChannelFree;    PhyStatusType status;    Phy802_15_4PLMECCArequest(node, interfaceIndex, &isChannelFree);    if(isChannelFree == TRUE)    {        status = PHY_IDLE;    }    else    {        status = PHY_BUSY_TX;    }    Mac802_15_4PLME_CCA_confirm(node, interfaceIndex, status);}staticM802_15_4PIB MPIB ={    M802_15_4_ACKWAITDURATION,    M802_15_4_ASSOCIATIONPERMIT,    M802_15_4_AUTOREQUEST,    M802_15_4_BATTLIFEEXT,    M802_15_4_BATTLIFEEXTPERIODS,    M802_15_4_BEACONPAYLOAD,    M802_15_4_BEACONPAYLOADLENGTH,    M802_15_4_BEACONORDER,    M802_15_4_BEACONTXTIME,    M802_15_4_BSN,    M802_15_4_COORDEXTENDEDADDRESS,    M802_15_4_COORDSHORTADDRESS,    M802_15_4_DSN,    M802_15_4_GTSPERMIT,    M802_15_4_MAXCSMABACKOFFS,    M802_15_4_MINBE,    M802_15_4_PANID,    M802_15_4_PROMISCUOUSMODE,    M802_15_4_RXONWHENIDLE,    M802_15_4_SHORTADDRESS,    M802_15_4_SUPERFRAMEORDER,    M802_15_4_TRANSACTIONPERSISTENCETIME,    M802_15_4_ACLENTRYDESCRIPTORSET,    M802_15_4_ACLENTRYDESCRIPTORSETSIZE,    M802_15_4_DEFAULTSECURITY,    M802_15_4_ACLDEFAULTSECURITYMATERIALLENGTH,    M802_15_4_DEFAULTSECURITYMATERIAL,    M802_15_4_DEFAULTSECURITYSUITE,    M802_15_4_SECURITYMODE};// /**// FUNCTION   :: Mac802_15_4SetTimer// LAYER      :: MAC// PURPOSE    :: Set a timer message. If a non-NULL message pointer is//               passed in, this function will just send out that msg.//               If NULL message is passed in, it will create a new//               message and send it out. In either case, a pointer to//               the message is returned, in case the caller wants to//               cancel the message in the future.// PARAMETERS ::// + node      : Node*                  : Pointer to node// + mac802_15_4: MacData802_15_4*      : Pointer to MAC802.15.4 structure// + timerType : Mac802_15_4TimerType   : Type of the timer// + delay     : clocktype              : Delay of this timer// + msg       : Message*               : If non-NULL, use this message// + infoVal   : unsigned int           : Additional info if needed// RETURN     :: Message*               : Pointer to the timer message// **/Message* Mac802_15_4SetTimer(        Node* node,        MacData802_15_4* mac802_15_4,        M802_15_4TimerType timerType,        clocktype delay,        Message* msg){    Message* timerMsg = NULL;    M802_15_4Timer* timerInfo;    if (msg != NULL)    {        timerMsg = msg;    }    else    {        // allocate the timer message and send out        timerMsg = MESSAGE_Alloc(node,                                 MAC_LAYER,                                 MAC_PROTOCOL_802_15_4,                                 MSG_MAC_TimerExpired);        MESSAGE_SetInstanceId(timerMsg, (short)                mac802_15_4->myMacData->interfaceIndex);        MESSAGE_InfoAlloc(node, timerMsg, sizeof(M802_15_4Timer));        timerInfo = (M802_15_4Timer*) MESSAGE_ReturnInfo(timerMsg);        timerInfo->timerType = timerType;    }    MESSAGE_Send(node, timerMsg, delay);    return (timerMsg);}// /**// FUNCTION   :: Mac802_15_4ConstructPayload// LAYER      :: Mac// PURPOSE    :: Add MHR to a message// PARAMETERS ::// + node           : Node*     : Node receiving call// + interfaceIndex : int       : Interface index// + msg            : Message*  : Message pointer// + frmType        : UInt8     : Frame type (beacon/data/ack/MAC cmd)// + SuperSpec      : UInt16    : Superframe specification// + GTSFields      : M802_15_4GTSFields: GTS fields - not used currently// + PendAddrFields : M802_15_4PendAddrField : List of devices for whom data//                                      is pending// + CmdType        : UInt8     : Command type for MAC cmd frame// RETURN  :: None// **/staticvoid Mac802_15_4ConstructPayload(        Node* node,        int interfaceIndex,        Message** msg,        UInt8 frmType,        UInt16 SuperSpec,        M802_15_4GTSFields* GTSFields,

⌨️ 快捷键说明

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