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_mac->Dispose();
95 m_phy->Dispose();
96 m_csmaca->Dispose();
97 m_phy = nullptr;
98 m_mac = nullptr;
99 m_csmaca = nullptr;
100 m_node = nullptr;
101 // chain up.
103}
104
105void
107{
108 NS_LOG_FUNCTION(this);
109 m_phy->Initialize();
110 m_mac->Initialize();
112}
113
114void
116{
117 NS_LOG_FUNCTION(this);
118 if (!m_mac || !m_phy || !m_csmaca || !m_node || m_configComplete)
119 {
120 return;
121 }
122 m_mac->SetPhy(m_phy);
123 m_mac->SetCsmaCa(m_csmaca);
124 m_mac->SetMcpsDataIndicationCallback(MakeCallback(&LrWpanNetDevice::McpsDataIndication, this));
125 m_csmaca->SetMac(m_mac);
126
128 m_phy->SetErrorModel(model);
129 m_phy->SetDevice(this);
130
131 m_phy->SetPdDataIndicationCallback(MakeCallback(&LrWpanMac::PdDataIndication, m_mac));
132 m_phy->SetPdDataConfirmCallback(MakeCallback(&LrWpanMac::PdDataConfirm, m_mac));
133 m_phy->SetPlmeEdConfirmCallback(MakeCallback(&LrWpanMac::PlmeEdConfirm, m_mac));
134 m_phy->SetPlmeGetAttributeConfirmCallback(
136 m_phy->SetPlmeSetTRXStateConfirmCallback(
138 m_phy->SetPlmeSetAttributeConfirmCallback(
140
141 m_csmaca->SetLrWpanMacStateCallback(MakeCallback(&LrWpanMac::SetLrWpanMacState, m_mac));
142 m_phy->SetPlmeCcaConfirmCallback(MakeCallback(&LrWpanCsmaCa::PlmeCcaConfirm, m_csmaca));
143 m_configComplete = true;
144}
145
146void
153
154void
161
162void
169
170void
172{
173 NS_LOG_FUNCTION(this << channel);
174 m_phy->SetChannel(channel);
175 channel->AddRx(m_phy);
177}
178
181{
182 // NS_LOG_FUNCTION (this);
183 return m_mac;
184}
185
188{
189 NS_LOG_FUNCTION(this);
190 return m_phy;
191}
192
195{
196 NS_LOG_FUNCTION(this);
197 return m_csmaca;
198}
199
200void
202{
203 NS_LOG_FUNCTION(this << index);
204 m_ifIndex = index;
205}
206
209{
210 NS_LOG_FUNCTION(this);
211 return m_ifIndex;
212}
213
216{
217 NS_LOG_FUNCTION(this);
218 return m_phy->GetChannel();
219}
220
221void
223{
224 NS_LOG_FUNCTION(this);
225 m_linkUp = true;
227}
228
229void
231{
232 NS_LOG_FUNCTION(this);
233 m_linkUp = false;
235}
236
239{
240 NS_LOG_FUNCTION(this);
241 return m_phy->GetChannel();
242}
243
244void
246{
247 NS_LOG_FUNCTION(this);
248 if (Mac16Address::IsMatchingType(address))
249 {
250 m_mac->SetShortAddress(Mac16Address::ConvertFrom(address));
251 }
252 else if (Mac64Address::IsMatchingType(address))
253 {
254 m_mac->SetExtendedAddress(Mac64Address::ConvertFrom(address));
255 }
256 else if (Mac48Address::IsMatchingType(address))
257 {
258 uint8_t buf[6];
260 addr.CopyTo(buf);
261 Mac16Address addr16;
262 addr16.CopyFrom(buf + 4);
263 m_mac->SetShortAddress(addr16);
264 uint16_t panId;
265 panId = buf[0];
266 panId <<= 8;
267 panId |= buf[1];
268 m_mac->SetPanId(panId);
269 }
270 else
271 {
272 NS_ABORT_MSG("LrWpanNetDevice::SetAddress - address is not of a compatible type");
273 }
274}
275
278{
279 NS_LOG_FUNCTION(this);
280
281 if (m_mac->GetShortAddress() == Mac16Address("00:00"))
282 {
283 return m_mac->GetExtendedAddress();
284 }
285
286 Mac48Address pseudoAddress = BuildPseudoMacAddress(m_mac->GetPanId(), m_mac->GetShortAddress());
287
288 return pseudoAddress;
289}
290
291void
293 Mac64Address coordExtAddr,
294 Mac16Address coordShortAddr,
295 Mac16Address assignedShortAddr)
296{
297 NS_LOG_FUNCTION(this);
298 m_mac->SetPanId(panId);
299 m_mac->SetAssociatedCoor(coordExtAddr);
300 m_mac->SetAssociatedCoor(coordShortAddr);
301 m_mac->SetShortAddress(assignedShortAddr);
302}
303
304bool
305LrWpanNetDevice::SetMtu(const uint16_t mtu)
306{
307 NS_ABORT_MSG("Unsupported");
308 return false;
309}
310
311uint16_t
313{
314 NS_LOG_FUNCTION(this);
315 // Maximum payload size is: max psdu - frame control - seqno - addressing - security - fcs
316 // = 127 - 2 - 1 - (2+2+2+2) - 0 - 2
317 // = 114
318 // assuming no security and addressing with only 16 bit addresses without pan id compression.
319 return 114;
320}
321
322bool
324{
325 NS_LOG_FUNCTION(this);
326 return m_phy && m_linkUp;
327}
328
329void
335
336bool
338{
339 NS_LOG_FUNCTION(this);
340 return true;
341}
342
345{
346 NS_LOG_FUNCTION(this);
347
348 Mac48Address pseudoAddress =
350
351 return pseudoAddress;
352}
353
354bool
356{
357 NS_LOG_FUNCTION(this);
358 return true;
359}
360
363{
364 NS_ABORT_MSG("Unsupported");
365 return Address();
366}
367
370{
371 NS_LOG_FUNCTION(this << addr);
372
373 Mac48Address pseudoAddress =
375
376 return pseudoAddress;
377}
378
379bool
381{
382 NS_LOG_FUNCTION(this);
383 return false;
384}
385
386bool
388{
389 NS_LOG_FUNCTION(this);
390 return false;
391}
392
393bool
394LrWpanNetDevice::Send(Ptr<Packet> packet, const Address& dest, uint16_t protocolNumber)
395{
396 // This method basically assumes an 802.3-compliant device, but a raw
397 // 802.15.4 device does not have an ethertype, and requires specific
398 // McpsDataRequest parameters.
399 // For further study: how to support these methods somehow, such as
400 // inventing a fake ethertype and packet tag for McpsDataRequest
401 NS_LOG_FUNCTION(this << packet << dest << protocolNumber);
402
403 if (packet->GetSize() > GetMtu())
404 {
405 NS_LOG_ERROR("Fragmentation is needed for this packet, drop the packet ");
406 return false;
407 }
408
409 McpsDataRequestParams m_mcpsDataRequestParams;
410
411 Mac16Address dst16;
413 {
414 uint8_t buf[6];
415 dest.CopyTo(buf);
416 dst16.CopyFrom(buf + 4);
417 }
418 else
419 {
420 dst16 = Mac16Address::ConvertFrom(dest);
421 }
422 m_mcpsDataRequestParams.m_dstAddr = dst16;
423 m_mcpsDataRequestParams.m_dstAddrMode = SHORT_ADDR;
424 m_mcpsDataRequestParams.m_dstPanId = m_mac->GetPanId();
425 m_mcpsDataRequestParams.m_srcAddrMode = SHORT_ADDR;
426 // Using ACK requests for broadcast destinations is ok here. They are disabled
427 // by the MAC.
428 if (m_useAcks)
429 {
430 m_mcpsDataRequestParams.m_txOptions = TX_OPTION_ACK;
431 }
432 m_mcpsDataRequestParams.m_msduHandle = 0;
433 m_mac->McpsDataRequest(m_mcpsDataRequestParams, packet);
434 return true;
435}
436
437bool
439 const Address& source,
440 const Address& dest,
441 uint16_t protocolNumber)
442{
443 NS_ABORT_MSG("Unsupported");
444 // TODO: To support SendFrom, the MACs McpsDataRequest has to use the provided source address,
445 // instead of to local one.
446 return false;
447}
448
451{
452 NS_LOG_FUNCTION(this);
453 return m_node;
454}
455
456void
458{
459 NS_LOG_FUNCTION(this);
460 m_node = node;
462}
463
464bool
466{
467 NS_LOG_FUNCTION(this);
468 return true;
469}
470
471void
477
478void
480{
481 // This method basically assumes an 802.3-compliant device, but a raw
482 // 802.15.4 device does not have an ethertype, and requires specific
483 // McpsDataIndication parameters.
484 // For further study: how to support these methods somehow, such as
485 // inventing a fake ethertype and packet tag for McpsDataRequest
486 NS_LOG_WARN("Unsupported; use LrWpan MAC APIs instead");
487}
488
489void
491{
492 NS_LOG_FUNCTION(this);
493 // TODO: Use the PromiscReceiveCallback if the MAC is in promiscuous mode.
494
495 if (params.m_dstAddrMode == SHORT_ADDR)
496 {
497 m_receiveCallback(this, pkt, 0, BuildPseudoMacAddress(params.m_srcPanId, params.m_srcAddr));
498 }
499 else
500 {
501 m_receiveCallback(this, pkt, 0, params.m_srcExtAddr);
502 }
503}
504
505bool
507{
509 return false;
510}
511
514{
515 NS_LOG_FUNCTION(this);
516
517 uint8_t buf[6];
518
520 {
521 buf[0] = panId >> 8;
522 // Make sure the U/L bit is set
523 buf[0] |= 0x02;
524 buf[1] = panId & 0xff;
525 }
526 else
527 {
528 // Make sure the U/L bit is set
529 buf[0] = 0x02;
530 buf[1] = 0x00;
531 }
532 buf[2] = 0;
533 buf[3] = 0;
534 shortAddr.CopyTo(buf + 4);
535
536 Mac48Address pseudoAddress;
537 pseudoAddress.CopyFrom(buf);
538
539 return pseudoAddress;
540}
541
542int64_t
544{
545 NS_LOG_FUNCTION(stream);
546 int64_t streamIndex = stream;
547 streamIndex += m_csmaca->AssignStreams(stream);
548 streamIndex += m_phy->AssignStreams(stream);
549 NS_LOG_DEBUG("Number of assigned RV streams: " << (streamIndex - stream));
550 return (streamIndex - stream);
551}
552
553} // namespace lrwpan
554} // 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
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
virtual void DoDispose()
Destructor implementation.
Definition object.cc:433
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:48
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 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_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.
Ptr< const AttributeChecker > MakeBooleanChecker()
Definition boolean.cc:113
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
Ptr< const AttributeAccessor > MakeBooleanAccessor(T1 a1)
Definition boolean.h:70
Ptr< const AttributeAccessor > MakeEnumAccessor(T1 a1)
Definition enum.h:221
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)