]> git.webhop.me Git - lcd4linux.git/commitdiff
[lcd4linux @ 2000-03-07 11:01:34 by reinelt]
authorreinelt <reinelt@3ae390bd-cb1e-0410-b409-cd5a39f66f1f>
Tue, 7 Mar 2000 11:01:34 +0000 (11:01 +0000)
committerreinelt <reinelt@3ae390bd-cb1e-0410-b409-cd5a39f66f1f>
Tue, 7 Mar 2000 11:01:34 +0000 (11:01 +0000)
system.c cleanup

git-svn-id: https://ssl.bulix.org/svn/lcd4linux/trunk@5 3ae390bd-cb1e-0410-b409-cd5a39f66f1f

Makefile
config.c
isdn.c
isdn.h
lcd4linux.conf.sample [new file with mode: 0644]
lcd4linux.h
system.c
system.h

index 218e2966e94c4dfde102b2e704eff470c51dcee9..46a87cdec01b5b91bee710c87485e7e29eafe3b7 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -2,6 +2,9 @@ CC=gcc -m486 -g -Wall -I. -DVERSION=\"V0.5\"
 
 all: lcd4linux
 
+system: system.c system.h config.c config.h filter.c filter.h
+       ${CC} -DSTANDALONE -lm -o system system.c config.c filter.c
+
 #lcd4linux: lcd4linux.c config.c lcd2041.c system.c isdn.c filter.c Makefile
 #      ${CC} -lm -o lcd4linux lcd4linux.c config.c lcd2041.c system.c isdn.c filter.c
 
index 0f30f1139b34a3a90e120350336920013e4e9ea6..6841c205888439b9378ea796d1ff9114f417cd62 100644 (file)
--- a/config.c
+++ b/config.c
@@ -1,4 +1,4 @@
-/* $Id: config.c,v 1.2 2000/03/06 06:04:06 reinelt Exp $
+/* $Id: config.c,v 1.3 2000/03/07 11:01:34 reinelt Exp $
  *
  * config file stuff
  *
  *
  *
  * $Log: config.c,v $
+ * Revision 1.3  2000/03/07 11:01:34  reinelt
+ *
+ * system.c cleanup
+ *
  * Revision 1.2  2000/03/06 06:04:06  reinelt
  *
  * minor cleanups
@@ -149,7 +153,7 @@ int cfg_read (char *file)
       *s='\0';
       p++;
     }
-    set_cfg (line, p);
+    cfg_set (line, p);
   }
   fclose (stream);
   return 0;
diff --git a/isdn.c b/isdn.c
index eaecbc359bf8c8962460a21949e72c3f5dc11677..e5e7b68232b9f1c476bcc0b55d47010b7af69dcd 100644 (file)
--- a/isdn.c
+++ b/isdn.c
@@ -1,4 +1,4 @@
-/* $Id: isdn.c,v 1.2 2000/03/06 06:04:06 reinelt Exp $
+/* $Id: isdn.c,v 1.3 2000/03/07 11:01:34 reinelt Exp $
  *
  * ISDN specific functions
  *
  *
  *
  * $Log: isdn.c,v $
+ * Revision 1.3  2000/03/07 11:01:34  reinelt
+ *
+ * system.c cleanup
+ *
  * Revision 1.2  2000/03/06 06:04:06  reinelt
  *
  * minor cleanups
@@ -30,8 +34,9 @@
 /* 
  * exported functions:
  *
- * Isdn (int *rx, int *tx)
- *   returns all channel's USAGE or'ed together
+ * Isdn (int *usage, int *rx, int *tx)
+ *   returns 0 if ok, -1 if error
+ *   sets *usage to all channels USAGE or'ed together
  *   sets received/transmitted bytes in *rx, *tx
  *
  */
@@ -93,33 +98,33 @@ static int Usage (void)
   for (i=0; i<ISDN_MAX_CHANNELS; i++) {
     usage|=strtol(p, &p, 10);
   }
-
   return usage;
 }
 
