265 lines
9.8 KiB
C
Raw Normal View History

2020-12-01 12:43:43 +01:00
#include <cmdHelper.h>
#include <logger.h>
#include <eeprom.h>
#include <config.h>
2020-12-11 12:10:26 +01:00
#include <string.h>
2020-12-15 10:14:16 +01:00
#include <stdlib.h>
2020-12-11 12:10:26 +01:00
2020-12-01 12:43:43 +01:00
static bool showConfigCmd(uint8_t argc, char **args) {
bool retCode = true;
t_configBlock configBlock;
eepromReadConfigBlock(&configBlock);
2020-12-15 10:14:16 +01:00
sendString("This is the saved configuration.\n\r");
sendString("It is not necessarily the active configuration.\n\r");
2020-12-01 12:43:43 +01:00
sendFormatString("configMagic: %lx\n\r", configBlock.configMagic);
sendFormatString("deviceName: %s\n\r", configBlock.deviceName);
sendFormatString("MAC address: %02x:%02x:%02x:%02x:%02x:%02x\n\r", configBlock.macAddress[0],
configBlock.macAddress[1],
configBlock.macAddress[2],
configBlock.macAddress[3],
configBlock.macAddress[4],
configBlock.macAddress[5]);
sendFormatString("frontend threshold: %ld\n\r", configBlock.frontendThreshold);
sendFormatString("broker: %s\n\r", configBlock.brokerName);
sendFormatString("watchdogTopic: %s\n\r", configBlock.watchdogTopic);
sendFormatString("startupTopic: %s\n\r", configBlock.startupTopic);
sendFormatString("statusTopic: %s\n\r", configBlock.statusTopic);
sendFormatString("mbusDataTopic: %s\n\r", configBlock.mbusDataTopic);
sendFormatString("syslog server: %s\n\r", configBlock.syslogServerName);
sendFormatString("device block cnt: %d\n\r", configBlock.numOfDeviceBlocks);
for (uint8_t i = 0; i < configBlock.numOfDeviceBlocks; i++) {
t_deviceBlock tmpDeviceBlock;
eepromReadDeviceBlock(i, &tmpDeviceBlock);
if (tmpDeviceBlock.deviceMagic == DEVICE_MAGIC) {
sendFormatString("device %d: \n\r", i);
sendFormatString(" Name: %s, Address: %d, Period: %d\n\r",
tmpDeviceBlock.deviceName, tmpDeviceBlock.address, tmpDeviceBlock.period);
sendFormatString(" Considered Fields: %d %d %d %d\n\r",
tmpDeviceBlock.consideredField[0],
tmpDeviceBlock.consideredField[1],
tmpDeviceBlock.consideredField[2],
tmpDeviceBlock.consideredField[3]);
if (tmpDeviceBlock.deviceMagic != DEVICE_MAGIC) {
sendString(" DEVICE MAGIC DOES NOT MATCH\n\r");
}
}
}
return retCode;
}
2020-12-11 12:42:16 +01:00
2020-12-11 22:48:22 +01:00
2020-12-15 10:14:16 +01:00
static bool setStringParameterCmd(uint8_t argc, char **args, size_t offset, size_t length) {
2020-12-11 12:42:16 +01:00
bool retCode = true;
2020-12-11 12:45:06 +01:00
t_configBlock configBlock;
2020-12-11 22:48:22 +01:00
char *parameterName = args[1];
char *newParameterValue = args[2];
2020-12-15 10:14:16 +01:00
if (strlen(newParameterValue) >= length) {
sendString("given new value for is too long\n\r");
2020-12-11 12:42:16 +01:00
retCode = false;
} else {
2020-12-11 22:48:22 +01:00
sendFormatString("set %s to %s\n\r", parameterName, newParameterValue);
2020-12-11 12:42:16 +01:00
eepromReadConfigBlock(&configBlock);
2020-12-13 22:26:39 +01:00
strcpy((((char*)&configBlock) + offset), newParameterValue);
2020-12-11 12:42:16 +01:00
eepromWriteConfigBlock(&configBlock);
}
return retCode;
}
2020-12-12 22:40:56 +01:00
2020-12-15 10:14:16 +01:00
static bool setInt32ParameterCmd(uint8_t argc, char **args, size_t offset, int32_t minV, int32_t maxV) {
bool retCode = true;
t_configBlock configBlock;
char *parameterName = args[1];
char *newParameterValue = args[2];
long int value = strtol(newParameterValue, NULL, 10);
if (value < minV) {
sendString("value is too small\n\r");
retCode = false;
} else if (value > maxV) {
sendString("value is too large\n\r");
} else {
int32_t v = (int32_t) value;
sendFormatString("set %s to %ld\n\r", parameterName, v);
eepromReadConfigBlock(&configBlock);
*((int32_t*)(((uint8_t*)&configBlock) + offset)) = v;
eepromWriteConfigBlock(&configBlock);
}
return retCode;
}
2020-12-12 22:40:56 +01:00
static bool setDeviceNameCmd(uint8_t argc, char **args) {
2020-12-15 10:14:16 +01:00
return setStringParameterCmd(argc, args,
offsetof(t_configBlock, deviceName),
sizeof(((t_configBlock*)0)->deviceName));
2020-12-12 22:40:56 +01:00
}
2020-12-13 22:26:39 +01:00
static bool setBrokerNameCmd(uint8_t argc, char **args) {
2020-12-15 10:14:16 +01:00
return setStringParameterCmd(argc, args,
offsetof(t_configBlock, brokerName),
sizeof(((t_configBlock*)0)->brokerName));
2020-12-13 22:26:39 +01:00
}
static bool setSyslogServerCmd(uint8_t argc, char **args) {
2020-12-15 10:14:16 +01:00
return setStringParameterCmd(argc, args,
offsetof(t_configBlock, syslogServerName),
sizeof(((t_configBlock*)0)->syslogServerName));
2020-12-13 22:26:39 +01:00
}
static bool setWatchdogTopicCmd(uint8_t argc, char **args) {
2020-12-15 10:14:16 +01:00
return setStringParameterCmd(argc, args,
offsetof(t_configBlock, watchdogTopic),
sizeof(((t_configBlock*)0)->watchdogTopic));
2020-12-13 22:26:39 +01:00
}
static bool setStartupTopicCmd(uint8_t argc, char **args) {
2020-12-15 10:14:16 +01:00
return setStringParameterCmd(argc, args,
offsetof(t_configBlock, startupTopic),
sizeof(((t_configBlock*)0)->startupTopic));
2020-12-13 22:26:39 +01:00
}
static bool setStatusTopicCmd(uint8_t argc, char **args) {
2020-12-15 10:14:16 +01:00
return setStringParameterCmd(argc, args,
offsetof(t_configBlock, statusTopic),
sizeof(((t_configBlock*)0)->statusTopic));
2020-12-13 22:26:39 +01:00
}
static bool setMbusDataTopicCmd(uint8_t argc, char **args) {
2020-12-15 10:14:16 +01:00
return setStringParameterCmd(argc, args,
offsetof(t_configBlock, mbusDataTopic),
sizeof(((t_configBlock*)0)->mbusDataTopic));
2020-12-13 22:26:39 +01:00
}
2020-12-15 10:14:16 +01:00
static bool setFrontendThresholdCmd(uint8_t argc, char **args) {
return setInt32ParameterCmd(argc, args,
offsetof(t_configBlock, frontendThreshold),
0, 1023);
}
2020-12-12 22:40:56 +01:00
2020-12-11 12:45:06 +01:00
const static cmd_t SET_COMMANDS[] = {
2020-12-11 12:42:16 +01:00
{ .name = "devicename", .cmdFunc = setDeviceNameCmd,
.help = \
"devicename ........................... Name of this device\n\r"
},
2020-12-13 22:26:39 +01:00
{ .name = "brokername", .cmdFunc = setBrokerNameCmd,
.help = \
"brokername ........................... Hostname of the MQTT broker\n\r"
},
{ .name = "syslogserver", .cmdFunc = setSyslogServerCmd,
.help = \
"syslogserver ......................... Hostname of the Syslog server\n\r"
},
{ .name = "watchdogtopic", .cmdFunc = setWatchdogTopicCmd,
.help = \
"watchdogtopic ........................ Watchdog Topic\n\r"
},
{ .name = "startuptopic", .cmdFunc = setStartupTopicCmd,
.help = \
"startuptopic ......................... Startup Topic\n\r"
},
{ .name = "statustopic", .cmdFunc = setStatusTopicCmd,
.help = \
"statustopic .......................... Status Topic\n\r"
},
{ .name = "mbusdatatopic", .cmdFunc = setMbusDataTopicCmd,
.help = \
"mbusdatatopic ........................ MBus Data Topic\n\r"
},
2020-12-15 10:14:16 +01:00
{ .name = "frontendthreshold", .cmdFunc = setFrontendThresholdCmd,
.help = \
2020-12-15 10:19:29 +01:00
"frontendthreshold .................... Frontend Threshold (default: 240)\n\r"
2020-12-15 10:14:16 +01:00
},
2020-12-11 12:42:16 +01:00
{ .name = "END_OF_CMDS", .help = "",.cmdFunc = NULL }
};
2020-12-13 22:26:39 +01:00
2020-12-11 12:46:09 +01:00
const static char UNKNOWN_PARAMETER[] = "unknown parameter\n\r";
2020-12-11 12:45:06 +01:00
const static char OK_MSG[] = "OK\n\r";
const static char FAILED_MSG[] = "Failed\n\r";
2020-12-11 12:42:16 +01:00
2020-12-11 12:12:57 +01:00
static bool setCmd(uint8_t argc, char **args) {
2020-12-11 12:09:32 +01:00
bool retCode = false;
2020-12-11 12:42:16 +01:00
uint8_t *messageToSend = NULL;
2020-12-11 12:09:32 +01:00
2020-12-11 12:45:06 +01:00
char *cmd = args[1];
2020-12-11 12:18:22 +01:00
if (argc >= 2) {
2020-12-13 22:26:39 +01:00
if (0 == strcmp("help", cmd)) {
2020-12-11 12:09:32 +01:00
sendString("You can set the following parameters:\n\r");
2020-12-13 22:26:39 +01:00
uint8_t cmdIdx = 0;
while (true) {
cmd_t command = SET_COMMANDS[cmdIdx];
if (0 == strcmp("END_OF_CMDS", command.name)) {
break;
}
sendString(command.help);
cmdIdx++;
}
2020-12-11 12:09:32 +01:00
retCode = true;
2020-12-11 12:42:16 +01:00
} else {
uint8_t cmdIdx = 0;
while (true) {
cmd_t command = SET_COMMANDS[cmdIdx];
if (0 == strcmp("END_OF_CMDS", command.name)) {
messageToSend = (uint8_t*) UNKNOWN_PARAMETER;
break;
}
if (0 == strcmp(cmd, command.name)) {
2020-12-11 12:48:49 +01:00
retCode = command.cmdFunc(argc, args);
messageToSend = retCode ? (uint8_t*)OK_MSG : (uint8_t*)FAILED_MSG;
2020-12-13 22:26:39 +01:00
sendString("Remember you need to restart to active this change.\n\r");
2020-12-11 12:42:16 +01:00
break;
}
cmdIdx++;
}
2020-12-11 12:46:50 +01:00
sendString((const char*)messageToSend);
2020-12-11 12:09:32 +01:00
}
}
return retCode;
}
2020-12-01 12:43:43 +01:00
static bool restartCmd(uint8_t argc, char **args) {
HAL_NVIC_SystemReset();
2020-12-11 12:09:32 +01:00
// you won't come here ...
2020-12-01 12:43:43 +01:00
return true;
}
const cmd_t CONFIG_COMMANDS[] = {
{ .name = "show", .cmdFunc = showConfigCmd,
2020-12-11 12:42:16 +01:00
.help = \
2020-12-01 12:43:43 +01:00
"show ................................. Show the configuration\n\r"
},
2020-12-11 12:13:51 +01:00
{ .name = "set", .cmdFunc = setCmd,
2020-12-11 12:12:57 +01:00
.help = \
"set .................................. Set configuration parameters\n\r" \
2020-12-13 22:26:39 +01:00
" Argument help gives a list of \n\r" \
2020-12-11 12:12:57 +01:00
" parameters\n\r"
},
2020-12-01 12:43:43 +01:00
{ .name = "restart", .cmdFunc = restartCmd,
.help = \
"restart .............................. Restart the system,\n\r" \
" Required to reload config\n\r"
},
{ .name = "END_OF_CMDS", .help = "",.cmdFunc = NULL }
};
const cmd_t *getConfigCommands() {
return CONFIG_COMMANDS;
}