ns-3 PLC model
|
00001 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ 00002 /* 00003 * Copyright (c) 2012 University of British Columbia, Vancouver 00004 * 00005 * This program is free software; you can redistribute it and/or modify 00006 * it under the terms of the GNU General Public License version 2 as 00007 * published by the Free Software Foundation; 00008 * 00009 * This program is distributed in the hope that it will be useful, 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 * GNU General Public License for more details. 00013 * 00014 * You should have received a copy of the GNU General Public License 00015 * along with this program; if not, write to the Free Software 00016 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00017 * 00018 * Author: Alexander Schloegl <alexander.schloegl@gmx.de> 00019 */ 00020 00021 #ifndef PLC_MAC_H_ 00022 #define PLC_MAC_H_ 00023 00024 #include <set> 00025 #include <map> 00026 #include <deque> 00027 #include <ns3/object.h> 00028 #include <ns3/callback.h> 00029 #include <ns3/packet.h> 00030 #include <ns3/event-id.h> 00031 #include "plc-phy.h" 00032 #include "plc-header.h" 00033 00034 namespace ns3 { 00035 00036 typedef enum 00037 { 00038 CHANNEL_IDLE, 00039 CHANNEL_ACCESS_FAILURE 00040 } PLC_CsmaCaState; 00041 00042 typedef Callback<void, Ptr<Packet> , Mac48Address, Mac48Address > PLC_MacDataCallback; 00043 typedef Callback<void> PLC_MacAcknowledgementCallback; 00044 typedef Callback<void> PLC_MacTransmissionFailedCallback; 00045 typedef Callback< void > PLC_CcaRequestCallback; // This method informs MAC that channel is idle or busy 00046 00053 class PLC_Mac : public Object 00054 { 00055 public: 00056 static TypeId GetTypeId (void); 00057 00058 PLC_Mac (void); 00059 ~PLC_Mac (void) = 0; 00060 00061 void SetCcaRequestCallback (PLC_CcaRequestCallback c); 00062 void RequestCca (); 00063 void StartCsmaCa (void); 00064 void RandomBackoffDelay (); 00065 void CcaConfirm (PLC_PhyCcaResult status); 00066 void CsmaCaConfirm (PLC_CsmaCaState state); 00067 virtual void NotifyCcaConfirm (PLC_PhyCcaResult status) = 0; 00068 virtual void NotifyCsmaCaConfirm (PLC_CsmaCaState state) = 0; 00069 00073 void SetMacDataCallback (PLC_MacDataCallback c); 00074 00078 void SetTransmissionFailedCallback (PLC_MacTransmissionFailedCallback c); 00079 00083 void SetPromiscuousMacDataCallback (PLC_MacDataCallback c); 00084 00089 void SetMacAcknowledgementCallback (PLC_MacAcknowledgementCallback c); 00090 00096 void SetAddress (Mac48Address addr); 00097 00101 Mac48Address GetAddress (void); 00102 00108 void SetBroadcastAddress (Mac48Address addr); 00109 00113 Mac48Address GetBroadcastAddress (void); 00114 00120 void SetMulticastAddress (Mac48Address addr); 00121 00125 Mac48Address GetMulticastAddress (void); 00126 00133 bool Send (Ptr<Packet> p, Mac48Address dst); 00134 00142 bool SendFrom (Ptr<Packet> p, Mac48Address src, Mac48Address dst); 00143 00144 void SetPhy (Ptr<PLC_Phy> phy); 00145 Ptr<PLC_Phy> GetPhy (void); 00146 00151 virtual void NotifyTransmissionEnd (void) = 0; 00152 00158 void NotifyReceptionEndOk (Ptr<const Packet> p); 00159 00160 protected: 00161 // static MAC list needed for opportunistic relaying 00162 static std::map<Mac48Address, Ptr<PLC_Mac> > mac_list; 00163 00164 virtual void DoStart (void); 00165 virtual void DoDispose (void); 00166 virtual bool DoSendFrom (Ptr<Packet> p, Mac48Address src, Mac48Address dst) = 0; 00167 virtual void DoProcess (Ptr<const Packet> p) = 0; 00168 virtual void DoSetPhy (Ptr<PLC_Phy> phy) = 0; 00169 virtual Ptr<PLC_Phy> DoGetPhy (void) = 0; 00170 00171 Mac48Address m_address; 00172 Mac48Address m_broadcast_address; 00173 Mac48Address m_multicast_address; 00174 00175 // according to unslotted CSMA/CA of IEEE 802.15.4 00176 bool m_csmaca_active; 00177 bool m_promiscuous_mode; 00178 uint32_t m_csmaca_attempts; 00179 uint8_t m_NB; // number of backoffs for the current transmission 00180 uint8_t m_BE; // backoff exponent 00181 uint8_t m_macMinBE; // 0-macMaxBE default 3 00182 uint8_t m_macMaxBE; // 3-8 default 5 00183 uint8_t m_macMaxCSMABackoffs; // 0-5 default 4 00184 uint64_t m_aUnitBackoffPeriod; // 20 symbols in each backoff periods 00185 EventId m_requestCCAEvent; 00186 EventId m_backoffEndEvent; 00187 00188 PLC_MacDataCallback m_data_callback; 00189 PLC_MacTransmissionFailedCallback m_transmission_failed_callback; 00190 PLC_MacDataCallback m_promiscuous_data_callback; 00191 PLC_MacAcknowledgementCallback m_acknowledgement_callback; 00192 PLC_CcaRequestCallback m_cca_request_callback; 00193 }; 00194 00195 class PLC_ArqMac : public PLC_Mac 00196 { 00197 public: 00198 static TypeId GetTypeId (void); 00199 00200 PLC_ArqMac (void); 00201 00202 void SetTimeout (Time timeout) { m_timeout = timeout; } 00203 Time GetTimeout (void) { return m_timeout; } 00204 00205 void SetMaxReplays (size_t max_replays) { m_max_replays = max_replays; } 00206 size_t GetMaxReplays (void) { return m_max_replays; } 00207 00208 virtual void NotifyCcaConfirm (PLC_PhyCcaResult status); 00209 virtual void NotifyCsmaCaConfirm (PLC_CsmaCaState state); 00210 virtual void NotifyTransmissionEnd (void); 00211 00212 void AcknowledgementTimeout (void); 00213 00214 private: 00215 virtual void DoStart (void); 00216 virtual void DoDispose (void); 00217 virtual bool DoSendFrom (Ptr<Packet> p, Mac48Address src, Mac48Address dst); 00218 virtual void DoProcess (Ptr<const Packet> p); 00219 virtual void DoSetPhy (Ptr<PLC_Phy> phy); 00220 virtual Ptr<PLC_Phy> DoGetPhy (void); 00221 00222 Ptr<PLC_HalfDuplexOfdmPhy> m_phy; 00223 00224 Ptr<Packet> m_txPacket; 00225 Ptr<Packet> m_rxPacket; 00226 PLC_MacHeader m_txHeader; 00227 PLC_MacHeader m_rxHeader; 00228 uint16_t m_sequence_number; 00229 00230 bool m_awaiting_ack; 00231 Time m_timeout; 00232 EventId m_timeoutEvent; 00233 size_t m_max_replays; 00234 size_t m_replays; 00235 }; 00236 00237 // TODO: PLC_HarqMac, PLC_Relay 00238 00239 } 00240 00241 #endif /* PLC_MAC_H_ */