summaryrefslogtreecommitdiff
path: root/cesar/maximus/channel/inc/ChannelComputer.h
blob: 479ca5999d1ca86d891ad175378521978254ab2f (plain)
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
/************************************************************************
                        ChannelComputer.h - Copyright buret

Here you can write a license for your code, some comments or any other
information you want to have in your generated code. To to this simply
configure the "headings" directory in uml to point to a directory
where you have your heading files.

or you can just replace the contents of this file with your own.
If you want to do this, this file is located at

/usr/share/apps/umbrello/headings/heading.h

-->Code Generators searches for heading files based on the file extension
   i.e. it will look for a file name ending in ".h" to include in C++ header
   files, and for a file name ending in ".java" to include in all generated
   java code.
   If you name the file "heading.<extension>", Code Generator will always
   choose this file even if there are other files with the same extension in the
   directory. If you name the file something else, it must be the only one with that
   extension in the directory to guarantee that Code Generator will choose it.

you can use variables in your heading files which are replaced at generation
time. possible variables are : author, date, time, filename and filepath.
just write %variable_name%

This file was generated on %date% at %time%
The original location of this file is /home/buret/eclipse/maximus/channel/inc/ChannelComputer.h
**************************************************************************/

#ifndef CHANNELCOMPUTER_H
#define CHANNELCOMPUTER_H

#include "IChannel.h"
#include "SystemManager.h" // for 'StationsList'

#include "hal/phy/defs.h" // for 'PHY_CARRIER_NB'
#include "channel_types.h" // for 'MAXIMUS_CHANNEL_MOD_NB'

#include <map>
#include <boost/random/mersenne_twister.hpp> // for random

class IPhy;
class PhySciMsgNoise;
class ChannelSettings;
class PhySciMsgMpdu;
class ChannelPoint;

struct Channel_Settings_Key 
{
  Sci_Msg_Station_Id tx_station_id;
  Sci_Msg_Station_Id rx_station_id;
};
struct ltkey
{
  bool operator()(const Channel_Settings_Key & key1, const Channel_Settings_Key & key2) const
  {
    if (key1.tx_station_id != key2.tx_station_id)
    {
      return key1.tx_station_id < key2.tx_station_id;
    }
    else
    {
      return key1.rx_station_id < key2.rx_station_id;
    }
  }
};
typedef std::map<const Channel_Settings_Key, ChannelSettings *, ltkey> ChannelSettingsList;


/**
  * class ChannelComputer
  */

class ChannelComputer : public IChannel
{

public:

  // public attributes
  //  

private:

  // private attributes
  //

  boost::mt19937 mEngine; // for random

  static unsigned short int mNumberOfCopiesArray[PHY_MOD_ROBO_NB];
  static float mPowerScaleArray[MAXIMUS_CHANNEL_MOD_NB];
  uint8_t * mpTonemask;
  StationsList * mpListOfStations;
  ChannelSettingsList mListOfChannelSettings;
  unsigned int mBeaconPeriod;

protected:
  
  // protected attributes
  //  
  
public:

  // Constructors/Destructors
  //  

  /**
   * Constructor
   */
  ChannelComputer ( IPhy * p_phy, ISystem * p_system );

  /**
   * Empty Destructor
   */
  virtual ~ChannelComputer ( );

  // public methods
  //  

  /**
   * Duplicate the MPDU payload.
   * @param  mpdu  a PHY SCI message MPDU that contains the complete received MPDU payload
   * @return  bool
   */
  bool duplicateMpduPayload ( PhySciMsgMpdu & mpdu );

  /**
   * Compute PB measurement and NOISE.
   * @param  pb_measurement_array  the PB measurement to fill in (for all PBs composing the MPDU)
   * @param  noise  the PHY SCI message NOISE to fill in
   * @param  pb_header_array  the PB header of each PB composing the MPDU
   * @param  mpdu  a PHY SCI message MPDU that contains the duplicated MPDU payload
   * @param  tx_station_id  the ID of the transmitting station
   * @param  rx_station_id  the ID of the destination station
   * @param  current_tick  the current Network Clock tick
   * @return  bool
   */
  bool addPerturbation ( uint32_t * pb_measurement_array,
                         PhySciMsgNoise & noise,
                         const uint32_t * pb_header_array,
                         const PhySciMsgMpdu & mpdu,
                         const Sci_Msg_Station_Id tx_station_id,
                         const Sci_Msg_Station_Id rx_station_id,
                         const Network_Clock_Tick current_tick );

  /**
   * Add Channel Settings to the list.
   * Called when a station is created => create its associated Channel Settings.
   * @return  bool
   */
  bool addChannelSettings ( const Sci_Msg_Station_Id created_station_id );

