408 lines
15 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-17 09:34:34 +01:00
static bool makeDevice(uint8_t argOffset, uint8_t argc, char **args, t_deviceBlock *deviceBlock) {
2020-12-17 09:15:01 +01:00
if (strcmp(args[1], "help") == 0) {
2020-12-17 09:00:00 +01:00
sendString("deviceName address period field1 field2 field3 field4\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");
sendString("fields: between -1 (not considered) and 254\n\r");
2020-12-17 09:02:46 +01:00
return false;
2020-12-17 09:00:00 +01:00
}
2020-12-17 09:04:27 +01:00
2020-12-17 09:15:01 +01:00
2020-12-17 09:04:27 +01:00
if ((argc - argOffset) != 8) {
sendString("wrong number of arguments\n\r");
return false;
}
2020-12-17 09:15:01 +01:00
char *deviceName = args[1 + argOffset];
2020-12-17 15:59:44 +01:00
if (strcmp(deviceName, "*") != 0) {
if (strlen(deviceName) >= sizeof(deviceBlock->deviceName)) {
sendString("devicename too long\n\r");
return false;
}
strcpy(deviceBlock->deviceName, deviceName);
2020-12-16 21:12:48 +01:00
}
2020-12-16 20:25:06 +01:00
2020-12-17 15:59:44 +01:00
char *rawAddressStr = args[2 + argOffset];
if (strcmp(rawAddressStr, "*") != 0) {
long int rawAddress = strtol(rawAddressStr, NULL, 10);
if (rawAddress < 1 || rawAddress > 254) {
sendString("illegal address\n\r");
return false;
}
deviceBlock->address = (uint8_t)rawAddress;
2020-12-16 20:25:06 +01:00
}
2020-12-16 21:12:48 +01:00
2020-12-17 15:59:44 +01:00
char *rawPeriodStr = args[3 + argOffset];
if (strcmp(rawPeriodStr, "*") != 0) {
long int rawPeriod = strtol(rawPeriodStr, NULL, 10);
if (rawPeriod < 0 || rawPeriod > 86400) {
sendString("illegal period\n\r");
return false;
}
deviceBlock->period = (int32_t) rawPeriod;
2020-12-17 09:00:00 +01:00
}
2020-12-16 20:25:06 +01:00
2020-12-17 09:00:00 +01:00
for (uint8_t i = 0; i < MBUSDEVICE_NUM_OF_CONSIDEREDFIELDS; i++) {
2020-12-17 15:59:44 +01:00
char *rawFieldNumStr = args[4 + i + argOffset];
if (strcmp(rawFieldNumStr, "*") != 0) {
long int rawFieldNum = strtol(rawFieldNumStr, NULL, 10);
if (rawFieldNum < -1 || rawFieldNum > 127) {
sendString("illegal considered field index\n\r");
return false;
}
deviceBlock->consideredField[i] = (int8_t) rawFieldNum;
2020-12-17 09:00:00 +01:00
}
}
2020-12-16 20:25:06 +01:00
return true;
}
2020-12-17 09:00:00 +01:00
static bool addDeviceCmd(uint8_t argc, char **args) {
2020-12-17 15:59:44 +01:00
t_deviceBlock deviceBlock = { .deviceName = "", .address = 0, .period = 0, .consideredField = { -1, -1, -1, -1}};
2020-12-17 09:34:34 +01:00
bool retCode = makeDevice(0, argc, args, &deviceBlock);
2020-12-17 09:02:46 +01:00
if (retCode) {
sendString("New device would be:\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]);
}
2020-12-17 09:00:00 +01:00
}
return retCode;
}
2020-12-17 09:15:01 +01:00
static bool changeDeviceCmd(uint8_t argc, char **args) {
2020-12-17 16:01:52 +01:00
if (strcmp(args[1], "help") == 0) {
2020-12-17 09:34:34 +01:00
sendString("First argument: index of device in list\n\r");
sendFormatString("Between 0 and %d\n\r", getConfig()->numOfDeviceBlocks);
2020-12-17 15:59:44 +01:00
sendString("For further arguments use a * to keep the value\n\r");
2020-12-17 09:34:34 +01:00
return makeDevice(0, argc, args, NULL);
}
2020-12-17 13:44:49 +01:00
long int rawIndex = strtol(args[1], NULL, 10);
if (rawIndex < 0 || rawIndex > getConfig()->numOfDeviceBlocks) {
sendFormatString("illegal index, must be greater 0 and less %d\n\r", getConfig()->numOfDeviceBlocks);
return false;
}
uint8_t index = (uint8_t) rawIndex;
2020-12-17 09:15:01 +01:00
t_deviceBlock deviceBlock;
2020-12-17 15:59:44 +01:00
eepromReadDeviceBlock(index, &deviceBlock);
2020-12-17 09:34:34 +01:00
bool retCode = makeDevice(1, argc, args, &deviceBlock);
2020-12-17 09:15:01 +01:00
if (retCode) {
2020-12-17 16:11:11 +01:00
sendString("Changed device will be:\n\r");
2020-12-17 09:17:07 +01:00
sendFormatString(" Index: %d\n\r", index);
2020-12-17 09:15:01 +01:00
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]);
}
2020-12-17 16:11:11 +01:00
if (deviceBlock.period == 0) {
sendString(" Device is marked as inactive\n\r");
}
eepromWriteDeviceBlock(index, &deviceBlock);
2020-12-17 09:15:01 +01:00
}
return retCode;
}
2020-12-17 16:27:54 +01:00
static bool listDevicesCmd(uint8_t argc, char **args) {
for (uint8_t i = 0; i < getConfig()->numOfDeviceBlocks; i++) {
t_deviceBlock deviceBlock;
eepromReadDeviceBlock(i, &deviceBlock);
sendFormatString(" Index: %d\n\r", i);
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]);
}
if (deviceBlock.period == 0) {
sendString(" Device is marked as inactive\n\r");
} else if (deviceBlock.period == -1) {
sendString(" Device is marked as deleted\n\r");
}
}
return true;
}
2020-12-17 09:15:01 +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-17 09:00:00 +01:00
{ .name = "addDevice", .cmdFunc = addDeviceCmd,
.help = \
"addDevice ............................ Add a new device to the end of the list\n\r"
},
2020-12-17 09:15:01 +01:00
{ .name = "changeDevice", .cmdFunc = changeDeviceCmd,
.help = \
"changeDevice ......................... Change a new device by index\n\r"
},
2020-12-17 16:27:54 +01:00
{ .name = "listDevices", .cmdFunc = listDevicesCmd,
.help = \
"listDevices .......................... List the configured devices\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;
}