00001
00002 #ifndef RFID_READER_MAC_H
00003 #define RFID_READER_MAC_H
00004
00005 #include <vector>
00006 using namespace std;
00007 #include <boost/shared_ptr.hpp>
00008
00009 #include "simulator.hpp"
00010 #include "mac_protocol.hpp"
00011 #include "packet.hpp"
00012
00013 class RfidTagMacData;
00014 typedef boost::shared_ptr<RfidTagMacData> RfidTagMacDataPtr;
00015 class RfidReaderApp;
00016 typedef boost::shared_ptr<RfidReaderApp> RfidReaderAppPtr;
00017
00019
00021
00026 class RfidReaderMacData : public PacketData {
00027 friend ostream& operator<< (ostream& s, const RfidReaderMacData& data);
00028 public:
00030 typedef boost::shared_ptr<RfidReaderMacData> RfidReaderMacDataPtr;
00031
00036 enum Types {
00037 Types_Generic,
00038 Types_Request,
00039 Types_Ack,
00040 Types_Select
00041 };
00042
00047 static inline RfidReaderMacDataPtr create();
00048
00053 static inline RfidReaderMacDataPtr create(
00054 const RfidReaderMacData& rhs);
00055
00060 virtual inline t_uint getSizeInBytes() const;
00061
00066 void setSenderId(const NodeId& nodeId);
00067
00072 NodeId getSenderId() const;
00073
00078 void setReceiverId(const NodeId& nodeId);
00079
00084 NodeId getReceiverId() const;
00085
00090 inline void setType(Types type);
00091
00096 inline Types getType() const;
00097
00102 inline void setNumberOfSlots(t_uchar numberOfSlots);
00103
00108 inline t_uchar getNumberOfSlots() const;
00109
00113 inline ostream& print(ostream& s) const;
00114
00115 protected:
00116
00118 RfidReaderMacData();
00119
00121 RfidReaderMacData(const RfidReaderMacData& rhs);
00122
00129 virtual PacketDataPtr clone() const;
00130
00131 private:
00132
00134 static const t_uint m_senderIdBytes = 4;
00135
00137 static const t_uint m_receiverIdBytes = 12;
00138
00140 static const t_uint m_typeBytes = 1;
00141
00143 static const t_uint m_numberOfSlotsBytes = 1;
00144
00146 t_uchar m_senderId[m_senderIdBytes];
00147
00149 t_uchar m_receiverId[m_receiverIdBytes];
00150
00152 t_uchar m_numberOfSlots;
00153
00155 Types m_type;
00156
00157 };
00158 typedef boost::shared_ptr<RfidReaderMacData> RfidReaderMacDataPtr;
00159
00161
00163
00167 class RfidReaderMac : public SlottedMac {
00168 friend class RfidMacCycleEvent;
00169 public:
00171 typedef boost::shared_ptr<RfidReaderMac> RfidReaderMacPtr;
00172
00174 virtual ~RfidReaderMac();
00175
00182 static inline RfidReaderMacPtr create(NodePtr node,
00183 RfidReaderAppPtr readerApp);
00184
00191 virtual inline RfidReaderMacPtr thisRfidReaderMac();
00192
00199 virtual inline SlottedMacPtr thisSlottedMac();
00200
00207 virtual inline MacProtocolPtr thisMacProtocol();
00208
00215 virtual inline SimulationEndListenerPtr thisSimulationEndListener();
00216
00220 virtual void simulationEndHandler();
00221
00229 bool handleRecvdMacPacket(PacketPtr packet, t_uint sendingLayerIdx);
00230
00238 bool handleRecvdUpperLayerPacket(PacketPtr packet,
00239 t_uint sendingLayerIdx);
00240
00248 virtual void handleChannelBusy(PacketPtr packet);
00249
00257 virtual void handlePacketSent(PacketPtr packet);
00258
00259 protected:
00260
00264 static const double m_READER_IFS;
00265
00268 static const t_uint m_DEFAULT_NUMBER_OF_SLOTS;
00269
00272 static const double m_DEFAULT_CYCLE_TIME;
00273
00276 static const t_uint m_MISSED_READ_THRESHOLD;
00277
00279
00280 static const string m_MISSED_READ_TOTAL_STRING;
00281 static const string m_MISSED_READ_SLOT_AVG_STRING;
00282 static const string m_WINNING_SLOT_AVG_STRING;
00284
00287 bool m_doResetSlot;
00288
00291 t_uint m_resetSlotNumber;
00292
00296 bool m_doEntireReadCycle;
00297
00301 t_uint m_missedReadCount;
00302
00304 TimerPtr m_cycleTimer;
00305
00308 PacketPtr m_currentAppReadPacket;
00309
00312 t_uint m_nextCycleNumberOfSlots;
00313
00315 SimTime m_nextCycleTime;
00316
00318 RfidReaderAppPtr m_readerApp;
00319
00322 vector<pair<NodeId,t_uint> > m_winningSlotNumbers;
00323
00326 vector<t_uint> m_missedReads;
00327
00329 RfidReaderMac(NodePtr node, RfidReaderAppPtr readerApp);
00330
00334 virtual void beginSlotEvent();
00335
00339 virtual void endRequestCycleEvent();
00340
00345 PacketPtr createRequestPacket() const;
00346
00353 PacketPtr createAckPacket(NodeId destination) const;
00354
00361 void addGenericHeader(PacketPtr packet, NodeId receiverId) const;
00362
00369 void addSelectHeader(PacketPtr packet, NodeId receiverId) const;
00370
00376 bool packetIsForMe(RfidTagMacDataPtr macData) const;
00377
00383 bool isEnoughTimeForContentionCycle() const;
00384
00389 void startNextContentionCycle();
00390
00398 bool isPacketType(PacketPtr packet,
00399 RfidReaderMacData::Types type) const;
00400
00401 private:
00402
00404 static const bool m_DEBUG = true;
00405
00407 static const bool m_DEBUG_CONTENTION_CYCLE_TIME = false;
00408
00411 boost::weak_ptr<RfidReaderMac> m_weakThis;
00412
00413 };
00414 typedef boost::shared_ptr<RfidReaderMac> RfidReaderMacPtr;
00415
00417
00419
00424 class RfidMacCycleEvent : public Event {
00425 public:
00427 typedef boost::shared_ptr<RfidMacCycleEvent>
00428 RfidMacCycleEventPtr;
00429
00434 static inline RfidMacCycleEventPtr create(
00435 RfidReaderMacPtr rfidReaderMac)
00436 {
00437 RfidMacCycleEventPtr p(
00438 new RfidMacCycleEvent(rfidReaderMac));
00439 return p;
00440 }
00441
00442 void execute()
00443 {
00444 m_rfidReaderMac->endRequestCycleEvent();
00445 }
00446
00447 protected:
00449 RfidMacCycleEvent(RfidReaderMacPtr rfidReaderMac)
00450 : Event()
00451 {
00452 m_rfidReaderMac = rfidReaderMac;
00453 }
00454 private:
00455 RfidReaderMacPtr m_rfidReaderMac;
00456 };
00457 typedef boost::shared_ptr<RfidMacCycleEvent>
00458 RfidMacCycleEventPtr;
00459
00461
00463
00465
00467
00468 inline RfidReaderMacPtr RfidReaderMac::create(NodePtr node,
00469 RfidReaderAppPtr readerApp)
00470 {
00471 RfidReaderMacPtr p(new RfidReaderMac(node, readerApp));
00472
00473 p->m_weakThis = p;
00474
00475 SlottedMacSlotEventPtr slotEvent =
00476 SlottedMacSlotEvent::create(p->thisSlottedMac());
00477 p->m_slotTimer = Timer::create(p->getNode(), slotEvent);
00478 p->m_slotTimer->start(SimTime(0.0));
00479
00480 RfidMacCycleEventPtr cycleEvent =
00481 RfidMacCycleEvent::create(p->thisRfidReaderMac());
00482 p->m_cycleTimer = Timer::create(p->getNode(), cycleEvent);
00483
00484 Simulator::instance()->addSimulationEndListener(
00485 p->thisSimulationEndListener());
00486
00487 return p;
00488 }
00489
00490 inline RfidReaderMacPtr RfidReaderMac::thisRfidReaderMac()
00491 {
00492 RfidReaderMacPtr p(m_weakThis);
00493 return p;
00494 }
00495
00496 inline SlottedMacPtr RfidReaderMac::thisSlottedMac()
00497 {
00498 SlottedMacPtr p(m_weakThis);
00499 return p;
00500 }
00501
00502 inline MacProtocolPtr RfidReaderMac::thisMacProtocol()
00503 {
00504 MacProtocolPtr p(m_weakThis);
00505 return p;
00506 }
00507
00508 inline SimulationEndListenerPtr RfidReaderMac::thisSimulationEndListener()
00509 {
00510 SimulationEndListenerPtr p(m_weakThis);
00511 return p;
00512 }
00513
00515
00517
00519
00521
00522 inline RfidReaderMacDataPtr RfidReaderMacData::create()
00523 {
00524 RfidReaderMacDataPtr p(new RfidReaderMacData());
00525 return p;
00526 }
00527
00528 inline RfidReaderMacDataPtr RfidReaderMacData::create(
00529 const RfidReaderMacData& rhs)
00530 {
00531 RfidReaderMacDataPtr p =
00532 boost::dynamic_pointer_cast<RfidReaderMacData>(rhs.clone());
00533
00534 assert(p.get() != 0);
00535 return p;
00536 }
00537
00538 inline t_uint RfidReaderMacData::getSizeInBytes() const
00539 {
00540 t_uint byteCount = (m_senderIdBytes + m_receiverIdBytes + m_typeBytes);
00541
00542 switch(getType()) {
00543 case Types_Request:
00544 byteCount += m_numberOfSlotsBytes;
00545 break;
00546 default:
00547 break;
00548 }
00549
00550 return byteCount;
00551 }
00552
00553 inline void RfidReaderMacData::setType(RfidReaderMacData::Types type)
00554 {
00555 m_type = type;
00556 }
00557
00558 inline RfidReaderMacData::Types RfidReaderMacData::getType() const
00559 {
00560 return m_type;
00561 }
00562
00563 inline void RfidReaderMacData::setNumberOfSlots(t_uchar numberOfSlots)
00564 {
00565 m_numberOfSlots = numberOfSlots;
00566 }
00567
00568 inline t_uchar RfidReaderMacData::getNumberOfSlots() const
00569 {
00570 return m_numberOfSlots;
00571 }
00572
00574
00576
00577 inline ostream& operator<< (ostream& s,
00578 const RfidReaderMacData::Types& dataType)
00579 {
00580 switch(dataType) {
00581 case RfidReaderMacData::Types_Generic:
00582 s << "GENERIC";
00583 break;
00584 case RfidReaderMacData::Types_Request:
00585 s << "REQUEST";
00586 break;
00587 case RfidReaderMacData::Types_Ack:
00588 s << "ACK";
00589 break;
00590 case RfidReaderMacData::Types_Select:
00591 s << "SELECT";
00592 break;
00593 }
00594 return s;
00595 }
00596
00597 inline ostream& RfidReaderMacData::print(ostream& s) const
00598 {
00599 s << "type=" << m_type << ", " <<
00600 "senderId=" <<
00601 NodeId(m_senderId, m_senderIdBytes) << ", " <<
00602 "receiverId=" <<
00603 NodeId(m_receiverId, m_receiverIdBytes) << ", " <<
00604 "numberOfSlots=" << static_cast<t_uint>(m_numberOfSlots);
00605 return s;
00606 }
00607
00608 #endif // RFID_READER_MAC_H
00609