1 /* Serial interface for local (hardwired) serial ports on Windows systems
3 Copyright (C) 2006-2018 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 struct ser_windows_state
42 /* CancelIo is not available for Windows 95 OS, so we need to use
43 LoadLibrary/GetProcAddress to avoid a startup failure. */
44 #define CancelIo dyn_CancelIo
45 typedef BOOL WINAPI (CancelIo_ftype) (HANDLE);
46 static CancelIo_ftype *CancelIo;
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 = XCNEW (struct ser_windows_state);
92 /* Create a manual reset event to watch the input buffer. */
93 state->ov.hEvent = CreateEvent (0, TRUE, FALSE, 0);
95 /* Create a (currently unused) handle to record exceptions. */
96 state->except_event = CreateEvent (0, TRUE, FALSE, 0);
101 /* Wait for the output to drain away, as opposed to flushing (discarding)
105 ser_windows_drain_output (struct serial *scb)
107 HANDLE h = (HANDLE) _get_osfhandle (scb->fd);
109 return (FlushFileBuffers (h) != 0) ? 0 : -1;
113 ser_windows_flush_output (struct serial *scb)
115 HANDLE h = (HANDLE) _get_osfhandle (scb->fd);
117 return (PurgeComm (h, PURGE_TXCLEAR) != 0) ? 0 : -1;
121 ser_windows_flush_input (struct serial *scb)
123 HANDLE h = (HANDLE) _get_osfhandle (scb->fd);
125 return (PurgeComm (h, PURGE_RXCLEAR) != 0) ? 0 : -1;
129 ser_windows_send_break (struct serial *scb)
131 HANDLE h = (HANDLE) _get_osfhandle (scb->fd);
133 if (SetCommBreak (h) == 0)
136 /* Delay for 250 milliseconds. */
139 if (ClearCommBreak (h))
146 ser_windows_raw (struct serial *scb)
148 HANDLE h = (HANDLE) _get_osfhandle (scb->fd);
151 if (GetCommState (h, &state) == 0)
154 state.fOutxCtsFlow = FALSE;
155 state.fOutxDsrFlow = FALSE;
156 state.fDtrControl = DTR_CONTROL_ENABLE;
157 state.fDsrSensitivity = FALSE;
161 state.fAbortOnError = FALSE;
164 if (SetCommState (h, &state) == 0)
165 warning (_("SetCommState failed"));
169 ser_windows_setstopbits (struct serial *scb, int num)
171 HANDLE h = (HANDLE) _get_osfhandle (scb->fd);
174 if (GetCommState (h, &state) == 0)
179 case SERIAL_1_STOPBITS:
180 state.StopBits = ONESTOPBIT;
182 case SERIAL_1_AND_A_HALF_STOPBITS:
183 state.StopBits = ONE5STOPBITS;
185 case SERIAL_2_STOPBITS:
186 state.StopBits = TWOSTOPBITS;
192 return (SetCommState (h, &state) != 0) ? 0 : -1;
195 /* Implement the "setparity" serial_ops callback. */
198 ser_windows_setparity (struct serial *scb, int parity)
200 HANDLE h = (HANDLE) _get_osfhandle (scb->fd);
203 if (GetCommState (h, &state) == 0)
209 state.Parity = NOPARITY;
210 state.fParity = FALSE;
213 state.Parity = ODDPARITY;
214 state.fParity = TRUE;
217 state.Parity = EVENPARITY;
218 state.fParity = TRUE;
221 internal_warning (__FILE__, __LINE__,
222 "Incorrect parity value: %d", parity);
226 return (SetCommState (h, &state) != 0) ? 0 : -1;
230 ser_windows_setbaudrate (struct serial *scb, int rate)
232 HANDLE h = (HANDLE) _get_osfhandle (scb->fd);
235 if (GetCommState (h, &state) == 0)
238 state.BaudRate = rate;
240 return (SetCommState (h, &state) != 0) ? 0 : -1;
244 ser_windows_close (struct serial *scb)
246 struct ser_windows_state *state;
248 /* Stop any pending selects. On Windows 95 OS, CancelIo function does
249 not exist. In that case, it can be replaced by a call to CloseHandle,
250 but this is not necessary here as we do close the Windows handle
251 by calling close (scb->fd) below. */
253 CancelIo ((HANDLE) _get_osfhandle (scb->fd));
254 state = (struct ser_windows_state *) scb->state;
255 CloseHandle (state->ov.hEvent);
256 CloseHandle (state->except_event);
268 ser_windows_wait_handle (struct serial *scb, HANDLE *read, HANDLE *except)
270 struct ser_windows_state *state;
273 HANDLE h = (HANDLE) _get_osfhandle (scb->fd);
275 state = (struct ser_windows_state *) scb->state;
277 *except = state->except_event;
278 *read = state->ov.hEvent;
280 if (state->in_progress)
283 /* Reset the mask - we are only interested in any characters which
284 arrive after this point, not characters which might have arrived
285 and already been read. */
287 /* This really, really shouldn't be necessary - just the second one.
288 But otherwise an internal flag for EV_RXCHAR does not get
289 cleared, and we get a duplicated event, if the last batch
290 of characters included at least two arriving close together. */
291 if (!SetCommMask (h, 0))
292 warning (_("ser_windows_wait_handle: reseting mask failed"));
294 if (!SetCommMask (h, EV_RXCHAR))
295 warning (_("ser_windows_wait_handle: reseting mask failed (2)"));
297 /* There's a potential race condition here; we must check cbInQue
298 and not wait if that's nonzero. */
300 ClearCommError (h, &errors, &status);
301 if (status.cbInQue > 0)
303 SetEvent (state->ov.hEvent);
307 state->in_progress = 1;
308 ResetEvent (state->ov.hEvent);
309 state->lastCommMask = -2;
310 if (WaitCommEvent (h, &state->lastCommMask, &state->ov))
312 gdb_assert (state->lastCommMask & EV_RXCHAR);
313 SetEvent (state->ov.hEvent);
316 gdb_assert (GetLastError () == ERROR_IO_PENDING);
320 ser_windows_read_prim (struct serial *scb, size_t count)
322 struct ser_windows_state *state;
324 DWORD bytes_read, bytes_read_tmp;
328 state = (struct ser_windows_state *) scb->state;
329 if (state->in_progress)
331 WaitForSingleObject (state->ov.hEvent, INFINITE);
332 state->in_progress = 0;
333 ResetEvent (state->ov.hEvent);
336 memset (&ov, 0, sizeof (OVERLAPPED));
337 ov.hEvent = CreateEvent (0, FALSE, FALSE, 0);
338 h = (HANDLE) _get_osfhandle (scb->fd);
340 if (!ReadFile (h, scb->buf, /* count */ 1, &bytes_read, &ov))
342 if (GetLastError () != ERROR_IO_PENDING
343 || !GetOverlappedResult (h, &ov, &bytes_read, TRUE))
347 CloseHandle (ov.hEvent);
352 ser_windows_write_prim (struct serial *scb, const void *buf, size_t len)
354 struct ser_windows_state *state;
359 memset (&ov, 0, sizeof (OVERLAPPED));
360 ov.hEvent = CreateEvent (0, FALSE, FALSE, 0);
361 h = (HANDLE) _get_osfhandle (scb->fd);
362 if (!WriteFile (h, buf, len, &bytes_written, &ov))
364 if (GetLastError () != ERROR_IO_PENDING
365 || !GetOverlappedResult (h, &ov, &bytes_written, TRUE))
369 CloseHandle (ov.hEvent);
370 return bytes_written;
373 /* On Windows, gdb_select is implemented using WaitForMulpleObjects.
374 A "select thread" is created for each file descriptor. These
375 threads looks for activity on the corresponding descriptor, using
376 whatever techniques are appropriate for the descriptor type. When
377 that activity occurs, the thread signals an appropriate event,
378 which wakes up WaitForMultipleObjects.
380 Each select thread is in one of two states: stopped or started.
381 Select threads begin in the stopped state. When gdb_select is
382 called, threads corresponding to the descriptors of interest are
383 started by calling a wait_handle function. Each thread that
384 notices activity signals the appropriate event and then reenters
385 the stopped state. Before gdb_select returns it calls the
386 wait_handle_done functions, which return the threads to the stopped
389 enum select_thread_state {
394 struct ser_console_state
396 /* Signaled by the select thread to indicate that data is available
397 on the file descriptor. */
399 /* Signaled by the select thread to indicate that an exception has
400 occurred on the file descriptor. */
402 /* Signaled by the select thread to indicate that it has entered the
403 started state. HAVE_STARTED and HAVE_STOPPED are never signaled
406 /* Signaled by the select thread to indicate that it has stopped,
407 either because data is available (and READ_EVENT is signaled),
408 because an exception has occurred (and EXCEPT_EVENT is signaled),
409 or because STOP_SELECT was signaled. */
412 /* Signaled by the main program to tell the select thread to enter
413 the started state. */
415 /* Signaled by the main program to tell the select thread to enter
416 the stopped state. */
418 /* Signaled by the main program to tell the select thread to
422 /* The handle for the select thread. */
424 /* The state of the select thread. This field is only accessed in
425 the main program, never by the select thread itself. */
426 enum select_thread_state thread_state;
429 /* Called by a select thread to enter the stopped state. This
430 function does not return until the thread has re-entered the
433 select_thread_wait (struct ser_console_state *state)
435 HANDLE wait_events[2];
437 /* There are two things that can wake us up: a request that we enter
438 the started state, or that we exit this thread. */
439 wait_events[0] = state->start_select;
440 wait_events[1] = state->exit_select;
441 if (WaitForMultipleObjects (2, wait_events, FALSE, INFINITE)
443 /* Either the EXIT_SELECT event was signaled (requesting that the
444 thread exit) or an error has occurred. In either case, we exit
448 /* We are now in the started state. */
449 SetEvent (state->have_started);
452 typedef DWORD WINAPI (*thread_fn_type)(void *);
454 /* Create a new select thread for SCB executing THREAD_FN. The STATE
455 will be filled in by this function before return. */
457 create_select_thread (thread_fn_type thread_fn,
459 struct ser_console_state *state)
463 /* Create all of the events. These are all auto-reset events. */
464 state->read_event = CreateEvent (NULL, FALSE, FALSE, NULL);
465 state->except_event = CreateEvent (NULL, FALSE, FALSE, NULL);
466 state->have_started = CreateEvent (NULL, FALSE, FALSE, NULL);
467 state->have_stopped = CreateEvent (NULL, FALSE, FALSE, NULL);
468 state->start_select = CreateEvent (NULL, FALSE, FALSE, NULL);
469 state->stop_select = CreateEvent (NULL, FALSE, FALSE, NULL);
470 state->exit_select = CreateEvent (NULL, FALSE, FALSE, NULL);
472 state->thread = CreateThread (NULL, 0, thread_fn, scb, 0, &threadId);
473 /* The thread begins in the stopped state. */
474 state->thread_state = STS_STOPPED;
477 /* Destroy the select thread indicated by STATE. */
479 destroy_select_thread (struct ser_console_state *state)
481 /* Ask the thread to exit. */
482 SetEvent (state->exit_select);
483 /* Wait until it does. */
484 WaitForSingleObject (state->thread, INFINITE);
486 /* Destroy the events. */
487 CloseHandle (state->read_event);
488 CloseHandle (state->except_event);
489 CloseHandle (state->have_started);
490 CloseHandle (state->have_stopped);
491 CloseHandle (state->start_select);
492 CloseHandle (state->stop_select);
493 CloseHandle (state->exit_select);
496 /* Called by gdb_select to start the select thread indicated by STATE.
497 This function does not return until the thread has started. */
499 start_select_thread (struct ser_console_state *state)
501 /* Ask the thread to start. */
502 SetEvent (state->start_select);
503 /* Wait until it does. */
504 WaitForSingleObject (state->have_started, INFINITE);
505 /* The thread is now started. */
506 state->thread_state = STS_STARTED;
509 /* Called by gdb_select to stop the select thread indicated by STATE.
510 This function does not return until the thread has stopped. */
512 stop_select_thread (struct ser_console_state *state)
514 /* If the thread is already in the stopped state, we have nothing to
515 do. Some of the wait_handle functions avoid calling
516 start_select_thread if they notice activity on the relevant file
517 descriptors. The wait_handle_done functions still call
518 stop_select_thread -- but it is already stopped. */
519 if (state->thread_state != STS_STARTED)
521 /* Ask the thread to stop. */
522 SetEvent (state->stop_select);
523 /* Wait until it does. */
524 WaitForSingleObject (state->have_stopped, INFINITE);
525 /* The thread is now stopped. */
526 state->thread_state = STS_STOPPED;
530 console_select_thread (void *arg)
532 struct serial *scb = (struct serial *) arg;
533 struct ser_console_state *state;
537 state = (struct ser_console_state *) scb->state;
538 h = (HANDLE) _get_osfhandle (scb->fd);
542 HANDLE wait_events[2];
546 select_thread_wait (state);
550 wait_events[0] = state->stop_select;
553 event_index = WaitForMultipleObjects (2, wait_events,
556 if (event_index == WAIT_OBJECT_0
557 || WaitForSingleObject (state->stop_select, 0) == WAIT_OBJECT_0)
560 if (event_index != WAIT_OBJECT_0 + 1)
562 /* Wait must have failed; assume an error has occured, e.g.
563 the handle has been closed. */
564 SetEvent (state->except_event);
568 /* We've got a pending event on the console. See if it's
570 if (!PeekConsoleInput (h, &record, 1, &n_records) || n_records != 1)
572 /* Something went wrong. Maybe the console is gone. */
573 SetEvent (state->except_event);
577 if (record.EventType == KEY_EVENT && record.Event.KeyEvent.bKeyDown)
579 WORD keycode = record.Event.KeyEvent.wVirtualKeyCode;
581 /* Ignore events containing only control keys. We must
582 recognize "enhanced" keys which we are interested in
583 reading via getch, if they do not map to ASCII. But we
584 do not want to report input available for e.g. the
585 control key alone. */
587 if (record.Event.KeyEvent.uChar.AsciiChar != 0
588 || keycode == VK_PRIOR
589 || keycode == VK_NEXT
591 || keycode == VK_HOME
592 || keycode == VK_LEFT
594 || keycode == VK_RIGHT
595 || keycode == VK_DOWN
596 || keycode == VK_INSERT
597 || keycode == VK_DELETE)
599 /* This is really a keypress. */
600 SetEvent (state->read_event);
605 /* Otherwise discard it and wait again. */
606 ReadConsoleInput (h, &record, 1, &n_records);
609 SetEvent(state->have_stopped);
617 if (PeekNamedPipe ((HANDLE) _get_osfhandle (fd), NULL, 0, NULL, NULL, NULL))
626 if (GetFileType ((HANDLE) _get_osfhandle (fd)) == FILE_TYPE_DISK)
633 pipe_select_thread (void *arg)
635 struct serial *scb = (struct serial *) arg;
636 struct ser_console_state *state;
640 state = (struct ser_console_state *) scb->state;
641 h = (HANDLE) _get_osfhandle (scb->fd);
647 select_thread_wait (state);
649 /* Wait for something to happen on the pipe. */
652 if (!PeekNamedPipe (h, NULL, 0, NULL, &n_avail, NULL))
654 SetEvent (state->except_event);
660 SetEvent (state->read_event);
664 /* Delay 10ms before checking again, but allow the stop
666 if (WaitForSingleObject (state->stop_select, 10) == WAIT_OBJECT_0)
670 SetEvent (state->have_stopped);
676 file_select_thread (void *arg)
678 struct serial *scb = (struct serial *) arg;
679 struct ser_console_state *state;
683 state = (struct ser_console_state *) scb->state;
684 h = (HANDLE) _get_osfhandle (scb->fd);
688 select_thread_wait (state);
690 if (SetFilePointer (h, 0, NULL, FILE_CURRENT)
691 == INVALID_SET_FILE_POINTER)
692 SetEvent (state->except_event);
694 SetEvent (state->read_event);
696 SetEvent (state->have_stopped);
702 ser_console_wait_handle (struct serial *scb, HANDLE *read, HANDLE *except)
704 struct ser_console_state *state = (struct ser_console_state *) scb->state;
708 thread_fn_type thread_fn;
711 is_tty = isatty (scb->fd);
712 if (!is_tty && !fd_is_file (scb->fd) && !fd_is_pipe (scb->fd))
719 state = XCNEW (struct ser_console_state);
723 thread_fn = console_select_thread;
724 else if (fd_is_pipe (scb->fd))
725 thread_fn = pipe_select_thread;
727 thread_fn = file_select_thread;
729 create_select_thread (thread_fn, scb, state);
732 *read = state->read_event;
733 *except = state->except_event;
735 /* Start from a blank state. */
736 ResetEvent (state->read_event);
737 ResetEvent (state->except_event);
738 ResetEvent (state->stop_select);
740 /* First check for a key already in the buffer. If there is one,
741 we don't need a thread. This also catches the second key of
742 multi-character returns from getch, for instance for arrow
743 keys. The second half is in a C library internal buffer,
744 and PeekConsoleInput will not find it. */
747 SetEvent (state->read_event);
751 /* Otherwise, start the select thread. */
752 start_select_thread (state);
756 ser_console_done_wait_handle (struct serial *scb)
758 struct ser_console_state *state = (struct ser_console_state *) scb->state;
763 stop_select_thread (state);
767 ser_console_close (struct serial *scb)
769 struct ser_console_state *state = (struct ser_console_state *) scb->state;
773 destroy_select_thread (state);
778 struct ser_console_ttystate
783 static serial_ttystate
784 ser_console_get_tty_state (struct serial *scb)
786 if (isatty (scb->fd))
788 struct ser_console_ttystate *state;
790 state = XNEW (struct ser_console_ttystate);
800 /* Since we use the pipe_select_thread for our select emulation,
801 we need to place the state structure it requires at the front
803 struct ser_console_state wait;
805 /* The pex obj for our (one-stage) pipeline. */
808 /* Streams for the pipeline's input and output. */
809 FILE *input, *output;
812 static struct pipe_state *
813 make_pipe_state (void)
815 struct pipe_state *ps = XCNEW (struct pipe_state);
817 ps->wait.read_event = INVALID_HANDLE_VALUE;
818 ps->wait.except_event = INVALID_HANDLE_VALUE;
819 ps->wait.start_select = INVALID_HANDLE_VALUE;
820 ps->wait.stop_select = INVALID_HANDLE_VALUE;
826 free_pipe_state (struct pipe_state *ps)
828 int saved_errno = errno;
830 if (ps->wait.read_event != INVALID_HANDLE_VALUE)
831 destroy_select_thread (&ps->wait);
833 /* Close the pipe to the child. We must close the pipe before
834 calling pex_free because pex_free will wait for the child to exit
835 and the child will not exit until the pipe is closed. */
841 /* pex_free closes ps->output. */
851 struct pipe_state_destroyer
853 void operator() (pipe_state *ps) const
855 free_pipe_state (ps);
859 typedef std::unique_ptr<pipe_state, pipe_state_destroyer> pipe_state_up;
862 pipe_windows_open (struct serial *scb, const char *name)
867 error_no_arg (_("child command"));
869 gdb_argv argv (name);
871 if (! argv[0] || argv[0][0] == '\0')
872 error (_("missing child command"));
874 pipe_state_up ps (make_pipe_state ());
876 ps->pex = pex_init (PEX_USE_PIPES, "target remote pipe", NULL);
879 ps->input = pex_input_pipe (ps->pex, 1);
886 = pex_run (ps->pex, PEX_SEARCH | PEX_BINARY_INPUT | PEX_BINARY_OUTPUT
887 | PEX_STDERR_TO_PIPE,
888 argv[0], argv.get (), NULL, NULL,
893 /* Our caller expects us to return -1, but all they'll do with
894 it generally is print the message based on errno. We have
895 all the same information here, plus err_msg provided by
896 pex_run, so we just raise the error here. */
898 error (_("error starting child process '%s': %s: %s"),
899 name, err_msg, safe_strerror (err));
901 error (_("error starting child process '%s': %s"),
906 ps->output = pex_read_output (ps->pex, 1);
909 scb->fd = fileno (ps->output);
911 pex_stderr = pex_read_err (ps->pex, 1);
914 scb->error_fd = fileno (pex_stderr);
916 scb->state = ps.release ();
922 pipe_windows_fdopen (struct serial *scb, int fd)
924 struct pipe_state *ps;
926 ps = make_pipe_state ();
928 ps->input = fdopen (fd, "r+");
932 ps->output = fdopen (fd, "r+");
937 scb->state = (void *) ps;
942 free_pipe_state (ps);
947 pipe_windows_close (struct serial *scb)
949 struct pipe_state *ps = (struct pipe_state *) scb->state;
951 /* In theory, we should try to kill the subprocess here, but the pex
952 interface doesn't give us enough information to do that. Usually
953 closing the input pipe will get the message across. */
955 free_pipe_state (ps);
960 pipe_windows_read (struct serial *scb, size_t count)
962 HANDLE pipeline_out = (HANDLE) _get_osfhandle (scb->fd);
966 if (pipeline_out == INVALID_HANDLE_VALUE)
969 if (! PeekNamedPipe (pipeline_out, NULL, 0, NULL, &available, NULL))
972 if (count > available)
975 if (! ReadFile (pipeline_out, scb->buf, count, &bytes_read, NULL))
983 pipe_windows_write (struct serial *scb, const void *buf, size_t count)
985 struct pipe_state *ps = (struct pipe_state *) scb->state;
989 int pipeline_in_fd = fileno (ps->input);
990 if (pipeline_in_fd < 0)
993 pipeline_in = (HANDLE) _get_osfhandle (pipeline_in_fd);
994 if (pipeline_in == INVALID_HANDLE_VALUE)
997 if (! WriteFile (pipeline_in, buf, count, &written, NULL))
1005 pipe_wait_handle (struct serial *scb, HANDLE *read, HANDLE *except)
1007 struct pipe_state *ps = (struct pipe_state *) scb->state;
1009 /* Have we allocated our events yet? */
1010 if (ps->wait.read_event == INVALID_HANDLE_VALUE)
1011 /* Start the thread. */
1012 create_select_thread (pipe_select_thread, scb, &ps->wait);
1014 *read = ps->wait.read_event;
1015 *except = ps->wait.except_event;
1017 /* Start from a blank state. */
1018 ResetEvent (ps->wait.read_event);
1019 ResetEvent (ps->wait.except_event);
1020 ResetEvent (ps->wait.stop_select);
1022 start_select_thread (&ps->wait);
1026 pipe_done_wait_handle (struct serial *scb)
1028 struct pipe_state *ps = (struct pipe_state *) scb->state;
1030 /* Have we allocated our events yet? */
1031 if (ps->wait.read_event == INVALID_HANDLE_VALUE)
1034 stop_select_thread (&ps->wait);
1038 pipe_avail (struct serial *scb, int fd)
1040 HANDLE h = (HANDLE) _get_osfhandle (fd);
1042 BOOL r = PeekNamedPipe (h, NULL, 0, NULL, &numBytes, NULL);
1050 gdb_pipe (int pdes[2])
1052 if (_pipe (pdes, 512, _O_BINARY | _O_NOINHERIT) == -1)
1057 struct net_windows_state
1059 struct ser_console_state base;
1064 /* Check whether the socket has any pending data to be read. If so,
1065 set the select thread's read event. On error, set the select
1066 thread's except event. If any event was set, return true,
1067 otherwise return false. */
1070 net_windows_socket_check_pending (struct serial *scb)
1072 struct net_windows_state *state = (struct net_windows_state *) scb->state;
1073 unsigned long available;
1075 if (ioctlsocket (scb->fd, FIONREAD, &available) != 0)
1077 /* The socket closed, or some other error. */
1078 SetEvent (state->base.except_event);
1081 else if (available > 0)
1083 SetEvent (state->base.read_event);
1091 net_windows_select_thread (void *arg)
1093 struct serial *scb = (struct serial *) arg;
1094 struct net_windows_state *state;
1097 state = (struct net_windows_state *) scb->state;
1101 HANDLE wait_events[2];
1102 WSANETWORKEVENTS events;
1104 select_thread_wait (&state->base);
1106 wait_events[0] = state->base.stop_select;
1107 wait_events[1] = state->sock_event;
1109 /* Wait for something to happen on the socket. */
1112 event_index = WaitForMultipleObjects (2, wait_events, FALSE, INFINITE);
1114 if (event_index == WAIT_OBJECT_0
1115 || WaitForSingleObject (state->base.stop_select, 0) == WAIT_OBJECT_0)
1117 /* We have been requested to stop. */
1121 if (event_index != WAIT_OBJECT_0 + 1)
1123 /* Some error has occured. Assume that this is an error
1125 SetEvent (state->base.except_event);
1129 /* Enumerate the internal network events, and reset the
1130 object that signalled us to catch the next event. */
1131 if (WSAEnumNetworkEvents (scb->fd, state->sock_event, &events) != 0)
1133 /* Something went wrong. Maybe the socket is gone. */
1134 SetEvent (state->base.except_event);
1138 if (events.lNetworkEvents & FD_READ)
1140 if (net_windows_socket_check_pending (scb))
1143 /* Spurious wakeup. That is, the socket's event was
1144 signalled before we last called recv. */
1147 if (events.lNetworkEvents & FD_CLOSE)
1149 SetEvent (state->base.except_event);
1154 SetEvent (state->base.have_stopped);
1160 net_windows_wait_handle (struct serial *scb, HANDLE *read, HANDLE *except)
1162 struct net_windows_state *state = (struct net_windows_state *) scb->state;
1164 /* Start from a clean slate. */
1165 ResetEvent (state->base.read_event);
1166 ResetEvent (state->base.except_event);
1167 ResetEvent (state->base.stop_select);
1169 *read = state->base.read_event;
1170 *except = state->base.except_event;
1172 /* Check any pending events. Otherwise, start the select
1174 if (!net_windows_socket_check_pending (scb))
1175 start_select_thread (&state->base);
1179 net_windows_done_wait_handle (struct serial *scb)
1181 struct net_windows_state *state = (struct net_windows_state *) scb->state;
1183 stop_select_thread (&state->base);
1187 net_windows_open (struct serial *scb, const char *name)
1189 struct net_windows_state *state;
1193 ret = net_open (scb, name);
1197 state = XCNEW (struct net_windows_state);
1200 /* Associate an event with the socket. */
1201 state->sock_event = CreateEvent (0, TRUE, FALSE, 0);
1202 WSAEventSelect (scb->fd, state->sock_event, FD_READ | FD_CLOSE);
1204 /* Start the thread. */
1205 create_select_thread (net_windows_select_thread, scb, &state->base);
1212 net_windows_close (struct serial *scb)
1214 struct net_windows_state *state = (struct net_windows_state *) scb->state;
1216 destroy_select_thread (&state->base);
1217 CloseHandle (state->sock_event);
1224 /* The serial port driver. */
1226 static const struct serial_ops hardwire_ops =
1234 ser_windows_flush_output,
1235 ser_windows_flush_input,
1236 ser_windows_send_break,
1238 /* These are only used for stdin; we do not need them for serial
1239 ports, so supply the standard dummies. */
1240 ser_base_get_tty_state,
1241 ser_base_copy_tty_state,
1242 ser_base_set_tty_state,
1243 ser_base_print_tty_state,
1244 ser_windows_setbaudrate,
1245 ser_windows_setstopbits,
1246 ser_windows_setparity,
1247 ser_windows_drain_output,
1249 ser_windows_read_prim,
1250 ser_windows_write_prim,
1252 ser_windows_wait_handle
1255 /* The dummy serial driver used for terminals. We only provide the
1256 TTY-related methods. */
1258 static const struct serial_ops tty_ops =
1270 ser_console_get_tty_state,
1271 ser_base_copy_tty_state,
1272 ser_base_set_tty_state,
1273 ser_base_print_tty_state,
1277 ser_base_drain_output,
1282 ser_console_wait_handle,
1283 ser_console_done_wait_handle
1286 /* The pipe interface. */
1288 static const struct serial_ops pipe_ops =
1293 pipe_windows_fdopen,
1296 ser_base_flush_output,
1297 ser_base_flush_input,
1298 ser_base_send_break,
1300 ser_base_get_tty_state,
1301 ser_base_copy_tty_state,
1302 ser_base_set_tty_state,
1303 ser_base_print_tty_state,
1304 ser_base_setbaudrate,
1305 ser_base_setstopbits,
1307 ser_base_drain_output,
1313 pipe_done_wait_handle
1316 /* The TCP/UDP socket driver. */
1318 static const struct serial_ops tcp_ops =
1326 ser_base_flush_output,
1327 ser_base_flush_input,
1330 ser_base_get_tty_state,
1331 ser_base_copy_tty_state,
1332 ser_base_set_tty_state,
1333 ser_base_print_tty_state,
1334 ser_base_setbaudrate,
1335 ser_base_setstopbits,
1337 ser_base_drain_output,
1342 net_windows_wait_handle,
1343 net_windows_done_wait_handle
1347 _initialize_ser_windows (void)
1350 struct serial_ops *ops;
1354 /* First find out if kernel32 exports CancelIo function. */
1355 hm = LoadLibrary ("kernel32.dll");
1358 CancelIo = (CancelIo_ftype *) GetProcAddress (hm, "CancelIo");
1364 serial_add_interface (&hardwire_ops);
1365 serial_add_interface (&tty_ops);
1366 serial_add_interface (&pipe_ops);
1368 /* If WinSock works, register the TCP/UDP socket driver. */
1370 if (WSAStartup (MAKEWORD (1, 0), &wsa_data) != 0)
1371 /* WinSock is unavailable. */
1374 serial_add_interface (&tcp_ops);