//-----------------------------------------------------------------------
//
// 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
{
using System.Collections.Generic;
///
/// The supported protocols as defined by the J2534 API.
///
public class Protocols
{
/* Protocol ID:s. */
///
/// Protocol AT Commands over Serial Connection.
///
public const int SERIAL_SIMPLE = -3;
///
/// Protocol AT Commands over Serial Connection.
///
public const int SERIAL_AT_ELM = -2;
///
/// Protocol AT Commands over Serial Connection.
///
public const int SERIAL_AT_AGV = -1;
///
/// Protocol J1850VPW.
///
public const int J1850VPW = 1;
///
/// Protocol J1850PWM.
///
public const int J1850PWM = 2;
///
/// Protocol ISO9141.
///
public const int ISO9141 = 3;
///
/// Protocol ISO14230 (KWP2000).
///
public const int ISO14230 = 4;
///
/// Protocol CAN.
///
public const int CAN = 5;
///
/// Protocol ISO15765.
///
public const int ISO15765 = 6;
///
/// Protocol SCI_A_ENGINE.
///
public const int SCI_A_ENGINE = 7;
///
/// Protocol SCI_A_TRANS.
///
public const int SCI_A_TRANS = 8;
///
/// Protocol SCI_B_ENGINE.
///
public const int SCI_B_ENGINE = 9;
///
/// Protocol SCI_B_TRANS.
///
public const int SCI_B_TRANS = 10;
/* Protocol Families */
///
/// Protocol Family SERIAL.
///
public const int SERIAL = -1;
///
/// Protocol Family VPW_PWM.
///
public const int VPW_PWM = 1;
///
/// Protocol Family KL_LINE.
///
public const int KL_LINE = 2;
///
/// Protocol Family CANBUS.
///
public const int CANBUS = 3;
///
/// List of generic addresses to use if no ECU:s or addresses are declared.
///
/// Notice: Not used for ISO15765 11-bit.
///
///
public static readonly uint[] genericAddressList =
{
0x10,
0x11,
0x17,
0x18,
0x1a,
0x1f
};
///
/// Address bit declaration alternatives.
///
public static readonly AddressData[] addrISO15765 =
{
new AddressData(11, "0x07df Broadcast", "0x07e0", "0x07e8", 0x00, new uint[]{0x00}),
new AddressData(29, "0x18db3300 Broadcast", "0x18da0000", "0x18da0000", 0x100, new uint[]{0xf1, 0xf2, 0xf0, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff}),
};
///
/// Address bit declaration alternatives.
///
public static readonly AddressData[] addrISO9141 =
{
new AddressData(8, "0x6a Broadcast", "0x6a", "0xf1", 0x00, new uint[]{0x00})
};
///
/// Address bit declaration alternatives.
///
public static readonly AddressData[] addrISO14230 =
{
new AddressData(8, "0x33 Broadcast", "0x33", "0xf1", 0x00, new uint[]{0x00})
};
///
/// Declare the IDs of the protocols to scan when trying to detect protocol.
///
/// Notice that the order matters.
///
///
public static readonly int[] protocolScanOrder = new int[]
{
ISO15765,
ISO14230,
ISO9141,
J1850PWM,
J1850VPW,
SERIAL_AT_ELM,
SERIAL_AT_AGV,
CAN,
};
///
/// Array of protocols.
///
public static readonly Protocols[] protocols = new Protocols[]
{
new Protocols(SERIAL_SIMPLE, "SERIAL_SIMPLE", new int[]{SERIAL}, null, false, false, 1000),
new Protocols(SERIAL_AT_ELM, "SERIAL_AT_ELM", new int[]{SERIAL}, null, false, false, 1000),
new Protocols(SERIAL_AT_AGV, "SERIAL_AT_AGV", new int[]{SERIAL}, null, false, false, 1000),
new Protocols(J1850VPW, "J1850VPW", new int[]{VPW_PWM}, null, false, true, 1000),
new Protocols(J1850PWM, "J1850PWM", new int[]{VPW_PWM}, null, false, true, 1000),
new Protocols(ISO9141, "ISO9141", new int[]{KL_LINE}, addrISO9141, false, true, 4000),
new Protocols(ISO14230, "ISO14230 (KWP2000)", new int[]{KL_LINE}, addrISO14230, false, true, 4000),
new Protocols(CAN, "CAN", new int[]{CANBUS}, null, true, true, 100),
new Protocols(ISO15765, "ISO15765", new int[]{CANBUS}, addrISO15765, true, true, 100),
new Protocols(SCI_A_ENGINE, "SCI_A_ENGINE", new int[]{KL_LINE, CANBUS}, null, false, false, 1000),
new Protocols(SCI_A_TRANS, "SCI_A_TRANS", new int[]{KL_LINE, CANBUS}, null, false, false, 1000),
new Protocols(SCI_B_ENGINE, "SCI_B_ENGINE", new int[]{KL_LINE, CANBUS}, null, false, false, 1000),
new Protocols(SCI_B_TRANS, "SCI_B_TRANS", new int[]{KL_LINE, CANBUS}, null, false, false, 1000)
};
///
/// Gets Protocol ID.
///
public int id { get; private set; }
///
/// Gets Protocol Name.
///
public string name { get; private set; }
///
/// Gets a value indicating whether asynchronous calls can be made.
///
/// This indicates that the protocol allows for asynchronous
/// calls, but it depends on the vehicle if they can be successful.
///
///
public bool isAsync { get; private set; }
///
/// Gets a value indicating whether Protocol is possible to detect using auto-detect mechanism.
///
public bool canDetect { get; private set; }
///
/// List of possible address bits alternatives.
///
private List addrDataList = new List();
///
/// Gets Address Bits alternatives where applicable.
///
public IList addressDataList
{
get
{
return this.addrDataList;
}
}
///
/// List of protocol families.
///
private List familyList = new List();
///
/// Gets Protocol Family.
///
public IList families
{
get
{
return this.familyList;
}
}
///
/// Gets timeout waiting for response.
///
public int timeout { get; private set; }
///
/// Initializes a new instance of the class.
///
/// Protocol ID.
/// Protocol Name.
/// Protocol Families.
/// Addressing data struct array.
/// Asynchronous calls can be made.
/// Protocol can be auto-detected.
/// Detection timeout for protocol.
private Protocols(int id, string name, int[] families, AddressData[] addrData, bool isAsync, bool canDetect, int timeout)
{
this.id = id;
this.name = name;
this.isAsync = isAsync;
this.canDetect = canDetect;
this.timeout = timeout;
foreach (int family in families)
{
this.families.Add(family);
}
if (addrData != null)
{
foreach (AddressData addrDataItem in addrData)
{
this.addrDataList.Add(addrDataItem);
}
}
}
///
/// Get the protocol with the given ID.
///
/// Protocol ID.
/// The matching protocol or 'null' if no match.
public static Protocols getProtocolById(int id)
{
Protocols protocol = null;
foreach (Protocols proto in protocols)
{
if (proto.id == id)
{
protocol = proto;
break;
}
}
return protocol;
}
///
/// Get the protocol with the given name.
///
/// Protocol name.
/// The matching protocol or 'null' if no match.
public static Protocols getProtocolByName(string name)
{
Protocols protocol = null;
foreach (Protocols proto in protocols)
{
if (proto.name == name)
{
protocol = proto;
break;
}
}
return protocol;
}
///
/// Address data for protocol.
///
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1034:NestedTypesShouldNotBeVisible", Justification = "Reviewed, intentional.")]
public class AddressData
{
///
/// Gets address bits for protocol.
///
public int bits { get; private set; }
///
/// Gets broadcast (functional) address for protocol.
///
public string broadcastAddress { get; private set; }
///
/// Gets base destination address for protocol.
///
public string baseDestinationAddress { get; private set; }
///
/// Gets base source address for protocol.
///
public string baseSourceAddress { get; private set; }
///
/// Gets connection flags for address alternative.
///
public uint connFlags { get; private set; }
///
/// Gets tool source addresses; 0xf0, 0xf1 etc.
///
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays", Justification = "Reviewed")]
public uint[] srcAddr { get; private set; }
///
/// Initializes a new instance of the class.
///
/// Address bits for protocol.
/// Broadcast (functional) address for protocol.
/// Base destination address for protocol.
/// Base source address for protocol.
/// Connection Flags.
/// Array of possible source addresses to use.
internal AddressData(int bits, string broadcastAddress, string baseDestinationAddress, string baseSourceAddress, uint connFlags, uint[] srcAddr)
{
this.bits = bits;
this.broadcastAddress = broadcastAddress;
this.baseDestinationAddress = baseDestinationAddress;
this.baseSourceAddress = baseSourceAddress;
this.connFlags = connFlags;
this.srcAddr = srcAddr;
}
}
}
}