feat(config-ext): Methods for handling optionals

There are several cases across configuration files where
optional values are reflected as values such as 0, -1 or empty
strings. Provide explicit means to handle optional configuration
values which can be easily reflected in the xml style configuraion
as empty nodes.
This commit is contained in:
icex2
2024-08-17 12:50:14 +02:00
parent 8b6cd4a58e
commit a1d103f661
2 changed files with 293 additions and 0 deletions

View File

@@ -129,4 +129,269 @@ void bt_core_config_ext_net_addr_get(const bt_core_config_t *config, const char
if (!net_str_parse(buffer, addr)) {
log_fatal("Cannot parse server address: %s", buffer);
}
}
bool bt_core_config_ext_s8_optional_get(
const bt_core_config_t *config, const char *path, int8_t *value)
{
bt_core_config_result_t result;
result = bt_core_config_s8_get(config, path, value);
switch (result) {
case BT_CORE_CONFIG_RESULT_SUCCESS:
return true;
case BT_CORE_CONFIG_RESULT_VALUE_NOT_FOUND:
return false;
default:
bt_core_config_fatal_on_error(result, path);
// Should not reach
return false;
}
}
bool bt_core_config_ext_u8_optional_get(
const bt_core_config_t *config, const char *path, uint8_t *value)
{
bt_core_config_result_t result;
result = bt_core_config_u8_get(config, path, value);
switch (result) {
case BT_CORE_CONFIG_RESULT_SUCCESS:
return true;
case BT_CORE_CONFIG_RESULT_VALUE_NOT_FOUND:
return false;
default:
bt_core_config_fatal_on_error(result, path);
// Should not reach
return false;
}
}
bool bt_core_config_ext_s16_optional_get(
const bt_core_config_t *config, const char *path, int16_t *value)
{
bt_core_config_result_t result;
result = bt_core_config_s16_get(config, path, value);
switch (result) {
case BT_CORE_CONFIG_RESULT_SUCCESS:
return true;
case BT_CORE_CONFIG_RESULT_VALUE_NOT_FOUND:
return false;
default:
bt_core_config_fatal_on_error(result, path);
// Should not reach
return false;
}
}
bool bt_core_config_ext_u16_optional_get(
const bt_core_config_t *config, const char *path, uint16_t *value)
{
bt_core_config_result_t result;
result = bt_core_config_u16_get(config, path, value);
switch (result) {
case BT_CORE_CONFIG_RESULT_SUCCESS:
return true;
case BT_CORE_CONFIG_RESULT_VALUE_NOT_FOUND:
return false;
default:
bt_core_config_fatal_on_error(result, path);
// Should not reach
return false;
}
}
bool bt_core_config_ext_s32_optional_get(
const bt_core_config_t *config, const char *path, int32_t *value)
{
bt_core_config_result_t result;
result = bt_core_config_s32_get(config, path, value);
switch (result) {
case BT_CORE_CONFIG_RESULT_SUCCESS:
return true;
case BT_CORE_CONFIG_RESULT_VALUE_NOT_FOUND:
return false;
default:
bt_core_config_fatal_on_error(result, path);
// Should not reach
return false;
}
}
bool bt_core_config_ext_u32_optional_get(
const bt_core_config_t *config, const char *path, uint32_t *value)
{
bt_core_config_result_t result;
result = bt_core_config_u32_get(config, path, value);
switch (result) {
case BT_CORE_CONFIG_RESULT_SUCCESS:
return true;
case BT_CORE_CONFIG_RESULT_VALUE_NOT_FOUND:
return false;
default:
bt_core_config_fatal_on_error(result, path);
// Should not reach
return false;
}
}
bool bt_core_config_ext_s64_optional_get(
const bt_core_config_t *config, const char *path, int64_t *value)
{
bt_core_config_result_t result;
result = bt_core_config_s64_get(config, path, value);
switch (result) {
case BT_CORE_CONFIG_RESULT_SUCCESS:
return true;
case BT_CORE_CONFIG_RESULT_VALUE_NOT_FOUND:
return false;
default:
bt_core_config_fatal_on_error(result, path);
// Should not reach
return false;
}
}
bool bt_core_config_ext_u64_optional_get(
const bt_core_config_t *config, const char *path, uint64_t *value)
{
bt_core_config_result_t result;
result = bt_core_config_u64_get(config, path, value);
switch (result) {
case BT_CORE_CONFIG_RESULT_SUCCESS:
return true;
case BT_CORE_CONFIG_RESULT_VALUE_NOT_FOUND:
return false;
default:
bt_core_config_fatal_on_error(result, path);
// Should not reach
return false;
}
}
bool bt_core_config_ext_float_optional_get(
const bt_core_config_t *config, const char *path, float *value)
{
bt_core_config_result_t result;
result = bt_core_config_float_get(config, path, value);
switch (result) {
case BT_CORE_CONFIG_RESULT_SUCCESS:
return true;
case BT_CORE_CONFIG_RESULT_VALUE_NOT_FOUND:
return false;
default:
bt_core_config_fatal_on_error(result, path);
// Should not reach
return false;
}
}
bool bt_core_config_ext_double_optional_get(
const bt_core_config_t *config, const char *path, double *value)
{
bt_core_config_result_t result;
result = bt_core_config_double_get(config, path, value);
switch (result) {
case BT_CORE_CONFIG_RESULT_SUCCESS:
return true;
case BT_CORE_CONFIG_RESULT_VALUE_NOT_FOUND:
return false;
default:
bt_core_config_fatal_on_error(result, path);
// Should not reach
return false;
}
}
bool bt_core_config_ext_bool_optional_get(
const bt_core_config_t *config, const char *path, bool *value)
{
bt_core_config_result_t result;
result = bt_core_config_bool_get(config, path, value);
switch (result) {
case BT_CORE_CONFIG_RESULT_SUCCESS:
return true;
case BT_CORE_CONFIG_RESULT_VALUE_NOT_FOUND:
return false;
default:
bt_core_config_fatal_on_error(result, path);
// Should not reach
return false;
}
}
bool bt_core_config_ext_bin_optional_get(
const bt_core_config_t *config, const char *path, void *value, size_t len)
{
bt_core_config_result_t result;
result = bt_core_config_bin_get(config, path, value, len);
switch (result) {
case BT_CORE_CONFIG_RESULT_SUCCESS:
return true;
case BT_CORE_CONFIG_RESULT_VALUE_NOT_FOUND:
return false;
default:
bt_core_config_fatal_on_error(result, path);
// Should not reach
return false;
}
}
bool bt_core_config_ext_str_optional_get(
const bt_core_config_t *config, const char *path, char *value, size_t len)
{
bt_core_config_result_t result;
result = bt_core_config_str_get(config, path, value, len);
switch (result) {
case BT_CORE_CONFIG_RESULT_SUCCESS:
return true;
case BT_CORE_CONFIG_RESULT_VALUE_NOT_FOUND:
return false;
default:
bt_core_config_fatal_on_error(result, path);
// Should not reach
return false;
}
}
bool bt_core_config_ext_net_addr_optional_get(const bt_core_config_t *config, const char *path, struct net_addr *addr)
{
char buffer[1024];
bool result;
result = bt_core_config_ext_str_optional_get(config, path, buffer, sizeof(buffer));
if (!result) {
return false;
}
if (!net_str_parse(buffer, addr)) {
log_fatal("Cannot parse server address: %s", buffer);
}
return true;
}

View File

@@ -33,4 +33,32 @@ void bt_core_config_ext_str_get(
const bt_core_config_t *config, const char *path, char *value, size_t len);
void bt_core_config_ext_net_addr_get(const bt_core_config_t *config, const char *path, struct net_addr *addr);
bool bt_core_config_ext_s8_optional_get(
const bt_core_config_t *config, const char *path, int8_t *value);
bool bt_core_config_ext_u8_optional_get(
const bt_core_config_t *config, const char *path, uint8_t *value);
bool bt_core_config_ext_s16_optional_get(
const bt_core_config_t *config, const char *path, int16_t *value);
bool bt_core_config_ext_u16_optional_get(
const bt_core_config_t *config, const char *path, uint16_t *value);
bool bt_core_config_ext_s32_optional_get(
const bt_core_config_t *config, const char *path, int32_t *value);
bool bt_core_config_ext_u32_optional_get(
const bt_core_config_t *config, const char *path, uint32_t *value);
bool bt_core_config_ext_s64_optional_get(
const bt_core_config_t *config, const char *path, int64_t *value);
bool bt_core_config_ext_u64_optional_get(
const bt_core_config_t *config, const char *path, uint64_t *value);
bool bt_core_config_ext_float_optional_get(
const bt_core_config_t *config, const char *path, float *value);
bool bt_core_config_ext_double_optional_get(
const bt_core_config_t *config, const char *path, double *value);
bool bt_core_config_ext_bool_optional_get(
const bt_core_config_t *config, const char *path, bool *value);
bool bt_core_config_ext_bin_optional_get(
const bt_core_config_t *config, const char *path, void *value, size_t len);
bool bt_core_config_ext_str_optional_get(
const bt_core_config_t *config, const char *path, char *value, size_t len);
bool bt_core_config_ext_net_addr_optional_get(const bt_core_config_t *config, const char *path, struct net_addr *addr);
#endif