1 /* Serial interface for local (hardwired) serial ports on Windows systems
3 Copyright (C) 2006-2015 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
30 #include <sys/types.h>
34 void _initialize_ser_windows (void);
36 struct ser_windows_state
44 /* CancelIo is not available for Windows 95 OS, so we need to use
45 LoadLibrary/GetProcAddress to avoid a startup failure. */
46 #define CancelIo dyn_CancelIo
47 static BOOL
WINAPI (*CancelIo
) (HANDLE
);
49 /* Open up a real live device for serial I/O. */
52 ser_windows_open (struct serial
*scb
, const char *name
)
55 struct ser_windows_state
*state
;
56 COMMTIMEOUTS timeouts
;
58 h
= CreateFile (name
, GENERIC_READ
| GENERIC_WRITE
, 0, NULL
,
59 OPEN_EXISTING
, FILE_FLAG_OVERLAPPED
, NULL
);
60 if (h
== INVALID_HANDLE_VALUE
)
66 scb
->fd
= _open_osfhandle ((intptr_t) h
, O_RDWR
);
73 if (!SetCommMask (h
, EV_RXCHAR
))
79 timeouts
.ReadIntervalTimeout
= MAXDWORD
;
80 timeouts
.ReadTotalTimeoutConstant
= 0;
81 timeouts
.ReadTotalTimeoutMultiplier
= 0;
82 timeouts
.WriteTotalTimeoutConstant
= 0;
83 timeouts
.WriteTotalTimeoutMultiplier
= 0;
84 if (!SetCommTimeouts (h
, &timeouts
))
90 state
= XCNEW (struct ser_windows_state
);
93 /* Create a manual reset event to watch the input buffer. */
94 state
->ov
.hEvent
= CreateEvent (0, TRUE
, FALSE
, 0);
96 /* Create a (currently unused) handle to record exceptions. */
97 state
->except_event
= CreateEvent (0, TRUE
, FALSE
, 0);
102 /* Wait for the output to drain away, as opposed to flushing (discarding)
106 ser_windows_drain_output (struct serial
*scb
)
108 HANDLE h
= (HANDLE
) _get_osfhandle (scb
->fd
);
110 return (FlushFileBuffers (h
) != 0) ? 0 : -1;
114 ser_windows_flush_output (struct serial
*scb
)
116 HANDLE h
= (HANDLE
) _get_osfhandle (scb
->fd
);
118 return (PurgeComm (h
, PURGE_TXCLEAR
) != 0) ? 0 : -1;
122 ser_windows_flush_input (struct serial
*scb
)
124 HANDLE h
= (HANDLE
) _get_osfhandle (scb
->fd
);
126 return (PurgeComm (h
, PURGE_RXCLEAR
) != 0) ? 0 : -1;
130 ser_windows_send_break (struct serial
*scb
)
132 HANDLE h
= (HANDLE
) _get_osfhandle (scb
->fd
);
134 if (SetCommBreak (h
) == 0)
137 /* Delay for 250 milliseconds. */
140 if (ClearCommBreak (h
))
147 ser_windows_raw (struct serial
*scb
)
149 HANDLE h
= (HANDLE
) _get_osfhandle (scb
->fd
);
152 if (GetCommState (h
, &state
) == 0)
155 state
.fOutxCtsFlow
= FALSE
;
156 state
.fOutxDsrFlow
= FALSE
;
157 state
.fDtrControl
= DTR_CONTROL_ENABLE
;
158 state
.fDsrSensitivity
= FALSE
;
162 state
.fAbortOnError
= FALSE
;
165 scb
->current_timeout
= 0;
167 if (SetCommState (h
, &state
) == 0)
168 warning (_("SetCommState failed"));
172 ser_windows_setstopbits (struct serial
*scb
, int num
)
174 HANDLE h
= (HANDLE
) _get_osfhandle (scb
->fd
);
177 if (GetCommState (h
, &state
) == 0)
182 case SERIAL_1_STOPBITS
:
183 state
.StopBits
= ONESTOPBIT
;
185 case SERIAL_1_AND_A_HALF_STOPBITS
:
186 state
.StopBits
= ONE5STOPBITS
;
188 case SERIAL_2_STOPBITS
:
189 state
.StopBits
= TWOSTOPBITS
;
195 return (SetCommState (h
, &state
) != 0) ? 0 : -1;
198 /* Implement the "setparity" serial_ops callback. */
201 ser_windows_setparity (struct serial
*scb
, int parity
)
203 HANDLE h
= (HANDLE
) _get_osfhandle (scb
->fd
);
206 if (GetCommState (h
, &state
) == 0)
212 state
.Parity
= NOPARITY
;
213 state
.fParity
= FALSE
;
216 state
.Parity
= ODDPARITY
;
217 state
.fParity
= TRUE
;
220 state
.Parity
= EVENPARITY
;
221 state
.fParity
= TRUE
;
224 internal_warning (__FILE__
, __LINE__
,
225 "Incorrect parity value: %d", parity
);
229 return (SetCommState (h
, &state
) != 0) ? 0 : -1;
233 ser_windows_setbaudrate (struct serial
*scb
, int rate
)
235 HANDLE h
= (HANDLE
) _get_osfhandle (scb
->fd
);
238 if (GetCommState (h
, &state
) == 0)
241 state
.BaudRate
= rate
;
243 return (SetCommState (h
, &state
) != 0) ? 0 : -1;
247 ser_windows_close (struct serial
*scb
)
249 struct ser_windows_state
*state
;
251 /* Stop any pending selects. On Windows 95 OS, CancelIo function does
252 not exist. In that case, it can be replaced by a call to CloseHandle,
253 but this is not necessary here as we do close the Windows handle
254 by calling close (scb->fd) below. */
256 CancelIo ((HANDLE
) _get_osfhandle (scb
->fd
));
258 CloseHandle (state
->ov
.hEvent
);
259 CloseHandle (state
->except_event
);
271 ser_windows_wait_handle (struct serial
*scb
, HANDLE
*read
, HANDLE
*except
)
273 struct ser_windows_state
*state
;
276 HANDLE h
= (HANDLE
) _get_osfhandle (scb
->fd
);
280 *except
= state
->except_event
;
281 *read
= state
->ov
.hEvent
;
283 if (state
->in_progress
)
286 /* Reset the mask - we are only interested in any characters which
287 arrive after this point, not characters which might have arrived
288 and already been read. */
290 /* This really, really shouldn't be necessary - just the second one.
291 But otherwise an internal flag for EV_RXCHAR does not get
292 cleared, and we get a duplicated event, if the last batch
293 of characters included at least two arriving close together. */
294 if (!SetCommMask (h
, 0))
295 warning (_("ser_windows_wait_handle: reseting mask failed"));
297 if (!SetCommMask (h
, EV_RXCHAR
))
298 warning (_("ser_windows_wait_handle: reseting mask failed (2)"));
300 /* There's a potential race condition here; we must check cbInQue
301 and not wait if that's nonzero. */
303 ClearCommError (h
, &errors
, &status
);
304 if (status
.cbInQue
> 0)
306 SetEvent (state
->ov
.hEvent
);
310 state
->in_progress
= 1;
311 ResetEvent (state
->ov
.hEvent
);
312 state
->lastCommMask
= -2;
313 if (WaitCommEvent (h
, &state
->lastCommMask
, &state
->ov
))
315 gdb_assert (state
->lastCommMask
& EV_RXCHAR
);
316 SetEvent (state
->ov
.hEvent
);
319 gdb_assert (GetLastError () == ERROR_IO_PENDING
);
323 ser_windows_read_prim (struct serial
*scb
, size_t count
)
325 struct ser_windows_state
*state
;
327 DWORD bytes_read
, bytes_read_tmp
;
332 if (state
->in_progress
)
334 WaitForSingleObject (state
->ov
.hEvent
, INFINITE
);
335 state
->in_progress
= 0;
336 ResetEvent (state
->ov
.hEvent
);
339 memset (&ov
, 0, sizeof (OVERLAPPED
));
340 ov
.hEvent
= CreateEvent (0, FALSE
, FALSE
, 0);
341 h
= (HANDLE
) _get_osfhandle (scb
->fd
);
343 if (!ReadFile (h
, scb
->buf
, /* count */ 1, &bytes_read
, &ov
))
345 if (GetLastError () != ERROR_IO_PENDING
346 || !GetOverlappedResult (h
, &ov
, &bytes_read
, TRUE
))
350 CloseHandle (ov
.hEvent
);
355 ser_windows_write_prim (struct serial
*scb
, const void *buf
, size_t len
)
357 struct ser_windows_state
*state
;
362 memset (&ov
, 0, sizeof (OVERLAPPED
));
363 ov
.hEvent
= CreateEvent (0, FALSE
, FALSE
, 0);
364 h
= (HANDLE
) _get_osfhandle (scb
->fd
);
365 if (!WriteFile (h
, buf
, len
, &bytes_written
, &ov
))
367 if (GetLastError () != ERROR_IO_PENDING
368 || !GetOverlappedResult (h
, &ov
, &bytes_written
, TRUE
))
372 CloseHandle (ov
.hEvent
);
373 return bytes_written
;
376 /* On Windows, gdb_select is implemented using WaitForMulpleObjects.
377 A "select thread" is created for each file descriptor. These
378 threads looks for activity on the corresponding descriptor, using
379 whatever techniques are appropriate for the descriptor type. When
380 that activity occurs, the thread signals an appropriate event,
381 which wakes up WaitForMultipleObjects.
383 Each select thread is in one of two states: stopped or started.
384 Select threads begin in the stopped state. When gdb_select is
385 called, threads corresponding to the descriptors of interest are
386 started by calling a wait_handle function. Each thread that
387 notices activity signals the appropriate event and then reenters
388 the stopped state. Before gdb_select returns it calls the
389 wait_handle_done functions, which return the threads to the stopped
392 enum select_thread_state
{
397 struct ser_console_state
399 /* Signaled by the select thread to indicate that data is available
400 on the file descriptor. */
402 /* Signaled by the select thread to indicate that an exception has
403 occurred on the file descriptor. */
405 /* Signaled by the select thread to indicate that it has entered the
406 started state. HAVE_STARTED and HAVE_STOPPED are never signaled
409 /* Signaled by the select thread to indicate that it has stopped,
410 either because data is available (and READ_EVENT is signaled),
411 because an exception has occurred (and EXCEPT_EVENT is signaled),
412 or because STOP_SELECT was signaled. */
415 /* Signaled by the main program to tell the select thread to enter
416 the started state. */
418 /* Signaled by the main program to tell the select thread to enter
419 the stopped state. */
421 /* Signaled by the main program to tell the select thread to
425 /* The handle for the select thread. */
427 /* The state of the select thread. This field is only accessed in
428 the main program, never by the select thread itself. */
429 enum select_thread_state thread_state
;
432 /* Called by a select thread to enter the stopped state. This
433 function does not return until the thread has re-entered the
436 select_thread_wait (struct ser_console_state
*state
)
438 HANDLE wait_events
[2];
440 /* There are two things that can wake us up: a request that we enter
441 the started state, or that we exit this thread. */
442 wait_events
[0] = state
->start_select
;
443 wait_events
[1] = state
->exit_select
;
444 if (WaitForMultipleObjects (2, wait_events
, FALSE
, INFINITE
)
446 /* Either the EXIT_SELECT event was signaled (requesting that the
447 thread exit) or an error has occurred. In either case, we exit
451 /* We are now in the started state. */
452 SetEvent (state
->have_started
);
455 typedef DWORD
WINAPI (*thread_fn_type
)(void *);
457 /* Create a new select thread for SCB executing THREAD_FN. The STATE
458 will be filled in by this function before return. */
460 create_select_thread (thread_fn_type thread_fn
,
462 struct ser_console_state
*state
)
466 /* Create all of the events. These are all auto-reset events. */
467 state
->read_event
= CreateEvent (NULL
, FALSE
, FALSE
, NULL
);
468 state
->except_event
= CreateEvent (NULL
, FALSE
, FALSE
, NULL
);
469 state
->have_started
= CreateEvent (NULL
, FALSE
, FALSE
, NULL
);
470 state
->have_stopped
= CreateEvent (NULL
, FALSE
, FALSE
, NULL
);
471 state
->start_select
= CreateEvent (NULL
, FALSE
, FALSE
, NULL
);
472 state
->stop_select
= CreateEvent (NULL
, FALSE
, FALSE
, NULL
);
473 state
->exit_select
= CreateEvent (NULL
, FALSE
, FALSE
, NULL
);
475 state
->thread
= CreateThread (NULL
, 0, thread_fn
, scb
, 0, &threadId
);
476 /* The thread begins in the stopped state. */
477 state
->thread_state
= STS_STOPPED
;
480 /* Destroy the select thread indicated by STATE. */
482 destroy_select_thread (struct ser_console_state
*state
)
484 /* Ask the thread to exit. */
485 SetEvent (state
->exit_select
);
486 /* Wait until it does. */
487 WaitForSingleObject (state
->thread
, INFINITE
);
489 /* Destroy the events. */
490 CloseHandle (state
->read_event
);
491 CloseHandle (state
->except_event
);
492 CloseHandle (state
->have_started
);
493 CloseHandle (state
->have_stopped
);
494 CloseHandle (state
->start_select
);
495 CloseHandle (state
->stop_select
);
496 CloseHandle (state
->exit_select
);
499 /* Called by gdb_select to start the select thread indicated by STATE.
500 This function does not return until the thread has started. */
502 start_select_thread (struct ser_console_state
*state
)
504 /* Ask the thread to start. */
505 SetEvent (state
->start_select
);
506 /* Wait until it does. */
507 WaitForSingleObject (state
->have_started
, INFINITE
);
508 /* The thread is now started. */
509 state
->thread_state
= STS_STARTED
;
512 /* Called by gdb_select to stop the select thread indicated by STATE.
513 This function does not return until the thread has stopped. */
515 stop_select_thread (struct ser_console_state
*state
)
517 /* If the thread is already in the stopped state, we have nothing to
518 do. Some of the wait_handle functions avoid calling
519 start_select_thread if they notice activity on the relevant file
520 descriptors. The wait_handle_done functions still call
521 stop_select_thread -- but it is already stopped. */
522 if (state
->thread_state
!= STS_STARTED
)
524 /* Ask the thread to stop. */
525 SetEvent (state
->stop_select
);
526 /* Wait until it does. */
527 WaitForSingleObject (state
->have_stopped
, INFINITE
);
528 /* The thread is now stopped. */
529 state
->thread_state
= STS_STOPPED
;
533 console_select_thread (void *arg
)
535 struct serial
*scb
= arg
;
536 struct ser_console_state
*state
;
541 h
= (HANDLE
) _get_osfhandle (scb
->fd
);
545 HANDLE wait_events
[2];
549 select_thread_wait (state
);
553 wait_events
[0] = state
->stop_select
;
556 event_index
= WaitForMultipleObjects (2, wait_events
,
559 if (event_index
== WAIT_OBJECT_0
560 || WaitForSingleObject (state
->stop_select
, 0) == WAIT_OBJECT_0
)
563 if (event_index
!= WAIT_OBJECT_0
+ 1)
565 /* Wait must have failed; assume an error has occured, e.g.
566 the handle has been closed. */
567 SetEvent (state
->except_event
);
571 /* We've got a pending event on the console. See if it's
573 if (!PeekConsoleInput (h
, &record
, 1, &n_records
) || n_records
!= 1)
575 /* Something went wrong. Maybe the console is gone. */
576 SetEvent (state
->except_event
);
580 if (record
.EventType
== KEY_EVENT
&& record
.Event
.KeyEvent
.bKeyDown
)
582 WORD keycode
= record
.Event
.KeyEvent
.wVirtualKeyCode
;
584 /* Ignore events containing only control keys. We must
585 recognize "enhanced" keys which we are interested in
586 reading via getch, if they do not map to ASCII. But we
587 do not want to report input available for e.g. the
588 control key alone. */
590 if (record
.Event
.KeyEvent
.uChar
.AsciiChar
!= 0
591 || keycode
== VK_PRIOR
592 || keycode
== VK_NEXT
594 || keycode
== VK_HOME
595 || keycode
== VK_LEFT
597 || keycode
== VK_RIGHT
598 || keycode
== VK_DOWN
599 || keycode
== VK_INSERT
600 || keycode
== VK_DELETE
)
602 /* This is really a keypress. */
603 SetEvent (state
->read_event
);
608 /* Otherwise discard it and wait again. */
609 ReadConsoleInput (h
, &record
, 1, &n_records
);
612 SetEvent(state
->have_stopped
);
620 if (PeekNamedPipe ((HANDLE
) _get_osfhandle (fd
), NULL
, 0, NULL
, NULL
, NULL
))
629 if (GetFileType ((HANDLE
) _get_osfhandle (fd
)) == FILE_TYPE_DISK
)
636 pipe_select_thread (void *arg
)
638 struct serial
*scb
= arg
;
639 struct ser_console_state
*state
;
644 h
= (HANDLE
) _get_osfhandle (scb
->fd
);
650 select_thread_wait (state
);
652 /* Wait for something to happen on the pipe. */
655 if (!PeekNamedPipe (h
, NULL
, 0, NULL
, &n_avail
, NULL
))
657 SetEvent (state
->except_event
);
663 SetEvent (state
->read_event
);
667 /* Delay 10ms before checking again, but allow the stop
669 if (WaitForSingleObject (state
->stop_select
, 10) == WAIT_OBJECT_0
)
673 SetEvent (state
->have_stopped
);
679 file_select_thread (void *arg
)
681 struct serial
*scb
= arg
;
682 struct ser_console_state
*state
;
687 h
= (HANDLE
) _get_osfhandle (scb
->fd
);
691 select_thread_wait (state
);
693 if (SetFilePointer (h
, 0, NULL
, FILE_CURRENT
)
694 == INVALID_SET_FILE_POINTER
)
695 SetEvent (state
->except_event
);
697 SetEvent (state
->read_event
);
699 SetEvent (state
->have_stopped
);
705 ser_console_wait_handle (struct serial
*scb
, HANDLE
*read
, HANDLE
*except
)
707 struct ser_console_state
*state
= scb
->state
;
711 thread_fn_type thread_fn
;
714 is_tty
= isatty (scb
->fd
);
715 if (!is_tty
&& !fd_is_file (scb
->fd
) && !fd_is_pipe (scb
->fd
))
722 state
= XCNEW (struct ser_console_state
);
726 thread_fn
= console_select_thread
;
727 else if (fd_is_pipe (scb
->fd
))
728 thread_fn
= pipe_select_thread
;
730 thread_fn
= file_select_thread
;
732 create_select_thread (thread_fn
, scb
, state
);
735 *read
= state
->read_event
;
736 *except
= state
->except_event
;
738 /* Start from a blank state. */
739 ResetEvent (state
->read_event
);
740 ResetEvent (state
->except_event
);
741 ResetEvent (state
->stop_select
);
743 /* First check for a key already in the buffer. If there is one,
744 we don't need a thread. This also catches the second key of
745 multi-character returns from getch, for instance for arrow
746 keys. The second half is in a C library internal buffer,
747 and PeekConsoleInput will not find it. */
750 SetEvent (state
->read_event
);
754 /* Otherwise, start the select thread. */
755 start_select_thread (state
);
759 ser_console_done_wait_handle (struct serial
*scb
)
761 struct ser_console_state
*state
= scb
->state
;
766 stop_select_thread (state
);
770 ser_console_close (struct serial
*scb
)
772 struct ser_console_state
*state
= scb
->state
;
776 destroy_select_thread (state
);
781 struct ser_console_ttystate
786 static serial_ttystate
787 ser_console_get_tty_state (struct serial
*scb
)
789 if (isatty (scb
->fd
))
791 struct ser_console_ttystate
*state
;
793 state
= XNEW (struct ser_console_ttystate
);
803 /* Since we use the pipe_select_thread for our select emulation,
804 we need to place the state structure it requires at the front
806 struct ser_console_state wait
;
808 /* The pex obj for our (one-stage) pipeline. */
811 /* Streams for the pipeline's input and output. */
812 FILE *input
, *output
;
815 static struct pipe_state
*
816 make_pipe_state (void)
818 struct pipe_state
*ps
= XCNEW (struct pipe_state
);
820 ps
->wait
.read_event
= INVALID_HANDLE_VALUE
;
821 ps
->wait
.except_event
= INVALID_HANDLE_VALUE
;
822 ps
->wait
.start_select
= INVALID_HANDLE_VALUE
;
823 ps
->wait
.stop_select
= INVALID_HANDLE_VALUE
;
829 free_pipe_state (struct pipe_state
*ps
)
831 int saved_errno
= errno
;
833 if (ps
->wait
.read_event
!= INVALID_HANDLE_VALUE
)
834 destroy_select_thread (&ps
->wait
);
836 /* Close the pipe to the child. We must close the pipe before
837 calling pex_free because pex_free will wait for the child to exit
838 and the child will not exit until the pipe is closed. */
844 /* pex_free closes ps->output. */
855 cleanup_pipe_state (void *untyped
)
857 struct pipe_state
*ps
= untyped
;
859 free_pipe_state (ps
);
863 pipe_windows_open (struct serial
*scb
, const char *name
)
865 struct pipe_state
*ps
;
868 struct cleanup
*back_to
;
871 error_no_arg (_("child command"));
873 argv
= gdb_buildargv (name
);
874 back_to
= make_cleanup_freeargv (argv
);
876 if (! argv
[0] || argv
[0][0] == '\0')
877 error (_("missing child command"));
879 ps
= make_pipe_state ();
880 make_cleanup (cleanup_pipe_state
, ps
);
882 ps
->pex
= pex_init (PEX_USE_PIPES
, "target remote pipe", NULL
);
885 ps
->input
= pex_input_pipe (ps
->pex
, 1);
892 = pex_run (ps
->pex
, PEX_SEARCH
| PEX_BINARY_INPUT
| PEX_BINARY_OUTPUT
893 | PEX_STDERR_TO_PIPE
,
894 argv
[0], argv
, NULL
, NULL
,
899 /* Our caller expects us to return -1, but all they'll do with
900 it generally is print the message based on errno. We have
901 all the same information here, plus err_msg provided by
902 pex_run, so we just raise the error here. */
904 error (_("error starting child process '%s': %s: %s"),
905 name
, err_msg
, safe_strerror (err
));
907 error (_("error starting child process '%s': %s"),
912 ps
->output
= pex_read_output (ps
->pex
, 1);
915 scb
->fd
= fileno (ps
->output
);
917 pex_stderr
= pex_read_err (ps
->pex
, 1);
920 scb
->error_fd
= fileno (pex_stderr
);
922 scb
->state
= (void *) ps
;
924 discard_cleanups (back_to
);
928 do_cleanups (back_to
);
933 pipe_windows_fdopen (struct serial
*scb
, int fd
)
935 struct pipe_state
*ps
;
937 ps
= make_pipe_state ();
939 ps
->input
= fdopen (fd
, "r+");
943 ps
->output
= fdopen (fd
, "r+");
948 scb
->state
= (void *) ps
;
953 free_pipe_state (ps
);
958 pipe_windows_close (struct serial
*scb
)
960 struct pipe_state
*ps
= scb
->state
;
962 /* In theory, we should try to kill the subprocess here, but the pex
963 interface doesn't give us enough information to do that. Usually
964 closing the input pipe will get the message across. */
966 free_pipe_state (ps
);
971 pipe_windows_read (struct serial
*scb
, size_t count
)
973 HANDLE pipeline_out
= (HANDLE
) _get_osfhandle (scb
->fd
);
977 if (pipeline_out
== INVALID_HANDLE_VALUE
)
980 if (! PeekNamedPipe (pipeline_out
, NULL
, 0, NULL
, &available
, NULL
))
983 if (count
> available
)
986 if (! ReadFile (pipeline_out
, scb
->buf
, count
, &bytes_read
, NULL
))
994 pipe_windows_write (struct serial
*scb
, const void *buf
, size_t count
)
996 struct pipe_state
*ps
= scb
->state
;
1000 int pipeline_in_fd
= fileno (ps
->input
);
1001 if (pipeline_in_fd
< 0)
1004 pipeline_in
= (HANDLE
) _get_osfhandle (pipeline_in_fd
);
1005 if (pipeline_in
== INVALID_HANDLE_VALUE
)
1008 if (! WriteFile (pipeline_in
, buf
, count
, &written
, NULL
))
1016 pipe_wait_handle (struct serial
*scb
, HANDLE
*read
, HANDLE
*except
)
1018 struct pipe_state
*ps
= scb
->state
;
1020 /* Have we allocated our events yet? */
1021 if (ps
->wait
.read_event
== INVALID_HANDLE_VALUE
)
1022 /* Start the thread. */
1023 create_select_thread (pipe_select_thread
, scb
, &ps
->wait
);
1025 *read
= ps
->wait
.read_event
;
1026 *except
= ps
->wait
.except_event
;
1028 /* Start from a blank state. */
1029 ResetEvent (ps
->wait
.read_event
);
1030 ResetEvent (ps
->wait
.except_event
);
1031 ResetEvent (ps
->wait
.stop_select
);
1033 start_select_thread (&ps
->wait
);
1037 pipe_done_wait_handle (struct serial
*scb
)
1039 struct pipe_state
*ps
= scb
->state
;
1041 /* Have we allocated our events yet? */
1042 if (ps
->wait
.read_event
== INVALID_HANDLE_VALUE
)
1045 stop_select_thread (&ps
->wait
);
1049 pipe_avail (struct serial
*scb
, int fd
)
1051 HANDLE h
= (HANDLE
) _get_osfhandle (fd
);
1053 BOOL r
= PeekNamedPipe (h
, NULL
, 0, NULL
, &numBytes
, NULL
);
1061 gdb_pipe (int pdes
[2])
1063 if (_pipe (pdes
, 512, _O_BINARY
| _O_NOINHERIT
) == -1)
1068 struct net_windows_state
1070 struct ser_console_state base
;
1075 /* Check whether the socket has any pending data to be read. If so,
1076 set the select thread's read event. On error, set the select
1077 thread's except event. If any event was set, return true,
1078 otherwise return false. */
1081 net_windows_socket_check_pending (struct serial
*scb
)
1083 struct net_windows_state
*state
= scb
->state
;
1084 unsigned long available
;
1086 if (ioctlsocket (scb
->fd
, FIONREAD
, &available
) != 0)
1088 /* The socket closed, or some other error. */
1089 SetEvent (state
->base
.except_event
);
1092 else if (available
> 0)
1094 SetEvent (state
->base
.read_event
);
1102 net_windows_select_thread (void *arg
)
1104 struct serial
*scb
= arg
;
1105 struct net_windows_state
*state
;
1112 HANDLE wait_events
[2];
1113 WSANETWORKEVENTS events
;
1115 select_thread_wait (&state
->base
);
1117 wait_events
[0] = state
->base
.stop_select
;
1118 wait_events
[1] = state
->sock_event
;
1120 /* Wait for something to happen on the socket. */
1123 event_index
= WaitForMultipleObjects (2, wait_events
, FALSE
, INFINITE
);
1125 if (event_index
== WAIT_OBJECT_0
1126 || WaitForSingleObject (state
->base
.stop_select
, 0) == WAIT_OBJECT_0
)
1128 /* We have been requested to stop. */
1132 if (event_index
!= WAIT_OBJECT_0
+ 1)
1134 /* Some error has occured. Assume that this is an error
1136 SetEvent (state
->base
.except_event
);
1140 /* Enumerate the internal network events, and reset the
1141 object that signalled us to catch the next event. */
1142 if (WSAEnumNetworkEvents (scb
->fd
, state
->sock_event
, &events
) != 0)
1144 /* Something went wrong. Maybe the socket is gone. */
1145 SetEvent (state
->base
.except_event
);
1149 if (events
.lNetworkEvents
& FD_READ
)
1151 if (net_windows_socket_check_pending (scb
))
1154 /* Spurious wakeup. That is, the socket's event was
1155 signalled before we last called recv. */
1158 if (events
.lNetworkEvents
& FD_CLOSE
)
1160 SetEvent (state
->base
.except_event
);
1165 SetEvent (state
->base
.have_stopped
);
1171 net_windows_wait_handle (struct serial
*scb
, HANDLE
*read
, HANDLE
*except
)
1173 struct net_windows_state
*state
= scb
->state
;
1175 /* Start from a clean slate. */
1176 ResetEvent (state
->base
.read_event
);
1177 ResetEvent (state
->base
.except_event
);
1178 ResetEvent (state
->base
.stop_select
);
1180 *read
= state
->base
.read_event
;
1181 *except
= state
->base
.except_event
;
1183 /* Check any pending events. Otherwise, start the select
1185 if (!net_windows_socket_check_pending (scb
))
1186 start_select_thread (&state
->base
);
1190 net_windows_done_wait_handle (struct serial
*scb
)
1192 struct net_windows_state
*state
= scb
->state
;
1194 stop_select_thread (&state
->base
);
1198 net_windows_open (struct serial
*scb
, const char *name
)
1200 struct net_windows_state
*state
;
1204 ret
= net_open (scb
, name
);
1208 state
= XCNEW (struct net_windows_state
);
1211 /* Associate an event with the socket. */
1212 state
->sock_event
= CreateEvent (0, TRUE
, FALSE
, 0);
1213 WSAEventSelect (scb
->fd
, state
->sock_event
, FD_READ
| FD_CLOSE
);
1215 /* Start the thread. */
1216 create_select_thread (net_windows_select_thread
, scb
, &state
->base
);
1223 net_windows_close (struct serial
*scb
)
1225 struct net_windows_state
*state
= scb
->state
;
1227 destroy_select_thread (&state
->base
);
1228 CloseHandle (state
->sock_event
);
1235 /* The serial port driver. */
1237 static const struct serial_ops hardwire_ops
=
1245 ser_windows_flush_output
,
1246 ser_windows_flush_input
,
1247 ser_windows_send_break
,
1249 /* These are only used for stdin; we do not need them for serial
1250 ports, so supply the standard dummies. */
1251 ser_base_get_tty_state
,
1252 ser_base_copy_tty_state
,
1253 ser_base_set_tty_state
,
1254 ser_base_print_tty_state
,
1255 ser_base_noflush_set_tty_state
,
1256 ser_windows_setbaudrate
,
1257 ser_windows_setstopbits
,
1258 ser_windows_setparity
,
1259 ser_windows_drain_output
,
1261 ser_windows_read_prim
,
1262 ser_windows_write_prim
,
1264 ser_windows_wait_handle
1267 /* The dummy serial driver used for terminals. We only provide the
1268 TTY-related methods. */
1270 static const struct serial_ops tty_ops
=
1282 ser_console_get_tty_state
,
1283 ser_base_copy_tty_state
,
1284 ser_base_set_tty_state
,
1285 ser_base_print_tty_state
,
1286 ser_base_noflush_set_tty_state
,
1290 ser_base_drain_output
,
1295 ser_console_wait_handle
,
1296 ser_console_done_wait_handle
1299 /* The pipe interface. */
1301 static const struct serial_ops pipe_ops
=
1306 pipe_windows_fdopen
,
1309 ser_base_flush_output
,
1310 ser_base_flush_input
,
1311 ser_base_send_break
,
1313 ser_base_get_tty_state
,
1314 ser_base_copy_tty_state
,
1315 ser_base_set_tty_state
,
1316 ser_base_print_tty_state
,
1317 ser_base_noflush_set_tty_state
,
1318 ser_base_setbaudrate
,
1319 ser_base_setstopbits
,
1321 ser_base_drain_output
,
1327 pipe_done_wait_handle
1330 /* The TCP/UDP socket driver. */
1332 static const struct serial_ops tcp_ops
=
1340 ser_base_flush_output
,
1341 ser_base_flush_input
,
1344 ser_base_get_tty_state
,
1345 ser_base_copy_tty_state
,
1346 ser_base_set_tty_state
,
1347 ser_base_print_tty_state
,
1348 ser_base_noflush_set_tty_state
,
1349 ser_base_setbaudrate
,
1350 ser_base_setstopbits
,
1352 ser_base_drain_output
,
1357 net_windows_wait_handle
,
1358 net_windows_done_wait_handle
1362 _initialize_ser_windows (void)
1365 struct serial_ops
*ops
;
1369 /* First find out if kernel32 exports CancelIo function. */
1370 hm
= LoadLibrary ("kernel32.dll");
1373 CancelIo
= (void *) GetProcAddress (hm
, "CancelIo");
1379 serial_add_interface (&hardwire_ops
);
1380 serial_add_interface (&tty_ops
);
1381 serial_add_interface (&pipe_ops
);
1383 /* If WinSock works, register the TCP/UDP socket driver. */
1385 if (WSAStartup (MAKEWORD (1, 0), &wsa_data
) != 0)
1386 /* WinSock is unavailable. */
1389 serial_add_interface (&tcp_ops
);