From 64441ed6d8d4ccf7f7d22d0b9079d44b3ecc437f Mon Sep 17 00:00:00 2001 From: DL6ER Date: Mon, 9 Jan 2023 20:19:59 +0100 Subject: [PATCH] Add config.misc.temp_limit setting primed by setupVars.conf:TEMPERATURE_LIMIT (if available) used to signal beyond which temperature the frontend should consider the temperature "hot" Signed-off-by: DL6ER --- src/api/config.c | 26 +++++++++++++++++++------- src/api/docs/content/specs/config.yaml | 3 +++ src/api/docs/content/specs/ftl.yaml | 10 +++++++++- src/api/ftl.c | 11 ++++++++--- src/config/config.c | 6 ++++++ src/config/config.h | 3 +++ src/config/toml_helper.c | 14 +++++++++++++- src/setupVars.c | 13 +++++++++++++ 8 files changed, 74 insertions(+), 12 deletions(-) diff --git a/src/api/config.c b/src/api/config.c index 2f63c48a..4865933f 100644 --- a/src/api/config.c +++ b/src/api/config.c @@ -79,6 +79,8 @@ static cJSON *addJSONvalue(const enum conf_type conf_type, union conf_value *val return cJSON_CreateNumber(val->l); case CONF_ULONG: return cJSON_CreateNumber(val->ul); + case CONF_DOUBLE: + return cJSON_CreateNumber(val->d); case CONF_STRING: case CONF_STRING_ALLOCATED: return val->s ? cJSON_CreateStringReference(val->s) : cJSON_CreateNull(); @@ -130,7 +132,7 @@ static const char *getJSONvalue(struct conf_item *conf_item, cJSON *elem) return "not of type bool"; // Set item conf_item->v.b = elem->valueint; - log_debug(DEBUG_CONFIG, "Set %s to %s", conf_item->k, elem->valueint ? "true" : "false"); + log_debug(DEBUG_CONFIG, "Set %s to %s", conf_item->k, conf_item->v.b ? "true" : "false"); break; } case CONF_INT: @@ -142,7 +144,7 @@ static const char *getJSONvalue(struct conf_item *conf_item, cJSON *elem) return "not of type integer"; // Set item conf_item->v.i = elem->valueint; - log_debug(DEBUG_CONFIG, "Set %s to %i", conf_item->k, elem->valueint); + log_debug(DEBUG_CONFIG, "Set %s to %i", conf_item->k, conf_item->v.i); break; } case CONF_UINT: @@ -154,7 +156,7 @@ static const char *getJSONvalue(struct conf_item *conf_item, cJSON *elem) return "not of type unsigned integer"; // Set item conf_item->v.ui = elem->valuedouble; - log_debug(DEBUG_CONFIG, "Set %s to %u", conf_item->k, (unsigned int)elem->valuedouble); + log_debug(DEBUG_CONFIG, "Set %s to %u", conf_item->k, conf_item->v.ui); break; } case CONF_LONG: @@ -166,7 +168,7 @@ static const char *getJSONvalue(struct conf_item *conf_item, cJSON *elem) return "not of type long"; // Set item conf_item->v.l = elem->valuedouble; - log_debug(DEBUG_CONFIG, "Set %s to %li", conf_item->k, (long)elem->valuedouble); + log_debug(DEBUG_CONFIG, "Set %s to %li", conf_item->k, conf_item->v.l); break; } case CONF_ULONG: @@ -177,8 +179,18 @@ static const char *getJSONvalue(struct conf_item *conf_item, cJSON *elem) elem->valuedouble < 0 || elem->valuedouble > ULONG_MAX) return "not of type unsigned long"; // Set item - conf_item->v.l = elem->valuedouble; - log_debug(DEBUG_CONFIG, "Set %s to %lu", conf_item->k, (unsigned long)elem->valuedouble); + conf_item->v.ul = elem->valuedouble; + log_debug(DEBUG_CONFIG, "Set %s to %lu", conf_item->k, conf_item->v.ul); + break; + } + case CONF_DOUBLE: + { + // Check it is a number + if(!cJSON_IsNumber(elem)) + return "not of type unsigned long"; + // Set item + conf_item->v.d = elem->valuedouble; + log_debug(DEBUG_CONFIG, "Set %s to %f", conf_item->k, conf_item->v.d); break; } case CONF_STRING: @@ -257,7 +269,7 @@ static const char *getJSONvalue(struct conf_item *conf_item, cJSON *elem) return "not within valid range"; // Set item conf_item->v.i = elem->valueint; - log_debug(DEBUG_CONFIG, "Set %s to %d", conf_item->k, elem->valueint); + log_debug(DEBUG_CONFIG, "Set %s to %d", conf_item->k, conf_item->v.i); break; } case CONF_STRUCT_IN_ADDR: diff --git a/src/api/docs/content/specs/config.yaml b/src/api/docs/content/specs/config.yaml index 86f2e43b..880ec7af 100644 --- a/src/api/docs/content/specs/config.yaml +++ b/src/api/docs/content/specs/config.yaml @@ -237,6 +237,8 @@ components: type: boolean privacylevel: type: integer + temp_limit: + type: number check: type: object properties: @@ -376,6 +378,7 @@ components: delay_startup: 10 addr2line: true privacylevel: 0 + temp_limit: 60.0 check: load: true shmem: 90 diff --git a/src/api/docs/content/specs/ftl.yaml b/src/api/docs/content/specs/ftl.yaml index 6a341151..0080a86e 100644 --- a/src/api/docs/content/specs/ftl.yaml +++ b/src/api/docs/content/specs/ftl.yaml @@ -285,10 +285,18 @@ components: value: type: number description: Sensor value + hot_limit: + type: number + description: Limit defined in FTL's config beyond which the CPU should be considered hot + unit: + type: string + description: Sensor value unit example: - - name: null + - name: "Composite" path: "hwmon0/temp1" value: 30.4 + hot_limit: 60.0 + unit: "C" model: type: string description: Device model (if available, `null` otherwise) diff --git a/src/api/ftl.c b/src/api/ftl.c index 2fef89cd..91c3f364 100644 --- a/src/api/ftl.c +++ b/src/api/ftl.c @@ -210,9 +210,9 @@ static int read_temp_sensor(struct ftl_conn *api, FILE *f_value = fopen(value_path, "r"); if(f_value != NULL) { - int temp = 0; + int raw_temp = 0; char label[1024]; - if(fscanf(f_value, "%d", &temp) == 1) + if(fscanf(f_value, "%d", &raw_temp) == 1) { cJSON *item = JSON_NEW_OBJECT(); if(f_label && fgets(label, sizeof(label)-1, f_label)) @@ -227,7 +227,12 @@ static int read_temp_sensor(struct ftl_conn *api, JSON_ADD_NULL_TO_OBJECT(item, "name"); } JSON_COPY_STR_TO_OBJECT(item, "path", short_path); - JSON_ADD_NUMBER_TO_OBJECT(item, "value", temp < 1000 ? temp : 1e-3*temp); + + // Compute actual temperature + double temp = raw_temp < 1000 ? raw_temp : 1e-3*raw_temp; + JSON_ADD_NUMBER_TO_OBJECT(item, "value", temp); + JSON_ADD_NUMBER_TO_OBJECT(item, "hot_limit", config.misc.temp_limit.v.d); + JSON_REF_STR_IN_OBJECT(item, "unit", "C"); JSON_ADD_ITEM_TO_ARRAY(object, item); } } diff --git a/src/config/config.c b/src/config/config.c index 79c68c3b..94d30f63 100644 --- a/src/config/config.c +++ b/src/config/config.c @@ -478,6 +478,12 @@ void initConfig(void) config.misc.delay_startup.t = CONF_UINT; config.misc.delay_startup.d.ui = 0; + // sub-struct misc.temp + config.misc.temp_limit.k = "misc.temp_limit"; + config.misc.temp_limit.h = "Which upper temperature limit should be used by Pi-hole [°C]? Temperatures above this limit will be shown as \"hot\""; + config.misc.temp_limit.t = CONF_DOUBLE; + config.misc.temp_limit.d.d = 60.0; // °C + // sub-struct misc.check config.misc.check.load.k = "misc.check.load"; config.misc.check.load.h = "Should FTL check the 15 min average of CPU load and complain if the load is larger than the number of available CPU cores?"; diff --git a/src/config/config.h b/src/config/config.h index cc53bc1b..724c6194 100644 --- a/src/config/config.h +++ b/src/config/config.h @@ -51,6 +51,7 @@ union conf_value { unsigned int ui; // unsigned int value long l; // long value unsigned long ul; // unsigned long value + double d; // double value char *s; // char * value enum ptr_type ptr_type; // enum ptr_type value enum busy_reply busy_reply; // enum busy_reply value @@ -69,6 +70,7 @@ enum conf_type { CONF_UINT, CONF_LONG, CONF_ULONG, + CONF_DOUBLE, CONF_STRING, CONF_STRING_ALLOCATED, CONF_ENUM_PTR_TYPE, @@ -189,6 +191,7 @@ struct config { struct conf_item delay_startup; struct conf_item addr2line; struct conf_item privacylevel; + struct conf_item temp_limit; struct { struct conf_item load; struct conf_item shmem; diff --git a/src/config/toml_helper.c b/src/config/toml_helper.c index 91e7541a..bbd92696 100644 --- a/src/config/toml_helper.c +++ b/src/config/toml_helper.c @@ -69,7 +69,7 @@ static void printTOMLstring(FILE *fp, const char *s) } // If string is printable and does not contain any special characters, we can - // print it as is without furhter escaping + // print it as is without further escaping if (ok) { fprintf(fp, "\"%s\"", s); @@ -132,6 +132,9 @@ void writeTOMLvalue(FILE * fp, const enum conf_type t, union conf_value *v) case CONF_ULONG: fprintf(fp, "%lu", v->ul); break; + case CONF_DOUBLE: + fprintf(fp, "%f", v->d); + break; case CONF_STRING: case CONF_STRING_ALLOCATED: printTOMLstring(fp, v->s); @@ -236,6 +239,15 @@ void readTOMLvalue(struct conf_item *conf_item, const char* key, toml_table_t *t log_debug(DEBUG_CONFIG, "%s does not exist or is not of type unsigned long", conf_item->k); break; } + case CONF_DOUBLE: + { + const toml_datum_t val = toml_double_in(toml, key); + if(val.ok) + conf_item->v.d = val.u.d; + else + log_debug(DEBUG_CONFIG, "%s does not exist or is not of type double", conf_item->k); + break; + } case CONF_STRING: case CONF_STRING_ALLOCATED: { diff --git a/src/setupVars.c b/src/setupVars.c index 6267ac55..d1186ecc 100644 --- a/src/setupVars.c +++ b/src/setupVars.c @@ -85,6 +85,19 @@ void importsetupVarsConf(void) // Free memory, harmless to call if read_setupVarsconf() didn't return a result clearSetupVarsArray(); + + // Try to obtain blocking active boolean + const char* temp_limit = read_setupVarsconf("TEMPERATURE_LIMIT"); + + if(temp_limit != NULL) + { + double lim; + if(sscanf(temp_limit, "%lf", &lim) == 1) + config.misc.temp_limit.v.d = lim; + } + + // Free memory, harmless to call if read_setupVarsconf() didn't return a result + clearSetupVarsArray(); } char* __attribute__((pure)) find_equals(const char* s)