Subject: torsmo acpi temperature patch
To: None <ghent@netbsd.org, tech-pkg@netbsd.org, pkgsrc-users@netbsd.org>
From: haad <haaaad@gmail.com>
List: pkgsrc-users
Date: 08/15/2006 00:37:32
This is a multi-part message in MIME format.
--------------070803080104010801040100
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi

Today I wrote patch against netbsd.c in torsmo package.

This patch enable acpitz sensor optput for configuration parameter acpitemp.

Pleas submit these patch to cvs if it's correct

- --
Adam Hamsik
ICQ 249727910
jabber haad@jabber.org
- --------------------------------------------------------------
There are 10 kinds of people in the world. Those who understand
binary numbers, and those who don't.
				
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.4 (NetBSD)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFE4Pss9Wt2FT7y228RAoSuAJ9HZIbLpPAVdCk+BjAfT++D/rstYgCggxEh
UVsVTL+Hqlnq0TJOh4rK7Qo=
=tkwB
-----END PGP SIGNATURE-----

--------------070803080104010801040100
Content-Type: text/plain;
 name="patch-ab"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="patch-ab"

$NetBSD: patch-ab,v 1.1.1.1 2006/03/29 21:42:55 ghen Exp $

--- netbsd.c.orig	2006-08-14 23:58:58.000000000 +0200
+++ netbsd.c
@@ -9,10 +9,12 @@
 #include <err.h>
 #include <limits.h>
 #include <paths.h>
+#include <ctype.h>
 
 #include <kvm.h>
 #include <nlist.h>
 
+#include <sys/ioctl.h>
 #include <sys/time.h>
 #include <sys/param.h>
 #include <sys/sysctl.h>
@@ -221,6 +223,33 @@ void update_net_stats() 
     }
 }
 
+static char *freq = NULL;
+char * get_freq()
+{
+    size_t val_len;
+    u_int32_t val;
+
+    if (freq == NULL) {
+        freq = malloc(10);
+        if (freq == NULL) {
+            return NULL;
+        } else {
+            freq[0] = '\0';
+        }
+    }
+
+    /* Laptops with enhanced speed step. */
+    if (sysctlbyname("machdep.est.frequency.current",
+                     NULL, &val_len, NULL, 0) == 0) {
+        sysctlbyname("machdep.est.frequency.current", &val, &val_len, NULL, 0);
+        sprintf(freq, "%d", val);
+    } else {
+        /* XXX: Parse out the clockspeed from machdep.cpu_brand */
+    }
+
+    return freq;
+}
+
 void update_total_processes() 
 {
     /* It's easier to use kvm here than sysctl */
@@ -302,6 +331,7 @@ void update_cpu_usage() 
 }
 
 double get_i2c_info(int fd, int div) {
+    (void) fd; (void) div;
     return -1;
 }
 
@@ -315,24 +345,137 @@ void update_load_average() {
 }
 
 double get_acpi_temperature(int fd) {
-    return -1;
-}
-
-void get_battery_stuff(char *buf, unsigned int n, const char *bat) {
+  
+  envsys_basic_info_t info;    /* sensor misc. info */
+  struct envsys_tre_data sen_data;
+  double temp;
+
+  
+  /*
+   *Ineed numbr of acpitz sensor in envsys api
+   */
+  
+  /* iterate through available sensors, until the first invalid */
+  for(info.sensor=0; ; info.sensor++) {
+
+	/* stop if we can't ioctl() */
+	if (ioctl(fd, ENVSYS_GTREINFO, &info) < 0) break;
+	/* stop if that sensor is not valid */
+	if (!(info.validflags & ENVSYS_FVALID)) break;
+
+	if (info.units ==  ENVSYS_STEMP)
+	  break;
+  }
+
+  /*get data from sensor*/
+  sen_data.sensor=info.sensor;
+  if (ioctl(fd, ENVSYS_GTREDATA, &sen_data) == -1)
+	err(EXIT_FAILURE, "Can't get sensor data for sensor acpitz");
+
+  /*test validity of returned data*/
+  if (!(sen_data.validflags & (ENVSYS_FVALID|ENVSYS_FCURVALID))) return(-1);
+  
+  /*count temperature*/
+  temp =((sen_data.cur.data_us / 1000000.0)-273.15);
+  
+  return temp;
 }
 
 int open_i2c_sensor(const char *dev, const char *type, int n, int *div)
 {
+    (void) dev; (void) type; (void) n; (void) div;
     return -1;
 }
 
 int open_acpi_temperature(const char *name) {
-    return -1;
+    (void) name;
+	int fd;
+
+	/*Open /dev/sysmon device to connect with envsys API*/
+	if (name == NULL)
+	  name = _PATH_SYSMON;
+	
+	if ((fd = open(name, O_RDONLY)) == -1)
+	  err(1, "unable to open %s", name);
+
+	return fd;
 }
 
+static char acpi_ac_str[64] = "N/A";
 char * get_acpi_ac_adapter(void)
 {
-    return "N/A";
+    /* get_battery_stuff() actually populates this for us. */
+    return acpi_ac_str;
+}
+
+static char last_battery_str[64];
+static double last_battery_time;
+
+void get_battery_stuff(char *buf, unsigned int n, const char *bat) {
+    FILE *f;
+    char *foo;
+    char b[4096];
+    char b_name[32];
+    int bat_num;
+    int found_acpibat = 0;
+    int found_acpiacad = 0;
+    float bat_charge = 0.0;
+
+    /* Don't update battery too often. */
+    if (current_update_time - last_battery_time < 29.5) {
+        snprintf(buf, n, "%s", last_battery_str);
+        return;
+    }
+    last_battery_time = current_update_time;
+
+    sscanf(bat, "BAT%d", &bat_num);
+    sprintf(b_name, "acpibat%d", bat_num);
+
+    /*
+     * Using the envsys API is like pulling teeth without anesthetic.
+     * so just popen envstat and parse the output instead.
+     */
+    f = popen("/usr/sbin/envstat -r", "r");
+    if (f != NULL) {
+        while(!feof(f)) {
+            fgets(b, 4096, f);
+
+            /* AC adapter state. */
+            if (strstr(b, "acpiacad0")) {
+                if (strstr(b, "disconnected")) {
+                    sprintf(acpi_ac_str, "disconnected");
+                } else {
+                    sprintf(acpi_ac_str, "connected");
+                }
+                found_acpiacad = 1;
+            }
+
+            if (bat && strstr(b, b_name)) {
+                if (strstr(b, "charge:")) {
+                    foo = strstr(b, "%)");
+                    while (*foo != ' ' && *foo != '(') {
+                        foo--;
+                    }
+                    foo = foo + 1;
+                    sscanf(foo, "%f", &bat_charge);
+                    found_acpibat = 1;
+                }
+            }
+        }
+        pclose(f);
+    }
+
+    if (found_acpibat) {
+        snprintf(last_battery_str, 64, "%.2f%%", bat_charge);
+        snprintf(buf, n, "%s", last_battery_str);
+    } else {
+        /* XXX: If that failed, try to use APM. */
+    }
+
+    if (!found_acpiacad) {
+        sprintf(acpi_ac_str, "N/A");
+    }
+
 }
 
 char* get_acpi_fan() {

--------------070803080104010801040100--