Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 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 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | #include <termios.h> |
| 24 | #include "libbb.h" |
| 25 | |
| 26 | struct speed_map { |
| 27 | unsigned short speed; |
| 28 | unsigned short value; |
| 29 | }; |
| 30 | |
| 31 | static const struct speed_map speeds[] = { |
| 32 | {B0, 0}, |
| 33 | {B50, 50}, |
| 34 | {B75, 75}, |
| 35 | {B110, 110}, |
| 36 | {B134, 134}, |
| 37 | {B150, 150}, |
| 38 | {B200, 200}, |
| 39 | {B300, 300}, |
| 40 | {B600, 600}, |
| 41 | {B1200, 1200}, |
| 42 | {B1800, 1800}, |
| 43 | {B2400, 2400}, |
| 44 | {B4800, 4800}, |
| 45 | {B9600, 9600}, |
| 46 | #ifdef B19200 |
| 47 | {B19200, 19200}, |
| 48 | #elif defined(EXTA) |
| 49 | {EXTA, 19200}, |
| 50 | #endif |
| 51 | #ifdef B38400 |
| 52 | {B38400, 38400/256 + 0x8000U}, |
| 53 | #elif defined(EXTB) |
| 54 | {EXTB, 38400/256 + 0x8000U}, |
| 55 | #endif |
| 56 | #ifdef B57600 |
| 57 | {B57600, 57600/256 + 0x8000U}, |
| 58 | #endif |
| 59 | #ifdef B115200 |
| 60 | {B115200, 115200/256 + 0x8000U}, |
| 61 | #endif |
| 62 | #ifdef B230400 |
| 63 | {B230400, 230400/256 + 0x8000U}, |
| 64 | #endif |
| 65 | #ifdef B460800 |
| 66 | {B460800, 460800/256 + 0x8000U}, |
| 67 | #endif |
| 68 | }; |
| 69 | |
Rob Landley | bc68cd1 | 2006-03-10 19:22:06 +0000 | [diff] [blame^] | 70 | enum { NUM_SPEEDS = (sizeof(speeds) / sizeof(struct speed_map)) }; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 71 | |
| 72 | unsigned long bb_baud_to_value(speed_t speed) |
| 73 | { |
| 74 | int i = 0; |
| 75 | |
| 76 | do { |
| 77 | if (speed == speeds[i].speed) { |
| 78 | if (speeds[i].value & 0x8000U) { |
| 79 | return ((unsigned long) (speeds[i].value) & 0x7fffU) * 256; |
| 80 | } |
| 81 | return speeds[i].value; |
| 82 | } |
| 83 | } while (++i < NUM_SPEEDS); |
| 84 | |
| 85 | return 0; |
| 86 | } |
| 87 | |
| 88 | speed_t bb_value_to_baud(unsigned long value) |
| 89 | { |
| 90 | int i = 0; |
| 91 | |
| 92 | do { |
| 93 | if (value == bb_baud_to_value(speeds[i].speed)) { |
| 94 | return speeds[i].speed; |
| 95 | } |
| 96 | } while (++i < NUM_SPEEDS); |
| 97 | |
| 98 | return (speed_t) - 1; |
| 99 | } |
| 100 | |
| 101 | #if 0 |
| 102 | /* testing code */ |
| 103 | #include <stdio.h> |
| 104 | |
| 105 | int main(void) |
| 106 | { |
| 107 | unsigned long v; |
| 108 | speed_t s; |
| 109 | |
| 110 | for (v = 0 ; v < 500000 ; v++) { |
| 111 | s = bb_value_to_baud(v); |
| 112 | if (s == (speed_t) -1) { |
| 113 | continue; |
| 114 | } |
| 115 | printf("v = %lu -- s = %0lo\n", v, (unsigned long) s); |
| 116 | } |
| 117 | |
| 118 | printf("-------------------------------\n"); |
| 119 | |
| 120 | for (s = 0 ; s < 010017+1 ; s++) { |
| 121 | v = bb_baud_to_value(s); |
| 122 | if (!v) { |
| 123 | continue; |
| 124 | } |
| 125 | printf("v = %lu -- s = %0lo\n", v, (unsigned long) s); |
| 126 | } |
| 127 | |
| 128 | return 0; |
| 129 | } |
| 130 | #endif |