00001 00002 #ifndef RFID_TAG_APP_H 00003 #define RFID_TAG_APP_H 00004 00005 #include <algorithm> 00006 using namespace std; 00007 #include <boost/shared_ptr.hpp> 00008 00009 #include "application_layer.hpp" 00010 #include "packet.hpp" 00011 #include "simulator.hpp" 00012 00016 class RfidTagApp : public ApplicationLayer { 00017 public: 00019 typedef boost::shared_ptr<RfidTagApp> RfidTagAppPtr; 00020 00025 static inline RfidTagAppPtr create(NodePtr node); 00026 00033 virtual inline RfidTagAppPtr thisRfidTagApp(); 00034 00041 virtual inline ApplicationLayerPtr thisApplicationLayer(); 00042 00049 virtual inline SimulationEndListenerPtr thisSimulationEndListener(); 00050 00054 void startHandler(); 00055 00059 void stopHandler(); 00060 00066 bool getReplyToReads() const; 00067 00073 void setReplyToReads(bool replyToReads); 00074 00078 virtual void simulationEndHandler(); 00079 00083 inline ostream& print(ostream& s) const; 00084 00085 protected: 00086 00088 RfidTagApp(NodePtr node); 00089 00097 bool handleRecvdPacket(PacketPtr packet, t_uint sendingLayerIdx); 00098 00099 private: 00100 00103 boost::weak_ptr<RfidTagApp> m_weakThis; 00104 00107 bool m_replyToReads; 00108 00114 void sendIdPacket(const NodeId& destination); 00115 00116 }; 00117 typedef boost::shared_ptr<RfidTagApp> RfidTagAppPtr; 00118 00120 // Inline Functions 00122 00123 inline RfidTagAppPtr RfidTagApp::create(NodePtr node) 00124 { 00125 RfidTagAppPtr p(new RfidTagApp(node)); 00126 p->m_weakThis = p; 00127 // weakThis *must* be set before the this function is called. 00128 Simulator::instance()->addSimulationEndListener( 00129 p->thisSimulationEndListener()); 00130 return p; 00131 } 00132 00133 inline RfidTagAppPtr RfidTagApp::thisRfidTagApp() 00134 { 00135 RfidTagAppPtr p(m_weakThis); 00136 return p; 00137 } 00138 00139 inline ApplicationLayerPtr RfidTagApp::thisApplicationLayer() 00140 { 00141 ApplicationLayerPtr p(m_weakThis); 00142 return p; 00143 } 00144 00145 inline SimulationEndListenerPtr RfidTagApp::thisSimulationEndListener() 00146 { 00147 SimulationEndListenerPtr p(m_weakThis); 00148 return p; 00149 } 00150 00152 // Overloaded Operators 00154 00156 // PacketData Subclass 00158 00163 class RfidTagAppData : public PacketData { 00164 public: 00166 typedef boost::shared_ptr<RfidTagAppData> RfidTagAppDataPtr; 00167 00172 enum Types { 00173 Types_NoType, 00174 Types_Reply 00175 }; 00176 00181 static inline RfidTagAppDataPtr create(); 00182 00187 static inline RfidTagAppDataPtr create(const RfidTagAppData& rhs); 00188 00193 virtual inline t_uint getSizeInBytes() const; 00194 00199 void setTagId(const NodeId& nodeId); 00200 00205 NodeId getTagId() const; 00206 00211 inline void setType(Types type); 00212 00217 inline Types getType() const; 00218 00222 inline ostream& print(ostream& s) const; 00223 00224 protected: 00225 00227 RfidTagAppData(); 00228 00230 RfidTagAppData(const RfidTagAppData& rhs); 00231 00238 virtual PacketDataPtr clone() const; 00239 00240 private: 00241 00244 static const t_uint m_nodeIdBytes = 12; 00245 00247 static const t_uint m_typeBytes = 1; 00248 00250 t_uchar m_nodeId[m_nodeIdBytes]; 00251 00253 Types m_type; 00254 00255 }; 00256 typedef boost::shared_ptr<RfidTagAppData> RfidTagAppDataPtr; 00257 00259 // Inline Functions 00261 00262 inline RfidTagAppDataPtr RfidTagAppData::create() 00263 { 00264 RfidTagAppDataPtr p(new RfidTagAppData()); 00265 return p; 00266 } 00267 00268 inline RfidTagAppDataPtr RfidTagAppData::create(const RfidTagAppData& rhs) 00269 { 00270 RfidTagAppDataPtr p = 00271 boost::dynamic_pointer_cast<RfidTagAppData>(rhs.clone()); 00272 // If the shared_ptr is empty, then the cast failed. 00273 assert(p.get() != 0); 00274 return p; 00275 } 00276 00277 inline t_uint RfidTagAppData::getSizeInBytes() const 00278 { 00279 return (m_nodeIdBytes + m_typeBytes); 00280 } 00281 00282 inline void RfidTagAppData::setType(RfidTagAppData::Types type) 00283 { 00284 m_type = type; 00285 } 00286 00287 inline RfidTagAppData::Types RfidTagAppData::getType() const 00288 { 00289 return m_type; 00290 } 00291 00293 // Overloaded Operators 00295 00296 inline ostream& operator<< (ostream& s, 00297 const RfidTagAppData::Types& dataType) 00298 { 00299 switch(dataType) { 00300 case RfidTagAppData::Types_NoType: 00301 s << "NO_TYPE"; 00302 break; 00303 case RfidTagAppData::Types_Reply: 00304 s << "REPLY"; 00305 break; 00306 } 00307 return s; 00308 } 00309 00310 inline ostream& RfidTagAppData::print(ostream& s) const 00311 { 00312 s << "type=" << m_type << ", " << 00313 "nodeId=" << NodeId(m_nodeId, m_nodeIdBytes); 00314 return s; 00315 } 00316 00317 #endif // RFID_TAG_APP_H 00318