From 0972eb698614ab91dcbc47e707f4074665b42b05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Tue, 26 Dec 2023 19:23:26 +0800 Subject: [PATCH] PhysicalDisk: detect temp --- src/detection/physicaldisk/physicaldisk.h | 3 ++ .../physicaldisk/physicaldisk_apple.c | 1 + src/detection/physicaldisk/physicaldisk_bsd.c | 1 + .../physicaldisk/physicaldisk_linux.c | 16 +++++++++++ .../physicaldisk/physicaldisk_windows.c | 20 +++++++++++++ src/modules/physicaldisk/option.h | 1 + src/modules/physicaldisk/physicaldisk.c | 28 +++++++++++++++++++ 7 files changed, 70 insertions(+) diff --git a/src/detection/physicaldisk/physicaldisk.h b/src/detection/physicaldisk/physicaldisk.h index 15bb7ce5..76863f78 100644 --- a/src/detection/physicaldisk/physicaldisk.h +++ b/src/detection/physicaldisk/physicaldisk.h @@ -1,5 +1,7 @@ #include "fastfetch.h" +#define FF_PHYSICALDISK_TEMP_UNSET (0/0.0) + typedef enum FFPhysicalDiskType { FF_PHYSICALDISK_TYPE_NONE = 0, @@ -24,6 +26,7 @@ typedef struct FFPhysicalDiskResult FFstrbuf revision; FFPhysicalDiskType type; uint64_t size; + double temperature; } FFPhysicalDiskResult; const char* ffDetectPhysicalDisk(FFlist* result, FFPhysicalDiskOptions* options); diff --git a/src/detection/physicaldisk/physicaldisk_apple.c b/src/detection/physicaldisk/physicaldisk_apple.c index 85ffbbd2..2f7d1cc1 100644 --- a/src/detection/physicaldisk/physicaldisk_apple.c +++ b/src/detection/physicaldisk/physicaldisk_apple.c @@ -92,6 +92,7 @@ const char* ffDetectPhysicalDisk(FFlist* result, FFPhysicalDiskOptions* options) } } } + device->temperature = FF_PHYSICALDISK_TEMP_UNSET; } return NULL; diff --git a/src/detection/physicaldisk/physicaldisk_bsd.c b/src/detection/physicaldisk/physicaldisk_bsd.c index 513d0f8e..a6920b88 100644 --- a/src/detection/physicaldisk/physicaldisk_bsd.c +++ b/src/detection/physicaldisk/physicaldisk_bsd.c @@ -67,6 +67,7 @@ const char* ffDetectPhysicalDisk(FFlist* result, FFPhysicalDiskOptions* options) type |= acw ? FF_PHYSICALDISK_TYPE_READONLY : FF_PHYSICALDISK_TYPE_READWRITE; device->type = type; + device->temperature = FF_PHYSICALDISK_TEMP_UNSET; } geom_stats_snapshot_free(snap); diff --git a/src/detection/physicaldisk/physicaldisk_linux.c b/src/detection/physicaldisk/physicaldisk_linux.c index 4a3a1922..884339ca 100644 --- a/src/detection/physicaldisk/physicaldisk_linux.c +++ b/src/detection/physicaldisk/physicaldisk_linux.c @@ -1,6 +1,7 @@ #include "physicaldisk.h" #include "common/io/io.h" #include "common/properties.h" +#include "detection/temps/temps_linux.h" #include "util/stringUtils.h" #include @@ -129,6 +130,21 @@ const char* ffDetectPhysicalDisk(FFlist* result, FFPhysicalDiskOptions* options) if (ffReadFileBuffer(pathSysBlock, &device->revision)) ffStrbufTrimRightSpace(&device->revision); } + + device->temperature = FF_PHYSICALDISK_TEMP_UNSET; + if (options->temp) + { + const FFlist* tempsResult = ffDetectTemps(); + + FF_LIST_FOR_EACH(FFTempValue, value, *tempsResult) + { + if (ffStrStartsWith(devName, value->deviceName.chars)) // nvme0 - nvme0n1 + { + device->temperature = value->value; + break; + } + } + } } return NULL; diff --git a/src/detection/physicaldisk/physicaldisk_windows.c b/src/detection/physicaldisk/physicaldisk_windows.c index c8ffb748..e9d8f2e0 100644 --- a/src/detection/physicaldisk/physicaldisk_windows.c +++ b/src/detection/physicaldisk/physicaldisk_windows.c @@ -151,6 +151,26 @@ static bool detectPhysicalDisk(const wchar_t* szDevice, FFlist* result, FFPhysic device->size = (uint64_t) diskInfo->NumberMediaSides * diskInfo->TracksPerCylinder * diskInfo->SectorsPerTrack * diskInfo->BytesPerSector; } + device->temperature = FF_PHYSICALDISK_TEMP_UNSET; + if (options->temp) + { + STORAGE_TEMPERATURE_DATA_DESCRIPTOR stdd = {}; + if(DeviceIoControl( + hDevice, + IOCTL_STORAGE_QUERY_PROPERTY, + &(STORAGE_PROPERTY_QUERY) { + .PropertyId = StorageDeviceTemperatureProperty, + .QueryType = PropertyStandardQuery, + }, + sizeof(STORAGE_PROPERTY_QUERY), + &stdd, + sizeof(stdd), + &retSize, + NULL + ) && retSize == sizeof(stdd)) + device->temperature = stdd.TemperatureInfo[0].Temperature; + } + return true; } diff --git a/src/modules/physicaldisk/option.h b/src/modules/physicaldisk/option.h index 243a98a6..f57770d6 100644 --- a/src/modules/physicaldisk/option.h +++ b/src/modules/physicaldisk/option.h @@ -10,4 +10,5 @@ typedef struct FFPhysicalDiskOptions FFModuleArgs moduleArgs; FFstrbuf namePrefix; + bool temp; } FFPhysicalDiskOptions; diff --git a/src/modules/physicaldisk/physicaldisk.c b/src/modules/physicaldisk/physicaldisk.c index 07be8875..864f5a67 100644 --- a/src/modules/physicaldisk/physicaldisk.c +++ b/src/modules/physicaldisk/physicaldisk.c @@ -90,6 +90,14 @@ void ffPrintPhysicalDisk(FFPhysicalDiskOptions* options) } ffStrbufAppendC(&buffer, ']'); } + + if (dev->temperature == dev->temperature) //FF_PHYSICALDISK_TEMP_UNSET + { + if(buffer.length > 0) + ffStrbufAppendS(&buffer, " - "); + + ffParseTemperature(dev->temperature, &buffer); + } ffStrbufPutTo(&buffer, stdout); } else @@ -107,6 +115,7 @@ void ffPrintPhysicalDisk(FFPhysicalDiskOptions* options) {FF_FORMAT_ARG_TYPE_STRING, removableType}, {FF_FORMAT_ARG_TYPE_STRING, readOnlyType}, {FF_FORMAT_ARG_TYPE_STRBUF, &dev->revision}, + {FF_FORMAT_ARG_TYPE_DOUBLE, &dev->temperature}, }); } ++index; @@ -134,6 +143,12 @@ bool ffParsePhysicalDiskCommandOptions(FFPhysicalDiskOptions* options, const cha return true; } + if (ffStrEqualsIgnCase(subKey, "temp")) + { + options->temp = ffOptionParseBoolean(value); + return true; + } + return false; } @@ -156,6 +171,12 @@ void ffParsePhysicalDiskJsonObject(FFPhysicalDiskOptions* options, yyjson_val* m continue; } + if (ffStrEqualsIgnCase(key, "temp")) + { + options->temp = yyjson_get_bool(val); + continue; + } + ffPrintError(FF_PHYSICALDISK_MODULE_NAME, 0, &options->moduleArgs, "Unknown JSON key %s", key); } } @@ -169,6 +190,9 @@ void ffGeneratePhysicalDiskJsonConfig(FFPhysicalDiskOptions* options, yyjson_mut if (!ffStrbufEqual(&options->namePrefix, &defaultOptions.namePrefix)) yyjson_mut_obj_add_strbuf(doc, module, "namePrefix", &options->namePrefix); + + if (options->temp != defaultOptions.temp) + yyjson_mut_obj_add_bool(doc, module, "temp", options->temp); } void ffGeneratePhysicalDiskJsonResult(FFPhysicalDiskOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) @@ -215,6 +239,8 @@ void ffGeneratePhysicalDiskJsonResult(FFPhysicalDiskOptions* options, yyjson_mut yyjson_mut_obj_add_null(doc, obj, "readOnly"); yyjson_mut_obj_add_strbuf(doc, obj, "revision", &dev->revision); + + yyjson_mut_obj_add_real(doc, obj, "temperature", dev->temperature); } FF_LIST_FOR_EACH(FFPhysicalDiskResult, dev, result) @@ -237,6 +263,7 @@ void ffPrintPhysicalDiskHelpFormat(void) "Device kind (Removable or Fixed)", "Device kind (Read-only or Read-write)", "Product revision", + "Device temperature", }); } @@ -256,6 +283,7 @@ void ffInitPhysicalDiskOptions(FFPhysicalDiskOptions* options) ffOptionInitModuleArg(&options->moduleArgs); ffStrbufInit(&options->namePrefix); + options->temp = false; } void ffDestroyPhysicalDiskOptions(FFPhysicalDiskOptions* options)