int6kdetect.c.html 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  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. int6kdetect.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='int6k.c.html' title=' int6k.c '>PREV</a>]
  17. [<a href='toolkit.html' title=' Index '>HOME</a>]
  18. [<a href='int6keth.c.html' title=' int6keth.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. * int6kdetect.c
  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;fcntl.h&gt;
  74. #include &lt;limits.h&gt;
  75. #include &lt;stdio.h&gt;
  76. #include &lt;stdlib.h&gt;
  77. #include &lt;string.h&gt;
  78. #include &lt;unistd.h&gt;
  79. #ifdef WIN32
  80. #include &lt;windows.h&gt;
  81. #else
  82. #include &lt;termios.h&gt;
  83. #endif
  84. /*====================================================================*
  85. * custom header files;
  86. *--------------------------------------------------------------------*/
  87. #include &quot;../tools/error.h&quot;
  88. #include &quot;../tools/files.h&quot;
  89. #include &quot;../tools/flags.h&quot;
  90. #include &quot;../tools/putoptv.h&quot;
  91. #include &quot;../tools/getoptv.h&quot;
  92. #include &quot;../serial/serial.h&quot;
  93. /*====================================================================*
  94. * custom source files;
  95. *--------------------------------------------------------------------*/
  96. #ifndef MAKEFILE
  97. #include &quot;../tools/getoptv.c&quot;
  98. #include &quot;../tools/putoptv.c&quot;
  99. #include &quot;../tools/version.c&quot;
  100. #include &quot;../tools/error.c&quot;
  101. #endif
  102. #ifndef MAKEFILE
  103. #include &quot;../serial/baudrate.c&quot;
  104. #endif
  105. /*====================================================================*
  106. * program constants;
  107. *--------------------------------------------------------------------*/
  108. #define SERIAL_PORT &quot;/dev/ttyS0&quot;
  109. #define INT6KDETECT_QUIET (1 &lt;&lt; 0)
  110. #define INT6KDETECT_CMD_MODE (1 &lt;&lt; 1)
  111. struct serial
  112. {
  113. #ifdef WIN32
  114. HANDLE h;
  115. #else
  116. int fd;
  117. #endif
  118. };
  119. #define UART_NOPARITY 0
  120. #define UART_EVENPARITY 1
  121. #define UART_ODDPARITY 2
  122. struct serial_mode
  123. {
  124. int baud_rate;
  125. int parity;
  126. int data_bits;
  127. int stop_bits;
  128. };
  129. ssize_t read_serial (struct serial *s, void *buf, size_t nbytes)
  130. {
  131. #ifdef WIN32
  132. DWORD read;
  133. BOOL r;
  134. r = ReadFile (s-&gt;h, buf, (DWORD) nbytes, &amp;read, NULL);
  135. if (r) return read;
  136. else return -1;
  137. #else
  138. return (read (s-&gt;fd, buf, nbytes));
  139. #endif
  140. }
  141. ssize_t write_serial (struct serial *s, void *buf, size_t nbytes)
  142. {
  143. #ifdef WIN32
  144. DWORD written;
  145. BOOL r;
  146. r = WriteFile (s-&gt;h, buf, (DWORD) nbytes, &amp;written, NULL);
  147. if (r) return written;
  148. else return -1;
  149. #else
  150. return (write (s-&gt;fd, buf, nbytes));
  151. #endif
  152. }
  153. int open_serial (char const *file, struct serial *s)
  154. {
  155. #ifdef WIN32
  156. s-&gt;h = CreateFile (file, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
  157. if (s-&gt;h == INVALID_HANDLE_VALUE)
  158. {
  159. return (-1);
  160. }
  161. #else
  162. if ((s-&gt;fd = open (file, O_RDWR)) == -1)
  163. {
  164. return (-1);
  165. }
  166. #endif
  167. return (0);
  168. }
  169. int close_serial (struct serial *s)
  170. {
  171. #ifdef WIN32
  172. if (CloseHandle (s-&gt;h)) return 0;
  173. else return -1;
  174. #else
  175. return (close (s-&gt;fd));
  176. #endif
  177. }
  178. int set_serial (struct serial *s, struct serial_mode *serial_mode)
  179. {
  180. #ifdef WIN32
  181. COMMTIMEOUTS timeouts;
  182. DCB dcbSerial;
  183. memset (&amp;dcbSerial, 0, sizeof (dcbSerial));
  184. dcbSerial.DCBlength = sizeof (dcbSerial);
  185. if (!GetCommState (s-&gt;h, &amp;dcbSerial))
  186. {
  187. return (-1);
  188. }
  189. dcbSerial.BaudRate = serial_mode-&gt;baud_rate;
  190. dcbSerial.ByteSize = serial_mode-&gt;data_bits;
  191. switch (serial_mode-&gt;stop_bits)
  192. {
  193. case 1:
  194. dcbSerial.StopBits = ONESTOPBIT;
  195. break;
  196. case 2:
  197. dcbSerial.StopBits = TWOSTOPBITS;
  198. break;
  199. default:
  200. error (1, 0, &quot;invalid stop bit setting&quot;);
  201. }
  202. switch (serial_mode-&gt;parity)
  203. {
  204. case UART_ODDPARITY:
  205. dcbSerial.Parity = ODDPARITY;
  206. dcbSerial.fParity = TRUE;
  207. break;
  208. case UART_EVENPARITY:
  209. dcbSerial.Parity = EVENPARITY;
  210. dcbSerial.fParity = TRUE;
  211. break;
  212. case UART_NOPARITY:
  213. dcbSerial.Parity = NOPARITY;
  214. dcbSerial.fParity = FALSE;
  215. break;
  216. default:
  217. error (1, 0, &quot;invalid parity serial_mode&quot;);
  218. }
  219. if (!SetCommState (s-&gt;h, &amp;dcbSerial))
  220. {
  221. error (0, 0, &quot;could not set serial port settings&quot;);
  222. return (-1);
  223. }
  224. timeouts.ReadIntervalTimeout = 0;
  225. timeouts.ReadTotalTimeoutConstant = 10;
  226. timeouts.ReadTotalTimeoutMultiplier = 0;
  227. timeouts.WriteTotalTimeoutConstant = 10;
  228. timeouts.WriteTotalTimeoutMultiplier = 10;
  229. if (!SetCommTimeouts (s-&gt;h, &amp;timeouts))
  230. {
  231. return (-1);
  232. }
  233. #else
  234. struct termios termios;
  235. speed_t speed;
  236. tcgetattr (s-&gt;fd, &amp;termios);
  237. cfmakeraw (&amp;termios);
  238. termios.c_cflag &amp;= ~CSIZE;
  239. switch (serial_mode-&gt;data_bits)
  240. {
  241. case 8:
  242. termios.c_cflag |= CS8;
  243. break;
  244. case 7:
  245. termios.c_cflag |= CS7;
  246. break;
  247. case 6:
  248. termios.c_cflag |= CS6;
  249. break;
  250. case 5:
  251. termios.c_cflag |= CS5;
  252. break;
  253. default:
  254. error (1, 0, &quot;invalid serial byte size&quot;);
  255. }
  256. switch (serial_mode-&gt;stop_bits)
  257. {
  258. case 2:
  259. termios.c_cflag |= CSTOPB;
  260. break;
  261. case 1:
  262. termios.c_cflag &amp;= ~CSTOPB;
  263. break;
  264. default:
  265. error (1, 0, &quot;invalid number of stop bits&quot;);
  266. }
  267. switch (serial_mode-&gt;parity)
  268. {
  269. case UART_ODDPARITY:
  270. termios.c_cflag |= PARENB;
  271. termios.c_cflag |= PARODD;
  272. break;
  273. case UART_EVENPARITY:
  274. termios.c_cflag |= PARENB;
  275. termios.c_cflag &amp;= ~PARODD;
  276. break;
  277. case UART_NOPARITY:
  278. termios.c_cflag &amp;= ~PARENB;
  279. break;
  280. default:
  281. error (1, 0, &quot;invalid parity serial_mode&quot;);
  282. }
  283. if (baudrate (serial_mode-&gt;baud_rate, &amp;speed) == -1)
  284. {
  285. error (0, 0, &quot;warning: unsupported baud rate: %d&quot;, serial_mode-&gt;baud_rate);
  286. return (-1);
  287. }
  288. if (cfsetspeed (&amp;termios, speed) == -1) error (1, 0, &quot;could not set serial baud rate&quot;);
  289. termios.c_cc [VTIME] = 1;
  290. termios.c_cc [VMIN] = 0;
  291. if (tcsetattr (s-&gt;fd, TCSANOW, &amp;termios) == -1) error (1, 0, &quot;could not set serial attributes&quot;);
  292. #endif
  293. return (0);
  294. }
  295. int at_cmd (struct serial *s)
  296. {
  297. char buf [32];
  298. ssize_t r;
  299. if (write_serial (s, &quot;AT\r&quot;, 3) == -1) error (1, 0, &quot;could not write&quot;);
  300. memset (buf, 0, sizeof (buf));
  301. r = read_serial (s, buf, sizeof (buf) - 1);
  302. if (r &lt; 0) return -1;
  303. else if (r == 0) return -1;
  304. if (!strcmp (buf, &quot;OK\r&quot;)) return 0;
  305. return (-1);
  306. }
  307. void wakeup (struct serial *s)
  308. {
  309. sleep (1);
  310. if (write_serial (s, &quot;+++&quot;, 3) == -1) error (1, 0, &quot;could not write&quot;);
  311. sleep (1);
  312. }
  313. void dump_serial_mode (struct serial_mode *serial_mode)
  314. {
  315. printf (&quot;baud_rate = %d\n&quot;, serial_mode-&gt;baud_rate);
  316. printf (&quot;stop_bits = %d\n&quot;, serial_mode-&gt;stop_bits);
  317. printf (&quot;data_bits = %d\n&quot;, serial_mode-&gt;data_bits);
  318. printf (&quot;parity = %d\n&quot;, serial_mode-&gt;parity);
  319. }
  320. int try_serial_mode (struct serial *s, struct serial_mode *serial_mode, flag_t flags)
  321. {
  322. if (set_serial (s, serial_mode) == -1)
  323. {
  324. error (0, 0, &quot;could not set serial_mode&quot;);
  325. return (-1);
  326. }
  327. if (!_anyset (flags, INT6KDETECT_CMD_MODE)) wakeup (s);
  328. at_cmd (s);
  329. return (at_cmd (s));
  330. }
  331. int detect (struct serial *s, struct serial_mode *serial_mode, flag_t flags)
  332. {
  333. static int rate [] =
  334. {
  335. 115200,
  336. 9600,
  337. 460800,
  338. 230400,
  339. 57600,
  340. 38400,
  341. 19200,
  342. 4800,
  343. 2400,
  344. 600,
  345. 300,
  346. 50
  347. };
  348. static int parity [] =
  349. {
  350. UART_NOPARITY,
  351. UART_EVENPARITY,
  352. UART_ODDPARITY
  353. };
  354. size_t i;
  355. size_t j;
  356. unsigned current;
  357. unsigned total;
  358. total = 2 * 2 * 3 * (sizeof (rate) / sizeof (int));
  359. current = 0;
  360. for (serial_mode-&gt;stop_bits = 1; serial_mode-&gt;stop_bits &lt;= 2; ++serial_mode-&gt;stop_bits)
  361. {
  362. for (serial_mode-&gt;data_bits = 8; serial_mode-&gt;data_bits &gt;= 7; --serial_mode-&gt;data_bits)
  363. {
  364. for (i = 0; i &lt; sizeof (parity) / sizeof (int); ++i)
  365. {
  366. serial_mode-&gt;parity = parity [i];
  367. for (j = 0; j &lt; sizeof (rate) / sizeof (int); ++j)
  368. {
  369. serial_mode-&gt;baud_rate = rate [j];
  370. ++current;
  371. if (!_anyset (flags, INT6KDETECT_QUIET))
  372. {
  373. printf (&quot;\rTesting mode: %03d/%03d (%.01f%%)...&quot;, current, total, current * 100.0 / total);
  374. fflush (stdout);
  375. }
  376. if (!try_serial_mode (s, serial_mode, flags))
  377. {
  378. if (!_anyset (flags, INT6KDETECT_QUIET)) printf (&quot;\n&quot;);
  379. return (0);
  380. }
  381. }
  382. }
  383. }
  384. }
  385. if (!_anyset (flags, INT6KDETECT_QUIET)) printf (&quot;\n&quot;);
  386. return (-1);
  387. }
  388. int main (int argc, char const * argv [])
  389. {
  390. static char const * optv [] =
  391. {
  392. &quot;cl:qv&quot;,
  393. &quot;&quot;,
  394. &quot;Atheros UART Device Detector&quot;,
  395. &quot;c\tassume device is in command mode&quot;,
  396. &quot;l f\tserial port is (f) [&quot; SERIAL_PORT &quot;]&quot;,
  397. &quot;q\tquiet mode&quot;,
  398. &quot;v\tverbose mode&quot;,
  399. (char const *) (0)
  400. };
  401. signed c;
  402. char const *line = SERIAL_PORT;
  403. struct serial serial;
  404. struct serial_mode serial_mode;
  405. flag_t flags = 0;
  406. optind = 1;
  407. while ((c = getoptv (argc, argv, optv)) != -1)
  408. {
  409. switch ((char) (c))
  410. {
  411. case 'c':
  412. _setbits (flags, INT6KDETECT_CMD_MODE);
  413. break;
  414. case 'l':
  415. line = optarg;
  416. break;
  417. case 'q':
  418. _setbits (flags, INT6KDETECT_QUIET);
  419. break;
  420. default:
  421. break;
  422. }
  423. }
  424. argc -= optind;
  425. argv += optind;
  426. if (open_serial (line, &amp;serial) == -1) error (1, errno, &quot;could not open %s&quot;, line);
  427. if (detect (&amp;serial, &amp;serial_mode, flags) == -1) error (1, 0, &quot;could not detect device&quot;);
  428. printf (&quot;Detected the following serial mode:\n&quot;);
  429. dump_serial_mode (&amp;serial_mode);
  430. close_serial (&amp;serial);
  431. return (0);
  432. }
  433. </pre>
  434. <div class='footerlink'>
  435. [<a href='int6k.c.html' title=' int6k.c '>PREV</a>]
  436. [<a href='toolkit.html' title=' Index '>HOME</a>]
  437. [<a href='int6keth.c.html' title=' int6keth.c '>NEXT</a>]
  438. </div>
  439. </body>
  440. </html>