-int Isdn (int *rx, int *tx)
+int Isdn (int *usage, int *rx, int *tx)
 {
   static int fd=-2;
   CPS cps[ISDN_MAX_CHANNELS];
   double cps_i, cps_o;
   int i;
 
+  *usage=0;
   *rx=0;
   *tx=0;
 
-  if (fd==-1) return 0;
+  if (fd==-1) return -1;
   
   if (fd==-2) {
     fd = open("/dev/isdninfo", O_RDONLY | O_NDELAY);
     if (fd==-1) {
       perror ("open(/dev/isdninfo) failed");
-      return 0;
+      return -1;
     }
   }
   if (ioctl(fd, IIOCGETCPS, &cps)) {
     perror("ioctl(IIOCGETCPS) failed");
     fd=-1;
-    return 0;
+    return -1;
   }
   cps_i=0;
   cps_o=0;
@@ -130,7 +135,8 @@ int Isdn (int *rx, int *tx)
 
   *rx=(int)smooth("isdn_rx", 1000, cps_i);
   *tx=(int)smooth("isdn_tx", 1000, cps_o);
+  *usage=Usage();
 
-  return Usage();
+  return 0;
 }
 
diff --git a/isdn.h b/isdn.h
index 2c9831beb2523cceb08f785d7857aebca80e070e..93416dd7b4451a20d767268236a66b444940af64 100644 (file)
--- a/isdn.h
+++ b/isdn.h
@@ -1,4 +1,4 @@
-/* $Id: isdn.h,v 1.2 2000/03/06 06:04:06 reinelt Exp $
+/* $Id: isdn.h,v 1.3 2000/03/07 11:01:34 reinelt Exp $
  *
  * ISDN specific functions
  *
  *
  *
  * $Log: isdn.h,v $
+ * Revision 1.3  2000/03/07 11:01:34  reinelt
+ *
+ * system.c cleanup
+ *
  * Revision 1.2  2000/03/06 06:04:06  reinelt
  *
  * minor cleanups
@@ -30,6 +34,6 @@
 #ifndef _ISDN_H_
 #define _ISDN_H_
 
-int Isdn (int *rx, int *tx);
+int Isdn (int *usage, int *rx, int *tx);
 
 #endif
diff --git a/lcd4linux.conf.sample b/lcd4linux.conf.sample
new file mode 100644 (file)
index 0000000..956119e
--- /dev/null
@@ -0,0 +1,17 @@
+#Row1 "%p%t\337$h10rt"
+Row1 "Busy %b%% $h5rb$h5rl"
+Row2 "Disk%D%dm $H5rdr$H5rdw"
+Row3 "Netz%N%nm $H5rnr$H5rnw"
+Row4 "ISDN%I%im $h5rio$h5rii"
+
+Port /dev/ttyS2
+Fifo /var/run/lcd4linux
+Tick 100
+Tack 400
+Tau 500
+Contrast 160
+Overload 2.0
+
+Sensor1 /proc/sys/dev/sensors/w83781d-isa-0290/temp2
+Sensor1_min 30
+Sensor1_max 50
index ec9da8cddc84f98cd025a51473364a6d4865e878..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 100644 (file)
@@ -1 +0,0 @@
-extern char *sensor;
index 228913964b9b128c8120e96c143326cf872f5875..109992e4be362e6f1028a0961a52a9b107459688 100644 (file)
--- a/system.c
+++ b/system.c
@@ -1,4 +1,4 @@
-/* $Id: system.c,v 1.2 2000/03/06 06:04:06 reinelt Exp $
+/* $Id: system.c,v 1.3 2000/03/07 11:01:34 reinelt Exp $
  *
  * system status retreivement
  *
  *
  *
  * $Log: system.c,v $
+ * Revision 1.3  2000/03/07 11:01:34  reinelt
+ *
+ * system.c cleanup
+ *
  * Revision 1.2  2000/03/06 06:04:06  reinelt
  *
  * minor cleanups
  * int   Memory (void);
  *   returns main memory (Megabytes)
  *
- * double Load (void);
- *   returns load average
+ * int Load (double *load1, double *load2, double *load3)
+ *   sets load average during thwe last 1, 5 and 15 minutes
+ *   retuns 0 if ok, -1 on error
  *
- * double Busy (void);
- *   returns CPU utilization
+ * int Busy (double *user, double *nice, double *system, double *idle)
+ *   sets percentage of CPU time spent in user processes, nice'd processes
+ *   system calls and idle state
+ *   returns 0 if ok, -1 on error
  *
  * int Disk (int *r, int *w);
- *   returns disk utilization
+ *   sets number of read and write accesses to all disks 
+ *   returns 0 if ok, -1 on error
  *
- * int Net (int *r, int *w);
- *   returns network utilization
+ * int Net (int *rx, int *tx);
+ *   sets number of packets received and transmitted
+ *   returns 0 if ok, -1 on error
  *
- * double Temperature (void);
- *   returns temperature 
- *   a sensor must be specified with a 'temperature'-line  
- *   in the config file
+ * int Sensor (int index, double *val, double *min, double *max)
+ *   sets the current value of the index'th sensor and
+ *   the minimum and maximum values from the config file
+ *   returns 0 if ok, -1 on error
  *
  */
 
@@ -74,8 +83,8 @@
 #include <asm/param.h>
 
 #include "system.h"
+#include "config.h"
 #include "filter.h"
-#include "lcd4linux.h"
 
 char *System(void)
 {
@@ -144,83 +153,105 @@ int Memory(void)
 }
 
 
-double Load (void)
+int Load (double *load1, double *load2, double *load3)
 {
   static int fd=-2;
   char buffer[16];
-  static double value=0;
+  static double val1=0;
+  static double val2=0;
+  static double val3=0;
   static time_t now=0;
 
-  if (fd==-1) return 0;
+  *load1=val1;
+  *load2=val2;
+  *load3=val3;
+
+  if (fd==-1) return -1;
   
-  if (time(NULL)==now) return value;
+  if (time(NULL)==now) return 0;
   time(&now);
 
   if (fd==-2) {
     fd=open("/proc/loadavg", O_RDONLY);
     if (fd==-1) {
       perror ("open(/proc/loadavg) failed");
-      return 0;
+      return -1;
     }
   }
   if (lseek(fd, 0L, SEEK_SET)!=0) {
     perror("lseek(/proc/loadavg) failed");
     fd=-1;
-    return 0;
+    return -1;
   }
   if (read (fd, &buffer, sizeof(buffer)-1)==-1) {
     perror("read(/proc/loadavg) failed");
     fd=-1;
-    return 0;
+    return -1;
   }
-  if (sscanf(buffer, "%lf", &value)<1) {
+  if (sscanf(buffer, "%lf %lf %lf", &val1, &val2, &val3)<3) {
     fprintf(stderr, "scanf(/proc/loadavg) failed\n");
     fd=-1;
-    return 0;
+    return -1;
   }
-  return (value);
+
+  *load1=val1;
+  *load2=val2;
+  *load3=val3;
+
+  return 0;
 }
 
 
-double Busy (void)
+int Busy (double *user, double *nice, double *system, double *idle)
 {
   static int fd=-2;
   char buffer[64];
   unsigned long v1, v2, v3, v4;
-  double busy, idle;
+  double d1, d2, d3, d4, d5;
+
+  *user=0.0;
+  *nice=0.0;
+  *system=0.0;
+  *idle=0.0;
 
-  if (fd==-1) return 0;
+  if (fd==-1) return -1;
 
   if (fd==-2) {
     fd=open("/proc/stat", O_RDONLY);
     if (fd==-1) {
       perror ("open(proc/stat) failed");
-      return 0;
+      return -1;
     }
   }
   if (lseek(fd, 0L, SEEK_SET)!=0) {
     perror ("lseek(/proc/stat) failed");
     fd=-1;
-    return 0;
+    return -1;
   }
   if (read (fd, &buffer, sizeof(buffer)-1)==-1) {
     perror ("read(/proc/stat) failed");
     fd=-1;
-    return 0;
+    return -1;
   }
   if (sscanf(buffer, "%*s %lu %lu %lu %lu\n", &v1, &v2, &v3, &v4)<4) {
     fprintf (stderr, "scanf(/proc/stat) failed\n");
     fd=-1;
-    return 0;
+    return -1;
   }
 
-  busy=smooth("cpu_busy", 500, v1+v2+v3);
-  idle=smooth("cpu_idle", 500, v4);
-  
-  if (busy+idle==0.0)
-    return 0.0;
-  else
-    return busy/(busy+idle);
+  d1=smooth("cpu_user", 500, v1);
+  d2=smooth("cpu_nice", 500, v2);
+  d3=smooth("cpu_sys",  500, v3);
+  d4=smooth("cpu_idle", 500, v4);
+  d5=d1+d2+d3+d4;
+  if (d5!=0.0) {
+    *user=(d1+d2)/d5;
+    *nice=d2/d5;
+    *system=d3/d5;
+    *idle=d4/d5;
+  }
+  return 0;
 }
 
 
@@ -234,53 +265,53 @@ int Disk (int *r, int *w)
   *r=0;
   *w=0;
 
-  if (fd==-1) return 0;
+  if (fd==-1) return -1;
   
   if (fd==-2) {
     fd = open("/proc/stat", O_RDONLY | O_NDELAY);
     if (fd==-1) {
       perror ("open(/proc/stat) failed");
-      return 0;
+      return -1;
     }
   }
   
   if (lseek(fd, 0L, SEEK_SET)!=0) {
     perror ("lseek(/proc/stat) failed");
     fd=-1;
-    return 0;
+    return -1;
   }
   if (read (fd, &buffer, sizeof(buffer)-1)==-1) {
     perror ("read(/proc/stat) failed");
     fd=-1;
-    return 0;
+    return -1;
   }
   p=strstr(buffer, "disk_rblk");
   if (p==NULL) {
     fprintf (stderr, "parse(/proc/stat) failed: no disk_rblk line\n");
     fd=-1;
-    return 0;
+    return -1;
   }
   if (sscanf(p+9, "%lu %lu %lu %lu\n", &r1, &r2, &r3, &r4)<4) {
     fprintf (stderr, "scanf(/proc/stat) failed\n");
     fd=-1;
-    return 0;
+    return -1;
   }
   p=strstr(buffer, "disk_wblk");
   if (p==NULL) {
     fprintf (stderr, "parse(/proc/stat) failed: no disk_wblk line\n");
     fd=-1;
-    return 0;
+    return -1;
   }
   if (sscanf(p+9, "%lu %lu %lu %lu\n", &w1, &w2, &w3, &w4)<4) {
     fprintf (stderr, "scanf(/proc/stat) failed\n");
     fd=-1;
-    return 0;
+    return -1;
   }
   
   *r=smooth ("disk_r", 500, r1+r2+r3+r4);
   *w=smooth ("disk_w", 500, w1+w2+w3+w4);
 
-  return *r+*w;
+  return 0;
 }
 
 