  /**
   * Find Channel Settings.
   * @param  tx_station_id  the ID of the transmitting station
   * @param  rx_station_id  the ID of the destination station
   * @return  a list of Channel Settings
   */
  std::vector<ChannelSettings *> & findChannelSettings ( const Sci_Msg_Station_Id tx_station_id,
                                                         const Sci_Msg_Station_Id rx_station_id,
                                                         const bool both_directions );

  /**
   * Called when the PHY processor receives a preamble.
   * @param  current_tick  the current Network Clock tick
   * @param  tx_station_id  the ID of the transmitting station
   * @return  bool
   */
  bool receivePre ( const Sci_Msg_Station_Id tx_station_id, const Network_Clock_Tick current_tick );

  /**
   * Called when the PHY processor receives a frame control.
   * @param  current_tick  the current Network Clock tick
   * @param  tx_station_id  the ID of the transmitting station
   * @return  bool
   */
  bool receiveFc ( const Sci_Msg_Station_Id tx_station_id, const Network_Clock_Tick current_tick );

  // public attribute accessor methods
  //  

  // private attribute accessor methods
  //  

  /**
   * Sets the tonemask.
   * @param  p_tonemask  the new value of mpTonemask
   * @return bool
   */
  bool setTonemask ( const uint8_t * p_tonemask );

  /**
   * Sets the tonemap of the current transmission.
   * @return bool
   * @param  tx_station_id  the ID of the transmitting station
   * @param  length  the tonemap length in octets
   * @param  p_tonemap  the tonemap
   */
  bool setTonemap ( const Sci_Msg_Station_Id tx_station_id,
                    const unsigned long length,
                    const unsigned char * p_tonemap );

  /**
   * Sets the beacon period in ticks 25 MHz.
   * @return bool
   * @param  frequency  the power-line frequency
   */
  bool setBeaconPeriod ( const float frequency );

  // protected attribute accessor methods
  //  

private:

  // private methods
  //  

  /**
   * Initializes the Channel Computer class attributes.
   */
  void initAttributes ( );

  /**
   * Gets a tonemask value.
   * @param  carrier_index
   * @return a boolean value indicating if the requested carrier is enabled or not
   */
  const bool getTonemask ( const unsigned short int carrier_index ) const;

  /**
   * Update the list of Channel Settings.
   * Called when channel perturbation has to be added.
   * Check if stations have been removed => remove their associated Channel Settings.
   */ 
  void updateListOfChannelSettings ( );

  /**
   * Gets the beacon period in ticks 25 MHz.
   * @return mBeaconPeriod
   */
  const unsigned int getBeaconPeriod ( ) const;

protected:

  // protected methods
  // All following protected methods could be declared as private methods,
  // but they are declared as protected methods to be unitarly tested.
  //

  /**
   * Gets the sigma factor.
   * @param  modulation  the tonemap modulation
   * @param  snr_in_db  the SNR value in dB
   * @return  the sigma factor
   */
  const double getSigma ( const Channel_Mod modulation, const float snr_in_db ) const;

  /**
   * Gets the PowerScale.
   * @param  modulation  the tonemap modulation
   * @return  1 / (PowerScale^2)
   */
  const float getPowerScale ( const Channel_Mod modulation ) const;

  /**
   * Gets the linear SNR.
   * @param  snr_in_db  the SNR value in dB
   * @return  the linear SNR value
   */
  const double getLinearSnr ( const float snr_in_db ) const;

  /**
   * Add noise.
   * @param  point  the original point coordinates
   * @param  sigma  the sigma factor
   * @return the noise point coordinates
   */
  const ChannelPoint addNoise ( const ChannelPoint & point, const double sigma );

  /**
   * Compute the BER.
   * @param  codeA / codeB  the 2 codes to compare
   * @return the BER
   */
  const unsigned short int computeBer ( const unsigned short int codeA,
                                        const unsigned short int codeB ) const;

  /**
   * Compute the noise.
   * @param  pointA / pointB  the 2 points coordinates
   * @return the (distance)^2 between the 2 points
   */
  const unsigned int computeNoise ( const ChannelPoint & pointA,
                                    const ChannelPoint & pointB ) const;

  /**
   * Compute the FER.
   * @param  ber  the BER of the current PB
   * @param  n  the average number of bits coded per carrier on the current PB
   * @param  modulation  the tonemap modulation
   * @return the FER
   */
  const float computeFer ( const unsigned short int ber,
                           const unsigned int n,
                           const Channel_Mod modulation ) const;

  /**
   * Compute the CRC error.
   * @param  fer  the FER of the current PB
   * @return the CRC error
   */
  const bool computeCrcError ( const float fer );
};


#endif /* CHANNELCOMPUTER_H */