speed_table.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. #include <termios.h>
  10. #include "libbb.h"
  11. struct speed_map {
  12. unsigned short speed;
  13. unsigned short value;
  14. };
  15. static const struct speed_map speeds[] = {
  16. {B0, 0},
  17. {B50, 50},
  18. {B75, 75},
  19. {B110, 110},
  20. {B134, 134},
  21. {B150, 150},
  22. {B200, 200},
  23. {B300, 300},
  24. {B600, 600},
  25. {B1200, 1200},
  26. {B1800, 1800},
  27. {B2400, 2400},
  28. {B4800, 4800},
  29. {B9600, 9600},
  30. #ifdef B19200
  31. {B19200, 19200},
  32. #elif defined(EXTA)
  33. {EXTA, 19200},
  34. #endif
  35. #ifdef B38400
  36. {B38400, 38400/256 + 0x8000U},
  37. #elif defined(EXTB)
  38. {EXTB, 38400/256 + 0x8000U},
  39. #endif
  40. #ifdef B57600
  41. {B57600, 57600/256 + 0x8000U},
  42. #endif
  43. #ifdef B115200
  44. {B115200, 115200/256 + 0x8000U},
  45. #endif
  46. #ifdef B230400
  47. {B230400, 230400/256 + 0x8000U},
  48. #endif
  49. #ifdef B460800
  50. {B460800, 460800/256 + 0x8000U},
  51. #endif
  52. };
  53. enum { NUM_SPEEDS = (sizeof(speeds) / sizeof(struct speed_map)) };
  54. unsigned int tty_baud_to_value(speed_t speed)
  55. {
  56. int i = 0;
  57. do {
  58. if (speed == speeds[i].speed) {
  59. if (speeds[i].value & 0x8000U) {
  60. return ((unsigned long) (speeds[i].value) & 0x7fffU) * 256;
  61. }
  62. return speeds[i].value;
  63. }
  64. } while (++i < NUM_SPEEDS);
  65. return 0;
  66. }
  67. speed_t tty_value_to_baud(unsigned int value)
  68. {
  69. int i = 0;
  70. do {
  71. if (value == tty_baud_to_value(speeds[i].speed)) {
  72. return speeds[i].speed;
  73. }
  74. } while (++i < NUM_SPEEDS);
  75. return (speed_t) - 1;
  76. }
  77. #if 0
  78. /* testing code */
  79. #include <stdio.h>
  80. int main(void)
  81. {
  82. unsigned long v;
  83. speed_t s;
  84. for (v = 0 ; v < 500000 ; v++) {
  85. s = tty_value_to_baud(v);
  86. if (s == (speed_t) -1) {
  87. continue;
  88. }
  89. printf("v = %lu -- s = %0lo\n", v, (unsigned long) s);
  90. }
  91. printf("-------------------------------\n");
  92. for (s = 0 ; s < 010017+1 ; s++) {
  93. v = tty_baud_to_value(s);
  94. if (!v) {
  95. continue;
  96. }
  97. printf("v = %lu -- s = %0lo\n", v, (unsigned long) s);
  98. }
  99. return 0;
  100. }
  101. #endif