From b8075073720aefe4680afe8f368c508e3e97ea8a Mon Sep 17 00:00:00 2001 From: rechrtb Date: Tue, 28 May 2024 17:05:55 +0800 Subject: [PATCH] Fix handling change USB mode gcode command --- src/GCodes/GCodes2.cpp | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/src/GCodes/GCodes2.cpp b/src/GCodes/GCodes2.cpp index e39c7f35b..966f436e8 100644 --- a/src/GCodes/GCodes2.cpp +++ b/src/GCodes/GCodes2.cpp @@ -3773,21 +3773,34 @@ bool GCodes::HandleMcode(GCodeBuffer& gb, const StringRef& reply) THROWS(GCodeEx const size_t chan = gb.GetLimitedUIValue('P', NumSerialChannels); bool hostMode = false; bool seen = false; -#if SUPPORT_USB_DRIVE - if (chan == 0 && gb.TryGetBValue('H', hostMode, seen)) // switch modes first + + // Check if a USB mode change is requested through the 'H' parameter on channel 0. + // If not on channel 0, short-circuit evaluation means TryGetBValue is not performed + // and hostMode == false, result == GCodeResult:ok is unchanged. + if (chan == 0 && gb.TryGetBValue('H', hostMode, seen)) { +#if SUPPORT_USB_DRIVE if (!platform.SetUsbHostMode(hostMode, reply)) { - reply.printf("Unable to set to %s mode", hostMode ? "host" : "device"); + reply.printf("Unable to set to USB %s mode", hostMode ? "host" : "device"); result = GCodeResult::error; } - } #else - reply.printf("USB host mode unsupported"); - result = GCodeResult::error; + // No support for changing to host mode; changing to device mode is ignored. + if (hostMode) + { + reply.printf("USB host mode unsupported"); + result = GCodeResult::error; + } #endif - if (result == GCodeResult::ok && !hostMode) // switched to device mode with no error, handle other device mode args + } + + // Handle the other parameters if specified along with the change to USB device mode, + // no USB mode change requested, or communication parameter configuration on other channels. + if (result == GCodeResult::ok && !hostMode) { + seen = false; + GCodeBuffer * const gbp = (chan == 0) ? UsbGCode() : (chan == 1) ? AuxGCode() : Aux2GCode(); if (gb.Seen('B')) {