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>
32 #include "gdb_assert.h"
37 void _initialize_ser_windows (void);
39 struct ser_windows_state
47 /* CancelIo is not available for Windows 95 OS, so we need to use
48 LoadLibrary/GetProcAddress to avoid a startup failure. */
49 #define CancelIo dyn_CancelIo
50 static BOOL WINAPI (*CancelIo) (HANDLE);
52 /* Open up a real live device for serial I/O. */
55 ser_windows_open (struct serial *scb, const char *name)
58 struct ser_windows_state *state;
59 COMMTIMEOUTS timeouts;
61 h = CreateFile (name, GENERIC_READ | GENERIC_WRITE, 0, NULL,
62 OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
63 if (h == INVALID_HANDLE_VALUE)
69 scb->fd = _open_osfhandle ((intptr_t) h, O_RDWR);
76 if (!SetCommMask (h, EV_RXCHAR))
82 timeouts.ReadIntervalTimeout = MAXDWORD;
83 timeouts.ReadTotalTimeoutConstant = 0;
84 timeouts.ReadTotalTimeoutMultiplier = 0;
85 timeouts.WriteTotalTimeoutConstant = 0;
86 timeouts.WriteTotalTimeoutMultiplier = 0;
87 if (!SetCommTimeouts (h, &timeouts))
93 state = xmalloc (sizeof (struct ser_windows_state));
94 memset (state, 0, sizeof (struct ser_windows_state));
97 /* Create a manual reset event to watch the input buffer. */
98 state->ov.hEvent = CreateEvent (0, TRUE, FALSE, 0);
100 /* Create a (currently unused) handle to record exceptions. */
101 state->except_event = CreateEvent (0, TRUE, FALSE, 0);
106 /* Wait for the output to drain away, as opposed to flushing (discarding)
110 ser_windows_drain_output (struct serial *scb)
112 HANDLE h = (HANDLE) _get_osfhandle (scb->fd);
114 return (FlushFileBuffers (h) != 0) ? 0 : -1;
118 ser_windows_flush_output (struct serial *scb)
120 HANDLE h = (HANDLE) _get_osfhandle (scb->fd);
122 return (PurgeComm (h, PURGE_TXCLEAR) != 0) ? 0 : -1;
126 ser_windows_flush_input (struct serial *scb)
128 HANDLE h = (HANDLE) _get_osfhandle (scb->fd);
130 return (PurgeComm (h, PURGE_RXCLEAR) != 0) ? 0 : -1;
134 ser_windows_send_break (struct serial *scb)
136 HANDLE h = (HANDLE) _get_osfhandle (scb->fd);
138 if (SetCommBreak (h) == 0)
141 /* Delay for 250 milliseconds. */
144 if (ClearCommBreak (h))
151 ser_windows_raw (struct serial *scb)
153 HANDLE h = (HANDLE) _get_osfhandle (scb->fd);
156 if (GetCommState (h, &state) == 0)
159 state.fParity = FALSE;
160 state.fOutxCtsFlow = FALSE;
161 state.fOutxDsrFlow = FALSE;
162 state.fDtrControl = DTR_CONTROL_ENABLE;
163 state.fDsrSensitivity = FALSE;
167 state.fAbortOnError = FALSE;
169 state.Parity = NOPARITY;
171 scb->current_timeout = 0;
173 if (SetCommState (h, &state) == 0)
174 warning (_("SetCommState failed"));
178 ser_windows_setstopbits (struct serial *scb, int num)
180 HANDLE h = (HANDLE) _get_osfhandle (scb->fd);
183 if (GetCommState (h, &state) == 0)
188 case SERIAL_1_STOPBITS:
189 state.StopBits = ONESTOPBIT;
191 case SERIAL_1_AND_A_HALF_STOPBITS:
192 state.StopBits = ONE5STOPBITS;
194 case SERIAL_2_STOPBITS:
195 state.StopBits = TWOSTOPBITS;
201 return (SetCommState (h, &state) != 0) ? 0 : -1;
205 ser_windows_setbaudrate (struct serial *scb, int rate)
207 HANDLE h = (HANDLE) _get_osfhandle (scb->fd);
210 if (GetCommState (h, &state) == 0)
213 state.BaudRate = rate;
215 return (SetCommState (h, &state) != 0) ? 0 : -1;
219 ser_windows_close (struct serial *scb)
221 struct ser_windows_state *state;
223 /* Stop any pending selects. On Windows 95 OS, CancelIo function does
224 not exist. In that case, it can be replaced by a call to CloseHandle,
225 but this is not necessary here as we do close the Windows handle
226 by calling close (scb->fd) below. */
228 CancelIo ((HANDLE) _get_osfhandle (scb->fd));
230 CloseHandle (state->ov.hEvent);
231 CloseHandle (state->except_event);
243 ser_windows_wait_handle (struct serial *scb, HANDLE *read, HANDLE *except)
245 struct ser_windows_state *state;
248 HANDLE h = (HANDLE) _get_osfhandle (scb->fd);
252 *except = state->except_event;
253 *read = state->ov.hEvent;
255 if (state->in_progress)
258 /* Reset the mask - we are only interested in any characters which
259 arrive after this point, not characters which might have arrived
260 and already been read. */
262 /* This really, really shouldn't be necessary - just the second one.
263 But otherwise an internal flag for EV_RXCHAR does not get
264 cleared, and we get a duplicated event, if the last batch
265 of characters included at least two arriving close together. */
266 if (!SetCommMask (h, 0))
267 warning (_("ser_windows_wait_handle: reseting mask failed"));
269 if (!SetCommMask (h, EV_RXCHAR))
270 warning (_("ser_windows_wait_handle: reseting mask failed (2)"));
272 /* There's a potential race condition here; we must check cbInQue
273 and not wait if that's nonzero. */
275 ClearCommError (h, &errors, &status);
276 if (status.cbInQue > 0)
278 SetEvent (state->ov.hEvent);
282 state->in_progress = 1;
283 ResetEvent (state->ov.hEvent);
284 state->lastCommMask = -2;
285 if (WaitCommEvent (h, &state->lastCommMask, &state->ov))
287 gdb_assert (state->lastCommMask & EV_RXCHAR);
288 SetEvent (state->ov.hEvent);
291 gdb_assert (GetLastError () == ERROR_IO_PENDING);
295 ser_windows_read_prim (struct serial *scb, size_t count)
297 struct ser_windows_state *state;
299 DWORD bytes_read, bytes_read_tmp;
304 if (state->in_progress)
306 WaitForSingleObject (state->ov.hEvent, INFINITE);
307 state->in_progress = 0;
308 ResetEvent (state->ov.hEvent);
311 memset (&ov, 0, sizeof (OVERLAPPED));
312 ov.hEvent = CreateEvent (0, FALSE, FALSE, 0);
313 h = (HANDLE) _get_osfhandle (scb->fd);
315 if (!ReadFile (h, scb->buf, /* count */ 1, &bytes_read, &ov))
317 if (GetLastError () != ERROR_IO_PENDING
318 || !GetOverlappedResult (h, &ov, &bytes_read, TRUE))
322 CloseHandle (ov.hEvent);
327 ser_windows_write_prim (struct serial *scb, const void *buf, size_t len)
329 struct ser_windows_state *state;
334 memset (&ov, 0, sizeof (OVERLAPPED));
335 ov.hEvent = CreateEvent (0, FALSE, FALSE, 0);
336 h = (HANDLE) _get_osfhandle (scb->fd);
337 if (!WriteFile (h, buf, len, &bytes_written, &ov))
339 if (GetLastError () != ERROR_IO_PENDING
340 || !GetOverlappedResult (h, &ov, &bytes_written, TRUE))
344 CloseHandle (ov.hEvent);
345 return bytes_written;
348 /* On Windows, gdb_select is implemented using WaitForMulpleObjects.
349 A "select thread" is created for each file descriptor. These
350 threads looks for activity on the corresponding descriptor, using
351 whatever techniques are appropriate for the descriptor type. When
352 that activity occurs, the thread signals an appropriate event,
353 which wakes up WaitForMultipleObjects.
355 Each select thread is in one of two states: stopped or started.
356 Select threads begin in the stopped state. When gdb_select is
357 called, threads corresponding to the descriptors of interest are
358 started by calling a wait_handle function. Each thread that
359 notices activity signals the appropriate event and then reenters
360 the stopped state. Before gdb_select returns it calls the
361 wait_handle_done functions, which return the threads to the stopped
364 enum select_thread_state {
369 struct ser_console_state
371 /* Signaled by the select thread to indicate that data is available
372 on the file descriptor. */
374 /* Signaled by the select thread to indicate that an exception has
375 occurred on the file descriptor. */
377 /* Signaled by the select thread to indicate that it has entered the
378 started state. HAVE_STARTED and HAVE_STOPPED are never signaled
381 /* Signaled by the select thread to indicate that it has stopped,
382 either because data is available (and READ_EVENT is signaled),
383 because an exception has occurred (and EXCEPT_EVENT is signaled),
384 or because STOP_SELECT was signaled. */
387 /* Signaled by the main program to tell the select thread to enter
388 the started state. */
390 /* Signaled by the main program to tell the select thread to enter
391 the stopped state. */
393 /* Signaled by the main program to tell the select thread to
397 /* The handle for the select thread. */
399 /* The state of the select thread. This field is only accessed in
400 the main program, never by the select thread itself. */
401 enum select_thread_state thread_state;
404 /* Called by a select thread to enter the stopped state. This
405 function does not return until the thread has re-entered the
408 select_thread_wait (struct ser_console_state *state)
410 HANDLE wait_events[2];
412 /* There are two things that can wake us up: a request that we enter
413 the started state, or that we exit this thread. */
414 wait_events[0] = state->start_select;
415 wait_events[1] = state->exit_select;
416 if (WaitForMultipleObjects (2, wait_events, FALSE, INFINITE)
418 /* Either the EXIT_SELECT event was signaled (requesting that the
419 thread exit) or an error has occurred. In either case, we exit
423 /* We are now in the started state. */
424 SetEvent (state->have_started);
427 typedef DWORD WINAPI (*thread_fn_type)(void *);
429 /* Create a new select thread for SCB executing THREAD_FN. The STATE
430 will be filled in by this function before return. */
432 create_select_thread (thread_fn_type thread_fn,
434 struct ser_console_state *state)
438 /* Create all of the events. These are all auto-reset events. */
439 state->read_event = CreateEvent (NULL, FALSE, FALSE, NULL);
440 state->except_event = CreateEvent (NULL, FALSE, FALSE, NULL);
441 state->have_started = CreateEvent (NULL, FALSE, FALSE, NULL);
442 state->have_stopped = CreateEvent (NULL, FALSE, FALSE, NULL);
443 state->start_select = CreateEvent (NULL, FALSE, FALSE, NULL);
444 state->stop_select = CreateEvent (NULL, FALSE, FALSE, NULL);
445 state->exit_select = CreateEvent (NULL, FALSE, FALSE, NULL);
447 state->thread = CreateThread (NULL, 0, thread_fn, scb, 0, &threadId);
448 /* The thread begins in the stopped state. */
449 state->thread_state = STS_STOPPED;
452 /* Destroy the select thread indicated by STATE. */
454 destroy_select_thread (struct ser_console_state *state)
456 /* Ask the thread to exit. */
457 SetEvent (state->exit_select);
458 /* Wait until it does. */
459 WaitForSingleObject (state->thread, INFINITE);
461 /* Destroy the events. */
462 CloseHandle (state->read_event);
463 CloseHandle (state->except_event);
464 CloseHandle (state->have_started);
465 CloseHandle (state->have_stopped);
466 CloseHandle (state->start_select);
467 CloseHandle (state->stop_select);
468 CloseHandle (state->exit_select);
471 /* Called by gdb_select to start the select thread indicated by STATE.
472 This function does not return until the thread has started. */
474 start_select_thread (struct ser_console_state *state)
476 /* Ask the thread to start. */
477 SetEvent (state->start_select);
478 /* Wait until it does. */
479 WaitForSingleObject (state->have_started, INFINITE);
480 /* The thread is now started. */
481 state->thread_state = STS_STARTED;
484 /* Called by gdb_select to stop the select thread indicated by STATE.
485 This function does not return until the thread has stopped. */
487 stop_select_thread (struct ser_console_state *state)
489 /* If the thread is already in the stopped state, we have nothing to
490 do. Some of the wait_handle functions avoid calling
491 start_select_thread if they notice activity on the relevant file
492 descriptors. The wait_handle_done functions still call
493 stop_select_thread -- but it is already stopped. */
494 if (state->thread_state != STS_STARTED)
496 /* Ask the thread to stop. */
497 SetEvent (state->stop_select);
498 /* Wait until it does. */
499 WaitForSingleObject (state->have_stopped, INFINITE);
500 /* The thread is now stopped. */
501 state->thread_state = STS_STOPPED;
505 console_select_thread (void *arg)
507 struct serial *scb = arg;
508 struct ser_console_state *state;
513 h = (HANDLE) _get_osfhandle (scb->fd);
517 HANDLE wait_events[2];
521 select_thread_wait (state);
525 wait_events[0] = state->stop_select;
528 event_index = WaitForMultipleObjects (2, wait_events,
531 if (event_index == WAIT_OBJECT_0
532 || WaitForSingleObject (state->stop_select, 0) == WAIT_OBJECT_0)
535 if (event_index != WAIT_OBJECT_0 + 1)
537 /* Wait must have failed; assume an error has occured, e.g.
538 the handle has been closed. */
539 SetEvent (state->except_event);
543 /* We've got a pending event on the console. See if it's
545 if (!PeekConsoleInput (h, &record, 1, &n_records) || n_records != 1)
547 /* Something went wrong. Maybe the console is gone. */
548 SetEvent (state->except_event);
552 if (record.EventType == KEY_EVENT && record.Event.KeyEvent.bKeyDown)
554 WORD keycode = record.Event.KeyEvent.wVirtualKeyCode;
556 /* Ignore events containing only control keys. We must
557 recognize "enhanced" keys which we are interested in
558 reading via getch, if they do not map to ASCII. But we
559 do not want to report input available for e.g. the
560 control key alone. */
562 if (record.Event.KeyEvent.uChar.AsciiChar != 0
563 || keycode == VK_PRIOR
564 || keycode == VK_NEXT
566 || keycode == VK_HOME
567 || keycode == VK_LEFT
569 || keycode == VK_RIGHT
570 || keycode == VK_DOWN
571 || keycode == VK_INSERT
572 || keycode == VK_DELETE)
574 /* This is really a keypress. */
575 SetEvent (state->read_event);
580 /* Otherwise discard it and wait again. */
581 ReadConsoleInput (h, &record, 1, &n_records);
584 SetEvent(state->have_stopped);
592 if (PeekNamedPipe ((HANDLE) _get_osfhandle (fd), NULL, 0, NULL, NULL, NULL))
601 if (GetFileType ((HANDLE) _get_osfhandle (fd)) == FILE_TYPE_DISK)
608 pipe_select_thread (void *arg)
610 struct serial *scb = arg;
611 struct ser_console_state *state;
616 h = (HANDLE) _get_osfhandle (scb->fd);
622 select_thread_wait (state);
624 /* Wait for something to happen on the pipe. */
627 if (!PeekNamedPipe (h, NULL, 0, NULL, &n_avail, NULL))
629 SetEvent (state->except_event);
635 SetEvent (state->read_event);
639 /* Delay 10ms before checking again, but allow the stop
641 if (WaitForSingleObject (state->stop_select, 10) == WAIT_OBJECT_0)
645 SetEvent (state->have_stopped);
651 file_select_thread (void *arg)
653 struct serial *scb = arg;
654 struct ser_console_state *state;
659 h = (HANDLE) _get_osfhandle (scb->fd);
663 select_thread_wait (state);
665 if (SetFilePointer (h, 0, NULL, FILE_CURRENT)
666 == INVALID_SET_FILE_POINTER)
667 SetEvent (state->except_event);
669 SetEvent (state->read_event);
671 SetEvent (state->have_stopped);
677 ser_console_wait_handle (struct serial *scb, HANDLE *read, HANDLE *except)
679 struct ser_console_state *state = scb->state;
683 thread_fn_type thread_fn;
686 is_tty = isatty (scb->fd);
687 if (!is_tty && !fd_is_file (scb->fd) && !fd_is_pipe (scb->fd))
694 state = xmalloc (sizeof (struct ser_console_state));
695 memset (state, 0, sizeof (struct ser_console_state));
699 thread_fn = console_select_thread;
700 else if (fd_is_pipe (scb->fd))
701 thread_fn = pipe_select_thread;
703 thread_fn = file_select_thread;
705 create_select_thread (thread_fn, scb, state);
708 *read = state->read_event;
709 *except = state->except_event;
711 /* Start from a blank state. */
712 ResetEvent (state->read_event);
713 ResetEvent (state->except_event);
714 ResetEvent (state->stop_select);
716 /* First check for a key already in the buffer. If there is one,
717 we don't need a thread. This also catches the second key of
718 multi-character returns from getch, for instance for arrow
719 keys. The second half is in a C library internal buffer,
720 and PeekConsoleInput will not find it. */
723 SetEvent (state->read_event);
727 /* Otherwise, start the select thread. */
728 start_select_thread (state);
732 ser_console_done_wait_handle (struct serial *scb)
734 struct ser_console_state *state = scb->state;
739 stop_select_thread (state);
743 ser_console_close (struct serial *scb)
745 struct ser_console_state *state = scb->state;
749 destroy_select_thread (state);
754 struct ser_console_ttystate
759 static serial_ttystate
760 ser_console_get_tty_state (struct serial *scb)
762 if (isatty (scb->fd))
764 struct ser_console_ttystate *state;
766 state = (struct ser_console_ttystate *) xmalloc (sizeof *state);
776 /* Since we use the pipe_select_thread for our select emulation,
777 we need to place the state structure it requires at the front
779 struct ser_console_state wait;
781 /* The pex obj for our (one-stage) pipeline. */
784 /* Streams for the pipeline's input and output. */
785 FILE *input, *output;
788 static struct pipe_state *
789 make_pipe_state (void)
791 struct pipe_state *ps = XNEW (struct pipe_state);
793 memset (ps, 0, sizeof (*ps));
794 ps->wait.read_event = INVALID_HANDLE_VALUE;
795 ps->wait.except_event = INVALID_HANDLE_VALUE;
796 ps->wait.start_select = INVALID_HANDLE_VALUE;
797 ps->wait.stop_select = INVALID_HANDLE_VALUE;
803 free_pipe_state (struct pipe_state *ps)
805 int saved_errno = errno;
807 if (ps->wait.read_event != INVALID_HANDLE_VALUE)
808 destroy_select_thread (&ps->wait);
810 /* Close the pipe to the child. We must close the pipe before
811 calling pex_free because pex_free will wait for the child to exit
812 and the child will not exit until the pipe is closed. */
818 /* pex_free closes ps->output. */
829 cleanup_pipe_state (void *untyped)
831 struct pipe_state *ps = untyped;
833 free_pipe_state (ps);
837 pipe_windows_open (struct serial *scb, const char *name)
839 struct pipe_state *ps;
842 struct cleanup *back_to;
845 error_no_arg (_("child command"));
847 argv = gdb_buildargv (name);
848 back_to = make_cleanup_freeargv (argv);
850 if (! argv[0] || argv[0][0] == '\0')
851 error (_("missing child command"));
853 ps = make_pipe_state ();
854 make_cleanup (cleanup_pipe_state, ps);
856 ps->pex = pex_init (PEX_USE_PIPES, "target remote pipe", NULL);
859 ps->input = pex_input_pipe (ps->pex, 1);
866 = pex_run (ps->pex, PEX_SEARCH | PEX_BINARY_INPUT | PEX_BINARY_OUTPUT
867 | PEX_STDERR_TO_PIPE,
868 argv[0], argv, NULL, NULL,
873 /* Our caller expects us to return -1, but all they'll do with
874 it generally is print the message based on errno. We have
875 all the same information here, plus err_msg provided by
876 pex_run, so we just raise the error here. */
878 error (_("error starting child process '%s': %s: %s"),
879 name, err_msg, safe_strerror (err));
881 error (_("error starting child process '%s': %s"),
886 ps->output = pex_read_output (ps->pex, 1);
889 scb->fd = fileno (ps->output);
891 pex_stderr = pex_read_err (ps->pex, 1);
894 scb->error_fd = fileno (pex_stderr);
896 scb->state = (void *) ps;
898 discard_cleanups (back_to);
902 do_cleanups (back_to);
907 pipe_windows_fdopen (struct serial *scb, int fd)
909 struct pipe_state *ps;
911 ps = make_pipe_state ();
913 ps->input = fdopen (fd, "r+");
917 ps->output = fdopen (fd, "r+");
922 scb->state = (void *) ps;
927 free_pipe_state (ps);
932 pipe_windows_close (struct serial *scb)
934 struct pipe_state *ps = scb->state;
936 /* In theory, we should try to kill the subprocess here, but the pex
937 interface doesn't give us enough information to do that. Usually
938 closing the input pipe will get the message across. */
940 free_pipe_state (ps);
945 pipe_windows_read (struct serial *scb, size_t count)
947 HANDLE pipeline_out = (HANDLE) _get_osfhandle (scb->fd);
951 if (pipeline_out == INVALID_HANDLE_VALUE)
954 if (! PeekNamedPipe (pipeline_out, NULL, 0, NULL, &available, NULL))
957 if (count > available)
960 if (! ReadFile (pipeline_out, scb->buf, count, &bytes_read, NULL))
968 pipe_windows_write (struct serial *scb, const void *buf, size_t count)
970 struct pipe_state *ps = scb->state;
974 int pipeline_in_fd = fileno (ps->input);
975 if (pipeline_in_fd < 0)
978 pipeline_in = (HANDLE) _get_osfhandle (pipeline_in_fd);
979 if (pipeline_in == INVALID_HANDLE_VALUE)
982 if (! WriteFile (pipeline_in, buf, count, &written, NULL))
990 pipe_wait_handle (struct serial *scb, HANDLE *read, HANDLE *except)
992 struct pipe_state *ps = scb->state;
994 /* Have we allocated our events yet? */
995 if (ps->wait.read_event == INVALID_HANDLE_VALUE)
996 /* Start the thread. */
997 create_select_thread (pipe_select_thread, scb, &ps->wait);
999 *read = ps->wait.read_event;
1000 *except = ps->wait.except_event;
1002 /* Start from a blank state. */
1003 ResetEvent (ps->wait.read_event);
1004 ResetEvent (ps->wait.except_event);
1005 ResetEvent (ps->wait.stop_select);
1007 start_select_thread (&ps->wait);
1011 pipe_done_wait_handle (struct serial *scb)
1013 struct pipe_state *ps = scb->state;
1015 /* Have we allocated our events yet? */
1016 if (ps->wait.read_event == INVALID_HANDLE_VALUE)
1019 stop_select_thread (&ps->wait);
1023 pipe_avail (struct serial *scb, int fd)
1025 HANDLE h = (HANDLE) _get_osfhandle (fd);
1027 BOOL r = PeekNamedPipe (h, NULL, 0, NULL, &numBytes, NULL);
1035 gdb_pipe (int pdes[2])
1037 if (_pipe (pdes, 512, _O_BINARY | _O_NOINHERIT) == -1)
1042 struct net_windows_state
1044 struct ser_console_state base;
1049 /* Check whether the socket has any pending data to be read. If so,
1050 set the select thread's read event. On error, set the select
1051 thread's except event. If any event was set, return true,
1052 otherwise return false. */
1055 net_windows_socket_check_pending (struct serial *scb)
1057 struct net_windows_state *state = scb->state;
1058 unsigned long available;
1060 if (ioctlsocket (scb->fd, FIONREAD, &available) != 0)
1062 /* The socket closed, or some other error. */
1063 SetEvent (state->base.except_event);
1066 else if (available > 0)
1068 SetEvent (state->base.read_event);
1076 net_windows_select_thread (void *arg)
1078 struct serial *scb = arg;
1079 struct net_windows_state *state;
1086 HANDLE wait_events[2];
1087 WSANETWORKEVENTS events;
1089 select_thread_wait (&state->base);
1091 wait_events[0] = state->base.stop_select;
1092 wait_events[1] = state->sock_event;
1094 /* Wait for something to happen on the socket. */
1097 event_index = WaitForMultipleObjects (2, wait_events, FALSE, INFINITE);
1099 if (event_index == WAIT_OBJECT_0
1100 || WaitForSingleObject (state->base.stop_select, 0) == WAIT_OBJECT_0)
1102 /* We have been requested to stop. */
1106 if (event_index != WAIT_OBJECT_0 + 1)
1108 /* Some error has occured. Assume that this is an error
1110 SetEvent (state->base.except_event);
1114 /* Enumerate the internal network events, and reset the
1115 object that signalled us to catch the next event. */
1116 if (WSAEnumNetworkEvents (scb->fd, state->sock_event, &events) != 0)
1118 /* Something went wrong. Maybe the socket is gone. */
1119 SetEvent (state->base.except_event);
1123 if (events.lNetworkEvents & FD_READ)
1125 if (net_windows_socket_check_pending (scb))
1128 /* Spurious wakeup. That is, the socket's event was
1129 signalled before we last called recv. */
1132 if (events.lNetworkEvents & FD_CLOSE)
1134 SetEvent (state->base.except_event);
1139 SetEvent (state->base.have_stopped);
1145 net_windows_wait_handle (struct serial *scb, HANDLE *read, HANDLE *except)
1147 struct net_windows_state *state = scb->state;
1149 /* Start from a clean slate. */
1150 ResetEvent (state->base.read_event);
1151 ResetEvent (state->base.except_event);
1152 ResetEvent (state->base.stop_select);
1154 *read = state->base.read_event;
1155 *except = state->base.except_event;
1157 /* Check any pending events. Otherwise, start the select
1159 if (!net_windows_socket_check_pending (scb))
1160 start_select_thread (&state->base);
1164 net_windows_done_wait_handle (struct serial *scb)
1166 struct net_windows_state *state = scb->state;
1168 stop_select_thread (&state->base);
1172 net_windows_open (struct serial *scb, const char *name)
1174 struct net_windows_state *state;
1178 ret = net_open (scb, name);
1182 state = xmalloc (sizeof (struct net_windows_state));
1183 memset (state, 0, sizeof (struct net_windows_state));
1186 /* Associate an event with the socket. */
1187 state->sock_event = CreateEvent (0, TRUE, FALSE, 0);
1188 WSAEventSelect (scb->fd, state->sock_event, FD_READ | FD_CLOSE);
1190 /* Start the thread. */
1191 create_select_thread (net_windows_select_thread, scb, &state->base);
1198 net_windows_close (struct serial *scb)
1200 struct net_windows_state *state = scb->state;
1202 destroy_select_thread (&state->base);
1203 CloseHandle (state->sock_event);
1210 /* The serial port driver. */
1212 static const struct serial_ops hardwire_ops =
1220 ser_windows_flush_output,
1221 ser_windows_flush_input,
1222 ser_windows_send_break,
1224 /* These are only used for stdin; we do not need them for serial
1225 ports, so supply the standard dummies. */
1226 ser_base_get_tty_state,
1227 ser_base_copy_tty_state,
1228 ser_base_set_tty_state,
1229 ser_base_print_tty_state,
1230 ser_base_noflush_set_tty_state,
1231 ser_windows_setbaudrate,
1232 ser_windows_setstopbits,
1233 ser_windows_drain_output,
1235 ser_windows_read_prim,
1236 ser_windows_write_prim,
1238 ser_windows_wait_handle
1241 /* The dummy serial driver used for terminals. We only provide the
1242 TTY-related methods. */
1244 static const struct serial_ops tty_ops =
1256 ser_console_get_tty_state,
1257 ser_base_copy_tty_state,
1258 ser_base_set_tty_state,
1259 ser_base_print_tty_state,
1260 ser_base_noflush_set_tty_state,
1263 ser_base_drain_output,
1268 ser_console_wait_handle,
1269 ser_console_done_wait_handle
1272 /* The pipe interface. */
1274 static const struct serial_ops pipe_ops =
1279 pipe_windows_fdopen,
1282 ser_base_flush_output,
1283 ser_base_flush_input,
1284 ser_base_send_break,
1286 ser_base_get_tty_state,
1287 ser_base_copy_tty_state,
1288 ser_base_set_tty_state,
1289 ser_base_print_tty_state,
1290 ser_base_noflush_set_tty_state,
1291 ser_base_setbaudrate,
1292 ser_base_setstopbits,
1293 ser_base_drain_output,
1299 pipe_done_wait_handle
1302 /* The TCP/UDP socket driver. */
1304 static const struct serial_ops tcp_ops =
1312 ser_base_flush_output,
1313 ser_base_flush_input,
1316 ser_base_get_tty_state,
1317 ser_base_copy_tty_state,
1318 ser_base_set_tty_state,
1319 ser_base_print_tty_state,
1320 ser_base_noflush_set_tty_state,
1321 ser_base_setbaudrate,
1322 ser_base_setstopbits,
1323 ser_base_drain_output,
1328 net_windows_wait_handle,
1329 net_windows_done_wait_handle
1333 _initialize_ser_windows (void)
1336 struct serial_ops *ops;
1340 /* First find out if kernel32 exports CancelIo function. */
1341 hm = LoadLibrary ("kernel32.dll");
1344 CancelIo = (void *) GetProcAddress (hm, "CancelIo");
1350 serial_add_interface (&hardwire_ops);
1351 serial_add_interface (&tty_ops);
1352 serial_add_interface (&pipe_ops);
1354 /* If WinSock works, register the TCP/UDP socket driver. */
1356 if (WSAStartup (MAKEWORD (1, 0), &wsa_data) != 0)
1357 /* WinSock is unavailable. */
1360 serial_add_interface (&tcp_ops);