host.c
5.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/**************************************************************************
* *
* 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);
}