aboutsummaryrefslogtreecommitdiffstats
path: root/serial.c
blob: 2066448b0134aac0c770717891058e354c32c8be (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/*
 * Open a host serial port, and configure it for a specific speed
 * and, optionally, enable flow control. This is horrendously target-specific.
 */

#include "compiler.h"
#include "serial.h"

#ifdef _WIN32
# define NOPREFIX   "/.\\"
# define _PATH_DEV  "\\\\.\\"
#else
# define NOPREFIX "/."
# ifndef _PATH_DEV
#  define _PATH_DEV "/dev/"
# endif
#endif

static char *strdup2(const char *s1, const char *s2)
{
    char *p;
    size_t l1 = strlen(s1);
    size_t l2 = strlen(s2) + 1;	/* Final NULL byte from here */

    p = malloc(l1 + l2);
    if (!p)
	return NULL;

    memcpy(p, s1, l1);
    memcpy(p+l1, s2, l2);

    return p;
}

static char *port_path(const char *port)
{
    const char *prefix = _PATH_DEV;

    if (!port[0]) {
	errno = ENOENT;
	return NULL;
    }

    if (strchr(NOPREFIX, port[0]))
	prefix = "";

    return strdup2(prefix, port);
}

#ifdef _WIN32

#include <windows.h>

static int config_port(int fd, unsigned long baud, enum flowctrl flowctrl)
{
    HANDLE fh = (HANDLE)_get_osfhandle(fd);
    DCB dcb;
    COMMTIMEOUTS cto;

    memset(&dcb, 0, sizeof dcb);
    dcb.DCBlength = sizeof dcb;

    if (!GetCommState(fh, &dcb)) {
	errno = ENOTTY;
	return -1;
    }

    dcb.BaudRate = baud;
    dcb.fBinary = TRUE;
    dcb.fParity = FALSE;
    dcb.fTXContinueOnXoff = FALSE;
    dcb.fOutX = FALSE;
    dcb.fInX = FALSE;
    dcb.fErrorChar = FALSE;
    dcb.fNull = FALSE;
    dcb.fAbortOnError = FALSE;
    dcb.ByteSize = 8;
    dcb.Parity = NOPARITY;
    dcb.StopBits = ONESTOPBIT;
    dcb.XonChar = 0;
    dcb.XoffChar = 0;
    dcb.ErrorChar = 0;
    dcb.EofChar = 0;
    dcb.EvtChar = 0;
    dcb.fOutxDsrFlow = FALSE;
    dcb.fDtrControl = DTR_CONTROL_ENABLE;
    dcb.fDsrSensitivity = FALSE;
    dcb.fOutxCtsFlow = FALSE;
    dcb.fRtsControl = RTS_CONTROL_ENABLE;

    switch (flowctrl) {
    case FLOW_DTR:
	dcb.fDtrControl = DTR_CONTROL_HANDSHAKE;
	dcb.fOutxDsrFlow = TRUE;
	break;

    case FLOW_RTS:
	dcb.fRtsControl = CTS_CONTROL_HANDSHAKE;
	dcb.fOutxCtsFlow = TRUE;
	break;

    default:
	/* Already set up */
	break;
    }

    if (!SetCommState(fh, &dcb)) {
	errno = EINVAL;
	return -1;
    }

    memset(&cto, 0, sizeof cto);
    cto.ReadIntervalTimeout = 1;

    if (SetCommTimeouts(fh, &cto)) {
	errno = EINVAL;
	return -1;
    }

    PurgeComm(fh, PURGE_TXCLEAR|PURGE_RXCLEAR);
    ClearCommError(fh, CE_RXOVER|CE_OVERRUN|CE_RXPARITY|CE_FRAME|CE_BREAK,
		   NULL);

    return 0;
}

#elif defined(HAVE_TERMIOS_H)

/*
 * POSIX systems
 */

# ifdef __linux__
/*
 * Linux has been able to set arbitrary speeds for ages, but glibc never
 * caught up.  Our own mini-implementation of termios...
 */
#  include <sys/ioctl.h>
#  include <asm/termbits.h>	/* struct termios2 */

#ifndef TCGETS2			/* On PowerPC kernel termios == termios2 */
typedef struct termios my_termios;
# define TCGETS2  TCGETS
# define TCSETS2  TCSETS
#else
typedef struct termios2 my_termios;
#endif

static int mytcgetattr(int fd, my_termios *tio)
{
    return ioctl(fd, TCGETS2, tio);
}

static int mytcsetattr(int fd, const my_termios *tio)
{
    return ioctl(fd, TCSETS2, tio);
}

static int mycfsetbaud(my_termios *tio, unsigned long baud)
{
    tio->c_cflag &= ~(CBAUD | CIBAUD);
    tio->c_cflag |= BOTHER;
    tio->c_ispeed = tio->c_ospeed = baud;
    return 0;
}

static int mytcflush(int fd, int queue)
{
    return ioctl(fd, TCFLSH, queue);
}

# else /* not Linux */

#  include <termios.h>
#  include "baudtospeed.c"

typedef struct termios my_termios;

# define mytcgetattr(x,y) tcgetattr(x, y)
# define mytcsetattr(x,y) tcsetattr(x, TCSANOW, y)
# define mytcflush(x,y)   tcflush(x, y)

static int mycfsetbaud(my_termios *tio, unsigned long baud)
{
    speed_t speed = baudtospeed(baud);
    if (speed == B0) {
	errno = EINVAL;
	return -1;
    }
    return cfsetospeed(tio, speed) | cfsetispeed(tio, speed);
}

# endif /* not Linux */

#ifdef CRTSCTS
/* All good */
#elif defined(CCTS_OFLOW) && defined(CRTS_IFLOW)
# define CRTSCTS (CCTS_OFLOW|CRTS_IFLOW)
#else
# define CRTSCTS 0
#endif

static int config_port(int fd, unsigned long baud, enum flowctrl flowctrl)
{
    my_termios tio;

    if (!baud) {
	errno = EINVAL;
	return -1;
    }

    if (mytcgetattr(fd, &tio))
	return -1;

    tio.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP
		     | INLCR | IGNCR | ICRNL | IXON);
    tio.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
    tio.c_oflag &= ~OPOST;
    tio.c_cflag &= ~(CSIZE | CSTOPB | PARENB | CRTSCTS);
    tio.c_cflag |= CREAD | CS8 | CLOCAL;
    tio.c_cc[VMIN]  = 1;
    tio.c_cc[VTIME] = 0;

    switch (flowctrl) {
    default:
	/* Do nothing */
	break;

    case FLOW_RTS:
	tio.c_cflag |= CRTSCTS;
	break;
    }

    if (mycfsetbaud(&tio, baud))
	return -1;

    if (mytcsetattr(fd, &tio))
	return -1;

    return mytcflush(fd, TCIOFLUSH);
}

#else

# error "Don't know how to configure a serial port on this system"

#endif

int open_serial(const char *port, unsigned long baud, enum flowctrl flowctrl)
{
    char *path = port_path(port);
    int fd = -1;

    if (!path)
	goto fail;

    fd = open(path, O_RDWR);
    free(path);
    if (fd < 0)
	goto fail;

    if (config_port(fd, baud, flowctrl))
	goto fail;

    return fd;

fail:
    if (fd >= 0)
	close(fd);

    return -1;
}