aboutsummaryrefslogtreecommitdiffstats
path: root/cputemp.c
diff options
context:
space:
mode:
authorPA4WDH2024-07-28 14:45:59 +0200
committerPA4WDH2024-07-28 14:45:59 +0200
commit36162d6931aaaf56c60c2e1c77cd3cdc2eba8363 (patch)
tree6bb22ce09315d14a5b7fb4ba2e5178d1b5dd9450 /cputemp.c
parentFLush after writes to CSV file (diff)
downloadcputemp2maxfreq-36162d6931aaaf56c60c2e1c77cd3cdc2eba8363.tar.gz
cputemp2maxfreq-36162d6931aaaf56c60c2e1c77cd3cdc2eba8363.tar.bz2
cputemp2maxfreq-36162d6931aaaf56c60c2e1c77cd3cdc2eba8363.zip
Add autodetection for temperature input
Diffstat (limited to 'cputemp.c')
-rw-r--r--cputemp.c128
1 files changed, 128 insertions, 0 deletions
diff --git a/cputemp.c b/cputemp.c
new file mode 100644
index 0000000..559056f
--- /dev/null
+++ b/cputemp.c
@@ -0,0 +1,128 @@
+#include <stdio.h>
+#include <sys/types.h>
+#include <dirent.h>
+#include <string.h>
+#include "cputemp2maxfreq.h"
+#include "debug.h"
+#include "sysfs.h"
+#include "cputemp.h"
+
+extern struct s_config config;
+
+// Read a single hwmon directory and search for the requested sensor name
+void cputemp_read_hwmon(char *hwmon,char *sensor_name,struct s_sensor *sensor)
+{
+ DIR *hwmon_dir;
+ struct dirent *hwmon_dirent;
+ int count;
+ char buf[255];
+ int datalen;
+ char sysfs_file[255];
+ long int testdata;
+ int offset;
+
+ DEBUG1_CPUTEMP("Searching for sensors in %s\n",hwmon);
+
+ hwmon_dir=opendir(hwmon);
+ if (hwmon_dir==NULL)
+ {
+ DEBUG1_CPUTEMP("Failed to open hwmon dir\n");
+ return;
+ }
+
+ hwmon_dirent=readdir(hwmon_dir);
+ while((hwmon_dirent!=NULL) && (sensor->valid==0))
+ {
+ DEBUG3_CPUTEMP("%s\n",hwmon_dirent->d_name);
+
+// We are searching for files named "temp<number>_label"
+ if (strncmp(hwmon_dirent->d_name,"temp",4)==0)
+ {
+ DEBUG3_CPUTEMP("Found temp entry: %s\n",hwmon_dirent->d_name);
+ offset=0;
+ for(count=5;count<7;count++)
+ {
+ if (hwmon_dirent->d_name[count]=='_')
+ {
+ DEBUG3_CPUTEMP("Underscore at %d\n",count);
+ offset=count+1;
+ break;
+ }
+ }
+ if (strncmp(hwmon_dirent->d_name+offset,"label",5)==0)
+ {
+ DEBUG2_CPUTEMP("Found label %s\n",hwmon_dirent->d_name);
+ sprintf(sysfs_file,"%s/%s",hwmon,hwmon_dirent->d_name);
+
+// Read the label to see if it matches what we're looking for
+ datalen=sysfs_read_str(sysfs_file,buf,sizeof(buf));
+ if (datalen<0)
+ {
+ DEBUG2_CPUTEMP("Failed to read label\n");
+ } else {
+ DEBUG1_CPUTEMP("Label is: %s\n",buf);
+ if (strncasecmp(buf,sensor_name,255)==0)
+ {
+ DEBUG2_CPUTEMP("Label machtes our search criteria\n");
+ offset=strlen(sysfs_file)-5;
+ strncpy(sysfs_file+offset,"input",5);
+ DEBUG1_CPUTEMP("Input filename: %s\n",sysfs_file);
+
+// Validate the sensor by reading it
+ testdata=sysfs_read_long_int(sysfs_file);
+ DEBUG2_CPUTEMP("Input value: %ld\n",testdata);
+ if ((testdata>=VALID_TEMP_MIN) && (testdata<=VALID_TEMP_MAX))
+ {
+ DEBUG1_CPUTEMP("Input data is valid, marking sensor as valid\n");
+ sensor->valid=1;
+ strncpy(sensor->name,buf,sizeof(buf));
+ strncpy(sensor->filename,sysfs_file,sizeof(sysfs_file));
+ } else {
+ DEBUG1_CPUTEMP("Input data is invalid, ignoring sensor\n");
+ }
+ }
+ }
+ }
+ }
+ hwmon_dirent=readdir(hwmon_dir);
+ }
+ closedir(hwmon_dir);
+}
+
+// Find all entries under /sys/class/hwmon and check them with
+// cputemp_read_hwmon
+void cputemp_find_sensor(char *sensor_name,struct s_sensor *sensor)
+{
+ DIR *hwmon_dir;
+ struct dirent *hwmon_dirent;
+ char subdir[255];
+
+ DEBUG1_CPUTEMP("Searching for sensor %s\n",sensor_name);
+
+// Mark sensor data as invalid, it will be set valid if we find something
+ sensor->valid=0;
+
+ hwmon_dir=opendir("/sys/class/hwmon");
+ if (hwmon_dir==NULL)
+ {
+ DEBUG1_CPUTEMP("Failed to open /sys/class/hwmon, is sysfs mounted?\n");
+ return;
+ }
+
+ hwmon_dirent=readdir(hwmon_dir);
+ while((hwmon_dirent!=NULL) && (sensor->valid==0))
+ {
+ DEBUG3_CPUTEMP("Found file: %s\n",hwmon_dirent->d_name);
+
+// If the name starts with hwmon try to read it
+// Note that /sys/class/hwmon actually contains symlinks but we treat them like directories
+ if (strncmp(hwmon_dirent->d_name,"hwmon",5)==0)
+ {
+ sprintf(subdir,"/sys/class/hwmon/%s",hwmon_dirent->d_name);
+ DEBUG2_CPUTEMP("Found hwmon entry: %s\n",subdir);
+ cputemp_read_hwmon(subdir,sensor_name,sensor);
+ }
+ hwmon_dirent=readdir(hwmon_dir);
+ }
+ closedir(hwmon_dir);
+}