aboutsummaryrefslogtreecommitdiffstats
path: root/cputemp.c
blob: 559056f46190e5e314819552b7404f5c20694675 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
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);
}