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");
31NS_OBJECT_ENSURE_REGISTERED(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
84
89
90void
92{
93 NS_LOG_FUNCTION(this);
94 m_phy->Dispose();
95 m_csmaca->Dispose();
96 m_phy = nullptr;
97 m_mac = nullptr;
98 m_csmaca = nullptr;
99 m_node = nullptr;
100 // chain up.
102}
103
104void
106{
107 NS_LOG_FUNCTION(this);
108
109 m_phy->Initialize();
112
114}
115
116void
118{
119 if (!m_mac || !m_phy || !m_csmaca || !m_node || m_configComplete)
120 {
121 return;
122 }
123
124 NS_LOG_FUNCTION(this);
125 m_mac->SetPhy(m_phy);
126 m_mac->SetCsmaCa(m_csmaca);
127 m_mac->SetMcpsDataIndicationCallback(MakeCallback(&LrWpanNetDevice::McpsDataIndication, this));
128 m_csmaca->SetMac(m_mac);
129
131 m_phy->SetErrorModel(model);
132 m_phy->SetDevice(this);
133
134 m_phy->SetPdDataIndicationCallback(MakeCallback(&LrWpanMac::PdDataIndication, m_mac));
135 m_phy->SetPdDataConfirmCallback(MakeCallback(&LrWpanMac::PdDataConfirm, m_mac));
136 m_phy->SetPlmeEdConfirmCallback(MakeCallback(&LrWpanMac::PlmeEdConfirm, m_mac));
137 m_phy->SetPlmeGetAttributeConfirmCallback(
139 m_phy->SetPlmeSetTRXStateConfirmCallback(
141 m_phy->SetPlmeSetAttributeConfirmCallback(
143
144 m_csmaca->SetLrWpanMacStateCallback(MakeCallback(&LrWpanMac::SetLrWpanMacState, m_mac));
145 m_phy->SetPlmeCcaConfirmCallback(MakeCallback(&LrWpanCsmaCa::PlmeCcaConfirm, m_csmaca));
146 m_configComplete = true;
147}
148
149void
151{
153 "MAC layer cannot be set after initialization");
154 m_mac = mac;
155}
156
157void
159{
161 "PHY layer cannot be set after initialization");
162 m_phy = phy;
163}
164
165void
167{
168 NS_ABORT_MSG_IF(LrWpanNetDevice::IsInitialized(), "CSMA/CA cannot be set after initialization");
169 m_csmaca = csmaca;
170}
171
172void
174{
176 "Spectrum channel cannot be set after initialization");
177 m_phy->SetChannel(channel);
178 channel->AddRx(m_phy);
179}
180
183{
184 return m_mac;
185}
186
189{
190 return m_phy;
191}
192
195{
196 return m_csmaca;
197}
198
199void
201{
202 m_ifIndex = index;
203}
204
207{
208 return m_ifIndex;
209}
210
213{
214 return m_phy->GetChannel();
215}
216
217void
219{
220 NS_LOG_FUNCTION(this);
221 m_linkUp = true;
223}
224
225void
227{
228 NS_LOG_FUNCTION(this);
229 m_linkUp = false;
231}
232
235{
236 NS_LOG_FUNCTION(this);
237 return m_phy->GetChannel();
238}
239
240void
242{
243 NS_LOG_FUNCTION(this);
244 if (Mac16Address::IsMatchingType(address))
245 {
246 m_mac->SetShortAddress(Mac16Address::ConvertFrom(address));
247 }
248 else if (Mac64Address::IsMatchingType(address))
249 {
250 m_mac->SetExtendedAddress(Mac64Address::ConvertFrom(address));
251 }
252 else if (Mac48Address::IsMatchingType(address))
253 {
254 uint8_t buf[6];
256 addr.CopyTo(buf);
257 Mac16Address addr16;
258 addr16.CopyFrom(buf + 4);
259 m_mac->SetShortAddress(addr16);
260 uint16_t panId;
261 panId = buf[0];
262 panId <<= 8;
263 panId |= buf[1];
264 m_mac->SetPanId(panId);
265 }
266 else
267 {
268 NS_ABORT_MSG("LrWpanNetDevice::SetAddress - address is not of a compatible type");
269 }
270}
271
274{
275 NS_LOG_FUNCTION(this);
276
277 if (m_mac->GetShortAddress() == Mac16Address("00:00"))
278 {
279 return m_mac->GetExtendedAddress();
280 }
281
282 Mac48Address pseudoAddress = BuildPseudoMacAddress(m_mac->GetPanId(), m_mac->GetShortAddress());
283
284 return pseudoAddress;
285}
286
287void
289 Mac64Address coordExtAddr,
290 Mac16Address coordShortAddr,
291 Mac16Address assignedShortAddr)
292{
293 NS_LOG_FUNCTION(this);
294 m_mac->SetPanId(panId);
295 m_mac->SetAssociatedCoor(coordExtAddr);
296 m_mac->SetAssociatedCoor(coordShortAddr);
297 m_mac->SetShortAddress(assignedShortAddr);
298}
299
300bool
301LrWpanNetDevice::SetMtu(const uint16_t mtu)
302{
303 NS_ABORT_MSG("Unsupported");
304 return false;
305}
306
307uint16_t
309{
310 NS_LOG_FUNCTION(this);
311 // Maximum payload size is: max psdu - frame control - seqno - addressing - security - fcs
312 // = 127 - 2 - 1 - (2+2+2+2) - 0 - 2
313 // = 114
314 // assuming no security and addressing with only 16 bit addresses without pan id compression.
315 return 114;
316}
317
318bool
320{
321 NS_LOG_FUNCTION(this);
322 return m_phy && m_linkUp;
323}
324
325void
331
332bool
334{
335 NS_LOG_FUNCTION(this);
336 return true;
337}
338
341{
342 NS_LOG_FUNCTION(this);
343
344 Mac48Address pseudoAddress =
346
347 return pseudoAddress;
348}
349
350bool
352{
353 NS_LOG_FUNCTION(this);
354 return true;
355}
356
359{
360 NS_ABORT_MSG("Unsupported");
361 return Address();
362}
363
366{
367 NS_LOG_FUNCTION(this << addr);
368
369 Mac48Address pseudoAddress =
371
372 return pseudoAddress;
373}
374
375bool
377{
378 NS_LOG_FUNCTION(this);
379 return false;
380}
381
382bool
384{
385 NS_LOG_FUNCTION(this);
386 return false;
387}
388
389bool
390LrWpanNetDevice::Send(Ptr<Packet> packet, const Address& dest, uint16_t protocolNumber)
391{
392 // This method basically assumes an 802.3-compliant device, but a raw
393 // 802.15.4 device does not have an ethertype, and requires specific
394 // McpsDataRequest parameters.
395 // For further study: how to support these methods somehow, such as
396 // inventing a fake ethertype and packet tag for McpsDataRequest
397 NS_LOG_FUNCTION(this << packet << dest << protocolNumber);
398
399 if (packet->GetSize() > GetMtu())
400 {
401 NS_LOG_ERROR("Fragmentation is needed for this packet, drop the packet ");
402 return false;
403 }
404
405 McpsDataRequestParams m_mcpsDataRequestParams;
406
407 Mac16Address dst16;
409 {
410 uint8_t buf[6];
411 dest.CopyTo(buf);
412 dst16.CopyFrom(buf + 4);
413 }
414 else
415 {
416 dst16 = Mac16Address::ConvertFrom(dest);
417 }
418 m_mcpsDataRequestParams.m_dstAddr = dst16;
419 m_mcpsDataRequestParams.m_dstAddrMode = SHORT_ADDR;
420 m_mcpsDataRequestParams.m_dstPanId = m_mac->GetPanId();
421 m_mcpsDataRequestParams.m_srcAddrMode = SHORT_ADDR;
422 // Using ACK requests for broadcast destinations is ok here. They are disabled
423 // by the MAC.
424 if (m_useAcks)
425 {
426 m_mcpsDataRequestParams.m_txOptions = TX_OPTION_ACK;
427 }
428 m_mcpsDataRequestParams.m_msduHandle = 0;
429 m_mac->McpsDataRequest(m_mcpsDataRequestParams, packet);
430 return true;
431}
432
433bool
435 const Address& source,
436 const Address& dest,
437 uint16_t protocolNumber)
438{
439 NS_ABORT_MSG("Unsupported");
440 // TODO: To support SendFrom, the MACs McpsDataRequest has to use the provided source address,
441 // instead of to local one.
442 return false;
443}
444
447{
448 return m_node;
449}
450
451void
453{
454 m_node = node;
456}
457
458bool
460{
461 NS_LOG_FUNCTION(this);
462 return true;
463}
464
465void
471
472void
474{
475 // This method basically assumes an 802.3-compliant device, but a raw
476 // 802.15.4 device does not have an ethertype, and requires specific
477 // McpsDataIndication parameters.
478 // For further study: how to support these methods somehow, such as
479 // inventing a fake ethertype and packet tag for McpsDataRequest
480 NS_LOG_WARN("Unsupported; use LrWpan MAC APIs instead");
481}
482
483void
485{
486 NS_LOG_FUNCTION(this);
487 // TODO: Use the PromiscReceiveCallback if the MAC is in promiscuous mode.
488
489 if (params.m_dstAddrMode == SHORT_ADDR)
490 {
491 m_receiveCallback(this, pkt, 0, BuildPseudoMacAddress(params.m_srcPanId, params.m_srcAddr));
492 }
493 else
494 {
495 m_receiveCallback(this, pkt, 0, params.m_srcExtAddr);
496 }
497}
498
499bool
501{
503 return false;
504}
505
508{
509 NS_LOG_FUNCTION(this);
510
511 uint8_t buf[6];
512
514 {
515 buf[0] = panId >> 8;
516 // Make sure the U/L bit is set
517 buf[0] |= 0x02;
518 buf[1] = panId & 0xff;
519 }
520 else
521 {
522 // Make sure the U/L bit is set
523 buf[0] = 0x02;
524 buf[1] = 0x00;
525 }
526 buf[2] = 0;
527 buf[3] = 0;
528 shortAddr.CopyTo(buf + 4);
529
530 Mac48Address pseudoAddress;
531 pseudoAddress.CopyFrom(buf);
532
533 return pseudoAddress;
534}
535
536int64_t
538{
539 NS_LOG_FUNCTION(stream);
540 int64_t streamIndex = stream;
541 streamIndex += m_csmaca->AssignStreams(stream);
542 streamIndex += m_phy->AssignStreams(stream);
543 NS_LOG_DEBUG("Number of assigned RV streams: " << (streamIndex - stream));
544 return (streamIndex - stream);
545}
546
547} // namespace lrwpan
548} // namespace ns3
a polymophic address class
Definition address.h:90
uint32_t CopyTo(uint8_t buffer[MAX_SIZE]) const
Copy the address bytes into a buffer.
Definition address.cc:75
AttributeValue implementation for Boolean.
Definition boolean.h:26
Callback template class.
Definition callback.h:422
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
virtual void DoInitialize()
Initialize() implementation.
Definition object.cc:440
void AggregateObject(Ptr< Object > other)
Aggregate two Objects together.
Definition object.cc:298
virtual void DoDispose()
Destructor implementation.
Definition object.cc:433
bool IsInitialized() const
Check if the object has been initialized.
Definition object.cc:240
AttributeValue implementation for Pointer.
Smart pointer class similar to boost::intrusive_ptr.
void ConnectWithoutContext(const CallbackBase &callback)
Append a Callback to the chain (without a context).
a unique identifier for an interface.
Definition type-id.h:49
TypeId AddDeprecatedName(const std::string &name)
Add an deprecated name for a TypeId.
Definition type-id.cc:862
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition type-id.cc:1001
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:221
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:248
Ptr< AttributeChecker > MakePointerChecker()
Create a PointerChecker for a type.
Definition pointer.h:269
#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:243
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition log.h:257
#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:250
@ TX_OPTION_ACK
TX_OPTION_ACK.
Definition lr-wpan-mac.h:53
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:619
#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.
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:684
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:179
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)