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

📄 macp.nc

📁 tinyos-2.x.rar
💻 NC
📖 第 1 页 / 共 5 页
字号:
	/*function to process the beacon information*/
	void process_beacon(MPDU *packet,uint8_t ppduLinkQuality);
	

/*****************************************************/
/*	 Fault tolerance functions            */
/*****************************************************/ 
//FAULT-TOLERANCE	
	void create_coordinator_realignment_cmd(uint32_t device_extended0, uint32_t device_extended1, uint16_t device_short_address);

	void create_orphan_notification();
	
	void process_coordinator_realignment(MPDU *pdu);

	/*******************************************/
	/*******BEACON SCHEDULING IMPLEMENTATION****/
	/*******************************************/
	
	//bolean that checks if the device is in the parent active period
	uint8_t I_AM_IN_PARENT_CAP = 0;
	
	
	//upstream buffer
	norace MPDUBuffer upstream_buffer[UPSTREAM_BUFFER_SIZE];
	uint8_t upstream_buffer_count=0;
	uint8_t upstream_buffer_msg_in=0;
	uint8_t upstream_buffer_msg_out=0;

	uint8_t sending_upstream_frame=0;
	
	task void send_frame_csma_upstream();

/***************************DEBUG FUNCTIONS******************************/
/* This function are list functions with the purpose of debug, to use then uncomment the declatarion
on top of this file*/
/*
	void list_mac_pib();
	
	void list_gts();
	
	void list_my_gts();
	void list_gts_null();
	*/
	//list all the handles in the indirect transmission buffer, debug purposes
	void list_indirect_trans_buffer();	
			
/***************************END DEBUG FUNCTIONS******************************/


/***************** Init Commands ****************/
  command error_t Init.init() {
  
  call AMControl.start();


	//initialization of the beacon structure
	mac_beacon_txmpdu_ptr = &mac_beacon_txmpdu;



   atomic{

	//inicialize the mac PIB
	init_MacPIB();
	
	init_GTS_db();
	
	init_GTS_null_db();
	
	init_gts_slot_list();
	
	init_available_gts_index();
	
	aExtendedAddress0=TOS_NODE_ID;
	aExtendedAddress1=TOS_NODE_ID;
	


	call AddressFilter.set_address(mac_PIB.macShortAddress, aExtendedAddress0, aExtendedAddress1);

	call AddressFilter.set_coord_address(mac_PIB.macCoordShortAddress, mac_PIB.macPANId);
	

											
	init_indirect_trans_buffer();


	}
	
	//beacon
	mac_beacon_txmpdu_ptr = &mac_beacon_txmpdu;
		
	//ack
	mac_ack_ptr = &mac_ack;
	
	//Other timers, sync timers units expressed in miliseconds
	ackwait_period = ((mac_PIB.macAckWaitDuration * 4.0 ) / 250.0) * 3;

	response_wait_time = ((aResponseWaitTime * 4.0) / 250.0) * 2;

	atomic{
	
		
		BI = aBaseSuperframeDuration * powf(2,mac_PIB.macBeaconOrder);
		SD = aBaseSuperframeDuration * powf(2,mac_PIB.macSuperframeOrder);
		
		
		//backoff_period
		backoff = aUnitBackoffPeriod;
		//backoff_period_boundary
		
		time_slot = SD / NUMBER_TIME_SLOTS;
			
		call TimerAsync.set_enable_backoffs(1);	
		call TimerAsync.set_backoff_symbols(backoff);
		
		call TimerAsync.set_bi_sd(BI,SD);
		
		call TimerAsync.start();
	}


printfUART_init();

    return SUCCESS;
  }

  event void AMControl.startDone(error_t err) {
    if (err == SUCCESS) {
     	
	call TimerAsync.start();
		
	}
    else {
      call AMControl.start();
    }
  }

  event void AMControl.stopDone(error_t err) {
  }


/*****************************************************/
/*				TIMERS FIRED       					 */
/*****************************************************/ 

