"Fossies" - the Fresh Open Source Software Archive

Member "procmeter3-3.6+svn387/modules/libsensors-fan.c" (24 Dec 2010, 7041 Bytes) of package /linux/misc/procmeter3-3.6+svn387.tgz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "libsensors-fan.c" see the Fossies "Dox" file reference documentation.

    1 /***************************************
    2   $Header: /home/amb/CVS/procmeter3/modules/libsensors-fan.c,v 1.1 2010-02-20 13:33:24 amb Exp $
    3 
    4   ProcMeter - A system monitoring program for Linux - Version 3.5c.
    5 
    6   lm_sensors module source file.
    7   ******************/ /******************
    8   Written by Mike T. Liang
    9 
   10   This file Copyright 2009, 2010 Mike T. Liang
   11   It may be distributed under the GNU Public License, version 2, or
   12   any higher version.  See section COPYING of the GNU Public license
   13   for conditions under which this file may be redistributed.
   14   ***************************************/
   15 
   16 #ifndef INCLUDED
   17 #define FANSPEED
   18 const char *FILE__=__FILE__;
   19 #endif
   20 
   21 #include <stdio.h>
   22 #include <stdlib.h>
   23 #include <string.h>
   24 #include <errno.h>
   25 #include <sensors/sensors.h>
   26 #include <sensors/error.h>
   27 
   28 #include "procmeter.h"
   29 
   30 #if !defined(SENSORS_API_VERSION) || (SENSORS_API_VERSION & 0xf00) != 0x400
   31 #error "This module requires libsensors version 3"
   32 #else
   33 
   34 /* The interface information.  */
   35 
   36 /*+ The template output +*/
   37 ProcMeterOutput template_output=
   38 {
   39  /* char  name[];          */ "unknown",
   40 #if defined(FANSPEED)
   41  /* char *description;     */ "Fan speed. [From %s %s]",
   42 #elif defined(TEMPERATURE)
   43  /* char *description;     */ "Temperature. [From %s %s]",
   44 #elif defined(VOLTAGE)
   45  /* char *description;     */ "Voltage level. [From %s %s]",
   46 #else
   47 #error Undefined module.
   48 #endif
   49  /* char  type;            */ PROCMETER_GRAPH|PROCMETER_TEXT|PROCMETER_BAR,
   50  /* short interval;        */ 1,
   51  /* char  text_value[];    */ "unknown",
   52  /* long  graph_value;     */ 0,
   53 #if defined(FANSPEED)
   54  /* short graph_scale;     */ 1000,
   55  /* char  graph_units[];   */ "(%d RPM)"
   56 #elif defined(TEMPERATURE)
   57  /* short graph_scale;     */ 10,
   58  /* char  graph_units[];   */ "(%d C)"
   59 #elif defined(VOLTAGE)
   60  /* short graph_scale;     */ 1,
   61  /* char  graph_units[];   */ "(%d V)"
   62 #else
   63 #error Undefined module.
   64 #endif
   65 };
   66 
   67 /*+ The outputs. +*/
   68 ProcMeterOutput **outputs=NULL;
   69 
   70 /*+ The module. +*/
   71 ProcMeterModule module=
   72 {
   73 #if defined(FANSPEED)
   74  /* char name[];           */ "FanSpeed",
   75 #elif defined(TEMPERATURE)
   76  /* char name[];           */ "Temperature",
   77 #elif defined(VOLTAGE)
   78  /* char name[];           */ "Voltage",
   79 #else
   80 #error Undefined module.
   81 #endif
   82  /* char *description;     */ "Hardware sensor information from libsensors.",
   83 };
   84 
   85 typedef struct {
   86     char *description;
   87     const sensors_chip_name *chip;
   88     char *label;
   89     int number;
   90 } Sensor;
   91 
   92 int count;
   93 Sensor *sensorv;
   94 ProcMeterOutput *outputv;
   95 
   96 /*++++++++++++++++++++++++++++++++++++++
   97   Load the module.
   98 
   99   ProcMeterModule *Load Returns the module information.
  100   ++++++++++++++++++++++++++++++++++++++*/
  101 
  102 ProcMeterModule *Load(void)
  103 {
  104  return(&module);
  105 }
  106 
  107 
  108 /*++++++++++++++++++++++++++++++++++++++
  109   Initialise the module, creating the outputs as required.
  110 
  111   ProcMeterOutput **Initialise Returns a NULL terminated list of outputs.
  112 
  113   char *options The options string for the module from the .procmeterrc file.
  114   ++++++++++++++++++++++++++++++++++++++*/
  115 
  116 ProcMeterOutput **Initialise(char *options)
  117 {
  118     int error;
  119     int i=0;
  120 
  121     /* Uninitialized libsensors will report no detected chips. */
  122     if (!sensors_get_detected_chips(NULL, &i)) {
  123     FILE *input=NULL;
  124     if (options) {
  125         input=fopen(options, "r");
  126         if (!input) {
  127         fprintf(stderr,"ProcMeter(%s): %s: %s\n", FILE__, options, strerror(errno));
  128         }
  129     }
  130     error=sensors_init(input);
  131     if (input) fclose(input);
  132     if (error) {
  133         fprintf(stderr,"ProcMeter(%s): %s\n", FILE__, sensors_strerror(error));
  134         sensors_cleanup();
  135     }
  136     }
  137 
  138     const sensors_chip_name *chip;
  139     for (count=0, i=0; (chip=sensors_get_detected_chips(NULL, &i));) {
  140 
  141     char name[1024];
  142     error=sensors_snprintf_chip_name(name, sizeof(name), chip);
  143     if (error<0) {
  144         fprintf(stderr,"ProcMeter(%s): %s\n", FILE__, sensors_strerror(error));
  145         strcpy(name, "unknown");
  146     }
  147 
  148     const sensors_feature *feature;
  149     int f=0;
  150     while ((feature=sensors_get_features(chip, &f))) {
  151         const sensors_subfeature *subfeature;
  152 
  153 #if defined(FANSPEED)
  154         if (feature->type!=SENSORS_FEATURE_FAN) continue;
  155         subfeature=sensors_get_subfeature(chip, feature, SENSORS_SUBFEATURE_FAN_INPUT);
  156 
  157 #elif defined(TEMPERATURE)
  158         if (feature->type!=SENSORS_FEATURE_TEMP) continue;
  159         subfeature=sensors_get_subfeature(chip, feature, SENSORS_SUBFEATURE_TEMP_INPUT);
  160 
  161 #elif defined(VOLTAGE)
  162         if (feature->type==SENSORS_FEATURE_IN) {
  163         subfeature=sensors_get_subfeature(chip, feature, SENSORS_SUBFEATURE_IN_INPUT);
  164         } else if (feature->type==SENSORS_FEATURE_VID) {
  165         subfeature=sensors_get_subfeature(chip, feature, SENSORS_SUBFEATURE_VID);
  166         } else {
  167         continue;
  168         }
  169 
  170 #else
  171 #error Undefined module.
  172 #endif
  173 
  174         if (!subfeature) continue;
  175 
  176         char *label=sensors_get_label(chip, feature);
  177         char *description=malloc(strlen(template_output.description)+strlen(name)+strlen(label));
  178         sprintf(description, template_output.description, name, label);
  179 
  180         char *s;
  181         while ((s=strchr(label, ' '))) *s='_';
  182 
  183         if (!count) sensorv=malloc(sizeof(*sensorv));
  184         else sensorv=realloc(sensorv, (count+1)*sizeof(*sensorv));
  185 
  186         sensorv[count].description=description;
  187         sensorv[count].chip=chip;
  188         sensorv[count].label=label;
  189         sensorv[count].number=subfeature->number;
  190         ++count;
  191     }
  192     }
  193 
  194     if (count) outputv=malloc(count*sizeof(*outputv));
  195     outputs=malloc((count+1)*sizeof(*outputs));
  196     outputs[count]=0;
  197 
  198     for (i=0; i<count; ++i) {
  199     outputv[i]=template_output;
  200     snprintf(outputv[i].name, sizeof(outputv[i].name), "%s", sensorv[i].label);
  201     outputv[i].description=sensorv[i].description;
  202     outputs[i]=&outputv[i];
  203     }
  204 
  205     return outputs;
  206 }
  207 
  208 
  209 /*++++++++++++++++++++++++++++++++++++++
  210   Perform an update on one of the statistics.
  211 
  212   int Update Returns 0 if OK, else -1.
  213 
  214   time_t now The current time.
  215 
  216   ProcMeterOutput *output The output that the value is wanted for.
  217   ++++++++++++++++++++++++++++++++++++++*/
  218 
  219 int Update(time_t now,ProcMeterOutput *output)
  220 {
  221     int i=output-outputv;
  222     if ((i<0)||(i>=count)) return -1;
  223 
  224     double value;
  225     int error=sensors_get_value(sensorv[i].chip, sensorv[i].number, &value);
  226     if (error<0) {
  227     fprintf(stderr,"ProcMeter(%s): %s\n", FILE__, sensors_strerror(error));
  228     return -1;
  229     }
  230 
  231 #if defined(FANSPEED)
  232     sprintf(output->text_value, "%.0lf RPM", value);
  233 #elif defined(TEMPERATURE)
  234     sprintf(output->text_value, "%.1lf C", value);
  235 #elif defined(VOLTAGE)
  236     sprintf(output->text_value, "%.3lf V", value);
  237 #else
  238 #error Undefined module.
  239 #endif
  240     output->graph_value=PROCMETER_GRAPH_FLOATING(value/output->graph_scale);
  241 
  242     return 0;
  243 }
  244 
  245 
  246 /*++++++++++++++++++++++++++++++++++++++
  247   Tidy up and prepare to have the module unloaded.
  248   ++++++++++++++++++++++++++++++++++++++*/
  249 
  250 void Unload(void)
  251 {
  252     free(outputs);
  253 
  254     int i;
  255     for (i=0; i<count; ++i) {
  256     free(sensorv[i].description);
  257     free(sensorv[i].label);
  258     }
  259 
  260     if (count) {
  261     free(outputv);
  262     free(sensorv);
  263     }
  264 
  265     /* Multiple modules use libsensors, so don't call sensors_cleanup(). */
  266 }
  267 
  268 #endif