/// <summary> /// Gets the <see cref="SpecialByte"/> entry associated with the given value. /// </summary> /// <param name="dumb"></param> /// <param name="value">Value of the <see cref="SpecialByte"/> to retrieve.</param> /// <returns><see cref="SpecialByte"/> associated to the given value, <c>0</c> if it does not /// exist in the list.</returns> public static SpecialByte Get(this SpecialByte dumb, byte value) { var values = Enum.GetValues(typeof(SpecialByte)).OfType <SpecialByte>(); if (values.Cast <byte>().Contains(value)) { return((SpecialByte)value); } return(0); }
/// <summary> /// Generates the XBee packet byte array escaping the special bytes. /// </summary> /// <remarks>Use only while working in <see cref="OperatingMode.API_ESCAPE"/> mode. If working in /// <see cref="OperatingMode.API"/> mode, use <see cref="GenerateByteArray"/>.</remarks> /// <returns>he XBee packet byte array with escaped characters.</returns> /// <seealso cref="GenerateByteArray"/> public byte[] GenerateByteArrayEscaped() { byte[] unescapedArray = GenerateByteArray(); using (var os = new MemoryStream()) { // Write header byte and do not escape it. os.WriteByte(SpecialByte.HEADER_BYTE.GetValue()); for (int i = 1; i < unescapedArray.Length; i++) { // Start at 1 to avoid escaping header byte. if (SpecialByte.ESCAPE_BYTE.IsSpecialByte(unescapedArray[i])) { os.WriteByte(SpecialByte.ESCAPE_BYTE.GetValue()); SpecialByte specialByte = SpecialByte.ESCAPE_BYTE.Get(unescapedArray[i]); os.WriteByte(specialByte.EscapeByte()); } else { os.WriteByte(unescapedArray[i]); } } return(os.ToArray()); } }
/// <summary> /// Checks whether the given byte is special or not. /// </summary> /// <param name="source"></param> /// <param name="byteToCheck">Byte to check.</param> /// <returns><c>true</c> if given byte is special, <c>false</c> otherwise.</returns> public static bool IsSpecialByte(this SpecialByte source, byte byteToCheck) { return(source.Get(byteToCheck) != 0); }
/// <summary> /// Escapes the byte by performing a XOR operation with <code>0x20</code> value. /// </summary> /// <param name="source"></param> /// <returns>Escaped byte value.</returns> public static byte EscapeByte(this SpecialByte source) { return((byte)(((byte)source) ^ 0x20)); }
/// <summary> /// Gest the special byte value. /// </summary> /// <param name="source"></param> /// <returns>The special byte value.</returns> public static byte GetValue(this SpecialByte source) { return((byte)source); }
/// <summary> /// Checks whether the given byte is special or not. /// </summary> /// <param name="dumb"></param> /// <param name="byteToCheck">Byte to check.</param> /// <returns>true if given byte is special, false otherwise.</returns> public static bool IsSpecialByte(this SpecialByte dumb, byte byteToCheck) { return(dumb.Get(byteToCheck) != null); }