From 9f71620697f73269c68534cd54d9051210102f07 Mon Sep 17 00:00:00 2001 From: Wolfgang Hottgenroth Date: Thu, 17 Dec 2020 09:15:01 +0100 Subject: [PATCH] ChangeDevice --- cube/User/Inc/config.h | 1 + cube/User/Src/configCmds.c | 48 ++++++++++++++++++++++++++++++++++---- 2 files changed, 45 insertions(+), 4 deletions(-) diff --git a/cube/User/Inc/config.h b/cube/User/Inc/config.h index 41b7477..e4c261b 100644 --- a/cube/User/Inc/config.h +++ b/cube/User/Inc/config.h @@ -24,6 +24,7 @@ typedef struct __attribute__((__packed__)) s_configBlock { } t_configBlock; +#define MAX_MBUS_DEVICES 16 #define MBUSDEVICE_NAMELENGTH 16 #define MBUSDEVICE_NUM_OF_CONSIDEREDFIELDS 4 diff --git a/cube/User/Src/configCmds.c b/cube/User/Src/configCmds.c index 6a96a6d..e122556 100644 --- a/cube/User/Src/configCmds.c +++ b/cube/User/Src/configCmds.c @@ -150,11 +150,16 @@ static bool setFrontendThresholdCmd(uint8_t argc, char **args) { 0, 1023); } -static bool makeDevice(uint8_t argOffset, uint8_t argc, char **args, t_deviceBlock *deviceBlock) { - char *deviceName = args[1 + argOffset]; - if (strcmp(deviceName, "help") == 0) { +static bool makeDevice(uint8_t argc, char **args, t_deviceBlock *deviceBlock, uint8_t *index) { + if (strcmp(args[1], "help") == 0) { sendString("Arguments are\n\r"); + if (index) { + sendString("index "); + } sendString("deviceName address period field1 field2 field3 field4\n\r"); + if (index) { + sendString("index: index in list of devices\n\r"); + } sendString("deviceName: max. length = 16\n\r"); sendString("address: between 1 and 254\n\r"); sendString("period: in seconds, between 0 (disabled) and 86400 (1 day)\n\r"); @@ -162,11 +167,23 @@ static bool makeDevice(uint8_t argOffset, uint8_t argc, char **args, t_deviceBlo return false; } + uint8_t argOffset = 0; + if (index) { + argOffset = 1; + long int rawIndex = strtol(args[1], NULL, 10); + if (rawIndex < 0 || rawIndex > MAX_MBUS_DEVICES) { + sendString("illegal index\n\r"); + return false; + } + *index = (uint8_t) rawIndex; + } + if ((argc - argOffset) != 8) { sendString("wrong number of arguments\n\r"); return false; } + char *deviceName = args[1 + argOffset]; if (strlen(deviceName) >= sizeof(deviceBlock->deviceName)) { sendString("devicename too long\n\r"); return false; @@ -201,7 +218,7 @@ static bool makeDevice(uint8_t argOffset, uint8_t argc, char **args, t_deviceBlo static bool addDeviceCmd(uint8_t argc, char **args) { t_deviceBlock deviceBlock; - bool retCode = makeDevice(0, argc, args, &deviceBlock); + bool retCode = makeDevice(argc, args, &deviceBlock, NULL); if (retCode) { sendString("New device would be:\n\r"); sendFormatString(" Name: %s, Address: %d, Period: %d\n\r", @@ -214,6 +231,25 @@ static bool addDeviceCmd(uint8_t argc, char **args) { return retCode; } +static bool changeDeviceCmd(uint8_t argc, char **args) { + t_deviceBlock deviceBlock; + uint8_t index; + bool retCode = makeDevice(argc, args, &deviceBlock, &index); + if (retCode) { + sendString("Changed device would be:\n\r"); + sendFormatString(" Index: %d\n\r"); + sendFormatString(" Name: %s, Address: %d, Period: %d\n\r", + deviceBlock.deviceName, deviceBlock.address, deviceBlock.period); + for (uint8_t i = 0; i < MBUSDEVICE_NUM_OF_CONSIDEREDFIELDS; i++) { + sendFormatString(" Considered field: %d\n\r", deviceBlock.consideredField[i]); + } + } + + return retCode; +} + + + const static cmd_t SET_COMMANDS[] = { { .name = "devicename", .cmdFunc = setDeviceNameCmd, .help = \ @@ -318,6 +354,10 @@ const cmd_t CONFIG_COMMANDS[] = { .help = \ "addDevice ............................ Add a new device to the end of the list\n\r" }, + { .name = "changeDevice", .cmdFunc = changeDeviceCmd, + .help = \ + "changeDevice ......................... Change a new device by index\n\r" + }, { .name = "restart", .cmdFunc = restartCmd, .help = \ "restart .............................. Restart the system,\n\r" \