00001
00002 #ifndef PHYSICAL_LAYER_H
00003 #define PHYSICAL_LAYER_H
00004
00005 #include <map>
00006 #include <boost/shared_ptr.hpp>
00007
00008 #include "utility.hpp"
00009 #include "communication_layer.hpp"
00010 #include "wireless_comm_signal.hpp"
00011 #include "wireless_channel_manager.hpp"
00012 #include "log_stream_manager.hpp"
00013 #include "node.hpp"
00014 #include "timer.hpp"
00015
00016 typedef map<WirelessCommSignalPtr,double> SignalStrengthMap;
00017
00022 class PhysicalLayer : public CommunicationLayer {
00023 friend class SignalRecvEvent;
00024 public:
00026 typedef boost::shared_ptr<PhysicalLayer> PhysicalLayerPtr;
00027
00031 virtual ~PhysicalLayer();
00032
00039 virtual PhysicalLayerPtr thisPhysicalLayer() = 0;
00040
00046 inline void setWirelessChannelManager(
00047 WirelessChannelManagerPtr wirelessChannelManager);
00048
00062 bool recvPendingSignal(WirelessCommSignalPtr signal,
00063 double recvdSignalStrength);
00064
00070 virtual bool sendSignal(WirelessCommSignalPtr signal);
00071
00077 bool isTransmitting() const;
00078
00086 inline void setSignalSendingDelay(SimTime signalSendingDelay);
00087
00095 inline SimTime getSignalSendingDelay() const;
00096
00106 bool recvFromLayer(CommunicationLayer::Directions direction,
00107 PacketPtr packet, t_uint sendingLayerIdx);
00108
00115 Location getLocation() const;
00116
00125 void addSignal(WirelessCommSignalPtr signal,
00126 double signalStrength);
00127
00135 void removeSignal(WirelessCommSignalPtr signal);
00136
00144 bool pendingSignalIsWeak();
00145
00156 bool captureSignal(double signalStrength) const;
00157
00165 double getCulmulativeSignalStrength() const;
00166
00171 inline double getNoiseFloor() const;
00172
00181 void setPendingSignal(WirelessCommSignalPtr signal);
00182
00188 inline void setPendingSignalError(bool hasError);
00189
00196 inline bool getPendingSignalError() const;
00197
00206 ConstWirelessCommSignalPtr getPendingSignal() const;
00207
00217 double getPendingSignalStrength();
00218
00228 double getPendingSignalSinr();
00229
00235 void resetPendingSignal();
00236
00242 void resetRecvSignals();
00243
00248 bool channelCarrierSensedBusy() const;
00249
00255 inline void setCurrentTxPower(double txPower);
00256
00261 inline double getCurrentTxPower() const;
00262
00267 inline double getMaxTxPower() const;
00268
00273 inline void setRxThreshold(double rxThreshold);
00274
00279 inline double getRxThreshold() const;
00280
00285 inline void setCsThreshold(double csThreshold);
00286
00291 inline double getCsThreshold() const;
00292
00298 inline void setCaptureThreshold(double captureThreshold);
00299
00305 inline double getCaptureThreshold() const;
00306
00312 inline void setMinimumSignalStrength(double minimumSignalStrength);
00313
00319 inline double getMinimumSignalStrength() const;
00320
00325 inline void setDataRate(double dataRate);
00326
00331 inline double getDataRate() const;
00332
00337 inline void setBandwidth(double bandwidth);
00338
00343 inline double getBandwidth() const;
00344
00349 inline double getGain() const;
00350
00356 inline double getWavelength() const;
00357
00362 inline CommunicationLayer::Types getLayerType() const;
00363
00364 protected:
00365
00368 WirelessChannelManagerPtr m_wirelessChannelManagerPtr;
00369
00373 PhysicalLayer(NodePtr node);
00374
00384 virtual void recvdErrorFreeSignal(WirelessCommSignalPtr signal,
00385 double recvdSignalStrength);
00386
00394 inline void setMaxTxPower(double txPower);
00395
00402 bool scheduleSignal(WirelessCommSignalPtr signal);
00403
00404 private:
00405
00407 static const double m_DEFAULT_TX_POWER;
00408
00410 static const double m_DEFAULT_MAX_TX_POWER;
00411
00413 static const double m_DEFAULT_RX_THRESHOLD;
00414
00416 static const double m_DEFAULT_CS_THRESHOLD;
00417
00419 static const double m_DEFAULT_CAPTURE_THRESHOLD;
00420
00422 static const double m_DEFAULT_MINIMUM_SIGNAL_STRENGTH;
00423
00425 static const double m_DEFAULT_DATA_RATE;
00426
00428 static const double m_DEFAULT_BANDWIDTH;
00429
00431 static const double m_RADIO_TEMPERATURE;
00432
00434 static const double m_RADIO_NOISE_FACTOR;
00435
00437 static const double m_BOLTZMANNS_CONSTANT;
00438
00440 static const t_uint m_PHYSICAL_QUEUE_LENGTH;
00441
00443 static const bool m_DEBUG_SIGNAL_CAPTURE = true;
00444 static const bool m_DEBUG_TRANSMIT_POWER = true;
00445
00449 double m_currentTxPower;
00450
00454 double m_maxTxPower;
00455
00459 double m_rxThreshold;
00460
00464 double m_csThreshold;
00465
00469 double m_captureThreshold;
00470
00476 double m_minimumSignalStrength;
00477
00481 double m_dataRate;
00482
00486 double m_bandwidth;
00487
00492 bool m_pendingRecvSignalError;
00493
00500 SignalStrengthMap m_signalStrengths;
00501
00507 WirelessCommSignalPtr m_pendingRecvSignal;
00508
00512 SimTime m_signalSendingDelay;
00513
00517 TimerPtr m_transmittingTimer;
00518
00519 };
00520 typedef boost::shared_ptr<PhysicalLayer> PhysicalLayerPtr;
00521
00523
00525
00526 inline void PhysicalLayer::setWirelessChannelManager(
00527 WirelessChannelManagerPtr wirelessChannelManager)
00528 {
00529 m_wirelessChannelManagerPtr = wirelessChannelManager;
00530 }
00531
00532 inline void PhysicalLayer::setCurrentTxPower(double txPower)
00533 {
00534 assert(txPower > 0.0);
00535 m_currentTxPower = txPower;
00536 }
00537
00538 inline double PhysicalLayer::getCurrentTxPower() const
00539 {
00540 return m_currentTxPower;
00541 }
00542
00543 inline void PhysicalLayer::setMaxTxPower(double txPower)
00544 {
00545 assert(txPower > 0.0);
00546 m_maxTxPower = txPower;
00547 m_currentTxPower = min(m_currentTxPower, m_maxTxPower);
00548 }
00549
00550 inline double PhysicalLayer::getMaxTxPower() const
00551 {
00552 return m_maxTxPower;
00553 }
00554
00555 inline void PhysicalLayer::setRxThreshold(double rxThreshold)
00556 {
00557 assert(rxThreshold > 0.0);
00558 m_rxThreshold = rxThreshold;
00559 }
00560
00561 inline double PhysicalLayer::getRxThreshold() const
00562 {
00563 return m_rxThreshold;
00564 }
00565
00566 inline void PhysicalLayer::setCsThreshold(double csThreshold)
00567 {
00568 assert(csThreshold > 0.0);
00569 m_csThreshold = csThreshold;
00570 }
00571
00572 inline double PhysicalLayer::getCsThreshold() const
00573 {
00574 return m_csThreshold;
00575 }
00576
00577 inline void PhysicalLayer::setCaptureThreshold(double captureThreshold)
00578 {
00579 assert(captureThreshold > 0.0);
00580 m_captureThreshold = captureThreshold;
00581 }
00582
00583 inline double PhysicalLayer::getCaptureThreshold() const
00584 {
00585 return m_captureThreshold;
00586 }
00587
00588 inline void PhysicalLayer::setMinimumSignalStrength(
00589 double minimumSignalStrength)
00590 {
00591 assert(minimumSignalStrength > 0.0);
00592 m_minimumSignalStrength = minimumSignalStrength;
00593 }
00594
00595 inline double PhysicalLayer::getMinimumSignalStrength() const
00596 {
00597 return m_minimumSignalStrength;
00598 }
00599
00600 inline void PhysicalLayer::setDataRate(double dataRate)
00601 {
00602 assert(dataRate > 0.0);
00603 m_dataRate = dataRate;
00604 }
00605
00606 inline double PhysicalLayer::getDataRate() const
00607 {
00608 return m_dataRate;
00609 }
00610
00611 inline void PhysicalLayer::setBandwidth(double bandwidth)
00612 {
00613 assert(bandwidth > 0.0);
00614 m_bandwidth = bandwidth;
00615 }
00616
00617 inline double PhysicalLayer::getBandwidth() const
00618 {
00619 return m_bandwidth;
00620 }
00621
00622 inline double PhysicalLayer::getNoiseFloor() const
00623 {
00624 return (m_RADIO_NOISE_FACTOR * m_BOLTZMANNS_CONSTANT *
00625 m_RADIO_TEMPERATURE * getBandwidth());
00626 }
00627
00628 inline double PhysicalLayer::getGain() const
00629 {
00630
00631 return 1.0;
00632 }
00633
00634 inline void PhysicalLayer::setPendingSignalError(bool hasError)
00635 {
00636 m_pendingRecvSignalError = hasError;
00637 }
00638
00639 inline bool PhysicalLayer::getPendingSignalError() const
00640 {
00641 return m_pendingRecvSignalError;
00642 }
00643
00644 inline double PhysicalLayer::getWavelength() const
00645 {
00646 return (SPEED_OF_LIGHT / m_bandwidth);
00647 }
00648
00649 inline void PhysicalLayer::setSignalSendingDelay(
00650 SimTime signalSendingDelay)
00651 {
00652 m_signalSendingDelay = signalSendingDelay;
00653 }
00654
00655 inline SimTime PhysicalLayer::getSignalSendingDelay() const
00656 {
00657 return m_signalSendingDelay;
00658 }
00659
00660 inline CommunicationLayer::Types PhysicalLayer::getLayerType() const
00661 {
00662 return CommunicationLayer::Types_Physical;
00663 }
00664
00666
00668
00670
00672
00676 class SignalRecvEvent : public Event {
00677 public:
00679 typedef boost::shared_ptr<SignalRecvEvent> SignalRecvEventPtr;
00680
00682 SignalRecvEvent(WirelessChannelManagerPtr channelManager,
00683 PhysicalLayerPtr sender, WirelessCommSignalPtr signal)
00684 : Event(), m_channelManager(channelManager),
00685 m_sender(sender), m_signal(signal)
00686 {
00687 assert(m_channelManager.get() != 0);
00688 }
00689
00690 void execute()
00691 {
00692 assert(m_channelManager.get() != 0);
00693 LogStreamManager::instance()->logPktSendItem(
00694 m_sender->getNodeId(), m_sender->getLayerType(),
00695 *(m_signal->getPacketPtr()));
00696 assert(m_sender->m_transmittingTimer.get() != 0);
00697 assert(!m_sender->m_transmittingTimer->isRunning());
00698
00699
00700 m_sender->m_transmittingTimer->reschedule(m_signal->getDuration());
00701 m_channelManager->recvSignal(m_sender, m_signal);
00702 }
00703
00704 private:
00705 WirelessChannelManagerPtr m_channelManager;
00706 PhysicalLayerPtr m_sender;
00707 WirelessCommSignalPtr m_signal;
00708 };
00709 typedef boost::shared_ptr<SignalRecvEvent> SignalRecvEventPtr;
00710
00711 #endif // PHYSICAL_LAYER_H
00712