//-----------------------------------------------------------------------
//
// Copyright © 2012 Nils Hammar. All rights reserved.
//
//-----------------------------------------------------------------------
/*
* Software to access vehicle information via the OBD-II connector.
*
* Copyright © 2012 Nils Hammar
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program 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 General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*
* Alternative licensing is possible, see the licensing document.
*
* The above text may not be removed or modified.
*/
namespace SharedObjects.Protocol
{
///
/// Wrapper class for the payload byte array and with some commonly used data.
///
public class TxMsg
{
///
/// Tx mode MODE_CURRENT.
///
public const byte MODE_CURRENT = 0x01;
///
/// Tx mode MODE_FREEZE_FRAME.
///
public const byte MODE_FREEZE_FRAME = 0x02;
///
/// Tx mode MODE_DTC_READ.
///
public const byte MODE_DTC_READ = 0x03;
///
/// Tx mode MODE_DTC_CLEAR.
///
public const byte MODE_DTC_CLEAR = 0x04;
///
/// Tx mode MODE_OBD_MID.
///
public const byte MODE_OBD_MID = 0x06;
///
/// Tx mode MODE_PENDING_DTC_READ.
///
public const byte MODE_PENDING_DTC_READ = 0x07;
///
/// Tx mode MODE_VEHICLE_INFO.
///
public const byte MODE_VEHICLE_INFO = 0x09;
///
/// Tx mode MODE_PERMANENT_DTC_READ.
///
public const byte MODE_PERMANENT_DTC_READ = 0x0a;
///
/// Tx mode MODE_DTC_READ.
///
public const byte MODE_ISO14229_DTC_READ = 0x19;
// Some default payloads.
///
/// Message GET_SUPPORTED_PIDS
///
public static readonly byte[] GET_SUPPORTED_PIDS = new byte[] { MODE_CURRENT, 0x00 };
///
/// Message GET_SUPPORTED_PIDS
///
public static readonly byte[] GET_SUPPORTED_OBDMIDS = new byte[] { MODE_OBD_MID, 0x00 };
//// public static readonly byte[] GET_SUPPORTED_OBDMIDS = new byte[] { MODE_OBD_MID, 0x00, 0x20, 0x40, 0x60, 0x80, 0xa0 };
///
/// Message GET_FREEZEFRAME_PIDS
///
public static readonly byte[] GET_FREEZEFRAME_PIDS = new byte[] { MODE_FREEZE_FRAME, 0x00, 0x00 };
///
/// Message GET_DTCS
///
public static readonly byte[] GET_DTCS = new byte[] { MODE_DTC_READ };
///
/// Message GET_PENDING_DTCS
///
public static readonly byte[] GET_PENDING_DTCS = new byte[] { MODE_PENDING_DTC_READ };
///
/// Message GET_PERMANENT_DTCS
///
public static readonly byte[] GET_PERMANENT_DTCS = new byte[] { MODE_PERMANENT_DTC_READ };
///
/// Message CLEAR_DTCS
///
public static readonly byte[] CLEAR_DTCS = new byte[] { MODE_DTC_CLEAR };
///
/// Message GET_INFO_PIDS
///
public static readonly byte[] GET_INFO_PIDS = new byte[] { MODE_VEHICLE_INFO, 0x00 };
///
/// Message GET_INFO_VIN_MSGS
///
public static readonly byte[] GET_INFO_VIN_MSGS = new byte[] { MODE_VEHICLE_INFO, 0x01 };
///
/// Message GET_INFO_VIN
///
public static readonly byte[] GET_INFO_VIN = new byte[] { MODE_VEHICLE_INFO, 0x02 };
///
/// Message GET_INFO_CALIBRATION_ID
///
public static readonly byte[] GET_INFO_CALIBRATION_ID = new byte[] { MODE_VEHICLE_INFO, 0x04 };
///
/// Message GET_INFO_CALIBRATION
///
public static readonly byte[] GET_INFO_CALIBRATION = new byte[] { MODE_VEHICLE_INFO, 0x06 };
///
/// Message SSM_INIT
///
public static readonly byte[] SSM_INIT = new byte[] { 0xaa };
///
/// Gets payload data.
///
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays", Justification = "Reviewed, intentional.")]
public byte[] payload { get; private set; }
///
/// Initializes a new instance of the class.
///
/// Payload data.
public TxMsg(byte[] payload)
{
this.payload = payload;
}
///
/// Calculate Hashcode.
///
/// Calculated Hashcode.
public override int GetHashCode()
{
int hashCode = 0;
for (int i = 0; i < this.payload.Length; i++)
{
hashCode = (int)(hashCode << 7 | hashCode >> 25);
hashCode ^= this.payload[i].GetHashCode();
}
return hashCode;
}
///
/// Compare with another object.
///
/// Object to compare with.
/// 'true' if equal.
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1800:DoNotCastUnnecessarily", Justification = "False warning.")]
public override bool Equals(object obj)
{
if (obj is TxMsg)
{
TxMsg msg1 = (TxMsg)obj;
if (msg1.payload is byte[])
{
bool equals = (msg1.payload.Length == this.payload.Length);
for (int i = 0; equals && i < this.payload.Length; i++)
{
equals = (msg1.payload[i] == this.payload[i]);
}
return equals;
}
}
return false;
}
///
/// Get the payload data as a string.
///
/// String data.
public override string ToString()
{
string str = string.Empty;
foreach (byte b1 in this.payload)
{
if (!string.IsNullOrEmpty(str))
{
str += ", ";
}
str += "0x" + b1.ToString("x2");
}
return str;
}
}
}