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

📄 macp.nc

📁 tinyos-2.x.rar
💻 NC
📖 第 1 页 / 共 5 页
字号:
/*
 * @author IPP HURRAY http://www.hurray.isep.ipp.pt/art-wise
 * @author Andre Cunha
 *
 */

#include <Timer.h>

#include "printfUART.h"

#include "frame_format.h"
#include "phy_const.h"

#include "mac_const.h"
#include "mac_enumerations.h"

#include "mac_func.h"

module MacP {

#ifndef TKN154_MAC 
	provides interface Init;
	
	provides interface MLME_START;
	provides interface MLME_SET;
    provides interface MLME_GET;
	
	provides interface MLME_ASSOCIATE;
	provides interface MLME_DISASSOCIATE;
	
	provides interface MLME_BEACON_NOTIFY;
	provides interface MLME_GTS;
	
	provides interface MLME_ORPHAN;
	
	provides interface MLME_SYNC;
	provides interface MLME_SYNC_LOSS;
		
	provides interface MLME_RESET;
	
	provides interface MLME_SCAN;
	
	//MCPS
	provides interface MCPS_DATA;
	provides interface MCPS_PURGE;
#endif
		
	
	uses interface Timer<TMilli> as T_ackwait;	
		
	uses interface Timer<TMilli> as	T_ResponseWaitTime;
	
	uses interface Timer<TMilli> as	T_ScanDuration;
	
	uses interface Leds;
	
	uses interface SplitControl as AMControl;
	
	uses interface Random;
	
	uses interface GeneralIO as CCA;
	
	
	//uses interface CC2420Config;
	
	uses interface AddressFilter;
	
	//uses interface Test_send;
	
	uses interface TimerAsync;
	
	uses interface PD_DATA;
	
	uses interface PLME_ED;
	uses interface PLME_CCA;
	uses interface PLME_SET;
	uses interface PLME_GET;
	uses interface PLME_SET_TRX_STATE;


  
}
implementation {
 
/*****************************************************/
/*				GENERAL 				 */
/*****************************************************/  
	/***************Variables*************************/
	//local extended address
	uint32_t aExtendedAddress0;
	uint32_t aExtendedAddress1;

	macPIB mac_PIB;

//If the the MLME receives a start request the node becomes a pan coordinator
	//and start transmiting beacons 
	bool PANCoordinator = 0;
	//(0 NO beacon transmission; 1 beacon transmission);
	bool Beacon_enabled_PAN = 0;
	
	//(RESET) when the reset command arrives it checks whether or not to reset the PIB
	bool SetDefaultPIB=0;
	
	//use security
	bool SecurityEnable=0;
	
	//others
	bool pending_reset=0;
	
	//transceiver status -every time the transceiver changes state this variable is updated
	uint8_t trx_status;
	
	//defines the transmission
	bool beacon_enabled=0;
	
	/***************Functions Definition***************/
	
	void init_MacPIB(); 
	
	uint8_t min(uint8_t val1, uint8_t val2);
	
	void init_MacCon();
	
	
	task void signal_loss();
	

	void create_data_request_cmd();
	void create_beacon_request_cmd();
	void create_gts_request_cmd(uint8_t gts_characteristics);
	
	void build_ack(uint8_t sequence,uint8_t frame_pending);
	
	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);




/*****************************************************/	
/*				Association 				         */
/*****************************************************/ 
	/***************Variables*************************/
	uint8_t associating = 0;
	uint8_t association_cmd_seq_num =0;
	
	/*association parameters*/
	
	uint8_t a_LogicalChannel;
	uint8_t a_CoordAddrMode;
	uint16_t a_CoordPANId;
	uint32_t a_CoordAddress[2];
	uint8_t a_CapabilityInformation;
	bool a_securityenable;
	
	/***************Functions Definition***************/		
	
	void create_association_request_cmd(uint8_t CoordAddrMode,uint16_t CoordPANId,uint32_t CoordAddress[],uint8_t CapabilityInformation);

	error_t create_association_response_cmd(uint32_t DeviceAddress[],uint16_t shortaddress, uint8_t status);
	
	void create_disassociation_notification_cmd(uint32_t DeviceAddress[],uint8_t disassociation_reason);
	
	void process_dissassociation_notification(MPDU *pdu);

