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

class PhyUmts;

class PhyUmtsTimer : public Handler {
public:
	PhyUmtsTimer(PhyUmts* m, double s = 0) : phy(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:
	PhyUmts	*phy;
	int	busy_;
	int	paused_;
	Event intr;
	double stime;	// start time
	double rtime;	// remaining time
	double	slottime_;

};

// Backoff timer for random access -
class PreambleTimer : public PhyUmtsTimer {
	public:
		PreambleTimer(PhyUmts *m) : PhyUmtsTimer(m) {}
	 	void start(void); // - over written to get a simple timer.
		void stop(void);
		void handle(Event *e);
};

class UpSlotUmtsTimer : public PhyUmtsTimer {
public:
	UpSlotUmtsTimer(PhyUmts *m) : PhyUmtsTimer(m) {}
	void handle(Event *e);
};

class RachUpSlotUmtsTimer : public PhyUmtsTimer {
public:
	RachUpSlotUmtsTimer(PhyUmts *m) : PhyUmtsTimer(m) {}
	void handle(Event *e);
};

/* Timers to control packet sending and receiving time. */
class RxPktUmtsTimer : public PhyUmtsTimer {
public:
	RxPktUmtsTimer(PhyUmts *m) : PhyUmtsTimer(m) {}
	void	handle(Event *e);
};

class TxPktUmtsTimer : public PhyUmtsTimer {
public:
	TxPktUmtsTimer(PhyUmts *m) : PhyUmtsTimer(m) {}
	void	handle(Event *e);
};

class BlerTimer : public PhyUmtsTimer {
public:
	BlerTimer(PhyUmts *m) : PhyUmtsTimer(m) {}
	void	handle(Event *e);
};

class CellTimer : public PhyUmtsTimer {
public:
	CellTimer(PhyUmts *m) : PhyUmtsTimer(m) {}
 	void start(double time); // - over written to get a simple timer.
	void stop(void);
	void	handle(Event *e);
};






class PhyUmtsNodeb;

class PhyNodebTimer : public Handler {
public:
	PhyNodebTimer(PhyUmtsNodeb* m, double s = 0) : phy(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:
	PhyUmtsNodeb	*phy;
	int	busy_;
	int	paused_;
	Event intr;
	double stime;	// start time
	double rtime;	// remaining time
	double	slottime_;

};

// Backoff timer for random access -
class PagingTimer : public PhyNodebTimer {
public:
		PagingTimer(PhyUmtsNodeb *m) : PhyNodebTimer(m) {}
	 	void start(double time); // - over written to get a simple timer.
		void stop(void);
		void handle(Event *e);
};

class DownSlotUmtsTimer : public PhyNodebTimer {
public:
	DownSlotUmtsTimer(PhyUmtsNodeb *m) : PhyNodebTimer(m) {}
	void handle(Event *e);
};

class AichSlotUmtsTimer : public PhyNodebTimer {
public:
	AichSlotUmtsTimer(PhyUmtsNodeb *m) : PhyNodebTimer(m) {}
	void handle(Event *e);
};

/* Timers to control packet sending and receiving time. */
class RxPktNodebTimer : public PhyNodebTimer {
public:
	RxPktNodebTimer(PhyUmtsNodeb *m) : PhyNodebTimer(m) {}
	void	handle(Event *e);
};

class TxPktNodebTimer : public PhyNodebTimer {
public:
	TxPktNodebTimer(PhyUmtsNodeb *m) : PhyNodebTimer(m) {}
	void	handle(Event *e);
};

class BlerNodebTimer : public PhyNodebTimer {
public:
	BlerNodebTimer(PhyUmtsNodeb *m) : PhyNodebTimer(m) {}
	void	handle(Event *e);
};

#endif
