Skip to content

Commit

Permalink
Add replace -C paramter for setting config data
Browse files Browse the repository at this point in the history
  • Loading branch information
SciLor committed Sep 23, 2024
1 parent 739bf63 commit bee3dab
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 64 deletions.
18 changes: 18 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,24 @@
"MIMode": "gdb",
},
},
{
"name": "[linux] teddyCloud Settings Port Test",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/bin/teddycloud",
"args": [
"--config-set",
"core.server.http_port=90,core.server.https_web_port=9443"
],
"stopAtEntry": false,
"cwd": "${workspaceFolder}/",
"environment": [],
"externalConsole": false,
"preLaunchTask": "build",
"linux": {
"MIMode": "gdb",
},
},
{
"name": "[linux] teddyCloud Time Test",
"type": "cppdbg",
Expand Down
4 changes: 4 additions & 0 deletions include/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,10 @@ bool settings_set_u64_array(const char *item, const uint64_t *value, size_t len)
bool settings_set_u64_array_ovl(const char *item, const uint64_t *value, size_t len, const char *overlay_name);
bool settings_set_u64_array_id(const char *item, const uint64_t *value, size_t len, uint8_t settingsId);

bool settings_set_by_string(const char *item, const char *value);
bool settings_set_by_string_ovl(const char *item, const char *value, const char *overlay_name);
bool settings_set_by_string_id(const char *item, const char *value, uint8_t settingsId);

void settings_load_all_certs();
error_t settings_try_load_certs_id(uint8_t settingsId);
error_t settings_load_certs_id(uint8_t settingsId);
Expand Down
64 changes: 3 additions & 61 deletions src/handler_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -486,69 +486,11 @@ error_t handleApiSettingsSet(HttpConnection *connection, const char_t *uri, cons
{
TRACE_DEBUG("got overlay '%s'\r\n", overlay);
}
setting_item_t *opt = settings_get_by_name_ovl(item, overlay);
if (opt == NULL)
{
return ERROR_NOT_FOUND;
}
bool success = false;

if (size > 0 || opt->type == TYPE_STRING)
{
if (opt)
{
switch (opt->type)
{
case TYPE_BOOL:
{
success = settings_set_bool_ovl(item, !strcasecmp(data, "true"), overlay);
break;
}
case TYPE_STRING:
{
success = settings_set_string_ovl(item, data, overlay);
break;
}
case TYPE_HEX:
{
uint32_t value = strtoul(data, NULL, 16);
success = settings_set_unsigned_ovl(item, value, overlay);
break;
}

case TYPE_UNSIGNED:
{
uint32_t value = strtoul(data, NULL, 10);
success = settings_set_unsigned_ovl(item, value, overlay);
break;
}

case TYPE_SIGNED:
{
int32_t value = strtol(data, NULL, 10);
success = settings_set_signed_ovl(item, value, overlay);
break;
}

case TYPE_FLOAT:
{
float value = strtof(data, NULL);
success = settings_set_float_ovl(item, value, overlay);
break;
}

default:
break;
}
}
else
{
TRACE_WARNING("Setting: '%s' cannot be set to '%s'\r\n", item, data);
}
}
else
bool success = false;
if (size > 0)
{
TRACE_ERROR("Setting '%s' is unknown", item);
success = settings_set_by_string_ovl(item, data, overlay);
}

if (success)
Expand Down
43 changes: 40 additions & 3 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,24 @@ void cbr_header(void *ctx, HttpClientContext *cloud_ctx, const char *header, con
}
}

void set_settings(const char *option)
{
// Option will be in the format "name=value"
char *data = strdup(option);
char *name = strtok(data, "=");
char *value = strtok(NULL, "=");

if (name && value)
{
TRACE_INFO("Setting config entry '%s' to value '%s'\r\n", name, value);
settings_set_by_string(name, value);
}
else
{
TRACE_ERROR("Invalid config-set option format. Expected name=value.\r\n");
}
}

int_t main(int argc, char *argv[])
{
char cwd[PATH_LEN] = {0};
Expand Down Expand Up @@ -244,6 +262,7 @@ int_t main(int argc, char *argv[])
const char *hostname;
const char *oldrtnlhost;
const char *oldapihost;
const char *config_set;
} options = {0};

options.base_path = BASE_PATH;
Expand All @@ -269,15 +288,16 @@ int_t main(int argc, char *argv[])
{"esp32-extract", required_argument, 0, 'X'},
{"docker-test", no_argument, 0, 'D'},
{"url-test", required_argument, 0, 'U'},
{"cloud-test", required_argument, 0, 'C'},
{"cloud-test", required_argument, 0, 'T'},
{"hash", required_argument, 0, 'H'},
{"hostname", required_argument, 0, 'h'},
{"config-set", required_argument, 0, 'C'},
{"help", no_argument, 0, '?'},
{0, 0, 0, 0}};

