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

📄 mac01.c

📁 zigbee程序
💻 C
📖 第 1 页 / 共 5 页
字号:
							break;
							
			case TYPE_ACK: //printfUART("ra\n","");
							//ack_received = 1;
							indication_ack(&buffer_msg[current_msg_out],link_qual);
							
							break;
							
			case TYPE_CMD:  //printfUART("rc\n","");
							indication_cmd(&buffer_msg[current_msg_out],link_qual);
							break;
			
			case TYPE_BEACON:
							
							//printfUART("rb %i\n",buffer_msg[current_msg_out].seq_num);
							if (mac_PIB.macShortAddress == 0x0000) 
							{
								buffer_count--;
							}
							else
							{
								process_beacon(&buffer_msg[current_msg_out],link_qual);
								
							}

							break;
			default: 
						atomic buffer_count--;
						////printfUART("Invalid frame type\n","");

						break;
	}
	atomic{
			current_msg_out++;
		if ( current_msg_out == RECEIVE_BUFFER_SIZE )	
			current_msg_out = 0;
		}
	}
	return;
}


/****************DATA indication functions******************/

void indication_data(MPDU *pdu, int8_t ppduLinkQuality)
{
	uint8_t data_len;
	
	uint8_t payload[80];
	uint8_t msdu_length=0;
	
	//int i;
	
	uint32_t SrcAddr[2];
	uint32_t DstAddr[2];
	
	
	//frame control variables
	uint8_t source_address=0;
	uint8_t destination_address=0;


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

	//implement the intra PAN data messages
	//intra_pan_source_short *intra_pan_source_short_ptr;
	//intra_pan_source_long *intra_pan_source_long_ptr;
	
	
	source_address=get_fc2_source_addr(pdu->frame_control2);
	destination_address=get_fc2_dest_addr(pdu->frame_control2);
	
	//decrement buffer count
	atomic buffer_count --;
	
	SrcAddr[0]=0x00000000;
	SrcAddr[1]=0x00000000;
	DstAddr[0]=0x00000000;
	DstAddr[1]=0x00000000;


	////printfUART("id %i %i \n",source_address,destination_address);


	if ( get_fc1_intra_pan(pdu->frame_control1)== 0 )
	{
	//INTRA PAN
		if (destination_address > 1 && source_address > 1)
		{
			// Destination LONG - Source LONG	
			if (destination_address == LONG_ADDRESS && source_address == LONG_ADDRESS)
			{
				dest_long_ptr = (dest_long *) &pdu->data[0];
				source_long_ptr = (source_long *) &pdu->data[DEST_LONG_LEN];
				
				//If a short destination address is included in the frame, it shall match either macShortAddress or the
				//broadcast address (0 x ffff). Otherwise, if an extended destination address is included in the frame, it
				//shall match aExtendedAddress.
				if ( dest_long_ptr->destination_address0 !=aExtendedAddress0 && dest_long_ptr->destination_address1 !=aExtendedAddress1 )
				{
					//////////printfUART("data rejected, ext destination not for me\n", ""); 
					return;
				}
				//If a destination PAN identifier is included in the frame, it shall match macPANId or shall be the
				//broadcast PAN identifier (0 x ffff).
				if(dest_long_ptr->destination_PAN_identifier != 0xffff && dest_long_ptr->destination_PAN_identifier != mac_PIB.macPANId )
				{
					//////////printfUART("data rejected, wrong destination PAN\n", ""); 
					return;
				}
				data_len = 20;
				
				
				DstAddr[1] = dest_long_ptr->destination_address0;
				DstAddr[0] =dest_long_ptr->destination_address1;
				
				SrcAddr[1] =source_long_ptr->source_address0;
				SrcAddr[0] =source_long_ptr->source_address1;
				
				msdu_length = pdu->length - data_len;
				/*
				for(i=0 ; i < msdu_length; i++)
				{
					payload[i] = pdu->data[data_len +i];
				}
				*/
				memcpy(&payload,&pdu->data[data_len],msdu_length * sizeof(uint8_t));
				
				signal MCPS_DATA.indication((uint16_t)source_address, (uint16_t)source_long_ptr->source_PAN_identifier, SrcAddr,(uint16_t)destination_address, (uint16_t)dest_long_ptr->destination_PAN_identifier, DstAddr, (uint16_t)msdu_length, payload, (uint16_t)ppduLinkQuality, 0x0000,0x0000);  
				
			}
			
			// Destination SHORT - Source LONG
			if ( destination_address == SHORT_ADDRESS && source_address == LONG_ADDRESS )
			{
				dest_short_ptr = (dest_short *) &pdu->data[0];
				source_long_ptr = (source_long *) &pdu->data[DEST_SHORT_LEN];
				
				//If a short destination address is included in the frame, it shall match either macShortAddress or the
				//broadcast address (0 x ffff). Otherwise, if an extended destination address is included in the frame, it
				//shall match aExtendedAddress.
				if ( dest_short_ptr->destination_address != 0xffff && dest_short_ptr->destination_address != mac_PIB.macShortAddress)
				{
					//////////printfUART("data rejected, short destination not for me\n", ""); 
					return;
				}
				//If a destination PAN identifier is included in the frame, it shall match macPANId or shall be the
				//broadcast PAN identifier (0 x ffff).
				if(dest_short_ptr->destination_PAN_identifier != 0xffff && dest_short_ptr->destination_PAN_identifier != mac_PIB.macPANId )
				{
					//////////printfUART("data rejected, wrong destination PAN\n", ""); 
					return;
				}
				
				data_len = 14;
				
				DstAddr[0] =dest_short_ptr->destination_address;
				
				SrcAddr[1] =source_long_ptr->source_address0;
				SrcAddr[0] =source_long_ptr->source_address1;
				
				msdu_length = pdu->length - data_len;
				/*
				for(i=0 ; i < msdu_length; i++)
				{
					payload[i] = pdu->data[data_len +i];
				}
				*/
				memcpy(&payload,&pdu->data[data_len],msdu_length * sizeof(uint8_t));
				
				signal MCPS_DATA.indication((uint16_t)source_address, (uint16_t)source_long_ptr->source_PAN_identifier, SrcAddr,(uint16_t)destination_address, (uint16_t)dest_short_ptr->destination_PAN_identifier, DstAddr, (uint16_t)msdu_length, payload, (uint16_t)ppduLinkQuality, 0x0000,0x0000);  

			}
			// Destination LONG - Source SHORT
			if ( destination_address == LONG_ADDRESS && source_address == SHORT_ADDRESS )
			{
				dest_long_ptr = (dest_long *) &pdu->data[0];
				source_short_ptr = (source_short *) &pdu->data[DEST_LONG_LEN];
				
				//If a short destination address is included in the frame, it shall match either macShortAddress or the
				//broadcast address (0 x ffff). Otherwise, if an extended destination address is included in the frame, it
				//shall match aExtendedAddress.
				if ( dest_long_ptr->destination_address0 !=aExtendedAddress0 && dest_long_ptr->destination_address1 !=aExtendedAddress1 )
				{
					//////////printfUART("data rejected, ext destination not for me\n", ""); 
					return;
				}
				//If a destination PAN identifier is included in the frame, it shall match macPANId or shall be the
				//broadcast PAN identifier (0 x ffff).
				if(dest_long_ptr->destination_PAN_identifier != 0xffff && dest_long_ptr->destination_PAN_identifier != mac_PIB.macPANId )
				{
					//////////printfUART("data rejected, wrong destination PAN\n", ""); 
					return;
				}
				
				data_len = 14;
				
				DstAddr[1] = dest_long_ptr->destination_address0;
				DstAddr[0] =dest_long_ptr->destination_address1;
				
				
				SrcAddr[0] =source_short_ptr->source_address;
				
				msdu_length = pdu->length - data_len;
				
				/*
				for(i=0 ; i < msdu_length; i++)
				{
					payload[i] = pdu->data[data_len +i];
				}
				*/
				memcpy(&payload,&pdu->data[data_len],msdu_length * sizeof(uint8_t));
				
				signal MCPS_DATA.indication((uint16_t)source_address, (uint16_t)source_short_ptr->source_PAN_identifier, SrcAddr,(uint16_t)destination_address, (uint16_t)dest_long_ptr->destination_PAN_identifier, DstAddr, (uint16_t)msdu_length, payload, (uint16_t)ppduLinkQuality, 0x0000,0x0000);  

			}
			
			
			//Destination SHORT - Source SHORT
			if ( destination_address == SHORT_ADDRESS && source_address == SHORT_ADDRESS )	
			{
				dest_short_ptr = (dest_short *) &pdu->data[0];
				source_short_ptr = (source_short *) &pdu->data[DEST_SHORT_LEN];
				
				//If a short destination address is included in the frame, it shall match either macShortAddress or the
				//broadcast address (0 x ffff). Otherwise, if an extended destination address is included in the frame, it
				//shall match aExtendedAddress.
				if ( dest_short_ptr->destination_address != 0xffff && dest_short_ptr->destination_address != mac_PIB.macShortAddress)
				{
					//printfUART("data rejected, short destination not for me\n", ""); 
					return;
				}
				//If a destination PAN identifier is included in the frame, it shall match macPANId or shall be the
				//broadcast PAN identifier (0 x ffff).
				if(dest_short_ptr->destination_PAN_identifier != 0xffff && dest_short_ptr->destination_PAN_identifier != mac_PIB.macPANId )
				{
					//printfUART("SH SH data rejected, wrong destination PAN %x\n",mac_PIB.macPANId ); 
					return;
				}
				
				data_len = 8;
				
				//gts control
				/*
				//////////printfUART("check %i\n", source_short_ptr->source_address); 
				atomic{
					if(GTS_db[15-gts_receive_slot].DevAddressType == source_short_ptr->source_address)
					{
						//////////printfUART("checked\n",""); 
						GTS_db[15-gts_receive_slot].expiration=0;	
			
					}
				}
				*/
				
				if ( get_fc1_ack_request(pdu->frame_control1) == 1 ) 
				{
					build_ack(pdu->seq_num,0);
				}
				
				DstAddr[0] =dest_short_ptr->destination_address;
				
				SrcAddr[0] =source_short_ptr->source_address;
				
				msdu_length = (pdu->length - 5) - data_len;
				
				////printfUART("mac %i %i \n",pdu->length, msdu_length); 
				
				/*
				for(i=0 ; i < msdu_length+8; i++)
				{
					//payload[i] = pdu->data[data_len +i];
					//printfUART("l %x \n",pdu->data[i]);
				}
				*/
				
				memcpy(&payload,&pdu->data[data_len],msdu_length * sizeof(uint8_t));
				/*
				for(i=0 ; i < msdu_length; i++)
				{
					//payload[i] = pdu->data[data_len +i];
					//printfUART("l %x \n",payload[i]);
				}
				*/
				
				
				////printfUART("macdest %x %x \n",DstAddr[0], DstAddr[1]); 
				
				//indication(uint16_t SrcAddrMode, uint16_t SrcPANId, uint32_t SrcAddr[2], uint16_t DstAddrMode, uint16_t DestPANId, uint32_t DstAddr[2], uint16_t msduLength,uint8_t msdu[],uint8_t mpduLinkQuality, uint8_t SecurityUse, uint8_t ACLEntry);  
				
				signal MCPS_DATA.indication((uint16_t)source_address, (uint16_t)source_short_ptr->source_PAN_identifier, SrcAddr,(uint16_t)destination_address, (uint16_t)dest_short_ptr->destination_PAN_identifier, DstAddr, (uint16_t)msdu_length,payload, (uint16_t)ppduLinkQuality, 0x0000,0x0000);  

			}
		}
		
		/*********NO DESTINATION ADDRESS PRESENT ****************/
		
		if ( destination_address == 0 && source_address > 1 )
		{
				
			if (source_address == LONG_ADDRESS)
			{//Source LONG
				source_long_ptr = (source_long *) &pdu->data[0];
				
				//If only source addressing fields are included in a data or MAC command frame, the frame shall be
				//accepted only if the device is a PAN coordinator and the source PAN identifier matches macPANId.
				if ( PANCoordinator==0 || source_long_ptr->source_PAN_identifier != mac_PIB.macPANId )
				{
					//////////printfUART("data rejected, im not pan\n", ""); 
					return;
				}
				
				data_len = 10;
				
				SrcAddr[1] =source_long_ptr->source_address0;
				SrcAddr[0] =source_long_ptr->source_address1;
				
				msdu_length = pdu->length - data_len;
				/*
				for(i=0 ; i < msdu_length; i++)
				{
					payload[i] = pdu->data[data_len +i];
				}
				*/
				memcpy(&payload,&pdu->data[data_len],msdu_length * sizeof(uint8_t));
				
				signal MCPS_DATA.indication((uint16_t)source_address,(uint16_t)source_long_ptr->source_PAN_identifier, SrcAddr,(uint16_t)destination_address, 0x0000, DstAddr, (uint16_t)msdu_length, payload, (uint16_t)ppduLinkQuality, 0x0000,0x0000);  

			}
			else
			{//Source SHORT

				source_short_ptr = (source_short *) &pdu->data[0];
				//If only source addressing fields are included in a data or MAC command frame, the frame shall be
				//accepted only if the device is a PAN coordinator and the source PAN identifier matches macPANId.
				if ( PANCoordinator==0 || source_short_ptr->source_PAN_identifier != mac_PIB.macPANId )
				{
					//////////printfUART("data rejected, im not pan\n", ""); 
					return;
				}
				
				data_len = 4;

				
				SrcAddr[0] =source_short_ptr->source_address;
				
				msdu_length = pdu->length - data_len;
				/*
				for(i=0 ; i < msdu_length; i++)
				{
					payload[i] = pdu->data[data_len +i];
				}
				*/
				memcpy(&payload,&pdu->data[data_len],msdu_length * sizeof(uint8_t));
				
				signal MCPS_DATA.indication((uint16_t)source_address, (uint16_t)source_short_ptr->source_PAN_identifier, SrcAddr,(uint16_t)destination_address, 0x0000, DstAddr, (uint16_t)msdu_length, payload, (uint16_t)ppduLinkQuality, 0x0000,0x0000);  

			}
		}
		/*********NO SOURCE ADDRESS PRESENT ****************/
		
		if ( destination_address > 1 && source_address == 0 )
		{
			if (destination_address == LONG_ADDRESS)
			{//Destination LONG
				dest_long_ptr = (dest_long *) &pdu->data[0];
				
				//If a short destination address is included in the frame, it shall match either macShortAddress or the
				//broadcast address (0 x ffff). Otherwise, if an extended destination address is included in the frame, it
				//shall match aExtendedAddress.
				if ( dest_long_ptr->destination_address0 !=aExtendedAddress0 && dest_long_ptr->destination_address1 !=aExtendedAddress1 )
				{
					//////////printfUART("data rejected, ext destination not for me\n", ""); 
					return;
				}
				//If a destination PAN identifier is included in the frame, it shall match macPANId or shall be the
				//broadcast PAN identifier (0 x ffff).
				if(dest_long_ptr->destination_PAN_identifier != 0xffff && dest_long_ptr->destination_PAN_identifier != mac_PIB.macPANId )
				{
					//////////printfUART("data rejected, wrong destination PAN\n", ""); 
					return;
				}
				
				data_len = 10;
				
				DstAddr[1] = dest_long_ptr->destination_address0;
				DstAddr[0] =dest_long_ptr->destination_address1;

⌨️ 快捷键说明

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