ttysend.c 6.2 KB

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