00001
00002 #include "rfid_reader_app.hpp"
00003 #include "rfid_tag_app.hpp"
00004 #include "node.hpp"
00005 #include "physical_layer.hpp"
00006
00008
00010
00011 const string RfidReaderApp::m_TAGS_READ_COUNT_STRING = "tagsReadCount";
00012 const string RfidReaderApp::m_TAGS_READ_AT_POWER_COUNT_STRING = "tagsReadCountAtLevel_";
00013 const string RfidReaderApp::m_TAGS_READ_AVG_LATENCY_STRING = "avgTagReadLatency";
00014 const string RfidReaderApp::m_TAGS_READ_PROCESS_AVG_LATENCY_STRING = "avgTagReadProcessLatency";
00015 const string RfidReaderApp::m_LAST_TAG_READ_LATENCY_STRING = "lastTagReadLatency";
00016 const string RfidReaderApp::m_TAG_READ_PROCESS_LATENCY_STRING = "tagReadProcessLatency";
00017 const string RfidReaderApp::m_TAG_READ_LATENCY_STRING = "tagReadLatency";
00018 const string RfidReaderApp::m_TAG_READ_LEVEL_STRING = "tagReadPowerLevel";
00019 const string RfidReaderApp::m_TAG_READ_ID_STRING = "tagReadId";
00020 const string RfidReaderApp::m_TAG_READ_TIME_STRING = "tagReadTime";
00021 const double RfidReaderApp::m_DEFAULT_READ_PERIOD = 60.0;
00022 const t_uint RfidReaderApp::m_DEFAULT_NUM_POWER_CONTROL_LEVELS = 1;
00023
00029 RfidReaderApp::RfidReaderApp(NodePtr node, PhysicalLayerPtr physicalLayer)
00030 : ApplicationLayer(node),
00031 m_firstReadSentTime(0.0),
00032 m_previousReadSentTime(0.0),
00033 m_lastTagRead(0,ReadTagData(0,0.0,0.0)),
00034 m_physicalLayer(physicalLayer),
00035 m_readPeriod(m_DEFAULT_READ_PERIOD), m_doRepeatedReads(false),
00036 m_doReset(true),
00037 m_numPowerControlLevels(m_DEFAULT_NUM_POWER_CONTROL_LEVELS),
00038 m_maxTxPower(0.0),
00039 m_currentTxPowerLevel(m_DEFAULT_NUM_POWER_CONTROL_LEVELS)
00040 {
00041
00042 }
00043
00044 RfidReaderApp::~RfidReaderApp()
00045 {
00046
00047 }
00048
00049 void RfidReaderApp::simulationEndHandler()
00050 {
00051
00052 SimTime readLatencySum(0.0);
00053 SimTime readProcessLatencySum(0.0);
00054 for(TagIterator i = m_readTags.begin(); i != m_readTags.end(); ++i) {
00055 SimTime tagReadProcessLatency =
00056 (i->second.getTimeRead() - m_firstReadSentTime);
00057 readProcessLatencySum += tagReadProcessLatency;
00058
00059 SimTime tagReadLatency = i->second.getReadLatency();
00060 readLatencySum += tagReadLatency;
00061
00062 ostringstream tagIdStream;
00063 tagIdStream << i->second.getReadTagId();
00064 LogStreamManager::instance()->logStatsItem(getNodeId(),
00065 m_TAG_READ_ID_STRING, tagIdStream.str());
00066
00067 ostringstream tagReadLevelStream;
00068 tagReadLevelStream << (i->first + 1);
00069 LogStreamManager::instance()->logStatsItem(getNodeId(),
00070 m_TAG_READ_LEVEL_STRING, tagReadLevelStream.str());
00071
00072 ostringstream tagReadTimeStream;
00073 tagReadTimeStream << i->second.getTimeRead();
00074 LogStreamManager::instance()->logStatsItem(getNodeId(),
00075 m_TAG_READ_TIME_STRING, tagReadTimeStream.str());
00076
00077 ostringstream tagReadLatencyStream;
00078 tagReadLatencyStream << setprecision(8) << tagReadProcessLatency;
00079 LogStreamManager::instance()->logStatsItem(getNodeId(),
00080 m_TAG_READ_PROCESS_LATENCY_STRING, tagReadLatencyStream.str());
00081
00082
00083
00084
00085
00086
00087
00088 }
00089
00090 for(t_uint i = 0; i < m_numPowerControlLevels; ++i) {
00091 pair<TagIterator,TagIterator> p = m_readTags.equal_range(i);
00092
00093 t_uint tagsReadAtPowerTotal = 0;
00094 for(TagIterator j = p.first; j != p.second; ++j)
00095 tagsReadAtPowerTotal++;
00096
00097 ostringstream tagsReadAtPowerTotalStream;
00098 tagsReadAtPowerTotalStream << tagsReadAtPowerTotal;
00099 ostringstream powerLevelStream;
00100 powerLevelStream << m_TAGS_READ_AT_POWER_COUNT_STRING << (i + 1);
00101 LogStreamManager::instance()->logStatsItem(getNodeId(),
00102 powerLevelStream.str(), tagsReadAtPowerTotalStream.str());
00103
00104 }
00105
00106 t_uint tagsReadTotal = m_readTags.size();
00107 ostringstream tagsReadTotalStream;
00108 tagsReadTotalStream << tagsReadTotal;
00109 LogStreamManager::instance()->logStatsItem(getNodeId(),
00110 m_TAGS_READ_COUNT_STRING, tagsReadTotalStream.str());
00111
00112 double readLatencyAverage = 0.0;
00113 double readProcessLatencyAverage = 0.0;
00114 if(tagsReadTotal > 0) {
00115 readLatencyAverage =
00116 readLatencySum.getTimeInSeconds() / tagsReadTotal;
00117 readProcessLatencyAverage =
00118 readProcessLatencySum.getTimeInSeconds() / tagsReadTotal;
00119 }
00120
00121
00122
00123
00124
00125
00126
00127
00128 ostringstream readProcessLatencyStream;
00129 readProcessLatencyStream << setprecision(8) <<
00130 readProcessLatencyAverage;
00131 LogStreamManager::instance()->logStatsItem(getNodeId(),
00132 m_TAGS_READ_PROCESS_AVG_LATENCY_STRING,
00133 readProcessLatencyStream.str());
00134
00135 SimTime lastReadLatency(0.0);
00136 if(!m_readTags.empty()) {
00137 lastReadLatency = (m_lastTagRead.second.getTimeRead() -
00138 m_firstReadSentTime);
00139 }
00140 ostringstream lastTagReadLatencyStream;
00141 lastTagReadLatencyStream << setprecision(8) << lastReadLatency;
00142 LogStreamManager::instance()->logStatsItem(getNodeId(),
00143 m_LAST_TAG_READ_LATENCY_STRING, lastTagReadLatencyStream.str());
00144 }
00145
00146 void RfidReaderApp::startHandler()
00147 {
00148
00149
00150
00151 m_maxTxPower = m_physicalLayer->getMaxTxPower();
00152 doReadProcess();
00153 }
00154
00155 void RfidReaderApp::stopHandler()
00156 {
00157 m_readTimer->stop();
00158 }
00159
00160 bool RfidReaderApp::handleRecvdPacket(PacketPtr packet,
00161 t_uint recvLayerIdx)
00162 {
00163 if(!m_isRunning)
00164 return false;
00165
00166 RfidTagAppDataPtr tagData =
00167 boost::dynamic_pointer_cast<RfidTagAppData>
00168 (packet->getData(Packet::DataTypes_Application));
00169
00170 RfidReaderAppDataPtr readerData =
00171 boost::dynamic_pointer_cast<RfidReaderAppData>
00172 (packet->getData(Packet::DataTypes_Application));
00173
00174 bool wasSuccessful = false;
00175
00176
00177
00178 if(tagData.get() != 0) {
00179 NodeId readTagId = tagData->getTagId();
00180
00181 bool isNewTagId =
00182 (m_readTagIds.find(readTagId) == m_readTagIds.end());
00183 if(isNewTagId) {
00184 SimTime timeRead = Simulator::instance()->currentTime();
00185 m_lastTagRead =
00186 make_pair(m_currentTxPowerLevel, ReadTagData(readTagId,
00187 timeRead, m_previousReadSentTime));
00188 m_readTags.insert(m_lastTagRead);
00189 pair<set<NodeId>::iterator, bool> p =
00190 m_readTagIds.insert(readTagId);
00191 assert(p.second);
00192 }
00193
00194 wasSuccessful = true;
00195 } else if(readerData.get() != 0) {
00196
00197 wasSuccessful = true;
00198 }
00199
00200 return wasSuccessful;
00201 }
00202
00203 void RfidReaderApp::signalReadEnd()
00204 {
00205 m_currentTxPowerLevel++;
00206 doNextRead();
00207 }
00208
00209 void RfidReaderApp::doNextRead()
00210 {
00211 if(m_currentTxPowerLevel < m_numPowerControlLevels) {
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224 double nextTxPower =
00225 m_maxTxPower *
00226 pow(((m_currentTxPowerLevel + 1) /
00227 static_cast<double>(m_numPowerControlLevels)), 2);
00228
00229 if(m_DEBUG_POWER_CONTROL) {
00230 ostringstream debugStream;
00231 debugStream << __PRETTY_FUNCTION__ << " nextTxPower: " <<
00232 nextTxPower << ", maxTxPower: " << m_maxTxPower;
00233 LogStreamManager::instance()->logDebugItem(debugStream.str());
00234 }
00235 m_previousReadSentTime = Simulator::instance()->currentTime();
00236 sendReadPacket(nextTxPower);
00237 } else {
00238
00239 if(m_doRepeatedReads)
00240 m_readTimer->reschedule(m_readPeriod);
00241 }
00242 }
00243
00244 void RfidReaderApp::doReadProcess()
00245 {
00246
00247
00248
00249 if(m_doReset)
00250 sendResetPacket();
00251
00252 assert(m_numPowerControlLevels > 0);
00253
00254 m_firstReadSentTime = Simulator::instance()->currentTime();
00255 m_currentTxPowerLevel = 0;
00256 doNextRead();
00257 }
00258
00259 void RfidReaderApp::sendResetPacket()
00260 {
00261 PacketPtr packetToSend = Packet::create();
00262 packetToSend->setDestination(NodeId::broadcastDestination());
00263
00264 RfidReaderAppDataPtr appData = RfidReaderAppData::create();
00265 appData->setType(RfidReaderAppData::Types_Reset);
00266 appData->setReaderId(getNodeId());
00267 packetToSend->addData(Packet::DataTypes_Application, *appData);
00268
00269 sendToQueue(packetToSend);
00270 }
00271
00272 void RfidReaderApp::sendReadPacket(double txPower)
00273 {
00274 PacketPtr packetToSend = Packet::create();
00275 packetToSend->setTxPower(txPower);
00276 packetToSend->setDestination(NodeId::broadcastDestination());
00277
00278 RfidReaderAppDataPtr appData = RfidReaderAppData::create();
00279 appData->setType(RfidReaderAppData::Types_Read);
00280 appData->setReaderId(getNodeId());
00281
00282
00283 if(packetToSend->getTxPower() == m_maxTxPower)
00284 appData->setDoEntireReadCycle(true);
00285 else
00286 appData->setDoEntireReadCycle(false);
00287
00288 packetToSend->addData(Packet::DataTypes_Application, *appData);
00289
00290
00291 sendToQueue(packetToSend);
00292 }
00293
00295
00297
00298 RfidReaderAppData::RfidReaderAppData()
00299 : m_type(RfidReaderAppData::Types_NoType), m_doEntireReadCycle(false)
00300 {
00301 fill(m_nodeId, &m_nodeId[m_nodeIdBytes], 0);
00302 }
00303
00304 RfidReaderAppData::RfidReaderAppData(const RfidReaderAppData& rhs)
00305 : PacketData(rhs), m_type(rhs.m_type),
00306 m_doEntireReadCycle(rhs.m_doEntireReadCycle)
00307 {
00308 copy(rhs.m_nodeId, &rhs.m_nodeId[m_nodeIdBytes], m_nodeId);
00309 }
00310
00311 PacketDataPtr RfidReaderAppData::clone() const
00312 {
00313 PacketDataPtr p(new RfidReaderAppData(*this));
00314 return p;
00315 }
00316
00317 void RfidReaderAppData::setReaderId(const NodeId& nodeId)
00318 {
00319 nodeId.writeToByteArray(m_nodeId, m_nodeIdBytes);
00320 }
00321
00322 NodeId RfidReaderAppData::getReaderId() const
00323 {
00324 NodeId nodeId(m_nodeId, m_nodeIdBytes);
00325 return nodeId;
00326 }
00327
00328