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 
132  if (!mobility)
133  {
134  NS_LOG_WARN ("LrWpanNetDevice: no Mobility found on the node, probably it's not a good idea.");
135  }
136  m_phy->SetMobility (mobility);
137  Ptr<LrWpanErrorModel> model = CreateObject<LrWpanErrorModel> ();
138  m_phy->SetErrorModel (model);
139  m_phy->SetDevice (this);
140 
141  m_phy->SetPdDataIndicationCallback (MakeCallback (&LrWpanMac::PdDataIndication, m_mac));
142  m_phy->SetPdDataConfirmCallback (MakeCallback (&LrWpanMac::PdDataConfirm, m_mac));
143  m_phy->SetPlmeEdConfirmCallback (MakeCallback (&LrWpanMac::PlmeEdConfirm, m_mac));
144  m_phy->SetPlmeGetAttributeConfirmCallback (MakeCallback (&LrWpanMac::PlmeGetAttributeConfirm, m_mac));
145  m_phy->SetPlmeSetTRXStateConfirmCallback (MakeCallback (&LrWpanMac::PlmeSetTRXStateConfirm, m_mac));
146  m_phy->SetPlmeSetAttributeConfirmCallback (MakeCallback (&LrWpanMac::PlmeSetAttributeConfirm, m_mac));
147 
148  m_csmaca->SetLrWpanMacStateCallback (MakeCallback (&LrWpanMac::SetLrWpanMacState, m_mac));
149  m_phy->SetPlmeCcaConfirmCallback (MakeCallback (&LrWpanCsmaCa::PlmeCcaConfirm, m_csmaca));
150  m_configComplete = true;
151 }
152 
153 void
155 {
156  NS_LOG_FUNCTION (this);
157  m_mac = mac;
158  CompleteConfig ();
159 }
160 
161 void
163 {
164  NS_LOG_FUNCTION (this);
165  m_phy = phy;
166  CompleteConfig ();
167 }
168 
169 void
171 {
172  NS_LOG_FUNCTION (this);
173  m_csmaca = csmaca;
174  CompleteConfig ();
175 }
176 
177 void
179 {
180  NS_LOG_FUNCTION (this << channel);
181  m_phy->SetChannel (channel);
182  channel->AddRx (m_phy);
183  CompleteConfig ();
184 }
185 
188 {
189  // NS_LOG_FUNCTION (this);
190  return m_mac;
191 }
192 
195 {
196  NS_LOG_FUNCTION (this);
197  return m_phy;
198 }
199 
202 {
203  NS_LOG_FUNCTION (this);
204  return m_csmaca;
205 }
206 void
207 LrWpanNetDevice::SetIfIndex (const uint32_t index)
208 {
209  NS_LOG_FUNCTION (this << index);
210  m_ifIndex = index;
211 }
212 
213 uint32_t
215 {
216  NS_LOG_FUNCTION (this);
217  return m_ifIndex;
218 }
219 
222 {
223  NS_LOG_FUNCTION (this);
224  return m_phy->GetChannel ();
225 }
226 
227 void
229 {
230  NS_LOG_FUNCTION (this);
231  m_linkUp = true;
232  m_linkChanges ();
233 }
234 
235 void
237 {
238  NS_LOG_FUNCTION (this);
239  m_linkUp = false;
240  m_linkChanges ();
241 }
242 
245 {
246  NS_LOG_FUNCTION (this);
247  return m_phy->GetChannel ();
248 }
249 
250 void
252 {
253  NS_LOG_FUNCTION (this);
254  m_mac->SetShortAddress (Mac16Address::ConvertFrom (address));
255 }
256 
257 Address
259 {
260  NS_LOG_FUNCTION (this);
261  return m_mac->GetShortAddress ();
262 }
263 
264 bool
265 LrWpanNetDevice::SetMtu (const uint16_t mtu)
266 {
267  NS_ABORT_MSG ("Unsupported");
268  return false;
269 }
270 
271 uint16_t
273 {
274  NS_LOG_FUNCTION (this);
275  // Maximum payload size is: max psdu - frame control - seqno - addressing - security - fcs
276  // = 127 - 2 - 1 - (2+2+2+2) - 0 - 2
277  // = 114
278  // assuming no security and addressing with only 16 bit addresses without pan id compression.
279  return 114;
280 }
281 
282 bool
284 {
285  NS_LOG_FUNCTION (this);
286  return m_phy != 0 && m_linkUp;
287 }
288 
289 void
291 {
292  NS_LOG_FUNCTION (this);
294 }
295 
296 bool
298 {
299  NS_LOG_FUNCTION (this);
300  return true;
301 }
302 
303 Address
305 {
306  NS_LOG_FUNCTION (this);
307  return Mac16Address ("ff:ff");
308 }
309 
310 bool
312 {
313  NS_LOG_FUNCTION (this);
314  return true;
315 }
316 
317 Address
319 {
320  NS_ABORT_MSG ("Unsupported");
321  return Address ();
322 }
323 
324 Address
326 {
327  NS_LOG_FUNCTION (this);
328  /* Implementation based on RFC 4944 Section 9.
329  * An IPv6 packet with a multicast destination address (DST),
330  * consisting of the sixteen octets DST[1] through DST[16], is
331  * transmitted to the following 802.15.4 16-bit multicast address:
332  * 0 1
333  * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
334  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
335  * |1 0 0|DST[15]* | DST[16] |
336  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
337  * Here, DST[15]* refers to the last 5 bits in octet DST[15], that is,
338  * bits 3-7 within DST[15]. The initial 3-bit pattern of "100" follows
339  * the 16-bit address format for multicast addresses (Section 12). */
340 
341  // \todo re-add this once Lr-Wpan will be able to accept these multicast addresses
342  // uint8_t buf[16];
343  // uint8_t buf2[2];
344  //
345  // addr.GetBytes(buf);
346  //
347  // buf2[0] = 0x80 | (buf[14] & 0x1F);
348  // buf2[1] = buf[15];
349  //
350  // Mac16Address newaddr = Mac16Address();
351  // newaddr.CopyFrom(buf2);
352  // return newaddr;
353 
354  return Mac16Address ("ff:ff");
355 }
356 
357 bool
359 {
360  NS_LOG_FUNCTION (this);
361  return false;
362 }
363 
364 bool
366 {
367  NS_LOG_FUNCTION (this);
368  return false;
369 }
370 
371 bool
372 LrWpanNetDevice::Send (Ptr<Packet> packet, const Address& dest, uint16_t protocolNumber)
373 {
374  // This method basically assumes an 802.3-compliant device, but a raw
375  // 802.15.4 device does not have an ethertype, and requires specific
376  // McpsDataRequest parameters.
377  // For further study: how to support these methods somehow, such as
378  // inventing a fake ethertype and packet tag for McpsDataRequest
379  NS_LOG_FUNCTION (this << packet << dest << protocolNumber);
380 
381  if (packet->GetSize () > GetMtu ())
382  {
383  NS_LOG_ERROR ("Fragmentation is needed for this packet, drop the packet ");
384  return false;
385  }
386 
387  McpsDataRequestParams m_mcpsDataRequestParams;
388  m_mcpsDataRequestParams.m_dstAddr = Mac16Address::ConvertFrom (dest);
389  m_mcpsDataRequestParams.m_dstAddrMode = SHORT_ADDR;
390  m_mcpsDataRequestParams.m_dstPanId = m_mac->GetPanId ();
391  m_mcpsDataRequestParams.m_srcAddrMode = SHORT_ADDR;
392  // Using ACK requests for broadcast destinations is ok here. They are disabled
393  // by the MAC.
394  if (m_useAcks)
395  {
396  m_mcpsDataRequestParams.m_txOptions = TX_OPTION_ACK;
397  }
398  m_mcpsDataRequestParams.m_msduHandle = 0;
399  m_mac->McpsDataRequest (m_mcpsDataRequestParams, packet);
400  return true;
401 }
402 
403 bool
404 LrWpanNetDevice::SendFrom (Ptr<Packet> packet, const Address& source, const Address& dest, uint16_t protocolNumber)
405 {
406  NS_ABORT_MSG ("Unsupported");
407  // TODO: To support SendFrom, the MACs McpsDataRequest has to use the provided source address, instead of to local one.
408  return false;
409 }
410 
411 Ptr<Node>
413 {
414  NS_LOG_FUNCTION (this);
415  return m_node;
416 }
417 
418 void
420 {
421  NS_LOG_FUNCTION (this);
422  m_node = node;
423  CompleteConfig ();
424 }
425 
426 bool
428 {
429  NS_LOG_FUNCTION (this);
430  return true;
431 }
432 
433 void
435 {
436  NS_LOG_FUNCTION (this);
437  m_receiveCallback = cb;
438 }
439 
440 void
442 {
443  // This method basically assumes an 802.3-compliant device, but a raw
444  // 802.15.4 device does not have an ethertype, and requires specific
445  // McpsDataIndication parameters.
446  // For further study: how to support these methods somehow, such as
447  // inventing a fake ethertype and packet tag for McpsDataRequest
448  NS_LOG_WARN ("Unsupported; use LrWpan MAC APIs instead");
449 }
450 
451 void
453 {
454  NS_LOG_FUNCTION (this);
455  // TODO: Use the PromiscReceiveCallback if the MAC is in promiscuous mode.
456  m_receiveCallback (this, pkt, 0, params.m_srcAddr);
457 }
458 
459 bool
461 {
463  return false;
464 }
465 
466 int64_t
468 {
469  NS_LOG_FUNCTION (stream);
470  int64_t streamIndex = stream;
471  streamIndex += m_csmaca->AssignStreams (stream);
472  streamIndex += m_phy->AssignStreams (stream);
473  NS_LOG_DEBUG ("Number of assigned RV streams: " << (streamIndex - stream));
474  return (streamIndex - stream);
475 }
476 
477 } // 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:159
virtual uint16_t GetMtu(void) const
virtual void SetNode(Ptr< Node > node)
virtual void DoInitialize(void)
Initialize() implementation.
Definition: object.cc:353
virtual void AddLinkChangeCallback(Callback< void > callback)
#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:36
#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:45
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:917
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:852
bool m_useAcks
Configure the NetDevice to request MAC layer acknowledgments when sending packets using the Send() AP...
virtual Address GetBroadcast(void) const
Mac16Address m_dstAddr
Destination address.
Definition: lr-wpan-mac.h:160
static Mac16Address ConvertFrom(const Address &address)
ReceiveCallback m_receiveCallback
Upper layer callback used for notification of new data packet arrivals.
static TypeId GetTypeId(void)
Get the type ID.
TracedCallback m_linkChanges
Trace source for link up/down changes.
virtual uint32_t GetIfIndex(void) const
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:84
void PlmeCcaConfirm(LrWpanPhyEnumeration status)
IEEE 802.15.4-2006 section 6.2.2.2 PLME-CCA.confirm status.
virtual bool SupportsSendFrom(void) const
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:204
virtual void SetReceiveCallback(NetDevice::ReceiveCallback cb)
virtual bool SendFrom(Ptr< Packet > packet, const Address &source, const Address &dest, uint16_t protocolNumber)
virtual Address GetAddress(void) const
This method indirects to LrWpanMac::SetShortAddress ()
virtual void SetPromiscReceiveCallback(PromiscReceiveCallback cb)
virtual void DoDispose(void)
Destructor implementation.
Definition: object.cc:346
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
virtual void DoDispose(void)
Destructor implementation.
virtual bool IsBridge(void) const
Return true if the net device is acting as a bridge.
Network layer to device interface.
uint8_t m_msduHandle
MSDU handle.
Definition: lr-wpan-mac.h:162
a polymophic address class
Definition: address.h:90
Ptr< LrWpanPhy > GetPhy(void) const
Get the PHY used by this NetDevice.
channel
Definition: third.py:85
mobility
Definition: third.py:101
virtual void SetAddress(Address address)
This method indirects to LrWpanMac::SetShortAddress ()
phy
Definition: third.py:86
virtual Ptr< Channel > GetChannel(void) const
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:970
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:910
TX_OPTION_ACK.
Definition: lr-wpan-mac.h:57
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:220
virtual void DoInitialize(void)
Initialize() implementation.
virtual bool IsMulticast(void) const
mac
Definition: third.py:92
void LinkDown(void)
Mark NetDevice link as down.
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1489
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:821
uint8_t m_txOptions
Tx Options (bitfield)
Definition: lr-wpan-mac.h:163
void SetMac(Ptr< LrWpanMac > mac)
Set the MAC to be used by this NetDevice.
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:491
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:459
LrWpanAddressMode m_srcAddrMode
Source address mode.
Definition: lr-wpan-mac.h:157
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<T>.
Definition: pointer.h:36
address
Definition: first.py:37
LrWpanAddressMode m_dstAddrMode
Destination address mode.
Definition: lr-wpan-mac.h:158
Ptr< const AttributeChecker > MakeBooleanChecker(void)
Definition: boolean.cc:121
Ptr< LrWpanPhy > m_phy
The PHY for this NetDevice.
void CompleteConfig(void)
Configure PHY, MAC and CSMA/CA.
Ptr< SpectrumChannel > DoGetChannel(void) const
Attribute accessor method for the "Channel" attribute.
uint32_t m_ifIndex
The interface index of this NetDevice.
Mac16Address m_srcAddr
Source address.
Definition: lr-wpan-mac.h:186
This class can contain 16 bit addresses.
Definition: mac16-address.h:41
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:49
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:40
virtual bool IsPointToPoint(void) const
Return true if the net device is on a point-to-point link.
void ConnectWithoutContext(const CallbackBase &callback)
Append a Callback to the chain (without a context).
Network layer to device interface.
Definition: net-device.h:95
#define NS_LOG_WARN(msg)
Use NS_LOG to output a message of level LOG_WARN.
Definition: log.h:264
void LinkUp(void)
Mark NetDevice link as up.
Ptr< LrWpanCsmaCa > GetCsmaCa(void) const
Get the CSMA/CA implementation used by this NetDevice.
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:272
Ptr< Node > m_node
The node associated with this NetDevice.
virtual Ptr< Node > GetNode(void) const
#define NS_LOG_ERROR(msg)
Use NS_LOG to output a message of level LOG_ERROR.
Definition: log.h:256
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:925
MCPS-DATA.request params.
Definition: lr-wpan-mac.h:146
virtual bool IsLinkUp(void) const
virtual Address GetMulticast(Ipv4Address multicastGroup) const
Make and return a MAC multicast address using the provided multicast group.
Ptr< LrWpanMac > GetMac(void) const
Get the MAC used by this NetDevice.
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.
a unique identifier for an interface.
Definition: type-id.h:58
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:915
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:963
virtual bool SetMtu(const uint16_t mtu)
virtual bool IsBroadcast(void) const
MCPS-DATA.indication params.
Definition: lr-wpan-mac.h:182
virtual bool NeedsArp(void) const