src/devices/wifi/wifi-net-device.cc
author vincent@clarinet.u-strasbg.fr
Fri Nov 07 11:36:15 2008 -0800 (2008-11-07)
changeset 3852 9cf7ad0cac85
parent 3841 1e7abf5fca79
child 3914 18ac5bec5c49
child 3936 e525995ce5dc
permissions -rw-r--r--
Initial IPv6 capability
mathieu@1956
     1
/* -*-  Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
mathieu@1956
     2
/*
mathieu@1956
     3
 * Copyright (c) 2005,2006 INRIA
mathieu@1956
     4
 *
mathieu@1956
     5
 * This program is free software; you can redistribute it and/or modify
mathieu@1956
     6
 * it under the terms of the GNU General Public License version 2 as 
mathieu@1956
     7
 * published by the Free Software Foundation;
mathieu@1956
     8
 *
mathieu@1956
     9
 * This program is distributed in the hope that it will be useful,
mathieu@1956
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
mathieu@1956
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
mathieu@1956
    12
 * GNU General Public License for more details.
mathieu@1956
    13
 *
mathieu@1956
    14
 * You should have received a copy of the GNU General Public License
mathieu@1956
    15
 * along with this program; if not, write to the Free Software
mathieu@1956
    16
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
mathieu@1956
    17
 *
mathieu@1956
    18
 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
mathieu@1956
    19
 */
mathieu@2524
    20
#include "wifi-net-device.h"
mathieu@2524
    21
#include "wifi-mac.h"
mathieu@2524
    22
#include "wifi-phy.h"
mathieu@2524
    23
#include "wifi-remote-station-manager.h"
mathieu@2524
    24
#include "wifi-channel.h"
mathieu@2524
    25
#include "ns3/llc-snap-header.h"
mathieu@1956
    26
#include "ns3/packet.h"
mathieu@2524
    27
#include "ns3/uinteger.h"
mathieu@2927
    28
#include "ns3/pointer.h"
mathieu@1956
    29
#include "ns3/node.h"
mathieu@2524
    30
#include "ns3/trace-source-accessor.h"
mathieu@1956
    31
mathieu@1956
    32
