1 /* Serial interface for local (hardwired) serial ports on Windows systems
3 Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011
4 Free Software Foundation, Inc.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
31 #include <sys/types.h>
33 #include "gdb_assert.h"
34 #include "gdb_string.h"
38 void _initialize_ser_windows (void);
40 struct ser_windows_state
48 /* Open up a real live device for serial I/O. */
51 ser_windows_open (struct serial *scb, const char *name)
54 struct ser_windows_state *state;
55 COMMTIMEOUTS timeouts;
57 h = CreateFile (name, GENERIC_READ | GENERIC_WRITE, 0, NULL,
58 OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
59 if (h == INVALID_HANDLE_VALUE)
65 scb->fd = _open_osfhandle ((intptr_t) h, O_RDWR);
72 if (!SetCommMask (h, EV_RXCHAR))
78 timeouts.ReadIntervalTimeout = MAXDWORD;
79 timeouts.ReadTotalTimeoutConstant = 0;
80 timeouts.ReadTotalTimeoutMultiplier = 0;
81 timeouts.WriteTotalTimeoutConstant = 0;
82 timeouts.WriteTotalTimeoutMultiplier = 0;
83 if (!SetCommTimeouts (h, &timeouts))
89 state = xmalloc (sizeof (struct ser_windows_state));
90 memset (state, 0, sizeof (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.fParity = FALSE;
156 state.fOutxCtsFlow = FALSE;
157 state.fOutxDsrFlow = FALSE;
158 state.fDtrControl = DTR_CONTROL_ENABLE;
159 state.fDsrSensitivity = FALSE;
163 state.fAbortOnError = FALSE;
165 state.Parity = NOPARITY;
167 scb->current_timeout = 0;
169 if (SetCommState (h, &state) == 0)
170 warning (_("SetCommState failed\n"));
174 ser_windows_setstopbits (struct serial *scb, int num)
176 HANDLE h = (HANDLE) _get_osfhandle (scb->fd);
179 if (GetCommState (h, &state) == 0)
184 case SERIAL_1_STOPBITS:
185 state.StopBits = ONESTOPBIT;
187 case SERIAL_1_AND_A_HALF_STOPBITS:
188 state.StopBits = ONE5STOPBITS;
190 case SERIAL_2_STOPBITS:
191 state.StopBits = TWOSTOPBITS;
197 return (SetCommState (h, &state) != 0) ? 0 : -1;
201 ser_windows_setbaudrate (struct serial *scb, int rate)
203 HANDLE h = (HANDLE) _get_osfhandle (scb->fd);
206 if (GetCommState (h, &state) == 0)
209 state.BaudRate = rate;
211 return (SetCommState (h, &state) != 0) ? 0 : -1;
215 ser_windows_close (struct serial *scb)
217 struct ser_windows_state *state;
219 /* Stop any pending selects. */
220 CancelIo ((HANDLE) _get_osfhandle (scb->fd));
222 CloseHandle (state->ov.hEvent);
223 CloseHandle (state->except_event);
235 ser_windows_wait_handle (struct serial *scb, HANDLE *read, HANDLE *except)
237 struct ser_windows_state *state;
240 HANDLE h = (HANDLE) _get_osfhandle (scb->fd);
244 *except = state->except_event;
245 *read = state->ov.hEvent;
247 if (state->in_progress)
250 /* Reset the mask - we are only interested in any characters which
251 arrive after this point, not characters which might have arrived
252 and already been read. */
254 /* This really, really shouldn't be necessary - just the second one.
255 But otherwise an internal flag for EV_RXCHAR does not get
256 cleared, and we get a duplicated event, if the last batch
257 of characters included at least two arriving close together. */
258 if (!SetCommMask (h, 0))
259 warning (_("ser_windows_wait_handle: reseting mask failed"));
261 if (!SetCommMask (h, EV_RXCHAR))
262 warning (_("ser_windows_wait_handle: reseting mask failed (2)"));
264 /* There's a potential race condition here; we must check cbInQue
265 and not wait if that's nonzero. */
267 ClearCommError (h, &errors, &status);
268 if (status.cbInQue > 0)
270 SetEvent (state->ov.hEvent);
274 state->in_progress = 1;
275 ResetEvent (state->ov.hEvent);
276 state->lastCommMask = -2;
277 if (WaitCommEvent (h, &state->lastCommMask, &state->ov))
279 gdb_assert (state->lastCommMask & EV_RXCHAR);
280 SetEvent (state->ov.hEvent);
283 gdb_assert (GetLastError () == ERROR_IO_PENDING);
287 ser_windows_read_prim (struct serial *scb, size_t count)
289 struct ser_windows_state *state;
291 DWORD bytes_read, bytes_read_tmp;
296 if (state->in_progress)
298 WaitForSingleObject (state->ov.hEvent, INFINITE);
299 state->in_progress = 0;
300 ResetEvent (state->ov.hEvent);
303 memset (&ov, 0, sizeof (OVERLAPPED));
304 ov.hEvent = CreateEvent (0, FALSE, FALSE, 0);
305 h = (HANDLE) _get_osfhandle (scb->fd);
307 if (!ReadFile (h, scb->buf, /* count */ 1, &bytes_read, &ov))
309 if (GetLastError () != ERROR_IO_PENDING
310 || !GetOverlappedResult (h, &ov, &bytes_read, TRUE))
314 CloseHandle (ov.hEvent);
319 ser_windows_write_prim (struct serial *scb, const void *buf, size_t len)
321 struct ser_windows_state *state;
326 memset (&ov, 0, sizeof (OVERLAPPED));
327 ov.hEvent = CreateEvent (0, FALSE, FALSE, 0);
328 h = (HANDLE) _get_osfhandle (scb->fd);
329 if (!WriteFile (h, buf, len, &bytes_written, &ov))
331 if (GetLastError () != ERROR_IO_PENDING
332 || !GetOverlappedResult (h, &ov, &bytes_written, TRUE))
336 CloseHandle (ov.hEvent);
337 return bytes_written;
340 /* On Windows, gdb_select is implemented using WaitForMulpleObjects.
341 A "select thread" is created for each file descriptor. These
342 threads looks for activity on the corresponding descriptor, using
343 whatever techniques are appropriate for the descriptor type. When
344 that activity occurs, the thread signals an appropriate event,
345 which wakes up WaitForMultipleObjects.
347 Each select thread is in one of two states: stopped or started.
348 Select threads begin in the stopped state. When gdb_select is
349 called, threads corresponding to the descriptors of interest are
350 started by calling a wait_handle function. Each thread that
351 notices activity signals the appropriate event and then reenters
352 the stopped state. Before gdb_select returns it calls the
353 wait_handle_done functions, which return the threads to the stopped
356 enum select_thread_state {
361 struct ser_console_state
363 /* Signaled by the select thread to indicate that data is available
364 on the file descriptor. */
366 /* Signaled by the select thread to indicate that an exception has
367 occurred on the file descriptor. */
369 /* Signaled by the select thread to indicate that it has entered the
370 started state. HAVE_STARTED and HAVE_STOPPED are never signaled
373 /* Signaled by the select thread to indicate that it has stopped,
374 either because data is available (and READ_EVENT is signaled),
375 because an exception has occurred (and EXCEPT_EVENT is signaled),
376 or because STOP_SELECT was signaled. */
379 /* Signaled by the main program to tell the select thread to enter
380 the started state. */
382 /* Signaled by the main program to tell the select thread to enter
383 the stopped state. */
385 /* Signaled by the main program to tell the select thread to
389 /* The handle for the select thread. */
391 /* The state of the select thread. This field is only accessed in
392 the main program, never by the select thread itself. */
393 enum select_thread_state thread_state;
396 /* Called by a select thread to enter the stopped state. This
397 function does not return until the thread has re-entered the
400 select_thread_wait (struct ser_console_state *state)
402 HANDLE wait_events[2];
404 /* There are two things that can wake us up: a request that we enter
405 the started state, or that we exit this thread. */
406 wait_events[0] = state->start_select;
407 wait_events[1] = state->exit_select;
408 if (WaitForMultipleObjects (2, wait_events, FALSE, INFINITE)
410 /* Either the EXIT_SELECT event was signaled (requesting that the
411 thread exit) or an error has occurred. In either case, we exit
415 /* We are now in the started state. */
416 SetEvent (state->have_started);
419 typedef DWORD WINAPI (*thread_fn_type)(void *);
421 /* Create a new select thread for SCB executing THREAD_FN. The STATE
422 will be filled in by this function before return. */
424 create_select_thread (thread_fn_type thread_fn,
426 struct ser_console_state *state)
430 /* Create all of the events. These are all auto-reset events. */
431 state->read_event = CreateEvent (NULL, FALSE, FALSE, NULL);
432 state->except_event = CreateEvent (NULL, FALSE, FALSE, NULL);
433 state->have_started = CreateEvent (NULL, FALSE, FALSE, NULL);
434 state->have_stopped = CreateEvent (NULL, FALSE, FALSE, NULL);
435 state->start_select = CreateEvent (NULL, FALSE, FALSE, NULL);
436 state->stop_select = CreateEvent (NULL, FALSE, FALSE, NULL);
437 state->exit_select = CreateEvent (NULL, FALSE, FALSE, NULL);
439 state->thread = CreateThread (NULL, 0, thread_fn, scb, 0, &threadId);
440 /* The thread begins in the stopped state. */
441 state->thread_state = STS_STOPPED;
444 /* Destroy the select thread indicated by STATE. */
446 destroy_select_thread (struct ser_console_state *state)
448 /* Ask the thread to exit. */
449 SetEvent (state->exit_select);
450 /* Wait until it does. */
451 WaitForSingleObject (state->thread, INFINITE);
453 /* Destroy the events. */
454 CloseHandle (state->read_event);
455 CloseHandle (state->except_event);
456 CloseHandle (state->have_started);
457 CloseHandle (state->have_stopped);
458 CloseHandle (state->start_select);
459 CloseHandle (state->stop_select);
460 CloseHandle (state->exit_select);
463 /* Called by gdb_select to start the select thread indicated by STATE.
464 This function does not return until the thread has started. */
466 start_select_thread (struct ser_console_state *state)
468 /* Ask the thread to start. */
469 SetEvent (state->start_select);
470 /* Wait until it does. */
471 WaitForSingleObject (state->have_started, INFINITE);
472 /* The thread is now started. */
473 state->thread_state = STS_STARTED;
476 /* Called by gdb_select to stop the select thread indicated by STATE.
477 This function does not return until the thread has stopped. */
479 stop_select_thread (struct ser_console_state *state)
481 /* If the thread is already in the stopped state, we have nothing to
482 do. Some of the wait_handle functions avoid calling
483 start_select_thread if they notice activity on the relevant file
484 descriptors. The wait_handle_done functions still call
485 stop_select_thread -- but it is already stopped. */
486 if (state->thread_state != STS_STARTED)
488 /* Ask the thread to stop. */
489 SetEvent (state->stop_select);
490 /* Wait until it does. */
491 WaitForSingleObject (state->have_stopped, INFINITE);
492 /* The thread is now stopped. */
493 state->thread_state = STS_STOPPED;
497 console_select_thread (void *arg)
499 struct serial *scb = arg;
500 struct ser_console_state *state;
505 h = (HANDLE) _get_osfhandle (scb->fd);
509 HANDLE wait_events[2];
513 select_thread_wait (state);
517 wait_events[0] = state->stop_select;
520 event_index = WaitForMultipleObjects (2, wait_events, FALSE, INFINITE);
522 if (event_index == WAIT_OBJECT_0
523 || WaitForSingleObject (state->stop_select, 0) == WAIT_OBJECT_0)
526 if (event_index != WAIT_OBJECT_0 + 1)
528 /* Wait must have failed; assume an error has occured, e.g.
529 the handle has been closed. */
530 SetEvent (state->except_event);
534 /* We've got a pending event on the console. See if it's
536 if (!PeekConsoleInput (h, &record, 1, &n_records) || n_records != 1)
538 /* Something went wrong. Maybe the console is gone. */
539 SetEvent (state->except_event);
543 if (record.EventType == KEY_EVENT && record.Event.KeyEvent.bKeyDown)
545 WORD keycode = record.Event.KeyEvent.wVirtualKeyCode;
547 /* Ignore events containing only control keys. We must
548 recognize "enhanced" keys which we are interested in
549 reading via getch, if they do not map to ASCII. But we
550 do not want to report input available for e.g. the
551 control key alone. */
553 if (record.Event.KeyEvent.uChar.AsciiChar != 0
554 || keycode == VK_PRIOR
555 || keycode == VK_NEXT
557 || keycode == VK_HOME
558 || keycode == VK_LEFT
560 || keycode == VK_RIGHT
561 || keycode == VK_DOWN
562 || keycode == VK_INSERT
563 || keycode == VK_DELETE)
565 /* This is really a keypress. */
566 SetEvent (state->read_event);
571 /* Otherwise discard it and wait again. */
572 ReadConsoleInput (h, &record, 1, &n_records);
575 SetEvent(state->have_stopped);
583 if (PeekNamedPipe ((HANDLE) _get_osfhandle (fd), NULL, 0, NULL, NULL, NULL))
592 if (GetFileType ((HANDLE) _get_osfhandle (fd)) == FILE_TYPE_DISK)
599 pipe_select_thread (void *arg)
601 struct serial *scb = arg;
602 struct ser_console_state *state;
607 h = (HANDLE) _get_osfhandle (scb->fd);
613 select_thread_wait (state);
615 /* Wait for something to happen on the pipe. */
618 if (!PeekNamedPipe (h, NULL, 0, NULL, &n_avail, NULL))
620 SetEvent (state->except_event);
626 SetEvent (state->read_event);
630 /* Delay 10ms before checking again, but allow the stop
632 if (WaitForSingleObject (state->stop_select, 10) == WAIT_OBJECT_0)
636 SetEvent (state->have_stopped);
642 file_select_thread (void *arg)
644 struct serial *scb = arg;
645 struct ser_console_state *state;
650 h = (HANDLE) _get_osfhandle (scb->fd);
654 select_thread_wait (state);
656 if (SetFilePointer (h, 0, NULL, FILE_CURRENT) == INVALID_SET_FILE_POINTER)
657 SetEvent (state->except_event);
659 SetEvent (state->read_event);
661 SetEvent (state->have_stopped);
667 ser_console_wait_handle (struct serial *scb, HANDLE *read, HANDLE *except)
669 struct ser_console_state *state = scb->state;
673 thread_fn_type thread_fn;
676 is_tty = isatty (scb->fd);
677 if (!is_tty && !fd_is_file (scb->fd) && !fd_is_pipe (scb->fd))
684 state = xmalloc (sizeof (struct ser_console_state));
685 memset (state, 0, sizeof (struct ser_console_state));
689 thread_fn = console_select_thread;
690 else if (fd_is_pipe (scb->fd))
691 thread_fn = pipe_select_thread;
693 thread_fn = file_select_thread;
695 create_select_thread (thread_fn, scb, state);
698 *read = state->read_event;
699 *except = state->except_event;
701 /* Start from a blank state. */
702 ResetEvent (state->read_event);
703 ResetEvent (state->except_event);
704 ResetEvent (state->stop_select);
706 /* First check for a key already in the buffer. If there is one,
707 we don't need a thread. This also catches the second key of
708 multi-character returns from getch, for instance for arrow
709 keys. The second half is in a C library internal buffer,
710 and PeekConsoleInput will not find it. */
713 SetEvent (state->read_event);
717 /* Otherwise, start the select thread. */
718 start_select_thread (state);
722 ser_console_done_wait_handle (struct serial *scb)
724 struct ser_console_state *state = scb->state;
729 stop_select_thread (state);
733 ser_console_close (struct serial *scb)
735 struct ser_console_state *state = scb->state;
739 destroy_select_thread (state);
744 struct ser_console_ttystate
749 static serial_ttystate
750 ser_console_get_tty_state (struct serial *scb)
752 if (isatty (scb->fd))
754 struct ser_console_ttystate *state;
756 state = (struct ser_console_ttystate *) xmalloc (sizeof *state);
766 /* Since we use the pipe_select_thread for our select emulation,
767 we need to place the state structure it requires at the front
769 struct ser_console_state wait;
771 /* The pex obj for our (one-stage) pipeline. */
774 /* Streams for the pipeline's input and output. */
775 FILE *input, *output;
778 static struct pipe_state *
779 make_pipe_state (void)
781 struct pipe_state *ps = XMALLOC (struct pipe_state);
783 memset (ps, 0, sizeof (*ps));
784 ps->wait.read_event = INVALID_HANDLE_VALUE;
785 ps->wait.except_event = INVALID_HANDLE_VALUE;
786 ps->wait.start_select = INVALID_HANDLE_VALUE;
787 ps->wait.stop_select = INVALID_HANDLE_VALUE;
793 free_pipe_state (struct pipe_state *ps)
795 int saved_errno = errno;
797 if (ps->wait.read_event != INVALID_HANDLE_VALUE)
798 destroy_select_thread (&ps->wait);
800 /* Close the pipe to the child. We must close the pipe before
801 calling pex_free because pex_free will wait for the child to exit
802 and the child will not exit until the pipe is closed. */
808 /* pex_free closes ps->output. */
819 cleanup_pipe_state (void *untyped)
821 struct pipe_state *ps = untyped;
823 free_pipe_state (ps);
827 pipe_windows_open (struct serial *scb, const char *name)
829 struct pipe_state *ps;
832 struct cleanup *back_to;
835 error_no_arg (_("child command"));
837 argv = gdb_buildargv (name);
838 back_to = make_cleanup_freeargv (argv);
840 if (! argv[0] || argv[0][0] == '\0')
841 error ("missing child command");
843 ps = make_pipe_state ();
844 make_cleanup (cleanup_pipe_state, ps);
846 ps->pex = pex_init (PEX_USE_PIPES, "target remote pipe", NULL);
849 ps->input = pex_input_pipe (ps->pex, 1);
856 = pex_run (ps->pex, PEX_SEARCH | PEX_BINARY_INPUT | PEX_BINARY_OUTPUT
857 | PEX_STDERR_TO_PIPE,
858 argv[0], argv, NULL, NULL,
863 /* Our caller expects us to return -1, but all they'll do with
864 it generally is print the message based on errno. We have
865 all the same information here, plus err_msg provided by
866 pex_run, so we just raise the error here. */
868 error ("error starting child process '%s': %s: %s",
869 name, err_msg, safe_strerror (err));
871 error ("error starting child process '%s': %s",
876 ps->output = pex_read_output (ps->pex, 1);
879 scb->fd = fileno (ps->output);
881 pex_stderr = pex_read_err (ps->pex, 1);
884 scb->error_fd = fileno (pex_stderr);
886 scb->state = (void *) ps;
888 discard_cleanups (back_to);
892 do_cleanups (back_to);
897 pipe_windows_fdopen (struct serial *scb, int fd)
899 struct pipe_state *ps;
901 ps = make_pipe_state ();
903 ps->input = fdopen (fd, "r+");
907 ps->output = fdopen (fd, "r+");
912 scb->state = (void *) ps;
917 free_pipe_state (ps);
922 pipe_windows_close (struct serial *scb)
924 struct pipe_state *ps = scb->state;
926 /* In theory, we should try to kill the subprocess here, but the pex
927 interface doesn't give us enough information to do that. Usually
928 closing the input pipe will get the message across. */
930 free_pipe_state (ps);
935 pipe_windows_read (struct serial *scb, size_t count)
937 HANDLE pipeline_out = (HANDLE) _get_osfhandle (scb->fd);
941 if (pipeline_out == INVALID_HANDLE_VALUE)
944 if (! PeekNamedPipe (pipeline_out, NULL, 0, NULL, &available, NULL))
947 if (count > available)
950 if (! ReadFile (pipeline_out, scb->buf, count, &bytes_read, NULL))
958 pipe_windows_write (struct serial *scb, const void *buf, size_t count)
960 struct pipe_state *ps = scb->state;
964 int pipeline_in_fd = fileno (ps->input);
965 if (pipeline_in_fd < 0)
968 pipeline_in = (HANDLE) _get_osfhandle (pipeline_in_fd);
969 if (pipeline_in == INVALID_HANDLE_VALUE)
972 if (! WriteFile (pipeline_in, buf, count, &written, NULL))
980 pipe_wait_handle (struct serial *scb, HANDLE *read, HANDLE *except)
982 struct pipe_state *ps = scb->state;
984 /* Have we allocated our events yet? */
985 if (ps->wait.read_event == INVALID_HANDLE_VALUE)
986 /* Start the thread. */
987 create_select_thread (pipe_select_thread, scb, &ps->wait);
989 *read = ps->wait.read_event;
990 *except = ps->wait.except_event;
992 /* Start from a blank state. */
993 ResetEvent (ps->wait.read_event);
994 ResetEvent (ps->wait.except_event);
995 ResetEvent (ps->wait.stop_select);
997 start_select_thread (&ps->wait);
1001 pipe_done_wait_handle (struct serial *scb)
1003 struct pipe_state *ps = scb->state;
1005 /* Have we allocated our events yet? */
1006 if (ps->wait.read_event == INVALID_HANDLE_VALUE)
1009 stop_select_thread (&ps->wait);
1013 pipe_avail (struct serial *scb, int fd)
1015 HANDLE h = (HANDLE) _get_osfhandle (fd);
1017 BOOL r = PeekNamedPipe (h, NULL, 0, NULL, &numBytes, NULL);
1025 gdb_pipe (int pdes[2])
1027 if (_pipe (pdes, 512, _O_BINARY | _O_NOINHERIT) == -1)
1032 struct net_windows_state
1034 struct ser_console_state base;
1040 net_windows_select_thread (void *arg)
1042 struct serial *scb = arg;
1043 struct net_windows_state *state;
1050 HANDLE wait_events[2];
1051 WSANETWORKEVENTS events;
1053 select_thread_wait (&state->base);
1055 wait_events[0] = state->base.stop_select;
1056 wait_events[1] = state->sock_event;
1058 event_index = WaitForMultipleObjects (2, wait_events, FALSE, INFINITE);
1060 if (event_index == WAIT_OBJECT_0
1061 || WaitForSingleObject (state->base.stop_select, 0) == WAIT_OBJECT_0)
1062 /* We have been requested to stop. */
1064 else if (event_index != WAIT_OBJECT_0 + 1)
1065 /* Some error has occured. Assume that this is an error
1067 SetEvent (state->base.except_event);
1070 /* Enumerate the internal network events, and reset the
1071 object that signalled us to catch the next event. */
1072 WSAEnumNetworkEvents (scb->fd, state->sock_event, &events);
1074 gdb_assert (events.lNetworkEvents & (FD_READ | FD_CLOSE));
1076 if (events.lNetworkEvents & FD_READ)
1077 SetEvent (state->base.read_event);
1079 if (events.lNetworkEvents & FD_CLOSE)
1080 SetEvent (state->base.except_event);
1083 SetEvent (state->base.have_stopped);
1088 net_windows_wait_handle (struct serial *scb, HANDLE *read, HANDLE *except)
1090 struct net_windows_state *state = scb->state;
1092 /* Start from a clean slate. */
1093 ResetEvent (state->base.read_event);
1094 ResetEvent (state->base.except_event);
1095 ResetEvent (state->base.stop_select);
1097 *read = state->base.read_event;
1098 *except = state->base.except_event;
1100 /* Check any pending events. This both avoids starting the thread
1101 unnecessarily, and handles stray FD_READ events (see below). */
1102 if (WaitForSingleObject (state->sock_event, 0) == WAIT_OBJECT_0)
1104 WSANETWORKEVENTS events;
1107 /* Enumerate the internal network events, and reset the object that
1108 signalled us to catch the next event. */
1109 WSAEnumNetworkEvents (scb->fd, state->sock_event, &events);
1111 /* You'd think that FD_READ or FD_CLOSE would be set here. But,
1112 sometimes, neither is. I suspect that the FD_READ is set and
1113 the corresponding event signalled while recv is running, and
1114 the FD_READ is then lowered when recv consumes all the data,
1115 but there's no way to un-signal the event. This isn't a
1116 problem for the call in net_select_thread, since any new
1117 events after this point will not have been drained by recv.
1118 It just means that we can't have the obvious assert here. */
1120 /* If there is a read event, it might be still valid, or it might
1121 not be - it may have been signalled before we last called
1122 recv. Double-check that there is data. */
1123 if (events.lNetworkEvents & FD_READ)
1125 unsigned long available;
1127 if (ioctlsocket (scb->fd, FIONREAD, &available) == 0
1130 SetEvent (state->base.read_event);
1134 /* Oops, no data. This call to recv will cause future
1135 data to retrigger the event, e.g. while we are
1136 in net_select_thread. */
1137 recv (scb->fd, NULL, 0, 0);
1140 /* If there's a close event, then record it - it is obviously
1141 still valid, and it will not be resignalled. */
1142 if (events.lNetworkEvents & FD_CLOSE)
1144 SetEvent (state->base.except_event);
1148 /* If we set either handle, there's no need to wake the thread. */
1153 start_select_thread (&state->base);
1157 net_windows_done_wait_handle (struct serial *scb)
1159 struct net_windows_state *state = scb->state;
1161 stop_select_thread (&state->base);
1165 net_windows_open (struct serial *scb, const char *name)
1167 struct net_windows_state *state;
1171 ret = net_open (scb, name);
1175 state = xmalloc (sizeof (struct net_windows_state));
1176 memset (state, 0, sizeof (struct net_windows_state));
1179 /* Associate an event with the socket. */
1180 state->sock_event = CreateEvent (0, TRUE, FALSE, 0);
1181 WSAEventSelect (scb->fd, state->sock_event, FD_READ | FD_CLOSE);
1183 /* Start the thread. */
1184 create_select_thread (net_windows_select_thread, scb, &state->base);
1191 net_windows_close (struct serial *scb)
1193 struct net_windows_state *state = scb->state;
1195 destroy_select_thread (&state->base);
1196 CloseHandle (state->sock_event);
1204 _initialize_ser_windows (void)
1207 struct serial_ops *ops;
1209 /* First register the serial port driver. */
1211 ops = XMALLOC (struct serial_ops);
1212 memset (ops, 0, sizeof (struct serial_ops));
1213 ops->name = "hardwire";
1215 ops->open = ser_windows_open;
1216 ops->close = ser_windows_close;
1218 ops->flush_output = ser_windows_flush_output;
1219 ops->flush_input = ser_windows_flush_input;
1220 ops->send_break = ser_windows_send_break;
1222 /* These are only used for stdin; we do not need them for serial
1223 ports, so supply the standard dummies. */
1224 ops->get_tty_state = ser_base_get_tty_state;
1225 ops->set_tty_state = ser_base_set_tty_state;
1226 ops->print_tty_state = ser_base_print_tty_state;
1227 ops->noflush_set_tty_state = ser_base_noflush_set_tty_state;
1229 ops->go_raw = ser_windows_raw;
1230 ops->setbaudrate = ser_windows_setbaudrate;
1231 ops->setstopbits = ser_windows_setstopbits;
1232 ops->drain_output = ser_windows_drain_output;
1233 ops->readchar = ser_base_readchar;
1234 ops->write = ser_base_write;
1235 ops->async = ser_base_async;
1236 ops->read_prim = ser_windows_read_prim;
1237 ops->write_prim = ser_windows_write_prim;
1238 ops->wait_handle = ser_windows_wait_handle;
1240 serial_add_interface (ops);
1242 /* Next create the dummy serial driver used for terminals. We only
1243 provide the TTY-related methods. */
1245 ops = XMALLOC (struct serial_ops);
1246 memset (ops, 0, sizeof (struct serial_ops));
1248 ops->name = "terminal";
1251 ops->close = ser_console_close;
1252 ops->get_tty_state = ser_console_get_tty_state;
1253 ops->set_tty_state = ser_base_set_tty_state;
1254 ops->print_tty_state = ser_base_print_tty_state;
1255 ops->noflush_set_tty_state = ser_base_noflush_set_tty_state;
1256 ops->drain_output = ser_base_drain_output;
1257 ops->wait_handle = ser_console_wait_handle;
1258 ops->done_wait_handle = ser_console_done_wait_handle;
1260 serial_add_interface (ops);
1262 /* The pipe interface. */
1264 ops = XMALLOC (struct serial_ops);
1265 memset (ops, 0, sizeof (struct serial_ops));
1268 ops->open = pipe_windows_open;
1269 ops->close = pipe_windows_close;
1270 ops->fdopen = pipe_windows_fdopen;
1271 ops->readchar = ser_base_readchar;
1272 ops->write = ser_base_write;
1273 ops->flush_output = ser_base_flush_output;
1274 ops->flush_input = ser_base_flush_input;
1275 ops->send_break = ser_base_send_break;
1276 ops->go_raw = ser_base_raw;
1277 ops->get_tty_state = ser_base_get_tty_state;
1278 ops->set_tty_state = ser_base_set_tty_state;
1279 ops->print_tty_state = ser_base_print_tty_state;
1280 ops->noflush_set_tty_state = ser_base_noflush_set_tty_state;
1281 ops->setbaudrate = ser_base_setbaudrate;
1282 ops->setstopbits = ser_base_setstopbits;
1283 ops->drain_output = ser_base_drain_output;
1284 ops->async = ser_base_async;
1285 ops->read_prim = pipe_windows_read;
1286 ops->write_prim = pipe_windows_write;
1287 ops->wait_handle = pipe_wait_handle;
1288 ops->done_wait_handle = pipe_done_wait_handle;
1289 ops->avail = pipe_avail;
1291 serial_add_interface (ops);
1293 /* If WinSock works, register the TCP/UDP socket driver. */
1295 if (WSAStartup (MAKEWORD (1, 0), &wsa_data) != 0)
1296 /* WinSock is unavailable. */
1299 ops = XMALLOC (struct serial_ops);
1300 memset (ops, 0, sizeof (struct serial_ops));
1303 ops->open = net_windows_open;
1304 ops->close = net_windows_close;
1305 ops->readchar = ser_base_readchar;
1306 ops->write = ser_base_write;
1307 ops->flush_output = ser_base_flush_output;
1308 ops->flush_input = ser_base_flush_input;
1309 ops->send_break = ser_tcp_send_break;
1310 ops->go_raw = ser_base_raw;
1311 ops->get_tty_state = ser_base_get_tty_state;
1312 ops->set_tty_state = ser_base_set_tty_state;
1313 ops->print_tty_state = ser_base_print_tty_state;
1314 ops->noflush_set_tty_state = ser_base_noflush_set_tty_state;
1315 ops->setbaudrate = ser_base_setbaudrate;
1316 ops->setstopbits = ser_base_setstopbits;
1317 ops->drain_output = ser_base_drain_output;
1318 ops->async = ser_base_async;
1319 ops->read_prim = net_read_prim;
1320 ops->write_prim = net_write_prim;
1321 ops->wait_handle = net_windows_wait_handle;
1322 ops->done_wait_handle = net_windows_done_wait_handle;
1323 serial_add_interface (ops);