rfid_reader_app.hpp

00001 
00002 #ifndef RFID_READER_APP_H
00003 #define RFID_READER_APP_H
00004 
00005 #include <map>
00006 #include <set>
00007 #include <algorithm>
00008 using namespace std;
00009 #include <boost/shared_ptr.hpp>
00010 
00011 #include "application_layer.hpp"
00012 #include "packet.hpp"
00013 #include "timer.hpp"
00014 #include "simulator.hpp"
00015 
00016 class PhysicalLayer;
00017 typedef boost::shared_ptr<PhysicalLayer> PhysicalLayerPtr;
00018 
00022 class ReadTagData {
00023 friend ostream& operator<< (ostream& s, const ReadTagData& readTagData);
00024 public:
00026    ReadTagData(const NodeId& readTagId, const SimTime& timeRead,
00027       const SimTime& timeReadSent)
00028       : m_readTagId(readTagId), m_timeRead(timeRead), 
00029       m_timeReadSent(timeReadSent) {}
00030 
00035    NodeId getReadTagId() const
00036    {
00037       return m_readTagId;
00038    }
00039 
00044    SimTime getTimeRead() const
00045    {
00046       return m_timeRead;
00047    }
00048 
00054    SimTime getTimeReadSent() const
00055    {
00056       return m_timeReadSent;
00057    }
00058 
00064    SimTime getReadLatency() const
00065    {
00066       return (m_timeRead - m_timeReadSent);
00067    }
00068 
00069 private:
00070    NodeId m_readTagId;
00071    SimTime m_timeRead;
00072    SimTime m_timeReadSent;
00073 };
00074 
00075 inline ostream& operator<< (ostream& s, const ReadTagData& readTagData)
00076 {
00077    return s << "[ tagId=" << readTagData.m_readTagId << 
00078       ", timeRead=" << readTagData.m_timeRead << 
00079       ", timeReadSent=" << readTagData.m_timeReadSent << " ]";
00080 }
00081 
00085 class RfidReaderApp : public ApplicationLayer {
00086 friend class RfidReaderAppReadEvent;
00087 public:
00089    typedef boost::shared_ptr<RfidReaderApp> RfidReaderAppPtr;
00091    typedef multimap<t_uint,ReadTagData>::const_iterator TagIterator;
00092 
00094    virtual ~RfidReaderApp();
00095 
00100    static inline RfidReaderAppPtr create(NodePtr node, 
00101       PhysicalLayerPtr physicalLayer);
00102 
00109    virtual inline RfidReaderAppPtr thisRfidReaderApp();
00110 
00117    virtual inline ApplicationLayerPtr thisApplicationLayer();
00118 
00125    virtual inline SimulationEndListenerPtr thisSimulationEndListener();
00126 
00130    void startHandler();
00131 
00135    void stopHandler();
00136 
00140    virtual void simulationEndHandler();
00141 
00148    inline void setReadPeriod(SimTime readPeriod);
00149 
00155    inline void setDoRepeatedReads(bool doRepeatedReads);
00156 
00162    inline void setDoReset(bool doReset);
00163 
00170    inline SimTime getNextReadTime() const;
00171 
00176    inline SimTime getReadPeriod() const;
00177 
00183    inline bool getDoRepeatedReads() const;
00184 
00190    inline bool getDoReset() const;
00191 
00203    inline void setNumPowerControlLevels(t_uint numPowerControlLevels);
00204 
00211    inline t_uint getNumPowerControlLevels() const;
00212 
00220    void signalReadEnd();
00221 
00222 protected:
00223 
00225 
00226    static const string m_TAGS_READ_COUNT_STRING;
00227    static const string m_TAGS_READ_AT_POWER_COUNT_STRING;
00228    static const string m_TAGS_READ_AVG_LATENCY_STRING;
00229    static const string m_TAGS_READ_PROCESS_AVG_LATENCY_STRING;
00230    static const string m_LAST_TAG_READ_LATENCY_STRING;
00231    static const string m_TAG_READ_PROCESS_LATENCY_STRING;
00232    static const string m_TAG_READ_LATENCY_STRING;
00233    static const string m_TAG_READ_ID_STRING;
00234    static const string m_TAG_READ_LEVEL_STRING;
00235    static const string m_TAG_READ_TIME_STRING;
00237 
00239    SimTime m_firstReadSentTime;
00240 
00242    SimTime m_previousReadSentTime;
00243 
00246    multimap<t_uint,ReadTagData> m_readTags;
00247 
00249    pair<t_uint,ReadTagData> m_lastTagRead;
00250 
00252    set<NodeId> m_readTagIds;
00253 
00255    RfidReaderApp(NodePtr node, PhysicalLayerPtr physicalLayer);
00256 
00264    bool handleRecvdPacket(PacketPtr packet, t_uint sendingLayerIdx);
00265 
00269    virtual void doReadProcess();
00270 
00271 private: 
00272 
00274    static const bool m_DEBUG_POWER_CONTROL = true;
00275 
00278    boost::weak_ptr<RfidReaderApp> m_weakThis;
00279 
00281    static const double m_DEFAULT_READ_PERIOD;
00282 
00284    static const t_uint m_DEFAULT_NUM_POWER_CONTROL_LEVELS;
00285 
00287    PhysicalLayerPtr m_physicalLayer;
00288 
00290    TimerPtr m_readTimer;
00291 
00295    SimTime m_readPeriod;
00296 
00301    bool m_doRepeatedReads;
00302 
00307    bool m_doReset;
00308 
00310    t_uint m_numPowerControlLevels;
00311 
00313    double m_maxTxPower;
00314 
00317    t_uint m_currentTxPowerLevel;
00318 
00324    void sendReadPacket(double txPower);
00325 
00329    void sendResetPacket();
00330 
00335    void doNextRead();
00336 
00337 };
00338 typedef boost::shared_ptr<RfidReaderApp> RfidReaderAppPtr;
00339 
00341 // Event Subclasses
00343 
00348 class RfidReaderAppReadEvent : public Event {
00349 public:
00351    typedef boost::shared_ptr<RfidReaderAppReadEvent> 
00352       RfidReaderAppReadEventPtr;
00353 
00358    static inline RfidReaderAppReadEventPtr create(
00359       RfidReaderAppPtr readerApp)
00360    {
00361       RfidReaderAppReadEventPtr p(new RfidReaderAppReadEvent(readerApp));
00362       return p;
00363    }
00364 
00365    void execute()
00366    {
00367       m_readerApp->doReadProcess();
00368    }
00369 
00370 protected:
00372    RfidReaderAppReadEvent(RfidReaderAppPtr readerApp) 
00373       : Event()
00374    { 
00375       m_readerApp = readerApp;
00376    }
00377 private:
00378    RfidReaderAppPtr m_readerApp;
00379 };
00380 typedef boost::shared_ptr<RfidReaderAppReadEvent> 
00381    RfidReaderAppReadEventPtr;
00382 
00384 // Back to RfidReaderApp Class
00386 
00388 // Inline Functions
00390 
00391 inline RfidReaderAppPtr RfidReaderApp::create(NodePtr node, 
00392    PhysicalLayerPtr physicalLayer)
00393 {
00394    RfidReaderAppPtr p(new RfidReaderApp(node, physicalLayer));
00395    // weakThis *must* be set before the thisReaderApp
00396    // function is called.
00397    p->m_weakThis = p;
00398    RfidReaderAppReadEventPtr readEvent = 
00399       RfidReaderAppReadEvent::create(p->thisRfidReaderApp());
00400    p->m_readTimer = Timer::create(p->getNode(), readEvent);
00401 
00402    Simulator::instance()->addSimulationEndListener(
00403       p->thisSimulationEndListener());
00404 
00405    return p;
00406 }
00407 
00408 inline RfidReaderAppPtr RfidReaderApp::thisRfidReaderApp()
00409 {
00410    RfidReaderAppPtr p(m_weakThis);
00411    return p;
00412 }
00413 
00414 inline ApplicationLayerPtr RfidReaderApp::thisApplicationLayer()
00415 {
00416    ApplicationLayerPtr p(m_weakThis);
00417    return p;
00418 }
00419 
00420 inline SimulationEndListenerPtr RfidReaderApp::thisSimulationEndListener()
00421 {
00422    SimulationEndListenerPtr p(m_weakThis);
00423    return p;
00424 }
00425 inline void RfidReaderApp::setReadPeriod(SimTime readPeriod)
00426 {
00427    m_readPeriod = readPeriod;
00428 }
00429 
00430 inline void RfidReaderApp::setDoRepeatedReads(bool doRepeatedReads)
00431 {
00432    m_doRepeatedReads = doRepeatedReads;
00433 }
00434 
00435 inline void RfidReaderApp::setDoReset(bool doReset)
00436 {
00437    m_doReset = doReset;
00438 }
00439 
00440 inline SimTime RfidReaderApp::getNextReadTime() const
00441 {
00442    SimTime nextReadTime(0.0);
00443    if(m_readTimer->isRunning()) {
00444       nextReadTime = (m_readTimer->timeRemaining() + 
00445          Simulator::instance()->currentTime());
00446    }
00447    return nextReadTime;
00448 }
00449 
00450 inline SimTime RfidReaderApp::getReadPeriod() const
00451 {
00452    return m_readPeriod;
00453 }
00454 
00455 inline bool RfidReaderApp::getDoRepeatedReads() const
00456 {
00457    return m_doRepeatedReads;
00458 }
00459 
00460 inline bool RfidReaderApp::getDoReset() const
00461 {
00462    return m_doReset;
00463 }
00464 
00465 inline void RfidReaderApp::setNumPowerControlLevels(
00466    t_uint numPowerControlLevels)
00467 {
00468    assert(numPowerControlLevels > 0);
00469    m_numPowerControlLevels = numPowerControlLevels;
00470 }
00471 
00472 inline t_uint RfidReaderApp::getNumPowerControlLevels() const
00473 {
00474    return m_numPowerControlLevels;
00475 }
00476 
00478 // Overloaded Operators
00480 
00482 // PacketData Subclass
00484 
00489 class RfidReaderAppData : public PacketData {
00490 public:
00492    typedef boost::shared_ptr<RfidReaderAppData> RfidReaderAppDataPtr;
00493 
00498    enum Types {
00499       Types_NoType, 
00500       Types_Read, 
00501       Types_Reset 
00502    };
00503 
00508    static inline RfidReaderAppDataPtr create();
00509 
00514    static inline RfidReaderAppDataPtr create(
00515       const RfidReaderAppData& rhs);
00516 
00521    virtual inline t_uint getSizeInBytes() const;
00522 
00527    void setReaderId(const NodeId& nodeId);
00528 
00533    NodeId getReaderId() const;
00534 
00539    inline void setType(Types type);
00540 
00545    inline Types getType() const;
00546 
00556    inline void setDoEntireReadCycle(bool doEntireReadCycle);
00557 
00567    inline bool getDoEntireReadCycle() const;
00568 
00572    inline ostream& print(ostream& s) const;
00573 
00574 protected:
00575 
00577    RfidReaderAppData();
00578 
00580    RfidReaderAppData(const RfidReaderAppData& rhs);
00581 
00588    virtual PacketDataPtr clone() const;
00589 
00590 private:
00591 
00593    static const t_uint m_nodeIdBytes = 4;
00594 
00596    static const t_uint m_typeBytes = 1;
00597 
00599    t_uchar m_nodeId[m_nodeIdBytes];
00600 
00602    Types m_type;
00603 
00608    bool m_doEntireReadCycle;
00609 
00610 };
00611 typedef boost::shared_ptr<RfidReaderAppData> RfidReaderAppDataPtr;
00612 
00614 // Inline Functions
00616 
00617 inline RfidReaderAppDataPtr RfidReaderAppData::create()
00618 {
00619    RfidReaderAppDataPtr p(new RfidReaderAppData());
00620    return p;
00621 }
00622 
00623 inline RfidReaderAppDataPtr RfidReaderAppData::create(
00624    const RfidReaderAppData& rhs)
00625 {
00626    RfidReaderAppDataPtr p = 
00627       boost::dynamic_pointer_cast<RfidReaderAppData>(rhs.clone());
00628    // If the shared_ptr is empty, then the cast failed.
00629    assert(p.get() != 0);
00630    return p;
00631 }
00632 
00633 inline t_uint RfidReaderAppData::getSizeInBytes() const
00634 {
00635    t_uint byteCount = (m_nodeIdBytes + m_typeBytes);
00636 
00637    switch(getType()) {
00638    default:
00639       break;
00640    }
00641 
00642    return byteCount;
00643 }
00644 
00645 inline void RfidReaderAppData::setType(RfidReaderAppData::Types type)
00646 {
00647    m_type = type;
00648 }
00649 
00650 inline RfidReaderAppData::Types RfidReaderAppData::getType() const
00651 {
00652    return m_type;
00653 }
00654 
00655 inline void RfidReaderAppData::setDoEntireReadCycle(
00656    bool doEntireReadCycle)
00657 {
00658    m_doEntireReadCycle = doEntireReadCycle;
00659 }
00660 
00661 inline bool RfidReaderAppData::getDoEntireReadCycle() const
00662 {
00663    return m_doEntireReadCycle;
00664 }
00665 
00667 // Overloaded Operators
00669 
00670 inline ostream& operator<< (ostream& s, 
00671       const RfidReaderAppData::Types& dataType)
00672 {
00673    switch(dataType) {
00674    case RfidReaderAppData::Types_NoType:
00675       s << "NO_TYPE";
00676       break;
00677    case RfidReaderAppData::Types_Read:
00678       s << "READ";
00679       break;
00680    case RfidReaderAppData::Types_Reset:
00681       s << "RESET";
00682       break;
00683    }
00684    return s;
00685 }
00686 
00687 inline ostream& RfidReaderAppData::print(ostream& s) const
00688 {
00689    s << "type=" << m_type << ", " <<
00690       "nodeId=" << NodeId(m_nodeId, m_nodeIdBytes);
00691    return s;
00692 }
00693 
00694 #endif // RFID_READER_APP_H
00695 

Generated on Tue Dec 12 17:04:38 2006 for rfidsim by  doxygen 1.4.7