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

📄 mac-gprs.h

📁 一个很好的LINUX底下的GPRS协议栈
💻 H
字号:
/* -*-	Mode:C++; c-basic-offset:8; tab-width:8; indent-tabs-mode:t -*- *//* By Richa Jain, * Indian Institute of Technology, Bombay. * June, 2001.*/ /* Copyright (c) 2001 Indian Insitute of Technology, Bombay.   * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: *  * 1. Redistributions of source code and binary code must contain * the above copyright notice, this list of conditions and the following  * disclaimer. *  * 2. All advertising materials mentioning features or use of this software * must display the following acknowledgement:  * This product includes software developed at Indian Insitute of * Technology, Bombay.  * * 3. The name of the Institute may not be used to endorse or promote  * products derived from this software without specific prior written  * permission. * INDIAN INSTITUTE OF TECHNOLOGY, BOMBAY, MAKES NO REPRESENTATIONS  * CONCERNING EITHER THE MERCHANTABILITY OF THIS SOFTWARE OR THE  * SUITABILITY OF THIS SOFTWARE FOR ANY PARTICULAR PURPOSE.  The software  * is provided "as is" without express or implied warranty of any kind.*//* A special slotted Aloha based MAC for GPRS/GSM  */ #ifndef ns_mac_gprs_h#define ns_mac_gprs_h// #define DEBUG//#include <debug.h>#include "marshall.h"#include <delay.h>#include <connector.h>#include <packet.h>#include <random.h>#include <arp.h>#include <ll.h>#include <mac.h>// some macros for address conversion#define GET_ETHER_TYPE(x)		GET2BYTE((x))#define SET_ETHER_TYPE(x,y)    {u_int16_t t = (y); STORE2BYTE(x,&t);}/* We are still using these specs for phy layer---same as 802.11. *//* * IEEE 802.11 Spec, section 15.3.2 *	- default values for the DSSS PHY MIB */#define DSSS_CWMin			31#define DSSS_CWMax			1023#define DSSS_SlotTime			0.000020	// 20us#define	DSSS_CCATime			0.000015	// 15us#define DSSS_RxTxTurnaroundTime		0.000005	// 5us#define DSSS_SIFSTime			0.000010	// 10us#define DSSS_PreambleLength		144		// 144 bits#define DSSS_PLCPHeaderLength		48		// 48 bitsclass PHY_MIB {public:	u_int32_t	CWMin;	u_int32_t	CWMax;	double		SlotTime;	double		CCATime;	double		RxTxTurnaroundTime;	double		SIFSTime;	u_int32_t	PreambleLength;	u_int32_t	PLCPHeaderLength;};/* ======================================================================   Frame Formats   ====================================================================== */#define MAX_NUM_FREQ 30  //maximum number of frequencies permissiblein the cell#define MAX_NUM_MS 64    //maximum number of Mobiles permissible in the cell#define SLOTS_PER_FRAME 8 // number of slots in evry TDMA frame #define	MAC_ProtocolVersion	0x00// Packets can be of GPRS Mac type - Data or Control// with subtypes - data, resource request, reply or resource release.#define MAC_Type_Data		0x02#define MAC_Type_Control	0x03#define MAC_Subtype_data	0x00#define MAC_Subtype_res_request	0x01  // resource request#define MAC_Subtype_res_reply	0x02  // resource reply#define MAC_Subtype_tx_end		0x03  // resource release#define NONE 0// channel states for the (Packet) Random Acess channel. #define IDLE 0#define BUSY 1#define COLL 2// To indicate whether the radio's ON or OFF#define ON                       1#define OFF                      0// Quoted from MAC-802.11. #define DATA_DURATION           5// We use the same frame structure as 802.11 struct frame_control {	u_char		fc_subtype		: 4;	u_char		fc_type			: 2;	u_char		fc_protocol_version	: 2;	u_char		fc_order		: 1;	u_char		fc_wep			: 1;	u_char		fc_more_data		: 1;	u_char		fc_pwr_mgt		: 1;	u_char		fc_retry		: 1;	u_char		fc_more_frag		: 1;	u_char		fc_from_ds		: 1;	u_char		fc_to_ds		: 1;};// the GPRS headerstruct hdr_mac_gprs {	struct frame_control	dh_fc;	u_int16_t		dh_duration;	u_char			dh_da[ETHER_ADDR_LEN];	u_char			dh_sa[ETHER_ADDR_LEN];	u_char			dh_bssid[ETHER_ADDR_LEN];	u_char			dh_body[0]; // XXX Non-ANSI	int             dh_freq; // freq/slot information to be sent down 	int             dh_slot; // in the resource reply message - rj		int             dh_wait; // to resume tx at BS after slot_allot - rj							 //dh_wait =1 => dont tx now, wait .....	};// in GPRS, the mac/rlc hdr is 8 bits.#define ETHER_HDR_LEN 1struct data_at_bs{		//i th element is the hier address of node with index_=i		int hier_addr_[MAX_NUM_MS] ;			//these tables store the mac index_ of the node that has been    		// alloted freq i, slot j		int up_table[MAX_NUM_FREQ][SLOTS_PER_FRAME];	 	int down_table[MAX_NUM_FREQ][SLOTS_PER_FRAME];   };/* Timers */class MacGprs;class MacGprsTimer : public Handler {public:	MacGprsTimer(MacGprs* m, double s = 0) : mac(m) {		busy_ = paused_ = 0; stime = rtime = 0.0; slottime_ = s;	}	virtual void handle(Event *e) = 0;	virtual void start(Packet *p, double time);	virtual void stop(Packet *p);	virtual void pause(void) { assert(0); }	virtual void resume(void) { assert(0); }	inline int busy(void) { return busy_; }	inline int paused(void) { return paused_; }	inline double slottime(void) { return slottime_; }	inline double expire(void) {		return ((stime + rtime) - Scheduler::instance().clock());	}protected:	MacGprs	*mac;	int	busy_;	int	paused_;	Event intr;	double stime;	// start time	double rtime;	// remaining time	double	slottime_;};// Timer to release resources (ie free a slot)class SlotReleaseTimer : public MacGprsTimer {	public: 		SlotReleaseTimer(MacGprs *m) : MacGprsTimer(m) {} 	 	void start(double time); // - over written to get a simple timer.		void stop(void);		void handle(Event *e);};		 //timer to wait for res_replyclass WaitTimer : public MacGprsTimer {	public: 		WaitTimer(MacGprs *m) : MacGprsTimer(m) {} 	 	void start(double time); // - over written to get a simple timer.		void stop(void);		void handle(Event *e);};		 // Backoff timer for random access -class BackoffGprsTimer : public MacGprsTimer {	public: 		BackoffGprsTimer(MacGprs *m) : MacGprsTimer(m) {} 	 	void start(double time); // - over written to get a simple timer.		void stop(void);		void handle(Event *e);};		 // Timers to time the Upslots and Downslots.class DownSlotGprsTimer : public MacGprsTimer {public:	DownSlotGprsTimer(MacGprs *m) : MacGprsTimer(m) {}	void handle(Event *e);};class UpSlotGprsTimer : public MacGprsTimer {public:	UpSlotGprsTimer(MacGprs *m) : MacGprsTimer(m) {}	void handle(Event *e);};/* Timers to control packet sending and receiving time. */class RxPktGprsTimer : public MacGprsTimer {public:	RxPktGprsTimer(MacGprs *m) : MacGprsTimer(m) {}	void	handle(Event *e);};class TxPktGprsTimer : public MacGprsTimer {public:	TxPktGprsTimer(MacGprs *m) : MacGprsTimer(m) {}	void	handle(Event *e);};/* GPRS Mac layer. */class MacGprs : public Mac {  friend class DownSlotGprsTimer;  friend class UpSlotGprsTimer;  friend class TxPktGprsTimer;  friend class RxPktGprsTimer;  friend class SlotReleaseTimer;  friend class BackoffGprsTimer;  friend class WaitTimer;	  public:  MacGprs(PHY_MIB* p);  void recv(Packet *p, Handler *h);    inline int hdr_dst(char* hdr, int dst = -2);  inline int hdr_src(char* hdr, int src = -2);  inline int hdr_type(char* hdr, u_int16_t type = 0);    // Timer handlers   void downslotHandler(Event *e);  void upslotHandler(Event *e);  void recvHandler(Event *e);  void sendHandler(Event *e);  void releaseHandler(void);  void backoffHandler(void);  void waitHandler(void);   protected:  PHY_MIB		*phymib_;    Packet * p_temp; //temporary storage for a pkt while waiting for a res_reply  Packet * temp_reply; // Storage in case two res_replies are  					   //generated at the BS in a particular TDMA frame  // user defined variables					     static int slot_packet_len_; // max pkt size that can be txd in one slot  static int max_num_ms_;      // user defined max num of MS in cell  static int max_num_freq_;    // num of frequencies to be permitted inthe cell  static int gprs_slots_per_frame_ ; // how many slots of the 8 per frame  									// are to be reserved for gprs.  static int rlc_error_; // whether or not to include the rlc error model  static int error_rate_;// the "error rate"  static int verbose_;   // to include a verbose output ..      // for the slot level error model  static int drop_gap_;  static int drop_counter_;  static int next_drop_;    static int tx_rate;     int gprs_; //to indicate whether the mobile is a GPRS MS or GSM   private:  int command(int argc, const char*const* argv);    void ms_recv(Packet *p, Handler *h);  void bs_recv(Packet *p, Handler *h);    void slot_allot(int ms_, int &freq, int &slot);  void radioSwitch(int i);  // Packet Transmission Functions.  void rx_from_phy(Packet* p);   void rx_from_ll(Packet* p); // called when a pkt is to be acceptedfrom the IFQ  void fwd_DATA_to_LL(Packet *p); //fwding data to the upper layer: LL/RLC  void tx_onto_PHY(Packet *p );    // to create the message packets.  void send_res_reply (int dst, int freq, int slot);  void send_res_request ();  void send_let_go();    // to calculate tx time for a packet  double DATA_Time(int len) ;     double TX_Time(Packet *p) ;    /* Debugging Functions.*/  void trace_pkt(Packet *p);  void dump(char* fname);  void mac_log(Packet *p) {    logtarget_->recv(p, (Handler*) 0);  }    // Other fncs  inline u_int16_t usec(double t) {    u_int16_t us = (u_int16_t)ceil(t *= 1e6);    return us;  };// variables:  static double start_time_; // The start time for whole TDMA scheduling.   static double slot_time_;  // The duration of each TDMA slot  static int active_node_ ; //num of active nodes with this BS(including BS)   // These data structures shud ideally reside at the BS. But since  // there is no distinct BaseStation created at the C++ level in ns,   // we create them as static structures in the MAC itself. This  // causes them to be unique for each instance of the MAC -  // equivalent to each cell. And hence we have to limit our  // simulations to a single cell. If a separate BS were created at  // the C++ level, these could be shifted there and configured for  // a multi-cell layout.      //static Packet * rxQ[max_num_freq_][SLOTS_PER_FRAME];  //static Packet * txQ[max_num_freq_][SLOTS_PER_FRAME];  static Packet * rxQ[][];  static Packet * txQ[][];  static struct data_at_bs vlr_; //equivalent to the VLR at the BS    // for the Random Access Channel - PRACH - uplink freq 0, slot 0  static int chan0_0;    // a variable identifying the PRACH  static int coll_count; // num of MS transmitting on the PRACH	    // indicates whether the radio is active or not. Each MS has a seperate radio  int radio_active_;     //shudnt these be static- since for 1 BS(ie cell) only.??  int down_slot_;    int up_slot_;   int first_round_;    //indicates that the MS has sent a request and is waiting for a reply.  int wait_for_res;     double slot_release_time_;    //shud be only for MS ??  int tx_chan[SLOTS_PER_FRAME];  int rx_chan[SLOTS_PER_FRAME];  Packet * pktTx[SLOTS_PER_FRAME];   Packet * pktRx[SLOTS_PER_FRAME];      // Timers   DownSlotGprsTimer mhDownSlot_;  UpSlotGprsTimer mhUpSlot_;  TxPktGprsTimer mhTxPkt_;  RxPktGprsTimer mhRxPkt_;  SlotReleaseTimer mhRel_;  BackoffGprsTimer mhBackoff_;	   WaitTimer mhwait_;    NsObject*	logtarget_;};//for the static variables: - shifted to mac-gprs.cc/*Packet * MacGprs::rxQ[MAX_NUM_FREQ][SLOTS_PER_FRAME]={NULL};Packet * MacGprs::txQ[MAX_NUM_FREQ][SLOTS_PER_FRAME]={NULL}; struct data_at_bs MacGprs::vlr_ = {0};int MacGprs:: slot_packet_len_ = {0};int MacGprs:: tx_rate = {0};int MacGprs:: max_num_ms_ = {0};int MacGprs:: max_num_freq_ = {0};int MacGprs:: gprs_slots_per_frame_ = {0};double MacGprs::slot_time_ = 0;double MacGprs::start_time_ = 0;int MacGprs::chan0_0 = {0};int MacGprs::coll_count = {0}; int MacGprs::active_node_ = {0};*/#endif /* __mac_gprs_h__ */

⌨️ 快捷键说明

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