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

📄 macm.nc.svn-base

📁 802.15.4协议的实现
💻 SVN-BASE
📖 第 1 页 / 共 5 页
字号:
void create_association_request_cmd(uint8_t CoordAddrMode,uint16_t CoordPANId,uint32_t CoordAddress[])
{
atomic{
	cmd_association_request_short *mac_send_request_ptr;
	mac_send_request_ptr = (cmd_association_request_short*) &mac_txmpdu.data;
		
		
	
		mac_txmpdu_ptr->length = 21;
		mac_txmpdu_ptr->frame_control = set_frame_control(TYPE_CMD,0,0,1,0,SHORT_ADDRESS,LONG_ADDRESS);
		mac_txmpdu_ptr->seq_num = mac_PIB.macDSN;
		mac_PIB.macDSN++;
		
		mac_send_request_ptr->destination_PAN_identifier = CoordPANId; //mac_PIB.macPANId;
		
		if (CoordAddrMode == SHORT_ADDRESS )
		{
			mac_send_request_ptr->destination_address = (uint16_t)(CoordAddress[1] & 0x000000ff) ;  //mac_PIB.macPANId;
		}
		else
		{
		//CHECK
		
		//implement the long address version
		
		
		}
		
		mac_send_request_ptr->source_PAN_identifier = 0xffff;
		
		mac_send_request_ptr->source_address0 = aExtendedAddress0;
		mac_send_request_ptr->source_address1 = aExtendedAddress1;
		
		mac_send_request_ptr->command_frame_identifier = CMD_ASSOCIATION_REQUEST;
		
		mac_send_request_ptr->capability_information = set_capability_information(0,0, 0, 0,0, 1);

		ack_wait=1;
		//activate the aResponseWaitTime Timer
		call T_ResponseWaitTime.start(TIMER_ONE_SHOT, response_wait_time);
		
		
		send_frame(mac_txmpdu_ptr->length,(uint8_t*)mac_txmpdu_ptr,TYPE_CMD,1);
	}
return;
}

void create_data_request_cmd()
{
printfUART("create_data_request_cmd\n", create_data_request_cmd); 

atomic{
		cmd_data_request_0_3 *mac_data_request;
	
		mac_data_request= (cmd_data_request_0_3*) &mac_txmpdu_ptr->data;
	
		mac_txmpdu_ptr->length = 16;							
		mac_txmpdu_ptr->frame_control = set_frame_control(TYPE_CMD,0,0,1,1,0,LONG_ADDRESS);   //dest | source
		mac_txmpdu_ptr->seq_num = mac_PIB.macDSN;
		mac_PIB.macDSN++;
		
		mac_data_request->source_PAN_identifier = mac_PIB.macPANId;
		
		mac_data_request->source_address0 = aExtendedAddress0;
		mac_data_request->source_address1 = aExtendedAddress1;
		
		
		mac_data_request->command_frame_identifier = CMD_DATA_REQUEST;
		
		send_frame(mac_txmpdu_ptr->length,(uint8_t*)mac_txmpdu_ptr,TYPE_CMD,0);
		}

return;
}

void create_beacon_request_cmd()
{

atomic{
		cmd_beacon_request *mac_beacon_request;
	
		mac_beacon_request= (cmd_beacon_request*) &mac_txmpdu_ptr->data;
	
		mac_txmpdu_ptr->length = 10;							
		mac_txmpdu_ptr->frame_control = set_frame_control(TYPE_CMD,0,0,0,0,SHORT_ADDRESS,0);   //dest | source
		mac_txmpdu_ptr->seq_num = mac_PIB.macDSN;
		mac_PIB.macDSN++;
		
		mac_beacon_request->destination_PAN_identifier = 0xffff;
		
		mac_beacon_request->destination_address = 0xffff;
	
		mac_beacon_request->command_frame_identifier = CMD_BEACON_REQUEST;
		
		send_frame(mac_txmpdu_ptr->length,(uint8_t*)mac_txmpdu_ptr,TYPE_CMD,0);
		}

return;
}

