Files

105 lines
3.6 KiB
C#
Raw Permalink Normal View History

2026-04-17 14:55:32 -04:00
namespace DTS.DASLib.Command.SLICE.DownloadCommands
{
/// <summary>
/// this is ported from FirmwareTestUtility where it was written by Loc Pham. It is included for
/// 10573 implement SW side of single command/streaming download
/// </summary>
public class DownloadByteConverter
{
public enum DlByteIndex
{
Sync = 0,
Type = 1,
Command = 2,
Status = 3,
Group = 4,
Id = 5,
Datalength = 6,
SequenceNumber = 8,
HeaderCRC = 10,
DataCRC = 12,
DataADCStartOffset = 14,
DataADCEndOffset = 22,
DataStart = 30
}
public ushort SeqNumber { get; }
public ushort SeqNumberPrev { get; }
public ulong DlAdcStartOffset { get; }
public ulong DlAdcEndOffset { get; }
public ulong DlStreamSampleNumber { get; }
public ushort[] DlData { get; }
public uint[] DlChannels { get; }
public DownloadByteConverter(byte[] bytes)
{
SeqNumber = GetUShort(bytes, (int)DlByteIndex.SequenceNumber);
DlAdcStartOffset = GetUInt(bytes, (int)DlByteIndex.DataADCStartOffset);
DlAdcStartOffset = GetULong(bytes, (int)DlByteIndex.DataADCEndOffset);
DlData = new ushort[(bytes.Length - (int)DlByteIndex.DataStart) / 2];
if (SeqNumber == 0)
{
SeqNumberPrev = 0;
}
else
{
if (SeqNumberPrev != 0 &&
SeqNumberPrev != SeqNumber - 1)
{
System.Diagnostics.Debug.WriteLine("sequence out of order: ");
System.Diagnostics.Debug.WriteLine(SeqNumberPrev.ToString());
System.Diagnostics.Debug.WriteLine(SeqNumber.ToString());
}
}
SeqNumberPrev = SeqNumber;
for (var idx = 0; idx < DlData.Length; idx++)
{
DlData[idx] = GetDownloadUShort(bytes, (int)(DlByteIndex.DataStart + idx * 2));
}
}
private static ulong GetULong(byte[] bytes, int offset)
{
return (ulong)bytes[offset + 7] << 0 |
(ulong)bytes[offset + 6] << 8 |
(ulong)bytes[offset + 5] << 16 |
(ulong)bytes[offset + 4] << 24 |
(ulong)bytes[offset + 3] << 32 |
(ulong)bytes[offset + 2] << 40 |
(ulong)bytes[offset + 1] << 48 |
(ulong)bytes[offset + 0] << 56;
}
private static ushort GetUShort(byte[] bytes, int offset)
{
return (ushort)(bytes[offset + 1] |
bytes[offset + 0] << 8);
}
private static uint GetUInt(byte[] bytes, int offset)
{
return (uint)bytes[offset + 3] << 0 |
(uint)bytes[offset + 2] << 8 |
(uint)bytes[offset + 1] << 16 |
(uint)bytes[offset + 0] << 32;
}
/// <summary>
/// data apparently has a different byte order than parameters, so we have to
/// rearrange byte order for parameters but not data.
/// </summary>
/// <param name="bytes"></param>
/// <param name="offset"></param>
/// <returns></returns>
private static ushort GetDownloadUShort(byte[] bytes, int offset)
{
return (ushort)(bytes[offset + 0] | bytes[offset + 1] << 8);
}
}
}