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

📄 p802_15_4sscs.cc

📁 802.15.4 mac 协议源程序
💻 CC
📖 第 1 页 / 共 3 页
字号:
	{		fprintf(stdout,"[%f](node %d) coordinator relocation successful, begin to re-synchronize with the coordinator\n",CURRENT_TIME,mac->index_);		//re-synchronize with the coordinator		mac->phy->PLME_GET_request(phyCurrentChannel);		mac->MLME_SYNC_request(mac->tmp_ppib.phyCurrentChannel,true);	}	else	{		bool isCoord = ((mac->capability.FFD)&&(numberDeviceLink(&mac->deviceLink1) > 0));		fprintf(stdout,"[%f](node %d) coordinator relocation failed%s\n",CURRENT_TIME,mac->index_,(isCoord)?".":" --> try to reassociate ..."); 		if (!isCoord)		//I am not a coordinator 		{	 		t_mpib.macShortAddress = 0xffff;			mac->MLME_SET_request(macShortAddress,&t_mpib);	 		t_mpib.macCoordExtendedAddress = def_macCoordExtendedAddress;			mac->MLME_SET_request(macCoordExtendedAddress,&t_mpib);			startDevice(t_isCT,t_isFFD,t_assoPermit,t_txBeacon,t_BO,t_SO,true);		}	}}void SSCS802_15_4::MLME_COMM_STATUS_indication(UINT_16 PANId,UINT_8 SrcAddrMode,IE3ADDR SrcAddr,				 UINT_8 DstAddrMode,IE3ADDR DstAddr,MACenum status){}void SSCS802_15_4::MLME_START_confirm(MACenum status){	dispatch(status,"MLME_START_confirm");}void SSCS802_15_4::MLME_SYNC_LOSS_indication(MACenum LossReason){	fprintf(stdout,"[%f](node %d) synchronization loss\n",CURRENT_TIME,mac->index_);	mac->MLME_SCAN_request(0x03,SSCS802_15_4::ScanChannels,0);}void SSCS802_15_4::MLME_POLL_confirm(MACenum status){}//--------------------------------------------------------------------------char *sscsTaskName[] = {"NONE",			"startPANCoord",			"startDevice"};void SSCS802_15_4::checkTaskOverflow(UINT_8 task){	if (sscsTaskP.taskStatus(task))	{		fprintf(stdout,"[SSCS][%f](node %d) task overflow: %s\n",CURRENT_TIME,mac->index_,sscsTaskName[task]);		exit(1);	}	else		sscsTaskP.taskStep(task) = 0;}void SSCS802_15_4::dispatch(MACenum status,char *frFunc){	if (strcmp(frFunc,"MLME_SCAN_confirm") == 0)	{		if (sscsTaskP.taskStatus(sscsTP_startPANCoord))			startPANCoord(sscsTaskP.startPANCoord_isCluster_Tree,sscsTaskP.startPANCoord_txBeacon,sscsTaskP.startPANCoord_BO,sscsTaskP.startPANCoord_SO,false,status);		else if (sscsTaskP.taskStatus(sscsTP_startDevice))			startDevice(sscsTaskP.startDevice_isCluster_Tree,sscsTaskP.startDevice_isFFD,sscsTaskP.startDevice_assoPermit,sscsTaskP.startDevice_txBeacon,sscsTaskP.startDevice_BO,sscsTaskP.startDevice_SO,false,status);	}	else if (strcmp(frFunc,"MLME_START_confirm") == 0)	{		if(sscsTaskP.taskStatus(sscsTP_startPANCoord))			startPANCoord(sscsTaskP.startPANCoord_isCluster_Tree,sscsTaskP.startPANCoord_txBeacon,sscsTaskP.startPANCoord_BO,sscsTaskP.startPANCoord_SO,false,status);		else if (sscsTaskP.taskStatus(sscsTP_startDevice))			startDevice(sscsTaskP.startDevice_isCluster_Tree,sscsTaskP.startDevice_isFFD,sscsTaskP.startDevice_assoPermit,sscsTaskP.startDevice_txBeacon,sscsTaskP.startDevice_BO,sscsTaskP.startDevice_SO,false,status);		else	//default handling		{			if (mac->mpib.macBeaconOrder == 15)				fprintf(stdout,"[%f](node %d) beacon transmission stopped [channel:%d] [PAN_ID:%d]\n",CURRENT_TIME,mac->index_,mac->tmp_ppib.phyCurrentChannel,mac->mpib.macPANId);			else if (status == m_SUCCESS)				fprintf(stdout,"[%f](node %d) beacon transmission successful [channel:%d] [PAN_ID:%d]\n",CURRENT_TIME,mac->index_,mac->tmp_ppib.phyCurrentChannel,mac->mpib.macPANId);			else				fprintf(stdout,"<!>[%f](node %d) failed to transmit beacons -> %s [channel:%d] [PAN_ID:%d]\n",CURRENT_TIME,mac->index_,statusName(status),mac->tmp_ppib.phyCurrentChannel,mac->mpib.macPANId);		}	}	else if (strcmp(frFunc,"MLME_ASSOCIATE_confirm") == 0)	{		if(sscsTaskP.taskStatus(sscsTP_startDevice))			startDevice(sscsTaskP.startDevice_isCluster_Tree,sscsTaskP.startDevice_isFFD,sscsTaskP.startDevice_assoPermit,sscsTaskP.startDevice_txBeacon,sscsTaskP.startDevice_BO,sscsTaskP.startDevice_SO,false,status);	}}void SSCS802_15_4::startPANCoord(bool isClusterTree,bool txBeacon,UINT_8 BO,UINT_8 SO,bool firsttime,MACenum status){	UINT_8 step;	MAC_PIB t_mpib;	PHY_PIB t_ppib;	int i;			if (firsttime) checkTaskOverflow(sscsTP_startPANCoord);	step = sscsTaskP.taskStep(sscsTP_startPANCoord);	switch(step)	{		case 0:			fprintf(stdout,"--- startPANCoord [%d] ---\n",mac->index_);			sscsTaskP.taskStatus(sscsTP_startPANCoord) = true;			sscsTaskP.taskStep(sscsTP_startPANCoord)++;			sscsTaskP.startPANCoord_isCluster_Tree = isClusterTree;			sscsTaskP.startPANCoord_txBeacon = txBeacon;			sscsTaskP.startPANCoord_BO = BO;			sscsTaskP.startPANCoord_SO = SO;			//must be an FFD			mac->capability.setFFD(true);			//assign a short address for myself#ifdef ZigBeeIF			if (isClusterTree)			{				assertZBR();				zbr->myDepth = 0;				zbr->myNodeID = 0;			//assign logic address 0 for myself				zbr->myParentNodeID = 0;		//no parent, assign my own ID				chkAddCTAddrLink(zbr->myNodeID,mac->index_);				activateCTAddrLink(zbr->myNodeID,mac->index_);			}#endif			t_mpib.macShortAddress = mac->index_;			mac->MLME_SET_request(macShortAddress,&t_mpib);			//scan the channels			fprintf(stdout,"[%f](node %d) performing active channel scan\n",CURRENT_TIME,mac->index_);			mac->MLME_SCAN_request(0x01,SSCS802_15_4::ScanChannels,BO);			break;		case 1:			if (status != m_SUCCESS)			{				fprintf(stdout,"<!>[%f](node %d) unable to start as a PAN coordinator: active channel scan failed -> %s\n",CURRENT_TIME,mac->index_,statusName(status));				sscsTaskP.taskStatus(sscsTP_startPANCoord) = false;				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 ((T_UnscannedChannels & (1 << i)) == 0)				break;			if (i >= 27)			for (i=0;i<11;i++)			if ((T_UnscannedChannels & (1 << i)) == 0)				break;			sscsTaskP.startPANCoord_Channel = i;			//permit association			t_mpib.macAssociationPermit = true;			mac->MLME_SET_request(macAssociationPermit,&t_mpib);			if (txBeacon)			{				sscsTaskP.taskStep(sscsTP_startPANCoord)++;				fprintf(stdout,"[%f](node %d) begin to transmit beacons\n",CURRENT_TIME,mac->index_);				mac->MLME_START_request(mac->index_,i,BO,SO,true,false,false,false);			}			else			{				mac->isPanCoor(true);				t_mpib.macCoordExtendedAddress = mac->index_;				mac->MLME_SET_request(macCoordExtendedAddress,&t_mpib);				t_ppib.phyCurrentChannel = i;				mac->phy->PLME_SET_request(phyCurrentChannel,&t_ppib);				sscsTaskP.taskStatus(sscsTP_startPANCoord) = false;				fprintf(stdout,"[%f](node %d) successfully started a new PAN (non-beacon enabled) [channel:%d] [PAN_ID:%d]\n",CURRENT_TIME,mac->index_,sscsTaskP.startPANCoord_Channel,mac->index_);				t_mpib.macPANId = mac->index_;				mac->MLME_SET_request(macPANId,&t_mpib);				t_mpib.macBeaconOrder = 15;				mac->MLME_SET_request(macBeaconOrder,&t_mpib);				t_mpib.macSuperframeOrder = 15;				mac->MLME_SET_request(macSuperframeOrder,&t_mpib);			}#ifdef ZigBeeIF			if (isClusterTree)			{				assertZBR();				zbr->dRate = mac->phy->getRate('d');			}#endif			break;		case 2:			sscsTaskP.taskStatus(sscsTP_startPANCoord) = false;			if (status == m_SUCCESS)				fprintf(stdout,"[%f](node %d) successfully started a new PAN (beacon enabled) [channel:%d] [PAN_ID:%d]\n",CURRENT_TIME,mac->index_,sscsTaskP.startPANCoord_Channel,mac->index_);			else				fprintf(stdout,"<!>[%f](node %d) failed to transmit beacons -> %s [channel:%d] [PAN_ID:%d]\n",CURRENT_TIME,mac->index_,statusName(status),sscsTaskP.startPANCoord_Channel,mac->index_);			break;		default:			break;	}}void SSCS802_15_4::startDevice(bool isClusterTree,bool isFFD,bool assoPermit,bool txBeacon,UINT_8 BO,UINT_8 SO,bool firsttime,MACenum status){	UINT_8 step,scan_BO;	MAC_PIB t_mpib;	SuperframeSpec sfSpec;	UINT_8 ch,fstChannel,fstChannel2_4G;	char tmpstr[30];	int i,k,l,m,n,depth;			if (firsttime) checkTaskOverflow(sscsTP_startDevice);	step = sscsTaskP.taskStep(sscsTP_startDevice);	switch(step)	{		case 0:			fprintf(stdout,"--- startDevice [%d] ---\n",mac->index_);			sscsTaskP.taskStatus(sscsTP_startDevice) = true;			sscsTaskP.taskStep(sscsTP_startDevice)++;			mac->capability.setFFD(isFFD);			sscsTaskP.startDevice_isCluster_Tree = isClusterTree;			sscsTaskP.startDevice_isFFD = isFFD;			sscsTaskP.startDevice_assoPermit = assoPermit;			sscsTaskP.startDevice_txBeacon = txBeacon;			sscsTaskP.startDevice_BO = BO;			sscsTaskP.startDevice_SO = SO;			scan_BO = sscsTaskP.startDevice_BO + 1;			//set FFD			mac->capability.setFFD(isFFD);			//scan the channels			fprintf(stdout,"[%f](node %d) performing active channel scan ...\n",CURRENT_TIME,mac->index_);			mac->MLME_SCAN_request(0x01,SSCS802_15_4::ScanChannels,scan_BO);			break;		case 1:			if (status != m_SUCCESS)			{				sscsTaskP.taskStatus(sscsTP_startDevice) = false;				fprintf(stdout,"<!>[%f](node %d) unable to start as a device: active channel scan failed -> %s\n",CURRENT_TIME,mac->index_,statusName(status));				assoH.start(assoRetryInterval);				return;			}			//select a PAN and a coordinator to join			fstChannel = 0xff;			fstChannel2_4G = 0xff;			for (i=0;i<T_ResultListSize;i++)			{				sfSpec.SuperSpec = T_PANDescriptorList[i].SuperframeSpec;				sfSpec.parse();				n = updateHListLink(hl_oper_est,&hlistLink1,&hlistLink2,(UINT_16)T_PANDescriptorList[i].CoordAddress_64);				if ((!sfSpec.AssoPmt)||(!n))					continue;				else				{					if (T_PANDescriptorList[i].LogicalChannel < 11)					{						if (fstChannel == 0xff)						{							fstChannel = T_PANDescriptorList[i].LogicalChannel;							k = i;						}					}					else					{						if (fstChannel2_4G == 0xff)						{							fstChannel2_4G = 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			{				sscsTaskP.taskStatus(sscsTP_startDevice) = false;				fprintf(stdout,"<!>[%f](node %d) no coordinator found for association.\n",CURRENT_TIME,mac->index_);				assoH.start(assoRetryInterval);				return;			}			else			{				//select the least depth for cluster tree association#ifdef ZigBeeIF				if (isClusterTree)				{					depth = T_PANDescriptorList[i].clusTreeDepth;					for (m=0;m<T_ResultListSize;m++)					{						n = updateHListLink(hl_oper_est,&hlistLink1,&hlistLink2,(UINT_16)T_PANDescriptorList[m].CoordAddress_64);						if ((ch == T_PANDescriptorList[m].LogicalChannel)&&(n))						if (T_PANDescriptorList[m].clusTreeDepth < depth)						{							depth = T_PANDescriptorList[m].clusTreeDepth;							i = m;						}					}				}#endif				//If the coordinator is in beacon-enabled mode, we may begin to track beacons now.				//But this is only possible if the network is a one-hop star; otherwise we don't know				//which coordinator to track, since there may be more than one beaconing coordinators				//in a device's neighborhood and MLME-SYNC.request() has no parameter telling which 				//coordinator to track. As this is an optional step, we will not track beacons here.				t_mpib.macAssociationPermit = assoPermit;

⌨️ 快捷键说明

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