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

Revision 4232, 12.6 KB (checked in by khali, 8 years ago)

Add user-space support for the Fintek F71872F/FG (chip name f71872f).

  • 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, 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, --no-adapter      Do not show adapter for each chip\n");
74  printf("  -U, --no-unknown      Do not show unknown chips\n");
75  printf("  -u, --unknown         Treat chips as unknown ones (testing only)\n");
76  printf("  -v, --version         Display the program version\n");
77  printf("\n");
78  printf("Use `-' after `-c' to read the config file from stdin.\n");
79  printf("If no chips are specified, all chip info will be printed.\n");
80  printf("Example chip names:\n");
81  printf("\tlm78-i2c-0-2d\t*-i2c-0-2d\n");
82  printf("\tlm78-i2c-0-*\t*-i2c-0-*\n");
83  printf("\tlm78-i2c-*-2d\t*-i2c-*-2d\n");
84  printf("\tlm78-i2c-*-*\t*-i2c-*-*\n");
85  printf("\tlm78-isa-0290\t*-isa-0290\n");
86  printf("\tlm78-isa-*\t*-isa-*\n");
87  printf("\tlm78-*\n");
88}
89
90void print_version(void)
91{
92  printf("%s version %s with libsensors version %s\n", PROGRAM, VERSION, libsensors_version);
93}
94
95/* This examines global var config_file, and leaves the name there too.
96   It also opens config_file. */
97void open_config_file(const char* config_file_name)
98{
99  if (!strcmp(config_file_name,"-")) {
100    config_file = stdin;
101    return;
102  }
103
104  config_file = fopen(config_file_name, "r");
105  if (!config_file) {
106    fprintf(stderr, "Could not open config file\n");
107    perror(config_file_name);
108    exit(1);
109  }
110}
111   
112void close_config_file(const char* config_file_name)
113{
114  if (fclose(config_file) == EOF) {
115    fprintf(stderr,"Could not close config file\n");
116    perror(config_file_name);
117  }
118}
119
120static void set_degstr(void)
121{
122  const char *deg_default_text[2] = {" C", " F"};
123
124#ifdef HAVE_ICONV
125  /* Size hardcoded for better performance.
126     Don't forget to count the trailing \0! */
127  size_t deg_latin1_size = 3;
128  char *deg_latin1_text[2] = {"\260C", "\260F"};
129  size_t nconv;
130  size_t degstr_size = sizeof(degstr);
131  char *degstr_ptr = degstr;
132
133  iconv_t cd = iconv_open(nl_langinfo(CODESET), "ISO-8859-1");
134  if (cd != (iconv_t) -1) {
135    nconv = iconv(cd, &(deg_latin1_text[fahrenheit]), &deg_latin1_size,
136                  &degstr_ptr, &degstr_size);
137    iconv_close(cd);
138   
139    if (nconv != (size_t) -1)
140      return;     
141  }
142#endif /* HAVE_ICONV */
143
144  /* There was an error during the conversion, use the default text */
145  strcpy(degstr, deg_default_text[fahrenheit]);
146}
147
148int main (int argc, char *argv[])
149{
150  int c,res,i,error;
151  const char *config_file_name = ETCDIR "/" DEFAULT_CONFIG_FILE_NAME;
152
153  struct option long_opts[] =  {
154    { "help", no_argument, NULL, 'h' },
155    { "set", no_argument, NULL, 's' },
156    { "version", no_argument, NULL, 'v'},
157    { "fahrenheit", no_argument, NULL, 'f' },
158    { "no-adapter", no_argument, NULL, 'A' },
159    { "no-unknown", no_argument, NULL, 'U' },
160    { "config-file", required_argument, NULL, 'c' },
161    { "unknown", no_argument, NULL, 'u' },
162    /* next option accepted for compatibility, but otherwise ignored */
163    { "algorithm", no_argument, NULL, 'a' },
164    { 0,0,0,0 }
165  };
166
167  setlocale(LC_CTYPE, "");
168
169  do_unknown = 0;
170  do_sets = 0;
171  hide_adapter = 0;
172  hide_unknown = 0;
173  while (1) {
174    c = getopt_long(argc,argv,"hsvfaAUc:u",long_opts,NULL);
175    if (c == EOF)
176      break;
177    switch(c) {
178    case ':':
179    case '?':
180      print_short_help();
181      exit(1);
182    case 'h':
183      print_long_help();
184      exit(0);
185    case 'v':
186      print_version();
187      exit(0);
188    case 'c':
189      config_file_name = optarg;
190      break;
191    case 's':
192      do_sets = 1;
193      break;
194    case 'f':
195      fahrenheit = 1;
196      break;
197    case 'A':
198      hide_adapter = 1;
199      break;
200    case 'U':
201      hide_unknown = 1;
202      break;
203    case 'u':
204      do_unknown = 1;
205      break;
206    case 'a':
207      /* Ignore for compatibility */
208      break;
209    default:
210      fprintf(stderr,"Internal error while parsing options!\n");
211      exit(1);
212    }
213  }
214
215  if (optind == argc) {
216    chips[0].prefix = SENSORS_CHIP_NAME_PREFIX_ANY;
217    chips[0].bus = SENSORS_CHIP_NAME_BUS_ANY;
218    chips[0].addr = SENSORS_CHIP_NAME_ADDR_ANY;
219    chips_count = 1;
220  } else 
221    for(i = optind; i < argc; i++) 
222      if ((res = sensors_parse_chip_name(argv[i],chips+chips_count))) {
223        fprintf(stderr,"Parse error in chip name `%s'\n",argv[i]);
224        print_short_help();
225        exit(1);
226      } else if (++chips_count == CHIPS_MAX) {
227        fprintf(stderr,"Too many chips on command line!\n");
228        exit(1);
229      }
230
231  open_config_file(config_file_name);
232  if ((res = sensors_init(config_file))) {
233    fprintf(stderr,"%s\n",sensors_strerror(res));
234    if (res == -SENSORS_ERR_PROC)
235      fprintf(stderr,
236              "Kernel interface access error\n"
237              "For 2.6 kernels, make sure you have mounted sysfs and libsensors\n"
238              "was compiled with sysfs support!\n");
239    exit(1);
240  }
241  close_config_file(config_file_name);
242
243  /* build the degrees string */
244  set_degstr();
245
246  if(do_the_real_work(&error)) {
247    sensors_cleanup();
248    exit(error);
249  } else {
250    if(chips[0].prefix == SENSORS_CHIP_NAME_PREFIX_ANY)
251            fprintf(stderr,
252                    "No sensors found!\n"
253                    "Make sure you loaded all the kernel drivers you need.\n"
254                    "Try sensors-detect to find out which these are.\n");
255    else
256            fprintf(stderr,"Specified sensor(s) not found!\n");
257    sensors_cleanup();
258    exit(1);
259  }
260}
261
262/* returns number of chips found */
263int do_the_real_work(int *error)
264{
265  const sensors_chip_name *chip;
266  int chip_nr,i;
267  int cnt = 0;
268
269  *error = 0;
270  for (chip_nr = 0; (chip = sensors_get_detected_chips(&chip_nr));)
271    for(i = 0; i < chips_count; i++)
272      if (sensors_match_chip(*chip,chips[i])) {
273        if(do_sets) {
274          if (do_a_set(*chip))
275            *error = 1;
276        } else
277          do_a_print(*chip);
278        i = chips_count;
279        cnt++;
280      }
281   return(cnt);
282}
283
284/* returns 1 on error */
285int do_a_set(sensors_chip_name name)
286{
287  int res;
288
289  if ((res = sensors_do_chip_sets(name))) {
290    if (res == -SENSORS_ERR_PROC) {
291      fprintf(stderr,"%s: %s for writing;\n",sprintf_chip_name(name),
292              sensors_strerror(res));
293      fprintf(stderr,"Run as root?\n");
294      return 1;
295    } else if (res == -SENSORS_ERR_ACCESS_W) {
296      fprintf(stderr, "%s: At least one \"set\" statement failed\n",
297              sprintf_chip_name(name));
298    } else {
299      fprintf(stderr,"%s: %s\n",sprintf_chip_name(name),
300              sensors_strerror(res));
301    }
302  }
303  return 0;
304}
305
306const char *sprintf_chip_name(sensors_chip_name name)
307{
308  #define BUF_SIZE 200
309  static char buf[BUF_SIZE];
310
311  if (name.bus == SENSORS_CHIP_NAME_BUS_ISA)
312    snprintf(buf,BUF_SIZE,"%s-isa-%04x",name.prefix,name.addr);
313  else if (name.bus == SENSORS_CHIP_NAME_BUS_PCI)
314    snprintf(buf,BUF_SIZE,"%s-pci-%04x",name.prefix,name.addr);
315  else if (name.bus == SENSORS_CHIP_NAME_BUS_DUMMY)
316    snprintf(buf,BUF_SIZE,"%s-%s-%04x",name.prefix,name.busname,name.addr);
317  else
318    snprintf(buf,BUF_SIZE,"%s-i2c-%d-%02x",name.prefix,name.bus,name.addr);
319  return buf;
320}
321
322struct match {
323        const char * prefix;
324        void (*fn) (const sensors_chip_name *name);
325};
326
327struct match matches[] = {
328        { "ds1621", print_ds1621 },
329        { "lm75", print_lm75 },
330        { "adm1021", print_adm1021 },
331        { "max1617", print_adm1021 },
332        { "max1617a", print_adm1021 },
333        { "thmc10", print_adm1021 },
334        { "lm84", print_adm1021 },
335        { "gl523", print_adm1021 },
336        { "adm1023", print_adm1021 },
337        { "mc1066", print_adm1021 },
338        { "adm9240", print_adm9240 },
339        { "ds1780", print_adm9240 },
340        { "lm81", print_adm9240 },
341        { "lm78", print_lm78 },
342        { "lm78-j", print_lm78 },
343        { "lm79", print_lm78 },
344        { "mtp008", print_mtp008 },
345        { "sis5595", print_sis5595 },
346        { "via686a", print_via686a },
347        { "lm80", print_lm80 },
348        { "lm85", print_lm85 },
349        { "lm85b", print_lm85 },
350        { "lm85c", print_lm85 },
351        { "adm1027", print_lm85 },
352        { "adt7463", print_lm85 },
353        { "emc6d100", print_lm85 },
354        { "emc6d102", print_lm85 },
355        { "lm87", print_lm87 },
356        { "gl518sm", print_gl518 },
357        { "gl520sm", print_gl520 },
358        { "adm1025", print_adm1025 },
359        { "ne1619", print_adm1025 },
360        { "adm1024", print_adm1024 },
361        { "w83781d", print_w83781d },
362        { "w83782d", print_w83781d },
363        { "w83783d", print_w83781d },
364        { "w83627hf", print_w83781d },
365        { "w83627thf", print_w83781d },
366        { "w83637hf", print_w83781d },
367        { "w83697hf", print_w83781d },
368        { "w83687thf", print_w83781d },
369        { "w83627ehf", print_w83627ehf },
370        { "w83791d", print_w83781d },
371        { "w83792d", print_w83792d },
372        { "w83793", print_w83793 },
373        { "w83l785ts", print_w83l785ts },
374        { "as99127f", print_w83781d },
375        { "maxilife", print_maxilife },
376        { "maxilife-cg", print_maxilife },
377        { "maxilife-co", print_maxilife },
378        { "maxilife-as", print_maxilife },
379        { "maxilife-nba", print_maxilife },
380        { "it87", print_it87 },
381        { "it8712", print_it87 },
382        { "it8716", print_it87 },
383        { "it8718", print_it87 },
384        { "ddcmon", print_ddcmon },
385        { "eeprom", print_eeprom },
386        { "fscpos", print_fscpos },
387        { "fscscy", print_fscscy },
388        { "fscher", print_fscher },
389        { "pcf8591", print_pcf8591 },
390        { "vt1211", print_vt1211 },
391        { "smsc47m192", print_smsc47m192 },
392        { "smsc47m1", print_smsc47m1 },
393        { "pc87360", print_pc87360 },
394        { "pc87363", print_pc87360 },
395        { "pc87364", print_pc87364 },
396        { "pc87365", print_pc87366 },
397        { "pc87366", print_pc87366 },
398        { "pc87427", print_pc87427 },
399        { "lm92", print_lm92 },
400        { "vt8231", print_vt8231 },
401        { "bmc", print_bmc },
402        { "adm1026", print_adm1026 },
403        { "lm83", print_lm83 },
404        { "lm90", print_lm90 },
405        { "adm1032", print_lm90 },
406        { "lm99", print_lm90 },
407        { "lm86", print_lm90 },
408        { "max6657", print_lm90 },
409        { "adt7461", print_lm90 },
410        { "lm63", print_lm63 },
411        { "xeontemp", print_xeontemp },
412        { "max6650", print_max6650 },
413        { "asb100", print_asb100 },
414        { "adm1030", print_adm1031 },
415        { "adm1031", print_adm1031 },
416        { "lm93", print_lm93 },
417        { "smsc47b397", print_smsc47b397 },
418        { "f71805f", print_f71805f },
419        { "f71872f", print_f71805f },
420        { "abituguru", print_abituguru },
421        { "k8temp", print_k8temp },
422        { "coretemp", print_coretemp },
423        { NULL, NULL }
424};
425
426void do_a_print(sensors_chip_name name)
427{
428  const char *adap;
429  struct match *m;
430
431  /* do we know how to display it? */
432  for(m = matches; m->prefix != NULL; m++) {
433    if(!strcmp(name.prefix, m->prefix)) break;
434  }
435
436  if(m->prefix==NULL && hide_unknown)
437    return;
438
439  printf("%s\n",sprintf_chip_name(name));
440  adap = sensors_get_adapter_name(name.bus);
441  if (adap && !hide_adapter)
442    printf("Adapter: %s\n",adap);
443  if (!adap)
444    fprintf(stderr, "Can't get adapter name for bus %d\n", name.bus);
445  if (do_unknown)
446    print_unknown_chip(&name);
447  else {
448    if(m->prefix == NULL)
449        print_unknown_chip(&name);
450    else
451        m->fn(&name);
452  }
453  printf("\n");
454}
Note: See TracBrowser for help on using the browser.