A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
lr-wpan-net-device.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2011 The Boeing Company
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author:
7 * Tom Henderson <thomas.r.henderson@boeing.com>
8 * Tommaso Pecorella <tommaso.pecorella@unifi.it>
9 * Margherita Filippetti <morag87@gmail.com>
10 */
11#include "lr-wpan-net-device.h"
12
13#include "lr-wpan-csmaca.h"
14#include "lr-wpan-error-model.h"
15#include "lr-wpan-phy.h"
16
17#include "ns3/abort.h"
18#include "ns3/boolean.h"
19#include "ns3/log.h"
20#include "ns3/node.h"
21#include "ns3/packet.h"
22#include "ns3/pointer.h"
23#include "ns3/spectrum-channel.h"
24
25namespace ns3
26{
27namespace lrwpan
28{
29
30NS_LOG_COMPONENT_DEFINE("LrWpanNetDevice");
32
33TypeId
35{
36 static TypeId tid =
37 TypeId("ns3::lrwpan::LrWpanNetDevice")
38 .AddDeprecatedName("ns3::LrWpanNetDevice")
40 .SetGroupName("LrWpan")
41 .AddConstructor<LrWpanNetDevice>()
42 .AddAttribute("Channel",
43 "The channel attached to this device",
47 .AddAttribute("Phy",
48 "The PHY layer attached to this device.",
52 .AddAttribute("Mac",
53 "The MAC layer attached to this device.",
57 .AddAttribute("UseAcks",
58 "Request acknowledgments for data frames.",
59 BooleanValue(true),
62 .AddAttribute(
63 "PseudoMacAddressMode",
64 "Build the pseudo-MAC Address according to RFC 4944 or RFC 6282 "
65 "(default: RFC 6282).",
69 "RFC 6282 (don't use PanId)",
71 "RFC 4944 (use PanId)"));
72 return tid;
73}
74
88
93
94void
96{
97 NS_LOG_FUNCTION(this);
98 m_phy->Dispose();
99 m_csmaca->Dispose();
100 m_phy = nullptr;
101 m_mac = nullptr;
102 m_csmaca = nullptr;
103 m_node = nullptr;
104 // chain up.
106}
107
108void
110{
111 NS_LOG_FUNCTION(this);
112
113 m_phy->Initialize();
116
118}
119
120void
122{
123 if (!m_mac || !m_phy || !m_csmaca || !m_node || m_configComplete)
124 {
125 return;
126 }
127
128 NS_LOG_FUNCTION(this);
129 m_mac->SetMcpsDataIndicationCallback(MakeCallback(&LrWpanNetDevice::McpsDataIndication, this));
130
132 m_phy->SetErrorModel(model);
133 m_phy->SetDevice(this);
134
135 m_phy->SetPdDataIndicationCallback(MakeCallback(&LrWpanMac::PdDataIndication, m_mac));
136 m_phy->SetPdDataConfirmCallback(MakeCallback(&LrWpanMac::PdDataConfirm, m_mac));
137 m_phy->SetPlmeEdConfirmCallback(MakeCallback(&LrWpanMac::PlmeEdConfirm, m_mac));
138 m_phy->SetPlmeGetAttributeConfirmCallback(
140 m_phy->SetPlmeSetTRXStateConfirmCallback(
142 m_phy->SetPlmeSetAttributeConfirmCallback(
144
145 m_csmaca->SetLrWpanMacStateCallback(MakeCallback(&LrWpanMac::SetLrWpanMacState, m_mac));
146 m_phy->SetPlmeCcaConfirmCallback(MakeCallback(&LrWpanCsmaCa::PlmeCcaConfirm, m_csmaca));
147 m_configComplete = true;
148}
149
150void
152{
154 "MAC layer cannot be set after initialization");
155 m_mac = mac;
156}
157
158void
160{
162 "PHY layer cannot be set after initialization");
163 m_phy = phy;
164}
165
166void
168{
169 NS_ABORT_MSG_IF(LrWpanNetDevice::IsInitialized(), "CSMA/CA cannot be set after initialization");
170 m_csmaca = csmaca;
171}
172
173void
175{
177 "Spectrum channel cannot be set after initialization");
178 m_phy->SetChannel(channel);
179 channel->AddRx(m_phy);
180}
181
184{
185 return m_mac;
186}
187
190{
191 return m_phy;
192}
193
196{
197 return m_csmaca;
198}
199
200void
202{
203 m_ifIndex = index;
204}
205
208{
209 return m_ifIndex;
210}
211
214{
215 return m_phy->GetChannel();
216}
217
218void
220{
221 NS_LOG_FUNCTION(this);
222 m_linkUp = true;
224}
225
226void
228{
229 NS_LOG_FUNCTION(this);
230 m_linkUp = false;
232}
233
236{
237 NS_LOG_FUNCTION(this);
238 return m_phy->GetChannel();
239}
240
241void
243{
244 NS_LOG_FUNCTION(this);
245 if (Mac16Address::IsMatchingType(address))
246 {
247 m_mac->SetShortAddress(Mac16Address::ConvertFrom(address));
248 }
249 else if (Mac64Address::IsMatchingType(address))
250 {
251 m_mac->SetExtendedAddress(Mac64Address::ConvertFrom(address));
252 }
253 else if (Mac48Address::IsMatchingType(address))
254 {
255 uint8_t buf[6];
257 addr.CopyTo(buf);
258 Mac16Address addr16;
259 addr16.CopyFrom(buf + 4);
260 m_mac->SetShortAddress(addr16);
261 uint16_t panId;
262 panId = buf[0];
263 panId <<= 8;
264 panId |= buf[1];
265 m_mac->SetPanId(panId);
266 }
267 else
268 {
269 NS_ABORT_MSG("LrWpanNetDevice::SetAddress - address is not of a compatible type");
270 }
271}
272
275{
276 NS_LOG_FUNCTION(this);
277
278 if (m_mac->GetShortAddress() == Mac16Address("00:00"))
279 {
280 return m_mac->GetExtendedAddress();
281 }
282
283 Mac48Address pseudoAddress = BuildPseudoMacAddress(m_mac->GetPanId(), m_mac->GetShortAddress());
284
285 return pseudoAddress;
286}
287
288void
290 Mac64Address coordExtAddr,
291 Mac16Address coordShortAddr,
292 Mac16Address assignedShortAddr)
293{
294 NS_LOG_FUNCTION(this);
295 m_mac->SetPanId(panId);
296 m_mac->SetAssociatedCoor(coordExtAddr);
297 m_mac->SetAssociatedCoor(coordShortAddr);
298 m_mac->SetShortAddress(assignedShortAddr);
299}
300
301bool
302LrWpanNetDevice::SetMtu(const uint16_t mtu)
303{
304 NS_ABORT_MSG("Unsupported");
305 return false;
306}
307
308uint16_t
310{
311 NS_LOG_FUNCTION(this);
312 // Maximum payload size is: max psdu - frame control - seqno - addressing - security - fcs
313 // = 127 - 2 - 1 - (2+2+2+2) - 0 - 2
314 // = 114
315 // assuming no security and addressing with only 16 bit addresses without pan id compression.
316 return 114;
317}
318
319bool
321{
322 NS_LOG_FUNCTION(this);
323 return m_phy && m_linkUp;
324}
325
326void
328{
329 NS_LOG_FUNCTION(this);
330 m_linkChanges.ConnectWithoutContext(callback);
331}
332
333bool
335{
336 NS_LOG_FUNCTION(this);
337 return true;
338}
339
342{
343 NS_LOG_FUNCTION(this);
344
345 Mac48Address pseudoAddress =
347
348 return pseudoAddress;
349}
350
351bool
353{
354 NS_LOG_FUNCTION(this);
355 return true;
356}
357
360{
361 NS_ABORT_MSG("Unsupported");
362 return Address();
363}
364
367{
368 NS_LOG_FUNCTION(this << addr);
369
370 Mac48Address pseudoAddress =
372
373 return pseudoAddress;
374}
375
376bool
378{
379 NS_LOG_FUNCTION(this);
380 return false;
381}
382
383bool
385{
386 NS_LOG_FUNCTION(this);
387 return false;
388}
389
390bool
391LrWpanNetDevice::Send(Ptr<Packet> packet, const Address& dest, uint16_t protocolNumber)
392{
393 // This method basically assumes an 802.3-compliant device, but a raw
394 // 802.15.4 device does not have an ethertype, and requires specific
395 // McpsDataRequest parameters.
396 // For further study: how to support these methods somehow, such as
397 // inventing a fake ethertype and packet tag for McpsDataRequest
398 NS_LOG_FUNCTION(this << packet << dest << protocolNumber);
399
400 if (packet->GetSize() > GetMtu())
401 {
402 NS_LOG_ERROR("Fragmentation is needed for this packet, drop the packet ");
403 return false;
404 }
405
406 McpsDataRequestParams m_mcpsDataRequestParams;
407
408 Mac16Address dst16;
410 {
411 uint8_t buf[6];
412 dest.CopyTo(buf);
413 dst16.CopyFrom(buf + 4);
414 }
415 else
416 {
417 dst16 = Mac16Address::ConvertFrom(dest);
418 }
419 m_mcpsDataRequestParams.m_dstAddr = dst16;
420 m_mcpsDataRequestParams.m_dstAddrMode = SHORT_ADDR;
421 m_mcpsDataRequestParams.m_dstPanId = m_mac->GetPanId();
422 m_mcpsDataRequestParams.m_srcAddrMode = SHORT_ADDR;
423 // Using ACK requests for broadcast destinations is ok here. They are disabled
424 // by the MAC.
425 if (m_useAcks)
426 {
427 m_mcpsDataRequestParams.m_txOptions = TX_OPTION_ACK;
428 }
429 m_mcpsDataRequestParams.m_msduHandle = 0;
430 m_mac->McpsDataRequest(m_mcpsDataRequestParams, packet);
431 return true;
432}
433
434bool
436 const Address& source,
437 const Address& dest,
438 uint16_t protocolNumber)
439{
440 NS_ABORT_MSG("Unsupported");
441 // TODO: To support SendFrom, the MACs McpsDataRequest has to use the provided source address,
442 // instead of to local one.
443 return false;
444}
445
448{
449 return m_node;
450}
451
452void
454{
455 m_node = node;
457}
458
459bool
461{
462 NS_LOG_FUNCTION(this);
463 return true;
464}
465
466void
472
473void
475{
476 // This method basically assumes an 802.3-compliant device, but a raw
477 // 802.15.4 device does not have an ethertype, and requires specific
478 // McpsDataIndication parameters.
479 // For further study: how to support these methods somehow, such as
480 // inventing a fake ethertype and packet tag for McpsDataRequest
481 NS_LOG_WARN("Unsupported; use LrWpan MAC APIs instead");
482}
483
484void
486{
487 NS_LOG_FUNCTION(this);
488 // TODO: Use the PromiscReceiveCallback if the MAC is in promiscuous mode.
489
490 if (params.m_dstAddrMode == SHORT_ADDR)
491 {
492 m_receiveCallback(this, pkt, 0, BuildPseudoMacAddress(params.m_srcPanId, params.m_srcAddr));
493 }
494 else
495 {
496 m_receiveCallback(this, pkt, 0, params.m_srcExtAddr);
497 }
498}
499
500bool
502{
504 return false;
505}
506
509{
510 NS_LOG_FUNCTION(this);
511
512 uint8_t buf[6];
513
515 {
516 buf[0] = panId >> 8;
517 // Make sure the U/L bit is set
518 buf[0] |= 0x02;
519 buf[1] = panId & 0xff;
520 }
521 else
522 {
523 // Make sure the U/L bit is set
524 buf[0] = 0x02;
525 buf[1] = 0x00;
526 }
527 buf[2] = 0;
528 buf[3] = 0;
529 shortAddr.CopyTo(buf + 4);
530
531 Mac48Address pseudoAddress;
532 pseudoAddress.CopyFrom(buf);
533
534 return pseudoAddress;
535}
536
537int64_t
539{
540 NS_LOG_FUNCTION(stream);
541 int64_t streamIndex = stream;
542 streamIndex += m_csmaca->AssignStreams(stream);
543 streamIndex += m_phy->AssignStreams(stream);
544 streamIndex += m_mac->AssignStreams(stream);
545 NS_LOG_DEBUG("Number of assigned RV streams: " << (streamIndex - stream));
546 return (streamIndex - stream);
547}
548
549} // namespace lrwpan
550} // namespace ns3
a polymophic address class
Definition address.h:114
uint32_t CopyTo(uint8_t buffer[MAX_SIZE]) const
Copy the address bytes into a buffer.
Definition address.cc:70
AttributeValue implementation for Boolean.
Definition boolean.h:26
Callback template class.
Definition callback.h:428
Hold variables of type enum.
Definition enum.h:52
Ipv4 addresses are stored in host order in this class.
Describes an IPv6 address.
This class can contain 16 bit addresses.
static Mac16Address GetMulticast(Ipv6Address address)
Returns the multicast address associated with an IPv6 address according to RFC 4944 Section 9.
static bool IsMatchingType(const Address &address)
static Mac16Address ConvertFrom(const Address &address)
void CopyTo(uint8_t buffer[2]) const
void CopyFrom(const uint8_t buffer[2])
static Mac16Address GetBroadcast()
an EUI-48 address
static bool IsMatchingType(const Address &address)
void CopyFrom(const uint8_t buffer[6])
static Mac48Address ConvertFrom(const Address &address)
void CopyTo(uint8_t buffer[6]) const
an EUI-64 address
static bool IsMatchingType(const Address &address)
static Mac64Address ConvertFrom(const Address &address)
Network layer to device interface.
Definition net-device.h:87
Callback< bool, Ptr< NetDevice >, Ptr< const Packet >, uint16_t, const Address &, const Address &, PacketType > PromiscReceiveCallback
Definition net-device.h:341
Callback< bool, Ptr< NetDevice >, Ptr< const Packet >, uint16_t, const Address & > ReceiveCallback
Definition net-device.h:311
virtual void DoInitialize()
Initialize() implementation.
Definition object.cc:437
void AggregateObject(Ptr< Object > other)
Aggregate two Objects together.
Definition object.cc:295
virtual void DoDispose()
Destructor implementation.
Definition object.cc:430
bool IsInitialized() const
Check if the object has been initialized.
Definition object.cc:237
AttributeValue implementation for Pointer.
Definition pointer.h:37
Smart pointer class similar to boost::intrusive_ptr.
Definition ptr.h:70
a unique identifier for an interface.
Definition type-id.h:50
TypeId AddDeprecatedName(const std::string &name)
Add an deprecated name for a TypeId.
Definition type-id.cc:860
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition type-id.cc:999
void PlmeCcaConfirm(PhyEnumeration status)
IEEE 802.15.4-2006 section 6.2.2.2 PLME-CCA.confirm status.
void PdDataConfirm(PhyEnumeration status)
IEEE 802.15.4-2006 section 6.2.1.2 Confirm the end of transmission of an MPDU to MAC.
void SetLrWpanMacState(MacState macState)
CSMA-CA algorithm calls back the MAC after executing channel assessment.
void PdDataIndication(uint32_t psduLength, Ptr< Packet > p, uint8_t lqi)
IEEE 802.15.4-2006 section 6.2.1.3 PD-DATA.indication Indicates the transfer of an MPDU from PHY to M...
void PlmeGetAttributeConfirm(PhyEnumeration status, PhyPibAttributeIdentifier id, Ptr< PhyPibAttributes > attribute)
IEEE 802.15.4-2006 section 6.2.2.6 PLME-GET.confirm Get attributes per definition from Table 23 in se...
void PlmeSetAttributeConfirm(PhyEnumeration status, PhyPibAttributeIdentifier id)
IEEE 802.15.4-2006 section 6.2.2.10 PLME-SET.confirm Set attributes per definition from Table 23 in s...
void PlmeSetTRXStateConfirm(PhyEnumeration status)
IEEE 802.15.4-2006 section 6.2.2.8 PLME-SET-TRX-STATE.confirm Set PHY state.
void PlmeEdConfirm(PhyEnumeration status, uint8_t energyLevel)
IEEE 802.15.4-2006 section 6.2.2.4 PLME-ED.confirm status and energy level.
Network layer to device interface.
bool SupportsSendFrom() const override
void LinkDown()
Mark NetDevice link as down.
Address GetMulticast(Ipv4Address multicastGroup) const override
Make and return a MAC multicast address using the provided multicast group.
bool Send(Ptr< Packet > packet, const Address &dest, uint16_t protocolNumber) override
Ptr< LrWpanPhy > m_phy
The PHY for this NetDevice.
uint32_t m_ifIndex
The interface index of this NetDevice.
void LinkUp()
Mark NetDevice link as up.
PseudoMacAddressMode_e m_pseudoMacMode
How the pseudo MAC address is created.
Ptr< LrWpanMac > m_mac
The MAC for this NetDevice.
Ptr< LrWpanMac > GetMac() const
Get the MAC used by this NetDevice.
uint16_t GetMtu() const override
int64_t AssignStreams(int64_t stream)
Assign a fixed random variable stream number to the random variables used by this model.
void SetIfIndex(const uint32_t index) override
bool SendFrom(Ptr< Packet > packet, const Address &source, const Address &dest, uint16_t protocolNumber) override
void SetAddress(Address address) override
This method indirects to LrWpanMac::SetShortAddress ().
ReceiveCallback m_receiveCallback
Upper layer callback used for notification of new data packet arrivals.
bool m_configComplete
True if MAC, PHY and CSMA/CA where successfully configured and the NetDevice is ready for being used.
bool SetMtu(const uint16_t mtu) override
Ptr< SpectrumChannel > DoGetChannel() const
Attribute accessor method for the "Channel" attribute.
bool m_useAcks
Configure the NetDevice to request MAC layer acknowledgments when sending packets using the Send() AP...
void SetPanAssociation(uint16_t panId, Mac64Address coordExtAddr, Mac16Address coordShortAddr, Mac16Address assignedShortAddr)
This method is use to manually configure the coordinator through which the device or coordinator is a...
Address GetAddress() const override
This method indirects to LrWpanMac::SetShortAddress ().
void AddLinkChangeCallback(Callback< void > callback) override
uint32_t GetIfIndex() const override
@ RFC4944
YYYY:0000:XXXX (with U/L bit set to local).
void SetCsmaCa(Ptr< LrWpanCsmaCa > csmaca)
Set the CSMA/CA implementation to be used by the MAC and this NetDevice.
bool IsBroadcast() const override
void CompleteConfig()
Configure PHY, MAC and CSMA/CA.
void SetPromiscReceiveCallback(PromiscReceiveCallback cb) override
Ptr< LrWpanCsmaCa > m_csmaca
The CSMA/CA implementation for this NetDevice.
TracedCallback m_linkChanges
Trace source for link up/down changes.
Address GetBroadcast() const override
Ptr< Channel > GetChannel() const override
Mac48Address BuildPseudoMacAddress(uint16_t panId, Mac16Address shortAddr) const
Builds a "pseudo 48-bit address" from the PanId and Short Address The form is PanId : 0x0 : 0x0 : Sho...
Ptr< LrWpanCsmaCa > GetCsmaCa() const
Get the CSMA/CA implementation used by this NetDevice.
void SetPhy(Ptr< LrWpanPhy > phy)
Set the PHY to be used by the MAC and this NetDevice.
void DoDispose() override
Destructor implementation.
bool IsPointToPoint() const override
Return true if the net device is on a point-to-point link.
void SetChannel(Ptr< SpectrumChannel > channel)
Set the channel to which the NetDevice, and therefore the PHY, should be attached to.
Ptr< LrWpanPhy > GetPhy() const
Get the PHY used by this NetDevice.
bool m_linkUp
Is the link/device currently up and running?
bool IsBridge() const override
Return true if the net device is acting as a bridge.
void SetNode(Ptr< Node > node) override
bool IsMulticast() const override
static TypeId GetTypeId()
Get the type ID.
void McpsDataIndication(McpsDataIndicationParams params, Ptr< Packet > pkt)
The callback used by the MAC to hand over incoming packets to the NetDevice.
void SetReceiveCallback(NetDevice::ReceiveCallback cb) override
void DoInitialize() override
Initialize() implementation.
void SetMac(Ptr< LrWpanMac > mac)
Set the MAC to be used by this NetDevice.
Ptr< Node > m_node
The node associated with this NetDevice.
Ptr< Node > GetNode() const override
Ptr< const AttributeChecker > MakeBooleanChecker()
Definition boolean.cc:113
Ptr< const AttributeAccessor > MakeBooleanAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
Definition boolean.h:70
Ptr< const AttributeAccessor > MakeEnumAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
Definition enum.h:223
Ptr< const AttributeAccessor > MakePointerAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
Definition pointer.h:250
Ptr< AttributeChecker > MakePointerChecker()
Create a PointerChecker for a type.
Definition pointer.h:273
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_ABORT_MSG(msg)
Unconditional abnormal program termination with a message.
Definition abort.h:38
#define NS_ABORT_MSG_IF(cond, msg)
Abnormal program termination if a condition is true, with a message.
Definition abort.h:97
#define NS_LOG_ERROR(msg)
Use NS_LOG to output a message of level LOG_ERROR.
Definition log.h:246
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:194
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition log.h:260
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_LOG_WARN(msg)
Use NS_LOG to output a message of level LOG_WARN.
Definition log.h:253
@ TX_OPTION_ACK
TX_OPTION_ACK.
Definition lr-wpan-mac.h:54
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:627
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition object-base.h:35
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Ptr< const AttributeChecker > MakeEnumChecker(T v, std::string n, Ts... args)
Make an EnumChecker pre-configured with a set of allowed values by name.
Definition enum.h:181
AddressMode m_dstAddrMode
Destination address mode.
Mac16Address m_dstAddr
Destination address.
uint16_t m_dstPanId
Destination PAN identifier.
AddressMode m_srcAddrMode
Source address mode.
uint8_t m_txOptions
Tx Options (bitfield).