| 1 |
/* |
| 2 |
* tclWinSerial.c -- |
| 3 |
* |
| 4 |
* This file implements the Windows-specific serial port functions, |
| 5 |
* and the "serial" channel driver. |
| 6 |
* |
| 7 |
* Copyright (c) 1999 by Scriptics Corp. |
| 8 |
* |
| 9 |
* See the file "license.terms" for information on usage and redistribution |
| 10 |
* of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
| 11 |
* |
| 12 |
* Serial functionality implemented by [email protected] |
| 13 |
* |
| 14 |
* RCS: @(#) $Id: tclWinSerial.c,v 1.25.2.1 2003/05/11 00:31:41 hobbs Exp $ |
| 15 |
*/ |
| 16 |
|
| 17 |
#include "tclWinInt.h" |
| 18 |
|
| 19 |
#include <fcntl.h> |
| 20 |
#include <io.h> |
| 21 |
#include <sys/stat.h> |
| 22 |
|
| 23 |
/* |
| 24 |
* The following variable is used to tell whether this module has been |
| 25 |
* initialized. |
| 26 |
*/ |
| 27 |
|
| 28 |
static int initialized = 0; |
| 29 |
|
| 30 |
/* |
| 31 |
* The serialMutex locks around access to the initialized variable, and it is |
| 32 |
* used to protect background threads from being terminated while they are |
| 33 |
* using APIs that hold locks. |
| 34 |
*/ |
| 35 |
|
| 36 |
TCL_DECLARE_MUTEX(serialMutex) |
| 37 |
|
| 38 |
/* |
| 39 |
* Bit masks used in the flags field of the SerialInfo structure below. |
| 40 |
*/ |
| 41 |
|
| 42 |
#define SERIAL_PENDING (1<<0) /* Message is pending in the queue. */ |
| 43 |
#define SERIAL_ASYNC (1<<1) /* Channel is non-blocking. */ |
| 44 |
|
| 45 |
/* |
| 46 |
* Bit masks used in the sharedFlags field of the SerialInfo structure below. |
| 47 |
*/ |
| 48 |
|
| 49 |
#define SERIAL_EOF (1<<2) /* Serial has reached EOF. */ |
| 50 |
#define SERIAL_ERROR (1<<4) |
| 51 |
|
| 52 |
/* |
| 53 |
* Default time to block between checking status on the serial port. |
| 54 |
*/ |
| 55 |
#define SERIAL_DEFAULT_BLOCKTIME 10 /* 10 msec */ |
| 56 |
|
| 57 |
/* |
| 58 |
* Define Win32 read/write error masks returned by ClearCommError() |
| 59 |
*/ |
| 60 |
#define SERIAL_READ_ERRORS ( CE_RXOVER | CE_OVERRUN | CE_RXPARITY \ |
| 61 |
| CE_FRAME | CE_BREAK ) |
| 62 |
#define SERIAL_WRITE_ERRORS ( CE_TXFULL | CE_PTO ) |
| 63 |
|
| 64 |
/* |
| 65 |
* This structure describes per-instance data for a serial based channel. |
| 66 |
*/ |
| 67 |
|
| 68 |
typedef struct SerialInfo { |
| 69 |
HANDLE handle; |
| 70 |
struct SerialInfo *nextPtr; /* Pointer to next registered serial. */ |
| 71 |
Tcl_Channel channel; /* Pointer to channel structure. */ |
| 72 |
int validMask; /* OR'ed combination of TCL_READABLE, |
| 73 |
* TCL_WRITABLE, or TCL_EXCEPTION: indicates |
| 74 |
* which operations are valid on the file. */ |
| 75 |
int watchMask; /* OR'ed combination of TCL_READABLE, |
| 76 |
* TCL_WRITABLE, or TCL_EXCEPTION: indicates |
| 77 |
* which events should be reported. */ |
| 78 |
int flags; /* State flags, see above for a list. */ |
| 79 |
int readable; /* flag that the channel is readable */ |
| 80 |
int writable; /* flag that the channel is writable */ |
| 81 |
int blockTime; /* max. blocktime in msec */ |
| 82 |
unsigned int lastEventTime; /* Time in milliseconds since last readable event */ |
| 83 |
/* Next readable event only after blockTime */ |
| 84 |
DWORD error; /* pending error code returned by |
| 85 |
* ClearCommError() */ |
| 86 |
DWORD lastError; /* last error code, can be fetched with |
| 87 |
* fconfigure chan -lasterror */ |
| 88 |
DWORD sysBufRead; /* Win32 system buffer size for read ops, |
| 89 |
* default=4096 */ |
| 90 |
DWORD sysBufWrite; /* Win32 system buffer size for write ops, |
| 91 |
* default=4096 */ |
| 92 |
|
| 93 |
Tcl_ThreadId threadId; /* Thread to which events should be reported. |
| 94 |
* This value is used by the reader/writer |
| 95 |
* threads. */ |
| 96 |
OVERLAPPED osRead; /* OVERLAPPED structure for read operations */ |
| 97 |
OVERLAPPED osWrite; /* OVERLAPPED structure for write operations */ |
| 98 |
HANDLE writeThread; /* Handle to writer thread. */ |
| 99 |
CRITICAL_SECTION csWrite; /* Writer thread synchronisation */ |
| 100 |
HANDLE evWritable; /* Manual-reset event to signal when the |
| 101 |
* writer thread has finished waiting for |
| 102 |
* the current buffer to be written. */ |
| 103 |
HANDLE evStartWriter; /* Auto-reset event used by the main thread to |
| 104 |
* signal when the writer thread should attempt |
| 105 |
* to write to the serial. */ |
| 106 |
HANDLE evStopWriter; /* Auto-reset event used by the main thread to |
| 107 |
* signal when the writer thread should close. |
| 108 |
*/ |
| 109 |
DWORD writeError; /* An error caused by the last background |
| 110 |
* write. Set to 0 if no error has been |
| 111 |
* detected. This word is shared with the |
| 112 |
* writer thread so access must be |
| 113 |
* synchronized with the evWritable object. |
| 114 |
*/ |
| 115 |
char *writeBuf; /* Current background output buffer. |
| 116 |
* Access is synchronized with the evWritable |
| 117 |
* object. */ |
| 118 |
int writeBufLen; /* Size of write buffer. Access is |
| 119 |
* synchronized with the evWritable |
| 120 |
* object. */ |
| 121 |
int toWrite; /* Current amount to be written. Access is |
| 122 |
* synchronized with the evWritable object. */ |
| 123 |
int writeQueue; /* Number of bytes pending in output queue. |
| 124 |
* Offset to DCB.cbInQue. |
| 125 |
* Used to query [fconfigure -queue] */ |
| 126 |
} SerialInfo; |
| 127 |
|
| 128 |
typedef struct ThreadSpecificData { |
| 129 |
/* |
| 130 |
* The following pointer refers to the head of the list of serials |
| 131 |
* that are being watched for file events. |
| 132 |
*/ |
| 133 |
|
| 134 |
SerialInfo *firstSerialPtr; |
| 135 |
} ThreadSpecificData; |
| 136 |
|
| 137 |
static Tcl_ThreadDataKey dataKey; |
| 138 |
|
| 139 |
/* |
| 140 |
* The following structure is what is added to the Tcl event queue when |
| 141 |
* serial events are generated. |
| 142 |
*/ |
| 143 |
|
| 144 |
typedef struct SerialEvent { |
| 145 |
Tcl_Event header; /* Information that is standard for |
| 146 |
* all events. */ |
| 147 |
SerialInfo *infoPtr; /* Pointer to serial info structure. Note |
| 148 |
* that we still have to verify that the |
| 149 |
* serial exists before dereferencing this |
| 150 |
* pointer. */ |
| 151 |
} SerialEvent; |
| 152 |
|
| 153 |
/* |
| 154 |
* We don't use timeouts. |
| 155 |
*/ |
| 156 |
|
| 157 |
static COMMTIMEOUTS no_timeout = { |
| 158 |
0, /* ReadIntervalTimeout */ |
| 159 |
0, /* ReadTotalTimeoutMultiplier */ |
| 160 |
0, /* ReadTotalTimeoutConstant */ |
| 161 |
0, /* WriteTotalTimeoutMultiplier */ |
| 162 |
0, /* WriteTotalTimeoutConstant */ |
| 163 |
}; |
| 164 |
|
| 165 |
/* |
| 166 |
* Declarations for functions used only in this file. |
| 167 |
*/ |
| 168 |
|
| 169 |
static int SerialBlockProc(ClientData instanceData, int mode); |
| 170 |
static void SerialCheckProc(ClientData clientData, int flags); |
| 171 |
static int SerialCloseProc(ClientData instanceData, |
| 172 |
Tcl_Interp *interp); |
| 173 |
static int SerialEventProc(Tcl_Event *evPtr, int flags); |
| 174 |
static void SerialExitHandler(ClientData clientData); |
| 175 |
static int SerialGetHandleProc(ClientData instanceData, |
| 176 |
int direction, ClientData *handlePtr); |
| 177 |
static ThreadSpecificData *SerialInit(void); |
| 178 |
static int SerialInputProc(ClientData instanceData, char *buf, |
| 179 |
int toRead, int *errorCode); |
| 180 |
static int SerialOutputProc(ClientData instanceData, CONST char *buf, |
| 181 |
int toWrite, int *errorCode); |
| 182 |
static void SerialSetupProc(ClientData clientData, int flags); |
| 183 |
static void SerialWatchProc(ClientData instanceData, int mask); |
| 184 |
static void ProcExitHandler(ClientData clientData); |
| 185 |
static int SerialGetOptionProc _ANSI_ARGS_((ClientData instanceData, |
| 186 |
Tcl_Interp *interp, CONST char *optionName, |
| 187 |
Tcl_DString *dsPtr)); |
| 188 |
static int SerialSetOptionProc _ANSI_ARGS_((ClientData instanceData, |
| 189 |
Tcl_Interp *interp, CONST char *optionName, |
| 190 |
CONST char *value)); |
| 191 |
static DWORD WINAPI SerialWriterThread(LPVOID arg); |
| 192 |
|
| 193 |
/* |
| 194 |
* This structure describes the channel type structure for command serial |
| 195 |
* based IO. |
| 196 |
*/ |
| 197 |
|
| 198 |
static Tcl_ChannelType serialChannelType = { |
| 199 |
"serial", /* Type name. */ |
| 200 |
TCL_CHANNEL_VERSION_2, /* v2 channel */ |
| 201 |
SerialCloseProc, /* Close proc. */ |
| 202 |
SerialInputProc, /* Input proc. */ |
| 203 |
SerialOutputProc, /* Output proc. */ |
| 204 |
NULL, /* Seek proc. */ |
| 205 |
SerialSetOptionProc, /* Set option proc. */ |
| 206 |
SerialGetOptionProc, /* Get option proc. */ |
| 207 |
SerialWatchProc, /* Set up notifier to watch the channel. */ |
| 208 |
SerialGetHandleProc, /* Get an OS handle from channel. */ |
| 209 |
NULL, /* close2proc. */ |
| 210 |
SerialBlockProc, /* Set blocking or non-blocking mode.*/ |
| 211 |
NULL, /* flush proc. */ |
| 212 |
NULL, /* handler proc. */ |
| 213 |
}; |
| 214 |
|
| 215 |
/* |
| 216 |
*---------------------------------------------------------------------- |
| 217 |
* |
| 218 |
* SerialInit -- |
| 219 |
* |
| 220 |
* This function initializes the static variables for this file. |
| 221 |
* |
| 222 |
* Results: |
| 223 |
* None. |
| 224 |
* |
| 225 |
* Side effects: |
| 226 |
* Creates a new event source. |
| 227 |
* |
| 228 |
*---------------------------------------------------------------------- |
| 229 |
*/ |
| 230 |
|
| 231 |
static ThreadSpecificData * |
| 232 |
SerialInit() |
| 233 |
{ |
| 234 |
ThreadSpecificData *tsdPtr; |
| 235 |
|
| 236 |
/* |
| 237 |
* Check the initialized flag first, then check it again in the mutex. |
| 238 |
* This is a speed enhancement. |
| 239 |
*/ |
| 240 |
|
| 241 |
if (!initialized) { |
| 242 |
Tcl_MutexLock(&serialMutex); |
| 243 |
if (!initialized) { |
| 244 |
initialized = 1; |
| 245 |
Tcl_CreateExitHandler(ProcExitHandler, NULL); |
| 246 |
} |
| 247 |
Tcl_MutexUnlock(&serialMutex); |
| 248 |
} |
| 249 |
|
| 250 |
tsdPtr = (ThreadSpecificData *)TclThreadDataKeyGet(&dataKey); |
| 251 |
if (tsdPtr == NULL) { |
| 252 |
tsdPtr = TCL_TSD_INIT(&dataKey); |
| 253 |
tsdPtr->firstSerialPtr = NULL; |
| 254 |
Tcl_CreateEventSource(SerialSetupProc, SerialCheckProc, NULL); |
| 255 |
Tcl_CreateThreadExitHandler(SerialExitHandler, NULL); |
| 256 |
} |
| 257 |
return tsdPtr; |
| 258 |
} |
| 259 |
|
| 260 |
/* |
| 261 |
*---------------------------------------------------------------------- |
| 262 |
* |
| 263 |
* SerialExitHandler -- |
| 264 |
* |
| 265 |
* This function is called to cleanup the serial module before |
| 266 |
* Tcl is unloaded. |
| 267 |
* |
| 268 |
* Results: |
| 269 |
* None. |
| 270 |
* |
| 271 |
* Side effects: |
| 272 |
* Removes the serial event source. |
| 273 |
* |
| 274 |
*---------------------------------------------------------------------- |
| 275 |
*/ |
| 276 |
|
| 277 |
static void |
| 278 |
SerialExitHandler( |
| 279 |
ClientData clientData) /* Old window proc */ |
| 280 |
{ |
| 281 |
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey); |
| 282 |
SerialInfo *infoPtr; |
| 283 |
|
| 284 |
/* |
| 285 |
* Clear all eventually pending output. |
| 286 |
* Otherwise Tcl's exit could totally block, |
| 287 |
* because it performs a blocking flush on all open channels. |
| 288 |
* Note that serial write operations may be blocked due to handshake. |
| 289 |
*/ |
| 290 |
for (infoPtr = tsdPtr->firstSerialPtr; infoPtr != NULL; |
| 291 |
infoPtr = infoPtr->nextPtr) { |
| 292 |
PurgeComm(infoPtr->handle, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR |
| 293 |
| PURGE_RXCLEAR); |
| 294 |
|
| 295 |
} |
| 296 |
Tcl_DeleteEventSource(SerialSetupProc, SerialCheckProc, NULL); |
| 297 |
} |
| 298 |
|
| 299 |
/* |
| 300 |
*---------------------------------------------------------------------- |
| 301 |
* |
| 302 |
* ProcExitHandler -- |
| 303 |
* |
| 304 |
* This function is called to cleanup the process list before |
| 305 |
* Tcl is unloaded. |
| 306 |
* |
| 307 |
* Results: |
| 308 |
* None. |
| 309 |
* |
| 310 |
* Side effects: |
| 311 |
* Resets the process list. |
| 312 |
* |
| 313 |
*---------------------------------------------------------------------- |
| 314 |
*/ |
| 315 |
|
| 316 |
static void |
| 317 |
ProcExitHandler( |
| 318 |
ClientData clientData) /* Old window proc */ |
| 319 |
{ |
| 320 |
Tcl_MutexLock(&serialMutex); |
| 321 |
initialized = 0; |
| 322 |
Tcl_MutexUnlock(&serialMutex); |
| 323 |
} |
| 324 |
|
| 325 |
/* |
| 326 |
*---------------------------------------------------------------------- |
| 327 |
* |
| 328 |
* SerialBlockTime -- |
| 329 |
* |
| 330 |
* Wrapper to set Tcl's block time in msec |
| 331 |
* |
| 332 |
* Results: |
| 333 |
* None. |
| 334 |
*---------------------------------------------------------------------- |
| 335 |
*/ |
| 336 |
|
| 337 |
static void |
| 338 |
SerialBlockTime( |
| 339 |
int msec) /* milli-seconds */ |
| 340 |
{ |
| 341 |
Tcl_Time blockTime; |
| 342 |
|
| 343 |
blockTime.sec = msec / 1000; |
| 344 |
blockTime.usec = (msec % 1000) * 1000; |
| 345 |
Tcl_SetMaxBlockTime(&blockTime); |
| 346 |
} |
| 347 |
/* |
| 348 |
*---------------------------------------------------------------------- |
| 349 |
* |
| 350 |
* SerialGetMilliseconds -- |
| 351 |
* |
| 352 |
* Get current time in milliseconds, |
| 353 |
* Don't care about integer overruns |
| 354 |
* |
| 355 |
* Results: |
| 356 |
* None. |
| 357 |
*---------------------------------------------------------------------- |
| 358 |
*/ |
| 359 |
|
| 360 |
static unsigned int |
| 361 |
SerialGetMilliseconds( |
| 362 |
void) |
| 363 |
{ |
| 364 |
Tcl_Time time; |
| 365 |
|
| 366 |
TclpGetTime(&time); |
| 367 |
|
| 368 |
return (time.sec * 1000 + time.usec / 1000); |
| 369 |
} |
| 370 |
/* |
| 371 |
*---------------------------------------------------------------------- |
| 372 |
* |
| 373 |
* SerialSetupProc -- |
| 374 |
* |
| 375 |
* This procedure is invoked before Tcl_DoOneEvent blocks waiting |
| 376 |
* for an event. |
| 377 |
* |
| 378 |
* Results: |
| 379 |
* None. |
| 380 |
* |
| 381 |
* Side effects: |
| 382 |
* Adjusts the block time if needed. |
| 383 |
* |
| 384 |
*---------------------------------------------------------------------- |
| 385 |
*/ |
| 386 |
|
| 387 |
void |
| 388 |
SerialSetupProc( |
| 389 |
ClientData data, /* Not used. */ |
| 390 |
int flags) /* Event flags as passed to Tcl_DoOneEvent. */ |
| 391 |
{ |
| 392 |
SerialInfo *infoPtr; |
| 393 |
int block = 1; |
| 394 |
int msec = INT_MAX; /* min. found block time */ |
| 395 |
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey); |
| 396 |
|
| 397 |
if (!(flags & TCL_FILE_EVENTS)) { |
| 398 |
return; |
| 399 |
} |
| 400 |
|
| 401 |
/* |
| 402 |
* Look to see if any events handlers installed. If they are, do not block. |
| 403 |
*/ |
| 404 |
|
| 405 |
for (infoPtr = tsdPtr->firstSerialPtr; infoPtr != NULL; |
| 406 |
infoPtr = infoPtr->nextPtr) { |
| 407 |
|
| 408 |
if (infoPtr->watchMask & TCL_WRITABLE) { |
| 409 |
if (WaitForSingleObject(infoPtr->evWritable, 0) != WAIT_TIMEOUT) { |
| 410 |
block = 0; |
| 411 |
msec = min( msec, infoPtr->blockTime ); |
| 412 |
} |
| 413 |
} |
| 414 |
if( infoPtr->watchMask & TCL_READABLE ) { |
| 415 |
block = 0; |
| 416 |
msec = min( msec, infoPtr->blockTime ); |
| 417 |
} |
| 418 |
} |
| 419 |
|
| 420 |
if (!block) { |
| 421 |
SerialBlockTime(msec); |
| 422 |
} |
| 423 |
} |
| 424 |
|
| 425 |
/* |
| 426 |
*---------------------------------------------------------------------- |
| 427 |
* |
| 428 |
* SerialCheckProc -- |
| 429 |
* |
| 430 |
* This procedure is called by Tcl_DoOneEvent to check the serial |
| 431 |
* event source for events. |
| 432 |
* |
| 433 |
* Results: |
| 434 |
* None. |
| 435 |
* |
| 436 |
* Side effects: |
| 437 |
* May queue an event. |
| 438 |
* |
| 439 |
*---------------------------------------------------------------------- |
| 440 |
*/ |
| 441 |
|
| 442 |
static void |
| 443 |
SerialCheckProc( |
| 444 |
ClientData data, /* Not used. */ |
| 445 |
int flags) /* Event flags as passed to Tcl_DoOneEvent. */ |
| 446 |
{ |
| 447 |
SerialInfo *infoPtr; |
| 448 |
SerialEvent *evPtr; |
| 449 |
int needEvent; |
| 450 |
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey); |
| 451 |
COMSTAT cStat; |
| 452 |
unsigned int time; |
| 453 |
|
| 454 |
if (!(flags & TCL_FILE_EVENTS)) { |
| 455 |
return; |
| 456 |
} |
| 457 |
|
| 458 |
/* |
| 459 |
* Queue events for any ready serials that don't already have events |
| 460 |
* queued. |
| 461 |
*/ |
| 462 |
|
| 463 |
for (infoPtr = tsdPtr->firstSerialPtr; infoPtr != NULL; |
| 464 |
infoPtr = infoPtr->nextPtr) { |
| 465 |
if (infoPtr->flags & SERIAL_PENDING) { |
| 466 |
continue; |
| 467 |
} |
| 468 |
|
| 469 |
needEvent = 0; |
| 470 |
|
| 471 |
/* |
| 472 |
* If WRITABLE watch mask is set |
| 473 |
* look for infoPtr->evWritable object |
| 474 |
*/ |
| 475 |
if (infoPtr->watchMask & TCL_WRITABLE) { |
| 476 |
if (WaitForSingleObject(infoPtr->evWritable, 0) != WAIT_TIMEOUT) { |
| 477 |
infoPtr->writable = 1; |
| 478 |
needEvent = 1; |
| 479 |
} |
| 480 |
} |
| 481 |
|
| 482 |
/* |
| 483 |
* If READABLE watch mask is set |
| 484 |
* call ClearCommError to poll cbInQue |
| 485 |
* Window errors are ignored here |
| 486 |
*/ |
| 487 |
|
| 488 |
if( infoPtr->watchMask & TCL_READABLE ) { |
| 489 |
if( ClearCommError( infoPtr->handle, &infoPtr->error, &cStat ) ) { |
| 490 |
/* |
| 491 |
* Look for characters already pending in windows queue. |
| 492 |
* If they are, poll. |
| 493 |
*/ |
| 494 |
|
| 495 |
if( infoPtr->watchMask & TCL_READABLE ) { |
| 496 |
/* |
| 497 |
* force fileevent after serial read error |
| 498 |
*/ |
| 499 |
if( (cStat.cbInQue > 0) || |
| 500 |
(infoPtr->error & SERIAL_READ_ERRORS) ) { |
| 501 |
infoPtr->readable = 1; |
| 502 |
time = SerialGetMilliseconds(); |
| 503 |
if ((unsigned int) (time - infoPtr->lastEventTime) |
| 504 |
>= (unsigned int) infoPtr->blockTime) { |
| 505 |
needEvent = 1; |
| 506 |
infoPtr->lastEventTime = time; |
| 507 |
} |
| 508 |
} |
| 509 |
} |
| 510 |
} |
| 511 |
} |
| 512 |
|
| 513 |
/* |
| 514 |
* Queue an event if the serial is signaled for reading or writing. |
| 515 |
*/ |
| 516 |
if (needEvent) { |
| 517 |
infoPtr->flags |= SERIAL_PENDING; |
| 518 |
evPtr = (SerialEvent *) ckalloc(sizeof(SerialEvent)); |
| 519 |
evPtr->header.proc = SerialEventProc; |
| 520 |
evPtr->infoPtr = infoPtr; |
| 521 |
Tcl_QueueEvent((Tcl_Event *) evPtr, TCL_QUEUE_TAIL); |
| 522 |
} |
| 523 |
} |
| 524 |
} |
| 525 |
|
| 526 |
/* |
| 527 |
*---------------------------------------------------------------------- |
| 528 |
* |
| 529 |
* SerialBlockProc -- |
| 530 |
* |
| 531 |
* Set blocking or non-blocking mode on channel. |
| 532 |
* |
| 533 |
* Results: |
| 534 |
* 0 if successful, errno when failed. |
| 535 |
* |
| 536 |
* Side effects: |
| 537 |
* Sets the device into blocking or non-blocking mode. |
| 538 |
* |
| 539 |
*---------------------------------------------------------------------- |
| 540 |
*/ |
| 541 |
|
| 542 |
static int |
| 543 |
SerialBlockProc( |
| 544 |
ClientData instanceData, /* Instance data for channel. */ |
| 545 |
int mode) /* TCL_MODE_BLOCKING or |
| 546 |
* TCL_MODE_NONBLOCKING. */ |
| 547 |
{ |
| 548 |
int errorCode = 0; |
| 549 |
|
| 550 |
SerialInfo *infoPtr = (SerialInfo *) instanceData; |
| 551 |
|
| 552 |
/* |
| 553 |
* Only serial READ can be switched between blocking & nonblocking |
| 554 |
* using COMMTIMEOUTS. |
| 555 |
* Serial write emulates blocking & nonblocking by the SerialWriterThread. |
| 556 |
*/ |
| 557 |
|
| 558 |
if (mode == TCL_MODE_NONBLOCKING) { |
| 559 |
infoPtr->flags |= SERIAL_ASYNC; |
| 560 |
} else { |
| 561 |
infoPtr->flags &= ~(SERIAL_ASYNC); |
| 562 |
} |
| 563 |
return errorCode; |
| 564 |
} |
| 565 |
|
| 566 |
/* |
| 567 |
*---------------------------------------------------------------------- |
| 568 |
* |
| 569 |
* SerialCloseProc -- |
| 570 |
* |
| 571 |
* Closes a serial based IO channel. |
| 572 |
* |
| 573 |
* Results: |
| 574 |
* 0 on success, errno otherwise. |
| 575 |
* |
| 576 |
* Side effects: |
| 577 |
* Closes the physical channel. |
| 578 |
* |
| 579 |
*---------------------------------------------------------------------- |
| 580 |
*/ |
| 581 |
|
| 582 |
static int |
| 583 |
SerialCloseProc( |
| 584 |
ClientData instanceData, /* Pointer to SerialInfo structure. */ |
| 585 |
Tcl_Interp *interp) /* For error reporting. */ |
| 586 |
{ |
| 587 |
SerialInfo *serialPtr = (SerialInfo *) instanceData; |
| 588 |
int errorCode, result = 0; |
| 589 |
SerialInfo *infoPtr, **nextPtrPtr; |
| 590 |
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey); |
| 591 |
DWORD exitCode; |
| 592 |
|
| 593 |
errorCode = 0; |
| 594 |
|
| 595 |
if (serialPtr->validMask & TCL_READABLE) { |
| 596 |
PurgeComm(serialPtr->handle, PURGE_RXABORT | PURGE_RXCLEAR); |
| 597 |
CloseHandle(serialPtr->osRead.hEvent); |
| 598 |
} |
| 599 |
serialPtr->validMask &= ~TCL_READABLE; |
| 600 |
|
| 601 |
if (serialPtr->validMask & TCL_WRITABLE) { |
| 602 |
|
| 603 |
/* |
| 604 |
* Generally we cannot wait for a pending write operation |
| 605 |
* because it may hang due to handshake |
| 606 |
* WaitForSingleObject(serialPtr->evWritable, INFINITE); |
| 607 |
*/ |
| 608 |
|
| 609 |
/* |
| 610 |
* The thread may have already closed on it's own. Check it's |
| 611 |
* exit code. |
| 612 |
*/ |
| 613 |
|
| 614 |
GetExitCodeThread(serialPtr->writeThread, &exitCode); |
| 615 |
|
| 616 |
if (exitCode == STILL_ACTIVE) { |
| 617 |
/* |
| 618 |
* Set the stop event so that if the writer thread is |
| 619 |
* blocked in SerialWriterThread on WaitForMultipleEvents, it |
| 620 |
* will exit cleanly. |
| 621 |
*/ |
| 622 |
|
| 623 |
SetEvent(serialPtr->evStopWriter); |
| 624 |
|
| 625 |
/* |
| 626 |
* Wait at most 20 milliseconds for the writer thread to |
| 627 |
* close. |
| 628 |
*/ |
| 629 |
|
| 630 |
if (WaitForSingleObject(serialPtr->writeThread, 20) |
| 631 |
== WAIT_TIMEOUT) { |
| 632 |
/* |
| 633 |
* Forcibly terminate the background thread as a last |
| 634 |
* resort. Note that we need to guard against |
| 635 |
* terminating the thread while it is in the middle of |
| 636 |
* Tcl_ThreadAlert because it won't be able to release |
| 637 |
* the notifier lock. |
| 638 |
*/ |
| 639 |
|
| 640 |
Tcl_MutexLock(&serialMutex); |
| 641 |
|
| 642 |
/* BUG: this leaks memory */ |
| 643 |
TerminateThread(serialPtr->writeThread, 0); |
| 644 |
|
| 645 |
Tcl_MutexUnlock(&serialMutex); |
| 646 |
} |
| 647 |
} |
| 648 |
|
| 649 |
CloseHandle(serialPtr->writeThread); |
| 650 |
CloseHandle(serialPtr->osWrite.hEvent); |
| 651 |
DeleteCriticalSection(&serialPtr->csWrite); |
| 652 |
CloseHandle(serialPtr->evWritable); |
| 653 |
CloseHandle(serialPtr->evStartWriter); |
| 654 |
CloseHandle(serialPtr->evStopWriter); |
| 655 |
serialPtr->writeThread = NULL; |
| 656 |
|
| 657 |
PurgeComm(serialPtr->handle, PURGE_TXABORT | PURGE_TXCLEAR); |
| 658 |
} |
| 659 |
serialPtr->validMask &= ~TCL_WRITABLE; |
| 660 |
|
| 661 |
/* |
| 662 |
* Don't close the Win32 handle if the handle is a standard channel |
| 663 |
* during the thread exit process. Otherwise, one thread may kill |
| 664 |
* the stdio of another. |
| 665 |
*/ |
| 666 |
|
| 667 |
if (!TclInThreadExit() |
| 668 |
|| ((GetStdHandle(STD_INPUT_HANDLE) != serialPtr->handle) |
| 669 |
&& (GetStdHandle(STD_OUTPUT_HANDLE) != serialPtr->handle) |
| 670 |
&& (GetStdHandle(STD_ERROR_HANDLE) != serialPtr->handle))) { |
| 671 |
if (CloseHandle(serialPtr->handle) == FALSE) { |
| 672 |
TclWinConvertError(GetLastError()); |
| 673 |
errorCode = errno; |
| 674 |
} |
| 675 |
} |
| 676 |
|
| 677 |
serialPtr->watchMask &= serialPtr->validMask; |
| 678 |
|
| 679 |
/* |
| 680 |
* Remove the file from the list of watched files. |
| 681 |
*/ |
| 682 |
|
| 683 |
for (nextPtrPtr = &(tsdPtr->firstSerialPtr), infoPtr = *nextPtrPtr; |
| 684 |
infoPtr != NULL; |
| 685 |
nextPtrPtr = &infoPtr->nextPtr, infoPtr = *nextPtrPtr) { |
| 686 |
if (infoPtr == (SerialInfo *)serialPtr) { |
| 687 |
*nextPtrPtr = infoPtr->nextPtr; |
| 688 |
break; |
| 689 |
} |
| 690 |
} |
| 691 |
|
| 692 |
/* |
| 693 |
* Wrap the error file into a channel and give it to the cleanup |
| 694 |
* routine. |
| 695 |
*/ |
| 696 |
if (serialPtr->writeBuf != NULL) { |
| 697 |
ckfree(serialPtr->writeBuf); |
| 698 |
serialPtr->writeBuf = NULL; |
| 699 |
} |
| 700 |
ckfree((char*) serialPtr); |
| 701 |
|
| 702 |
if (errorCode == 0) { |
| 703 |
return result; |
| 704 |
} |
| 705 |
return errorCode; |
| 706 |
} |
| 707 |
|
| 708 |
/* |
| 709 |
*---------------------------------------------------------------------- |
| 710 |
* |
| 711 |
* blockingRead -- |
| 712 |
* |
| 713 |
* Perform a blocking read into the buffer given. Returns |
| 714 |
* count of how many bytes were actually read, and an error indication. |
| 715 |
* |
| 716 |
* Results: |
| 717 |
* A count of how many bytes were read is returned and an error |
| 718 |
* indication is returned. |
| 719 |
* |
| 720 |
* Side effects: |
| 721 |
* Reads input from the actual channel. |
| 722 |
* |
| 723 |
*---------------------------------------------------------------------- |
| 724 |
*/ |
| 725 |
static int |
| 726 |
blockingRead( |
| 727 |
SerialInfo *infoPtr, /* Serial info structure */ |
| 728 |
LPVOID buf, /* The input buffer pointer */ |
| 729 |
DWORD bufSize, /* The number of bytes to read */ |
| 730 |
LPDWORD lpRead, /* Returns number of bytes read */ |
| 731 |
LPOVERLAPPED osPtr ) /* OVERLAPPED structure */ |
| 732 |
{ |
| 733 |
/* |
| 734 |
* Perform overlapped blocking read. |
| 735 |
* 1. Reset the overlapped event |
| 736 |
* 2. Start overlapped read operation |
| 737 |
* 3. Wait for completion |
| 738 |
*/ |
| 739 |
|
| 740 |
/* |
| 741 |
* Set Offset to ZERO, otherwise NT4.0 may report an error. |
| 742 |
*/ |
| 743 |
osPtr->Offset = osPtr->OffsetHigh = 0; |
| 744 |
ResetEvent(osPtr->hEvent); |
| 745 |
if (! ReadFile(infoPtr->handle, buf, bufSize, lpRead, osPtr) ) { |
| 746 |
if (GetLastError() != ERROR_IO_PENDING) { |
| 747 |
/* ReadFile failed, but it isn't delayed. Report error. */ |
| 748 |
return FALSE; |
| 749 |
} else { |
| 750 |
/* Read is pending, wait for completion, timeout ? */ |
| 751 |
if (! GetOverlappedResult(infoPtr->handle, osPtr, lpRead, TRUE) ) { |
| 752 |
return FALSE; |
| 753 |
} |
| 754 |
} |
| 755 |
} else { |
| 756 |
/* ReadFile completed immediately. */ |
| 757 |
} |
| 758 |
return TRUE; |
| 759 |
} |
| 760 |
|
| 761 |
/* |
| 762 |
*---------------------------------------------------------------------- |
| 763 |
* |
| 764 |
* blockingWrite -- |
| 765 |
* |
| 766 |
* Perform a blocking write from the buffer given. Returns |
| 767 |
* count of how many bytes were actually written, and an error indication. |
| 768 |
* |
| 769 |
* Results: |
| 770 |
* A count of how many bytes were written is returned and an error |
| 771 |
* indication is returned. |
| 772 |
* |
| 773 |
* Side effects: |
| 774 |
* Writes output to the actual channel. |
| 775 |
* |
| 776 |
*---------------------------------------------------------------------- |
| 777 |
*/ |
| 778 |
static int |
| 779 |
blockingWrite( |
| 780 |
SerialInfo *infoPtr, /* Serial info structure */ |
| 781 |
LPVOID buf, /* The output buffer pointer */ |
| 782 |
DWORD bufSize, /* The number of bytes to write */ |
| 783 |
LPDWORD lpWritten, /* Returns number of bytes written */ |
| 784 |
LPOVERLAPPED osPtr ) /* OVERLAPPED structure */ |
| 785 |
{ |
| 786 |
int result; |
| 787 |
/* |
| 788 |
* Perform overlapped blocking write. |
| 789 |
* 1. Reset the overlapped event |
| 790 |
* 2. Remove these bytes from the output queue counter |
| 791 |
* 3. Start overlapped write operation |
| 792 |
* 3. Remove these bytes from the output queue counter |
| 793 |
* 4. Wait for completion |
| 794 |
* 5. Adjust the output queue counter |
| 795 |
*/ |
| 796 |
ResetEvent(osPtr->hEvent); |
| 797 |
|
| 798 |
EnterCriticalSection(&infoPtr->csWrite); |
| 799 |
infoPtr->writeQueue -= bufSize; |
| 800 |
/* |
| 801 |
* Set Offset to ZERO, otherwise NT4.0 may report an error |
| 802 |
*/ |
| 803 |
osPtr->Offset = osPtr->OffsetHigh = 0; |
| 804 |
result = WriteFile(infoPtr->handle, buf, bufSize, lpWritten, osPtr); |
| 805 |
LeaveCriticalSection(&infoPtr->csWrite); |
| 806 |
|
| 807 |
if (result == FALSE ) { |
| 808 |
int err = GetLastError(); |
| 809 |
switch (err) { |
| 810 |
case ERROR_IO_PENDING: |
| 811 |
/* Write is pending, wait for completion */ |
| 812 |
if (! GetOverlappedResult(infoPtr->handle, osPtr, lpWritten, TRUE) ) { |
| 813 |
return FALSE; |
| 814 |
} |
| 815 |
break; |
| 816 |
case ERROR_COUNTER_TIMEOUT: |
| 817 |
/* Write timeout handled in SerialOutputProc */ |
| 818 |
break; |
| 819 |
default: |
| 820 |
/* WriteFile failed, but it isn't delayed. Report error */ |
| 821 |
return FALSE; |
| 822 |
} |
| 823 |
} else { |
| 824 |
/* WriteFile completed immediately. */ |
| 825 |
} |
| 826 |
|
| 827 |
EnterCriticalSection(&infoPtr->csWrite); |
| 828 |
infoPtr->writeQueue += (*lpWritten - bufSize); |
| 829 |
LeaveCriticalSection(&infoPtr->csWrite); |
| 830 |
|
| 831 |
return TRUE; |
| 832 |
} |
| 833 |
|
| 834 |
/* |
| 835 |
*---------------------------------------------------------------------- |
| 836 |
* |
| 837 |
* SerialInputProc -- |
| 838 |
* |
| 839 |
* Reads input from the IO channel into the buffer given. Returns |
| 840 |
* count of how many bytes were actually read, and an error indication. |
| 841 |
* |
| 842 |
* Results: |
| 843 |
* A count of how many bytes were read is returned and an error |
| 844 |
* indication is returned in an output argument. |
| 845 |
* |
| 846 |
* Side effects: |
| 847 |
* Reads input from the actual channel. |
| 848 |
* |
| 849 |
*---------------------------------------------------------------------- |
| 850 |
*/ |
| 851 |
static int |
| 852 |
SerialInputProc( |
| 853 |
ClientData instanceData, /* Serial state. */ |
| 854 |
char *buf, /* Where to store data read. */ |
| 855 |
int bufSize, /* How much space is available |
| 856 |
* in the buffer? */ |
| 857 |
int *errorCode) /* Where to store error code. */ |
| 858 |
{ |
| 859 |
SerialInfo *infoPtr = (SerialInfo *) instanceData; |
| 860 |
DWORD bytesRead = 0; |
| 861 |
COMSTAT cStat; |
| 862 |
|
| 863 |
*errorCode = 0; |
| 864 |
|
| 865 |
/* |
| 866 |
* Check if there is a CommError pending from SerialCheckProc |
| 867 |
*/ |
| 868 |
if( infoPtr->error & SERIAL_READ_ERRORS ){ |
| 869 |
goto commError; |
| 870 |
} |
| 871 |
|
| 872 |
/* |
| 873 |
* Look for characters already pending in windows queue. |
| 874 |
* This is the mainly restored good old code from Tcl8.0 |
| 875 |
*/ |
| 876 |
|
| 877 |
if( ClearCommError( infoPtr->handle, &infoPtr->error, &cStat ) ) { |
| 878 |
/* |
| 879 |
* Check for errors here, but not in the evSetup/Check procedures |
| 880 |
*/ |
| 881 |
|
| 882 |
if( infoPtr->error & SERIAL_READ_ERRORS ) { |
| 883 |
goto commError; |
| 884 |
} |
| 885 |
if( infoPtr->flags & SERIAL_ASYNC ) { |
| 886 |
/* |
| 887 |
* NON_BLOCKING mode: |
| 888 |
* Avoid blocking by reading more bytes than available |
| 889 |
* in input buffer |
| 890 |
*/ |
| 891 |
|
| 892 |
if( cStat.cbInQue > 0 ) { |
| 893 |
if( (DWORD) bufSize > cStat.cbInQue ) { |
| 894 |
bufSize = cStat.cbInQue; |
| 895 |
} |
| 896 |
} else { |
| 897 |
errno = *errorCode = EAGAIN; |
| 898 |
return -1; |
| 899 |
} |
| 900 |
} else { |
| 901 |
/* |
| 902 |
* BLOCKING mode: |
| 903 |
* Tcl trys to read a full buffer of 4 kBytes here |
| 904 |
*/ |
| 905 |
|
| 906 |
if( cStat.cbInQue > 0 ) { |
| 907 |
if( (DWORD) bufSize > cStat.cbInQue ) { |
| 908 |
bufSize = cStat.cbInQue; |
| 909 |
} |
| 910 |
} else { |
| 911 |
bufSize = 1; |
| 912 |
} |
| 913 |
} |
| 914 |
} |
| 915 |
|
| 916 |
if( bufSize == 0 ) { |
| 917 |
return bytesRead = 0; |
| 918 |
} |
| 919 |
|
| 920 |
/* |
| 921 |
* Perform blocking read. Doesn't block in non-blocking mode, |
| 922 |
* because we checked the number of available bytes. |
| 923 |
*/ |
| 924 |
if (blockingRead(infoPtr, (LPVOID) buf, (DWORD) bufSize, &bytesRead, |
| 925 |
&infoPtr->osRead) == FALSE) { |
| 926 |
goto error; |
| 927 |
} |
| 928 |
return bytesRead; |
| 929 |
|
| 930 |
error: |
| 931 |
TclWinConvertError(GetLastError()); |
| 932 |
*errorCode = errno; |
| 933 |
return -1; |
| 934 |
|
| 935 |
commError: |
| 936 |
infoPtr->lastError = infoPtr->error; /* save last error code */ |
| 937 |
infoPtr->error = 0; /* reset error code */ |
| 938 |
*errorCode = EIO; /* to return read-error only once */ |
| 939 |
return -1; |
| 940 |
} |
| 941 |
|
| 942 |
/* |
| 943 |
*---------------------------------------------------------------------- |
| 944 |
* |
| 945 |
* SerialOutputProc -- |
| 946 |
* |
| 947 |
* Writes the given output on the IO channel. Returns count of how |
| 948 |
* many characters were actually written, and an error indication. |
| 949 |
* |
| 950 |
* Results: |
| 951 |
* A count of how many characters were written is returned and an |
| 952 |
* error indication is returned in an output argument. |
| 953 |
* |
| 954 |
* Side effects: |
| 955 |
* Writes output on the actual channel. |
| 956 |
* |
| 957 |
*---------------------------------------------------------------------- |
| 958 |
*/ |
| 959 |
|
| 960 |
static int |
| 961 |
SerialOutputProc( |
| 962 |
ClientData instanceData, /* Serial state. */ |
| 963 |
CONST char *buf, /* The data buffer. */ |
| 964 |
int toWrite, /* How many bytes to write? */ |
| 965 |
int *errorCode) /* Where to store error code. */ |
| 966 |
{ |
| 967 |
SerialInfo *infoPtr = (SerialInfo *) instanceData; |
| 968 |
DWORD bytesWritten, timeout; |
| 969 |
|
| 970 |
*errorCode = 0; |
| 971 |
|
| 972 |
/* |
| 973 |
* At EXIT Tcl trys to flush all open channels in blocking mode. |
| 974 |
* We avoid blocking output after ExitProc or CloseHandler(chan) |
| 975 |
* has been called by checking the corrresponding variables. |
| 976 |
*/ |
| 977 |
if( ! initialized || TclInExit() ) { |
| 978 |
return toWrite; |
| 979 |
} |
| 980 |
|
| 981 |
/* |
| 982 |
* Check if there is a CommError pending from SerialCheckProc |
| 983 |
*/ |
| 984 |
if( infoPtr->error & SERIAL_WRITE_ERRORS ){ |
| 985 |
infoPtr->lastError = infoPtr->error; /* save last error code */ |
| 986 |
infoPtr->error = 0; /* reset error code */ |
| 987 |
errno = EIO; |
| 988 |
goto error; |
| 989 |
} |
| 990 |
|
| 991 |
timeout = (infoPtr->flags & SERIAL_ASYNC) ? 0 : INFINITE; |
| 992 |
if (WaitForSingleObject(infoPtr->evWritable, timeout) == WAIT_TIMEOUT) { |
| 993 |
/* |
| 994 |
* The writer thread is blocked waiting for a write to complete |
| 995 |
* and the channel is in non-blocking mode. |
| 996 |
*/ |
| 997 |
|
| 998 |
errno = EWOULDBLOCK; |
| 999 |
goto error1; |
| 1000 |
} |
| 1001 |
/* |
| 1002 |
* Check for a background error on the last write. |
| 1003 |
*/ |
| 1004 |
|
| 1005 |
if (infoPtr->writeError) { |
| 1006 |
TclWinConvertError(infoPtr->writeError); |
| 1007 |
infoPtr->writeError = 0; |
| 1008 |
goto error1; |
| 1009 |
} |
| 1010 |
|
| 1011 |
/* |
| 1012 |
* Remember the number of bytes in output queue |
| 1013 |
*/ |
| 1014 |
EnterCriticalSection(&infoPtr->csWrite); |
| 1015 |
infoPtr->writeQueue += toWrite; |
| 1016 |
LeaveCriticalSection(&infoPtr->csWrite); |
| 1017 |
|
| 1018 |
if (infoPtr->flags & SERIAL_ASYNC) { |
| 1019 |
/* |
| 1020 |
* The serial is non-blocking, so copy the data into the output |
| 1021 |
* buffer and restart the writer thread. |
| 1022 |
*/ |
| 1023 |
|
| 1024 |
if (toWrite > infoPtr->writeBufLen) { |
| 1025 |
/* |
| 1026 |
* Reallocate the buffer to be large enough to hold the data. |
| 1027 |
*/ |
| 1028 |
|
| 1029 |
if (infoPtr->writeBuf) { |
| 1030 |
ckfree(infoPtr->writeBuf); |
| 1031 |
} |
| 1032 |
infoPtr->writeBufLen = toWrite; |
| 1033 |
infoPtr->writeBuf = ckalloc((unsigned int) toWrite); |
| 1034 |
} |
| 1035 |
memcpy(infoPtr->writeBuf, buf, (size_t) toWrite); |
| 1036 |
infoPtr->toWrite = toWrite; |
| 1037 |
ResetEvent(infoPtr->evWritable); |
| 1038 |
SetEvent(infoPtr->evStartWriter); |
| 1039 |
bytesWritten = (DWORD) toWrite; |
| 1040 |
|
| 1041 |
} else { |
| 1042 |
/* |
| 1043 |
* In the blocking case, just try to write the buffer directly. |
| 1044 |
* This avoids an unnecessary copy. |
| 1045 |
*/ |
| 1046 |
if (! blockingWrite(infoPtr, (LPVOID) buf, (DWORD) toWrite, |
| 1047 |
&bytesWritten, &infoPtr->osWrite) ) { |
| 1048 |
goto writeError; |
| 1049 |
} |
| 1050 |
if (bytesWritten != (DWORD) toWrite) { |
| 1051 |
/* Write timeout */ |
| 1052 |
infoPtr->lastError |= CE_PTO; |
| 1053 |
errno = EIO; |
| 1054 |
goto error; |
| 1055 |
} |
| 1056 |
} |
| 1057 |
|
| 1058 |
return (int) bytesWritten; |
| 1059 |
|
| 1060 |
writeError: |
| 1061 |
TclWinConvertError(GetLastError()); |
| 1062 |
|
| 1063 |
error: |
| 1064 |
/* |
| 1065 |
* Reset the output queue counter on error during blocking output |
| 1066 |
*/ |
| 1067 |
/* |
| 1068 |
EnterCriticalSection(&infoPtr->csWrite); |
| 1069 |
infoPtr->writeQueue = 0; |
| 1070 |
LeaveCriticalSection(&infoPtr->csWrite); |
| 1071 |
*/ |
| 1072 |
error1: |
| 1073 |
*errorCode = errno; |
| 1074 |
return -1; |
| 1075 |
} |
| 1076 |
|
| 1077 |
/* |
| 1078 |
*---------------------------------------------------------------------- |
| 1079 |
* |
| 1080 |
* SerialEventProc -- |
| 1081 |
* |
| 1082 |
* This function is invoked by Tcl_ServiceEvent when a file event |
| 1083 |
* reaches the front of the event queue. This procedure invokes |
| 1084 |
* Tcl_NotifyChannel on the serial. |
| 1085 |
* |
| 1086 |
* Results: |
| 1087 |
* Returns 1 if the event was handled, meaning it should be removed |
| 1088 |
* from the queue. Returns 0 if the event was not handled, meaning |
| 1089 |
* it should stay on the queue. The only time the event isn't |
| 1090 |
* handled is if the TCL_FILE_EVENTS flag bit isn't set. |
| 1091 |
* |
| 1092 |
* Side effects: |
| 1093 |
* Whatever the notifier callback does. |
| 1094 |
* |
| 1095 |
*---------------------------------------------------------------------- |
| 1096 |
*/ |
| 1097 |
|
| 1098 |
static int |
| 1099 |
SerialEventProc( |
| 1100 |
Tcl_Event *evPtr, /* Event to service. */ |
| 1101 |
int flags) /* Flags that indicate what events to |
| 1102 |
* handle, such as TCL_FILE_EVENTS. */ |
| 1103 |
{ |
| 1104 |
SerialEvent *serialEvPtr = (SerialEvent *)evPtr; |
| 1105 |
SerialInfo *infoPtr; |
| 1106 |
int mask; |
| 1107 |
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey); |
| 1108 |
|
| 1109 |
if (!(flags & TCL_FILE_EVENTS)) { |
| 1110 |
return 0; |
| 1111 |
} |
| 1112 |
|
| 1113 |
/* |
| 1114 |
* Search through the list of watched serials for the one whose handle |
| 1115 |
* matches the event. We do this rather than simply dereferencing |
| 1116 |
* the handle in the event so that serials can be deleted while the |
| 1117 |
* event is in the queue. |
| 1118 |
*/ |
| 1119 |
|
| 1120 |
for (infoPtr = tsdPtr->firstSerialPtr; infoPtr != NULL; |
| 1121 |
infoPtr = infoPtr->nextPtr) { |
| 1122 |
if (serialEvPtr->infoPtr == infoPtr) { |
| 1123 |
infoPtr->flags &= ~(SERIAL_PENDING); |
| 1124 |
break; |
| 1125 |
} |
| 1126 |
} |
| 1127 |
|
| 1128 |
/* |
| 1129 |
* Remove stale events. |
| 1130 |
*/ |
| 1131 |
|
| 1132 |
if (!infoPtr) { |
| 1133 |
return 1; |
| 1134 |
} |
| 1135 |
|
| 1136 |
/* |
| 1137 |
* Check to see if the serial is readable. Note |
| 1138 |
* that we can't tell if a serial is writable, so we always report it |
| 1139 |
* as being writable unless we have detected EOF. |
| 1140 |
*/ |
| 1141 |
|
| 1142 |
mask = 0; |
| 1143 |
if( infoPtr->watchMask & TCL_WRITABLE ) { |
| 1144 |
if( infoPtr->writable ) { |
| 1145 |
mask |= TCL_WRITABLE; |
| 1146 |
infoPtr->writable = 0; |
| 1147 |
} |
| 1148 |
} |
| 1149 |
|
| 1150 |
if( infoPtr->watchMask & TCL_READABLE ) { |
| 1151 |
if( infoPtr->readable ) { |
| 1152 |
mask |= TCL_READABLE; |
| 1153 |
infoPtr->readable = 0; |
| 1154 |
} |
| 1155 |
} |
| 1156 |
|
| 1157 |
/* |
| 1158 |
* Inform the channel of the events. |
| 1159 |
*/ |
| 1160 |
|
| 1161 |
Tcl_NotifyChannel(infoPtr->channel, infoPtr->watchMask & mask); |
| 1162 |
return 1; |
| 1163 |
} |
| 1164 |
|
| 1165 |
/* |
| 1166 |
*---------------------------------------------------------------------- |
| 1167 |
* |
| 1168 |
* SerialWatchProc -- |
| 1169 |
* |
| 1170 |
* Called by the notifier to set up to watch for events on this |
| 1171 |
* channel. |
| 1172 |
* |
| 1173 |
* Results: |
| 1174 |
* None. |
| 1175 |
* |
| 1176 |
* Side effects: |
| 1177 |
* None. |
| 1178 |
* |
| 1179 |
*---------------------------------------------------------------------- |
| 1180 |
*/ |
| 1181 |
|
| 1182 |
static void |
| 1183 |
SerialWatchProc( |
| 1184 |
ClientData instanceData, /* Serial state. */ |
| 1185 |
int mask) /* What events to watch for, OR-ed |
| 1186 |
* combination of TCL_READABLE, |
| 1187 |
* TCL_WRITABLE and TCL_EXCEPTION. */ |
| 1188 |
{ |
| 1189 |
SerialInfo **nextPtrPtr, *ptr; |
| 1190 |
SerialInfo *infoPtr = (SerialInfo *) instanceData; |
| 1191 |
int oldMask = infoPtr->watchMask; |
| 1192 |
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey); |
| 1193 |
|
| 1194 |
/* |
| 1195 |
* Since the file is always ready for events, we set the block time |
| 1196 |
* so we will poll. |
| 1197 |
*/ |
| 1198 |
|
| 1199 |
infoPtr->watchMask = mask & infoPtr->validMask; |
| 1200 |
if (infoPtr->watchMask) { |
| 1201 |
if (!oldMask) { |
| 1202 |
infoPtr->nextPtr = tsdPtr->firstSerialPtr; |
| 1203 |
tsdPtr->firstSerialPtr = infoPtr; |
| 1204 |
} |
| 1205 |
SerialBlockTime(infoPtr->blockTime); |
| 1206 |
} else { |
| 1207 |
if (oldMask) { |
| 1208 |
/* |
| 1209 |
* Remove the serial port from the list of watched serial ports. |
| 1210 |
*/ |
| 1211 |
|
| 1212 |
for (nextPtrPtr = &(tsdPtr->firstSerialPtr), ptr = *nextPtrPtr; |
| 1213 |
ptr != NULL; |
| 1214 |
nextPtrPtr = &ptr->nextPtr, ptr = *nextPtrPtr) { |
| 1215 |
if (infoPtr == ptr) { |
| 1216 |
*nextPtrPtr = ptr->nextPtr; |
| 1217 |
break; |
| 1218 |
} |
| 1219 |
} |
| 1220 |
} |
| 1221 |
} |
| 1222 |
} |
| 1223 |
|
| 1224 |
/* |
| 1225 |
*---------------------------------------------------------------------- |
| 1226 |
* |
| 1227 |
* SerialGetHandleProc -- |
| 1228 |
* |
| 1229 |
* Called from Tcl_GetChannelHandle to retrieve OS handles from |
| 1230 |
* inside a command serial port based channel. |
| 1231 |
* |
| 1232 |
* Results: |
| 1233 |
* Returns TCL_OK with the fd in handlePtr, or TCL_ERROR if |
| 1234 |
* there is no handle for the specified direction. |
| 1235 |
* |
| 1236 |
* Side effects: |
| 1237 |
* None. |
| 1238 |
* |
| 1239 |
*---------------------------------------------------------------------- |
| 1240 |
*/ |
| 1241 |
|
| 1242 |
static int |
| 1243 |
SerialGetHandleProc( |
| 1244 |
ClientData instanceData, /* The serial state. */ |
| 1245 |
int direction, /* TCL_READABLE or TCL_WRITABLE */ |
| 1246 |
ClientData *handlePtr) /* Where to store the handle. */ |
| 1247 |
{ |
| 1248 |
SerialInfo *infoPtr = (SerialInfo *) instanceData; |
| 1249 |
|
| 1250 |
*handlePtr = (ClientData) infoPtr->handle; |
| 1251 |
return TCL_OK; |
| 1252 |
} |
| 1253 |
|
| 1254 |
/* |
| 1255 |
*---------------------------------------------------------------------- |
| 1256 |
* |
| 1257 |
* SerialWriterThread -- |
| 1258 |
* |
| 1259 |
* This function runs in a separate thread and writes data |
| 1260 |
* onto a serial. |
| 1261 |
* |
| 1262 |
* Results: |
| 1263 |
* Always returns 0. |
| 1264 |
* |
| 1265 |
* Side effects: |
| 1266 |
* Signals the main thread when an output operation is completed. |
| 1267 |
* May cause the main thread to wake up by posting a message. |
| 1268 |
* |
| 1269 |
*---------------------------------------------------------------------- |
| 1270 |
*/ |
| 1271 |
|
| 1272 |
static DWORD WINAPI |
| 1273 |
SerialWriterThread(LPVOID arg) |
| 1274 |
{ |
| 1275 |
|
| 1276 |
SerialInfo *infoPtr = (SerialInfo *)arg; |
| 1277 |
DWORD bytesWritten, toWrite, waitResult; |
| 1278 |
char *buf; |
| 1279 |
OVERLAPPED myWrite; /* have an own OVERLAPPED in this thread */ |
| 1280 |
HANDLE wEvents[2]; |
| 1281 |
|
| 1282 |
/* |
| 1283 |
* The stop event takes precedence by being first in the list. |
| 1284 |
*/ |
| 1285 |
wEvents[0] = infoPtr->evStopWriter; |
| 1286 |
wEvents[1] = infoPtr->evStartWriter; |
| 1287 |
|
| 1288 |
for (;;) { |
| 1289 |
/* |
| 1290 |
* Wait for the main thread to signal before attempting to write. |
| 1291 |
*/ |
| 1292 |
|
| 1293 |
waitResult = WaitForMultipleObjects(2, wEvents, FALSE, INFINITE); |
| 1294 |
|
| 1295 |
if (waitResult != (WAIT_OBJECT_0 + 1)) { |
| 1296 |
/* |
| 1297 |
* The start event was not signaled. It might be the stop event |
| 1298 |
* or an error, so exit. |
| 1299 |
*/ |
| 1300 |
|
| 1301 |
break; |
| 1302 |
} |
| 1303 |
|
| 1304 |
buf = infoPtr->writeBuf; |
| 1305 |
toWrite = infoPtr->toWrite; |
| 1306 |
|
| 1307 |
myWrite.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL); |
| 1308 |
|
| 1309 |
/* |
| 1310 |
* Loop until all of the bytes are written or an error occurs. |
| 1311 |
*/ |
| 1312 |
|
| 1313 |
while (toWrite > 0) { |
| 1314 |
/* |
| 1315 |
* Check for pending writeError |
| 1316 |
* Ignore all write operations until the user has been notified |
| 1317 |
*/ |
| 1318 |
if (infoPtr->writeError) { |
| 1319 |
break; |
| 1320 |
} |
| 1321 |
if (blockingWrite(infoPtr, (LPVOID) buf, (DWORD) toWrite, |
| 1322 |
&bytesWritten, &myWrite) == FALSE) { |
| 1323 |
infoPtr->writeError = GetLastError(); |
| 1324 |
break; |
| 1325 |
} |
| 1326 |
if (bytesWritten != toWrite) { |
| 1327 |
/* Write timeout */ |
| 1328 |
infoPtr->writeError = ERROR_WRITE_FAULT; |
| 1329 |
break; |
| 1330 |
} |
| 1331 |
toWrite -= bytesWritten; |
| 1332 |
buf += bytesWritten; |
| 1333 |
} |
| 1334 |
|
| 1335 |
CloseHandle(myWrite.hEvent); |
| 1336 |
/* |
| 1337 |
* Signal the main thread by signalling the evWritable event and |
| 1338 |
* then waking up the notifier thread. |
| 1339 |
*/ |
| 1340 |
SetEvent(infoPtr->evWritable); |
| 1341 |
|
| 1342 |
/* |
| 1343 |
* Alert the foreground thread. Note that we need to treat this like |
| 1344 |
* a critical section so the foreground thread does not terminate |
| 1345 |
* this thread while we are holding a mutex in the notifier code. |
| 1346 |
*/ |
| 1347 |
|
| 1348 |
Tcl_MutexLock(&serialMutex); |
| 1349 |
Tcl_ThreadAlert(infoPtr->threadId); |
| 1350 |
Tcl_MutexUnlock(&serialMutex); |
| 1351 |
} |
| 1352 |
|
| 1353 |
return 0; |
| 1354 |
} |
| 1355 |
|
| 1356 |
|
| 1357 |
/* |
| 1358 |
*---------------------------------------------------------------------- |
| 1359 |
* |
| 1360 |
* TclWinSerialReopen -- |
| 1361 |
* |
| 1362 |
* Reopens the serial port with the OVERLAPPED FLAG set |
| 1363 |
* |
| 1364 |
* Results: |
| 1365 |
* Returns the new handle, or INVALID_HANDLE_VALUE |
| 1366 |
* Normally there shouldn't be any error, |
| 1367 |
* because the same channel has previously been succeesfully opened. |
| 1368 |
* |
| 1369 |
* Side effects: |
| 1370 |
* May close the original handle |
| 1371 |
* |
| 1372 |
*---------------------------------------------------------------------- |
| 1373 |
*/ |
| 1374 |
|
| 1375 |
HANDLE |
| 1376 |
TclWinSerialReopen(handle, name, access) |
| 1377 |
HANDLE handle; |
| 1378 |
CONST TCHAR *name; |
| 1379 |
DWORD access; |
| 1380 |
{ |
| 1381 |
ThreadSpecificData *tsdPtr; |
| 1382 |
|
| 1383 |
tsdPtr = SerialInit(); |
| 1384 |
|
| 1385 |
/* |
| 1386 |
* Multithreaded I/O needs the overlapped flag set |
| 1387 |
* otherwise ClearCommError blocks under Windows NT/2000 until serial |
| 1388 |
* output is finished |
| 1389 |
*/ |
| 1390 |
if (CloseHandle(handle) == FALSE) { |
| 1391 |
return INVALID_HANDLE_VALUE; |
| 1392 |
} |
| 1393 |
handle = (*tclWinProcs->createFileProc)(name, access, |
| 1394 |
0, 0, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0); |
| 1395 |
return handle; |
| 1396 |
} |
| 1397 |
/* |
| 1398 |
*---------------------------------------------------------------------- |
| 1399 |
* |
| 1400 |
* TclWinOpenSerialChannel -- |
| 1401 |
* |
| 1402 |
* Constructs a Serial port channel for the specified standard OS handle. |
| 1403 |
* This is a helper function to break up the construction of |
| 1404 |
* channels into File, Console, or Serial. |
| 1405 |
* |
| 1406 |
* Results: |
| 1407 |
* Returns the new channel, or NULL. |
| 1408 |
* |
| 1409 |
* Side effects: |
| 1410 |
* May open the channel |
| 1411 |
* |
| 1412 |
*---------------------------------------------------------------------- |
| 1413 |
*/ |
| 1414 |
|
| 1415 |
Tcl_Channel |
| 1416 |
TclWinOpenSerialChannel(handle, channelName, permissions) |
| 1417 |
HANDLE handle; |
| 1418 |
char *channelName; |
| 1419 |
int permissions; |
| 1420 |
{ |
| 1421 |
SerialInfo *infoPtr; |
| 1422 |
ThreadSpecificData *tsdPtr; |
| 1423 |
DWORD id; |
| 1424 |
|
| 1425 |
tsdPtr = SerialInit(); |
| 1426 |
|
| 1427 |
infoPtr = (SerialInfo *) ckalloc((unsigned) sizeof(SerialInfo)); |
| 1428 |
memset(infoPtr, 0, sizeof(SerialInfo)); |
| 1429 |
|
| 1430 |
infoPtr->validMask = permissions; |
| 1431 |
infoPtr->handle = handle; |
| 1432 |
|
| 1433 |
/* |
| 1434 |
* Use the pointer to keep the channel names unique, in case |
| 1435 |
* the handles are shared between multiple channels (stdin/stdout). |
| 1436 |
*/ |
| 1437 |
|
| 1438 |
wsprintfA(channelName, "file%lx", (int) infoPtr); |
| 1439 |
|
| 1440 |
infoPtr->channel = Tcl_CreateChannel(&serialChannelType, channelName, |
| 1441 |
(ClientData) infoPtr, permissions); |
| 1442 |
|
| 1443 |
infoPtr->readable = 0; |
| 1444 |
infoPtr->writable = 1; |
| 1445 |
infoPtr->toWrite = infoPtr->writeQueue = 0; |
| 1446 |
infoPtr->blockTime = SERIAL_DEFAULT_BLOCKTIME; |
| 1447 |
infoPtr->lastEventTime = 0; |
| 1448 |
infoPtr->lastError = infoPtr->error = 0; |
| 1449 |
infoPtr->threadId = Tcl_GetCurrentThread(); |
| 1450 |
infoPtr->sysBufRead = infoPtr->sysBufWrite = 4096; |
| 1451 |
|
| 1452 |
SetupComm(handle, infoPtr->sysBufRead, infoPtr->sysBufWrite); |
| 1453 |
PurgeComm(handle, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR |
| 1454 |
| PURGE_RXCLEAR); |
| 1455 |
|
| 1456 |
/* |
| 1457 |
* default is blocking |
| 1458 |
*/ |
| 1459 |
SetCommTimeouts(handle, &no_timeout); |
| 1460 |
|
| 1461 |
|
| 1462 |
if (permissions & TCL_READABLE) { |
| 1463 |
infoPtr->osRead.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL); |
| 1464 |
} |
| 1465 |
if (permissions & TCL_WRITABLE) { |
| 1466 |
/* |
| 1467 |
* Initially the channel is writable |
| 1468 |
* and the writeThread is idle. |
| 1469 |
*/ |
| 1470 |
infoPtr->osWrite.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL); |
| 1471 |
infoPtr->evWritable = CreateEvent(NULL, TRUE, TRUE, NULL); |
| 1472 |
infoPtr->evStartWriter = CreateEvent(NULL, FALSE, FALSE, NULL); |
| 1473 |
infoPtr->evStopWriter = CreateEvent(NULL, FALSE, FALSE, NULL); |
| 1474 |
InitializeCriticalSection(&infoPtr->csWrite); |
| 1475 |
infoPtr->writeThread = CreateThread(NULL, 256, SerialWriterThread, |
| 1476 |
infoPtr, 0, &id); |
| 1477 |
} |
| 1478 |
|
| 1479 |
/* |
| 1480 |
* Files have default translation of AUTO and ^Z eof char, which |
| 1481 |
* means that a ^Z will be accepted as EOF when reading. |
| 1482 |
*/ |
| 1483 |
|
| 1484 |
Tcl_SetChannelOption(NULL, infoPtr->channel, "-translation", "auto"); |
| 1485 |
Tcl_SetChannelOption(NULL, infoPtr->channel, "-eofchar", "\032 {}"); |
| 1486 |
|
| 1487 |
return infoPtr->channel; |
| 1488 |
} |
| 1489 |
|
| 1490 |
/* |
| 1491 |
*---------------------------------------------------------------------- |
| 1492 |
* |
| 1493 |
* SerialErrorStr -- |
| 1494 |
* |
| 1495 |
* Converts a Win32 serial error code to a list of readable errors |
| 1496 |
* |
| 1497 |
*---------------------------------------------------------------------- |
| 1498 |
*/ |
| 1499 |
static void |
| 1500 |
SerialErrorStr(error, dsPtr) |
| 1501 |
DWORD error; /* Win32 serial error code */ |
| 1502 |
Tcl_DString *dsPtr; /* Where to store string */ |
| 1503 |
{ |
| 1504 |
if( (error & CE_RXOVER) != 0) { |
| 1505 |
Tcl_DStringAppendElement(dsPtr, "RXOVER"); |
| 1506 |
} |
| 1507 |
if( (error & CE_OVERRUN) != 0) { |
| 1508 |
Tcl_DStringAppendElement(dsPtr, "OVERRUN"); |
| 1509 |
} |
| 1510 |
if( (error & CE_RXPARITY) != 0) { |
| 1511 |
Tcl_DStringAppendElement(dsPtr, "RXPARITY"); |
| 1512 |
} |
| 1513 |
if( (error & CE_FRAME) != 0) { |
| 1514 |
Tcl_DStringAppendElement(dsPtr, "FRAME"); |
| 1515 |
} |
| 1516 |
if( (error & CE_BREAK) != 0) { |
| 1517 |
Tcl_DStringAppendElement(dsPtr, "BREAK"); |
| 1518 |
} |
| 1519 |
if( (error & CE_TXFULL) != 0) { |
| 1520 |
Tcl_DStringAppendElement(dsPtr, "TXFULL"); |
| 1521 |
} |
| 1522 |
if( (error & CE_PTO) != 0) { /* PTO used to signal WRITE-TIMEOUT */ |
| 1523 |
Tcl_DStringAppendElement(dsPtr, "TIMEOUT"); |
| 1524 |
} |
| 1525 |
if( (error & ~((DWORD) (SERIAL_READ_ERRORS | SERIAL_WRITE_ERRORS))) != 0) { |
| 1526 |
char buf[TCL_INTEGER_SPACE + 1]; |
| 1527 |
wsprintfA(buf, "%d", error); |
| 1528 |
Tcl_DStringAppendElement(dsPtr, buf); |
| 1529 |
} |
| 1530 |
} |
| 1531 |
/* |
| 1532 |
*---------------------------------------------------------------------- |
| 1533 |
* |
| 1534 |
* SerialModemStatusStr -- |
| 1535 |
* |
| 1536 |
* Converts a Win32 modem status list of readable flags |
| 1537 |
* |
| 1538 |
*---------------------------------------------------------------------- |
| 1539 |
*/ |
| 1540 |
static void |
| 1541 |
SerialModemStatusStr(status, dsPtr) |
| 1542 |
DWORD status; /* Win32 modem status */ |
| 1543 |
Tcl_DString *dsPtr; /* Where to store string */ |
| 1544 |
{ |
| 1545 |
Tcl_DStringAppendElement(dsPtr, "CTS"); |
| 1546 |
Tcl_DStringAppendElement(dsPtr, (status & MS_CTS_ON) ? "1" : "0"); |
| 1547 |
Tcl_DStringAppendElement(dsPtr, "DSR"); |
| 1548 |
Tcl_DStringAppendElement(dsPtr, (status & MS_DSR_ON) ? "1" : "0"); |
| 1549 |
Tcl_DStringAppendElement(dsPtr, "RING"); |
| 1550 |
Tcl_DStringAppendElement(dsPtr, (status & MS_RING_ON) ? "1" : "0"); |
| 1551 |
Tcl_DStringAppendElement(dsPtr, "DCD"); |
| 1552 |
Tcl_DStringAppendElement(dsPtr, (status & MS_RLSD_ON) ? "1" : "0"); |
| 1553 |
} |
| 1554 |
|
| 1555 |
/* |
| 1556 |
*---------------------------------------------------------------------- |
| 1557 |
* |
| 1558 |
* SerialSetOptionProc -- |
| 1559 |
* |
| 1560 |
* Sets an option on a channel. |
| 1561 |
* |
| 1562 |
* Results: |
| 1563 |
* A standard Tcl result. Also sets the interp's result on error if |
| 1564 |
* interp is not NULL. |
| 1565 |
* |
| 1566 |
* Side effects: |
| 1567 |
* May modify an option on a device. |
| 1568 |
* |
| 1569 |
*---------------------------------------------------------------------- |
| 1570 |
*/ |
| 1571 |
static int |
| 1572 |
SerialSetOptionProc(instanceData, interp, optionName, value) |
| 1573 |
ClientData instanceData; /* File state. */ |
| 1574 |
Tcl_Interp *interp; /* For error reporting - can be NULL. */ |
| 1575 |
CONST char *optionName; /* Which option to set? */ |
| 1576 |
CONST char *value; /* New value for option. */ |
| 1577 |
{ |
| 1578 |
SerialInfo *infoPtr; |
| 1579 |
DCB dcb; |
| 1580 |
BOOL result, flag; |
| 1581 |
size_t len, vlen; |
| 1582 |
Tcl_DString ds; |
| 1583 |
CONST TCHAR *native; |
| 1584 |
int argc; |
| 1585 |
CONST char **argv; |
| 1586 |
|
| 1587 |
infoPtr = (SerialInfo *) instanceData; |
| 1588 |
|
| 1589 |
/* |
| 1590 |
* Parse options |
| 1591 |
*/ |
| 1592 |
len = strlen(optionName); |
| 1593 |
vlen = strlen(value); |
| 1594 |
|
| 1595 |
/* |
| 1596 |
* Option -mode baud,parity,databits,stopbits |
| 1597 |
*/ |
| 1598 |
if ((len > 2) && (strncmp(optionName, "-mode", len) == 0)) { |
| 1599 |
|
| 1600 |
if (! GetCommState(infoPtr->handle, &dcb)) { |
| 1601 |
if (interp) { |
| 1602 |
Tcl_AppendResult(interp, |
| 1603 |
"can't get comm state", (char *) NULL); |
| 1604 |
} |
| 1605 |
return TCL_ERROR; |
| 1606 |
} |
| 1607 |
native = Tcl_WinUtfToTChar(value, -1, &ds); |
| 1608 |
result = (*tclWinProcs->buildCommDCBProc)(native, &dcb); |
| 1609 |
Tcl_DStringFree(&ds); |
| 1610 |
|
| 1611 |
if (result == FALSE) { |
| 1612 |
if (interp) { |
| 1613 |
Tcl_AppendResult(interp, |
| 1614 |
"bad value for -mode: should be baud,parity,data,stop", |
| 1615 |
(char *) NULL); |
| 1616 |
} |
| 1617 |
return TCL_ERROR; |
| 1618 |
} |
| 1619 |
|
| 1620 |
/* Default settings for serial communications */ |
| 1621 |
dcb.fBinary = TRUE; |
| 1622 |
dcb.fErrorChar = FALSE; |
| 1623 |
dcb.fNull = FALSE; |
| 1624 |
dcb.fAbortOnError = FALSE; |
| 1625 |
|
| 1626 |
if (! SetCommState(infoPtr->handle, &dcb) ) { |
| 1627 |
if (interp) { |
| 1628 |
Tcl_AppendResult(interp, |
| 1629 |
"can't set comm state", (char *) NULL); |
| 1630 |
} |
| 1631 |
return TCL_ERROR; |
| 1632 |
} |
| 1633 |
return TCL_OK; |
| 1634 |
} |
| 1635 |
|
| 1636 |
/* |
| 1637 |
* Option -handshake none|xonxoff|rtscts|dtrdsr |
| 1638 |
*/ |
| 1639 |
if ((len > 1) && (strncmp(optionName, "-handshake", len) == 0)) { |
| 1640 |
|
| 1641 |
if (! GetCommState(infoPtr->handle, &dcb)) { |
| 1642 |
if (interp) { |
| 1643 |
Tcl_AppendResult(interp, |
| 1644 |
"can't get comm state", (char *) NULL); |
| 1645 |
} |
| 1646 |
return TCL_ERROR; |
| 1647 |
} |
| 1648 |
/* |
| 1649 |
* Reset all handshake options |
| 1650 |
* DTR and RTS are ON by default |
| 1651 |
*/ |
| 1652 |
dcb.fOutX = dcb.fInX = FALSE; |
| 1653 |
dcb.fOutxCtsFlow = dcb.fOutxDsrFlow = dcb.fDsrSensitivity = FALSE; |
| 1654 |
dcb.fDtrControl = DTR_CONTROL_ENABLE; |
| 1655 |
dcb.fRtsControl = RTS_CONTROL_ENABLE; |
| 1656 |
dcb.fTXContinueOnXoff = FALSE; |
| 1657 |
|
| 1658 |
/* |
| 1659 |
* Adjust the handshake limits. |
| 1660 |
* Yes, the XonXoff limits seem to influence even hardware handshake |
| 1661 |
*/ |
| 1662 |
dcb.XonLim = (WORD) (infoPtr->sysBufRead*1/2); |
| 1663 |
dcb.XoffLim = (WORD) (infoPtr->sysBufRead*1/4); |
| 1664 |
|
| 1665 |
if (strnicmp(value, "NONE", vlen) == 0) { |
| 1666 |
/* leave all handshake options disabled */ |
| 1667 |
} else if (strnicmp(value, "XONXOFF", vlen) == 0) { |
| 1668 |
dcb.fOutX = dcb.fInX = TRUE; |
| 1669 |
} else if (strnicmp(value, "RTSCTS", vlen) == 0) { |
| 1670 |
dcb.fOutxCtsFlow = TRUE; |
| 1671 |
dcb.fRtsControl = RTS_CONTROL_HANDSHAKE; |
| 1672 |
} else if (strnicmp(value, "DTRDSR", vlen) == 0) { |
| 1673 |
dcb.fOutxDsrFlow = TRUE; |
| 1674 |
dcb.fDtrControl = DTR_CONTROL_HANDSHAKE; |
| 1675 |
} else { |
| 1676 |
if (interp) { |
| 1677 |
Tcl_AppendResult(interp, "bad value for -handshake: ", |
| 1678 |
"must be one of xonxoff, rtscts, dtrdsr or none", |
| 1679 |
(char *) NULL); |
| 1680 |
return TCL_ERROR; |
| 1681 |
} |
| 1682 |
} |
| 1683 |
|
| 1684 |
if (! SetCommState(infoPtr->handle, &dcb)) { |
| 1685 |
if (interp) { |
| 1686 |
Tcl_AppendResult(interp, |
| 1687 |
"can't set comm state", (char *) NULL); |
| 1688 |
} |
| 1689 |
return TCL_ERROR; |
| 1690 |
} |
| 1691 |
return TCL_OK; |
| 1692 |
} |
| 1693 |
|
| 1694 |
/* |
| 1695 |
* Option -xchar {\x11 \x13} |
| 1696 |
*/ |
| 1697 |
if ((len > 1) && (strncmp(optionName, "-xchar", len) == 0)) { |
| 1698 |
|
| 1699 |
if (! GetCommState(infoPtr->handle, &dcb)) { |
| 1700 |
if (interp) { |
| 1701 |
Tcl_AppendResult(interp, |
| 1702 |
"can't get comm state", (char *) NULL); |
| 1703 |
} |
| 1704 |
return TCL_ERROR; |
| 1705 |
} |
| 1706 |
|
| 1707 |
if (Tcl_SplitList(interp, value, &argc, &argv) == TCL_ERROR) { |
| 1708 |
return TCL_ERROR; |
| 1709 |
} |
| 1710 |
if (argc == 2) { |
| 1711 |
dcb.XonChar = argv[0][0]; |
| 1712 |
dcb.XoffChar = argv[1][0]; |
| 1713 |
} else { |
| 1714 |
if (interp) { |
| 1715 |
Tcl_AppendResult(interp, |
| 1716 |
"bad value for -xchar: should be a list of two elements", |
| 1717 |
(char *) NULL); |
| 1718 |
} |
| 1719 |
return TCL_ERROR; |
| 1720 |
} |
| 1721 |
|
| 1722 |
if (! SetCommState(infoPtr->handle, &dcb)) { |
| 1723 |
if (interp) { |
| 1724 |
Tcl_AppendResult(interp, |
| 1725 |
"can't set comm state", (char *) NULL); |
| 1726 |
} |
| 1727 |
return TCL_ERROR; |
| 1728 |
} |
| 1729 |
return TCL_OK; |
| 1730 |
} |
| 1731 |
|
| 1732 |
/* |
| 1733 |
* Option -ttycontrol {DTR 1 RTS 0 BREAK 0} |
| 1734 |
*/ |
| 1735 |
if ((len > 4) && (strncmp(optionName, "-ttycontrol", len) == 0)) { |
| 1736 |
|
| 1737 |
if (Tcl_SplitList(interp, value, &argc, &argv) == TCL_ERROR) { |
| 1738 |
return TCL_ERROR; |
| 1739 |
} |
| 1740 |
if ((argc % 2) == 1) { |
| 1741 |
if (interp) { |
| 1742 |
Tcl_AppendResult(interp, |
| 1743 |
"bad value for -ttycontrol: should be a list of signal,value pairs", |
| 1744 |
(char *) NULL); |
| 1745 |
} |
| 1746 |
return TCL_ERROR; |
| 1747 |
} |
| 1748 |
while (argc > 1) { |
| 1749 |
if (Tcl_GetBoolean(interp, argv[1], &flag) == TCL_ERROR) { |
| 1750 |
return TCL_ERROR; |
| 1751 |
} |
| 1752 |
if (strnicmp(argv[0], "DTR", strlen(argv[0])) == 0) { |
| 1753 |
if (! EscapeCommFunction(infoPtr->handle, flag ? |
| 1754 |
(DWORD) SETDTR : (DWORD) CLRDTR)) { |
| 1755 |
if (interp) { |
| 1756 |
Tcl_AppendResult(interp, |
| 1757 |
"can't set DTR signal", (char *) NULL); |
| 1758 |
} |
| 1759 |
return TCL_ERROR; |
| 1760 |
} |
| 1761 |
} else if (strnicmp(argv[0], "RTS", strlen(argv[0])) == 0) { |
| 1762 |
if (! EscapeCommFunction(infoPtr->handle, flag ? |
| 1763 |
(DWORD) SETRTS : (DWORD) CLRRTS)) { |
| 1764 |
if (interp) { |
| 1765 |
Tcl_AppendResult(interp, |
| 1766 |
"can't set RTS signal", (char *) NULL); |
| 1767 |
} |
| 1768 |
return TCL_ERROR; |
| 1769 |
} |
| 1770 |
} else if (strnicmp(argv[0], "BREAK", strlen(argv[0])) == 0) { |
| 1771 |
if (! EscapeCommFunction(infoPtr->handle, flag ? |
| 1772 |
(DWORD) SETBREAK : (DWORD) CLRBREAK)) { |
| 1773 |
if (interp) { |
| 1774 |
Tcl_AppendResult(interp, |
| 1775 |
"can't set BREAK signal", (char *) NULL); |
| 1776 |
} |
| 1777 |
return TCL_ERROR; |
| 1778 |
} |
| 1779 |
} else { |
| 1780 |
if (interp) { |
| 1781 |
Tcl_AppendResult(interp, |
| 1782 |
"bad signal for -ttycontrol: must be DTR, RTS or BREAK", |
| 1783 |
(char *) NULL); |
| 1784 |
} |
| 1785 |
return TCL_ERROR; |
| 1786 |
} |
| 1787 |
argc -= 2, argv += 2; |
| 1788 |
} /* while (argc > 1) */ |
| 1789 |
|
| 1790 |
return TCL_OK; |
| 1791 |
} |
| 1792 |
|
| 1793 |
/* |
| 1794 |
* Option -sysbuffer {read_size write_size} |
| 1795 |
* Option -sysbuffer read_size |
| 1796 |
*/ |
| 1797 |
if ((len > 1) && (strncmp(optionName, "-sysbuffer", len) == 0)) { |
| 1798 |
|
| 1799 |
/* |
| 1800 |
* -sysbuffer 4096 or -sysbuffer {64536 4096} |
| 1801 |
*/ |
| 1802 |
size_t inSize = (size_t) -1, outSize = (size_t) -1; |
| 1803 |
|
| 1804 |
if (Tcl_SplitList(interp, value, &argc, &argv) == TCL_ERROR) { |
| 1805 |
return TCL_ERROR; |
| 1806 |
} |
| 1807 |
if (argc == 1) { |
| 1808 |
inSize = atoi(argv[0]); |
| 1809 |
outSize = infoPtr->sysBufWrite; |
| 1810 |
} else if (argc == 2) { |
| 1811 |
inSize = atoi(argv[0]); |
| 1812 |
outSize = atoi(argv[1]); |
| 1813 |
} |
| 1814 |
if ( (inSize <= 0) || (outSize <= 0) ) { |
| 1815 |
if (interp) { |
| 1816 |
Tcl_AppendResult(interp, |
| 1817 |
"bad value for -sysbuffer: should be a list of one or two integers > 0", |
| 1818 |
(char *) NULL); |
| 1819 |
} |
| 1820 |
return TCL_ERROR; |
| 1821 |
} |
| 1822 |
if (! SetupComm(infoPtr->handle, inSize, outSize)) { |
| 1823 |
if (interp) { |
| 1824 |
Tcl_AppendResult(interp, |
| 1825 |
"can't setup comm buffers", (char *) NULL); |
| 1826 |
} |
| 1827 |
return TCL_ERROR; |
| 1828 |
} |
| 1829 |
infoPtr->sysBufRead = inSize; |
| 1830 |
infoPtr->sysBufWrite = outSize; |
| 1831 |
|
| 1832 |
/* |
| 1833 |
* Adjust the handshake limits. |
| 1834 |
* Yes, the XonXoff limits seem to influence even hardware handshake |
| 1835 |
*/ |
| 1836 |
if (! GetCommState(infoPtr->handle, &dcb)) { |
| 1837 |
if (interp) { |
| 1838 |
Tcl_AppendResult(interp, |
| 1839 |
"can't get comm state", (char *) NULL); |
| 1840 |
} |
| 1841 |
return TCL_ERROR; |
| 1842 |
} |
| 1843 |
dcb.XonLim = (WORD) (infoPtr->sysBufRead*1/2); |
| 1844 |
dcb.XoffLim = (WORD) (infoPtr->sysBufRead*1/4); |
| 1845 |
if (! SetCommState(infoPtr->handle, &dcb)) { |
| 1846 |
if (interp) { |
| 1847 |
Tcl_AppendResult(interp, |
| 1848 |
"can't set comm state", (char *) NULL); |
| 1849 |
} |
| 1850 |
return TCL_ERROR; |
| 1851 |
} |
| 1852 |
return TCL_OK; |
| 1853 |
} |
| 1854 |
|
| 1855 |
/* |
| 1856 |
* Option -pollinterval msec |
| 1857 |
*/ |
| 1858 |
if ((len > 1) && (strncmp(optionName, "-pollinterval", len) == 0)) { |
| 1859 |
|
| 1860 |
if ( Tcl_GetInt(interp, value, &(infoPtr->blockTime)) != TCL_OK ) { |
| 1861 |
return TCL_ERROR; |
| 1862 |
} |
| 1863 |
return TCL_OK; |
| 1864 |
} |
| 1865 |
|
| 1866 |
/* |
| 1867 |
* Option -timeout msec |
| 1868 |
*/ |
| 1869 |
if ((len > 2) && (strncmp(optionName, "-timeout", len) == 0)) { |
| 1870 |
int msec; |
| 1871 |
COMMTIMEOUTS tout = {0,0,0,0,0}; |
| 1872 |
|
| 1873 |
if ( Tcl_GetInt(interp, value, &msec) != TCL_OK ) { |
| 1874 |
return TCL_ERROR; |
| 1875 |
} |
| 1876 |
tout.ReadTotalTimeoutConstant = msec; |
| 1877 |
if (! SetCommTimeouts(infoPtr->handle, &tout)) { |
| 1878 |
if (interp) { |
| 1879 |
Tcl_AppendResult(interp, |
| 1880 |
"can't set comm timeouts", (char *) NULL); |
| 1881 |
} |
| 1882 |
return TCL_ERROR; |
| 1883 |
} |
| 1884 |
|
| 1885 |
return TCL_OK; |
| 1886 |
} |
| 1887 |
|
| 1888 |
return Tcl_BadChannelOption(interp, optionName, |
| 1889 |
"mode handshake pollinterval sysbuffer timeout ttycontrol xchar"); |
| 1890 |
} |
| 1891 |
|
| 1892 |
/* |
| 1893 |
*---------------------------------------------------------------------- |
| 1894 |
* |
| 1895 |
* SerialGetOptionProc -- |
| 1896 |
* |
| 1897 |
* Gets a mode associated with an IO channel. If the optionName arg |
| 1898 |
* is non NULL, retrieves the value of that option. If the optionName |
| 1899 |
* arg is NULL, retrieves a list of alternating option names and |
| 1900 |
* values for the given channel. |
| 1901 |
* |
| 1902 |
* Results: |
| 1903 |
* A standard Tcl result. Also sets the supplied DString to the |
| 1904 |
* string value of the option(s) returned. |
| 1905 |
* |
| 1906 |
* Side effects: |
| 1907 |
* The string returned by this function is in static storage and |
| 1908 |
* may be reused at any time subsequent to the call. |
| 1909 |
* |
| 1910 |
*---------------------------------------------------------------------- |
| 1911 |
*/ |
| 1912 |
static int |
| 1913 |
SerialGetOptionProc(instanceData, interp, optionName, dsPtr) |
| 1914 |
ClientData instanceData; /* File state. */ |
| 1915 |
Tcl_Interp *interp; /* For error reporting - can be NULL. */ |
| 1916 |
CONST char *optionName; /* Option to get. */ |
| 1917 |
Tcl_DString *dsPtr; /* Where to store value(s). */ |
| 1918 |
{ |
| 1919 |
SerialInfo *infoPtr; |
| 1920 |
DCB dcb; |
| 1921 |
size_t len; |
| 1922 |
int valid = 0; /* flag if valid option parsed */ |
| 1923 |
|
| 1924 |
infoPtr = (SerialInfo *) instanceData; |
| 1925 |
|
| 1926 |
if (optionName == NULL) { |
| 1927 |
len = 0; |
| 1928 |
} else { |
| 1929 |
len = strlen(optionName); |
| 1930 |
} |
| 1931 |
|
| 1932 |
/* |
| 1933 |
* get option -mode |
| 1934 |
*/ |
| 1935 |
|
| 1936 |
if (len == 0) { |
| 1937 |
Tcl_DStringAppendElement(dsPtr, "-mode"); |
| 1938 |
} |
| 1939 |
if ((len == 0) || |
| 1940 |
((len > 2) && (strncmp(optionName, "-mode", len) == 0))) { |
| 1941 |
|
| 1942 |
char parity; |
| 1943 |
char *stop; |
| 1944 |
char buf[2 * TCL_INTEGER_SPACE + 16]; |
| 1945 |
|
| 1946 |
if (! GetCommState(infoPtr->handle, &dcb)) { |
| 1947 |
if (interp) { |
| 1948 |
Tcl_AppendResult(interp, |
| 1949 |
"can't get comm state", (char *) NULL); |
| 1950 |
} |
| 1951 |
return TCL_ERROR; |
| 1952 |
} |
| 1953 |
|
| 1954 |
valid = 1; |
| 1955 |
parity = 'n'; |
| 1956 |
if (dcb.Parity <= 4) { |
| 1957 |
parity = "noems"[dcb.Parity]; |
| 1958 |
} |
| 1959 |
stop = (dcb.StopBits == ONESTOPBIT) ? "1" : |
| 1960 |
(dcb.StopBits == ONE5STOPBITS) ? "1.5" : "2"; |
| 1961 |
|
| 1962 |
wsprintfA(buf, "%d,%c,%d,%s", dcb.BaudRate, parity, |
| 1963 |
dcb.ByteSize, stop); |
| 1964 |
Tcl_DStringAppendElement(dsPtr, buf); |
| 1965 |
} |
| 1966 |
|
| 1967 |
/* |
| 1968 |
* get option -pollinterval |
| 1969 |
*/ |
| 1970 |
|
| 1971 |
if (len == 0) { |
| 1972 |
Tcl_DStringAppendElement(dsPtr, "-pollinterval"); |
| 1973 |
} |
| 1974 |
if ((len == 0) || |
| 1975 |
((len > 1) && (strncmp(optionName, "-pollinterval", len) == 0))) { |
| 1976 |
char buf[TCL_INTEGER_SPACE + 1]; |
| 1977 |
|
| 1978 |
valid = 1; |
| 1979 |
wsprintfA(buf, "%d", infoPtr->blockTime); |
| 1980 |
Tcl_DStringAppendElement(dsPtr, buf); |
| 1981 |
} |
| 1982 |
|
| 1983 |
/* |
| 1984 |
* get option -sysbuffer |
| 1985 |
*/ |
| 1986 |
|
| 1987 |
if (len == 0) { |
| 1988 |
Tcl_DStringAppendElement(dsPtr, "-sysbuffer"); |
| 1989 |
Tcl_DStringStartSublist(dsPtr); |
| 1990 |
} |
| 1991 |
if ((len == 0) || |
| 1992 |
((len > 1) && (strncmp(optionName, "-sysbuffer", len) == 0))) { |
| 1993 |
|
| 1994 |
char buf[TCL_INTEGER_SPACE + 1]; |
| 1995 |
valid = 1; |
| 1996 |
|
| 1997 |
wsprintfA(buf, "%d", infoPtr->sysBufRead); |
| 1998 |
Tcl_DStringAppendElement(dsPtr, buf); |
| 1999 |
wsprintfA(buf, "%d", infoPtr->sysBufWrite); |
| 2000 |
Tcl_DStringAppendElement(dsPtr, buf); |
| 2001 |
} |
| 2002 |
if (len == 0) { |
| 2003 |
Tcl_DStringEndSublist(dsPtr); |
| 2004 |
} |
| 2005 |
|
| 2006 |
/* |
| 2007 |
* get option -xchar |
| 2008 |
*/ |
| 2009 |
|
| 2010 |
if (len == 0) { |
| 2011 |
Tcl_DStringAppendElement(dsPtr, "-xchar"); |
| 2012 |
Tcl_DStringStartSublist(dsPtr); |
| 2013 |
} |
| 2014 |
if ((len == 0) || |
| 2015 |
((len > 1) && (strncmp(optionName, "-xchar", len) == 0))) { |
| 2016 |
|
| 2017 |
char buf[4]; |
| 2018 |
valid = 1; |
| 2019 |
|
| 2020 |
if (! GetCommState(infoPtr->handle, &dcb)) { |
| 2021 |
if (interp) { |
| 2022 |
Tcl_AppendResult(interp, |
| 2023 |
"can't get comm state", (char *) NULL); |
| 2024 |
} |
| 2025 |
return TCL_ERROR; |
| 2026 |
} |
| 2027 |
sprintf(buf, "%c", dcb.XonChar); |
| 2028 |
Tcl_DStringAppendElement(dsPtr, buf); |
| 2029 |
sprintf(buf, "%c", dcb.XoffChar); |
| 2030 |
Tcl_DStringAppendElement(dsPtr, buf); |
| 2031 |
} |
| 2032 |
if (len == 0) { |
| 2033 |
Tcl_DStringEndSublist(dsPtr); |
| 2034 |
} |
| 2035 |
|
| 2036 |
/* |
| 2037 |
* get option -lasterror |
| 2038 |
* option is readonly and returned by [fconfigure chan -lasterror] |
| 2039 |
* but not returned by unnamed [fconfigure chan] |
| 2040 |
*/ |
| 2041 |
|
| 2042 |
if ( (len > 1) && (strncmp(optionName, "-lasterror", len) == 0) ) { |
| 2043 |
valid = 1; |
| 2044 |
SerialErrorStr(infoPtr->lastError, dsPtr); |
| 2045 |
} |
| 2046 |
|
| 2047 |
/* |
| 2048 |
* get option -queue |
| 2049 |
* option is readonly and returned by [fconfigure chan -queue] |
| 2050 |
*/ |
| 2051 |
|
| 2052 |
if ((len > 1) && (strncmp(optionName, "-queue", len) == 0)) { |
| 2053 |
char buf[TCL_INTEGER_SPACE + 1]; |
| 2054 |
COMSTAT cStat; |
| 2055 |
DWORD error; |
| 2056 |
int inBuffered, outBuffered, count; |
| 2057 |
|
| 2058 |
valid = 1; |
| 2059 |
|
| 2060 |
/* |
| 2061 |
* Query the pending data in Tcl's internal queues |
| 2062 |
*/ |
| 2063 |
inBuffered = Tcl_InputBuffered(infoPtr->channel); |
| 2064 |
outBuffered = Tcl_OutputBuffered(infoPtr->channel); |
| 2065 |
|
| 2066 |
/* |
| 2067 |
* Query the number of bytes in our output queue: |
| 2068 |
* 1. The bytes pending in the output thread |
| 2069 |
* 2. The bytes in the system drivers buffer |
| 2070 |
* The writer thread should not interfere this action. |
| 2071 |
*/ |
| 2072 |
EnterCriticalSection(&infoPtr->csWrite); |
| 2073 |
ClearCommError( infoPtr->handle, &error, &cStat ); |
| 2074 |
count = (int)cStat.cbOutQue + infoPtr->writeQueue; |
| 2075 |
LeaveCriticalSection(&infoPtr->csWrite); |
| 2076 |
|
| 2077 |
wsprintfA(buf, "%d", inBuffered + cStat.cbInQue); |
| 2078 |
Tcl_DStringAppendElement(dsPtr, buf); |
| 2079 |
wsprintfA(buf, "%d", outBuffered + count); |
| 2080 |
Tcl_DStringAppendElement(dsPtr, buf); |
| 2081 |
} |
| 2082 |
|
| 2083 |
/* |
| 2084 |
* get option -ttystatus |
| 2085 |
* option is readonly and returned by [fconfigure chan -ttystatus] |
| 2086 |
* but not returned by unnamed [fconfigure chan] |
| 2087 |
*/ |
| 2088 |
if ( (len > 4) && (strncmp(optionName, "-ttystatus", len) == 0) ) { |
| 2089 |
|
| 2090 |
DWORD status; |
| 2091 |
|
| 2092 |
if (! GetCommModemStatus(infoPtr->handle, &status)) { |
| 2093 |
if (interp) { |
| 2094 |
Tcl_AppendResult(interp, |
| 2095 |
"can't get tty status", (char *) NULL); |
| 2096 |
} |
| 2097 |
return TCL_ERROR; |
| 2098 |
} |
| 2099 |
valid = 1; |
| 2100 |
SerialModemStatusStr(status, dsPtr); |
| 2101 |
} |
| 2102 |
|
| 2103 |
if (valid) { |
| 2104 |
return TCL_OK; |
| 2105 |
} else { |
| 2106 |
return Tcl_BadChannelOption(interp, optionName, |
| 2107 |
"mode pollinterval lasterror queue sysbuffer ttystatus xchar"); |
| 2108 |
} |
| 2109 |
} |