ns-3 PLC model
|
00001 /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ 00002 /* 00003 * Copyright (c) 2011 The Boeing Company 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: kwong yin <kwong-sang.yin@boeing.com> 00019 */ 00020 00021 #ifndef PLC_CSMACA_H 00022 #define PLC_CSMACA_H 00023 00024 #include <stdint.h> 00025 #include "ns3/object.h" 00026 #include "ns3/nstime.h" 00027 #include "ns3/random-variable.h" 00028 #include "ns3/packet.h" 00029 #include "ns3/plc-phy.h" 00030 00031 namespace ns3 { 00032 00033 typedef enum 00034 { 00035 CHANNEL_IDLE, 00036 CHANNEL_ACCESS_FAILURE 00037 } PLC_CsmaCaState; 00038 00039 /* This method informs MAC that channel is idle or busy */ 00040 typedef Callback< void > PLC_CsmaCaCcaRequestCallback; 00041 typedef Callback< void > PLC_CsmaCaCcaCancelCallback; 00042 typedef Callback< void, PLC_CsmaCaState> PLC_CsmaCaMacCallback; 00043 00044 class PLC_Mac; 00045 00052 class PLC_CsmaCa : public Object 00053 { 00054 00055 public: 00056 static TypeId GetTypeId (); 00057 00058 PLC_CsmaCa (); 00059 ~PLC_CsmaCa (); 00060 00061 void setSlottedCsmaCa (void); 00062 void setUnSlottedCsmaCa (void); 00063 bool isSlottedCsmaCa (void) const; 00064 bool isUnSlottedCsmaCa (void) const; 00065 00066 void setMacMinBE (uint8_t macMinBE); 00067 uint8_t getMacMinBE (void) const; 00068 void setMacMaxBE (uint8_t macMaxBE); 00069 uint8_t getMacMaxBE (void) const; 00070 void setmacMaxCSMABackoffs (uint8_t macMaxCSMABackoffs); 00071 uint8_t getmacMaxCSMABackoffs (void) const; 00072 00073 void setUnitBackoffPeriod (uint64_t unitBackoffPeriod); 00074 uint64_t getUnitBackoffPeriod (void) const; 00075 00076 /* 00077 * Amount of time from now to the beginning of the next slot 00078 */ 00079 uint64_t getTimeToNextSlot (void) const; 00080 00081 /* 00082 * Start CSMA-CA algorithm (step 1), initialize NB, BE for both slotted and unslotted 00083 * CSMA-CA. For the slotted intialize CW plus also start on the backoff boundary 00084 */ 00085 void Start (void); 00086 00087 /* 00088 * cancel CSMA-CA algorithm 00089 */ 00090 void Cancel (); 00091 00092 /* 00093 * In step 2 of the CSMA-CA, perform a random backoff in the range of 0 to 2^BE -1 00094 */ 00095 void RandomBackoffDelay (); 00096 00097 /* 00098 * In the slotted CSMA-CA, after random backoff, Determine if the remaining 00099 * CSMA-CA operation can proceed ie. Can the entire transactions can be transmitted 00100 * before the end of the CAP. This step is performed between step 2 and 3. 00101 * This step is NOT performed for the unslotted CSMA-CA. If it can proceed 00102 * function CCAconfirmed() is called. 00103 */ 00104 void CanProceed (); 00105 00106 /* 00107 * Request the Phy to perform CCA (Step 3) 00108 * 00109 */ 00110 void RequestCCA (); 00111 00123 void CcaConfirm (PLC_PhyCcaResult status); 00124 00125 void SetCcaRequestCallback (PLC_CsmaCaCcaRequestCallback c); 00126 00127 void SetCcaCancelCallback (PLC_CsmaCaCcaCancelCallback c); 00128 00134 void SetCsmaCaMacCallback (PLC_CsmaCaMacCallback c); 00135 00136 bool IsActive(void) { return m_active; } 00137 00138 private: 00139 virtual void DoDispose (); 00140 PLC_CsmaCaCcaRequestCallback m_csmaCaCcaRequestCallback; 00141 PLC_CsmaCaCcaCancelCallback m_csmaCaCcaCancelCallback; 00142 PLC_CsmaCaMacCallback m_csmaCaMacCallback; 00143 00144 bool m_isSlotted; // beacon-enabled slotted or nonbeacon-enabled unslotted CSMA-CA 00145 // beacon order == 15 means nonbeacon-enabled 00146 00147 uint8_t m_NB; // number of backoffs for the current transmission 00148 uint8_t m_CW; // contention window length (used in slotted ver only) 00149 uint8_t m_BE; // backoff exponent 00150 bool m_BLE; // Battery Life Extension 00151 00152 uint8_t m_macMinBE; //0-macMaxBE default 3 00153 uint8_t m_macMaxBE; //3-8 default 5 00154 uint8_t m_macMaxCSMABackoffs; //0-5 default 4 00155 uint64_t m_aUnitBackoffPeriod; // 20 symbols in each backoff periods 00156 00157 bool m_active; 00158 00159 EventId m_requestCCAEvent; 00160 EventId m_backoffEndEvent; 00161 EventId m_canProceedEvent; 00162 }; 00163 00164 } 00165 00166 // namespace ns-3 00167 00168 #endif /* PLC_CSMACA_H */