29 #if !defined(CONFIG_VERSION_MAJOR) || !defined(CONFIG_VERSION_MINOR)
37 #if (defined(__STDC__) && (__STDC_VERSION__ < 199901L))
38 # error This needs C99 or later!
43 if ( (name == NULL) || (strlen(name) < 2) )
49 if ( (name[0] ==
'0') && (name[1] ==
'x') )
53 if ( (sscanf(name,
"%"SCNx32
"%n", &
id, &numChar) == 1) && (numChar == (
int)strlen(name)) )
62 for (
int ix = 0; ix < _UBLOXCFG_NUM_ITEMS; ix++)
64 if (strcmp(allItems[ix]->name, name) == 0)
78 for (
int ix = 0; ix < _UBLOXCFG_NUM_ITEMS; ix++)
80 if (allItems[ix]->
id ==
id)
91 *num = _UBLOXCFG_NUM_ITEMS;
103 for (
int ix = 0; ix < _UBLOXCFG_NUM_RATES; ix++)
105 if (strcmp(allRates[ix]->msgName, msgName) == 0)
107 rates = allRates[ix];
116 *num = _UBLOXCFG_NUM_RATES;
122 if ( (data == NULL) || (size <= 0) || (keyVal == NULL) || (nKeyVal < 1) || (dataSize == NULL) )
129 memset(data, 0, size);
131 for (
int kvIx = 0; res && (kvIx < nKeyVal); kvIx++)
134 if ( (size - dataIx) < 4 )
142 const uint32_t key = kv->
id;
143 data[dataIx++] = (key >> 0) & 0xff;
144 data[dataIx++] = (key >> 8) & 0xff;
145 data[dataIx++] = (key >> 16) & 0xff;
146 data[dataIx++] = (key >> 24) & 0xff;
154 if ( (size - dataIx) < 1 )
159 data[dataIx++] = val.
_bytes[0];
162 if ( (size - dataIx) < 1 )
167 data[dataIx++] = val.
_bytes[0];
170 if ( (size - dataIx) < 2 )
175 data[dataIx++] = val.
_bytes[0];
176 data[dataIx++] = val.
_bytes[1];
179 if ( (size - dataIx) < 4 )
184 data[dataIx++] = val.
_bytes[0];
185 data[dataIx++] = val.
_bytes[1];
186 data[dataIx++] = val.
_bytes[2];
187 data[dataIx++] = val.
_bytes[3];
190 if ( (size - dataIx) < 8 )
195 data[dataIx++] = val.
_bytes[0];
196 data[dataIx++] = val.
_bytes[1];
197 data[dataIx++] = val.
_bytes[2];
198 data[dataIx++] = val.
_bytes[3];
199 data[dataIx++] = val.
_bytes[4];
200 data[dataIx++] = val.
_bytes[5];
201 data[dataIx++] = val.
_bytes[6];
202 data[dataIx++] = val.
_bytes[7];
216 if ( (data == NULL) || (size <= 0) || (keyVal == NULL) || (maxKeyVal < 1) || (nKeyVal == NULL) )
224 memset(keyVal, 0, maxKeyVal *
sizeof(*keyVal));
229 if ( dataIx > (size - 4) )
233 const uint8_t k0 = data[dataIx++];
234 const uint8_t k1 = data[dataIx++];
235 const uint8_t k2 = data[dataIx++];
236 const uint8_t k3 = data[dataIx++];
237 const uint32_t
id = k0 | (k1 << 8) | (k2 << 16) | (k3 << 24);
244 if ( dataIx > (size - 1) )
251 val.
_bytes[0] = data[dataIx++];
255 if ( dataIx > (size - 1) )
262 val.
_bytes[0] = data[dataIx++];
266 if ( dataIx > (size - 1) )
273 val.
_bytes[0] = data[dataIx++];
274 val.
_bytes[1] = data[dataIx++];
278 if ( dataIx > (size - 1) )
285 val.
_bytes[0] = data[dataIx++];
286 val.
_bytes[1] = data[dataIx++];
287 val.
_bytes[2] = data[dataIx++];
288 val.
_bytes[3] = data[dataIx++];
292 if ( dataIx > (size - 1) )
299 val.
_bytes[0] = data[dataIx++];
300 val.
_bytes[1] = data[dataIx++];
301 val.
_bytes[2] = data[dataIx++];
302 val.
_bytes[3] = data[dataIx++];
303 val.
_bytes[4] = data[dataIx++];
304 val.
_bytes[5] = data[dataIx++];
305 val.
_bytes[6] = data[dataIx++];
306 val.
_bytes[7] = data[dataIx++];
317 if (kvIx < maxKeyVal)
319 keyVal[kvIx].
id = id;
320 keyVal[kvIx].
val = val;
364 if ( (str == NULL) || (size <= 0) || ((item != NULL) && (item->
type != type)) )
377 snprintf(str, size,
"%" PRIu8, val->
U1);
384 snprintf(str, size,
"%" PRIu16, val->
U2);
391 snprintf(str, size,
"%" PRIu32, val->
U4);
398 snprintf(str, size,
"%" PRIu64, val->
U8);
405 snprintf(str, size,
"%" PRIi8, val->
I1);
412 snprintf(str, size,
"%" PRIi16, val->
I2);
419 snprintf(str, size,
"%" PRIi32, val->
I4);
426 snprintf(str, size,
"%" PRIi64, val->
I8);
434 if (size >= (19 + 20))
436 const char *fmt1 = NULL;
437 const char *fmt2 = NULL;
441 case UBLOXCFG_TYPE_X1: fmt1 =
"0x%02" PRIx64
" "; fmt2 =
"|0x%02" PRIx64; valX = val->
X1;
break;
442 case UBLOXCFG_TYPE_X2: fmt1 =
"0x%04" PRIx64
" "; fmt2 =
"|0x%04" PRIx64; valX = val->
X2;
break;
443 case UBLOXCFG_TYPE_X4: fmt1 =
"0x%08" PRIx64
" "; fmt2 =
"|0x%08" PRIx64; valX = val->
X4;
break;
444 case UBLOXCFG_TYPE_X8: fmt1 =
"0x%016" PRIx64
" "; fmt2 =
"|0x%016" PRIx64; valX = val->
X8;
break;
448 int len = snprintf(str, size, fmt1, valX);
449 const int ixBracket = len;
451 uint64_t usedBits = 0;
454 for (
int ix = 0; ix < item->
nConsts; ix++)
458 len += snprintf(&str[len], size - len,
"|%s", item->
consts[ix].
name);
460 if ((size - len - 1) <= 0)
468 if ((size - len - 1) > 0)
470 const uint64_t unusedBits = valX & (~usedBits);
471 if (unusedBits == valX)
473 strncat(&str[len],
"|n/a", size - len);
476 else if (unusedBits != 0)
478 len += snprintf(&str[len], size - len, fmt2, unusedBits);
482 str[ixBracket] =
'(';
483 if ((size - len - 1) > 0)
494 if (size > (15 + 30))
506 for (
int ix = 0; ix < item->
nConsts; ix++)
518 snprintf(str, size,
"%" PRIi8
" (n/a)", valE);
526 snprintf(str, size,
"%.24g", val->
R4);
533 snprintf(str, size,
"%.54g", val->
R8);
540 snprintf(str, size, val->
L ?
"1 (true)" :
"0 (false)");
551 if ( (str == NULL) || (valueStr == NULL) || (prettyStr == NULL) )
557 char *space = strchr(str,
' ');
560 *prettyStr = &space[2];
561 char *bracket = strchr(space,
')');
567 if (strcmp(*prettyStr,
"n/a") == 0)
577 if ( (str == NULL) || (size <= 0) || (keyVal == NULL) )
591 len = snprintf(str, size,
"CFG-?-? (0x%02" PRIx32
", ?0) = 0x%"PRIx8,
595 len = snprintf(str, size,
"CFG-?-? (0x%02" PRIx32
", ?1) = 0x%02"PRIx8,
599 len = snprintf(str, size,
"CFG-?-? (0x%02" PRIx32
", ?2) = 0x%04"PRIx16,
603 len = snprintf(str, size,
"CFG-?-? (0x%02" PRIx32
", ?4) = 0x%08"PRIx32,
607 len = snprintf(str, size,
"CFG-?-? (0x%08" PRIx32
", ?8) = 0x%016"PRIx64,
616 int len = snprintf(str, size,
"%s (0x%08" PRIx32
", %s) = ", item->
name, item->
id, type != NULL ? type :
"?");
617 if ((size - 1) <= len)
632 if ((size - 1 - 3) <= len)
639 if ( (item->
scale != NULL) || (item->
unit != NULL) )
645 if (item->
scale != NULL)
647 len += snprintf(&str[len], size - len,
"%s", item->
scale);
649 if ((size - 1 - 2) <= len)
654 if (item->
unit != NULL)
656 len += snprintf(&str[len], size - len,
"%s", item->
unit);
658 if ((size - 1 - 1) <= len)
670 static bool strToValUnsigned(
const char *str,
const UBLOXCFG_TYPE_t type, uint64_t *val);
671 static bool strToValSigned(
const char *str,
const UBLOXCFG_TYPE_t type, int64_t *val);
672 static bool findEnumValue(
const char *str,
const UBLOXCFG_ITEM_t *item, int64_t *val);
673 static bool findConstValue(
const char *str,
const UBLOXCFG_ITEM_t *item, uint64_t *val);
677 if ( (str == NULL) || (value == NULL) || ((item != NULL) && (item->
type != type)) )
684 uint64_t valUnsigned;
689 const int len = strlen(str);
690 memset(value, 0,
sizeof(*value));
694 if (strcmp(str,
"false") == 0)
699 else if (strcmp(str,
"true") == 0)
706 if (valUnsigned == 0)
711 else if (valUnsigned == 1)
719 if (strToValUnsigned(str, type, &valUnsigned))
721 value->
U1 = (uint8_t)valUnsigned;
726 if (strToValUnsigned(str, type, &valUnsigned))
728 value->
U2 = (uint16_t)valUnsigned;
733 if (strToValUnsigned(str, type, &valUnsigned))
735 value->
U4 = (uint32_t)valUnsigned;
740 if (strToValUnsigned(str, type, &valUnsigned))
742 value->
U8 = valUnsigned;
747 if (findConstValue(str, item, &valUnsigned))
749 value->
X1 = (uint8_t)valUnsigned;
752 else if (strToValUnsigned(str, type, &valUnsigned))
754 value->
U1 = (uint8_t)valUnsigned;
759 if (findConstValue(str, item, &valUnsigned))
761 value->
X2 = (uint16_t)valUnsigned;
764 else if (strToValUnsigned(str, type, &valUnsigned))
766 value->
X2 = (uint16_t)valUnsigned;
771 if (findConstValue(str, item, &valUnsigned))
773 value->
X4 = (uint32_t)valUnsigned;
776 else if (strToValUnsigned(str, type, &valUnsigned))
778 value->
X4 = (uint32_t)valUnsigned;
783 if (findConstValue(str, item, &valUnsigned))
785 value->
X8 = valUnsigned;
788 else if (strToValUnsigned(str, type, &valUnsigned))
790 value->
X8 = valUnsigned;
795 if (strToValSigned(str, type, &valSigned))
797 value->
I1 = (int8_t)valSigned;
802 if (strToValSigned(str, type, &valSigned))
804 value->
I2 = (int16_t)valSigned;
809 if (strToValSigned(str, type, &valSigned))
811 value->
I4 = (int32_t)valSigned;
816 if (strToValSigned(str, type, &valSigned))
818 value->
I8 = valSigned;
823 if ( (sscanf(str,
"%f%n", &valFloat, &numChar) == 1) && (numChar == len) )
825 value->
R4 = valFloat;
830 if ( (sscanf(str,
"%lf%n", &valDouble, &numChar) == 1) && (numChar == len) )
832 value->
R8 = valDouble;
837 if (findEnumValue(str, item, &valSigned))
839 value->
E1 = (int8_t)valSigned;
842 else if (strToValSigned(str, type, &valSigned))
844 value->
I1 = (int8_t)valSigned;
849 if (findEnumValue(str, item, &valSigned))
851 value->
E2 = (int16_t)valSigned;
854 else if (strToValSigned(str, type, &valSigned))
856 value->
I2 = (int16_t)valSigned;
861 if (findEnumValue(str, item, &valSigned))
863 value->
E4 = (int32_t)valSigned;
866 else if (strToValSigned(str, type, &valSigned))
868 value->
I4= (int32_t)valSigned;
876 static bool strToValUnsigned(
const char *str,
const UBLOXCFG_TYPE_t type, uint64_t *val)
878 if ( (str == NULL) || (val == NULL) )
882 const int len = strlen(str);
883 if ( (len < 1) || (isspace((
int)str[0]) != 0) )
916 else if ( (len > 1) && (str[0] ==
'0') && (str[1] ==
'x') )
919 if ( (len > 2) && (sscanf(str,
"%" SCNx64
"%n", &value, &numChar) == 1) && (numChar == len) )
925 else if (str[0] ==
'0')
928 if ( (sscanf(str,
"%" SCNo64
"%n", &value, &numChar) == 1) && (numChar == len) )
937 if ( (sscanf(str,
"%" SCNu64
"%n", &value, &numChar) == 1) && (numChar == len) )
947 return res && (value <= max);
951 static bool strToValSigned(
const char *str,
UBLOXCFG_TYPE_t type, int64_t *val)
953 if ( (str == NULL) || (val == NULL) )
957 const int len = strlen(str);
958 if ( (len < 1) || (isspace((
int)str[0]) != 0) )
991 if ( (len > 1) && (str[0] ==
'0') && (str[1] ==
'x') )
993 uint64_t valUnsigned;
995 if ( (len > 2) && ( sscanf(str,
"%" SCNx64
"%n", &valUnsigned, &numChar) == 1) && (numChar ==len) )
1002 value = (int64_t)(int8_t)valUnsigned;
1006 value = (int64_t)(int16_t)valUnsigned;
1010 value = (int64_t)(int32_t)valUnsigned;
1013 value = (int64_t)valUnsigned;
1025 if ( (sscanf(str,
"%" SCNi64
"%n", &value, &numChar) == 1) && (numChar == len) )
1035 return res && (value >= min) && (value <= max);
1039 static bool findConstValue(
const char *str,
const UBLOXCFG_ITEM_t *item, uint64_t *val)
1041 if ( (item == NULL) || (val == NULL) || (str == NULL) || (strlen(str) < 1) )
1047 uint64_t valRes = 0;
1050 const char sep =
'|';
1051 const char *pStr = str;
1052 const char *pSep = strchr(pStr, sep);
1053 while (*pStr !=
'\0')
1056 const int cmpLen = strlen(pStr) - ( (pSep != NULL) ? strlen(pSep) : 0 );
1059 if ( (cmpLen > 2) && (pStr[0] ==
'0') && (pStr[1] ==
'x') )
1063 if ( (cmpLen < 3) || (sscanf(pStr,
"%" SCNx64
"%n", &v, &numChar) != 1) || (numChar != cmpLen) )
1073 for (
int ix = 0; (ix < item->
nConsts) && !found; ix++)
1075 const int nameLen = strlen(item->
consts[ix].
name);
1076 if ( (nameLen == cmpLen) && (strncmp(item->
consts[ix].
name, pStr, cmpLen) == 0) )
1093 pSep = strchr(pStr, sep);
1108 static bool findEnumValue(
const char *str,
const UBLOXCFG_ITEM_t *item, int64_t *val)
1113 for (
int ix = 0; ix < item->
nConsts; ix++)
1140 if ( (name == NULL) || (name[0] ==
'\0') )
1145 int len = strlen(name);
1146 if (len > ((
int)
sizeof(str) - 1))
1154 str[len] = tolower(name[len]);
1156 if (strcmp(str,
"ram") == 0)
1160 else if (strcmp(str,
"bbr") == 0)
1164 else if (strcmp(str,
"flash") == 0)
1168 else if (strcmp(str,
"default") == 0)
1181 return ((uint16_t)CONFIG_VERSION_MAJOR << 8 | ((uint16_t)CONFIG_VERSION_MINOR & 0xff));
1186 *numSources = _UBLOXCFG_NUM_SOURCES;
1187 return _ubloxcfg_allSources();