--- a/pbmd.c Tue May 25 11:43:19 2010 +0100
+++ b/pbmd.c Wed May 26 11:36:44 2010 +0100
@@ -20,21 +20,21 @@
#define LOG_PATH "/var/log/pbm.log"
#define ERR_PATH "/var/log/pbm.err"
+char* getProgName(char* path);
void redirectOutput();
-void validateArgs(int argc, char* argv[]);
-void clearShapFile(char *snapFileName);
-void getBootflag(char *bootFileName, char *bootflag);
-void setBootflag(char *bootFileName, char *bootflag);
+int validateArgs(int argc, char* argv[]);
+int clearShapFile(char *snapFileName);
+int getBootflag(char *bootFileName, char *bootflag);
+int setBootflag(char *bootFileName, char *bootflag);
int main(int argc, char* argv[])
{
- char *progName = strrchr( argv[0], '/' ) + 1; // extract program name from file path
- printf("the prog name is: %s\n", progName);
+ char *progName = getProgName(argv[0]);
FILE *logFile;
char *snapFileName;
char *bootFileName;
- int sleepTime;
- int testVoltage;
+ int sleepTime;
+ int ret;
char *bootflag = (char*) malloc( 2 );
time_t boottime;
@@ -52,7 +52,6 @@
snapFileName = argv[2]; // Arg 2 is snapshot file
bootFileName = argv[3]; // Arg 3 is boot flag file
sleepTime = atoi( argv[4] ); // Arg 4 is sleep interval
- testVoltage = atoi( argv[5] ); // Arg 5 is used for testing - 0 is use voltmeter reading - any other value overrides
if (logFile == 0)
syslog ( LOG_ERR, "Could not open log file: %s Continuing without logging", argv[1] );
@@ -64,13 +63,24 @@
fputs("\nYYYY-M-MDD,HH:MM:SS,mVolts,mAmps,Lux\n", logFile);
- test_interfacekit(progName, logFile, snapFileName, bootflag, boottime, sleepTime, testVoltage);// and off we go
+ ret = test_interfacekit(progName, logFile, snapFileName, bootflag, boottime, sleepTime);// and off we go
fclose ( logFile );
- syslog (LOG_NOTICE, "Program closing" );
+ ret == 0 ? syslog (LOG_NOTICE, "Program closing" ) : syslog (LOG_ERR, "Program closing with error code: %d", ret );
closelog ();
free ( bootflag );
- return 0;
+ return ret;
+}
+
+// extract program name from file path
+char* getProgName(char* path)
+{
+ char* ret = strrchr( path, '/' ); //returns null if '/' not found
+
+ if( ret == NULL )
+ return path;
+ else
+ return ret + 1;
}
// Close out stdout and point to a file cos if we are running as a demon we need to log this somewhere.
@@ -81,15 +91,17 @@
stdout = fopen(LOG_PATH, "a");
stderr = fopen(ERR_PATH, "a");
+
+ return;
}
-void validateArgs(int argc, char* argv[])
+int validateArgs(int argc, char* argv[])
{
int i;
- if ( argc != 6 ) // 6 args inc program name
+ if ( argc != 5 ) // 5 args inc program name
{
- syslog (LOG_ERR, "Program started with incorrect args. Usage: %s logfile snapshotfile bootflagfile sleeptime testvoltage", argv[0]);
+ syslog (LOG_ERR, "Error: Program started with incorrect args. Usage: %s logfile snapshotfile bootflagfile sleeptime", argv[0]);
exit(1);
}
@@ -97,23 +109,29 @@
{
if(argv[i] == NULL)
{
- syslog (LOG_ERR, "Program started with incorrect arg %d. Usage: %s logfile snapshotfile bootflagfile sleeptime testvoltage", i, argv[0]);
+ syslog (LOG_ERR, "Error: Program started with incorrect arg %d. Usage: %s logfile snapshotfile bootflagfile sleeptime", i, argv[0]);
exit(1);
}
}
- syslog (LOG_NOTICE, "Program started correctly with args: logfile=%s, snapshotfile=%s, bootflagfile=%s, sleeptime=%s, testvoltage=%s",
- argv[1], argv[2], argv[3], argv[4], argv[5]);
+ syslog (LOG_NOTICE, "Program started correctly with args: logfile=%s, snapshotfile=%s, bootflagfile=%s, sleeptime=%s",
+ argv[1], argv[2], argv[3], argv[4]);
+
+ return 0;
}
-void clearShapFile(char *snapFileName)
+int clearShapFile(char *snapFileName)
{
FILE *filesnap = fopen( snapFileName, "w" );
if (filesnap == 0)
+ {
syslog ( LOG_ERR, "Could not open snapshot file: %s", snapFileName );
-
- fclose( filesnap ); // cos we need to overwrite it every time we write to it
+ return -1;
+ }
+
+ fclose( filesnap ); // we need to overwrite it every time we write to it
+ return 0;
}
// The system reboots after a sleep period to ensure everyting is restarted properly
@@ -122,7 +140,7 @@
// in order to force a wakeup, so we dont want to go asleep straight away
// If the value is S then we have scheduled a wakeup. Anything else, or no file means manual
// override.
-void getBootflag(char *bootFileName, char *bootflag)
+int getBootflag(char *bootFileName, char *bootflag)
{
FILE *fileboot = fopen(bootFileName, "r");
@@ -130,13 +148,14 @@
{
syslog ( LOG_INFO, "Could not open bootflag file: %s - assuming manual override \"%s\"", bootFileName, BOOTFLAG_MAN_OVERRIDE );
sprintf(bootflag, BOOTFLAG_MAN_OVERRIDE);
+ return -1;
}
else // read from boot state file
{
fgets(bootflag, 2, fileboot); // bootflag should be 'S'
syslog ( LOG_INFO, "Opened bootflag file, boot flag is: %s", bootflag);
- if ( strcmp (bootflag, BOOTFLAG_NORMAL) == 0 )
+ if ( strncmp (bootflag, BOOTFLAG_NORMAL, 1) == 0 )
syslog ( LOG_INFO, "Bootflag is normal \"%s\"", BOOTFLAG_NORMAL );
else
{
@@ -145,22 +164,27 @@
}
fclose( fileboot );
- }
+ return 0;
+ }
}
// clear bootflag file - an S in this file means we are controlling the reboot (eg the user
// did not manually turn off and on the box
-void setBootflag(char *bootFileName, char *bootflag)
+int setBootflag(char *bootFileName, char *bootflag)
{
FILE *fileboot = fopen(bootFileName, "w");
if ( (fileboot == 0) )
+ {
syslog ( LOG_ERR, "Could not open bootflag file: %s", bootFileName );
+ return -1;
+ }
else
{
syslog ( LOG_INFO, "Writing bootflag \"%s\" to file: %s", bootflag, bootFileName );
fputs(bootflag, fileboot);
fclose( fileboot );
+ return 0;
}
}
--- a/stateManager.c Tue May 25 11:43:19 2010 +0100
+++ b/stateManager.c Wed May 26 11:36:44 2010 +0100
@@ -11,6 +11,8 @@
printf("%s\n", deviceptr);
printf("Version: %8d \nSerialNumber: %10d\n", version, sernum);
+
+ return;
}
void display_IFK_properties(CPhidgetInterfaceKitHandle phid)
@@ -25,6 +27,8 @@
display_generic_properties((CPhidgetHandle)phid);
printf("Sensors:%d Inputs:%d Outputs:%d\n", numSensors, numInputs, numOutputs);
+
+ return;
}
void display_LCD_properties(CPhidgetTextLCDHandle phid)
@@ -43,16 +47,18 @@
printf("# Rows: %d\n# Columns: %d\n", numRows, numColumns);
printf("Current Contrast Level: %d\nBacklight Status: %d\n", contrast, backlight);
printf("Cursor Status: %d\nCursor Blink Status: %d\n", cursor, cursor_blink);
+
+ return;
}
-void getVoltage(CPhidgetInterfaceKitHandle IFK, int voltIndex, int *Voltage)
+int getVoltage(CPhidgetInterfaceKitHandle IFK)
{
float fValue = 0, avgValue = 0;
int Value = 0, i = 0;
CPhidgetInterfaceKit_setRatiometric(IFK, PFALSE); // set to non-ratio
sleep(2); // wait at least 800ms to settle
- CPhidgetInterfaceKit_getSensorRawValue(IFK, voltIndex, &Value); // Voltmeter
+ CPhidgetInterfaceKit_getSensorRawValue(IFK, VOLTINDEX, &Value); // Voltmeter
// Calibration - this is zero point calibration.
Value += 5;
@@ -67,17 +73,17 @@
sleep (1);
}
- *Voltage = avgValue / 5;
+ return avgValue / 5;
}
-void getAmps(CPhidgetInterfaceKitHandle IFK, int ampIndex, int *Amps)
+int getAmps(CPhidgetInterfaceKitHandle IFK)
{
float fValue;
int Value;
CPhidgetInterfaceKit_setRatiometric (IFK, PTRUE); // set to ratiometric
sleep(2); // wait at least 800ms
- CPhidgetInterfaceKit_getSensorRawValue(IFK, ampIndex, &Value); // Ampmeter
+ CPhidgetInterfaceKit_getSensorRawValue(IFK, AMPINDEX, &Value); // Ampmeter
// Calibration
Value += 2;
@@ -85,25 +91,10 @@
fValue = Value;
fValue = (((fValue/4.095)/13.2)-37.8787)*1000; // multiply raw value to get amps
- *Amps = fValue * -1; // invert so + is charging and - is amp draw
+ return fValue * -1; // invert so + is charging and - is amp draw
}
-void getLux(CPhidgetInterfaceKitHandle IFK, int luxIndex, int *Lux)
-{
- int Value;
-
- CPhidgetInterfaceKit_setRatiometric(IFK, PFALSE); // set to non-ratio
- sleep(2); // wait at least 800ms
- CPhidgetInterfaceKit_getSensorValue(IFK, luxIndex, &Value); // Voltmeter
-
- // Calibration
- // None required
- // End calibration
-
- *Lux = Value;
-}
-
-void updateSnapshotfile(const char *snapFileName, int Voltage, int Amps, int Lux, const char *stateDesc)
+int updateSnapshotfile(const char *snapFileName, int Voltage, int Amps, int state)
{
// Snapshot file is a single line file, overwritten each time with the current and latest values
time_t rawtime;
@@ -114,38 +105,51 @@
time ( &rawtime );
timeinfo = localtime ( &rawtime );
- strftime (timeStr,80,"%Y-%m-%d,%H:%M:%S",timeinfo);
+ strftime (timeStr, 80, "%Y-%m-%d,%H:%M:%S", timeinfo);
FILE *snapfile = fopen( snapFileName, "w" ); // Open file (delete previous)
if (snapfile == 0)
+ {
syslog ( LOG_ERR, "Could not open snapshot file: %s", snapFileName );
+ return -1;
+ }
else
{
- sprintf( snapEntry, "%s,%d,%d,%d,%s\n", timeStr, Voltage, Amps, Lux, stateDesc );
+ snprintf( snapEntry, 200, "%s %dmV %dmA %s\n", timeStr, Voltage, Amps, getStateDesc(state) );
fputs( snapEntry, snapfile );
fflush( snapfile );
fclose( snapfile );
- }
+ return 0;
+ }
}
-void updateLogfile(FILE *logfile, int Voltage, int Amps, int Lux, const char *stateDesc, const char *progName)
+int updateLogfile(FILE *logfile, int Voltage, int Amps, int state, const char *progName)
{
- time_t rawtime;
- struct tm * timeinfo;
+ time_t rawTime;
+ struct tm * timeInfo;
char timeStr [80];
char hostName [80];
char logEntry [200];
- time ( &rawtime );
- timeinfo = localtime ( &rawtime );
+ time ( &rawTime );
+ timeInfo = localtime ( &rawTime );
- strftime (timeStr,80,"%Y-%m-%d,%H:%M:%S",timeinfo);
+ strftime (timeStr, 80, "%Y-%m-%d,%H:%M:%S", timeInfo);
gethostname(hostName, 80);
- sprintf( logEntry, "%s %s %s %d %d %d %s\n", timeStr, hostName, progName, Voltage, Amps, Lux, stateDesc );
- fputs( logEntry, logfile );
- fflush( logfile );
+ if (logfile == 0)
+ {
+ syslog ( LOG_ERR, "Error: log file not open" );
+ return -1;
+ }
+ else
+ {
+ snprintf( logEntry, 200, "%s %s %s %dmV %dmA %s\n", timeStr, hostName, progName, Voltage, Amps, getStateDesc(state) );
+ fputs( logEntry, logfile );
+ fflush( logfile );
+ return 0;
+ }
}
// getTimeString returns the time in a string in the form "HH:MM". It takes the current
@@ -160,21 +164,25 @@
rawtime = rawtime + wakeTimeSec;
timeinfo = localtime ( &rawtime );
- //strftime (wakeTimeStr,80,"%Y-%m-%d,%H:%M:%S",timeinfo);
- strftime (wakeTimeStr,20,"%H:%M",timeinfo);
+ strftime (wakeTimeStr, 20, "%H:%M", timeinfo);
+ return;
}
void getnewState(int Voltage, const char *bootflag, int *newState)
{
- char timeStr[20];
int localState = 0;
-
- getTimeString (0, timeStr ); // Use this to return current time as a string
+
+ time_t rawtime;
+ struct tm * timeinfo;
+
+ time ( &rawtime );
+ timeinfo = localtime ( &rawtime );
+ mktime ( timeinfo );
- if ( strcmp(bootflag, BOOTFLAG_NORMAL) != 0 ) // Anything except an S will mean manual start
+ if ( strncmp(bootflag, BOOTFLAG_NORMAL, 1) != 0 ) // Anything except an S will mean manual start
localState = OVERRIDE;
-
- else if ( strcmp(timeStr, SLEEPTIMESTART) > 0 || strcmp(timeStr, SLEEPTIMEEND) < 0 )
+
+ else if (timeinfo->tm_hour < SLEEPHOUREND || timeinfo->tm_hour >= SLEEPHOURSTART )
localState = SLEEP;
else if ( Voltage >= GREEN)
@@ -188,21 +196,27 @@
if ( *newState != localState )
- syslog ( LOG_INFO, "Moving from state %d to state %d, bootflag=%s, voltage=%d", *newState, localState, bootflag, Voltage );
+ syslog ( LOG_INFO, "Moving from state %s to state %s, bootflag=%s, voltage=%d",
+ getStateDesc(*newState), getStateDesc(localState), bootflag, Voltage );
else
- syslog ( LOG_DEBUG, "Remaining in same state %d, bootflag=%s, voltage=%d", localState, bootflag, Voltage );
+ syslog ( LOG_DEBUG, "Remaining in same state %s, bootflag=%s, voltage=%d",
+ getStateDesc(localState), bootflag, Voltage );
- *newState = localState;
+ *newState = localState;
+ return;
}
void closeCPhidget(CPhidgetHandle handle)
{
CPhidget_close(handle);
CPhidget_delete(handle);
+ return;
}
//if the change in voltage is greater than the max allowable change in the given time step, a spike has occured
-void testVoltageSpike(int *prevVoltage, time_t *prevVoltageTime, int *Voltage, time_t *VoltageTime)
+//if spike: set voltage to prevVoltage and return 1
+//otherwise: update prevVoltage to voltage and return 0
+int testVoltageSpike(int *prevVoltage, time_t *prevVoltageTime, int *Voltage, time_t *VoltageTime)
{
const int timeStep = 120;
const int maxDeltaVoltage = 1000;
@@ -212,40 +226,59 @@
int deltaTime = (*VoltageTime) - (*prevVoltageTime);
- if( deltaVoltage > maxDeltaVoltage && deltaTime < timeStep )
- syslog ( LOG_ERR, "Voltage spike from: %dv to: %dv in %d seconds", *prevVoltage, *Voltage, deltaTime );
-
- prevVoltage = Voltage;
- prevVoltageTime = VoltageTime;
+ if( deltaVoltage > maxDeltaVoltage && deltaTime < timeStep )
+ {
+ syslog ( LOG_ERR, "Voltage spike from: %dv to: %dv in %d seconds. Using prevVoltage: %d", *prevVoltage, *Voltage, deltaTime, *prevVoltage );
+ *Voltage=*prevVoltage;
+ *VoltageTime=*prevVoltageTime;
+ return 1;
+ }
+ else
+ {
+ *prevVoltage = *Voltage;
+ *prevVoltageTime = *VoltageTime;
+ return 0;
+ }
}
-void test_interfacekit(const char *progName, FILE *logfile, const char *snapFileName, char * bootflag, time_t boottime, int sleepTime, int testVoltage)
+char* getStateDesc(int state)
+{
+ if ( state == INIT )
+ return "Initialise";
+ else if ( state == STBYLONG )
+ return "STBYLONG";
+ else if ( state == STBYSHORT )
+ return "STBYSHORT";
+ else if ( state == OVERRIDE )
+ return "OVERRIDE";
+ else if ( state == SLEEP )
+ return "SLEEP";
+ else if ( state == UP )
+ return "UP";
+ else
+ return "INVALID STATE";
+}
+
+int test_interfacekit(const char *progName, FILE *logfile, const char *snapFileName, char * bootflag, time_t boottime, int sleepTime)
{
syslog ( LOG_DEBUG,
- "test_interfacekit called with args: progName=%s, logfile=..., snapFileName=%s, bootflag=%s, boottime=..., sleepTime=%d, testVoltage=%d",
- progName, snapFileName, bootflag, sleepTime, testVoltage);
+ "test_interfacekit called with args: progName=%s, logfile=..., snapFileName=%s, bootflag=%s, boottime=..., sleepTime=%d",
+ progName, snapFileName, bootflag, sleepTime);
int err;
int lightTrigger;
- int prevVoltage = 0, Voltage = 0, Amps = 0, Lux = 0;
+ int prevVoltage = 0, Voltage = 0, Amps = 0;
int tts;
int newState = 0;
int ret;
time_t timenow, prevVoltageTime, VoltageTime;
- char buffer1 [80];
- char buffer2 [80];
+ const int bufferSize = 80;
+ char buffer0 [bufferSize];
+ char buffer1 [bufferSize];
char *wakeTimeStr = malloc(20);
- char *stateDesc[10];
- stateDesc[0] = "Initialise";
- stateDesc[1] = "STBYLONG";
- stateDesc[2] = "STBYSHORT";
- stateDesc[3] = "OVERRIDE";
- stateDesc[4] = "SLEEP";
- stateDesc[9] = "UP";
-
CPhidgetInterfaceKitHandle IFK = 0;
CPhidgetInterfaceKit_create(&IFK);
@@ -261,20 +294,6 @@
CPhidget_open((CPhidgetHandle)IFK, -1);
CPhidget_open((CPhidgetHandle)LCD, -1);
- //wait 5 seconds for attachment of the Interface Kit
- syslog ( LOG_DEBUG, "Waiting %d seconds for IFK to be attached...", IFK_WAIT_TIME/1000 );
- if( (err = CPhidget_waitForAttachment((CPhidgetHandle)IFK, IFK_WAIT_TIME)) != EPHIDGET_OK )
- {
- const char *errStr;
- CPhidget_getErrorDescription( err, &errStr );
- syslog ( LOG_ERR, "Error waiting for IFK attachment: %d %s", err, errStr );
-
- closeCPhidget((CPhidgetHandle)IFK);
- closeCPhidget((CPhidgetHandle)LCD);
- return;
- }
- //display_IFK_properties(IFK); //Display the properties of the attached Interface Kit device
-
//get the program to wait 10 seconds for an TextLCD device to be attached
syslog ( LOG_DEBUG, "Waiting %d seconds for LCD to be attached...", LCD_WAIT_TIME/1000 );
if( (err = CPhidget_waitForAttachment((CPhidgetHandle)LCD, LCD_WAIT_TIME)) != EPHIDGET_OK )
@@ -285,74 +304,95 @@
closeCPhidget((CPhidgetHandle)IFK);
closeCPhidget((CPhidgetHandle)LCD);
- return;
+ return -1;
}
//display_LCD_properties(LCD); //Display the properties of the attached Text LCD device
+
+ //Set the LCD startup screen
+ snprintf (buffer0, bufferSize, "Welcome to N4C");
+ snprintf (buffer1, bufferSize, "Loading...");
+ CPhidgetTextLCD_setDisplayString (LCD, 0, buffer0);
+ CPhidgetTextLCD_setDisplayString (LCD, 1, buffer1);
+
+ //wait 5 seconds for attachment of the Interface Kit
+ syslog ( LOG_DEBUG, "Waiting %d seconds for IFK to be attached...", IFK_WAIT_TIME/1000 );
+ if( (err = CPhidget_waitForAttachment((CPhidgetHandle)IFK, IFK_WAIT_TIME)) != EPHIDGET_OK )
+ {
+ const char *errStr;
+ CPhidget_getErrorDescription( err, &errStr );
+ syslog ( LOG_ERR, "Error waiting for IFK attachment: %d %s", err, errStr );
+
+ closeCPhidget((CPhidgetHandle)IFK);
+ closeCPhidget((CPhidgetHandle)LCD);
+ return -1;
+ }
+ //display_IFK_properties(IFK); //Display the properties of the attached Interface Kit device
+
CPhidgetInterfaceKit_getSensorChangeTrigger (IFK, 0, &lightTrigger);
/******************************************************************************************/
prevVoltageTime = boottime; //initialise previous voltage time
- getVoltage(IFK, VOLTINDEX, &prevVoltage); //ininitalise voltage
+ prevVoltage = getVoltage(IFK); //ininitalise voltage
while(1)
- {
- if (testVoltage == 0)
- {
- time (&VoltageTime);
- getVoltage(IFK, VOLTINDEX, &Voltage);
- testVoltageSpike(&prevVoltage, &prevVoltageTime, &Voltage, &VoltageTime);
- }
- else
- Voltage = testVoltage;
+ {
+ time (&VoltageTime);
+ Voltage = getVoltage(IFK);
+ testVoltageSpike(&prevVoltage, &prevVoltageTime, &Voltage, &VoltageTime);
- getAmps(IFK, AMPINDEX, &Amps);
- getLux(IFK, LUXINDEX, &Lux);
+ Amps = getAmps(IFK);
getnewState (Voltage, bootflag, &newState);
- updateLogfile (logfile, Voltage, Amps, Lux, stateDesc[newState], progName);
- updateSnapshotfile (snapFileName, Voltage, Amps, Lux, stateDesc[newState]);
+ updateLogfile (logfile, Voltage, Amps, newState, progName);
+ updateSnapshotfile (snapFileName, Voltage, Amps, newState);
// Now Update the LCD
getTimeString (0, wakeTimeStr);
- sprintf (buffer1, "V:%4d A:%-5d %s", Voltage, Amps,wakeTimeStr);
- sprintf (buffer2, "%-s", stateDesc[newState]);
+ snprintf (buffer0, bufferSize, "V:%4d A:%-5d %s", Voltage, Amps,wakeTimeStr);
+ snprintf (buffer1, bufferSize, "%-s", getStateDesc(newState));
- CPhidgetTextLCD_setDisplayString (LCD, 0, buffer1);
- CPhidgetTextLCD_setDisplayString (LCD, 1, buffer2);
+ CPhidgetTextLCD_setDisplayString (LCD, 0, buffer0);
+ CPhidgetTextLCD_setDisplayString (LCD, 1, buffer1);
// Do what the state demands
- syslog ( LOG_DEBUG, "Begin main while loop: State is %s", stateDesc[newState] );
+ syslog ( LOG_DEBUG, "Begin main while loop: State is %s", getStateDesc(newState) );
if(newState == STBYLONG)
{
// sleep for 3 hours, then wake, reboot
getTimeString (STBYLONG_TIME, wakeTimeStr);
- sprintf (buffer2, "%-s Back@%-s", stateDesc[newState], wakeTimeStr);
+ snprintf (buffer1, bufferSize, "%-s Back@%-s", getStateDesc(newState), wakeTimeStr);
- CPhidgetTextLCD_setDisplayString (LCD, 1, buffer2);
+ CPhidgetTextLCD_setDisplayString (LCD, 1, buffer1);
- ret = system ("/etc/stbylong.sh");
- if (!ret)
- syslog ( LOG_NOTICE, "STBYLONG executing, back at: %s", wakeTimeStr);
- else
- syslog ( LOG_ERR, "STBYLONG failed to run. Error: %d", ret);
+ syslog ( LOG_NOTICE, "Going to STBYLONG, back at: %s", wakeTimeStr);
+
+ if ( (ret = system ("/etc/stbylong.sh")) != 0 );
+ syslog ( LOG_ERR, "STBYLONG failed to run. Error: %d", ret);
+
+ closeCPhidget((CPhidgetHandle)IFK);
+ closeCPhidget((CPhidgetHandle)LCD);
+ return 0;
}
else if(newState == STBYSHORT)
{
// sleep for 30 mins, then wake, reboot
getTimeString (STBYSHORT_TIME, wakeTimeStr);
- sprintf (buffer2, "%-s Wake@%-s", stateDesc[newState], wakeTimeStr);
+ snprintf (buffer1, bufferSize, "%-s Wake@%-s", getStateDesc(newState), wakeTimeStr);
- CPhidgetTextLCD_setDisplayString (LCD, 1, buffer2);
+ CPhidgetTextLCD_setDisplayString (LCD, 1, buffer1);
- ret = system ("/etc/stbyshort.sh");
- if (!ret)
- syslog ( LOG_NOTICE, "STBYSHORT executing, back at: %s", wakeTimeStr);
- else
- syslog ( LOG_ERR, "STBYSHORT failed to run. Error: %d", ret);
+ syslog ( LOG_NOTICE, "Going to STBYSHORT, back at: %s", wakeTimeStr);
+
+ if ( (ret = system ("/etc/stbyshort.sh")) != 0)
+ syslog ( LOG_ERR, "STBYSHORT failed to run. Error: %d", ret);
+
+ closeCPhidget((CPhidgetHandle)IFK);
+ closeCPhidget((CPhidgetHandle)LCD);
+ return 0;
}
else if(newState == OVERRIDE)
{
@@ -361,45 +401,31 @@
tts = OVERRIDE_TIME - (timenow - boottime);
syslog ( LOG_DEBUG, "In OVERRIDE %d sec to sleep", tts );
- sprintf (buffer2, "%-s Time:%dmin", stateDesc[newState], tts/60);
-
- CPhidgetTextLCD_setDisplayString (LCD, 1, buffer2);
+ snprintf (buffer1, bufferSize, "%-s Time:%dmin", getStateDesc(newState), tts/60);
+ CPhidgetTextLCD_setDisplayString (LCD, 1, buffer1);
if (timenow > boottime + OVERRIDE_TIME)
{
syslog ( LOG_INFO, "OVERRIDE %d mins expired, switching to normal mode", OVERRIDE_TIME/60 );
- sprintf(bootflag, BOOTFLAG_NORMAL);
+ snprintf(bootflag, 2, BOOTFLAG_NORMAL);
}
- /*
- else
- {
- struct tm * timeinfo;
- char timenowStr [80];
- char boottimeStr [80];
-
- timeinfo = localtime ( &timenow );
- strftime (timenowStr,80,"%Y-%m-%d,%H:%M:%S",timeinfo);
-
- timeinfo = localtime ( &boottime );
- strftime (boottimeStr,80,"%Y-%m-%d,%H:%M:%S",timeinfo);
-
- printf ("OVERRIDE - boottime: %s timenow: %s override time: %d\n", boottimeStr, timenowStr, OVERRIDE_TIME);
- }
- */
}
else if(newState == SLEEP)
{
// We're running during night time - go to sleep
- wakeTimeStr = SLEEPTIMEEND;
- sprintf (buffer2, "%-s Wake@%-s", stateDesc[newState], wakeTimeStr);
+ wakeTimeStr = SLEEPTIMEENDSTR;
+ snprintf (buffer1, bufferSize, "%-s Wake@%-s", getStateDesc(newState), wakeTimeStr);
- CPhidgetTextLCD_setDisplayString (LCD, 1, buffer2);
+ CPhidgetTextLCD_setDisplayString (LCD, 1, buffer1);
- ret = system ("/etc/sleeprtc.sh");
- if (!ret)
- syslog ( LOG_NOTICE, "SLEEPRTC executing, back at: %s", wakeTimeStr);
- else
+ syslog ( LOG_NOTICE, "Going to SLEEPRTC, back at: %s", wakeTimeStr);
+
+ if ( (ret = system ("/etc/sleeprtc.sh")) != 0 )
syslog ( LOG_ERR, "SLEEPRTC failed to run. Error: %d", ret);
+
+ closeCPhidget((CPhidgetHandle)IFK);
+ closeCPhidget((CPhidgetHandle)LCD);
+ return 0;
}
else if(newState == UP)
{
@@ -412,9 +438,6 @@
fflush (stdout);
sleep (sleepTime);
- }//end while
-
- closeCPhidget((CPhidgetHandle)IFK);
- closeCPhidget((CPhidgetHandle)LCD);
+ }//end while
}