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 | * |
Rob Landley | 290fcb4 | 2006-06-18 23:59:03 +0000 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
Rob Landley | 57c1f73 | 2006-06-20 16:17:24 +0000 | [diff] [blame] | 10 | #include <termios.h> |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 11 | #include "libbb.h" |
| 12 | |
Rob Landley | 57c1f73 | 2006-06-20 16:17:24 +0000 | [diff] [blame] | 13 | struct speed_map { |
| 14 | unsigned short speed; |
| 15 | unsigned short value; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 16 | }; |
| 17 | |
Rob Landley | 57c1f73 | 2006-06-20 16:17:24 +0000 | [diff] [blame] | 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 | }; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 56 | |
Denis Vlasenko | 80b8b39 | 2007-06-25 10:55:35 +0000 | [diff] [blame] | 57 | enum { NUM_SPEEDS = ARRAY_SIZE(speeds) }; |
Rob Landley | 57c1f73 | 2006-06-20 16:17:24 +0000 | [diff] [blame] | 58 | |
Rob Landley | 8437ede | 2006-06-20 16:43:05 +0000 | [diff] [blame] | 59 | unsigned int tty_baud_to_value(speed_t speed) |
Rob Landley | 57c1f73 | 2006-06-20 16:17:24 +0000 | [diff] [blame] | 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); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 71 | |
| 72 | return 0; |
| 73 | } |
| 74 | |
Rob Landley | 8437ede | 2006-06-20 16:43:05 +0000 | [diff] [blame] | 75 | speed_t tty_value_to_baud(unsigned int value) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 76 | { |
Rob Landley | 57c1f73 | 2006-06-20 16:17:24 +0000 | [diff] [blame] | 77 | int i = 0; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 78 | |
Rob Landley | 57c1f73 | 2006-06-20 16:17:24 +0000 | [diff] [blame] | 79 | do { |
Peter Kjellerstedt | 5ab8f7d | 2006-06-20 16:35:37 +0000 | [diff] [blame] | 80 | if (value == tty_baud_to_value(speeds[i].speed)) { |
Rob Landley | 57c1f73 | 2006-06-20 16:17:24 +0000 | [diff] [blame] | 81 | return speeds[i].speed; |
| 82 | } |
| 83 | } while (++i < NUM_SPEEDS); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 84 | |
Rob Landley | 57c1f73 | 2006-06-20 16:17:24 +0000 | [diff] [blame] | 85 | return (speed_t) - 1; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 86 | } |
Rob Landley | 57c1f73 | 2006-06-20 16:17:24 +0000 | [diff] [blame] | 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 | |
Denis Vlasenko | b71c668 | 2007-07-21 15:08:09 +0000 | [diff] [blame] | 97 | for (v = 0 ; v < 500000; v++) { |
Peter Kjellerstedt | 5ab8f7d | 2006-06-20 16:35:37 +0000 | [diff] [blame] | 98 | s = tty_value_to_baud(v); |
Rob Landley | 57c1f73 | 2006-06-20 16:17:24 +0000 | [diff] [blame] | 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 | |
Denis Vlasenko | b71c668 | 2007-07-21 15:08:09 +0000 | [diff] [blame] | 107 | for (s = 0 ; s < 010017+1; s++) { |
Peter Kjellerstedt | 5ab8f7d | 2006-06-20 16:35:37 +0000 | [diff] [blame] | 108 | v = tty_baud_to_value(s); |
Rob Landley | 57c1f73 | 2006-06-20 16:17:24 +0000 | [diff] [blame] | 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 |