170 lines
5.5 KiB
C
Raw Normal View History

2021-01-09 22:01:21 +01:00
#include <cmdHelper.h>
#include <logger.h>
#include <eeprom.h>
#include <config.h>
#include <string.h>
#include <stdlib.h>
static bool showConfigCmd(uint8_t argc, char **args) {
bool retCode = true;
t_configBlock configBlock;
eepromReadConfigBlock(&configBlock);
sendString("This is the saved configuration.\n\r");
sendString("It is not necessarily the active configuration.\n\r");
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]);
return retCode;
}
static bool setStringParameterCmd(uint8_t argc, char **args, size_t offset, size_t length) {
bool retCode = true;
t_configBlock configBlock;
char *parameterName = args[1];
char *newParameterValue = args[2];
if (strlen(newParameterValue) >= length) {
sendString("given new value for is too long\n\r");
retCode = false;
} else {
sendFormatString("set %s to %s\n\r", parameterName, newParameterValue);
eepromReadConfigBlock(&configBlock);
strcpy((((char*)&configBlock) + offset), newParameterValue);
eepromWriteConfigBlock(&configBlock);
}
return retCode;
}
2021-01-09 23:32:01 +01:00
/*
2021-01-09 22:01:21 +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;
}
2021-01-09 23:32:01 +01:00
*/
2021-01-09 22:01:21 +01:00
static bool setDeviceNameCmd(uint8_t argc, char **args) {
return setStringParameterCmd(argc, args,
offsetof(t_configBlock, deviceName),
sizeof(((t_configBlock*)0)->deviceName));
}
const static cmd_t SET_COMMANDS[] = {
{ .name = "devicename", .cmdFunc = setDeviceNameCmd,
.help = \
"devicename ........................... Name of this device\n\r"
},
{ .name = "END_OF_CMDS", .help = "",.cmdFunc = NULL }
};
const static char UNKNOWN_PARAMETER[] = "unknown parameter\n\r";
const static char OK_MSG[] = "OK\n\r";
const static char FAILED_MSG[] = "Failed\n\r";
static bool setCmd(uint8_t argc, char **args) {
bool retCode = false;
uint8_t *messageToSend = NULL;
char *cmd = args[1];
if (argc >= 2) {
if (0 == strcmp("help", cmd)) {
sendString("You can set the following parameters:\n\r");
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++;
}
retCode = true;
} 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)) {
retCode = command.cmdFunc(argc, args);
messageToSend = retCode ? (uint8_t*)OK_MSG : (uint8_t*)FAILED_MSG;
sendString("Remember you need to restart to active this change.\n\r");
break;
}
cmdIdx++;
}
sendString((const char*)messageToSend);
}
}
return retCode;
}
static bool restartCmd(uint8_t argc, char **args) {
HAL_NVIC_SystemReset();
// you won't come here ...
return true;
}
const cmd_t CONFIG_COMMANDS[] = {
{ .name = "show", .cmdFunc = showConfigCmd,
.help = \
"show ................................. Show the configuration\n\r"
},
{ .name = "set", .cmdFunc = setCmd,
.help = \
"set .................................. Set configuration parameters\n\r" \
" Argument help gives a list of \n\r" \
" parameters\n\r"
},
{ .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;
}