1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2009 CTTC
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  *
18  * Author: Nicola Baldo <nbaldo@cttc.es>
19  *         Giuseppe Piro  <g.piro@poliba.it>
20  * Modified by: Marco Miozzo <mmiozzo@cttc.es> (introduce physical error model)
21  */
22 
23 #ifndef LTE_SPECTRUM_PHY_H
24 #define LTE_SPECTRUM_PHY_H
25 
26 #include <ns3/event-id.h>
27 #include <ns3/spectrum-value.h>
28 #include <ns3/mobility-model.h>
29 #include <ns3/packet.h>
30 #include <ns3/nstime.h>
31 #include <ns3/net-device.h>
32 #include <ns3/spectrum-phy.h>
33 #include <ns3/spectrum-channel.h>
34 #include <ns3/spectrum-interference.h>
35 #include <ns3/data-rate.h>
36 #include <ns3/generic-phy.h>
37 #include <ns3/packet-burst.h>
38 #include <ns3/lte-interference.h>
39 #include "ns3/random-variable-stream.h"
40 #include <map>
41 #include <ns3/ff-mac-common.h>
42 #include <ns3/lte-harq-phy.h>
43 #include <ns3/lte-common.h>
44 
45 namespace ns3 {
46 
47 /// TbId_t structure
48 struct TbId_t
49 {
50   uint16_t m_rnti; ///< RNTI
51   uint8_t m_layer; ///< layer
52 
53   public:
54   TbId_t ();
55   /**
56    * Constructor
57    *
58    * \param a RNTI
59    * \param b Layer
60    */
61   TbId_t (const uint16_t a, const uint8_t b);
62 
63   friend bool operator == (const TbId_t &a, const TbId_t &b);
64   friend bool operator < (const TbId_t &a, const TbId_t &b);
65 };
66 
67 
68 /// tbInfo_t structure
69 struct tbInfo_t
70 {
71   uint8_t ndi; ///< New data indicator
72   uint16_t size; ///< Transport block size
73   uint8_t mcs; ///< MCS
74   std::vector<int> rbBitmap; ///< Resource block bitmap
75   uint8_t harqProcessId; ///< HARQ process id
76   uint8_t rv; ///< Redundancy version
77   double mi; ///< Mutual information
78   bool downlink; ///< whether is downlink
79   bool corrupt; ///< whether is corrupt
80   bool harqFeedbackSent; ///< is HARQ feedback sent
81 };
82 
83 typedef std::map<TbId_t, tbInfo_t> expectedTbs_t; ///< expectedTbs_t typedef
84 
85 
86 class LteNetDevice;
87 class AntennaModel;
88 class LteControlMessage;
89 struct LteSpectrumSignalParametersDataFrame;
90 struct LteSpectrumSignalParametersDlCtrlFrame;
91 struct LteSpectrumSignalParametersUlSrsFrame;
92 
93 /**
94 * This method is used by the LteSpectrumPhy to notify the PHY that a
95 * previously started RX attempt has terminated without success
96 */
97 typedef Callback< void > LtePhyRxDataEndErrorCallback;
98 /**
99 * This method is used by the LteSpectrumPhy to notify the PHY that a
100 * previously started RX attempt has been successfully completed.
101 *
102 * @param packet the received Packet
103 */
104 typedef Callback< void, Ptr<Packet> > LtePhyRxDataEndOkCallback;
105 
106 
107 /**
108 * This method is used by the LteSpectrumPhy to notify the PHY that a
109 * previously started RX of a control frame attempt has been
110 * successfully completed.
111 *
112 * @param packet the received Packet
113 */
114 typedef Callback< void, std::list<Ptr<LteControlMessage> > > LtePhyRxCtrlEndOkCallback;
115 
116 /**
117 * This method is used by the LteSpectrumPhy to notify the PHY that a
118 * previously started RX of a control frame attempt has terminated
119 * without success.
120 */
121 typedef Callback< void > LtePhyRxCtrlEndErrorCallback;
122 
123 /**
124 * This method is used by the LteSpectrumPhy to notify the UE PHY that a
125 * PSS has been received
126 */
127 typedef Callback< void, uint16_t, Ptr<SpectrumValue> > LtePhyRxPssCallback;
128 
129 
130 /**
131 * This method is used by the LteSpectrumPhy to notify the PHY about
132 * the status of a certain DL HARQ process
133 */
134 typedef Callback< void, DlInfoListElement_s > LtePhyDlHarqFeedbackCallback;
135 
136 /**
137 * This method is used by the LteSpectrumPhy to notify the PHY about
138 * the status of a certain UL HARQ process
139 */
140 typedef Callback< void, UlInfoListElement_s > LtePhyUlHarqFeedbackCallback;
141 
142 
143 
144 /**
145  * \ingroup lte
146  * \class LteSpectrumPhy
147  *
148  * The LteSpectrumPhy models the physical layer of LTE
149  *
150  * It supports a single antenna model instance which is
151  * used for both transmission and reception.
152  */
153 class LteSpectrumPhy : public SpectrumPhy
154 {
155 
156 public:
157   LteSpectrumPhy ();
158   virtual ~LteSpectrumPhy ();
159 
160   /**
161    *  PHY states
162    */
163   enum State
164   {
165     IDLE, TX_DL_CTRL, TX_DATA, TX_UL_SRS, RX_DL_CTRL, RX_DATA, RX_UL_SRS
166   };
167 
168   /**
169    * \brief Get the type ID.
170    * \return the object TypeId
171    */
172   static TypeId GetTypeId (void);
173   // inherited from Object
174   virtual void DoDispose ();
175 
176   // inherited from SpectrumPhy
177   void SetChannel (Ptr<SpectrumChannel> c);
178   void SetMobility (Ptr<MobilityModel> m);
179   void SetDevice (Ptr<NetDevice> d);
180   Ptr<MobilityModel> GetMobility () const;
181   Ptr<NetDevice> GetDevice () const;
182   Ptr<const SpectrumModel> GetRxSpectrumModel () const;
183   Ptr<AntennaModel> GetRxAntenna () const;
184   void StartRx (Ptr<SpectrumSignalParameters> params);
185   /**
186    * \brief Start receive data function
187    * \param params Ptr<LteSpectrumSignalParametersDataFrame>
188    */
189   void StartRxData (Ptr<LteSpectrumSignalParametersDataFrame> params);
190   /**
191    * \brief Start receive DL control function
192    * \param lteDlCtrlRxParams Ptr<LteSpectrumSignalParametersDlCtrlFrame>
193    */
194   void StartRxDlCtrl (Ptr<LteSpectrumSignalParametersDlCtrlFrame> lteDlCtrlRxParams);
195   /**
196    * \brief Start receive UL SRS function
197    * \param lteUlSrsRxParams Ptr<LteSpectrumSignalParametersUlSrsFrame>
198    */
199   void StartRxUlSrs (Ptr<LteSpectrumSignalParametersUlSrsFrame> lteUlSrsRxParams);
200   /**
201    * \brief Set HARQ phy function
202    * \param harq the HARQ phy module
203    */
204   void SetHarqPhyModule (Ptr<LteHarqPhy> harq);
205 
206   /**
207    * set the Power Spectral Density of outgoing signals in W/Hz.
208    *
209    * @param txPsd
210    */
211   void SetTxPowerSpectralDensity (Ptr<SpectrumValue> txPsd);
212 
213   /**
214    * \brief set the noise power spectral density
215    * @param noisePsd the Noise Power Spectral Density in power units
216    * (Watt, Pascal...) per Hz.
217    */
218   void SetNoisePowerSpectralDensity (Ptr<const SpectrumValue> noisePsd);
219 
220   /**
221    * reset the internal state
222    *
223    */
224   void Reset ();
225 
226   /**
227    * set the AntennaModel to be used
228    *
229    * \param a the Antenna Model
230    */
231   void SetAntenna (Ptr<AntennaModel> a);
232 
233   /**
234   * Start a transmission of data frame in DL and UL
235   *
236   *
237   * @param pb the burst of packets to be transmitted in PDSCH/PUSCH
238   * @param ctrlMsgList the list of LteControlMessage to send
239   * @param duration the duration of the data frame
240   *
241   * @return true if an error occurred and the transmission was not
242   * started, false otherwise.
243   */
244   bool StartTxDataFrame (Ptr<PacketBurst> pb, std::list<Ptr<LteControlMessage> > ctrlMsgList, Time duration);
245 
246   /**
247   * Start a transmission of control frame in DL
248   *
249   *
250   * @param ctrlMsgList the burst of control messages to be transmitted
251   * @param pss the flag for transmitting the primary synchronization signal
252   *
253   * @return true if an error occurred and the transmission was not
254   * started, false otherwise.
255   */
256   bool StartTxDlCtrlFrame (std::list<Ptr<LteControlMessage> > ctrlMsgList, bool pss);
257 
258 
259   /**
260   * Start a transmission of control frame in UL
261   *
262   * @return true if an error occurred and the transmission was not
263   * started, false otherwise.
264   */
265   bool StartTxUlSrsFrame ();
266 
267   /**
268    * set the callback for the end of a RX in error, as part of the
269    * interconnections between the PHY and the MAC
270    *
271    * @param c the callback
272    */
273   void SetLtePhyRxDataEndErrorCallback (LtePhyRxDataEndErrorCallback c);
274 
275   /**
276    * set the callback for the successful end of a RX, as part of the
277    * interconnections between the PHY and the MAC
278    *
279    * @param c the callback
280    */
281   void SetLtePhyRxDataEndOkCallback (LtePhyRxDataEndOkCallback c);
282 
283   /**
284   * set the callback for the successful end of a RX ctrl frame, as part
285   * of the interconnections between the LteSpectrumPhy and the PHY
286   *
287   * @param c the callback
288   */
289   void SetLtePhyRxCtrlEndOkCallback (LtePhyRxCtrlEndOkCallback c);
290 
291   /**
292   * set the callback for the erroneous end of a RX ctrl frame, as part
293   * of the interconnections between the LteSpectrumPhy and the PHY
294   *
295   * @param c the callback
296   */
297   void SetLtePhyRxCtrlEndErrorCallback (LtePhyRxCtrlEndErrorCallback c);
298 
299   /**
300   * set the callback for the reception of the PSS as part
301   * of the interconnections between the LteSpectrumPhy and the UE PHY
302   *
303   * @param c the callback
304   */
305   void SetLtePhyRxPssCallback (LtePhyRxPssCallback c);
306 
307   /**
308   * set the callback for the DL HARQ feedback as part of the
309   * interconnections between the LteSpectrumPhy and the PHY
310   *
311   * @param c the callback
312   */
313   void SetLtePhyDlHarqFeedbackCallback (LtePhyDlHarqFeedbackCallback c);
314 
315   /**
316   * set the callback for the UL HARQ feedback as part of the
317   * interconnections between the LteSpectrumPhy and the PHY
318   *
319   * @param c the callback
320   */
321   void SetLtePhyUlHarqFeedbackCallback (LtePhyUlHarqFeedbackCallback c);
322 
323   /**
324    * \brief Set the state of the phy layer
325    * \param newState the state
326    */
327   void SetState (State newState);
328 
329   /**
330    *
331    *
332    * \param cellId the Cell Identifier
333    */
334   void SetCellId (uint16_t cellId);
335 
336   /**
337    *
338    * \param componentCarrierId the component carrier id
339    */
340   void SetComponentCarrierId (uint8_t componentCarrierId);
341 
342   /**
343   *
344   *
345   * \param p the new LteChunkProcessor to be added to the RS power
346   *          processing chain
347   */
348   void AddRsPowerChunkProcessor (Ptr<LteChunkProcessor> p);
349 
350   /**
351   *
352   *
353   * \param p the new LteChunkProcessor to be added to the Data Channel power
354   *          processing chain
355   */
356   void AddDataPowerChunkProcessor (Ptr<LteChunkProcessor> p);
357 
358   /**
359   *
360   *
361   * \param p the new LteChunkProcessor to be added to the data processing chain
362   */
363   void AddDataSinrChunkProcessor (Ptr<LteChunkProcessor> p);
364 
365   /**
366   *  LteChunkProcessor devoted to evaluate interference + noise power
367   *  in control symbols of the subframe
368   *
369   * \param p the new LteChunkProcessor to be added to the data processing chain
370   */
371   void AddInterferenceCtrlChunkProcessor (Ptr<LteChunkProcessor> p);
372 
373   /**
374   *  LteChunkProcessor devoted to evaluate interference + noise power
375   *  in data symbols of the subframe
376   *
377   * \param p the new LteChunkProcessor to be added to the data processing chain
378   */
379   void AddInterferenceDataChunkProcessor (Ptr<LteChunkProcessor> p);
380 
381 
382   /**
383   *
384   *
385   * \param p the new LteChunkProcessor to be added to the ctrl processing chain
386   */
387   void AddCtrlSinrChunkProcessor (Ptr<LteChunkProcessor> p);
388 
389   /**
390   *
391   *
392   * \param rnti the RNTI of the source of the TB
393   * \param ndi new data indicator flag
394   * \param size the size of the TB
395   * \param mcs the MCS of the TB
396   * \param map the map of RB(s) used
397   * \param layer the layer (in case of MIMO tx)
398   * \param harqId the id of the HARQ process (valid only for DL)
399   * \param rv the redundancy version
400   * \param downlink true when the TB is for DL
401   */
402   void AddExpectedTb (uint16_t  rnti, uint8_t ndi, uint16_t size, uint8_t mcs, std::vector<int> map, uint8_t layer, uint8_t harqId, uint8_t rv, bool downlink);
403   /**
404    * \brief Remove expected transport block.
405    *
406    * When UE context at eNodeB is removed and if UL TB is expected to be received
407    * but not transmitted due to break in radio link. The TB with different rnti or lcid
408    * remains during the transmission of a new TB and causes problems with
409    * m_ulPhyReception trace, since the UE context was already removed. TB has to be
410    * removed when ue context at eNodeB is removed
411    *
412    * \param rnti The RNTI of the UE
413    */
414   void RemoveExpectedTb (uint16_t  rnti);
415 
416   /**
417   *
418   *
419   * \param sinr vector of sinr perceived per each RB
420   */
421   void UpdateSinrPerceived (const SpectrumValue& sinr);
422 
423   /**
424   *
425   *
426   * \param txMode UE transmission mode (SISO, MIMO tx diversity, ...)
427   */
428   void SetTransmissionMode (uint8_t txMode);
429 
430 
431   /**
432    *
433    * \return the previously set channel
434    */
435   Ptr<SpectrumChannel> GetChannel ();
436 
437   /// allow LteUePhy class friend access
438   friend class LteUePhy;
439 
440  /**
441   * Assign a fixed random variable stream number to the random variables
442   * used by this model.  Return the number of streams (possibly zero) that
443   * have been assigned.
444   *
445   * \param stream first stream index to use
446   * \return the number of stream indices assigned by this model
447   */
448   int64_t AssignStreams (int64_t stream);
449 
450 private:
451   /**
452   * \brief Change state function
453   *
454   * \param newState the new state to set
455   */
456   void ChangeState (State newState);
457   /// End transmit data function
458   void EndTxData ();
459   /// End transmit DL control function
460   void EndTxDlCtrl ();
461   /// End transmit UL SRS function
462   void EndTxUlSrs ();
463   /// End receive data function
464   void EndRxData ();
465   /// End receive DL control function
466   void EndRxDlCtrl ();
467   /// End receive UL SRS function
468   void EndRxUlSrs ();
469 
470   /**
471   * \brief Set transmit mode gain function
472   *
473   * \param txMode the transmit mode
474   * \param gain the gain to set
475   */
476   void SetTxModeGain (uint8_t txMode, double gain);
477 
478 
479   Ptr<MobilityModel> m_mobility; ///< the modility model
480   Ptr<AntennaModel> m_antenna; ///< the antenna model
481   Ptr<NetDevice> m_device; ///< the device
482 
483   Ptr<SpectrumChannel> m_channel; ///< the channel
484 
485   Ptr<const SpectrumModel> m_rxSpectrumModel; ///< the spectrum model
486   Ptr<SpectrumValue> m_txPsd; ///< the transmit PSD
487   Ptr<PacketBurst> m_txPacketBurst; ///< the transmit packet burst
488   std::list<Ptr<PacketBurst> > m_rxPacketBurstList; ///< the receive burst list
489 
490   std::list<Ptr<LteControlMessage> > m_txControlMessageList; ///< the transmit control message list
491   std::list<Ptr<LteControlMessage> > m_rxControlMessageList; ///< the receive control message list
492 
493 
494   State m_state; ///< the state
495   Time m_firstRxStart; ///< the first receive start
496   Time m_firstRxDuration; ///< the first receive duration
497 
498   TracedCallback<Ptr<const PacketBurst> > m_phyTxStartTrace; ///< the phy transmit start trace callback
499   TracedCallback<Ptr<const PacketBurst> > m_phyTxEndTrace; ///< the phy transmit end trace callback
500   TracedCallback<Ptr<const PacketBurst> > m_phyRxStartTrace; ///< the phy receive start trace callback
501   TracedCallback<Ptr<const Packet> >      m_phyRxEndOkTrace; ///< the phy receive end ok trace callback
502   TracedCallback<Ptr<const Packet> >      m_phyRxEndErrorTrace; ///< the phy receive end error trace callback
503 
504   LtePhyRxDataEndErrorCallback   m_ltePhyRxDataEndErrorCallback; ///< the LTE phy receive data end error callback
505   LtePhyRxDataEndOkCallback      m_ltePhyRxDataEndOkCallback; ///< the LTE phy receive data end ok callback
506 
507   LtePhyRxCtrlEndOkCallback     m_ltePhyRxCtrlEndOkCallback; ///< the LTE phy receive control end ok callback
508   LtePhyRxCtrlEndErrorCallback  m_ltePhyRxCtrlEndErrorCallback; ///< the LTE phy receive control end error callback
509   LtePhyRxPssCallback  m_ltePhyRxPssCallback; ///< the LTE phy receive PSS callback
510 
511   Ptr<LteInterference> m_interferenceData; ///< the data interference
512   Ptr<LteInterference> m_interferenceCtrl; ///< the control interference
513 
514   uint16_t m_cellId; ///< the cell ID
515 
516   uint8_t m_componentCarrierId; ///< the component carrier ID
517   expectedTbs_t m_expectedTbs; ///< the expected TBS
518   SpectrumValue m_sinrPerceived; ///< the preceived SINR
519 
520   /// Provides uniform random variables.
521   Ptr<UniformRandomVariable> m_random;
522   bool m_dataErrorModelEnabled; ///< when true (default) the phy error model is enabled
523   bool m_ctrlErrorModelEnabled; ///< when true (default) the phy error model is enabled for DL ctrl frame
524 
525   uint8_t m_transmissionMode; ///< for UEs: store the transmission mode
526   uint8_t m_layersNum; ///< layers num
527   std::vector <double> m_txModeGain; ///< duplicate value of LteUePhy
528 
529   Ptr<LteHarqPhy> m_harqPhyModule; ///< the HARQ phy module
530   LtePhyDlHarqFeedbackCallback m_ltePhyDlHarqFeedbackCallback; ///< the LTE phy DL HARQ feedback callback
531   LtePhyUlHarqFeedbackCallback m_ltePhyUlHarqFeedbackCallback; ///< the LTE phy UL HARQ feedback callback
532 
533 
534   /**
535    * Trace information regarding PHY stats from DL Rx perspective
536    * PhyReceptionStatParameters (see lte-common.h)
537    */
538   TracedCallback<PhyReceptionStatParameters> m_dlPhyReception;
539 
540 
541   /**
542    * Trace information regarding PHY stats from UL Rx perspective
543    * PhyReceptionStatParameters (see lte-common.h)
544    */
545   TracedCallback<PhyReceptionStatParameters> m_ulPhyReception;
546 
547   EventId m_endTxEvent; ///< end transmit event
548   EventId m_endRxDataEvent; ///< end receive data event
549   EventId m_endRxDlCtrlEvent; ///< end receive DL control event
550   EventId m_endRxUlSrsEvent; ///< end receive UL SRS event
551 
552 
553 };
554 
555 
556 
557 
558 
559 
560 }
561 
562 #endif /* LTE_SPECTRUM_PHY_H */
563