int6kbaud.c 10 KB

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