/* -*-	Mode:C++; c-basic-offset:8; tab-width:8; indent-tabs-mode:t -*- */

/* By Pablo Martin and Paula Ballester,
 * Strathclyde University, Glasgow.
 * June, 2003.
*/

/* Copyright (c) 2003 Strathclyde University of Glasgow, Scotland.
 * 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 Strathclyde University of
 * Glasgow, Scotland.
 *
 * 3. The name of the University may not be used to endorse or promote
 * products derived from this software without specific prior written
 * permission.
 * STRATHCLYDE UNIVERSITY OF GLASGOW, 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.
*/

#ifndef ns_mac_umts_h
#define ns_mac_umts_h

#include "marshall.h"
#include "phy-umts.h"
#include "mac.h"
#include <delay.h>
#include <connector.h>
#include <packet.h>
#include <random.h>
#include <arp.h>
#include <ll.h>
#include <mac.h>

// Quoted from MAC-802.11.
#define DATA_DURATION           5
// in Umts, the mac/rlc hdr is 8 bits.
#define MAC_HDR_SZ 		  2 // mac header size in bytes.

/* Timers */
class MacUmts;

class MacUmtsTimer : public Handler {
public:
	MacUmtsTimer(MacUmts* 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:
	MacUmts	*mac;
	int	busy_;
	int	paused_;
	Event intr;
	double stime;	// start time
	double rtime;	// remaining time
	double	slottime_;
};

//timer to wait tti in MacUmts
class WaitTimer : public MacUmtsTimer {
	public:
		WaitTimer(MacUmts *m) : MacUmtsTimer(m) {}
	 	void start(double time); // - over written to get a simple timer.
		void stop(void);
		void handle(Event *e);
};

/* Umts Mac layer. */
class MacUmts : public Mac {
  friend class WaitTimer;

public:
  MacUmts();
  void recv(Packet *p, Handler *h);

  PhyUmts *phy_;	// phy layer

  void waitHandler(void);   // handler of class WaitTimer

 protected:
  static int verbose_;   // to include a verbose output ..
  Event intr_;

 private:
  int command(int argc, const char*const* argv);

  void dl_interference(unsigned long error);	// for passing results from PHY
  int check_error(void);	// returns if the actual transport block is erroneous
  void sendpkt(Packet* p);	// if erroneous packet, drop it, if not, send it

  int error_;		// 0 not erroneous, 1 error in TB
  unsigned long dl_error;	// error probability in transport block

  // Timers
  WaitTimer mhwait_;	// timer for error calculation
};

#endif /* __mac_umts_h__ */