/*****************************************************/
/*				Synchronization					 */
/*****************************************************/ 
	/***************Variables*************************/
	//(SYNC)the device will try to track the beacon ie enable its receiver just before the espected time of each beacon
	bool TrackBeacon=0;
	bool beacon_processed=0;
	//beacon loss indication
	uint8_t beacon_loss_reason;
	
	//(SYNC)the device will try to locate one beacon
	bool findabeacon=0;
	//(SYNC)number of beacons lost before sending a Beacon-Lost indication comparing to aMaxLostBeacons
	uint8_t missed_beacons=0;
	//boolean variable stating if the device is synchonized with the beacon or not
	uint8_t on_sync=0;
	
	uint32_t parent_offset=0x00000000;


/*****************************************************/
/*				GTS Variables					 */
/*****************************************************/  
	/***************Variables*************************/
	
	uint8_t gts_request=0;
	uint8_t gts_request_seq_num=0;
	
	bool gts_confirm;
	
	uint8_t GTS_specification;
	bool GTSCapability=1;
	
	uint8_t final_CAP_slot=15;
	
	//GTS descriptor variables, coordinator usage only
	GTSinfoEntryType GTS_db[7];
	uint8_t GTS_descriptor_count=0;
	uint8_t GTS_startslot=16;
	uint8_t GTS_id=0x01;


	//null gts descriptors
	GTSinfoEntryType_null GTS_null_db[7];
	
	uint8_t GTS_null_descriptor_count=0;
	//uint8_t GTS_null_id=0x01;
	
	//node GTS variables
	// 1 GTS for transmit
	uint8_t s_GTSss=0;           //send gts start slot
	uint8_t s_GTS_length=0;		 //send gts length
	//1 GTS for receive
	uint8_t r_GTSss=0;			 //receive gts start slot
	uint8_t r_GTS_length=0;		 //receive gts lenght
	
	//used to state that the device is on its transmit slot
	uint8_t on_s_GTS=0;
	//used to state that the device is on its receive slot
	uint8_t on_r_GTS=0;
	
	//used to determine if the next time slot is used for transmission
	uint8_t next_on_s_GTS=0;
	//used to determine if the next time slot is used for reception
	uint8_t next_on_r_GTS=0;
	
	//variable stating if the coordinator allow GTS allocations
	uint8_t allow_gts=1;
	
	//COORDINATOR GTS BUFFER 	
	gts_slot_element gts_slot_list[7];
	uint8_t available_gts_index[GTS_SEND_BUFFER_SIZE];
	uint8_t available_gts_index_count;
	
	uint8_t coordinator_gts_send_pending_data=0;
	uint8_t coordinator_gts_send_time_slot=0;
	
	//gts buffer used to store the gts messages both in COORDINATOR and NON COORDINATOR
	norace MPDU gts_send_buffer[GTS_SEND_BUFFER_SIZE];
	
	//NON PAN COORDINATOR BUFFER
	//buffering for sending
	uint8_t gts_send_buffer_count=0;
	uint8_t gts_send_buffer_msg_in=0;
	uint8_t gts_send_buffer_msg_out=0;
	uint8_t gts_send_pending_data=0;
	

	/***************Functions Definition***************/
	
	void process_gts_request(MPDU *pdu);	
	void init_available_gts_index();
	task void start_coordinator_gts_send();
	
	
	//GTS FUNCTIONS
	error_t remove_gts_entry(uint16_t DevAddressType);
	error_t add_gts_entry(uint8_t gts_length,bool direction,uint16_t DevAddressType);
	error_t add_gts_null_entry(uint8_t gts_length,bool direction,uint16_t DevAddressType);
	
	//increment the idle GTS for GTS deallocation purposes, not fully implemented yet
	task void increment_gts_null();
	
	task void start_gts_send();
	
	
	
	//initialization functions
	void init_gts_slot_list();
	void init_GTS_null_db();
	
	void init_GTS_db();


	uint32_t calculate_gts_expiration();
	task void check_gts_expiration();


/*****************************************************/
/*				CHANNEL SCAN Variables				 */
/*****************************************************/ 
	//current_channel
	uint8_t current_channel=0;

	/***************Variables*************************/
	//ED-SCAN variables
	
	bool scanning_channels;
	
	uint32_t channels_to_scan;
	uint8_t current_scanning=0;
	//uint8_t scan_count=0;
	uint8_t scanned_values[16];
	uint8_t scan_type;
	
	SCAN_PANDescriptor scan_pans[16];
	
	uint16_t scan_duration;
	
	task void data_channel_scan_indication();
	
/*****************************************************/
/*				TIMER VARIABLES					 */
/*****************************************************/  
	/***************Variables*************************/
	uint32_t response_wait_time;
	
	//Beacon Interval
	uint32_t BI;
	//Superframe duration
	uint32_t SD;
	
	//timer variables
	uint32_t time_slot; //backoff boundary timer
	uint32_t backoff;  //backoff timer
	
	//current number of backoffs in the active period
	uint8_t number_backoff=1;
	uint8_t number_time_slot=0;
	
	bool csma_slotted=0;
