A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 NS_LOG_COMPONENT_DEFINE ("LrWpanNetDevice");
38 
39 namespace ns3 {
40 
41 NS_OBJECT_ENSURE_REGISTERED (LrWpanNetDevice);
42 
43 TypeId
45 {
46  static TypeId tid = TypeId ("ns3::LrWpanNetDevice")
47  .SetParent<NetDevice> ()
48  .AddConstructor<LrWpanNetDevice> ()
49  .AddAttribute ("Channel", "The channel attached to this device",
50  PointerValue (),
51  MakePointerAccessor (&LrWpanNetDevice::DoGetChannel),
52  MakePointerChecker<SpectrumChannel> ())
53  .AddAttribute ("Phy", "The PHY layer attached to this device.",
54  PointerValue (),
55  MakePointerAccessor (&LrWpanNetDevice::GetPhy,
57  MakePointerChecker<LrWpanPhy> ())
58  .AddAttribute ("Mac", "The MAC layer attached to this device.",
59  PointerValue (),
60  MakePointerAccessor (&LrWpanNetDevice::GetMac,
62  MakePointerChecker<LrWpanMac> ())
63  .AddAttribute ("UseAcks", "Request acknowledgments for data frames.",
64  BooleanValue (true),
65  MakeBooleanAccessor (&LrWpanNetDevice::m_useAcks),
66  MakeBooleanChecker ())
67  ;
68  return tid;
69 }
70 
72  : m_configComplete (false)
73 {
74  NS_LOG_FUNCTION (this);
75  m_mac = CreateObject<LrWpanMac> ();
76  m_phy = CreateObject<LrWpanPhy> ();
77  m_csmaca = CreateObject<LrWpanCsmaCa> ();
78  CompleteConfig ();
79 }
80 
82 {
83  NS_LOG_FUNCTION (this);
84 }
85 
86 
87 void
89 {
90  NS_LOG_FUNCTION (this);
91  m_mac->Dispose ();
92  m_phy->Dispose ();
93  m_csmaca->Dispose ();
94  m_phy = 0;
95  m_mac = 0;
96  m_csmaca = 0;
97  m_node = 0;
98  // chain up.
100 
101 }
102 
103 void
105 {
106  NS_LOG_FUNCTION (this);
107  m_phy->Initialize ();
108  m_mac->Initialize ();
110 }
111 
112 
113 void
115 {
116  NS_LOG_FUNCTION (this);
117  if (m_mac == 0
118  || m_phy == 0
119  || m_csmaca == 0
120  || m_node == 0
121  || m_configComplete)
122  {
123  return;
124  }
125  m_mac->SetPhy (m_phy);
126  m_mac->SetCsmaCa (m_csmaca);
127  m_mac->SetMcpsDataIndicationCallback (MakeCallback (&LrWpanNetDevice::McpsDataIndication, this));
128  m_csmaca->SetMac (m_mac);
129 
130  m_phy->SetMobility (m_node->GetObject<MobilityModel> ());
131  Ptr<LrWpanErrorModel> model = CreateObject<LrWpanErrorModel> ();
132  m_phy->SetErrorModel (model);
133  m_phy->SetDevice (this);
134 
135  m_phy->SetPdDataIndicationCallback (MakeCallback (&LrWpanMac::PdDataIndication, m_mac));
136  m_phy->SetPdDataConfirmCallback (MakeCallback (&LrWpanMac::PdDataConfirm, m_mac));
137  m_phy->SetPlmeEdConfirmCallback (MakeCallback (&LrWpanMac::PlmeEdConfirm, m_mac));
138  m_phy->SetPlmeGetAttributeConfirmCallback (MakeCallback (&LrWpanMac::PlmeGetAttributeConfirm, m_mac));
139  m_phy->SetPlmeSetTRXStateConfirmCallback (MakeCallback (&LrWpanMac::PlmeSetTRXStateConfirm, m_mac));
140  m_phy->SetPlmeSetAttributeConfirmCallback (MakeCallback (&LrWpanMac::PlmeSetAttributeConfirm, m_mac));
141 
142  m_csmaca->SetLrWpanMacStateCallback (MakeCallback (&LrWpanMac::SetLrWpanMacState, m_mac));
143  m_phy->SetPlmeCcaConfirmCallback (MakeCallback (&LrWpanCsmaCa::PlmeCcaConfirm, m_csmaca));
144  m_configComplete = true;
145 }
146 
147 void
149 {
150  NS_LOG_FUNCTION (this);
151  m_mac = mac;
152  CompleteConfig ();
153 }
154 
155 void
157 {
158  NS_LOG_FUNCTION (this);
159  m_phy = phy;
160  CompleteConfig ();
161 }
162 
163 void
165 {
166  NS_LOG_FUNCTION (this);
167  m_csmaca = csmaca;
168  CompleteConfig ();
169 }
170 
171 void
173 {
174  NS_LOG_FUNCTION (this << channel);
175  m_phy->SetChannel (channel);
176  channel->AddRx (m_phy);
177  CompleteConfig ();
178 }
179 
182 {
183  // NS_LOG_FUNCTION (this);
184  return m_mac;
185 }
186 
189 {
190  NS_LOG_FUNCTION (this);
191  return m_phy;
192 }
193 
196 {
197  NS_LOG_FUNCTION (this);
198  return m_csmaca;
199 }
200 void
201 LrWpanNetDevice::SetIfIndex (const uint32_t index)
202 {
203  NS_LOG_FUNCTION (this << index);
204  m_ifIndex = index;
205 }
206 
207 uint32_t
209 {
210  NS_LOG_FUNCTION (this);
211  return m_ifIndex;
212 }
213 
216 {
217  NS_LOG_FUNCTION (this);
218  return m_phy->GetChannel ();
219 }
220 
221 void
223 {
224  NS_LOG_FUNCTION (this);
225  m_linkUp = true;
226  m_linkChanges ();
227 }
228 
229 void
231 {
232  NS_LOG_FUNCTION (this);
233  m_linkUp = false;
234  m_linkChanges ();
235 }
236 
239 {
240  NS_LOG_FUNCTION (this);
241  return m_phy->GetChannel ();
242 }
243 
244 void
246 {
247  NS_LOG_FUNCTION (this);
248  m_mac->SetShortAddress (Mac16Address::ConvertFrom (address));
249 }
250 
251 Address
253 {
254  NS_LOG_FUNCTION (this);
255  return m_mac->GetShortAddress ();
256 }
257 
258 bool
259 LrWpanNetDevice::SetMtu (const uint16_t mtu)
260 {
261  NS_ABORT_MSG ("Unsupported");
262  return false;
263 }
264 
265 uint16_t
267 {
268  NS_LOG_FUNCTION (this);
269  // Maximum payload size is: max psdu - frame control - seqno - addressing - security - fcs
270  // = 127 - 2 - 1 - (2+2+2+2) - 0 - 2
271  // = 114
272  // assuming no security and addressing with only 16 bit addresses without pan id compression.
273  return 114;
274 }
275 
276 bool
278 {
279  NS_LOG_FUNCTION (this);
280  return m_phy != 0 && m_linkUp;
281 }
282 
283 void
285 {
286  NS_LOG_FUNCTION (this);
288 }
289 
290 bool
292 {
293  NS_LOG_FUNCTION (this);
294  return true;
295 }
296 
297 Address
299 {
300  NS_LOG_FUNCTION (this);
301  return Mac16Address ("ff:ff");
302 }
303 
304 bool
306 {
307  NS_LOG_FUNCTION (this);
308  return true;
309 }
310 
311 Address
313 {
314  NS_ABORT_MSG ("Unsupported");
315  return Address ();
316 }
317 
318 Address
320 {
321  NS_LOG_FUNCTION (this);
322  /* Implementation based on RFC 4944 Section 9.
323  * An IPv6 packet with a multicast destination address (DST),
324  * consisting of the sixteen octets DST[1] through DST[16], is
325  * transmitted to the following 802.15.4 16-bit multicast address:
326  * 0 1
327  * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
328  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
329  * |1 0 0|DST[15]* | DST[16] |
330  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
331  * Here, DST[15]* refers to the last 5 bits in octet DST[15], that is,
332  * bits 3-7 within DST[15]. The initial 3-bit pattern of "100" follows
333  * the 16-bit address format for multicast addresses (Section 12). */
334 
335  // \todo re-add this once Lr-Wpan will be able to accept these multicast addresses
336  // uint8_t buf[16];
337  // uint8_t buf2[2];
338  //
339  // addr.GetBytes(buf);
340  //
341  // buf2[0] = 0x80 | (buf[14] & 0x1F);
342  // buf2[1] = buf[15];
343  //
344  // Mac16Address newaddr = Mac16Address();
345  // newaddr.CopyFrom(buf2);
346  // return newaddr;
347 
348  return Mac16Address ("ff:ff");
349 }
350 
351 bool
353 {
354  NS_LOG_FUNCTION (this);
355  return false;
356 }
357 
358 bool
360 {
361  NS_LOG_FUNCTION (this);
362  return false;
363 }
364 
365 bool
366 LrWpanNetDevice::Send (Ptr<Packet> packet, const Address& dest, uint16_t protocolNumber)
367 {
368  // This method basically assumes an 802.3-compliant device, but a raw
369  // 802.15.4 device does not have an ethertype, and requires specific
370  // McpsDataRequest parameters.
371  // For further study: how to support these methods somehow, such as
372  // inventing a fake ethertype and packet tag for McpsDataRequest
373  NS_LOG_FUNCTION (this << packet << dest << protocolNumber);
374 
375  if (packet->GetSize () > GetMtu ())
376  {
377  NS_LOG_ERROR ("Fragmentation is needed for this packet, drop the packet ");
378  return false;
379  }
380 
381  McpsDataRequestParams m_mcpsDataRequestParams;
382  m_mcpsDataRequestParams.m_dstAddr = Mac16Address::ConvertFrom (dest);
383  m_mcpsDataRequestParams.m_dstAddrMode = SHORT_ADDR;
384  m_mcpsDataRequestParams.m_dstPanId = m_mac->GetPanId ();
385  m_mcpsDataRequestParams.m_srcAddrMode = SHORT_ADDR;
386  // Using ACK requests for broadcast destinations is ok here. They are disabled
387  // by the MAC.
388  if (m_useAcks)
389  {
390  m_mcpsDataRequestParams.m_txOptions = TX_OPTION_ACK;
391  }
392  m_mcpsDataRequestParams.m_msduHandle = 0;
393  m_mac->McpsDataRequest (m_mcpsDataRequestParams, packet);
394  return true;
395 }
396 
397 bool
398 LrWpanNetDevice::SendFrom (Ptr<Packet> packet, const Address& source, const Address& dest, uint16_t protocolNumber)
399 {
400  NS_ABORT_MSG ("Unsupported");
401  // TODO: To support SendFrom, the MACs McpsDataRequest has to use the provided source address, instead of to local one.
402  return false;
403 }
404 
405 Ptr<Node>
407 {
408  NS_LOG_FUNCTION (this);
409  return m_node;
410 }
411 
412 void
414 {
415  NS_LOG_FUNCTION (this);
416  m_node = node;
417  CompleteConfig ();
418 }
419 
420 bool
422 {
423  NS_LOG_FUNCTION (this);
424  return true;
425 }
426 
427 void
429 {
430  NS_LOG_FUNCTION (this);
431  m_receiveCallback = cb;
432 }
433 
434 void
436 {
437  // This method basically assumes an 802.3-compliant device, but a raw
438  // 802.15.4 device does not have an ethertype, and requires specific
439  // McpsDataIndication parameters.
440  // For further study: how to support these methods somehow, such as
441  // inventing a fake ethertype and packet tag for McpsDataRequest
442  NS_LOG_WARN ("Unsupported; use LrWpan MAC APIs instead");
443 }
444 
445 void
447 {
448  NS_LOG_FUNCTION (this);
449  // TODO: Use the PromiscReceiveCallback if the MAC is in promiscuous mode.
450  m_receiveCallback (this, pkt, 0, params.m_srcAddr);
451 }
452 
453 bool
455 {
457  return false;
458 }
459 
460 int64_t
462 {
463  NS_LOG_FUNCTION (stream);
464  int64_t streamIndex = stream;
465  streamIndex += m_csmaca->AssignStreams (stream);
466  streamIndex += m_phy->AssignStreams (stream);
467  NS_LOG_DEBUG ("Number of assigned RV streams: " << (streamIndex - stream));
468  return (streamIndex - stream);
469 }
470 
471 } // namespace ns3
virtual bool Send(Ptr< Packet > packet, const Address &dest, uint16_t protocolNumber)
virtual void SetNode(Ptr< Node > node)
virtual void AddLinkChangeCallback(Callback< void > callback)
smart pointer class similar to boost::intrusive_ptr
Definition: ptr.h:60
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
Hold a bool native type.
Definition: boolean.h:38
virtual void SetReceiveCallback(ReceiveCallback cb)
#define NS_ABORT_MSG(msg)
Abnormal program termination.
Definition: abort.h:44
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register the class in the ns-3 factory.
Definition: object-base.h:38
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:850
bool m_useAcks
Configure the NetDevice to request MAC layer acknowledgements when sending packets using the Send() A...
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.
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:170
virtual bool NeedsArp(void) const
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:744
virtual void SetPromiscReceiveCallback(PromiscReceiveCallback cb)
virtual void DoDispose(void)
This method is called by Object::Dispose or by the object's destructor, whichever comes first...
Definition: object.cc:335
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)
This method is called by Object::Dispose or by the object's destructor, whichever comes first...
virtual Ptr< Channel > GetChannel(void) const
virtual bool IsMulticast(void) const
a polymophic address class
Definition: address.h:86
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:903
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:843
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.
virtual void DoInitialize(void)
This method is called only once by Object::Initialize.
void LinkDown(void)
Mark NetDevice link as down.
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1242
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:754
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:438
LrWpanAddressMode m_srcAddrMode
Definition: lr-wpan-mac.h:119
bool m_linkUp
Is the link/device currently up and running?
hold objects of type Ptr
Definition: pointer.h:33
LrWpanAddressMode m_dstAddrMode
Definition: lr-wpan-mac.h:120
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.
This class can contain 16 bit addresses.
Definition: mac16-address.h:39
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:46
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:38
void ConnectWithoutContext(const CallbackBase &callback)
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:203
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:213
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:193
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:858
virtual bool IsBroadcast(void) const
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
Definition: object.h:362
a unique identifier for an interface.
Definition: type-id.h:49
TypeId SetParent(TypeId tid)
Definition: type-id.cc:610
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:896
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)
This method is called only once by Object::Initialize.
Definition: object.cc:342
Ptr< LrWpanMac > GetMac(void) const
Get the MAC used by this NetDevice.