1 /* Serial interface for local (hardwired) serial ports on Windows systems
3 Copyright (C) 2006-2014 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 = xmalloc (sizeof (struct ser_windows_state));
91 memset (state, 0, sizeof (struct ser_windows_state));
94 /* Create a manual reset event to watch the input buffer. */
95 state->ov.hEvent = CreateEvent (0, TRUE, FALSE, 0);
97 /* Create a (currently unused) handle to record exceptions. */
98 state->except_event = CreateEvent (0, TRUE, FALSE, 0);
103 /* Wait for the output to drain away, as opposed to flushing (discarding)
107 ser_windows_drain_output (struct serial *scb)
109 HANDLE h = (HANDLE) _get_osfhandle (scb->fd);
111 return (FlushFileBuffers (h) != 0) ? 0 : -1;
115 ser_windows_flush_output (struct serial *scb)
117 HANDLE h = (HANDLE) _get_osfhandle (scb->fd);
119 return (PurgeComm (h, PURGE_TXCLEAR) != 0) ? 0 : -1;
123 ser_windows_flush_input (struct serial *scb)
125 HANDLE h = (HANDLE) _get_osfhandle (scb->fd);
127 return (PurgeComm (h, PURGE_RXCLEAR) != 0) ? 0 : -1;
131 ser_windows_send_break (struct serial *scb)
133 HANDLE h = (HANDLE) _get_osfhandle (scb->fd);
135 if (SetCommBreak (h) == 0)
138 /* Delay for 250 milliseconds. */
141 if (ClearCommBreak (h))
148 ser_windows_raw (struct serial *scb)
150 HANDLE h = (HANDLE) _get_osfhandle (scb->fd);
153 if (GetCommState (h, &state) == 0)
156 state.fParity = FALSE;
157 state.fOutxCtsFlow = FALSE;
158 state.fOutxDsrFlow = FALSE;
159 state.fDtrControl = DTR_CONTROL_ENABLE;
160 state.fDsrSensitivity = FALSE;
164 state.fAbortOnError = FALSE;
166 state.Parity = NOPARITY;
168 scb->current_timeout = 0;
170 if (SetCommState (h, &state) == 0)
171 warning (_("SetCommState failed"));
175 ser_windows_setstopbits (struct serial *scb, int num)
177 HANDLE h = (HANDLE) _get_osfhandle (scb->fd);
180 if (GetCommState (h, &state) == 0)
185 case SERIAL_1_STOPBITS:
186 state.StopBits = ONESTOPBIT;
188 case SERIAL_1_AND_A_HALF_STOPBITS:
189 state.StopBits = ONE5STOPBITS;
191 case SERIAL_2_STOPBITS:
192 state.StopBits = TWOSTOPBITS;
198 return (SetCommState (h, &state) != 0) ? 0 : -1;
202 ser_windows_setbaudrate (struct serial *scb, int rate)
204 HANDLE h = (HANDLE) _get_osfhandle (scb->fd);
207 if (GetCommState (h, &state) == 0)
210 state.BaudRate = rate;
212 return (SetCommState (h, &state) != 0) ? 0 : -1;
216 ser_windows_close (struct serial *scb)
218 struct ser_windows_state *state;
220 /* Stop any pending selects. On Windows 95 OS, CancelIo function does
221 not exist. In that case, it can be replaced by a call to CloseHandle,
222 but this is not necessary here as we do close the Windows handle
223 by calling close (scb->fd) below. */
225 CancelIo ((HANDLE) _get_osfhandle (scb->fd));
227 CloseHandle (state->ov.hEvent);
228 CloseHandle (state->except_event);
240 ser_windows_wait_handle (struct serial *scb, HANDLE *read, HANDLE *except)
242 struct ser_windows_state *state;
245 HANDLE h = (HANDLE) _get_osfhandle (scb->fd);
249 *except = state->except_event;
250 *read = state->ov.hEvent;
252 if (state->in_progress)
255 /* Reset the mask - we are only interested in any characters which
256 arrive after this point, not characters which might have arrived
257 and already been read. */
259 /* This really, really shouldn't be necessary - just the second one.
260 But otherwise an internal flag for EV_RXCHAR does not get
261 cleared, and we get a duplicated event, if the last batch
262 of characters included at least two arriving close together. */
263 if (!SetCommMask (h, 0))
264 warning (_("ser_windows_wait_handle: reseting mask failed"));
266 if (!SetCommMask (h, EV_RXCHAR))
267 warning (_("ser_windows_wait_handle: reseting mask failed (2)"));
269 /* There's a potential race condition here; we must check cbInQue
270 and not wait if that's nonzero. */
272 ClearCommError (h, &errors, &status);
273 if (status.cbInQue > 0)
275 SetEvent (state->ov.hEvent);
279 state->in_progress = 1;
280 ResetEvent (state->ov.hEvent);
281 state->lastCommMask = -2;
282 if (WaitCommEvent (h, &state->lastCommMask, &state->ov))
284 gdb_assert (state->lastCommMask & EV_RXCHAR);
285 SetEvent (state->ov.hEvent);
288 gdb_assert (GetLastError () == ERROR_IO_PENDING);
292 ser_windows_read_prim (struct serial *scb, size_t count)
294 struct ser_windows_state *state;
296 DWORD bytes_read, bytes_read_tmp;
301 if (state->in_progress)
303 WaitForSingleObject (state->ov.hEvent, INFINITE);
304 state->in_progress = 0;
305 ResetEvent (state->ov.hEvent);
308 memset (&ov, 0, sizeof (OVERLAPPED));
309 ov.hEvent = CreateEvent (0, FALSE, FALSE, 0);
310 h = (HANDLE) _get_osfhandle (scb->fd);
312 if (!ReadFile (h, scb->buf, /* count */ 1, &bytes_read, &ov))
314 if (GetLastError () != ERROR_IO_PENDING
315 || !GetOverlappedResult (h, &ov, &bytes_read, TRUE))
319 CloseHandle (ov.hEvent);
324 ser_windows_write_prim (struct serial *scb, const void *buf, size_t len)
326 struct ser_windows_state *state;
331 memset (&ov, 0, sizeof (OVERLAPPED));
332 ov.hEvent = CreateEvent (0, FALSE, FALSE, 0);
333 h = (HANDLE) _get_osfhandle (scb->fd);
334 if (!WriteFile (h, buf, len, &bytes_written, &ov))
336 if (GetLastError () != ERROR_IO_PENDING
337 || !GetOverlappedResult (h, &ov, &bytes_written, TRUE))
341 CloseHandle (ov.hEvent);
342 return bytes_written;
345 /* On Windows, gdb_select is implemented using WaitForMulpleObjects.
346 A "select thread" is created for each file descriptor. These
347 threads looks for activity on the corresponding descriptor, using
348 whatever techniques are appropriate for the descriptor type. When
349 that activity occurs, the thread signals an appropriate event,
350 which wakes up WaitForMultipleObjects.
352 Each select thread is in one of two states: stopped or started.
353 Select threads begin in the stopped state. When gdb_select is
354 called, threads corresponding to the descriptors of interest are
355 started by calling a wait_handle function. Each thread that
356 notices activity signals the appropriate event and then reenters
357 the stopped state. Before gdb_select returns it calls the
358 wait_handle_done functions, which return the threads to the stopped
361 enum select_thread_state {
366 struct ser_console_state
368 /* Signaled by the select thread to indicate that data is available
369 on the file descriptor. */
371 /* Signaled by the select thread to indicate that an exception has
372 occurred on the file descriptor. */
374 /* Signaled by the select thread to indicate that it has entered the
375 started state. HAVE_STARTED and HAVE_STOPPED are never signaled
378 /* Signaled by the select thread to indicate that it has stopped,
379 either because data is available (and READ_EVENT is signaled),
380 because an exception has occurred (and EXCEPT_EVENT is signaled),
381 or because STOP_SELECT was signaled. */
384 /* Signaled by the main program to tell the select thread to enter
385 the started state. */
387 /* Signaled by the main program to tell the select thread to enter
388 the stopped state. */
390 /* Signaled by the main program to tell the select thread to
394 /* The handle for the select thread. */
396 /* The state of the select thread. This field is only accessed in
397 the main program, never by the select thread itself. */
398 enum select_thread_state thread_state;
401 /* Called by a select thread to enter the stopped state. This
402 function does not return until the thread has re-entered the
405 select_thread_wait (struct ser_console_state *state)
407 HANDLE wait_events[2];
409 /* There are two things that can wake us up: a request that we enter
410 the started state, or that we exit this thread. */
411 wait_events[0] = state->start_select;
412 wait_events[1] = state->exit_select;
413 if (WaitForMultipleObjects (2, wait_events, FALSE, INFINITE)
415 /* Either the EXIT_SELECT event was signaled (requesting that the
416 thread exit) or an error has occurred. In either case, we exit
420 /* We are now in the started state. */
421 SetEvent (state->have_started);
424 typedef DWORD WINAPI (*thread_fn_type)(void *);
426 /* Create a new select thread for SCB executing THREAD_FN. The STATE
427 will be filled in by this function before return. */
429 create_select_thread (thread_fn_type thread_fn,
431 struct ser_console_state *state)
435 /* Create all of the events. These are all auto-reset events. */
436 state->read_event = CreateEvent (NULL, FALSE, FALSE, NULL);
437 state->except_event = CreateEvent (NULL, FALSE, FALSE, NULL);
438 state->have_started = CreateEvent (NULL, FALSE, FALSE, NULL);
439 state->have_stopped = CreateEvent (NULL, FALSE, FALSE, NULL);
440 state->start_select = CreateEvent (NULL, FALSE, FALSE, NULL);
441 state->stop_select = CreateEvent (NULL, FALSE, FALSE, NULL);
442 state->exit_select = CreateEvent (NULL, FALSE, FALSE, NULL);
444 state->thread = CreateThread (NULL, 0, thread_fn, scb, 0, &threadId);
445 /* The thread begins in the stopped state. */
446 state->thread_state = STS_STOPPED;
449 /* Destroy the select thread indicated by STATE. */
451 destroy_select_thread (struct ser_console_state *state)
453 /* Ask the thread to exit. */
454 SetEvent (state->exit_select);
455 /* Wait until it does. */
456 WaitForSingleObject (state->thread, INFINITE);
458 /* Destroy the events. */
459 CloseHandle (state->read_event);
460 CloseHandle (state->except_event);
461 CloseHandle (state->have_started);
462 CloseHandle (state->have_stopped);
463 CloseHandle (state->start_select);
464 CloseHandle (state->stop_select);
465 CloseHandle (state->exit_select);
468 /* Called by gdb_select to start the select thread indicated by STATE.
469 This function does not return until the thread has started. */
471 start_select_thread (struct ser_console_state *state)
473 /* Ask the thread to start. */
474 SetEvent (state->start_select);
475 /* Wait until it does. */
476 WaitForSingleObject (state->have_started, INFINITE);
477 /* The thread is now started. */
478 state->thread_state = STS_STARTED;
481 /* Called by gdb_select to stop the select thread indicated by STATE.
482 This function does not return until the thread has stopped. */
484 stop_select_thread (struct ser_console_state *state)
486 /* If the thread is already in the stopped state, we have nothing to
487 do. Some of the wait_handle functions avoid calling
488 start_select_thread if they notice activity on the relevant file
489 descriptors. The wait_handle_done functions still call
490 stop_select_thread -- but it is already stopped. */
491 if (state->thread_state != STS_STARTED)
493 /* Ask the thread to stop. */
494 SetEvent (state->stop_select);
495 /* Wait until it does. */
496 WaitForSingleObject (state->have_stopped, INFINITE);
497 /* The thread is now stopped. */
498 state->thread_state = STS_STOPPED;
502 console_select_thread (void *arg)
504 struct serial *scb = arg;
505 struct ser_console_state *state;
510 h = (HANDLE) _get_osfhandle (scb->fd);
514 HANDLE wait_events[2];
518 select_thread_wait (state);
522 wait_events[0] = state->stop_select;
525 event_index = WaitForMultipleObjects (2, wait_events,
528 if (event_index == WAIT_OBJECT_0
529 || WaitForSingleObject (state->stop_select, 0) == WAIT_OBJECT_0)
532 if (event_index != WAIT_OBJECT_0 + 1)
534 /* Wait must have failed; assume an error has occured, e.g.
535 the handle has been closed. */
536 SetEvent (state->except_event);
540 /* We've got a pending event on the console. See if it's
542 if (!PeekConsoleInput (h, &record, 1, &n_records) || n_records != 1)
544 /* Something went wrong. Maybe the console is gone. */
545 SetEvent (state->except_event);
549 if (record.EventType == KEY_EVENT && record.Event.KeyEvent.bKeyDown)
551 WORD keycode = record.Event.KeyEvent.wVirtualKeyCode;
553 /* Ignore events containing only control keys. We must
554 recognize "enhanced" keys which we are interested in
555 reading via getch, if they do not map to ASCII. But we
556 do not want to report input available for e.g. the
557 control key alone. */
559 if (record.Event.KeyEvent.uChar.AsciiChar != 0
560 || keycode == VK_PRIOR
561 || keycode == VK_NEXT
563 || keycode == VK_HOME
564 || keycode == VK_LEFT
566 || keycode == VK_RIGHT
567 || keycode == VK_DOWN
568 || keycode == VK_INSERT
569 || keycode == VK_DELETE)
571 /* This is really a keypress. */
572 SetEvent (state->read_event);
577 /* Otherwise discard it and wait again. */
578 ReadConsoleInput (h, &record, 1, &n_records);
581 SetEvent(state->have_stopped);
589 if (PeekNamedPipe ((HANDLE) _get_osfhandle (fd), NULL, 0, NULL, NULL, NULL))
598 if (GetFileType ((HANDLE) _get_osfhandle (fd)) == FILE_TYPE_DISK)
605 pipe_select_thread (void *arg)
607 struct serial *scb = arg;
608 struct ser_console_state *state;
613 h = (HANDLE) _get_osfhandle (scb->fd);
619 select_thread_wait (state);
621 /* Wait for something to happen on the pipe. */
624 if (!PeekNamedPipe (h, NULL, 0, NULL, &n_avail, NULL))
626 SetEvent (state->except_event);
632 SetEvent (state->read_event);
636 /* Delay 10ms before checking again, but allow the stop
638 if (WaitForSingleObject (state->stop_select, 10) == WAIT_OBJECT_0)
642 SetEvent (state->have_stopped);
648 file_select_thread (void *arg)
650 struct serial *scb = arg;
651 struct ser_console_state *state;
656 h = (HANDLE) _get_osfhandle (scb->fd);
660 select_thread_wait (state);
662 if (SetFilePointer (h, 0, NULL, FILE_CURRENT)
663 == INVALID_SET_FILE_POINTER)
664 SetEvent (state->except_event);
666 SetEvent (state->read_event);
668 SetEvent (state->have_stopped);
674 ser_console_wait_handle (struct serial *scb, HANDLE *read, HANDLE *except)
676 struct ser_console_state *state = scb->state;
680 thread_fn_type thread_fn;
683 is_tty = isatty (scb->fd);
684 if (!is_tty && !fd_is_file (scb->fd) && !fd_is_pipe (scb->fd))
691 state = xmalloc (sizeof (struct ser_console_state));
692 memset (state, 0, sizeof (struct ser_console_state));
696 thread_fn = console_select_thread;
697 else if (fd_is_pipe (scb->fd))
698 thread_fn = pipe_select_thread;
700 thread_fn = file_select_thread;
702 create_select_thread (thread_fn, scb, state);
705 *read = state->read_event;
706 *except = state->except_event;
708 /* Start from a blank state. */
709 ResetEvent (state->read_event);
710 ResetEvent (state->except_event);
711 ResetEvent (state->stop_select);
713 /* First check for a key already in the buffer. If there is one,
714 we don't need a thread. This also catches the second key of
715 multi-character returns from getch, for instance for arrow
716 keys. The second half is in a C library internal buffer,
717 and PeekConsoleInput will not find it. */
720 SetEvent (state->read_event);
724 /* Otherwise, start the select thread. */
725 start_select_thread (state);
729 ser_console_done_wait_handle (struct serial *scb)
731 struct ser_console_state *state = scb->state;
736 stop_select_thread (state);
740 ser_console_close (struct serial *scb)
742 struct ser_console_state *state = scb->state;
746 destroy_select_thread (state);
751 struct ser_console_ttystate
756 static serial_ttystate
757 ser_console_get_tty_state (struct serial *scb)
759 if (isatty (scb->fd))
761 struct ser_console_ttystate *state;
763 state = (struct ser_console_ttystate *) xmalloc (sizeof *state);
773 /* Since we use the pipe_select_thread for our select emulation,
774 we need to place the state structure it requires at the front
776 struct ser_console_state wait;
778 /* The pex obj for our (one-stage) pipeline. */
781 /* Streams for the pipeline's input and output. */
782 FILE *input, *output;
785 static struct pipe_state *
786 make_pipe_state (void)
788 struct pipe_state *ps = XNEW (struct pipe_state);
790 memset (ps, 0, sizeof (*ps));
791 ps->wait.read_event = INVALID_HANDLE_VALUE;
792 ps->wait.except_event = INVALID_HANDLE_VALUE;
793 ps->wait.start_select = INVALID_HANDLE_VALUE;
794 ps->wait.stop_select = INVALID_HANDLE_VALUE;
800 free_pipe_state (struct pipe_state *ps)
802 int saved_errno = errno;
804 if (ps->wait.read_event != INVALID_HANDLE_VALUE)
805 destroy_select_thread (&ps->wait);
807 /* Close the pipe to the child. We must close the pipe before
808 calling pex_free because pex_free will wait for the child to exit
809 and the child will not exit until the pipe is closed. */
815 /* pex_free closes ps->output. */
826 cleanup_pipe_state (void *untyped)
828 struct pipe_state *ps = untyped;
830 free_pipe_state (ps);
834 pipe_windows_open (struct serial *scb, const char *name)
836 struct pipe_state *ps;
839 struct cleanup *back_to;
842 error_no_arg (_("child command"));
844 argv = gdb_buildargv (name);
845 back_to = make_cleanup_freeargv (argv);
847 if (! argv[0] || argv[0][0] == '\0')
848 error (_("missing child command"));
850 ps = make_pipe_state ();
851 make_cleanup (cleanup_pipe_state, ps);
853 ps->pex = pex_init (PEX_USE_PIPES, "target remote pipe", NULL);
856 ps->input = pex_input_pipe (ps->pex, 1);
863 = pex_run (ps->pex, PEX_SEARCH | PEX_BINARY_INPUT | PEX_BINARY_OUTPUT
864 | PEX_STDERR_TO_PIPE,
865 argv[0], argv, NULL, NULL,
870 /* Our caller expects us to return -1, but all they'll do with
871 it generally is print the message based on errno. We have
872 all the same information here, plus err_msg provided by
873 pex_run, so we just raise the error here. */
875 error (_("error starting child process '%s': %s: %s"),
876 name, err_msg, safe_strerror (err));
878 error (_("error starting child process '%s': %s"),
883 ps->output = pex_read_output (ps->pex, 1);
886 scb->fd = fileno (ps->output);
888 pex_stderr = pex_read_err (ps->pex, 1);
891 scb->error_fd = fileno (pex_stderr);
893 scb->state = (void *) ps;
895 discard_cleanups (back_to);
899 do_cleanups (back_to);
904 pipe_windows_fdopen (struct serial *scb, int fd)
906 struct pipe_state *ps;
908 ps = make_pipe_state ();
910 ps->input = fdopen (fd, "r+");
914 ps->output = fdopen (fd, "r+");
919 scb->state = (void *) ps;
924 free_pipe_state (ps);
929 pipe_windows_close (struct serial *scb)
931 struct pipe_state *ps = scb->state;
933 /* In theory, we should try to kill the subprocess here, but the pex
934 interface doesn't give us enough information to do that. Usually
935 closing the input pipe will get the message across. */
937 free_pipe_state (ps);
942 pipe_windows_read (struct serial *scb, size_t count)
944 HANDLE pipeline_out = (HANDLE) _get_osfhandle (scb->fd);
948 if (pipeline_out == INVALID_HANDLE_VALUE)
951 if (! PeekNamedPipe (pipeline_out, NULL, 0, NULL, &available, NULL))
954 if (count > available)
957 if (! ReadFile (pipeline_out, scb->buf, count, &bytes_read, NULL))
965 pipe_windows_write (struct serial *scb, const void *buf, size_t count)
967 struct pipe_state *ps = scb->state;
971 int pipeline_in_fd = fileno (ps->input);
972 if (pipeline_in_fd < 0)
975 pipeline_in = (HANDLE) _get_osfhandle (pipeline_in_fd);
976 if (pipeline_in == INVALID_HANDLE_VALUE)
979 if (! WriteFile (pipeline_in, buf, count, &written, NULL))
987 pipe_wait_handle (struct serial *scb, HANDLE *read, HANDLE *except)
989 struct pipe_state *ps = scb->state;
991 /* Have we allocated our events yet? */
992 if (ps->wait.read_event == INVALID_HANDLE_VALUE)
993 /* Start the thread. */
994 create_select_thread (pipe_select_thread, scb, &ps->wait);
996 *read = ps->wait.read_event;
997 *except = ps->wait.except_event;
999 /* Start from a blank state. */
1000 ResetEvent (ps->wait.read_event);
1001 ResetEvent (ps->wait.except_event);
1002 ResetEvent (ps->wait.stop_select);
1004 start_select_thread (&ps->wait);
1008 pipe_done_wait_handle (struct serial *scb)
1010 struct pipe_state *ps = scb->state;
1012 /* Have we allocated our events yet? */
1013 if (ps->wait.read_event == INVALID_HANDLE_VALUE)
1016 stop_select_thread (&ps->wait);
1020 pipe_avail (struct serial *scb, int fd)
1022 HANDLE h = (HANDLE) _get_osfhandle (fd);
1024 BOOL r = PeekNamedPipe (h, NULL, 0, NULL, &numBytes, NULL);
1032 gdb_pipe (int pdes[2])
1034 if (_pipe (pdes, 512, _O_BINARY | _O_NOINHERIT) == -1)
1039 struct net_windows_state
1041 struct ser_console_state base;
1046 /* Check whether the socket has any pending data to be read. If so,
1047 set the select thread's read event. On error, set the select
1048 thread's except event. If any event was set, return true,
1049 otherwise return false. */
1052 net_windows_socket_check_pending (struct serial *scb)
1054 struct net_windows_state *state = scb->state;
1055 unsigned long available;
1057 if (ioctlsocket (scb->fd, FIONREAD, &available) != 0)
1059 /* The socket closed, or some other error. */
1060 SetEvent (state->base.except_event);
1063 else if (available > 0)
1065 SetEvent (state->base.read_event);
1073 net_windows_select_thread (void *arg)
1075 struct serial *scb = arg;
1076 struct net_windows_state *state;
1083 HANDLE wait_events[2];
1084 WSANETWORKEVENTS events;
1086 select_thread_wait (&state->base);
1088 wait_events[0] = state->base.stop_select;
1089 wait_events[1] = state->sock_event;
1091 /* Wait for something to happen on the socket. */
1094 event_index = WaitForMultipleObjects (2, wait_events, FALSE, INFINITE);
1096 if (event_index == WAIT_OBJECT_0
1097 || WaitForSingleObject (state->base.stop_select, 0) == WAIT_OBJECT_0)
1099 /* We have been requested to stop. */
1103 if (event_index != WAIT_OBJECT_0 + 1)
1105 /* Some error has occured. Assume that this is an error
1107 SetEvent (state->base.except_event);
1111 /* Enumerate the internal network events, and reset the
1112 object that signalled us to catch the next event. */
1113 if (WSAEnumNetworkEvents (scb->fd, state->sock_event, &events) != 0)
1115 /* Something went wrong. Maybe the socket is gone. */
1116 SetEvent (state->base.except_event);
1120 if (events.lNetworkEvents & FD_READ)
1122 if (net_windows_socket_check_pending (scb))
1125 /* Spurious wakeup. That is, the socket's event was
1126 signalled before we last called recv. */
1129 if (events.lNetworkEvents & FD_CLOSE)
1131 SetEvent (state->base.except_event);
1136 SetEvent (state->base.have_stopped);
1142 net_windows_wait_handle (struct serial *scb, HANDLE *read, HANDLE *except)
1144 struct net_windows_state *state = scb->state;
1146 /* Start from a clean slate. */
1147 ResetEvent (state->base.read_event);
1148 ResetEvent (state->base.except_event);
1149 ResetEvent (state->base.stop_select);
1151 *read = state->base.read_event;
1152 *except = state->base.except_event;
1154 /* Check any pending events. Otherwise, start the select
1156 if (!net_windows_socket_check_pending (scb))
1157 start_select_thread (&state->base);
1161 net_windows_done_wait_handle (struct serial *scb)
1163 struct net_windows_state *state = scb->state;
1165 stop_select_thread (&state->base);
1169 net_windows_open (struct serial *scb, const char *name)
1171 struct net_windows_state *state;
1175 ret = net_open (scb, name);
1179 state = xmalloc (sizeof (struct net_windows_state));
1180 memset (state, 0, sizeof (struct net_windows_state));
1183 /* Associate an event with the socket. */
1184 state->sock_event = CreateEvent (0, TRUE, FALSE, 0);
1185 WSAEventSelect (scb->fd, state->sock_event, FD_READ | FD_CLOSE);
1187 /* Start the thread. */
1188 create_select_thread (net_windows_select_thread, scb, &state->base);
1195 net_windows_close (struct serial *scb)
1197 struct net_windows_state *state = scb->state;
1199 destroy_select_thread (&state->base);
1200 CloseHandle (state->sock_event);
1207 /* The serial port driver. */
1209 static const struct serial_ops hardwire_ops =
1217 ser_windows_flush_output,
1218 ser_windows_flush_input,
1219 ser_windows_send_break,
1221 /* These are only used for stdin; we do not need them for serial
1222 ports, so supply the standard dummies. */
1223 ser_base_get_tty_state,
1224 ser_base_copy_tty_state,
1225 ser_base_set_tty_state,
1226 ser_base_print_tty_state,
1227 ser_base_noflush_set_tty_state,
1228 ser_windows_setbaudrate,
1229 ser_windows_setstopbits,
1230 ser_windows_drain_output,
1232 ser_windows_read_prim,
1233 ser_windows_write_prim,
1235 ser_windows_wait_handle
1238 /* The dummy serial driver used for terminals. We only provide the
1239 TTY-related methods. */
1241 static const struct serial_ops tty_ops =
1253 ser_console_get_tty_state,
1254 ser_base_copy_tty_state,
1255 ser_base_set_tty_state,
1256 ser_base_print_tty_state,
1257 ser_base_noflush_set_tty_state,
1260 ser_base_drain_output,
1265 ser_console_wait_handle,
1266 ser_console_done_wait_handle
1269 /* The pipe interface. */
1271 static const struct serial_ops pipe_ops =
1276 pipe_windows_fdopen,
1279 ser_base_flush_output,
1280 ser_base_flush_input,
1281 ser_base_send_break,
1283 ser_base_get_tty_state,
1284 ser_base_copy_tty_state,
1285 ser_base_set_tty_state,
1286 ser_base_print_tty_state,
1287 ser_base_noflush_set_tty_state,
1288 ser_base_setbaudrate,
1289 ser_base_setstopbits,
1290 ser_base_drain_output,
1296 pipe_done_wait_handle
1299 /* The TCP/UDP socket driver. */
1301 static const struct serial_ops tcp_ops =
1309 ser_base_flush_output,
1310 ser_base_flush_input,
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,
1320 ser_base_drain_output,
1325 net_windows_wait_handle,
1326 net_windows_done_wait_handle
1330 _initialize_ser_windows (void)
1333 struct serial_ops *ops;
1337 /* First find out if kernel32 exports CancelIo function. */
1338 hm = LoadLibrary ("kernel32.dll");
1341 CancelIo = (void *) GetProcAddress (hm, "CancelIo");
1347 serial_add_interface (&hardwire_ops);
1348 serial_add_interface (&tty_ops);
1349 serial_add_interface (&pipe_ops);
1351 /* If WinSock works, register the TCP/UDP socket driver. */
1353 if (WSAStartup (MAKEWORD (1, 0), &wsa_data) != 0)
1354 /* WinSock is unavailable. */
1357 serial_add_interface (&tcp_ops);