A Discrete-Event Network Simulator
API
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("PseudoMacAddressMode",
72 "Build the pseudo-MAC Address according to RFC 4944 or RFC 6282 "
73 "(default: RFC 6282).",
77 "RFC 6282 (don't use PanId)",
79 "RFC 4944 (use PanId)"));
80 return tid;
81}
82
84 : m_configComplete(false)
85{
86 NS_LOG_FUNCTION(this);
87 m_mac = CreateObject<LrWpanMac>();
88 m_phy = CreateObject<LrWpanPhy>();
89 m_csmaca = CreateObject<LrWpanCsmaCa>();
91}
92
94{
95 NS_LOG_FUNCTION(this);
96}
97
98void
100{
101 NS_LOG_FUNCTION(this);
102 m_mac->Dispose();
103 m_phy->Dispose();
104 m_csmaca->Dispose();
105 m_phy = nullptr;
106 m_mac = nullptr;
107 m_csmaca = nullptr;
108 m_node = nullptr;
109 // chain up.
111}
112
113void
115{
116 NS_LOG_FUNCTION(this);
117 m_phy->Initialize();
118 m_mac->Initialize();
120}
121
122void
124{
125 NS_LOG_FUNCTION(this);
126 if (!m_mac || !m_phy || !m_csmaca || !m_node || m_configComplete)
127 {
128 return;
129 }
130 m_mac->SetPhy(m_phy);
131 m_mac->SetCsmaCa(m_csmaca);
132 m_mac->SetMcpsDataIndicationCallback(MakeCallback(&LrWpanNetDevice::McpsDataIndication, this));
133 m_csmaca->SetMac(m_mac);
134
135 Ptr<LrWpanErrorModel> model = CreateObject<LrWpanErrorModel>();
136 m_phy->SetErrorModel(model);
137 m_phy->SetDevice(this);
138
139 m_phy->SetPdDataIndicationCallback(MakeCallback(&LrWpanMac::PdDataIndication, m_mac));
140 m_phy->SetPdDataConfirmCallback(MakeCallback(&LrWpanMac::PdDataConfirm, m_mac));
141 m_phy->SetPlmeEdConfirmCallback(MakeCallback(&LrWpanMac::PlmeEdConfirm, m_mac));
142 m_phy->SetPlmeGetAttributeConfirmCallback(
144 m_phy->SetPlmeSetTRXStateConfirmCallback(
146 m_phy->SetPlmeSetAttributeConfirmCallback(
148
149 m_csmaca->SetLrWpanMacStateCallback(MakeCallback(&LrWpanMac::SetLrWpanMacState, m_mac));
150 m_phy->SetPlmeCcaConfirmCallback(MakeCallback(&LrWpanCsmaCa::PlmeCcaConfirm, m_csmaca));
151 m_configComplete = true;
152}
153
154void
156{
157 NS_LOG_FUNCTION(this);
158 m_mac = mac;
160}
161
162void
164{
165 NS_LOG_FUNCTION(this);
166 m_phy = phy;
168}
169
170void
172{
173 NS_LOG_FUNCTION(this);
174 m_csmaca = csmaca;
176}
177
178void
180{
181 NS_LOG_FUNCTION(this << channel);
182 m_phy->SetChannel(channel);
183 channel->AddRx(m_phy);
185}
186
189{
190 // NS_LOG_FUNCTION (this);
191 return m_mac;
192}
193
196{
197 NS_LOG_FUNCTION(this);
198 return m_phy;
199}
200
203{
204 NS_LOG_FUNCTION(this);
205 return m_csmaca;
206}
207
208void
210{
211 NS_LOG_FUNCTION(this << index);
212 m_ifIndex = index;
213}
214
217{
218 NS_LOG_FUNCTION(this);
219 return m_ifIndex;
220}
221
224{
225 NS_LOG_FUNCTION(this);
226 return m_phy->GetChannel();
227}
228
229void
231{
232 NS_LOG_FUNCTION(this);
233 m_linkUp = true;
235}
236
237void
239{
240 NS_LOG_FUNCTION(this);
241 m_linkUp = false;
243}
244
247{
248 NS_LOG_FUNCTION(this);
249 return m_phy->GetChannel();
250}
251
252void
254{
255 NS_LOG_FUNCTION(this);
257 {
258 m_mac->SetShortAddress(Mac16Address::ConvertFrom(address));
259 }
261 {
262 uint8_t buf[6];
264 addr.CopyTo(buf);
265 Mac16Address addr16;
266 addr16.CopyFrom(buf + 4);
267 m_mac->SetShortAddress(addr16);
268 uint16_t panId;
269 panId = buf[0];
270 panId <<= 8;
271 panId |= buf[1];
272 m_mac->SetPanId(panId);
273 }
274 else
275 {
276 NS_ABORT_MSG("LrWpanNetDevice::SetAddress - address is not of a compatible type");
277 }
278}
279
282{
283 NS_LOG_FUNCTION(this);
284
285 if (m_mac->GetShortAddress() == Mac16Address("00:00"))
286 {
287 return m_mac->GetExtendedAddress();
288 }
289
290 Mac48Address pseudoAddress = BuildPseudoMacAddress(m_mac->GetPanId(), m_mac->GetShortAddress());
291
292 return pseudoAddress;
293}
294
295bool
296LrWpanNetDevice::SetMtu(const uint16_t mtu)
297{
298 NS_ABORT_MSG("Unsupported");
299 return false;
300}
301
302uint16_t
304{
305 NS_LOG_FUNCTION(this);
306 // Maximum payload size is: max psdu - frame control - seqno - addressing - security - fcs
307 // = 127 - 2 - 1 - (2+2+2+2) - 0 - 2
308 // = 114
309 // assuming no security and addressing with only 16 bit addresses without pan id compression.
310 return 114;
311}
312
313bool
315{
316 NS_LOG_FUNCTION(this);
317 return m_phy && m_linkUp;
318}
319
320void
322{
323 NS_LOG_FUNCTION(this);
325}
326
327bool
329{
330 NS_LOG_FUNCTION(this);
331 return true;
332}
333
336{
337 NS_LOG_FUNCTION(this);
338
339 Mac48Address pseudoAddress =
341
342 return pseudoAddress;
343}
344
345bool
347{
348 NS_LOG_FUNCTION(this);
349 return true;
350}
351
354{
355 NS_ABORT_MSG("Unsupported");
356 return Address();
357}
358
361{
362 NS_LOG_FUNCTION(this << addr);
363
364 Mac48Address pseudoAddress =
366
367 return pseudoAddress;
368}
369
370bool
372{
373 NS_LOG_FUNCTION(this);
374 return false;
375}
376
377bool
379{
380 NS_LOG_FUNCTION(this);
381 return false;
382}
383
384bool
385LrWpanNetDevice::Send(Ptr<Packet> packet, const Address& dest, uint16_t protocolNumber)
386{
387 // This method basically assumes an 802.3-compliant device, but a raw
388 // 802.15.4 device does not have an ethertype, and requires specific
389 // McpsDataRequest parameters.
390 // For further study: how to support these methods somehow, such as
391 // inventing a fake ethertype and packet tag for McpsDataRequest
392 NS_LOG_FUNCTION(this << packet << dest << protocolNumber);
393
394 if (packet->GetSize() > GetMtu())
395 {
396 NS_LOG_ERROR("Fragmentation is needed for this packet, drop the packet ");
397 return false;
398 }
399
400 McpsDataRequestParams m_mcpsDataRequestParams;
401
402 Mac16Address dst16;
404 {
405 uint8_t buf[6];
406 dest.CopyTo(buf);
407 dst16.CopyFrom(buf + 4);
408 }
409 else
410 {
411 dst16 = Mac16Address::ConvertFrom(dest);
412 }
413 m_mcpsDataRequestParams.m_dstAddr = dst16;
414 m_mcpsDataRequestParams.m_dstAddrMode = SHORT_ADDR;
415 m_mcpsDataRequestParams.m_dstPanId = m_mac->GetPanId();
416 m_mcpsDataRequestParams.m_srcAddrMode = SHORT_ADDR;
417 // Using ACK requests for broadcast destinations is ok here. They are disabled
418 // by the MAC.
419 if (m_useAcks)
420 {
421 m_mcpsDataRequestParams.m_txOptions = TX_OPTION_ACK;
422 }
423 m_mcpsDataRequestParams.m_msduHandle = 0;
424 m_mac->McpsDataRequest(m_mcpsDataRequestParams, packet);
425 return true;
426}
427
428bool
430 const Address& source,
431 const Address& dest,
432 uint16_t protocolNumber)
433{
434 NS_ABORT_MSG("Unsupported");
435 // TODO: To support SendFrom, the MACs McpsDataRequest has to use the provided source address,
436 // instead of to local one.
437 return false;
438}
439
442{
443 NS_LOG_FUNCTION(this);
444 return m_node;
445}
446
447void
449{
450 NS_LOG_FUNCTION(this);
451 m_node = node;
453}
454
455bool
457{
458 NS_LOG_FUNCTION(this);
459 return true;
460}
461
462void
464{
465 NS_LOG_FUNCTION(this);
467}
468
469void
471{
472 // This method basically assumes an 802.3-compliant device, but a raw
473 // 802.15.4 device does not have an ethertype, and requires specific
474 // McpsDataIndication parameters.
475 // For further study: how to support these methods somehow, such as
476 // inventing a fake ethertype and packet tag for McpsDataRequest
477 NS_LOG_WARN("Unsupported; use LrWpan MAC APIs instead");
478}
479
480void
482{
483 NS_LOG_FUNCTION(this);
484 // TODO: Use the PromiscReceiveCallback if the MAC is in promiscuous mode.
485
486 if (params.m_dstAddrMode == SHORT_ADDR)
487 {
488 m_receiveCallback(this, pkt, 0, BuildPseudoMacAddress(params.m_srcPanId, params.m_srcAddr));
489 }
490 else
491 {
492 m_receiveCallback(this, pkt, 0, params.m_srcExtAddr);
493 }
494}
495
496bool
498{
500 return false;
501}
502
505{
506 NS_LOG_FUNCTION(this);
507
508 uint8_t buf[6];
509
511 {
512 buf[0] = panId >> 8;
513 // Make sure the U/L bit is set
514 buf[0] |= 0x02;
515 buf[1] = panId & 0xff;
516 }
517 else
518 {
519 // Make sure the U/L bit is set
520 buf[0] = 0x02;
521 buf[1] = 0x00;
522 }
523 buf[2] = 0;
524 buf[3] = 0;
525 shortAddr.CopyTo(buf + 4);
526
527 Mac48Address pseudoAddress;
528 pseudoAddress.CopyFrom(buf);
529
530 return pseudoAddress;
531}
532
533int64_t
535{
536 NS_LOG_FUNCTION(stream);
537 int64_t streamIndex = stream;
538 streamIndex += m_csmaca->AssignStreams(stream);
539 streamIndex += m_phy->AssignStreams(stream);
540 NS_LOG_DEBUG("Number of assigned RV streams: " << (streamIndex - stream));
541 return (streamIndex - stream);
542}
543
544} // namespace ns3
a polymophic address class
Definition: address.h:92
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
Hold variables of type enum.
Definition: enum.h:56
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:43
Describes an IPv6 address.
Definition: ipv6-address.h:50
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 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 PlmeGetAttributeConfirm(LrWpanPhyEnumeration status, LrWpanPibAttributeIdentifier id, 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 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 ()
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
Network layer to device interface.
Definition: net-device.h:98
virtual void DoInitialize()
Initialize() implementation.
Definition: object.cc:360
virtual void DoDispose()
Destructor implementation.
Definition: object.cc:353
uint32_t GetSize() const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:863
Hold objects of type Ptr<T>.
Definition: pointer.h:37
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:78
void ConnectWithoutContext(const CallbackBase &callback)
Append a Callback to the chain (without a context).
a unique identifier for an interface.
Definition: type-id.h:60
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:935
Ptr< const AttributeAccessor > MakeBooleanAccessor(T1 a1)
Definition: boolean.h:86
Ptr< const AttributeChecker > MakeBooleanChecker()
Definition: boolean.cc:124
Ptr< const AttributeAccessor > MakeEnumAccessor(T1 a1)
Definition: enum.h:205
Ptr< const AttributeAccessor > MakePointerAccessor(T1 a1)
Definition: pointer.h:230
#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:62
@ SHORT_ADDR
Definition: lr-wpan-mac.h:156
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:45
address
Definition: first.py:40
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:691
Ptr< const AttributeChecker > MakeEnumChecker(int v, std::string n, Ts... args)
Make an EnumChecker pre-configured with a set of allowed values by name.
Definition: enum.h:163
channel
Definition: third.py:81
mac
Definition: third.py:85
phy
Definition: third.py:82
MCPS-DATA.indication params.
Definition: lr-wpan-mac.h:373
uint8_t m_dstAddrMode
Destination address mode.
Definition: lr-wpan-mac.h:378
uint16_t m_srcPanId
Source PAN identifier.
Definition: lr-wpan-mac.h:375
Mac64Address m_srcExtAddr
Source extended address.
Definition: lr-wpan-mac.h:377
Mac16Address m_srcAddr
Source address.
Definition: lr-wpan-mac.h:376
MCPS-DATA.request params.
Definition: lr-wpan-mac.h:345
LrWpanAddressMode m_srcAddrMode
Source address mode.
Definition: lr-wpan-mac.h:346
LrWpanAddressMode m_dstAddrMode
Destination address mode.
Definition: lr-wpan-mac.h:347
uint16_t m_dstPanId
Destination PAN identifier.
Definition: lr-wpan-mac.h:348
Mac16Address m_dstAddr
Destination address.
Definition: lr-wpan-mac.h:349
uint8_t m_msduHandle
MSDU handle.
Definition: lr-wpan-mac.h:351
uint8_t m_txOptions
Tx Options (bitfield)
Definition: lr-wpan-mac.h:352