void create_gts_request_cmd(uint8_t gts_characteristics)
{
atomic{
		cmd_gts_request *mac_gts_request;
	
		mac_gts_request= (cmd_gts_request*) &mac_txmpdu_ptr->data;
	
		mac_txmpdu_ptr->length = 11;	
		if ( get_characteristic_type(gts_characteristics) != 0 )
		{   //request no ack
			mac_txmpdu_ptr->frame_control = set_frame_control(TYPE_CMD,0,0,0,1,0,SHORT_ADDRESS);   //dest | source
		}
		else
		{	//dealocate must be ack
			mac_txmpdu_ptr->frame_control = set_frame_control(TYPE_CMD,0,0,1,1,0,SHORT_ADDRESS);
		}
		mac_txmpdu_ptr->seq_num = mac_PIB.macDSN;
		mac_PIB.macDSN++;
		
		mac_gts_request->source_PAN_identifier = mac_PIB.macPANId;
		
		mac_gts_request->source_address = mac_PIB.macShortAddress;
	
	
		mac_gts_request->command_frame_identifier = CMD_GTS_REQUEST;
		
		//mac_gts_request->gts_characteristics = set_gts_characteristics(2,1,1);
		mac_gts_request->gts_characteristics =gts_characteristics;
		
		send_frame(mac_txmpdu_ptr->length,(uint8_t*)mac_txmpdu_ptr,TYPE_CMD,0);
		}

return;
}



void create_disassociation_notification_cmd(uint8_t disassociation_reason)
{

atomic{
		cmd_disassociation_notification *mac_disassociation_notification;
		
		//creation of a pointer to the disassociation notification structure
		mac_disassociation_notification = (cmd_disassociation_notification*) &mac_txmpdu.data;									
		
		
		mac_txmpdu_ptr->length = 27;
		mac_txmpdu_ptr->frame_control = set_frame_control(TYPE_CMD,0,0,1,0,LONG_ADDRESS,LONG_ADDRESS);
		mac_txmpdu_ptr->seq_num = mac_PIB.macDSN;
		mac_PIB.macDSN++;
		
		mac_disassociation_notification->destination_PAN_identifier = mac_PIB.macPANId;

		mac_disassociation_notification->destination_address0 = mac_PIB.macCoordExtendedAddress0;
		mac_disassociation_notification->destination_address1 = mac_PIB.macCoordExtendedAddress1;
	
		mac_disassociation_notification->source_PAN_identifier = mac_PIB.macPANId;
	
		mac_disassociation_notification->source_address0 = aExtendedAddress0;
		mac_disassociation_notification->source_address1 = aExtendedAddress1;
	
		mac_disassociation_notification->command_frame_identifier = CMD_DISASSOCIATION_NOTIFICATION;
		
		mac_disassociation_notification->disassociation_reason = disassociation_reason;
	
		send_frame(mac_txmpdu_ptr->length,(uint8_t*)mac_txmpdu_ptr,TYPE_CMD,0);
		}
return;
}



void build_ack(uint8_t sequence,uint8_t frame_pending)
{

	printfUART(" build_ack\n","");

	atomic{
			mac_txmpdu_ptr->length = 5;
			mac_txmpdu_ptr->frame_control = set_frame_control(TYPE_ACK,0,frame_pending,0,0,0,0);
			mac_txmpdu_ptr->seq_num = sequence;
			
			send_frame(mac_txmpdu_ptr->length,(uint8_t*)mac_txmpdu_ptr,TYPE_ACK,0);
	
	}
}


