📄 macm.nc.svn-base
字号:
/*
* @author IPP HURRAY http://www.hurray.isep.ipp.pt/art-wise
* @author Andre Cunha
*
*/
module MacM
{
provides {
interface StdControl;
//MLME
interface MLME_START;
interface MLME_ASSOCIATE;
interface MLME_DISASSOCIATE;
interface MLME_SYNC;
interface MLME_SYNC_LOSS;
interface MLME_SCAN;
interface MLME_RESET;
interface MLME_BEACON_NOTIFY;
interface MLME_COMM_STATUS;
interface MLME_SET;
interface MLME_GET;
interface MLME_GTS;
//MCPS
interface MCPS_DATA;
interface MCPS_PURGE;
}
uses {
interface StdControl as Phy_control;
interface StdControl as Mac_control;
interface PD_DATA;
//PLME-SAP
interface PLME_CCA;
interface PLME_SET;
interface PLME_SET_TRX_STATE;
interface PLME_GET;
interface PLME_ED;
interface Random;
interface Time;
/*LEDS*///TEMP VAR
interface Leds;
/*****************************************************/
/* TIMERS */
/*****************************************************/
//Timer ScanDuration
interface Timer as T_scan_duration;
//Response wait time timer
interface Timer as T_ResponseWaitTime;
//wait for an acknouledgment
interface Timer as T_ackwait;
//Beacon tracking IMPLEMENT
//interface Timer as T_btp;
//Carrier Sense Timer
interface Timer as T_cca;
interface TimerAsync;
}
}
implementation
{
//local extended address
uint32_t aExtendedAddress0;
uint32_t aExtendedAddress1;
macPIB mac_PIB;
uint8_t csma_backoff_counter=0;
uint8_t csma_backoff_counter_inst=0;
/****************************************************/
/* Association */
/*****************************************************/
uint8_t associating = 0;
uint8_t association_retries=0;
uint8_t a_LogicalChannel;
uint8_t a_CoordAddrMode;
uint16_t a_CoordPANId;
uint32_t a_CoordAddress[2];
uint8_t a_CapabilityInformation;
bool a_securityenable;
/*****************************************************/
/* Synchronization */
/*****************************************************/
uint32_t receive_tick_counter;
//(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;
uint8_t on_sync;
/*****************************************************/
/* GTS Variables */
/*****************************************************/
//GTS variables
bool gts_ack;
bool gts_confirm;
//gts alocation e dealocation variables
//uint8_t GTS_request_type;//UNUSED
uint8_t GTS_specification;
bool GTSCapability=1;
uint8_t final_CAP_slot=15;
GTSinfoEntryType GTS_db[7];
uint8_t GTS_descriptor_count=0;
uint8_t GTS_startslot=17;
uint8_t GTS_id=0x01;
//GTS FUNCTIONS
result_t remove_gts_entry(uint16_t DevAddressType);
result_t add_gts_entry(uint8_t gts_length,bool direction,uint16_t DevAddressType);
void list_gts();
void init_GTS_db();
void list_my_gts();
//null gts descriptors
GTSinfoEntryType_null GTS_null_db[7];
uint8_t GTS_null_descriptor_count=0;
//uint8_t GTS_null_id=0x01;
void init_GTS_null_db();
void list_gts_null();
result_t add_gts_null_entry(uint8_t gts_length,bool direction,uint16_t DevAddressType);
task void increment_gts_null();
//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
uint8_t on_s_GTS=0;
uint8_t on_r_GTS=0;
gts_buffer_element gts_buffer_list[MAX_GTS_BUFFER];
uint8_t gts_buffer_count=0;
uint8_t gts_index_to_send;
MPDU *gts_frame_to_send;
void clear_gts_buffer();
void add_gts_buffer_element(uint8_t *frame_ptr,uint8_t pan_coord,uint8_t send_slot);
int transmit_gts_buffer_element(int start_i,uint8_t send_slot);
/*****************************************************/
/* CHANNEL SCAN 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;
uint8_t scan_duration;
/*****************************************************/
/* Boolean Variables */
/*****************************************************/
//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;
//unused so far
/*
bool AssocMember;
bool beacon_request;
bool ble;
bool broadcast;
bool co_re;
bool decode_successful;
bool findcoordinator_realignment;
bool FFD_device;
bool InACL;
bool key_found;
bool msdu_found;
bool notcorrect;
bool passed;
bool PHY_in_TX;
bool r_acle;
bool r_sec;
bool sec_proc;
bool too_late;
bool too_long;
bool will_fit;
*/
//others
bool pending_reset=0;
bool first=1;
/*****************************************************/
/* Integer Variables */
/*****************************************************/
uint32_t response_wait_time;
//Beacon Interval
uint32_t BI;
//Superframe duration
uint32_t SD;
uint8_t trx_status;
//defines the transmission
bool beacon_enabled=0;
bool csma_slotted=0;
//CSMA/CA Variables
//timer variables
uint32_t bb; //backoff boundary timer
uint32_t b; //backoff timer
//STEP 2
uint8_t delay_backoff_period;
bool csma_delay=0;
//transmit frame pointer
uint8_t *transmit_frame_ptr;
uint8_t transmit_frame_length;
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
//current number of backoffs in the active period
uint8_t number_b=1;
uint8_t number_bb=0;
//current_channel
uint8_t current_channel=0;
//first channel available
uint8_t original_channel = 0x0B;
// CHECK dont forget to include the variables in the init function
//unused so far
/*
uint8_t AssocDevAddr;
uint8_t cam;
uint8_t clc;
uint8_t cpid;
uint8_t co_PID;
uint8_t co_shortaddr;
uint8_t current_channel;
uint8_t dam;
uint8_t data_request;
uint8_t RxOnTime;
uint8_t DisassociateReason;
uint8_t dpid;
uint8_t etbd;
uint8_t iPANcoordinator;
//uint8_t length;
uint8_t ml;
uint8_t vmlq;
uint8_t num_GTSs;
uint8_t numCommFailures;
uint8_t numPend;
uint8_t PANID;
uint8_t resultlistsize;
uint8_t r_sn;
uint8_t r_dam;
uint8_t r_dpid;
uint8_t r_sam;
uint8_t r_spid;
uint8_t RxOnDuration;
uint8_t sam;
uint8_t spid;
uint8_t statA;
uint8_t strength;
uint8_t beaconOrder;
uint8_t SuperframeOrder;
*/
//////////////////////////////////////////////////////////////
//////////////////Indirect Transmission buffers///////////////
/////////////////////////////////////////////////////////////
/*transmission variables*/
bool indirect_trans_status_pending=0;
uint8_t indirect_trans_pending_handler=0;
MPDU *ind_frame_ptr;
uint8_t indirect_trans_size = 2;
norace indirect_transmission_element indirect_trans_queue[2];
uint8_t indirect_trans_count=0;
uint8_t short_addr_pending=0;
uint8_t long_addr_pending=0;
//////////////////////////////////////////////////////////////
////////////////////////////Buffering////////////////////////
/////////////////////////////////////////////////////////////
//buffering variables
int buffer_size = 5;
norace MPDU buffer_msg[5];
int current_msg_in=0;
int current_msg_out=0;
int buffer_count=0;
task void message_out();
task void message_in();
//////////////////////////////////////////////////////////////
////////////////////////////RECEPTION AND SENDING////////////
/////////////////////////////////////////////////////////////
//used in sending
norace MPDU mac_txmpdu;
MPDU *mac_txmpdu_ptr;
uint8_t ackwait_period;
bool ack_received;
bool ack_wait;
uint8_t link_quality;
//used in reception
cmd_association_request_short *cmd_association_reception_short_ptr;
cmd_association_response *cmd_association_response_reception_ptr;
cmd_data_request_0_3 *cmd_data_request_0_3_reception;
//norace MPDU mac_beacon_txmpdu;
uint8_t *send_beacon_frame_ptr;
uint8_t send_beacon_length;
//////////////////////////////////////////////////////
/////////////////FUNCTIONS DEFINITIONS////////////////
//////////////////////////////////////////////////////
void init_MacCon();
void init_MacPIB();
task void create_beacon();
//void build_tx_command(uint8_t cmd_type);
void create_association_request_cmd(uint8_t CoordAddrMode,uint16_t CoordPANId,uint32_t CoordAddress[]);
void create_association_response_cmd(uint32_t DeviceAddress[],uint16_t shortaddress, uint8_t status);
void create_data_request_cmd();
void create_beacon_request_cmd();
void create_gts_request_cmd(uint8_t gts_characteristics);
void create_disassociation_notification_cmd(uint8_t disassociation_reason);
//CHECK create other process functions, one for each command
void process_dissassociation_notification(MPDU *pdu);
void process_gts_request(MPDU *pdu);
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);
void build_ack(uint8_t sequence,uint8_t frame_pending);
task void data_indication();
void indication_beacon(MPDU *pdu, int8_t ppduLinkQuality);
void indication_cmd(MPDU *pdu, int8_t ppduLinkQuality);
void indication_ack(MPDU *pdu, int8_t ppduLinkQuality);
void indication_data(MPDU *pdu, int8_t ppduLinkQuality);
void process_beacon(MPDU *packet);
void process_command(MPDU *packet);
void process_ack(MPDU *packet);
//send function
void send_frame(uint8_t length, uint8_t *frame_ptr,uint8_t frame_type,bool csma_ca);
//Indirect transmission functions
void add_ind_queue(uint8_t dest_addr_mode, uint32_t DeviceAddress[],uint8_t *frame_ptr);
void init_indirect_trans_buffer();
result_t remove_indirect_trans(uint8_t handler);
void increment_indirect_trans();
void send_ind_trans_addr(uint8_t dest_addr_mode, uint32_t DeviceAddress[]);
//used in the CSMA-CA
void init_csma_ca(bool slotted);
void perform_csma_ca();
uint8_t min(uint8_t val1, uint8_t val2);
task void perform_csma_ca_unslotted();
task void perform_csma_ca_slotted();
task void start_csma_ca_slotted();
task void set_trx();
task void create_data();
task void signal_loss();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -