Skip to content

Commit

Permalink
New enum type (appears as number in JSON for now) and handling old se…
Browse files Browse the repository at this point in the history
…tting for arrays
  • Loading branch information
revk committed Jan 22, 2025
1 parent e955977 commit 4b7ea80
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 25 deletions.
1 change: 1 addition & 0 deletions Manuals/revk-settings-dev.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ Note the basic syntax of the definition files are checked, and some invalid comb
|----|-------|
|`bit`|A single bit value holding `0` or `1` (also `false` or `true` in JSON). This is implemented as a bit field in C and a `#define` to allow access by name.|
|`gpio`|A GPIO definition, see below|
|`enum`|uint8_t with an enum. Set `.enums="..."` comma separated value names||
|`blob`|Binary data (up to 64K if space in NVS), see below|
|`json`|A string `char*` internally, that is JSON in the settings|
|`s`|String i.e. `char*`|
Expand Down
23 changes: 23 additions & 0 deletions revk.c
Original file line number Diff line number Diff line change
Expand Up @@ -3237,6 +3237,29 @@ revk_web_setting (httpd_req_t * req, const char *tag, const char *field)
place = "Unused";
if (s->ptr == &hostname)
place = revk_id; // Special case
#ifdef REVK_SETTINGS_HAS_ENUM
if (s->isenum)
{
revk_web_send (req, "<td nowrap><select name=\"_%s\" onchange=\"this.name='%s';settings.__%s.name='%s';\">", field, field,
field, field);
const char *e = s->enums;
int n = 0;
int v = atoi (value);
if (e)
while (*e)
{
const char *p = e;
while (*e && *e != ',')
e++;
revk_web_send (req, "<option value=\"%d\"%s>%.*s</option>", n, n == v ? " selected" : "", (int) (e - p), p);
if (*e == ',')
e++;
n++;
}
revk_web_send (req, "</select></td><td>%s</td></tr>", comment);
return;
}
#endif
#ifdef REVK_SETTINGS_HAS_BIT
if (s->type == REVK_SETTINGS_BIT)
{
Expand Down
83 changes: 65 additions & 18 deletions revk_settings.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ typename (FILE * O, const char *type)
fprintf (O, "revk_gpio_t");
else if (!strcmp (type, "blob"))
fprintf (O, "revk_settings_blob_t*");
else if (!strcmp (type, "enum"))
fprintf (O, "uint8_t");
else if (!strcmp (type, "s") || !strcmp (type, "json"))
fprintf (O, "char*");
else if (*type == 'c' && is_digit (type[1]))
Expand Down Expand Up @@ -401,10 +403,6 @@ main (int argc, const char *argv[])
hasplace = 1;
}

for (d = defs; d && (!d->attributes || !strstr (d->attributes, ".enum=")); d = d->next);
if (d)
hasenum = 1;

for (d = defs; d && (!d->attributes || !strstr (d->attributes, ".old=")); d = d->next);
if (d)
hasold = 1;
Expand Down Expand Up @@ -435,6 +433,14 @@ main (int argc, const char *argv[])
if (d)
hasjson = 1;

for (d = defs; d && (!d->type || strcmp (d->type, "enum")); d = d->next);
if (d)
hasenum = 1;

for (d = defs; d && (!d->type || strcmp (d->type, "gpio")); d = d->next);
if (d)
hasgpio = 1;

fprintf (C, "\n");
fprintf (C, "#include <stdint.h>\n");
fprintf (C, "#include \"sdkconfig.h\"\n");
Expand All @@ -451,10 +457,14 @@ main (int argc, const char *argv[])
"struct revk_settings_s {\n" //
" void *ptr;\n" //
" const char name[%d];\n" //
" const char *def;\n" //
" const char *flags;\n", maxname + 1);
" const char *def;\n", maxname + 1);
if (hasenum)
fprintf (H, " const char *enum;\n");
fprintf (H, " union {\n" //
" const char *flags;\n" //
" const char *enums;\n" //
" };\n");
else
fprintf (H, " const char *flags;\n");
if (hasold)
fprintf (H, " const char *old;\n");
if (hasunit)
Expand Down Expand Up @@ -483,15 +493,52 @@ main (int argc, const char *argv[])
" uint8_t base64:1;\n" //
" uint8_t secret:1;\n" //
" uint8_t dq:1;\n" //
" uint8_t gpio:1;\n" //
" uint8_t rtc:1;\n" //
"};\n");

