/**
*	watchdogService.c
*	
*	@Author: E. Martin
*	@Copyright: MBARI 2015
*
*	@Summary: This simple executable with start and pet
*	the watchdog of a Beaglebone Black, and once started,
*	will intentionally abandon the FD associated with the
*	watchdog, so that if the service gets killed, the 
*	will reboot. In order to prevent creating a service 
*	that is unrecoverable, a delay must be incorporated
*   prior to watchdog kickoff
*
*   
*
**/

#include <stdio.h>
#include <unistd.h>
#include <syslog.h>

//FOR OPEN()
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>


int main (int argc, char const *argv[])
{ 
	//TODO Check for root user permission
	
	
	printf("Initializing watchdog program.\n");
	
	//// decrease the priority of the process to ensure success
	nice(20);
	
	//// Wait to start the watchdog
	sleep(1);
	
	printf("Opening Watchdog\n");
	
	int fd = open("/dev/watchdog",O_WRONLY);
	
	if(fd==-1)
	{
		printf("ERROR: File not opened");
		return(-1);
	}
	
	for (;;) {
		printf("Pet\n");
		//fflush(fd);
		//fwrite("a",sizeof(char),1,fd);
		write(fd,"\n",1);
		printf("Wag\n");
		sleep(120);
	}
	
	return 0;
}