/*****************************************************/
/*				CSMA VARIABLES					 */
/*****************************************************/  	
	/***************Variables*************************/	

	//DEFERENCE CHANGE
	uint8_t cca_deference = 0;
	uint8_t backoff_deference = 0;
	uint8_t check_csma_ca_backoff_send_conditions(uint32_t delay_backoffs);

	//STEP 2
	uint8_t delay_backoff_period;
	bool csma_delay=0;
	
	bool csma_locate_backoff_boundary=0;
	
	bool csma_cca_backoff_boundary=0;
	
	//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.
	bool performing_csma_ca=0;
	
	//CSMA-CA variables
	uint8_t BE; //backoff exponent
	uint8_t CW; //contention window (number of backoffs to clear the channel)
	uint8_t NB; //number of backoffs

	/***************Functions Definition***************/	
	
	void init_csma_ca(bool slotted);
	void perform_csma_ca();
	task void perform_csma_ca_unslotted();
	task void perform_csma_ca_slotted();
	//task void start_csma_ca_slotted();
	
/*****************************************************/
/*				Indirect Transmission buffers		 */
/*****************************************************/
	/***************Variables*************************/	
	//indirect transmission buffer
	norace indirect_transmission_element indirect_trans_queue[INDIRECT_BUFFER_SIZE];
	//indirect transmission message counter
	uint8_t indirect_trans_count=0;

	/***************Functions Definition***************/	
	
	//function used to initialize the indirect transmission buffer
	void init_indirect_trans_buffer();
	//function used to search and send an existing indirect transmission message
	void send_ind_trans_addr(uint32_t DeviceAddress[]);
	//function used to remove an existing indirect transmission message
	error_t remove_indirect_trans(uint8_t handler);
	//function used to increment the transaction persistent time on each message
	//if the transaction time expires the messages are discarded
	void increment_indirect_trans();
	
/*****************************************************/
/*				RECEIVE buffers		                  */
/*****************************************************/  
	/***************Variables*************************/
	
	//buffering variables
	norace MPDU buffer_msg[RECEIVE_BUFFER_SIZE];
	int current_msg_in=0;
	int current_msg_out=0;
	int buffer_count=0;

	/***************Functions Definition***************/	
	
	task void data_indication();
	
	void indication_cmd(MPDU *pdu, int8_t ppduLinkQuality);
	void indication_ack(MPDU *pdu, int8_t ppduLinkQuality);
	void indication_data(MPDU *pdu, int8_t ppduLinkQuality);
/*****************************************************/
/*				RECEPTION AND TRANSMISSION		                  */
/*****************************************************/  
	
	/***************Variables*************************/
	
	//buffering for sending
	norace MPDUBuffer send_buffer[SEND_BUFFER_SIZE];
	uint8_t send_buffer_count=0;
	uint8_t send_buffer_msg_in=0;
	uint8_t send_buffer_msg_out=0;
	
	//retransmission information
	uint8_t send_ack_check;//ack requested in the transmitted frame
	uint8_t retransmit_count;//retransmission count
	uint8_t ack_sequence_number_check;//transmission sequence number
	uint8_t send_retransmission;
	uint8_t send_indirect_transmission;

	uint8_t pending_request_data=0;
	
	uint8_t ackwait_period;
	
	uint8_t link_quality;
	
	norace ACK mac_ack;
	ACK *mac_ack_ptr;
	
	uint32_t gts_expiration;

	uint8_t I_AM_IN_CAP=0;
	uint8_t I_AM_IN_CFP=0;
	uint8_t I_AM_IN_IP=0;
	
	/***************Functions Definition***************/	
	
	task void send_frame_csma();
	
	uint8_t check_csma_ca_send_conditions(uint8_t frame_length,uint8_t frame_control1);

	uint8_t check_gts_send_conditions(uint8_t frame_length);
	
	uint8_t calculate_ifs(uint8_t pk_length);



/*****************************************************/
/*				BEACON MANAGEMENT  		              */
/*****************************************************/ 
	/***************Variables*************************/
	norace MPDU mac_beacon_txmpdu;
	MPDU *mac_beacon_txmpdu_ptr;
	
	uint8_t *send_beacon_frame_ptr;
	uint8_t send_beacon_length;

	/***************Functions Definition***************/	
	/*function to create the beacon*/
	task void create_beacon();

⌨️ 快捷键说明

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