3 * $Id: tx-serial-unix.c,v 1.2 2002/01/30 09:24:24 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 /*----- Revision history --------------------------------------------------*
31 * $Log: tx-serial-unix.c,v $
32 * Revision 1.2 2002/01/30 09:24:24 mdw
33 * Restructure for new transport configuration interface.
35 * Revision 1.1 2002/01/25 19:34:45 mdw
40 /*----- Header files ------------------------------------------------------*/
48 #include <sys/types.h>
53 #include <mLib/darray.h>
54 #include <mLib/fdflags.h>
65 /*----- Data structures ---------------------------------------------------*/
68 txport tx
; /* Transport base */
69 struct txsu
*next
, **prev
; /* Chain of serial transports */
70 int fd
; /* File descriptor */
71 serial_config sc
; /* Internal serial config */
72 struct termios ta
, old_ta
; /* External serial configs */
75 /*----- Static variables --------------------------------------------------*/
77 struct baudmap
{ unsigned long baud
; unsigned long magic
; };
79 static const struct baudmap baudmap
[] = {
137 static unsigned long csize
[] = { CS5
, CS6
, CS7
, CS8
};
139 static txsu
*active
= 0;
141 /*----- Main code ---------------------------------------------------------*/
143 /* --- @txsu_shutdown@ --- *
149 * Use: Restores terminal settings on exit.
152 void txsu_shutdown(void)
156 for (tx
= active
; tx
; tx
= tx
->next
)
157 tcsetattr(tx
->fd
, TCSAFLUSH
, &tx
->old_ta
);
160 /* --- @setconfig@ --- *
162 * Arguments: @txsu *tx@ = pointer to serial transport
163 * @serial_config *sc@ = pointer to configuration to set
165 * Returns: Zero if OK, nonzero on error.
167 * Use: Updates the external configuration from an internal
171 static int setconfig(txsu
*tx
, serial_config
*sc
)
173 struct termios
*ta
= &tx
->ta
;
174 const struct baudmap
*b
;
176 for (b
= baudmap
; b
->baud
&& b
->baud
!= sc
->baud
; b
++)
179 sc
->wordlen
< 5 || sc
->wordlen
> 8 ||
180 sc
->stopbits
< 1 || sc
->stopbits
> 2) {
181 err_report(ERR_TXPORT
, ERRTX_CONFIG
, 0, "bad serial configuration");
185 ta
->c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
|INLCR
|IGNCR
|ICRNL
);
186 ta
->c_oflag
&= ~OPOST
;
187 ta
->c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|ISIG
|IEXTEN
);
191 cfsetospeed(ta
, b
->magic
);
192 cfsetispeed(ta
, b
->magic
);
193 ta
->c_cflag
= (ta
->c_cflag
& ~CSIZE
) | csize
[sc
->wordlen
- 5];
194 switch (sc
->parity
) {
195 case PARITY_NONE
: ta
->c_cflag
&= ~PARENB
; break;
196 case PARITY_ODD
: ta
->c_cflag
|= PARENB
| PARODD
; break;
197 case PARITY_EVEN
: ta
->c_cflag
|= PARENB
; ta
->c_cflag
&= ~PARODD
; break;
199 switch (sc
->stopbits
) {
200 case 1: ta
->c_cflag
&= ~CSTOPB
; break;
201 case 2: ta
->c_cflag
|= CSTOPB
; break;
206 ta
->c_cflag
&= ~CRTSCTS
;
207 ta
->c_iflag
&= ~(IXON
| IXOFF
);
210 ta
->c_cflag
&= ~CRTSCTS
;
211 ta
->c_iflag
|= IXON
| IXOFF
;
214 ta
->c_cflag
|= CRTSCTS
;
215 ta
->c_iflag
&= ~(IXON
| IXOFF
);
219 if (tcsetattr(tx
->fd
, TCSAFLUSH
, ta
)) {
220 err_report(ERR_TXPORT
, ERRTX_CREATE
, errno
,
221 "couldn't set terminal attributes: %s", strerror(errno
));
229 /* --- @txsu_create@ --- *
231 * Arguments: @const char *file@ = filename for serial port
233 * Returns: Pointer to created transport block.
235 * Use: Creates a serial port transport.
238 txport
*txsu_create(const char *file
)
240 txsu
*tx
= CREATE(txsu
);
241 serial_config sc
= SERIAL_INIT
;
243 if ((tx
->fd
= open(file
, O_RDWR
| O_NOCTTY
| O_NONBLOCK
)) < 0) {
244 err_report(ERR_TXPORT
, ERRTX_CREATE
, errno
,
245 "couldn't open device `%s': %s", file
, strerror(errno
));
248 if (fdflags(tx
->fd
, O_NONBLOCK
, 0, 0, 0)) {
249 err_report(ERR_TXPORT
, ERRTX_CREATE
, errno
,
250 "fcntl(clear O_NONBLOCK): %s", file
, strerror(errno
));
254 if (tcgetattr(tx
->fd
, &tx
->old_ta
)) {
255 err_report(ERR_TXPORT
, ERRTX_CREATE
, errno
,
256 "couldn't get terminal attributes: %s", strerror(errno
));
260 if (setconfig(tx
, &sc
))
268 /* --- Tidy up because it all went horribly wrong --- */
277 /* --- @txsu_configure@ --- *
279 * Arguments: @txport *txg@ = pointer to transport block
280 * @const char *k@ = configuration keyword
281 * @const char *v@ = value
283 * Returns: Nonzero if handled, zero otherwise.
285 * Use: Configures a serial port.
288 int txsu_configure(txport
*txg
, const char *k
, const char *v
)
290 txsu
*tx
= (txsu
*)txg
;
291 serial_config sc
= tx
->sc
;
294 err_report(ERR_TXPORT
, ERRTX_CONFIG
, 0,
295 "syntax error in serial config `%s'", k
);
298 if (serial_parse(&sc
, k
, v
)) {
299 err_report(ERR_TXPORT
, ERRTX_CONFIG
, 0,
300 "syntax error in serial config `%s=%s'", k
, v
);
303 if (setconfig(tx
, &sc
))
308 /* --- @txsu_write@ --- *
310 * Arguments: @txport *txg@ = pointer to transport context
311 * @const void *p@ = pointer to buffer
312 * @size_t sz@ = size of the buffer
314 * Returns: Number of bytes written, or @-1@ on error.
316 * Use: Writes data to a transport.
319 ssize_t
txsu_write(txport
*txg
, const void *p
, size_t sz
)
321 txsu
*tx
= (txsu
*)txg
;
323 return (write(tx
->fd
, p
, sz
));
326 /* --- @txsu_fetch@ --- *
328 * Arguments: @void *txv@ = pointer to transport context
330 * Returns: Nothing of interest.
332 * Use: Thread to fetch data from a serial port.
335 void *txsu_fetch(void *txv
)
338 unsigned char buf
[BUFSIZ
];
342 /* --- Read data while it arrives --- */
345 n
= read(tx
->fd
, buf
, sizeof(buf
));
347 err_report(ERR_TXPORT
, ERRTX_READ
, errno
,
348 "error reading from serial port: %s", strerror(errno
));
356 if ((e
= pthread_mutex_lock(&tx
->tx
.mx
)) != 0) {
357 err_report(ERR_TXPORT
, ERRTX_READ
, e
,
358 "error locking mutex: %s", strerror(e
));
361 DA_ENSURE(&tx
->tx
.buf
, n
);
362 memcpy(DA(&tx
->tx
.buf
) + DA_LEN(&tx
->tx
.buf
), buf
, n
);
363 DA_EXTEND(&tx
->tx
.buf
, n
);
364 pthread_cond_signal(&tx
->tx
.cv
);
365 pthread_mutex_unlock(&tx
->tx
.mx
);
368 /* --- Deal with crapness --- */
370 e
= pthread_mutex_lock(&tx
->tx
.mx
);
372 pthread_cond_signal(&tx
->tx
.cv
);
373 pthread_mutex_unlock(&tx
->tx
.mx
);
377 /* --- @txsu_destroy@ --- *
379 * Arguments: @txport *txg@ = pointer to transport context
383 * Use: Destroys a serial port transport.
386 void txsu_destroy(txport
*txg
)
388 txsu
*tx
= (txsu
*)txg
;
390 tcsetattr(tx
->fd
, TCSAFLUSH
, &tx
->old_ta
);
392 *tx
->prev
= tx
->next
;
394 tx
->next
->prev
= tx
->prev
;
398 /*----- That's all, folks -------------------------------------------------*/