wifi-simple-adhoc example packets reception time

1,698 views
Skip to first unread message

Hassan

unread,
Sep 20, 2013, 10:23:40 AM9/20/13
to ns-3-...@googlegroups.com
I would like to display the timing of the received packets in the wifi-simple-adhoc.cc example. I would like to display the packets reception timing at the nodes connected to the AP............Can anyone help me??

Konstantinos

unread,
Sep 20, 2013, 10:30:30 AM9/20/13
to ns-3-...@googlegroups.com
Hi Hassan,

Do you just need the time (simulation time) that they were received or the delay (time difference from the Tx to Rx)?

The first is easy, just add a print with either the NS_LOG component or with standard std::cout and print the Simulator::Now() within the ReceivePacket method.

For the second, you have to keep track the time they were sent. This can be done by adding the SeqTsHeader to the packets and then getting the time they were transmitted out of this header.

Regards,
K. 
Message has been deleted

Hassan

unread,
Sep 20, 2013, 10:52:50 AM9/20/13
to ns-3-...@googlegroups.com
Thanks a lot Konstantinos for your reply........Actually I need the second one which is the reception time of the packets........Actually I am quite new to ns3 so could you show me where exactly I could add this function in the code.

And this the code of the example:

/* -*-  Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
/*
 * Copyright (c) 2009 The Boeing Company
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation;
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

//
// This script configures two nodes on an 802.11b physical layer, with
// 802.11b NICs in adhoc mode, and by default, sends one packet of 1000
// (application) bytes to the other node.  The physical layer is configured
// to receive at a fixed RSS (regardless of the distance and transmit
// power); therefore, changing position of the nodes has no effect.
//
// There are a number of command-line options available to control
// the default behavior.  The list of available command-line options
// can be listed with the following command:
// ./waf --run "wifi-simple-adhoc --help"
//
// For instance, for this configuration, the physical layer will
// stop successfully receiving packets when rss drops below -97 dBm.
// To see this effect, try running:
//
// ./waf --run "wifi-simple-adhoc --rss=-97 --numPackets=20"
// ./waf --run "wifi-simple-adhoc --rss=-98 --numPackets=20"
// ./waf --run "wifi-simple-adhoc --rss=-99 --numPackets=20"
//
// Note that all ns-3 attributes (not just the ones exposed in the below
// script) can be changed at command line; see the documentation.
//
// This script can also be helpful to put the Wifi layer into verbose
// logging mode; this command will turn on all wifi logging:
//
// ./waf --run "wifi-simple-adhoc --verbose=1"
//
// When you are done, you will notice two pcap trace files in your directory.
// If you have tcpdump installed, you can try this:
//
// tcpdump -r wifi-simple-adhoc-0-0.pcap -nn -tt
//

#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/mobility-module.h"
#include "ns3/config-store-module.h"
#include "ns3/wifi-module.h"
#include "ns3/internet-module.h"

#include <iostream>
#include <fstream>
#include <vector>
#include <string>

NS_LOG_COMPONENT_DEFINE ("WifiSimpleAdhoc");

using namespace ns3;

void ReceivePacket (Ptr<Socket> socket)
{
  NS_LOG_UNCOND ("Received one packet!");
}

static void GenerateTraffic (Ptr<Socket> socket, uint32_t pktSize,
                             uint32_t pktCount, Time pktInterval )
{
 if (pktCount > 0)
    {
      socket->Send (Create<Packet> (pktSize));
        Simulator::Schedule (pktInterval, &GenerateTraffic,
                           socket, pktSize,pktCount-1, pktInterval);
    }
  else
    {
      socket->Close ();
    }
}


int main (int argc, char *argv[])
{
  std::string phyMode ("DsssRate1Mbps");
  double rss = -80;  // -dBm
  uint32_t packetSize = 1000; // bytes
  uint32_t numPackets = 1;
  double interval = 1.0; // seconds
  bool verbose = false;

  CommandLine cmd;

  cmd.AddValue ("phyMode", "Wifi Phy mode", phyMode);
  cmd.AddValue ("rss", "received signal strength", rss);
  cmd.AddValue ("packetSize", "size of application packet sent", packetSize);
  cmd.AddValue ("numPackets", "number of packets generated", numPackets);
  cmd.AddValue ("interval", "interval (seconds) between packets", interval);
  cmd.AddValue ("verbose", "turn on all WifiNetDevice log components", verbose);

  cmd.Parse (argc, argv);

  // Convert to time object
  Time interPacketInterval = Seconds (interval);

  // disable fragmentation for frames below 2200 bytes
  Config::SetDefault ("ns3::WifiRemoteStationManager::FragmentationThreshold", StringValue ("2200"));
  // turn off RTS/CTS for frames below 2200 bytes
  Config::SetDefault ("ns3::WifiRemoteStationManager::RtsCtsThreshold", StringValue ("2200"));
  // Fix non-unicast data rate to be the same as that of unicast
  Config::SetDefault ("ns3::WifiRemoteStationManager::NonUnicastMode",
                      StringValue (phyMode));

  NodeContainer c;
  c.Create (4);

  // The below set of helpers will help us to put together the wifi NICs we want
  WifiHelper wifi;
   if (verbose)
  {
      wifi.EnableLogComponents ();  // Turn on all Wifi logging
  }
  wifi.SetStandard (WIFI_PHY_STANDARD_80211b);

  YansWifiPhyHelper wifiPhy =  YansWifiPhyHelper::Default ();
  // This is one parameter that matters when using FixedRssLossModel
  // set it to zero; otherwise, gain will be added
  wifiPhy.Set ("RxGain", DoubleValue (0) );
  // ns-3 supports RadioTap and Prism tracing extensions for 802.11b
  wifiPhy.SetPcapDataLinkType (YansWifiPhyHelper::DLT_IEEE802_11_RADIO);

  YansWifiChannelHelper wifiChannel;
  wifiChannel.SetPropagationDelay ("ns3::ConstantSpeedPropagationDelayModel");
  // The below FixedRssLossModel will cause the rss to be fixed regardless
  // of the distance between the two stations, and the transmit power
  wifiChannel.AddPropagationLoss ("ns3::FixedRssLossModel","Rss",DoubleValue (rss));
  wifiPhy.SetChannel (wifiChannel.Create ());

  // Add a non-QoS upper mac, and disable rate control
  NqosWifiMacHelper wifiMac = NqosWifiMacHelper::Default ();
  wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager",
                                "DataMode",StringValue (phyMode),
                                "ControlMode",StringValue (phyMode));
  // Set it to adhoc mode
  wifiMac.SetType ("ns3::AdhocWifiMac");
  NetDeviceContainer devices = wifi.Install (wifiPhy, wifiMac, c);

  // Note that with FixedRssLossModel, the positions below are not
  // used for received signal strength.
  MobilityHelper mobility;
  Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator> ();
  positionAlloc->Add (Vector (0.0, 0.0, 0.0));
  positionAlloc->Add (Vector (5.0, 0.0, 0.0));
  mobility.SetPositionAllocator (positionAlloc);
  mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
  mobility.Install (c);

  InternetStackHelper internet;
  internet.Install (c);

  Ipv4AddressHelper ipv4;
  NS_LOG_INFO ("Assign IP Addresses.");
  ipv4.SetBase ("10.1.1.0", "255.255.255.0");
  Ipv4InterfaceContainer i = ipv4.Assign (devices);

  TypeId tid = TypeId::LookupByName ("ns3::UdpSocketFactory");
  Ptr<Socket> recvSink = Socket::CreateSocket (c.Get (0), tid);
  InetSocketAddress local = InetSocketAddress (Ipv4Address::GetAny (), 80);
  recvSink->Bind (local);
  recvSink->SetRecvCallback (MakeCallback (&ReceivePacket));

  Ptr<Socket> source = Socket::CreateSocket (c.Get (1), tid);
  InetSocketAddress remote = InetSocketAddress (Ipv4Address ("255.255.255.255"), 80);
  source->SetAllowBroadcast (true);
  source->Connect (remote);

  // Tracing
  wifiPhy.EnablePcap ("wifi-simple-adhoc", devices);

  wifiPhy.EnablePcap ("zzz", devices.Get(3));


  // Output what we are doing
  NS_LOG_UNCOND ("Testing " << numPackets  << " packets sent with receiver rss " << rss );


     Simulator::ScheduleWithContext (source->GetNode ()->GetId (),
                                  Seconds (1.0), &GenerateTraffic,
                                 source, packetSize, numPackets, interPacketInterval);

  Simulator::Run ();
  Simulator::Destroy ();

  return 0;
}

Am Freitag, 20. September 2013 16:30:30 UTC+2 schrieb Konstantinos:
- zitierten Text einblenden -

Konstantinos

unread,
Sep 20, 2013, 11:25:15 AM9/20/13
to ns-3-...@googlegroups.com
Hi,

First of all, welcome to the ns-3 mailing list and please follow the common rules of posting in it. Do not post big chunks of code because it will become unreadable.
Especially your scenario which is an example script that everyone gets with NS-3.

To answer now your question, the second metric I mentioned in my post is not the reception time. It is the delay from the transmission to the reception.
The reception time, is the time a packet was received is the first.

To print the (simulation) time a packet is received you have just to add a printing method when you receive it. This will look like this:

void ReceivePacket (Ptr<Socket> socket)
{
  NS_LOG_UNCOND ("Received one packet at time T = " << Simulator::Now());
}

~~~~
To print the time difference between Tx and Rx you have to add a header or a tag with the time it was created. The packet is created with the GenerateTraffic method in this scenario. Then, when you receive it (in the ReceivePacket) you have to 'extract' this information.
Please read the documentation and search the mailing list on how you can add a packet tag or a header.


Regards,
K. 

Hassan

unread,
Sep 23, 2013, 5:42:26 AM9/23/13
to ns-3-...@googlegroups.com
Hi,

Actually I used this method addHeader() but in the documentation there is no clear example showing how to track the packet Tx and Rx timing using this method. 

Konstantinos

unread,
Sep 23, 2013, 6:02:32 AM9/23/13
to ns-3-...@googlegroups.com
When you create a SeqTsHeader it 'automatically' gets the timestamp of that time.
Then you add the header to the packet and you send it.
See this documentation on how to add/remove a header here: http://www.nsnam.org/support/faq/miscellaneous/

See in red the changes you have to do in the GenerateTraffic() in order to add the header. Then, you will need to remove the header in the ReceivePacket() and calculate the time difference between the timestamp on the packet header and the timestamp you received the packet.

     Ptr<Packet> pkt = Packet(pktSize);
     SeqTsHeader hdr = SeqTsHeader();
     pkt->AddHeader(hdr); 
      socket->Send (pkt);

        Simulator::Schedule (pktInterval, &GenerateTraffic, 
                           socket, pktSize,pktCount-1, pktInterval);

Konstantinos

unread,
Sep 23, 2013, 6:03:55 AM9/23/13
to ns-3-...@googlegroups.com
When you create a SeqTsHeader it 'automatically' gets the timestamp of that time.
Then you add the header to the packet and you send it.
See this documentation on how to add/remove a header here: http://www.nsnam.org/support/faq/miscellaneous/

See in red the changes you have to do in the GenerateTraffic() in order to add the header. Then, you will need to remove the header in the ReceivePacket() and calculate the time difference between the timestamp on the packet header and the timestamp you received the packet.

     Ptr<Packet> pkt = Packet(pktSize);
     SeqTsHeader hdr = SeqTsHeader();
     pkt->AddHeader(hdr); 
      socket->Send (pkt);

        Simulator::Schedule (pktInterval, &GenerateTraffic, 
                           socket, pktSize,pktCount-1, pktInterval);

Regards,
K.

On Monday, September 23, 2013 10:42:26 AM UTC+1, Hassan wrote:

Hassan

unread,
Sep 23, 2013, 7:15:50 AM9/23/13
to ns-3-...@googlegroups.com
Hi,

I tried this code however I receive an Error.
This is the way I added the code to the example:


static void GenerateTraffic (Ptr<Socket> socket, uint32_t pktSize,
                             uint32_t pktCount, Time pktInterval )
{
 if (pktCount > 0)
    {
      Ptr<Packet> pkt = Packet(pktSize);
      SeqTsHeader hdr = SeqTsHeader();
      pkt->AddHeader(hdr);
      socket->Send (pkt);
      //socket->Send (Create<Packet> (pktSize));

        Simulator::Schedule (pktInterval, &GenerateTraffic,
                           socket, pktSize,pktCount-1, pktInterval);
    }
  else
    {
      socket->Close ();
    }
}

Thank you for your help.

Konstantinos

unread,
Sep 23, 2013, 8:27:35 AM9/23/13
to ns-3-...@googlegroups.com
What is the error? An "undefined" class?
That's because you have to include the appropriate header files in your script (the header file for SeqTsHeader). 

Hassan

unread,
Sep 23, 2013, 8:55:22 AM9/23/13
to ns-3-...@googlegroups.com
Hi,

This is the error that I receive:

../examples/wireless/wifi-simple-adhoc.cc: In function ‘void GenerateTraffic(ns3::Ptr<ns3::Socket>, uint32_t, uint32_t, ns3::Time)’:
../examples/wireless/wifi-simple-adhoc.cc:85:40: error: conversion from ‘ns3::Packet’ to non-scalar type ‘ns3::Ptr<ns3::Packet>’ requested

Sincerely
Hassan

Konstantinos

unread,
Sep 23, 2013, 10:11:37 AM9/23/13
to ns-3-...@googlegroups.com
Just change the 

 Ptr<Packet> pkt = Packet(pktSize);

with
Ptr<Packet> pkt = Create<Packet> (pktSize);

Hassan

unread,
Sep 23, 2013, 10:54:18 AM9/23/13
to ns-3-...@googlegroups.com
Hi,

Thanks Konstatinos for your help.
Actually I tried this change but still I receive an error:

examples/wireless/wifi-simple-adhoc.cc.8.o: In function `GenerateTraffic':
/home/hassan/ns/ns-allinone-3.17/ns-3.17/build/../examples/wireless/wifi-simple-adhoc.cc:87: undefined reference to `ns3::SeqTsHeader::SeqTsHeader()'
examples/wireless/wifi-simple-adhoc.cc.8.o: In function `ns3::SeqTsHeader::~SeqTsHeader()':
/home/hassan/ns/ns-allinone-3.17/ns-3.17/build/./ns3/seq-ts-header.h:35: undefined reference to `vtable for ns3::SeqTsHeader'
collect2: error: ld returned 1 exit status




Konstantinos

unread,
Sep 23, 2013, 11:01:12 AM9/23/13
to ns-3-...@googlegroups.com
I have already given you guidance how to solve this, include the appropriate header file to your scenario.

Hassan

unread,
Sep 25, 2013, 5:14:02 AM9/25/13
to ns-3-...@googlegroups.com
Hi,

I added:
#include "ns3/seq-ts-header.h"
but still I receive the same error.

Konstantinos

unread,
Sep 25, 2013, 6:08:29 AM9/25/13
to ns-3-...@googlegroups.com
I see that you have modify the example script and you run it from the examples folder. Since you modified this, you have to add the appropriate dependencies in the wscript file. 
However, this is not the proper place (examples folder) to experiment and run scripts. It is advised to run the scripts in the /scratch folder. This is explained in the tutorial. Just copy and rename the wifi-simple-adhoc.cc in the scratch folder and run it from there.

Hassan

unread,
Sep 25, 2013, 7:51:07 AM9/25/13
to ns-3-...@googlegroups.com
I tried also running it from the scratch but still I have that error

Nissa wang

unread,
Apr 5, 2014, 2:20:46 PM4/5/14
to ns-3-...@googlegroups.com
Hi Konstantinos ,
 It's much helpful to read your answer.Could you tell me record the simulation time in about 1000 times.

在 2013年9月20日星期五UTC+8下午10时30分30秒,Konstantinos写道:

Tommaso Pecorella

unread,
Apr 5, 2014, 4:22:28 PM4/5/14
to ns-3-...@googlegroups.com
Hi,

you asked the same thing in another thread. Please avoid that at all costs.

T

Menaka S

unread,
May 29, 2014, 3:37:30 PM5/29/14
to ns-3-...@googlegroups.com
Hai ALL
I need to design a secured cross layered DSR routing protocol and using NS3 simulator.
Can anyone help me to address the follwoing issues
1. coding To retrieve the received signal strength of a node
2. Coding to compute the bit error rate of a link
3. to add hashing code in DSR routing protocol in NS3

anyone with solutions, reply

Tommaso Pecorella

unread,
May 29, 2014, 4:06:16 PM5/29/14
to ns-3-...@googlegroups.com
Can we lock this thread ?

For some unknown reason, this thread's topic is encouraging the users to post new (and totally unrelated) questions as replies to this thread and not a separate new threads (as they should).

Thanks,

T.

PS: for the records, I usually am extremely harsh toward unrelated questions in threads. However I promised to not be harsh. So I'll simply not reply to the question, I hope you appreciate.
Reply all
Reply to author
Forward
0 new messages