source: xtideuniversalbios/trunk/Serial_Server/win32/Win32.cpp@ 216

Last change on this file since 216 was 215, checked in by gregli@…, 13 years ago

More minor improvements to the serial server, in particular improved timeout recovery.

File size: 5.2 KB
RevLine 
[209]1//======================================================================
2//
3// Project: XTIDE Universal BIOS, Serial Port Server
4//
5// File: Win32.cpp - Microsoft Windows 32-bit application
6//
7// This file contains the entry point for the Win32 version of the server.
8// It also handles log reporting, timers, and command line parameter parsing.
9//
10
11#include <stdio.h>
12#include <stdlib.h>
13#include <fcntl.h>
14#include <stdarg.h>
15
16#include "../library/library.h"
17#include "../library/flatimage.h"
18#include "Win32Serial.h"
19
20void usage(void)
21{
22 char *usageStrings[] = {
[211]23 "SerDrive - XTIDE Universal BIOS Serial Drive Server",
24 "Version 1.2.0_wip, Built " __DATE__,
25 "",
26 "usage: SerDrive [options] imagefile [[slave-options] slave-imagefile]",
27 "",
[209]28 " -g cyl:sect:head Geometry in cylinders, sectors per cylinder, and heads",
29 " (default is 65:63:16 for a 32 MB disk)",
[211]30 "",
31 " -n [megabytes] Create new disk with given size or use -g geometry",
32 "",
33 " -p Named Pipe mode for emulators (pipe is '" PIPENAME "')",
34 "",
35 " -c COMPortNumber COM Port to use (default is first found)",
36 "",
[209]37 " -b BaudRate Baud rate to use on the COM port ",
[211]38 " Without a rate multiplier: 2400, 9600, 38400, 115200",
39 " With a 2x rate multiplier: 4800, 19200, 76800, 230400",
40 " With a 4x rate multiplier: 9600, 38400, 153600, 460800",
41 " Abbreviations also accepted (ie, '460K', '38.4K', etc)",
[215]42 " (default is 38400, 115200 in named pipe mode)",
[211]43 "",
[209]44 " -t Disable timeout, useful for long delays when debugging",
[211]45 "",
46 " -r Read Only disk, do not allow writes",
47 "",
48 " -v [level] Reporting level 1-6, with increasing information",
[215]49 "",
50 "On the client computer, a serial port can be configured for use as a hard disk",
51 "with xtidecfg.com. Or one can hold down the ALT key at the end of the normal",
52 "IDE hard disk scan and the XTIDE Universal BIOS will scan COM1-7, at each of",
53 "the four speeds given above for BaudRate. Note that hardware rate multipliers",
54 "must be taken into account on the server end, but are invisible on the client.",
[209]55 NULL };
56
57 for( int t = 0; usageStrings[t]; t++ )
58 fprintf( stderr, "%s\n", usageStrings[t] );
59
60 exit( 1 );
61}
62
[211]63int verbose = 0;
[209]64
65int main(int argc, char* argv[])
66{
67 DWORD len;
68
69 unsigned long check;
70 unsigned char w;
71
72 unsigned short wbuff[256];
73
74 Serial *serial;
75 Image *img;
[215]76 struct baudRate *baudRate = NULL;
[209]77
78 int timeoutEnabled = 1;
79
80 char *ComPort = NULL, ComPortBuff[20];
81
82 _fmode = _O_BINARY;
83
84 unsigned long cyl = 0, sect = 0, head = 0;
85 int readOnly = 0, createFile = 0, explicitGeometry = 0;
86
87 int imagecount = 0;
88 Image *images[2] = { NULL, NULL };
89
90 for( int t = 1; t < argc; t++ )
91 {
92 if( argv[t][0] == '/' || argv[t][0] == '-' )
93 {
94 char *c;
95 unsigned long a;
96 for( c = &argv[t][1]; *c && !isdigit( *c ); c++ )
97 ;
98 a = atol(c);
99
100 switch( argv[t][1] )
101 {
102 case 'c': case 'C':
103 a = atol( argv[++t] );
104 if( a < 1 )
105 usage();
106 sprintf( ComPortBuff, "COM%d", a );
107 ComPort = &ComPortBuff[0];
108 break;
109 case 'v': case 'V':
110 if( atol(argv[t+1]) != 0 )
111 verbose = atol(argv[++t]);
112 else
[213]113 verbose = 1;
[209]114 break;
115 case 'r': case 'R':
116 readOnly = 1;
117 break;
118 case 'p': case 'P':
119 ComPort = "PIPE";
[215]120 if( !baudRate )
121 baudRate = baudRateMatchString( "115200" );
[209]122 break;
123 case 'g': case 'G':
124 if( !Image::parseGeometry( argv[++t], &cyl, &sect, &head ) )
125 usage();
126 explicitGeometry = 1;
127 break;
128 case 'h': case 'H': case '?':
129 usage();
130 break;
131 case 'n': case 'N':
132 createFile = 1;
133 if( atol(argv[t+1]) != 0 )
134 {
135 unsigned long size = atol(argv[++t]);
136 sect = 63;
137 head = 16;
138 cyl = (size*1024*2) / (16*63);
139 explicitGeometry = 1;
140 }
141 break;
142 case 't': case 'T':
143 timeoutEnabled = 0;
144 break;
145 case 'b': case 'B':
146 if( !(baudRate = baudRateMatchString( argv[++t] )) )
147 {
[215]148 fprintf( stderr, "Unknown Baud Rate %s\n\n", argv[t] );
149 usage();
[209]150 }
151 break;
152 default:
153 fprintf( stderr, "Unknown Option: %s\n\n", argv[t] );
154 usage();
155 }
156 }
157 else if( imagecount < 2 )
158 {
159 images[imagecount] = new FlatImage( argv[t], readOnly, imagecount, createFile, cyl, sect, head );
160 imagecount++;
161 createFile = readOnly = cyl = sect = head = 0;
162 }
163 else
164 usage();
165 }
166
167 if( imagecount == 0 )
168 usage();
169
[215]170 if( !baudRate )
171 baudRate = baudRateMatchString( "38400" );
172
[209]173 do
174 {
175 serial = new Win32Serial( ComPort, baudRate );
176
177 processRequests( serial, images[0], images[1], timeoutEnabled, verbose );
178
179 delete serial;
180
181 if( serial->resetConnection )
[211]182 log( 0, "Connection closed, reset..." );
[209]183 }
184 while( serial->resetConnection );
185}
186
187void log( int level, char *message, ... )
188{
189 va_list args;
190
191 va_start( args, message );
192
[211]193 if( level < 0 )
[209]194 {
[211]195 fprintf( stderr, "ERROR: " );
196 vfprintf( stderr, message, args );
197 fprintf( stderr, "\n" );
198 exit( 1 );
[209]199 }
200 else if( verbose >= level )
201 {
[211]202 vprintf( message, args );
203 printf( "\n" );
[209]204 }
205
206 va_end( args );
207}
208
209unsigned long GetTime(void)
210{
211 return( GetTickCount() );
212}
213
214unsigned long GetTime_Timeout(void)
215{
216 return( 1000 );
217}
Note: See TracBrowser for help on using the repository browser.