1 /* Serial interface for local (hardwired) serial ports on Windows systems
3 Copyright (C) 2006, 2007 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 2 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, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA. */
32 #include <sys/types.h>
34 #include "gdb_assert.h"
35 #include "gdb_string.h"
37 void _initialize_ser_windows (void);
39 struct ser_windows_state
47 /* Open up a real live device for serial I/O. */
50 ser_windows_open (struct serial
*scb
, const char *name
)
53 struct ser_windows_state
*state
;
54 COMMTIMEOUTS timeouts
;
56 /* Only allow COM ports. */
57 if (strncmp (name
, "COM", 3) != 0)
63 h
= CreateFile (name
, GENERIC_READ
| GENERIC_WRITE
, 0, NULL
,
64 OPEN_EXISTING
, FILE_FLAG_OVERLAPPED
, NULL
);
65 if (h
== INVALID_HANDLE_VALUE
)
71 scb
->fd
= _open_osfhandle ((long) h
, O_RDWR
);
78 if (!SetCommMask (h
, EV_RXCHAR
))
84 timeouts
.ReadIntervalTimeout
= MAXDWORD
;
85 timeouts
.ReadTotalTimeoutConstant
= 0;
86 timeouts
.ReadTotalTimeoutMultiplier
= 0;
87 timeouts
.WriteTotalTimeoutConstant
= 0;
88 timeouts
.WriteTotalTimeoutMultiplier
= 0;
89 if (!SetCommTimeouts (h
, &timeouts
))
95 state
= xmalloc (sizeof (struct ser_windows_state
));
96 memset (state
, 0, sizeof (struct ser_windows_state
));
99 /* Create a manual reset event to watch the input buffer. */
100 state
->ov
.hEvent
= CreateEvent (0, TRUE
, FALSE
, 0);
102 /* Create a (currently unused) handle to record exceptions. */
103 state
->except_event
= CreateEvent (0, TRUE
, FALSE
, 0);
108 /* Wait for the output to drain away, as opposed to flushing (discarding)
112 ser_windows_drain_output (struct serial
*scb
)
114 HANDLE h
= (HANDLE
) _get_osfhandle (scb
->fd
);
116 return (FlushFileBuffers (h
) != 0) ? 0 : -1;
120 ser_windows_flush_output (struct serial
*scb
)
122 HANDLE h
= (HANDLE
) _get_osfhandle (scb
->fd
);
124 return (PurgeComm (h
, PURGE_TXCLEAR
) != 0) ? 0 : -1;
128 ser_windows_flush_input (struct serial
*scb
)
130 HANDLE h
= (HANDLE
) _get_osfhandle (scb
->fd
);
132 return (PurgeComm (h
, PURGE_RXCLEAR
) != 0) ? 0 : -1;
136 ser_windows_send_break (struct serial
*scb
)
138 HANDLE h
= (HANDLE
) _get_osfhandle (scb
->fd
);
140 if (SetCommBreak (h
) == 0)
143 /* Delay for 250 milliseconds. */
146 if (ClearCommBreak (h
))
153 ser_windows_raw (struct serial
*scb
)
155 HANDLE h
= (HANDLE
) _get_osfhandle (scb
->fd
);
158 if (GetCommState (h
, &state
) == 0)
161 state
.fParity
= FALSE
;
162 state
.fOutxCtsFlow
= FALSE
;
163 state
.fOutxDsrFlow
= FALSE
;
164 state
.fDtrControl
= DTR_CONTROL_ENABLE
;
165 state
.fDsrSensitivity
= FALSE
;
169 state
.fAbortOnError
= FALSE
;
171 state
.Parity
= NOPARITY
;
173 scb
->current_timeout
= 0;
175 if (SetCommState (h
, &state
) == 0)
176 warning (_("SetCommState failed\n"));
180 ser_windows_setstopbits (struct serial
*scb
, int num
)
182 HANDLE h
= (HANDLE
) _get_osfhandle (scb
->fd
);
185 if (GetCommState (h
, &state
) == 0)
190 case SERIAL_1_STOPBITS
:
191 state
.StopBits
= ONESTOPBIT
;
193 case SERIAL_1_AND_A_HALF_STOPBITS
:
194 state
.StopBits
= ONE5STOPBITS
;
196 case SERIAL_2_STOPBITS
:
197 state
.StopBits
= TWOSTOPBITS
;
203 return (SetCommState (h
, &state
) != 0) ? 0 : -1;
207 ser_windows_setbaudrate (struct serial
*scb
, int rate
)
209 HANDLE h
= (HANDLE
) _get_osfhandle (scb
->fd
);
212 if (GetCommState (h
, &state
) == 0)
215 state
.BaudRate
= rate
;
217 return (SetCommState (h
, &state
) != 0) ? 0 : -1;
221 ser_windows_close (struct serial
*scb
)
223 struct ser_windows_state
*state
;
225 /* Stop any pending selects. */
226 CancelIo ((HANDLE
) _get_osfhandle (scb
->fd
));
228 CloseHandle (state
->ov
.hEvent
);
229 CloseHandle (state
->except_event
);
241 ser_windows_wait_handle (struct serial
*scb
, HANDLE
*read
, HANDLE
*except
)
243 struct ser_windows_state
*state
;
246 HANDLE h
= (HANDLE
) _get_osfhandle (scb
->fd
);
250 *except
= state
->except_event
;
251 *read
= state
->ov
.hEvent
;
253 if (state
->in_progress
)
256 /* Reset the mask - we are only interested in any characters which
257 arrive after this point, not characters which might have arrived
258 and already been read. */
260 /* This really, really shouldn't be necessary - just the second one.
261 But otherwise an internal flag for EV_RXCHAR does not get
262 cleared, and we get a duplicated event, if the last batch
263 of characters included at least two arriving close together. */
264 if (!SetCommMask (h
, 0))
265 warning (_("ser_windows_wait_handle: reseting mask failed"));
267 if (!SetCommMask (h
, EV_RXCHAR
))
268 warning (_("ser_windows_wait_handle: reseting mask failed (2)"));
270 /* There's a potential race condition here; we must check cbInQue
271 and not wait if that's nonzero. */
273 ClearCommError (h
, &errors
, &status
);
274 if (status
.cbInQue
> 0)
276 SetEvent (state
->ov
.hEvent
);
280 state
->in_progress
= 1;
281 ResetEvent (state
->ov
.hEvent
);
282 state
->lastCommMask
= -2;
283 if (WaitCommEvent (h
, &state
->lastCommMask
, &state
->ov
))
285 gdb_assert (state
->lastCommMask
& EV_RXCHAR
);
286 SetEvent (state
->ov
.hEvent
);
289 gdb_assert (GetLastError () == ERROR_IO_PENDING
);
293 ser_windows_read_prim (struct serial
*scb
, size_t count
)
295 struct ser_windows_state
*state
;
297 DWORD bytes_read
, bytes_read_tmp
;
302 if (state
->in_progress
)
304 WaitForSingleObject (state
->ov
.hEvent
, INFINITE
);
305 state
->in_progress
= 0;
306 ResetEvent (state
->ov
.hEvent
);
309 memset (&ov
, 0, sizeof (OVERLAPPED
));
310 ov
.hEvent
= CreateEvent (0, FALSE
, FALSE
, 0);
311 h
= (HANDLE
) _get_osfhandle (scb
->fd
);
313 if (!ReadFile (h
, scb
->buf
, /* count */ 1, &bytes_read
, &ov
))
315 if (GetLastError () != ERROR_IO_PENDING
316 || !GetOverlappedResult (h
, &ov
, &bytes_read
, TRUE
))
320 CloseHandle (ov
.hEvent
);
325 ser_windows_write_prim (struct serial
*scb
, const void *buf
, size_t len
)
327 struct ser_windows_state
*state
;
332 memset (&ov
, 0, sizeof (OVERLAPPED
));
333 ov
.hEvent
= CreateEvent (0, FALSE
, FALSE
, 0);
334 h
= (HANDLE
) _get_osfhandle (scb
->fd
);
335 if (!WriteFile (h
, buf
, len
, &bytes_written
, &ov
))
337 if (GetLastError () != ERROR_IO_PENDING
338 || !GetOverlappedResult (h
, &ov
, &bytes_written
, TRUE
))
342 CloseHandle (ov
.hEvent
);
343 return bytes_written
;
346 struct ser_console_state
360 console_select_thread (void *arg
)
362 struct serial
*scb
= arg
;
363 struct ser_console_state
*state
;
368 h
= (HANDLE
) _get_osfhandle (scb
->fd
);
372 HANDLE wait_events
[2];
376 SetEvent (state
->have_stopped
);
378 wait_events
[0] = state
->start_select
;
379 wait_events
[1] = state
->exit_select
;
381 if (WaitForMultipleObjects (2, wait_events
, FALSE
, INFINITE
) != WAIT_OBJECT_0
)
384 ResetEvent (state
->have_stopped
);
387 wait_events
[0] = state
->stop_select
;
390 event_index
= WaitForMultipleObjects (2, wait_events
, FALSE
, INFINITE
);
392 if (event_index
== WAIT_OBJECT_0
393 || WaitForSingleObject (state
->stop_select
, 0) == WAIT_OBJECT_0
)
396 if (event_index
!= WAIT_OBJECT_0
+ 1)
398 /* Wait must have failed; assume an error has occured, e.g.
399 the handle has been closed. */
400 SetEvent (state
->except_event
);
404 /* We've got a pending event on the console. See if it's
406 if (!PeekConsoleInput (h
, &record
, 1, &n_records
) || n_records
!= 1)
408 /* Something went wrong. Maybe the console is gone. */
409 SetEvent (state
->except_event
);
413 if (record
.EventType
== KEY_EVENT
&& record
.Event
.KeyEvent
.bKeyDown
)
415 WORD keycode
= record
.Event
.KeyEvent
.wVirtualKeyCode
;
417 /* Ignore events containing only control keys. We must
418 recognize "enhanced" keys which we are interested in
419 reading via getch, if they do not map to ASCII. But we
420 do not want to report input available for e.g. the
421 control key alone. */
423 if (record
.Event
.KeyEvent
.uChar
.AsciiChar
!= 0
424 || keycode
== VK_PRIOR
425 || keycode
== VK_NEXT
427 || keycode
== VK_HOME
428 || keycode
== VK_LEFT
430 || keycode
== VK_RIGHT
431 || keycode
== VK_DOWN
432 || keycode
== VK_INSERT
433 || keycode
== VK_DELETE
)
435 /* This is really a keypress. */
436 SetEvent (state
->read_event
);
441 /* Otherwise discard it and wait again. */
442 ReadConsoleInput (h
, &record
, 1, &n_records
);
450 if (PeekNamedPipe ((HANDLE
) _get_osfhandle (fd
), NULL
, 0, NULL
, NULL
, NULL
))
457 pipe_select_thread (void *arg
)
459 struct serial
*scb
= arg
;
460 struct ser_console_state
*state
;
465 h
= (HANDLE
) _get_osfhandle (scb
->fd
);
469 HANDLE wait_events
[2];
472 SetEvent (state
->have_stopped
);
474 wait_events
[0] = state
->start_select
;
475 wait_events
[1] = state
->exit_select
;
477 if (WaitForMultipleObjects (2, wait_events
, FALSE
, INFINITE
) != WAIT_OBJECT_0
)
480 ResetEvent (state
->have_stopped
);
483 if (!PeekNamedPipe (h
, NULL
, 0, NULL
, &n_avail
, NULL
))
485 SetEvent (state
->except_event
);
491 SetEvent (state
->read_event
);
495 /* Delay 10ms before checking again, but allow the stop event
497 if (WaitForSingleObject (state
->stop_select
, 10) == WAIT_OBJECT_0
)
505 ser_console_wait_handle (struct serial
*scb
, HANDLE
*read
, HANDLE
*except
)
507 struct ser_console_state
*state
= scb
->state
;
514 is_tty
= isatty (scb
->fd
);
515 if (!is_tty
&& !fd_is_pipe (scb
->fd
))
522 state
= xmalloc (sizeof (struct ser_console_state
));
523 memset (state
, 0, sizeof (struct ser_console_state
));
526 /* Create auto reset events to wake, stop, and exit the select
528 state
->start_select
= CreateEvent (0, FALSE
, FALSE
, 0);
529 state
->stop_select
= CreateEvent (0, FALSE
, FALSE
, 0);
530 state
->exit_select
= CreateEvent (0, FALSE
, FALSE
, 0);
532 /* Create a manual reset event to signal whether the thread is
533 stopped. This must be manual reset, because we may wait on
534 it multiple times without ever starting the thread. */
535 state
->have_stopped
= CreateEvent (0, TRUE
, FALSE
, 0);
537 /* Create our own events to report read and exceptions separately. */
538 state
->read_event
= CreateEvent (0, FALSE
, FALSE
, 0);
539 state
->except_event
= CreateEvent (0, FALSE
, FALSE
, 0);
542 state
->thread
= CreateThread (NULL
, 0, console_select_thread
, scb
, 0,
545 state
->thread
= CreateThread (NULL
, 0, pipe_select_thread
, scb
, 0,
549 *read
= state
->read_event
;
550 *except
= state
->except_event
;
552 /* Start from a blank state. */
553 ResetEvent (state
->read_event
);
554 ResetEvent (state
->except_event
);
555 ResetEvent (state
->stop_select
);
557 /* First check for a key already in the buffer. If there is one,
558 we don't need a thread. This also catches the second key of
559 multi-character returns from getch, for instance for arrow
560 keys. The second half is in a C library internal buffer,
561 and PeekConsoleInput will not find it. */
564 SetEvent (state
->read_event
);
568 /* Otherwise, start the select thread. */
569 SetEvent (state
->start_select
);
573 ser_console_done_wait_handle (struct serial
*scb
)
575 struct ser_console_state
*state
= scb
->state
;
580 SetEvent (state
->stop_select
);
581 WaitForSingleObject (state
->have_stopped
, INFINITE
);
585 ser_console_close (struct serial
*scb
)
587 struct ser_console_state
*state
= scb
->state
;
591 SetEvent (state
->exit_select
);
593 WaitForSingleObject (state
->thread
, INFINITE
);
595 CloseHandle (state
->start_select
);
596 CloseHandle (state
->stop_select
);
597 CloseHandle (state
->exit_select
);
598 CloseHandle (state
->have_stopped
);
600 CloseHandle (state
->read_event
);
601 CloseHandle (state
->except_event
);
607 struct ser_console_ttystate
612 static serial_ttystate
613 ser_console_get_tty_state (struct serial
*scb
)
615 if (isatty (scb
->fd
))
617 struct ser_console_ttystate
*state
;
618 state
= (struct ser_console_ttystate
*) xmalloc (sizeof *state
);
628 /* Since we use the pipe_select_thread for our select emulation,
629 we need to place the state structure it requires at the front
631 struct ser_console_state wait
;
633 /* The pex obj for our (one-stage) pipeline. */
636 /* Streams for the pipeline's input and output. */
637 FILE *input
, *output
;
640 static struct pipe_state
*
641 make_pipe_state (void)
643 struct pipe_state
*ps
= XMALLOC (struct pipe_state
);
645 memset (ps
, 0, sizeof (*ps
));
646 ps
->wait
.read_event
= INVALID_HANDLE_VALUE
;
647 ps
->wait
.except_event
= INVALID_HANDLE_VALUE
;
648 ps
->wait
.start_select
= INVALID_HANDLE_VALUE
;
649 ps
->wait
.stop_select
= INVALID_HANDLE_VALUE
;
655 free_pipe_state (struct pipe_state
*ps
)
657 int saved_errno
= errno
;
659 if (ps
->wait
.read_event
!= INVALID_HANDLE_VALUE
)
661 SetEvent (ps
->wait
.exit_select
);
663 WaitForSingleObject (ps
->wait
.thread
, INFINITE
);
665 CloseHandle (ps
->wait
.start_select
);
666 CloseHandle (ps
->wait
.stop_select
);
667 CloseHandle (ps
->wait
.exit_select
);
668 CloseHandle (ps
->wait
.have_stopped
);
670 CloseHandle (ps
->wait
.read_event
);
671 CloseHandle (ps
->wait
.except_event
);
674 /* Close the pipe to the child. We must close the pipe before
675 calling pex_free because pex_free will wait for the child to exit
676 and the child will not exit until the pipe is closed. */
681 /* pex_free closes ps->output. */
689 cleanup_pipe_state (void *untyped
)
691 struct pipe_state
*ps
= untyped
;
693 free_pipe_state (ps
);
697 pipe_windows_open (struct serial
*scb
, const char *name
)
699 struct pipe_state
*ps
;
701 char **argv
= buildargv (name
);
702 struct cleanup
*back_to
= make_cleanup_freeargv (argv
);
703 if (! argv
[0] || argv
[0][0] == '\0')
704 error ("missing child command");
707 ps
= make_pipe_state ();
708 make_cleanup (cleanup_pipe_state
, ps
);
710 ps
->pex
= pex_init (PEX_USE_PIPES
, "target remote pipe", NULL
);
713 ps
->input
= pex_input_pipe (ps
->pex
, 1);
720 = pex_run (ps
->pex
, PEX_SEARCH
| PEX_BINARY_INPUT
| PEX_BINARY_OUTPUT
,
721 argv
[0], argv
, NULL
, NULL
,
726 /* Our caller expects us to return -1, but all they'll do with
727 it generally is print the message based on errno. We have
728 all the same information here, plus err_msg provided by
729 pex_run, so we just raise the error here. */
731 error ("error starting child process '%s': %s: %s",
732 name
, err_msg
, safe_strerror (err
));
734 error ("error starting child process '%s': %s",
739 ps
->output
= pex_read_output (ps
->pex
, 1);
743 scb
->fd
= fileno (ps
->output
);
744 scb
->state
= (void *) ps
;
746 discard_cleanups (back_to
);
750 do_cleanups (back_to
);
756 pipe_windows_close (struct serial
*scb
)
758 struct pipe_state
*ps
= scb
->state
;
760 /* In theory, we should try to kill the subprocess here, but the pex
761 interface doesn't give us enough information to do that. Usually
762 closing the input pipe will get the message across. */
764 free_pipe_state (ps
);
769 pipe_windows_read (struct serial
*scb
, size_t count
)
771 HANDLE pipeline_out
= (HANDLE
) _get_osfhandle (scb
->fd
);
775 if (pipeline_out
== INVALID_HANDLE_VALUE
)
778 if (! PeekNamedPipe (pipeline_out
, NULL
, 0, NULL
, &available
, NULL
))
781 if (count
> available
)
784 if (! ReadFile (pipeline_out
, scb
->buf
, count
, &bytes_read
, NULL
))
792 pipe_windows_write (struct serial
*scb
, const void *buf
, size_t count
)
794 struct pipe_state
*ps
= scb
->state
;
798 int pipeline_in_fd
= fileno (ps
->input
);
799 if (pipeline_in_fd
< 0)
802 pipeline_in
= (HANDLE
) _get_osfhandle (pipeline_in_fd
);
803 if (pipeline_in
== INVALID_HANDLE_VALUE
)
806 if (! WriteFile (pipeline_in
, buf
, count
, &written
, NULL
))
814 pipe_wait_handle (struct serial
*scb
, HANDLE
*read
, HANDLE
*except
)
816 struct pipe_state
*ps
= scb
->state
;
818 /* Have we allocated our events yet? */
819 if (ps
->wait
.read_event
== INVALID_HANDLE_VALUE
)
823 /* Create auto reset events to wake, stop, and exit the select
825 ps
->wait
.start_select
= CreateEvent (0, FALSE
, FALSE
, 0);
826 ps
->wait
.stop_select
= CreateEvent (0, FALSE
, FALSE
, 0);
827 ps
->wait
.exit_select
= CreateEvent (0, FALSE
, FALSE
, 0);
829 /* Create a manual reset event to signal whether the thread is
830 stopped. This must be manual reset, because we may wait on
831 it multiple times without ever starting the thread. */
832 ps
->wait
.have_stopped
= CreateEvent (0, TRUE
, FALSE
, 0);
834 /* Create our own events to report read and exceptions separately.
835 The exception event is currently never used. */
836 ps
->wait
.read_event
= CreateEvent (0, FALSE
, FALSE
, 0);
837 ps
->wait
.except_event
= CreateEvent (0, FALSE
, FALSE
, 0);
839 /* Start the select thread. */
840 CreateThread (NULL
, 0, pipe_select_thread
, scb
, 0, &threadId
);
843 *read
= ps
->wait
.read_event
;
844 *except
= ps
->wait
.except_event
;
846 /* Start from a blank state. */
847 ResetEvent (ps
->wait
.read_event
);
848 ResetEvent (ps
->wait
.except_event
);
849 ResetEvent (ps
->wait
.stop_select
);
851 /* Start the select thread. */
852 SetEvent (ps
->wait
.start_select
);
856 pipe_done_wait_handle (struct serial
*scb
)
858 struct pipe_state
*ps
= scb
->state
;
860 /* Have we allocated our events yet? */
861 if (ps
->wait
.read_event
== INVALID_HANDLE_VALUE
)
864 SetEvent (ps
->wait
.stop_select
);
865 WaitForSingleObject (ps
->wait
.have_stopped
, INFINITE
);
868 struct net_windows_state
884 net_windows_select_thread (void *arg
)
886 struct serial
*scb
= arg
;
887 struct net_windows_state
*state
, state_copy
;
894 HANDLE wait_events
[2];
895 WSANETWORKEVENTS events
;
897 SetEvent (state
->have_stopped
);
899 wait_events
[0] = state
->start_select
;
900 wait_events
[1] = state
->exit_select
;
902 if (WaitForMultipleObjects (2, wait_events
, FALSE
, INFINITE
) != WAIT_OBJECT_0
)
905 ResetEvent (state
->have_stopped
);
907 wait_events
[0] = state
->stop_select
;
908 wait_events
[1] = state
->sock_event
;
910 event_index
= WaitForMultipleObjects (2, wait_events
, FALSE
, INFINITE
);
912 if (event_index
== WAIT_OBJECT_0
913 || WaitForSingleObject (state
->stop_select
, 0) == WAIT_OBJECT_0
)
916 if (event_index
!= WAIT_OBJECT_0
+ 1)
918 /* Some error has occured. Assume that this is an error
920 SetEvent (state
->except_event
);
924 /* Enumerate the internal network events, and reset the object that
925 signalled us to catch the next event. */
926 WSAEnumNetworkEvents (scb
->fd
, state
->sock_event
, &events
);
928 gdb_assert (events
.lNetworkEvents
& (FD_READ
| FD_CLOSE
));
930 if (events
.lNetworkEvents
& FD_READ
)
931 SetEvent (state
->read_event
);
933 if (events
.lNetworkEvents
& FD_CLOSE
)
934 SetEvent (state
->except_event
);
939 net_windows_wait_handle (struct serial
*scb
, HANDLE
*read
, HANDLE
*except
)
941 struct net_windows_state
*state
= scb
->state
;
943 /* Start from a clean slate. */
944 ResetEvent (state
->read_event
);
945 ResetEvent (state
->except_event
);
946 ResetEvent (state
->stop_select
);
948 *read
= state
->read_event
;
949 *except
= state
->except_event
;
951 /* Check any pending events. This both avoids starting the thread
952 unnecessarily, and handles stray FD_READ events (see below). */
953 if (WaitForSingleObject (state
->sock_event
, 0) == WAIT_OBJECT_0
)
955 WSANETWORKEVENTS events
;
958 /* Enumerate the internal network events, and reset the object that
959 signalled us to catch the next event. */
960 WSAEnumNetworkEvents (scb
->fd
, state
->sock_event
, &events
);
962 /* You'd think that FD_READ or FD_CLOSE would be set here. But,
963 sometimes, neither is. I suspect that the FD_READ is set and
964 the corresponding event signalled while recv is running, and
965 the FD_READ is then lowered when recv consumes all the data,
966 but there's no way to un-signal the event. This isn't a
967 problem for the call in net_select_thread, since any new
968 events after this point will not have been drained by recv.
969 It just means that we can't have the obvious assert here. */
971 /* If there is a read event, it might be still valid, or it might
972 not be - it may have been signalled before we last called
973 recv. Double-check that there is data. */
974 if (events
.lNetworkEvents
& FD_READ
)
976 unsigned long available
;
978 if (ioctlsocket (scb
->fd
, FIONREAD
, &available
) == 0
981 SetEvent (state
->read_event
);
985 /* Oops, no data. This call to recv will cause future
986 data to retrigger the event, e.g. while we are
987 in net_select_thread. */
988 recv (scb
->fd
, NULL
, 0, 0);
991 /* If there's a close event, then record it - it is obviously
992 still valid, and it will not be resignalled. */
993 if (events
.lNetworkEvents
& FD_CLOSE
)
995 SetEvent (state
->except_event
);
999 /* If we set either handle, there's no need to wake the thread. */
1004 /* Start the select thread. */
1005 SetEvent (state
->start_select
);
1009 net_windows_done_wait_handle (struct serial
*scb
)
1011 struct net_windows_state
*state
= scb
->state
;
1013 SetEvent (state
->stop_select
);
1014 WaitForSingleObject (state
->have_stopped
, INFINITE
);
1018 net_windows_open (struct serial
*scb
, const char *name
)
1020 struct net_windows_state
*state
;
1024 ret
= net_open (scb
, name
);
1028 state
= xmalloc (sizeof (struct net_windows_state
));
1029 memset (state
, 0, sizeof (struct net_windows_state
));
1032 /* Create auto reset events to wake, stop, and exit the select
1034 state
->start_select
= CreateEvent (0, FALSE
, FALSE
, 0);
1035 state
->stop_select
= CreateEvent (0, FALSE
, FALSE
, 0);
1036 state
->exit_select
= CreateEvent (0, FALSE
, FALSE
, 0);
1038 /* Create a manual reset event to signal whether the thread is
1039 stopped. This must be manual reset, because we may wait on
1040 it multiple times without ever starting the thread. */
1041 state
->have_stopped
= CreateEvent (0, TRUE
, FALSE
, 0);
1043 /* Associate an event with the socket. */
1044 state
->sock_event
= CreateEvent (0, TRUE
, FALSE
, 0);
1045 WSAEventSelect (scb
->fd
, state
->sock_event
, FD_READ
| FD_CLOSE
);
1047 /* Create our own events to report read and close separately. */
1048 state
->read_event
= CreateEvent (0, FALSE
, FALSE
, 0);
1049 state
->except_event
= CreateEvent (0, FALSE
, FALSE
, 0);
1051 /* And finally start the select thread. */
1052 state
->thread
= CreateThread (NULL
, 0, net_windows_select_thread
, scb
, 0,
1060 net_windows_close (struct serial
*scb
)
1062 struct net_windows_state
*state
= scb
->state
;
1064 SetEvent (state
->exit_select
);
1065 WaitForSingleObject (state
->thread
, INFINITE
);
1067 CloseHandle (state
->read_event
);
1068 CloseHandle (state
->except_event
);
1070 CloseHandle (state
->start_select
);
1071 CloseHandle (state
->stop_select
);
1072 CloseHandle (state
->exit_select
);
1073 CloseHandle (state
->have_stopped
);
1075 CloseHandle (state
->sock_event
);
1083 _initialize_ser_windows (void)
1086 struct serial_ops
*ops
;
1088 /* First register the serial port driver. */
1090 ops
= XMALLOC (struct serial_ops
);
1091 memset (ops
, 0, sizeof (struct serial_ops
));
1092 ops
->name
= "hardwire";
1094 ops
->open
= ser_windows_open
;
1095 ops
->close
= ser_windows_close
;
1097 ops
->flush_output
= ser_windows_flush_output
;
1098 ops
->flush_input
= ser_windows_flush_input
;
1099 ops
->send_break
= ser_windows_send_break
;
1101 /* These are only used for stdin; we do not need them for serial
1102 ports, so supply the standard dummies. */
1103 ops
->get_tty_state
= ser_base_get_tty_state
;
1104 ops
->set_tty_state
= ser_base_set_tty_state
;
1105 ops
->print_tty_state
= ser_base_print_tty_state
;
1106 ops
->noflush_set_tty_state
= ser_base_noflush_set_tty_state
;
1108 ops
->go_raw
= ser_windows_raw
;
1109 ops
->setbaudrate
= ser_windows_setbaudrate
;
1110 ops
->setstopbits
= ser_windows_setstopbits
;
1111 ops
->drain_output
= ser_windows_drain_output
;
1112 ops
->readchar
= ser_base_readchar
;
1113 ops
->write
= ser_base_write
;
1114 ops
->async
= ser_base_async
;
1115 ops
->read_prim
= ser_windows_read_prim
;
1116 ops
->write_prim
= ser_windows_write_prim
;
1117 ops
->wait_handle
= ser_windows_wait_handle
;
1119 serial_add_interface (ops
);
1121 /* Next create the dummy serial driver used for terminals. We only
1122 provide the TTY-related methods. */
1124 ops
= XMALLOC (struct serial_ops
);
1125 memset (ops
, 0, sizeof (struct serial_ops
));
1127 ops
->name
= "terminal";
1130 ops
->close
= ser_console_close
;
1131 ops
->get_tty_state
= ser_console_get_tty_state
;
1132 ops
->set_tty_state
= ser_base_set_tty_state
;
1133 ops
->print_tty_state
= ser_base_print_tty_state
;
1134 ops
->noflush_set_tty_state
= ser_base_noflush_set_tty_state
;
1135 ops
->drain_output
= ser_base_drain_output
;
1136 ops
->wait_handle
= ser_console_wait_handle
;
1137 ops
->done_wait_handle
= ser_console_done_wait_handle
;
1139 serial_add_interface (ops
);
1141 /* The pipe interface. */
1143 ops
= XMALLOC (struct serial_ops
);
1144 memset (ops
, 0, sizeof (struct serial_ops
));
1147 ops
->open
= pipe_windows_open
;
1148 ops
->close
= pipe_windows_close
;
1149 ops
->readchar
= ser_base_readchar
;
1150 ops
->write
= ser_base_write
;
1151 ops
->flush_output
= ser_base_flush_output
;
1152 ops
->flush_input
= ser_base_flush_input
;
1153 ops
->send_break
= ser_base_send_break
;
1154 ops
->go_raw
= ser_base_raw
;
1155 ops
->get_tty_state
= ser_base_get_tty_state
;
1156 ops
->set_tty_state
= ser_base_set_tty_state
;
1157 ops
->print_tty_state
= ser_base_print_tty_state
;
1158 ops
->noflush_set_tty_state
= ser_base_noflush_set_tty_state
;
1159 ops
->setbaudrate
= ser_base_setbaudrate
;
1160 ops
->setstopbits
= ser_base_setstopbits
;
1161 ops
->drain_output
= ser_base_drain_output
;
1162 ops
->async
= ser_base_async
;
1163 ops
->read_prim
= pipe_windows_read
;
1164 ops
->write_prim
= pipe_windows_write
;
1165 ops
->wait_handle
= pipe_wait_handle
;
1166 ops
->done_wait_handle
= pipe_done_wait_handle
;
1168 serial_add_interface (ops
);
1170 /* If WinSock works, register the TCP/UDP socket driver. */
1172 if (WSAStartup (MAKEWORD (1, 0), &wsa_data
) != 0)
1173 /* WinSock is unavailable. */
1176 ops
= XMALLOC (struct serial_ops
);
1177 memset (ops
, 0, sizeof (struct serial_ops
));
1180 ops
->open
= net_windows_open
;
1181 ops
->close
= net_windows_close
;
1182 ops
->readchar
= ser_base_readchar
;
1183 ops
->write
= ser_base_write
;
1184 ops
->flush_output
= ser_base_flush_output
;
1185 ops
->flush_input
= ser_base_flush_input
;
1186 ops
->send_break
= ser_base_send_break
;
1187 ops
->go_raw
= ser_base_raw
;
1188 ops
->get_tty_state
= ser_base_get_tty_state
;
1189 ops
->set_tty_state
= ser_base_set_tty_state
;
1190 ops
->print_tty_state
= ser_base_print_tty_state
;
1191 ops
->noflush_set_tty_state
= ser_base_noflush_set_tty_state
;
1192 ops
->setbaudrate
= ser_base_setbaudrate
;
1193 ops
->setstopbits
= ser_base_setstopbits
;
1194 ops
->drain_output
= ser_base_drain_output
;
1195 ops
->async
= ser_base_async
;
1196 ops
->read_prim
= net_read_prim
;
1197 ops
->write_prim
= net_write_prim
;
1198 ops
->wait_handle
= net_windows_wait_handle
;
1199 ops
->done_wait_handle
= net_windows_done_wait_handle
;
1200 serial_add_interface (ops
);