📄 macp.nc
字号:
//DEFERENCE CHANGE
if (backoff_deference == 0)
{
//normal situation
delay_backoff_period = (call Random.rand16() & ((uint8_t)(powf(2,BE)) - 1));
if (check_csma_ca_backoff_send_conditions((uint32_t) delay_backoff_period) == 1)
{
backoff_deference = 1;
}
}
else
{
backoff_deference = 0;
}
csma_delay=1;
}
if( csma_cca_backoff_boundary == 1 )
post perform_csma_ca_slotted();
}
//CSMA/CA
atomic{
if(csma_delay == 1 )
{
if (delay_backoff_period == 0)
{
if(csma_slotted == 0)
{
post perform_csma_ca_unslotted();
}
else
{
//CSMA/CA SLOTTED
csma_delay=0;
csma_cca_backoff_boundary=1;
}
}
delay_backoff_period--;
}
}
number_backoff++;
return SUCCESS;
}
/*******************T_ackwait**************************/
event void T_ackwait.fired() {
//////////printfUART("Tfd \n", "");
//call Leds.redToggle();
if (send_ack_check == 1)
{
retransmit_count++;
if (retransmit_count == aMaxFrameRetries || send_indirect_transmission > 0)
{
//check the type of data being send
/*
if (associating == 1)
{
//printfUART("af ack\n", "");
associating=0;
signal MLME_ASSOCIATE.confirm(0x0000,MAC_NO_ACK);
}
*/
atomic{
////////////printfUART("TRANSMISSION FAIL\n","");
//stardard procedure, if fail discard the packet
atomic send_buffer_count --;
send_buffer_msg_out++;
//failsafe
if(send_buffer_count > SEND_BUFFER_SIZE)
{
atomic 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;
if (send_buffer_count > 0)
post send_frame_csma();
send_ack_check=0;
retransmit_count=0;
ack_sequence_number_check=0;
}
}
////////////printfUART("RETRY\n","");
//retransmissions
post send_frame_csma();
}
}
/*******************T_ResponseWaitTime**************************/
event void T_ResponseWaitTime.fired() {
//command response wait time
////////////printfUART("T_ResponseWaitTime.fired\n", "");
if (associating == 1)
{
//printfUART("af rwt\n", "");
associating=0;
signal MLME_ASSOCIATE.confirm(0x0000,MAC_NO_DATA);
}
}
/*****************************************************
****************PD_DATA EVENTS***********************
******************************************************/
async event error_t PD_DATA.confirm(uint8_t status) {
return SUCCESS;
}
/*****************************************************
**************** PD_DATA ********************
******************************************************/
async event error_t PD_DATA.indication(uint8_t psduLenght,uint8_t* psdu, int8_t ppduLinkQuality){
//if(I_AM_IN_CAP == 1 || I_AM_IN_CFP == 1 || mac_PIB.macShortAddress==0xffff || scanning_channels ==1 || findabeacon == 1)
//{
if (buffer_count > RECEIVE_BUFFER_SIZE)
{
//call Leds.redToggle();
//printfUART("full\n","");
}
else
{
memcpy(&buffer_msg[current_msg_in],psdu,sizeof(MPDU));
atomic{
current_msg_in++;
if ( current_msg_in == RECEIVE_BUFFER_SIZE )
current_msg_in = 0;
buffer_count ++;
}
link_quality = ppduLinkQuality;
if (scanning_channels ==1)
{
//channel scan operation, accepts beacons only
post data_channel_scan_indication();
}
else
{
//normal operation
post data_indication();
}
}
//}
//else
//{
// //printfUART("drop\n","");
//}
return SUCCESS;
}
task void data_indication()
{
//check all the conditions for a receiver packet
//pag 155
uint8_t link_qual;
atomic link_qual = link_quality;
////printfUART("data_indication\n","");
//////////printfUART("buf %i %i\n",buffer_count,indirect_trans_count);
//Although the receiver of the device is enabled during the channel assessment portion of this algorithm, the
//device shall discard any frames received during this time.
//////////////printfUART("performing_csma_ca: %i\n",performing_csma_ca);
if (performing_csma_ca == 1)
{
//////////////printfUART("REJ CSMA\n","");
atomic{
buffer_count--;
current_msg_out++;
if ( current_msg_out == RECEIVE_BUFFER_SIZE )
current_msg_out = 0;
}
return;
}
//while performing channel scan disable the packet reception
if ( scanning_channels == 1)
{
atomic{
buffer_count--;
current_msg_out++;
if ( current_msg_out == RECEIVE_BUFFER_SIZE )
current_msg_out = 0;
}
return;
}
atomic{
//////printfUART("data ind %x %x %i\n",buffer_msg[current_msg_out].frame_control1,buffer_msg[current_msg_out].frame_control2,(buffer_msg[current_msg_out].frame_control2 & 0x7));
//check the frame type of the received packet
switch( (buffer_msg[current_msg_out].frame_control1 & 0x7) )
{
case TYPE_DATA: ////printfUART("rd %i\n",buffer_msg[current_msg_out].seq_num);
indication_data(&buffer_msg[current_msg_out],link_qual);
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;
}
/*****************************************************
****************PLME_ED EVENTS***********************
******************************************************/
event error_t PLME_CCA.confirm(uint8_t status){
return SUCCESS;
}
event error_t PLME_SET.confirm(uint8_t status, uint8_t PIBAttribute){
return SUCCESS;
}
async event error_t PLME_SET_TRX_STATE.confirm(uint8_t status){
return SUCCESS;
}
event error_t PLME_GET.confirm(uint8_t status,uint8_t PIBAttribute, uint8_t PIBAttributeValue){
return SUCCESS;
}
event error_t PLME_ED.confirm(uint8_t status,int8_t EnergyLevel){
return SUCCESS;
}
/*******************************************************************************************************/
/*******************************************************************************************************/
/*******************************************************************************************************/
/****************************************PACKET PROCESSING FUNCTIONS************************************/
/*******************************************************************************************************/
/*******************************************************************************************************/
/*******************************************************************************************************/
void process_beacon(MPDU *packet,uint8_t ppduLinkQuality)
{
/*
ORGANIZE THE PROCESS BEACON FUNCION AS FOLLOWS.
1- GET THE BEACON ORDER
2- GET THE SUPERFRAME ORDER
3- GET THE FINAL CAP SLOT
4 - COMPUTE SD, BI, TS, BACKOFF PERIOD IN MILLISECONDS
4- SYNCHRONIZE THE NODE BY DOING THE FOLLOWING
- SET A TIMER IN MS FOR THE FINAL TIME SLOT (SUPERFRAME DURATION) : IT EXPRIES AFTER SD - TX TIME - PROCESS TIME
- SET A TIMER IN MS FOR THE GTS IF ANY EXIST IT EXPRIES AFTER GTS_NBR * TIME_SLOT - TX TIME - PROCESS TIME
*/
uint32_t SO_EXPONENT;
uint32_t BO_EXPONENT;
int i=0;
uint16_t gts_descriptor_addr;
uint8_t data_count;
uint8_t gts_directions;
uint8_t gts_des_count;
uint8_t gts_ss;
uint8_t gts_l;
uint8_t dir;
uint8_t dir_mask;
//end gts variables
//function that processes the received beacon
beacon_addr_short *beacon_ptr;
PANDescriptor pan_descriptor;
// beacon_trans_delay = (((packet->length(bytes) * 8.0) / 250.00(bits/s) ) / 0.34(s) (timer_granularity) ) (symbols)
//pending frames
uint8_t short_addr_pending=0;
uint8_t long_addr_pending=0;
//used in the synchronization
//uint32_t process_tick_counter; //symbols
//uint32_t becon_trans_delay; //symbols
//uint32_t start_reset_ct; //number of clock ticks since the start of the beacon interval
//used in the track beacon
beacon_processed = 1;
missed_beacons=0;
//initializing pointer to data structure
beacon_ptr = (beacon_addr_short*) (packet->data);
//decrement buffer count
atomic buffer_count --;
////printfUART("Received Beacon\n","");
////printfUART("rb panid: %x %x \n",beacon_ptr->source_address,mac_PIB.macCoordShortAddress);
////////printfUART("My macPANID: %x\n",mac_PIB.macPANId);
if( beacon_ptr->source_address != mac_PIB.macCoordShortAddress)
{
//
return;
}
////printfUART("bea %i\n",call TimerAsync.get_current_ticks());
/**********************************************************************************/
/* PROCESSING THE SUPERFRAME STRUCTURE */
/**********************************************************************************/
if (PANCoordinator == 0)
{
mac_PIB.macBeaconOrder = get_beacon_order(beacon_ptr->superframe_specification);
mac_PIB.macSuperframeOrder = get_superframe_order(beacon_ptr->superframe_specification);
//mac_PIB.macCoordShortAddress = beacon_ptr->source_address;
////printfUART("BO,SO:%i %i\n",mac_PIB.macBeaconOrder,mac_PIB.macSuperframeOrder);
//mac_PIB.macPANId = beacon_ptr->source_PAN_identifier;
//beacon order check if it changed
if (mac_PIB.macSuperframeOrder == 0)
{
SO_EXPONENT = 1;
}
else
{
SO_EXPONENT = powf(2,mac_PIB.macSuperframeOrder);
}
if ( mac_PIB.macBeaconOrder ==0)
{
BO_EXPONENT =1;
}
else
{
BO_EXPONENT = powf(2,mac_PIB.macBeaconOrder);
}
BI = aBaseSuperframeDuration * BO_EXPONENT;
SD = aBaseSuperframeDuration * SO_EXPONENT;
//backoff_period
backoff = aUnitBackoffPeriod;
time_slot = SD / NUMBER_TIME_SLOTS;
call TimerAsync.set_bi_sd(BI,SD);
}
/**********************************************************************************/
/* PROCESS GTS CHARACTERISTICS */
/**********************************************************************************/
allow_gts =1;
//initializing the gts variables
s_GTSss=0;
s_GTS_length=0;
r_GTSss=0;
r_GTS_length=0;
/*
send_s_GTSss=0;
send_r_GTSss=0;
send_s_GTS_len=0;
send_r_GTS_len=0;
*/
final_CAP_slot = 15;
gts_des_count = (packet->data[8] & 0x0f);
data_count = 9;
final_CAP_slot = 15 - gts_des_count;
if (gts_des_count > 0 )
{
data_count = 10; //position of the current data count
//process descriptors
gts_directions = packet->data[9];
////printfUART("gts_directions:%x\n",gts_directions);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -