ioPAC RTU Controllers
C/C++ Sample Code Programming Guide
Functions
software_watchdog.c File Reference

Software Watchdog More...

#include <libmoxa_rtu.h>

Functions

int main (int argc, char *argv[])
 

Detailed Description

Software Watchdog

Date
04-10-2013
Author
Eddy Kao
Version
V1.0
Introduction:
Software Watchdog is used to guarantee a critical section which must be executed at least every N seconds.
If the critical section is not executed after N seconds, the watchdog will restart the system.
Example:
1. Using default: ./software_watchdog
2. Setting Ack Time: ./software_watchdog -t5000
Default:
Ack Time = 10000 ms (10 seconds)
 SOFTWARE_WATCHDOG_MIN_TIME 
 SOFTWARE_WATCHDOG_MAX_TIME 
Help:
root@Moxa:/tmp#./software_watchdog -h
Software Watchdog.

Usage: ./software_watchdog [OPTIONS]

Options:
        -t       Ack Time [50-60000]. Default Ack Time = 10000 ms

Library:
SoftwareWatchdog APIs

Function Documentation

int main ( int  argc,
char *  argv[] 
)
/*******************************************************************************
* Copyright Moxa Inc.
*
* Software Watchdog
*
* Date Author Comment
* 04-10-2013 Eddy Kao Created.
******************************************************************************/
#include <libmoxa_rtu.h>
int main(int argc, char *argv[])
{
int retval = 0;
UINT32 rc = 0;
UINT32 ackTime = 10 * 1000; // 10 seconds
struct swtd_setting setting;
while((retval = getopt(argc, argv, "ht:")) != -1)
{
switch(retval)
{
case 't':
ackTime = atoi(optarg);
if(ackTime < SOFTWARE_WATCHDOG_MIN_TIME || ackTime > SOFTWARE_WATCHDOG_MAX_TIME)
{
printf("Invalid Ack Time\r\n");
exit(1);
}
break;
case '?':
case 'h':
default:
printf("Software Watchdog.\n\n");
printf("Usage: ./software_watchdog [OPTIONS]\n\n");
printf("Options:\n");
printf("\t%-8s Ack Time [%d-%d]. Default Ack Time = %d ms\n", "-t", SOFTWARE_WATCHDOG_MIN_TIME, SOFTWARE_WATCHDOG_MAX_TIME, ackTime);
printf("\n");
return 0;
}
}
rc = MX_RTU_SWTD_Enable(ackTime);
if(rc != MISC_ERR_OK)
{
printf("MX_RTU_SWTD_Enable(%d), return code = %d\r\n", ackTime, rc);
exit(1);
}
memset(&setting, 0, sizeof(struct swtd_setting));
rc = MX_RTU_SWTD_Get_Setting(&setting);
if(rc != MISC_ERR_OK)
{
printf("MX_RTU_SWTD_Get_Setting(&setting), return code = %d\r\n", rc);
exit(1);
}
else
{
printf("Software Watchdog: enable = %d, time = %d ms\r\n", setting.enable, setting.time);
}
while(1)
{
//This is a critical section, and it should be executed at least every N seconds.
//do something
//If the critical section is not executed over N seconds, watchdog will restart system.
if(rc != MISC_ERR_OK)
{
printf("MX_RTU_SWTD_Ack(), return code = %d\r\n", rc);
exit(1);
}
}
return 0;
}