/* Watchdog time test.

   Author: Moki Matsushima

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software Foundation,
   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */

/* File level history (record changes for this file here.)

   v 0.0.0  13 Feb 2019 by Winson
     create, blah blah... 
     This demo support LX-2031 PAC.
     Support CPU watchdog.
*/

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <getopt.h>
#include "msw.h"
#define iTalkVersion	15

void usage();
char* const short_options = "hvs";
struct option long_options[] = {
        {"help", 0, NULL, 'h'},
        {"version", 0, NULL, 'v'},
        {"support", 0, NULL, 's'},
        {0, 0, 0, 0},
};

int main(int argc, char **argv)
{
	unsigned int option, msecond = 10000;	//default wdt timer setting 10s
	int tmp, c;


	while((c = getopt_long (argc, argv, short_options, long_options, NULL)) != -1)
        {
                switch (c)
                {
                        case 'h':
                                usage();
                                return 0;
                        case 'v':
                                printf("SDK version = %s \n",GetSDKversion());
                                return 0;
                        case 's':
                                printf("Support LX-2031\n");
                                return 0;
                        default:
                                printf("Try `wdt -h' for more information.\n");
                                return 0;
                        break;
                }
        }

	if(argc == 2)
		msecond = atoi(argv[1]);

	tmp = EnableSysWDT(msecond);	/* Enable watchdog timer */

	if(tmp)
	{
		switch(tmp)
		{
			case ComPortNotOpen:
				printf("Open Com port fail!\n");
				break;
			case ExceedInputRange:
				printf("Timer setting is out of range (1~510 seconds)\n");
				break;
			default:
				printf("Driver module does not loaded\n");
				break;
		}

		return FAILURE;
	}

	sync();
	printf("Set time out %d seconds\n",msecond / 1000);
	printf("Press Enter to kick watchdog (ESC to Disable)\n");

	while (getchar()==10){
		printf("Kick watchdog.\n");
		ReloadSysWDT();      	/* Refresh watchdog timer */
		sync();
	}
	
	printf("Disable watchdog timer\n");
	DisableSysWDT();	/* Disable watchdog timer */
	return SUCCESS;
}

void usage()
{
        printf(".\n");
        printf("-h,     display this help and exit\n");
        printf("-v,     output SDK version and exit\n");
        printf("-s,     support LinPAC platform\n");
	printf(" 10000,	set WDT timer to 10000 msecond\n");
}

