source: MondoRescue/branches/3.3/mindi-busybox/libbb/speed_table.c@ 3621

Last change on this file since 3621 was 3621, checked in by Bruno Cornec, 7 years ago

New 3?3 banch for incorporation of latest busybox 1.25. Changing minor version to handle potential incompatibilities.

File size: 2.1 KB
Line 
1/* 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 *
7 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
8 */
9
10#include "libbb.h"
11
12struct speed_map {
13#if defined __FreeBSD__
14 /* On FreeBSD, B<num> constants don't fit into a short */
15 unsigned speed;
16#else
17 unsigned short speed;
18#endif
19 unsigned short value;
20};
21
22static const struct speed_map speeds[] = {
23 {B0, 0},
24 {B50, 50},
25 {B75, 75},
26 {B110, 110},
27 {B134, 134},
28 {B150, 150},
29 {B200, 200},
30 {B300, 300},
31 {B600, 600},
32 {B1200, 1200},
33 {B1800, 1800},
34 {B2400, 2400},
35 {B4800, 4800},
36 {B9600, 9600},
37#ifdef B19200
38 {B19200, 19200},
39#elif defined(EXTA)
40 {EXTA, 19200},
41#endif
42#ifdef B38400
43 {B38400, 38400/256 + 0x8000U},
44#elif defined(EXTB)
45 {EXTB, 38400/256 + 0x8000U},
46#endif
47#ifdef B57600
48 {B57600, 57600/256 + 0x8000U},
49#endif
50#ifdef B115200
51 {B115200, 115200/256 + 0x8000U},
52#endif
53#ifdef B230400
54 {B230400, 230400/256 + 0x8000U},
55#endif
56#ifdef B460800
57 {B460800, 460800/256 + 0x8000U},
58#endif
59#ifdef B921600
60 {B921600, 921600/256 + 0x8000U},
61#endif
62};
63
64enum { NUM_SPEEDS = ARRAY_SIZE(speeds) };
65
66unsigned FAST_FUNC tty_baud_to_value(speed_t speed)
67{
68 int i = 0;
69
70 do {
71 if (speed == speeds[i].speed) {
72 if (speeds[i].value & 0x8000U) {
73 return ((unsigned long) (speeds[i].value) & 0x7fffU) * 256;
74 }
75 return speeds[i].value;
76 }
77 } while (++i < NUM_SPEEDS);
78
79 return 0;
80}
81
82speed_t FAST_FUNC tty_value_to_baud(unsigned int value)
83{
84 int i = 0;
85
86 do {
87 if (value == tty_baud_to_value(speeds[i].speed)) {
88 return speeds[i].speed;
89 }
90 } while (++i < NUM_SPEEDS);
91
92 return (speed_t) - 1;
93}
94
95#if 0
96/* testing code */
97#include <stdio.h>
98
99int main(void)
100{
101 unsigned long v;
102 speed_t s;
103
104 for (v = 0 ; v < 1000000; v++) {
105 s = tty_value_to_baud(v);
106 if (s == (speed_t) -1) {
107 continue;
108 }
109 printf("v = %lu -- s = %0lo\n", v, (unsigned long) s);
110 }
111
112 printf("-------------------------------\n");
113
114 for (s = 0 ; s < 010017+1; s++) {
115 v = tty_baud_to_value(s);
116 if (!v) {
117 continue;
118 }
119 printf("v = %lu -- s = %0lo\n", v, (unsigned long) s);
120 }
121
122 return 0;
123}
124#endif
Note: See TracBrowser for help on using the repository browser.