//----------------------------------------------------------------------- // // 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 SimProtocol { /// /// Container for multi-framed data. /// public class MultiFrameData { /// /// Gets a value indicating whether an error was detected. (Unexpected data segment number). /// public bool error { get; private set; } /// /// Gets a value indicating whether complete data has been received. /// public bool complete { get; private set; } /// /// Gets total length of expected data. /// public uint length { get; private set; } /// /// Gets data array. /// [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays", Justification = "Reviewed, intentional")] public byte[] data { get; private set; } /// /// Pointer to current byte in data array. /// private int n = 0; /// /// Number of next expected segment. /// private int nextSegment = 0; /// /// Initializes a new instance of the class. /// /// Msb 4 bits. /// Data packet. /// Pointer to first data. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1062:Validate arguments of public methods", MessageId = "1", Justification = "Reviewed, intentional")] public MultiFrameData(uint zLen, byte[] data0, uint p0) { this.error = true; this.complete = false; this.length = (zLen & 0x0f) << 8 | data0[p0++]; this.data = new byte[this.length]; this.addData(0, data0, p0); } /// /// Add a new data segment. /// /// Checks if correct segment is received and sets 'error' to 'true' if not. /// /// /// If complete data is received 'complete' is set to 'true'. /// /// /// Segment number. /// Data to add. /// Pointer to first data. public void addData(uint segment, byte[] data0, uint p0) { if (data0 != null) { if ((this.nextSegment & 0x0f) == (segment & 0x0f)) { this.error = false; this.nextSegment++; while (p0 < data0.Length && this.n < this.data.Length) { this.data[this.n++] = data0[p0++]; } this.complete = this.n >= this.data.Length; } else { this.error = true; } } else { this.error = true; } } } }