if(hasenum)
for (d = defs; d;d=d->next)if(d->attributes && strstr (d->attributes, ".enum="))
{ // Create local enums
" uint8_t rtc:1;\n");
if (hasgpio)
fprintf (H, " uint8_t gpio:1;\n");
if (hasenum)
fprintf (H, " uint8_t isenum:1;\n"); //
fprintf (H, "};\n");

}
if (hasenum)
for (d = defs; d; d = d->next)
if (d->type && !strcmp (d->type, "enum"))
{ // Create local enums
const char *e = NULL;
if (!d->attributes || !(e = strstr (d->attributes, ".enums=\"")))
errx (1, "enum needs .enums=\"...\"");
e += 8;
int n=0;
fprintf (H, "enum {\n");
while (*e && *e != '"')
{
fprintf (H, " REVK_SETTINGS_");
const char *p = d->name;
while (*p)
{
if (isalnum (*p))
putc (toupper (*p), H);
else
putc ('_', H);
p++;
}
putc ('_', H);
while (*e && *e != ',' && *e != '"')
{
if (isalnum (*e))
putc (toupper (*e), H);
else
putc ('_', H);
e++;
}
if (*e == ',')
e++;
fprintf (H, ",\n");
n++;
}
fprintf (H, "};\n");
if(n>255)errx(1,"Enum too big");
}

for (d = defs; d && (!d->type || strcmp (d->type, "blob")); d = d->next);
if (d)
Expand All @@ -503,10 +550,8 @@ main (int argc, const char *argv[])
"};\n");
hasblob = 1;
}
for (d = defs; d && (!d->type || strcmp (d->type, "gpio")); d = d->next);
if (d)
if (hasgpio)
{
hasgpio = 1;
hasunsigned = 1; // GPIO is treated as a u16
fprintf (H, "typedef struct revk_gpio_s revk_gpio_t;\n" //
"struct revk_gpio_s {\n" //
Expand Down Expand Up @@ -645,6 +690,8 @@ main (int argc, const char *argv[])
fprintf (C, ".type=REVK_SETTINGS_UNSIGNED");
else if (!strcmp (d->type, "gpio"))
fprintf (C, ".type=REVK_SETTINGS_UNSIGNED,.gpio=1");
else if (!strcmp (d->type, "enum"))
fprintf (C, ".type=REVK_SETTINGS_UNSIGNED,.isenum=1");
else if (!strcmp (d->type, "bit"))
fprintf (C, ".type=REVK_SETTINGS_BIT");
else if (!strcmp (d->type, "blob"))
Expand Down
31 changes: 24 additions & 7 deletions settings_lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,10 @@ parse_numeric (revk_settings_t * s, void **pp, const char **dp, const char *e)
f |= (1ULL << (--bits));
int top = bits - 1;
const char *b = s->flags;
#ifdef REVK_SETTINGS_HAS_ENUM
if (s->isenum)
b = NULL;
#endif
void scan (void)
{ // Scan for flags
while (d < e && *d != ' ' && *d != ',')
Expand Down Expand Up @@ -557,7 +561,11 @@ text_numeric (revk_settings_t * s, void *p)
{
const char *f = NULL;
int bit = bits - 1;
if (s->flags)
if (s->flags
#ifdef REVK_SETTINGS_HAS_ENUM
&& !s->isenum
#endif
)
{
// Count down bits in use
for (f = s->flags; *f; f++)
Expand Down Expand Up @@ -1015,15 +1023,24 @@ revk_settings_load (const char *tag, const char *appname)
} else
{
#ifdef REVK_SETTINGS_HAS_OLD
for (s = revk_settings;
s->len && !(s->revk == revk && s->old && !s->array && !strcmp (s->old, info.key)); s++);
if (s->len)
{
err = nvs_get (s, info.key, 0); // Exact match (old)
if (l && (info.key[l - 1] & 0x80))
{ // Array (new style)
for (s = revk_settings;
s->len && !(s->revk == revk && s->old && s->array && strlen (s->old) == l - 1
&& !strncmp (s->old, info.key, l - 1)); s++);
if (s->len)
err = nvs_get (s, info.key, index = info.key[l - 1] - 0x80); // Exact match (old)
} else
{ // Non array
for (s = revk_settings;
s->len && !(s->revk == revk && s->old && !s->array && !strcmp (s->old, info.key)); s++);
if (s->len)
err = nvs_get (s, info.key, 0); // Exact match (old)
}
if (!s->len)
#endif
{
addzap (NULL, 0); // Not doing old array or old style array - can add if needed
addzap (NULL, 0);
err = "Not matched";
}
}
Expand Down

0 comments on commit 4b7ea80

Please sign in to comment.