Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Has anyone been able to use libsensors properly?

Long story short I am trying to write an application that can check cpu temperatures. Using the libsensors(3) man pages I've been able to at least get the libsensors_version number. As of now, here is my code:

#include <sensors/sensors.h>
#include "SensorData.h"
#include <string>
#include <sstream>


using namespace std;

SensorData::SensorData()
{
   sensors_init(NULL);
}

SensorData::~SensorData()
{
    sensors_cleanup();
}

string SensorData::GetVersion()
{
    ostringstream Converter;
    Converter<<"Version: "<<libsensors_version;
    return Converter.str();
}

void SensorData::FetchTemp()
{
    //sensors_get_value()
}

With the man pages I know that sensors_get_value expects

const sensors_chip_name *name
int subfeat_nr
double *value 

to be passed to it. The problem is I have no idea what those are exactly. Just about every function in the documentation has this problem. They all expect vague things I don't know how to supply.

So here is the bulk of the question: Does anyone have any working examples of this library I could look at? Or at the very least does anyone know how to give these functions the values they need?

EDIT:

Since no one seems to know much about this library, does anyone know of a different way to get temperatures?

like image 519
CountMurphy Avatar asked Dec 19 '11 02:12

CountMurphy


2 Answers

You can find out how to use the API by browsing the source code. The code for the sensors program isn't too complex to follow.

To get you started, here's a quick function that:

  • enumerates all the chips
  • enumerates all their features
  • prints the values of their readable subfeatures

You can just add it to your existing skeleton class as-is.

(This code is for demo purposes only, not tested thoroughly at all.)

void SensorData::FetchTemp()
{
    sensors_chip_name const * cn;
    int c = 0;
    while ((cn = sensors_get_detected_chips(0, &c)) != 0) {
        std::cout << "Chip: " << cn->prefix << "/" << cn->path << std::endl;

        sensors_feature const *feat;
        int f = 0;

        while ((feat = sensors_get_features(cn, &f)) != 0) {
            std::cout << f << ": " << feat->name << std::endl;

            sensors_subfeature const *subf;
            int s = 0;

            while ((subf = sensors_get_all_subfeatures(cn, feat, &s)) != 0) {
                std::cout << f << ":" << s << ":" << subf->name
                          << "/" << subf->number << " = ";
                double val;
                if (subf->flags & SENSORS_MODE_R) {
                    int rc = sensors_get_value(cn, subf->number, &val);
                    if (rc < 0) {
                        std::cout << "err: " << rc;
                    } else {
                        std::cout << val;
                    }
                }
                std::cout << std::endl;
            }
        }
    }
}
like image 75
Mat Avatar answered Sep 22 '22 21:09

Mat


The Gnome panel Sensors applet works with libsensors (and other backends); the full sources are available from Sourceforge, here: http://sensors-applet.sourceforge.net/index.php?content=source

… in particular, the libsensors plug-in looks fairly legible… I believe this should be a usable gitweb link straight to that code: http://sensors-applet.git.sourceforge.net/git/gitweb.cgi?p=sensors-applet/sensors-applet;a=blob;f=plugins/libsensors/libsensors-plugin.c;h=960c19f4c36902dee4e20b690f2e3dfe6c715279;hb=HEAD

like image 23
BRPocock Avatar answered Sep 25 '22 21:09

BRPocock