diff --git a/samples/main-ap-wifi.cc b/samples/main-ap-wifi.cc index 78187a797..42ee59af0 100644 --- a/samples/main-ap-wifi.cc +++ b/samples/main-ap-wifi.cc @@ -32,12 +32,19 @@ #include "ns3/inet-socket-address.h" #include "ns3/global-route-manager.h" #include "ns3/packet.h" +#include "ns3/node-list.h" #include using namespace ns3; +static void +WifiNetDeviceTrace (const TraceContext &context, Packet p, Mac48Address address) +{ + std::cout << context << " ad=" << address << " p: " << p << std::endl; +} + static Ptr CreateApNode (Ptr channel, Position position, @@ -157,6 +164,8 @@ int main (int argc, char *argv[]) GlobalRouteManager::PopulateRoutingTables (); + NodeList::Connect ("/nodes/*/devices/*/*", MakeCallback (&WifiNetDeviceTrace)); + Simulator::Run (); Simulator::Destroy (); diff --git a/src/devices/wifi/wifi-net-device.cc b/src/devices/wifi/wifi-net-device.cc index af7728507..42627a449 100644 --- a/src/devices/wifi/wifi-net-device.cc +++ b/src/devices/wifi/wifi-net-device.cc @@ -20,6 +20,7 @@ #include "ns3/packet.h" #include "ns3/llc-snap-header.h" #include "ns3/node.h" +#include "ns3/composite-trace-resolver.h" #include "wifi-net-device.h" #include "wifi-phy.h" @@ -41,6 +42,44 @@ namespace ns3 { +WifiNetDeviceTraceType::WifiNetDeviceTraceType () + : m_type (RX) +{} + +WifiNetDeviceTraceType::WifiNetDeviceTraceType (enum Type type) + : m_type (type) +{} +enum WifiNetDeviceTraceType::Type +WifiNetDeviceTraceType::Get (void) const +{ + return m_type; +} +uint16_t +WifiNetDeviceTraceType::GetUid (void) +{ + static uint16_t uid = AllocateUid ("ns3::WifiNetDeviceTraceType"); + return uid; +} +void +WifiNetDeviceTraceType::Print (std::ostream &os) const +{ + os << "event="; + switch (m_type) { + case RX: + os << "rx"; + break; + case TX: + os << "tx"; + break; + } +} +std::string +WifiNetDeviceTraceType::GetTypeName (void) const +{ + return "ns3::WifiNetDeviceTraceType"; +} + + static WifiMode GetWifiModeForPhyMode (WifiPhy *phy, enum WifiDefaultParameters::PhyModeParameter mode) { @@ -165,6 +204,27 @@ WifiNetDevice::CreateDca (uint32_t minCw, uint32_t maxCw) const return dca; } +Ptr +WifiNetDevice::GetTraceResolver (void) const +{ + Ptr resolver = + Create (); + resolver->AddSource ("rx", + TraceDoc ("Receive a packet", + "Packet", "the packet received", + "Mac48Address", "the sender of the packet"), + m_rxLogger, + WifiNetDeviceTraceType (WifiNetDeviceTraceType::RX)); + resolver->AddSource ("tx", + TraceDoc ("Send a packet", + "Packet", "the packet to send", + "Mac48Address", "the destination of the packet"), + m_txLogger, + WifiNetDeviceTraceType (WifiNetDeviceTraceType::TX)); + resolver->SetParentResolver (NetDevice::GetTraceResolver ()); + return resolver; +} + void WifiNetDevice::ConnectTo (Ptr channel) { diff --git a/src/devices/wifi/wifi-net-device.h b/src/devices/wifi/wifi-net-device.h index 6e420e3c2..3b5678b1a 100644 --- a/src/devices/wifi/wifi-net-device.h +++ b/src/devices/wifi/wifi-net-device.h @@ -42,6 +42,31 @@ class MacHighAdhoc; class MacHighNqsta; class MacHighNqap; +/** + * \brief hold the type of trace event generated by + * a WifiNetDevice. + */ +class WifiNetDeviceTraceType : public TraceContextElement +{ +public: + enum Type { + RX, + TX + }; + WifiNetDeviceTraceType (); + WifiNetDeviceTraceType (enum Type type); + /** + * \returns the type of event + */ + enum Type Get (void) const; + + static uint16_t GetUid (void); + void Print (std::ostream &os) const; + std::string GetTypeName (void) const; +private: + enum Type m_type; +}; + /** * \brief the base class for 802.11 network interfaces * @@ -77,6 +102,7 @@ private: virtual bool DoNeedsArp (void) const; virtual Ptr DoGetChannel (void) const; virtual bool SendTo (const Packet &packet, const Address &to, uint16_t protocolNumber); + virtual Ptr GetTraceResolver (void) const; // defined for children virtual void NotifyConnected (void) = 0; virtual bool DoSendTo (const Packet &packet, const Mac48Address &to) = 0;