ttyrecv.c.html 9.0 KB

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