ttysig.c.html 9.6 KB

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