//-----------------------------------------------------------------------
//
// 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.CAN.Objects
{
using System;
using System.Runtime.Serialization;
///
/// Container class for raw data.
///
[Serializable]
public class RawLogObject
{
///
/// Gets or sets Timestamp for object.
///
public long timestamp { get; set; }
///
/// Gets or sets Source Address data
///
public uint sourceAddress { get; set; }
///
/// Gets or sets Mode byte
///
public byte mode { get; set; }
///
/// Gets or sets PID value
///
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1720:IdentifiersShouldNotContainTypeNames", MessageId = "int", Justification = "Reviewed, intentional")]
public uint pid_int { get; set; }
///
/// Gets or sets Data length
///
public uint dataLen { get; set; }
///
/// Gets or sets Payload data
///
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays", Justification = "Reviewed, intentional")]
public byte[] data { get; set; }
///
/// Serialize the data.
///
/// Serialization info
/// Serialization context
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "ctx", Justification = "Reviewed, intentional")]
public void GetObjectData(SerializationInfo info, StreamingContext ctx)
{
if (info != null)
{
info.AddValue("timestamp", this.timestamp);
info.AddValue("sourceAddress", this.sourceAddress);
info.AddValue("mode", this.mode);
info.AddValue("pid_int", this.pid_int);
info.AddValue("dataLen", this.dataLen);
info.AddValue("data", this.data);
}
}
///
/// Initializes a new instance of the class.
///
public RawLogObject()
{
this.timestamp = DateTime.Now.Ticks;
this.sourceAddress = 0;
this.mode = 0;
this.pid_int = 0;
this.dataLen = 0;
this.data = new byte[0];
}
///
/// Initializes a new instance of the class.
///
/// Source Address data
/// Mode byte
/// PID value
/// Data length
/// Payload data
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1720:IdentifiersShouldNotContainTypeNames", MessageId = "int", Justification = "Reviewed, intentional")]
public RawLogObject(uint sourceAddress, byte mode, uint pid_int, uint dataLen, byte[] data)
{
this.timestamp = DateTime.Now.Ticks;
this.sourceAddress = sourceAddress;
this.mode = mode;
this.pid_int = pid_int;
this.dataLen = dataLen;
this.data = data;
}
///
/// Initializes a new instance of the class.
///
/// Serialization info
/// Serialization context
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "ctx", Justification = "Reviewed, intentional")]
protected RawLogObject(SerializationInfo info, StreamingContext ctx)
{
if (info != null)
{
this.timestamp = (uint)info.GetValue("timestamp", typeof(uint));
this.sourceAddress = (uint)info.GetValue("sourceAddress", typeof(uint));
this.mode = (byte)info.GetValue("mode", typeof(byte));
this.pid_int = (uint)info.GetValue("pid_int", typeof(uint));
this.dataLen = (uint)info.GetValue("dataLen", typeof(uint));
this.data = (byte[])info.GetValue("data", typeof(byte[]));
}
}
///
/// Get string representation of object.
///
/// Baseline timestamp.
/// String representation of object.
public string ToString(long t0)
{
string str = string.Empty;
str += "TS: " + ((double)(this.timestamp - t0) / TimeSpan.TicksPerSecond).ToString("#,##0.000");
str += ", Addr: 0x" + this.sourceAddress.ToString("X2");
str += ", Mode: 0x" + this.mode.ToString("X2");
str += ", PID: 0x" + this.pid_int.ToString("X2");
str += ", DATA: [";
string str2 = string.Empty;
foreach (byte b1 in this.data)
{
if (!string.IsNullOrEmpty(str2))
{
str2 += " ";
}
str2 += b1.ToString("X2");
}
str += str2 + "] (" + this.dataLen.ToString() + " bytes)";
return str;
}
}
}