blob: 94a296238e9066c8eabc638e03208e9045609042 [file] [log] [blame]
Manuel Novoa III cad53642003-03-19 09:13:01 +00001/* vi: set sw=4 ts=4: */
2/*
3 * compact speed_t <-> speed functions for busybox
4 *
5 * Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org>
6 *
Rob Landley290fcb42006-06-18 23:59:03 +00007 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Manuel Novoa III cad53642003-03-19 09:13:01 +00008 */
9
Rob Landley57c1f732006-06-20 16:17:24 +000010#include <termios.h>
Manuel Novoa III cad53642003-03-19 09:13:01 +000011#include "libbb.h"
12
Rob Landley57c1f732006-06-20 16:17:24 +000013struct speed_map {
14 unsigned short speed;
15 unsigned short value;
Manuel Novoa III cad53642003-03-19 09:13:01 +000016};
17
Rob Landley57c1f732006-06-20 16:17:24 +000018static const struct speed_map speeds[] = {
19 {B0, 0},
20 {B50, 50},
21 {B75, 75},
22 {B110, 110},
23 {B134, 134},
24 {B150, 150},
25 {B200, 200},
26 {B300, 300},
27 {B600, 600},
28 {B1200, 1200},
29 {B1800, 1800},
30 {B2400, 2400},
31 {B4800, 4800},
32 {B9600, 9600},
33#ifdef B19200
34 {B19200, 19200},
35#elif defined(EXTA)
36 {EXTA, 19200},
37#endif
38#ifdef B38400
39 {B38400, 38400/256 + 0x8000U},
40#elif defined(EXTB)
41 {EXTB, 38400/256 + 0x8000U},
42#endif
43#ifdef B57600
44 {B57600, 57600/256 + 0x8000U},
45#endif
46#ifdef B115200
47 {B115200, 115200/256 + 0x8000U},
48#endif
49#ifdef B230400
50 {B230400, 230400/256 + 0x8000U},
51#endif
52#ifdef B460800
53 {B460800, 460800/256 + 0x8000U},
54#endif
55};
Manuel Novoa III cad53642003-03-19 09:13:01 +000056
Denis Vlasenko80b8b392007-06-25 10:55:35 +000057enum { NUM_SPEEDS = ARRAY_SIZE(speeds) };
Rob Landley57c1f732006-06-20 16:17:24 +000058
Rob Landley8437ede2006-06-20 16:43:05 +000059unsigned int tty_baud_to_value(speed_t speed)
Rob Landley57c1f732006-06-20 16:17:24 +000060{
61 int i = 0;
62
63 do {
64 if (speed == speeds[i].speed) {
65 if (speeds[i].value & 0x8000U) {
66 return ((unsigned long) (speeds[i].value) & 0x7fffU) * 256;
67 }
68 return speeds[i].value;
69 }
70 } while (++i < NUM_SPEEDS);
Manuel Novoa III cad53642003-03-19 09:13:01 +000071
72 return 0;
73}
74
Rob Landley8437ede2006-06-20 16:43:05 +000075speed_t tty_value_to_baud(unsigned int value)
Manuel Novoa III cad53642003-03-19 09:13:01 +000076{
Rob Landley57c1f732006-06-20 16:17:24 +000077 int i = 0;
Manuel Novoa III cad53642003-03-19 09:13:01 +000078
Rob Landley57c1f732006-06-20 16:17:24 +000079 do {
Peter Kjellerstedt5ab8f7d2006-06-20 16:35:37 +000080 if (value == tty_baud_to_value(speeds[i].speed)) {
Rob Landley57c1f732006-06-20 16:17:24 +000081 return speeds[i].speed;
82 }
83 } while (++i < NUM_SPEEDS);
Manuel Novoa III cad53642003-03-19 09:13:01 +000084
Rob Landley57c1f732006-06-20 16:17:24 +000085 return (speed_t) - 1;
Manuel Novoa III cad53642003-03-19 09:13:01 +000086}
Rob Landley57c1f732006-06-20 16:17:24 +000087
88#if 0
89/* testing code */
90#include <stdio.h>
91
92int main(void)
93{
94 unsigned long v;
95 speed_t s;
96
Denis Vlasenkob71c6682007-07-21 15:08:09 +000097 for (v = 0 ; v < 500000; v++) {
Peter Kjellerstedt5ab8f7d2006-06-20 16:35:37 +000098 s = tty_value_to_baud(v);
Rob Landley57c1f732006-06-20 16:17:24 +000099 if (s == (speed_t) -1) {
100 continue;
101 }
102 printf("v = %lu -- s = %0lo\n", v, (unsigned long) s);
103 }
104
105 printf("-------------------------------\n");
106
Denis Vlasenkob71c6682007-07-21 15:08:09 +0000107 for (s = 0 ; s < 010017+1; s++) {
Peter Kjellerstedt5ab8f7d2006-06-20 16:35:37 +0000108 v = tty_baud_to_value(s);
Rob Landley57c1f732006-06-20 16:17:24 +0000109 if (!v) {
110 continue;
111 }
112 printf("v = %lu -- s = %0lo\n", v, (unsigned long) s);
113 }
114
115 return 0;
116}
117#endif