[821] | 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 tarball for details.
|
---|
| 8 | */
|
---|
| 9 |
|
---|
| 10 | #include <termios.h>
|
---|
| 11 | #include "libbb.h"
|
---|
| 12 |
|
---|
| 13 | struct speed_map {
|
---|
| 14 | unsigned short speed;
|
---|
| 15 | unsigned short value;
|
---|
| 16 | };
|
---|
| 17 |
|
---|
| 18 | static 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 | };
|
---|
| 56 |
|
---|
| 57 | enum { NUM_SPEEDS = (sizeof(speeds) / sizeof(struct speed_map)) };
|
---|
| 58 |
|
---|
| 59 | unsigned int tty_baud_to_value(speed_t speed)
|
---|
| 60 | {
|
---|
| 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);
|
---|
| 71 |
|
---|
| 72 | return 0;
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | speed_t tty_value_to_baud(unsigned int value)
|
---|
| 76 | {
|
---|
| 77 | int i = 0;
|
---|
| 78 |
|
---|
| 79 | do {
|
---|
| 80 | if (value == tty_baud_to_value(speeds[i].speed)) {
|
---|
| 81 | return speeds[i].speed;
|
---|
| 82 | }
|
---|
| 83 | } while (++i < NUM_SPEEDS);
|
---|
| 84 |
|
---|
| 85 | return (speed_t) - 1;
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | #if 0
|
---|
| 89 | /* testing code */
|
---|
| 90 | #include <stdio.h>
|
---|
| 91 |
|
---|
| 92 | int main(void)
|
---|
| 93 | {
|
---|
| 94 | unsigned long v;
|
---|
| 95 | speed_t s;
|
---|
| 96 |
|
---|
| 97 | for (v = 0 ; v < 500000 ; v++) {
|
---|
| 98 | s = tty_value_to_baud(v);
|
---|
| 99 | 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 |
|
---|
| 107 | for (s = 0 ; s < 010017+1 ; s++) {
|
---|
| 108 | v = tty_baud_to_value(s);
|
---|
| 109 | if (!v) {
|
---|
| 110 | continue;
|
---|
| 111 | }
|
---|
| 112 | printf("v = %lu -- s = %0lo\n", v, (unsigned long) s);
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | return 0;
|
---|
| 116 | }
|
---|
| 117 | #endif
|
---|