c++linuxtemperature

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?


Solution

  • 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:

    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;
                }
            }
        }
    }