📄 macp.nc
字号:
data_len = 4;
SrcAddr[0] =source_short_ptr->source_address;
msdu_length = pdu->length - data_len;
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;
msdu_length = pdu->length - data_len;
memcpy(&payload,&pdu->data[data_len],msdu_length * sizeof(uint8_t));
signal MCPS_DATA.indication((uint16_t)source_address,0x0000, SrcAddr,(uint16_t)destination_address, (uint16_t)dest_long_ptr->destination_PAN_identifier, DstAddr, (uint16_t)msdu_length, payload, (uint16_t)ppduLinkQuality, 0x0000,0x0000);
}
else
{//Destination SHORT
dest_short_ptr = (dest_short *) &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_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 = 4;
DstAddr[0] =dest_short_ptr->destination_address;
msdu_length = pdu->length - data_len;
memcpy(&payload,&pdu->data[data_len],msdu_length * sizeof(uint8_t));
signal MCPS_DATA.indication((uint16_t)source_address,0x0000, SrcAddr,(uint16_t)destination_address, (uint16_t)dest_short_ptr->destination_PAN_identifier, DstAddr, (uint16_t)msdu_length, payload, (uint16_t)ppduLinkQuality, 0x0000,0x0000);
data_len = 4;
}
}
}
else
{
//intra_pan == 1
}
return;
}
void indication_cmd(MPDU *pdu, int8_t ppduLinkQuality)
{
uint8_t cmd_type;
//uint8_t pk_ptr;
uint8_t addressing_fields_length=0;
uint32_t SrcAddr[2];
//uint32_t DstAddr[2];//NOT USED SO FAR
//frame control variables
uint8_t source_address=0;
uint8_t destination_address=0;
//NOT USED SO FAR
//dest_short *dest_short_ptr;
//dest_long *dest_long_ptr;
//NOT USED SO FAR
//source_short *source_short_ptr;
source_long *source_long_ptr;
dest_short *dest_short_ptr;
dest_long *dest_long_ptr;
//CHECK IMPLEMENT
//intra_pan_source_short *intra_pan_source_short_ptr;
//intra_pan_source_long *intra_pan_source_long_ptr;
destination_address=get_fc2_dest_addr(pdu->frame_control2);
source_address=get_fc2_source_addr(pdu->frame_control2);
//decrement buffer count
atomic buffer_count --;
switch(destination_address)
{
case LONG_ADDRESS: addressing_fields_length = DEST_LONG_LEN;
dest_long_ptr = (dest_long *) &pdu->data[0];
if(dest_long_ptr->destination_address0 !=aExtendedAddress0 && dest_long_ptr->destination_address1 !=aExtendedAddress1)
{
//printfUART("NOT FOR ME","");
return;
}
break;
case SHORT_ADDRESS: addressing_fields_length = DEST_SHORT_LEN;
dest_short_ptr= (dest_short *) &pdu->data[0];
//destination command not for me
if (dest_short_ptr->destination_address != mac_PIB.macShortAddress && dest_short_ptr->destination_address !=0xffff)
{
//printfUART("NOT FOR ME","");
////////////printfUART("NOT FOR ME %x me %e\n", dest_short_ptr->destination_address,mac_PIB.macShortAddress);
return;
}
break;
}
switch(source_address)
{
case LONG_ADDRESS: addressing_fields_length = addressing_fields_length + SOURCE_LONG_LEN;
break;
case SHORT_ADDRESS: addressing_fields_length = addressing_fields_length + SOURCE_SHORT_LEN;
break;
}
cmd_type = pdu->data[addressing_fields_length];
switch(cmd_type)
{
case CMD_ASSOCIATION_REQUEST:
//check if association is allowed, if not discard the frame
////////printfUART("CMD_ASSOCIATION_REQUEST \n", "");
if (mac_PIB.macAssociationPermit == 0 )
{
////////////printfUART("Association not alowed\n", "");
if ( get_fc1_ack_request(pdu->frame_control1) == 1 )
{
build_ack(pdu->seq_num,0);
}
return;
}
if ( PANCoordinator==0 )
{
////////////printfUART("i�m not a pan\n", "");
return;
}
atomic{
source_long_ptr = (source_long *) &pdu->data[DEST_SHORT_LEN];
SrcAddr[1] =source_long_ptr->source_address0;
SrcAddr[0] =source_long_ptr->source_address1;
signal MLME_ASSOCIATE.indication(SrcAddr, pdu->data[addressing_fields_length+1] , 0, 0);
}
if ( get_fc1_ack_request(pdu->frame_control1) == 1 )
{
build_ack(pdu->seq_num,1);
}
break;
case CMD_ASSOCIATION_RESPONSE: atomic{
//printfUART("CMD_ASSOCIATION_RESPONSE\n", "");
associating =0;
call T_ResponseWaitTime.stop();
if ( get_fc1_ack_request(pdu->frame_control1) == 1 )
{
build_ack(pdu->seq_num,0);
}
signal MLME_ASSOCIATE.confirm((uint16_t)(pdu->data[addressing_fields_length+1] + (pdu->data[addressing_fields_length+2] << 8)), pdu->data[addressing_fields_length+3]);
}
break;
case CMD_DISASSOCIATION_NOTIFICATION: ////////////printfUART("Received CMD_DISASSOCIATION_NOTIFICATION\n", "");
if ( get_fc1_ack_request(pdu->frame_control1) == 1 )
{
build_ack(pdu->seq_num,0);
}
process_dissassociation_notification(pdu);
break;
case CMD_DATA_REQUEST:
////printfUART("CMD_DATA_REQUEST\n", "");
////////printfUART("DR\n", "");
if ( get_fc1_ack_request(pdu->frame_control1) == 1 )
{
//TODO
//Problems with consecutive reception of messages
//build_ack(pdu->seq_num,0);
}
//cmd_data_request_0_3_reception = (cmd_data_request_0_3 *) pdu->data;
source_long_ptr = (source_long *) &pdu->data[0];
SrcAddr[1] =source_long_ptr->source_address0;
SrcAddr[0] =source_long_ptr->source_address1;
send_ind_trans_addr(SrcAddr);
break;
case CMD_PANID_CONFLICT:
break;
case CMD_ORPHAN_NOTIFICATION:
////printfUART("CMD_ORPHAN_NOTIFICATION\n", "");
source_long_ptr = (source_long *) &pdu->data[DEST_SHORT_LEN];
SrcAddr[1] =source_long_ptr->source_address0;
SrcAddr[0] =source_long_ptr->source_address1;
signal MLME_ORPHAN.indication(SrcAddr, 0x00,0x00);
break;
case CMD_BEACON_REQUEST:
break;
case CMD_COORDINATOR_REALIGNMENT:
//printfUART("CMD_COORDINATOR_REALIGNMENT\n", "");
process_coordinator_realignment(pdu);
break;
case CMD_GTS_REQUEST:
//////////////printfUART("Received CMD_GTS_REQUEST\n", "");
if ( get_fc1_ack_request(pdu->frame_control1) == 1 )
{
build_ack(pdu->seq_num,0);
}
process_gts_request(pdu);
break;
default: break;
}
return;
}
void indication_ack(MPDU *pdu, int8_t ppduLinkQuality)
{
//decrement buffer count
atomic buffer_count --;
//////////////printfUART("ACK Received\n","");
atomic{
if (send_ack_check == 1 && ack_sequence_number_check == pdu->seq_num)
{
//transmission SUCCESS
call T_ackwait.stop();
send_buffer_count --;
send_buffer_msg_out++;
//failsafe
if(send_buffer_count > SEND_BUFFER_SIZE)
{
send_buffer_count =0;
send_buffer_msg_out=0;
send_buffer_msg_in=0;
}
if (send_buffer_msg_out == SEND_BUFFER_SIZE)
send_buffer_msg_out=0;
//received an ack for the association request
if( associating == 1 && association_cmd_seq_num == pdu->seq_num )
{
////////////printfUART("ASSOC ACK\n","");
call T_ResponseWaitTime.startOneShot(response_wait_time);
//call T_ResponseWaitTime.start(TIMER_ONE_SHOT, response_wait_time);
}
if (gts_request == 1 && gts_request_seq_num == pdu->seq_num)
{
call T_ResponseWaitTime.startOneShot(response_wait_time);
//call T_ResponseWaitTime.start(TIMER_ONE_SHOT, response_wait_time);
}
////////////printfUART("TRANSMISSION SUCCESS\n","");
if (send_indirect_transmission > 0 )
{ //the message send was indirect
//remove the message from the indirect transmission queue
indirect_trans_queue[send_indirect_transmission-1].handler=0x00;
indirect_trans_count--;
////////////printfUART("SU id:%i ct:%i\n", send_indirect_transmission,indirect_trans_count);
}
send_ack_check=0;
retransmit_count=0;
ack_sequence_number_check=0;
if (send_buffer_count > 0)
post send_frame_csma();
}
}
//CHECK
if (get_fc1_frame_pending(pdu->frame_control1) == 1 && pending_request_data ==1)// && associating == 1
{
////////////printfUART("Frame_pending\n","");
pending_request_data=0;
create_data_request_cmd();
}
//GTS mechanism, after the confirmation of the GTS request, must check if the beacon has the gts
/*
if (gts_ack == 1)
{
gts_ack=0;
gts_confirm=1;
call T_ResponseWaitTime.stop();
}
*/
if(gts_send_pending_data==1)
post start_gts_send();
if(coordinator_gts_send_pending_data==1 && coordinator_gts_send_time_slot == number_time_slot)
post start_coordinator_gts_send();
return;
}
void process_dissassociation_notification(MPDU *pdu)
{
atomic{
cmd_disassociation_notification *mac_disassociation_notification;
//creation of a pointer to the disassociation notification structure
mac_disassociation_notification = (cmd_disassociation_notification*) pdu->data;
signal MLME_DISASSOCIATE.indication(&mac_disassociation_notification->source_address0, mac_disassociation_notification->disassociation_reason, 0, 0);
}
return;
}
void process_coordinator_realignment(MPDU *pdu)
{
atomic{
cmd_coord_realignment *cmd_realignment = 0;
dest_long *dest_long_ptr=0;
source_short *source_short_ptr=0;
cmd_realignment = (cmd_coord_realignment*) &pdu->data[DEST_LONG_LEN + SOURCE_SHORT_LEN];
//creation of a pointer the addressing structures
dest_long_ptr = (dest_long *) &pdu->data[0];
source_short_ptr = (source_short *) &pdu->data[DEST_LONG_LEN];
mac_PIB.macCoordShortAddress = ((cmd_realignment->coordinator_short_address0 << 8) | cmd_realignment->coordinator_short_address0 );
mac_PIB.macShortAddress = cmd_realignment->short_address;
//printfUART("PCR %i %i\n",mac_PIB.macCoordShortAddress,mac_PIB.macShortAddress);
}
return;
}
/*****************************************************************************************************/
/*****************************************************************************************************/
/*****************************************************************************************************/
/************************ BUILD FRAMES FUNCTIONS **********************************/
/*****************************************************************************************************/
/*****************************************************************************************************/
/*****************************************************************************************************/
task void create_beacon()
{
int i=0;
uint8_t packet_length = 25;
int data_count=0;
int pending_data_index=0;
MPDU* pkt_ptr=0;
//pending frames
uint8_t short_addr_pending=0;
uint8_t long_addr_pending=0;
uint8_t gts_directions=0x00;
uint16_t frame_control;
atomic{
beacon_addr_short *mac_beacon_addr_short_ptr;
//mac_beacon_addr_short_ptr = (beacon_addr_short*) &mac_txmpdu.data[
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -