📄 rfid_reader_app.hpp
字号:
#ifndef RFID_READER_APP_H #define RFID_READER_APP_H #include <map> #include <set> #include <algorithm> using namespace std; #include <boost/shared_ptr.hpp> #include "application_layer.hpp" #include "packet.hpp" #include "timer.hpp" #include "simulator.hpp" class PhysicalLayer; typedef boost::shared_ptr<PhysicalLayer> PhysicalLayerPtr; class ReadTagData { friend ostream& operator<< (ostream& s, const ReadTagData& readTagData); public: ReadTagData(const NodeId& readTagId, const SimTime& timeRead, const SimTime& timeReadSent) : m_readTagId(readTagId), m_timeRead(timeRead), m_timeReadSent(timeReadSent) {} NodeId getReadTagId() const { return m_readTagId; } SimTime getTimeRead() const { return m_timeRead; } SimTime getTimeReadSent() const { return m_timeReadSent; } SimTime getReadLatency() const { return (m_timeRead - m_timeReadSent); } private: NodeId m_readTagId; SimTime m_timeRead; SimTime m_timeReadSent; }; inline ostream& operator<< (ostream& s, const ReadTagData& readTagData) { return s << "[ tagId=" << readTagData.m_readTagId << ", timeRead=" << readTagData.m_timeRead << ", timeReadSent=" << readTagData.m_timeReadSent << " ]"; } class RfidReaderApp : public ApplicationLayer { friend class RfidReaderAppReadEvent; public: typedef boost::shared_ptr<RfidReaderApp> RfidReaderAppPtr; typedef multimap<t_uint,ReadTagData>::const_iterator TagIterator; virtual ~RfidReaderApp(); static inline RfidReaderAppPtr create(NodePtr node, PhysicalLayerPtr physicalLayer); virtual inline RfidReaderAppPtr thisRfidReaderApp(); virtual inline ApplicationLayerPtr thisApplicationLayer(); virtual inline SimulationEndListenerPtr thisSimulationEndListener(); void startHandler(); void stopHandler(); virtual void simulationEndHandler(); inline void setReadPeriod(SimTime readPeriod); inline void setDoRepeatedReads(bool doRepeatedReads); inline void setDoReset(bool doReset); inline SimTime getNextReadTime() const; inline SimTime getReadPeriod() const; inline bool getDoRepeatedReads() const; inline bool getDoReset() const; inline void setNumPowerControlLevels(t_uint numPowerControlLevels); inline t_uint getNumPowerControlLevels() const; void signalReadEnd(); protected: static const string m_TAGS_READ_COUNT_STRING; static const string m_TAGS_READ_AT_POWER_COUNT_STRING; static const string m_TAGS_READ_AVG_LATENCY_STRING; static const string m_TAGS_READ_PROCESS_AVG_LATENCY_STRING; static const string m_LAST_TAG_READ_LATENCY_STRING; static const string m_TAG_READ_PROCESS_LATENCY_STRING; static const string m_TAG_READ_LATENCY_STRING; static const string m_TAG_READ_ID_STRING; static const string m_TAG_READ_LEVEL_STRING; static const string m_TAG_READ_TIME_STRING; SimTime m_firstReadSentTime; SimTime m_previousReadSentTime; multimap<t_uint,ReadTagData> m_readTags; pair<t_uint,ReadTagData> m_lastTagRead; set<NodeId> m_readTagIds; RfidReaderApp(NodePtr node, PhysicalLayerPtr physicalLayer); bool handleRecvdPacket(PacketPtr packet, t_uint sendingLayerIdx); virtual void doReadProcess(); private: static const bool m_DEBUG_POWER_CONTROL = true; boost::weak_ptr<RfidReaderApp> m_weakThis; static const double m_DEFAULT_READ_PERIOD; static const t_uint m_DEFAULT_NUM_POWER_CONTROL_LEVELS; PhysicalLayerPtr m_physicalLayer; TimerPtr m_readTimer; SimTime m_readPeriod; bool m_doRepeatedReads; bool m_doReset; t_uint m_numPowerControlLevels; double m_maxTxPower; t_uint m_currentTxPowerLevel; void sendReadPacket(double txPower); void sendResetPacket(); void doNextRead(); }; typedef boost::shared_ptr<RfidReaderApp> RfidReaderAppPtr; // Event Subclasses class RfidReaderAppReadEvent : public Event { public: typedef boost::shared_ptr<RfidReaderAppReadEvent> RfidReaderAppReadEventPtr; static inline RfidReaderAppReadEventPtr create( RfidReaderAppPtr readerApp) { RfidReaderAppReadEventPtr p(new RfidReaderAppReadEvent(readerApp)); return p; } void execute() { m_readerApp->doReadProcess(); } protected: RfidReaderAppReadEvent(RfidReaderAppPtr readerApp) : Event() { m_readerApp = readerApp; } private: RfidReaderAppPtr m_readerApp; }; typedef boost::shared_ptr<RfidReaderAppReadEvent> RfidReaderAppReadEventPtr; // Back to RfidReaderApp Class // Inline Functions inline RfidReaderAppPtr RfidReaderApp::create(NodePtr node, PhysicalLayerPtr physicalLayer) { RfidReaderAppPtr p(new RfidReaderApp(node, physicalLayer)); // weakThis *must* be set before the thisReaderApp // function is called. p->m_weakThis = p; RfidReaderAppReadEventPtr readEvent = RfidReaderAppReadEvent::create(p->thisRfidReaderApp()); p->m_readTimer = Timer::create(p->getNode(), readEvent); Simulator::instance()->addSimulationEndListener( p->thisSimulationEndListener()); return p; } inline RfidReaderAppPtr RfidReaderApp::thisRfidReaderApp() { RfidReaderAppPtr p(m_weakThis); return p; } inline ApplicationLayerPtr RfidReaderApp::thisApplicationLayer() { ApplicationLayerPtr p(m_weakThis); return p; } inline SimulationEndListenerPtr RfidReaderApp::thisSimulationEndListener() { SimulationEndListenerPtr p(m_weakThis); return p; } inline void RfidReaderApp::setReadPeriod(SimTime readPeriod) { m_readPeriod = readPeriod; } inline void RfidReaderApp::setDoRepeatedReads(bool doRepeatedReads) { m_doRepeatedReads = doRepeatedReads; } inline void RfidReaderApp::setDoReset(bool doReset) { m_doReset = doReset; } inline SimTime RfidReaderApp::getNextReadTime() const { SimTime nextReadTime(0.0); if(m_readTimer->isRunning()) { nextReadTime = (m_readTimer->timeRemaining() + Simulator::instance()->currentTime()); } return nextReadTime; } inline SimTime RfidReaderApp::getReadPeriod() const { return m_readPeriod; } inline bool RfidReaderApp::getDoRepeatedReads() const { return m_doRepeatedReads; } inline bool RfidReaderApp::getDoReset() const { return m_doReset; } inline void RfidReaderApp::setNumPowerControlLevels( t_uint numPowerControlLevels) { assert(numPowerControlLevels > 0); m_numPowerControlLevels = numPowerControlLevels; } inline t_uint RfidReaderApp::getNumPowerControlLevels() const { return m_numPowerControlLevels; } // Overloaded Operators // PacketData Subclass class RfidReaderAppData : public PacketData { public: typedef boost::shared_ptr<RfidReaderAppData> RfidReaderAppDataPtr; enum Types { Types_NoType, Types_Read, Types_Reset }; static inline RfidReaderAppDataPtr create(); static inline RfidReaderAppDataPtr create( const RfidReaderAppData& rhs); virtual inline t_uint getSizeInBytes() const; void setReaderId(const NodeId& nodeId); NodeId getReaderId() const; inline void setType(Types type); inline Types getType() const; inline void setDoEntireReadCycle(bool doEntireReadCycle); inline bool getDoEntireReadCycle() const; inline ostream& print(ostream& s) const; protected: RfidReaderAppData(); RfidReaderAppData(const RfidReaderAppData& rhs); virtual PacketDataPtr clone() const; private: static const t_uint m_nodeIdBytes = 4; static const t_uint m_typeBytes = 1; t_uchar m_nodeId[m_nodeIdBytes]; Types m_type; bool m_doEntireReadCycle; }; typedef boost::shared_ptr<RfidReaderAppData> RfidReaderAppDataPtr; // Inline Functions inline RfidReaderAppDataPtr RfidReaderAppData::create() { RfidReaderAppDataPtr p(new RfidReaderAppData()); return p; } inline RfidReaderAppDataPtr RfidReaderAppData::create( const RfidReaderAppData& rhs) { RfidReaderAppDataPtr p = boost::dynamic_pointer_cast<RfidReaderAppData>(rhs.clone()); // If the shared_ptr is empty, then the cast failed. assert(p.get() != 0); return p; } inline t_uint RfidReaderAppData::getSizeInBytes() const { t_uint byteCount = (m_nodeIdBytes + m_typeBytes); switch(getType()) { default: break; } return byteCount; } inline void RfidReaderAppData::setType(RfidReaderAppData::Types type) { m_type = type; } inline RfidReaderAppData::Types RfidReaderAppData::getType() const { return m_type; } inline void RfidReaderAppData::setDoEntireReadCycle( bool doEntireReadCycle) { m_doEntireReadCycle = doEntireReadCycle; } inline bool RfidReaderAppData::getDoEntireReadCycle() const { return m_doEntireReadCycle; } // Overloaded Operators inline ostream& operator<< (ostream& s, const RfidReaderAppData::Types& dataType) { switch(dataType) { case RfidReaderAppData::Types_NoType: s << "NO_TYPE"; break; case RfidReaderAppData::Types_Read: s << "READ"; break; case RfidReaderAppData::Types_Reset: s << "RESET"; break; } return s; } inline ostream& RfidReaderAppData::print(ostream& s) const { s << "type=" << m_type << ", " << "nodeId=" << NodeId(m_nodeId, m_nodeIdBytes); return s; } #endif // RFID_READER_APP_H
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -