[Olsr-dev] olsr linux/apm.c

Joe Gio (spam-protected)
Mon Feb 11 18:29:25 CET 2008


I've made some changes to linux/apm.c to capture changes of ac and/or 
battery power.

Regards
Joe Giovatto


--- /root/olsrd-a5b9cf969979/src/linux/apm.c    2008-02-06 
13:32:49.000000000 -0500
+++ src/linux/apm.c     2008-02-10 19:02:21.000000000 -0500
@@ -104,7 +104,11 @@
 #define USE_APM    1
 #define USE_ACPI   2

-static int method, fd_index;
+static int method;
+
+static int fd_index;
+
+static int ac_power_on;

 /* Prototypes */

@@ -139,7 +143,7 @@


 void
-apm_printinfo(struct olsr_apm_info *ainfo __attribute__((unused)))
+apm_printinfo(struct olsr_apm_info *ainfo)
 {
   OLSR_PRINTF(5, "APM info:\n\tAC status %d\n\tBattery percentage 
%d%%\n\tBattery time left %d mins\n\n",
              ainfo->ac_line_status,
@@ -237,15 +241,26 @@
   FILE *fd;
   int bat_max = 5000; /* Find some sane value */
   int bat_val = 0;
-
-  printf("READING ACPI\n");
+  int result;
+
+  /* reporbe in case ac status changed */
+  fd_index = acpi_probe();

+  /* No battery was found */
   if(fd_index < 0)
     {
-      /* No battery was found or AC power was detected */
-      ainfo->ac_line_status = OLSR_AC_POWERED;
-      ainfo->battery_percentage = 100;
-      return 1;
+      /* but we have ac */
+      if(ac_power_on)
+        {
+           ainfo->ac_line_status = OLSR_AC_POWERED;
+
+           ainfo->battery_percentage = -1;
+
+           return 1;
+        }
+
+      /* not enough info */
+      return 0;
     }

   /* Get maxvalue */
@@ -282,8 +297,11 @@
     }
   fclose(fd);

-  ainfo->ac_line_status = OLSR_BATTERY_POWERED;
-  ainfo->battery_percentage = bat_val * 100 / bat_max;
+  ainfo->ac_line_status = ac_power_on ? OLSR_AC_POWERED : 
OLSR_BATTERY_POWERED;
+
+  result = bat_val * 100 / bat_max;
+
+  ainfo->battery_percentage = result > 100 ? 100 : result;

   return 1;
 }
@@ -295,6 +313,7 @@
   unsigned int i;

   /* First check for AC power */
+  ac_power_on = 0;

   for(i = 0; i < ACPI_AC_CNT; i++)
     {
@@ -316,8 +335,13 @@
          continue;

       /* Running on AC power */
-      if(!strcasecmp(s2, "on-line"))
-       return -1;
+      if(!strcasecmp(s2, "on-line")) {
+
+        /* ac power enabled */
+        ac_power_on = 1;
+
+       break;
+      }
     }

   /* Only checking the first found battery entry... */
@@ -333,6 +357,7 @@

       /* Extract info */
       rc = fscanf(fd, "%s %s", s1, s2);
+
       /* Close info entry */
       fclose(fd);

-------------- next part --------------
A non-text attachment was scrubbed...
Name: apm.c.diff
Type: text/x-patch
Size: 2281 bytes
Desc: not available
URL: <http://lists.olsr.org/pipermail/olsr-dev/attachments/20080211/411a0f0f/attachment.bin>


More information about the Olsr-dev mailing list