namespace ns3 {
mathieu@1956
    33
mathieu@2942
    34
NS_OBJECT_ENSURE_REGISTERED (WifiNetDevice);
mathieu@2942
    35
mathieu@2524
    36
TypeId 
mathieu@2524
    37
WifiNetDevice::GetTypeId (void)
mathieu@2053
    38
{
mathieu@2602
    39
  static TypeId tid = TypeId ("ns3::WifiNetDevice")
mathieu@2524
    40
    .SetParent<NetDevice> ()
mathieu@2961
    41
    .AddAttribute ("Channel", "The channel attached to this device",
mathieu@2965
    42
                   PointerValue (),
mathieu@2927
    43
                   MakePointerAccessor (&WifiNetDevice::DoGetChannel,
mathieu@2927
    44
                                        &WifiNetDevice::SetChannel),
mathieu@2935
    45
                   MakePointerChecker<WifiChannel> ())
mathieu@2961
    46
    .AddAttribute ("Phy", "The PHY layer attached to this device.",
mathieu@2965
    47
                   PointerValue (),
mathieu@2927
    48
                   MakePointerAccessor (&WifiNetDevice::GetPhy,
mathieu@2927
    49
                                        &WifiNetDevice::SetPhy),
mathieu@2927
    50
                   MakePointerChecker<WifiPhy> ())
mathieu@2961
    51
    .AddAttribute ("Mac", "The MAC layer attached to this device.",
mathieu@2965
    52
                   PointerValue (),
mathieu@2927
    53
                   MakePointerAccessor (&WifiNetDevice::GetMac,
mathieu@2927
    54
                                        &WifiNetDevice::SetMac),
mathieu@2927
    55
                   MakePointerChecker<WifiMac> ())
mathieu@2961
    56
    .AddAttribute ("RemoteStationManager", "The station manager attached to this device.",
mathieu@2965
    57
                   PointerValue (),
mathieu@2927
    58
                   MakePointerAccessor (&WifiNetDevice::SetRemoteStationManager,
mathieu@2927
    59
                                        &WifiNetDevice::GetRemoteStationManager),
mathieu@2927
    60
                   MakePointerChecker<WifiRemoteStationManager> ())
mathieu@2961
    61
    .AddTraceSource ("Rx", "Received payload from the MAC layer.",
mathieu@2524
    62
                     MakeTraceSourceAccessor (&WifiNetDevice::m_rxLogger))
mathieu@2961
    63
    .AddTraceSource ("Tx", "Send payload to the MAC layer.",
mathieu@2524
    64
                     MakeTraceSourceAccessor (&WifiNetDevice::m_txLogger))
mathieu@2524
    65
    ;
mathieu@2524
    66
  return tid;
mathieu@2053
    67
}
mathieu@2053
    68
mathieu@2524
    69
WifiNetDevice::WifiNetDevice ()
mathieu@2562
    70
  : m_mtu (0)
mathieu@2524
    71
{}
mathieu@1956
    72
WifiNetDevice::~WifiNetDevice ()
mathieu@2002
    73
{}
mathieu@2530
    74
mathieu@2530
    75
void
mathieu@2530
    76
WifiNetDevice::DoDispose (void)
mathieu@2530
    77
{
mathieu@2530
    78
  m_node = 0;
mathieu@2530
    79
  m_mac->Dispose ();
mathieu@2530
    80
  m_phy->Dispose ();
mathieu@2530
    81
  m_stationManager->Dispose ();
mathieu@2530
    82
  m_mac = 0;
mathieu@2530
    83
  m_phy = 0;
mathieu@2618
    84
  m_channel = 0;
mathieu@2530
    85
  m_stationManager = 0;
mathieu@2530
    86
  // chain up.
mathieu@2530
    87
  NetDevice::DoDispose ();
mathieu@2530
    88
}
mathieu@2524
    89
  
mathieu@2524
    90
void 
mathieu@2601
    91
WifiNetDevice::SetMac (Ptr<WifiMac> mac)
mathieu@2524
    92
{
mathieu@2524
    93
  m_mac = mac;
mathieu@2601
    94
  if (m_mac != 0)
mathieu@2601
    95
    {
mathieu@2601
    96
      if (m_stationManager != 0)
mathieu@2601
    97
        {
mathieu@2601
    98
          m_mac->SetWifiRemoteStationManager (m_stationManager);
mathieu@2601
    99
        }
mathieu@2601
   100
      if (m_phy != 0)
mathieu@2601
   101
        {
mathieu@2601
   102
          m_mac->SetWifiPhy (m_phy);
mathieu@2601
   103
        }
mathieu@2601
   104
      m_mac->SetForwardUpCallback (MakeCallback (&WifiNetDevice::ForwardUp, this));
mathieu@2601
   105
      m_mac->SetLinkUpCallback (MakeCallback (&WifiNetDevice::LinkUp, this));
mathieu@2601
   106
      m_mac->SetLinkDownCallback (MakeCallback (&WifiNetDevice::LinkDown, this));
mathieu@2601
   107
    }
mathieu@2524
   108
}
mathieu@2620
   109
void 
mathieu@2620
   110
WifiNetDevice::SetPhy (Ptr<WifiPhy> phy)
mathieu@2620
   111
{
mathieu@2620
   112
  m_phy = phy;
mathieu@2620
   113
  if (m_phy != 0)
mathieu@2620
   114
    {
mathieu@2620
   115
      if (m_channel != 0)
mathieu@2620
   116
        {
mathieu@2620
   117
          m_channel->Add (this, m_phy);
mathieu@2620
   118
          m_phy->SetChannel (m_channel);
mathieu@2620
   119
        }
mathieu@2620
   120
      if (m_stationManager != 0)
mathieu@2620
   121
        {
mathieu@2620
   122
          m_stationManager->SetupPhy (m_phy);
mathieu@2620
   123
        }
mathieu@2620
   124
      if (m_mac != 0)
mathieu@2620
   125
        {
mathieu@2620
   126
          m_mac->SetWifiPhy (m_phy);
mathieu@2620
   127
        }
mathieu@2620
   128
    }
mathieu@2620
   129
}
mathieu@2620
   130
void 
mathieu@2620
   131
WifiNetDevice::SetRemoteStationManager (Ptr<WifiRemoteStationManager> manager)
mathieu@2620
   132
{
mathieu@2620
   133
  m_stationManager = manager;
mathieu@2620
   134
  if (m_stationManager != 0)
mathieu@2620
   135
    {
mathieu@2620
   136
      if (m_phy != 0)
mathieu@2620
   137
        {
mathieu@2620
   138
          m_stationManager->SetupPhy (m_phy);
mathieu@2620
   139
        }
mathieu@2620
   140
      if (m_mac != 0)
mathieu@2620
   141
        {
mathieu@2620
   142
          m_mac->SetWifiRemoteStationManager (m_stationManager);
mathieu@2620
   143
        }
mathieu@2620
   144
    }
mathieu@2620
   145
}
mathieu@2620
   146
void 
mathieu@2620
   147
WifiNetDevice::SetChannel (Ptr<WifiChannel> channel)
mathieu@2620
   148
{
mathieu@2620
   149
  m_channel = channel;
mathieu@2620
   150
  if (m_channel != 0 && m_phy != 0)
mathieu@2620
   151
    {
mathieu@2620
   152
      m_channel->Add (this, m_phy);
mathieu@2620
   153
      m_phy->SetChannel (m_channel);
mathieu@2620
   154
    }
mathieu@2620
   155
}
mathieu@2524
   156
Ptr<WifiMac> 
mathieu@2524
   157
WifiNetDevice::GetMac (void) const
mathieu@1964
   158
{
mathieu@2524
   159
  return m_mac;
mathieu@1964
   160
}
mathieu@2524
   161
Ptr<WifiPhy> 
mathieu@2524
   162
WifiNetDevice::GetPhy (void) const
mathieu@1964
   163
{
mathieu@2524
   164
  return m_phy;
mathieu@1964
   165
}
mathieu@2524
   166
Ptr<WifiRemoteStationManager> 
mathieu@2524
   167
WifiNetDevice::GetRemoteStationManager (void) const
mathieu@2053
   168
{
mathieu@2524
   169
  return m_stationManager;
mathieu@2470
   170
}
mathieu@2470
   171
mathieu@2470
   172
void 
mathieu@2470
   173
WifiNetDevice::SetName(const std::string name)
mathieu@2470
   174
{
mathieu@2470
   175
  m_name = name;
mathieu@2470
   176
}
mathieu@2470
   177
std::string 
mathieu@2470
   178
WifiNetDevice::GetName(void) const
mathieu@2470
   179
{
mathieu@2470
   180
  return m_name;
mathieu@2470
   181
}
mathieu@2470
   182
void 
mathieu@2470
   183
WifiNetDevice::SetIfIndex(const uint32_t index)
mathieu@2470
   184
{
mathieu@2470
   185
  m_ifIndex = index;
mathieu@2470
   186
}
mathieu@2470
   187
uint32_t 
mathieu@2470
   188
WifiNetDevice::GetIfIndex(void) const
mathieu@2470
   189
{
mathieu@2470
   190
  return m_ifIndex;
mathieu@2470
   191
}
mathieu@2470
   192
Ptr<Channel> 
mathieu@2470
   193
WifiNetDevice::GetChannel (void) const
mathieu@2470
   194
{
mathieu@2601
   195
  return m_channel;
mathieu@2470
   196
}
mathieu@2620
   197
Ptr<WifiChannel> 
mathieu@2620
   198
WifiNetDevice::DoGetChannel (void) const
mathieu@2620
   199
{
mathieu@2620
   200
  return m_channel;
mathieu@2620
   201
}
mathieu@2470
   202
Address 
mathieu@2470
   203
WifiNetDevice::GetAddress (void) const
mathieu@2470
   204
{
mathieu@2524
   205
  return m_mac->GetAddress ();
mathieu@2470
   206
}
mathieu@2470
   207
bool 
mathieu@2470
   208
WifiNetDevice::SetMtu (const uint16_t mtu)
mathieu@2470
   209
{
mathieu@2965
   210
  UintegerValue maxMsduSize;
mathieu@2965
   211
  m_mac->GetAttribute ("MaxMsduSize", maxMsduSize);
mathieu@2965
   212
  if (mtu > maxMsduSize.Get () || mtu == 0)
mathieu@2524
   213
    {
mathieu@2524
   214
      return false;
mathieu@2524
   215
    }
mathieu@2470
   216
  m_mtu = mtu;
mathieu@2470
   217
  return true;
mathieu@2470
   218
}
mathieu@2470
   219
uint16_t 
mathieu@2470
   220
WifiNetDevice::GetMtu (void) const
mathieu@2470
   221
{
mathieu@2524
   222
  if (m_mtu == 0)
mathieu@2524
   223
    {
mathieu@2965
   224
      UintegerValue maxMsduSize;
mathieu@2965
   225
      m_mac->GetAttribute ("MaxMsduSize", maxMsduSize);
mathieu@2965
   226
      m_mtu = maxMsduSize.Get ();
mathieu@2524
   227
    }
mathieu@2470
   228
  return m_mtu;
mathieu@2470
   229
}
mathieu@2470
   230
bool 
mathieu@2470
   231
WifiNetDevice::IsLinkUp (void) const
mathieu@2470
   232
{
mathieu@2524
   233
  return m_phy != 0 && m_linkUp;
mathieu@2470
   234
}
mathieu@2470
   235
void 
mathieu@2470
   236
WifiNetDevice::SetLinkChangeCallback (Callback<void> callback)
mathieu@2470
   237
{
mathieu@2524
   238
  m_linkChange = callback;
mathieu@2470
   239
}
mathieu@2470
   240
bool 
mathieu@2470
   241
WifiNetDevice::IsBroadcast (void) const
mathieu@2470
   242
{
mathieu@2470
   243
  return true;
mathieu@2470
   244
}
mathieu@2524
   245
Address 
mathieu@2470
   246
WifiNetDevice::GetBroadcast (void) const
mathieu@2470
   247
{
mathieu@2524
   248
  return Mac48Address::GetBroadcast ();
mathieu@2470
   249
}
mathieu@2470
   250
bool 
mathieu@2470
   251
WifiNetDevice::IsMulticast (void) const
mathieu@2470
   252
{
mathieu@2470
   253
  return false;
mathieu@2470
   254
}
mathieu@2470
   255
Address 
craigdo@3841
   256
WifiNetDevice::GetMulticast (Ipv4Address multicastGroup) const
mathieu@2470
   257
{
mathieu@3549
   258
  return Mac48Address::GetMulticast (multicastGroup);
mathieu@2470
   259
}
vincent@3852
   260
Address WifiNetDevice::GetMulticast (Ipv6Address addr) const
vincent@3852
   261
{
vincent@3852
   262
  return Mac48Address::GetMulticast (addr);
vincent@3852
   263
}
mathieu@2470
   264
bool 
mathieu@2470
   265
WifiNetDevice::IsPointToPoint (void) const
mathieu@2470
   266
{
mathieu@2470
   267
  return false;
mathieu@2470
   268
}
mathieu@2470
   269
bool 
mathieu@2524
   270
WifiNetDevice::Send(Ptr<Packet> packet, const Address& dest, uint16_t protocolNumber)
mathieu@2470
   271
{
mathieu@2524
   272
  NS_ASSERT (Mac48Address::IsMatchingType (dest));
mathieu@2470
   273
mathieu@2524
   274
  Mac48Address realTo = Mac48Address::ConvertFrom (dest);
mathieu@2470
   275
mathieu@2470
   276
  LlcSnapHeader llc;
mathieu@2470
   277
  llc.SetType (protocolNumber);
mathieu@2470
   278
  packet->AddHeader (llc);
mathieu@2470
   279
mathieu@2470
   280
  m_txLogger (packet, realTo);
mathieu@2470
   281
mathieu@3602
   282
  m_mac->Enqueue (packet, realTo);
mathieu@2524
   283
  return true;
mathieu@2470
   284
}
mathieu@2470
   285
Ptr<Node> 
mathieu@2470
   286
WifiNetDevice::GetNode (void) const
mathieu@2470
   287
{
mathieu@2470
   288
  return m_node;
mathieu@2470
   289
}
mathieu@2600
   290
void 
mathieu@2600
   291
WifiNetDevice::SetNode (Ptr<Node> node)
mathieu@2600
   292
{
mathieu@2600
   293
  m_node = node;
mathieu@2600
   294
}
mathieu@2470
   295
bool 
mathieu@2470
   296
WifiNetDevice::NeedsArp (void) const
mathieu@2470
   297
{
mathieu@2470
   298
  return true;
mathieu@2470
   299
}
mathieu@2470
   300
void 
mathieu@2470
   301
WifiNetDevice::SetReceiveCallback (NetDevice::ReceiveCallback cb)
mathieu@2470
   302
{
mathieu@2524
   303
  m_forwardUp = cb;
mathieu@1998
   304
}
mathieu@1964
   305
mathieu@2524
   306
void
mathieu@3604
   307
WifiNetDevice::ForwardUp (Ptr<Packet> packet, Mac48Address from, Mac48Address to)
mathieu@2524
   308
{
mathieu@2649
   309
  m_rxLogger (packet, from);
mathieu@2524
   310
  LlcSnapHeader llc;
mathieu@2524
   311
  packet->RemoveHeader (llc);
mathieu@3604
   312
  enum NetDevice::PacketType type;
mathieu@3604
   313
  if (to.IsBroadcast ())
mathieu@3604
   314
    {
mathieu@3604
   315
      type = NetDevice::PACKET_BROADCAST;
mathieu@3604
   316
    }
mathieu@3604
   317
  else if (to.IsMulticast ())
mathieu@3604
   318
    {
mathieu@3604
   319
      type = NetDevice::PACKET_MULTICAST;
mathieu@3604
   320
    }
mathieu@3604
   321
  else if (to == m_mac->GetAddress ())
mathieu@3604
   322
    {
mathieu@3604
   323
      type = NetDevice::PACKET_HOST;
mathieu@3604
   324
    }
mathieu@3604
   325
  else
mathieu@3604
   326
    {
mathieu@3604
   327
      type = NetDevice::PACKET_OTHERHOST;
mathieu@3604
   328
    }
mathieu@3604
   329
  if (type != NetDevice::PACKET_OTHERHOST)
mathieu@3604
   330
    {
mathieu@3604
   331
      m_forwardUp (this, packet, llc.GetType (), from);
mathieu@3604
   332
    }
mathieu@3604
   333
  if (!m_promiscRx.IsNull ())
mathieu@3604
   334
    {
mathieu@3604
   335
      m_promiscRx (this, packet, llc.GetType (), from, to, type);
mathieu@3604
   336
    }
mathieu@2524
   337
}
mathieu@1956
   338
mathieu@2524
   339
void
mathieu@2524
   340
WifiNetDevice::LinkUp (void)
mathieu@2151
   341
{
mathieu@2524
   342
  m_linkUp = true;
mathieu@2524
   343
  if (!m_linkChange.IsNull ())
mathieu@2524
   344
    {
mathieu@2524
   345
      m_linkChange ();
mathieu@2524
   346
    }
mathieu@2151
   347
}
mathieu@2151
   348
void
mathieu@2524
   349
WifiNetDevice::LinkDown (void)
mathieu@2151
   350
{
mathieu@2524
   351
  m_linkUp = false;
mathieu@2524
   352
  if (!m_linkChange.IsNull ())
mathieu@1964
   353
    {
mathieu@2524
   354
      m_linkChange ();
mathieu@2524
   355
    }
mathieu@2350
   356
}
mathieu@1956
   357
gjc@3480
   358
bool
gjc@3480
   359
WifiNetDevice::SendFrom (Ptr<Packet> packet, const Address& source, const Address& dest, uint16_t protocolNumber)
gjc@3480
   360
{
mathieu@3517
   361
  NS_ASSERT (Mac48Address::IsMatchingType (dest));
mathieu@3517
   362
  NS_ASSERT (Mac48Address::IsMatchingType (source));
mathieu@3517
   363
mathieu@3517
   364
  Mac48Address realTo = Mac48Address::ConvertFrom (dest);
mathieu@3517
   365
  Mac48Address realFrom = Mac48Address::ConvertFrom (source);
mathieu@3517
   366
mathieu@3517
   367
  LlcSnapHeader llc;
mathieu@3517
   368
  llc.SetType (protocolNumber);
mathieu@3517
   369
  packet->AddHeader (llc);
mathieu@3517
   370
mathieu@3517
   371
  m_txLogger (packet, realTo);
mathieu@3517
   372
mathieu@3517
   373
  m_mac->Enqueue (packet, realTo, realFrom);
mathieu@3517
   374
mathieu@3517
   375
  return true;
gjc@3480
   376
}
gjc@3480
   377
gjc@3480
   378
void
gjc@3480
   379
WifiNetDevice::SetPromiscReceiveCallback (PromiscReceiveCallback cb)
gjc@3480
   380
{
mathieu@3604
   381
  m_promiscRx = cb;
gjc@3480
   382
}
gjc@3480
   383
gjc@3480
   384
bool
mathieu@3584
   385
WifiNetDevice::SupportsSendFrom (void) const
gjc@3480
   386
{
mathieu@3603
   387
  return m_mac->SupportsSendFrom ();
gjc@3480
   388
}
gjc@3480
   389
mathieu@1956
   390
} // namespace ns3
mathieu@1956
   391