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

/* Modified and extended 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.
*/

/* By Sandeep Kumar, Kopparapu Suman and 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.
*/

#ifndef ns_rlc_umts_h
#define ns_rlc_umts_h

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

#define MAX_FLOWS		10	// in and out flows
#define RLC_HDR_SZ 	1 // rlc header size in bytes.

/*************************** Timers *****************************/

class RlcUmts;
class rTxTimer : public Handler {
public:
	rTxTimer(RlcUmts* m, double s = 0) : rlc(m), Next(0) {
		busy_ = paused_ = 0;stime = rtime = 0.0; flow_ = -1;}

	virtual void handle(Event *e);
	virtual void start(double time);
	virtual void stop(void);
	virtual void pause(void) { assert(0); }
	virtual void resume(void) { assert(0); }

	RlcUmts 	*rlc;
  	int 		flow_;
	int		busy_;
	int		paused_;
	Event		intr;
	double	stime;	// start time
	double	rtime;	// remaining time
	rTxTimer* 	Next;
};

typedef rTxTimer* rTxTimerPtr;

class rTxList
{ public:
  rTxList(RlcUmts* m) : rlc(m) {
   	Head = new rTxTimer(rlc); Tail=Head; CurrentPtr = Head;
  }
  ~rTxList();
  rTxTimerPtr Previous(rTxTimerPtr);
  void AddANode(int flow);
  void DeleteANode(rTxTimerPtr);
  void DeleteANode(int flow);
  rTxTimerPtr GetNode(int flow);

  void start(int flow, double time);
  void stop(int flow);

  RlcUmts *rlc;
  rTxTimerPtr Head,Tail,CurrentPtr;
};

/***************************** Class RlcUmts **********************************/

enum RLCFrameType {
	RLC_DATA		= 0x0001,
	RLC_ACK		= 0x0010,
	RLC_NACK		= 0x0000
};

struct hdr_rlc_umts {
	RLCFrameType rlctype_;	// link-layer frame type
	int ack_;			// 1 ack mode
	int frag_;			// 1 frag mode
	int seqno_;			// sequence number
	int ackno_;			// acknowledgement number
	int bopno_;			// begin of packet seqno
	int eopno_;			// end of packet seqno
	int psize_;			// size of packet
	double sendtime_;		// time the packet is sent

	static int offset_;
	inline int& offset() { return offset_; }
	static hdr_rlc_umts* access(const Packet* p) {
		return (hdr_rlc_umts*) p->access(offset_);
	}

	inline RLCFrameType& rlctype() { return rlctype_; }
	inline int& ack() { return ack_; }
	inline int& frag() { return frag_; }
	inline int& seqno() { return seqno_; }
	inline int& ackno() { return ackno_; }
	inline int& bopno() { return bopno_; }
	inline int& eopno() { return eopno_; }
	inline int& psize() { return psize_; }
	inline double& sendtime() { return sendtime_; }
};

struct flow_inf {
	int flow_;
	int fraged_;
	int acked_;
	int seqno_;				// link-layer sequence number
	int ackno_;				// ACK received so far
	int rackno_;			// seq no of left most pkt
	int window_;			// window size for sack
	PacketQueue* Txbuf_;		// Tx buffer
	PacketQueue* Rxbuf_;		// Rx buffer
	int unackseqno_;
	int numdups_;
	Packet* lastRx_;			// last packet received in sequence
};


class RlcUmts : public LinkDelay {
public:
	RlcUmts();

	virtual void recv(Packet* p, Handler* h);
	void handle(Event* e) { recv((Packet*)e, 0); }
	inline int initialized() {
		return (uptarget_ && downtarget_);
	}

	int look_for(int flow);
	int get_acked(int flow);
	int get_fraged(int flow);
	void handover(int hand);
	virtual void recvACK(Packet* p);
	virtual void recvDATA(Packet* p);
	virtual void sendUpDATA(Packet* p, int pos);
	virtual void enqueDATA(Packet* p, int pos);
	virtual void sendDownDATA(int pos);
	virtual void sendACK(Packet* p, int pos);
	virtual void sendDownDATAonACK(int pos, int seqno);

	void store_flow(int acked, int fraged, int flowid);
	void remove_flow(int flowid);
	void rTxHandler(int flow);

	inline nsaddr_t& ip_nodeb() { return ip_nodeb_; }
	inline nsaddr_t& addr() { return ip_ue_; }
	inline int rlcfragsize() { return rlcfragsz_; }
      inline NsObject* downtarget() { return downtarget_; }
      inline NsObject* uptarget() { return uptarget_; }


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

  	static int rlcverbose_;

	flow_inf info_[MAX_FLOWS];	// array with each flow information
	flow_inf hinfo_[MAX_FLOWS];	// array with each flow information during handover procedure;

      PacketQueue* buf_;
	nsaddr_t ip_nodeb_;
	nsaddr_t ip_ue_;
	int handover_;

	NsObject* downtarget_;		// for outgoing packet
	NsObject* uptarget_;		// for incoming packet

	int rlcfragsz_;
	double rlctime_;

	rTxList *rtx_;			// list of timers to retransmit packets not acknowledged
private:
	int length_;		// length for buf_
};

#endif
