//----------------------------------------------------------------------- // // 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 Protocol { using System.Collections.Generic; using global::SharedObjects; using global::SharedObjects.Api; using global::SharedObjects.GUI; using global::SharedObjects.Protocol; using global::SharedObjects.Protocol.OBD; /// /// Common functionality for all protocol implementations. /// public abstract class Protocol_Common { /// /// Gets Current protocol. /// public Protocols protocol { get; private set; } /// /// Gets Source address. /// public uint srcAddr { get; private set; } /// /// Gets Logging interface. /// protected ILogging iLogging { get; private set; } /// /// Gets Current message handler instance. /// protected MessageHandler messageHandler { get; private set; } /// /// Gets a value indicating whether checksum is enabled. /// protected bool checksum { get; private set; } /// /// Gets callback interface for enabling the buttons when a protocol is fully initialized. /// protected IEnableButtons iEnableButtons { get; private set; } /// /// List of registered mode parsers. /// private IList modeParsers = new List(); /// /// Initializes a new instance of the class. /// /// Logging interface. /// Current protocol. /// Current message handler instance. /// Source address. /// Checksum flag. /// Callback interface for enabling the buttons when a protocol is fully initialized. protected Protocol_Common( ILogging iLogging, Protocols protocol, MessageHandler messageHandler, uint srcAddr, bool checksum, IEnableButtons iEnableButtons) { this.iLogging = iLogging; this.protocol = protocol; this.messageHandler = messageHandler; this.srcAddr = srcAddr; this.checksum = checksum; this.iEnableButtons = iEnableButtons; } /// /// Callback to protocol specific method of fetching the payload part. /// /// Message to get payload from. /// Payload part. public abstract byte[] getPayload(IPassThruMsg msg); /// /// Add a new mode parser instance. /// /// Mode Parser Instance to add. public void addModeParser(IModeParser modeParser) { this.modeParsers.Add(modeParser); } /// /// Remove mode parser instance. /// /// Mode Parser Instance to remove. public void removeModeParser(IModeParser modeParser) { this.modeParsers.Remove(modeParser); } /// /// Extract payload from message. /// /// Message source. /// Start position for data. /// Byte array with payload part. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1062:Validate arguments of public methods", MessageId = "0", Justification = "Reviewed.")] protected static byte[] extractPayload(IPassThruMsg msg, int dataStart) { int sz = msg.DataSize - dataStart; byte[] payload; if (sz >= 0) { payload = new byte[sz]; for (int i = 0; i < sz; i++) { payload[i] = msg.Data[i + dataStart]; } } else { payload = new byte[0]; } return payload; } /// /// Dispatch received message to mode parser(s). /// /// Message instance. /// Message payload. /// Source address of message. /// Detected protocol by ELM/AGV adapter. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1062:Validate arguments of public methods", MessageId = "0", Justification = "Reviewed.")] protected void dispatchMessage(IPassThruMsg rxmsg, byte[] payload, uint srcAddress, uint detectedProtocol) { if (payload != null && payload.Length > 0) { if (this.modeParsers == null || this.modeParsers.Count == 0) { this.iLogging.appendText("No parser initialized yet.\r\n", LogLevel.LOG_WARN); } else { // Skip loopback messages. if ((rxmsg.RxStatus & PassThruConstants.rxflags.TX_MSG_TYPE) == 0) { // Distribute message to all listeners. foreach (IModeParser modeParser in this.modeParsers) { modeParser.receiveMessageData(payload, srcAddress, detectedProtocol); } } } } } } }