forked from matthewjmiller1/RFIDSIM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
physical_layer.hpp
712 lines (596 loc) · 18.8 KB
/
physical_layer.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
#ifndef PHYSICAL_LAYER_H
#define PHYSICAL_LAYER_H
#include <map>
#include <boost/shared_ptr.hpp>
#include "utility.hpp"
#include "communication_layer.hpp"
#include "wireless_comm_signal.hpp"
#include "wireless_channel_manager.hpp"
#include "log_stream_manager.hpp"
#include "node.hpp"
#include "timer.hpp"
typedef map<WirelessCommSignalPtr,double> SignalStrengthMap;
/**
* The class for the physical layer of a node (i.e., the
* radio).
*/
class PhysicalLayer : public CommunicationLayer {
friend class SignalRecvEvent;
public:
/// Smart pointer that clients should use.
typedef boost::shared_ptr<PhysicalLayer> PhysicalLayerPtr;
/// A destructor
/// A virtual destructor is recommended since the class
/// has virtual functions.
virtual ~PhysicalLayer();
/**
* Gives \c this pointer as a \c PhysicalLayer object.
* This is used to deal with \c shared_ptr's, \c this, and
* inheritance.
* @return a pointer to \c this object.
*/
virtual PhysicalLayerPtr thisPhysicalLayer() = 0;
/**
* Add the channel manager.
* @param wirelessChannelManager a pointer to the channel
* manager being added.
*/
inline void setWirelessChannelManager(
WirelessChannelManagerPtr wirelessChannelManager);
/**
* Handle the successfully received signal.
* The caller of this function should ensure that
* a deep copy of the signal is being passed to this object
* since the signal may be modified locally and changes should
* not affect the signal globally.
* @param signal a pointer to a deep copy of the
* signal being received.
* @param recvdSignalStrength the strength of the signal
* at the receiver.
* @return true if the signal was able to be handled
* by this object.
*/
bool recvPendingSignal(WirelessCommSignalPtr signal,
double recvdSignalStrength);
/**
* Send a wireless signal on the channel.
* @param signal the signal to send.
* @return true if the signal was sent successfully.
*/
virtual bool sendSignal(WirelessCommSignalPtr signal);
/**
* Determine if this layer is currently transmitting a
* signal.
* @return true if the layer is transmitting a signal.
*/
bool isTransmitting() const;
/**
* Set the delay for how long it takes before a signal is
* transmitted on the channel.
* @param signalSendingDelay the delay from the time
* a signal is sent by this object until it actually
* reaches the channel.
*/
inline void setSignalSendingDelay(SimTime signalSendingDelay);
/**
* Get the delay for how long it takes to send a signal
* on the channel.
* @return the delay from the time
* a signal is sent by this object until it actually
* reaches the channel.
*/
inline SimTime getSignalSendingDelay() const;
/**
* Receives a packet from a sending layer.
* @param direction the direction the packet was sent.
* @param packet a pointer to the received packet.
* @param sendingLayerIdx the index of the layer that sent the
* packet.
* @return true if the specified layer was able to receive
* the packet.
*/
bool recvFromLayer(CommunicationLayer::Directions direction,
PacketPtr packet, t_uint sendingLayerIdx);
/**
* Return the geographic location of this physical layer.
* This is needed to compute received signal strength.
* @return a pointer to the location of the node that owns
* this physical layer.
*/
Location getLocation() const;
/**
* Add a signal currently being received and its computed
* signal strength.
* @param signal a pointer to the signal to add.
* @param signalStrength the strength of the signal.
* @see removeSignal()
* @see getCulmulativeSignalStrength()
*/
void addSignal(WirelessCommSignalPtr signal,
double signalStrength);
/**
* Remove a signal from the signals currently being received by
* this object.
* @param signal a pointer to the signal to remove.
* @see addSignal()
* @see getCulmulativeSignalStrength()
*/
void removeSignal(WirelessCommSignalPtr signal);
/**
* Determine if the currently pending signal is still
* sufficiently strong to be captured at this object.
* @return true if the currently pending signal is now too
* weak to be captured to be received or there is no currently
* pending signal.
*/
bool pendingSignalIsWeak();
/**
* Determine whether of not a given signal is strong enough
* to be captured at this object.
* @param signalStrength the strength of the signal at this
* receiver. This signal should \e not have been added
* (via addSignal) at the time this function is called.
* @return true if the signal is strong enough for the
* receiver to capture it.
* @see addSignal()
*/
bool captureSignal(double signalStrength) const;
/**
* Return the total signal strength of all signals
* currently being received by this object.
* @return the culmulative signal strength of the signals.
* @see addSignal()
* @see removeSignal()
*/
double getCulmulativeSignalStrength() const;
/**
* Return the noise floor for the radio.
* @return the noise floor in watts.
*/
inline double getNoiseFloor() const;
/**
* Set a signal as the signal which would be received if
* it were to end right now.
* @param signal a pointer to the signal.
* @see getPendingSignal()
* @see resetPendingSignal()
* @see getPendingSignalStrength()
*/
void setPendingSignal(WirelessCommSignalPtr signal);
/**
* Set whether or not the currently pending signal
* will have an error when/if received.
* @param hasError true if the signal will have an error.
*/
inline void setPendingSignalError(bool hasError);
/**
* Get whether or not the currently pending signal
* will have an error upon reception.
* @return true if the signal will have an error upon
* reception.
*/
inline bool getPendingSignalError() const;
/**
* Get a pointer to the signal which would be received
* if it were to end right now.
* @return a pointer to the signal.
* @see setPendingSignal()
* @see resetPendingSignal()
* @see getPendingSignalStrength()
*/
ConstWirelessCommSignalPtr getPendingSignal() const;
/**
* Get the signal strength of the signal which would be
* received if it were to end right now.
* @return the signal strength of the signal or zero if
* no such signal is being received.
* @see setPendingSignal()
* @see getPendingSignal()
* @see resetPendingSignal()
*/
double getPendingSignalStrength();
/**
* Get the SINR of the signal which would be received
* if it were to end right now.
* @return the SINR of the signal or zero if no such
* signal is being received.
* @see setPendingSignal()
* @see getPendingSignal()
* @see resetPendingSignal()
*/
double getPendingSignalSinr();
/**
* Reset this object such that there is no currently pending
* signal which is strong enough to receive right now.
* @see resetRecvSignals()
*/
void resetPendingSignal();
/**
* Reset this object such that there are \e no signals
* currently pending.
* @see resetPendingSignal()
*/
void resetRecvSignals();
/**
* Determine whether the channel is carrier sensed busy.
* @return true if the channel is carrier sensed busy.
*/
bool channelCarrierSensedBusy() const;
/**
* Set the current transmit power for the physical layer.
* @param txPower the new power level in watts.
* Must be greater than zero.
*/
inline void setCurrentTxPower(double txPower);
/**
* Get the current transmit power in watts.
* @return the current transmit power in watts.
*/
inline double getCurrentTxPower() const;
/**
* Get the maximum transmit power in watts.
* @return the maximum transmit power in watts.
*/
inline double getMaxTxPower() const;
/**
* Set the power threshold for receiving a packet.
* @param rxThreshold the new receive power threshold.
*/
inline void setRxThreshold(double rxThreshold);
/**
* Get the power threshold for receiving a packet.
* @return the current receive power threshold.
*/
inline double getRxThreshold() const;
/**
* Set the power threshold for carrier sensing a channel busy.
* @param csThreshold the new carrier sense power threshold.
*/
inline void setCsThreshold(double csThreshold);
/**
* Get the power threshold for carrier sensing a channel busy.
* @return the current carrier sense power threshold.
*/
inline double getCsThreshold() const;
/**
* Set the capture threshold for considering a SINR sufficiently
* strong.
* @param captureThreshold the new capture threshold.
*/
inline void setCaptureThreshold(double captureThreshold);
/**
* Get the capture threshold for considering a SINR sufficiently
* strong.
* @return the current capture threshold.
*/
inline double getCaptureThreshold() const;
/**
* Set the minimum signal strength for considering a signal
* stronger than thermal noise.
* @param minimumSignalStrength the new minimum signal strength.
*/
inline void setMinimumSignalStrength(double minimumSignalStrength);
/**
* Get the minimum signal strength for considering a signal
* stronger than thermal noise.
* @return the current minimum signal strength.
*/
inline double getMinimumSignalStrength() const;
/**
* Set the current data rate in bits per second.
* @param dataRate the new data rate.
*/
inline void setDataRate(double dataRate);
/**
* Get the current data rate.
* @return data rate in bits per second.
*/
inline double getDataRate() const;
/**
* Set the current radio bandwidth in Hz.
* @param bandwidth the new radio bandwidth.
*/
inline void setBandwidth(double bandwidth);
/**
* Get the current radio bandwidth in Hz.
* @return the radio bandwidth in Hz.
*/
inline double getBandwidth() const;
/**
* Get the gain of the antenna.
* @return the gain (unit-less).
*/
inline double getGain() const;
/**
* Get the wavelength of the antenna based on the
* current bandwidth.
* @return the wavelength in meters.
*/
inline double getWavelength() const;
/**
* Get the layer type for an object.
* @return the type of the layer.
*/
inline CommunicationLayer::Types getLayerType() const;
protected:
/// The channel manager being used by this object.
/// @see setWirelessChannelManager()
WirelessChannelManagerPtr m_wirelessChannelManagerPtr;
/// A constructor
/// This is protected to ensure that all objects
/// are created via \c new since we are using smart pointers.
PhysicalLayer(NodePtr node);
/**
* This function is called when an error free signal is
* received, just before its packet is passed to the upper layer.
* This gives subclasses an opportunity to adjust their
* state in response to the signal reception if need be.
* @param signal the signal that was successfully received.
* @param recvdSignalStrength the strength at which the
* signal was received.
*/
virtual void recvdErrorFreeSignal(WirelessCommSignalPtr signal,
double recvdSignalStrength);
/**
* Set the maximum transmit power for the physical layer.
* If the current transmit power level is less than the new
* max value, then it will be set to the new max value.
* @param txPower the new maximum power level in watts.
* Must be greater than zero.
*/
inline void setMaxTxPower(double txPower);
/**
* Schedule the signal to be received by the channel
* manager.
* @param signal the signal to be scheduled.
* @return true if the signal was scheduled.
*/
bool scheduleSignal(WirelessCommSignalPtr signal);
private:
/// The default transmit power.
static const double m_DEFAULT_TX_POWER;
/// The default maximum transmit power.
static const double m_DEFAULT_MAX_TX_POWER;
/// The default receive power threshold.
static const double m_DEFAULT_RX_THRESHOLD;
/// The default carrier sense power threshold.
static const double m_DEFAULT_CS_THRESHOLD;
/// The default capture threshold.
static const double m_DEFAULT_CAPTURE_THRESHOLD;
/// The default minimum signal strength.
static const double m_DEFAULT_MINIMUM_SIGNAL_STRENGTH;
/// The default data rate.
static const double m_DEFAULT_DATA_RATE;
/// The default bandwidth.
static const double m_DEFAULT_BANDWIDTH;
/// The default radio temperature.
static const double m_RADIO_TEMPERATURE;
/// The default radio noise factor.
static const double m_RADIO_NOISE_FACTOR;
/// Boltzmann's constant.
static const double m_BOLTZMANNS_CONSTANT;
/// Queue length for physical layers.
static const t_uint m_PHYSICAL_QUEUE_LENGTH;
/// Determine when debugging info gets printed.
static const bool m_DEBUG_SIGNAL_CAPTURE = true;
static const bool m_DEBUG_TRANSMIT_POWER = true;
/// The transmit power in watts.
/// @see getCurrentTxPower()
/// @see setCurrentTxPower()
double m_currentTxPower;
/// The maximum transmit power in watts.
/// @see getMaxTxPower()
/// @see setMaxTxPower()
double m_maxTxPower;
/// The receive power threshold in decibels.
/// @see getRxThreshold()
/// @see setRxThreshold()
double m_rxThreshold;
/// The carrier sense power threshold in decibels.
/// @see getCsThreshold()
/// @see setCsThreshold()
double m_csThreshold;
/// The SINR required for a signal to be captured.
/// @see getCaptureThreshold()
/// @see setCaptureThreshold()
double m_captureThreshold;
/// The minimum signal strength for a signal to
/// be added in the culmulative signal strength; weaker
/// signals blend in with the thermal noise.
/// @see getMinimumSignalStrength()
/// @see setMinimumSignalStrength()
double m_minimumSignalStrength;
/// The radio's current data rate.
/// @see getDataRate()
/// @see setDataRate()
double m_dataRate;
/// The radio's bandwidth.
/// @see getBandwidth()
/// @see setBandwidth()
double m_bandwidth;
/// Keeps track of whether the pending packet will
/// have an error upon reception.
/// @see setPendingSignalError()
/// @see getPendingSignalError()
bool m_pendingRecvSignalError;
/// Map of signals being received to their corresponding
/// signal strength.
/// @see addSignal()
/// @see removeSignal()
/// @see getCulmulativeSignalStrength()
/// @see resetRecvSignals
SignalStrengthMap m_signalStrengths;
/// A pointer to the current signal that this object
/// would receive if it were to finish right now.
/// @see setPendingSignal()
/// @see getPendingSignal()
/// @see resetPendingSignal()
WirelessCommSignalPtr m_pendingRecvSignal;
/// The delay for sending a packet to the channel.
/// @see setSignalSendingDelay()
/// @see getSignalSendingDelay()
SimTime m_signalSendingDelay;
/// A timer that is running whenever a signal is
/// being transmitting by this object.
/// @see isTransmitting()
TimerPtr m_transmittingTimer;
};
typedef boost::shared_ptr<PhysicalLayer> PhysicalLayerPtr;
/////////////////////////////////////////////////
// Inline Functions
/////////////////////////////////////////////////
inline void PhysicalLayer::setWirelessChannelManager(
WirelessChannelManagerPtr wirelessChannelManager)
{
m_wirelessChannelManagerPtr = wirelessChannelManager;
}
inline void PhysicalLayer::setCurrentTxPower(double txPower)
{
assert(txPower > 0.0);
m_currentTxPower = txPower;
}
inline double PhysicalLayer::getCurrentTxPower() const
{
return m_currentTxPower;
}
inline void PhysicalLayer::setMaxTxPower(double txPower)
{
assert(txPower > 0.0);
m_maxTxPower = txPower;
m_currentTxPower = min(m_currentTxPower, m_maxTxPower);
}
inline double PhysicalLayer::getMaxTxPower() const
{
return m_maxTxPower;
}
inline void PhysicalLayer::setRxThreshold(double rxThreshold)
{
assert(rxThreshold > 0.0);
m_rxThreshold = rxThreshold;
}
inline double PhysicalLayer::getRxThreshold() const
{
return m_rxThreshold;
}
inline void PhysicalLayer::setCsThreshold(double csThreshold)
{
assert(csThreshold > 0.0);
m_csThreshold = csThreshold;
}
inline double PhysicalLayer::getCsThreshold() const
{
return m_csThreshold;
}
inline void PhysicalLayer::setCaptureThreshold(double captureThreshold)
{
assert(captureThreshold > 0.0);
m_captureThreshold = captureThreshold;
}
inline double PhysicalLayer::getCaptureThreshold() const
{
return m_captureThreshold;
}
inline void PhysicalLayer::setMinimumSignalStrength(
double minimumSignalStrength)
{
assert(minimumSignalStrength > 0.0);
m_minimumSignalStrength = minimumSignalStrength;
}
inline double PhysicalLayer::getMinimumSignalStrength() const
{
return m_minimumSignalStrength;
}
inline void PhysicalLayer::setDataRate(double dataRate)
{
assert(dataRate > 0.0);
m_dataRate = dataRate;
}
inline double PhysicalLayer::getDataRate() const
{
return m_dataRate;
}
inline void PhysicalLayer::setBandwidth(double bandwidth)
{
assert(bandwidth > 0.0);
m_bandwidth = bandwidth;
}
inline double PhysicalLayer::getBandwidth() const
{
return m_bandwidth;
}
inline double PhysicalLayer::getNoiseFloor() const
{
return (m_RADIO_NOISE_FACTOR * m_BOLTZMANNS_CONSTANT *
m_RADIO_TEMPERATURE * getBandwidth());
}
inline double PhysicalLayer::getGain() const
{
// This is just a placeholder function for now.
return 1.0;
}
inline void PhysicalLayer::setPendingSignalError(bool hasError)
{
m_pendingRecvSignalError = hasError;
}
inline bool PhysicalLayer::getPendingSignalError() const
{
return m_pendingRecvSignalError;
}
inline double PhysicalLayer::getWavelength() const
{
return (SPEED_OF_LIGHT / m_bandwidth);
}
inline void PhysicalLayer::setSignalSendingDelay(
SimTime signalSendingDelay)
{
m_signalSendingDelay = signalSendingDelay;
}
inline SimTime PhysicalLayer::getSignalSendingDelay() const
{
return m_signalSendingDelay;
}
inline CommunicationLayer::Types PhysicalLayer::getLayerType() const
{
return CommunicationLayer::Types_Physical;
}
/////////////////////////////////////////////////
// Overloaded Operators
/////////////////////////////////////////////////
////////////////////////////////////////////////
// Event Subclasses
/////////////////////////////////////////////////
/**
* The event for when a signal is transmitted onto the channel.
*/
class SignalRecvEvent : public Event {
public:
/// Smart pointer that clients should use.
typedef boost::shared_ptr<SignalRecvEvent> SignalRecvEventPtr;
/// A constructor.
SignalRecvEvent(WirelessChannelManagerPtr channelManager,
PhysicalLayerPtr sender, WirelessCommSignalPtr signal)
: Event(), m_channelManager(channelManager),
m_sender(sender), m_signal(signal)
{
assert(m_channelManager.get() != 0);
}
void execute()
{
assert(m_channelManager.get() != 0);
LogStreamManager::instance()->logPktSendItem(
m_sender->getNodeId(), m_sender->getLayerType(),
*(m_signal->getPacketPtr()));
assert(m_sender->m_transmittingTimer.get() != 0);
assert(!m_sender->m_transmittingTimer->isRunning());
// Start a timer so we know if the layer is in the process
// of sending a packet.
m_sender->m_transmittingTimer->reschedule(m_signal->getDuration());
m_channelManager->recvSignal(m_sender, m_signal);
}
private:
WirelessChannelManagerPtr m_channelManager;
PhysicalLayerPtr m_sender;
WirelessCommSignalPtr m_signal;
};
typedef boost::shared_ptr<SignalRecvEvent> SignalRecvEventPtr;
#endif // PHYSICAL_LAYER_H