int6kbaud.c.html 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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. int6kbaud.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='int64host.c.html' title=' int64host.c '>PREV</a>]
  17. [<a href='toolkit.html' title=' Index '>HOME</a>]
  18. [<a href='int6kboot.c.html' title=' int6kboot.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. * int6kbaud.c - Atheros Serial Line Device Manager;
  64. *
  65. *
  66. * Contributor(s):
  67. * Nathaniel Houghton &lt;nhoughto@qca.qualcomm.com&gt;
  68. *
  69. *--------------------------------------------------------------------*/
  70. /*====================================================================*
  71. * system header files;
  72. *--------------------------------------------------------------------*/
  73. #include &lt;unistd.h&gt;
  74. #include &lt;stdlib.h&gt;
  75. #include &lt;string.h&gt;
  76. #include &lt;limits.h&gt;
  77. #if defined (WIN32)
  78. #elif defined (__linux__)
  79. # include &lt;termios.h&gt;
  80. #elif defined (__APPLE__)
  81. # include &lt;termios.h&gt;
  82. #elif defined (__OpenBSD__)
  83. # include &lt;termios.h&gt;
  84. #else
  85. #error &quot;Unknown Environment&quot;
  86. #endif
  87. /*====================================================================*
  88. * custom header files;
  89. *--------------------------------------------------------------------*/
  90. #include &quot;../tools/getoptv.h&quot;
  91. #include &quot;../tools/putoptv.h&quot;
  92. #include &quot;../tools/number.h&quot;
  93. #include &quot;../tools/memory.h&quot;
  94. #include &quot;../tools/endian.h&quot;
  95. #include &quot;../tools/symbol.h&quot;
  96. #include &quot;../tools/files.h&quot;
  97. #include &quot;../tools/flags.h&quot;
  98. #include &quot;../tools/error.h&quot;
  99. #include &quot;../tools/types.h&quot;
  100. #include &quot;../serial/serial.h&quot;
  101. #include &quot;../plc/plc.h&quot;
  102. /*====================================================================*
  103. * custom source files;
  104. *--------------------------------------------------------------------*/
  105. #ifndef MAKEFILE
  106. #include &quot;../tools/getoptv.c&quot;
  107. #include &quot;../tools/putoptv.c&quot;
  108. #include &quot;../tools/version.c&quot;
  109. #include &quot;../tools/uintspec.c&quot;
  110. #include &quot;../tools/basespec.c&quot;
  111. #include &quot;../tools/synonym.c&quot;
  112. #include &quot;../tools/todigit.c&quot;
  113. #include &quot;../tools/error.c&quot;
  114. #include &quot;../tools/checksum32.c&quot;
  115. #include &quot;../tools/hexencode.c&quot;
  116. #include &quot;../tools/hexdump.c&quot;
  117. #include &quot;../tools/hexstring.c&quot;
  118. #include &quot;../tools/hexdecode.c&quot;
  119. #include &quot;../tools/synonym.c&quot;
  120. #include &quot;../tools/error.c&quot;
  121. #endif
  122. #ifndef MAKEFILE
  123. #include &quot;../serial/openport.c&quot;
  124. #include &quot;../serial/closeport.c&quot;
  125. #include &quot;../serial/serial.c&quot;
  126. #endif
  127. /*====================================================================*
  128. * program variables;
  129. *--------------------------------------------------------------------*/
  130. typedef struct uart
  131. {
  132. struct _file_ port;
  133. char const * string;
  134. byte mode;
  135. uint64_t baudrate;
  136. byte databits;
  137. byte parity;
  138. byte stopbits;
  139. byte flowctrl;
  140. unsigned flags;
  141. }
  142. uart;
  143. /*====================================================================*
  144. * program variables;
  145. *--------------------------------------------------------------------*/
  146. static const struct _term_ modes [] =
  147. {
  148. {
  149. &quot;command&quot;,
  150. &quot;2&quot;
  151. },
  152. {
  153. &quot;transparent&quot;,
  154. &quot;1&quot;
  155. }
  156. };
  157. static const struct _term_ paritybits [] =
  158. {
  159. {
  160. &quot;even&quot;,
  161. &quot;2&quot;
  162. },
  163. {
  164. &quot;none&quot;,
  165. &quot;0&quot;
  166. },
  167. {
  168. &quot;odd&quot;,
  169. &quot;1&quot;
  170. }
  171. };
  172. static const struct _term_ flowctrls [] =
  173. {
  174. {
  175. &quot;none&quot;,
  176. &quot;0&quot;
  177. },
  178. {
  179. &quot;off&quot;,
  180. &quot;0&quot;
  181. },
  182. {
  183. &quot;on&quot;,
  184. &quot;1&quot;
  185. }
  186. };
  187. /*====================================================================*
  188. * program constants;
  189. *--------------------------------------------------------------------*/
  190. #define MODES (sizeof (modes) / sizeof (struct _term_))
  191. #define PARITYBITS (sizeof (paritybits) / sizeof (struct _term_))
  192. #define FLOWCTRLS (sizeof (flowctrls) / sizeof (struct _term_))
  193. /*====================================================================*
  194. *
  195. * void at_command (struct uart * uart);
  196. *
  197. *--------------------------------------------------------------------*/
  198. static void at_command (struct uart * uart)
  199. {
  200. clearcommand ();
  201. while (*uart-&gt;string)
  202. {
  203. insert (*uart-&gt;string++);
  204. }
  205. insert ('\r');
  206. sendcommand (&amp;uart-&gt;port, uart-&gt;flags);
  207. readcommand (&amp;uart-&gt;port, uart-&gt;flags);
  208. return;
  209. }
  210. /*====================================================================*
  211. *
  212. * void at_wake (struct uart * uart);
  213. *
  214. * send wake command &quot;+++&quot; to enter command mode;
  215. *
  216. *--------------------------------------------------------------------*/
  217. static void at_wake (struct uart * uart)
  218. {
  219. clearcommand ();
  220. insert ('+');
  221. insert ('+');
  222. insert ('+');
  223. sendcommand (&amp;uart-&gt;port, uart-&gt;flags);
  224. readcommand (&amp;uart-&gt;port, uart-&gt;flags);
  225. mustbe ('O');
  226. mustbe ('K');
  227. mustbe ('\r');
  228. return;
  229. }
  230. /*====================================================================*
  231. *
  232. * void atbr (struct uart * uart);
  233. *
  234. * set serial line parameters;
  235. *
  236. *--------------------------------------------------------------------*/
  237. static void atbr (struct uart * uart)
  238. {
  239. clearcommand ();
  240. insert ('A');
  241. insert ('T');
  242. insert ('B');
  243. insert ('R');
  244. decode (&amp;uart-&gt;mode, sizeof (uart-&gt;mode));
  245. insert (',');
  246. uart-&gt;baudrate = HTOBE64 (uart-&gt;baudrate);
  247. decode (&amp;uart-&gt;baudrate, sizeof (uart-&gt;baudrate));
  248. uart-&gt;baudrate = BE64TOH (uart-&gt;baudrate);
  249. insert (',');
  250. decode (&amp;uart-&gt;databits, sizeof (uart-&gt;databits));
  251. insert (',');
  252. decode (&amp;uart-&gt;parity, sizeof (uart-&gt;parity));
  253. insert (',');
  254. decode (&amp;uart-&gt;stopbits, sizeof (uart-&gt;stopbits));
  255. insert (',');
  256. decode (&amp;uart-&gt;flowctrl, sizeof (uart-&gt;flowctrl));
  257. insert ('\r');
  258. sendcommand (&amp;uart-&gt;port, uart-&gt;flags);
  259. readcommand (&amp;uart-&gt;port, uart-&gt;flags);
  260. mustbe ('O');
  261. mustbe ('K');
  262. mustbe ('\r');
  263. return;
  264. }
  265. /*====================================================================*
  266. *
  267. * void manager (struct uart * uart);
  268. *
  269. * examine flagword in struct uart and perform requested operations
  270. * in the order that bits are tested; the order that bits are tested
  271. * may be changed as needed;
  272. *
  273. *--------------------------------------------------------------------*/
  274. static void manager (struct uart * uart)
  275. {
  276. if (_anyset (uart-&gt;flags, UART_WAKE))
  277. {
  278. at_wake (uart);
  279. }
  280. if (_anyset (uart-&gt;flags, UART_COMMAND))
  281. {
  282. at_command (uart);
  283. }
  284. if (_anyset (uart-&gt;flags, UART_ATBR))
  285. {
  286. atbr (uart);
  287. }
  288. return;
  289. }
  290. /*====================================================================*
  291. *
  292. * int main (int argc, char const * argv []);
  293. *
  294. *
  295. *
  296. *
  297. *--------------------------------------------------------------------*/
  298. int main (int argc, char const * argv [])
  299. {
  300. static char const * optv [] =
  301. {
  302. &quot;B:c:D:F:m:p:P:q:S:uvw&quot;,
  303. &quot;&quot;,
  304. &quot;Atheros Serial Line Device Settings&quot;,
  305. &quot;B n\tbaud rate is (n) [&quot; LITERAL (UART_BAUDRATE) &quot;]&quot;,
  306. &quot;c s\tsend custom serial line command (s)&quot;,
  307. &quot;D n\tuse (n) data bits [&quot; LITERAL (UART_DATABITS) &quot;]&quot;,
  308. &quot;F n\tflow control is (n) [&quot;LITERAL (UART_FLOWCTRL) &quot;]&quot;,
  309. &quot;m n\tcommand mode is (n)&quot;,
  310. &quot;p f\tserial port is (f) [&quot; DEVICE &quot;]&quot;,
  311. &quot;P n\tuse (n) parity bits [&quot; LITERAL (UART_PARITY) &quot;]&quot;,
  312. &quot;q\tquiet mode&quot;,
  313. &quot;S n\tuse (n) stop bits [&quot; LITERAL (UART_STOPBITS) &quot;]&quot;,
  314. &quot;u\tforce default host port settings [115200 8N1]&quot;,
  315. &quot;v\tverbose mode&quot;,
  316. &quot;w\twake device [+++]&quot;,
  317. (char const *) (0)
  318. };
  319. struct uart uart =
  320. {
  321. {
  322. 0,
  323. DEVICE
  324. },
  325. (char *)(0),
  326. UART_MODE,
  327. UART_BAUDRATE,
  328. UART_DATABITS,
  329. UART_PARITY,
  330. UART_STOPBITS,
  331. UART_FLOWCTRL,
  332. 0
  333. };
  334. signed c;
  335. if (getenv (UART_PORT))
  336. {
  337. uart.port.name = strdup (getenv (UART_PORT));
  338. }
  339. while ((c = getoptv (argc, argv, optv)) != -1)
  340. {
  341. switch (c)
  342. {
  343. case 'B':
  344. _setbits (uart.flags, UART_ATBR);
  345. uart.baudrate = (uint64_t)(uintspec (optarg, 1, ULONG_MAX));
  346. break;
  347. case 'c':
  348. _setbits (uart.flags, UART_COMMAND);
  349. uart.string = optarg;
  350. break;
  351. case 'D':
  352. _setbits (uart.flags, UART_ATBR);
  353. uart.databits = (byte)(uintspec (optarg, 7, 8));
  354. break;
  355. case 'F':
  356. _setbits (uart.flags, UART_ATBR);
  357. uart.flowctrl = (byte)(uintspec (synonym (optarg, flowctrls, FLOWCTRLS), 0, UCHAR_MAX));
  358. break;
  359. case 'm':
  360. _setbits (uart.flags, UART_ATBR);
  361. uart.mode = (byte)(uintspec (synonym (optarg, modes, MODES), 0, UCHAR_MAX));
  362. case 'P':
  363. _setbits (uart.flags, UART_ATBR);
  364. uart.parity = (byte)(uintspec (synonym (optarg, paritybits, PARITYBITS), 0, UCHAR_MAX));
  365. break;
  366. case 'q':
  367. _setbits (uart.flags, UART_SILENCE);
  368. break;
  369. case 'p':
  370. uart.port.name = optarg;
  371. break;
  372. case 'S':
  373. _setbits (uart.flags, UART_ATBR);
  374. uart.stopbits = (unsigned)(uintspec (optarg, 1, 2));
  375. break;
  376. case 'u':
  377. _setbits (uart.flags, UART_DEFAULT);
  378. break;
  379. case 'v':
  380. _setbits (uart.flags, UART_VERBOSE);
  381. break;
  382. case 'w':
  383. _setbits (uart.flags, UART_WAKE);
  384. break;
  385. default:
  386. break;
  387. }
  388. }
  389. argc -= optind;
  390. argv += optind;
  391. if (argc)
  392. {
  393. error (1, ENOTSUP, ERROR_TOOMANY);
  394. }
  395. openport (&amp;uart.port, uart.flags);
  396. manager (&amp;uart);
  397. closeport (&amp;uart.port);
  398. return (0);
  399. }
  400. </pre>
  401. <div class='footerlink'>
  402. [<a href='int64host.c.html' title=' int64host.c '>PREV</a>]
  403. [<a href='toolkit.html' title=' Index '>HOME</a>]
  404. [<a href='int6kboot.c.html' title=' int6kboot.c '>NEXT</a>]
  405. </div>
  406. </body>
  407. </html>