--- a/Makefile Fri May 14 12:06:25 2010 +0100
+++ b/Makefile Fri May 14 15:53:05 2010 +0100
@@ -1,11 +1,12 @@
-OBJECTS:=pbmd stateManager handlers
+SRC=pbmd.c stateManager.c handlers.c
+OBJECTS=pbmd stateManager handlers
CFLAGS=-g -O0 -Wall -I../
LIBS= -lphidget21 -lpthread -ldl
all: $(OBJECTS)
%: %.c
- $(CC) -o $@ $^ $(CFLAGS) $(LIBS)
+ $(CC) $(SRC) -o $@ $^ $(CFLAGS) $(LIBS)
clean:
-rm *.o $(OBJECTS)
--- a/handlers.c Fri May 14 12:06:25 2010 +0100
+++ b/handlers.c Fri May 14 15:53:05 2010 +0100
@@ -1,6 +1,4 @@
#include "handlers.h"
-#include <stdio.h>
-#include <phidget21.h>
int IFK_DetachHandler(CPhidgetHandle IFK, void *userptr)
{
--- a/handlers.h Fri May 14 12:06:25 2010 +0100
+++ b/handlers.h Fri May 14 15:53:05 2010 +0100
@@ -1,6 +1,7 @@
#ifndef HANDLERS
#define HANDLERS
+#include <stdio.h>
#include <phidget21.h>
int IFK_DetachHandler(CPhidgetHandle IFK, void *userptr);
Binary file pbmd has changed
--- a/pbmd.c Fri May 14 12:06:25 2010 +0100
+++ b/pbmd.c Fri May 14 15:53:05 2010 +0100
@@ -20,9 +20,9 @@
int main(int argc, char* argv[])
{
- int Sleep, testVoltage;
+ int i, Sleep, testVoltage;
char *SNAPSHOTFILE;
- char *bootflag = malloc(2);
+ char *bootflag = (char*) malloc(1);
time_t boottime;
if ( argc != 6 ) // 5 args inc program name
@@ -30,12 +30,23 @@
printf( "usage: %s logfile snapshotfile rebootfile sleeptime testvoltage\n", argv[0] );
exit(1);
}
-
+
+ //ensure args are not null
+ for(i=0; i<argc; i++)
+ if(argv[i] == NULL)
+ {
+ printf( "error: arg %d is null\n", i );
+ printf( "usage: %s logfile snapshotfile rebootfile sleeptime testvoltage\n", argv[0] );
+ exit(1);
+ }
+
+
// Close out stdout and point to a file cos if we are running as a demon we need to log this somewhere.
fclose(stdout);
fclose(stderr);
- stdout = fopen("/var/log/pbm.log", "a");
- stderr = fopen("/var/log/pbm.err", "a");
+
+ stdout = fopen("/var/log/pbm.log", "a");//todo: change this to syslog
+ stderr = fopen("/var/log/pbm.err", "a");//todo: change this to syslog
Sleep = atoi(argv[4]); // Arg 4 is sleep interval
testVoltage = atoi(argv[5]); // Arg 5 is used for testing
@@ -46,11 +57,11 @@
FILE *filesnap = fopen( argv[2], "w" ); // Open snapshot file
//todo: don't exit here
- if ( (filelog == 0) || (filesnap == 0)) // Aaargh! Problem ...
- {
- printf( "Could not open log file or snapshot file\n" );
- exit(1);
- }
+ //if ( (filelog == 0) || (filesnap == 0)) // Aaargh! Problem ...
+ //{
+ // printf( "Could not open log file or snapshot file\n" );
+ // exit(1);
+ //}
fclose( filesnap ); // cos we need to overwrite it every time we write to it
@@ -65,22 +76,23 @@
if ( (fileboot == 0) )
{
printf( "Could not open boot flag file - assuming manual override\n" );
- bootflag = "N";
+ *bootflag = BOOTFLAG_MAN_OVERRIDE;
}
else
{
// read from boot state file
- printf( "Opened boot flag file\n");
- // bootflag should be "S"
- if (fscanf (fileboot, "%s", bootflag))
+ printf( "Opened boot flag file\n");
+
+ *bootflag = fgetc(fileboot); // bootflag should be 'S'
+ if (*bootflag == BOOTFLAG_NORMAL)
{
- time ( &boottime ); // log time we booted
+ time ( &boottime ); // log time we booted
fclose( fileboot );
}
else
{
printf( "Error reading in from bootflag file - assuming manual restart\n" );
- bootflag = "X";
+ *bootflag = BOOTFLAG_MAN_RESTART;
}
}
@@ -104,7 +116,7 @@
printf ("Sleep time is %d\n", Sleep);
printf ("Bootflag is %s\n", bootflag);
- fprintf (filelog, "\nYYYY-M-MDD,HH:MM:SS,mVolts,mAmps,Lux\n");
+ //fprintf (filelog, "\nYYYY-M-MDD,HH:MM:SS,mVolts,mAmps,Lux\n");
SNAPSHOTFILE = argv[2];
--- a/stateManager.c Fri May 14 12:06:25 2010 +0100
+++ b/stateManager.c Fri May 14 15:53:05 2010 +0100
@@ -1,23 +1,4 @@
#include "stateManager.h"
-#include <stdio.h>
-#include <string.h>
-#include <unistd.h>
-#include <phidget21.h>
-#include <time.h>
-#include <stdlib.h>
-#include "handlers.h"
-
-#define INIT 0 // Initialised (first run or reset manually)
-#define STBYLONG 1 // Standby 180 - reboot, check voltage, sleep 3 hours
-#define STBYSHORT 2 // Standby 30 - boot, check voltage, sleep 30 mins
-#define OVERRIDE 3 // manually started - run 30 mins then sleep
-#define SLEEP 4 // Sleep during the night
-#define UP 9 // Running normally - dont sleep
-#define GREEN 1150 // Voltage level for normal running
-#define AMBER 1129 // Voltage level at which we must go asleep
-#define SLEEPTIMESTART "22:00" // Time to start sleeping
-#define SLEEPTIMEEND "06:00" // Time to end sleeping
-
void display_generic_properties(CPhidgetHandle phid)
{
@@ -152,8 +133,8 @@
strftime (timeStr,80,"%Y-%m-%d,%H:%M:%S",timeinfo);
- fprintf(filelog, "%s,%d,%d,%d\n", timeStr, Voltage, Amps, Lux);
- fflush(filelog);
+ //fprintf(filelog, "%s,%d,%d,%d\n", timeStr, Voltage, Amps, Lux);
+ //fflush(filelog);
}
@@ -178,12 +159,12 @@
char *timeStr = malloc(20);
int localState = 0;
- printf ("In getnewState: Bootflag %s Voltage %d\n", bootflag, Voltage);
+ printf ("In getnewState: Bootflag %c Voltage %d\n", *bootflag, Voltage);
*newState = localState; // Just to be sure its 0
getTimeString (0, timeStr); // Use this to return current time as a string
- if (strncmp(bootflag, "S",1) != 0) // Anything except an S will mean manual start
+ if (*bootflag != BOOTFLAG_NORMAL) // Anything except an S will mean manual start
localState = OVERRIDE;
else if (strcmp(timeStr, SLEEPTIMESTART) > 0 || strcmp(timeStr, SLEEPTIMEEND) < 0)
@@ -388,8 +369,8 @@
if (timenow > boottime + 1800)
{
printf ("OVERRIDE - 30 mins expired ... switching to normal mode\n");
- bootflag = "S";
- printf ("Bootflag in case OVERRIDE %s \n", bootflag);
+ *bootflag = BOOTFLAG_NORMAL;
+ printf ("Bootflag in case OVERRIDE %c \n", *bootflag);
}
}
else if(newState == SLEEP)
--- a/stateManager.h Fri May 14 12:06:25 2010 +0100
+++ b/stateManager.h Fri May 14 15:53:05 2010 +0100
@@ -1,7 +1,27 @@
#ifndef STATE_MANAGER
#define STATE_MANAGER
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
#include <phidget21.h>
+#include <time.h>
+#include <stdlib.h>
+#include "handlers.h"
+
+#define INIT 0 // Initialised (first run or reset manually)
+#define STBYLONG 1 // Standby 180 - reboot, check voltage, sleep 3 hours
+#define STBYSHORT 2 // Standby 30 - boot, check voltage, sleep 30 mins
+#define OVERRIDE 3 // manually started - run 30 mins then sleep
+#define SLEEP 4 // Sleep during the night
+#define UP 9 // Running normally - dont sleep
+#define GREEN 1150 // Voltage level for normal running
+#define AMBER 1129 // Voltage level at which we must go asleep
+#define SLEEPTIMESTART "22:00" // Time to start sleeping
+#define SLEEPTIMEEND "06:00" // Time to end sleeping
+#define BOOTFLAG_MAN_RESTART 'X'
+#define BOOTFLAG_MAN_OVERRIDE 'N'
+#define BOOTFLAG_NORMAL 'S'
void display_generic_properties(CPhidgetHandle phid);
void display_properties(CPhidgetTextLCDHandle phid);