ttysend.c.html 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. ttysend.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='ttyrecv.c.html' title=' ttyrecv.c '>PREV</a>]
  17. [<a href='toolkit.html' title=' Index '>HOME</a>]
  18. [<a href='ttysig.c.html' title=' ttysig.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. * ttysend.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/files.h&quot;
  86. #include &quot;../tools/putoptv.h&quot;
  87. #include &quot;../tools/getoptv.h&quot;
  88. #include &quot;../tools/number.h&quot;
  89. #include &quot;../serial/serial.h&quot;
  90. /*====================================================================*
  91. * custom source files;
  92. *--------------------------------------------------------------------*/
  93. #ifndef MAKEFILE
  94. #include &quot;../tools/getoptv.c&quot;
  95. #include &quot;../tools/putoptv.c&quot;
  96. #include &quot;../tools/version.c&quot;
  97. #include &quot;../tools/uintspec.c&quot;
  98. #include &quot;../tools/todigit.c&quot;
  99. #include &quot;../tools/error.c&quot;
  100. #endif
  101. #ifndef MAKEFILE
  102. #include &quot;../serial/baudrate.c&quot;
  103. #endif
  104. /*====================================================================*
  105. * program constants;
  106. *--------------------------------------------------------------------*/
  107. #define SERIAL_PORT &quot;/dev/ttyS0&quot;
  108. void ttysend (int ifd, int ofd, size_t time, size_t chunk_size)
  109. {
  110. char *buf;
  111. char *p;
  112. ssize_t r;
  113. ssize_t w;
  114. struct timeval tv_start,
  115. tv_now,
  116. tv_result;
  117. buf = malloc (chunk_size);
  118. if (buf == NULL)
  119. {
  120. error (1, errno, &quot;could not allocate memory&quot;);
  121. }
  122. if (ifd == - 1)
  123. {
  124. unsigned i;
  125. for (i = 0; i &lt; chunk_size; ++ i)
  126. {
  127. buf [i] = i % 256;
  128. }
  129. }
  130. if (gettimeofday (&amp; tv_start, NULL) == - 1)
  131. {
  132. error (1, errno, &quot;could not get time&quot;);
  133. }
  134. do
  135. {
  136. if (ifd == -1)
  137. {
  138. r = chunk_size;
  139. }
  140. else
  141. {
  142. r = read (ifd, buf, chunk_size);
  143. if (r == - 1)
  144. {
  145. error (1, errno, &quot;could not read&quot;);
  146. }
  147. if (r == 0)
  148. {
  149. free (buf);
  150. return;
  151. }
  152. }
  153. p = buf;
  154. while (r)
  155. {
  156. w = write (ofd, p, r);
  157. if (w == - 1)
  158. {
  159. error (1, errno, &quot;could not write&quot;);
  160. }
  161. p += w;
  162. r -= w;
  163. }
  164. if (gettimeofday (&amp; tv_now, NULL) == - 1)
  165. {
  166. error (1, errno, &quot;could not get time&quot;);
  167. }
  168. timersub (&amp; tv_now, &amp; tv_start, &amp; tv_result);
  169. }
  170. while (tv_result.tv_sec &lt; (signed)(time));
  171. free (buf);
  172. }
  173. int main (int argc, char const * argv [])
  174. {
  175. static char const * optv [] =
  176. {
  177. &quot;f:l:s:t:qv&quot;,
  178. &quot;&quot;,
  179. &quot;Serial Line Rate Tester&quot;,
  180. &quot;f f\tsend file (f)&quot;,
  181. &quot;l f\tserial port is (f) [&quot; SERIAL_PORT &quot;]&quot;,
  182. &quot;s n\tport speed [ 115200 ]&quot;,
  183. &quot;t n\ttransmit for (n) seconds [ 15 ]&quot;,
  184. &quot;q\tquiet mode&quot;,
  185. &quot;v\tverbose mode&quot;,
  186. (char const *) (0)
  187. };
  188. int fd;
  189. signed c;
  190. optind = 1;
  191. char const *line = SERIAL_PORT;
  192. struct _file_ file =
  193. {
  194. -1,
  195. NULL
  196. };
  197. speed_t speed = B115200;
  198. size_t time = 15;
  199. size_t chunk_size = 256;
  200. struct termios termios;
  201. while ((c = getoptv (argc, argv, optv)) != -1)
  202. {
  203. switch ((char) (c))
  204. {
  205. case 'f':
  206. file.name = optarg;
  207. if (!strcmp (file.name, &quot;-&quot;)) file.file = STDIN_FILENO;
  208. else
  209. {
  210. file.file = open (file.name, O_BINARY | O_RDONLY);
  211. if (file.file == - 1)
  212. {
  213. error (1, errno, &quot;could not open %s&quot;, file.name);
  214. }
  215. }
  216. break;
  217. case 'l':
  218. line = optarg;
  219. break;
  220. case 's':
  221. if (baudrate (uintspec (optarg, 0, UINT_MAX), &amp; speed))
  222. {
  223. error (1, 0, &quot;could not set baud rate&quot;);
  224. }
  225. break;
  226. case 't':
  227. time = uintspec (optarg, 0, SIZE_MAX);
  228. break;
  229. default:
  230. break;
  231. }
  232. }
  233. argc -= optind;
  234. argv += optind;
  235. fd = open (line, O_RDWR | O_NONBLOCK | O_NOCTTY);
  236. if (fd == - 1)
  237. {
  238. error (1, errno, &quot;could not open %s&quot;, line);
  239. }
  240. if (fcntl(fd, F_SETFL, 0) == -1)
  241. {
  242. error (1, errno, &quot;failed to set tty flags&quot;);
  243. }
  244. if (tcgetattr (fd, &amp; termios) == - 1)
  245. {
  246. error (1, errno, &quot;could not get tty attributes&quot;);
  247. }
  248. cfmakeraw (&amp; termios);
  249. termios.c_cflag = CS8 | CREAD | CLOCAL;
  250. if (cfsetspeed (&amp; termios, speed) == - 1)
  251. {
  252. error (1, errno, &quot;could not set tty speed&quot;);
  253. }
  254. if (tcsetattr (fd, TCSANOW, &amp; termios) == - 1)
  255. {
  256. error (1, errno, &quot;could not set tty attributes&quot;);
  257. }
  258. ttysend (file.file, fd, time, chunk_size);
  259. close (fd);
  260. return (0);
  261. }
  262. </pre>
  263. <div class='footerlink'>
  264. [<a href='ttyrecv.c.html' title=' ttyrecv.c '>PREV</a>]
  265. [<a href='toolkit.html' title=' Index '>HOME</a>]
  266. [<a href='ttysig.c.html' title=' ttysig.c '>NEXT</a>]
  267. </div>
  268. </body>
  269. </html>