/// <summary>
 ///     Sets the SSID with a string
 /// </summary>
 /// <param name="ssidTag">The SSID tag on which to set the value</param>
 /// <param name="value">The value to set the SSID</param>
 public static void SetSSID(this SSIDTag ssidTag, string value)
 {
     ssidTag.SSIDBytes = Encoding.UTF8.GetBytes(value);
 }
        /// <summary>
        ///     Attempts to parse raw data into a structured packet
        /// </summary>
        /// <param name="buffer">Raw data to parse</param>
        /// <param name="packet">Parsed packet</param>
        /// <param name="count">The length of the packet in bytes</param>
        /// <param name="index">The index into the buffer at which the packet begins</param>
        /// <returns>True if parsing was successful, false if it is not.</returns>
        internal static bool TryParse(byte[] buffer, int index, int count, out Beacon802_11 packet)
        {
            try
            {
                if (count < MinimumParseableBytes)
                {
                    packet = null;
                    return(false);
                }

                using (var ms = new MemoryStream(buffer, index, count, false))
                {
                    using (var br = new BinaryReader(ms))
                    {
                        packet = new Beacon802_11();

                        packet.Timestamp      = br.ReadUInt64();
                        packet.BeaconInterval = br.ReadBytes(2);
                        packet.Capabilities   = br.ReadUInt16();

                        while (br.BaseStream.Position <= count - 2)
                        {
                            IManagementTag newTag     = null;
                            var            tagType    = br.ReadByte();
                            var            tagLength  = br.ReadByte();
                            var            safeLength = Math.Min(tagLength, count - (int)br.BaseStream.Position);
                            switch (tagType)
                            {
                            case (byte)ManagementTagTypes.SSID:
                            {
                                SSIDTag tag;
                                if (SSIDTag.TryParse(
                                        buffer,
                                        index + (int)br.BaseStream.Position - 2,
                                        2 + safeLength,
                                        out tag))
                                {
                                    newTag = tag;
                                }
                            }
                            break;

                            case (byte)ManagementTagTypes.VendorSpecific:
                            {
                                /* We'll ask each vendor to parse. Not the most efficient solution.
                                 * MSTag msTag;
                                 * if (MSTag.TryParse(
                                 *  buffer,
                                 *  index + (int)br.BaseStream.Position - 2,
                                 *  2 + safeLength,
                                 *  out msTag))
                                 * {
                                 *  newTag = msTag;
                                 * }
                                 * else
                                 * {*/
                                VendorTag tag;
                                if (VendorTag.TryParse(
                                        buffer,
                                        index + (int)br.BaseStream.Position - 2,
                                        2 + safeLength,
                                        out tag))
                                {
                                    newTag = tag;
                                }
                                /*}*/
                            }
                            break;
                            }

                            if (newTag == null)
                            {
                                UnsupportedTag tag;
                                UnsupportedTag.TryParse(
                                    buffer,
                                    index + (int)br.BaseStream.Position - 2,
                                    2 + safeLength,
                                    out tag);
                                newTag = tag;
                            }
                            br.BaseStream.Seek(safeLength, SeekOrigin.Current);
                            packet.Tags.Add(newTag);
                        }

                        return(true);
                    }
                }
            }
            catch (Exception)
            {
                packet = null;
                return(false);
            }
        }
 /// <summary>
 ///     Gets the SSID as a string
 /// </summary>
 /// <param name="ssidTag">The SSID tag</param>
 /// <returns>The SSID as a string</returns>
 public static string SSIDString(this SSIDTag ssidTag)
 {
     return(Encoding.UTF8.GetString(ssidTag.SSIDBytes, 0, ssidTag.SSIDBytes.Length));
 }