void create_data_frame(uint8_t SrcAddrMode, uint16_t SrcPANId, uint32_t SrcAddr[], uint8_t DstAddrMode, uint16_t DestPANId, uint32_t DstAddr[], uint8_t msduLength, uint8_t msdu[],uint8_t msduHandle, uint8_t TxOptions,uint8_t on_gts_slot,uint8_t pan)
{
	//	uint8_t length=0;
	int i;
	//data_addr_short *mac_data_addr_short;

	dest_short *dest_short_ptr;
	dest_long *dest_long_ptr;
	
	source_short *source_short_ptr;
	source_long *source_long_ptr;

	
	//intra_pan_source_short *intra_pan_source_short_ptr;
	//intra_pan_source_long *intra_pan_source_long_ptr;
		
	//CHECK
	uint8_t intra_pan=0;
	
	uint8_t data_len=0;
	
atomic{


	if (intra_pan == 0 )
	{
	
		if ( DstAddrMode > 1 && SrcAddrMode > 1 )
		{
			// Destination LONG - Source LONG	
			if (DstAddrMode == LONG_ADDRESS && SrcAddrMode == LONG_ADDRESS)
			{
				dest_long_ptr = (dest_long *) &mac_txmpdu.data[0];
				source_long_ptr = (source_long *) &mac_txmpdu.data[DEST_LONG_LEN];
				
				dest_long_ptr->destination_PAN_identifier=DestPANId;
				dest_long_ptr->destination_address0=DstAddr[1];
				dest_long_ptr->destination_address1=DstAddr[0];
				
				source_long_ptr->source_PAN_identifier=SrcPANId;
				source_long_ptr->source_address0=SrcAddr[1];
				source_long_ptr->source_address1=SrcAddr[0];
				
				data_len = 20;
			}
			
			// Destination SHORT - Source LONG
			if ( DstAddrMode == SHORT_ADDRESS && SrcAddrMode == LONG_ADDRESS )
			{
				dest_short_ptr = (dest_short *) &mac_txmpdu.data[0];
				source_long_ptr = (source_long *) &mac_txmpdu.data[DEST_SHORT_LEN];
				
				dest_short_ptr->destination_PAN_identifier=DestPANId;
				dest_short_ptr->destination_address=(uint16_t)DstAddr[1];
				
				source_long_ptr->source_PAN_identifier=SrcPANId;
				source_long_ptr->source_address0=SrcAddr[1];
				source_long_ptr->source_address1=SrcAddr[0];
				
				data_len = 14;
			}
			// Destination LONG - Source SHORT
			if ( DstAddrMode == LONG_ADDRESS && SrcAddrMode == SHORT_ADDRESS )
			{
				dest_long_ptr = (dest_long *) &mac_txmpdu.data[0];
				source_short_ptr = (source_short *) &mac_txmpdu.data[DEST_LONG_LEN];
				
				dest_long_ptr->destination_PAN_identifier=DestPANId;
				dest_long_ptr->destination_address0=DstAddr[1];
				dest_long_ptr->destination_address1=DstAddr[0];
				
				source_short_ptr->source_PAN_identifier=SrcPANId;
				source_short_ptr->source_address=(uint16_t)SrcAddr[1];
				
				data_len = 14;
			}
			
			
			//Destination SHORT - Source SHORT
			if ( DstAddrMode == SHORT_ADDRESS && SrcAddrMode == SHORT_ADDRESS )	
			{
				dest_short_ptr = (dest_short *) &mac_txmpdu.data[0];
				source_short_ptr = (source_short *) &mac_txmpdu.data[DEST_SHORT_LEN];
				
				dest_short_ptr->destination_PAN_identifier=DestPANId;
				dest_short_ptr->destination_address=(uint16_t)DstAddr[0];
				
				source_short_ptr->source_PAN_identifier=SrcPANId;
				source_short_ptr->source_address=(uint16_t)SrcAddr[1];
				
				data_len = 8;
			}
		}
		
		if ( DstAddrMode == 0 && SrcAddrMode > 1 )
		{
				
			if (SrcAddrMode == LONG_ADDRESS)
			{//Source LONG
				source_long_ptr = (source_long *) &mac_txmpdu.data[0];
				
				source_long_ptr->source_PAN_identifier=SrcPANId;
				source_long_ptr->source_address0=SrcAddr[1];
				source_long_ptr->source_address1=SrcAddr[0];
				
				data_len = 10;
			}
			else
			{//Source SHORT

				source_short_ptr = (source_short *) &mac_txmpdu.data[0];
				
				source_short_ptr->source_PAN_identifier=SrcPANId;
				source_short_ptr->source_address=(uint16_t)SrcAddr[1];
				
				data_len = 4;
			}
		}
		
		if ( DstAddrMode > 1 && SrcAddrMode == 0 )
		{
			if (DstAddrMode == LONG_ADDRESS)
			{//Destination LONG
				dest_long_ptr = (dest_long *) &mac_txmpdu.data[0];
		
				dest_long_ptr->destination_PAN_identifier=DestPANId;
				dest_long_ptr->destination_address0=DstAddr[1];
				dest_long_ptr->destination_address1=DstAddr[0];

				data_len = 10;
			}
			else
			{//Destination SHORT
				dest_short_ptr = (dest_short *) &mac_txmpdu.data[0];

				dest_short_ptr->destination_PAN_identifier=DestPANId;
				dest_short_ptr->destination_address=(uint16_t)DstAddr[1];
				
				data_len = 4;
			}
		}
	}
	else
	{
	//intra_pan == 1


	}


		/*
		printfUART("Tack: %i\n", get_txoptions_ack(TxOptions) ); 
		printfUART("Tgts: %i\n", get_txoptions_gts(TxOptions) ); 
		printfUART("Tindi: %i\n", get_txoptions_indirect_transmission(TxOptions) ); 
		printfUART("Tsec: %i\n", get_txoptions_security(TxOptions) ); 
		*/


		for (i=0 ; i <  msduLength ; i++)
		{
			mac_txmpdu_ptr->data[data_len + i] = (uint8_t) msdu[i];
		}
		
	
		//if (get_txoptions_gts(TxOptions) == 1)
		if(on_gts_slot==1)
		{
			printfUART("GTS send\n", "");
			mac_txmpdu_ptr->length = 100;
			mac_txmpdu_ptr->frame_control = set_frame_control(TYPE_DATA,0,0,1,intra_pan,DstAddrMode,SrcAddrMode);
			mac_txmpdu_ptr->seq_num = mac_PIB.macDSN;
			mac_PIB.macDSN++;
			
			//void add_gts_buffer_element(uint8_t *frame_ptr,uint8_t pan_coord,uint8_t send_slot)
			//TEST IM NOT A PAN
			add_gts_buffer_element((uint8_t*)mac_txmpdu_ptr,0,0);
		
			//send_frame(mac_txmpdu_ptr->length,(uint8_t*)mac_txmpdu_ptr,TYPE_DATA,0);
		}
		else
		{
			printfUART("CSMA send\n", "");
			mac_txmpdu_ptr->length = 100;
			mac_txmpdu_ptr->frame_control = set_frame_control(TYPE_DATA,0,0,0,intra_pan,DstAddrMode,SrcAddrMode);
			mac_txmpdu_ptr->seq_num = mac_PIB.macDSN;
			mac_PIB.macDSN++;
			
			send_frame(mac_txmpdu_ptr->length,(uint8_t*)mac_txmpdu_ptr,TYPE_DATA,1);
		}
		
		//printfUART("Backoff Period Number Data send %i\n", number_b);
}

return;
}


