LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 04-01-2017, 12:33 PM   #1
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: FreeBSD/Slackware-14.2+/ Win10
Posts: 9,516

Rep: Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040
accessing lm_sensors header files to get the cpu temp using C


Hopefully all I'll just need some guidance in the logical line of thought on a procedural order to get the cpu temp using lm_sensors function calls.

I am still trying to formulate this question in my head as I write this.

I already have some working code using wmfu dockapp that is written to use ACPI. I want to change that to use lm_sensors to get the cpu temp because ACPI is not returning a temp for my cpu cores. I got a feeler out on ACPI and core temp in here as well . but i am using this as a fail safe.

in wmfu /linux/system.c these two functions call for temps
Code:
int
system_get_max_temp (SENSOR *list, int *temp, int *crit)
{
    SENSOR *s;
    char buf[BUFSIZ];

    int tmp, max = -1;

    *crit = 0;

    list_foreach (s, list) {
                      #needs to be changed to lm_sensors 
	if (s->type == S_ACPI_THERMAL_ZONE &&
	    sensors_read_line(s->filename, BUFSIZ, buf) > 0 &&
	    sscanf(buf, "temperature: %d", &tmp) == 1) {
	    if (100 * tmp > 90 * s->idata) {
		*crit = 1;
	    }
	    if (max < tmp) {
		max = tmp;
	    }
	}
	if (s->type == S_HWMON_CORETEMP &&
	    sensors_read_line(s->filename, BUFSIZ, buf) > 0 &&
	    sscanf(buf, "%d", &tmp) == 1) {
	    if (100 * tmp > 90 * s->idata) {
		*crit = 1;
	    }
	    if (max * 1000 < tmp) {
		max = tmp / 1000;
	    }
	}
    }

    if (max < 0) {
	return 0;
    }

    *temp = max;
    return 1;
}


int
system_get_temperature (SENSOR *list, int num, TEMPSTAT temp[])
{
    SENSOR *s;
    char buf[BUFSIZ];
    int i = 0;

    list_foreach (s, list) {
	if (i >= num) {
	    break;
	}
	if (s->type == S_ACPI_THERMAL_ZONE &&
	    sensors_read_line(s->filename, BUFSIZ, buf) > 0 &&
	    sscanf(buf, "temperature: %d", &temp[i].temp) == 1) {
	    temp[i].max = s->idata;
	    strncpy(temp[i].name, s->name, 7);
	    temp[i].name[7] = '\0';
	    i++;
	}
	if (s->type == S_HWMON_CORETEMP &&
	    sensors_read_line(s->filename, BUFSIZ, buf) > 0 &&
	    sscanf(buf, "%d", &temp[i].temp) == 1) {
	    temp[i].temp /= 1000;
	    temp[i].max = s->idata / 1000;
	    strncpy(temp[i].name, s->name, 7);
	    temp[i].name[7] = '\0';
	    i++;
	}
	if (s->type == S_NVIDIA_SETTINGS_GPUCORETEMP &&
	    sensors_nvidia(s->name, &temp[i].temp) == 0) {
	    temp[i].max = s->idata;
	    strncpy(temp[i].name, s->name, 7);
	    temp[i].name[7] = '\0';
	    i++;
	}
    }

    return i;
}
In the wmfu /linux/sensors.c there is a switch that calls for S_ACPI_THERMAL_ZONE
Code:
 switch (type) {
	case S_ACPI_THERMAL_ZONE:
	    p = malloc(strlen(filename) + 32);
	    if (p == NULL) {
		break;
	    }
	    strcat(strcpy(p, filename), "/trip_points");
	    s->idata = 100;
	    if (sensors_read_line(p, sizeof(buf), buf) > 0) {
		sscanf(buf, "%*s %*s %d", &s->idata);
	    }
	    s->filename = strcat(strcpy(p, filename), "/temperature");
	    s->name = strdup(name);
	    if (s->name == NULL) {
		free(p);
		break;
	    }
	    list_append(list, s);
	    return 0;

I think all I'd need to declare is sensors.h from lm_sensors to get the definitions of the functions I'll be needing to call. then just change the function calls and figure out the rest.

I know this is a local header call
Code:
#include "../list.h"
#include "../sensors.h"
#include "../system.h"
in github lm_sensors I just found this.
https://github.com/groeck/lm-sensors...ensors-API.txt

Code:
0x410	lm-sensors 3.1.0
* Added bus type "acpi":
  #define SENSORS_BUS_TYPE_ACPI
* Added support for instantaneous power sensors
  enum sensors_subfeature_type SENSORS_SUBFEATURE_POWER_INPUT
  enum sensors_subfeature_type SENSORS_SUBFEATURE_POWER_INPUT_HIGHEST
enum sensors_subfeature_type SENSORS_SUBFEATURE_POWER_INPUT_LOWEST
acpi support???

can someone lead me to a better documents to read so I can try and lead myself to the right function calls and whatever these are called when variables are written in all CAPS.



kind of looking for something like this .. back when I was using imlib2 to write some C to access it for a fork of hsetroot I updated adding more features to it for my benefit. (and others if they ever downloaded it off Sourceforge. )

https://docs.enlightenment.org/api/i...imlib2_8c.html

I have been finding some stuff on this libsensors is that C++ only or C too.


I just want to add C code to get the cpu temps using lm_sensors.

it cannot be that hard.

yeah I am rusty in programming. But I know I can get this to work eventually. I just need a push in the right direction for now.

Last edited by BW-userx; 04-01-2017 at 12:45 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
lm_sensors cpu temp v.slightly different to MOBO temp on Intel D865GLC sc_3007 Linux - Hardware 5 11-13-2009 01:17 PM
CPU Temp Pedroski Linux - Hardware 1 12-02-2007 12:04 PM
CPU Temp Pumalite Linux - Software 3 05-09-2007 11:12 PM
cpu temp lord-fu Slackware 8 03-31-2006 09:17 PM
How to get the cpu temp? jon_k Linux - Software 1 10-26-2004 02:13 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 03:26 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration