3 * $Id: serial.c,v 1.2 2002/01/30 09:23:58 mdw Exp $
5 * Common serial functionality
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 --------------------------------------------------*
32 * Revision 1.2 2002/01/30 09:23:58 mdw
33 * Follow new transport configuration interface. Add parameters for flow
34 * control. Break the format into separate parameters for independent
37 * Revision 1.1 2002/01/25 19:34:45 mdw
42 /*----- Header files ------------------------------------------------------*/
50 /*----- Main code ---------------------------------------------------------*/
52 /* --- @getbaud@ --- *
54 * Arguments: @const char *p@ = pointer to string
55 * @const char **pp@ = where to store final pointer
57 * Returns: Baud rate, or @-1@ on failure.
59 * Use: Parses a baud rate, either as a raw number or as an
60 * expression like `9k6'.
63 static long getbaud(const char *p
, const char **pp
)
69 /* --- Main loop --- */
72 int ch
= (unsigned char)*p
;
100 x
= x
* s
+ (ch
- '0') * f
;
113 /* --- @getparity@ --- *
115 * Arguments: @const char *p@ = pointer to string
116 * @const char **pp@ = where to store final pointer
118 * Returns: Parity constant or @-1@ on error.
120 * Use: Parses a parity setting.
123 static int getparity(const char *p
, const char **pp
)
127 for (q
= p
; isalpha((unsigned char)*q
); q
++)
133 if (strncmp(p
, "none", q
- p
) == 0)
134 return (PARITY_NONE
);
135 else if (strncmp(p
, "odd", q
- p
) == 0)
137 else if (strncmp(p
, "even", q
- p
) == 0)
138 return (PARITY_EVEN
);
143 /* --- @getflow@ --- *
145 * Arguments: @const char *p@ = pointer to string
146 * @const char **pp@ = where to store final pointer
148 * Returns: Flow control constant or @-1@ on error.
150 * Use: Parses a flow control setting.
153 static int getflow(const char *p
, const char **pp
)
157 for (q
= p
; isalpha((unsigned char)*q
); q
++)
163 if (strncmp(p
, "none", q
- p
) == 0)
165 else if (strncmp(p
, "soft", q
- p
) == 0 ||
166 strncmp(p
, "xon-xoff", q
- p
) == 0 ||
167 strncmp(p
, "xon/xoff", q
- p
) == 0 ||
168 strncmp(p
, "xonxoff", q
- p
) == 0)
169 return (FLOW_XONXOFF
);
170 else if (strncmp(p
, "hard", q
- p
) == 0 ||
171 strncmp(p
, "cts-rts", q
- p
) == 0 ||
172 strncmp(p
, "cts/rts", q
- p
) == 0 ||
173 strncmp(p
, "ctsrts", q
- p
) == 0 ||
174 strncmp(p
, "rts/cts", q
- p
) == 0 ||
175 strncmp(p
, "rts-cts", q
- p
) == 0 ||
176 strncmp(p
, "rtscts", q
- p
) == 0)
177 return (FLOW_RTSCTS
);
182 /* --- @serial_parse@ --- *
184 * Arguments: @serial_config *sc@ = pointer to serial config structure
185 * @const char *k, *v@ = keyword and value pair
187 * Returns: Zero if OK, or @-1@ on error.
189 * Use: Parses a serial config string into something more
190 * reasonable. The config structure should have been
191 * initialized to something sensible (e.g., @SERIAL_INIT@)
195 int serial_parse(serial_config
*sc
, const char *k
, const char *v
)
201 if (strcmp(k
, "baud") == 0 || strcmp(k
, "bps") == 0) {
202 if (!v
|| (x
= getbaud(v
, &q
)) < 0 || *q
)
205 } else if (strcmp(k
, "data") == 0 || strcmp(k
, "data-bits") == 0 ||
206 strcmp(k
, "databits") == 0 || strcmp(k
, "data-len") == 0 ||
207 strcmp(k
, "datalen") == 0 || strcmp(k
, "word") == 0 ||
208 strcmp(k
, "word-len") == 0 || strcmp(k
, "wordlen") == 0) {
209 if (!v
|| (x
= strtol(v
, &qq
, 10)) == 0 || *qq
)
212 } else if (strcmp(k
, "parity") == 0) {
213 if (!v
|| (x
= getparity(v
, &q
)) < 0 || *q
)
216 } else if (strcmp(k
, "stop") == 0 || strcmp(k
, "stop-bits") == 0 ||
217 strcmp(k
, "stopbits") == 0) {
218 if (!v
|| (x
= strtol(v
, &qq
, 10)) == 0 || *qq
)
221 } else if (strcmp(k
, "flow") == 0 || strcmp(k
, "flow-control") == 0 ||
222 strcmp(k
, "flowcontrol") == 0) {
223 if (!v
|| (x
= getflow(v
, &q
)) < 0 || *q
)
226 } else if (strcmp(k
, "format") == 0) {
229 if ((x
= strtol(v
, &qq
, 10)) == 0)
235 if ((x
= getparity(v
, &q
)) < 0)
241 if ((x
= strtol(v
, &qq
, 10)) == 0)
253 /*----- Test rig ----------------------------------------------------------*/
260 int main(int argc
, char *argv
[])
263 static const char *ptab
[] = { "none", "odd", "even" };
264 static const char *ftab
[] = { "none", "xon/xoff", "rts/cts" };
266 for (i
= 1; i
< argc
; i
++) {
267 serial_config sc
= SERIAL_INIT
;
270 for (k
= strtok(argv
[i
], ";"); k
; k
= strtok(0, ";")) {
271 if ((v
= strchr(k
, '=')) != 0)
273 if (serial_parse(&sc
, k
, v
)) {
274 printf("invalid serial configuration `%s=%s'\n", k
, v
);
278 printf("%lu %u-%s-%u %s\n", sc
.baud
, sc
.wordlen
,
279 ptab
[sc
.parity
], sc
.stopbits
, ftab
[sc
.flow
]);
287 /*----- That's all, folks -------------------------------------------------*/