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("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);
256 if (Mac16Address::IsMatchingType(address))
257 {
258 m_mac->SetShortAddress(Mac16Address::ConvertFrom(address));
259 }
260 else if (Mac64Address::IsMatchingType(address))
261 {
262 m_mac->SetExtendedAddress(Mac64Address::ConvertFrom(address));
263 }
264 else if (Mac48Address::IsMatchingType(address))
265 {
266 uint8_t buf[6];
268 addr.CopyTo(buf);
269 Mac16Address addr16;
270 addr16.CopyFrom(buf + 4);
271 m_mac->SetShortAddress(addr16);
272 uint16_t panId;
273 panId = buf[0];
274 panId <<= 8;
275 panId |= buf[1];
276 m_mac->SetPanId(panId);
277 }
278 else
279 {
280 NS_ABORT_MSG("LrWpanNetDevice::SetAddress - address is not of a compatible type");
281 }
282}
283
286{
287 NS_LOG_FUNCTION(this);
288
289 if (m_mac->GetShortAddress() == Mac16Address("00:00"))
290 {
291 return m_mac->GetExtendedAddress();
292 }
293
294 Mac48Address pseudoAddress = BuildPseudoMacAddress(m_mac->GetPanId(), m_mac->GetShortAddress());
295
296 return pseudoAddress;
297}
298
299void
301 Mac64Address coordExtAddr,
302 Mac16Address coordShortAddr,
303 Mac16Address assignedShortAddr)
304{
305 NS_LOG_FUNCTION(this);
306 m_mac->SetPanId(panId);
307 m_mac->SetAssociatedCoor(coordExtAddr);
308 m_mac->SetAssociatedCoor(coordShortAddr);
309 m_mac->SetShortAddress(assignedShortAddr);
310}
311
312bool
313LrWpanNetDevice::SetMtu(const uint16_t mtu)
314{
315 NS_ABORT_MSG("Unsupported");
316 return false;
317}
318
319uint16_t
321{
322 NS_LOG_FUNCTION(this);
323 // Maximum payload size is: max psdu - frame control - seqno - addressing - security - fcs
324 // = 127 - 2 - 1 - (2+2+2+2) - 0 - 2
325 // = 114
326 // assuming no security and addressing with only 16 bit addresses without pan id compression.
327 return 114;
328}
329
330bool
332{
333 NS_LOG_FUNCTION(this);
334 return m_phy && m_linkUp;
335}
336
337void
339{
340 NS_LOG_FUNCTION(this);
342}
343
344bool
346{
347 NS_LOG_FUNCTION(this);
348 return true;
349}
350
353{
354 NS_LOG_FUNCTION(this);
355
356 Mac48Address pseudoAddress =
358
359 return pseudoAddress;
360}
361
362bool
364{
365 NS_LOG_FUNCTION(this);
366 return true;
367}
368
371{
372 NS_ABORT_MSG("Unsupported");
373 return Address();
374}
375
378{
379 NS_LOG_FUNCTION(this << addr);
380
381 Mac48Address pseudoAddress =
383
384 return pseudoAddress;
385}
386
387bool
389{
390 NS_LOG_FUNCTION(this);
391 return false;
392}
393
394bool
396{
397 NS_LOG_FUNCTION(this);
398 return false;
399}
400
401bool
402LrWpanNetDevice::Send(Ptr<Packet> packet, const Address& dest, uint16_t protocolNumber)
403{
404 // This method basically assumes an 802.3-compliant device, but a raw
405 // 802.15.4 device does not have an ethertype, and requires specific
406 // McpsDataRequest parameters.
407 // For further study: how to support these methods somehow, such as
408 // inventing a fake ethertype and packet tag for McpsDataRequest
409 NS_LOG_FUNCTION(this << packet << dest << protocolNumber);
410
411 if (packet->GetSize() > GetMtu())
412 {
413 NS_LOG_ERROR("Fragmentation is needed for this packet, drop the packet ");
414 return false;
415 }
416
417 McpsDataRequestParams m_mcpsDataRequestParams;
418
419 Mac16Address dst16;
421 {
422 uint8_t buf[6];
423 dest.CopyTo(buf);
424 dst16.CopyFrom(buf + 4);
425 }
426 else
427 {
428 dst16 = Mac16Address::ConvertFrom(dest);
429 }
430 m_mcpsDataRequestParams.m_dstAddr = dst16;
431 m_mcpsDataRequestParams.m_dstAddrMode = SHORT_ADDR;
432 m_mcpsDataRequestParams.m_dstPanId = m_mac->GetPanId();
433 m_mcpsDataRequestParams.m_srcAddrMode = SHORT_ADDR;
434 // Using ACK requests for broadcast destinations is ok here. They are disabled
435 // by the MAC.
436 if (m_useAcks)
437 {
438 m_mcpsDataRequestParams.m_txOptions = TX_OPTION_ACK;
439 }
440 m_mcpsDataRequestParams.m_msduHandle = 0;
441 m_mac->McpsDataRequest(m_mcpsDataRequestParams, packet);
442 return true;
443}
444
445bool
447 const Address& source,
448 const Address& dest,
449 uint16_t protocolNumber)
450{
451 NS_ABORT_MSG("Unsupported");
452 // TODO: To support SendFrom, the MACs McpsDataRequest has to use the provided source address,
453 // instead of to local one.
454 return false;
455}
456
459{
460 NS_LOG_FUNCTION(this);
461 return m_node;
462}
463
464void
466{
467 NS_LOG_FUNCTION(this);
468 m_node = node;
470}
471
472bool
474{
475 NS_LOG_FUNCTION(this);
476 return true;
477}
478
479void
481{
482 NS_LOG_FUNCTION(this);
484}
485
486void
488{
489 // This method basically assumes an 802.3-compliant device, but a raw
490 // 802.15.4 device does not have an ethertype, and requires specific
491 // McpsDataIndication parameters.
492 // For further study: how to support these methods somehow, such as
493 // inventing a fake ethertype and packet tag for McpsDataRequest
494 NS_LOG_WARN("Unsupported; use LrWpan MAC APIs instead");
495}
496
497void
499{
500 NS_LOG_FUNCTION(this);
501 // TODO: Use the PromiscReceiveCallback if the MAC is in promiscuous mode.
502
503 if (params.m_dstAddrMode == SHORT_ADDR)
504 {
505 m_receiveCallback(this, pkt, 0, BuildPseudoMacAddress(params.m_srcPanId, params.m_srcAddr));
506 }
507 else
508 {
509 m_receiveCallback(this, pkt, 0, params.m_srcExtAddr);
510 }
511}
512
513bool
515{
517 return false;
518}
519
522{
523 NS_LOG_FUNCTION(this);
524
525 uint8_t buf[6];
526
528 {
529 buf[0] = panId >> 8;
530 // Make sure the U/L bit is set
531 buf[0] |= 0x02;
532 buf[1] = panId & 0xff;
533 }
534 else
535 {
536 // Make sure the U/L bit is set
537 buf[0] = 0x02;
538 buf[1] = 0x00;
539 }
540 buf[2] = 0;
541 buf[3] = 0;
542 shortAddr.CopyTo(buf + 4);
543
544 Mac48Address pseudoAddress;
545 pseudoAddress.CopyFrom(buf);
546
547 return pseudoAddress;
548}
549
550int64_t
552{
553 NS_LOG_FUNCTION(stream);
554 int64_t streamIndex = stream;
555 streamIndex += m_csmaca->AssignStreams(stream);
556 streamIndex += m_phy->AssignStreams(stream);
557 NS_LOG_DEBUG("Number of assigned RV streams: " << (streamIndex - stream));
558 return (streamIndex - stream);
559}
560
561} // namespace ns3
a polymophic address class
Definition: address.h:100
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:56
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:360
virtual void DoDispose()
Destructor implementation.
Definition: object.cc:353
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:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:936
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:227
#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: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:702
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
MCPS-DATA.indication params.
Definition: lr-wpan-mac.h:430
MCPS-DATA.request params.
Definition: lr-wpan-mac.h:402
LrWpanAddressMode m_srcAddrMode
Source address mode.
Definition: lr-wpan-mac.h:403
LrWpanAddressMode m_dstAddrMode
Destination address mode.
Definition: lr-wpan-mac.h:404
uint16_t m_dstPanId
Destination PAN identifier.
Definition: lr-wpan-mac.h:405
Mac16Address m_dstAddr
Destination address.
Definition: lr-wpan-mac.h:406
uint8_t m_msduHandle
MSDU handle.
Definition: lr-wpan-mac.h:408
uint8_t m_txOptions
Tx Options (bitfield)
Definition: lr-wpan-mac.h:409