//inicialization of the mac constants
void init_MacCon()
{
/*****************************************************/
/*				Boolean Variables					 */
/*****************************************************/  
PANCoordinator = 0;
//(0 NO beacon transmission; 1 beacon transmission);
Beacon_enabled_PAN = 0;
//(SYNC)the device will try to track the beacon ie enable its receiver just before the espected time of each beacon
TrackBeacon=0;
//(SYNC)the device will try to locate one beacon
findabeacon=0;
//(RESET) when the reset command arrives it checks whether or not to reset the PIB
SetDefaultPIB=0;
/*****************************************************/
/*				Integer Variables					 */
/*****************************************************/  
/*
//Beacon Interval
uint32_t BI;
//Superframe duration
uint32_t SD;
*/
//(SYNC)number of beacons lost before sending a Beacon-Lost indication comparing to aMaxLostBeacons
missed_beacons=0;
//current_channel
current_channel=0;


/*****************************************************/
/*				Other Variables					 */
/*****************************************************/ 
pending_reset=0;

}

//inicialization of the mac PIB
void init_MacPIB()
{

atomic{
	//mac PIB default values
	
	//mac_PIB.macAckWaitDuration = 54; OLD VALUE//Number os symbols to wait for an acknowledgment
	mac_PIB.macAckWaitDuration = 65;
	
	mac_PIB.macAssociationPermit = 1; //1 - PAN allowing associations
	mac_PIB.macAutoRequest = 1; //indication if a device automatically sends a data request command if address is listed in the beacon frame
	
	mac_PIB.macBattLifeExt= 0; //batery life extension CSMA-CA
	
	mac_PIB.macBattLifeExtPeriods=6;
	//mac_PIB.macBeaconPayload; //payload of the beacon
	mac_PIB.macBeaconPayloadLenght=0; //beacon payload lenght
	mac_PIB.macBeaconOrder=8;  //specification of how often the coordinator transmits a beacon
	mac_PIB.macBeaconTxTime=(0xffffff << 24); //*****
	mac_PIB.macBSN=call Random.rand(); //sequence number of the beacon frame
	mac_PIB.macCoordExtendedAddress0 = 0x00000000; //64bits address of the coordinator with witch the device is associated
	mac_PIB.macCoordExtendedAddress1 = 0x00000000;
	
	mac_PIB.macCoordShortAddress = 0xffff; //16bits address of the coordinator with witch the device is associated
	
	mac_PIB.macDSN=call Random.rand(); //sequence number of the transmited data or MAC command frame
	
	//alowing gts requests (used in beacon)
	mac_PIB.macGTSPermit=1; //
	
	//Number of maximum CSMA backoffs
	mac_PIB.macMaxCSMABackoffs=4;
	mac_PIB.macMinBE=3;
	
	mac_PIB.macPANId=0xffff; //16bits identifier of the PAN on witch this device is operating
	
	mac_PIB.macPromiscuousMode=0;
	mac_PIB.macRxOnWhenIdle=0;
 
	//mac_PIB.macShortAddress=TOS_LOCAL_ADDRESS; //16bits short address
	mac_PIB.macShortAddress=0xffff;


	mac_PIB.macSuperframeOrder=6;
	
	//default mac_PIB.macTransactionPersistenceTime=0x01f4;
	mac_PIB.macTransactionPersistenceTime=0x0010;
	
	//*******************************************

	}
}



/*****************************************************************************************************/
/*****************************************************************************************************/
/*****************************************************************************************************/
/************************		MLME IMPLEMENTATION      			**********************************/
/*****************************************************************************************************/ 
/*****************************************************************************************************/
/**************************

⌨️ 快捷键说明

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