BOROSOFTWAREAll work →
scantool/diag_tty.h 134 lines
1#ifndef _DIAG_TTY_H_
2#define _DIAG_TTY_H_
3
4/*
5 * Serial port settings.
6 * We allow most expected settings, except that we need to
7 * support arbitrary speeds for some L0 links.
8 */
9
10
11#include "diag.h"
12#include "diag_l0.h" //needed for diag_l0_debug
13
14#define IFLUSH_TIMEOUT 30 //timeout to use when calling diag_tty_read from diag_tty_iflush to purge RX buffer.
15//must not be too long or diag_l0_dumb:slowinit() will not work
16#define MAXTIMEOUT 10000 //ms; for diag_tty_read()
17
18/*
19 * Parity settings
20 */
21enum diag_parity {
22 diag_par_e = 1, /* Even parity */
23 diag_par_o = 2, /* Odd parity */
24 diag_par_n = 3 /* No parity */
25};
26
27enum diag_databits {
28 diag_databits_8 = 8,
29 diag_databits_7 = 7,
30 diag_databits_6 = 6,
31 diag_databits_5 = 5
32};
33
34enum diag_stopbits {
35 diag_stopbits_1 = 1,
36 diag_stopbits_2 = 2
37};
38
39struct diag_serial_settings {
40 unsigned int speed; //in bps of course
41 enum diag_databits databits;
42 enum diag_stopbits stopbits;
43 enum diag_parity parflag;
44};
45
46
47/*** Public functions ***/
48typedef void ttyp; //used as "(tty_internal_struct *) ttyp" in tty code
49
50/** Get available serial ports
51 *
52 * @param[out] numports : will hold the # of ports found.
53 * @return argv-style list of full port names, that must be free'd with
54 * strlist_free()
55 *
56 */
57char **diag_tty_getportlist(int *numports);
58
59/** Open serial port
60 * @param portname: serial port device / file / tty name
61 * @return new ttyp handle if ok, NULL if failed
62 */
63ttyp *diag_tty_open(const char *portname);
64
65/** Close serial port
66 *
67 * Also frees everything allocated in diag_tty_open().
68 */
69void diag_tty_close(ttyp *tty_int);
70
71/** Set speed/parity.
72 *
73 * @return 0 if ok.
74 */
75int diag_tty_setup(ttyp *tty_int,
76 const struct diag_serial_settings *pss);
77
78/** Set DTR and RTS lines.
79 *
80 * Terminology : rts=1 or dtr=1 ==\> set DTR/RTS ==\> set pin at positive voltage !
81 * (opposite polarity of the TX/RX pins!!)
82 * @return 0 if ok
83 */
84int diag_tty_control(ttyp *tty_int, unsigned int dtr, unsigned int rts);
85
86
87/** Flush pending input.
88 *
89 * This probably always takes IFLUSH_TIMEOUT to complete since it calls diag_tty_read.
90 * @return 0 if ok
91 */
92int diag_tty_iflush(ttyp *tty_int);
93
94// diag_tty_read : (count >0 && timeout >0)
95// a) read up to (count) bytes until (timeout) expires; return the # of bytes read.
96// b) if no bytes were read and timeout expired: return DIAG_ERR_TIMEOUT
97// *without* diag_iseterr(); L2 code uses this for message splitting.
98// c) if there was a real error, return diag_iseterr(x)
99// d) never return 0
100// TODO : clarify if calling with timeout==0 is useful (probably not, nobody does).
101ssize_t diag_tty_read(ttyp *tty_int,
102 void *buf, size_t count, unsigned int timeout);
103
104/** Write bytes to tty (blocking).
105 *
106 * @param count: Attempt to write [count] bytes, block (== do not return) until write has completed.
107 * @return # of bytes written; \<0 if error.
108 * @note It is unclear whether the different OS mechanisms to flush write buffers actually
109 * guarantee that serial data has physically sent,
110 * or only that the data was flushed as far "downstream" as possible, for example
111 * to a UART / device driver buffer.
112 */
113ssize_t diag_tty_write(ttyp *tty_int,
114 const void *buf, const size_t count);
115
116
117/** Send a break on TXD.
118 * @param ms: duration (milliseconds)
119 * @return 0 if ok, after clearing break
120 */
121int diag_tty_break(ttyp *tty_int, const unsigned int ms);
122
123/** Send fixed 25ms break pattern on TXD.
124 *
125 * Sets break for 25ms and returns after requested duration.
126 * This is for ISO14230 fast init : typically diag_tty_fastbreak(tty_int, 50)
127 * @param ms: Total pattern length (\>25ms)
128 * @return 0 if ok; returns [ms] after starting the break.
129 */
130int diag_tty_fastbreak(ttyp *tty_int, const unsigned int ms);
131
132
133#endif /* _DIAG_TTY_H_ */
134