serial.h 11 KB

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