irrigation_system/software/lib/shell_modbus/shell_modbus.c

119 lines
3.3 KiB
C

/**
* @file shell_modbus.c
* @brief Provides shell commands for Modbus and valve configuration.
*
* This file implements a set of commands for the Zephyr shell to allow
* runtime configuration of the Modbus server (baudrate, slave ID) and the
* valve (max opening/closing times). The settings are persisted to non-volatile
* storage.
*/
#include <zephyr/shell/shell.h>
#include <lib/modbus_server.h>
#include <stdlib.h>
/**
* @brief Shell command to set the Modbus baudrate.
*
* @param sh The shell instance.
* @param argc Argument count.
* @param argv Argument values.
* @return 0 on success, -EINVAL on error.
*/
static int cmd_modbus_setb(const struct shell *sh, size_t argc, char **argv)
{
if (argc != 2) {
shell_error(sh, "Usage: setb <baudrate>");
return -EINVAL;
}
uint32_t new_baud = (uint32_t)strtoul(argv[1], NULL, 10);
const uint32_t valid_baud_rates[] = {1200, 2400, 4800, 9600, 19200, 38400, 57600, 115200};
bool is_valid = false;
for (int i = 0; i < ARRAY_SIZE(valid_baud_rates); i++) {
if (new_baud == valid_baud_rates[i]) {
is_valid = true;
break;
}
}
if (!is_valid) {
char error_msg[128];
int offset =
snprintf(error_msg, sizeof(error_msg), "Invalid baudrate. Valid rates are: ");
for (int i = 0; i < ARRAY_SIZE(valid_baud_rates); i++) {
offset += snprintf(error_msg + offset,
sizeof(error_msg) - offset,
"%u ",
valid_baud_rates[i]);
}
shell_error(sh, "%s", error_msg);
return -EINVAL;
}
if (modbus_reconfigure(new_baud, modbus_get_unit_id()) != 0) {
shell_error(sh, "Failed to apply new baudrate");
} else {
shell_print(sh, "Modbus baudrate set to: %u (and saved)", new_baud);
}
return 0;
}
/**
* @brief Shell command to set the Modbus slave ID.
*
* @param sh The shell instance.
* @param argc Argument count.
* @param argv Argument values.
* @return 0 on success, -EINVAL on error.
*/
static int cmd_modbus_setid(const struct shell *sh, size_t argc, char **argv)
{
if (argc != 2) {
shell_error(sh, "Usage: setid <slave_id>");
return -EINVAL;
}
uint32_t new_id_u32 = (uint32_t)strtoul(argv[1], NULL, 10);
if (new_id_u32 == 0 || new_id_u32 > 247) {
shell_error(sh, "Invalid slave ID: %s. Must be between 1 and 247.", argv[1]);
return -EINVAL;
}
uint8_t new_id = (uint8_t)new_id_u32;
if (modbus_reconfigure(modbus_get_baudrate(), new_id) != 0) {
shell_error(sh, "Failed to apply new slave ID");
} else {
shell_print(sh, "Modbus slave ID set to: %u (and saved)", new_id);
}
return 0;
}
/**
* @brief Shell command to show the current Modbus configuration.
*
* @param sh The shell instance.
* @param argc Argument count.
* @param argv Argument values.
* @return 0 on success.
*/
static int cmd_modbus_show(const struct shell *sh, size_t argc, char **argv)
{
const int label_width = 15;
shell_print(sh, "Modbus Settings:");
shell_print(sh, "%*s %u", label_width, "Baudrate:", modbus_get_baudrate());
shell_print(sh, "%*s %u", label_width, "Slave ID:", modbus_get_unit_id());
return 0;
}
SHELL_STATIC_SUBCMD_SET_CREATE(sub_modbus_cmds,
SHELL_CMD(setb, NULL, "Set Modbus baudrate", cmd_modbus_setb),
SHELL_CMD(setid, NULL, "Set Modbus slave ID", cmd_modbus_setid),
SHELL_CMD(show, NULL, "Show Modbus configuration", cmd_modbus_show),
SHELL_SUBCMD_SET_END);
SHELL_CMD_REGISTER(modbus, &sub_modbus_cmds, "Modbus commands", NULL);