- Timestamp:
- Jan 25, 2012, 7:04:43 AM (13 years ago)
- google:author:
- gregli@hotmail.com
- Location:
- trunk/Serial_Server
- Files:
-
- 2 added
- 2 deleted
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Serial_Server/library/FlatImage.h
r217 r219 6 6 // 7 7 8 #include <stdio.h>9 8 #include "library.h" 10 9 … … 12 11 { 13 12 private: 14 FILE *fp;13 class FileAccess fp; 15 14 16 15 public: 17 FlatImage( char *name, int p_readOnly, int p_drive, int create, unsigned long cyl, unsigned long head, unsigned long sect, int useCHS ); 18 ~FlatImage(); 16 FlatImage( char *name, int p_readOnly, int p_drive, int p_create, unsigned long p_cyl, unsigned long p_head, unsigned long p_sect, int p_useCHS ) : Image( name, p_readOnly, p_drive, p_create, p_cyl, p_head, p_sect, p_useCHS ) 17 { 18 long filesize; 19 19 20 int seekSector( unsigned long cyl, unsigned long sect, unsigned long head ); 21 int seekSector( unsigned long lba ); 22 int writeSector( void *buff ); 23 int readSector( void *buff ); 20 if( p_create ) 21 { 22 char buff[512]; 23 unsigned long size; 24 double sizef; 25 FileAccess cf; 26 27 size = (unsigned long) p_cyl * (unsigned long) p_sect * (unsigned long) p_head; 28 if( size > cf.MaxSectors ) 29 log( -1, "'%s', can't create flat file with size greater than %lu 512-byte sectors", name, cf.MaxSectors ); 30 sizef = size / 2048.0; // 512 byte sectors -> MB 31 32 cf.Create( name ); 33 34 memset( &buff[0], 0, 512 ); 35 while( size-- ) 36 cf.Write( &buff[0], 512 ); 37 38 if( p_cyl > 1024 ) 39 log( 0, "Created file '%s', size %.1lf MB", name, sizef ); 40 else 41 log( 0, "Created file '%s', geometry %u:%u:%u, size %.1lf MB", name, p_cyl, p_sect, p_head, sizef ); 42 43 cf.Close(); 44 } 45 46 fp.Open( name ); 47 48 totallba = fp.SizeSectors(); 49 50 init( name, p_readOnly, p_drive, p_cyl, p_head, p_sect, p_useCHS ); 51 } 52 53 FlatImage::~FlatImage() 54 { 55 fp.Close(); 56 } 57 58 void seekSector( unsigned long lba ) 59 { 60 fp.SeekSectors( lba ); 61 } 62 63 void writeSector( void *buff ) 64 { 65 fp.Write( buff, 512 ); 66 } 67 68 void readSector( void *buff ) 69 { 70 fp.Read( buff, 512 ); 71 } 24 72 }; 25 73 -
trunk/Serial_Server/library/Image.cpp
r217 r219 10 10 #include <stdlib.h> 11 11 #include <string.h> 12 #include <stdio.h> 12 13 13 14 Image::Image( char *name, int p_readOnly, int p_drive ) … … 82 83 sizef = totallba/2048.0; 83 84 if( useCHS ) 84 log( 0, "Opening '%s', CHS geometry %u:%u:%u, total size %.1lf MB", name, cyl, sect, head, sizef );85 else 86 log( 0, "Opening '%s', total lba%lu, total size %.1lf MB", name, totallba, sizef );85 log( 0, "Opening '%s', CHS geometry %u:%u:%u, total LBA %lu, total size %.1lf MB", name, cyl, sect, head, totallba, sizef ); 86 else 87 log( 0, "Opening '%s', total LBA %lu, total size %.1lf MB", name, totallba, sizef ); 87 88 } 88 89 -
trunk/Serial_Server/library/Library.h
r217 r219 9 9 #define LIBRARY_H_INCLUDED 10 10 11 #include "stdio.h" 11 void log( int level, char *message, ... ); 12 12 13 void log( int level, char *message, ... );14 13 unsigned long GetTime(void); 15 14 unsigned long GetTime_Timeout(void); … … 20 19 { 21 20 public: 22 virtual int seekSector( unsigned long cyl, unsigned long sect, unsigned long head ) = 0; 23 virtual int seekSector( unsigned long lba ) = 0; 21 virtual void seekSector( unsigned long lba ) = 0; 24 22 25 virtual intwriteSector( void *buff ) = 0;23 virtual void writeSector( void *buff ) = 0; 26 24 27 virtual intreadSector( void *buff ) = 0;25 virtual void readSector( void *buff ) = 0; 28 26 29 27 Image( char *name, int p_readOnly, int p_drive ); … … 57 55 struct baudRate *baudRateMatchDivisor( unsigned char divisor ); 58 56 59 class Serial 60 { 61 public: 62 virtual unsigned long readCharacters( void *buff, unsigned long len ) = 0; 57 #ifdef WIN32 58 #include "../win32/win32serial.h" 59 #else 60 // there is no standard way to read/write and configure the serial port, OS specifc only 61 #endif 63 62 64 virtual unsigned long writeCharacters( void *buff, unsigned long len ) = 0; 63 #ifdef WIN32 64 #include "../win32/win32file.h" 65 #else 66 #include "file.h" 67 #endif 65 68 66 Serial( char *name, struct baudRate *p_baudRate ) 67 { 68 speedEmulation = 0; 69 resetConnection = 0; 70 baudRate = p_baudRate; 71 }; 72 73 virtual ~Serial() {}; 74 75 int speedEmulation; 76 int resetConnection; 77 78 struct baudRate *baudRate; 79 }; 80 81 void processRequests( Serial *serial, Image *image0, Image *image1, int timeoutEnabled, int verboseLevel ); 82 83 #define ATA_COMMAND_LBA 0x40 84 #define ATA_COMMAND_HEADMASK 0xf 85 86 #define ATA_DriveAndHead_Drive 0x10 69 void processRequests( SerialAccess *serial, Image *image0, Image *image1, int timeoutEnabled, int verboseLevel ); 87 70 88 71 #endif -
trunk/Serial_Server/library/Process.cpp
r217 r219 9 9 #include <memory.h> 10 10 #include <string.h> 11 #include <stdio.h> 11 12 12 13 union _buff { … … 54 55 #define SERIAL_INQUIRE_PORTANDBAUD_PORTTRANSLATE( a ) ( ((a) & SERIAL_INQUIRE_PORTANDBAUD_PORT) << 1 | SERIAL_INQUIRE_PORTANDBAUD_STARTINGPORT ) 55 56 57 #define ATA_COMMAND_LBA 0x40 58 #define ATA_COMMAND_HEADMASK 0xf 59 60 #define ATA_DriveAndHead_Drive 0x10 61 56 62 void logBuff( char *message, unsigned long buffoffset, unsigned long readto, int verboseLevel ) 57 63 { … … 75 81 } 76 82 77 void processRequests( Serial *serial, Image *image0, Image *image1, int timeoutEnabled, int verboseLevel )83 void processRequests( SerialAccess *serial, Image *image0, Image *image1, int timeoutEnabled, int verboseLevel ) 78 84 { 79 85 unsigned char workCommand; … … 204 210 // Echo back the CRC 205 211 // 206 if( serial->writeCharacters( &buff.w[256], 2 ) != 2)207 log( 0, "Serial Port Write Error" );212 if( !serial->writeCharacters( &buff.w[256], 2 ) ) 213 break; 208 214 209 215 workOffset++; … … 340 346 buff.w[256] = checksum( &buff.w[0], 256 ); 341 347 342 if( serial->writeCharacters( &buff.w[0], 514 ) != 514)343 log( 0, "Serial Port Write Error" );348 if( !serial->writeCharacters( &buff.w[0], 514 ) ) 349 break; 344 350 345 351 if( verboseLevel >= 3 ) -
trunk/Serial_Server/makefile
r211 r219 9 9 10 10 CL = cl 11 CLFLAGS = /Ox 11 CLFLAGS = /Ox /DWIN32 12 12 13 WIN32OBJS = build/win32.obj build/ win32serial.obj build/checksum.obj build/serial.obj build/flatimage.obj build/process.obj build/image.obj13 WIN32OBJS = build/win32.obj build/checksum.obj build/serial.obj build/process.obj build/image.obj 14 14 15 15 build/serdrive.exe: $(WIN32OBJS) … … 22 22 $(CL) /c $(CLFLAGS) $< /Fo$@ 23 23 24 $(WIN32OBJS): library/library.h 24 $(WIN32OBJS): library/library.h win32/win32file.h win32/win32serial.h library/file.h library/flatimage.h 25 25 26 26 build/checksum_test.exe: library/checksum.cpp -
trunk/Serial_Server/win32/Win32.cpp
r217 r219 16 16 #include "../library/library.h" 17 17 #include "../library/flatimage.h" 18 #include "Win32Serial.h"19 18 20 19 void usage(void) … … 30 29 "", 31 30 " -n [megabytes] Create new disk with given size or use -g geometry", 32 " Maximum size is 137.4 GB (LBA28 limit)",31 " Maximum size is " USAGE_MAXSECTORS, 33 32 " (default is a 32 MB disk, with CHS geometry 65:63:16)", 34 33 "", … … 74 73 unsigned short wbuff[256]; 75 74 76 Serial *serial;75 SerialAccess serial; 77 76 Image *img; 78 77 struct baudRate *baudRate = NULL; … … 178 177 do 179 178 { 180 serial = new Win32Serial( ComPort, baudRate );181 182 processRequests( serial, images[0], images[1], timeoutEnabled, verbose );183 184 delete serial;185 186 if( serial ->resetConnection )187 log( 0, " Connection closed, reset..." );188 } 189 while( serial ->resetConnection );179 serial.Connect( ComPort, baudRate ); 180 181 processRequests( &serial, images[0], images[1], timeoutEnabled, verbose ); 182 183 serial.Disconnect(); 184 185 if( serial.resetConnection ) 186 log( 0, "Serial Connection closed, reset..." ); 187 } 188 while( serial.resetConnection ); 190 189 } 191 190 -
trunk/Serial_Server/win32/Win32Serial.h
r209 r219 6 6 // 7 7 8 #include <stdio.h> 8 9 #include "windows.h" 9 10 #include "../library/library.h" … … 11 12 #define PIPENAME "\\\\.\\pipe\\xtide" 12 13 13 class Win32Serial : public Serial14 class SerialAccess 14 15 { 15 16 public: 16 Win32Serial( char *name, struct baudRate *baudRate ); 17 ~Win32Serial(); 17 void Connect( char *name, struct baudRate *p_baudRate ) 18 { 19 char buff1[20], buff2[1024]; 18 20 19 unsigned long readCharacters( void *buff, unsigned long len ); 20 unsigned long writeCharacters( void *buff, unsigned long len ); 21 baudRate = p_baudRate; 22 23 pipe = NULL; 24 25 if( !name ) 26 { 27 for( int t = 1; t <= 30 && !name; t++ ) 28 { 29 sprintf( buff1, "COM%d", t ); 30 if( QueryDosDeviceA( buff1, buff2, sizeof(buff2) ) ) 31 name = buff1; 32 } 33 if( !name ) 34 log( -1, "No physical COM ports found" ); 35 } 36 37 if( !strcmp( name, "PIPE" ) ) 38 { 39 log( 0, "Opening named pipe %s (simulating %lu baud)", PIPENAME, baudRate->rate ); 40 41 pipe = CreateNamedPipeA( PIPENAME, PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE|PIPE_REJECT_REMOTE_CLIENTS, 2, 1024, 1024, 0, NULL ); 42 if( !pipe ) 43 log( -1, "Could not CreateNamedPipe " PIPENAME ); 44 45 if( !ConnectNamedPipe( pipe, NULL ) ) 46 log( -1, "Could not ConnectNamedPipe" ); 47 48 if( baudRate->divisor > 3 ) 49 log( -1, "Cannot simulate baud rates with hardware multipliers" ); 50 51 speedEmulation = 1; 52 resetConnection = 1; 53 } 54 else 55 { 56 if( QueryDosDeviceA( name, buff2, sizeof(buff2) ) ) 57 { 58 COMMTIMEOUTS timeouts; 59 DCB dcb; 60 61 log( 0, "Opening %s (%lu baud)", name, baudRate->rate ); 62 63 pipe = CreateFileA( name, GENERIC_READ|GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0 ); 64 if( !pipe ) 65 log( -1, "Could not Open \"%s\"", name ); 66 67 FillMemory(&dcb, sizeof(dcb), 0); 68 FillMemory(&timeouts, sizeof(timeouts), 0); 69 70 dcb.DCBlength = sizeof(dcb); 71 dcb.BaudRate = baudRate->rate; 72 dcb.ByteSize = 8; 73 dcb.StopBits = ONESTOPBIT; 74 dcb.Parity = NOPARITY; 75 if( !SetCommState( pipe, &dcb ) ) 76 log( -1, "Could not SetCommState" ); 77 78 if( !SetCommTimeouts( pipe, &timeouts ) ) 79 log( -1, "Could not SetCommTimeouts" ); 80 } 81 else 82 { 83 char logbuff[ 1024 ]; 84 int found = 0; 85 86 sprintf( logbuff, "serial port '%s' not found, detected COM ports:", name ); 87 88 for( int t = 1; t <= 40; t++ ) 89 { 90 sprintf( buff1, "COM%d", t ); 91 if( QueryDosDeviceA( buff1, buff2, sizeof(buff2) ) ) 92 { 93 strcat( logbuff, "\n " ); 94 strcat( logbuff, buff1 ); 95 found = 1; 96 } 97 } 98 if( !found ) 99 strcat( logbuff, "\n (none)" ); 100 101 log( -1, logbuff ); 102 } 103 } 104 } 105 106 void Disconnect() 107 { 108 if( pipe ) 109 { 110 CloseHandle( pipe ); 111 pipe = NULL; 112 } 113 } 114 115 unsigned long readCharacters( void *buff, unsigned long len ) 116 { 117 unsigned long readLen; 118 int ret; 119 120 ret = ReadFile( pipe, buff, len, &readLen, NULL ); 121 122 if( !ret || readLen == 0 ) 123 { 124 if( GetLastError() == ERROR_BROKEN_PIPE ) 125 return( 0 ); 126 else 127 log( -1, "read serial failed (error code %d)", GetLastError() ); 128 } 129 130 return( readLen ); 131 } 132 133 int writeCharacters( void *buff, unsigned long len ) 134 { 135 unsigned long writeLen; 136 int ret; 137 138 ret = WriteFile( pipe, buff, len, &writeLen, NULL ); 139 140 if( !ret || len != writeLen ) 141 { 142 if( GetLastError() == ERROR_BROKEN_PIPE ) 143 return( 0 ); 144 else 145 log( -1, "write serial failed (error code %d)", GetLastError() ); 146 } 147 148 return( 1 ); 149 } 150 151 SerialAccess() 152 { 153 pipe = NULL; 154 speedEmulation = 0; 155 resetConnection = 0; 156 baudRate = NULL; 157 } 158 159 ~SerialAccess() 160 { 161 Disconnect(); 162 } 163 164 int speedEmulation; 165 int resetConnection; 166 167 struct baudRate *baudRate; 21 168 22 169 private:
Note:
See TracChangeset
for help on using the changeset viewer.