using System; using System.Collections.Generic; using DTS.Common.Enums.DASFactory; using DTS.Common.ICommunication; namespace DTS.DASLib.Command.SLICE { public abstract class FirmwareUpdateCommands : CommandBase { protected enum Commands { Reserved = 0x00, QueryFirmwareUpdateBlockSize = 0x01, QueryBootloaderVersion = 0x02, SwitchToFirmwareUpdateMode = 0x03, BeginFirmwareUpdate = 0x04, ProgramFirmwareUpdateBlock = 0x05, FinishFirmwareUpdate = 0x06, Reboot = 0x07, QueryInFirmwareUpdateMode = 0x08, // added to support slice pro (Bridge/IEPE) firmware update via USB. Load_SliceProStackFirmwareImage = 0x09, // CMDFW_LOAD_STACK_FW, // = 0x09, loading (in 400-byte blocks) firmware image to base NAND. Query_SliceProStackFirmwareImage = 0x0A, // CMDFW_QUERY_STACK_FW, // = 0x0A, reading block (in 400-byte blocks) of firmware image from base NAND at a time. Query_SliceProStackFirmwareCRC = 0x0B, // CMDFW_QUERY_STACK_FW_CRC, // = 0x0B, read firmware CRC for validation purpose. Start_SliceProStackFirmwareUpdate = 0x0C, // CMDFW_START_STACK_FW_UPDADE, // = 0x0C, stack module to be upgraded with firmware. Stop_SliceProStackFirmwareUpdate = 0x0D, // CMDFW_STOP_STACK_FW_UPDADE, // = 0x0D, stop firmware upgrade for after completes the on-going one. Query_SliceProStackFirmwareUpdateMode = 0x0E, // CMDFW_QUERY_STACK_FW_UPDATE_MODE, // = 0x0E, polling updatae mode from stack Set_SliceProStackFirmwareUpdateMode = 0x0F, // CMDFW_SET_STACK_FW_UPDATE_MODE, // = 0x0F, Set stack module to update mode MAX_CMDFW_COMMAND // = 0x10 }; public enum FirmwareMode { FW = 0, BL = 1, BLL = 2, }; //protected const int MIN_SLICE2_FILEDATA_PROTOCOL = 130; //SLICE 2 Protocol cmd protected const int MAX_FILE_ID = 30; // user = 10, firmware = 20, max internally = 65. 10; protected const int MAX_FILE_LENGTH = 400; protected abstract Commands _Command { get; } protected FirmwareUpdateCommands(DTS.Common.Interface.DASFactory.ICommunication sock) : base(sock) { command.Type = CommandPacket.CommandType.FirmwareUpdate; command.SetCommand((byte)_Command, _Command.ToString()); } protected FirmwareUpdateCommands(DTS.Common.Interface.DASFactory.ICommunication sock, int TimeoutMillisec) : base(sock, TimeoutMillisec) { command.Type = CommandPacket.CommandType.FirmwareUpdate; command.SetCommand((byte)_Command, _Command.ToString()); } } public class QueryFirmwareUpdateBlockSize : FirmwareUpdateCommands { protected override Commands _Command => Commands.QueryFirmwareUpdateBlockSize; private ushort _size; public ushort BlockSize => _size; public QueryFirmwareUpdateBlockSize(DTS.Common.Interface.DASFactory.ICommunication sock) : base(sock) { _size = 0; } public QueryFirmwareUpdateBlockSize(DTS.Common.Interface.DASFactory.ICommunication sock, int TimeoutMillisec) : base(sock, TimeoutMillisec) { _size = 0; } protected override CommandReceiveAction WholePackage() { if (response.Status == DFConstantsAndEnums.CommandStatus.StatusNoError) { response.GetParameter(0, out _size); } return CommandReceiveAction.StopReceiving; } public override void ResponseToString(ref List> lines) { base.CommandToString(ref lines); lines.Add(new List() { $"BlockSize: {BlockSize}" }); } } public class QueryBootloaderVersion : FirmwareUpdateCommands { protected override Commands _Command => Commands.QueryBootloaderVersion; private string _version = string.Empty; public string Version => _version; public QueryBootloaderVersion(DTS.Common.Interface.DASFactory.ICommunication sock) : base(sock) { } public QueryBootloaderVersion(DTS.Common.Interface.DASFactory.ICommunication sock, int TimeoutMillisec) : base(sock, TimeoutMillisec) { } protected override CommandReceiveAction WholePackage() { if (response.Status == DFConstantsAndEnums.CommandStatus.StatusNoError) { response.GetParameter(0, out _version); } return CommandReceiveAction.StopReceiving; } public override void ResponseToString(ref List> lines) { base.ResponseToString(ref lines); lines.Add(new List() { $"Version: {Version}" }); } } public class SwitchToFirmwareUpdateMode : FirmwareUpdateCommands { protected override Commands _Command => Commands.SwitchToFirmwareUpdateMode; public SwitchToFirmwareUpdateMode(DTS.Common.Interface.DASFactory.ICommunication sock) : base(sock) { } public SwitchToFirmwareUpdateMode(DTS.Common.Interface.DASFactory.ICommunication sock, int TimeoutMillisec) : base(sock, TimeoutMillisec) { } } public class BeginFirmwareUpdate : FirmwareUpdateCommands { protected override Commands _Command => Commands.BeginFirmwareUpdate; public BeginFirmwareUpdate(DTS.Common.Interface.DASFactory.ICommunication sock) : base(sock) { } public BeginFirmwareUpdate(DTS.Common.Interface.DASFactory.ICommunication sock, int TimeoutMillisec) : base(sock, TimeoutMillisec) { } } public class ProgramFirmwareUpdateBlock : FirmwareUpdateCommands { protected override Commands _Command => Commands.ProgramFirmwareUpdateBlock; private uint _address; private ushort _blockcrc; private ushort _upperlower; private byte[] _data; public uint BlockBaseAddress { get => _address; set { _address = value; command.SetParameter(0, _address); } } public ushort BlockSize { get; private set; } public ushort BlockCRC16 { get => _blockcrc; set { _blockcrc = value; command.SetParameter(6, _blockcrc); } } public bool Upper { get => _upperlower == 1 ? true : false; set { if (value) { _upperlower = 1; command.SetParameter(8, _upperlower); } } } public byte[] Data { get => _data; set { if (null == value) { return; } _data = value; if (command.Parameter.Length < value.Length + 10) { var newparameter = new byte[value.Length + 10]; BlockSize = (ushort)value.Length; command.SetParameter(4, BlockSize); Buffer.BlockCopy(command.Parameter, 0, newparameter, 0, 10); command.Parameter = newparameter; Buffer.BlockCopy(_data, 0, command.Parameter, 10, _data.Length); } } } public ProgramFirmwareUpdateBlock(DTS.Common.Interface.DASFactory.ICommunication sock) : base(sock) { command.Parameter = new byte[10]; Upper = false; } public ProgramFirmwareUpdateBlock(DTS.Common.Interface.DASFactory.ICommunication sock, int TimeoutMillisec) : base(sock, TimeoutMillisec) { command.Parameter = new byte[10]; Upper = false; } public override void CommandToString(ref List> lines) { base.CommandToString(ref lines); lines.Add(new List() { $"BlockBaseAddress: {BlockBaseAddress}, BlockSize: {BlockSize}, BlockCRC16: {BlockCRC16}, Upper: {Upper}" }); } } public class FinishFirmwareUpdate : FirmwareUpdateCommands { protected override Commands _Command => Commands.FinishFirmwareUpdate; private ushort _imagecrc; public ushort ImageCRC16 { get => _imagecrc; set { _imagecrc = value; command.SetParameter(0, _imagecrc); } } public FinishFirmwareUpdate(DTS.Common.Interface.DASFactory.ICommunication sock) : base(sock) { command.Parameter = new byte[2]; } public FinishFirmwareUpdate(DTS.Common.Interface.DASFactory.ICommunication sock, int TimeoutMillisec) : base(sock, TimeoutMillisec) { command.Parameter = new byte[2]; } public override void CommandToString(ref List> lines) { base.CommandToString(ref lines); lines.Add(new List() { $"ImageCRC16: {ImageCRC16}" }); } public override void ResponseToString(ref List> lines) { base.ResponseToString(ref lines); lines.Add(new List() { $"ImageCRC16: {ImageCRC16}" }); } } public class Reboot : FirmwareUpdateCommands { protected override Commands _Command => Commands.Reboot; public Reboot(DTS.Common.Interface.DASFactory.ICommunication sock) : base(sock) { } public Reboot(DTS.Common.Interface.DASFactory.ICommunication sock, int TimeoutMillisec) : base(sock, TimeoutMillisec) { } } public class QueryInFirmwareUpdateMode : FirmwareUpdateCommands { protected override Commands _Command => Commands.QueryInFirmwareUpdateMode; private byte _updateMode = (byte)FirmwareMode.FW; public bool InUpdateMode => FirmwareMode.FW != (FirmwareMode)_updateMode; public bool IsFwMode => FirmwareMode.FW == (FirmwareMode)_updateMode; public bool IsBlMode => FirmwareMode.BL == (FirmwareMode)_updateMode; public bool IsBllMode => FirmwareMode.BLL == (FirmwareMode)_updateMode; public FirmwareMode Mode => (FirmwareMode)_updateMode; public QueryInFirmwareUpdateMode(DTS.Common.Interface.DASFactory.ICommunication sock) : base(sock) { } public QueryInFirmwareUpdateMode(DTS.Common.Interface.DASFactory.ICommunication sock, int TimeoutMillisec) : base(sock, TimeoutMillisec) { } protected override CommandReceiveAction WholePackage() { if (response.Status == DFConstantsAndEnums.CommandStatus.StatusNoError) { response.GetParameter(0, out _updateMode); } return CommandReceiveAction.StopReceiving; } public override void ResponseToString(ref List> lines) { base.ResponseToString(ref lines); lines.Add(new List() { string.Format($"UpdateMode: {(FirmwareMode)_updateMode}") }); } } /// ///These stack firmware command updates need at least protocol version 137 ///No parameter. All slice in bootloader mode will be updated with new firmware. /// /// public class Load_SliceProStackFirmwareImage : FirmwareUpdateCommands { protected override Commands _Command => Commands.Load_SliceProStackFirmwareImage; #region Private Variables private readonly ushort _fileID; //Byte 0-1: 16-bit file ID ranges from 1-10. private uint _startByteCount; private byte[] _data; private static byte PARAM_NUM = 6; #endregion #region Command Parameters public uint StartByteCount { get => _startByteCount; set { _startByteCount = value; command.SetParameter(2, _startByteCount); } } public byte[] Data { get => _data; set { if (null == value) { return; } if (MAX_FILE_LENGTH < value.Length) { throw new NotImplementedException(); } _data = value; //if (this.command.Parameter.Length < value.Length+ PARAM_NUM) //{ byte[] newparameter = new byte[value.Length + PARAM_NUM]; Buffer.BlockCopy(command.Parameter, 0, newparameter, 0, PARAM_NUM); Size = value.Length; command.Parameter = newparameter; Buffer.BlockCopy(_data, 0, command.Parameter, PARAM_NUM, _data.Length); //} } } public int Size { get; private set; } public int MaximumFileStreamBytes => MAX_FILE_LENGTH; #endregion #region Command Functions public Load_SliceProStackFirmwareImage(DTS.Common.Interface.DASFactory.ICommunication sock) : base(sock) { command.Parameter = new byte[PARAM_NUM]; MinimumProtocolVersion = sock.GetMinProto(DFConstantsAndEnums.ProtocolLimitedCommands.StackFirmwareUpdate); _data = null; _fileID = 20; command.SetParameter(0, _fileID); _startByteCount = 0; command.ShouldLog = false; } public Load_SliceProStackFirmwareImage(DTS.Common.Interface.DASFactory.ICommunication sock, int TimeoutMillisec) : base(sock, TimeoutMillisec) { command.Parameter = new byte[PARAM_NUM]; MinimumProtocolVersion = sock.GetMinProto(DFConstantsAndEnums.ProtocolLimitedCommands.StackFirmwareUpdate); _data = null; _fileID = 20; command.SetParameter(0, _fileID); _startByteCount = 0; command.ShouldLog = false; } #endregion #region Log Functions public override void CommandToString(ref List> lines) { lines.Add(new List() { $"Store ID: {_fileID}, Start Byte: {StartByteCount}, End Byte: {Size + StartByteCount}" }); } public void LogResponse() { LogCommand(false); } #endregion } public class Start_SliceProStackFirmwareUpdate : FirmwareUpdateCommands { protected override Commands _Command => Commands.Start_SliceProStackFirmwareUpdate; public Start_SliceProStackFirmwareUpdate(DTS.Common.Interface.DASFactory.ICommunication sock) : base(sock) { MinimumProtocolVersion = sock.GetMinProto(DFConstantsAndEnums.ProtocolLimitedCommands.StackFirmwareUpdate); command.Type = CommandPacket.CommandType.FirmwareUpdate; command.SetCommand((byte)_Command, _Command.ToString()); } public Start_SliceProStackFirmwareUpdate(DTS.Common.Interface.DASFactory.ICommunication sock, int TimeoutMillisec) : base(sock, TimeoutMillisec) { MinimumProtocolVersion = sock.GetMinProto(DFConstantsAndEnums.ProtocolLimitedCommands.StackFirmwareUpdate); command.Type = CommandPacket.CommandType.FirmwareUpdate; command.SetCommand((byte)_Command, _Command.ToString()); } } public class Set_SliceProStackFirmwareUpdateMode : FirmwareUpdateCommands { protected override Commands _Command => Commands.Set_SliceProStackFirmwareUpdateMode; private int _sliceCount = 0; private byte[] _sliceProUpdateMode; public int SliceModuleCount { get => _sliceCount; set { _sliceCount = value; _sliceProUpdateMode = new byte[_sliceCount]; } } public byte[] SliceModuledateMode { get => _sliceProUpdateMode; set { for (int i = 0; i < _sliceCount; i++) { _sliceProUpdateMode[i] = value[i]; } command.Parameter = _sliceProUpdateMode; } } public Set_SliceProStackFirmwareUpdateMode(DTS.Common.Interface.DASFactory.ICommunication sock) : base(sock) { MinimumProtocolVersion = sock.GetMinProto(DFConstantsAndEnums.ProtocolLimitedCommands.StackFirmwareUpdate); command.Type = CommandPacket.CommandType.FirmwareUpdate; command.SetCommand((byte)_Command, _Command.ToString()); } public Set_SliceProStackFirmwareUpdateMode(DTS.Common.Interface.DASFactory.ICommunication sock, int TimeoutMillisec) : base(sock, TimeoutMillisec) { MinimumProtocolVersion = sock.GetMinProto(DFConstantsAndEnums.ProtocolLimitedCommands.StackFirmwareUpdate); command.Type = CommandPacket.CommandType.FirmwareUpdate; command.SetCommand((byte)_Command, _Command.ToString()); } } public class Get_SliceProStackFirmwareUpdateMode : FirmwareUpdateCommands { protected override Commands _Command => Commands.Query_SliceProStackFirmwareUpdateMode; public int SliceModuleCount { get; private set; } = 0; public byte[] SliceModuledateMode { get; private set; } public Get_SliceProStackFirmwareUpdateMode(DTS.Common.Interface.DASFactory.ICommunication sock) : base(sock) { MinimumProtocolVersion = sock.GetMinProto(DFConstantsAndEnums.ProtocolLimitedCommands.StackFirmwareUpdate); command.Type = CommandPacket.CommandType.FirmwareUpdate; command.SetCommand((byte)_Command, _Command.ToString()); } public Get_SliceProStackFirmwareUpdateMode(DTS.Common.Interface.DASFactory.ICommunication sock, int TimeoutMillisec) : base(sock, TimeoutMillisec) { MinimumProtocolVersion = sock.GetMinProto(DFConstantsAndEnums.ProtocolLimitedCommands.StackFirmwareUpdate); command.Type = CommandPacket.CommandType.FirmwareUpdate; command.SetCommand((byte)_Command, _Command.ToString()); } protected override CommandReceiveAction WholePackage() { if (response.Status == DFConstantsAndEnums.CommandStatus.StatusNoError) { SliceModuleCount = response.Parameter.Length; SliceModuledateMode = new byte[SliceModuleCount]; for (int i = 0; i < SliceModuleCount; i++) { response.GetParameter(i, out SliceModuledateMode[i]); } } return CommandReceiveAction.StopReceiving; } } }