File: lmstemp.cc

package info (click to toggle)
xosview 1.24-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,184 kB
  • sloc: cpp: 11,975; makefile: 154; ansic: 32; awk: 13; sh: 8
file content (380 lines) | stat: -rw-r--r-- 12,200 bytes parent folder | download | duplicates (5)
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
//
//  Copyright (c) 2000, 2006 by Leopold Toetsch <lt@toetsch.at>
//
//  Read temperature entries from /proc/sys/dev/sensors/*/*
//  and display actual and high temperature
//  if actual >= high, actual temp changes color to indicate alarm
//
//  File based on btrymeter.* by
//  Copyright (c) 1997 by Mike Romberg ( mike.romberg@noaa.gov )
//
//  This file may be distributed under terms of the GPL
//
//
//
#include "lmstemp.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <iostream>
#include <string>
#include <fstream>

static const char PROC_SENSORS[] = "/proc/sys/dev/sensors";
static const char SYS_SENSORS[]  = "/sys/class/hwmon";


LmsTemp::LmsTemp( XOSView *parent, const char *name, const char *tempfile,
                  const char *highfile, const char *lowfile, const char *label,
                  const char *caption, unsigned int nbr )
  : SensorFieldMeter( parent, label, caption, 1, 1, 0 ){
  _nbr = nbr;
  _scale = 1.0;
  _isproc = _name_found = _temp_found = _high_found = _low_found = false;

  // Check if high is given as value
  if ( highfile && sscanf(highfile, "%lf", &high_) > 0 ) {
    has_high_ = _high_found = true;
    highfile = NULL;
  }
  // Check if low is given as value
  if ( lowfile && sscanf(lowfile, "%lf", &low_) > 0 ) {
    has_low_ = _low_found = true;
    lowfile = NULL;
  }

  if ( !checksensors(name, tempfile, highfile, lowfile) ) {
    if ( !_name_found &&
         (( !_temp_found && tempfile[0] != '/' ) ||
          ( !_high_found && (highfile && highfile[0] != '/') ) ||
          ( !_low_found && (lowfile && lowfile[0] != '/') )) )
      std::cerr << label << " : No sensor named " << name << " was found in "
                << SYS_SENSORS << "." << std::endl;
    else {
      if (!_temp_found && tempfile[0] != '/') {
        std::cerr << label << " : Could not find file " << tempfile << "{,_input}";
        if (name)
          std::cerr << " under " << name << " in " << SYS_SENSORS;
        else
          std::cerr << " under " << PROC_SENSORS << " or " << SYS_SENSORS;
        std::cerr << "." << std::endl;
      }
      if (!_high_found && highfile && highfile[0] != '/') {
        std::cerr << label << " : Could not find file " << highfile;
        if (name)
          std::cerr << " under " << name << " in " << SYS_SENSORS;
        else
          std::cerr << " under " << SYS_SENSORS;
        std::cerr << "." << std::endl;
      }
      if (!_low_found && lowfile && lowfile[0] != '/') {
        std::cerr << label << " : Could not find file " << lowfile;
        if (name)
          std::cerr << " under " << name << " in " << SYS_SENSORS;
        else
          std::cerr << " under " << SYS_SENSORS;
        std::cerr << "." << std::endl;
      }
    }
    parent_->done(1);
  }
}

LmsTemp::~LmsTemp( void ){
}

