1 1.1 christos /* Remote serial support interface definitions for GDB, the GNU Debugger. 2 1.11 christos Copyright (C) 1992-2024 Free Software Foundation, Inc. 3 1.1 christos 4 1.1 christos This file is part of GDB. 5 1.1 christos 6 1.1 christos This program is free software; you can redistribute it and/or modify 7 1.1 christos it under the terms of the GNU General Public License as published by 8 1.1 christos the Free Software Foundation; either version 3 of the License, or 9 1.1 christos (at your option) any later version. 10 1.1 christos 11 1.1 christos This program is distributed in the hope that it will be useful, 12 1.1 christos but WITHOUT ANY WARRANTY; without even the implied warranty of 13 1.1 christos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 1.1 christos GNU General Public License for more details. 15 1.1 christos 16 1.1 christos You should have received a copy of the GNU General Public License 17 1.1 christos along with this program. If not, see <http://www.gnu.org/licenses/>. */ 18 1.1 christos 19 1.1 christos #ifndef SERIAL_H 20 1.1 christos #define SERIAL_H 21 1.1 christos 22 1.1 christos #ifdef USE_WIN32API 23 1.1 christos #include <winsock2.h> 24 1.1 christos #include <windows.h> 25 1.1 christos #endif 26 1.1 christos 27 1.1 christos struct ui_file; 28 1.1 christos 29 1.1 christos /* For most routines, if a failure is indicated, then errno should be 30 1.1 christos examined. */ 31 1.1 christos 32 1.1 christos /* Terminal state pointer. This is specific to each type of 33 1.1 christos interface. */ 34 1.1 christos 35 1.1 christos typedef void *serial_ttystate; 36 1.1 christos struct serial; 37 1.6 christos struct serial_ops; 38 1.6 christos 39 1.10 christos /* Speed in bits per second, or -1 which means don't mess with the speed. */ 40 1.10 christos 41 1.10 christos extern int baud_rate; 42 1.10 christos 43 1.10 christos /* Parity for serial port */ 44 1.10 christos 45 1.10 christos extern int serial_parity; 46 1.10 christos 47 1.6 christos /* Create a new serial for OPS. The new serial is not opened. */ 48 1.1 christos 49 1.1 christos /* Try to open NAME. Returns a new `struct serial *' on success, NULL 50 1.1 christos on failure. The new serial object has a reference count of 1. 51 1.1 christos Note that some open calls can block and, if possible, should be 52 1.1 christos written to be non-blocking, with calls to ui_look_hook so they can 53 1.1 christos be cancelled. An async interface for open could be added to GDB if 54 1.1 christos necessary. */ 55 1.1 christos 56 1.1 christos extern struct serial *serial_open (const char *name); 57 1.1 christos 58 1.6 christos /* Open a new serial stream using OPS. */ 59 1.6 christos 60 1.6 christos extern struct serial *serial_open_ops (const struct serial_ops *ops); 61 1.6 christos 62 1.1 christos /* Returns true if SCB is open. */ 63 1.1 christos 64 1.1 christos extern int serial_is_open (struct serial *scb); 65 1.1 christos 66 1.1 christos /* Find an already opened serial stream using a file handle. */ 67 1.1 christos 68 1.1 christos extern struct serial *serial_for_fd (int fd); 69 1.1 christos 70 1.1 christos /* Open a new serial stream using a file handle. */ 71 1.1 christos 72 1.1 christos extern struct serial *serial_fdopen (const int fd); 73 1.1 christos 74 1.1 christos /* Push out all buffers, close the device and unref SCB. */ 75 1.1 christos 76 1.1 christos extern void serial_close (struct serial *scb); 77 1.1 christos 78 1.1 christos /* Increment reference count of SCB. */ 79 1.1 christos 80 1.1 christos extern void serial_ref (struct serial *scb); 81 1.1 christos 82 1.1 christos /* Decrement reference count of SCB. */ 83 1.1 christos 84 1.1 christos extern void serial_unref (struct serial *scb); 85 1.1 christos 86 1.6 christos /* Create a pipe, and put the read end in FILDES[0], and the write end 87 1.6 christos in FILDES[1]. Returns 0 for success, negative value for error (in 88 1.1 christos which case errno contains the error). */ 89 1.1 christos 90 1.1 christos extern int gdb_pipe (int fildes[2]); 91 1.1 christos 92 1.1 christos /* Create a pipe with each end wrapped in a `struct serial' interface. 93 1.1 christos Put the read end in scbs[0], and the write end in scbs[1]. Returns 94 1.1 christos 0 for success, negative value for error (in which case errno 95 1.1 christos contains the error). */ 96 1.1 christos 97 1.1 christos extern int serial_pipe (struct serial *scbs[2]); 98 1.1 christos 99 1.1 christos /* Push out all buffers and destroy SCB without closing the device. */ 100 1.1 christos 101 1.1 christos extern void serial_un_fdopen (struct serial *scb); 102 1.1 christos 103 1.1 christos /* Read one char from the serial device with TIMEOUT seconds to wait 104 1.1 christos or -1 to wait forever. Use timeout of 0 to effect a poll. 105 1.1 christos Infinite waits are not permitted. Returns unsigned char if ok, else 106 1.1 christos one of the following codes. Note that all error return-codes are 107 1.1 christos guaranteed to be < 0. */ 108 1.1 christos 109 1.1 christos enum serial_rc { 110 1.1 christos SERIAL_ERROR = -1, /* General error. */ 111 1.1 christos SERIAL_TIMEOUT = -2, /* Timeout or data-not-ready during read. 112 1.1 christos Unfortunately, through 113 1.1 christos deprecated_ui_loop_hook (), this can also 114 1.1 christos be a QUIT indication. */ 115 1.1 christos SERIAL_EOF = -3 /* General end-of-file or remote target 116 1.1 christos connection closed, indication. Includes 117 1.1 christos things like the line dropping dead. */ 118 1.1 christos }; 119 1.1 christos 120 1.1 christos extern int serial_readchar (struct serial *scb, int timeout); 121 1.1 christos 122 1.11 christos /* Write COUNT bytes from BUF to the port SCB. Throws exception on 123 1.11 christos error. */ 124 1.1 christos 125 1.11 christos extern void serial_write (struct serial *scb, const void *buf, size_t count); 126 1.1 christos 127 1.1 christos /* Write a printf style string onto the serial port. */ 128 1.1 christos 129 1.1 christos extern void serial_printf (struct serial *desc, 130 1.1 christos const char *,...) ATTRIBUTE_PRINTF (2, 3); 131 1.1 christos 132 1.1 christos /* Allow pending output to drain. */ 133 1.1 christos 134 1.1 christos extern int serial_drain_output (struct serial *); 135 1.1 christos 136 1.1 christos /* Flush (discard) pending output. Might also flush input (if this 137 1.1 christos system can't flush only output). */ 138 1.1 christos 139 1.1 christos extern int serial_flush_output (struct serial *); 140 1.1 christos 141 1.1 christos /* Flush pending input. Might also flush output (if this system can't 142 1.1 christos flush only input). */ 143 1.1 christos 144 1.1 christos extern int serial_flush_input (struct serial *); 145 1.1 christos 146 1.1 christos /* Send a break between 0.25 and 0.5 seconds long. */ 147 1.1 christos 148 1.11 christos extern void serial_send_break (struct serial *scb); 149 1.1 christos 150 1.1 christos /* Turn the port into raw mode. */ 151 1.1 christos 152 1.1 christos extern void serial_raw (struct serial *scb); 153 1.1 christos 154 1.1 christos /* Return a pointer to a newly malloc'd ttystate containing the state 155 1.1 christos of the tty. */ 156 1.1 christos 157 1.1 christos extern serial_ttystate serial_get_tty_state (struct serial *scb); 158 1.1 christos 159 1.1 christos /* Return a pointer to a newly malloc'd ttystate containing a copy 160 1.1 christos of the state in TTYSTATE. */ 161 1.1 christos 162 1.1 christos extern serial_ttystate serial_copy_tty_state (struct serial *scb, 163 1.1 christos serial_ttystate ttystate); 164 1.1 christos 165 1.1 christos /* Set the state of the tty to TTYSTATE. The change is immediate. 166 1.1 christos When changing to or from raw mode, input might be discarded. 167 1.1 christos Returns 0 for success, negative value for error (in which case 168 1.1 christos errno contains the error). */ 169 1.1 christos 170 1.1 christos extern int serial_set_tty_state (struct serial *scb, serial_ttystate ttystate); 171 1.1 christos 172 1.10 christos /* gdb_printf a user-comprehensible description of ttystate on 173 1.1 christos the specified STREAM. FIXME: At present this sends output to the 174 1.1 christos default stream - GDB_STDOUT. */ 175 1.1 christos 176 1.1 christos extern void serial_print_tty_state (struct serial *scb, 177 1.1 christos serial_ttystate ttystate, 178 1.1 christos struct ui_file *); 179 1.1 christos 180 1.11 christos /* Set the baudrate to the decimal value supplied. Throws exception 181 1.11 christos on error. */ 182 1.1 christos 183 1.11 christos extern void serial_setbaudrate (struct serial *scb, int rate); 184 1.1 christos 185 1.1 christos /* Set the number of stop bits to the value specified. Returns 0 for 186 1.1 christos success, -1 for failure. */ 187 1.1 christos 188 1.1 christos #define SERIAL_1_STOPBITS 1 189 1.1 christos #define SERIAL_1_AND_A_HALF_STOPBITS 2 /* 1.5 bits, snicker... */ 190 1.1 christos #define SERIAL_2_STOPBITS 3 191 1.1 christos 192 1.1 christos extern int serial_setstopbits (struct serial *scb, int num); 193 1.1 christos 194 1.5 christos #define GDBPARITY_NONE 0 195 1.5 christos #define GDBPARITY_ODD 1 196 1.5 christos #define GDBPARITY_EVEN 2 197 1.5 christos 198 1.5 christos /* Set parity for serial port. Returns 0 for success, -1 for failure. */ 199 1.5 christos 200 1.5 christos extern int serial_setparity (struct serial *scb, int parity); 201 1.5 christos 202 1.1 christos /* Asynchronous serial interface: */ 203 1.1 christos 204 1.1 christos /* Can the serial device support asynchronous mode? */ 205 1.1 christos 206 1.1 christos extern int serial_can_async_p (struct serial *scb); 207 1.1 christos 208 1.1 christos /* Has the serial device been put in asynchronous mode? */ 209 1.1 christos 210 1.1 christos extern int serial_is_async_p (struct serial *scb); 211 1.1 christos 212 1.1 christos /* For ASYNC enabled devices, register a callback and enable 213 1.1 christos asynchronous mode. To disable asynchronous mode, register a NULL 214 1.1 christos callback. */ 215 1.1 christos 216 1.1 christos typedef void (serial_event_ftype) (struct serial *scb, void *context); 217 1.1 christos extern void serial_async (struct serial *scb, 218 1.1 christos serial_event_ftype *handler, void *context); 219 1.1 christos 220 1.1 christos /* Trace/debug mechanism. 221 1.1 christos 222 1.1 christos serial_debug() enables/disables internal debugging. 223 1.1 christos serial_debug_p() indicates the current debug state. */ 224 1.1 christos 225 1.1 christos extern void serial_debug (struct serial *scb, int debug_p); 226 1.1 christos 227 1.1 christos extern int serial_debug_p (struct serial *scb); 228 1.1 christos 229 1.1 christos 230 1.1 christos /* Details of an instance of a serial object. */ 231 1.1 christos 232 1.1 christos struct serial 233 1.1 christos { 234 1.1 christos /* serial objects are ref counted (but not the underlying 235 1.1 christos connection, just the object's lifetime in memory). */ 236 1.1 christos int refcnt; 237 1.1 christos 238 1.1 christos int fd; /* File descriptor */ 239 1.1 christos /* File descriptor for a separate error stream that should be 240 1.1 christos immediately forwarded to gdb_stderr. This may be -1. 241 1.1 christos If != -1, this descriptor should be non-blocking or 242 1.1 christos ops->avail should be non-NULL. */ 243 1.1 christos int error_fd; 244 1.1 christos const struct serial_ops *ops; /* Function vector */ 245 1.1 christos void *state; /* Local context info for open FD */ 246 1.1 christos serial_ttystate ttystate; /* Not used (yet) */ 247 1.1 christos int bufcnt; /* Amount of data remaining in receive 248 1.1 christos buffer. -ve for sticky errors. */ 249 1.1 christos unsigned char *bufp; /* Current byte */ 250 1.1 christos unsigned char buf[BUFSIZ]; /* Da buffer itself */ 251 1.9 christos char *name; /* The name of the device or host */ 252 1.1 christos struct serial *next; /* Pointer to the next `struct serial *' */ 253 1.1 christos int debug_p; /* Trace this serial devices operation. */ 254 1.1 christos int async_state; /* Async internal state. */ 255 1.1 christos void *async_context; /* Async event thread's context */ 256 1.1 christos serial_event_ftype *async_handler;/* Async event handler */ 257 1.1 christos }; 258 1.1 christos 259 1.1 christos struct serial_ops 260 1.1 christos { 261 1.7 christos const char *name; 262 1.11 christos void (*open) (struct serial *, const char *name); 263 1.1 christos void (*close) (struct serial *); 264 1.1 christos int (*fdopen) (struct serial *, int fd); 265 1.1 christos int (*readchar) (struct serial *, int timeout); 266 1.11 christos void (*write) (struct serial *, const void *buf, size_t count); 267 1.1 christos /* Discard pending output */ 268 1.1 christos int (*flush_output) (struct serial *); 269 1.1 christos /* Discard pending input */ 270 1.1 christos int (*flush_input) (struct serial *); 271 1.11 christos void (*send_break) (struct serial *); 272 1.1 christos void (*go_raw) (struct serial *); 273 1.1 christos serial_ttystate (*get_tty_state) (struct serial *); 274 1.1 christos serial_ttystate (*copy_tty_state) (struct serial *, serial_ttystate); 275 1.1 christos int (*set_tty_state) (struct serial *, serial_ttystate); 276 1.1 christos void (*print_tty_state) (struct serial *, serial_ttystate, 277 1.1 christos struct ui_file *); 278 1.11 christos void (*setbaudrate) (struct serial *, int rate); 279 1.1 christos int (*setstopbits) (struct serial *, int num); 280 1.5 christos /* Set the value PARITY as parity setting for serial object. 281 1.5 christos Return 0 in the case of success. */ 282 1.5 christos int (*setparity) (struct serial *, int parity); 283 1.1 christos /* Wait for output to drain. */ 284 1.1 christos int (*drain_output) (struct serial *); 285 1.1 christos /* Change the serial device into/out of asynchronous mode, call 286 1.1 christos the specified function when ever there is something 287 1.1 christos interesting. */ 288 1.1 christos void (*async) (struct serial *scb, int async_p); 289 1.1 christos /* Perform a low-level read operation, reading (at most) COUNT 290 1.1 christos bytes into SCB->BUF. Return zero at end of file. */ 291 1.1 christos int (*read_prim)(struct serial *scb, size_t count); 292 1.1 christos /* Perform a low-level write operation, writing (at most) COUNT 293 1.1 christos bytes from BUF. */ 294 1.1 christos int (*write_prim)(struct serial *scb, const void *buf, size_t count); 295 1.1 christos /* Return that number of bytes that can be read from FD 296 1.1 christos without blocking. Return value of -1 means that the 297 1.1 christos read will not block even if less that requested bytes 298 1.1 christos are available. */ 299 1.1 christos int (*avail)(struct serial *scb, int fd); 300 1.1 christos 301 1.1 christos #ifdef USE_WIN32API 302 1.1 christos /* Return a handle to wait on, indicating available data from SCB 303 1.1 christos when signaled, in *READ. Return a handle indicating errors 304 1.1 christos in *EXCEPT. */ 305 1.1 christos void (*wait_handle) (struct serial *scb, HANDLE *read, HANDLE *except); 306 1.1 christos void (*done_wait_handle) (struct serial *scb); 307 1.1 christos #endif /* USE_WIN32API */ 308 1.1 christos }; 309 1.1 christos 310 1.1 christos /* Add a new serial interface to the interface list. */ 311 1.1 christos 312 1.1 christos extern void serial_add_interface (const struct serial_ops * optable); 313 1.1 christos 314 1.1 christos /* File in which to record the remote debugging session. */ 315 1.1 christos 316 1.3 christos extern void serial_log_command (struct target_ops *self, const char *); 317 1.1 christos 318 1.1 christos #ifdef USE_WIN32API 319 1.1 christos 320 1.1 christos /* Windows-only: find or create handles that we can wait on for this 321 1.1 christos serial device. */ 322 1.1 christos extern void serial_wait_handle (struct serial *, HANDLE *, HANDLE *); 323 1.1 christos 324 1.1 christos /* Windows-only: signal that we are done with the wait handles. */ 325 1.1 christos extern void serial_done_wait_handle (struct serial *); 326 1.1 christos 327 1.1 christos #endif /* USE_WIN32API */ 328 1.1 christos 329 1.1 christos #endif /* SERIAL_H */ 330