Я не силён в программировании, поэтому вынужден попросить описать каждую строчку кода (что она значит). Код | 1 /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ 2 /* 3 * Copyright © 2011 The Boeing Company 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: Tom Henderson <[email protected]> 19 */ 20 21 /* 22 * Try to send data end-to-end through a LrWpanMac <-> LrWpanPhy <-> 23 * SpectrumChannel <-> LrWpanPhy <-> LrWpanMac chain 24 * 25 * Trace Phy state changes, and Mac DataIndication and DataConfirm events 26 * to stdout 27 */ 28 #include <ns3/log.h> 29 #include <ns3/core-module.h> 30 #include <ns3/lr-wpan-module.h> 31 #include <ns3/propagation-loss-model.h> 32 #include <ns3/propagation-delay-model.h> 33 #include <ns3/simulator.h> 34 #include <ns3/single-model-spectrum-channel.h> 35 #include <ns3/constant-position-mobility-model.h> 36 #include <ns3/packet.h> 37 38 #include <iostream> 39 40 using namespace ns3; 41 42 static void DataIndication (McpsDataIndicationParams params, Ptr<Packet> p) 43 { 44 NS_LOG_UNCOND ("Received packet of size " << p->GetSize ()); 45 } 46 47 static void DataConfirm (McpsDataConfirmParams params) 48 { 49 NS_LOG_UNCOND ("LrWpanMcpsDataConfirmStatus = " << params.m_status); 50 } 51 52 static void StateChangeNotification (std::string context, Time now, LrWpanPhyEnumeration oldState, LrWpanPhyEnumeration newState) 53 { 54 NS_LOG_UNCOND (context << " state change at " << now.GetSeconds () 55 << " from " << LrWpanHelper::LrWpanPhyEnumerationPrinter (oldState) 56 << " to " << LrWpanHelper::LrWpanPhyEnumerationPrinter (newState)); 57 } 58 59 int main (int argc, char *argv[]) 60 { 61 bool verbose = false; 62 63 CommandLine cmd; 64 65 cmd.AddValue ("verbose", "turn on all log components", verbose); 66 67 cmd.Parse (argc, argv); 68 69 LrWpanHelper lrWpanHelper; 70 if (verbose) 71 { 72 lrWpanHelper.EnableLogComponents (); 73 } 74 75 // Enable calculation of FCS in the trailers. Only necessary when interacting with real devices or wireshark. 76 // GlobalValue::Bind ("ChecksumEnabled", BooleanValue (true)); 77 78 // Create 2 nodes, and a NetDevice for each one 79 Ptr<Node> n0 = CreateObject <Node> (); 80 Ptr<Node> n1 = CreateObject <Node> (); 81 82 Ptr<LrWpanNetDevice> dev0 = CreateObject<LrWpanNetDevice> (); 83 Ptr<LrWpanNetDevice> dev1 = CreateObject<LrWpanNetDevice> (); 84 85 dev0->SetAddress (Mac16Address ("00:01")); 86 dev1->SetAddress (Mac16Address ("00:02")); 87 88 // Each device must be attached to the same channel 89 Ptr<SingleModelSpectrumChannel> channel = CreateObject<SingleModelSpectrumChannel> (); 90 Ptr<LogDistancePropagationLossModel> propModel = CreateObject<LogDistancePropagationLossModel> (); 91 Ptr<ConstantSpeedPropagationDelayModel> delayModel = CreateObject<ConstantSpeedPropagationDelayModel> (); 92 channel->AddPropagationLossModel (propModel); 93 channel->SetPropagationDelayModel (delayModel); 94 95 dev0->SetChannel (channel); 96 dev1->SetChannel (channel); 97 98 // To complete configuration, a LrWpanNetDevice must be added to a node 99 n0->AddDevice (dev0); 100 n1->AddDevice (dev1); 101 102 // Trace state changes in the phy 103 dev0->GetPhy ()->TraceConnect ("TrxState", std::string ("phy0"), MakeCallback (&StateChangeNotification)); 104 dev1->GetPhy ()->TraceConnect ("TrxState", std::string ("phy1"), MakeCallback (&StateChangeNotification)); 105 106 Ptr<ConstantPositionMobilityModel> sender0Mobility = CreateObject<ConstantPositionMobilityModel> (); 107 sender0Mobility->SetPosition (Vector (0,0,0)); 108 dev0->GetPhy ()->SetMobility (sender0Mobility); 109 Ptr<ConstantPositionMobilityModel> sender1Mobility = CreateObject<ConstantPositionMobilityModel> (); 110 // Configure position 10 m distance 111 sender1Mobility->SetPosition (Vector (0,10,0)); 112 dev1->GetPhy ()->SetMobility (sender1Mobility); 113 114 McpsDataConfirmCallback cb0; 115 cb0 = MakeCallback (&DataConfirm); 116 dev0->GetMac ()->SetMcpsDataConfirmCallback (cb0); 117 118 McpsDataIndicationCallback cb1; 119 cb1 = MakeCallback (&DataIndication); 120 dev0->GetMac ()->SetMcpsDataIndicationCallback (cb1); 121 122 McpsDataConfirmCallback cb2; 123 cb2 = MakeCallback (&DataConfirm); 124 dev1->GetMac ()->SetMcpsDataConfirmCallback (cb2); 125 126 McpsDataIndicationCallback cb3; 127 cb3 = MakeCallback (&DataIndication); 128 dev1->GetMac ()->SetMcpsDataIndicationCallback (cb3); 129 130 // Tracing 131 lrWpanHelper.EnablePcapAll (std::string ("lr-wpan-data"), true); 132 AsciiTraceHelper ascii; 133 Ptr<OutputStreamWrapper> stream = ascii.CreateFileStream ("lr-wpan-data.tr"); 134 lrWpanHelper.EnableAsciiAll (stream); 135 136 // The below should trigger two callbacks when end-to-end data is working 137 // 1) DataConfirm callback is called 138 // 2) DataIndication callback is called with value of 50 139 Ptr<Packet> p0 = Create<Packet> (50); // 50 bytes of dummy data 140 McpsDataRequestParams params; 141 params.m_srcAddrMode = SHORT_ADDR; 142 params.m_dstAddrMode = SHORT_ADDR; 143 params.m_dstPanId = 0; 144 params.m_dstAddr = Mac16Address ("00:02"); 145 params.m_msduHandle = 0; 146 params.m_txOptions = TX_OPTION_ACK; 147 // dev0->GetMac ()->McpsDataRequest (params, p0); 148 Simulator::ScheduleWithContext (1, Seconds (0.0), 149 &LrWpanMac::McpsDataRequest, 150 dev0->GetMac (), params, p0); 151 152 // Send a packet back at time 2 seconds 153 Ptr<Packet> p2 = Create<Packet> (60); // 60 bytes of dummy data 154 params.m_dstAddr = Mac16Address ("00:01"); 155 Simulator::ScheduleWithContext (2, Seconds (2.0), 156 &LrWpanMac::McpsDataRequest, 157 dev1->GetMac (), params, p2); 158 159 Simulator::Run (); 160 161 Simulator::Destroy (); 162 return 0; 163 }
|
Это сообщение отредактировал(а) ARTEMH - 18.6.2015, 21:11
|