Add a command to query current temperature from all sensors able to report it:
{ "execute": "query-temperature-sensors" } { "return": [ { "temperature": 25, "name": "videocore" }, { "temperature": 25, "name": "bcm2835-thermal-0" } ] } Signed-off-by: Philippe Mathieu-Daudé <f4...@amsat.org> --- qapi/misc.json | 24 ++++++++++++++++++++++ hw/misc/temp-sensor.c | 48 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) diff --git a/qapi/misc.json b/qapi/misc.json index 99b90ac80b..51881931e2 100644 --- a/qapi/misc.json +++ b/qapi/misc.json @@ -1550,3 +1550,27 @@ ## { 'command': 'query-vm-generation-id', 'returns': 'GuidInfo' } +## +# @TemperatureSensor: +# +# Temperature sensor information. +# +# @name: the name of the sensor +# +# @temperature: the current temperature of the sensor (in C) +# +# Since: 5.1 +## +{ 'struct': 'TemperatureSensor', + 'data': { 'name': 'str', + 'temperature': 'number' } } + +## +# @query-temperature-sensors: +# +# Return a list of TemperatureSensor for devices that support +# the TYPE_TEMPSENSOR_INTERFACE. +# +# Since: 5.1 +## +{ 'command': 'query-temperature-sensors', 'returns': ['TemperatureSensor']} diff --git a/hw/misc/temp-sensor.c b/hw/misc/temp-sensor.c index b7c1eb2d87..27750c533d 100644 --- a/hw/misc/temp-sensor.c +++ b/hw/misc/temp-sensor.c @@ -10,6 +10,54 @@ #include "qemu/osdep.h" #include "hw/misc/temp-sensor.h" +#include "qapi/qapi-commands-misc.h" +#include "qapi/error.h" + +static int query_temperature_sensors_foreach(Object *obj, void *opaque) +{ + TemperatureSensorList **list = opaque; + TempSensor *sensor; + TempSensorClass *k; + + if (!object_dynamic_cast(obj, TYPE_TEMPSENSOR_INTERFACE)) { + return 0; + } + + k = TEMPSENSOR_INTERFACE_GET_CLASS(obj); + if (!k->get_temperature) { + return 0; + } + + sensor = TEMPSENSOR_INTERFACE(obj); + for (size_t i = 0; i < k->sensor_count; i++) { + TemperatureSensorList *info = g_malloc0(sizeof(*info)); + TemperatureSensor *value = g_malloc0(sizeof(*value)); + + if (k->get_name) { + value->name = g_strdup(k->get_name(sensor, i)); + } else { + value->name = g_strdup_printf("%s-%zu", + object_get_typename(obj), i); + } + value->temperature = k->get_temperature(sensor, i); + + info->value = value; + info->next = *list; + *list = info; + } + + return 0; +} + +TemperatureSensorList *qmp_query_temperature_sensors(Error **errp) +{ + TemperatureSensorList *list = NULL; + + object_child_foreach_recursive(object_get_root(), + query_temperature_sensors_foreach, + &list); + return list; +} static TypeInfo tempsensor_interface_type_info = { .name = TYPE_TEMPSENSOR_INTERFACE, -- 2.21.1