rfid_tag_app.cpp

00001 
00002 #include "rfid_tag_app.hpp"
00003 #include "rfid_reader_app.hpp"
00004 #include "node.hpp"
00005 
00007 // RfidTagApp functions
00009 
00010 RfidTagApp::RfidTagApp(NodePtr node)
00011    : ApplicationLayer(node), m_replyToReads(true)
00012 {
00013 
00014 }
00015 
00016 void RfidTagApp::simulationEndHandler()
00017 {
00018 
00019 }
00020 
00021 void RfidTagApp::startHandler()
00022 {
00023 
00024 }
00025 
00026 void RfidTagApp::stopHandler()
00027 {
00028 
00029 }
00030 
00031 bool RfidTagApp::getReplyToReads() const
00032 {
00033    return m_replyToReads;
00034 }
00035 
00036 void RfidTagApp::setReplyToReads(bool replyToReads)
00037 {
00038    m_replyToReads = replyToReads;
00039 }
00040 
00041 bool RfidTagApp::handleRecvdPacket(PacketPtr packet, t_uint recvLayerIdx)
00042 {
00043    if(!m_isRunning)
00044       return false;
00045 
00046    RfidReaderAppDataPtr readerData = 
00047       boost::dynamic_pointer_cast<RfidReaderAppData>
00048       (packet->getData(Packet::DataTypes_Application));
00049 
00050    bool wasSuccessful = false;
00051    // If the shared_ptr is empty, then either the cast failed
00052    // (which could be the result of the packet having no
00053    // application data).
00054    if(readerData.get() != 0) {
00055       switch(readerData->getType()) {
00056       case RfidReaderAppData::Types_Read:
00057          if(m_replyToReads) {
00058             sendIdPacket(readerData->getReaderId());
00059          }
00060          wasSuccessful = true;
00061          break;
00062       case RfidReaderAppData::Types_Reset:
00063          m_replyToReads = true;
00064          wasSuccessful = true;
00065          break;
00066       default:
00067          wasSuccessful = false;
00068       }
00069    }
00070 
00071    return wasSuccessful;
00072 }
00073 
00074 void RfidTagApp::sendIdPacket(const NodeId& destination)
00075 {
00076    PacketPtr packetToSend = Packet::create();
00077    packetToSend->setDestination(destination);
00078 
00079    RfidTagAppDataPtr appData = RfidTagAppData::create();
00080    appData->setTagId(getNodeId());
00081    packetToSend->addData(Packet::DataTypes_Application, *appData);
00082 
00083    //sendToLayer(CommunicationLayer::Directions_Lower, packetToSend);
00084    sendToQueue(packetToSend);
00085 }
00086 
00088 // RfidTagAppData functions
00090 
00091 RfidTagAppData::RfidTagAppData()
00092    : m_type(RfidTagAppData::Types_NoType)
00093 {
00094    fill(m_nodeId, &m_nodeId[m_nodeIdBytes], 0);
00095 }
00096 
00097 RfidTagAppData::RfidTagAppData(const RfidTagAppData& rhs)
00098    : PacketData(rhs), m_type(rhs.m_type)
00099 {
00100    copy(rhs.m_nodeId, &rhs.m_nodeId[m_nodeIdBytes], m_nodeId);
00101 }
00102 
00103 PacketDataPtr RfidTagAppData::clone() const
00104 {
00105     PacketDataPtr p(new RfidTagAppData(*this));
00106     return p;
00107 }
00108 
00109 void RfidTagAppData::setTagId(const NodeId& nodeId)
00110 {
00111    nodeId.writeToByteArray(m_nodeId, m_nodeIdBytes);
00112 }
00113 
00114 NodeId RfidTagAppData::getTagId() const
00115 {
00116    NodeId nodeId(m_nodeId, m_nodeIdBytes);
00117    return nodeId;
00118 }
00119 
00120 

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