int6kbaud.c 8.8 KB

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