/* $Id: hpsremres.c,v 2.4 91/09/06 10:27:32 annette Exp $ */
/****************************************************************/
/* hpsremres.c:  HPS-6816 Remote Reset							*/
/****************************************************************/
/*																*/
/*                 Confidential Information                     */
/*      Limited Distribution to Authorized Persons Only         */
/*     Created 1986 and Protected as an Unpublished Work        */
/*          Under the U.S. Copyright Act of 1976.               */
/*         Copyright (c) 1986, Systech Corporation          	*/
/*                  All Rights Reserved                         */
/*																*/
/*	THIS SOFTWARE IS LICENSED BY AND IS THE	CONFIDENTIAL AND 	*/
/*	PROPRIETARY PROPERTY OF SYSTECH COMPUTER CORPORATION AND 	*/
/*	MAY NOT, WITHOUT THE PRIOR WRITTEN CONSENT OF SYSTECH 		*/
/*	COMPUTER CORPORATION, BE REPRODUCED, USED OR DISCLOSED 		*/
/*	EXCEPT IN ACCORDANCE WITH A SYSTECH COMPUTER CORPORATION 	*/
/*	SOFTWARE LICENSE AGREEMENT.									*/
/****************************************************************/
/*																*/
/*   This program sends a remote reset command through the		*/
/*   diagnostic application.									*/
/*																*/
/****************************************************************/

#include "signal.h"
#include "stdio.h"
#include "hps.types.h"
#include "hps.iocb.h"
#include "hps.ioctl.h"
#include "hps.onb.h"
#include "hps.net.h"
#include "hps.diag.h"

#define BUFF_SIZE	512

#define NUMBER		0		/* Default number of blocks */
#define TIME		30		/* run for 30 seconds */

extern int errno;

char  hps_intel		= 0;				/* host byte ordering	*/
long clock = 0;
long time = TIME;
int number = NUMBER;
static long mailbox = 0;

main(argc, argv)
int argc;
char **argv;
{
	int catcher();

	char c, *key, *arg, *devname, *tname;
	short cd;
	int fd, i, error, station;

#	ifdef BSD
	static int sigs[] =
	{
		SIGHUP,	SIGINT,	SIGQUIT, SIGILL, SIGTRAP, SIGIOT,
		SIGEMT,	SIGFPE,	SIGBUS, SIGSEGV, SIGSYS,
		SIGPIPE, SIGALRM, SIGTERM, 0
	};
#	else
	static int sigs[] =
	{
		SIGHUP,	SIGINT,	SIGQUIT, SIGILL, SIGTRAP, SIGIOT,
		SIGEMT,	SIGFPE,	SIGKILL, SIGBUS, SIGSEGV, SIGSYS,
		SIGPIPE, SIGALRM, SIGTERM, 0
	};
#	endif

	short tmp;

	tmp = 0;
	hps_intel = *(char *)&tmp;

	devname	= RAW_DEVNAME;
	error	= 0;
	station	= 0;


	/* Set switches according to command line options */
	if (argc >= 2)
	{
		key = argv[1];
		i = argc - 2;
		while (*key)
		{
			switch (*key++)
			{
				case 'a':	/* Get station address */
					if (i == 0)
					{
						fprintf(stderr,
							"%s: Keyletter \"a\" needs an argument\n",
							argv[0]);
						usage(argv[0]);
						error = 1;
					}
					else
					{
						station = atoi(argv[argc - i]);
						if ( station > 255)
						{
							fprintf(stderr,
							"%s: Station address %d out of range.\n",
							argv[0], station);
							usage(argv[0]);
							error = 1;
						}
						i--;
					}
					break;
					
				case 'd': /* Pick up device name */
					if (i == 0)
					{
						fprintf(stderr,
							"%s: Keyletter \"d\" needs an argument\n",
							argv[0]);
						usage(argv[0]);
						error = 1;
					}
					else
					{
						devname = argv[argc - i];
						i--;
					}
					break;

				default:
					fprintf(stderr,
						"%s: \"%c\" bad keyletter\n", argv[0], c);
					usage(argv[0]);
					error = 1;
					break;
			}
		}
	}
	if (error)
		exit(1);

	if (station == 0)
	{
		fprintf(stderr, "%s: Need station address (keyletter \"a\")\n",
			argv[0]);
		usage(argv[0]);
		exit(1);
	}

	/* Arrange to catch all signals, so we can teminate gracefully. */
	for (i=0; sigs[i] != 0; i++)
	{
		if ((int)signal(sigs[i], catcher) < 0)
		{
			fprintf(stderr, "%s: signal() failed.\n", argv[0]);
			if (errno)
				perror(argv[2]);
			exit(1);
		}
	}

	/* Open HPS raw device */
#ifdef HPS_STREAMS 
	if ((fd = open(devname, 2)) < 0)
#else
	if ((fd = open(devname, 0)) < 0)
#endif
	{
		if (errno)
			perror(argv[0]);
		fprintf(stderr, "%s: Cannot open device %s\n", argv[0], devname);
		exit(1);
	}

	if ( station < 0)
	{
		for (station *= -1; station > 0; station--)
		{

	/* Open on-board application--this will send the reset command */
			if ((cd = hps_open(fd, RRESET_DEV, station, &mailbox)) < 0)
			{
				fprintf(stderr, "%s: Cannot open diagnostic task.\n", argv[0]);

				if (errno)
					perror(argv[0]);
				else
					errtab(mailbox >> 16, argv[0]);
			}
		}

	}
	else
	{

		if ((cd = hps_open(fd, RRESET_DEV, station, &mailbox)) < 0)
		{
			fprintf(stderr, "%s: Cannot open diagnostic task.\n", argv[0]);
			if (errno)
				perror(argv[0]);
			else
				errtab(mailbox >> 16, argv[0]);
			exit( -1 );
		}
	}



	/* Close device */
	if (close(fd) < 0)
	{
		fprintf(stderr, "%s: Cannot close device\n", argv[0]);
		if (errno)
			perror(argv[0]);
		exit(1);
	}
	exit(0);
}


/* This routine catches all the signals */
catcher(sig)
int sig;
{
	switch (sig)
	{
		case SIGALRM:
			clock++;
			if (time)
				time--;

			signal(SIGALRM, catcher);
			alarm(1);
			break;

		default:
			time = number = 0;	/* Cease */
			signal(sig, catcher);
			break;
	}
}

usage(name)

char *name;

{
	fprintf (stderr, "%s: usage: %s a[d] <sta_addr> [<devname>]\n", name, name);
}

/*
 * MODIFICATION HISTORY:
 *
 * $Log:	hpsremres.c,v $
 * Revision 1.1.2.1  92/06/24  13:53:39  marker
 * Branch RISCOS_5_01_2_FIXES off of trunk.
 * 
 * Revision 2.4  91/09/06  10:27:32  annette
 * Changed mailbox to &mailbox.
 * INTERNAL
 * 
 * Revision 2.3  91/09/03  17:38:40  annette
 * Changed error message handling.
 * INTERNAL
 * 
 * Revision 2.2  91/01/10  17:23:07  dave
 * Modify header; add footer.
 * COSMETIC
 * 
 */
