| 1 |
#ifdef TRENT_IS_HERE |
| 2 |
#define TRACE |
| 3 |
#define DEBUG |
| 4 |
#define DEBUG_MW |
| 5 |
#ifdef DEBUG_MW |
| 6 |
extern void mexWarMsgTxt( const char * ); |
| 7 |
extern void mexPrintf( const char *, ... ); |
| 8 |
#endif |
| 9 |
#endif |
| 10 |
extern void report( char * ); |
| 11 |
extern void report_warning( char * ); |
| 12 |
extern void report_error( char * ); |
| 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 |
#include <windows.h> |
| 72 |
#include <stdio.h> |
| 73 |
#include <errno.h> |
| 74 |
#include <time.h> |
| 75 |
#include "win32termios.h" |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
#ifdef __LCC__ |
| 83 |
# include <winsock.h> |
| 84 |
#else |
| 85 |
# include <malloc.h> |
| 86 |
#endif |
| 87 |
|
| 88 |
#define SIGIO 0 |
| 89 |
|
| 90 |
int my_errno; |
| 91 |
extern int errno; |
| 92 |
struct termios_list |
| 93 |
{ |
| 94 |
char filename[80]; |
| 95 |
int my_errno; |
| 96 |
int interrupt; |
| 97 |
int event_flag; |
| 98 |
int tx_happened; |
| 99 |
unsigned long *hComm; |
| 100 |
struct termios *ttyset; |
| 101 |
struct serial_struct *sstruct; |
| 102 |
|
| 103 |
unsigned char MSR; |
| 104 |
struct async_struct *astruct; |
| 105 |
struct serial_icounter_struct *sis; |
| 106 |
int open_flags; |
| 107 |
OVERLAPPED rol; |
| 108 |
OVERLAPPED wol; |
| 109 |
OVERLAPPED sol; |
| 110 |
int fd; |
| 111 |
struct termios_list *next; |
| 112 |
struct termios_list *prev; |
| 113 |
}; |
| 114 |
struct termios_list *first_tl = NULL; |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
int serial_test( char * filename ) |
| 127 |
{ |
| 128 |
unsigned long *hcomm; |
| 129 |
int ret; |
| 130 |
hcomm = CreateFile( filename, GENERIC_READ |GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0 ); |
| 131 |
if ( hcomm == INVALID_HANDLE_VALUE ) |
| 132 |
{ |
| 133 |
if (GetLastError() == ERROR_ACCESS_DENIED) |
| 134 |
{ |
| 135 |
ret = 1; |
| 136 |
} |
| 137 |
else |
| 138 |
{ |
| 139 |
ret = 0; |
| 140 |
} |
| 141 |
} |
| 142 |
else |
| 143 |
{ |
| 144 |
ret = 1; |
| 145 |
} |
| 146 |
CloseHandle( hcomm ); |
| 147 |
return(ret); |
| 148 |
} |
| 149 |
|
| 150 |
void termios_setflags( int fd, int termios_flags[] ) |
| 151 |
{ |
| 152 |
struct termios_list *index = find_port( fd ); |
| 153 |
int i, result; |
| 154 |
int windows_flags[11] = { 0, EV_RXCHAR, EV_TXEMPTY, EV_CTS, EV_DSR, |
| 155 |
EV_RING|0x2000, EV_RLSD, EV_ERR, |
| 156 |
EV_ERR, EV_ERR, EV_BREAK |
| 157 |
}; |
| 158 |
if( !index ) |
| 159 |
{ |
| 160 |
LEAVE( "termios_setflags" ); |
| 161 |
return; |
| 162 |
} |
| 163 |
index->event_flag = 0; |
| 164 |
for(i=0;i<11;i++) |
| 165 |
if( termios_flags[i] ) |
| 166 |
index->event_flag |= windows_flags[i]; |
| 167 |
result = SetCommMask( index->hComm, index->event_flag ); |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
if( index->event_flag & 0x2000 && result == 0 ) |
| 182 |
{ |
| 183 |
index->event_flag &= ~0x2000; |
| 184 |
SetCommMask( index->hComm, index->event_flag ); |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
int get_fd( char *filename ) |
| 200 |
{ |
| 201 |
struct termios_list *index = first_tl; |
| 202 |
|
| 203 |
ENTER( "get_fd" ); |
| 204 |
if( !index ) |
| 205 |
{ |
| 206 |
return -1; |
| 207 |
} |
| 208 |
|
| 209 |
while( strcmp( index->filename, filename ) ) |
| 210 |
{ |
| 211 |
index = index->next; |
| 212 |
if( !index->next ) |
| 213 |
return( -1 ); |
| 214 |
} |
| 215 |
LEAVE( "get_fd" ); |
| 216 |
return( index->fd ); |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
char *get_filename( int fd ) |
| 231 |
{ |
| 232 |
struct termios_list *index = first_tl; |
| 233 |
|
| 234 |
ENTER( "get_filename" ); |
| 235 |
if( !index ) |
| 236 |
return( "bad" ); |
| 237 |
while( index->fd != fd ) |
| 238 |
{ |
| 239 |
if( index->next == NULL ) |
| 240 |
return( "bad" ); |
| 241 |
index = index->next; |
| 242 |
} |
| 243 |
LEAVE( "get_filename" ); |
| 244 |
return( index->filename ); |
| 245 |
} |
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
void dump_termios_list( char *foo ) |
| 259 |
{ |
| 260 |
#ifdef DEBUG |
| 261 |
struct termios_list *index = first_tl; |
| 262 |
printf( "============== %s start ===============\n", foo ); |
| 263 |
if ( index ) |
| 264 |
{ |
| 265 |
printf( "%i filename | %s\n", index->fd, index->filename ); |
| 266 |
} |
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
printf( "============== %s end ===============\n", foo ); |
| 274 |
#endif |
| 275 |
} |
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
void set_errno( int error ) |
| 289 |
{ |
| 290 |
my_errno = error; |
| 291 |
} |
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
void usleep( unsigned long usec ) |
| 305 |
{ |
| 306 |
Sleep( usec/1000 ); |
| 307 |
} |
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
int CBR_to_B( int Baud ) |
| 321 |
{ |
| 322 |
ENTER( "CBR_to_B" ); |
| 323 |
switch ( Baud ) |
| 324 |
{ |
| 325 |
|
| 326 |
case 0: return( B0 ); |
| 327 |
case 50: return( B50 ); |
| 328 |
case 75: return( B75 ); |
| 329 |
case CBR_110: return( B110 ); |
| 330 |
case 134: return( B134 ); |
| 331 |
case 150: return( B150 ); |
| 332 |
case 200: return( B200 ); |
| 333 |
case CBR_300: return( B300 ); |
| 334 |
case CBR_600: return( B600 ); |
| 335 |
case CBR_1200: return( B1200 ); |
| 336 |
case 1800: return( B1800 ); |
| 337 |
case CBR_2400: return( B2400 ); |
| 338 |
case CBR_4800: return( B4800 ); |
| 339 |
case CBR_9600: return( B9600 ); |
| 340 |
case CBR_14400: return( B14400 ); |
| 341 |
case CBR_19200: return( B19200 ); |
| 342 |
case CBR_28800: return( B28800 ); |
| 343 |
case CBR_38400: return( B38400 ); |
| 344 |
case CBR_57600: return( B57600 ); |
| 345 |
case CBR_115200: return( B115200 ); |
| 346 |
case CBR_128000: return( B128000 ); |
| 347 |
case CBR_230400: return( B230400 ); |
| 348 |
case CBR_256000: return( B256000 ); |
| 349 |
case CBR_460800: return( B460800 ); |
| 350 |
case CBR_500000: return( B500000 ); |
| 351 |
case CBR_576000: return( B576000 ); |
| 352 |
case CBR_921600: return( B921600 ); |
| 353 |
case CBR_1000000: return( B1000000 ); |
| 354 |
case CBR_1152000: return( B1152000 ); |
| 355 |
case CBR_1500000: return( B1500000 ); |
| 356 |
case CBR_2000000: return( B2000000 ); |
| 357 |
case CBR_2500000: return( B2500000 ); |
| 358 |
case CBR_3000000: return( B3000000 ); |
| 359 |
case CBR_3500000: return( B3500000 ); |
| 360 |
case CBR_4000000: return( B4000000 ); |
| 361 |
default: |
| 362 |
|
| 363 |
return( Baud ); |
| 364 |
} |
| 365 |
} |
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
int B_to_CBR( int Baud ) |
| 379 |
{ |
| 380 |
int ret; |
| 381 |
ENTER( "B_to_CBR" ); |
| 382 |
switch ( Baud ) |
| 383 |
{ |
| 384 |
case 0: ret = 0; break; |
| 385 |
case B50: ret = 50; break; |
| 386 |
case B75: ret = 75; break; |
| 387 |
case B110: ret = CBR_110; break; |
| 388 |
case B134: ret = 134; break; |
| 389 |
case B150: ret = 150; break; |
| 390 |
case B200: ret = 200; break; |
| 391 |
case B300: ret = CBR_300; break; |
| 392 |
case B600: ret = CBR_600; break; |
| 393 |
case B1200: ret = CBR_1200; break; |
| 394 |
case B1800: ret = 1800; break; |
| 395 |
case B2400: ret = CBR_2400; break; |
| 396 |
case B4800: ret = CBR_4800; break; |
| 397 |
case B9600: ret = CBR_9600; break; |
| 398 |
case B14400: ret = CBR_14400; break; |
| 399 |
case B19200: ret = CBR_19200; break; |
| 400 |
case B28800: ret = CBR_28800; break; |
| 401 |
case B38400: ret = CBR_38400; break; |
| 402 |
case B57600: ret = CBR_57600; break; |
| 403 |
case B115200: ret = CBR_115200; break; |
| 404 |
case B128000: ret = CBR_128000; break; |
| 405 |
case B230400: ret = CBR_230400; break; |
| 406 |
case B256000: ret = CBR_256000; break; |
| 407 |
case B460800: ret = CBR_460800; break; |
| 408 |
case B500000: ret = CBR_500000; break; |
| 409 |
case B576000: ret = CBR_576000; break; |
| 410 |
case B921600: ret = CBR_921600; break; |
| 411 |
case B1000000: ret = CBR_1000000; break; |
| 412 |
case B1152000: ret = CBR_1152000; break; |
| 413 |
case B1500000: ret = CBR_1500000; break; |
| 414 |
case B2000000: ret = CBR_2000000; break; |
| 415 |
case B2500000: ret = CBR_2500000; break; |
| 416 |
case B3000000: ret = CBR_3000000; break; |
| 417 |
case B3500000: ret = CBR_3500000; break; |
| 418 |
case B4000000: ret = CBR_4000000; break; |
| 419 |
|
| 420 |
default: |
| 421 |
|
| 422 |
return Baud; |
| 423 |
} |
| 424 |
LEAVE( "B_to_CBR" ); |
| 425 |
return ret; |
| 426 |
} |
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
int bytesize_to_termios( int ByteSize ) |
| 440 |
{ |
| 441 |
ENTER( "bytesize_to_termios" ); |
| 442 |
switch ( ByteSize ) |
| 443 |
{ |
| 444 |
case 5: return( CS5 ); |
| 445 |
case 6: return( CS6 ); |
| 446 |
case 7: return( CS7 ); |
| 447 |
case 8: |
| 448 |
default: return( CS8 ); |
| 449 |
} |
| 450 |
} |
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
int termios_to_bytesize( int cflag ) |
| 464 |
{ |
| 465 |
ENTER( "termios_to_bytesize" ); |
| 466 |
switch ( cflag & CSIZE ) |
| 467 |
{ |
| 468 |
case CS5: return( 5 ); |
| 469 |
case CS6: return( 6 ); |
| 470 |
case CS7: return( 7 ); |
| 471 |
case CS8: |
| 472 |
default: return( 8 ); |
| 473 |
} |
| 474 |
} |
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
const char *get_dos_port( char const *name ) |
| 488 |
{ |
| 489 |
ENTER( "get_dos_port" ); |
| 490 |
if ( !strcmp( name, "/dev/cua0" ) ) return( "COM1" ); |
| 491 |
if ( !strcmp( name, "/dev/cua1" ) ) return( "COM2" ); |
| 492 |
if ( !strcmp( name, "/dev/cua2" ) ) return( "COM3" ); |
| 493 |
if ( !strcmp( name, "/dev/cua3" ) ) return( "COM4" ); |
| 494 |
LEAVE( "get_dos_port" ); |
| 495 |
return( ( const char * ) name ); |
| 496 |
} |
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
int ClearErrors( struct termios_list *index, COMSTAT *Stat ) |
| 510 |
{ |
| 511 |
unsigned long ErrCode; |
| 512 |
int ret; |
| 513 |
|
| 514 |
ret = ClearCommError( index->hComm, &ErrCode, Stat ); |
| 515 |
if ( ret == 0 ) |
| 516 |
{ |
| 517 |
YACK(); |
| 518 |
return( ret ); |
| 519 |
} |
| 520 |
#ifdef DEBUG_ERRORS |
| 521 |
if ( ErrCode ) |
| 522 |
{ |
| 523 |
printf("%i frame %i %i overrun %i %i parity %u %i brk %i %i\n", |
| 524 |
(int) ErrCode, |
| 525 |
(int) ErrCode & CE_FRAME, |
| 526 |
index->sis->frame, |
| 527 |
(int) (ErrCode & CE_OVERRUN) | ( ErrCode & CE_RXOVER ), |
| 528 |
index->sis->overrun, |
| 529 |
(int) ErrCode & CE_RXPARITY, |
| 530 |
index->sis->parity, |
| 531 |
(int) ErrCode & CE_BREAK, |
| 532 |
index->sis->brk |
| 533 |
); |
| 534 |
} |
| 535 |
#endif |
| 536 |
if( ErrCode & CE_FRAME ) |
| 537 |
{ |
| 538 |
index->sis->frame++; |
| 539 |
ErrCode &= ~CE_FRAME; |
| 540 |
} |
| 541 |
#ifdef LIFE_IS_GOOD |
| 542 |
FIXME OVERRUN is spewing |
| 543 |
if( ErrCode & CE_OVERRUN ) |
| 544 |
{ |
| 545 |
index->sis->overrun++; |
| 546 |
ErrCode &= ~CE_OVERRUN; |
| 547 |
} |
| 548 |
|
| 549 |
else if( ErrCode & CE_RXOVER ) |
| 550 |
{ |
| 551 |
index->sis->overrun++; |
| 552 |
ErrCode &= ~CE_OVERRUN; |
| 553 |
} |
| 554 |
#endif |
| 555 |
if( ErrCode & CE_RXPARITY ) |
| 556 |
{ |
| 557 |
index->sis->parity++; |
| 558 |
ErrCode &= ~CE_RXPARITY; |
| 559 |
} |
| 560 |
if( ErrCode & CE_BREAK ) |
| 561 |
{ |
| 562 |
index->sis->brk++; |
| 563 |
ErrCode &= ~CE_BREAK; |
| 564 |
} |
| 565 |
return( ret ); |
| 566 |
} |
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
BOOL FillDCB( DCB *dcb, unsigned long *hCommPort, COMMTIMEOUTS Timeout ) |
| 580 |
{ |
| 581 |
|
| 582 |
ENTER( "FillDCB" ); |
| 583 |
dcb->DCBlength = sizeof( dcb ); |
| 584 |
if ( !GetCommState( hCommPort, dcb ) ) |
| 585 |
{ |
| 586 |
report( "GetCommState\n" ); |
| 587 |
return( -1 ); |
| 588 |
} |
| 589 |
dcb->BaudRate = CBR_9600 ; |
| 590 |
dcb->ByteSize = 8; |
| 591 |
dcb->Parity = NOPARITY; |
| 592 |
dcb->StopBits = ONESTOPBIT; |
| 593 |
dcb->fDtrControl = DTR_CONTROL_ENABLE; |
| 594 |
dcb->fRtsControl = RTS_CONTROL_ENABLE; |
| 595 |
dcb->fOutxCtsFlow = FALSE; |
| 596 |
dcb->fOutxDsrFlow = FALSE; |
| 597 |
dcb->fDsrSensitivity = FALSE; |
| 598 |
dcb->fOutX = FALSE; |
| 599 |
dcb->fInX = FALSE; |
| 600 |
dcb->fTXContinueOnXoff = FALSE; |
| 601 |
dcb->XonChar = 0x11; |
| 602 |
dcb->XoffChar = 0x13; |
| 603 |
dcb->XonLim = 0; |
| 604 |
dcb->XoffLim = 0; |
| 605 |
dcb->fParity = TRUE; |
| 606 |
if ( EV_BREAK|EV_CTS|EV_DSR|EV_ERR|EV_RING|( EV_RLSD & EV_RXFLAG ) ) |
| 607 |
dcb->EvtChar = '\n'; |
| 608 |
else dcb->EvtChar = '\0'; |
| 609 |
if ( !SetCommState( hCommPort, dcb ) ) |
| 610 |
{ |
| 611 |
report( "SetCommState\n" ); |
| 612 |
YACK(); |
| 613 |
return( -1 ); |
| 614 |
} |
| 615 |
if ( !SetCommTimeouts( hCommPort, &Timeout ) ) |
| 616 |
{ |
| 617 |
YACK(); |
| 618 |
report( "SetCommTimeouts\n" ); |
| 619 |
return( -1 ); |
| 620 |
} |
| 621 |
LEAVE( "FillDCB" ); |
| 622 |
return ( TRUE ) ; |
| 623 |
} |
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
int serial_close( int fd ) |
| 637 |
{ |
| 638 |
struct termios_list *index; |
| 639 |
|
| 640 |
|
| 641 |
ENTER( "serial_close" ); |
| 642 |
if( !first_tl || !first_tl->hComm ) |
| 643 |
{ |
| 644 |
report( "gotit!" ); |
| 645 |
return( 0 ); |
| 646 |
} |
| 647 |
index = find_port( fd ); |
| 648 |
if ( !index ) |
| 649 |
{ |
| 650 |
LEAVE( "serial_close" ); |
| 651 |
return -1; |
| 652 |
} |
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
if ( index->next && index->prev ) |
| 673 |
{ |
| 674 |
index->next->prev = index->prev; |
| 675 |
index->prev->next = index->next; |
| 676 |
} |
| 677 |
else if ( index->prev ) |
| 678 |
{ |
| 679 |
index->prev->next = NULL; |
| 680 |
} |
| 681 |
else if ( index->next ) |
| 682 |
{ |
| 683 |
index->next->prev = NULL; |
| 684 |
first_tl = index->next; |
| 685 |
} |
| 686 |
else |
| 687 |
first_tl = NULL; |
| 688 |
if ( index ) |
| 689 |
{ |
| 690 |
if ( index->rol.hEvent ) CloseHandle( index->rol.hEvent ); |
| 691 |
if ( index->wol.hEvent ) CloseHandle( index->wol.hEvent ); |
| 692 |
if ( index->sol.hEvent ) CloseHandle( index->sol.hEvent ); |
| 693 |
if ( index->hComm ) CloseHandle( index->hComm ); |
| 694 |
if ( index->ttyset ) free( index->ttyset ); |
| 695 |
if ( index->astruct ) free( index->astruct ); |
| 696 |
if ( index->sstruct ) free( index->sstruct ); |
| 697 |
if ( index->sis ) free( index->sis ); |
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
free( index ); |
| 702 |
} |
| 703 |
LEAVE( "serial_close" ); |
| 704 |
return 0; |
| 705 |
} |
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
|
| 717 |
|
| 718 |
void cfmakeraw( struct termios *s_termios ) |
| 719 |
{ |
| 720 |
ENTER( "cfmakeraw" ); |
| 721 |
s_termios->c_iflag &= ~( IGNBRK|BRKINT|PARMRK|ISTRIP |
| 722 |
|INLCR|IGNCR|ICRNL|IXON ); |
| 723 |
s_termios->c_oflag &= ~OPOST; |
| 724 |
s_termios->c_lflag &= ~( ECHO|ECHONL|ICANON|ISIG|IEXTEN ); |
| 725 |
s_termios->c_cflag &= ~( CSIZE|PARENB ); |
| 726 |
s_termios->c_cflag |= CS8; |
| 727 |
LEAVE( "cfmakeraw" ); |
| 728 |
} |
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
|
| 734 |
|
| 735 |
|
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
|
| 741 |
BOOL init_serial_struct( struct serial_struct *sstruct ) |
| 742 |
{ |
| 743 |
ENTER( "init_serial_struct" ); |
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
sstruct->custom_divisor = 0; |
| 752 |
sstruct->baud_base = 115200; |
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
sstruct->close_delay = 0; |
| 759 |
sstruct->closing_wait = 0; |
| 760 |
sstruct->iomem_reg_shift = 0; |
| 761 |
|
| 762 |
|
| 763 |
|
| 764 |
sstruct->type = 0; |
| 765 |
sstruct->line = 0; |
| 766 |
sstruct->irq = 0; |
| 767 |
sstruct->flags = 0; |
| 768 |
sstruct->xmit_fifo_size = 0; |
| 769 |
sstruct->hub6 = 0; |
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
sstruct->port = 0; |
| 774 |
sstruct->port_high = 0; |
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
sstruct->io_type = 0; |
| 779 |
|
| 780 |
|
| 781 |
|
| 782 |
sstruct->iomem_base = NULL; |
| 783 |
|
| 784 |
LEAVE( "init_serial_struct" ); |
| 785 |
return TRUE; |
| 786 |
|
| 787 |
} |
| 788 |
|
| 789 |
|
| 790 |
|
| 791 |
|
| 792 |
|
| 793 |
|
| 794 |
|
| 795 |
|
| 796 |
|
| 797 |
|
| 798 |
|
| 799 |
BOOL init_termios(struct termios *ttyset ) |
| 800 |
{ |
| 801 |
ENTER( "init_termios" ); |
| 802 |
if ( !ttyset ) |
| 803 |
return FALSE; |
| 804 |
memset( ttyset, 0, sizeof( struct termios ) ); |
| 805 |
cfsetospeed( ttyset, B9600 ); |
| 806 |
cfmakeraw( ttyset ); |
| 807 |
ttyset->c_cc[VINTR] = 0x03; |
| 808 |
ttyset->c_cc[VQUIT] = 0x1c; |
| 809 |
ttyset->c_cc[VERASE] = 0x7f; |
| 810 |
ttyset->c_cc[VKILL] = 0x15; |
| 811 |
ttyset->c_cc[VEOF] = 0x04; |
| 812 |
ttyset->c_cc[VTIME] = 0; |
| 813 |
ttyset->c_cc[VMIN] = 1; |
| 814 |
|
| 815 |
ttyset->c_cc[VSUSP] = 0x1a; |
| 816 |
ttyset->c_cc[VEOL] = '\r'; |
| 817 |
ttyset->c_cc[VREPRINT] = 0x12; |
| 818 |
|
| 819 |
|
| 820 |
|
| 821 |
ttyset->c_cc[VWERASE] = 0x17; |
| 822 |
ttyset->c_cc[VLNEXT] = 0x16; |
| 823 |
ttyset->c_cc[VEOL2] = '\n'; |
| 824 |
LEAVE( "init_termios" ); |
| 825 |
return TRUE; |
| 826 |
|
| 827 |
} |
| 828 |
|
| 829 |
|
| 830 |
|
| 831 |
|
| 832 |
|
| 833 |
|
| 834 |
|
| 835 |
|
| 836 |
|
| 837 |
|
| 838 |
|
| 839 |
|
| 840 |
int port_opened( const char *filename ) |
| 841 |
{ |
| 842 |
struct termios_list *index = first_tl; |
| 843 |
|
| 844 |
ENTER( "port_opened" ); |
| 845 |
if ( ! index ) |
| 846 |
return 0; |
| 847 |
if( !strcmp( index->filename, filename ) ) |
| 848 |
return index->fd; |
| 849 |
while ( index->next ) |
| 850 |
{ |
| 851 |
index = index->next; |
| 852 |
if( !strcmp( index->filename, filename ) ) |
| 853 |
return index->fd; |
| 854 |
} |
| 855 |
LEAVE( "port_opened" ); |
| 856 |
return 0; |
| 857 |
} |
| 858 |
|
| 859 |
|
| 860 |
|
| 861 |
|
| 862 |
|
| 863 |
|
| 864 |
|
| 865 |
|
| 866 |
|
| 867 |
|
| 868 |
|
| 869 |
|
| 870 |
|
| 871 |
|
| 872 |
|
| 873 |
|
| 874 |
|
| 875 |
|
| 876 |
|
| 877 |
|
| 878 |
|
| 879 |
int open_port( struct termios_list *port ) |
| 880 |
{ |
| 881 |
ENTER( "open_port" ); |
| 882 |
port->hComm = CreateFile( port->filename, |
| 883 |
GENERIC_READ | GENERIC_WRITE, |
| 884 |
0, |
| 885 |
0, |
| 886 |
OPEN_EXISTING, |
| 887 |
FILE_FLAG_OVERLAPPED, |
| 888 |
0 |
| 889 |
); |
| 890 |
if ( port->hComm == INVALID_HANDLE_VALUE ) |
| 891 |
{ |
| 892 |
YACK(); |
| 893 |
set_errno( EINVAL ); |
| 894 |
|
| 895 |
|
| 896 |
|
| 897 |
return -1; |
| 898 |
} |
| 899 |
if( !SetupComm( port->hComm, 2048, 1024 ) ) |
| 900 |
{ |
| 901 |
YACK(); |
| 902 |
return -1; |
| 903 |
} |
| 904 |
|
| 905 |
memset( &port->rol, 0, sizeof( OVERLAPPED ) ); |
| 906 |
memset( &port->wol, 0, sizeof( OVERLAPPED ) ); |
| 907 |
memset( &port->sol, 0, sizeof( OVERLAPPED ) ); |
| 908 |
|
| 909 |
port->rol.hEvent = CreateEvent( NULL, TRUE, FALSE, NULL ); |
| 910 |
|
| 911 |
if ( !port->rol.hEvent ) |
| 912 |
{ |
| 913 |
YACK(); |
| 914 |
report( "Could not create read overlapped\n" ); |
| 915 |
goto fail; |
| 916 |
} |
| 917 |
|
| 918 |
port->sol.hEvent = CreateEvent( NULL, TRUE, FALSE, NULL ); |
| 919 |
|
| 920 |
if ( !port->sol.hEvent ) |
| 921 |
{ |
| 922 |
YACK(); |
| 923 |
report( "Could not create select overlapped\n" ); |
| 924 |
goto fail; |
| 925 |
} |
| 926 |
port->wol.hEvent = CreateEvent( NULL, TRUE, FALSE, NULL ); |
| 927 |
|
| 928 |
if ( !port->wol.hEvent ) |
| 929 |
{ |
| 930 |
YACK(); |
| 931 |
report( "Could not create write overlapped\n" ); |
| 932 |
goto fail; |
| 933 |
} |
| 934 |
LEAVE( "open_port" ); |
| 935 |
return( 0 ); |
| 936 |
fail: |
| 937 |
return( -1 ); |
| 938 |
} |
| 939 |
|
| 940 |
|
| 941 |
|
| 942 |
|
| 943 |
|
| 944 |
|
| 945 |
|
| 946 |
|
| 947 |
|
| 948 |
|
| 949 |
|
| 950 |
|
| 951 |
|
| 952 |
|
| 953 |
|
| 954 |
struct termios_list *find_port( int fd ) |
| 955 |
{ |
| 956 |
|
| 957 |
char message[80]; |
| 958 |
struct termios_list *index = first_tl; |
| 959 |
|
| 960 |
ENTER( "find_port" ); |
| 961 |
if ( fd <= 0 || !first_tl ) goto fail; |
| 962 |
|
| 963 |
while( index->fd ) |
| 964 |
{ |
| 965 |
if ( index->fd == fd ) |
| 966 |
{ |
| 967 |
LEAVE( "find_port" ); |
| 968 |
return index; |
| 969 |
} |
| 970 |
if ( !index->next ) |
| 971 |
break; |
| 972 |
index = index->next; |
| 973 |
} |
| 974 |
fail: |
| 975 |
sprintf( message, "No info known about the port. %i\n", fd ); |
| 976 |
report( message ); |
| 977 |
set_errno( EBADF ); |
| 978 |
LEAVE( "find_port" ); |
| 979 |
return NULL; |
| 980 |
} |
| 981 |
|
| 982 |
|
| 983 |
|
| 984 |
|
| 985 |
|
| 986 |
|
| 987 |
|
| 988 |
|
| 989 |
|
| 990 |
|
| 991 |
|
| 992 |
|
| 993 |
int get_free_fd() |
| 994 |
{ |
| 995 |
int next, last; |
| 996 |
struct termios_list *index = first_tl; |
| 997 |
|
| 998 |
ENTER( "get_free_fd" ); |
| 999 |
if ( !index ) |
| 1000 |
{ |
| 1001 |
return( 1 ); |
| 1002 |
} |
| 1003 |
if ( !index->fd ) |
| 1004 |
{ |
| 1005 |
report( "!index->fd\n" ); |
| 1006 |
return( 1 ); |
| 1007 |
} |
| 1008 |
if ( index->fd > 1) |
| 1009 |
{ |
| 1010 |
first_tl = index; |
| 1011 |
return ( 1 ); |
| 1012 |
} |
| 1013 |
|
| 1014 |
last = index->fd; |
| 1015 |
|
| 1016 |
while( index->next ) |
| 1017 |
{ |
| 1018 |
next = index->next->fd; |
| 1019 |
if ( next != last + 1 ) |
| 1020 |
{ |
| 1021 |
return( last + 1 ); |
| 1022 |
|
| 1023 |
} |
| 1024 |
index = index->next; |
| 1025 |
last = next; |
| 1026 |
} |
| 1027 |
LEAVE( "get_free_fd" ); |
| 1028 |
return( index->fd + 1 ); |
| 1029 |
} |
| 1030 |
|
| 1031 |
|
| 1032 |
|
| 1033 |
|
| 1034 |
|
| 1035 |
|
| 1036 |
|
| 1037 |
|
| 1038 |
|
| 1039 |
|
| 1040 |
|
| 1041 |
|
| 1042 |
struct termios_list *add_port( const char *filename ) |
| 1043 |
{ |
| 1044 |
struct termios_list *index = first_tl; |
| 1045 |
struct termios_list *port; |
| 1046 |
|
| 1047 |
ENTER( "add_port" ); |
| 1048 |
|
| 1049 |
port = malloc( sizeof( struct termios_list ) ); |
| 1050 |
if( !port ) |
| 1051 |
goto fail; |
| 1052 |
memset( port, 0, sizeof( struct termios_list ) ); |
| 1053 |
|
| 1054 |
port->ttyset = malloc( sizeof( struct termios ) ); |
| 1055 |
if( ! port->ttyset ) |
| 1056 |
goto fail; |
| 1057 |
memset( port->ttyset, 0, sizeof( struct termios ) ); |
| 1058 |
|
| 1059 |
port->sstruct = malloc( sizeof( struct serial_struct ) ); |
| 1060 |
if( ! port->sstruct ) |
| 1061 |
goto fail; |
| 1062 |
memset( port->sstruct, 0, sizeof( struct serial_struct ) ); |
| 1063 |
port->sis = malloc( sizeof( struct serial_icounter_struct ) ); |
| 1064 |
if( ! port->sis ) |
| 1065 |
goto fail; |
| 1066 |
memset( port->sis, 0, sizeof( struct serial_icounter_struct ) ); |
| 1067 |
|
| 1068 |
|
| 1069 |
|
| 1070 |
|
| 1071 |
|
| 1072 |
|
| 1073 |
|
| 1074 |
port->MSR = 0; |
| 1075 |
|
| 1076 |
strcpy(port->filename, filename ); |
| 1077 |
|
| 1078 |
if( ! port->filename ) |
| 1079 |
goto fail; |
| 1080 |
|
| 1081 |
port->fd = get_free_fd(); |
| 1082 |
|
| 1083 |
|
| 1084 |
if ( !first_tl ) |
| 1085 |
{ |
| 1086 |
port->prev = NULL; |
| 1087 |
port->next = NULL; |
| 1088 |
first_tl = port; |
| 1089 |
} |
| 1090 |
else |
| 1091 |
{ |
| 1092 |
while ( ( index->fd < port->fd ) && index->next ) |
| 1093 |
index = index->next; |
| 1094 |
if ( index->fd > port->fd ) |
| 1095 |
{ |
| 1096 |
|
| 1097 |
port->prev = index->prev; |
| 1098 |
port->next=index; |
| 1099 |
index->prev->next = port; |
| 1100 |
index->prev = port; |
| 1101 |
} |
| 1102 |
else |
| 1103 |
{ |
| 1104 |
|
| 1105 |
port->prev = index; |
| 1106 |
port->next = NULL; |
| 1107 |
index->next = port; |
| 1108 |
} |
| 1109 |
} |
| 1110 |
LEAVE( "add_port" ); |
| 1111 |
return port; |
| 1112 |
|
| 1113 |
fail: |
| 1114 |
report( "add_port: Out Of Memory\n"); |
| 1115 |
if ( port->ttyset ) free( port->ttyset ); |
| 1116 |
if ( port->astruct ) free( port->astruct ); |
| 1117 |
if ( port->sstruct ) free( port->sstruct ); |
| 1118 |
if ( port->sis ) free( port->sis ); |
| 1119 |
|
| 1120 |
|
| 1121 |
|
| 1122 |
if ( port ) free( port ); |
| 1123 |
return port; |
| 1124 |
} |
| 1125 |
|
| 1126 |
|
| 1127 |
|
| 1128 |
|
| 1129 |
|
| 1130 |
|
| 1131 |
|
| 1132 |
|
| 1133 |
|
| 1134 |
|
| 1135 |
|
| 1136 |
|
| 1137 |
int check_port_capabilities( struct termios_list *index ) |
| 1138 |
{ |
| 1139 |
COMMPROP cp; |
| 1140 |
DCB dcb; |
| 1141 |
char message[80]; |
| 1142 |
|
| 1143 |
ENTER( "check_port_capabilities" ); |
| 1144 |
|
| 1145 |
GetCommProperties( index->hComm, &cp ); |
| 1146 |
if ( !( cp.dwProvCapabilities & PCF_DTRDSR ) ) |
| 1147 |
{ |
| 1148 |
sprintf( message, |
| 1149 |
"%s: no DTR & DSR support\n", index->filename ); |
| 1150 |
report( message ); |
| 1151 |
} |
| 1152 |
if ( !( cp.dwProvCapabilities & PCF_RLSD ) ) |
| 1153 |
{ |
| 1154 |
sprintf( message, "%s: no carrier detect (RLSD) support\n", |
| 1155 |
index->filename ); |
| 1156 |
report( message ); |
| 1157 |
} |
| 1158 |
if ( !( cp.dwProvCapabilities & PCF_RTSCTS ) ) |
| 1159 |
{ |
| 1160 |
sprintf( message, |
| 1161 |
"%s: no RTS & CTS support\n", index->filename ); |
| 1162 |
report( message ); |
| 1163 |
} |
| 1164 |
if ( !( cp.dwProvCapabilities & PCF_TOTALTIMEOUTS ) ) |
| 1165 |
{ |
| 1166 |
sprintf( message, "%s: no timeout support\n", index->filename ); |
| 1167 |
report( message ); |
| 1168 |
} |
| 1169 |
if ( !GetCommState( index->hComm, &dcb ) ) |
| 1170 |
{ |
| 1171 |
YACK(); |
| 1172 |
report( "GetCommState\n" ); |
| 1173 |
return -1; |
| 1174 |
} |
| 1175 |
LEAVE( "check_port_capabilities" ); |
| 1176 |
return 0; |
| 1177 |
|
| 1178 |
} |
| 1179 |
|
| 1180 |
|
| 1181 |
|
| 1182 |
|
| 1183 |
|
| 1184 |
|
| 1185 |
|
| 1186 |
|
| 1187 |
|
| 1188 |
|
| 1189 |
|
| 1190 |
|
| 1191 |
int serial_open( const char *filename, int flags, ... ) |
| 1192 |
{ |
| 1193 |
struct termios_list *index; |
| 1194 |
char message[80]; |
| 1195 |
|
| 1196 |
ENTER( "serial_open" ); |
| 1197 |
if ( port_opened( filename ) ) |
| 1198 |
{ |
| 1199 |
report( "Port is already opened\n" ); |
| 1200 |
return( -1 ); |
| 1201 |
} |
| 1202 |
index = add_port( filename ); |
| 1203 |
if( !index ) |
| 1204 |
{ |
| 1205 |
report( "serial_open !index\n" ); |
| 1206 |
return( -1 ); |
| 1207 |
} |
| 1208 |
|
| 1209 |
index->interrupt = 0; |
| 1210 |
index->tx_happened = 0; |
| 1211 |
if ( open_port( index ) ) |
| 1212 |
{ |
| 1213 |
sprintf( message, "serial_open(): Invalid Port Reference for %s\n", |
| 1214 |
filename ); |
| 1215 |
report( message ); |
| 1216 |
serial_close( index->fd ); |
| 1217 |
return -1; |
| 1218 |
} |
| 1219 |
|
| 1220 |
if( check_port_capabilities( index ) ) |
| 1221 |
{ |
| 1222 |
report( "check_port_capabilites!" ); |
| 1223 |
serial_close( index->fd ); |
| 1224 |
return -1; |
| 1225 |
} |
| 1226 |
|
| 1227 |
init_termios( index->ttyset ); |
| 1228 |
init_serial_struct( index->sstruct ); |
| 1229 |
|
| 1230 |
|
| 1231 |
tcsetattr( index->fd, 0, index->ttyset ); |
| 1232 |
|
| 1233 |
|
| 1234 |
if ( flags & O_NONBLOCK ) |
| 1235 |
index->open_flags = O_NONBLOCK; |
| 1236 |
else |
| 1237 |
index->open_flags = 0; |
| 1238 |
|
| 1239 |
|
| 1240 |
if( !first_tl->hComm ) |
| 1241 |
{ |
| 1242 |
sprintf( message, "open(): Invalid Port Reference for %s\n", |
| 1243 |
index->filename ); |
| 1244 |
report( message ); |
| 1245 |
} |
| 1246 |
if ( first_tl->hComm == INVALID_HANDLE_VALUE ) |
| 1247 |
report( "serial_open: test\n" ); |
| 1248 |
LEAVE( "serial_open" ); |
| 1249 |
return( index->fd ); |
| 1250 |
} |
| 1251 |
|
| 1252 |
|
| 1253 |
|
| 1254 |
|
| 1255 |
|
| 1256 |
|
| 1257 |
|
| 1258 |
|
| 1259 |
|
| 1260 |
|
| 1261 |
|
| 1262 |
|
| 1263 |
|
| 1264 |
|
| 1265 |
|
| 1266 |
int serial_write( int fd, char *Str, int length ) |
| 1267 |
{ |
| 1268 |
unsigned long nBytes; |
| 1269 |
struct termios_list *index; |
| 1270 |
|
| 1271 |
int old_flag; |
| 1272 |
|
| 1273 |
ENTER( "serial_write" ); |
| 1274 |
|
| 1275 |
if ( fd <= 0 ) |
| 1276 |
{ |
| 1277 |
return 0; |
| 1278 |
} |
| 1279 |
index = find_port( fd ); |
| 1280 |
if ( !index ) |
| 1281 |
{ |
| 1282 |
LEAVE( "serial_write"); |
| 1283 |
return -1; |
| 1284 |
} |
| 1285 |
old_flag = index->event_flag; |
| 1286 |
|
| 1287 |
|
| 1288 |
|
| 1289 |
|
| 1290 |
|
| 1291 |
index->wol.Offset = index->wol.OffsetHigh = 0; |
| 1292 |
ResetEvent( index->wol.hEvent ); |
| 1293 |
if ( !WriteFile( index->hComm, Str, length, &nBytes, &index->wol ) ) |
| 1294 |
{ |
| 1295 |
WaitForSingleObject( index->wol.hEvent,100 ); |
| 1296 |
if ( GetLastError() != ERROR_IO_PENDING ) |
| 1297 |
{ |
| 1298 |
|
| 1299 |
report( "serial_write error\n" ); |
| 1300 |
|
| 1301 |
YACK(); |
| 1302 |
errno = EIO; |
| 1303 |
nBytes=-1; |
| 1304 |
goto end; |
| 1305 |
} |
| 1306 |
|
| 1307 |
else while( !GetOverlappedResult( index->hComm, &index->wol, |
| 1308 |
&nBytes, TRUE ) ) |
| 1309 |
{ |
| 1310 |
if ( GetLastError() != ERROR_IO_INCOMPLETE ) |
| 1311 |
{ |
| 1312 |
|
| 1313 |
YACK(); |
| 1314 |
errno = EIO; |
| 1315 |
nBytes = -1; |
| 1316 |
goto end; |
| 1317 |
|
| 1318 |
} |
| 1319 |
} |
| 1320 |
} |
| 1321 |
else |
| 1322 |
{ |
| 1323 |
|
| 1324 |
|
| 1325 |
|
| 1326 |
|
| 1327 |
} |
| 1328 |
end: |
| 1329 |
|
| 1330 |
index->event_flag |= EV_TXEMPTY; |
| 1331 |
|
| 1332 |
SetCommMask( index->hComm, index->event_flag ); |
| 1333 |
|
| 1334 |
index->event_flag = old_flag; |
| 1335 |
index->tx_happened = 1; |
| 1336 |
LEAVE( "serial_write" ); |
| 1337 |
return nBytes; |
| 1338 |
} |
| 1339 |
|
| 1340 |
|
| 1341 |
|
| 1342 |
|
| 1343 |
|
| 1344 |
|
| 1345 |
|
| 1346 |
|
| 1347 |
|
| 1348 |
|
| 1349 |
|
| 1350 |
|
| 1351 |
|
| 1352 |
|
| 1353 |
int serial_read( int fd, void *vb, int size ) |
| 1354 |
{ |
| 1355 |
long start, now; |
| 1356 |
unsigned long nBytes = 0, total = 0, error; |
| 1357 |
|
| 1358 |
int err, vmin; |
| 1359 |
struct termios_list *index; |
| 1360 |
char message[80]; |
| 1361 |
COMSTAT stat; |
| 1362 |
clock_t c; |
| 1363 |
unsigned char *dest = vb; |
| 1364 |
|
| 1365 |
start = GetTickCount(); |
| 1366 |
ENTER( "serial_read" ); |
| 1367 |
|
| 1368 |
if ( fd <= 0 ) |
| 1369 |
{ |
| 1370 |
return 0; |
| 1371 |
} |
| 1372 |
index = find_port( fd ); |
| 1373 |
if ( !index ) |
| 1374 |
{ |
| 1375 |
LEAVE( "serial_read" ); |
| 1376 |
return -1; |
| 1377 |
} |
| 1378 |
|
| 1379 |
|
| 1380 |
|
| 1381 |
|
| 1382 |
|
| 1383 |
|
| 1384 |
|
| 1385 |
|
| 1386 |
if ( index->open_flags & O_NONBLOCK ) |
| 1387 |
{ |
| 1388 |
int ret; |
| 1389 |
vmin = 0; |
| 1390 |
|
| 1391 |
do { |
| 1392 |
#ifdef DEBUG_VERBOSE |
| 1393 |
report( "vmin=0\n" ); |
| 1394 |
#endif |
| 1395 |
ret = ClearErrors( index, &stat); |
| 1396 |
|
| 1397 |
now = GetTickCount(); |
| 1398 |
if ( index->ttyset->c_cc[VTIME] && |
| 1399 |
now-start >= (index->ttyset->c_cc[VTIME]*100)) { |
| 1400 |
|
| 1401 |
|
| 1402 |
|
| 1403 |
|
| 1404 |
return total; |
| 1405 |
} |
| 1406 |
} while( stat.cbInQue < size && size > 1 ); |
| 1407 |
} |
| 1408 |
else |
| 1409 |
{ |
| 1410 |
|
| 1411 |
|
| 1412 |
#ifdef DEBUG_VERBOSE |
| 1413 |
report( "vmin!=0\n" ); |
| 1414 |
#endif |
| 1415 |
vmin = index->ttyset->c_cc[VMIN]; |
| 1416 |
|
| 1417 |
c = clock() + index->ttyset->c_cc[VTIME] * CLOCKS_PER_SEC / 10; |
| 1418 |
do { |
| 1419 |
error = ClearErrors( index, &stat); |
| 1420 |
usleep(1000); |
| 1421 |
} while ( c > clock() ); |
| 1422 |
|
| 1423 |
} |
| 1424 |
|
| 1425 |
total = 0; |
| 1426 |
while ( size > 0 ) |
| 1427 |
{ |
| 1428 |
nBytes = 0; |
| 1429 |
|
| 1430 |
|
| 1431 |
index->rol.Offset = index->rol.OffsetHigh = 0; |
| 1432 |
ResetEvent( index->rol.hEvent ); |
| 1433 |
|
| 1434 |
err = ReadFile( index->hComm, dest + total, size, &nBytes, &index->rol ); |
| 1435 |
#ifdef DEBUG_VERBOSE |
| 1436 |
|
| 1437 |
sprintf(message, " ========== ReadFile = %i %s\n", |
| 1438 |
( int ) nBytes, (char *) dest + total ); |
| 1439 |
report( message ); |
| 1440 |
#endif |
| 1441 |
size -= nBytes; |
| 1442 |
total += nBytes; |
| 1443 |
|
| 1444 |
if ( !err ) |
| 1445 |
{ |
| 1446 |
switch ( GetLastError() ) |
| 1447 |
{ |
| 1448 |
case ERROR_BROKEN_PIPE: |
| 1449 |
report( "ERROR_BROKEN_PIPE\n "); |
| 1450 |
nBytes = 0; |
| 1451 |
break; |
| 1452 |
case ERROR_MORE_DATA: |
| 1453 |
report( "ERROR_MORE_DATA\n" ); |
| 1454 |
break; |
| 1455 |
case ERROR_IO_PENDING: |
| 1456 |
while( ! GetOverlappedResult( |
| 1457 |
index->hComm, |
| 1458 |
&index->rol, |
| 1459 |
&nBytes, |
| 1460 |
TRUE ) ) |
| 1461 |
{ |
| 1462 |
if( GetLastError() != |
| 1463 |
ERROR_IO_INCOMPLETE ) |
| 1464 |
{ |
| 1465 |
ClearErrors( |
| 1466 |
index, |
| 1467 |
&stat); |
| 1468 |
return( total ); |
| 1469 |
} |
| 1470 |
} |
| 1471 |
size -= nBytes; |
| 1472 |
total += nBytes; |
| 1473 |
if (size > 0) { |
| 1474 |
now = GetTickCount(); |
| 1475 |
sprintf(message, "size > 0: spent=%ld have=%d\n", now-start, index->ttyset->c_cc[VTIME]*100); |
| 1476 |
report( message ); |
| 1477 |
|
| 1478 |
|
| 1479 |
if ( index->ttyset->c_cc[VTIME] && now-start >= (index->ttyset->c_cc[VTIME]*100)) { |
| 1480 |
report( "TO " ); |
| 1481 |
|
| 1482 |
return total; |
| 1483 |
} |
| 1484 |
} |
| 1485 |
sprintf(message, "end nBytes=%ld] ", nBytes); |
| 1486 |
report( message ); |
| 1487 |
report( "ERROR_IO_PENDING\n" ); |
| 1488 |
break; |
| 1489 |
default: |
| 1490 |
YACK(); |
| 1491 |
return -1; |
| 1492 |
} |
| 1493 |
} |
| 1494 |
else |
| 1495 |
{ |
| 1496 |
ClearErrors( index, &stat); |
| 1497 |
return( total ); |
| 1498 |
} |
| 1499 |
} |
| 1500 |
LEAVE( "serial_read" ); |
| 1501 |
return total; |
| 1502 |
} |
| 1503 |
|
| 1504 |
#ifdef asdf |
| 1505 |
int serial_read( int fd, void *vb, int size ) |
| 1506 |
{ |
| 1507 |
long start, now; |
| 1508 |
unsigned long nBytes = 0, total = 0, error; |
| 1509 |
|
| 1510 |
int err, vmin; |
| 1511 |
struct termios_list *index; |
| 1512 |
char message[80]; |
| 1513 |
COMSTAT Stat; |
| 1514 |
clock_t c; |
| 1515 |
unsigned char *dest = vb; |
| 1516 |
|
| 1517 |
start = GetTickCount(); |
| 1518 |
ENTER( "serial_read" ); |
| 1519 |
|
| 1520 |
if ( fd <= 0 ) |
| 1521 |
{ |
| 1522 |
printf("1\n"); |
| 1523 |
return 0; |
| 1524 |
} |
| 1525 |
index = find_port( fd ); |
| 1526 |
if ( !index ) |
| 1527 |
{ |
| 1528 |
LEAVE( "serial_read 7" ); |
| 1529 |
errno = EIO; |
| 1530 |
printf("2\n"); |
| 1531 |
return -1; |
| 1532 |
} |
| 1533 |
|
| 1534 |
|
| 1535 |
|
| 1536 |
|
| 1537 |
|
| 1538 |
|
| 1539 |
|
| 1540 |
|
| 1541 |
ClearErrors( index, &Stat ); |
| 1542 |
if ( index->open_flags & O_NONBLOCK ) |
| 1543 |
{ |
| 1544 |
int ret; |
| 1545 |
vmin = 0; |
| 1546 |
|
| 1547 |
do { |
| 1548 |
#ifdef DEBUG_VERBOSE |
| 1549 |
report( "vmin=0\n" ); |
| 1550 |
#endif |
| 1551 |
ret = ClearErrors( index, &Stat); |
| 1552 |
|
| 1553 |
now = GetTickCount(); |
| 1554 |
if ( index->ttyset->c_cc[VTIME] && |
| 1555 |
now-start >= (index->ttyset->c_cc[VTIME]*100)) { |
| 1556 |
|
| 1557 |
|
| 1558 |
|
| 1559 |
|
| 1560 |
errno = EAGAIN; |
| 1561 |
printf("3\n"); |
| 1562 |
return -1; |
| 1563 |
} |
| 1564 |
} while( Stat.cbInQue < size && size > 1 ); |
| 1565 |
} |
| 1566 |
else |
| 1567 |
{ |
| 1568 |
|
| 1569 |
|
| 1570 |
#ifdef DEBUG_VERBOSE |
| 1571 |
report( "vmin!=0\n" ); |
| 1572 |
#endif |
| 1573 |
vmin = index->ttyset->c_cc[VMIN]; |
| 1574 |
|
| 1575 |
c = clock() + index->ttyset->c_cc[VTIME] * CLOCKS_PER_SEC / 10; |
| 1576 |
do { |
| 1577 |
error = ClearErrors( index, &Stat); |
| 1578 |
usleep(1000); |
| 1579 |
} while ( c > clock() ); |
| 1580 |
|
| 1581 |
} |
| 1582 |
|
| 1583 |
total = 0; |
| 1584 |
while ( size > 0 ) |
| 1585 |
{ |
| 1586 |
nBytes = 0; |
| 1587 |
|
| 1588 |
|
| 1589 |
index->rol.Offset = index->rol.OffsetHigh = 0; |
| 1590 |
ResetEvent( index->rol.hEvent ); |
| 1591 |
|
| 1592 |
err = ReadFile( index->hComm, dest + total, size, &nBytes, &index->rol ); |
| 1593 |
#ifdef DEBUG_VERBOSE |
| 1594 |
|
| 1595 |
sprintf(message, " ========== ReadFile = %i %s\n", |
| 1596 |
( int ) nBytes, (char *) dest + total ); |
| 1597 |
report( message ); |
| 1598 |
#endif |
| 1599 |
size -= nBytes; |
| 1600 |
total += nBytes; |
| 1601 |
|
| 1602 |
if ( !err ) |
| 1603 |
{ |
| 1604 |
switch ( GetLastError() ) |
| 1605 |
{ |
| 1606 |
case ERROR_BROKEN_PIPE: |
| 1607 |
report( "ERROR_BROKEN_PIPE\n "); |
| 1608 |
nBytes = 0; |
| 1609 |
break; |
| 1610 |
case ERROR_MORE_DATA: |
| 1611 |
report( "ERROR_MORE_DATA\n" ); |
| 1612 |
break; |
| 1613 |
case ERROR_IO_PENDING: |
| 1614 |
while( ! GetOverlappedResult( |
| 1615 |
index->hComm, |
| 1616 |
&index->rol, |
| 1617 |
&nBytes, |
| 1618 |
TRUE ) ) |
| 1619 |
{ |
| 1620 |
if( GetLastError() != |
| 1621 |
ERROR_IO_INCOMPLETE ) |
| 1622 |
{ |
| 1623 |
ClearErrors( |
| 1624 |
index, |
| 1625 |
&Stat); |
| 1626 |
printf("4\n"); |
| 1627 |
return( total ); |
| 1628 |
} |
| 1629 |
} |
| 1630 |
size -= nBytes; |
| 1631 |
total += nBytes; |
| 1632 |
if (size > 0) { |
| 1633 |
now = GetTickCount(); |
| 1634 |
sprintf(message, "size > 0: spent=%ld have=%d\n", now-start, index->ttyset->c_cc[VTIME]*100); |
| 1635 |
report( message ); |
| 1636 |
|
| 1637 |
|
| 1638 |
if ( index->ttyset->c_cc[VTIME] && now-start >= (index->ttyset->c_cc[VTIME]*100)) { |
| 1639 |
report( "TO " ); |
| 1640 |
|
| 1641 |
printf("5\n"); |
| 1642 |
return total; |
| 1643 |
} |
| 1644 |
} |
| 1645 |
sprintf(message, "end nBytes=%ld] ", nBytes); |
| 1646 |
report( message ); |
| 1647 |
report( "ERROR_IO_PENDING\n" ); |
| 1648 |
break; |
| 1649 |
default: |
| 1650 |
YACK(); |
| 1651 |
errno = EIO; |
| 1652 |
printf("6\n"); |
| 1653 |
return -1; |
| 1654 |
} |
| 1655 |
} |
| 1656 |
else |
| 1657 |
{ |
| 1658 |
ClearErrors( index, &Stat); |
| 1659 |
printf("7\n"); |
| 1660 |
return( total ); |
| 1661 |
} |
| 1662 |
} |
| 1663 |
LEAVE( "serial_read" ); |
| 1664 |
ClearErrors( index, &Stat); |
| 1665 |
return total; |
| 1666 |
} |
| 1667 |
#endif |
| 1668 |
|
| 1669 |
|
| 1670 |
|
| 1671 |
|
| 1672 |
|
| 1673 |
|
| 1674 |
|
| 1675 |
|
| 1676 |
|
| 1677 |
|
| 1678 |
|
| 1679 |
|
| 1680 |
int cfsetospeed( struct termios *s_termios, speed_t speed ) |
| 1681 |
{ |
| 1682 |
char message[80]; |
| 1683 |
ENTER( "cfsetospeed" ); |
| 1684 |
|
| 1685 |
s_termios->c_cflag &= ~CBAUD; |
| 1686 |
if ( speed & ~CBAUD ) |
| 1687 |
{ |
| 1688 |
sprintf( message, "cfsetospeed: not speed: %#o\n", speed ); |
| 1689 |
report( message ); |
| 1690 |
|
| 1691 |
s_termios->c_cflag |= B38400; |
| 1692 |
s_termios->c_cflag |= CBAUDEX; |
| 1693 |
} |
| 1694 |
else if( speed ) |
| 1695 |
{ |
| 1696 |
s_termios->c_cflag |= speed; |
| 1697 |
} |
| 1698 |
else |
| 1699 |
{ |
| 1700 |
|
| 1701 |
s_termios->c_cflag |= B9600; |
| 1702 |
} |
| 1703 |
s_termios->c_ispeed = s_termios->c_ospeed = speed; |
| 1704 |
LEAVE( "cfsetospeed" ); |
| 1705 |
return 1; |
| 1706 |
} |
| 1707 |
|
| 1708 |
|
| 1709 |
|
| 1710 |
|
| 1711 |
|
| 1712 |
|
| 1713 |
|
| 1714 |
|
| 1715 |
|
| 1716 |
|
| 1717 |
|
| 1718 |
|
| 1719 |
int cfsetispeed( struct termios *s_termios, speed_t speed ) |
| 1720 |
{ |
| 1721 |
return cfsetospeed( s_termios, speed ); |
| 1722 |
} |
| 1723 |
|
| 1724 |
|
| 1725 |
|
| 1726 |
|
| 1727 |
|
| 1728 |
|
| 1729 |
|
| 1730 |
|
| 1731 |
|
| 1732 |
|
| 1733 |
|
| 1734 |
|
| 1735 |
int cfsetspeed( struct termios *s_termios, speed_t speed ) |
| 1736 |
{ |
| 1737 |
return cfsetospeed( s_termios, speed ); |
| 1738 |
} |
| 1739 |
|
| 1740 |
|
| 1741 |
|
| 1742 |
|
| 1743 |
|
| 1744 |
|
| 1745 |
|
| 1746 |
|
| 1747 |
|
| 1748 |
|
| 1749 |
|
| 1750 |
|
| 1751 |
speed_t cfgetospeed( struct termios *s_termios ) |
| 1752 |
{ |
| 1753 |
ENTER( "cfgetospeed" ); |
| 1754 |
return s_termios->c_ospeed; |
| 1755 |
} |
| 1756 |
|
| 1757 |
|
| 1758 |
|
| 1759 |
|
| 1760 |
|
| 1761 |
|
| 1762 |
|
| 1763 |
|
| 1764 |
|
| 1765 |
|
| 1766 |
|
| 1767 |
|
| 1768 |
speed_t cfgetispeed( struct termios *s_termios ) |
| 1769 |
{ |
| 1770 |
ENTER( "cfgetospeed" ); |
| 1771 |
return s_termios->c_ispeed; |
| 1772 |
} |
| 1773 |
|
| 1774 |
|
| 1775 |
|
| 1776 |
|
| 1777 |
|
| 1778 |
|
| 1779 |
|
| 1780 |
|
| 1781 |
|
| 1782 |
|
| 1783 |
|
| 1784 |
int termios_to_DCB( struct termios *s_termios, DCB *dcb ) |
| 1785 |
{ |
| 1786 |
ENTER( "termios_to_DCB" ); |
| 1787 |
if ( !(s_termios->c_cflag & CBAUDEX) ) |
| 1788 |
s_termios->c_ispeed = s_termios->c_ospeed = s_termios->c_cflag & CBAUD; |
| 1789 |
dcb->BaudRate = B_to_CBR( s_termios->c_ispeed ); |
| 1790 |
dcb->ByteSize = termios_to_bytesize( s_termios->c_cflag ); |
| 1791 |
|
| 1792 |
if ( s_termios->c_cflag & PARENB ) |
| 1793 |
{ |
| 1794 |
if ( s_termios->c_cflag & PARODD |
| 1795 |
&& s_termios->c_cflag & CMSPAR ) |
| 1796 |
{ |
| 1797 |
dcb->Parity = MARKPARITY; |
| 1798 |
} |
| 1799 |
else if ( s_termios->c_cflag & PARODD ) |
| 1800 |
{ |
| 1801 |
dcb->Parity = ODDPARITY; |
| 1802 |
} |
| 1803 |
else if ( s_termios->c_cflag & CMSPAR ) |
| 1804 |
{ |
| 1805 |
dcb->Parity = SPACEPARITY; |
| 1806 |
} |
| 1807 |
else |
| 1808 |
{ |
| 1809 |
dcb->Parity = EVENPARITY; |
| 1810 |
} |
| 1811 |
} |
| 1812 |
else |
| 1813 |
{ |
| 1814 |
dcb->Parity = NOPARITY; |
| 1815 |
} |
| 1816 |
|
| 1817 |
if ( s_termios->c_cflag & CSTOPB ) |
| 1818 |
{ |
| 1819 |
if (dcb->ByteSize == 5) |
| 1820 |
{ |
| 1821 |
dcb->StopBits = ONE5STOPBITS; |
| 1822 |
} else dcb->StopBits = TWOSTOPBITS; |
| 1823 |
} |
| 1824 |
else |
| 1825 |
{ |
| 1826 |
dcb->StopBits = ONESTOPBIT; |
| 1827 |
} |
| 1828 |
|
| 1829 |
if ( s_termios->c_cflag & HARDWARE_FLOW_CONTROL ) |
| 1830 |
{ |
| 1831 |
dcb->fRtsControl = RTS_CONTROL_HANDSHAKE; |
| 1832 |
dcb->fOutxCtsFlow = TRUE; |
| 1833 |
} |
| 1834 |
else |
| 1835 |
{ |
| 1836 |
dcb->fRtsControl = RTS_CONTROL_DISABLE; |
| 1837 |
dcb->fOutxCtsFlow = FALSE; |
| 1838 |
} |
| 1839 |
|
| 1840 |
LEAVE( "termios_to_DCB" ); |
| 1841 |
return 0; |
| 1842 |
} |
| 1843 |
|
| 1844 |
|
| 1845 |
|
| 1846 |
|
| 1847 |
|
| 1848 |
|
| 1849 |
|
| 1850 |
|
| 1851 |
|
| 1852 |
|
| 1853 |
|
| 1854 |
int DCB_to_serial_struct( DCB *dcb, struct serial_struct *sstruct ) |
| 1855 |
{ |
| 1856 |
return( 0 ); |
| 1857 |
} |
| 1858 |
|
| 1859 |
|
| 1860 |
|
| 1861 |
|
| 1862 |
|
| 1863 |
|
| 1864 |
|
| 1865 |
|
| 1866 |
|
| 1867 |
|
| 1868 |
void DCB_to_termios( DCB *dcb, struct termios *s_termios ) |
| 1869 |
{ |
| 1870 |
ENTER( "DCB_to_termios" ); |
| 1871 |
s_termios->c_ispeed = CBR_to_B( dcb->BaudRate ); |
| 1872 |
s_termios->c_ospeed = s_termios->c_ispeed; |
| 1873 |
s_termios->c_cflag |= s_termios->c_ispeed & CBAUD; |
| 1874 |
LEAVE( "DCB_to_termios" ); |
| 1875 |
} |
| 1876 |
|
| 1877 |
|
| 1878 |
|
| 1879 |
|
| 1880 |
|
| 1881 |
|
| 1882 |
|
| 1883 |
|
| 1884 |
|
| 1885 |
|
| 1886 |
|
| 1887 |
void show_DCB( myDCB ) |
| 1888 |
{ |
| 1889 |
|
| 1890 |
#ifdef DEBUG_HOSED |
| 1891 |
char message[80]; |
| 1892 |
|
| 1893 |
sprintf( message, "DCBlength: %ld\n", myDCB.DCBlength ); |
| 1894 |
report( message ); |
| 1895 |
sprintf( "BaudRate: %ld\n", myDCB.BaudRate ); |
| 1896 |
report( message ); |
| 1897 |
if ( myDCB.fBinary ) |
| 1898 |
report( "fBinary\n" ); |
| 1899 |
if ( myDCB.fParity ) |
| 1900 |
{ |
| 1901 |
report( "fParity: " ); |
| 1902 |
if ( myDCB.fErrorChar ) |
| 1903 |
{ |
| 1904 |
sprintf( message, "fErrorChar: %#x\n", myDCB.ErrorChar ); |
| 1905 |
report( message ); |
| 1906 |
} |
| 1907 |
else |
| 1908 |
{ |
| 1909 |
report( "fErrorChar == false\n" ); |
| 1910 |
} |
| 1911 |
} |
| 1912 |
if ( myDCB.fOutxCtsFlow ) |
| 1913 |
report( "fOutxCtsFlow\n" ); |
| 1914 |
if ( myDCB.fOutxDsrFlow ) |
| 1915 |
report( "fOutxDsrFlow\n" ); |
| 1916 |
if ( myDCB.fDtrControl & DTR_CONTROL_HANDSHAKE ); |
| 1917 |
report( "DTR_CONTROL_HANDSHAKE\n" ); |
| 1918 |
if ( myDCB.fDtrControl & DTR_CONTROL_ENABLE ); |
| 1919 |
report( "DTR_CONTROL_ENABLE\n" ); |
| 1920 |
if ( myDCB.fDtrControl & DTR_CONTROL_DISABLE ); |
| 1921 |
report( "DTR_CONTROL_DISABLE\n" ); |
| 1922 |
if ( myDCB.fDsrSensitivity ) |
| 1923 |
report( "fDsrSensitivity\n" ); |
| 1924 |
if ( myDCB.fTXContinueOnXoff ) |
| 1925 |
report( "fTXContinueOnXoff\n" ); |
| 1926 |
if ( myDCB.fOutX ) |
| 1927 |
report( "fOutX\n" ); |
| 1928 |
if ( myDCB.fInX ) |
| 1929 |
report( "fInX\n" ); |
| 1930 |
if ( myDCB.fNull ) |
| 1931 |
report( "fNull\n" ); |
| 1932 |
if ( myDCB.fRtsControl & RTS_CONTROL_TOGGLE ) |
| 1933 |
report( "RTS_CONTROL_TOGGLE\n" ); |
| 1934 |
if ( myDCB.fRtsControl == 0 ) |
| 1935 |
report( "RTS_CONTROL_HANDSHAKE ( fRtsControl==0 )\n" ); |
| 1936 |
if ( myDCB.fRtsControl & RTS_CONTROL_HANDSHAKE ) |
| 1937 |
report( "RTS_CONTROL_HANDSHAKE\n" ); |
| 1938 |
if ( myDCB.fRtsControl & RTS_CONTROL_ENABLE ) |
| 1939 |
report( "RTS_CONTROL_ENABLE\n" ); |
| 1940 |
if ( myDCB.fRtsControl & RTS_CONTROL_DISABLE ) |
| 1941 |
report( "RTS_CONTROL_DISABLE\n" ); |
| 1942 |
if ( myDCB.fAbortOnError ) |
| 1943 |
report( "fAbortOnError\n" ); |
| 1944 |
sprintf( message, "XonLim: %d\n", myDCB.XonLim ); |
| 1945 |
report( message ); |
| 1946 |
sprintf( message, "XoffLim: %d\n", myDCB.XoffLim ); |
| 1947 |
report( message ); |
| 1948 |
sprintf( message, "ByteSize: %d\n", myDCB.ByteSize ); |
| 1949 |
report( message ); |
| 1950 |
switch ( myDCB.Parity ) |
| 1951 |
{ |
| 1952 |
case EVENPARITY: |
| 1953 |
report( "EVENPARITY" ); |
| 1954 |
break; |
| 1955 |
case MARKPARITY: |
| 1956 |
report( "MARKPARITY" ); |
| 1957 |
break; |
| 1958 |
case NOPARITY: |
| 1959 |
report( "NOPARITY" ); |
| 1960 |
break; |
| 1961 |
case ODDPARITY: |
| 1962 |
report( "ODDPARITY" ); |
| 1963 |
break; |
| 1964 |
default: |
| 1965 |
sprintf( message, |
| 1966 |
"unknown Parity (%#x ):", myDCB.Parity ); |
| 1967 |
report( message ); |
| 1968 |
break; |
| 1969 |
} |
| 1970 |
report( "\n" ); |
| 1971 |
switch( myDCB.StopBits ) |
| 1972 |
{ |
| 1973 |
case ONESTOPBIT: |
| 1974 |
report( "ONESTOPBIT" ); |
| 1975 |
break; |
| 1976 |
case ONE5STOPBITS: |
| 1977 |
report( "ONE5STOPBITS" ); |
| 1978 |
break; |
| 1979 |
case TWOSTOPBITS: |
| 1980 |
report( "TWOSTOPBITS" ); |
| 1981 |
break; |
| 1982 |
default: |
| 1983 |
report( "unknown StopBits (%#x ):", myDCB.StopBits ); |
| 1984 |
break; |
| 1985 |
} |
| 1986 |
report( "\n" ); |
| 1987 |
sprintf( message, "XonChar: %#x\n", myDCB.XonChar ); |
| 1988 |
report( message ); |
| 1989 |
sprintf( message, "XoffChar: %#x\n", myDCB.XoffChar ); |
| 1990 |
report( message ); |
| 1991 |
sprintf( message, "EofChar: %#x\n", myDCB.EofChar ); |
| 1992 |
report( message ); |
| 1993 |
sprintf( message, "EvtChar: %#x\n", myDCB.EvtChar ); |
| 1994 |
report( message ); |
| 1995 |
report( "\n" ); |
| 1996 |
#endif |
| 1997 |
} |
| 1998 |
|
| 1999 |
|
| 2000 |
|
| 2001 |
|
| 2002 |
|
| 2003 |
|
| 2004 |
|
| 2005 |
|
| 2006 |
|
| 2007 |
|
| 2008 |
|
| 2009 |
|
| 2010 |
int tcgetattr( int fd, struct termios *s_termios ) |
| 2011 |
{ |
| 2012 |
DCB myDCB; |
| 2013 |
COMMTIMEOUTS timeouts; |
| 2014 |
struct termios_list *index; |
| 2015 |
char message[80]; |
| 2016 |
|
| 2017 |
ENTER( "tcgetattr" ); |
| 2018 |
if ( fd <= 0 ) |
| 2019 |
return 0; |
| 2020 |
index = find_port( fd ); |
| 2021 |
if ( !index ) |
| 2022 |
{ |
| 2023 |
LEAVE( "tcgetattr" ); |
| 2024 |
return -1; |
| 2025 |
} |
| 2026 |
if ( !GetCommState( index->hComm, &myDCB ) ) |
| 2027 |
{ |
| 2028 |
sprintf( message, "GetCommState failed\n" ); |
| 2029 |
report( message ); |
| 2030 |
return -1; |
| 2031 |
} |
| 2032 |
memcpy( s_termios, index->ttyset, sizeof( struct termios ) ); |
| 2033 |
|
| 2034 |
show_DCB( myDCB ); |
| 2035 |
|
| 2036 |
|
| 2037 |
|
| 2038 |
if ( myDCB.fParity ) |
| 2039 |
{ |
| 2040 |
s_termios->c_iflag |= INPCK; |
| 2041 |
s_termios->c_iflag &= ~IGNPAR; |
| 2042 |
} else { |
| 2043 |
s_termios->c_iflag &= ~INPCK; |
| 2044 |
s_termios->c_iflag |= IGNPAR; |
| 2045 |
} |
| 2046 |
|
| 2047 |
|
| 2048 |
|
| 2049 |
if ( myDCB.fOutX ) |
| 2050 |
{ |
| 2051 |
s_termios->c_iflag |= IXON; |
| 2052 |
} |
| 2053 |
else |
| 2054 |
{ |
| 2055 |
|
| 2056 |
s_termios->c_iflag &= ~IXON; |
| 2057 |
} |
| 2058 |
if ( myDCB.fInX ) |
| 2059 |
{ |
| 2060 |
s_termios->c_iflag |= IXOFF; |
| 2061 |
} |
| 2062 |
else |
| 2063 |
{ |
| 2064 |
|
| 2065 |
s_termios->c_iflag &= ~IXOFF; |
| 2066 |
} |
| 2067 |
|
| 2068 |
if ( myDCB.fTXContinueOnXoff ) |
| 2069 |
{ |
| 2070 |
s_termios->c_iflag |= IXANY; |
| 2071 |
} |
| 2072 |
else |
| 2073 |
{ |
| 2074 |
|
| 2075 |
s_termios->c_iflag &= ~IXANY; |
| 2076 |
} |
| 2077 |
|
| 2078 |
|
| 2079 |
|
| 2080 |
|
| 2081 |
|
| 2082 |
|
| 2083 |
|
| 2084 |
if ( myDCB.StopBits == TWOSTOPBITS ) |
| 2085 |
{ |
| 2086 |
s_termios->c_cflag |= CSTOPB; |
| 2087 |
} else if ( myDCB.StopBits == ONE5STOPBITS ) |
| 2088 |
{ |
| 2089 |
s_termios->c_cflag |= CSTOPB; |
| 2090 |
s_termios->c_cflag |= CS5; |
| 2091 |
} else if ( myDCB.StopBits == ONESTOPBIT ) |
| 2092 |
{ |
| 2093 |
s_termios->c_cflag &= ~CSTOPB; |
| 2094 |
} |
| 2095 |
|
| 2096 |
|
| 2097 |
s_termios->c_cflag &= ~( PARENB | PARODD | CMSPAR ); |
| 2098 |
myDCB.fParity = 1; |
| 2099 |
if( myDCB.fParity ) |
| 2100 |
{ |
| 2101 |
report( "tcgetattr getting parity\n" ); |
| 2102 |
s_termios->c_cflag |= PARENB; |
| 2103 |
if ( myDCB.Parity == MARKPARITY ) |
| 2104 |
{ |
| 2105 |
s_termios->c_cflag |= ( PARODD | CMSPAR ); |
| 2106 |
} |
| 2107 |
else if ( myDCB.Parity == SPACEPARITY ) |
| 2108 |
{ |
| 2109 |
s_termios->c_cflag |= CMSPAR; |
| 2110 |
} |
| 2111 |
else if ( myDCB.Parity == ODDPARITY ) |
| 2112 |
{ |
| 2113 |
report( "ODDPARITY\n" ); |
| 2114 |
s_termios->c_cflag |= PARODD; |
| 2115 |
} |
| 2116 |
else if ( myDCB.Parity == EVENPARITY ) |
| 2117 |
{ |
| 2118 |
report( "EVENPARITY\n" ); |
| 2119 |
s_termios->c_cflag &= ~PARODD; |
| 2120 |
} |
| 2121 |
else if ( myDCB.Parity == NOPARITY ) |
| 2122 |
{ |
| 2123 |
s_termios->c_cflag &= ~(PARODD | CMSPAR | PARENB); |
| 2124 |
} |
| 2125 |
} else |
| 2126 |
{ |
| 2127 |
s_termios->c_cflag &= ~PARENB; |
| 2128 |
} |
| 2129 |
|
| 2130 |
s_termios->c_cflag |= bytesize_to_termios( myDCB.ByteSize ); |
| 2131 |
|
| 2132 |
if (( myDCB.fOutxCtsFlow == TRUE ) || |
| 2133 |
( myDCB.fRtsControl == RTS_CONTROL_HANDSHAKE)) |
| 2134 |
{ |
| 2135 |
s_termios->c_cflag |= HARDWARE_FLOW_CONTROL; |
| 2136 |
} |
| 2137 |
else |
| 2138 |
{ |
| 2139 |
s_termios->c_cflag &= ~HARDWARE_FLOW_CONTROL; |
| 2140 |
} |
| 2141 |
|
| 2142 |
|
| 2143 |
|
| 2144 |
|
| 2145 |
|
| 2146 |
|
| 2147 |
|
| 2148 |
|
| 2149 |
|
| 2150 |
|
| 2151 |
|
| 2152 |
|
| 2153 |
|
| 2154 |
|
| 2155 |
|
| 2156 |
|
| 2157 |
|
| 2158 |
|
| 2159 |
|
| 2160 |
|
| 2161 |
|
| 2162 |
|
| 2163 |
|
| 2164 |
|
| 2165 |
if ( !GetCommTimeouts( index->hComm, &timeouts ) ) |
| 2166 |
{ |
| 2167 |
YACK(); |
| 2168 |
report( "GetCommTimeouts\n" ); |
| 2169 |
return -1; |
| 2170 |
} |
| 2171 |
|
| 2172 |
s_termios->c_cc[VTIME] = timeouts.ReadTotalTimeoutConstant/100; |
| 2173 |
|
| 2174 |
|
| 2175 |
|
| 2176 |
|
| 2177 |
|
| 2178 |
s_termios->c_cc[VSTART] = myDCB.XonChar; |
| 2179 |
s_termios->c_cc[VSTOP] = myDCB.XoffChar; |
| 2180 |
s_termios->c_cc[VEOF] = myDCB.EofChar; |
| 2181 |
|
| 2182 |
#ifdef DEBUG_VERBOSE |
| 2183 |
sprintf( message, |
| 2184 |
"tcgetattr: VTIME:%d, VMIN:%d\n", s_termios->c_cc[VTIME], |
| 2185 |
s_termios->c_cc[VMIN] ); |
| 2186 |
report( message ); |
| 2187 |
#endif |
| 2188 |
|
| 2189 |
|
| 2190 |
|
| 2191 |
DCB_to_termios( &myDCB, s_termios ); |
| 2192 |
LEAVE( "tcgetattr" ); |
| 2193 |
return 0; |
| 2194 |
} |
| 2195 |
|
| 2196 |
|
| 2197 |
|
| 2198 |
|
| 2199 |
|
| 2200 |
|
| 2201 |
|
| 2202 |
|
| 2203 |
|
| 2204 |
|
| 2205 |
|
| 2206 |
|
| 2207 |
|
| 2208 |
|
| 2209 |
|
| 2210 |
|
| 2211 |
|
| 2212 |
|
| 2213 |
|
| 2214 |
|
| 2215 |
|
| 2216 |
|
| 2217 |
|
| 2218 |
|
| 2219 |
|
| 2220 |
|
| 2221 |
|
| 2222 |
|
| 2223 |
|
| 2224 |
|
| 2225 |
|
| 2226 |
|
| 2227 |
|
| 2228 |
|
| 2229 |
|
| 2230 |
int tcsetattr( int fd, int when, struct termios *s_termios ) |
| 2231 |
{ |
| 2232 |
int vtime; |
| 2233 |
DCB dcb; |
| 2234 |
COMMTIMEOUTS timeouts; |
| 2235 |
struct termios_list *index; |
| 2236 |
|
| 2237 |
ENTER( "tcsetattr" ); |
| 2238 |
if ( fd <= 0 ) |
| 2239 |
return 0; |
| 2240 |
index = find_port( fd ); |
| 2241 |
if ( !index ) |
| 2242 |
{ |
| 2243 |
LEAVE( "tcsetattr" ); |
| 2244 |
return -1; |
| 2245 |
} |
| 2246 |
fflush( stdout ); |
| 2247 |
if ( s_termios->c_lflag & ICANON ) |
| 2248 |
{ |
| 2249 |
report( "tcsetattr: no canonical mode support\n" ); |
| 2250 |
|
| 2251 |
return -1; |
| 2252 |
} |
| 2253 |
if ( !GetCommState( index->hComm, &dcb ) ) |
| 2254 |
{ |
| 2255 |
YACK(); |
| 2256 |
report( "tcsetattr:GetCommState\n" ); |
| 2257 |
return -1; |
| 2258 |
} |
| 2259 |
if ( !GetCommTimeouts( index->hComm, &timeouts ) ) |
| 2260 |
{ |
| 2261 |
YACK(); |
| 2262 |
report( "tcsetattr:GetCommTimeouts\n" ); |
| 2263 |
return -1; |
| 2264 |
} |
| 2265 |
|
| 2266 |
|
| 2267 |
if ( !( s_termios->c_cflag & CIGNORE ) ) |
| 2268 |
{ |
| 2269 |
dcb.fParity=1; |
| 2270 |
|
| 2271 |
|
| 2272 |
if ( termios_to_DCB( s_termios, &dcb ) < 0 ) return -1; |
| 2273 |
} |
| 2274 |
else |
| 2275 |
{ |
| 2276 |
} |
| 2277 |
|
| 2278 |
|
| 2279 |
|
| 2280 |
|
| 2281 |
|
| 2282 |
|
| 2283 |
|
| 2284 |
|
| 2285 |
|
| 2286 |
|
| 2287 |
|
| 2288 |
|
| 2289 |
|
| 2290 |
|
| 2291 |
|
| 2292 |
|
| 2293 |
|
| 2294 |
|
| 2295 |
|
| 2296 |
|
| 2297 |
|
| 2298 |
if ( s_termios->c_iflag & ISTRIP ) dcb.fBinary = FALSE; |
| 2299 |
|
| 2300 |
else dcb.fBinary = TRUE; |
| 2301 |
|
| 2302 |
|
| 2303 |
|
| 2304 |
if ( s_termios->c_iflag & IXON ) |
| 2305 |
{ |
| 2306 |
dcb.fOutX = TRUE; |
| 2307 |
} |
| 2308 |
else |
| 2309 |
{ |
| 2310 |
dcb.fOutX = FALSE; |
| 2311 |
} |
| 2312 |
if ( s_termios->c_iflag & IXOFF ) |
| 2313 |
{ |
| 2314 |
dcb.fInX = TRUE; |
| 2315 |
} |
| 2316 |
else |
| 2317 |
{ |
| 2318 |
dcb.fInX = FALSE; |
| 2319 |
} |
| 2320 |
dcb.fTXContinueOnXoff = ( s_termios->c_iflag & IXANY ) ? TRUE : FALSE; |
| 2321 |
|
| 2322 |
|
| 2323 |
|
| 2324 |
dcb.fDtrControl = DTR_CONTROL_ENABLE; |
| 2325 |
|
| 2326 |
dcb.fOutxDsrFlow = FALSE; |
| 2327 |
|
| 2328 |
dcb.fDsrSensitivity = FALSE; |
| 2329 |
dcb.XonChar = s_termios->c_cc[VSTART]; |
| 2330 |
dcb.XoffChar = s_termios->c_cc[VSTOP]; |
| 2331 |
dcb.XonLim = 0; |
| 2332 |
dcb.XoffLim = 0; |
| 2333 |
dcb.EofChar = s_termios->c_cc[VEOF]; |
| 2334 |
if( dcb.EofChar != '\0' ) |
| 2335 |
{ |
| 2336 |
dcb.fBinary = 0; |
| 2337 |
} |
| 2338 |
else |
| 2339 |
{ |
| 2340 |
dcb.fBinary = 1; |
| 2341 |
} |
| 2342 |
if ( EV_BREAK|EV_CTS|EV_DSR|EV_ERR|EV_RING | ( EV_RLSD & EV_RXFLAG ) ) |
| 2343 |
dcb.EvtChar = '\n'; |
| 2344 |
else |
| 2345 |
dcb.EvtChar = '\0'; |
| 2346 |
|
| 2347 |
if ( !SetCommState( index->hComm, &dcb ) ) |
| 2348 |
{ |
| 2349 |
report( "SetCommState error\n" ); |
| 2350 |
YACK(); |
| 2351 |
return -1; |
| 2352 |
} |
| 2353 |
|
| 2354 |
#ifdef DEBUG_VERBOSE |
| 2355 |
sprintf( message, "VTIME:%d, VMIN:%d\n", s_termios->c_cc[VTIME], |
| 2356 |
s_termios->c_cc[VMIN] ); |
| 2357 |
report( message ); |
| 2358 |
#endif |
| 2359 |
vtime = s_termios->c_cc[VTIME] * 100; |
| 2360 |
timeouts.ReadTotalTimeoutConstant = vtime; |
| 2361 |
timeouts.ReadIntervalTimeout = 0; |
| 2362 |
timeouts.ReadTotalTimeoutMultiplier = 0; |
| 2363 |
|
| 2364 |
timeouts.WriteTotalTimeoutConstant = vtime; |
| 2365 |
timeouts.WriteTotalTimeoutMultiplier = 0; |
| 2366 |
|
| 2367 |
if ( s_termios->c_cc[VMIN] > 0 && vtime > 0 ) |
| 2368 |
{ |
| 2369 |
|
| 2370 |
} else if ( s_termios->c_cc[VMIN] == 0 && vtime == 0 ) |
| 2371 |
{ |
| 2372 |
|
| 2373 |
timeouts.ReadIntervalTimeout = MAXDWORD; |
| 2374 |
timeouts.ReadTotalTimeoutConstant = 0; |
| 2375 |
timeouts.ReadTotalTimeoutMultiplier = 0; |
| 2376 |
} |
| 2377 |
#ifdef DEBUG_VERBOSE |
| 2378 |
sprintf( message, "ReadIntervalTimeout=%ld\n", |
| 2379 |
timeouts.ReadIntervalTimeout ); |
| 2380 |
report( message ); |
| 2381 |
sprintf( message, "c_cc[VTIME] = %d, c_cc[VMIN] = %d\n", |
| 2382 |
s_termios->c_cc[VTIME], s_termios->c_cc[VMIN] ); |
| 2383 |
report( message ); |
| 2384 |
sprintf( message, "ReadTotalTimeoutConstant: %ld\n", |
| 2385 |
timeouts.ReadTotalTimeoutConstant ); |
| 2386 |
report( message ); |
| 2387 |
sprintf( message, "ReadIntervalTimeout : %ld\n", |
| 2388 |
timeouts.ReadIntervalTimeout ); |
| 2389 |
report( message ); |
| 2390 |
sprintf( message, "ReadTotalTimeoutMultiplier: %ld\n", |
| 2391 |
timeouts.ReadTotalTimeoutMultiplier ); |
| 2392 |
report( message ); |
| 2393 |
#endif |
| 2394 |
if ( !SetCommTimeouts( index->hComm, &timeouts ) ) |
| 2395 |
{ |
| 2396 |
YACK(); |
| 2397 |
report( "SetCommTimeouts\n" ); |
| 2398 |
return -1; |
| 2399 |
} |
| 2400 |
memcpy( index->ttyset, s_termios, sizeof( struct termios ) ); |
| 2401 |
LEAVE( "tcsetattr" ); |
| 2402 |
return 0; |
| 2403 |
} |
| 2404 |
|
| 2405 |
|
| 2406 |
|
| 2407 |
|
| 2408 |
|
| 2409 |
|
| 2410 |
|
| 2411 |
|
| 2412 |
|
| 2413 |
|
| 2414 |
|
| 2415 |
|
| 2416 |
|
| 2417 |
|
| 2418 |
int tcsendbreak( int fd, int duration ) |
| 2419 |
{ |
| 2420 |
struct termios_list *index; |
| 2421 |
COMSTAT Stat; |
| 2422 |
|
| 2423 |
ENTER( "tcsendbreak" ); |
| 2424 |
|
| 2425 |
index = find_port( fd ); |
| 2426 |
|
| 2427 |
if ( !index ) |
| 2428 |
{ |
| 2429 |
LEAVE( "tcdrain" ); |
| 2430 |
return -1; |
| 2431 |
} |
| 2432 |
|
| 2433 |
if ( duration <= 0 ) duration = 1; |
| 2434 |
|
| 2435 |
if( !SetCommBreak( index->hComm ) ) |
| 2436 |
ClearErrors( index, &Stat ); |
| 2437 |
|
| 2438 |
usleep( duration * 250000 ); |
| 2439 |
if( !ClearCommBreak( index->hComm ) ) |
| 2440 |
ClearErrors( index, &Stat ); |
| 2441 |
LEAVE( "tcsendbreak" ); |
| 2442 |
return 1; |
| 2443 |
} |
| 2444 |
|
| 2445 |
|
| 2446 |
|
| 2447 |
|
| 2448 |
|
| 2449 |
|
| 2450 |
|
| 2451 |
|
| 2452 |
|
| 2453 |
|
| 2454 |
|
| 2455 |
|
| 2456 |
int tcdrain ( int fd ) |
| 2457 |
{ |
| 2458 |
struct termios_list *index; |
| 2459 |
char message[80]; |
| 2460 |
int old_flag; |
| 2461 |
|
| 2462 |
ENTER( "tcdrain" ); |
| 2463 |
index = find_port( fd ); |
| 2464 |
|
| 2465 |
if ( !index ) |
| 2466 |
{ |
| 2467 |
LEAVE( "tcdrain" ); |
| 2468 |
return -1; |
| 2469 |
} |
| 2470 |
old_flag = index->event_flag; |
| 2471 |
|
| 2472 |
|
| 2473 |
|
| 2474 |
|
| 2475 |
|
| 2476 |
if ( !FlushFileBuffers( index->hComm ) ) |
| 2477 |
{ |
| 2478 |
|
| 2479 |
|
| 2480 |
|
| 2481 |
|
| 2482 |
|
| 2483 |
|
| 2484 |
|
| 2485 |
|
| 2486 |
sprintf( message, "FlushFileBuffers() %i\n", |
| 2487 |
(int) GetLastError() ); |
| 2488 |
report( message ); |
| 2489 |
if( GetLastError() == 0 ) |
| 2490 |
{ |
| 2491 |
set_errno( 0 ); |
| 2492 |
return(0); |
| 2493 |
} |
| 2494 |
set_errno( EAGAIN ); |
| 2495 |
YACK(); |
| 2496 |
LEAVE( "tcdrain" ); |
| 2497 |
return -1; |
| 2498 |
} |
| 2499 |
|
| 2500 |
|
| 2501 |
|
| 2502 |
|
| 2503 |
|
| 2504 |
LEAVE( "tcdrain success" ); |
| 2505 |
index->event_flag |= EV_TXEMPTY; |
| 2506 |
SetCommMask( index->hComm, index->event_flag ); |
| 2507 |
index->event_flag = old_flag; |
| 2508 |
|
| 2509 |
|
| 2510 |
|
| 2511 |
return 0; |
| 2512 |
} |
| 2513 |
|
| 2514 |
|
| 2515 |
|
| 2516 |
|
| 2517 |
|
| 2518 |
|
| 2519 |
|
| 2520 |
|
| 2521 |
|
| 2522 |
|
| 2523 |
|
| 2524 |
|
| 2525 |
|
| 2526 |
|
| 2527 |
|
| 2528 |
int tcflush( int fd, int queue_selector ) |
| 2529 |
{ |
| 2530 |
struct termios_list *index; |
| 2531 |
int old_flag; |
| 2532 |
|
| 2533 |
index = find_port( fd ); |
| 2534 |
if( !index) |
| 2535 |
{ |
| 2536 |
LEAVE( "tclflush" ); |
| 2537 |
return(-1); |
| 2538 |
} |
| 2539 |
|
| 2540 |
old_flag = index->event_flag; |
| 2541 |
|
| 2542 |
|
| 2543 |
|
| 2544 |
|
| 2545 |
|
| 2546 |
ENTER( "tcflush" ); |
| 2547 |
if ( !index ) |
| 2548 |
{ |
| 2549 |
LEAVE( "tcflush" ); |
| 2550 |
return -1; |
| 2551 |
} |
| 2552 |
|
| 2553 |
index->tx_happened = 1; |
| 2554 |
switch( queue_selector ) |
| 2555 |
{ |
| 2556 |
case TCIFLUSH: |
| 2557 |
if ( !PurgeComm( index->hComm, PURGE_RXABORT ) ) |
| 2558 |
{ |
| 2559 |
goto fail; |
| 2560 |
} |
| 2561 |
break; |
| 2562 |
case TCOFLUSH: |
| 2563 |
if ( !PurgeComm( index->hComm, PURGE_TXABORT ) ) |
| 2564 |
{ |
| 2565 |
goto fail; |
| 2566 |
} |
| 2567 |
break; |
| 2568 |
case TCIOFLUSH: |
| 2569 |
if ( !PurgeComm( index->hComm, PURGE_TXABORT ) ) |
| 2570 |
{ |
| 2571 |
goto fail; |
| 2572 |
} |
| 2573 |
if ( !PurgeComm( index->hComm, PURGE_RXABORT ) ) |
| 2574 |
{ |
| 2575 |
goto fail; |
| 2576 |
} |
| 2577 |
break; |
| 2578 |
default: |
| 2579 |
|
| 2580 |
|
| 2581 |
|
| 2582 |
report( "tcflush: Unknown queue_selector\n" ); |
| 2583 |
LEAVE( "tcflush" ); |
| 2584 |
return -1; |
| 2585 |
} |
| 2586 |
index->event_flag |= EV_TXEMPTY; |
| 2587 |
SetCommMask( index->hComm, index->event_flag ); |
| 2588 |
index->event_flag = old_flag; |
| 2589 |
index->tx_happened = 1; |
| 2590 |
LEAVE( "tcflush" ); |
| 2591 |
return( 0 ); |
| 2592 |
|
| 2593 |
|
| 2594 |
|
| 2595 |
|
| 2596 |
|
| 2597 |
|
| 2598 |
fail: |
| 2599 |
LEAVE( "tcflush" ); |
| 2600 |
set_errno( EAGAIN ); |
| 2601 |
YACK(); |
| 2602 |
return -1; |
| 2603 |
|
| 2604 |
} |
| 2605 |
|
| 2606 |
|
| 2607 |
|
| 2608 |
|
| 2609 |
|
| 2610 |
|
| 2611 |
|
| 2612 |
|
| 2613 |
|
| 2614 |
|
| 2615 |
|
| 2616 |
|
| 2617 |
int tcflow( int fd, int action ) |
| 2618 |
{ |
| 2619 |
ENTER( "tcflow" ); |
| 2620 |
switch ( action ) |
| 2621 |
{ |
| 2622 |
|
| 2623 |
case TCOOFF: break; |
| 2624 |
|
| 2625 |
case TCOON: break; |
| 2626 |
|
| 2627 |
case TCIOFF: break; |
| 2628 |
|
| 2629 |
case TCION: break; |
| 2630 |
default: return -1; |
| 2631 |
} |
| 2632 |
LEAVE( "tcflow" ); |
| 2633 |
return 1; |
| 2634 |
} |
| 2635 |
|
| 2636 |
|
| 2637 |
|
| 2638 |
|
| 2639 |
|
| 2640 |
|
| 2641 |
|
| 2642 |
|
| 2643 |
|
| 2644 |
|
| 2645 |
|
| 2646 |
int fstat( int fd, ... ) |
| 2647 |
{ |
| 2648 |
return( 0 ); |
| 2649 |
} |
| 2650 |
|
| 2651 |
|
| 2652 |
|
| 2653 |
|
| 2654 |
|
| 2655 |
|
| 2656 |
|
| 2657 |
|
| 2658 |
|
| 2659 |
|
| 2660 |
|
| 2661 |
|
| 2662 |
|
| 2663 |
|
| 2664 |
|
| 2665 |
|
| 2666 |
|
| 2667 |
|
| 2668 |
|
| 2669 |
|
| 2670 |
|
| 2671 |
|
| 2672 |
|
| 2673 |
|
| 2674 |
|
| 2675 |
|
| 2676 |
int ioctl( int fd, int request, ... ) |
| 2677 |
{ |
| 2678 |
unsigned long dwStatus = 0; |
| 2679 |
va_list ap; |
| 2680 |
int *arg, ret, result, old_flag; |
| 2681 |
char message[80]; |
| 2682 |
|
| 2683 |
#ifdef TIOCGSERIAL |
| 2684 |
DCB *dcb; |
| 2685 |
struct serial_struct *sstruct; |
| 2686 |
#endif |
| 2687 |
COMSTAT Stat; |
| 2688 |
|
| 2689 |
struct termios_list *index; |
| 2690 |
struct async_struct *astruct; |
| 2691 |
struct serial_multiport_struct *mstruct; |
| 2692 |
#ifdef TIOCGICOUNT |
| 2693 |
struct serial_icounter_struct *sistruct; |
| 2694 |
#endif |
| 2695 |
|
| 2696 |
ENTER( "ioctl" ); |
| 2697 |
if ( fd <= 0 ) |
| 2698 |
return 0; |
| 2699 |
index = find_port( fd ); |
| 2700 |
if ( !index ) |
| 2701 |
{ |
| 2702 |
LEAVE( "ioctl" ); |
| 2703 |
return -1; |
| 2704 |
} |
| 2705 |
|
| 2706 |
va_start( ap, request ); |
| 2707 |
|
| 2708 |
ret = ClearErrors( index, &Stat ); |
| 2709 |
if (ret == 0) |
| 2710 |
{ |
| 2711 |
set_errno( EBADFD ); |
| 2712 |
YACK(); |
| 2713 |
report( "ClearError Failed! ernno EBADFD" ); |
| 2714 |
arg = va_arg( ap, int * ); |
| 2715 |
va_end( ap ); |
| 2716 |
return -1; |
| 2717 |
} |
| 2718 |
switch( request ) |
| 2719 |
{ |
| 2720 |
case TCSBRK: |
| 2721 |
arg = va_arg( ap, int * ); |
| 2722 |
va_end( ap ); |
| 2723 |
return -ENOIOCTLCMD; |
| 2724 |
case TCSBRKP: |
| 2725 |
arg = va_arg( ap, int * ); |
| 2726 |
va_end( ap ); |
| 2727 |
return -ENOIOCTLCMD; |
| 2728 |
case TIOCGSOFTCAR: |
| 2729 |
arg = va_arg( ap, int * ); |
| 2730 |
va_end( ap ); |
| 2731 |
return -ENOIOCTLCMD; |
| 2732 |
case TIOCSSOFTCAR: |
| 2733 |
arg = va_arg( ap, int * ); |
| 2734 |
va_end( ap ); |
| 2735 |
return -ENOIOCTLCMD; |
| 2736 |
|
| 2737 |
case TIOCMGET: |
| 2738 |
arg = va_arg( ap, int * ); |
| 2739 |
|
| 2740 |
if ( !GetCommModemStatus( index->hComm, &dwStatus ) ) |
| 2741 |
report_error( "GetCommMOdemStatus failed!\n" ); |
| 2742 |
if ( dwStatus & MS_RLSD_ON ) *arg |= TIOCM_CAR; |
| 2743 |
else *arg &= ~TIOCM_CAR; |
| 2744 |
if ( dwStatus & MS_RING_ON ) *arg |= TIOCM_RNG; |
| 2745 |
else *arg &= ~TIOCM_RNG; |
| 2746 |
if ( dwStatus & MS_DSR_ON ) *arg |= TIOCM_DSR; |
| 2747 |
else *arg &= ~TIOCM_DSR; |
| 2748 |
if ( dwStatus & MS_CTS_ON ) *arg |= TIOCM_CTS; |
| 2749 |
else *arg &= ~TIOCM_CTS; |
| 2750 |
|
| 2751 |
|
| 2752 |
|
| 2753 |
|
| 2754 |
|
| 2755 |
if ( index->MSR & TIOCM_DTR ) |
| 2756 |
*arg |= TIOCM_DTR; |
| 2757 |
else *arg &= ~TIOCM_DTR; |
| 2758 |
if ( index->MSR & TIOCM_RTS ) |
| 2759 |
*arg |= TIOCM_RTS; |
| 2760 |
else *arg &= ~TIOCM_RTS; |
| 2761 |
|
| 2762 |
|
| 2763 |
|
| 2764 |
|
| 2765 |
|
| 2766 |
|
| 2767 |
|
| 2768 |
va_end( ap ); |
| 2769 |
return( 0 ); |
| 2770 |
|
| 2771 |
case TIOCMBIS: |
| 2772 |
arg = va_arg( ap, int * ); |
| 2773 |
va_end( ap ); |
| 2774 |
return -ENOIOCTLCMD; |
| 2775 |
case TIOCMBIC: |
| 2776 |
arg = va_arg( ap, int * ); |
| 2777 |
va_end( ap ); |
| 2778 |
return -ENOIOCTLCMD; |
| 2779 |
case TIOCMSET: |
| 2780 |
arg = va_arg( ap, int * ); |
| 2781 |
if (( *arg & TIOCM_DTR) == (index->MSR & TIOCM_DTR) ) |
| 2782 |
{ |
| 2783 |
report( "DTR is unchanged\n" ); |
| 2784 |
} |
| 2785 |
sprintf(message, "DTR %i %i\n", *arg&TIOCM_DTR, index->MSR & TIOCM_DTR ); |
| 2786 |
report( message ); |
| 2787 |
if ( *arg & TIOCM_DTR ) |
| 2788 |
{ |
| 2789 |
index->MSR |= TIOCM_DTR; |
| 2790 |
} |
| 2791 |
else |
| 2792 |
{ |
| 2793 |
index->MSR &= ~TIOCM_DTR; |
| 2794 |
} |
| 2795 |
if ( EscapeCommFunction( index->hComm, |
| 2796 |
( *arg & TIOCM_DTR ) ? SETDTR : |
| 2797 |
CLRDTR ) ) |
| 2798 |
report( "EscapeCommFunction: True\n" ); |
| 2799 |
else |
| 2800 |
report( "EscapeCommFunction: False\n" ); |
| 2801 |
if ( (*arg & TIOCM_RTS) == ( index->MSR & TIOCM_RTS) ) |
| 2802 |
{ |
| 2803 |
report( "RTS is unchanged\n" ); |
| 2804 |
} |
| 2805 |
sprintf( message, "RTS %i %i\n", *arg&TIOCM_RTS, index->MSR & TIOCM_RTS ); |
| 2806 |
report( message ); |
| 2807 |
if ( *arg & TIOCM_RTS ) |
| 2808 |
{ |
| 2809 |
index->MSR |= TIOCM_RTS; |
| 2810 |
result &= SETRTS; |
| 2811 |
} |
| 2812 |
else |
| 2813 |
{ |
| 2814 |
index->MSR &= ~TIOCM_RTS; |
| 2815 |
result &= CLRRTS; |
| 2816 |
} |
| 2817 |
if( EscapeCommFunction( index->hComm, |
| 2818 |
( *arg & TIOCM_RTS ) ? SETRTS : CLRRTS ) ) |
| 2819 |
report( "EscapeCommFunction: True\n" ); |
| 2820 |
else |
| 2821 |
report( "EscapeCommFunction: False\n" ); |
| 2822 |
break; |
| 2823 |
|
| 2824 |
#ifdef TIOCGSERIAL |
| 2825 |
case TIOCGSERIAL: |
| 2826 |
report( "TIOCGSERIAL\n" ); |
| 2827 |
|
| 2828 |
dcb = malloc( sizeof( DCB ) ); |
| 2829 |
if( !dcb ) |
| 2830 |
{ |
| 2831 |
va_end( ap ); |
| 2832 |
return -1; |
| 2833 |
} |
| 2834 |
memset( dcb, 0, sizeof( DCB ) ); |
| 2835 |
GetCommState( index->hComm, dcb ); |
| 2836 |
|
| 2837 |
sstruct = va_arg( ap, struct serial_struct * ); |
| 2838 |
if ( DCB_to_serial_struct( dcb, sstruct ) < 0 ) |
| 2839 |
{ |
| 2840 |
va_end( ap ); |
| 2841 |
return -1; |
| 2842 |
} |
| 2843 |
index->sstruct = sstruct; |
| 2844 |
|
| 2845 |
report( "TIOCGSERIAL\n" ); |
| 2846 |
free(dcb); |
| 2847 |
break; |
| 2848 |
|
| 2849 |
#endif |
| 2850 |
#ifdef TIOCSSERIAL |
| 2851 |
case TIOCSSERIAL: |
| 2852 |
report( "TIOCSSERIAL\n" ); |
| 2853 |
|
| 2854 |
dcb = malloc( sizeof( DCB ) ); |
| 2855 |
if( !dcb ) |
| 2856 |
{ |
| 2857 |
va_end( ap ); |
| 2858 |
return -1; |
| 2859 |
} |
| 2860 |
memset( dcb, 0, sizeof( DCB ) ); |
| 2861 |
GetCommState( index->hComm, dcb ); |
| 2862 |
|
| 2863 |
index->sstruct = va_arg( ap, struct serial_struct * ); |
| 2864 |
|
| 2865 |
report( "TIOCSSERIAL\n" ); |
| 2866 |
free(dcb); |
| 2867 |
break; |
| 2868 |
|
| 2869 |
#endif |
| 2870 |
case TIOCSERCONFIG: |
| 2871 |
case TIOCSERGETLSR: |
| 2872 |
arg = va_arg( ap, int * ); |
| 2873 |
|
| 2874 |
|
| 2875 |
|
| 2876 |
|
| 2877 |
|
| 2878 |
ret = ClearErrors( index, &Stat ); |
| 2879 |
if ( ret == 0 ) |
| 2880 |
{ |
| 2881 |
|
| 2882 |
set_errno( EBADFD ); |
| 2883 |
YACK(); |
| 2884 |
report( "TIOCSERGETLSR EBADFD" ); |
| 2885 |
va_end( ap ); |
| 2886 |
return -1; |
| 2887 |
} |
| 2888 |
if ( (int ) Stat.cbOutQue == 0 ) |
| 2889 |
{ |
| 2890 |
|
| 2891 |
if( index->tx_happened == 1 ) |
| 2892 |
{ |
| 2893 |
old_flag = index->event_flag; |
| 2894 |
index->event_flag &= ~EV_TXEMPTY; |
| 2895 |
SetCommMask( index->hComm, |
| 2896 |
index->event_flag ); |
| 2897 |
index->event_flag = old_flag; |
| 2898 |
*arg = 1; |
| 2899 |
index->tx_happened = 0; |
| 2900 |
report( "ioctl: ouput empty\n" ); |
| 2901 |
} |
| 2902 |
else |
| 2903 |
{ |
| 2904 |
*arg = 0; |
| 2905 |
} |
| 2906 |
ret = 0; |
| 2907 |
} |
| 2908 |
else |
| 2909 |
{ |
| 2910 |
|
| 2911 |
*arg = 0; |
| 2912 |
ret = 0; |
| 2913 |
} |
| 2914 |
va_end( ap ); |
| 2915 |
return(0); |
| 2916 |
break; |
| 2917 |
case TIOCSERGSTRUCT: |
| 2918 |
astruct = va_arg( ap, struct async_struct * ); |
| 2919 |
va_end( ap ); |
| 2920 |
return -ENOIOCTLCMD; |
| 2921 |
case TIOCSERGETMULTI: |
| 2922 |
mstruct = va_arg( ap, struct serial_multiport_struct * ); |
| 2923 |
va_end( ap ); |
| 2924 |
return -ENOIOCTLCMD; |
| 2925 |
case TIOCSERSETMULTI: |
| 2926 |
mstruct = va_arg( ap, struct serial_multiport_struct * ); |
| 2927 |
va_end( ap ); |
| 2928 |
return -ENOIOCTLCMD; |
| 2929 |
case TIOCMIWAIT: |
| 2930 |
arg = va_arg( ap, int * ); |
| 2931 |
va_end( ap ); |
| 2932 |
return -ENOIOCTLCMD; |
| 2933 |
|
| 2934 |
|
| 2935 |
|
| 2936 |
|
| 2937 |
#ifdef TIOCGICOUNT |
| 2938 |
case TIOCGICOUNT: |
| 2939 |
sistruct= va_arg( ap, struct serial_icounter_struct * ); |
| 2940 |
ret = ClearErrors( index, &Stat ); |
| 2941 |
if ( ret == 0 ) |
| 2942 |
{ |
| 2943 |
|
| 2944 |
report( "TIOCGICOUNT failed\n" ); |
| 2945 |
set_errno( EBADFD ); |
| 2946 |
va_end( ap ); |
| 2947 |
return -1; |
| 2948 |
} |
| 2949 |
if( sistruct->frame != index->sis->frame ) |
| 2950 |
{ |
| 2951 |
sistruct->frame = index->sis->frame; |
| 2952 |
|
| 2953 |
|
| 2954 |
|
| 2955 |
} |
| 2956 |
if( sistruct->overrun != index->sis->overrun ) |
| 2957 |
{ |
| 2958 |
|
| 2959 |
|
| 2960 |
|
| 2961 |
sistruct->overrun = index->sis->overrun; |
| 2962 |
|
| 2963 |
} |
| 2964 |
if( sistruct->parity != index->sis->parity ) |
| 2965 |
{ |
| 2966 |
|
| 2967 |
|
| 2968 |
|
| 2969 |
sistruct->parity = index->sis->parity; |
| 2970 |
} |
| 2971 |
if( sistruct->brk != index->sis->brk ) |
| 2972 |
{ |
| 2973 |
|
| 2974 |
|
| 2975 |
|
| 2976 |
sistruct->brk = index->sis->brk; |
| 2977 |
} |
| 2978 |
va_end( ap ); |
| 2979 |
return 0; |
| 2980 |
|
| 2981 |
#endif |
| 2982 |
case TIOCSERGWILD: |
| 2983 |
case TIOCSERSWILD: |
| 2984 |
report( "TIOCSER[GS]WILD absolete\n" ); |
| 2985 |
va_end( ap ); |
| 2986 |
return 0; |
| 2987 |
|
| 2988 |
case FIONREAD: |
| 2989 |
arg = va_arg( ap, int * ); |
| 2990 |
ret = ClearErrors( index, &Stat ); |
| 2991 |
if ( ret == 0 ) |
| 2992 |
{ |
| 2993 |
|
| 2994 |
report( "FIONREAD failed\n" ); |
| 2995 |
set_errno( EBADFD ); |
| 2996 |
va_end( ap ); |
| 2997 |
return -1; |
| 2998 |
} |
| 2999 |
*arg = ( int ) Stat.cbInQue; |
| 3000 |
#ifdef DEBUG_VERBOSE |
| 3001 |
sprintf( message, "FIONREAD: %i bytes available\n", |
| 3002 |
(int) Stat.cbInQue ); |
| 3003 |
report( message ); |
| 3004 |
if( *arg ) |
| 3005 |
{ |
| 3006 |
sprintf( message, "FIONREAD: %i\n", *arg ); |
| 3007 |
report( message ); |
| 3008 |
} |
| 3009 |
#endif |
| 3010 |
ret = 0; |
| 3011 |
break; |
| 3012 |
|
| 3013 |
|
| 3014 |
case TIOCOUTQ: |
| 3015 |
arg = va_arg( ap, int * ); |
| 3016 |
va_end( ap ); |
| 3017 |
return -ENOIOCTLCMD; |
| 3018 |
default: |
| 3019 |
sprintf( message, |
| 3020 |
"FIXME: ioctl: unknown request: %#x\n", |
| 3021 |
request ); |
| 3022 |
report( message ); |
| 3023 |
va_end( ap ); |
| 3024 |
return -ENOIOCTLCMD; |
| 3025 |
} |
| 3026 |
va_end( ap ); |
| 3027 |
LEAVE( "ioctl" ); |
| 3028 |
return 0; |
| 3029 |
} |
| 3030 |
|
| 3031 |
|
| 3032 |
|
| 3033 |
|
| 3034 |
|
| 3035 |
|
| 3036 |
|
| 3037 |
|
| 3038 |
|
| 3039 |
|
| 3040 |
|
| 3041 |
|
| 3042 |
int fcntl( int fd, int command, ... ) |
| 3043 |
{ |
| 3044 |
int arg, ret = 0; |
| 3045 |
va_list ap; |
| 3046 |
struct termios_list *index; |
| 3047 |
char message[80]; |
| 3048 |
|
| 3049 |
ENTER( "fcntl" ); |
| 3050 |
if ( fd <= 0 ) |
| 3051 |
return 0; |
| 3052 |
index = find_port( fd ); |
| 3053 |
if ( !index ) |
| 3054 |
{ |
| 3055 |
LEAVE( "fcntl" ); |
| 3056 |
return -1; |
| 3057 |
} |
| 3058 |
|
| 3059 |
va_start( ap, command ); |
| 3060 |
|
| 3061 |
arg = va_arg( ap, int ); |
| 3062 |
switch ( command ) |
| 3063 |
{ |
| 3064 |
case F_SETOWN: |
| 3065 |
break; |
| 3066 |
case F_SETFL: |
| 3067 |
#ifdef DEBUG |
| 3068 |
sprintf( message, "F_SETFL fd=%d flags=%d\n", fd, arg ); |
| 3069 |
report( message ); |
| 3070 |
#endif |
| 3071 |
index->open_flags = arg; |
| 3072 |
break; |
| 3073 |
case F_GETFL: |
| 3074 |
ret = index->open_flags; |
| 3075 |
break; |
| 3076 |
default: |
| 3077 |
sprintf( message, "unknown fcntl command %#x\n", command ); |
| 3078 |
report( message ); |
| 3079 |
break; |
| 3080 |
} |
| 3081 |
|
| 3082 |
va_end( ap ); |
| 3083 |
LEAVE( "fcntl" ); |
| 3084 |
return ret; |
| 3085 |
} |
| 3086 |
|
| 3087 |
|
| 3088 |
|
| 3089 |
|
| 3090 |
|
| 3091 |
|
| 3092 |
|
| 3093 |
|
| 3094 |
|
| 3095 |
|
| 3096 |
|
| 3097 |
void termios_interrupt_event_loop( int fd, int flag ) |
| 3098 |
{ |
| 3099 |
struct termios_list * index = find_port( fd ); |
| 3100 |
if ( !index ) |
| 3101 |
{ |
| 3102 |
LEAVE( "termios_interrupt_event_loop" ); |
| 3103 |
return; |
| 3104 |
} |
| 3105 |
|
| 3106 |
|
| 3107 |
|
| 3108 |
|
| 3109 |
|
| 3110 |
|
| 3111 |
|
| 3112 |
index->interrupt = flag; |
| 3113 |
return; |
| 3114 |
} |
| 3115 |
|
| 3116 |
|
| 3117 |
|
| 3118 |
|
| 3119 |
|
| 3120 |
|
| 3121 |
|
| 3122 |
|
| 3123 |
|
| 3124 |
|
| 3125 |
|
| 3126 |
#ifndef __LCC__ |
| 3127 |
int serial_select( int n, fd_set *readfds, fd_set *writefds, |
| 3128 |
fd_set *exceptfds, struct timeval *timeout ) |
| 3129 |
{ |
| 3130 |
|
| 3131 |
unsigned long dwCommEvent, wait = WAIT_TIMEOUT; |
| 3132 |
int fd = n-1; |
| 3133 |
struct termios_list *index; |
| 3134 |
char message[80]; |
| 3135 |
COMSTAT Stat; |
| 3136 |
int ret; |
| 3137 |
|
| 3138 |
ENTER( "serial_select" ); |
| 3139 |
if ( fd <= 0 ) |
| 3140 |
{ |
| 3141 |
|
| 3142 |
goto fail; |
| 3143 |
} |
| 3144 |
|
| 3145 |
index = find_port( fd ); |
| 3146 |
if ( !index || !index->event_flag ) |
| 3147 |
{ |
| 3148 |
|
| 3149 |
|
| 3150 |
|
| 3151 |
|
| 3152 |
|
| 3153 |
|
| 3154 |
usleep(10000); |
| 3155 |
return(0); |
| 3156 |
} |
| 3157 |
|
| 3158 |
ResetEvent( index->wol.hEvent ); |
| 3159 |
ResetEvent( index->sol.hEvent ); |
| 3160 |
ResetEvent( index->rol.hEvent ); |
| 3161 |
ret = ClearErrors( index, &Stat ); |
| 3162 |
if (ret == 0) goto fail; |
| 3163 |
while ( wait == WAIT_TIMEOUT && index->sol.hEvent ) |
| 3164 |
{ |
| 3165 |
if( index->interrupt == 1 ) |
| 3166 |
{ |
| 3167 |
goto fail; |
| 3168 |
} |
| 3169 |
SetCommMask( index->hComm, index->event_flag ); |
| 3170 |
ClearErrors( index, &Stat ); |
| 3171 |
if ( !WaitCommEvent( index->hComm, &dwCommEvent, |
| 3172 |
&index->sol ) ) |
| 3173 |
{ |
| 3174 |
|
| 3175 |
if ( GetLastError() != ERROR_IO_PENDING ) |
| 3176 |
{ |
| 3177 |
ClearErrors( index, &Stat ); |
| 3178 |
goto fail; |
| 3179 |
} |
| 3180 |
|
| 3181 |
} |
| 3182 |
|
| 3183 |
|
| 3184 |
|
| 3185 |
ClearErrors( index, &Stat ); |
| 3186 |
wait = WaitForSingleObject( index->sol.hEvent, 100 ); |
| 3187 |
switch ( wait ) |
| 3188 |
{ |
| 3189 |
case WAIT_OBJECT_0: |
| 3190 |
goto end; |
| 3191 |
case WAIT_TIMEOUT: |
| 3192 |
goto timeout; |
| 3193 |
case WAIT_ABANDONED: |
| 3194 |
default: |
| 3195 |
goto fail; |
| 3196 |
|
| 3197 |
} |
| 3198 |
} |
| 3199 |
end: |
| 3200 |
|
| 3201 |
usleep(1000); |
| 3202 |
LEAVE( "serial_select" ); |
| 3203 |
return( 1 ); |
| 3204 |
timeout: |
| 3205 |
LEAVE( "serial_select" ); |
| 3206 |
return( 0 ); |
| 3207 |
fail: |
| 3208 |
YACK(); |
| 3209 |
sprintf( message, "< select called error %i\n", n ); |
| 3210 |
report( message ); |
| 3211 |
errno = EBADFD; |
| 3212 |
LEAVE( "serial_select" ); |
| 3213 |
return( 1 ); |
| 3214 |
} |
| 3215 |
#ifdef asdf |
| 3216 |
int serial_select( int n, fd_set *readfds, fd_set *writefds, |
| 3217 |
fd_set *exceptfds, struct timeval *timeout ) |
| 3218 |
{ |
| 3219 |
|
| 3220 |
unsigned long nBytes, dwCommEvent, wait = WAIT_TIMEOUT; |
| 3221 |
int fd = n-1; |
| 3222 |
struct termios_list *index; |
| 3223 |
char message[80]; |
| 3224 |
|
| 3225 |
ENTER( "serial_select" ); |
| 3226 |
if ( fd <= 0 ) |
| 3227 |
{ |
| 3228 |
usleep(1000); |
| 3229 |
return 1; |
| 3230 |
} |
| 3231 |
index = find_port( fd ); |
| 3232 |
if ( !index ) |
| 3233 |
{ |
| 3234 |
LEAVE( "serial_select" ); |
| 3235 |
return -1; |
| 3236 |
} |
| 3237 |
if( index->interrupt == 1 ) |
| 3238 |
{ |
| 3239 |
goto end; |
| 3240 |
} |
| 3241 |
while(!index->event_flag ) |
| 3242 |
{ |
| 3243 |
usleep(1000); |
| 3244 |
return -1; |
| 3245 |
} |
| 3246 |
|
| 3247 |
while ( wait == WAIT_TIMEOUT && index->sol.hEvent ) |
| 3248 |
{ |
| 3249 |
if( index->interrupt == 1 ) |
| 3250 |
{ |
| 3251 |
goto end; |
| 3252 |
} |
| 3253 |
if( !index->sol.hEvent ) |
| 3254 |
{ |
| 3255 |
return 1; |
| 3256 |
} |
| 3257 |
if ( !WaitCommEvent( index->hComm, &dwCommEvent, |
| 3258 |
&index->sol ) ) |
| 3259 |
{ |
| 3260 |
|
| 3261 |
if( index->interrupt == 1 ) |
| 3262 |
{ |
| 3263 |
goto end; |
| 3264 |
} |
| 3265 |
if ( GetLastError() != ERROR_IO_PENDING ) |
| 3266 |
{ |
| 3267 |
sprintf( message, "WaitCommEvent filename = %s\n", index->filename); |
| 3268 |
report( message ); |
| 3269 |
return(1); |
| 3270 |
|
| 3271 |
|
| 3272 |
|
| 3273 |
} |
| 3274 |
return(1); |
| 3275 |
} |
| 3276 |
if( index->interrupt == 1 ) |
| 3277 |
{ |
| 3278 |
goto end; |
| 3279 |
} |
| 3280 |
wait = WaitForSingleObject( index->sol.hEvent, 1000 ); |
| 3281 |
switch ( wait ) |
| 3282 |
{ |
| 3283 |
case WAIT_OBJECT_0: |
| 3284 |
if( index->interrupt == 1 ) |
| 3285 |
{ |
| 3286 |
goto end; |
| 3287 |
} |
| 3288 |
if( !index->sol.hEvent ) return(1); |
| 3289 |
if (!GetOverlappedResult( index->hComm, |
| 3290 |
&index->sol, &nBytes, TRUE )) |
| 3291 |
{ |
| 3292 |
goto end; |
| 3293 |
} |
| 3294 |
else if( index->tx_happened == 1 ) |
| 3295 |
{ |
| 3296 |
goto end; |
| 3297 |
} |
| 3298 |
else |
| 3299 |
goto end; |
| 3300 |
break; |
| 3301 |
case WAIT_TIMEOUT: |
| 3302 |
default: |
| 3303 |
return(1); |
| 3304 |
|
| 3305 |
} |
| 3306 |
} |
| 3307 |
end: |
| 3308 |
LEAVE( "serial_select" ); |
| 3309 |
return( 1 ); |
| 3310 |
#ifdef asdf |
| 3311 |
|
| 3312 |
fail: |
| 3313 |
sprintf( message, "< select called error %i\n", n ); |
| 3314 |
YACK(); |
| 3315 |
report( message ); |
| 3316 |
set_errno( EBADFD ); |
| 3317 |
LEAVE( "serial_select" ); |
| 3318 |
return( 1 ); |
| 3319 |
#endif |
| 3320 |
|
| 3321 |
} |
| 3322 |
#endif |
| 3323 |
#endif |
| 3324 |
|
| 3325 |
|
| 3326 |
|
| 3327 |
|
| 3328 |
|
| 3329 |
|
| 3330 |
|
| 3331 |
|
| 3332 |
|
| 3333 |
|
| 3334 |
|
| 3335 |
|
| 3336 |
int termiosGetParityErrorChar( int fd ) |
| 3337 |
{ |
| 3338 |
struct termios_list *index; |
| 3339 |
DCB dcb; |
| 3340 |
|
| 3341 |
ENTER( "termiosGetParityErrorChar" ); |
| 3342 |
index = find_port( fd ); |
| 3343 |
if( !index ) |
| 3344 |
{ |
| 3345 |
LEAVE( "termiosGetParityErrorChar" ); |
| 3346 |
return(-1); |
| 3347 |
} |
| 3348 |
GetCommState( index->hComm, &dcb ); |
| 3349 |
LEAVE( "termiosGetParityErrorChar" ); |
| 3350 |
return( dcb.ErrorChar ); |
| 3351 |
} |
| 3352 |
|
| 3353 |
|
| 3354 |
|
| 3355 |
|
| 3356 |
|
| 3357 |
|
| 3358 |
|
| 3359 |
|
| 3360 |
|
| 3361 |
|
| 3362 |
|
| 3363 |
|
| 3364 |
void termiosSetParityError( int fd, char value ) |
| 3365 |
{ |
| 3366 |
DCB dcb; |
| 3367 |
struct termios_list *index; |
| 3368 |
|
| 3369 |
ENTER( "termiosGetParityErrorChar" ); |
| 3370 |
index = find_port( fd ); |
| 3371 |
if ( !index ) |
| 3372 |
{ |
| 3373 |
LEAVE( "termiosSetParityError" ); |
| 3374 |
return; |
| 3375 |
} |
| 3376 |
GetCommState( index->hComm, &dcb ); |
| 3377 |
dcb.ErrorChar = value; |
| 3378 |
SetCommState( index->hComm, &dcb ); |
| 3379 |
LEAVE( "termiosGetParityErrorChar" ); |
| 3380 |
} |
| 3381 |
|
| 3382 |
#ifdef PLAYING_AROUND |
| 3383 |
static inline int inportb( int index ) |
| 3384 |
{ |
| 3385 |
unsigned char value; |
| 3386 |
__asm__ volatile ( "inb %1,%0" |
| 3387 |
: "=a" (value) |
| 3388 |
: "d" ((unsigned short)index) ); |
| 3389 |
return value; |
| 3390 |
} |
| 3391 |
|
| 3392 |
static inline void outportb(unsigned char val, unsigned short int index) |
| 3393 |
{ |
| 3394 |
__asm__ volatile ( |
| 3395 |
"outb %0,%1\n" |
| 3396 |
: |
| 3397 |
: "a" (val), "d" (index) |
| 3398 |
); |
| 3399 |
} |
| 3400 |
#endif |