ttyrecv.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*====================================================================*
  2. *
  3. * Copyright (c) 2013 Qualcomm Atheros, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or
  8. * without modification, are permitted (subject to the limitations
  9. * in the disclaimer below) provided that the following conditions
  10. * are met:
  11. *
  12. * * Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * * Redistributions in binary form must reproduce the above
  16. * copyright notice, this list of conditions and the following
  17. * disclaimer in the documentation and/or other materials
  18. * provided with the distribution.
  19. *
  20. * * Neither the name of Qualcomm Atheros nor the names of
  21. * its contributors may be used to endorse or promote products
  22. * derived from this software without specific prior written
  23. * permission.
  24. *
  25. * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE
  26. * GRANTED BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE
  27. * COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
  28. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  29. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  30. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
  31. * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  32. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  33. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  34. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  36. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  37. * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  38. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  39. *
  40. *--------------------------------------------------------------------*/
  41. /*====================================================================*
  42. *
  43. * ttyrecv.c
  44. *
  45. * Contributor(s):
  46. * Nathaniel Houghton
  47. *
  48. *--------------------------------------------------------------------*/
  49. /*====================================================================*
  50. * system header files;
  51. *--------------------------------------------------------------------*/
  52. #include <sys/time.h>
  53. #include <sys/types.h>
  54. #include <fcntl.h>
  55. #include <limits.h>
  56. #include <stdio.h>
  57. #include <stdlib.h>
  58. #include <string.h>
  59. #include <unistd.h>
  60. #include <termios.h>
  61. /*====================================================================*
  62. * custom header files;
  63. *--------------------------------------------------------------------*/
  64. #include "../tools/error.h"
  65. #include "../tools/putoptv.h"
  66. #include "../tools/getoptv.h"
  67. #include "../tools/number.h"
  68. #include "../tools/types.h"
  69. #include "../tools/flags.h"
  70. #include "../serial/serial.h"
  71. /*====================================================================*
  72. * custom source files;
  73. *--------------------------------------------------------------------*/
  74. #ifndef MAKEFILE
  75. #include "../tools/getoptv.c"
  76. #include "../tools/putoptv.c"
  77. #include "../tools/version.c"
  78. #include "../tools/error.c"
  79. #include "../tools/uintspec.c"
  80. #include "../tools/todigit.c"
  81. #endif
  82. #ifndef MAKEFILE
  83. #include "../serial/baudrate.c"
  84. #endif
  85. /*====================================================================*
  86. * program constants;
  87. *--------------------------------------------------------------------*/
  88. #define SERIAL_PORT "/dev/ttyS0"
  89. #define TTYRECV_VERBOSE (1 << 0)
  90. /*====================================================================*
  91. *
  92. * double ttyrecv (int ifd, int ofd, size_t time, size_t chunk_size, flag_t flags) ;
  93. *
  94. *--------------------------------------------------------------------*/
  95. static double ttyrecv (int ifd, int ofd, size_t time, size_t chunk_size, flag_t flags)
  96. {
  97. char *buf;
  98. char *p;
  99. ssize_t r;
  100. ssize_t w;
  101. size_t bytes_read;
  102. struct timeval tv_start,
  103. tv_now,
  104. tv_result;
  105. struct timeval tv_timeout;
  106. double bytes_sec;
  107. fd_set rfd;
  108. buf = malloc (chunk_size);
  109. if (buf == NULL)
  110. {
  111. error (1, errno, "could not allocate memory");
  112. }
  113. tcflush (ifd, TCIFLUSH);
  114. FD_ZERO (&rfd);
  115. FD_SET (ifd, &rfd);
  116. tv_timeout.tv_sec = 5;
  117. tv_timeout.tv_usec = 0;
  118. if (select (ifd + 1, &rfd, NULL, NULL, &tv_timeout) != 1)
  119. {
  120. error (1, errno, "timed out waiting for data");
  121. }
  122. if (gettimeofday (&tv_start, NULL) == -1)
  123. {
  124. error (1, errno, "could not get time");
  125. }
  126. if (_anyset (flags, TTYRECV_VERBOSE))
  127. {
  128. fprintf (stderr, "Started receive timer.\n");
  129. }
  130. bytes_read = 0;
  131. do
  132. {
  133. FD_ZERO (&rfd);
  134. FD_SET (ifd, &rfd);
  135. tv_timeout.tv_sec = 1;
  136. tv_timeout.tv_usec = 0;
  137. if (select (ifd + 1, &rfd, NULL, NULL, &tv_timeout) == 1)
  138. {
  139. r = read (ifd, buf, chunk_size);
  140. if (r == -1)
  141. {
  142. error (1, 0, "could not read");
  143. }
  144. bytes_read += r;
  145. if (ofd != -1)
  146. {
  147. p = buf;
  148. while (r)
  149. {
  150. w = write (ofd, p, r);
  151. if (w == -1)
  152. {
  153. error (1, errno, "could not write");
  154. }
  155. p += w;
  156. r -= w;
  157. }
  158. }
  159. }
  160. if (gettimeofday (&tv_now, NULL) == -1)
  161. {
  162. error (1, errno, "could not get time");
  163. }
  164. timersub (&tv_now, &tv_start, &tv_result);
  165. }
  166. while (tv_result.tv_sec < (signed)(time));
  167. bytes_sec = bytes_read / (tv_result.tv_sec + tv_result.tv_usec / 1000000.0);
  168. free (buf);
  169. return (bytes_sec * 8 / 1000.0);
  170. }
  171. /*====================================================================*
  172. *
  173. * int main (int argc, char const * argv []);
  174. *
  175. *
  176. *--------------------------------------------------------------------*/
  177. int main (int argc, char const * argv [])
  178. {
  179. static char const * optv [] =
  180. {
  181. "cl:rs:t:qv",
  182. "",
  183. "Serial Line Rate Tester",
  184. "c\tconsume received data (do not output to stdout)",
  185. "l f\tserial port is (f) [" SERIAL_PORT "]",
  186. "r\tprint the receive data rate to stdout",
  187. "s n\tport speed [ 115200 ]",
  188. "t n\treceive for (n) seconds [ 10 ]",
  189. "q\tquiet mode",
  190. "v\tverbose mode",
  191. (char const *) (0)
  192. };
  193. struct termios termios;
  194. char * line = SERIAL_PORT;
  195. double rate = 0;
  196. speed_t speed = B115200;
  197. size_t time = 10;
  198. size_t chunk_size = 256;
  199. flag_t flags = 0;
  200. signed consume = 0;
  201. signed rflag = 0;
  202. signed fd;
  203. signed c;
  204. optind = 1;
  205. while ((c = getoptv (argc, argv, optv)) != -1)
  206. {
  207. switch ((char) (c))
  208. {
  209. case 'c':
  210. consume = 1;
  211. break;
  212. case 'r':
  213. rflag = 1;
  214. break;
  215. case 'l':
  216. line = optarg;
  217. break;
  218. case 's':
  219. if (baudrate (uintspec (optarg, 0, UINT_MAX), &speed))
  220. {
  221. error (1, 0, "could not set baud rate");
  222. }
  223. break;
  224. case 't':
  225. time = uintspec (optarg, 0, SIZE_MAX);
  226. break;
  227. case 'v':
  228. _setbits (flags, TTYRECV_VERBOSE);
  229. break;
  230. default:
  231. break;
  232. }
  233. }
  234. argc -= optind;
  235. argv += optind;
  236. fd = open (line, O_RDWR | O_NONBLOCK | O_NOCTTY);
  237. if (fd == - 1)
  238. {
  239. error (1, errno, "could not open %s", line);
  240. }
  241. if (fcntl(fd, F_SETFL, 0) == -1)
  242. {
  243. error (1, errno, "failed to set tty flags");
  244. }
  245. if (tcgetattr (fd, & termios) == - 1)
  246. {
  247. error (1, errno, "could not get tty attributes");
  248. }
  249. cfmakeraw (& termios);
  250. termios.c_cflag = CS8 | CREAD | CLOCAL;
  251. if (cfsetspeed (& termios, speed) == - 1)
  252. {
  253. error (1, errno, "could not set tty speed");
  254. }
  255. if (tcsetattr (fd, TCSANOW, &termios) == -1)
  256. {
  257. error (1, errno, "could not set tty attributes");
  258. }
  259. if (!consume)
  260. {
  261. rate = ttyrecv (fd, STDOUT_FILENO, time, chunk_size, flags);
  262. }
  263. else
  264. {
  265. rate = ttyrecv (fd, -1, time, chunk_size, flags);
  266. }
  267. if (rflag)
  268. {
  269. fprintf (stderr, "%.02f Kbps\n", rate);
  270. }
  271. close (fd);
  272. return (0);
  273. }