/// <summary> /// Decode the incoming data for ensemble data. /// First search for header. When a header is found, /// verify it is a good header. When the header is /// verified, get the payload size. Determine the /// buffer contains the entire ensemble. If the /// entire ensemble is present in the buffer, retreive /// the ensemble and place in a seperate byte array /// and remove it from the buffer. Then decode the /// ensemble for all the ranges present. /// </summary> private void DecodeIncomingData() { // Find the beginning of an ensemble // It will contain 0x7D and either 0x00 or 0x01 SearchForHeaderStart(); // Verify the incoming data can at least fit an ensemble // Subtract the header size because it is already removed if (_incomingDataBuffer.Count >= MIN_ENS_SIZE - HEADER_SIZE) { // Create an array to hold the ensemble byte[] ensemble = new byte[MIN_ENS_SIZE]; // Add the header back to the ensemble data ensemble[0] = ID; // Copy the remainder of the ensemble for (int x = HEADER_SIZE; x < MIN_ENS_SIZE; x++) { ensemble[x] = _incomingDataBuffer.Take(); } // Get the checksum values long calculatedChecksum = PD0.CalculateChecksum(ensemble, MIN_ENS_SIZE - 2); long ensembleChecksum = RetrieveEnsembleChecksum(ensemble); // Verify the checksum match if (calculatedChecksum == ensembleChecksum) { // Decode the binary data DecodeAdcpData(ensemble); } //else //{ // Debug.WriteLine(string.Format("Checksums do not match Cal: {0} Actual:{1}", calculatedChecksum, ensembleChecksum)); //} } }
/// <summary> /// Decode the incoming data for ensemble data. /// First search for header. When a header is found, /// verify it is a good header. When the header is /// verified, get the payload size. Determine the /// buffer contains the entire ensemble. If the /// entire ensemble is present in the buffer, retreive /// the ensemble and place in a seperate byte array /// and remove it from the buffer. Then decode the /// ensemble for all the ranges present. /// </summary> private void DecodeIncomingData() { // Find the beginning of an ensemble // It will contain 2 0x7F at the start SearchForHeaderStart(); // Verify the incoming data can at least fit the header if (_incomingDataBuffer.Count > Pd0Header.HEADER_MIN_BYTE) { // If found 2 bytes of 0x7F // Continue forward // Find ensemble number and ensemble size if (VerifyHeaderStart()) { // Get Ensemble size int currentEnsembleSize = GetEnsembleSize(); // Ensure the entire ensemble is present // before proceeding if (_incomingDataBuffer.Count >= currentEnsembleSize && currentEnsembleSize > 0) { // Create an array to hold the ensemble byte[] ensemble = new byte[currentEnsembleSize]; // Lock the buffer, to ensure the same data is copied lock (_bufferLock) { // Check one more time just in case the buffer was modified if (_incomingDataBuffer.Count >= currentEnsembleSize) { // Copy the ensemble to a byte array _incomingDataBuffer.CopyTo(0, ensemble, 0, currentEnsembleSize); // Remove ensemble from buffer _incomingDataBuffer.RemoveRange(0, currentEnsembleSize); } } // Get the checksum values ushort calculatedChecksum = PD0.CalculateChecksum(ensemble, currentEnsembleSize - PD0.CHECKSUM_NUM_BYTE); ushort ensembleChecksum = RetrieveEnsembleChecksum(ensemble); // Verify the checksum match if (calculatedChecksum == ensembleChecksum) { // Decode the binary data DecodePd0Data(ensemble); } } } else { // Lock the buffer while removing lock (_bufferLock) { if (_incomingDataBuffer.Count > 0) { // Remove the first element to continue searching _incomingDataBuffer.RemoveAt(0); } } } } }
/// <summary> /// Find the entire ensemble in the file. This will look for the start location. /// Decode the payload size and checksum. If they are good, then generate an /// ensemble from the data. Add the data to the list and return it. /// </summary> /// <param name="ensStart">List of all the ensembles.</param> /// <param name="file">File to look for the ensembles.</param> /// <returns>List of all the ensembles in the file.</returns> protected List <DataSet.EnsemblePackage> FindCompleteEnsembles(List <int> ensStart, string file) { var list = new List <DataSet.EnsemblePackage>(); using (var fileStream = new FileStream(file, FileMode.Open, FileAccess.Read)) { foreach (var start in ensStart) { try { var buffer = new byte[DataSet.Ensemble.ENSEMBLE_HEADER_LEN]; //Buffer is byte array of size 32. In Binary codec, buffer is byte array of size 32, containing 32 bytes from file // Move the start location and read in the header fileStream.Seek(start, SeekOrigin.Begin); if (fileStream.Read(buffer, 0, buffer.Length) >= DataSet.Ensemble.ENSEMBLE_HEADER_LEN) // Always true, buffer always size of variable, this loads in bytes to Buffer, however { // Get the payload size int payloadSize = MathHelper.LsbMsbInt(buffer[2], buffer[3]) + PD0.CHECKSUM_NUM_BYTE; //When referencing positions in buffer, uses "start" Which implies it is looking for the position in the actual file. (Error?) // Get the ensemble size int ensSize = MathHelper.LsbMsbInt(buffer[2], buffer[3]) + PD0.CHECKSUM_NUM_BYTE; // Same equation as payload size, but the LsbMsbInt Might change buffer itself? // Sanity check if (ensSize > DataSet.Ensemble.ENSEMBLE_HEADER_LEN) { // Get the entire ensemble var rawEns = new byte[ensSize]; fileStream.Seek(start, SeekOrigin.Begin); fileStream.Read(rawEns, 0, rawEns.Length); // Check the checksum ushort calculatedChecksum = PD0.CalculateChecksum(rawEns, ensSize - PD0.CHECKSUM_NUM_BYTE); ushort ensembleChecksum = MathHelper.LsbMsbUShort(rawEns[rawEns.Length - 2], rawEns[rawEns.Length - 1]); if (calculatedChecksum == ensembleChecksum) { // Pass event that a good ensemble was found if (GoodEnsembleEvent != null) { GoodEnsembleEvent(); } //Pd0Codec _pd0Codec = new Pd0Codec(); //PD0 pd0 = _pd0Codec.DecodePd0Data(rawEns); PD0 pd0Ensemble = new PD0(rawEns); DataSet.Ensemble ens = new DataSet.Ensemble(pd0Ensemble); ens.FileName = file; // Generate a subsystem so that multiple configurations can be seprated // PD0 does not contain the CEPO index or CEPO Configuraiton Index if (ens.IsEnsembleAvail) { ens.EnsembleData.SubsystemConfig = _pd0SubsystemGen.GenSubsystem(ens); } // Package the data var ensPak = new DataSet.EnsemblePackage(); ensPak.Ensemble = ens; ensPak.RawEnsemble = rawEns; ensPak.OrigDataFormat = AdcpCodec.CodecEnum.PD0; list.Add(ensPak); } else { // Pass event that a good ensemble was found if (BadEnsembleEvent != null) { BadEnsembleEvent(); } } } } } catch (Exception e) { log.Error("Error looking for an ensemble. Loc: " + start, e); } } } return(list); }