mirror of
https://github.com/betaflight/betaflight.git
synced 2025-07-14 11:59:58 +03:00
Adding ability to specify a string as an option in the SET / GET setup
This commit is contained in:
parent
eb7e6c0b39
commit
4cb1f6509c
5 changed files with 281 additions and 153 deletions
|
@ -538,6 +538,10 @@ static void printValuePointer(const clivalue_t *var, const void *valuePointer, b
|
|||
} else {
|
||||
cliPrintf("OFF");
|
||||
}
|
||||
break;
|
||||
case MODE_STRING:
|
||||
cliPrintf("%s", (char *)valuePointer);
|
||||
break;
|
||||
}
|
||||
|
||||
if (valueIsCorrupted) {
|
||||
|
@ -756,7 +760,6 @@ static void cliSetVar(const clivalue_t *var, const uint32_t value)
|
|||
}
|
||||
*(uint32_t *)ptr = workValue;
|
||||
break;
|
||||
|
||||
}
|
||||
} else {
|
||||
switch (var->type & VALUE_TYPE_MASK) {
|
||||
|
@ -3954,6 +3957,19 @@ static uint8_t getWordLength(char *bufBegin, char *bufEnd)
|
|||
return bufEnd - bufBegin;
|
||||
}
|
||||
|
||||
uint16_t cliGetSettingIndex(char *name, uint8_t length)
|
||||
{
|
||||
for (uint32_t i = 0; i < valueTableEntryCount; i++) {
|
||||
const char *settingName = valueTable[i].name;
|
||||
|
||||
// ensure exact match when setting to prevent setting variables with shorter names
|
||||
if (strncasecmp(name, settingName, strlen(settingName)) == 0 && length == strlen(settingName)) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return valueTableEntryCount;
|
||||
}
|
||||
|
||||
STATIC_UNIT_TESTED void cliSet(char *cmdline)
|
||||
{
|
||||
const uint32_t len = strlen(cmdline);
|
||||
|
@ -3977,140 +3993,157 @@ STATIC_UNIT_TESTED void cliSet(char *cmdline)
|
|||
eqptr++;
|
||||
eqptr = skipSpace(eqptr);
|
||||
|
||||
for (uint32_t i = 0; i < valueTableEntryCount; i++) {
|
||||
const clivalue_t *val = &valueTable[i];
|
||||
|
||||
// ensure exact match when setting to prevent setting variables with shorter names
|
||||
if (strncasecmp(cmdline, val->name, strlen(val->name)) == 0 && variableNameLength == strlen(val->name)) {
|
||||
|
||||
bool valueChanged = false;
|
||||
int16_t value = 0;
|
||||
switch (val->type & VALUE_MODE_MASK) {
|
||||
case MODE_DIRECT: {
|
||||
if ((val->type & VALUE_TYPE_MASK) == VAR_UINT32) {
|
||||
uint32_t value = strtol(eqptr, NULL, 10);
|
||||
|
||||
if (value <= val->config.u32Max) {
|
||||
cliSetVar(val, value);
|
||||
valueChanged = true;
|
||||
}
|
||||
} else {
|
||||
int value = atoi(eqptr);
|
||||
|
||||
int min;
|
||||
int max;
|
||||
getMinMax(val, &min, &max);
|
||||
|
||||
if (value >= min && value <= max) {
|
||||
cliSetVar(val, value);
|
||||
valueChanged = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
case MODE_LOOKUP:
|
||||
case MODE_BITSET: {
|
||||
int tableIndex;
|
||||
if ((val->type & VALUE_MODE_MASK) == MODE_BITSET) {
|
||||
tableIndex = TABLE_OFF_ON;
|
||||
} else {
|
||||
tableIndex = val->config.lookup.tableIndex;
|
||||
}
|
||||
const lookupTableEntry_t *tableEntry = &lookupTables[tableIndex];
|
||||
bool matched = false;
|
||||
for (uint32_t tableValueIndex = 0; tableValueIndex < tableEntry->valueCount && !matched; tableValueIndex++) {
|
||||
matched = tableEntry->values[tableValueIndex] && strcasecmp(tableEntry->values[tableValueIndex], eqptr) == 0;
|
||||
|
||||
if (matched) {
|
||||
value = tableValueIndex;
|
||||
|
||||
cliSetVar(val, value);
|
||||
valueChanged = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case MODE_ARRAY: {
|
||||
const uint8_t arrayLength = val->config.array.length;
|
||||
char *valPtr = eqptr;
|
||||
|
||||
int i = 0;
|
||||
while (i < arrayLength && valPtr != NULL) {
|
||||
// skip spaces
|
||||
valPtr = skipSpace(valPtr);
|
||||
|
||||
// process substring starting at valPtr
|
||||
// note: no need to copy substrings for atoi()
|
||||
// it stops at the first character that cannot be converted...
|
||||
switch (val->type & VALUE_TYPE_MASK) {
|
||||
default:
|
||||
case VAR_UINT8:
|
||||
{
|
||||
// fetch data pointer
|
||||
uint8_t *data = (uint8_t *)cliGetValuePointer(val) + i;
|
||||
// store value
|
||||
*data = (uint8_t)atoi((const char*) valPtr);
|
||||
}
|
||||
|
||||
break;
|
||||
case VAR_INT8:
|
||||
{
|
||||
// fetch data pointer
|
||||
int8_t *data = (int8_t *)cliGetValuePointer(val) + i;
|
||||
// store value
|
||||
*data = (int8_t)atoi((const char*) valPtr);
|
||||
}
|
||||
|
||||
break;
|
||||
case VAR_UINT16:
|
||||
{
|
||||
// fetch data pointer
|
||||
uint16_t *data = (uint16_t *)cliGetValuePointer(val) + i;
|
||||
// store value
|
||||
*data = (uint16_t)atoi((const char*) valPtr);
|
||||
}
|
||||
|
||||
break;
|
||||
case VAR_INT16:
|
||||
{
|
||||
// fetch data pointer
|
||||
int16_t *data = (int16_t *)cliGetValuePointer(val) + i;
|
||||
// store value
|
||||
*data = (int16_t)atoi((const char*) valPtr);
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
// find next comma (or end of string)
|
||||
valPtr = strchr(valPtr, ',') + 1;
|
||||
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
// mark as changed
|
||||
valueChanged = true;
|
||||
|
||||
break;
|
||||
|
||||
}
|
||||
|
||||
if (valueChanged) {
|
||||
cliPrintf("%s set to ", val->name);
|
||||
cliPrintVar(val, 0);
|
||||
} else {
|
||||
cliPrintErrorLinef("INVALID VALUE");
|
||||
cliPrintVarRange(val);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
const uint16_t index = cliGetSettingIndex(cmdline, variableNameLength);
|
||||
if (index >= valueTableEntryCount) {
|
||||
cliPrintErrorLinef("INVALID NAME");
|
||||
return;
|
||||
}
|
||||
cliPrintErrorLinef("INVALID NAME");
|
||||
const clivalue_t *val = &valueTable[index];
|
||||
|
||||
bool valueChanged = false;
|
||||
int16_t value = 0;
|
||||
switch (val->type & VALUE_MODE_MASK) {
|
||||
case MODE_DIRECT: {
|
||||
if ((val->type & VALUE_TYPE_MASK) == VAR_UINT32) {
|
||||
uint32_t value = strtol(eqptr, NULL, 10);
|
||||
|
||||
if (value <= val->config.u32Max) {
|
||||
cliSetVar(val, value);
|
||||
valueChanged = true;
|
||||
}
|
||||
} else {
|
||||
int value = atoi(eqptr);
|
||||
|
||||
int min;
|
||||
int max;
|
||||
getMinMax(val, &min, &max);
|
||||
|
||||
if (value >= min && value <= max) {
|
||||
cliSetVar(val, value);
|
||||
valueChanged = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
case MODE_LOOKUP:
|
||||
case MODE_BITSET: {
|
||||
int tableIndex;
|
||||
if ((val->type & VALUE_MODE_MASK) == MODE_BITSET) {
|
||||
tableIndex = TABLE_OFF_ON;
|
||||
} else {
|
||||
tableIndex = val->config.lookup.tableIndex;
|
||||
}
|
||||
const lookupTableEntry_t *tableEntry = &lookupTables[tableIndex];
|
||||
bool matched = false;
|
||||
for (uint32_t tableValueIndex = 0; tableValueIndex < tableEntry->valueCount && !matched; tableValueIndex++) {
|
||||
matched = tableEntry->values[tableValueIndex] && strcasecmp(tableEntry->values[tableValueIndex], eqptr) == 0;
|
||||
|
||||
if (matched) {
|
||||
value = tableValueIndex;
|
||||
|
||||
cliSetVar(val, value);
|
||||
valueChanged = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case MODE_ARRAY: {
|
||||
const uint8_t arrayLength = val->config.array.length;
|
||||
char *valPtr = eqptr;
|
||||
|
||||
int i = 0;
|
||||
while (i < arrayLength && valPtr != NULL) {
|
||||
// skip spaces
|
||||
valPtr = skipSpace(valPtr);
|
||||
|
||||
// process substring starting at valPtr
|
||||
// note: no need to copy substrings for atoi()
|
||||
// it stops at the first character that cannot be converted...
|
||||
switch (val->type & VALUE_TYPE_MASK) {
|
||||
default:
|
||||
case VAR_UINT8:
|
||||
{
|
||||
// fetch data pointer
|
||||
uint8_t *data = (uint8_t *)cliGetValuePointer(val) + i;
|
||||
// store value
|
||||
*data = (uint8_t)atoi((const char*) valPtr);
|
||||
}
|
||||
|
||||
break;
|
||||
case VAR_INT8:
|
||||
{
|
||||
// fetch data pointer
|
||||
int8_t *data = (int8_t *)cliGetValuePointer(val) + i;
|
||||
// store value
|
||||
*data = (int8_t)atoi((const char*) valPtr);
|
||||
}
|
||||
|
||||
break;
|
||||
case VAR_UINT16:
|
||||
{
|
||||
// fetch data pointer
|
||||
uint16_t *data = (uint16_t *)cliGetValuePointer(val) + i;
|
||||
// store value
|
||||
*data = (uint16_t)atoi((const char*) valPtr);
|
||||
}
|
||||
|
||||
break;
|
||||
case VAR_INT16:
|
||||
{
|
||||
// fetch data pointer
|
||||
int16_t *data = (int16_t *)cliGetValuePointer(val) + i;
|
||||
// store value
|
||||
*data = (int16_t)atoi((const char*) valPtr);
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
// find next comma (or end of string)
|
||||
valPtr = strchr(valPtr, ',') + 1;
|
||||
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
// mark as changed
|
||||
valueChanged = true;
|
||||
|
||||
break;
|
||||
case MODE_STRING: {
|
||||
char *valPtr = eqptr;
|
||||
valPtr = skipSpace(valPtr);
|
||||
|
||||
const unsigned int len = strlen(valPtr);
|
||||
const uint8_t min = val->config.string.minlength;
|
||||
const uint8_t max = val->config.string.maxlength;
|
||||
const bool updatable = ((val->config.string.flags & STRING_FLAGS_WRITEONCE) == 0 ||
|
||||
strlen((char *)cliGetValuePointer(val)) == 0 ||
|
||||
strncmp(valPtr, (char *)cliGetValuePointer(val), len) == 0);
|
||||
|
||||
if (updatable && len > 0 && len <= max) {
|
||||
memset((char *)cliGetValuePointer(val), 0, max);
|
||||
if (len >= min && strncmp(valPtr, emptyName, len)) {
|
||||
strncpy((char *)cliGetValuePointer(val), valPtr, len);
|
||||
}
|
||||
valueChanged = true;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
if (valueChanged) {
|
||||
cliPrintf("%s set to ", val->name);
|
||||
cliPrintVar(val, 0);
|
||||
} else {
|
||||
cliPrintErrorLinef("INVALID VALUE");
|
||||
cliPrintVarRange(val);
|
||||
}
|
||||
|
||||
return;
|
||||
} else {
|
||||
// no equals, check for matching variables.
|
||||
cliGet(cmdline);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue