Blink the charger LED instead of the Power LED, when battery is low
[pandora-libraries.git] / apps / pndevmapperd.c
index afed8d9..292b27e 100644 (file)
 #include <fcntl.h> // for open(2)
 #include <errno.h> // for errno
 #include <time.h> // for time(2)
+#include <ctype.h> // for isdigit
+#include <signal.h> // for sigaction
+#include <sys/wait.h> // for wait
+#include <sys/time.h> // setitimer
 
 #include <linux/input.h> // for keys
 //#include "../../kernel-rip/input.h" // for keys
@@ -29,6 +33,9 @@
 #include "pnd_pndfiles.h"
 #include "pnd_pxml.h"
 #include "pnd_logger.h"
+#include "pnd_utility.h"
+#include "pnd_notify.h"
+#include "pnd_device.h"
 
 // daemon and logging
 //
@@ -92,26 +99,51 @@ typedef struct {
 evmap_t g_evmap [ MAXEVENTS ];
 unsigned int g_evmap_max = 0;
 
+// battery
+unsigned char b_threshold = 5;    // %battery
+unsigned int b_frequency = 300;   // frequency to check
+unsigned int b_blinkfreq = 2;     // blink every 2sec
+unsigned int b_blinkdur = 1000;   // blink duration (uSec), 0sec + uSec is assumed
+unsigned char b_active = 0;       // 0=inactive, 1=active and waiting to blink, 2=blink is on, waiting to turn off
+unsigned char b_shutdown = 1;     // %age battery to force a shutdown!
+char *b_shutdown_script = NULL;
+
 /* get to it
  */
 void dispatch_key ( int keycode, int val );
 void dispatch_event ( int code, int val );
+void sigchld_handler ( int n );
+unsigned char set_next_alarm ( unsigned int secs, unsigned int usecs );
+void sigalrm_handler ( int n );
 
 static void usage ( char *argv[] ) {
   printf ( "%s [-d]\n", argv [ 0 ] );
   printf ( "-d\tDaemon mode; detach from terminal, chdir to /tmp, suppress output. Optional.\n" );
-  printf ( "Signal: HUP the process to force reload of configuration and reset the notifier watch paths\n" );
+  printf ( "-l#\tLog-it; -l is 0-and-up (or all), and -l2 means 2-and-up (not all); l[0-3] for now. Log goes to /tmp/pndevmapperd.log\n" );
   return;
 }
 
 int main ( int argc, char *argv[] ) {
   int i;
+  int logall = -1; // -1 means normal logging rules; >=0 means log all!
 
   for ( i = 1; i < argc; i++ ) {
 
     if ( argv [ i ][ 0 ] == '-' && argv [ i ][ 1 ] == 'd' ) {
       //printf ( "Going daemon mode. Silent running.\n" );
       g_daemon_mode = 1;
+    } else if ( argv [ i ][ 0 ] == '-' && argv [ i ][ 1 ] == 'l' ) {
+
+      if ( isdigit ( argv [ i ][ 2 ] ) ) {
+       unsigned char x = atoi ( argv [ i ] + 2 );
+       if ( x >= 0 &&
+            x < pndn_none )
+       {
+         logall = x;
+       }
+      } else {
+       logall = 0;
+      }
     } else {
       usage ( argv );
       exit ( 0 );
@@ -121,14 +153,40 @@ int main ( int argc, char *argv[] ) {
 
   /* enable logging?
    */
-  if ( g_daemon_mode ) {
-    // nada
+  pnd_log_set_pretext ( "pndevmapperd" );
+  pnd_log_set_flush ( 1 );
+
+  if ( logall == -1 ) {
+    // standard logging; non-daemon versus daemon
+
+    if ( g_daemon_mode ) {
+      // nada
+    } else {
+      pnd_log_set_filter ( pndn_rem );
+      pnd_log_to_stdout();
+    }
+
   } else {
-    pnd_log_set_filter ( pndn_rem );
-    pnd_log_set_pretext ( "pndevmapperd" );
-    pnd_log_to_stdout();
-    pnd_log ( pndn_rem, "log level starting as %u", pnd_log_get_filter() );
-  }
+    FILE *f;
+
+    f = fopen ( "/tmp/pndevmapperd.log", "w" );
+
+    if ( f ) {
+      pnd_log_set_filter ( logall );
+      pnd_log_to_stream ( f );
+      pnd_log ( pndn_rem, "logall mode - logging to /tmp/pndevmapperd.log\n" );
+    }
+
+    if ( logall == pndn_debug ) {
+      pnd_log_set_buried_logging ( 1 ); // log the shit out of it
+      pnd_log ( pndn_rem, "logall mode 0 - turned on buried logging\n" );
+    }
+
+  } // logall
+
+  pnd_log ( pndn_rem, "%s built %s %s", argv [ 0 ], __DATE__, __TIME__ );
+
+  pnd_log ( pndn_rem, "log level starting as %u", pnd_log_get_filter() );
 
   // basic daemon set up
   if ( g_daemon_mode ) {
@@ -151,6 +209,45 @@ int main ( int argc, char *argv[] ) {
     
   } // set up daemon
 
+  /* hmm, seems to not like working right after boot.. do we depend on another daemon or
+   * on giving kernel time to init something, or ... wtf?
+   * -- lets give the system some time to wake up
+   */
+  { // delay
+
+    // this one works for pndnotifyd, which actually needs INOTIFYH..
+    //
+
+    // check if inotify is awake yet; if not, try waiting for awhile to see if it does
+    pnd_log ( pndn_rem, "Starting INOTIFY test; should be instant, but may take awhile...\n" );
+
+    if ( ! pnd_notify_wait_until_ready ( 120 /* seconds */ ) ) {
+      pnd_log ( pndn_error, "ERROR: INOTIFY refuses to be useful and quite awhile has passed. Bailing out.\n" );
+      return ( -1 );
+    }
+
+    pnd_log ( pndn_rem, "INOTIFY seems to be useful, whew.\n" );
+
+    // pndnotifyd also waits for user to log in .. pretty excessive, especially since
+    // what if user wants to close the lid while at the log in screen? for now play the
+    // odds as thats pretty unliekly usage scenariom but is clearly not acceptible :/
+    //
+
+    // wait for a user to be logged in - we should probably get hupped when a user logs in, so we can handle
+    // log-out and back in again, with SDs popping in and out between..
+    pnd_log ( pndn_rem, "Checking to see if a user is logged in\n" );
+    char tmp_username [ 128 ];
+    while ( 1 ) {
+      if ( pnd_check_login ( tmp_username, 127 ) ) {
+       break;
+      }
+      pnd_log ( pndn_debug, "  No one logged in yet .. spinning.\n" );
+      sleep ( 2 );
+    } // spin
+    pnd_log ( pndn_rem, "Looks like user '%s' is in, continue.\n", tmp_username );
+
+  } // delay
+
   /* inhale config or die trying
    */
   char *configpath;
@@ -234,6 +331,9 @@ int main ( int argc, char *argv[] ) {
     } else if ( strncmp ( k, "pndevmapperd.", 7 ) == 0 ) {
       // not consumed here, skip silently
 
+    } else if ( strncmp ( k, "battery.", 8 ) == 0 ) {
+      // not consumed here, skip silently
+
     } else {
       // uhhh
       pnd_log ( pndn_warning, "Unknown config key '%s'; skipping.\n", k );
@@ -252,6 +352,33 @@ int main ( int argc, char *argv[] ) {
     pnd_log ( pndn_rem, "config file causes minimum_separation to change to %u", g_minimum_separation );
   }
 
+  // battery conf
+  if ( pnd_conf_get_as_int ( evmaph, "battery.threshold" ) != PND_CONF_BADNUM ) {
+    b_threshold = pnd_conf_get_as_int ( evmaph, "battery.threshold" );
+    pnd_log ( pndn_rem, "Battery threshold set to %u", b_threshold );
+  }
+  if ( pnd_conf_get_as_int ( evmaph, "battery.check_interval" ) != PND_CONF_BADNUM ) {
+    b_frequency = pnd_conf_get_as_int ( evmaph, "battery.check_interval" );
+    pnd_log ( pndn_rem, "Battery check interval set to %u", b_frequency );
+  }
+  if ( pnd_conf_get_as_int ( evmaph, "battery.blink_interval" ) != PND_CONF_BADNUM ) {
+    b_blinkfreq = pnd_conf_get_as_int ( evmaph, "battery.blink_interval" );
+    pnd_log ( pndn_rem, "Battery blink interval set to %u", b_blinkfreq );
+  }
+  if ( pnd_conf_get_as_int ( evmaph, "battery.blink_duration" ) != PND_CONF_BADNUM ) {
+    b_blinkdur = pnd_conf_get_as_int ( evmaph, "battery.blink_duration" );
+    pnd_log ( pndn_rem, "Battery blink duration set to %u", b_blinkdur );
+  }
+  b_active = 0;
+  if ( pnd_conf_get_as_int ( evmaph, "battery.shutdown_threshold" ) != PND_CONF_BADNUM ) {
+    b_shutdown = pnd_conf_get_as_int ( evmaph, "battery.shutdown_threshold" );
+    pnd_log ( pndn_rem, "Battery shutdown threshold set to %u", b_shutdown );
+  }
+  if ( pnd_conf_get_as_char ( evmaph, "battery.shutdown_script" ) != NULL ) {
+    b_shutdown_script = strdup ( pnd_conf_get_as_char ( evmaph, "battery.shutdown_script" ) );
+    pnd_log ( pndn_rem, "Battery shutdown script set to %s", b_shutdown_script );
+  }
+
   /* do we have anything to do?
    */
   if ( ! g_evmap_max ) {
@@ -260,6 +387,30 @@ int main ( int argc, char *argv[] ) {
     exit ( -1 );
   } // spin
 
+  /* set up sigchld -- don't want zombies all over; well, we do, but not process zombies
+   */
+  sigset_t ss;
+  sigemptyset ( &ss );
+
+  struct sigaction siggy;
+  siggy.sa_handler = sigchld_handler;
+  siggy.sa_mask = ss; /* implicitly blocks the origin signal */
+  siggy.sa_flags = SA_RESTART; /* don't need anything */
+  sigaction ( SIGCHLD, &siggy, NULL );
+
+  /* set up the battery level warning timers
+   */
+  siggy.sa_handler = sigalrm_handler;
+  siggy.sa_mask = ss; /* implicitly blocks the origin signal */
+  siggy.sa_flags = SA_RESTART; /* don't need anything */
+  sigaction ( SIGALRM, &siggy, NULL );
+
+  if ( set_next_alarm ( b_frequency, 0 ) ) { // check every 'frequency' seconds
+    pnd_log ( pndn_rem, "Checking for low battery every %u seconds\n", b_frequency );
+  } else {
+    pnd_log ( pndn_error, "ERROR: Couldn't set up timer for every %u seconds\n", b_frequency );
+  }
+
   /* actually try to do something useful
    */
 
@@ -267,7 +418,7 @@ int main ( int argc, char *argv[] ) {
 
   // try to locate the appropriate devices
   int id;
-  int fds [ 5 ] = { -1, -1, -1, -1, -1 }; // 0 = keypad, 1 = gpio keys
+  int fds [ 8 ] = { -1, -1, -1, -1, -1, -1, -1, -1 }; // 0 = keypad, 1 = gpio keys
   int imaxfd = 0;
 
   for ( id = 0; ; id++ ) {
@@ -282,7 +433,11 @@ int main ( int argc, char *argv[] ) {
       break;
     }
 
-    ioctl (fd, EVIOCGNAME(sizeof(name)), name );
+    if ( ioctl (fd, EVIOCGNAME(sizeof(name)), name ) < 0 ) {
+      name [ 0 ] = '\0';
+    }
+
+    pnd_log ( pndn_rem, "%s maps to %s\n", fname, name );
 
     if ( strcmp ( name, "omap_twl4030keypad" ) == 0 ) {
       fds [ 0 ] = fd;
@@ -290,13 +445,24 @@ int main ( int argc, char *argv[] ) {
       fds [ 1 ] = fd;
     } else if ( strcmp ( name, "AT Translated Set 2 keyboard" ) == 0) { // for vmware, my dev environment
       fds [ 0 ] = fd;
+    } else if ( strcmp ( name, "triton2-pwrbutton" ) == 0) {
+      fds [ 2 ] = fd;
+    } else if ( strcmp ( name, "ADS784x Touchscreen" ) == 0) {
+      fds [ 3 ] = fd;
+    } else if ( strcmp ( name, "vsense66" ) == 0) {
+      fds [ 4 ] = fd;
+    } else if ( strcmp ( name, "vsense67" ) == 0) {
+      fds [ 5 ] = fd;
     } else {
       pnd_log ( pndn_rem, "Ignoring unknown device '%s'\n", name );
+      //fds [ 6 ] = fd;
       close ( fd );
+      fd = -1;
       continue;
     }
 
     if (imaxfd < fd) imaxfd = fd;
+
   } // for
 
   if ( fds [ 0 ] == -1 ) {
@@ -308,8 +474,8 @@ int main ( int argc, char *argv[] ) {
   }
 
   if ( fds [ 0 ] == -1 && fds [ 1 ] == -1 ) {
-    pnd_log ( pndn_error, "ERROR! Couldn't find either device; exiting!\n" );
-    exit ( -2 );
+    pnd_log ( pndn_error, "ERROR! Couldn't find either device!\n" );
+    //exit ( -2 );
   }
 
   /* loop forever, watching for events
@@ -318,14 +484,21 @@ int main ( int argc, char *argv[] ) {
   while ( 1 ) {
     struct input_event ev[64];
 
+    unsigned int max_fd = 3; /* imaxfd */
     int fd = -1, rd, ret;
     fd_set fdset;
 
     FD_ZERO ( &fdset );
 
-    for (i = 0; i < 2; i++) {
+    imaxfd = 0;
+    for (i = 0; i < max_fd /*imaxfd*/; i++) {
       if ( fds [ i ] != -1 ) {
        FD_SET( fds [ i ], &fdset );
+
+       if ( fds [ i ] > imaxfd ) {
+         imaxfd = fds [ i ];
+       }
+
       }
     }
 
@@ -333,14 +506,14 @@ int main ( int argc, char *argv[] ) {
 
     if ( ret == -1 ) {
       pnd_log ( pndn_error, "ERROR! select(2) failed with: %s\n", strerror ( errno ) );
-      break;
+      continue; // retry!
     }
 
-    for ( i = 0; i < 2; i++ ) {
+    for ( i = 0; i < max_fd; i++ ) {
       if ( fds [ i ] != -1 && FD_ISSET ( fds [ i ], &fdset ) ) {
        fd = fds [ i ];
-      }
-    }
+      } // fd is set?
+    } // for
 
     /* buttons or keypad */
     rd = read ( fd, ev, sizeof(struct input_event) * 64 );
@@ -419,6 +592,8 @@ void dispatch_key ( int keycode, int val ) {
   // 2 - down again (hold)
   // 0 - up (released)
 
+  pnd_log ( pndn_rem, "Dispatching Key..\n" );
+
   for ( i = 0; i < g_evmap_max; i++ ) {
 
     if ( ( g_evmap [ i ].key_p ) &&
@@ -476,6 +651,8 @@ void dispatch_event ( int code, int val ) {
   // 1 - closing
   // 0 - opening
 
+  pnd_log ( pndn_rem, "Dispatching Event..\n" );
+
   for ( i = 0; i < g_evmap_max; i++ ) {
 
     if ( ( g_evmap [ i ].key_p == 0 ) &&
@@ -516,3 +693,124 @@ void dispatch_event ( int code, int val ) {
 
   return;
 }
+
+void sigchld_handler ( int n ) {
+
+  pnd_log ( pndn_rem, "---[ SIGCHLD received ]---\n" );
+
+  int status;
+  wait ( &status );
+
+  pnd_log ( pndn_rem, "     SIGCHLD done ]---\n" );
+
+  return;
+}
+
+unsigned char set_next_alarm ( unsigned int secs, unsigned int usecs ) {
+
+  // assume that SIGALRM is already being caught, we just set the itimer here
+
+  struct itimerval itv;
+
+  // if no timer at all, set the 'current' one so it does something; otherwise
+  // let it continue..
+  getitimer ( ITIMER_REAL, &itv );
+
+  if ( itv.it_value.tv_sec == 0 && itv.it_value.tv_sec == 0 ) {
+    itv.it_value.tv_sec = secs;
+    itv.it_value.tv_usec = usecs;
+  }
+
+  // set the next timer
+  //bzero ( &itv, sizeof(struct itimerval) );
+
+  itv.it_interval.tv_sec = secs;
+  itv.it_interval.tv_usec = usecs;
+
+  // if next-timer is less than current, set current too
+  if ( itv.it_value.tv_sec > itv.it_interval.tv_sec ) {
+    itv.it_value.tv_sec = secs;
+    itv.it_value.tv_usec = usecs;
+  }
+
+  if ( setitimer ( ITIMER_REAL, &itv, NULL /* old value returned here */ ) < 0 ) {
+    // sucks
+    return ( 0 );
+  }
+  
+  return ( 1 );
+}
+
+void sigalrm_handler ( int n ) {
+
+  pnd_log ( pndn_debug, "---[ SIGALRM ]---\n" );
+
+  int batlevel = pnd_device_get_battery_gauge_perc();
+
+  if ( batlevel < 0 ) {
+    // couldn't read the battery level, so just assume low and make blinks?
+    batlevel = 4; // low, but not cause a shutdown
+  }
+
+  // first -- are we critical yet? if so, shut down!
+  if ( batlevel <= b_shutdown && b_shutdown_script ) {
+    int x;
+
+    pnd_log ( pndn_error, "CRITICAL BATTERY LEVEL -- shutdown the system down! Invoke: %s\n", b_shutdown_script );
+
+    if ( ( x = fork() ) < 0 ) {
+      pnd_log ( pndn_error, "ERROR: Couldn't fork()\n" );
+      exit ( -3 );
+    }
+
+    if ( x == 0 ) {
+      execl ( b_shutdown_script, b_shutdown_script, (char*)NULL );
+      pnd_log ( pndn_error, "ERROR: Couldn't exec(%s)\n", b_shutdown_script );
+      exit ( -4 );
+    }
+
+  }
+
+  // is battery warning already active?
+  if ( b_active ) {
+    // warning is on!
+
+    // is user charging up? if so, stop blinking.
+    // perhaps we shoudl check if charger is connected, and not blink at all in that case..
+    if ( batlevel > b_threshold + 1 /* allow for error in read */ ) {
+      pnd_log ( pndn_debug, "Battery is high again, flipping to non-blinker mode\n" );
+      b_active = 0;
+      set_next_alarm ( b_frequency, 0 );
+      pnd_device_set_led_charger_brightness ( 250 );
+      return;
+    }
+
+    if ( b_active == 1 ) {
+      // turn LED on
+      pnd_log ( pndn_debug, "Blink on\n" );
+      pnd_device_set_led_charger_brightness ( 200 );
+      // set timer to short duration
+      b_active = 2;
+      set_next_alarm ( 0, b_blinkdur );
+    } else if ( b_active == 2 ) {
+      // turn LED off
+      pnd_log ( pndn_debug, "Blink off\n" );
+      pnd_device_set_led_charger_brightness ( 10 );
+      // back to longer duration
+      b_active = 1;
+      set_next_alarm ( b_blinkfreq, 0 );
+    }
+
+    return;
+  }
+
+  // warning is off..
+  if ( batlevel <= b_threshold ) {
+    // battery seems low, go to active mode
+    pnd_log ( pndn_debug, "Battery is low, flipping to blinker mode\n" );
+    b_active = 1;
+    set_next_alarm ( b_blinkfreq, 0 );
+  } // battery level
+
+  return;
+}