diff --git a/.changes/serial-min-command-duration.md b/.changes/serial-min-command-duration.md new file mode 100644 index 000000000..dc059d282 --- /dev/null +++ b/.changes/serial-min-command-duration.md @@ -0,0 +1,15 @@ +--- +kind: changed +--- +Enforce a minimum duration on the serial rftransmit command and ignore the duration of a stop + +## Release Note +Serial rftransmit command checks the duration differently + +The `rftransmit` serial command now refuses a `durationMs` shorter than 300 ms, and no longer needs a `durationMs` at all when the type is `stop`. + +- The 300 ms length of a stop is now the `OPENSHOCK_RF_MIN_COMMAND_DURATION_MS` Kconfig option, and the serial command uses the same value as its minimum +- For a stop, `durationMs` may be missing or invalid and is not read + +## Notices +- warning: **The serial `rftransmit` command rejects durations under 300 ms.** Scripts that send shorter commands now get an error instead of a transmission. Send at least 300 ms. diff --git a/components/core/src/serialization/JsonSerial.cpp b/components/core/src/serialization/JsonSerial.cpp index 4f9277032..7cedf7e6c 100644 --- a/components/core/src/serialization/JsonSerial.cpp +++ b/components/core/src/serialization/JsonSerial.cpp @@ -82,21 +82,25 @@ std::optional JsonSerial::ParseShockerCommand(JSON:: } uint8_t intensityU8 = static_cast(*intensityInt); - JSON::JsonView durationMs = root["durationMs"]; - if (!durationMs.valid()) { - OS_LOGE(TAG, "missing 'durationMs' field"); - return std::nullopt; - } - auto durationMsInt = durationMs.tryGetI64(); - if (!durationMsInt) { - OS_LOGE(TAG, "value at 'durationMs' is not a number"); - return std::nullopt; - } - if (*durationMsInt < 0 || *durationMsInt > UINT16_MAX) { - OS_LOGE(TAG, "value at 'durationMs' is out of range (0-65535)"); - return std::nullopt; + // A stop has no duration, so the field is neither required nor validated for it. + uint16_t durationMsU16 = 0; + if (*commandType != ShockerCommandType::Stop) { + JSON::JsonView durationMs = root["durationMs"]; + if (!durationMs.valid()) { + OS_LOGE(TAG, "missing 'durationMs' field"); + return std::nullopt; + } + auto durationMsInt = durationMs.tryGetI64(); + if (!durationMsInt) { + OS_LOGE(TAG, "value at 'durationMs' is not a number"); + return std::nullopt; + } + if (*durationMsInt < CONFIG_OPENSHOCK_RF_MIN_COMMAND_DURATION_MS || *durationMsInt > UINT16_MAX) { + OS_LOGE(TAG, "value at 'durationMs' is out of range (%d-65535)", CONFIG_OPENSHOCK_RF_MIN_COMMAND_DURATION_MS); + return std::nullopt; + } + durationMsU16 = static_cast(*durationMsInt); } - uint16_t durationMsU16 = static_cast(*durationMsInt); return ShockerCommand { .model = *modelType, diff --git a/components/device_control/Kconfig b/components/device_control/Kconfig index 7bfe01fa5..4c32a841e 100644 --- a/components/device_control/Kconfig +++ b/components/device_control/Kconfig @@ -7,6 +7,15 @@ menu "OpenShock radio" help Commands waiting for the transmitter task. A full queue drops the command. + config OPENSHOCK_RF_MIN_COMMAND_DURATION_MS + int "Minimum command duration (ms)" + range 100 2000 + default 300 + help + Shortest duration a command is transmitted for. A stop is sent as a + zero-intensity command of this length, and the serial console rejects + commands shorter than this. + config OPENSHOCK_RF_TERMINATOR_DURATION_MS int "Stop terminator duration (ms)" range 100 2000 diff --git a/components/device_control/src/radio/RFTransmitter.cpp b/components/device_control/src/radio/RFTransmitter.cpp index 838059151..485ec80ce 100644 --- a/components/device_control/src/radio/RFTransmitter.cpp +++ b/components/device_control/src/radio/RFTransmitter.cpp @@ -126,7 +126,7 @@ bool RFTransmitter::SendCommand(ShockerModelType model, uint16_t shockerId, Shoc type = ShockerCommandType::Vibrate; intensity = 0; - durationMs = 300; + durationMs = CONFIG_OPENSHOCK_RF_MIN_COMMAND_DURATION_MS; flags = kFlagOverwrite | kFlagStop; } else { OS_LOGD(TAG, "Command received: %u %u %u %u", std::to_underlying(model), shockerId, std::to_underlying(type), intensity); diff --git a/components/serial_console/src/command_handlers/rftransmit.cpp b/components/serial_console/src/command_handlers/rftransmit.cpp index 3e97c7c21..afd0e2c42 100644 --- a/components/serial_console/src/command_handlers/rftransmit.cpp +++ b/components/serial_console/src/command_handlers/rftransmit.cpp @@ -4,6 +4,12 @@ #include "serialization/JsonSerial.h" #include "json/Json.h" +#include + +#define STRINGIFY_(x) #x +#define STRINGIFY(x) STRINGIFY_(x) +#define MIN_DURATION_STR STRINGIFY(CONFIG_OPENSHOCK_RF_MIN_COMMAND_DURATION_MS) + static void handleRFTransmitCommand(std::string_view arg, bool isAutomated) { if (arg.empty()) { @@ -40,7 +46,7 @@ OpenShock::SerialCmds::CommandGroup OpenShock::SerialCmds::CommandHandlers::RfTr "id (number) ID of the shocker (0-65535)", "type (string) Type of the command (\"shock\", \"vibrate\", \"sound\", \"light\", \"stop\")", "intensity (number) Intensity of the command (0-255)", - "durationMs (number) Duration of the command in milliseconds (0-65535)" + "durationMs (number) Duration of the command in milliseconds (" MIN_DURATION_STR "-65535, ignored for \"stop\")" }; auto& cmd = group.addCommand("Transmit a RF command", handleRFTransmitCommand);