/* getopt_long stores the option index here. */
int option_index = 0;
int c = getopt_long(argc, argv, "b:s:d:gc:e:E:S:P:F:I:X:DU:C:H:h:?", long_options, &option_index);
int c = getopt_long(argc, argv, "b:s:d:gc:e:E:S:P:F:I:X:DU:T:H:h:C:?", long_options, &option_index);

/* Detect the end of the options. */
if (c == -1)
Expand All @@ -304,9 +324,10 @@ int_t main(int argc, char *argv[])
OPT_SIMPLE_STR('X', esp32_extract);
OPT_SIMPLE_NON('D', docker_test);
OPT_SIMPLE_STR('U', url_test);
OPT_SIMPLE_STR('C', cloud_test);
OPT_SIMPLE_STR('T', cloud_test);
OPT_SIMPLE_STR('H', hash);
OPT_SIMPLE_STR('h', hostname);
OPT_SIMPLE_STR('C', config_set);
OPT_SIMPLE_STR(0x100, oldrtnlhost);
OPT_SIMPLE_STR(0x101, oldapihost);

Expand Down Expand Up @@ -336,6 +357,19 @@ int_t main(int argc, char *argv[])
get_settings()->internal.autogen_certs = autogen;
main_init_settings(cwd, options.base_path);

/* parse config-set option */
if (options.config_set)
{
char *config_set_ptr;
char *config_set = strdup(options.config_set);
char *option = strtok_r(config_set, ",", &config_set_ptr);
while (option)
{
set_settings(option);
option = strtok_r(NULL, ",", &config_set_ptr);
}
}

toniebox_state_init();
platform_init();

Expand Down Expand Up @@ -639,6 +673,9 @@ static void print_usage(char *argv[])
"\r\n"
" --encode-test <FILE>\r\n"
" Perform an internal encoding test on the specified file.\r\n"
"\r\n"
" --config-set <NAME>=<VALUE>,<NAME2>=<VALUE2>,...\r\n"
" Perform an internal encoding test on the specified file.\r\n"
"\r\n",

argv[0]);
Expand Down
71 changes: 71 additions & 0 deletions src/settings.c
Original file line number Diff line number Diff line change
Expand Up @@ -1505,6 +1505,77 @@ static char *settings_sanitize_box_id(const char *input_id)
return new_str;
}

bool settings_set_by_string(const char *item, const char *value)
{
return settings_set_by_string_id(item, value, 0);
}
bool settings_set_by_string_ovl(const char *item, const char *value, const char *overlay_name)
{
return settings_set_by_string_id(item, value, get_overlay_id(overlay_name));
}
bool settings_set_by_string_id(const char *item, const char *value, uint8_t settingsId)
{
bool success = false;
setting_item_t *opt = settings_get_by_name_id(item, settingsId);
if (opt == NULL)
{
TRACE_ERROR("Settings: %s not found\r\n", item);
return ERROR_NOT_FOUND;
}
else if (opt)
{
switch (opt->type)
{
case TYPE_BOOL:
{
success = settings_set_bool_id(item, !strcasecmp(value, "true"), settingsId);
break;
}
case TYPE_STRING:
{
success = settings_set_string_id(item, value, settingsId);
break;
}
case TYPE_HEX:
{
uint32_t data = strtoul(value, NULL, 16);
success = settings_set_unsigned_id(item, data, settingsId);
break;
}

case TYPE_UNSIGNED:
{
uint32_t data = strtoul(value, NULL, 10);
success = settings_set_unsigned_id(item, data, settingsId);
break;
}

case TYPE_SIGNED:
{
int32_t data = strtol(value, NULL, 10);
success = settings_set_signed_id(item, data, settingsId);
break;
}

case TYPE_FLOAT:
{
float data = strtof(value, NULL);
success = settings_set_float_id(item, data, settingsId);
break;
}

default:
break;
}
}
else
{
TRACE_WARNING("Setting: '%s' cannot be set to '%s'\r\n", item, value);
}

return success;
}

/* unused? */
void settings_load_all_certs()
{
Expand Down

0 comments on commit bee3dab

Please sign in to comment.