async event error_t TimerAsync.before_bi_fired()
{
	//printfUART("bbi %i\n",call TimerAsync.get_current_ticks());
		
	if (mac_PIB.macBeaconOrder != mac_PIB.macSuperframeOrder )
	{
		if ( Beacon_enabled_PAN == 1 )
		{
			//
			//post set_trx();
			trx_status = PHY_TX_ON;
			call PLME_SET_TRX_STATE.request(PHY_TX_ON);
		}
		else
		{
			//
			//post set_trx();
			trx_status = PHY_RX_ON;
			call PLME_SET_TRX_STATE.request(PHY_RX_ON);
		}
	}
	
	//I_AM_IN_CAP = 1;
	findabeacon = 1;
		
	return SUCCESS;
}

/*******************Timer BEACON INTERVAL******************/
async event error_t TimerAsync.bi_fired()
{
	call Leds.led2On();
	//call Test_send.send();
	
	I_AM_IN_CAP = 1;
	I_AM_IN_IP = 0;
	
	//printfUART("bi\n","");
	
	
	if ( Beacon_enabled_PAN == 1 )
	{
		//the beacon is send directly without CSMA/CA
		call PD_DATA.request(send_beacon_length,send_beacon_frame_ptr);
	}

	number_backoff =0;
	number_time_slot=0;
	
	
	//CHECK there is the need to wait a small amount of time before checking if the beacon as been processed or not
	//possible solition, if it receives a packet stand by for confirmation it its a beacon
	//The device must always receive the beacon
	if (TrackBeacon == 1) 
	{
		if (beacon_processed==1) 
		{
			beacon_processed=0;
		}
		else
		{
			//dealocate all GTS
			//beacon loss

			on_sync =0;
			beacon_loss_reason = MAC_BEACON_LOSS;
			
			//TODO
			//post signal_loss();
		}
	}
	
	post send_frame_csma();

	return SUCCESS;
}

/*******************Timer SUPERFRAME DURATION******************/
async event error_t TimerAsync.sd_fired()
{
	call Leds.led2Off();

	//printfUART("sd\n","");

	I_AM_IN_CFP = 0;
	I_AM_IN_IP = 1;
	
	
	number_backoff=0;
	number_time_slot=0;
	
	
	if (PANCoordinator == 0 && TYPE_DEVICE == ROUTER)
	{
		trx_status = PHY_RX_ON;
	
		call PLME_SET_TRX_STATE.request(PHY_RX_ON);
	}
	else
	{
		trx_status = PHY_RX_ON;
		
		call PLME_SET_TRX_STATE.request(PHY_RX_ON);
		
	}
	
	if (mac_PIB.macShortAddress==0xffff && TYPE_DEVICE == END_DEVICE)
	{
		trx_status = PHY_RX_ON;
		
		call PLME_SET_TRX_STATE.request(PHY_RX_ON);
	}
	
	//trx_status = PHY_RX_ON;
	//post set_trx();
	/*
		//turn the transceiver off
	if (mac_PIB.macBeaconOrder != mac_PIB.macSuperframeOrder )
	{
		if ( mac_PIB.macRxOnWhenIdle == 0 && findabeacon == 0) 
		{
			trx_status = PHY_TRX_OFF;
			post set_trx();
		}
		else
		{
			trx_status = PHY_RX_ON;
			post set_trx();
		}
	}
	//if the node is trying to synchronize
		if (on_sync == 0 || mac_PIB.macPromiscuousMode == 1)
	{
		atomic{
			trx_status = PHY_RX_ON;
			post set_trx();
		}
	}
	*/
	if (PANCoordinator == 1)
	{
		//increment the gts_null descriptors
		atomic{
			
				//if (GTS_null_descriptor_count > 0) post increment_gts_null();
			
				//if (GTS_descriptor_count >0 ) post check_gts_expiration();
			
				//if (indirect_trans_count > 0) increment_indirect_trans();
				
				//creation of the beacon
				post create_beacon();
		}
		//trx_status = PHY_TRX_OFF;
		//post set_trx();
	}
	else
	{
	//temporariamente aqui //aten��o quando for para o cluster-tree � preciso mudar para fora
	//e necessario destinguir ZC de ZR (que tem que manter a sync com o respectivo pai)
	if (on_sync == 0)
	{
		//sync not ok
		
		//findabeacon=1;
		if (missed_beacons == aMaxLostBeacons)
		{
		
			printfUART("sync_loss %i\n",missed_beacons);
			//out of sync
			post signal_loss();
		}
		//printfUART("out_sync %i\n",missed_beacons);
		missed_beacons++;
		call Leds.led1Off();
		
	}
	else
	{
		//sync ok
		missed_beacons=0;
	
		on_sync=0;
	}
	
	}

	//trx_status = PHY_TRX_OFF;
	//call PLME_SET_TRX_STATE.request(PHY_TRX_OFF);

	return SUCCESS;
}

