diff -urN apm-sleep-0.6-1/apm-sleep.c apm-sleep-0.6.nocrash/apm-sleep.c --- apm-sleep-0.6-1/apm-sleep.c Sun Jul 1 16:50:38 2001 +++ apm-sleep-0.6.nocrash/apm-sleep.c Sun Jul 1 17:43:29 2001 @@ -33,12 +33,23 @@ #ifndef AGENDA_VR3 #include #include +#else +#include +#include #endif /* AGENDA_VR3 */ #define MAX_IRQS 255 #define INTERRUPTS "/proc/interrupts" #define DEFAULT_SLEEP_TIME 10 #define PID_FILE "/var/run/apm-sleep.pid" +#ifdef AGENDA_VR3 +#define BATTERY "/dev/adif" +#define APM_SLEEP_FILE "/proc/sys/pm/sleep" +#define CRASH_VOLTAGE 660 +#define CRASH_DELTA 25 +#define CRASH_BLINK 3 +#define ALWAYS_LOG 0 +#endif /* AGENDA_VR3 */ int irqs[MAX_IRQS]; /* irqs to examine have a value of 1 */ int max_unused=120; /* in seconds - default 2 minutes */ @@ -46,7 +57,11 @@ int ac_max_unused; #ifdef AGENDA_VR3 -char *sleep_command="echo 1 > /proc/sys/pm/sleep"; +const char *sleep_command_default="echo 1 > /proc/sys/pm/sleep"; +static char *sleep_command; +static int crash_voltage=CRASH_VOLTAGE, crash_delta=CRASH_DELTA; +static int crash_blink=CRASH_BLINK; +static int always_log=ALWAYS_LOG; #else /* AGENDA_VR3 */ char *sleep_command="apm -s"; #endif /* AGENDA_VR3 */ @@ -77,6 +92,28 @@ fprintf(stderr, "Usage: apm-sleep [-s command] [-u n] [-U n] [-i n [-i n ..]] [-a] [-n] [-d] [-c n]\n"); } +#ifdef AGENDA_VR3 +void write_to_file(char *file, char *line); + +void go_to_sleep () { + /* provide for -s option ... */ + if (sleep_command != sleep_command_default) { + if (system(sleep_command) != 0) { + syslog(LOG_ERR, "%s failed", sleep_command); + }; + } else { + write_to_file(APM_SLEEP_FILE,"1"); + } +} + +void write_to_file(char *file, char *line) { + int fd; + fd=open(file,O_WRONLY); + write(fd,line,strlen(line)); + close(fd); +} +#endif + void parse_command_line (int argc, char **argv) { extern char *optarg; struct option long_options[] = { @@ -160,6 +197,9 @@ long v; time_t nowtime, oldtime=0; FILE *f; + int batteryfd; + unsigned short voltage,oldvoltage=0; + char vchar[8]; char line[64]; #ifndef AGENDA_VR3 apm_info ai; @@ -169,9 +209,48 @@ activity=0; f=fopen(INTERRUPTS, "r"); if (! f) { + syslog(LOG_ERR, INTERRUPTS); perror(INTERRUPTS); exit(1); } + if ((batteryfd=open(BATTERY,O_RDONLY))<0) { + syslog(LOG_ERR, BATTERY " open"); + perror(BATTERY); + exit(2); + } + if (read(batteryfd, &vchar, 8) != 8) { + syslog(LOG_ERR, BATTERY " read" ); + perror(BATTERY); + exit(2); + } + voltage = *(unsigned short *)vchar; + if (voltage - crash_voltage < (crash_blink*crash_delta)) { + sprintf(line,"\033[16;1]\033[17;5]\033[19;%i]\033[18;3]",crash_blink-((voltage - crash_voltage)/crash_delta)); + write_to_file("/dev/console",line); + } + if (always_log && oldvoltage && voltage != oldvoltage) + syslog(LOG_NOTICE, "now: %i, delta: %i",voltage,voltage-oldvoltage); + while (voltage < crash_voltage || (voltage - oldvoltage) < -crash_delta ) { + go_to_sleep(); + sleep(1); + syslog(LOG_NOTICE, "now: %i, delta: %i - going to sleep",voltage,voltage-oldvoltage); + close(batteryfd); + if ((batteryfd=open(BATTERY,O_RDONLY))<0) { + syslog(LOG_ERR, BATTERY " open"); + perror(BATTERY); + exit(2); + } + if (read(batteryfd, &vchar, 8) != 8) { + syslog(LOG_ERR, BATTERY " read" ); + perror(BATTERY); + exit(2); + } + oldvoltage=voltage; + voltage = *(unsigned short *)vchar; + } + oldvoltage=voltage; + close(batteryfd); + while(fgets(line,sizeof(line),f)) { if (sscanf(line,"%d: %ld", &i, &v) == 2) { if (autoprobe) { @@ -217,10 +296,14 @@ printf("Total unused = %d\n", total_unused); if (sleep_now) { - if(debug) printf("Going to bad\n"); + if(debug) printf("Going to bed\n"); syslog(LOG_NOTICE, "forcing sleep"); +#ifdef AGENDA_VR3 + go_to_sleep(); +#else /* AGENDA_VR3 */ if (system(sleep_command) != 0) syslog(LOG_ERR, "%s failed", sleep_command); +#endif /* AGENDA_VR3 */ total_unused=0; oldtime=0; sleep_now=0; @@ -249,14 +332,27 @@ int get_sleeptime() { char label[15]; - char time[3]; + char value[5]; + int sleeptime; + FILE* fp = fopen("/etc/apm-sleep.conf", "r"); if(fp == 0) return -1; /* unable to open file */ - fscanf(fp, "%s %s", label, time); + while (fscanf(fp, "%s %s", label, value) != EOF) { + if(strcmp(label, "sleeptime") == 0) + sleeptime=atoi(value); + else if(strcmp(label, "crash_voltage") == 0) + crash_voltage=atoi(value); + else if(strcmp(label, "crash_delta") == 0) + crash_delta=atoi(value); + else if(strcmp(label, "crash_blink") == 0) + crash_blink=atoi(value); + else if(strcmp(label, "always_log") == 0) + always_log=atoi(value); + } fclose(fp); - if(strcmp(label, "sleeptime") == 0) - return atoi(time); + if (sleeptime) + return (sleeptime); return -1; /* line doesn't exist */ } @@ -269,6 +365,7 @@ FILE *f; int unused_time; + sleep_command=(char *)sleep_command_default; signal(SIGHUP, sighup_handler); parse_command_line(argc, argv);