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 #include "ssid.h" 00021 #include "ns3/assert.h" 00022 00023 #define ELEMENT_ID (0) 00024 00025 namespace ns3 { 00026 00027 Ssid::Ssid () 00028 { 00029 m_length = 0; 00030 for (uint8_t i = 0; i < 33; i++) 00031 { 00032 m_ssid[i] = 0; 00033 } 00034 } 00035 Ssid::Ssid (std::string s) 00036 { 00037 NS_ASSERT (s.size () < 32); 00038 const char *ssid = s.c_str (); 00039 uint8_t len = 0; 00040 while (*ssid != 0 && len < 32) 00041 { 00042 m_ssid[len] = *ssid; 00043 ssid++; 00044 len++; 00045 } 00046 NS_ASSERT (len <= 32); 00047 m_length = len; 00048 while (len < 33) 00049 { 00050 m_ssid[len] = 0; 00051 len++; 00052 } 00053 } 00054 Ssid::Ssid (char const ssid[32], uint8_t length) 00055 { 00056 NS_ASSERT (length <= 32); 00057 uint8_t len = 0; 00058 while (len < length) 00059 { 00060 m_ssid[len] = ssid[len]; 00061 len++; 00062 } 00063 m_length = length; 00064 while (len < 33) 00065 { 00066 m_ssid[len] = 0; 00067 len++; 00068 } 00069 } 00070 bool 00071 Ssid::IsEqual (Ssid const &o) const 00072 { 00073 uint8_t i = 0; 00074 while (i < 32 && 00075 m_ssid[i] == o.m_ssid[i] && 00076 m_ssid[i] != 0) 00077 { 00078 i++; 00079 } 00080 if (m_ssid[i] != o.m_ssid[i]) 00081 { 00082 return false; 00083 } 00084 return true; 00085 } 00086 bool 00087 Ssid::IsBroadcast (void) const 00088 { 00089 if (m_ssid[0] == 0) 00090 { 00091 return true; 00092 } 00093 return false; 00094 } 00095 uint32_t 00096 Ssid::GetLength (void) const 00097 { 00098 uint8_t size = 0; 00099 while (m_ssid[size] != 0 && size < 32) 00100 { 00101 size++; 00102 } 00103 NS_ASSERT (size <= 32); 00104 return size; 00105 } 00106 00107 char * 00108 Ssid::PeekString (void) const 00109 { 00110 // it is safe to return a pointer to the buffer because it is 00111 // guaranteed to be zero-terminated. 00112 return (char *)m_ssid; 00113 } 00114 00115 uint32_t 00116 Ssid::GetSerializedSize (void) const 00117 { 00118 return 1 + 1 + m_length; 00119 } 00120 Buffer::Iterator 00121 Ssid::Serialize (Buffer::Iterator i) const 00122 { 00123 NS_ASSERT (m_length <= 32); 00124 i.WriteU8 (ELEMENT_ID); 00125 i.WriteU8 (m_length); 00126 i.Write (m_ssid, m_length); 00127 return i; 00128 } 00129 Buffer::Iterator 00130 Ssid::Deserialize (Buffer::Iterator i) 00131 { 00132 uint8_t elementId; 00133 elementId = i.ReadU8 (); 00134 NS_ASSERT (elementId == ELEMENT_ID); 00135 m_length = i.ReadU8 (); 00136 NS_ASSERT (m_length <= 32); 00137 i.Read (m_ssid, m_length); 00138 return i; 00139 } 00140 00141 ATTRIBUTE_HELPER_CPP (Ssid); 00142 00143 std::ostream & 00144 operator << (std::ostream &os, const Ssid &ssid) 00145 { 00146 os << ssid.PeekString (); 00147 return os; 00148 } 00149 00150 std::istream &operator >> (std::istream &is, Ssid &ssid) 00151 { 00152 std::string str; 00153 is >> str; 00154 ssid = Ssid (str.c_str ()); 00155 return is; 00156 } 00157 00158 00159 } // namespace ns3