bool LmsTemp::checksensors( const char *name, const char *tempfile,
                            const char *highfile, const char *lowfile ) {
// Logic:
//  0) tempfile must always be found, highfile and lowfile only if given
//  1) absolute path in any filename must match as is
//  2) if name is given, only files in that sysfs node can match
//  3) any filename matches as is
//  4) tempfile + "_input" matches and tempfile + "_max" and/or
//     tempfile + "_min" matches
  DIR *dir;
  struct dirent *ent;
  struct stat buf;
  std::string dirname, f, f2, n;

  /* First, check if absolute paths were given. */
  if (tempfile[0] == '/') {
    if ( stat(tempfile, &buf) == 0 && S_ISREG(buf.st_mode) ) {
      _tempfile = tempfile;
      _temp_found = true;
    }
    else
      std::cerr << title() << " : Could not find file " << tempfile << "." << std::endl;
  }
  if (highfile && highfile[0] == '/') {
    if ( stat(highfile, &buf) == 0 && S_ISREG(buf.st_mode) ) {
      _highfile = highfile;
      _high_found = true;
    }
    else
      std::cerr << title() << " : Could not find file " << highfile << "." << std::endl;
  }
  if (lowfile && lowfile[0] == '/') {
    if ( stat(lowfile, &buf) == 0 && S_ISREG(buf.st_mode) ) {
      _lowfile = lowfile;
      _low_found = true;
    }
    else
      std::cerr << title() << " : Could not find file " << lowfile << "." << std::endl;
  }

  if ( _temp_found && (_high_found || !highfile) && (_low_found || !lowfile) ) {
    _isproc = ( strncmp(_tempfile.c_str(), "/proc", 5) ? false : true );
    return true;
  }

  /* Then, try to find the given file. */
  /* Try /proc first. */
  if ( (dir = opendir(PROC_SENSORS)) ) {
    while ( !_temp_found && (ent = readdir(dir)) ) {
      if ( !strncmp(ent->d_name, ".", 1) ||
           !strncmp(ent->d_name, "..", 2) )
        continue;

      dirname = PROC_SENSORS;
      dirname += '/'; dirname += ent->d_name;
      if ( stat(dirname.c_str(), &buf) == 0 && S_ISDIR(buf.st_mode) ) {
        f = dirname + '/' + tempfile;
        if ( stat(f.c_str(), &buf) == 0 && S_ISREG(buf.st_mode) ) {
          _temp_found = true;
          _tempfile = f;
          _isproc = true;
        }
      }
    }
    closedir(dir);
    if (_temp_found)
      return true;
  }

  /* Next, try /sys. */
  if ( !(dir = opendir(SYS_SENSORS)) )
    return false;

  while ( !(_temp_found && (_high_found || !highfile) && (_low_found || !lowfile) ) &&
           (ent = readdir(dir)) ) {
    if ( !strncmp(ent->d_name, ".", 1) ||
         !strncmp(ent->d_name, "..", 2) )
      continue;

    // Try every node under /sys/class/hwmon
    dirname = SYS_SENSORS;
    dirname += '/'; dirname += ent->d_name;

    int i = 0;
    do {
      if ( stat(dirname.c_str(), &buf) == 0 && S_ISDIR(buf.st_mode) ) {
        // Try to get the sensor's name
        f = dirname + "/name";
        std::ifstream namefile( f.c_str() );
        if ( namefile.good() ) {
          namefile >> n;
          namefile.close();
        }
        if (!name || n == name) {
          // Either no name was given, or the name matches.
          // Check if the files exist here.
          _name_found = true;
          if (highfile && !_high_found) {
            f = dirname + '/' + highfile;
            if ( stat(f.c_str(), &buf) == 0 && S_ISREG(buf.st_mode) ) {
              _high_found = true;
              _highfile = f;
            }
          }
          if (lowfile && !_low_found) {
            f = dirname + '/' + lowfile;
            if ( stat(f.c_str(), &buf) == 0 && S_ISREG(buf.st_mode) ) {
              _low_found = true;
              _lowfile = f;
            }
          }
          f = dirname + '/' + tempfile;
          if ( stat(f.c_str(), &buf) == 0 && S_ISREG(buf.st_mode) ) {
            _temp_found = true;
            _tempfile = f;
          }
          else {
            f2 = f + "_input";
            if ( stat(f2.c_str(), &buf) == 0 && S_ISREG(buf.st_mode) ) {
              _temp_found = true;
              _tempfile = f2;
              f2 = f + "_max";
              if ( !_high_found && !highfile &&
                   stat(f2.c_str(), &buf) == 0 && S_ISREG(buf.st_mode) ) {
                _high_found = true;
                _highfile = f2;
              }
              f2 = f + "_min";
              if ( !_low_found && !lowfile &&
                   stat(f2.c_str(), &buf) == 0 && S_ISREG(buf.st_mode) ) {
                _low_found = true;
                _lowfile = f2;
              }
            }
          }
        }
      }

      // Some /sys sensors have the files in subdirectory /device
      dirname += "/device";
    } while ( ++i < 2 && !( _temp_found && (_high_found || !highfile) &&
                           (_low_found || !lowfile) ) );
  }
  closedir(dir);
  // No highfile or lowfile is OK.
  if (!highfile)
    _high_found = true;
  if (!lowfile)
    _low_found = true;

  return (_temp_found & _high_found & _low_found);
}

