You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

gbser.cc 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. Serial interface
  3. Copyright (C) 2006 Robert Lipe, robertlipe+source@gpsbabel.org
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111 USA
  15. */
  16. #include "defs.h"
  17. #include "gbser.h"
  18. #include "gbser_private.h"
  19. #include <assert.h>
  20. #include <stdarg.h>
  21. #include <stdio.h>
  22. void gbser__db(int l, const char* msg, ...)
  23. {
  24. va_list ap;
  25. va_start(ap, msg);
  26. if (global_opts.debug_level >= l) {
  27. vprintf(msg, ap);
  28. }
  29. va_end(ap);
  30. }
  31. /* Set the serial port speed.
  32. */
  33. int gbser_set_speed(void* handle, unsigned speed)
  34. {
  35. return gbser_set_port(handle, speed, 8, 0, 1);
  36. }
  37. static int parity_letter(char c)
  38. {
  39. switch (c) {
  40. case 'N':
  41. case 'n':
  42. return 0;
  43. case 'O':
  44. case 'o':
  45. return 1;
  46. case 'E':
  47. case 'e':
  48. return 2;
  49. default:
  50. return -1;
  51. }
  52. }
  53. /* Set the serial port up by parsing the supplied parameter string.
  54. * Valid parameter strings look like '4800,8,N,1'. Parsing is case-
  55. * insensitive, spaces are allowed around the commas and omitted
  56. * trailing fields will default to '8', 'N' and '1'
  57. */
  58. int gbser_setup(void* handle, const char* spec)
  59. {
  60. unsigned arg[] = { 4800, 8, 0, 1 };
  61. unsigned int ap;
  62. for (ap = 0; ap < sizeof(arg) / sizeof(arg[0]); ap++) {
  63. unsigned t = 0;
  64. int pl;
  65. while (isspace(*spec)) {
  66. spec++;
  67. }
  68. /* Allow 'N', 'O' or 'E' as the parity spec */
  69. if (ap == 2 && (pl = parity_letter(*spec), pl >= 0)) {
  70. t = pl;
  71. spec++;
  72. } else {
  73. if (!isdigit(*spec)) {
  74. break;
  75. }
  76. while (isdigit(*spec)) {
  77. t = t * 10 + *spec++ - '0';
  78. }
  79. }
  80. arg[ap] = t;
  81. while (isspace(*spec)) {
  82. spec++;
  83. }
  84. if (*spec != ',') {
  85. break;
  86. }
  87. spec++;
  88. }
  89. if (*spec != '\0') {
  90. return gbser_ERROR;
  91. }
  92. return gbser_set_port(handle, arg[0], arg[1], arg[2], arg[3]);
  93. }
  94. /* Return true if there are characters available on the serial port
  95. */
  96. int gbser_avail(void* handle)
  97. {
  98. return gbser__fill_buffer(handle, 1, NULL);
  99. }
  100. /* Read as many bytes as are available without blocking. At most |len|
  101. * bytes will be read. Returns the number of bytes read or gbser_ERROR if an
  102. * error occurs.
  103. */
  104. int gbser_read(void* handle, void* buf, unsigned len)
  105. {
  106. int got = 0;
  107. while (len > 0) {
  108. int rc = gbser__fill_buffer(handle, len, NULL);
  109. if (rc < 0) {
  110. /* error */
  111. return rc;
  112. } else if (rc == 0) {
  113. /* nothing available */
  114. break;
  115. }
  116. got += gbser__read_buffer(handle, &buf, &len);
  117. }
  118. return got;
  119. }
  120. /* Read the specified number of bytes. Block until the requested number
  121. * of bytes have been read or the timeout (in ms) is exceeded.
  122. */
  123. int gbser_read_wait(void* handle, void* buf, unsigned len, unsigned ms)
  124. {
  125. int got = 0;
  126. while (len > 0 && ms != 0) {
  127. int rc;
  128. if (rc = gbser__fill_buffer(handle, len, &ms), rc < 0) {
  129. return rc;
  130. }
  131. got += gbser__read_buffer(handle, &buf, &len);
  132. }
  133. return got;
  134. }
  135. /* Read a single character from the port, returning immediately if
  136. * none are available.
  137. */
  138. int gbser_readc(void* handle)
  139. {
  140. unsigned char buf;
  141. int rc;
  142. rc = gbser_read(handle, &buf, 1);
  143. if (rc > 0) {
  144. return buf;
  145. } else if (rc == 0) {
  146. return gbser_NOTHING;
  147. } else {
  148. return gbser_ERROR;
  149. }
  150. }
  151. /* Read a single character from the port, waiting up to |ms|
  152. * milliseconds for a character to be available.
  153. */
  154. int gbser_readc_wait(void* handle, unsigned ms)
  155. {
  156. unsigned char buf;
  157. int rc;
  158. rc = gbser_read_wait(handle, &buf, 1, ms);
  159. if (rc > 0) {
  160. return buf;
  161. } else if (rc == 0) {
  162. return gbser_NOTHING;
  163. } else {
  164. return gbser_ERROR;
  165. }
  166. }
  167. /* Write a null terminated string in |str| to the serial
  168. * port.
  169. */
  170. int gbser_print(void* handle, const char* str)
  171. {
  172. return gbser_write(handle, str, (unsigned) strlen(str));
  173. }
  174. /* Write a single character to the serial port.
  175. */
  176. int gbser_writec(void* handle, int c)
  177. {
  178. return gbser_write(handle, &c, 1);
  179. }