//----------------------------------------------------------------------- // // 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 DynamicApiLoading.Api.Dynamic.J2534 { using System; using global::DynamicApiLoading.Api.Dynamic; using global::SharedObjects.Api; /// /// Dynamic loader for J2534 PassThru API. /// public unsafe class DynamicLoader : IPassThruBase, IDisposable { /// /// Gets dynamic adapter instance. /// protected DynamicAdapter dynamicAdapter { get; private set; } /// /// Native methods instance. /// private NativeMethods nativeMethods; /// /// Initializes a new instance of the class. /// /// Dynamic adapter instance. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1062:Validate arguments of public methods", MessageId = "0", Justification = "Reviewed, intentional.")] public DynamicLoader(DynamicAdapter dynamicAdapter) { this.dynamicAdapter = dynamicAdapter; this.nativeMethods = new NativeMethods(dynamicAdapter.FunctionLibrary); } /// /// Disposes the object. /// public void Dispose() { this.Dispose(true); GC.SuppressFinalize(this); } /// /// Implementation of PassThruOpen. /// /// Always 'null'. /// Pointer to selected device ID. (return value) /// '0' if success, non-zero if a failure occurred. public unsafe PassThruConstants.resultCode PassThruOpen(void* pName, int* pDeviceID) { return (PassThruConstants.resultCode)this.nativeMethods.PassThruOpen(pName, pDeviceID); } /// /// Close the given device. /// /// ID of device to close. /// '0' if success, non-zero if a failure occurred. public PassThruConstants.resultCode PassThruClose(int DeviceID) { return (PassThruConstants.resultCode)this.nativeMethods.PassThruClose(DeviceID); } /// /// Establish a connection on the given device. /// /// ID of current device. /// ID of protocol to use. /// Connection Flags. /// Baudrate to use for connection. /// Pointer to value for channel id (return value) /// '0' if success, non-zero if a failure occurred. public unsafe PassThruConstants.resultCode PassThruConnect(int DeviceID, int ProtocolID, uint Flags, uint Baudrate, int* pChannelID) { return (PassThruConstants.resultCode)this.nativeMethods.PassThruConnect(DeviceID, ProtocolID, (int)Flags, (int)Baudrate, pChannelID); } /// /// Disconnect the given channel. /// /// ID of channel to disconnect. /// '0' if success, non-zero if a failure occurred. public PassThruConstants.resultCode PassThruDisconnect(int ChannelID) { return (PassThruConstants.resultCode)this.nativeMethods.PassThruDisconnect(ChannelID); } /// /// Read messages from the given channel. /// /// ID of channel to read from. /// Pointer to message(s) read. /// Number of messages to read, value set at return to actual number read. /// Timeout value before returning with no message read. /// '0' if success, non-zero if a failure occurred. public unsafe PassThruConstants.resultCode PassThruReadMsgs(int ChannelID, PASSTHRU_MSG* pMsg, int* pNumMsgs, int Timeout) { return (PassThruConstants.resultCode)this.nativeMethods.PassThruReadMsgs(ChannelID, pMsg, pNumMsgs, Timeout); } /// /// Write messages to the given channel. /// /// ID of channel to write to. /// Message(s) to write. /// Number of messages to write. /// Timeout when writing message to out channel. /// '0' if success, non-zero if a failure occurred. public unsafe PassThruConstants.resultCode PassThruWriteMsgs(int ChannelID, PASSTHRU_MSG* pMsg, int* pNumMsgs, int Timeout) { return (PassThruConstants.resultCode)this.nativeMethods.PassThruWriteMsgs(ChannelID, pMsg, pNumMsgs, Timeout); } /// /// Start a periodic message to send from application to vehicle. /// /// ID of channel to write to. /// Message(s) to write. /// Return value: ID of message being sent. /// Interval between each message in milliseconds. /// '0' if success, non-zero if a failure occurred. public unsafe PassThruConstants.resultCode PassThruStartPeriodicMsg(int ChannelID, PASSTHRU_MSG* pMsg, int* pMsgID, int TimeInterval) { return (PassThruConstants.resultCode)this.nativeMethods.PassThruStartPeriodicMsg(ChannelID, pMsg, pMsgID, TimeInterval); } /// /// Stop a periodic message from being sent to the vehicle. /// /// ID of channel to stop message on. /// ID of message to stop sending. /// '0' if success, non-zero if a failure occurred. public PassThruConstants.resultCode PassThruStopPeriodicMsg(int ChannelID, int MsgID) { return (PassThruConstants.resultCode)this.nativeMethods.PassThruStopPeriodicMsg(ChannelID, MsgID); } /// /// Configure a message filter. /// /// Channel to configure filter on. /// Type of filter to configure. /// Mask message in filter. /// Pattern message in filter. /// Flow control message in filter. /// ID of filter created (out value) /// '0' if success, non-zero if a failure occurred. public unsafe PassThruConstants.resultCode PassThruStartMsgFilter( int ChannelID, int FilterType, PASSTHRU_MSG* pMaskMsg, PASSTHRU_MSG* pPatternMsg, PASSTHRU_MSG* pFlowControlMsg, int* pMsgID) { return (PassThruConstants.resultCode)this.nativeMethods.PassThruStartMsgFilter(ChannelID, FilterType, pMaskMsg, pPatternMsg, pFlowControlMsg, pMsgID); } /// /// Stop a message filter. /// /// Channel to remove filter from. /// ID of filter to remove. /// '0' if success, non-zero if a failure occurred. public PassThruConstants.resultCode PassThruStopMsgFilter(int ChannelID, int MsgID) { return (PassThruConstants.resultCode)this.nativeMethods.PassThruStopMsgFilter(ChannelID, MsgID); } /// /// Configure the programming voltage pin and voltage. /// /// ID of Device to configure. /// Number of pin to configure. /// Voltage to set at pin. /// '0' if success, non-zero if a failure occurred. public PassThruConstants.resultCode PassThruSetProgrammingVoltage(int DeviceID, int Pin, int Voltage) { return (PassThruConstants.resultCode)this.nativeMethods.PassThruSetProgrammingVoltage(DeviceID, Pin, Voltage); } /// /// Read API version. /// /// ID to get version information from. /// Firmware version (return value). /// DLL version (return value). /// API version (return value). /// '0' if success, non-zero if a failure occurred. public unsafe PassThruConstants.resultCode PassThruReadVersion(int DeviceID, byte* pFirmwareVersion, byte* pDllVersion, byte* pApiVersion) { return (PassThruConstants.resultCode)this.nativeMethods.PassThruReadVersion(DeviceID, pFirmwareVersion, pDllVersion, pApiVersion); } /// /// Get the last error encountered on the device. /// /// Pointer to string with error description. /// '0' if success, non-zero if a failure occurred. public unsafe PassThruConstants.resultCode PassThruGetLastError(sbyte* pErrorDescription) { return (PassThruConstants.resultCode)this.nativeMethods.PassThruGetLastError(pErrorDescription); } /// /// Perform IOCTL operation on the device. /// /// ID of channel to perform operation on. /// IOCTL Operation. /// Input parameter for operation. /// Output parameter for operation. /// '0' if success, non-zero if a failure occurred. public unsafe PassThruConstants.resultCode PassThruIoctl( int ChannelID, int IoctlID, void* pInput, void* pOutput) { return (PassThruConstants.resultCode)this.nativeMethods.PassThruIoctl(ChannelID, IoctlID, pInput, pOutput); } /// /// Disposes the object. /// /// 'true' if call to Dispose() was made, 'false' if the class was finalized. protected virtual void Dispose(bool disposing) { if (this.nativeMethods != null) { this.nativeMethods.Dispose(); this.nativeMethods = null; } } /// /// Finalizes an instance of the class. /// ~DynamicLoader() { this.Dispose(false); } } }