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

Last change on this file since 213 was 213, checked in by gregli@…, 12 years ago

Minor improvements to serial server.

File size: 4.7 KB
Line 
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[] = {
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        "",
28        "  -g cyl:sect:head  Geometry in cylinders, sectors per cylinder, and heads",
29        "                    (default is 65:63:16 for a 32 MB disk)",
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        "",
37        "  -b BaudRate       Baud rate to use on the COM port ",
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)",
42        "                    (default is 9600, 115200 in named pipe mode)",
43        "",
44        "  -t                Disable timeout, useful for long delays when debugging",
45        "",
46        "  -r                Read Only disk, do not allow writes",
47        "",
48        "  -v [level]        Reporting level 1-6, with increasing information",
49        NULL };
50
51    for( int t = 0; usageStrings[t]; t++ )
52        fprintf( stderr, "%s\n", usageStrings[t] );
53
54    exit( 1 );
55}
56
57int verbose = 0;
58
59int main(int argc, char* argv[])
60{
61    DWORD len;
62
63    unsigned long check;
64    unsigned char w;
65
66    unsigned short wbuff[256];
67
68    Serial *serial;
69    Image *img;
70    struct baudRate *baudRate;
71
72    int timeoutEnabled = 1;
73
74    char *ComPort = NULL, ComPortBuff[20];
75
76    _fmode = _O_BINARY;
77
78    unsigned long cyl = 0, sect = 0, head = 0;
79    int readOnly = 0, createFile = 0, explicitGeometry = 0;
80
81    int imagecount = 0;
82    Image *images[2] = { NULL, NULL };
83
84    baudRate = baudRateMatchString( "9600" );
85
86    for( int t = 1; t < argc; t++ )
87    {
88        if( argv[t][0] == '/' || argv[t][0] == '-' )
89        {
90            char *c;
91            unsigned long a;
92            for( c = &argv[t][1]; *c && !isdigit( *c ); c++ ) 
93                ;
94            a = atol(c);
95
96            switch( argv[t][1] )
97            {
98            case 'c': case 'C':
99                a = atol( argv[++t] );
100                if( a < 1 )
101                    usage();
102                sprintf( ComPortBuff, "COM%d", a );
103                ComPort = &ComPortBuff[0];
104                break;
105            case 'v': case 'V':
106                if( atol(argv[t+1]) != 0 )
107                    verbose = atol(argv[++t]);
108                else
109                    verbose = 1;
110                break;
111            case 'r': case 'R':
112                readOnly = 1;
113                break;
114            case 'p': case 'P':
115                ComPort = "PIPE";
116                baudRate = baudRateMatchString( "115200" );
117                break;           
118            case 'g': case 'G':
119                if( !Image::parseGeometry( argv[++t], &cyl, &sect, &head ) )
120                    usage();
121                explicitGeometry = 1;
122                break;
123            case 'h': case 'H': case '?':
124                usage();
125                break;
126            case 'n': case 'N':
127                createFile = 1;
128                if( atol(argv[t+1]) != 0 )
129                {
130                    unsigned long size = atol(argv[++t]);
131                    sect = 63;
132                    head = 16;
133                    cyl = (size*1024*2) / (16*63);
134                    explicitGeometry = 1;
135                }
136                break;
137            case 't': case 'T':
138                timeoutEnabled = 0;
139                break;
140            case 'b': case 'B':
141                if( !(baudRate = baudRateMatchString( argv[++t] )) )
142                {
143                        fprintf( stderr, "Unknown Baud Rate %s\n\n", argv[t] );
144                        usage();
145                }
146                break;
147            default:
148                fprintf( stderr, "Unknown Option: %s\n\n", argv[t] );
149                usage();
150            }
151        }
152        else if( imagecount < 2 )
153        {
154            images[imagecount] = new FlatImage( argv[t], readOnly, imagecount, createFile, cyl, sect, head );
155            imagecount++;
156            createFile = readOnly = cyl = sect = head = 0;
157        }
158        else
159            usage();
160    }
161
162    if( imagecount == 0 )
163        usage();
164
165    do
166    {
167        serial = new Win32Serial( ComPort, baudRate );
168
169        processRequests( serial, images[0], images[1], timeoutEnabled, verbose );
170
171        delete serial;
172
173        if( serial->resetConnection )
174            log( 0, "Connection closed, reset..." );
175    }
176    while( serial->resetConnection );
177}
178
179void log( int level, char *message, ... )
180{
181    va_list args;
182
183    va_start( args, message );
184
185    if( level < 0 )
186    {
187        fprintf( stderr, "ERROR: " );
188        vfprintf( stderr, message, args );
189        fprintf( stderr, "\n" );
190        exit( 1 );
191    }
192    else if( verbose >= level )
193    {
194        vprintf( message, args );
195        printf( "\n" );
196    }
197
198    va_end( args );
199}
200
201unsigned long GetTime(void)
202{
203    return( GetTickCount() );
204}
205
206unsigned long GetTime_Timeout(void)
207{
208    return( 1000 );
209}
Note: See TracBrowser for help on using the repository browser.