A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
lr-wpan-phy-test.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2011 The Boeing Company
3 * Copyright (c) 2026 Tokushima University, Japan: Collision test and refactoring
4 *
5 * SPDX-License-Identifier: GPL-2.0-only
6 *
7 * Authors: Gary Pei <guangyu.pei@boeing.com>
8 * Alberto Gallegos Ramonet <alramonet@is.tokushima-u.ac.jp>
9 */
10#include "ns3/log.h"
11#include "ns3/lr-wpan-mac.h"
12#include "ns3/lr-wpan-phy.h"
13#include "ns3/packet.h"
14#include "ns3/simulator.h"
15#include "ns3/single-model-spectrum-channel.h"
16#include "ns3/test.h"
17
18using namespace ns3;
19using namespace ns3::lrwpan;
20
21/**
22 * @ingroup lr-wpan-test
23 * @ingroup tests
24 *
25 * @brief LrWpan PLME and PD Interfaces Test
26 */
28{
29 public:
32
33 private:
34 uint16_t m_receivedPackets{0}; //!< Counts the number of received packets
35 void DoRun() override;
36
37 /**
38 * @brief Receives a PdData indication
39 * @param psduLength The PSDU length.
40 * @param p The packet.
41 * @param lqi The LQI.
42 * @param rssi The RSSI
43 */
44 void ReceivePdDataIndication(uint32_t psduLength, Ptr<Packet> p, uint8_t lqi, int8_t rssi);
45};
46
47/**
48 * @ingroup lr-wpan-test
49 * @ingroup tests
50 *
51 * @brief Test the collision of two packets and the reaction ( packet drop )
52 * of this collision in the packet preamble
53 */
55{
56 public:
59
60 private:
61 uint16_t m_receivedPackets{0}; //!< Counts the number of received packets
62 int8_t m_receivedRssi{0}; //!< Saves the value of the received packet RSSI
64 0}; //!< Counts the number of packets dropped due to collision
65 void DoRun() override;
66
67 /**
68 * @brief Receives a PdData indication
69 * @param psduLength The PSDU length.
70 * @param p The packet.
71 * @param lqi The LQI.
72 * @param rssi The RSSI
73 */
74 void ReceivePdDataIndication(uint32_t psduLength, Ptr<Packet> p, uint8_t lqi, int8_t rssi);
75
76 /**
77 * @brief Callback for packet drop due to collision
78 * @param p The packet.
79 */
81};
82
84 : TestCase("PLME and PD SAP IEEE 802.15.4 interfaces test")
85{
86}
87
91
92void
95 uint8_t lqi,
96 int8_t rssi)
97{
98 NS_LOG_UNCOND("At: " << Simulator::Now() << " Received frame size: " << psduLength
99 << " LQI: " << static_cast<uint16_t>(lqi)
100 << " RSSI: " << static_cast<int16_t>(rssi) << " dBm.");
101
103}
104
105void
107{
108 // This test create two Lr-WPAN PHYs and tests a simple transmission and reception
109 // of a packet. It tests that the PHY interfaces (PLME primitives) are working.
110
112 LogComponentEnable("LrWpanPhy", LOG_LEVEL_ALL);
113
116
117 // Add the SpectrumPhy to a channel and set the initial state of the PHYs
118 // This is typically taken care by the NetDevice but in this case we are using the PHY directly.
120 sender->SetChannel(channel);
121 receiver->SetChannel(channel);
122 channel->AddRx(receiver);
123 sender->PlmeSetTRXStateRequest(IEEE_802_15_4_PHY_TX_ON);
124 receiver->PlmeSetTRXStateRequest(IEEE_802_15_4_PHY_RX_ON);
125
126 receiver->SetPdDataIndicationCallback(
128
129 Ptr<Packet> p = Create<Packet>(10); // Packet with 10 bytes
130 Simulator::Schedule(Seconds(1), &LrWpanPhy::PdDataRequest, sender, p->GetSize(), p);
131
134
136 0,
137 "Error,the number of expected received packets should be more than 0");
139}
140
142 : TestCase("Collision of two IEEE 802.15.4 frames test")
143{
144}
145
149
150void
152 Ptr<Packet> p,
153 uint8_t lqi,
154 int8_t rssi)
155{
156 NS_LOG_UNCOND("At: " << Simulator::Now() << " Received frame size: " << psduLength
157 << " LQI: " << static_cast<uint16_t>(lqi)
158 << " RSSI: " << static_cast<int16_t>(rssi) << " dBm.");
159
161 m_receivedRssi = rssi;
162}
163
164void
169
170void
172{
173 // Test the reaction of the PHY to receiving two packets simultaneously.
174 // Since the receiver device will be in BUSY_RX state after the first packet
175 // the 2nd packet will not be received. The first packet should be received correctly and
176 // the second packet should be dropped due to collision and registered
177 // in the drop trace (PhyRxDrop).
178 // The second packet also affects the received RSSI
179 // of the first packet since more energy will be present at the moment of receiving the packet.
180 // This change on energy is also tested.
181
183 LogComponentEnable("LrWpanPhy", LOG_LEVEL_ALL);
184
188
189 // Add the SpectrumPhy to a channel and set the initial state of the PHYs
190 // This is typically taken care by the NetDevice but in this case we are using the PHY directly.
192 sender->SetChannel(channel);
193 sender2->SetChannel(channel);
194 receiver->SetChannel(channel);
195 channel->AddRx(receiver);
196
197 sender->PlmeSetTRXStateRequest(IEEE_802_15_4_PHY_TX_ON);
198 sender2->PlmeSetTRXStateRequest(IEEE_802_15_4_PHY_TX_ON);
199 receiver->PlmeSetTRXStateRequest(IEEE_802_15_4_PHY_RX_ON);
200
201 // Set the callback to receive packets at the receiver PHY
202 receiver->SetPdDataIndicationCallback(
204
205 // Connect to trace source to log when a packet is dropped due to collision
206 receiver->TraceConnectWithoutContext(
207 "PhyRxDrop",
209
210 Ptr<Packet> p = Create<Packet>(10); // 10 bytes
211 Ptr<Packet> p2 = Create<Packet>(9); // 9 bytes
212 Simulator::Schedule(Seconds(2), &LrWpanPhy::PdDataRequest, sender, p->GetSize(), p);
213 Simulator::Schedule(Seconds(2), &LrWpanPhy::PdDataRequest, sender2, p2->GetSize(), p2);
214
217
219 1,
220 "Error,the receiver should only be able to receive the first packet");
221
223 1,
224 "Error, one packet should be dropped due to collision");
225
226 NS_TEST_ASSERT_MSG_GT(m_receivedRssi, 0, "Error,the received RSSI should be more than 0");
227
229}
230
231/**
232 * @ingroup lr-wpan-test
233 * @ingroup tests
234 *
235 * @brief LrWpan PLME and PD Interfaces TestSuite
236 */
242
244 : TestSuite("lr-wpan-phy-test", Type::UNIT)
245{
246 // AddTestCase(new LrWpanPlmeAndPdInterfaceTestCase, TestCase::Duration::QUICK);
248}
249
250// Do not forget to allocate an instance of this TestSuite
252 g_lrWpanPlmeAndPdInterfaceTestSuite; //!< Static variable for test initialization
Test the collision of two packets and the reaction ( packet drop ) of this collision in the packet pr...
void PacketCollisionDrop(Ptr< const Packet > p)
Callback for packet drop due to collision.
void ReceivePdDataIndication(uint32_t psduLength, Ptr< Packet > p, uint8_t lqi, int8_t rssi)
Receives a PdData indication.
void DoRun() override
Implementation to actually run this TestCase.
uint16_t m_receivedPackets
Counts the number of received packets.
int8_t m_receivedRssi
Saves the value of the received packet RSSI.
uint16_t m_packetDroppedByCollision
Counts the number of packets dropped due to collision.
void ReceivePdDataIndication(uint32_t psduLength, Ptr< Packet > p, uint8_t lqi, int8_t rssi)
Receives a PdData indication.
void DoRun() override
Implementation to actually run this TestCase.
uint16_t m_receivedPackets
Counts the number of received packets.
LrWpan PLME and PD Interfaces TestSuite.
Smart pointer class similar to boost::intrusive_ptr.
Definition ptr.h:70
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition simulator.h:580
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:125
static Time Now()
Return the current simulation virtual time.
Definition simulator.cc:191
static void Run()
Run the simulation.
Definition simulator.cc:161
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition simulator.cc:169
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition test.cc:296
@ QUICK
Fast test.
Definition test.h:1057
TestCase(const TestCase &)=delete
Caller graph was not generated because of its size.
Type
Type of test.
Definition test.h:1271
TestSuite(std::string name, Type type=Type::UNIT)
Construct a new test suite.
Definition test.cc:494
void PdDataRequest(const uint32_t psduLength, Ptr< Packet > p)
IEEE 802.15.4-2006 section 6.2.1.1 PD-DATA.request Request to transfer MPDU from MAC (transmitting).
Callback< R, Args... > MakeCallback(R(T::*memPtr)(Args...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition callback.h:690
#define NS_LOG_UNCOND(msg)
Output the requested message unconditionally.
@ IEEE_802_15_4_PHY_RX_ON
@ IEEE_802_15_4_PHY_TX_ON
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:627
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition ptr.h:454
#define NS_TEST_ASSERT_MSG_EQ(actual, limit, msg)
Test that an actual and expected (limit) value are equal and report and abort if not.
Definition test.h:133
#define NS_TEST_ASSERT_MSG_GT(actual, limit, msg)
Test that an actual value is greater than a limit and report and abort if not.
Definition test.h:863
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1273
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void LogComponentEnable(const std::string &name, LogLevel level)
Enable the logging output associated with that log component.
Definition log.cc:279
LogLevel
Logging severity classes and levels.
Definition log.h:86
@ LOG_LEVEL_ALL
Print everything.
Definition log.h:108
@ LOG_PREFIX_FUNC
Prefix all trace prints with function.
Definition log.h:110
@ LOG_PREFIX_TIME
Prefix all trace prints with simulation time.
Definition log.h:111
void LogComponentEnableAll(LogLevel level)
Enable the logging output for all registered log components.
Definition log.cc:297
static LrWpanPlmeAndPdInterfaceTestSuite g_lrWpanPlmeAndPdInterfaceTestSuite
Static variable for test initialization.