00001 /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ 00002 /* 00003 * Copyright (c) 2006 INRIA 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: Mathieu Lacage <mathieu.lacage@sophia.inria.fr> 00019 */ 00020 00021 #include "supported-rates.h" 00022 #include "ns3/assert.h" 00023 #include "ns3/log.h" 00024 00025 NS_LOG_COMPONENT_DEFINE ("SupportedRates"); 00026 00027 #define ELEMENT_ID (1) 00028 00029 namespace ns3 { 00030 00031 SupportedRates::SupportedRates () 00032 : m_nRates (0) 00033 {} 00034 00035 void 00036 SupportedRates::AddSupportedRate (uint32_t bs) 00037 { 00038 NS_ASSERT (m_nRates < 8); 00039 if (IsSupportedRate (bs)) 00040 { 00041 return; 00042 } 00043 m_rates[m_nRates] = bs/500000; 00044 m_nRates++; 00045 NS_LOG_DEBUG ("add rate="<<bs<<", n rates="<<(uint32_t)m_nRates); 00046 } 00047 void 00048 SupportedRates::SetBasicRate (uint32_t bs) 00049 { 00050 uint8_t rate = bs / 500000; 00051 for (uint8_t i = 0; i < m_nRates; i++) 00052 { 00053 if ((rate | 0x80) == m_rates[i]) 00054 { 00055 return; 00056 } 00057 if (rate == m_rates[i]) 00058 { 00059 NS_LOG_DEBUG ("set basic rate="<<bs<<", n rates="<<(uint32_t)m_nRates); 00060 m_rates[i] |= 0x80; 00061 return; 00062 } 00063 } 00064 AddSupportedRate (bs); 00065 SetBasicRate (bs); 00066 } 00067 bool 00068 SupportedRates::IsBasicRate (uint32_t bs) const 00069 { 00070 uint8_t rate = (bs / 500000) | 0x80; 00071 for (uint8_t i = 0; i < m_nRates; i++) 00072 { 00073 if (rate == m_rates[i]) 00074 { 00075 return true; 00076 } 00077 } 00078 return false; 00079 } 00080 bool 00081 SupportedRates::IsSupportedRate (uint32_t bs) const 00082 { 00083 uint8_t rate = bs / 500000; 00084 for (uint8_t i = 0; i < m_nRates; i++) 00085 { 00086 if (rate == m_rates[i] || 00087 (rate|0x80) == m_rates[i]) 00088 { 00089 return true; 00090 } 00091 } 00092 return false; 00093 } 00094 uint8_t 00095 SupportedRates::GetNRates (void) const 00096 { 00097 return m_nRates; 00098 } 00099 uint32_t 00100 SupportedRates::GetRate (uint8_t i) const 00101 { 00102 return (m_rates[i]&0x7f) * 500000; 00103 } 00104 uint32_t 00105 SupportedRates::GetSerializedSize (void) const 00106 { 00107 return m_nRates + 1 + 1; 00108 } 00109 Buffer::Iterator 00110 SupportedRates::Serialize (Buffer::Iterator start) const 00111 { 00112 start.WriteU8 (ELEMENT_ID); 00113 start.WriteU8 (m_nRates); 00114 start.Write (m_rates, m_nRates); 00115 return start; 00116 } 00117 Buffer::Iterator 00118 SupportedRates::Deserialize (Buffer::Iterator start) 00119 { 00120 uint8_t elementId; 00121 elementId = start.ReadU8 (); 00122 NS_ASSERT (elementId == ELEMENT_ID); 00123 m_nRates = start.ReadU8 (); 00124 NS_ASSERT (m_nRates <= 8); 00125 start.Read (m_rates, m_nRates); 00126 return start; 00127 } 00128 00129 std::ostream &operator << (std::ostream &os, const SupportedRates &rates) 00130 { 00131 os << "["; 00132 for (uint8_t i = 0; i < rates.GetNRates (); i++) 00133 { 00134 uint32_t rate = rates.GetRate (i); 00135 if (rates.IsBasicRate (rate)) 00136 { 00137 os << "*"; 00138 } 00139 os << rate << "mbs"; 00140 if (i < rates.GetNRates () - 1) 00141 { 00142 os << " "; 00143 } 00144 } 00145 os << "]"; 00146 return os; 00147 } 00148 00149 } // namespace ns3