/* Adapted from libsensors. */
void LmsTemp::determineScale( void ){
  char type[16], subtype[32];
  int n;
  std::string basename = _tempfile.substr(_tempfile.find_last_of('/') + 1);
  if ( sscanf(basename.c_str(), "%[a-z]%d_%s", type, &n, subtype) == 3 ) {
    if (( !strncmp(type, "in", strlen(type)) &&
          !strncmp(subtype, "input", strlen(subtype)) ) ||
        ( !strncmp(type, "temp", strlen(type)) &&
          !strncmp(subtype, "input", strlen(subtype)) ) ||
        ( !strncmp(type, "temp", strlen(type)) &&
          !strncmp(subtype, "offset", strlen(subtype)) ) ||
        ( !strncmp(type, "curr", strlen(type)) &&
          !strncmp(subtype, "input", strlen(subtype)) ) ||
        ( !strncmp(type, "power", strlen(type)) &&
          !strncmp(subtype, "average_interval", strlen(subtype)) ) ||
        ( !strncmp(type, "cpu", strlen(type)) &&
          !strncmp(subtype, "vid", strlen(subtype)) )) {
      _scale = 1000.0;
      return;
    }
    if (( !strncmp(type, "power", strlen(type)) &&
          !strncmp(subtype, "average", strlen(subtype)) ) ||
        ( !strncmp(type, "energy", strlen(type)) &&
          !strncmp(subtype, "input", strlen(subtype)) )) {
      _scale = 1000000.0;
      return;
    }
  }
  _scale = 1.0;
}

void LmsTemp::determineUnit( void ){
  char type[16], subtype[32];
  int n;
  std::string basename = _tempfile.substr(_tempfile.find_last_of('/') + 1);
  if ( sscanf(basename.c_str(), "%[a-z]%d_%s", type, &n, subtype) == 3 ) {
    if ( !strncmp(type, "temp", strlen(type)) )
      strcpy(unit_, "\260C");
    else if ( !strncmp(type, "in", strlen(type)) ||
              !strncmp(subtype, "vid", strlen(type)) )
      strcpy(unit_, "V");
    else if ( !strncmp(type, "fan", strlen(type)) )
      strcpy(unit_, "RPM");
    else if ( !strncmp(type, "power", strlen(type)) ) {
      if ( strncmp(subtype, "average_interval", strlen(type)) )
        strcpy(unit_, "s");
      else
        strcpy(unit_, "W");
    }
    else if ( !strncmp(type, "energy", strlen(type)) )
      strcpy(unit_, "J");
    else if ( !strncmp(type, "curr", strlen(type)) )
      strcpy(unit_, "A");
    else if ( !strncmp(type, "humidity", strlen(type)) )
      strcpy(unit_, "%");
  }
}

void LmsTemp::checkResources( void ){
  SensorFieldMeter::checkResources();

  char s[32];
  const char *tmp = NULL;
  actcolor_  = parent_->allocColor( parent_->getResource( "lmstempActColor" ) );
  highcolor_ = parent_->allocColor( parent_->getResource( "lmstempHighColor" ) );
  lowcolor_  = parent_->allocColor( parent_->getResource( "lmstempLowColor" ) );
  setfieldcolor( 0, actcolor_ );
  setfieldcolor( 1, parent_->getResource( "lmstempIdleColor") );
  setfieldcolor( 2, highcolor_ );
  tmp = parent_->getResourceOrUseDefault( "lmstempHighest", "0" );
  snprintf(s, 32, "lmstempHighest%d", _nbr);
  total_ = fabs( atof( parent_->getResourceOrUseDefault(s, tmp) ) );
  priority_ = atoi( parent_->getResource( "lmstempPriority" ) );
  tmp = parent_->getResource( "lmstempUsedFormat" );
  snprintf(s, 32, "lmstempUsedFormat%d", _nbr);
  SetUsedFormat( parent_->getResourceOrUseDefault(s, tmp) );

  if ( !_highfile.empty() )
    has_high_ = true;
  if ( !_lowfile.empty() )
    has_low_ = true;

  if (!has_high_)
    high_ = total_;
  if (!has_low_)
    low_ = 0;

  determineScale();
  determineUnit();
  updateLegend();
}

void LmsTemp::checkevent( void ){
  getlmstemp();

  drawfields();
}

void LmsTemp::getlmstemp( void ){
  double high = high_, low = low_;

  std::ifstream tempfile( _tempfile.c_str() );
  if (!tempfile) {
    std::cerr << "Can not open file : " << _tempfile << std::endl;
    parent_->done(1);
    return;
  }

  if (_isproc)
    tempfile >> high >> low >> fields_[0];
  else {
    tempfile >> fields_[0];
    fields_[0] /= _scale;
    if ( !_highfile.empty() ) {
      std::ifstream highfile( _highfile.c_str() );
      if (!highfile) {
        std::cerr << "Can not open file : " << _highfile << std::endl;
        parent_->done(1);
        return;
      }
      highfile >> high;
      high /= _scale;
    }
    if ( !_lowfile.empty() ) {
      std::ifstream lowfile( _lowfile.c_str() );
      if (!lowfile) {
        std::cerr << "Can not open file : " << _lowfile << std::endl;
        parent_->done(1);
        return;
      }
      lowfile >> low;
      low /= _scale;
    }
  }

  checkFields(low, high);
}