PhysicalDisk (Linux): cleanup temp detection code

This commit is contained in:
Carter Li
2024-09-23 16:22:11 +08:00
parent b3b6e5fec1
commit 86a96da9ab
+26 -13
View File
@@ -7,6 +7,29 @@
#include <ctype.h>
#include <limits.h>
static double detectNvmeTemp(const char* devName)
{
char pathSysBlock[PATH_MAX];
int index = snprintf(pathSysBlock, PATH_MAX, "/sys/block/%s/device/hwmon$/temp1_input", devName);
if (index <= 0) return FF_PHYSICALDISK_TEMP_UNSET;
index -= (int) sizeof("/temp1_input");
for (char c = '0'; c <= '9'; c++) // hopefully there's only one digit
{
pathSysBlock[index] = c;
char buffer[64];
ssize_t size = ffReadFileData(pathSysBlock, sizeof(buffer), buffer);
if (size > 0)
{
buffer[size] = '\0';
double temp = strtod(buffer, NULL);
return temp > 0 ? temp / 1000 : FF_PHYSICALDISK_TEMP_UNSET;
}
}
return FF_PHYSICALDISK_TEMP_UNSET;
}
const char* ffDetectPhysicalDisk(FFlist* result, FFPhysicalDiskOptions* options)
{
FF_AUTO_CLOSE_DIR DIR* sysBlockDirp = opendir("/sys/block/");
@@ -157,20 +180,10 @@ const char* ffDetectPhysicalDisk(FFlist* result, FFPhysicalDiskOptions* options)
}
}
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;
}
}
}
device->temperature = detectNvmeTemp(devName);
else
device->temperature = FF_PHYSICALDISK_TEMP_UNSET;
}
return NULL;