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>
36 void _initialize_ser_windows (void);
38 struct ser_windows_state
46 /* CancelIo is not available for Windows 95 OS, so we need to use
47 LoadLibrary/GetProcAddress to avoid a startup failure. */
48 #define CancelIo dyn_CancelIo
49 static BOOL WINAPI (*CancelIo) (HANDLE);
51 /* Open up a real live device for serial I/O. */
54 ser_windows_open (struct serial *scb, const char *name)
57 struct ser_windows_state *state;
58 COMMTIMEOUTS timeouts;
60 h = CreateFile (name, GENERIC_READ | GENERIC_WRITE, 0, NULL,
61 OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
62 if (h == INVALID_HANDLE_VALUE)
68 scb->fd = _open_osfhandle ((intptr_t) h, O_RDWR);
75 if (!SetCommMask (h, EV_RXCHAR))
81 timeouts.ReadIntervalTimeout = MAXDWORD;
82 timeouts.ReadTotalTimeoutConstant = 0;
83 timeouts.ReadTotalTimeoutMultiplier = 0;
84 timeouts.WriteTotalTimeoutConstant = 0;
85 timeouts.WriteTotalTimeoutMultiplier = 0;
86 if (!SetCommTimeouts (h, &timeouts))
92 state = xmalloc (sizeof (struct ser_windows_state));
93 memset (state, 0, sizeof (struct ser_windows_state));
96 /* Create a manual reset event to watch the input buffer. */
97 state->ov.hEvent = CreateEvent (0, TRUE, FALSE, 0);
99 /* Create a (currently unused) handle to record exceptions. */
100 state->except_event = CreateEvent (0, TRUE, FALSE, 0);
105 /* Wait for the output to drain away, as opposed to flushing (discarding)
109 ser_windows_drain_output (struct serial *scb)
111 HANDLE h = (HANDLE) _get_osfhandle (scb->fd);
113 return (FlushFileBuffers (h) != 0) ? 0 : -1;
117 ser_windows_flush_output (struct serial *scb)
119 HANDLE h = (HANDLE) _get_osfhandle (scb->fd);
121 return (PurgeComm (h, PURGE_TXCLEAR) != 0) ? 0 : -1;
125 ser_windows_flush_input (struct serial *scb)
127 HANDLE h = (HANDLE) _get_osfhandle (scb->fd);
129 return (PurgeComm (h, PURGE_RXCLEAR) != 0) ? 0 : -1;
133 ser_windows_send_break (struct serial *scb)
135 HANDLE h = (HANDLE) _get_osfhandle (scb->fd);
137 if (SetCommBreak (h) == 0)
140 /* Delay for 250 milliseconds. */
143 if (ClearCommBreak (h))
150 ser_windows_raw (struct serial *scb)
152 HANDLE h = (HANDLE) _get_osfhandle (scb->fd);
155 if (GetCommState (h, &state) == 0)
158 state.fParity = FALSE;
159 state.fOutxCtsFlow = FALSE;
160 state.fOutxDsrFlow = FALSE;
161 state.fDtrControl = DTR_CONTROL_ENABLE;
162 state.fDsrSensitivity = FALSE;
166 state.fAbortOnError = FALSE;
168 state.Parity = NOPARITY;
170 scb->current_timeout = 0;
172 if (SetCommState (h, &state) == 0)
173 warning (_("SetCommState failed"));
177 ser_windows_setstopbits (struct serial *scb, int num)
179 HANDLE h = (HANDLE) _get_osfhandle (scb->fd);
182 if (GetCommState (h, &state) == 0)
187 case SERIAL_1_STOPBITS:
188 state.StopBits = ONESTOPBIT;
190 case SERIAL_1_AND_A_HALF_STOPBITS:
191 state.StopBits = ONE5STOPBITS;
193 case SERIAL_2_STOPBITS:
194 state.StopBits = TWOSTOPBITS;
200 return (SetCommState (h, &state) != 0) ? 0 : -1;
204 ser_windows_setbaudrate (struct serial *scb, int rate)
206 HANDLE h = (HANDLE) _get_osfhandle (scb->fd);
209 if (GetCommState (h, &state) == 0)
212 state.BaudRate = rate;
214 return (SetCommState (h, &state) != 0) ? 0 : -1;
218 ser_windows_close (struct serial *scb)
220 struct ser_windows_state *state;
222 /* Stop any pending selects. On Windows 95 OS, CancelIo function does
223 not exist. In that case, it can be replaced by a call to CloseHandle,
224 but this is not necessary here as we do close the Windows handle
225 by calling close (scb->fd) below. */
227 CancelIo ((HANDLE) _get_osfhandle (scb->fd));
229 CloseHandle (state->ov.hEvent);
230 CloseHandle (state->except_event);
242 ser_windows_wait_handle (struct serial *scb, HANDLE *read, HANDLE *except)
244 struct ser_windows_state *state;
247 HANDLE h = (HANDLE) _get_osfhandle (scb->fd);
251 *except = state->except_event;
252 *read = state->ov.hEvent;
254 if (state->in_progress)
257 /* Reset the mask - we are only interested in any characters which
258 arrive after this point, not characters which might have arrived
259 and already been read. */
261 /* This really, really shouldn't be necessary - just the second one.
262 But otherwise an internal flag for EV_RXCHAR does not get
263 cleared, and we get a duplicated event, if the last batch
264 of characters included at least two arriving close together. */
265 if (!SetCommMask (h, 0))
266 warning (_("ser_windows_wait_handle: reseting mask failed"));
268 if (!SetCommMask (h, EV_RXCHAR))
269 warning (_("ser_windows_wait_handle: reseting mask failed (2)"));
271 /* There's a potential race condition here; we must check cbInQue
272 and not wait if that's nonzero. */
274 ClearCommError (h, &errors, &status);
275 if (status.cbInQue > 0)
277 SetEvent (state->ov.hEvent);
281 state->in_progress = 1;
282 ResetEvent (state->ov.hEvent);
283 state->lastCommMask = -2;
284 if (WaitCommEvent (h, &state->lastCommMask, &state->ov))
286 gdb_assert (state->lastCommMask & EV_RXCHAR);
287 SetEvent (state->ov.hEvent);
290 gdb_assert (GetLastError () == ERROR_IO_PENDING);
294 ser_windows_read_prim (struct serial *scb, size_t count)
296 struct ser_windows_state *state;
298 DWORD bytes_read, bytes_read_tmp;
303 if (state->in_progress)
305 WaitForSingleObject (state->ov.hEvent, INFINITE);
306 state->in_progress = 0;
307 ResetEvent (state->ov.hEvent);
310 memset (&ov, 0, sizeof (OVERLAPPED));
311 ov.hEvent = CreateEvent (0, FALSE, FALSE, 0);
312 h = (HANDLE) _get_osfhandle (scb->fd);
314 if (!ReadFile (h, scb->buf, /* count */ 1, &bytes_read, &ov))
316 if (GetLastError () != ERROR_IO_PENDING
317 || !GetOverlappedResult (h, &ov, &bytes_read, TRUE))
321 CloseHandle (ov.hEvent);
326 ser_windows_write_prim (struct serial *scb, const void *buf, size_t len)
328 struct ser_windows_state *state;
333 memset (&ov, 0, sizeof (OVERLAPPED));
334 ov.hEvent = CreateEvent (0, FALSE, FALSE, 0);
335 h = (HANDLE) _get_osfhandle (scb->fd);
336 if (!WriteFile (h, buf, len, &bytes_written, &ov))
338 if (GetLastError () != ERROR_IO_PENDING
339 || !GetOverlappedResult (h, &ov, &bytes_written, TRUE))
343 CloseHandle (ov.hEvent);
344 return bytes_written;
347 /* On Windows, gdb_select is implemented using WaitForMulpleObjects.
348 A "select thread" is created for each file descriptor. These
349 threads looks for activity on the corresponding descriptor, using
350 whatever techniques are appropriate for the descriptor type. When
351 that activity occurs, the thread signals an appropriate event,
352 which wakes up WaitForMultipleObjects.
354 Each select thread is in one of two states: stopped or started.
355 Select threads begin in the stopped state. When gdb_select is
356 called, threads corresponding to the descriptors of interest are
357 started by calling a wait_handle function. Each thread that
358 notices activity signals the appropriate event and then reenters
359 the stopped state. Before gdb_select returns it calls the
360 wait_handle_done functions, which return the threads to the stopped
363 enum select_thread_state {
368 struct ser_console_state
370 /* Signaled by the select thread to indicate that data is available
371 on the file descriptor. */
373 /* Signaled by the select thread to indicate that an exception has
374 occurred on the file descriptor. */
376 /* Signaled by the select thread to indicate that it has entered the
377 started state. HAVE_STARTED and HAVE_STOPPED are never signaled
380 /* Signaled by the select thread to indicate that it has stopped,
381 either because data is available (and READ_EVENT is signaled),
382 because an exception has occurred (and EXCEPT_EVENT is signaled),
383 or because STOP_SELECT was signaled. */
386 /* Signaled by the main program to tell the select thread to enter
387 the started state. */
389 /* Signaled by the main program to tell the select thread to enter
390 the stopped state. */
392 /* Signaled by the main program to tell the select thread to
396 /* The handle for the select thread. */
398 /* The state of the select thread. This field is only accessed in
399 the main program, never by the select thread itself. */
400 enum select_thread_state thread_state;
403 /* Called by a select thread to enter the stopped state. This
404 function does not return until the thread has re-entered the
407 select_thread_wait (struct ser_console_state *state)
409 HANDLE wait_events[2];
411 /* There are two things that can wake us up: a request that we enter
412 the started state, or that we exit this thread. */
413 wait_events[0] = state->start_select;
414 wait_events[1] = state->exit_select;
415 if (WaitForMultipleObjects (2, wait_events, FALSE, INFINITE)
417 /* Either the EXIT_SELECT event was signaled (requesting that the
418 thread exit) or an error has occurred. In either case, we exit
422 /* We are now in the started state. */
423 SetEvent (state->have_started);
426 typedef DWORD WINAPI (*thread_fn_type)(void *);
428 /* Create a new select thread for SCB executing THREAD_FN. The STATE
429 will be filled in by this function before return. */
431 create_select_thread (thread_fn_type thread_fn,
433 struct ser_console_state *state)
437 /* Create all of the events. These are all auto-reset events. */
438 state->read_event = CreateEvent (NULL, FALSE, FALSE, NULL);
439 state->except_event = CreateEvent (NULL, FALSE, FALSE, NULL);
440 state->have_started = CreateEvent (NULL, FALSE, FALSE, NULL);
441 state->have_stopped = CreateEvent (NULL, FALSE, FALSE, NULL);
442 state->start_select = CreateEvent (NULL, FALSE, FALSE, NULL);
443 state->stop_select = CreateEvent (NULL, FALSE, FALSE, NULL);
444 state->exit_select = CreateEvent (NULL, FALSE, FALSE, NULL);
446 state->thread = CreateThread (NULL, 0, thread_fn, scb, 0, &threadId);
447 /* The thread begins in the stopped state. */
448 state->thread_state = STS_STOPPED;
451 /* Destroy the select thread indicated by STATE. */
453 destroy_select_thread (struct ser_console_state *state)
455 /* Ask the thread to exit. */
456 SetEvent (state->exit_select);
457 /* Wait until it does. */
458 WaitForSingleObject (state->thread, INFINITE);
460 /* Destroy the events. */
461 CloseHandle (state->read_event);
462 CloseHandle (state->except_event);
463 CloseHandle (state->have_started);
464 CloseHandle (state->have_stopped);
465 CloseHandle (state->start_select);
466 CloseHandle (state->stop_select);
467 CloseHandle (state->exit_select);
470 /* Called by gdb_select to start the select thread indicated by STATE.
471 This function does not return until the thread has started. */
473 start_select_thread (struct ser_console_state *state)
475 /* Ask the thread to start. */
476 SetEvent (state->start_select);
477 /* Wait until it does. */
478 WaitForSingleObject (state->have_started, INFINITE);
479 /* The thread is now started. */
480 state->thread_state = STS_STARTED;
483 /* Called by gdb_select to stop the select thread indicated by STATE.
484 This function does not return until the thread has stopped. */
486 stop_select_thread (struct ser_console_state *state)
488 /* If the thread is already in the stopped state, we have nothing to
489 do. Some of the wait_handle functions avoid calling
490 start_select_thread if they notice activity on the relevant file
491 descriptors. The wait_handle_done functions still call
492 stop_select_thread -- but it is already stopped. */
493 if (state->thread_state != STS_STARTED)
495 /* Ask the thread to stop. */
496 SetEvent (state->stop_select);
497 /* Wait until it does. */
498 WaitForSingleObject (state->have_stopped, INFINITE);
499 /* The thread is now stopped. */
500 state->thread_state = STS_STOPPED;
504 console_select_thread (void *arg)
506 struct serial *scb = arg;
507 struct ser_console_state *state;
512 h = (HANDLE) _get_osfhandle (scb->fd);
516 HANDLE wait_events[2];
520 select_thread_wait (state);
524 wait_events[0] = state->stop_select;
527 event_index = WaitForMultipleObjects (2, wait_events,
530 if (event_index == WAIT_OBJECT_0
531 || WaitForSingleObject (state->stop_select, 0) == WAIT_OBJECT_0)
534 if (event_index != WAIT_OBJECT_0 + 1)
536 /* Wait must have failed; assume an error has occured, e.g.
537 the handle has been closed. */
538 SetEvent (state->except_event);
542 /* We've got a pending event on the console. See if it's
544 if (!PeekConsoleInput (h, &record, 1, &n_records) || n_records != 1)
546 /* Something went wrong. Maybe the console is gone. */
547 SetEvent (state->except_event);
551 if (record.EventType == KEY_EVENT && record.Event.KeyEvent.bKeyDown)
553 WORD keycode = record.Event.KeyEvent.wVirtualKeyCode;
555 /* Ignore events containing only control keys. We must
556 recognize "enhanced" keys which we are interested in
557 reading via getch, if they do not map to ASCII. But we
558 do not want to report input available for e.g. the
559 control key alone. */
561 if (record.Event.KeyEvent.uChar.AsciiChar != 0
562 || keycode == VK_PRIOR
563 || keycode == VK_NEXT
565 || keycode == VK_HOME
566 || keycode == VK_LEFT
568 || keycode == VK_RIGHT
569 || keycode == VK_DOWN
570 || keycode == VK_INSERT
571 || keycode == VK_DELETE)
573 /* This is really a keypress. */
574 SetEvent (state->read_event);
579 /* Otherwise discard it and wait again. */
580 ReadConsoleInput (h, &record, 1, &n_records);
583 SetEvent(state->have_stopped);
591 if (PeekNamedPipe ((HANDLE) _get_osfhandle (fd), NULL, 0, NULL, NULL, NULL))
600 if (GetFileType ((HANDLE) _get_osfhandle (fd)) == FILE_TYPE_DISK)
607 pipe_select_thread (void *arg)
609 struct serial *scb = arg;
610 struct ser_console_state *state;
615 h = (HANDLE) _get_osfhandle (scb->fd);
621 select_thread_wait (state);
623 /* Wait for something to happen on the pipe. */
626 if (!PeekNamedPipe (h, NULL, 0, NULL, &n_avail, NULL))
628 SetEvent (state->except_event);
634 SetEvent (state->read_event);
638 /* Delay 10ms before checking again, but allow the stop
640 if (WaitForSingleObject (state->stop_select, 10) == WAIT_OBJECT_0)
644 SetEvent (state->have_stopped);
650 file_select_thread (void *arg)
652 struct serial *scb = arg;
653 struct ser_console_state *state;
658 h = (HANDLE) _get_osfhandle (scb->fd);
662 select_thread_wait (state);
664 if (SetFilePointer (h, 0, NULL, FILE_CURRENT)
665 == INVALID_SET_FILE_POINTER)
666 SetEvent (state->except_event);
668 SetEvent (state->read_event);
670 SetEvent (state->have_stopped);
676 ser_console_wait_handle (struct serial *scb, HANDLE *read, HANDLE *except)
678 struct ser_console_state *state = scb->state;
682 thread_fn_type thread_fn;
685 is_tty = isatty (scb->fd);
686 if (!is_tty && !fd_is_file (scb->fd) && !fd_is_pipe (scb->fd))
693 state = xmalloc (sizeof (struct ser_console_state));
694 memset (state, 0, sizeof (struct ser_console_state));
698 thread_fn = console_select_thread;
699 else if (fd_is_pipe (scb->fd))
700 thread_fn = pipe_select_thread;
702 thread_fn = file_select_thread;
704 create_select_thread (thread_fn, scb, state);
707 *read = state->read_event;
708 *except = state->except_event;
710 /* Start from a blank state. */
711 ResetEvent (state->read_event);
712 ResetEvent (state->except_event);
713 ResetEvent (state->stop_select);
715 /* First check for a key already in the buffer. If there is one,
716 we don't need a thread. This also catches the second key of
717 multi-character returns from getch, for instance for arrow
718 keys. The second half is in a C library internal buffer,
719 and PeekConsoleInput will not find it. */
722 SetEvent (state->read_event);
726 /* Otherwise, start the select thread. */
727 start_select_thread (state);
731 ser_console_done_wait_handle (struct serial *scb)
733 struct ser_console_state *state = scb->state;
738 stop_select_thread (state);
742 ser_console_close (struct serial *scb)
744 struct ser_console_state *state = scb->state;
748 destroy_select_thread (state);
753 struct ser_console_ttystate
758 static serial_ttystate
759 ser_console_get_tty_state (struct serial *scb)
761 if (isatty (scb->fd))
763 struct ser_console_ttystate *state;
765 state = (struct ser_console_ttystate *) xmalloc (sizeof *state);
775 /* Since we use the pipe_select_thread for our select emulation,
776 we need to place the state structure it requires at the front
778 struct ser_console_state wait;
780 /* The pex obj for our (one-stage) pipeline. */
783 /* Streams for the pipeline's input and output. */
784 FILE *input, *output;
787 static struct pipe_state *
788 make_pipe_state (void)
790 struct pipe_state *ps = XNEW (struct pipe_state);
792 memset (ps, 0, sizeof (*ps));
793 ps->wait.read_event = INVALID_HANDLE_VALUE;
794 ps->wait.except_event = INVALID_HANDLE_VALUE;
795 ps->wait.start_select = INVALID_HANDLE_VALUE;
796 ps->wait.stop_select = INVALID_HANDLE_VALUE;
802 free_pipe_state (struct pipe_state *ps)
804 int saved_errno = errno;
806 if (ps->wait.read_event != INVALID_HANDLE_VALUE)
807 destroy_select_thread (&ps->wait);
809 /* Close the pipe to the child. We must close the pipe before
810 calling pex_free because pex_free will wait for the child to exit
811 and the child will not exit until the pipe is closed. */
817 /* pex_free closes ps->output. */
828 cleanup_pipe_state (void *untyped)
830 struct pipe_state *ps = untyped;
832 free_pipe_state (ps);
836 pipe_windows_open (struct serial *scb, const char *name)
838 struct pipe_state *ps;
841 struct cleanup *back_to;
844 error_no_arg (_("child command"));
846 argv = gdb_buildargv (name);
847 back_to = make_cleanup_freeargv (argv);
849 if (! argv[0] || argv[0][0] == '\0')
850 error (_("missing child command"));
852 ps = make_pipe_state ();
853 make_cleanup (cleanup_pipe_state, ps);
855 ps->pex = pex_init (PEX_USE_PIPES, "target remote pipe", NULL);
858 ps->input = pex_input_pipe (ps->pex, 1);
865 = pex_run (ps->pex, PEX_SEARCH | PEX_BINARY_INPUT | PEX_BINARY_OUTPUT
866 | PEX_STDERR_TO_PIPE,
867 argv[0], argv, NULL, NULL,
872 /* Our caller expects us to return -1, but all they'll do with
873 it generally is print the message based on errno. We have
874 all the same information here, plus err_msg provided by
875 pex_run, so we just raise the error here. */
877 error (_("error starting child process '%s': %s: %s"),
878 name, err_msg, safe_strerror (err));
880 error (_("error starting child process '%s': %s"),
885 ps->output = pex_read_output (ps->pex, 1);
888 scb->fd = fileno (ps->output);
890 pex_stderr = pex_read_err (ps->pex, 1);
893 scb->error_fd = fileno (pex_stderr);
895 scb->state = (void *) ps;
897 discard_cleanups (back_to);
901 do_cleanups (back_to);
906 pipe_windows_fdopen (struct serial *scb, int fd)
908 struct pipe_state *ps;
910 ps = make_pipe_state ();
912 ps->input = fdopen (fd, "r+");
916 ps->output = fdopen (fd, "r+");
921 scb->state = (void *) ps;
926 free_pipe_state (ps);
931 pipe_windows_close (struct serial *scb)
933 struct pipe_state *ps = scb->state;
935 /* In theory, we should try to kill the subprocess here, but the pex
936 interface doesn't give us enough information to do that. Usually
937 closing the input pipe will get the message across. */
939 free_pipe_state (ps);
944 pipe_windows_read (struct serial *scb, size_t count)
946 HANDLE pipeline_out = (HANDLE) _get_osfhandle (scb->fd);
950 if (pipeline_out == INVALID_HANDLE_VALUE)
953 if (! PeekNamedPipe (pipeline_out, NULL, 0, NULL, &available, NULL))
956 if (count > available)
959 if (! ReadFile (pipeline_out, scb->buf, count, &bytes_read, NULL))
967 pipe_windows_write (struct serial *scb, const void *buf, size_t count)
969 struct pipe_state *ps = scb->state;
973 int pipeline_in_fd = fileno (ps->input);
974 if (pipeline_in_fd < 0)
977 pipeline_in = (HANDLE) _get_osfhandle (pipeline_in_fd);
978 if (pipeline_in == INVALID_HANDLE_VALUE)
981 if (! WriteFile (pipeline_in, buf, count, &written, NULL))
989 pipe_wait_handle (struct serial *scb, HANDLE *read, HANDLE *except)
991 struct pipe_state *ps = scb->state;
993 /* Have we allocated our events yet? */
994 if (ps->wait.read_event == INVALID_HANDLE_VALUE)
995 /* Start the thread. */
996 create_select_thread (pipe_select_thread, scb, &ps->wait);
998 *read = ps->wait.read_event;
999 *except = ps->wait.except_event;
1001 /* Start from a blank state. */
1002 ResetEvent (ps->wait.read_event);
1003 ResetEvent (ps->wait.except_event);
1004 ResetEvent (ps->wait.stop_select);
1006 start_select_thread (&ps->wait);
1010 pipe_done_wait_handle (struct serial *scb)
1012 struct pipe_state *ps = scb->state;
1014 /* Have we allocated our events yet? */
1015 if (ps->wait.read_event == INVALID_HANDLE_VALUE)
1018 stop_select_thread (&ps->wait);
1022 pipe_avail (struct serial *scb, int fd)
1024 HANDLE h = (HANDLE) _get_osfhandle (fd);
1026 BOOL r = PeekNamedPipe (h, NULL, 0, NULL, &numBytes, NULL);
1034 gdb_pipe (int pdes[2])
1036 if (_pipe (pdes, 512, _O_BINARY | _O_NOINHERIT) == -1)
1041 struct net_windows_state
1043 struct ser_console_state base;
1048 /* Check whether the socket has any pending data to be read. If so,
1049 set the select thread's read event. On error, set the select
1050 thread's except event. If any event was set, return true,
1051 otherwise return false. */
1054 net_windows_socket_check_pending (struct serial *scb)
1056 struct net_windows_state *state = scb->state;
1057 unsigned long available;
1059 if (ioctlsocket (scb->fd, FIONREAD, &available) != 0)
1061 /* The socket closed, or some other error. */
1062 SetEvent (state->base.except_event);
1065 else if (available > 0)
1067 SetEvent (state->base.read_event);
1075 net_windows_select_thread (void *arg)
1077 struct serial *scb = arg;
1078 struct net_windows_state *state;
1085 HANDLE wait_events[2];
1086 WSANETWORKEVENTS events;
1088 select_thread_wait (&state->base);
1090 wait_events[0] = state->base.stop_select;
1091 wait_events[1] = state->sock_event;
1093 /* Wait for something to happen on the socket. */
1096 event_index = WaitForMultipleObjects (2, wait_events, FALSE, INFINITE);
1098 if (event_index == WAIT_OBJECT_0
1099 || WaitForSingleObject (state->base.stop_select, 0) == WAIT_OBJECT_0)
1101 /* We have been requested to stop. */
1105 if (event_index != WAIT_OBJECT_0 + 1)
1107 /* Some error has occured. Assume that this is an error
1109 SetEvent (state->base.except_event);
1113 /* Enumerate the internal network events, and reset the
1114 object that signalled us to catch the next event. */
1115 if (WSAEnumNetworkEvents (scb->fd, state->sock_event, &events) != 0)
1117 /* Something went wrong. Maybe the socket is gone. */
1118 SetEvent (state->base.except_event);
1122 if (events.lNetworkEvents & FD_READ)
1124 if (net_windows_socket_check_pending (scb))
1127 /* Spurious wakeup. That is, the socket's event was
1128 signalled before we last called recv. */
1131 if (events.lNetworkEvents & FD_CLOSE)
1133 SetEvent (state->base.except_event);
1138 SetEvent (state->base.have_stopped);
1144 net_windows_wait_handle (struct serial *scb, HANDLE *read, HANDLE *except)
1146 struct net_windows_state *state = scb->state;
1148 /* Start from a clean slate. */
1149 ResetEvent (state->base.read_event);
1150 ResetEvent (state->base.except_event);
1151 ResetEvent (state->base.stop_select);
1153 *read = state->base.read_event;
1154 *except = state->base.except_event;
1156 /* Check any pending events. Otherwise, start the select
1158 if (!net_windows_socket_check_pending (scb))
1159 start_select_thread (&state->base);
1163 net_windows_done_wait_handle (struct serial *scb)
1165 struct net_windows_state *state = scb->state;
1167 stop_select_thread (&state->base);
1171 net_windows_open (struct serial *scb, const char *name)
1173 struct net_windows_state *state;
1177 ret = net_open (scb, name);
1181 state = xmalloc (sizeof (struct net_windows_state));
1182 memset (state, 0, sizeof (struct net_windows_state));
1185 /* Associate an event with the socket. */
1186 state->sock_event = CreateEvent (0, TRUE, FALSE, 0);
1187 WSAEventSelect (scb->fd, state->sock_event, FD_READ | FD_CLOSE);
1189 /* Start the thread. */
1190 create_select_thread (net_windows_select_thread, scb, &state->base);
1197 net_windows_close (struct serial *scb)
1199 struct net_windows_state *state = scb->state;
1201 destroy_select_thread (&state->base);
1202 CloseHandle (state->sock_event);
1209 /* The serial port driver. */
1211 static const struct serial_ops hardwire_ops =
1219 ser_windows_flush_output,
1220 ser_windows_flush_input,
1221 ser_windows_send_break,
1223 /* These are only used for stdin; we do not need them for serial
1224 ports, so supply the standard dummies. */
1225 ser_base_get_tty_state,
1226 ser_base_copy_tty_state,
1227 ser_base_set_tty_state,
1228 ser_base_print_tty_state,
1229 ser_base_noflush_set_tty_state,
1230 ser_windows_setbaudrate,
1231 ser_windows_setstopbits,
1232 ser_windows_drain_output,
1234 ser_windows_read_prim,
1235 ser_windows_write_prim,
1237 ser_windows_wait_handle
1240 /* The dummy serial driver used for terminals. We only provide the
1241 TTY-related methods. */
1243 static const struct serial_ops tty_ops =
1255 ser_console_get_tty_state,
1256 ser_base_copy_tty_state,
1257 ser_base_set_tty_state,
1258 ser_base_print_tty_state,
1259 ser_base_noflush_set_tty_state,
1262 ser_base_drain_output,
1267 ser_console_wait_handle,
1268 ser_console_done_wait_handle
1271 /* The pipe interface. */
1273 static const struct serial_ops pipe_ops =
1278 pipe_windows_fdopen,
1281 ser_base_flush_output,
1282 ser_base_flush_input,
1283 ser_base_send_break,
1285 ser_base_get_tty_state,
1286 ser_base_copy_tty_state,
1287 ser_base_set_tty_state,
1288 ser_base_print_tty_state,
1289 ser_base_noflush_set_tty_state,
1290 ser_base_setbaudrate,
1291 ser_base_setstopbits,
1292 ser_base_drain_output,
1298 pipe_done_wait_handle
1301 /* The TCP/UDP socket driver. */
1303 static const struct serial_ops tcp_ops =
1311 ser_base_flush_output,
1312 ser_base_flush_input,
1315 ser_base_get_tty_state,
1316 ser_base_copy_tty_state,
1317 ser_base_set_tty_state,
1318 ser_base_print_tty_state,
1319 ser_base_noflush_set_tty_state,
1320 ser_base_setbaudrate,
1321 ser_base_setstopbits,
1322 ser_base_drain_output,
1327 net_windows_wait_handle,
1328 net_windows_done_wait_handle
1332 _initialize_ser_windows (void)
1335 struct serial_ops *ops;
1339 /* First find out if kernel32 exports CancelIo function. */
1340 hm = LoadLibrary ("kernel32.dll");
1343 CancelIo = (void *) GetProcAddress (hm, "CancelIo");
1349 serial_add_interface (&hardwire_ops);
1350 serial_add_interface (&tty_ops);
1351 serial_add_interface (&pipe_ops);
1353 /* If WinSock works, register the TCP/UDP socket driver. */
1355 if (WSAStartup (MAKEWORD (1, 0), &wsa_data) != 0)
1356 /* WinSock is unavailable. */
1359 serial_add_interface (&tcp_ops);