Class USB
- Namespace
- DuetControlServer.Link.Adapter
- Assembly
- DuetControlServer.dll
Class to handle the USB link to the firmware over ttyACM
[DiagnosticsPriority(-4)]
public class USB : IDiagnostics, ILinkAdapter
- Inheritance
-
USB
- Implements
- Inherited Members
Constructors
USB(EventLogger, ObjectModel, ILogger<USB>, IOptions<Settings>)
Constructor of this class
public USB(EventLogger eventLogger, ObjectModel model, ILogger<USB> logger, IOptions<Settings> settings)
Parameters
eventLoggerEventLoggerEvent logger
modelObjectModelObject model
loggerILogger<USB>Logger instance
settingsIOptions<Settings>Settings
Exceptions
- OperationCanceledException
Failed to connect to board
Properties
PacketsToRead
Returns the number of packets to read
public int PacketsToRead { get; }
Property Value
ProtocolVersion
Currently-used protocol version
public int ProtocolVersion { get; }
Property Value
Methods
Connect(CancellationToken)
Attempt to connect to the firmware by sending M576.1 and parsing the init response
public void Connect(CancellationToken cancellationToken = default)
Parameters
cancellationTokenCancellationTokenOptional cancellation token
DumpMalformedPacket()
Write the last packet + content for diagnostic purposes
public void DumpMalformedPacket()
FlashFirmwareSegment(Stream)
Flash another segment of the firmware via the IAP binary. The IAP sends a 0x1A ready byte before each block.
public bool FlashFirmwareSegment(Stream stream)
Parameters
streamStreamStream of the firmware binary
Returns
- bool
Whether another segment could be sent
GetMaxFullTransferDelay()
Get the maximum time between two full transfers
public double GetMaxFullTransferDelay()
Returns
- double
Time in ms
HadReset()
Check if the controller has been reset or disconnected. Unlike SPI (which uses sequence numbers), USB detects this via communication failure.
public bool HadReset()
Returns
- bool
Whether the controller has been reset
PerformFullTransfer(bool, CancellationToken)
Perform a full data transfer synchronously
public void PerformFullTransfer(bool connecting = false, CancellationToken cancellationToken = default)
Parameters
connectingboolWhether this an initial connection is being established
cancellationTokenCancellationTokenCancellation token to cancel the transfer
PrintDiagnostics(StringBuilder)
Print diagnostics to the given string builder
public void PrintDiagnostics(StringBuilder builder)
Parameters
builderStringBuilderTarget to write to
ReadAbortFile(out CodeChannel, out bool)
Read the content of an AbortFileHeader packet
public void ReadAbortFile(out CodeChannel channel, out bool abortAll)
Parameters
channelCodeChannelCode channel where all files are supposed to be aborted
abortAllboolWhether all files are supposed to be aborted
ReadCheckFileExists(out string)
Read a request to check if a file exists
public void ReadCheckFileExists(out string filename)
Parameters
filenamestringName of the file
ReadCloseFile(out uint)
Read a request to close a file
public void ReadCloseFile(out uint handle)
Parameters
handleuintFile handle
ReadCodeBufferUpdate(out ushort)
Read a code buffer update
public void ReadCodeBufferUpdate(out ushort bufferSpace)
Parameters
bufferSpaceushortBuffer space
ReadCodeChannel(out CodeChannel)
Read a code channel
public void ReadCodeChannel(out CodeChannel channel)
Parameters
channelCodeChannelCode channel that has acquired the lock
ReadDeleteFileOrDirectory(out string)
Read a request to delete a file or directory
public void ReadDeleteFileOrDirectory(out string filename)
Parameters
filenamestringName of the file
ReadDoCode(out CodeChannel, out string)
Read a code request
public void ReadDoCode(out CodeChannel channel, out string code)
Parameters
channelCodeChannelChannel to execute this code on
codestringCode to execute
ReadEvaluationResult(out CodeChannel?, out string, out object?)
Read the result of an expression evaluation request
public void ReadEvaluationResult(out CodeChannel? channel, out string expression, out object? result)
Parameters
channelCodeChannel?Channel where the evaluation was performed
expressionstringEvaluated expression
resultobjectResult
ReadFileChunkRequest(out string, out uint, out int)
Read a chunk of a FileChunk packet
public void ReadFileChunkRequest(out string filename, out uint offset, out int maxLength)
Parameters
ReadFileRequest(out uint, out int)
Read a request to read data from a file
public void ReadFileRequest(out uint handle, out int maxLength)
Parameters
ReadMacroRequest(out CodeChannel, out bool, out string)
Read the content of a ExecuteMacroHeader packet
public void ReadMacroRequest(out CodeChannel channel, out bool isSystemMacro, out string filename)
Parameters
channelCodeChannelChannel requesting a macro file
isSystemMacroboolIndicates if this code is not bound to a code being executed (e.g. when a trigger macro is requested)
filenamestringFilename of the requested macro
ReadMessage(out MessageTypeFlags, out string)
Read an incoming message
public void ReadMessage(out MessageTypeFlags messageType, out string reply)
Parameters
messageTypeMessageTypeFlagsMessage type flags of the reply
replystringCode reply
ReadNextPacket()
Read the next packet
public PacketHeader? ReadNextPacket()
Returns
- PacketHeader?
The next packet or null if none is available
ReadObjectModel(out ReadOnlySpan<byte>)
Read the result of a GetObjectModel request
public void ReadObjectModel(out ReadOnlySpan<byte> json)
Parameters
jsonReadOnlySpan<byte>JSON data
ReadOpenFile(out string, out bool, out bool, out long)
Read an open file request
public void ReadOpenFile(out string filename, out bool forWriting, out bool append, out long preAllocSize)
Parameters
filenamestringFilename to open
forWritingboolWhether the file is supposed to be written to
appendboolWhether data is supposed to be appended in write mode
preAllocSizelongHow many bytes to allocate if the file is created or overwritten
ReadPrintPaused(out uint, out uint, out PrintPausedReason)
Read the content of a PrintPausedHeader packet
public void ReadPrintPaused(out uint filePosition, out uint filePosition2, out PrintPausedReason reason)
Parameters
filePositionuintPosition where the print has been paused
filePosition2uintPosition where the second open file has been paused (if applicable)
reasonPrintPausedReasonReason why the print has been paused
ReadSeekFile(out uint, out long)
Read a request to seek in a file
public void ReadSeekFile(out uint handle, out long offset)
Parameters
ReadTruncateFile(out uint)
Read a request to truncate a file
public void ReadTruncateFile(out uint handle)
Parameters
handleuintFile handle
ReadWriteRequest(out uint, out ReadOnlySpan<byte>)
Read a request to write data to a file
public void ReadWriteRequest(out uint handle, out ReadOnlySpan<byte> data)
Parameters
handleuintFile handle
dataReadOnlySpan<byte>Data to write
ResendPacket(PacketHeader, out Request)
Resend a packet back to the firmware
public void ResendPacket(PacketHeader packet, out Request sbcRequest)
Parameters
packetPacketHeaderPacket holding the resend request
sbcRequestRequestContent of the packet to resend
StartIap(uint, CancellationToken)
Instruct the firmware to start the IAP binary
public void StartIap(uint firmwareLength, CancellationToken cancellationToken = default)
Parameters
firmwareLengthuintLength of the firmware binary in bytes; sent to IAP as part of the USB handshake
cancellationTokenCancellationTokenOptional cancellation token
VerifyFirmwareChecksum(long, ushort)
Send the CRC16 checksum of the firmware binary to the IAP program and verify the written data. Uses timing-based end-of-transfer detection (matching SPI IAP protocol).
public bool VerifyFirmwareChecksum(long firmwareLength, ushort crc16)
Parameters
firmwareLengthlongLength of the written firmware in bytes
crc16ushortCRC16 checksum of the firmware
Returns
- bool
Whether the firmware has been written successfully
WaitForIapReset()
Wait for the IAP program to reset the controller. Close the IAP serial port and flag for reconnection to the main firmware.
public void WaitForIapReset()
WriteCheckFileExistsResult(bool)
Send back whether a file exists or not
public bool WriteCheckFileExistsResult(bool exists)
Parameters
existsboolWhether the file exists
Returns
- bool
If the packet could be written
WriteCode(Code)
Request a code to be executed
public bool WriteCode(Code code)
Parameters
codeCodeCode to send
Returns
- bool
True if the packet could be written
WriteDeleteLocalVariable(CodeChannel, string)
Delete a local variable at the end of the current code block
public bool WriteDeleteLocalVariable(CodeChannel channel, string varName)
Parameters
channelCodeChannelG-code channel
varNamestringName of the variable excluding var prefix
Returns
- bool
True if the packet could be written
WriteEmergencyStop()
Request an emergency stop
public bool WriteEmergencyStop()
Returns
- bool
True if the packet could be written
WriteEvaluateExpression(CodeChannel, string)
Write a request for an expression evaluation
public bool WriteEvaluateExpression(CodeChannel channel, string expression)
Parameters
channelCodeChannelWhere to evaluate the expression
expressionstringExpression to evaluate
Returns
- bool
Whether the evaluation request has been written successfully
WriteFileChunk(Span<byte>, long)
Write another chunk of the file being requested
public bool WriteFileChunk(Span<byte> data, long fileLength)
Parameters
Returns
- bool
Whether the firmware has been written successfully
WriteFileDeleteResult(bool)
Send back whether a file or directory could be deleted
public bool WriteFileDeleteResult(bool success)
Parameters
successboolWhether the file operation was successful
Returns
- bool
If the packet could be written
WriteFileReadResult(Span<byte>, int)
Write requested read data from a file
public bool WriteFileReadResult(Span<byte> data, int bytesRead)
Parameters
Returns
- bool
If the packet could be written
WriteFileSeekResult(bool)
Tell RRF if the seek operation was successful
public bool WriteFileSeekResult(bool success)
Parameters
successboolIf the seek operation succeeded
Returns
- bool
If the packet could be written
WriteFileTruncateResult(bool)
Tell RRF if the seek operation was successful
public bool WriteFileTruncateResult(bool success)
Parameters
successboolIf the seek operation succeeded
Returns
- bool
If the packet could be written
WriteFileWriteResult(bool)
Tell RRF if the last file block could be written
public bool WriteFileWriteResult(bool success)
Parameters
successboolIf the file data could be written
Returns
- bool
If the packet could be written
WriteGetObjectModel(string, string)
Request the key of a object module of a specific module
public bool WriteGetObjectModel(string key, string flags)
Parameters
Returns
- bool
True if the packet could be written
WriteIapSegment(Stream, CancellationToken)
Write another segment of the IAP binary
public bool WriteIapSegment(Stream stream, CancellationToken cancellationToken = default)
Parameters
streamStreamIAP binary
cancellationTokenCancellationTokenOptional cancellation token
Returns
- bool
Whether another segment could be written
WriteInvalidateChannel(CodeChannel)
Called when a code channel is supposed to be invalidated (e.g. via abort keyword)
public bool WriteInvalidateChannel(CodeChannel channel)
Parameters
channelCodeChannelCode channel that requires the lock
Returns
- bool
True if the packet could be written
WriteLockAllMovementSystemsAndWaitForStandstill(CodeChannel)
Request the movement systems to be locked and wait for standstill
public bool WriteLockAllMovementSystemsAndWaitForStandstill(CodeChannel channel)
Parameters
channelCodeChannelCode channel that requires the lock
Returns
- bool
True if the packet could be written
WriteMacroCompleted(CodeChannel, bool)
Notify the firmware about a completed macro file. This function is only used for macro files that the firmware requested
public bool WriteMacroCompleted(CodeChannel channel, bool error)
Parameters
channelCodeChannelCode channel of the finished macro
errorboolWhether an error occurred
Returns
- bool
True if the packet could be written
WriteMacroStarted(CodeChannel)
Notify RepRapFirmware that a macro file could be started
public bool WriteMacroStarted(CodeChannel channel)
Parameters
channelCodeChannelCode channel that requires the lock
Returns
- bool
True if the packet could be written
WriteMessage(MessageTypeFlags, string)
Write a message
public bool WriteMessage(MessageTypeFlags flags, string message)
Parameters
flagsMessageTypeFlagsMessage flags
messagestringMessage content
Returns
- bool
Whether the firmware has been written successfully
WriteObjectModelKeyChanged(string)
Notify RRF that an object model key has changed
public bool WriteObjectModelKeyChanged(string key)
Parameters
keystringKey that has changed
Returns
- bool
If the packet could be written
WriteOpenFileResult(uint, long)
Write the new file handle and file length of the file that has just been opened
public bool WriteOpenFileResult(uint fileHandle, long length)
Parameters
fileHandleuintNew file handle or noFileHandle if the file could not be opened
lengthlongLength of the file
Returns
- bool
If the packet could be written
WritePrintFileInfo(GCodeFileInfo)
Notify the firmware that a file print has started
public bool WritePrintFileInfo(GCodeFileInfo info)
Parameters
infoGCodeFileInfoInformation about the file being printed
Returns
- bool
True if the packet could be written
WritePrintStopped(PrintStoppedReason)
Notify that a file print has been stopped
public bool WritePrintStopped(PrintStoppedReason reason)
Parameters
reasonPrintStoppedReasonReason why the print has been stopped
Returns
- bool
True if the packet could be written
WriteReset()
Request a firmware reset
public bool WriteReset()
Returns
- bool
True if the packet could be written
WriteSetLastCodeResult(CodeChannel, CodeResult)
Write the last code result for a specific code channel
public bool WriteSetLastCodeResult(CodeChannel channel, CodeResult result)
Parameters
channelCodeChannelCode channel
resultCodeResultLast code result
Returns
- bool
If the packet could be written
WriteSetVariable(CodeChannel, bool, string, string)
Set a global or local variable
public bool WriteSetVariable(CodeChannel channel, bool createVariable, string varName, string expression)
Parameters
channelCodeChannelG-code channel
createVariableboolWhether the variable should be created or updated
varNamestringName of the variable including global or var prefix
expressionstringNew value of the variable
Returns
- bool
True if the packet could be written
WriteUnlock(CodeChannel)
Release all acquired locks again
public bool WriteUnlock(CodeChannel channel)
Parameters
channelCodeChannelCode channel that releases the locks
Returns
- bool
True if the packet could be written