-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemps.c
151 lines (111 loc) · 4.27 KB
/
temps.c
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <stdbool.h>
#include <errno.h>
#include <stdio.h>
#include <dirent.h> // DIR, struct dirent, opendir, readdir
#include "sysmond.h"
/* See /usr/share/doc/linux<version>/hwmon/sysfs-interface.rst
Output for each interface:
buffer = interface:<name>,label:degC,...\n
strcat( out_string, buffer );
*/
// Start functions
void get_temps( char* out_string )
{
DIR* dir_fp;
DIR* subdir_fp;
struct dirent* ent; // /sys/class/hwmon entry
struct dirent* sub_ent; // subdirectory entry
char path [ 288 ]; // Size takes into account
char name [ 288 ]; // max size of filename
char interface[ 128 ];
char buffer [ 256 ];
char tempString [ 32 ][ 32 ];
char tempEntries[ 32 ][ 4 ]; // Numeric strings 1 or 2 chars
int n; // Number of temperature entries
// For each entry in hwmon, get info
dir_fp = opendir( "/sys/class/hwmon" );
while( (ent = readdir( dir_fp )) != NULL )
{
// Skip . and ..
if ( strncmp( ent->d_name, ".", 1 ) == 0 ) continue;
sprintf( path, "/sys/class/hwmon/%s", ent->d_name ); // Path to subdir
sprintf( name, "/sys/class/hwmon/%s/name", ent->d_name ); // Path to subdir/name
// Get interface name
FILE* fp = fopen( name, "r" );
fgets( interface, sizeof(interface), fp );
fclose( fp );
// Add to buffer string and remove newline
//printf( "interface %s", interface ); // includes a newline
sprintf( buffer, "interface;%s", interface );
buffer[ strcspn(buffer, "\n") ] = 0;
// Open the subdirectory
subdir_fp = opendir( path );
n = 0;
// Get 'temp<num>_' entries for each file in the directory
while( (sub_ent = readdir( subdir_fp ) ) != NULL )
{
// We only want entries like temp??_
if ( strncmp( sub_ent->d_name, "temp", 4 ) != 0 ) continue;
char filename[32]; // name of directory entry
strcpy( filename, sub_ent->d_name );
char* p = strtok( sub_ent->d_name, "_" );
char num[8];
strcpy( num, &p[4] );
// num is now number as in "temp" . num . "_somthing"
// If new num, save it in tempEntries
if ( n > 0 )
{
for ( int i = 0; i < n; i++ )
{
if ( strcmp( num, tempEntries[ i ] ) == 0 ) goto continue2;
}
}
strcpy( tempEntries[ n ], num );
n++;
continue2:
} // while readdir( subdir_fp )
closedir( subdir_fp ); // close subdirectory
chdir( path ); // Make it easier to get file names
// Cycle through for each temperature entry
for ( int i = 0; i < n; i++ )
{
// Read label if it exists
char label_name[ 192 ];
char label [ 32 ];
sprintf( label_name, "temp%s_label", tempEntries[ i ] );
FILE* label_fp = fopen( label_name, "r" );
if ( label_fp != NULL )
{
fgets( label, sizeof(label), label_fp);
fclose( label_fp );
}
else
strcpy( label, label_name );
// Remove newline
label[ strcspn(label, "\n") ] = 0;
// Now get the input temperature
char temperature [ 32 ];
char temperature_name[ 192 ];
sprintf( temperature_name, "temp%s_input", tempEntries[ i ] );
FILE* input_fp = fopen( temperature_name, "r" ); // open input file
fgets( temperature, sizeof(temperature), input_fp );
fclose( input_fp );
int degC = (int) strtol( temperature, NULL, 10 ) / 1000;
// If label only has one digit, insert a 0.
if ( strlen( tempEntries[ i ] ) == 1 &&
strncmp( label, "temp", 4) == 0 )
sprintf( label, "temp0%c", tempEntries[ i ][ 0 ] );
sprintf( tempString[ i ], ",%s:%d", label, degC );
// Add to buffer string
strcat( buffer, tempString[ i ] );
// Is data[256] large enough???
} // for each temp entry
// Add a newline to the buffer
strcat( buffer, "\n" );
strcat( out_string, buffer );
} // while reading /sys/class/hwmon
closedir( dir_fp );
}