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 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * Author:
18 * Tom Henderson <thomas.r.henderson@boeing.com>
19 * Tommaso Pecorella <tommaso.pecorella@unifi.it>
20 * Margherita Filippetti <morag87@gmail.com>
21 */
22#include "lr-wpan-net-device.h"
23
24#include "lr-wpan-csmaca.h"
25#include "lr-wpan-error-model.h"
26#include "lr-wpan-phy.h"
27
28#include <ns3/abort.h>
29#include <ns3/boolean.h>
30#include <ns3/log.h>
31#include <ns3/node.h>
32#include <ns3/packet.h>
33#include <ns3/pointer.h>
34#include <ns3/spectrum-channel.h>
35
36namespace ns3
37{
38
39NS_LOG_COMPONENT_DEFINE("LrWpanNetDevice");
40
41NS_OBJECT_ENSURE_REGISTERED(LrWpanNetDevice);
42
43TypeId
45{
46 static TypeId tid =
47 TypeId("ns3::LrWpanNetDevice")
49 .SetGroupName("LrWpan")
50 .AddConstructor<LrWpanNetDevice>()
51 .AddAttribute("Channel",
52 "The channel attached to this device",
55 MakePointerChecker<SpectrumChannel>())
56 .AddAttribute("Phy",
57 "The PHY layer attached to this device.",
60 MakePointerChecker<LrWpanPhy>())
61 .AddAttribute("Mac",
62 "The MAC layer attached to this device.",
65 MakePointerChecker<LrWpanMac>())
66 .AddAttribute("UseAcks",
67 "Request acknowledgments for data frames.",
68 BooleanValue(true),
71 .AddAttribute(
72 "PseudoMacAddressMode",
73 "Build the pseudo-MAC Address according to RFC 4944 or RFC 6282 "
74 "(default: RFC 6282).",
76 MakeEnumAccessor<PseudoMacAddressMode_e>(&LrWpanNetDevice::m_pseudoMacMode),
78 "RFC 6282 (don't use PanId)",
80 "RFC 4944 (use PanId)"));
81 return tid;
82}
83
85 : m_configComplete(false)
86{
87 NS_LOG_FUNCTION(this);
88 m_mac = CreateObject<LrWpanMac>();
89 m_phy = CreateObject<LrWpanPhy>();
90 m_csmaca = CreateObject<LrWpanCsmaCa>();
92}
93
95{
96 NS_LOG_FUNCTION(this);
97}
98
99void
101{
102 NS_LOG_FUNCTION(this);
103 m_mac->Dispose();
104 m_phy->Dispose();
105 m_csmaca->Dispose();
106 m_phy = nullptr;
107 m_mac = nullptr;
108 m_csmaca = nullptr;
109 m_node = nullptr;
110 // chain up.
112}
113
114void
116{
117 NS_LOG_FUNCTION(this);
118 m_phy->Initialize();
119 m_mac->Initialize();
121}
122
123void
125{
126 NS_LOG_FUNCTION(this);
127 if (!m_mac || !m_phy || !m_csmaca || !m_node || m_configComplete)
128 {
129 return;
130 }
131 m_mac->SetPhy(m_phy);
132 m_mac->SetCsmaCa(m_csmaca);
133 m_mac->SetMcpsDataIndicationCallback(MakeCallback(&LrWpanNetDevice::McpsDataIndication, this));
134 m_csmaca->SetMac(m_mac);
135
136 Ptr<LrWpanErrorModel> model = CreateObject<LrWpanErrorModel>();
137 m_phy->SetErrorModel(model);
138 m_phy->SetDevice(this);
139
140 m_phy->SetPdDataIndicationCallback(MakeCallback(&LrWpanMac::PdDataIndication, m_mac));
141 m_phy->SetPdDataConfirmCallback(MakeCallback(&LrWpanMac::PdDataConfirm, m_mac));
142 m_phy->SetPlmeEdConfirmCallback(MakeCallback(&LrWpanMac::PlmeEdConfirm, m_mac));
143 m_phy->SetPlmeGetAttributeConfirmCallback(
145 m_phy->SetPlmeSetTRXStateConfirmCallback(
147 m_phy->SetPlmeSetAttributeConfirmCallback(
149
150 m_csmaca->SetLrWpanMacStateCallback(MakeCallback(&LrWpanMac::SetLrWpanMacState, m_mac));
151 m_phy->SetPlmeCcaConfirmCallback(MakeCallback(&LrWpanCsmaCa::PlmeCcaConfirm, m_csmaca));
152 m_configComplete = true;
153}
154
155void
157{
158 NS_LOG_FUNCTION(this);
159 m_mac = mac;
161}
162
163void
165{
166 NS_LOG_FUNCTION(this);
167 m_phy = phy;
169}
170
171void
173{
174 NS_LOG_FUNCTION(this);
175 m_csmaca = csmaca;
177}
178
179void
181{
182 NS_LOG_FUNCTION(this << channel);
183 m_phy->SetChannel(channel);
184 channel->AddRx(m_phy);
186}
187
190{
191 // NS_LOG_FUNCTION (this);
192 return m_mac;
193}
194
197{
198 NS_LOG_FUNCTION(this);
199 return m_phy;
200}
201
204{
205 NS_LOG_FUNCTION(this);
206 return m_csmaca;
207}
208
209void
211{
212 NS_LOG_FUNCTION(this << index);
213 m_ifIndex = index;
214}
215
218{
219 NS_LOG_FUNCTION(this);
220 return m_ifIndex;
221}
222
225{
226 NS_LOG_FUNCTION(this);
227 return m_phy->GetChannel();
228}
229
230void
232{
233 NS_LOG_FUNCTION(this);
234 m_linkUp = true;
236}
237
238void
240{
241 NS_LOG_FUNCTION(this);
242 m_linkUp = false;
244}
245
248{
249 NS_LOG_FUNCTION(this);
250 return m_phy->GetChannel();
251}
252
253void
255{
256 NS_LOG_FUNCTION(this);
257 if (Mac16Address::IsMatchingType(address))
258 {
259 m_mac->SetShortAddress(Mac16Address::ConvertFrom(address));
260 }
261 else if (Mac64Address::IsMatchingType(address))
262 {
263 m_mac->SetExtendedAddress(Mac64Address::ConvertFrom(address));
264 }
265 else if (Mac48Address::IsMatchingType(address))
266 {
267 uint8_t buf[6];
269 addr.CopyTo(buf);
270 Mac16Address addr16;
271 addr16.CopyFrom(buf + 4);
272 m_mac->SetShortAddress(addr16);
273 uint16_t panId;
274 panId = buf[0];
275 panId <<= 8;
276 panId |= buf[1];
277 m_mac->SetPanId(panId);
278 }
279 else
280 {
281 NS_ABORT_MSG("LrWpanNetDevice::SetAddress - address is not of a compatible type");
282 }
283}
284
287{
288 NS_LOG_FUNCTION(this);
289
290 if (m_mac->GetShortAddress() == Mac16Address("00:00"))
291 {
292 return m_mac->GetExtendedAddress();
293 }
294
295 Mac48Address pseudoAddress = BuildPseudoMacAddress(m_mac->GetPanId(), m_mac->GetShortAddress());
296
297 return pseudoAddress;
298}
299
300void
302 Mac64Address coordExtAddr,
303 Mac16Address coordShortAddr,
304 Mac16Address assignedShortAddr)
305{
306 NS_LOG_FUNCTION(this);
307 m_mac->SetPanId(panId);
308 m_mac->SetAssociatedCoor(coordExtAddr);
309 m_mac->SetAssociatedCoor(coordShortAddr);
310 m_mac->SetShortAddress(assignedShortAddr);
311}
312
313bool
314LrWpanNetDevice::SetMtu(const uint16_t mtu)
315{
316 NS_ABORT_MSG("Unsupported");
317 return false;
318}
319
320uint16_t
322{
323 NS_LOG_FUNCTION(this);
324 // Maximum payload size is: max psdu - frame control - seqno - addressing - security - fcs
325 // = 127 - 2 - 1 - (2+2+2+2) - 0 - 2
326 // = 114
327 // assuming no security and addressing with only 16 bit addresses without pan id compression.
328 return 114;
329}
330
331bool
333{
334 NS_LOG_FUNCTION(this);
335 return m_phy && m_linkUp;
336}
337
338void
340{
341 NS_LOG_FUNCTION(this);
343}
344
345bool
347{
348 NS_LOG_FUNCTION(this);
349 return true;
350}
351
354{
355 NS_LOG_FUNCTION(this);
356
357 Mac48Address pseudoAddress =
359
360 return pseudoAddress;
361}
362
363bool
365{
366 NS_LOG_FUNCTION(this);
367 return true;
368}
369
372{
373 NS_ABORT_MSG("Unsupported");
374 return Address();
375}
376
379{
380 NS_LOG_FUNCTION(this << addr);
381
382 Mac48Address pseudoAddress =
384
385 return pseudoAddress;
386}
387
388bool
390{
391 NS_LOG_FUNCTION(this);
392 return false;
393}
394
395bool
397{
398 NS_LOG_FUNCTION(this);
399 return false;
400}
401
402bool
403LrWpanNetDevice::Send(Ptr<Packet> packet, const Address& dest, uint16_t protocolNumber)
404{
405 // This method basically assumes an 802.3-compliant device, but a raw
406 // 802.15.4 device does not have an ethertype, and requires specific
407 // McpsDataRequest parameters.
408 // For further study: how to support these methods somehow, such as
409 // inventing a fake ethertype and packet tag for McpsDataRequest
410 NS_LOG_FUNCTION(this << packet << dest << protocolNumber);
411
412 if (packet->GetSize() > GetMtu())
413 {
414 NS_LOG_ERROR("Fragmentation is needed for this packet, drop the packet ");
415 return false;
416 }
417
418 McpsDataRequestParams m_mcpsDataRequestParams;
419
420 Mac16Address dst16;
422 {
423 uint8_t buf[6];
424 dest.CopyTo(buf);
425 dst16.CopyFrom(buf + 4);
426 }
427 else
428 {
429 dst16 = Mac16Address::ConvertFrom(dest);
430 }
431 m_mcpsDataRequestParams.m_dstAddr = dst16;
432 m_mcpsDataRequestParams.m_dstAddrMode = SHORT_ADDR;
433 m_mcpsDataRequestParams.m_dstPanId = m_mac->GetPanId();
434 m_mcpsDataRequestParams.m_srcAddrMode = SHORT_ADDR;
435 // Using ACK requests for broadcast destinations is ok here. They are disabled
436 // by the MAC.
437 if (m_useAcks)
438 {
439 m_mcpsDataRequestParams.m_txOptions = TX_OPTION_ACK;
440 }
441 m_mcpsDataRequestParams.m_msduHandle = 0;
442 m_mac->McpsDataRequest(m_mcpsDataRequestParams, packet);
443 return true;
444}
445
446bool
448 const Address& source,
449 const Address& dest,
450 uint16_t protocolNumber)
451{
452 NS_ABORT_MSG("Unsupported");
453 // TODO: To support SendFrom, the MACs McpsDataRequest has to use the provided source address,
454 // instead of to local one.
455 return false;
456}
457
460{
461 NS_LOG_FUNCTION(this);
462 return m_node;
463}
464
465void
467{
468 NS_LOG_FUNCTION(this);
469 m_node = node;
471}
472
473bool
475{
476 NS_LOG_FUNCTION(this);
477 return true;
478}
479
480void
482{
483 NS_LOG_FUNCTION(this);
485}
486
487void
489{
490 // This method basically assumes an 802.3-compliant device, but a raw
491 // 802.15.4 device does not have an ethertype, and requires specific
492 // McpsDataIndication parameters.
493 // For further study: how to support these methods somehow, such as
494 // inventing a fake ethertype and packet tag for McpsDataRequest
495 NS_LOG_WARN("Unsupported; use LrWpan MAC APIs instead");
496}
497
498void
500{
501 NS_LOG_FUNCTION(this);
502 // TODO: Use the PromiscReceiveCallback if the MAC is in promiscuous mode.
503
504 if (params.m_dstAddrMode == SHORT_ADDR)
505 {
506 m_receiveCallback(this, pkt, 0, BuildPseudoMacAddress(params.m_srcPanId, params.m_srcAddr));
507 }
508 else
509 {
510 m_receiveCallback(this, pkt, 0, params.m_srcExtAddr);
511 }
512}
513
514bool
516{
518 return false;
519}
520
523{
524 NS_LOG_FUNCTION(this);
525
526 uint8_t buf[6];
527
529 {
530 buf[0] = panId >> 8;
531 // Make sure the U/L bit is set
532 buf[0] |= 0x02;
533 buf[1] = panId & 0xff;
534 }
535 else
536 {
537 // Make sure the U/L bit is set
538 buf[0] = 0x02;
539 buf[1] = 0x00;
540 }
541 buf[2] = 0;
542 buf[3] = 0;
543 shortAddr.CopyTo(buf + 4);
544
545 Mac48Address pseudoAddress;
546 pseudoAddress.CopyFrom(buf);
547
548 return pseudoAddress;
549}
550
551int64_t
553{
554 NS_LOG_FUNCTION(stream);
555 int64_t streamIndex = stream;
556 streamIndex += m_csmaca->AssignStreams(stream);
557 streamIndex += m_phy->AssignStreams(stream);
558 NS_LOG_DEBUG("Number of assigned RV streams: " << (streamIndex - stream));
559 return (streamIndex - stream);
560}
561
562} // namespace ns3
a polymophic address class
Definition: address.h:101
uint32_t CopyTo(uint8_t buffer[MAX_SIZE]) const
Copy the address bytes into a buffer.
Definition: address.cc:86
AttributeValue implementation for Boolean.
Definition: boolean.h:37
Callback template class.
Definition: callback.h:438
Hold variables of type enum.
Definition: enum.h:62
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:42
Describes an IPv6 address.
Definition: ipv6-address.h:49
void PlmeCcaConfirm(LrWpanPhyEnumeration status)
IEEE 802.15.4-2006 section 6.2.2.2 PLME-CCA.confirm status.
void PlmeSetTRXStateConfirm(LrWpanPhyEnumeration status)
IEEE 802.15.4-2006 section 6.2.2.8 PLME-SET-TRX-STATE.confirm Set PHY state.
void PlmeEdConfirm(LrWpanPhyEnumeration status, uint8_t energyLevel)
IEEE 802.15.4-2006 section 6.2.2.4 PLME-ED.confirm status and energy level.
void PlmeGetAttributeConfirm(LrWpanPhyEnumeration status, LrWpanPibAttributeIdentifier id, Ptr< LrWpanPhyPibAttributes > attribute)
IEEE 802.15.4-2006 section 6.2.2.6 PLME-GET.confirm Get attributes per definition from Table 23 in se...
void SetLrWpanMacState(LrWpanMacState 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 PlmeSetAttributeConfirm(LrWpanPhyEnumeration status, LrWpanPibAttributeIdentifier id)
IEEE 802.15.4-2006 section 6.2.2.10 PLME-SET.confirm Set attributes per definition from Table 23 in s...
void PdDataConfirm(LrWpanPhyEnumeration status)
IEEE 802.15.4-2006 section 6.2.1.2 Confirm the end of transmission of an MPDU to MAC.
Network layer to device interface.
void SetReceiveCallback(NetDevice::ReceiveCallback cb) override
void SetPromiscReceiveCallback(PromiscReceiveCallback cb) override
bool Send(Ptr< Packet > packet, const Address &dest, uint16_t protocolNumber) override
static TypeId GetTypeId()
Get the type ID.
PseudoMacAddressMode_e m_pseudoMacMode
How the pseudo MAC address is created.
@ RFC4944
YYYY:0000:XXXX (with U/L bit set to local)
@ RFC6282
0200:0000:XXXX
Ptr< LrWpanMac > m_mac
The MAC for this NetDevice.
Address GetAddress() const override
This method indirects to LrWpanMac::SetShortAddress ()
Ptr< Node > GetNode() const override
bool m_useAcks
Configure the NetDevice to request MAC layer acknowledgments when sending packets using the Send() AP...
void SetCsmaCa(Ptr< LrWpanCsmaCa > csmaca)
Set the CSMA/CA implementation to be used by the MAC and this NetDevice.
void SetChannel(Ptr< SpectrumChannel > channel)
Set the channel to which the NetDevice, and therefore the PHY, should be attached to.
Ptr< SpectrumChannel > DoGetChannel() const
Attribute accessor method for the "Channel" attribute.
Ptr< LrWpanMac > GetMac() const
Get the MAC used by this NetDevice.
bool SupportsSendFrom() const override
Address GetMulticast(Ipv4Address multicastGroup) const override
Make and return a MAC multicast address using the provided multicast group.
void AddLinkChangeCallback(Callback< void > callback) override
void SetNode(Ptr< Node > node) override
Ptr< Node > m_node
The node associated with this NetDevice.
void SetPhy(Ptr< LrWpanPhy > phy)
Set the PHY to be used by the MAC and this NetDevice.
int64_t AssignStreams(int64_t stream)
Assign a fixed random variable stream number to the random variables used by this model.
bool IsBroadcast() const override
bool SetMtu(const uint16_t mtu) override
uint16_t GetMtu() const override
bool IsMulticast() const override
void McpsDataIndication(McpsDataIndicationParams params, Ptr< Packet > pkt)
The callback used by the MAC to hand over incoming packets to the NetDevice.
ReceiveCallback m_receiveCallback
Upper layer callback used for notification of new data packet arrivals.
bool m_linkUp
Is the link/device currently up and running?
Ptr< LrWpanPhy > GetPhy() const
Get the PHY used by this NetDevice.
uint32_t GetIfIndex() const override
bool IsLinkUp() const override
Ptr< LrWpanPhy > m_phy
The PHY for this NetDevice.
void SetAddress(Address address) override
This method indirects to LrWpanMac::SetShortAddress ()
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...
Ptr< LrWpanCsmaCa > GetCsmaCa() const
Get the CSMA/CA implementation used by this NetDevice.
Address GetBroadcast() const override
void SetMac(Ptr< LrWpanMac > mac)
Set the MAC to be used by this NetDevice.
bool IsBridge() const override
Return true if the net device is acting as a bridge.
void SetIfIndex(const uint32_t index) override
bool IsPointToPoint() const override
Return true if the net device is on a point-to-point link.
bool m_configComplete
True if MAC, PHY and CSMA/CA where successfully configured and the NetDevice is ready for being used.
bool NeedsArp() const override
bool SendFrom(Ptr< Packet > packet, const Address &source, const Address &dest, uint16_t protocolNumber) 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 > m_csmaca
The CSMA/CA implementation for this NetDevice.
TracedCallback m_linkChanges
Trace source for link up/down changes.
void CompleteConfig()
Configure PHY, MAC and CSMA/CA.
void DoDispose() override
Destructor implementation.
void LinkUp()
Mark NetDevice link as up.
Ptr< Channel > GetChannel() const override
void DoInitialize() override
Initialize() implementation.
void LinkDown()
Mark NetDevice link as down.
uint32_t m_ifIndex
The interface index of this NetDevice.
This class can contain 16 bit addresses.
Definition: mac16-address.h:44
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
Definition: mac48-address.h:46
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
Definition: mac64-address.h:46
static bool IsMatchingType(const Address &address)
static Mac64Address ConvertFrom(const Address &address)
Network layer to device interface.
Definition: net-device.h:98
virtual void DoInitialize()
Initialize() implementation.
Definition: object.cc:451
virtual void DoDispose()
Destructor implementation.
Definition: object.cc:444
AttributeValue implementation for Pointer.
Definition: pointer.h:48
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
void ConnectWithoutContext(const CallbackBase &callback)
Append a Callback to the chain (without a context).
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:932
Ptr< const AttributeAccessor > MakeBooleanAccessor(T1 a1)
Definition: boolean.h:81
Ptr< const AttributeChecker > MakeBooleanChecker()
Definition: boolean.cc:124
Ptr< const AttributeAccessor > MakePointerAccessor(T1 a1)
Definition: pointer.h:259
#define NS_ABORT_MSG(msg)
Unconditional abnormal program termination with a message.
Definition: abort.h:49
#define NS_LOG_ERROR(msg)
Use NS_LOG to output a message of level LOG_ERROR.
Definition: log.h:254
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:268
#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:261
@ TX_OPTION_ACK
TX_OPTION_ACK.
Definition: lr-wpan-mac.h:61
@ SHORT_ADDR
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:46
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:704
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:189
MCPS-DATA.indication params.
MCPS-DATA.request params.
LrWpanAddressMode m_srcAddrMode
Source address mode.
LrWpanAddressMode m_dstAddrMode
Destination address mode.
uint16_t m_dstPanId
Destination PAN identifier.
Mac16Address m_dstAddr
Destination address.
uint8_t m_msduHandle
MSDU handle.
uint8_t m_txOptions
Tx Options (bitfield)