changeDevie

This commit is contained in:
2020-12-17 15:59:44 +01:00
parent 6b976f22cf
commit 29fcdb3e41

View File

@ -167,40 +167,51 @@ static bool makeDevice(uint8_t argOffset, uint8_t argc, char **args, t_deviceBlo
} }
char *deviceName = args[1 + argOffset]; char *deviceName = args[1 + argOffset];
if (strcmp(deviceName, "*") != 0) {
if (strlen(deviceName) >= sizeof(deviceBlock->deviceName)) { if (strlen(deviceName) >= sizeof(deviceBlock->deviceName)) {
sendString("devicename too long\n\r"); sendString("devicename too long\n\r");
return false; return false;
} }
strcpy(deviceBlock->deviceName, deviceName); strcpy(deviceBlock->deviceName, deviceName);
}
long int rawAddress = strtol(args[2 + argOffset], NULL, 10); char *rawAddressStr = args[2 + argOffset];
if (strcmp(rawAddressStr, "*") != 0) {
long int rawAddress = strtol(rawAddressStr, NULL, 10);
if (rawAddress < 1 || rawAddress > 254) { if (rawAddress < 1 || rawAddress > 254) {
sendString("illegal address\n\r"); sendString("illegal address\n\r");
return false; return false;
} }
deviceBlock->address = (uint8_t)rawAddress; deviceBlock->address = (uint8_t)rawAddress;
}
long int rawPeriod = strtol(args[3 + argOffset], NULL, 10); char *rawPeriodStr = args[3 + argOffset];
if (strcmp(rawPeriodStr, "*") != 0) {
long int rawPeriod = strtol(rawPeriodStr, NULL, 10);
if (rawPeriod < 0 || rawPeriod > 86400) { if (rawPeriod < 0 || rawPeriod > 86400) {
sendString("illegal period\n\r"); sendString("illegal period\n\r");
return false; return false;
} }
deviceBlock->period = (int32_t) rawPeriod; deviceBlock->period = (int32_t) rawPeriod;
}
for (uint8_t i = 0; i < MBUSDEVICE_NUM_OF_CONSIDEREDFIELDS; i++) { for (uint8_t i = 0; i < MBUSDEVICE_NUM_OF_CONSIDEREDFIELDS; i++) {
long int rawFieldNum = strtol(args[4 + i + argOffset], NULL, 10); char *rawFieldNumStr = args[4 + i + argOffset];
if (strcmp(rawFieldNumStr, "*") != 0) {
long int rawFieldNum = strtol(rawFieldNumStr, NULL, 10);
if (rawFieldNum < -1 || rawFieldNum > 127) { if (rawFieldNum < -1 || rawFieldNum > 127) {
sendString("illegal considered field index\n\r"); sendString("illegal considered field index\n\r");
return false; return false;
} }
deviceBlock->consideredField[i] = (int8_t) rawFieldNum; deviceBlock->consideredField[i] = (int8_t) rawFieldNum;
} }
}
return true; return true;
} }
static bool addDeviceCmd(uint8_t argc, char **args) { static bool addDeviceCmd(uint8_t argc, char **args) {
t_deviceBlock deviceBlock; t_deviceBlock deviceBlock = { .deviceName = "", .address = 0, .period = 0, .consideredField = { -1, -1, -1, -1}};
bool retCode = makeDevice(0, argc, args, &deviceBlock); bool retCode = makeDevice(0, argc, args, &deviceBlock);
if (retCode) { if (retCode) {
sendString("New device would be:\n\r"); sendString("New device would be:\n\r");
@ -218,6 +229,7 @@ static bool changeDeviceCmd(uint8_t argc, char **args) {
if (strcmp(args[1], "help")) { if (strcmp(args[1], "help")) {
sendString("First argument: index of device in list\n\r"); sendString("First argument: index of device in list\n\r");
sendFormatString("Between 0 and %d\n\r", getConfig()->numOfDeviceBlocks); sendFormatString("Between 0 and %d\n\r", getConfig()->numOfDeviceBlocks);
sendString("For further arguments use a * to keep the value\n\r");
return makeDevice(0, argc, args, NULL); return makeDevice(0, argc, args, NULL);
} }
@ -229,7 +241,8 @@ static bool changeDeviceCmd(uint8_t argc, char **args) {
uint8_t index = (uint8_t) rawIndex; uint8_t index = (uint8_t) rawIndex;
t_deviceBlock deviceBlock; t_deviceBlock deviceBlock;
uint8_t index; eepromReadDeviceBlock(index, &deviceBlock);
bool retCode = makeDevice(1, argc, args, &deviceBlock); bool retCode = makeDevice(1, argc, args, &deviceBlock);
if (retCode) { if (retCode) {
sendString("Changed device would be:\n\r"); sendString("Changed device would be:\n\r");