/* -*-	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_nodeb_h
#define ns_mac_umts_nodeb_h

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

/* Timers */
class MacUmtsNodeb;

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

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

// structure for error model
struct errormodule{
	int phyaddr_;			// physical address for the UE
	int error_;				// 0 not erroneus, 1 error in Transport Block
	unsigned long ul_error_;	// error probability in tx block.
};

/* Umts Mac layer in NodeB. */
class MacUmtsNodeb : public Mac {
  friend class WaitNodebTimer;

public:
  MacUmtsNodeb();
  void recv(Packet *p, Handler *h);
  PhyUmtsNodeb* phy_;	// Physical layer

  // Timer handlers
  void waitHandler(void);	// handler of class WaitNodebTimer

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

  Event intr_;

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

  void ul_interference(int addr[], unsigned long error[]);	// for passing results from PHY
  void remove(int addr);		// free resources of ul_interf[]
  void storepkt(Packet *p);		// if erroneous packet, drop it, if not, send it.
  int check_error(int pos);		// returns if the actual transport block is erroneous

  errormodule ul_interf[MAX_NUM_UE];	// structure for error model

  // Timers
  WaitNodebTimer mhwait_;	// timer for error calculation

};

#endif /* __mac_umts_nodeb_h__ */
