CUST: - FwUpdateSw: - LUT check and logging

Common/.../GenesisFile/MeterLutFile: - LUT file analyze function
This commit is contained in:
Thomas Wiedebusch
2023-11-13 13:49:06 +01:00
parent 54faa55355
commit 75cabb6f47
6 changed files with 170 additions and 106 deletions
@@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using System.Text;
using Xylem.Common.Logic.ProductionOrderCore.OrderData;
using Xylem.Common.Utils.Crc16Ccitt;
namespace Xylem.Common.Hardware.WaterMeter.Genesis.GenesisFile
{
@@ -93,6 +95,11 @@ namespace Xylem.Common.Hardware.WaterMeter.Genesis.GenesisFile
/// </summary>
public UInt16 CalculatedCrc;
/// <summary>
/// FileCRC and calculated CRC are different
/// </summary>
public Boolean CrcError;
/// <summary>
/// Validation of LUT file
/// </summary>
@@ -107,5 +114,48 @@ namespace Xylem.Common.Hardware.WaterMeter.Genesis.GenesisFile
/// Meter size
/// </summary>
public MeterSize MeterSize;
/// <summary>
/// Verifies the LUT format based on its contents
/// </summary>
/// <returns>true if valid</returns>
public Boolean ValidateLutFormat()
{
IsValid = false;
if (BinData == null || BinData.Count < SizeLutFile)
return false;
// extract the identifier and check, if this is a LUT file
StrLutFileId = Encoding.UTF8.GetString(BinData.ToArray(), MeterLutFile.IndexLutFileId,
MeterLutFile.IndexDataLength);
if (StrLutFileId != MeterLutFile.StrLutMeterFileId)
return false;
MeterSize = (MeterSize)BinData[MeterLutFile.IndexMeterSize];
FileCrc = (UInt16)(BinData[MeterLutFile.IndexCrc] +
(BinData[MeterLutFile.IndexCrc + 1] << 8));
Version = (UInt16)(BinData[MeterLutFile.IndexVersion] +
(BinData[MeterLutFile.IndexVersion + 1] << 8));
DataLength = (UInt32)(BinData[MeterLutFile.IndexDataLength]
+ (BinData[MeterLutFile.IndexDataLength + 1] << 8)
+ (BinData[MeterLutFile.IndexDataLength + 2] << 16)
+ (BinData[MeterLutFile.IndexDataLength + 3] << 24));
// calculate CRC and compare this with the file CRC
var entireFile = new List<Byte>();
entireFile.AddRange(BinData);
var payload = new List<Byte>();
payload.AddRange(entireFile.GetRange(MeterLutFile.IndexVersion,
MeterLutFile.LutFileDataLength + MeterLutFile.LengthVersion));
CalculatedCrc = Crc16Ccitt.CalculateMsb1021(payload.ToArray());
if (CalculatedCrc != FileCrc)
{
CrcError = true;
return false;
}
CrcError = false;
IsValid = true;
return true;
}
}
}
@@ -2,7 +2,6 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using Xylem.Common.Hardware.WaterMeter.Genesis.GenesisCore;
using Xylem.Common.Hardware.WaterMeter.Genesis.GenesisCore.Consts;
@@ -10,7 +9,6 @@ using Xylem.Common.Hardware.WaterMeter.Genesis.GenesisFile.Consts;
using Xylem.Common.Hardware.WaterMeter.Genesis.Registers;
using Xylem.Common.Hardware.WaterMeter.WaterMeterCore.Consts;
using Xylem.Common.Logic.ProductionOrderCore.OrderData;
using Xylem.Common.Utils.Crc16Ccitt;
using Xylem.Common.Utils.ProcessExec;
using Xylem.Common.Utils.ProcessExec.EventArguments;
@@ -483,6 +481,9 @@ namespace Xylem.Common.Hardware.WaterMeter.Genesis.GenesisFile
/// <remarks date="2022-Feb-25" author="Thomas Wiedebusch">
/// - Initial
/// </remarks>
/// <remarks date="2023-Nov-13" author="Thomas Wiedebusch">
/// - LUT file validation exported to <see cref="MeterLutFile.ValidateLutFormat"/>
/// </remarks>
private Boolean ValidateLutFile()
{
// check if LUT file contains data
@@ -492,38 +493,15 @@ namespace Xylem.Common.Hardware.WaterMeter.Genesis.GenesisFile
return false;
}
LutFile.IsValid = false;
// extract the identifier and check, if this is a LUT file
LutFile.StrLutFileId = Encoding.UTF8.GetString(LutFile.BinData.ToArray(), MeterLutFile.IndexLutFileId,
MeterLutFile.IndexDataLength);
if (LutFile.StrLutFileId != MeterLutFile.StrLutMeterFileId)
return false;
LutFile.MeterSize = (MeterSize)LutFile.BinData[MeterLutFile.IndexMeterSize];
LutFile.FileCrc = (UInt16)(LutFile.BinData[MeterLutFile.IndexCrc] +
(LutFile.BinData[MeterLutFile.IndexCrc + 1] << 8));
LutFile.Version = (UInt16)(LutFile.BinData[MeterLutFile.IndexVersion] +
(LutFile.BinData[MeterLutFile.IndexVersion + 1] << 8));
LutFile.DataLength = (UInt32)(LutFile.BinData[MeterLutFile.IndexDataLength]
+ (LutFile.BinData[MeterLutFile.IndexDataLength + 1] << 8)
+ (LutFile.BinData[MeterLutFile.IndexDataLength + 2] << 16)
+ (LutFile.BinData[MeterLutFile.IndexDataLength + 3] << 24));
// calculate CRC and compare this with the file CRC
var entireFile = new List<Byte>();
entireFile.AddRange(LutFile.BinData);
var payload = new List<Byte>();
payload.AddRange(entireFile.GetRange(MeterLutFile.IndexVersion,
MeterLutFile.LutFileDataLength + MeterLutFile.LengthVersion));
var calculatedCrc = Crc16Ccitt.CalculateMsb1021(payload.ToArray());
if (calculatedCrc != LutFile.FileCrc)
// Take the file content to validate the format based
var lutFileFormatValid = LutFile.ValidateLutFormat();
if (LutFile.CrcError)
{
ErrorMessage = "CRC mismatch!";
return false;
}
LutFile.CalculatedCrc = calculatedCrc;
LutFile.IsValid = true;
return true;
return lutFileFormatValid;
}
/// <summary>