ttysig.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. * ttysig.c - Serial Line Signal Controller;
  44. *
  45. * This program is for Linux only;
  46. *
  47. * Contributor(s):
  48. * Nathaniel Houghton
  49. *
  50. *--------------------------------------------------------------------*/
  51. /*====================================================================*
  52. * kernel header files;
  53. *--------------------------------------------------------------------*/
  54. #include <sys/ioctl.h>
  55. /*====================================================================*
  56. * system header files;
  57. *--------------------------------------------------------------------*/
  58. #include <ctype.h>
  59. #include <errno.h>
  60. #include <fcntl.h>
  61. #include <termios.h>
  62. #include <stdlib.h>
  63. #include <string.h>
  64. #include <unistd.h>
  65. /*====================================================================*
  66. * custom header files;
  67. *--------------------------------------------------------------------*/
  68. #include "../tools/getoptv.h"
  69. #include "../tools/putoptv.h"
  70. #include "../tools/version.h"
  71. #include "../tools/types.h"
  72. #include "../tools/flags.h"
  73. #include "../tools/number.h"
  74. #include "../tools/error.h"
  75. /*====================================================================*
  76. * custom source files;
  77. *--------------------------------------------------------------------*/
  78. #ifndef MAKEFILE
  79. #include "../tools/getoptv.c"
  80. #include "../tools/putoptv.c"
  81. #include "../tools/version.c"
  82. #include "../tools/uintspec.c"
  83. #include "../tools/todigit.c"
  84. #include "../tools/error.c"
  85. #endif
  86. /*====================================================================*
  87. * constants;
  88. *--------------------------------------------------------------------*/
  89. #define SERIAL_PORT "/dev/ttyS0"
  90. #define TTYSIG_SET_DTR (1 << 0)
  91. #define TTYSIG_SET_RTS (1 << 1)
  92. #define TTYSIG_READ_STATUS (1 << 2)
  93. #define TTYSIG_INTERACTIVE (1 << 3)
  94. #define TTYSIG_NOPROMPT (1 << 4)
  95. void set_status (int fd, int flag, int value)
  96. {
  97. int status;
  98. if (ioctl (fd, TIOCMGET, &status) == -1) error (1, errno, "TIOCMGET failed");
  99. if (value) status |= flag;
  100. else status &= ~flag;
  101. if (ioctl (fd, TIOCMSET, &status) == -1) error (1, errno, "TIOCMSET failed");
  102. }
  103. void print_status (int fd)
  104. {
  105. int status;
  106. if (ioctl (fd, TIOCMGET, &status) == -1) error (1, errno, "TIOCMGET failed");
  107. printf ("--> DTR: %s\n", (status & TIOCM_DTR)? "+V": "-V");
  108. printf ("--> RTS: %s\n", (status & TIOCM_RTS)? "+V": "-V");
  109. printf ("<-- CTS: %s\n", (status & TIOCM_CTS)? "+V": "-V");
  110. printf ("<-- DSR: %s\n", (status & TIOCM_DSR)? "+V": "-V");
  111. printf ("<-- DCD: %s\n", (status & TIOCM_CD)? "+V": "-V");
  112. printf ("<-- RI : %s\n", (status & TIOCM_RI)? "+V": "-V");
  113. }
  114. void comment (void)
  115. {
  116. int c;
  117. while ((c = getchar ()) != EOF)
  118. {
  119. if (c == '\n')
  120. {
  121. ungetc (c, stdin);
  122. break;
  123. }
  124. }
  125. }
  126. int number (char *buf, int *val)
  127. {
  128. char *p;
  129. while (isspace (*buf)) ++buf;
  130. if (!isdigit (*buf))
  131. {
  132. error (0, 0, "\"%s\" is not a number", buf);
  133. return (-1);
  134. }
  135. *val = atoi (buf);
  136. p = buf;
  137. while (isdigit (*buf)) ++buf;
  138. if (*buf != '\0')
  139. {
  140. error (0, 0, "\"%s\" is not a number", p);
  141. return (-1);
  142. }
  143. return (0);
  144. }
  145. void interactive (int fd, flag_t flags)
  146. {
  147. char buf [32];
  148. int i,
  149. c;
  150. int value;
  151. char *p;
  152. for (; ; )
  153. {
  154. if (!_anyset (flags, TTYSIG_NOPROMPT))
  155. {
  156. printf ("command (D #, R #, e, r, s, q): ");
  157. fflush (stdout);
  158. }
  159. i = 0;
  160. while ((c = getchar ()) != EOF)
  161. {
  162. if (c == '#')
  163. {
  164. comment ();
  165. continue;
  166. }
  167. if (c == '\n') break;
  168. if (i == sizeof (buf) - 1) error (1, 0, "input too large");
  169. buf [i++] = c;
  170. }
  171. if (c == EOF) return;
  172. if (i == 0) continue;
  173. buf [i] = '\0';
  174. switch (buf [0])
  175. {
  176. case 'D':
  177. if (number (buf + 1, &value)) break;
  178. set_status (fd, TIOCM_DTR, value);
  179. break;
  180. case 'e':
  181. p = buf + 1;
  182. if (*p == ' ') ++p;
  183. printf ("%s\n", p);
  184. fflush (stdout);
  185. break;
  186. case 'R':
  187. if (number (buf + 1, &value)) break;
  188. set_status (fd, TIOCM_RTS, value);
  189. break;
  190. case 'r':
  191. print_status (fd);
  192. break;
  193. case 's':
  194. if (number (buf + 1, &value)) break;
  195. sleep (value);
  196. break;
  197. case 'q':
  198. case 'Q':
  199. return;
  200. break;
  201. case '\0':
  202. break;
  203. default:
  204. error (0, 0, "invalid command");
  205. }
  206. }
  207. }
  208. int main (int argc, char const ** argv)
  209. {
  210. int fd;
  211. static char const * optv [] =
  212. {
  213. "s:D:IR:rqv",
  214. "[ttysig script filename]",
  215. "Serial Line Signal Controller",
  216. "D n\tset DTR (0 = -V, 1 = +V) at startup",
  217. "I\tInteractive mode",
  218. "R n\tset RTS (0 = -V, 1 = +V) at startup",
  219. "r\tread current RTS/DTR values at startup",
  220. "s f\tserial port is (f) [" SERIAL_PORT "]",
  221. "q\tquiet mode",
  222. "v\tverbose mode",
  223. (char const *) (0)
  224. };
  225. signed c;
  226. optind = 1;
  227. flag_t flags = 0;
  228. uint8_t dtr_value;
  229. uint8_t rts_value;
  230. char *device = SERIAL_PORT;
  231. int input = -1;
  232. while ((c = getoptv (argc, argv, optv)) != -1)
  233. {
  234. switch ((char) (c))
  235. {
  236. case 'D':
  237. dtr_value = uintspec (optarg, 0, 1);
  238. _setbits (flags, TTYSIG_SET_DTR);
  239. break;
  240. case 'I':
  241. _setbits (flags, TTYSIG_INTERACTIVE);
  242. break;
  243. case 'R':
  244. rts_value = uintspec (optarg, 0, 1);
  245. _setbits (flags, TTYSIG_SET_RTS);
  246. break;
  247. case 'r':
  248. _setbits (flags, TTYSIG_READ_STATUS);
  249. break;
  250. case 's':
  251. device = optarg;
  252. break;
  253. default:
  254. break;
  255. }
  256. }
  257. argc -= optind;
  258. argv += optind;
  259. if (argc == 1)
  260. {
  261. input = open (* argv, O_RDONLY);
  262. if (input == -1) error (1, errno, "%s", * argv);
  263. if (dup2 (input, STDIN_FILENO) == -1) error (1, errno, "%s", * argv);
  264. _setbits (flags, TTYSIG_INTERACTIVE | TTYSIG_NOPROMPT);
  265. }
  266. else if (argc > 0) error (1, 0, "Invalid arguments");
  267. fd = open (device, O_RDONLY);
  268. if (fd == -1) error (1, errno, "could not open %s", device);
  269. if (_anyset (flags, TTYSIG_SET_DTR)) set_status (fd, TIOCM_DTR, dtr_value);
  270. if (_anyset (flags, TTYSIG_SET_RTS)) set_status (fd, TIOCM_RTS, rts_value);
  271. if (_anyset (flags, TTYSIG_READ_STATUS)) print_status (fd);
  272. if (_anyset (flags, TTYSIG_INTERACTIVE)) interactive (fd, flags);
  273. close (fd);
  274. return (0);
  275. }