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

Revision 1283, 10.1 KB (checked in by mds, 12 years ago)

add support for ADM1023 (adm1021 driver)

  • 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
26#include "lib/sensors.h"
27#include "lib/error.h"
28#include "chips.h"
29#include "version.h"
30
31#define PROGRAM "sensors"
32#define VERSION LM_VERSION
33#define DEFAULT_CONFIG_FILE_NAME "sensors.conf"
34
35static char *config_file_name;
36FILE *config_file;
37static const char *config_file_path[] = 
38  { "/etc", "/usr/lib/sensors", "/usr/local/lib/sensors", "/usr/lib",
39    "/usr/local/lib", ".", 0 };
40
41extern int main(int argc, char *arv[]);
42static void print_short_help(void);
43static void print_long_help(void);
44static void print_version(void);
45static void open_config_file(void);
46static int open_this_config_file(char *filename);
47static void do_a_print(sensors_chip_name name);
48static void do_a_set(sensors_chip_name name);
49static int do_the_real_work(void);
50static const char *sprintf_chip_name(sensors_chip_name name);
51
52#define CHIPS_MAX 20
53sensors_chip_name chips[CHIPS_MAX];
54int chips_count=0;
55int do_sets, do_unknown, fahrenheit;
56
57void print_short_help(void)
58{
59  printf("Try `%s -h' for more information\n",PROGRAM);
60}
61
62void print_long_help(void)
63{
64  printf("Usage: %s [OPTION]... [CHIP]...\n",PROGRAM);
65  printf("  -c, --config-file     Specify a config file\n");
66  printf("  -h, --help            Display this help text\n");
67  printf("  -s, --set             Execute `set' statements too (root only)\n");
68  printf("  -f, --fahrenheit      Show temperatures in degrees fahrenheit\n" );
69  printf("  -u, --unknown         Treat chips as unknown ones (testing only)\n");
70  printf("  -v, --version         Display the program version\n");
71  printf("\n");
72  printf("By default, a list of directories is examined for config file `sensors.conf'.\n");
73  printf("Use `-' after `-c' to read the config file from stdin.\n");
74  printf("If no chips are specified, all chip info will be printed.\n");
75  printf("Example chip names:\n");
76  printf("\tlm78-i2c-0-2d\t*-i2c-0-2d\n");
77  printf("\tlm78-i2c-0-*\t*-i2c-0-*\n");
78  printf("\tlm78-i2c-*-2d\t*-i2c-*-2d\n");
79  printf("\tlm78-i2c-*-*\t*-i2c-*-*\n");
80  printf("\tlm78-isa-0290\t*-isa-0290\n");
81  printf("\tlm78-isa-*\t*-isa-*\n");
82  printf("\tlm78-*\n");
83}
84
85void print_version(void)
86{
87  printf("%s version %s\n", PROGRAM, VERSION);
88}
89
90/* This examines global var config_file, and leaves the name there too.
91   It also opens config_file. */
92void open_config_file(void)
93{
94#define MAX_FILENAME_LEN 1024
95  char *filename;
96  char buffer[MAX_FILENAME_LEN];
97  int res,i;
98
99  if (config_file_name && !strcmp(config_file_name,"-")) {
100    config_file = stdin;
101    return;
102  } else if (config_file_name && index(config_file_name,'/')) {
103    if ((res = open_this_config_file(config_file_name))) {
104      fprintf(stderr,"Could not locate or open config file\n");
105      fprintf(stderr,"%s: %s\n",config_file_name,strerror(res));
106      exit(1);
107    }
108  }
109  else {
110    if (config_file_name)
111      filename = config_file_name;
112    else
113      filename = strdup(DEFAULT_CONFIG_FILE_NAME);
114    for (i = 0; config_file_path[i]; i++) {
115      if ((snprintf(buffer,MAX_FILENAME_LEN,
116                   "%s/%s",config_file_path[i],filename)) < 1) {
117        fprintf(stderr,
118                "open_config_file: ridiculous long config file name!\n");
119        exit(1);
120      }
121      if (!open_this_config_file(buffer)) {
122        free(config_file_name);
123        config_file_name = strdup(buffer);
124        return;
125      }
126    }
127    fprintf(stderr,"Could not locate or open config file!\n");
128    exit(1);
129  }
130}
131   
132int open_this_config_file(char *filename)
133{
134  config_file = fopen(filename,"r");
135  if (! config_file)
136    return -errno;
137  return 0;
138}
139 
140
141int main (int argc, char *argv[])
142{
143  int c,res,i;
144
145  struct option long_opts[] =  {
146    { "help", no_argument, NULL, 'h' },
147    { "set", no_argument, NULL, 's' },
148    { "version", no_argument, NULL, 'v'},
149    { "fahrenheit", no_argument, NULL, 'f' },
150    { "config-file", required_argument, NULL, 'c' },
151    { "unknown", required_argument, NULL, 'u' },
152    { 0,0,0,0 }
153  };
154
155  do_unknown = 0;
156  do_sets = 0;
157  while (1) {
158    c = getopt_long(argc,argv,"hvufsc:",long_opts,NULL);
159    if (c == EOF)
160      break;
161    switch(c) {
162    case ':':
163    case '?':
164      print_short_help();
165      exit(1);
166    case 'h':
167      print_long_help();
168      exit(0);
169    case 'v':
170      print_version();
171      exit(0);
172    case 'c':
173      config_file_name = strdup(optarg);
174      break;
175    case 's':
176      do_sets = 1;
177      break;
178    case 'f':
179      fahrenheit = 1;
180      break;
181    case 'u':
182      do_unknown = 1;
183      break;
184    default:
185      fprintf(stderr,"Internal error while parsing options!\n");
186      exit(1);
187    }
188  }
189
190  if (optind == argc) {
191    chips[0].prefix = SENSORS_CHIP_NAME_PREFIX_ANY;
192    chips[0].bus = SENSORS_CHIP_NAME_BUS_ANY;
193    chips[0].addr = SENSORS_CHIP_NAME_ADDR_ANY;
194    chips_count = 1;
195  } else 
196    for(i = optind; i < argc; i++) 
197      if ((res = sensors_parse_chip_name(argv[i],chips+chips_count))) {
198        fprintf(stderr,"Parse error in chip name `%s'\n",argv[i]);
199        print_short_help();
200        exit(1);
201      } else if (++chips_count == CHIPS_MAX) {
202        fprintf(stderr,"Too many chips on command line!\n");
203        exit(1);
204      }
205
206
207  open_config_file();
208
209  if ((res = sensors_init(config_file))) {
210    fprintf(stderr,"%s\n",sensors_strerror(res));
211    if (res == -SENSORS_ERR_PROC)
212      fprintf(stderr,
213              "/proc/sys/dev/sensors/chips or /proc/bus/i2c unreadable;\n"
214              "Make sure you have done 'modprobe i2c-proc'!\n");
215    exit(1);
216  }
217
218  if(do_the_real_work()) {
219    exit(0);
220  } else {
221    if(chips[0].prefix == SENSORS_CHIP_NAME_PREFIX_ANY)
222            fprintf(stderr,"No sensors found!\n");
223    else
224            fprintf(stderr,"Specified sensor(s) not found!\n");
225    exit(1);
226  }
227}
228
229/* returns number of chips found */
230int do_the_real_work(void)
231{
232  const sensors_chip_name *chip;
233  int chip_nr,i;
234  int cnt = 0;
235
236  for (chip_nr = 0; (chip = sensors_get_detected_chips(&chip_nr));)
237    for(i = 0; i < chips_count; i++)
238      if (sensors_match_chip(*chip,chips[i])) {
239        if(do_sets)
240          do_a_set(*chip);
241        else
242          do_a_print(*chip);
243        i = chips_count;
244        cnt++;
245      }
246   return(cnt);
247}
248
249void do_a_set(sensors_chip_name name)
250{
251  int res;
252  if ((res = sensors_do_chip_sets(name))) {
253    if (res == -SENSORS_ERR_PROC) {
254      fprintf(stderr,"%s: %s for writing;\n",sprintf_chip_name(name),
255              sensors_strerror(res));
256      fprintf(stderr,"Run as root?\n");
257    } else {
258      fprintf(stderr,"%s: %s\n",sprintf_chip_name(name),
259              sensors_strerror(res));
260    }
261  }
262}
263
264const char *sprintf_chip_name(sensors_chip_name name)
265{
266  #define BUF_SIZE 200
267  static char buf[BUF_SIZE];
268
269  if (name.bus == SENSORS_CHIP_NAME_BUS_ISA)
270    snprintf(buf,BUF_SIZE,"%s-isa-%04x",name.prefix,name.addr);
271  else
272    snprintf(buf,BUF_SIZE,"%s-i2c-%d-%02x",name.prefix,name.bus,name.addr);
273  return buf;
274}
275
276void do_a_print(sensors_chip_name name)
277{
278  const char *algo,*adap;
279
280  printf("%s\n",sprintf_chip_name(name));
281  adap = sensors_get_adapter_name(name.bus);
282  if (adap)
283    printf("Adapter: %s\n",adap);
284  algo = sensors_get_algorithm_name(name.bus);
285  if (algo)
286    printf("Algorithm: %s\n",algo);
287  if (!algo || !adap)
288    printf(" ERROR: Can't get adapter or algorithm?!?\n");
289  if (do_unknown)
290    print_unknown_chip(&name);
291  else if (!strcmp(name.prefix,"ds1621"))
292    print_ds1621(&name);
293  else if (!strcmp(name.prefix,"lm75"))
294    print_lm75(&name);
295  else if (!strcmp(name.prefix,"adm1021") || !strcmp(name.prefix,"max1617") ||
296           !strcmp(name.prefix,"max1617a") || !strcmp(name.prefix, "thmc10") ||
297           !strcmp(name.prefix,"lm84") || !strcmp(name.prefix, "gl523") ||
298           !strcmp(name.prefix, "adm1023"))
299    print_adm1021(&name);
300  else if (!strcmp(name.prefix,"adm9240") ||
301           !strcmp(name.prefix,"ds1780") ||
302           !strcmp(name.prefix,"lm81"))
303    print_adm9240(&name);
304  else if (!strcmp(name.prefix,"lm78") || !strcmp(name.prefix,"lm78-j") ||
305           !strcmp(name.prefix,"lm79"))
306    print_lm78(&name);
307  else if (!strcmp(name.prefix,"mtp008"))
308    print_mtp008(&name);
309  else if (!strcmp(name.prefix,"sis5595"))
310    print_sis5595(&name);
311  else if (!strcmp(name.prefix,"via686a"))
312    print_via686a(&name);
313  else if (!strcmp(name.prefix,"lm80"))
314    print_lm80(&name);
315  else if (!strcmp(name.prefix,"lm87"))
316    print_lm87(&name);
317  else if (!strcmp(name.prefix,"gl518sm"))
318    print_gl518(&name);
319  else if (!strcmp(name.prefix,"adm1025"))
320    print_adm1025(&name);
321  else if (!strcmp(name.prefix,"adm1024"))
322    print_adm1024(&name);
323  else if ((!strcmp(name.prefix,"w83781d")) ||
324           (!strcmp(name.prefix,"w83782d")) ||
325           (!strcmp(name.prefix,"w83783s")) ||
326           (!strcmp(name.prefix,"w83627hf")) ||
327           (!strcmp(name.prefix,"w83697hf")) ||
328           (!strcmp(name.prefix,"as99127f")))
329    print_w83781d(&name);
330  else if (!strncmp(name.prefix,"maxilife-", 9))
331    print_maxilife(&name);
332  else if (!strncmp(name.prefix,"it87", 4))
333    print_it87(&name);
334  else if (!strcmp(name.prefix,"ddcmon"))
335    print_ddcmon(&name);
336  else if (!strcmp(name.prefix,"eeprom"))
337    print_eeprom(&name);
338  else if (!strcmp(name.prefix,"fscpos"))
339    print_fscpos(&name);
340  else if (!strcmp(name.prefix,"fscscy"))
341    print_fscscy(&name);
342  else if (!strcmp(name.prefix,"pcf8591"))
343    print_pcf8591(&name);
344  else
345    print_unknown_chip(&name);
346  printf("\n");
347}
Note: See TracBrowser for help on using the browser.