49#define DEBUG(level,text) {if (debug >= level) {std::cout << NOW << " UwTokenBus(" << node_id << "): " << text << std::endl;}}
62 : TclClass(
"Module/UW/TOKENBUS")
70 create(
int argc,
const char *
const * argv)
117 bind(
"n_nodes_", (
int *)&
n_nodes);
118 bind(
"slot_time_", (
double *)&
slot_time);
120 bind(
"debug_tb", (
int *)&
debug);
121 bind(
"debug_", (
int *)&debug_);
126 bind(
"mac2phy_delay_", (
double *)&mac2phy_delay_);
130 DEBUG(1,
" UwTokenBus() not valid slot_time_ < 0!! set to 1 by default ")
135 DEBUG(1,
" UwTokenBus() not valid token_pass_timeout_ < 0!! set to 1 by default ")
140 DEBUG(1,
" UwTokenBus() not valid queue_size_ < 0!! set to 1000 by default ")
145 DEBUG(1,
" UwTokenBus() not valid max_token_hold_time_ < 0!! set to 10 by default ")
150 DEBUG(1,
" UwTokenBus() not valid min_token_hold_time_ < 0!! set to 1 by default ")
155 DEBUG(1,
" UwTokenBus() drop_old_ and checkPriority cannot be set both to 1!! "
156 <<
"checkPriority set to 0 by default ")
159 if (mac2phy_delay_ <= 0)
161 DEBUG(1,
"mac2phy_delay_ < 0!! set to 1e-9 by default")
162 mac2phy_delay_ = 1e-9;
175 DEBUG(4,
" TOKEN PASS timer expired" )
182 DEBUG(1,
" BUS IDLE timer expired" )
200 DEBUG(10,
" recv packet from upperLayer: " << p <<
" queue size: " <<
buffer.size()+1)
201 bool is_cbr = (((hdr_cmn*)HDR_CMN(p))->ptype() ==
PT_UWCBR);
221 incrDiscardedPktsTx();
222 DEBUG(1,
" recvFromUpperLayers() dropping pkt due to buffer full ")
225 Packet *p_old =
buffer.front();
235 Packet *p_old =
buffer.back();
277 Packet *p =
buffer.front();
280 DEBUG(10,
" sending a data packet from queue: " << p)
290 <<
" max token hold timeout expired with " <<
buffer.size()
291 <<
" packets still in queue")
308 DEBUG(4,
" Waiting the token to start tx")
310 DEBUG(4,
" Wait earlier packet expires to send the current one")
312 DEBUG(1,
" txData() called while in RX status ")
319 DEBUG(1,
" called Mac2PhyStartTx() while already transmitting ")
321 DEBUG(1,
" called Mac2PhyStartTx() while receiving ")
324 hdr_cmn *ch = HDR_CMN(p);
325 hdr_mac *mach = HDR_MAC(p);
328 DEBUG(10,
" start tx of pkt " << p <<
" of type " << ch->ptype() <<
" size: " << ch->size() <<
" tx duration: " << Mac2PhyTxDuration(p)
330 MMac::Mac2PhyStartTx(p);
346 DEBUG(1,
" Phy2MacStartRx() while in TX status")
348 DEBUG(1,
" Phy2MacStartRx() while already in RX status")
361 DEBUG(1,
" received a token with invalid token id: " <<
id)
367 hdr_cmn *ch = HDR_CMN(p);
368 hdr_mac *mach = HDR_MAC(p);
369 int dest_mac = mach->macDA();
371 int pkt_token_id = tbh->
tokenId();
372 int pkt_node_id = (pkt_token_id %
n_nodes);
383 DEBUG(1,
" Phy2MacEndRx() dropping corrupted pkt ")
401 DEBUG(3,
" Received the token ")
404 else if (pkt_node_id <
node_id)
412 DEBUG(1,
" received a pkt with invalid token id ")
420 else if ((dest_mac != addr) && (dest_mac != BCAST_ADDR))
434 DEBUG(1,
" discard packet rx while tx; pkt token_id : " << pkt_token_id <<
" macSA: " << mach->macSA() <<
" macDA: " << mach->macDA())
441 hdr_cmn *ch = HDR_CMN(p);
443 (ch->size()) += tbh->
getSize();
449 DEBUG(1,
" attempt to send token without owning it; tokenid: " << token_id)
453 Packet *p = Packet::alloc();
454 hdr_cmn *ch = HDR_CMN(p);
455 hdr_mac *mach = HDR_MAC(p);
457 (ch->size()) += tbh->
getSize();
459 mach->set(MF_CONTROL, addr, BCAST_ADDR);
460 mach->macDA() = BCAST_ADDR;
463 DEBUG(3,
" pass TOKEN to node: " << (token_id %
n_nodes) <<
" with tokenid: " << token_id)
471 Tcl &tcl = Tcl::instance();
474 if (strcasecmp(argv[1],
"get_buffer_size") == 0)
476 tcl.resultf(
"%d",
buffer.size());
479 else if (strcasecmp(argv[1],
"get_count_token_resend") == 0)
484 else if (strcasecmp(argv[1],
"get_count_token_regen") == 0)
489 else if (strcasecmp(argv[1],
"get_count_token_invalid") == 0)
494 else if (strcasecmp(argv[1],
"get_bus_idle_exp") == 0)
499 else if (strcasecmp(argv[1],
"get_token_pass_exp") == 0)
507 if (strcasecmp(argv[1],
"set_slot_time") == 0)
512 else if (strcasecmp(argv[1],
"set_token_pass_timeout") == 0)
517 else if (strcasecmp(argv[1],
"set_bus_idle_timeout") == 0)
522 else if (strcasecmp(argv[1],
"setMacAddr") == 0)
524 addr = atoi(argv[2]);
525 DEBUG(4,
" MAC address: " << addr)
529 return MMac::command(argc, argv);
Class that represent the binding of the protocol with tcl.
UwTokenBusModuleClass()
Constructor of the UwTokenBusModule class.
TclObject * create(int argc, const char *const *argv)
Creates the TCL object needed for the tcl language interpretation.
virtual void expire(Event *e)
Method call when the timer expire.
virtual void expire(Event *e)
Method call when the timer expire.
Class that represents a TokenBus Node.
UWTokenBus_STATUS rtx_status
int count_token_invalid
node count of invalid received token
double token_pass_timeout
timeout for the namesake timer for token retransmission attempt, should be 2*slot_time+min_token_hold...
double min_token_hold_time
if the node has en empty queue when it receive the token, it waits this time before passing the token
virtual void initRing()
Initialize the network and generates the first token.
virtual void expireBusIdle()
called when bus_idle_timer expires
int debug
Debug variable: 0 for no info.
double slot_time
max travel time between any pair of nodes, used as time unit for some of the timers timeouts
virtual void Phy2MacEndTx(const Packet *p) override
Method called when the Mac Layer finish to transmit a Packet.
virtual int nextIdOwned(int id) const
static int count_token_pass_exp
count token pass timer expirations
virtual ~UwTokenBus()
Destructor of the TokenBus class.
TimerBusIdle bus_idle_timer
token_pass_timer is scheduled when a node pass the token, it's cancelled when activity from the follo...
int n_nodes
number of nodes in the ring
virtual int normId(int id) const
virtual void recvFromUpperLayers(Packet *p) override
Receive the packet from the upper layer (e.g.
virtual void Mac2PhyStartTx(Packet *p)
Method called when the Mac Layer start to transmit a Packet.
virtual int nextId(int id) const
static int count_nodes
counter of the instantiated nodes, used for assigning node ids in default contructor
virtual int command(int argc, const char *const *argv) override
TCL command interpreter.
double bus_idle_timeout
base timeout for the namesake timer should be (slot_time+max_token_hold_time)
double token_rx_time
time of token reception
virtual void Phy2MacStartRx(const Packet *p) override
Method called when the Phy Layer start to receive a Packet.
std::deque< Packet * > buffer
outgoing packets dequeue
TimerTokenPass token_pass_timer
int last_token_id_heard
last token id heard on the bus
int count_token_regen
node count of token regeneration
int drop_old_
flag to set the drop packet policy in case of buffer overflow: if 0 (default) drops the new packet,...
bool got_token
set if node is currently holding the token
virtual void Phy2MacEndRx(Packet *p) override
Method called when the Phy Layer finish to receive a Packet.
int last_token_id_owned
last token id owned
int max_queue_size
max packets in the queue
virtual void initPkt(Packet *p)
Method called to add the MAC header size.
virtual void sendToken(int next_id)
Passes the token to the next node.
int node_id
id of the node (0 to n_nodes-1)
int checkPriority
flag to set to 1 if UWCBR module uses packets with priority, set to 0 otherwise.
virtual void txData()
Starts transmitting the packets from the queue.
int count_token_resend
bus_idle_timer is rescheduled everytime a new token_id is heard on the bus: the first time node n hea...
virtual bool validToken(Packet *p) const
Assert if the received token id is valid, i.e it follows the monotonic progression taking in account ...
virtual void expireTokenPass()
called when token_pass_timer expires
static int count_bus_idle_exp
count bus idle timer expirations
UwTokenBus()
Default constructor of the TokenBus class.
double max_token_hold_time
max token holding time
Header of the token bus protocol.
size_t getSize() const
Returns the size of this header.
tokenid_t & tokenId()
Returns a reference to the token_id variable.
hdr_uwcbr describes UWCBR packets.
char & priority()
Reference to the priority_ variable.
Provides the UWCBR packets header description and the definition of the class UWCBR.
UwTokenBusModuleClass class_module_uwtokenbus
#define DEBUG(level, text)
Provides the definition of the class UwTokenBus.
Common structures and variables in the protocol.
#define HDR_TOKENBUS(p)
alias defined to access the TOKEN BUS HEADER
constexpr size_t TOKENIDMAX