host.c 5.22 KB
/**************************************************************************
 *                                                                        *
 *               Copyright (C) 1995, Silicon Graphics, Inc.               *
 *                                                                        *
 *  These coded instructions, statements, and computer programs  contain  *
 *  unpublished  proprietary  information of Silicon Graphics, Inc., and  *
 *  are protected by Federal copyright  law.  They  may not be disclosed  *
 *  to  third  parties  or copied or duplicated in any form, in whole or  *
 *  in part, without the prior written consent of Silicon Graphics, Inc.  *
 *                                                                        *
 *************************************************************************/

/*---------------------------------------------------------------------*
        Copyright (C) 1997 Nintendo. (Originated by SGI)
        
        $RCSfile: host.c,v $
        $Revision: 1.1.1.1 $
        $Date: 2002/05/02 03:27:17 $
 *---------------------------------------------------------------------*/

/*
 * This demo shows how a typical tool will use the hostio routines. There
 * are two parts, this host indy application and the game application which
 * is in game.c. 
 *
 * The host application sends a command block which tells what to do, and
 * in the case of data transfer tells how many bytes to transfer. After the
 * command block has been sent, the game either prints something, or the
 * host and game transfer data.
 *
 * Before doing anything, we open the DEV_U64_DATA device, then fork a process
 * that will launch gload. Then prompt the user to tell us what to do. Because
 * the shell is shared by this app, and gload, printf's can come out of order. 
 * When we do a transfer in this app, we are just sending garbage buffers, but 
 * in a real app, you would send your data.
 *
 */



#include <stdlib.h>
#include <sys/u64gio.h> 
#include <sys/wait.h>
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <stdio.h>
#include <ultrahost.h>
#include <unistd.h>
#include "hostio.h"

int  fd;

void HandlePrint(void);
void HandleSend(void);
void HandleGet(void);

char  ReadBuf[MY_MAX_BLOCK_SIZE];
char  SendBuf[MY_MAX_BLOCK_SIZE];

int main(int argc, char **argv)
{
    pid_t	pid;
    int		done = 0;
    char        cmdStr[64];
    int		status;
    

    if ((fd = uhOpenGame(DEV_U64_DATA)) == -1) 
    {
	fprintf(stderr, "host: Unable to open %s : %s\n", 
	DEV_U64_DATA, sys_errlist[errno]);
    	return(1);
    }
    if ((pid = fork()) == -1) 
    {
	fprintf(stderr, "host: fork: %s\n", sys_errlist[errno]);
	return(-1);
    } 
    else if (pid == 0) 
    {
	(void)execl("/usr/sbin/gload", "/usr/sbin/gload", 0);
	fprintf(stderr, "host: execl(\"gload\") failed\n");
	return(-1);
    }


    while(!done)
    {
	printf("host: Type S for send, G for get, P for print, Q for quit\n");
	
	scanf("%s",cmdStr);
	
	switch(cmdStr[0])
	{
	    case 'P':
	    case 'p':
		HandlePrint();
		break;
	    case 'S':
	    case 's':
		HandleSend();
		break;
	    case 'G':
	    case 'g':
		HandleGet();
		break;
	    case 'Q':
	    case 'q':
		done = 1;
		break;
	    default:
		printf("host: Sorry don't understand: %s\n",cmdStr);
	}
    }

    if (kill(pid, SIGTERM) == -1) {
      fprintf(stderr, "host: kill(SIGTERM): %s\n", sys_errlist[errno]);
      return(-1);
    }
    if (waitpid(pid, &status, WUNTRACED) == -1) {
      fprintf(stderr, "host: waitpid: %s\n", sys_errlist[errno]);
      return(-1);
    }

    return(0);
}

void HandlePrint(void)
{
    cmdBlk      cb;

    cb.command = CMD_PRINT_SOMETHING;

    if (uhWriteGame(fd, &cb, sizeof(cmdBlk)) == -1) 
    {
	fprintf(stderr, "host: uhWriteGame: %s\n", sys_errlist[errno]);
	return;
    }
    
}

void HandleSend(void)
{
    cmdBlk      cb;
    char        sizeStr[64];
    int         blkSize;

    printf("host: Send how many bytes?  (1 - %d) ", MY_MAX_BLOCK_SIZE);

    scanf("%s",sizeStr);
    blkSize = atoi(sizeStr);

    if(blkSize < 1 || blkSize > MY_MAX_BLOCK_SIZE)
    {
	printf("host: Sorry, block size is out of range %d bytes\n");
	blkSize++;
	return;
    }
    
    cb.command = CMD_WRITE_GAME;
    cb.blkSize = blkSize;

    if (uhWriteGame(fd, &cb, sizeof(cmdBlk)) == -1) 
    {
	fprintf(stderr, "host: uhWriteGame: %s\n", sys_errlist[errno]);
	return;
    }
    if (uhWriteGame(fd, SendBuf, blkSize) == -1) 
    {
	fprintf(stderr, "host: uhWriteGame: %s\n", sys_errlist[errno]);
	return;
    }

    printf("host: Sent %d bytes\n",blkSize);

}

void HandleGet(void)
{
    cmdBlk      cb;
    char        sizeStr[64];
    int         blkSize;

    printf("host: Get how many bytes?  (1 - %d) ", MY_MAX_BLOCK_SIZE);

    scanf("%s",sizeStr);
    blkSize = atoi(sizeStr);

    if(blkSize < 1 || blkSize > MY_MAX_BLOCK_SIZE)
    {
	printf("host: Sorry, block size is out of range %d bytes\n");
	blkSize++;
	return;
    }
    
    cb.command = CMD_READ_GAME;
    cb.blkSize = blkSize;

    if (uhWriteGame(fd, &cb, sizeof(cmdBlk)) == -1) 
    {
	fprintf(stderr, "host: uhWriteGame: %s\n", sys_errlist[errno]);
	return;
    }
    if (uhReadGame(fd, ReadBuf, blkSize) == -1) 
    {
	fprintf(stderr, "host: uhReadGame: %s\n", sys_errlist[errno]);
	return;
    }

    printf("host: Got %d bytes\n",blkSize);

}