/* This file is part of PacketDotNet PacketDotNet is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. PacketDotNet is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with PacketDotNet. If not, see . */ /* * Copyright 2010 Evan Plaice * Copyright 2010 Chris Morgan */ using System; using PacketDotNet.Utils; namespace PacketDotNet.LLDP { /// /// Base class for several TLV types that all contain strings /// public class StringTLV : TLV { #region Constructors /// /// Creates a String TLV /// /// /// /// /// The Port Description TLV's offset from the /// origin of the LLDP /// public StringTLV(byte[] bytes, int offset) : base(bytes, offset) {} /// /// Create from a type and string value /// /// /// A /// /// /// A /// public StringTLV(TLVTypes tlvType, string StringValue) { var bytes = new byte[TLVTypeLength.TypeLengthLength]; var offset = 0; tlvData = new ByteArraySegment(bytes, offset, bytes.Length); Type = tlvType; this.StringValue = StringValue; } #endregion #region Properties /// /// A textual Description of the port /// public string StringValue { get { return System.Text.ASCIIEncoding.ASCII.GetString(tlvData.Bytes, ValueOffset, Length); } set { var bytes = System.Text.ASCIIEncoding.ASCII.GetBytes(value); var length = TLVTypeLength.TypeLengthLength + bytes.Length; // is the tlv the correct size? if(tlvData.Length != length) { // allocate new memory for this tlv var newTLVBytes = new byte[length]; var offset = 0; // copy header over Array.Copy(tlvData.Bytes, tlvData.Offset, newTLVBytes, 0, TLVTypeLength.TypeLengthLength); tlvData = new ByteArraySegment(newTLVBytes, offset, length); } // set the description Array.Copy(bytes, 0, tlvData.Bytes, ValueOffset, bytes.Length); } } /// /// Convert this Port Description TLV to a string. /// /// /// A human readable string /// public override string ToString () { return string.Format("[{0}: Description={0}]", Type, StringValue); } #endregion } }