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

Revision 1088, 9.5 KB (checked in by mds, 13 years ago)

really fix sensors so that it recognizes all maxilifes.

didn't work last time.

  • 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    if (res == SENSORS_ERR_PROC)
211      fprintf(stderr,
212              "/proc/sys/dev/sensors/chips or /proc/bus/i2c unreadable:\n"
213              "Make sure you have inserted modules sensors.o and i2c-proc.o!");
214    else
215      fprintf(stderr,"%s\n",sensors_strerror(res));
216    exit(1);
217  }
218
219  if(do_the_real_work()) {
220    exit(0);
221  } else {
222    if(chips[0].prefix == SENSORS_CHIP_NAME_PREFIX_ANY)
223            fprintf(stderr,"No sensors found!\n");
224    else
225            fprintf(stderr,"Specified sensor(s) not found!\n");
226    exit(1);
227  }
228}
229
230/* returns number of chips found */
231int do_the_real_work(void)
232{
233  const sensors_chip_name *chip;
234  int chip_nr,i;
235  int cnt = 0;
236
237  for (chip_nr = 0; (chip = sensors_get_detected_chips(&chip_nr));)
238    for(i = 0; i < chips_count; i++)
239      if (sensors_match_chip(*chip,chips[i])) {
240        if(do_sets)
241          do_a_set(*chip);
242        else
243          do_a_print(*chip);
244        i = chips_count;
245        cnt++;
246      }
247   return(cnt);
248}
249
250void do_a_set(sensors_chip_name name)
251{
252  int res;
253  if ((res = sensors_do_chip_sets(name))) 
254    fprintf(stderr,"%s: %s\n",sprintf_chip_name(name),sensors_strerror(res));
255}
256
257const char *sprintf_chip_name(sensors_chip_name name)
258{
259  #define BUF_SIZE 200
260  static char buf[BUF_SIZE];
261
262  if (name.bus == SENSORS_CHIP_NAME_BUS_ISA)
263    snprintf(buf,BUF_SIZE,"%s-isa-%04x",name.prefix,name.addr);
264  else
265    snprintf(buf,BUF_SIZE,"%s-i2c-%d-%02x",name.prefix,name.bus,name.addr);
266  return buf;
267}
268
269void do_a_print(sensors_chip_name name)
270{
271  const char *algo,*adap;
272
273  printf("%s\n",sprintf_chip_name(name));
274  adap = sensors_get_adapter_name(name.bus);
275  if (adap)
276    printf("Adapter: %s\n",adap);
277  algo = sensors_get_algorithm_name(name.bus);
278  if (algo)
279    printf("Algorithm: %s\n",algo);
280  if (!algo || !adap)
281    printf(" ERROR: Can't get adapter or algorithm?!?\n");
282  if (do_unknown)
283    print_unknown_chip(&name);
284  else if (!strcmp(name.prefix,"ds1621"))
285    print_ds1621(&name);
286  else if (!strcmp(name.prefix,"lm75"))
287    print_lm75(&name);
288  else if (!strcmp(name.prefix,"adm1021") || !strcmp(name.prefix,"max1617") ||
289           !strcmp(name.prefix,"max1617a") || !strcmp(name.prefix, "thmc10") ||
290           !strcmp(name.prefix,"lm84") || !strcmp(name.prefix, "gl523"))
291    print_adm1021(&name);
292  else if (!strcmp(name.prefix,"adm9240") ||
293           !strcmp(name.prefix,"ds1780") ||
294           !strcmp(name.prefix,"lm81"))
295    print_adm9240(&name);
296  else if (!strcmp(name.prefix,"lm78") || !strcmp(name.prefix,"lm78-j") ||
297           !strcmp(name.prefix,"lm79"))
298    print_lm78(&name);
299  else if (!strcmp(name.prefix,"mtp008"))
300    print_mtp008(&name);
301  else if (!strcmp(name.prefix,"sis5595"))
302    print_sis5595(&name);
303  else if (!strcmp(name.prefix,"via686a"))
304    print_via686a(&name);
305  else if (!strcmp(name.prefix,"lm80"))
306    print_lm80(&name);
307  else if (!strcmp(name.prefix,"lm87"))
308    print_lm87(&name);
309  else if (!strcmp(name.prefix,"gl518sm"))
310    print_gl518(&name);
311  else if (!strcmp(name.prefix,"adm1025"))
312    print_adm1025(&name);
313  else if ((!strcmp(name.prefix,"w83781d")) ||
314           (!strcmp(name.prefix,"w83782d")) ||
315           (!strcmp(name.prefix,"w83783s")) ||
316           (!strcmp(name.prefix,"w83627hf")) ||
317           (!strcmp(name.prefix,"as99127f")))
318    print_w83781d(&name);
319  else if (!strncmp(name.prefix,"maxilife-", 9))
320    print_maxilife(&name);
321  else if (!strcmp(name.prefix,"ddcmon"))
322    print_ddcmon(&name);
323  else if (!strcmp(name.prefix,"eeprom"))
324    print_eeprom(&name);
325  else
326    print_unknown_chip(&name);
327  printf("\n");
328}
Note: See TracBrowser for help on using the browser.