/*******************Timer BEFORE TIME SLOT FIRED******************/
async event error_t TimerAsync.before_time_slot_fired()
{
	on_s_GTS=0;
	on_r_GTS=0;
	
	if (next_on_s_GTS == 1)
	{	
		on_s_GTS=1;
		next_on_s_GTS =0;
		trx_status = PHY_TX_ON;
		call PLME_SET_TRX_STATE.request(PHY_TX_ON);
		//post set_trx();
	}
	
	if(next_on_r_GTS == 1)
	{
		on_r_GTS=1;
		next_on_r_GTS=0;
		trx_status = PHY_RX_ON;
		call PLME_SET_TRX_STATE.request(PHY_RX_ON);
		//post set_trx();
	}

return SUCCESS;
}
/*******************Timer TIME SLOT FIRED******************/
async event error_t TimerAsync.time_slot_fired()
{
	//reset the backoff counter and increment the slot boundary
	number_backoff=0;
	number_time_slot++;
		
	//verify is there is data to send in the GTS, and try to send it
	if(PANCoordinator == 1 && GTS_db[15-number_time_slot].direction == 1 && GTS_db[15-number_time_slot].gts_id != 0)
	{
		//COORDINATOR SEND DATA
		//////////printfUART("bbck%i:%i:%i\n", (15-number_time_slot),GTS_db[15-number_time_slot].direction,gts_slot_list[15-number_time_slot].element_count);
		
		post start_coordinator_gts_send();
				
	}
	else
	{
		//DEVICE SEND DATA
		if (number_time_slot == s_GTSss && gts_send_buffer_count > 0 && on_sync == 1)//(send_s_GTSss-send_s_GTS_len) 
		{
				//current_time = call TimerAsync.get_total_tick_counter();
				post start_gts_send();
		}		
	}
	
	next_on_r_GTS =0;
	next_on_s_GTS=0;
	
	
	//printfUART("ts%i %i %i\n", number_time_slot,s_GTSss,r_GTSss);
	
	
	//verification if the time slot is entering the CAP
	//GTS FIELDS PROCESSING
	
	if ((number_time_slot + 1) >= final_CAP_slot && (number_time_slot + 1) < 16)
	{
		I_AM_IN_CAP = 0;
		I_AM_IN_CFP = 1;
		
		//printfUART("bts %i\n",I_AM_IN_CAP, number_time_slot); 
	
	atomic{
		
		//verification of the next time slot
		if(PANCoordinator == 1 && number_time_slot < 15)
		{
		//COORDINATOR verification of the next time slot
			if(GTS_db[14-number_time_slot].gts_id != 0x00 && GTS_db[14-number_time_slot].DevAddressType != 0x0000)
			{	
				if(GTS_db[14-number_time_slot].direction == 1 ) // device wants to receive
				{
					next_on_s_GTS =1; //PAN coord mode
				}
				else
				{
					next_on_r_GTS=1; //PAN coord mode
				}
			}	
		}
		else
		{
		//device verification of the next time slot
			if( (number_time_slot +1) == s_GTSss || (number_time_slot +1) == r_GTSss )
			{
				//printfUART("s_GTSss: %i r_GTSss: %i\n", s_GTSss,r_GTSss);
				if((number_time_slot + 1) == s_GTSss)
				{	
					//printfUART("MY SEND SLOT \n", "");
					next_on_s_GTS =1;
					s_GTS_length --;
					if (s_GTS_length != 0 )
					{
						s_GTSss++;
					}
				}
				else			
				{
					////////////printfUART("MY RECEIVE SLOT \n", "");
					next_on_r_GTS =1;
					r_GTS_length --;
					if (r_GTS_length != 0 )
					{
						r_GTSss++;
					}
				}				
			}
			else
			{
				//idle
				next_on_s_GTS=0;
				next_on_r_GTS=0;

⌨️ 快捷键说明

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