root/lm-sensors/trunk/prog/sensors/main.c @ 4089

Revision 4089, 12.5 KB (checked in by khali, 8 years ago)

Add support for the ITE IT8718F Super-I/O chip (prefix: it8718).
Unsurprisingly, it is very similar to the IT8716F.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/*
2    main.c - Part of sensors, a user-space program for hardware monitoring
3    Copyright (c) 1998, 1999  Frodo Looijaard <frodol@dds.nl>
4
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 2 of the License, or
8    (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software
17    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18*/
19
20#include <stdio.h>
21#include <stdlib.h>
22#include <getopt.h>
23#include <string.h>
24#include <errno.h>
25#include <locale.h>
26#include <langinfo.h>
27
28#ifndef __UCLIBC__
29#include <iconv.h>
30#define HAVE_ICONV
31#endif
32
33#include "lib/sensors.h"
34#include "lib/error.h"
35#include "chips.h"
36#include "version.h"
37
38#define PROGRAM "sensors"
39#define VERSION LM_VERSION
40#define DEFAULT_CONFIG_FILE_NAME "sensors.conf"
41
42FILE *config_file;
43extern const char *libsensors_version;
44
45extern int main(int argc, char *arv[]);
46static void print_short_help(void);
47static void print_long_help(void);
48static void print_version(void);
49static void do_a_print(sensors_chip_name name);
50static int do_a_set(sensors_chip_name name);
51static int do_the_real_work(int *error);
52static const char *sprintf_chip_name(sensors_chip_name name);
53
54#define CHIPS_MAX 20
55sensors_chip_name chips[CHIPS_MAX];
56int chips_count=0;
57int do_sets, do_unknown, fahrenheit, show_algorithm, hide_adapter, hide_unknown;
58
59char degstr[5]; /* store the correct string to print degrees */
60
61void print_short_help(void)
62{
63  printf("Try `%s -h' for more information\n",PROGRAM);
64}
65
66void print_long_help(void)
67{
68  printf("Usage: %s [OPTION]... [CHIP]...\n",PROGRAM);
69  printf("  -c, --config-file     Specify a config file (default: " ETCDIR "/" DEFAULT_CONFIG_FILE_NAME ")\n");
70  printf("  -h, --help            Display this help text\n");
71  printf("  -s, --set             Execute `set' statements too (root only)\n");
72  printf("  -f, --fahrenheit      Show temperatures in degrees fahrenheit\n");
73  printf("  -a, --algorithm       Show algorithm for each chip\n");
74  printf("  -A, --no-adapter      Do not show adapter for each chip\n");
75  printf("  -U, --no-unknown      Do not show unknown chips\n");
76  printf("  -u, --unknown         Treat chips as unknown ones (testing only)\n");
77  printf("  -v, --version         Display the program version\n");
78  printf("\n");
79  printf("Use `-' after `-c' to read the config file from stdin.\n");
80  printf("If no chips are specified, all chip info will be printed.\n");
81  printf("Example chip names:\n");
82  printf("\tlm78-i2c-0-2d\t*-i2c-0-2d\n");
83  printf("\tlm78-i2c-0-*\t*-i2c-0-*\n");
84  printf("\tlm78-i2c-*-2d\t*-i2c-*-2d\n");
85  printf("\tlm78-i2c-*-*\t*-i2c-*-*\n");
86  printf("\tlm78-isa-0290\t*-isa-0290\n");
87  printf("\tlm78-isa-*\t*-isa-*\n");
88  printf("\tlm78-*\n");
89}
90
91void print_version(void)
92{
93  printf("%s version %s with libsensors version %s\n", PROGRAM, VERSION, libsensors_version);
94}
95
96/* This examines global var config_file, and leaves the name there too.
97   It also opens config_file. */
98void open_config_file(const char* config_file_name)
99{
100  if (!strcmp(config_file_name,"-")) {
101    config_file = stdin;
102    return;
103  }
104
105  config_file = fopen(config_file_name, "r");
106  if (!config_file) {
107    fprintf(stderr, "Could not open config file\n");
108    perror(config_file_name);
109    exit(1);
110  }
111}
112   
113void close_config_file(const char* config_file_name)
114{
115  if (fclose(config_file) == EOF) {
116    fprintf(stderr,"Could not close config file\n");
117    perror(config_file_name);
118  }
119}
120
121static void set_degstr(void)
122{
123  const char *deg_default_text[2] = {" C", " F"};
124
125#ifdef HAVE_ICONV
126  /* Size hardcoded for better performance.
127     Don't forget to count the trailing \0! */
128  size_t deg_latin1_size = 3;
129  char *deg_latin1_text[2] = {"\260C", "\260F"};
130  size_t nconv;
131  size_t degstr_size = sizeof(degstr);
132  char *degstr_ptr = degstr;
133
134  iconv_t cd = iconv_open(nl_langinfo(CODESET), "ISO-8859-1");
135  if (cd != (iconv_t) -1) {
136    nconv = iconv(cd, &(deg_latin1_text[fahrenheit]), &deg_latin1_size,
137                  &degstr_ptr, &degstr_size);
138    iconv_close(cd);
139   
140    if (nconv != (size_t) -1)
141      return;     
142  }
143#endif /* HAVE_ICONV */
144
145  /* There was an error during the conversion, use the default text */
146  strcpy(degstr, deg_default_text[fahrenheit]);
147}
148
149int main (int argc, char *argv[])
150{
151  int c,res,i,error;
152  char *config_file_name = NULL;
153
154  struct option long_opts[] =  {
155    { "help", no_argument, NULL, 'h' },
156    { "set", no_argument, NULL, 's' },
157    { "version", no_argument, NULL, 'v'},
158    { "fahrenheit", no_argument, NULL, 'f' },
159    { "algorithm", no_argument, NULL, 'a' },
160    { "no-adapter", no_argument, NULL, 'A' },
161    { "no-unknown", no_argument, NULL, 'U' },
162    { "config-file", required_argument, NULL, 'c' },
163    { "unknown", no_argument, NULL, 'u' },
164    { 0,0,0,0 }
165  };
166
167  setlocale(LC_CTYPE, "");
168
169  do_unknown = 0;
170  do_sets = 0;
171  show_algorithm = 0;
172  hide_adapter = 0;
173  hide_unknown = 0;
174  while (1) {
175    c = getopt_long(argc,argv,"hsvfaAUc:u",long_opts,NULL);
176    if (c == EOF)
177      break;
178    switch(c) {
179    case ':':
180    case '?':
181      print_short_help();
182      exit(1);
183    case 'h':
184      print_long_help();
185      exit(0);
186    case 'v':
187      print_version();
188      exit(0);
189    case 'c':
190      config_file_name = strdup(optarg);
191      break;
192    case 's':
193      do_sets = 1;
194      break;
195    case 'f':
196      fahrenheit = 1;
197      break;
198    case 'a':
199      show_algorithm = 1;
200      break;
201    case 'A':
202      hide_adapter = 1;
203      break;
204    case 'U':
205      hide_unknown = 1;
206      break;
207    case 'u':
208      do_unknown = 1;
209      break;
210    default:
211      fprintf(stderr,"Internal error while parsing options!\n");
212      exit(1);
213    }
214  }
215
216  if (optind == argc) {
217    chips[0].prefix = SENSORS_CHIP_NAME_PREFIX_ANY;
218    chips[0].bus = SENSORS_CHIP_NAME_BUS_ANY;
219    chips[0].addr = SENSORS_CHIP_NAME_ADDR_ANY;
220    chips_count = 1;
221  } else 
222    for(i = optind; i < argc; i++) 
223      if ((res = sensors_parse_chip_name(argv[i],chips+chips_count))) {
224        fprintf(stderr,"Parse error in chip name `%s'\n",argv[i]);
225        print_short_help();
226        exit(1);
227      } else if (++chips_count == CHIPS_MAX) {
228        fprintf(stderr,"Too many chips on command line!\n");
229        exit(1);
230      }
231
232
233  if (config_file_name == NULL)
234    config_file_name = strdup(ETCDIR "/" DEFAULT_CONFIG_FILE_NAME);
235  open_config_file(config_file_name);
236
237  if ((res = sensors_init(config_file))) {
238    fprintf(stderr,"%s\n",sensors_strerror(res));
239    if (res == -SENSORS_ERR_PROC)
240      fprintf(stderr,
241              "Unable to find i2c bus information;\n"
242              "For 2.6 kernels, make sure you have mounted sysfs and libsensors\n"
243              "was compiled with sysfs support!\n"
244              "For older kernels, make sure you have done 'modprobe i2c-proc'!\n");
245    exit(1);
246  }
247
248  close_config_file(config_file_name);
249  free(config_file_name);
250
251  /* build the degrees string */
252  set_degstr();
253
254  if(do_the_real_work(&error)) {
255    sensors_cleanup();
256    exit(error);
257  } else {
258    if(chips[0].prefix == SENSORS_CHIP_NAME_PREFIX_ANY)
259            fprintf(stderr,"No sensors found!\n");
260    else
261            fprintf(stderr,"Specified sensor(s) not found!\n");
262    sensors_cleanup();
263    exit(1);
264  }
265}
266
267/* returns number of chips found */
268int do_the_real_work(int *error)
269{
270  const sensors_chip_name *chip;
271  int chip_nr,i;
272  int cnt = 0;
273
274  *error = 0;
275  for (chip_nr = 0; (chip = sensors_get_detected_chips(&chip_nr));)
276    for(i = 0; i < chips_count; i++)
277      if (sensors_match_chip(*chip,chips[i])) {
278        if(do_sets) {
279          if (do_a_set(*chip))
280            *error = 1;
281        } else
282          do_a_print(*chip);
283        i = chips_count;
284        cnt++;
285      }
286   return(cnt);
287}
288
289/* returns 1 on error */
290int do_a_set(sensors_chip_name name)
291{
292  int res;
293
294  if ((res = sensors_do_chip_sets(name))) {
295    if (res == -SENSORS_ERR_PROC) {
296      fprintf(stderr,"%s: %s for writing;\n",sprintf_chip_name(name),
297              sensors_strerror(res));
298      fprintf(stderr,"Run as root?\n");
299      return 1;
300    } else if (res == -SENSORS_ERR_ACCESS_W) {
301      fprintf(stderr, "%s: At least one \"set\" statement failed\n",
302              sprintf_chip_name(name));
303    } else {
304      fprintf(stderr,"%s: %s\n",sprintf_chip_name(name),
305              sensors_strerror(res));
306    }
307  }
308  return 0;
309}
310
311const char *sprintf_chip_name(sensors_chip_name name)
312{
313  #define BUF_SIZE 200
314  static char buf[BUF_SIZE];
315
316  if (name.bus == SENSORS_CHIP_NAME_BUS_ISA)
317    snprintf(buf,BUF_SIZE,"%s-isa-%04x",name.prefix,name.addr);
318  else if (name.bus == SENSORS_CHIP_NAME_BUS_DUMMY)
319    snprintf(buf,BUF_SIZE,"%s-%s-%04x",name.prefix,name.busname,name.addr);
320  else
321    snprintf(buf,BUF_SIZE,"%s-i2c-%d-%02x",name.prefix,name.bus,name.addr);
322  return buf;
323}
324
325struct match {
326        const char * prefix;
327        void (*fn) (const sensors_chip_name *name);
328};
329
330struct match matches[] = {
331        { "ds1621", print_ds1621 },
332        { "lm75", print_lm75 },
333        { "adm1021", print_adm1021 },
334        { "max1617", print_adm1021 },
335        { "max1617a", print_adm1021 },
336        { "thmc10", print_adm1021 },
337        { "lm84", print_adm1021 },
338        { "gl523", print_adm1021 },
339        { "adm1023", print_adm1021 },
340        { "mc1066", print_adm1021 },
341        { "adm9240", print_adm9240 },
342        { "ds1780", print_adm9240 },
343        { "lm81", print_adm9240 },
344        { "lm78", print_lm78 },
345        { "lm78-j", print_lm78 },
346        { "lm79", print_lm78 },
347        { "mtp008", print_mtp008 },
348        { "sis5595", print_sis5595 },
349        { "via686a", print_via686a },
350        { "lm80", print_lm80 },
351        { "lm85", print_lm85 },
352        { "lm85b", print_lm85 },
353        { "lm85c", print_lm85 },
354        { "adm1027", print_lm85 },
355        { "adt7463", print_lm85 },
356        { "emc6d100", print_lm85 },
357        { "emc6d102", print_lm85 },
358        { "lm87", print_lm87 },
359        { "gl518sm", print_gl518 },
360        { "gl520sm", print_gl520 },
361        { "adm1025", print_adm1025 },
362        { "ne1619", print_adm1025 },
363        { "adm1024", print_adm1024 },
364        { "w83781d", print_w83781d },
365        { "w83782d", print_w83781d },
366        { "w83783d", print_w83781d },
367        { "w83627hf", print_w83781d },
368        { "w83627thf", print_w83781d },
369        { "w83637hf", print_w83781d },
370        { "w83697hf", print_w83781d },
371        { "w83687thf", print_w83781d },
372        { "w83627ehf", print_w83627ehf },
373        { "w83791d", print_w83781d },
374        { "w83792d", print_w83792d },
375        { "w83l785ts", print_w83l785ts },
376        { "as99127f", print_w83781d },
377        { "maxilife", print_maxilife },
378        { "maxilife-cg", print_maxilife },
379        { "maxilife-co", print_maxilife },
380        { "maxilife-as", print_maxilife },
381        { "maxilife-nba", print_maxilife },
382        { "it87", print_it87 },
383        { "it8712", print_it87 },
384        { "it8716", print_it87 },
385        { "it8718", print_it87 },
386        { "ddcmon", print_ddcmon },
387        { "eeprom", print_eeprom },
388        { "fscpos", print_fscpos },
389        { "fscscy", print_fscscy },
390        { "fscher", print_fscher },
391        { "pcf8591", print_pcf8591 },
392        { "vt1211", print_vt1211 },
393        { "smsc47m192", print_smsc47m192 },
394        { "smsc47m1", print_smsc47m1 },
395        { "pc87360", print_pc87360 },
396        { "pc87363", print_pc87360 },
397        { "pc87364", print_pc87364 },
398        { "pc87365", print_pc87366 },
399        { "pc87366", print_pc87366 },
400        { "lm92", print_lm92 },
401        { "vt8231", print_vt8231 },
402        { "bmc", print_bmc },
403        { "adm1026", print_adm1026 },
404        { "lm83", print_lm83 },
405        { "lm90", print_lm90 },
406        { "adm1032", print_lm90 },
407        { "lm99", print_lm90 },
408        { "lm86", print_lm90 },
409        { "max6657", print_lm90 },
410        { "adt7461", print_lm90 },
411        { "lm63", print_lm63 },
412        { "xeontemp", print_xeontemp },
413        { "max6650", print_max6650 },
414        { "asb100", print_asb100 },
415        { "adm1030", print_adm1031 },
416        { "adm1031", print_adm1031 },
417        { "lm93", print_lm93 },
418        { "smsc47b397", print_smsc47b397 },
419        { "f71805f", print_f71805f },
420        { "abituguru", print_abituguru },
421        { NULL, NULL }
422};
423
424void do_a_print(sensors_chip_name name)
425{
426  const char *algo,*adap;
427  struct match *m;
428
429  /* do we know how to display it? */
430  for(m = matches; m->prefix != NULL; m++) {
431    if(!strcmp(name.prefix, m->prefix)) break;
432  }
433
434  if(m->prefix==NULL && hide_unknown)
435    return;
436
437  printf("%s\n",sprintf_chip_name(name));
438  adap = sensors_get_adapter_name(name.bus);
439  if (adap && !hide_adapter)
440    printf("Adapter: %s\n",adap);
441  algo = sensors_get_algorithm_name(name.bus);
442  if (algo && show_algorithm)
443    printf("Algorithm: %s\n",algo);
444  if (!algo || !adap)
445    printf(" ERROR: Can't get adapter or algorithm?!?\n");
446  if (do_unknown)
447    print_unknown_chip(&name);
448  else {
449    if(m->prefix == NULL)
450        print_unknown_chip(&name);
451    else
452        m->fn(&name);
453  }
454  printf("\n");
455}
Note: See TracBrowser for help on using the browser.