A Discrete-Event Network Simulator
API
lr-wpan-mlme.cc
Go to the documentation of this file.
1 /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2019 Ritsumeikan University, Shiga, Japan.
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: Alberto Gallegos Ramonet <ramonet@fc.ritsumei.ac.jp>
19  */
20 
21 /*
22  * Coordinator End Device
23  * N0 <---------------- N1
24  * (dev0) (dev1)
25  *
26  * This example demonstrate the usage of the MAC primitives involved in
27  * direct transmissions for the beacon enabled mode of IEEE 802.15.4-2011.
28  * A single packet is sent from an end device to the coordinator during the CAP
29  * of the first incoming superframe.
30  *
31  * This example do not demonstrate a full protocol stack usage.
32  * For full protocol stack usage refer to 6lowpan examples.
33  *
34  */
35 
36 
37 #include <ns3/log.h>
38 #include <ns3/core-module.h>
39 #include <ns3/lr-wpan-module.h>
40 #include <ns3/propagation-loss-model.h>
41 #include <ns3/propagation-delay-model.h>
42 #include <ns3/simulator.h>
43 #include <ns3/single-model-spectrum-channel.h>
44 #include <ns3/constant-position-mobility-model.h>
45 #include <ns3/packet.h>
46 #include <iostream>
47 
48 using namespace ns3;
49 
51 {
52  NS_LOG_UNCOND (Simulator::Now ().GetSeconds () << " secs | Received BEACON packet of size " << p->GetSize ());
53 }
54 
56 {
57  NS_LOG_UNCOND (Simulator::Now ().GetSeconds () << " secs | Received DATA packet of size " << p->GetSize ());
58 }
59 
61 {
62  // In the case of transmissions with the Ack flag activated, the transaction is only
63  // successful if the Ack was received.
65  {
66  NS_LOG_UNCOND (Simulator::Now ().GetSeconds () << " secs | Transmission successfully sent");
67  }
68 }
69 
71 {
72  NS_LOG_UNCOND (Simulator::Now ().GetSeconds () << "s Coordinator Received DATA packet (size " << p->GetSize () << " bytes)");
73 }
74 
76 {
77  if (params.m_status == MLMESTART_SUCCESS)
78  {
79  NS_LOG_UNCOND (Simulator::Now ().GetSeconds () << "Beacon status SUCESSFUL");
80  }
81 }
82 
83 
84 int main (int argc, char *argv[])
85 {
86 
87 
90  LogComponentEnable ("LrWpanMac", LOG_LEVEL_INFO);
91  LogComponentEnable ("LrWpanCsmaCa", LOG_LEVEL_INFO);
92 
93 
94  LrWpanHelper lrWpanHelper;
95 
96  // Create 2 nodes, and a NetDevice for each one
97  Ptr<Node> n0 = CreateObject <Node> ();
98  Ptr<Node> n1 = CreateObject <Node> ();
99 
100  Ptr<LrWpanNetDevice> dev0 = CreateObject<LrWpanNetDevice> ();
101  Ptr<LrWpanNetDevice> dev1 = CreateObject<LrWpanNetDevice> ();
102 
103  dev0->SetAddress (Mac16Address ("00:01"));
104  dev1->SetAddress (Mac16Address ("00:02"));
105 
106  Ptr<SingleModelSpectrumChannel> channel = CreateObject<SingleModelSpectrumChannel> ();
107  Ptr<LogDistancePropagationLossModel> propModel = CreateObject<LogDistancePropagationLossModel> ();
108  Ptr<ConstantSpeedPropagationDelayModel> delayModel = CreateObject<ConstantSpeedPropagationDelayModel> ();
109  channel->AddPropagationLossModel (propModel);
110  channel->SetPropagationDelayModel (delayModel);
111 
112  dev0->SetChannel (channel);
113  dev1->SetChannel (channel);
114 
115  n0->AddDevice (dev0);
116  n1->AddDevice (dev1);
117 
119  Ptr<ConstantPositionMobilityModel> sender0Mobility = CreateObject<ConstantPositionMobilityModel> ();
120  sender0Mobility->SetPosition (Vector (0,0,0));
121  dev0->GetPhy ()->SetMobility (sender0Mobility);
122  Ptr<ConstantPositionMobilityModel> sender1Mobility = CreateObject<ConstantPositionMobilityModel> ();
123 
124  sender1Mobility->SetPosition (Vector (0,10,0)); //10 m distance
125  dev1->GetPhy ()->SetMobility (sender1Mobility);
126 
127 
129 
131  cb0 = MakeCallback (&StartConfirm);
132  dev0->GetMac ()->SetMlmeStartConfirmCallback (cb0);
133 
136  dev1->GetMac ()->SetMcpsDataConfirmCallback (cb1);
137 
140  dev1->GetMac ()->SetMlmeBeaconNotifyIndicationCallback (cb3);
141 
143  cb4 = MakeCallback (&DataIndication);
144  dev1->GetMac ()->SetMcpsDataIndicationCallback (cb4);
145 
148  dev0->GetMac ()->SetMcpsDataIndicationCallback (cb5);
149 
150 
151 
153  // Note: We manually associate the devices to a PAN coordinator
154  // because currently there is no automatic association behavior (bootstrap);
155  // The PAN COORDINATOR does not need to associate or set its
156  // PAN Id or its own coordinator id, these are set
157  // by the MLME-start.request primitive when used.
158 
159  dev1->GetMac ()->SetPanId (5);
160  dev1->GetMac ()->SetAssociatedCoor (Mac16Address ("00:01"));
161 
162 
163 
165 
166  MlmeStartRequestParams params;
167  params.m_panCoor = true;
168  params.m_PanId = 5;
169  params.m_bcnOrd = 14;
170  params.m_sfrmOrd = 6;
173  dev0->GetMac (), params);
174 
176 
177  Ptr<Packet> p1 = Create<Packet> (5);
178  McpsDataRequestParams params2;
179  params2.m_dstPanId = 5;
180  params2.m_srcAddrMode = SHORT_ADDR;
181  params2.m_dstAddrMode = SHORT_ADDR;
182  params2.m_dstAddr = Mac16Address ("00:01");
183  params2.m_msduHandle = 0;
184  // params2.m_txOptions = TX_OPTION_ACK; // Enable direct transmission with Ack
185 
187  // Examples of time parameters for transmissions in the first incoming superframe. //
189 
190  // 2.981 sec No time to finish CCA in CAP, the transmission at this time will cause
191  // the packet to be deferred to the next superframe.
192 
193  // 2.98272 sec No time to finish random backoff delay in CAP, the transmission at this
194  // time will cause the packet to be deferred to the next superframe.
195 
196  // 2.93 sec Enough time, the packet can be transmitted within the CAP of the first superframe
197 
198 
199  // MCPS-DATA.request Beacon enabled Direct Transmission (dev1)
200  // Frame transmission from End Device to Coordinator (Direct transmission)
203  dev1->GetMac (), params2, p1);
204 
205 
206  Simulator::Stop (Seconds (600));
207  Simulator::Run ();
208 
210  return 0;
211 }
uint16_t m_dstPanId
Destination PAN identifier.
Definition: lr-wpan-mac.h:248
static void TransEndIndication(McpsDataConfirmParams params)
Definition: lr-wpan-mlme.cc:60
static void BeaconIndication(MlmeBeaconNotifyIndicationParams params, Ptr< Packet > p)
Definition: lr-wpan-mlme.cc:50
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:852
static void Run(void)
Run the simulation.
Definition: simulator.cc:170
static void DataIndication(McpsDataIndicationParams params, Ptr< Packet > p)
Definition: lr-wpan-mlme.cc:55
void McpsDataRequest(McpsDataRequestParams params, Ptr< Packet > p)
IEEE 802.15.4-2006, section 7.1.1.1 MCPS-DATA.request Request to transfer a MSDU. ...
Definition: lr-wpan-mac.cc:289
MLME-START.request params.
Definition: lr-wpan-mac.h:288
channel
Definition: third.py:92
MLME-BEACON-NOTIFY.indication params.
Definition: lr-wpan-mac.h:362
void MlmeStartRequest(MlmeStartRequestParams params)
IEEE 802.15.4-2006, section 7.1.14.1 MLME-START.request Request to allow a PAN coordinator to initiat...
Definition: lr-wpan-mac.cc:496
void LogComponentEnable(char const *name, enum LogLevel level)
Enable the logging output associated with that log component.
Definition: log.cc:370
uint8_t m_sfrmOrd
Superframe Order, indicates the length of the CAP in time slots.
Definition: lr-wpan-mac.h:307
LOG_INFO and above.
Definition: log.h:107
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1610
MCPS-DATA.confirm params.
Definition: lr-wpan-mac.h:260
uint8_t m_bcnOrd
Beacon Order, Used to calculate the beacon interval, a value of 15 indicates no periodic beacons will...
Definition: lr-wpan-mac.h:306
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:134
uint16_t m_PanId
Pan Identifier used by the device.
Definition: lr-wpan-mac.h:302
LrWpanMcpsDataConfirmStatus m_status
The status of the last MSDU transmission.
Definition: lr-wpan-mac.h:263
void LogComponentEnableAll(enum LogLevel level)
Enable the logging output for all registered log components.
Definition: log.cc:394
Prefix all trace prints with simulation time.
Definition: log.h:119
Every class exported by the ns3 library is enclosed in the ns3 namespace.
#define NS_LOG_UNCOND(msg)
Output the requested message unconditionally.
This class can contain 16 bit addresses.
Definition: mac16-address.h:41
static Time Now(void)
Return the current simulation virtual time.
Definition: simulator.cc:193
void SetPosition(const Vector &position)
static void ScheduleWithContext(uint32_t context, Time const &delay, MEM mem_ptr, OBJ obj)
Schedule an event with the given context.
Definition: simulator.h:1484
uint32_t AddDevice(Ptr< NetDevice > device)
Associate a NetDevice to this node.
Definition: node.cc:128
Prefix all trace prints with function.
Definition: log.h:118
static void Stop(void)
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:178
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1070
static void StartConfirm(MlmeStartConfirmParams params)
Definition: lr-wpan-mlme.cc:75
bool m_panCoor
On true this device will become coordinator.
Definition: lr-wpan-mac.h:308
MCPS-DATA.request params.
Definition: lr-wpan-mac.h:235
LrWpanMlmeStartConfirmStatus m_status
The status of a MLME-start.request.
Definition: lr-wpan-mac.h:355
helps to manage and create IEEE 802.15.4 NetDevice objects
MLME-START.confirm params.
Definition: lr-wpan-mac.h:353
MCPS-DATA.indication params.
Definition: lr-wpan-mac.h:270
static void DataIndicationCoordinator(McpsDataIndicationParams params, Ptr< Packet > p)
Definition: lr-wpan-mlme.cc:70