c4e9c5f6a0c31efc7ae0f73e2676003930127ad8
3 * $Id: tx-serial-unix.c,v 1.3 2002/02/02 19:17:33 mdw Exp $
5 * Unix/POSIX serial transport
7 * (c) 2001 Mark Wooding
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Jog: Programming for a jogging machine.
14 * Jog is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * Jog is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with Jog; if not, write to the Free Software Foundation,
26 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 /*----- Header files ------------------------------------------------------*/
41 #include <sys/types.h>
46 #include <mLib/darray.h>
47 #include <mLib/fdflags.h>
58 /*----- Data structures ---------------------------------------------------*/
61 txport tx
; /* Transport base */
62 struct txsu
*next
, **prev
; /* Chain of serial transports */
63 int fd
; /* File descriptor */
64 serial_config sc
; /* Internal serial config */
65 struct termios ta
, old_ta
; /* External serial configs */
68 /*----- Static variables --------------------------------------------------*/
70 struct baudmap
{ unsigned long baud
; unsigned long magic
; };
72 static const struct baudmap baudmap
[] = {
130 static unsigned long csize
[] = { CS5
, CS6
, CS7
, CS8
};
132 static txsu
*active
= 0;
134 /*----- Main code ---------------------------------------------------------*/
136 /* --- @txsu_shutdown@ --- *
142 * Use: Restores terminal settings on exit.
145 void txsu_shutdown(void)
149 for (tx
= active
; tx
; tx
= tx
->next
)
150 tcsetattr(tx
->fd
, TCSAFLUSH
, &tx
->old_ta
);
153 /* --- @setconfig@ --- *
155 * Arguments: @txsu *tx@ = pointer to serial transport
156 * @serial_config *sc@ = pointer to configuration to set
158 * Returns: Zero if OK, nonzero on error.
160 * Use: Updates the external configuration from an internal
164 static int setconfig(txsu
*tx
, serial_config
*sc
)
166 struct termios
*ta
= &tx
->ta
;
167 const struct baudmap
*b
;
169 for (b
= baudmap
; b
->baud
&& b
->baud
!= sc
->baud
; b
++)
172 sc
->wordlen
< 5 || sc
->wordlen
> 8 ||
173 sc
->stopbits
< 1 || sc
->stopbits
> 2) {
174 err_report(ERR_TXPORT
, ERRTX_CONFIG
, 0, "bad serial configuration");
178 ta
->c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
|INLCR
|IGNCR
|ICRNL
);
179 ta
->c_oflag
&= ~OPOST
;
180 ta
->c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|ISIG
|IEXTEN
);
184 cfsetospeed(ta
, b
->magic
);
185 cfsetispeed(ta
, b
->magic
);
186 ta
->c_cflag
= (ta
->c_cflag
& ~CSIZE
) | csize
[sc
->wordlen
- 5];
187 switch (sc
->parity
) {
188 case PARITY_NONE
: ta
->c_cflag
&= ~PARENB
; break;
189 case PARITY_ODD
: ta
->c_cflag
|= PARENB
| PARODD
; break;
190 case PARITY_EVEN
: ta
->c_cflag
|= PARENB
; ta
->c_cflag
&= ~PARODD
; break;
192 switch (sc
->stopbits
) {
193 case 1: ta
->c_cflag
&= ~CSTOPB
; break;
194 case 2: ta
->c_cflag
|= CSTOPB
; break;
199 ta
->c_cflag
&= ~CRTSCTS
;
200 ta
->c_iflag
&= ~(IXON
| IXOFF
);
203 ta
->c_cflag
&= ~CRTSCTS
;
204 ta
->c_iflag
|= IXON
| IXOFF
;
207 ta
->c_cflag
|= CRTSCTS
;
208 ta
->c_iflag
&= ~(IXON
| IXOFF
);
212 if (tcsetattr(tx
->fd
, TCSAFLUSH
, ta
)) {
213 err_report(ERR_TXPORT
, ERRTX_CREATE
, errno
,
214 "couldn't set terminal attributes: %s", strerror(errno
));
222 /* --- @txsu_create@ --- *
224 * Arguments: @const char *file@ = filename for serial port
226 * Returns: Pointer to created transport block.
228 * Use: Creates a serial port transport.
231 txport
*txsu_create(const char *file
)
233 txsu
*tx
= CREATE(txsu
);
234 serial_config sc
= SERIAL_INIT
;
236 if ((tx
->fd
= open(file
, O_RDWR
| O_NOCTTY
| O_NONBLOCK
)) < 0) {
237 err_report(ERR_TXPORT
, ERRTX_CREATE
, errno
,
238 "couldn't open device `%s': %s", file
, strerror(errno
));
241 if (fdflags(tx
->fd
, O_NONBLOCK
, 0, 0, 0)) {
242 err_report(ERR_TXPORT
, ERRTX_CREATE
, errno
,
243 "fcntl(clear O_NONBLOCK): %s", file
, strerror(errno
));
247 if (tcgetattr(tx
->fd
, &tx
->old_ta
)) {
248 err_report(ERR_TXPORT
, ERRTX_CREATE
, errno
,
249 "couldn't get terminal attributes: %s", strerror(errno
));
253 if (setconfig(tx
, &sc
))
261 /* --- Tidy up because it all went horribly wrong --- */
270 /* --- @txsu_configure@ --- *
272 * Arguments: @txport *txg@ = pointer to transport block
273 * @const char *k@ = configuration keyword
274 * @const char *v@ = value
276 * Returns: Nonzero if handled, zero otherwise.
278 * Use: Configures a serial port.
281 int txsu_configure(txport
*txg
, const char *k
, const char *v
)
283 txsu
*tx
= (txsu
*)txg
;
284 serial_config sc
= tx
->sc
;
287 err_report(ERR_TXPORT
, ERRTX_CONFIG
, 0,
288 "syntax error in serial config `%s'", k
);
291 if (serial_parse(&sc
, k
, v
)) {
292 err_report(ERR_TXPORT
, ERRTX_CONFIG
, 0,
293 "syntax error in serial config `%s=%s'", k
, v
);
296 if (setconfig(tx
, &sc
))
301 /* --- @txsu_write@ --- *
303 * Arguments: @txport *txg@ = pointer to transport context
304 * @const void *p@ = pointer to buffer
305 * @size_t sz@ = size of the buffer
307 * Returns: Number of bytes written, or @-1@ on error.
309 * Use: Writes data to a transport.
312 ssize_t
txsu_write(txport
*txg
, const void *p
, size_t sz
)
314 txsu
*tx
= (txsu
*)txg
;
316 return (write(tx
->fd
, p
, sz
));
319 /* --- @txsu_fetch@ --- *
321 * Arguments: @void *txv@ = pointer to transport context
323 * Returns: Nothing of interest.
325 * Use: Thread to fetch data from a serial port.
328 void *txsu_fetch(void *txv
)
331 unsigned char buf
[BUFSIZ
];
335 /* --- Read data while it arrives --- */
338 n
= read(tx
->fd
, buf
, sizeof(buf
));
340 err_report(ERR_TXPORT
, ERRTX_READ
, errno
,
341 "error reading from serial port: %s", strerror(errno
));
349 if ((e
= pthread_mutex_lock(&tx
->tx
.mx
)) != 0) {
350 err_report(ERR_TXPORT
, ERRTX_READ
, e
,
351 "error locking mutex: %s", strerror(e
));
354 DA_ENSURE(&tx
->tx
.buf
, n
);
355 memcpy(DA(&tx
->tx
.buf
) + DA_LEN(&tx
->tx
.buf
), buf
, n
);
356 DA_EXTEND(&tx
->tx
.buf
, n
);
357 pthread_cond_signal(&tx
->tx
.cv
);
358 pthread_mutex_unlock(&tx
->tx
.mx
);
361 /* --- Deal with crapness --- */
363 e
= pthread_mutex_lock(&tx
->tx
.mx
);
365 pthread_cond_signal(&tx
->tx
.cv
);
366 pthread_mutex_unlock(&tx
->tx
.mx
);
370 /* --- @txsu_destroy@ --- *
372 * Arguments: @txport *txg@ = pointer to transport context
376 * Use: Destroys a serial port transport.
379 void txsu_destroy(txport
*txg
)
381 txsu
*tx
= (txsu
*)txg
;
383 tcsetattr(tx
->fd
, TCSAFLUSH
, &tx
->old_ta
);
385 *tx
->prev
= tx
->next
;
387 tx
->next
->prev
= tx
->prev
;
391 /*----- That's all, folks -------------------------------------------------*/