📄 sscs_802_15_4.cpp
字号:
// 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 "sscs_802_15_4.h"#define DEBUG 0// /**// FUNCTION :: Sscs802_15_4StatusName// LAYER :: SSCS// PURPOSE :: Returns Status string for a given status// PARAMETERS ::// + status : M802_15_4_enum: Status of request// RETURN :: char* : Status string// **/staticchar* Sscs802_15_4StatusName(M802_15_4_enum status){ switch(status) { case M802_15_4_SUCCESS: return "SUCCESS"; case M802_15_4_PAN_AT_CAPACITY: return "PAN_at_capacity"; case M802_15_4_PAN_ACCESS_DENIED: return "PAN_access_denied"; case M802_15_4_BEACON_LOSS: return "BEACON_LOSS"; case M802_15_4_CHANNEL_ACCESS_FAILURE: return "CHANNEL_ACCESS_FAILURE"; case M802_15_4_DENIED: return "DENIED"; case M802_15_4_DISABLE_TRX_FAILURE: return "DISABLE_TRX_FAILURE"; case M802_15_4_FAILED_SECURITY_CHECK: return "FAILED_SECURITY_CHECK"; case M802_15_4_FRAME_TOO_LONG: return "FRAME_TOO_LONG"; case M802_15_4_INVALID_GTS: return "INVALID_GTS"; case M802_15_4_INVALID_HANDLE: return "INVALID_HANDLE"; case M802_15_4_INVALID_PARAMETER: return "INVALID_PARAMETER"; case M802_15_4_NO_ACK: return "NO_ACK"; case M802_15_4_NO_BEACON: return "NO_BEACON"; case M802_15_4_NO_DATA: return "NO_DATA"; case M802_15_4_NO_SHORT_ADDRESS: return "NO_SHORT_ADDRESS"; case M802_15_4_OUT_OF_CAP: return "OUT_OF_CAP"; case M802_15_4_PAN_ID_CONFLICT: return "PAN_ID_CONFLICT"; case M802_15_4_REALIGNMENT: return "REALIGNMENT"; case M802_15_4_TRANSACTION_EXPIRED: return "TRANSACTION_EXPIRED"; case M802_15_4_TRANSACTION_OVERFLOW: return "TRANSACTION_OVERFLOW"; case M802_15_4_TX_ACTIVE: return "TX_ACTIVE"; case M802_15_4_UNAVAILABLE_KEY: return "UNAVAILABLE_KEY"; case M802_15_4_UNSUPPORTED_ATTRIBUTE: return "UNSUPPORTED_ATTRIBUTE"; case M802_15_4_UNDEFINED: default: return "UNDEFINED"; }}// /**// FUNCTION :: Sscs802_15_4SetTimer// LAYER :: MAC// PURPOSE :: Set a timer message.// PARAMETERS ::// + node : Node* : Pointer to node// + interfaceIndex ; int : Interface index of device// + timerType : S802_15_4TimerType: Type of the timer// + delay : clocktype : Delay of this timer// RETURN :: None// **/staticvoid Sscs802_15_4SetTimer(Node* node, int interfaceIndex, S802_15_4TimerType timerType, clocktype delay){ Message* timerMsg = NULL; S802_15_4Timer* timerInfo; // allocate the timer message and send out timerMsg = MESSAGE_Alloc(node, MAC_LAYER, MAC_PROTOCOL_802_15_4, MSG_SSCS_802_15_4_TimerExpired); MESSAGE_SetInstanceId(timerMsg, (short)interfaceIndex); MESSAGE_InfoAlloc(node, timerMsg, sizeof(S802_15_4Timer)); timerInfo = (S802_15_4Timer*) MESSAGE_ReturnInfo(timerMsg); timerInfo->timerType = timerType; MESSAGE_Send(node, timerMsg, delay);}// /**// FUNCTION :: Sscs802_15_4StartPANCoord// LAYER :: SSCS// PURPOSE :: Start a PAN and PAN co-ordinator// PARAMETERS ::// + node : Node* : Node receiving call// + status : M802_15_4_enum: Status of request// RETURN :: None// **/staticvoid Sscs802_15_4StartPANCoord( Node* node, int interfaceIndex, M802_15_4_enum status){ MacData802_15_4* mac; SscsData802_15_4* sscs802_15_4; M802_15_4PIB t_mpib; PHY_PIB t_ppib; int i; //int BO; mac = (MacData802_15_4*) node->macData[interfaceIndex]->macVar; sscs802_15_4 = (SscsData802_15_4*)mac->sscs; switch(sscs802_15_4->state) { case S802_15_4NULL: { if(DEBUG) { printf("%lld : Node %d: 802.15.4SSCS : Starting PAN Coord\n", getSimTime(node), node->nodeId); } //must be an FFD mac->capability.FFD = TRUE; mac->isCoor = TRUE; //assign a short address for myself t_mpib.macShortAddress = (UInt16) node->nodeId; Mac802_15_4MLME_SET_request(node, interfaceIndex, macShortAddress, &t_mpib); //scan the channels if(DEBUG) { printf("%lld : Node %d: 802.15.4SSCS : Performing active " "channel scan\n", getSimTime(node), node->nodeId); } sscs802_15_4->state = S802_15_4SCANREQ; /*if (sscs802_15_4->t_BO == 15) { BO = 3; //dirty hack, otherwise scan //duration becomes too high } else { BO = sscs802_15_4->t_BO; }*/ Mac802_15_4MLME_SCAN_request( node, interfaceIndex, 0x01, (UInt32)S802_15_4SCANCHANNELS, S802_15_4DEF_SCANDUR); break; } case S802_15_4SCANREQ: { if (status != M802_15_4_SUCCESS) { if(DEBUG) { printf("%lld : Node %d: 802.15.4SSCS : " "Unable to start as a PAN coordinator: active " "channel scan failed -> %s\n", getSimTime(node), node->nodeId, Sscs802_15_4StatusName(status)); } sscs802_15_4->state = S802_15_4NULL; return; } //select a channel and a PAN ID (for simplicity, we just use the //IP address as the PAN ID) //(it's not an easy task to select a channel and PAN ID in //implementation!) for (i = 11; i < 27; i++) //we give priority to 2.4G { if ((sscs802_15_4->T_UnscannedChannels & (1 << i)) == 0) { break; } } if (i >= 27) { for (i = 0; i < 11; i++) { if ((sscs802_15_4->T_UnscannedChannels & (1 << i)) == 0) { break; } } } sscs802_15_4->Channel = (UInt8) i; //permit association t_mpib.macAssociationPermit = TRUE; Mac802_15_4MLME_SET_request( node, interfaceIndex, macAssociationPermit, &t_mpib); if (sscs802_15_4->t_BO < 15) //Transmit beacons { if(DEBUG) { printf("%lld : Node %d: 802.15.4SSCS : Begin to transmit" " beacons\n", getSimTime(node), node->nodeId); } sscs802_15_4->state = S802_15_4STARTREQ; Mac802_15_4MLME_START_request( node, interfaceIndex, (UInt16) node->nodeId, sscs802_15_4->Channel, sscs802_15_4->t_BO, sscs802_15_4->t_SO, TRUE, FALSE, FALSE, FALSE); } else //Do not transmit beacons { mac->isPANCoor = TRUE; t_mpib.macCoordExtendedAddress = (UInt16) node->nodeId; Mac802_15_4MLME_SET_request( node, interfaceIndex, macCoordExtendedAddress, &t_mpib); t_ppib.phyCurrentChannel = i; Phy802_15_4PLME_SET_request( node, interfaceIndex, phyCurrentChannel, &t_ppib); //Setting current listening channel mac->taskP.mlme_start_request_LogicalChannel = (UInt8) t_ppib.phyCurrentChannel; if(DEBUG) { printf("%lld : Node %d: 802.15.4SSCS : Successfully " "started a new PAN (non-beacon enabled) on " "channel %d with PAN Id %d\n", getSimTime(node), node->nodeId, sscs802_15_4->Channel, node->nodeId); } t_mpib.macPANId = (UInt16) node->nodeId; Mac802_15_4MLME_SET_request(node, interfaceIndex, macPANId, &t_mpib); t_mpib.macBeaconOrder = 15; Mac802_15_4MLME_SET_request(node, interfaceIndex, macBeaconOrder, &t_mpib); t_mpib.macSuperframeOrder = 15; Mac802_15_4MLME_SET_request(node, interfaceIndex, macSuperframeOrder, &t_mpib); sscs802_15_4->state = S802_15_4UP; } break; } case S802_15_4STARTREQ: { if (status == M802_15_4_SUCCESS) { if(DEBUG) { printf("%lld : Node %d: 802.15.4SSCS : Successfully " "started a new PAN (beacon enabled) on " "channel %d with PAN Id %d\n", getSimTime(node), node->nodeId, sscs802_15_4->Channel, node->nodeId); } sscs802_15_4->state = S802_15_4UP; } else { if(DEBUG) { printf("%lld : Node %d: 802.15.4SSCS : Failed to " "transmit beacons on channel %d with PAN Id %d " "-> %s\n", getSimTime(node), node->nodeId, sscs802_15_4->Channel, node->nodeId, Sscs802_15_4StatusName(status)); } sscs802_15_4->state = S802_15_4NULL; } break; } default: { break; } }}// /**// FUNCTION :: Sscs802_15_4StartDevice// LAYER :: SSCS// PURPOSE :: Start a Device// PARAMETERS ::// + node : Node* : Node receiving call// + assoPermit : BOOL : Whether association is permitted// + status : M802_15_4_enum: Status of request// RETURN :: None// **/staticvoid Sscs802_15_4StartDevice( Node* node, int interfaceIndex, BOOL assoPermit, M802_15_4_enum status){ MacData802_15_4* mac; SscsData802_15_4* sscs802_15_4; M802_15_4PIB t_mpib; UInt8 scan_BO; M802_15_4SuperframeSpec sfSpec; UInt8 ch; UInt8 fstChannel; UInt8 fstChannel2_4G; char tmpstr[30]; int i; int k = 0; int l = 0; mac = (MacData802_15_4*) node->macData[interfaceIndex]->macVar; sscs802_15_4 = (SscsData802_15_4*)mac->sscs; switch(sscs802_15_4->state) { case S802_15_4NULL: { if(DEBUG) { printf("%lld : Node %d: 802.15.4SSCS : Starting Device\n", getSimTime(node), node->nodeId); } scan_BO = sscs802_15_4->t_BO + 1; //set FFD mac->capability.FFD = sscs802_15_4->t_isFFD; //scan the channels if(DEBUG) { printf("%lld : Node %d: 802.15.4SSCS : Performing active " "channel scan\n", getSimTime(node), node->nodeId); } sscs802_15_4->state = S802_15_4SCANREQ; /*if (sscs802_15_4->t_BO == 15) { scan_BO = 3; //dirty hack }*/ Mac802_15_4MLME_SCAN_request( node, interfaceIndex, 0x01, (UInt32)S802_15_4SCANCHANNELS, S802_15_4DEF_SCANDUR); break; } case S802_15_4SCANREQ: { if (status != M802_15_4_SUCCESS) { sscs802_15_4->state = S802_15_4NULL; if(DEBUG) { printf("%lld : Node %d: 802.15.4SSCS : " "Unable to start as a Device: active " "channel scan failed -> %s\n", getSimTime(node), node->nodeId, Sscs802_15_4StatusName(status)); } //retry Sscs802_15_4SetTimer(node, interfaceIndex, S802_15_4ASSORETRY, S802_15_4ASSORETRY_INTERVAL); return; } //select a PAN and a coordinator to join fstChannel = 0xff; fstChannel2_4G = 0xff; for (i = 0; i < sscs802_15_4->T_ResultListSize; i++) { sfSpec.SuperSpec = sscs802_15_4->T_PANDescriptorList[i].SuperframeSpec; Mac802_15_4SuperFrameParse(&sfSpec); if (sfSpec.AssoPmt == FALSE) { continue; } else { if (sscs802_15_4->T_PANDescriptorList[i].LogicalChannel < 11) { if (fstChannel == 0xff) { fstChannel = sscs802_15_4-> T_PANDescriptorList[i].LogicalChannel; k = i; } } else { if (fstChannel2_4G == 0xff) { fstChannel2_4G = sscs802_15_4-> T_PANDescriptorList[i].LogicalChannel; l = i; } } } } if (fstChannel2_4G != 0xff) { ch = fstChannel2_4G; i = l; } else { ch = fstChannel; i = k; } if (ch == 0xff) //cannot find any coordinator for association { sscs802_15_4->state = S802_15_4NULL; if(DEBUG) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -