PhysicalDisk: detect temp

This commit is contained in:
李通洲 2023-12-26 19:23:26 +08:00
parent 0ee892f2de
commit 0972eb6986
No known key found for this signature in database
GPG Key ID: 79D0E1C4B64C86A0
7 changed files with 70 additions and 0 deletions

View File

@ -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);

View File

@ -92,6 +92,7 @@ const char* ffDetectPhysicalDisk(FFlist* result, FFPhysicalDiskOptions* options)
}
}
}
device->temperature = FF_PHYSICALDISK_TEMP_UNSET;
}
return NULL;

View File

@ -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);

View File

@ -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 <ctype.h>
@ -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;

View File

@ -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;
}

View File

@ -10,4 +10,5 @@ typedef struct FFPhysicalDiskOptions
FFModuleArgs moduleArgs;
FFstrbuf namePrefix;
bool temp;
} FFPhysicalDiskOptions;

View File

@ -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)