ns-3 PLC model
model/plc-defs.h
00001 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
00002 /*
00003  * Copyright (c) 2012 University of British Columbia, Vancouver
00004  *
00005  * This program is free software; you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License version 2 as
00007  * published by the Free Software Foundation;
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017  *
00018  * Author: Alexander Schloegl <alexander.schloegl@gmx.de>
00019  */
00020 
00021 #ifndef PLC_DEFS_H_
00022 #define PLC_DEFS_H_
00023 
00024 #include <ns3/object.h>
00025 
00026 #include <complex>
00027 #include <climits>
00028 #include <boost/config.hpp>
00029 #include <boost/property_map/property_map.hpp>
00030 #include <boost/graph/graph_traits.hpp>
00031 #include <boost/graph/adjacency_list.hpp>
00032 #include <boost/graph/dijkstra_shortest_paths.hpp>
00033 #include <boost/graph/depth_first_search.hpp>
00034 #include <boost/graph/undirected_dfs.hpp>
00035 #include <boost/pending/indirect_cmp.hpp>
00036 
00037 #include <boost/random/linear_congruential.hpp>
00038 #include <boost/random/uniform_int.hpp>
00039 #include <boost/random/uniform_real.hpp>
00040 #include <boost/random/variate_generator.hpp>
00041 #include <boost/generator_iterator.hpp>
00042 
00047 #ifdef _OPENMP
00048 #include <omp.h>
00049 #define PLC_MULTITHREADING
00050 #endif
00051 
00052 #define PLC_USE_IMPEDANCE_HASHING
00053 
00054 namespace ns3 {
00055 
00056 #define MAX_X   100000
00057 #define MAX_Y   100000
00058 #define MAX_Z   100000
00059 
00060 #define PL_PERIOD_EUROPE        0.02 // s
00061 #define PLC_RECEIVER_SENSIVITY -85 // dBm
00062 //#define PLC_NODAL_ATT_COMP 3
00063 #define PL_CHAR_IMPEDANCE_APPROX_THRESHOLD 10000
00064 
00065 #define CCA_THRESHOLD_POWER     1e-6 // mW
00066 #define CCA_NUM_SYMBOLS 8
00067 #define MAX_REPLAY_ATTEMPTS 500
00068 #define MAX_IR_PACKETS 100
00069 #define REPLAY_TIMEOUT 20000 // replay timeout in us
00070 #define MAX_CSMACA_ATTEMPTS 1000
00071 #define MAX_RELAY_ADVANCE 300
00072 #define REDUNDANCY_DIVISOR 20
00073 
00074 #define EDGE_GEN_SEED   32u
00075 #define LINE_GEN_SEED   124u
00076 #define NODE_GEN_SEED   62u
00077 #define CONST_IMP_NODE_GEN_SEED 723u
00078 #define CONST_IMP_REAL_GEN_SEED 22u
00079 #define CONST_IMP_IMAG_GEN_SEED 82u
00080 
00081 #define CreateBestCaseBgNoise(sm)       Create<PLC_ColoredNoiseFloor>(-145, 53.23, -0.337, sm)
00082 #define CreateWorstCaseBgNoise(sm)      Create<PLC_ColoredNoiseFloor>(-140, 38.75, -0.72, sm)
00083 
00084 enum ModulationAndCodingType
00085 {
00086         BPSK_1_4,
00087         BPSK_1_2,
00088         QPSK_1_2,
00089         QAM16_1_2,
00090         QAM64_16_21,
00091         BPSK_RATELESS,
00092         QAM4_RATELESS,
00093         QAM16_RATELESS,
00094 };
00095 
00096 std::ostream& operator<<(std::ostream& os, ModulationAndCodingType type);
00097 double GetCodeRate (ModulationAndCodingType mcs);
00098 size_t GetBitsPerSymbol (ModulationAndCodingType mcs);
00099 
00100 #define PLC_LOG_ENABLE(PLC_LOG_LEVEL)\
00101         do {\
00102                 LogComponentEnable("PLC_Cable", PLC_LOG_LEVEL);\
00103                 LogComponentEnable("PLC_Edge", PLC_LOG_LEVEL);\
00104                 LogComponentEnable("PLC_Node", PLC_LOG_LEVEL);\
00105                 LogComponentEnable("PLC_Graph", PLC_LOG_LEVEL);\
00106                 LogComponentEnable("PLC_Interface", PLC_LOG_LEVEL);\
00107                 LogComponentEnable("PLC_Outlet", PLC_LOG_LEVEL);\
00108                 LogComponentEnable("PLC_Channel", PLC_LOG_LEVEL);\
00109                 LogComponentEnable("PLC_ErrorModel", PLC_LOG_LEVEL);\
00110                 LogComponentEnable("PLC_Phy", LOG_LEVEL_INFO);\
00111                 LogComponentEnable("PLC_Mac", LOG_LEVEL_INFO);\
00112         } while(0)
00113 
00114 struct PLC_Mutex;
00115 extern PLC_Mutex g_log_mutex;
00116 extern PLC_Mutex g_smartpointer_mutex;
00117 
00118 #define PLC_LOG_FUNCTION(parameters)\
00119         do {\
00120                 g_log_mutex.Lock();\
00121                 NS_LOG_FUNCTION(parameters);\
00122                 g_log_mutex.Unlock();\
00123         } while(0)
00124 
00125 #define PLC_LOG_FUNCTION_NOARGS()\
00126         do {\
00127                 g_log_mutex.Lock();\
00128                 NS_LOG_FUNCTION_NOARGS();\
00129                 g_log_mutex.Unlock();\
00130         } while(0)
00131 
00132 #define PLC_LOG_LOGIC(msg)\
00133         do {\
00134                 g_log_mutex.Lock();\
00135                 NS_LOG_LOGIC(msg);\
00136                 g_log_mutex.Unlock();\
00137         } while(0)
00138 
00139 #define PLC_LOG_ERROR(msg)\
00140         do {\
00141                 g_log_mutex.Lock();\
00142                 NS_LOG_ERROR(msg));\
00143                 g_log_mutex.Unlock();\
00144         } while(0)
00145 
00146 #define PLC_LOG_WARN(msg)\
00147         do {\
00148                 g_log_mutex.Lock();\
00149                 NS_LOG_WARN(msg);\
00150                 g_log_mutex.Unlock();\
00151         } while(0)
00152 
00153 #define PLC_LOG_DEBUG(msg))\
00154         do {\
00155                 g_log_mutex.Lock();\
00156                 NS_LOG_DEBUG(msg);\
00157                 g_log_mutex.Unlock();\
00158         } while(0)
00159 
00160 #define PLC_LOG_INFO(msg)\
00161         do {\
00162                 g_log_mutex.Lock();\
00163                 NS_LOG_INFO(msg);\
00164                 g_log_mutex.Unlock();\
00165         } while(0)
00166 
00167 #define PLC_ASSERT_MSG(condition, msg)\
00168         do {\
00169                 g_log_mutex.Lock();\
00170                 NS_ASSERT_MSG(condition, msg);\
00171                 g_log_mutex.Unlock();\
00172         } while(0)
00173 
00174 
00175 #ifdef _OPENMP
00176 struct PLC_Mutex
00177 {
00178         PLC_Mutex() { omp_init_lock(&lock); }
00179         ~PLC_Mutex() { omp_destroy_lock(&lock); }
00180 
00181         void Lock() const { omp_set_lock(&lock); }
00182         void Unlock() const { omp_unset_lock(&lock); }
00183 
00184         PLC_Mutex(const PLC_Mutex& ) { omp_init_lock(&lock); }
00185         PLC_Mutex& operator= (const PLC_Mutex& ) { return *this; }
00186 
00187 public:
00188         mutable omp_lock_t lock;
00189 };
00190 
00191 #else
00192 
00193 struct PLC_Mutex
00194 {
00195         void Lock() {}
00196         void Unlock() {}
00197 };
00198 #endif
00199 
00200         typedef std::pair<unsigned int, std::pair<unsigned int, unsigned int> > PLC_BackboneBranchKey;
00201 
00202         class PLC_Edge;
00203         class PLC_Node;
00204         class PLC_Line;
00205 
00206         typedef std::vector<double> PLC_FreqSelectiveRealValue;
00207         typedef std::vector<double> PLC_TimeVariantRealValue;
00208         typedef std::vector<std::vector<double> > PLC_TimeVariantFreqSelectiveRealValue;
00209         typedef std::vector<double> PLC_FreqSelectiveResistance;
00210         typedef std::vector<double> PLC_FreqSelectiveConductance;
00211         typedef std::vector<double> PLC_FreqSelectiveCapacitance;
00212         typedef std::vector<double> PLC_FreqSelectiveInductance;
00213 
00214         typedef std::complex<double>    PLC_Value;
00215         typedef std::vector<PLC_Value>  PLC_ValueSpectrum;
00216         typedef std::vector<PLC_Value>  PLC_TimeVariantValue;
00217         typedef std::vector<PLC_ValueSpectrum> PLC_TimeVariantValueSpectrum;
00218 
00219         typedef PLC_Value                       PLC_ImpedanceValue;
00220         typedef PLC_ValueSpectrum       PLC_ImpedanceSpectrum;
00221 
00222         class PLC_ConstValue;
00223         class PLC_FreqSelectiveValue;
00224 
00225         class PLC_ValueBase;
00226         class PLC_ConstValue;
00227         class PLC_FreqSelectiveValue;
00228         class PLC_TimeVariantConstValue;
00229         class PLC_TimeVariantFreqSelectiveValue;
00230 
00231         typedef PLC_Value                                                       PLC_ImpedanceValue;
00232         typedef PLC_ValueSpectrum                                       PLC_ImpedanceSpectrum;
00233         typedef PLC_ValueBase                                           PLC_Impedance;
00234         typedef PLC_ConstValue                                          PLC_ConstImpedance;
00235         typedef PLC_FreqSelectiveValue                          PLC_FreqSelectiveImpedance;
00236         typedef PLC_TimeVariantConstValue                       PLC_TimeVariantConstImpedance;
00237         typedef PLC_TimeVariantFreqSelectiveValue       PLC_TimeVariantFreqSelectiveImpedance;
00238 
00239         typedef PLC_ValueBase                                           PLC_TransferBase;
00240         typedef PLC_FreqSelectiveValue                          PLC_TransferVector;
00241         typedef PLC_TimeVariantFreqSelectiveValue       PLC_TimeVariantTransferVector;
00242 
00243         typedef struct PLC_ImpedanceIndicator_t
00244         {
00245                 bool IsUp2Date;
00246                 bool IsTimeVariant;
00247         } PLC_ImpedanceIndicator;
00248 
00249         typedef std::pair<Ptr<PLC_Impedance>, PLC_ImpedanceIndicator>   PLC_InputImpedance;
00250 
00251         class PLC_EdgeTransferUnit;
00252 
00253         typedef struct PLC_EdgeTransferData_t
00254         {
00255                 PLC_InputImpedance                      input_impedance;
00256                 Ptr<PLC_Impedance>                      load_impedance;
00257                 Ptr<PLC_EdgeTransferUnit>       edge_transfer_unit;
00258                 bool                                            etf_initialized;
00259         } PLC_EdgeTransferData;
00260 
00261         typedef std::map<PLC_Node *, PLC_EdgeTransferData> PLC_EdgeTransferDataMap;
00262 
00263         class PLC_BackboneBranch;
00264 
00265         typedef std::vector<Ptr<PLC_BackboneBranch> > PLC_BackbonePath;
00266         typedef std::map<PLC_Node *, Ptr<PLC_Line> > PLC_NodeOutlinesMap;
00267 
00268         typedef std::map<PLC_Node *, Ptr<PLC_Edge> > PLC_NodeOutEdgesMap;
00269 
00270         class PLC_TxInterface;
00271         class PLC_RxInterface;
00272 
00273         typedef std::set<std::pair<PLC_TxInterface *, PLC_RxInterface *> > PLC_InterfaceTupel;
00274         typedef std::pair<Ptr<PLC_Node>, double>                PLC_RandomNodeDescriptor;
00275         typedef std::vector<PLC_RandomNodeDescriptor>   PLC_RandomLevelDescriptor;
00276 
00277         class SpectrumValue;
00278         typedef uint32_t Timeslot;
00279 
00280         typedef std::map<Timeslot, std::pair<Timeslot, Ptr<SpectrumValue> > >   PLC_RxPsdMap;
00281         // < rxInterface id, time variant rx PSDs >
00282         typedef std::map<uint32_t, PLC_RxPsdMap >                               PLC_ReceiveDescriptorMap;
00283         // < txInterface id, receive descriptor >
00284         typedef std::map<uint32_t, PLC_ReceiveDescriptorMap>    PLC_TransmissionDescriptorMap;
00285 
00286         class PLC_Outlet;
00287         class PLC_NoiseSource;
00288         class PLC_TwoPort;
00289         class PLC_NetDevice;
00290 
00291         typedef PLC_TwoPort     PLC_Transformer;
00292 
00293         typedef std::vector<Ptr<PLC_Node> >             PLC_NodeList;
00294         typedef std::vector<Ptr<PLC_Outlet> >           PLC_OutletList;
00295         typedef std::vector<Ptr<PLC_NoiseSource> >      PLC_NoiseSourceList;
00296         typedef std::vector<Ptr<PLC_Transformer> >      PLC_TransformerList;
00297         typedef std::vector<Ptr<PLC_NetDevice> >        PLC_NetdeviceList;
00298 
00299         typedef std::map<std::string, Ptr<PLC_NetDevice> > PLC_NetdeviceMap;
00300 }
00301 
00302 namespace boost {
00303 
00304         enum vertex_nodeptr_t { vertex_nodeptr };
00305         BOOST_INSTALL_PROPERTY(vertex, nodeptr);
00306 
00307         enum edge_plc_edgeptr_t { edge_plc_edgeptr };
00308         BOOST_INSTALL_PROPERTY(edge, plc_edgeptr);
00309 
00310         typedef property<vertex_nodeptr_t, ns3::PLC_Node *, property<vertex_color_t, default_color_type> > VertexProperty;
00311         typedef property<edge_weight_t, double, property<edge_color_t, default_color_type, property<edge_plc_edgeptr_t, ns3::PLC_Edge *> > > EdgeProperty;
00312 
00313         typedef adjacency_list <vecS, vecS, undirectedS, VertexProperty, EdgeProperty> UGraph;
00314 
00315         typedef std::pair<int, int> Edge;
00316         typedef graph_traits<UGraph>::vertex_descriptor vertex_descriptor;
00317         typedef graph_traits<UGraph>::edge_descriptor edge_descriptor;
00318 
00319         typedef property_map<UGraph, vertex_color_t>::type VertexColorMap;
00320         typedef property_map<UGraph, edge_color_t>::type EdgeColorMap;
00321 
00322         typedef graph_traits<UGraph>::vertex_iterator vertex_iterator;
00323 
00324         typedef property_traits<VertexColorMap>::value_type ColorValue;
00325         typedef color_traits<ColorValue> Color;
00326 
00327         typedef boost::minstd_rand base_generator_type;
00328 
00329         typedef boost::uniform_int<> distribution_type;
00330         typedef boost::variate_generator<boost::base_generator_type&, distribution_type> gen_type;
00331 }
00332 
00333 namespace ns3 {
00334 
00335         class PLC_BackboneBranch;
00336         class PLC_TxInterface;
00337         class PLC_RxInterface;
00338 
00339         typedef struct boostgraph_copy_t
00340         {
00341                 boost::UGraph                   graph;
00342                 mutable PLC_Mutex               graph_mutex;
00343         } boostgraph_copy;
00344 
00345         typedef struct thread_arg_t
00346         {
00347                 boostgraph_copy *graph_copy;
00348                 PLC_TxInterface *txInterface;
00349                 PLC_RxInterface *rxInterface;
00350         } thread_arg;
00351 
00352         typedef struct sub_thread_arg_t
00353         {
00354                 boostgraph_copy         *graph_copy;
00355                 PLC_BackboneBranch      *bb;
00356         } sub_thread_arg;
00357 
00358         typedef struct backbone_branch_discover_thread_arg_t
00359         {
00360                 boost::UGraph           *graph_copy;
00361                 PLC_Mutex                       *graph_copy_mutex;
00362                 PLC_BackboneBranch      *bb;
00363         } backbone_branch_discover_thread_arg;
00364 }
00365 
00366 #endif /* PLC_DEFS_H_ */
 All Classes Functions Variables Enumerations