A Discrete-Event Network Simulator
API
lr-wpan-net-device.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2011 The Boeing Company
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author:
19  * Tom Henderson <thomas.r.henderson@boeing.com>
20  * Tommaso Pecorella <tommaso.pecorella@unifi.it>
21  * Margherita Filippetti <morag87@gmail.com>
22  */
23 #include "lr-wpan-net-device.h"
24 #include "lr-wpan-phy.h"
25 #include "lr-wpan-csmaca.h"
26 #include "lr-wpan-error-model.h"
27 #include <ns3/abort.h>
28 #include <ns3/node.h>
29 #include <ns3/log.h>
30 #include <ns3/spectrum-channel.h>
31 #include <ns3/pointer.h>
32 #include <ns3/boolean.h>
33 #include <ns3/mobility-model.h>
34 #include <ns3/packet.h>
35 
36 
37 namespace ns3 {
38 
39 NS_LOG_COMPONENT_DEFINE ("LrWpanNetDevice");
40 
41 NS_OBJECT_ENSURE_REGISTERED (LrWpanNetDevice);
42 
43 TypeId
45 {
46  static TypeId tid = TypeId ("ns3::LrWpanNetDevice")
47  .SetParent<NetDevice> ()
48  .SetGroupName ("LrWpan")
49  .AddConstructor<LrWpanNetDevice> ()
50  .AddAttribute ("Channel", "The channel attached to this device",
51  PointerValue (),
53  MakePointerChecker<SpectrumChannel> ())
54  .AddAttribute ("Phy", "The PHY layer attached to this device.",
55  PointerValue (),
58  MakePointerChecker<LrWpanPhy> ())
59  .AddAttribute ("Mac", "The MAC layer attached to this device.",
60  PointerValue (),
63  MakePointerChecker<LrWpanMac> ())
64  .AddAttribute ("UseAcks", "Request acknowledgments for data frames.",
65  BooleanValue (true),
68  ;
69  return tid;
70 }
71 
73  : m_configComplete (false)
74 {
75  NS_LOG_FUNCTION (this);
76  m_mac = CreateObject<LrWpanMac> ();
77  m_phy = CreateObject<LrWpanPhy> ();
78  m_csmaca = CreateObject<LrWpanCsmaCa> ();
79  CompleteConfig ();
80 }
81 
83 {
84  NS_LOG_FUNCTION (this);
85 }
86 
87 
88 void
90 {
91  NS_LOG_FUNCTION (this);
92  m_mac->Dispose ();
93  m_phy->Dispose ();
94  m_csmaca->Dispose ();
95  m_phy = 0;
96  m_mac = 0;
97  m_csmaca = 0;
98  m_node = 0;
99  // chain up.
101 
102 }
103 
104 void
106 {
107  NS_LOG_FUNCTION (this);
108  m_phy->Initialize ();
109  m_mac->Initialize ();
111 }
112 
113 
114 void
116 {
117  NS_LOG_FUNCTION (this);
118  if (m_mac == 0
119  || m_phy == 0
120  || m_csmaca == 0
121  || m_node == 0
122  || m_configComplete)
123  {
124  return;
125  }
126  m_mac->SetPhy (m_phy);
127  m_mac->SetCsmaCa (m_csmaca);
128  m_mac->SetMcpsDataIndicationCallback (MakeCallback (&LrWpanNetDevice::McpsDataIndication, this));
129  m_csmaca->SetMac (m_mac);
130 
131  m_phy->SetMobility (m_node->GetObject<MobilityModel> ());
132  Ptr<LrWpanErrorModel> model = CreateObject<LrWpanErrorModel> ();
133  m_phy->SetErrorModel (model);
134  m_phy->SetDevice (this);
135 
136  m_phy->SetPdDataIndicationCallback (MakeCallback (&LrWpanMac::PdDataIndication, m_mac));
137  m_phy->SetPdDataConfirmCallback (MakeCallback (&LrWpanMac::PdDataConfirm, m_mac));
138  m_phy->SetPlmeEdConfirmCallback (MakeCallback (&LrWpanMac::PlmeEdConfirm, m_mac));
139  m_phy->SetPlmeGetAttributeConfirmCallback (MakeCallback (&LrWpanMac::PlmeGetAttributeConfirm, m_mac));
140  m_phy->SetPlmeSetTRXStateConfirmCallback (MakeCallback (&LrWpanMac::PlmeSetTRXStateConfirm, m_mac));
141  m_phy->SetPlmeSetAttributeConfirmCallback (MakeCallback (&LrWpanMac::PlmeSetAttributeConfirm, m_mac));
142 
143  m_csmaca->SetLrWpanMacStateCallback (MakeCallback (&LrWpanMac::SetLrWpanMacState, m_mac));
144  m_phy->SetPlmeCcaConfirmCallback (MakeCallback (&LrWpanCsmaCa::PlmeCcaConfirm, m_csmaca));
145  m_configComplete = true;
146 }
147 
148 void
150 {
151  NS_LOG_FUNCTION (this);
152  m_mac = mac;
153  CompleteConfig ();
154 }
155 
156 void
158 {
159  NS_LOG_FUNCTION (this);
160  m_phy = phy;
161  CompleteConfig ();
162 }
163 
164 void
166 {
167  NS_LOG_FUNCTION (this);
168  m_csmaca = csmaca;
169  CompleteConfig ();
170 }
171 
172 void
174 {
175  NS_LOG_FUNCTION (this << channel);
176  m_phy->SetChannel (channel);
177  channel->AddRx (m_phy);
178  CompleteConfig ();
179 }
180 
183 {
184  // NS_LOG_FUNCTION (this);
185  return m_mac;
186 }
187 
190 {
191  NS_LOG_FUNCTION (this);
192  return m_phy;
193 }
194 
197 {
198  NS_LOG_FUNCTION (this);
199  return m_csmaca;
200 }
201 void
202 LrWpanNetDevice::SetIfIndex (const uint32_t index)
203 {
204  NS_LOG_FUNCTION (this << index);
205  m_ifIndex = index;
206 }
207 
208 uint32_t
210 {
211  NS_LOG_FUNCTION (this);
212  return m_ifIndex;
213 }
214 
217 {
218  NS_LOG_FUNCTION (this);
219  return m_phy->GetChannel ();
220 }
221 
222 void
224 {
225  NS_LOG_FUNCTION (this);
226  m_linkUp = true;
227  m_linkChanges ();
228 }
229 
230 void
232 {
233  NS_LOG_FUNCTION (this);
234  m_linkUp = false;
235  m_linkChanges ();
236 }
237 
240 {
241  NS_LOG_FUNCTION (this);
242  return m_phy->GetChannel ();
243 }
244 
245 void
247 {
248  NS_LOG_FUNCTION (this);
249  m_mac->SetShortAddress (Mac16Address::ConvertFrom (address));
250 }
251 
252 Address
254 {
255  NS_LOG_FUNCTION (this);
256  return m_mac->GetShortAddress ();
257 }
258 
259 bool
260 LrWpanNetDevice::SetMtu (const uint16_t mtu)
261 {
262  NS_ABORT_MSG ("Unsupported");
263  return false;
264 }
265 
266 uint16_t
268 {
269  NS_LOG_FUNCTION (this);
270  // Maximum payload size is: max psdu - frame control - seqno - addressing - security - fcs
271  // = 127 - 2 - 1 - (2+2+2+2) - 0 - 2
272  // = 114
273  // assuming no security and addressing with only 16 bit addresses without pan id compression.
274  return 114;
275 }
276 
277 bool
279 {
280  NS_LOG_FUNCTION (this);
281  return m_phy != 0 && m_linkUp;
282 }
283 
284 void
286 {
287  NS_LOG_FUNCTION (this);
289 }
290 
291 bool
293 {
294  NS_LOG_FUNCTION (this);
295  return true;
296 }
297 
298 Address
300 {
301  NS_LOG_FUNCTION (this);
302  return Mac16Address ("ff:ff");
303 }
304 
305 bool
307 {
308  NS_LOG_FUNCTION (this);
309  return true;
310 }
311 
312 Address
314 {
315  NS_ABORT_MSG ("Unsupported");
316  return Address ();
317 }
318 
319 Address
321 {
322  NS_LOG_FUNCTION (this);
323  /* Implementation based on RFC 4944 Section 9.
324  * An IPv6 packet with a multicast destination address (DST),
325  * consisting of the sixteen octets DST[1] through DST[16], is
326  * transmitted to the following 802.15.4 16-bit multicast address:
327  * 0 1
328  * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
329  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
330  * |1 0 0|DST[15]* | DST[16] |
331  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
332  * Here, DST[15]* refers to the last 5 bits in octet DST[15], that is,
333  * bits 3-7 within DST[15]. The initial 3-bit pattern of "100" follows
334  * the 16-bit address format for multicast addresses (Section 12). */
335 
336  // \todo re-add this once Lr-Wpan will be able to accept these multicast addresses
337  // uint8_t buf[16];
338  // uint8_t buf2[2];
339  //
340  // addr.GetBytes(buf);
341  //
342  // buf2[0] = 0x80 | (buf[14] & 0x1F);
343  // buf2[1] = buf[15];
344  //
345  // Mac16Address newaddr = Mac16Address();
346  // newaddr.CopyFrom(buf2);
347  // return newaddr;
348 
349  return Mac16Address ("ff:ff");
350 }
351 
352 bool
354 {
355  NS_LOG_FUNCTION (this);
356  return false;
357 }
358 
359 bool
361 {
362  NS_LOG_FUNCTION (this);
363  return false;
364 }
365 
366 bool
367 LrWpanNetDevice::Send (Ptr<Packet> packet, const Address& dest, uint16_t protocolNumber)
368 {
369  // This method basically assumes an 802.3-compliant device, but a raw
370  // 802.15.4 device does not have an ethertype, and requires specific
371  // McpsDataRequest parameters.
372  // For further study: how to support these methods somehow, such as
373  // inventing a fake ethertype and packet tag for McpsDataRequest
374  NS_LOG_FUNCTION (this << packet << dest << protocolNumber);
375 
376  if (packet->GetSize () > GetMtu ())
377  {
378  NS_LOG_ERROR ("Fragmentation is needed for this packet, drop the packet ");
379  return false;
380  }
381 
382  McpsDataRequestParams m_mcpsDataRequestParams;
383  m_mcpsDataRequestParams.m_dstAddr = Mac16Address::ConvertFrom (dest);
384  m_mcpsDataRequestParams.m_dstAddrMode = SHORT_ADDR;
385  m_mcpsDataRequestParams.m_dstPanId = m_mac->GetPanId ();
386  m_mcpsDataRequestParams.m_srcAddrMode = SHORT_ADDR;
387  // Using ACK requests for broadcast destinations is ok here. They are disabled
388  // by the MAC.
389  if (m_useAcks)
390  {
391  m_mcpsDataRequestParams.m_txOptions = TX_OPTION_ACK;
392  }
393  m_mcpsDataRequestParams.m_msduHandle = 0;
394  m_mac->McpsDataRequest (m_mcpsDataRequestParams, packet);
395  return true;
396 }
397 
398 bool
399 LrWpanNetDevice::SendFrom (Ptr<Packet> packet, const Address& source, const Address& dest, uint16_t protocolNumber)
400 {
401  NS_ABORT_MSG ("Unsupported");
402  // TODO: To support SendFrom, the MACs McpsDataRequest has to use the provided source address, instead of to local one.
403  return false;
404 }
405 
406 Ptr<Node>
408 {
409  NS_LOG_FUNCTION (this);
410  return m_node;
411 }
412 
413 void
415 {
416  NS_LOG_FUNCTION (this);
417  m_node = node;
418  CompleteConfig ();
419 }
420 
421 bool
423 {
424  NS_LOG_FUNCTION (this);
425  return true;
426 }
427 
428 void
430 {
431  NS_LOG_FUNCTION (this);
432  m_receiveCallback = cb;
433 }
434 
435 void
437 {
438  // This method basically assumes an 802.3-compliant device, but a raw
439  // 802.15.4 device does not have an ethertype, and requires specific
440  // McpsDataIndication parameters.
441  // For further study: how to support these methods somehow, such as
442  // inventing a fake ethertype and packet tag for McpsDataRequest
443  NS_LOG_WARN ("Unsupported; use LrWpan MAC APIs instead");
444 }
445 
446 void
448 {
449  NS_LOG_FUNCTION (this);
450  // TODO: Use the PromiscReceiveCallback if the MAC is in promiscuous mode.
451  m_receiveCallback (this, pkt, 0, params.m_srcAddr);
452 }
453 
454 bool
456 {
458  return false;
459 }
460 
461 int64_t
463 {
464  NS_LOG_FUNCTION (stream);
465  int64_t streamIndex = stream;
466  streamIndex += m_csmaca->AssignStreams (stream);
467  streamIndex += m_phy->AssignStreams (stream);
468  NS_LOG_DEBUG ("Number of assigned RV streams: " << (streamIndex - stream));
469  return (streamIndex - stream);
470 }
471 
472 } // namespace ns3
virtual bool Send(Ptr< Packet > packet, const Address &dest, uint16_t protocolNumber)
uint16_t m_dstPanId
Destination PAN identifier.
Definition: lr-wpan-mac.h:144
virtual void SetNode(Ptr< Node > node)
virtual void AddLinkChangeCallback(Callback< void > callback)
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:73
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
AttributeValue implementation for Boolean.
Definition: boolean.h:34
#define NS_ABORT_MSG(msg)
Unconditional abnormal program termination with a message.
Definition: abort.h:50
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:44
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...
Definition: lr-wpan-mac.cc:876
bool m_useAcks
Configure the NetDevice to request MAC layer acknowledgments when sending packets using the Send() AP...
Mac16Address m_dstAddr
Destination address.
Definition: lr-wpan-mac.h:145
static Mac16Address ConvertFrom(const Address &address)
ReceiveCallback m_receiveCallback
Upper layer callback used for notification of new data packet arrivals.
Ptr< LrWpanPhy > GetPhy(void) const
Get the PHY used by this NetDevice.
static TypeId GetTypeId(void)
Get the type ID.
TracedCallback m_linkChanges
Trace source for link up/down changes.
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:81
void PlmeCcaConfirm(LrWpanPhyEnumeration status)
IEEE 802.15.4-2006 section 6.2.2.2 PLME-CCA.confirm status.
virtual bool IsPointToPoint(void) const
Return true if the net device is on a point-to-point link.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
virtual bool NeedsArp(void) const
virtual void SetReceiveCallback(NetDevice::ReceiveCallback cb)
virtual bool SendFrom(Ptr< Packet > packet, const Address &source, const Address &dest, uint16_t protocolNumber)
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:766
virtual void SetPromiscReceiveCallback(PromiscReceiveCallback cb)
virtual void DoDispose(void)
Destructor implementation.
Definition: object.cc:339
virtual Ptr< Node > GetNode(void) const
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
Ptr< SpectrumChannel > DoGetChannel(void) const
Attribute accessor method for the "Channel" attribute.
virtual void DoDispose(void)
Destructor implementation.
virtual Ptr< Channel > GetChannel(void) const
Network layer to device interface.
virtual bool IsMulticast(void) const
uint8_t m_msduHandle
MSDU handle.
Definition: lr-wpan-mac.h:146
a polymophic address class
Definition: address.h:90
virtual void SetAddress(Address address)
This method indirects to LrWpanMac::SetShortAddress ()
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...
void SetLrWpanMacState(LrWpanMacState macState)
CSMA-CA algorithm calls back the MAC after executing channel assessment.
Definition: lr-wpan-mac.cc:929
Keep track of the current position and velocity of an object.
Ptr< LrWpanMac > m_mac
The MAC for this NetDevice.
void PlmeEdConfirm(LrWpanPhyEnumeration status, uint8_t energyLevel)
IEEE 802.15.4-2006 section 6.2.2.4 PLME-ED.confirm status and energy level.
Definition: lr-wpan-mac.cc:869
TX_OPTION_ACK.
Definition: lr-wpan-mac.h:56
virtual void SetIfIndex(const uint32_t index)
void SetChannel(Ptr< SpectrumChannel > channel)
Set the channel to which the NetDevice, and therefore the PHY, should be attached to...
Ptr< LrWpanCsmaCa > m_csmaca
The CSMA/CA implementation for this NetDevice.
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:210
virtual void DoInitialize(void)
Initialize() implementation.
void LinkDown(void)
Mark NetDevice link as down.
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1296
void PdDataConfirm(LrWpanPhyEnumeration status)
IEEE 802.15.4-2006 section 6.2.1.2 Confirm the end of transmission of an MPDU to MAC.
Definition: lr-wpan-mac.cc:780
uint8_t m_txOptions
Tx Options (bitfield)
Definition: lr-wpan-mac.h:147
void SetMac(Ptr< LrWpanMac > mac)
Set the MAC to be used by this NetDevice.
virtual uint32_t GetIfIndex(void) const
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...
Definition: lr-wpan-mac.cc:464
LrWpanAddressMode m_srcAddrMode
Source address mode.
Definition: lr-wpan-mac.h:142
bool m_linkUp
Is the link/device currently up and running?
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Hold objects of type Ptr.
Definition: pointer.h:36
LrWpanAddressMode m_dstAddrMode
Destination address mode.
Definition: lr-wpan-mac.h:143
Ptr< const AttributeChecker > MakeBooleanChecker(void)
Definition: boolean.cc:121
virtual bool IsBridge(void) const
Return true if the net device is acting as a bridge.
virtual bool SupportsSendFrom(void) const
Ptr< LrWpanPhy > m_phy
The PHY for this NetDevice.
void CompleteConfig(void)
Configure PHY, MAC and CSMA/CA.
uint32_t m_ifIndex
The interface index of this NetDevice.
Mac16Address m_srcAddr
Source address.
Definition: lr-wpan-mac.h:170
This class can contain 16 bit addresses.
Definition: mac16-address.h:41
virtual Address GetBroadcast(void) const
virtual Address GetAddress(void) const
This method indirects to LrWpanMac::SetShortAddress ()
void McpsDataIndication(McpsDataIndicationParams params, Ptr< Packet > pkt)
The callback used by the MAC to hand over incoming packets to the NetDevice.
virtual ~LrWpanNetDevice(void)
Describes an IPv6 address.
Definition: ipv6-address.h:47
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:40
void ConnectWithoutContext(const CallbackBase &callback)
Append a Callback to the chain (without a context).
Network layer to device interface.
Definition: net-device.h:75
#define NS_LOG_WARN(msg)
Use NS_LOG to output a message of level LOG_WARN.
Definition: log.h:228
void LinkUp(void)
Mark NetDevice link as up.
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:236
virtual Address GetMulticast(Ipv4Address multicastGroup) const
Make and return a MAC multicast address using the provided multicast group.
Ptr< Node > m_node
The node associated with this NetDevice.
virtual uint16_t GetMtu(void) const
#define NS_LOG_ERROR(msg)
Use NS_LOG to output a message of level LOG_ERROR.
Definition: log.h:220
void PlmeSetTRXStateConfirm(LrWpanPhyEnumeration status)
IEEE 802.15.4-2006 section 6.2.2.8 PLME-SET-TRX-STATE.confirm Set PHY state.
Definition: lr-wpan-mac.cc:884
virtual bool IsBroadcast(void) const
MCPS-DATA.request params.
Definition: lr-wpan-mac.h:131
tuple address
Definition: first.py:37
virtual bool IsLinkUp(void) const
bool m_configComplete
True if MAC, PHY and CSMA/CA where successfully configured and the NetDevice is ready for being used...
void SetCsmaCa(Ptr< LrWpanCsmaCa > csmaca)
Set the CSMA/CA implementation to be used by the MAC and this NetDevice.
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:455
a unique identifier for an interface.
Definition: type-id.h:57
TypeId SetParent(TypeId tid)
Definition: type-id.cc:638
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...
Definition: lr-wpan-mac.cc:922
virtual bool SetMtu(const uint16_t mtu)
Ptr< LrWpanCsmaCa > GetCsmaCa(void) const
Get the CSMA/CA implementation used by this NetDevice.
virtual void DoInitialize(void)
Initialize() implementation.
Definition: object.cc:346
MCPS-DATA.indication params.
Definition: lr-wpan-mac.h:166
Ptr< LrWpanMac > GetMac(void) const
Get the MAC used by this NetDevice.