@@ -293,25 +324,25 @@ int Net (int *rx, int *tx)
   *rx=0;
   *tx=0;
 
-  if (fd==-1) return 0;
+  if (fd==-1) return -1;
   
   if (fd==-2) {
     fd = open("/proc/net/dev", O_RDONLY | O_NDELAY);
     if (fd==-1) {
       perror ("open(/proc/net/dev) failed");
-      return 0;
+      return -1;
     }
   }
   
   if (lseek(fd, 0L, SEEK_SET)!=0) {
     perror ("lseek(/proc/net/dev) failed");
     fd=-1;
-    return 0;
+    return -1;
   }
   if (read (fd, &buffer, sizeof(buffer)-1)==-1) {
     perror ("read(/proc/net/dev) failed");
     fd=-1;
-    return 0;
+    return -1;
   }
   pkg_rx=0;
   pkg_tx=0;
@@ -327,43 +358,122 @@ int Net (int *rx, int *tx)
   *rx=smooth("net_rx", 500, pkg_rx);
   *tx=smooth("net_tx", 500, pkg_tx);
 
-  return *rx+*tx;
+  return 0;
 }
 
 
-double Temperature (void)
+int Sensor (int index, double *val, double *min, double *max)
 {
-  static int fd=-2;
   char buffer[32];
-  static double value=0.0;
-  static time_t now=0;
-
-  if (fd==-1) return 0;
-  
-  if (time(NULL)==now) return value;
-  time(&now);
+  double value;
+  static int fd[SENSORS]={[0 ... SENSORS]=-2,};
+  static char *sensor[SENSORS]={NULL,};
+  static double val_buf[SENSORS]={0.0,};
+  static double min_buf[SENSORS]={0.0,};
+  static double max_buf[SENSORS]={0.0,};
+  static time_t now[SENSORS]={0,};
+
+  if (index<0 || index>=SENSORS) return -1;
+
+  *val=val_buf[index];
+  *min=min_buf[index];
+  *max=max_buf[index];
+
+  if (fd[index]==-1) return -1;
+
+  if (time(NULL)==now[index]) return 0;
+  time(&now[index]);
+
+  if (fd[index]==-2) {
+    snprintf(buffer, 32, "Sensor%d", index);
+    sensor[index]=cfg_get(buffer);
+    if (sensor[index]==NULL || *sensor[index]=='\0') {
+      fprintf (stderr, "%s: no entry for '%s'\n", cfg_file(), buffer);
+      fd[index]=-1;
+      return -1;
+    }
 
-  if (fd==-2) {
-    fd=open(sensor, O_RDONLY);
-    if (fd==-1) {
-      fprintf (stderr, "open (%s) failed: %s\n", sensor, strerror(errno));
-      return 0;
+    snprintf(buffer, 32, "Sensor%d_min", index);
+    min_buf[index]=atof(cfg_get(buffer));
+    snprintf(buffer, 32, "Sensor%d_max", index);
+    max_buf[index]=atof(cfg_get(buffer));
+    if (max_buf[index]==0.0) max_buf[index]=100.0;
+    
+    fd[index]=open(sensor[index], O_RDONLY);
+    if (fd[index]==-1) {
+      fprintf (stderr, "open (%s) failed: %s\n", sensor[index], strerror(errno));
+      return -1;
     }
   }
-  if (lseek(fd, 0L, SEEK_SET)!=0) {
-    fprintf (stderr, "lseek(%s) failed: %s\n", sensor, strerror(errno));
-    fd=-1;
-    return 0;
+  if (lseek(fd[index], 0L, SEEK_SET)!=0) {
+    fprintf (stderr, "lseek(%s) failed: %s\n", sensor[index], strerror(errno));
+    fd[index]=-1;
+    return -1;
   }
-  if (read (fd, &buffer, sizeof(buffer)-1)==-1) {
-    fprintf (stderr, "read(%s) failed: %s\n", sensor, strerror(errno));
-    fd=-1;
-    return 0;
+  if (read (fd[index], &buffer, sizeof(buffer)-1)==-1) {
+    fprintf (stderr, "read(%s) failed: %s\n", sensor[index], strerror(errno));
+    fd[index]=-1;
+    return -1;
   }
   if (sscanf(buffer, "%*f %*f %lf", &value)<1) {
-    fprintf (stderr, "scanf(%s) failed\n", sensor);
-    fd=-1;
-    return 0;
+    fprintf (stderr, "scanf(%s) failed\n", sensor[index]);
+    fd[index]=-1;
+    return -1;
   }
-  return (value);
+  val_buf[index]=value;
+  *val=value;
+  return 0;
 }
+
+
+#ifdef STANDALONE
+
+int tick, tack, tau;
+
+void main (void)
+{
+  char *cfg_file="./lcd4linux.conf.sample";
+  double load1, load2, load3;
+  double user, nice, system, idle;
+  int r, w;
+  int rx, tx;
+  double val, min, max;
+
+  if (cfg_read (cfg_file)==-1)
+    exit (1);
+
+  tick=atoi(cfg_get("tick"));
+  tack=atoi(cfg_get("tack"));
+  tau=atoi(cfg_get("tau"));
+
+  printf ("System    : %s\n", System());
+  printf ("Release   : %s\n", Release ());
+  printf ("Processor : %s\n", Processor ());
+  printf ("Memory    : %d MB\n", Memory ());
+
+  while (1) {
+    
+    Load (&load1, &load2, &load3);
+    printf ("Load      : %f %f %f\n", load1, load2, load3);
+    
+    Busy (&user, &nice, &system, &idle);
+    printf ("Busy      : %f %f %f %f\n", user, nice, system, idle);
+
+    Disk (&r, &w);
+    printf ("Disk      : %d %d\n", r, w);
+
+    Net (&rx, &tx);
+    printf ("Net       : %d %d\n", rx, tx);
+    
+    Sensor (1, &val, &min, &max);
+    printf ("Sensor 1  : %f %f %f\n", val, min, max);
+    
+    Sensor (2, &val, &min, &max);
+    printf ("Sensor 2  : %f %f %f\n", val, min, max);
+    
+    usleep(tack*1000);
+  }
+
+}
+
+#endif
index 0726d6cb7f9754d40618a8c43e4d783a98e2fc40..37dc77a5ce564ceb7c4073b5956401c77b08edde 100644 (file)
--- a/system.h
+++ b/system.h
@@ -1,4 +1,4 @@
-/* $Id: system.h,v 1.2 2000/03/06 06:04:06 reinelt Exp $
+/* $Id: system.h,v 1.3 2000/03/07 11:01:34 reinelt Exp $
  *
  * system status retreivement
  *
  *
  *
  * $Log: system.h,v $
+ * Revision 1.3  2000/03/07 11:01:34  reinelt
+ *
+ * system.c cleanup
+ *
  * Revision 1.2  2000/03/06 06:04:06  reinelt
  *
  * minor cleanups
 #ifndef _SYSTEM_H_
 #define _SYSTEM_H_
 
+#define SENSORS 16
+
 char *System (void);
 char *Release (void);
 char *Processor (void);
 int   Memory (void);
-double Load (void);
-double Busy (void);
-int Disk (int *r, int *w);
-int Net (int *r, int *w);
-double Temperature (void);
+int   Load (double *load1, double *load2, double *load3);
+int   Busy (double *user, double *nice, double *system, double *idle);
+int   Disk (int *r, int *w);
+int   Net (int *rx, int *tx);
+int   Sensor (int index, double *val, double *min, double *max);
 
 #endif