openport.c.html 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. openport.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='openchannel.c.html' title=' openchannel.c '>PREV</a>]
  17. [<a href='toolkit.html' title=' Index '>HOME</a>]
  18. [<a href='output.c.html' title=' output.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. * void openport (struct _file_ * port, flag_t flags);
  64. *
  65. * open the serial port named by port-&gt;name and set file descriptor
  66. * port-&gt;file; datatype struct _file_ is define in tools/types.h;
  67. *
  68. * this function no longer initializes port settings because there
  69. * are too many differences in constants, variables and functions
  70. * between Linux, OpenBSD, MacOSX and Windows to cleanly implement
  71. * a single approach to serial port configuration; this means that
  72. * users should manually configure a port and then leave it alone;
  73. *
  74. * use stty on Linux systems and the Control Panel on Windows;
  75. *
  76. * port configuration code for Linux and Windows can be enabled if
  77. * needed by defining SERIAL_CONFIG at compile time; this will not
  78. * restore original port setting when the port is closed;
  79. *
  80. *
  81. * Contributor(s):
  82. * Charles Maier &lt;cmaier@qca.qualcomm.com&gt;
  83. * Mathieu Olivari &lt;mathieu@qca.qualcomm.com&gt;
  84. *
  85. *--------------------------------------------------------------------*/
  86. #ifndef OPENPORT_SOURCE
  87. #define OPENPORT_SOURCE
  88. #include &lt;unistd.h&gt;
  89. #include &lt;stdlib.h&gt;
  90. #include &lt;errno.h&gt;
  91. #if defined (WIN32)
  92. # include &lt;windows.h&gt;
  93. #elif defined (__linux__)
  94. # include &lt;termios.h&gt;
  95. #elif defined (__APPLE__)
  96. # include &lt;termios.h&gt;
  97. # include &lt;net/ethernet.h&gt;
  98. #elif defined (__OpenBSD__)
  99. # include &lt;termios.h&gt;
  100. #else
  101. #error &quot;Unknown Environment&quot;
  102. #endif
  103. #include &quot;../tools/types.h&quot;
  104. #include &quot;../tools/files.h&quot;
  105. #include &quot;../tools/flags.h&quot;
  106. #include &quot;../tools/error.h&quot;
  107. #include &quot;../serial/serial.h&quot;
  108. void openport (struct _file_ * port, flag_t flags)
  109. {
  110. #if defined (WIN32)
  111. HANDLE hSerial;
  112. COMMTIMEOUTS timeouts;
  113. hSerial = CreateFile (port-&gt;name, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
  114. if (hSerial == INVALID_HANDLE_VALUE)
  115. {
  116. error (1, errno, &quot;%s&quot;, port-&gt;name);
  117. }
  118. if (_anyset (flags, UART_DEFAULT))
  119. {
  120. DCB dcbSerial =
  121. {
  122. 0
  123. };
  124. dcbSerial.DCBlength = sizeof (dcbSerial);
  125. if (!GetCommState (hSerial, &amp;dcbSerial))
  126. {
  127. error (1, errno, &quot;Can't read state: %s&quot;, port-&gt;name);
  128. }
  129. if (_anyset (flags, UART_VERBOSE))
  130. {
  131. printf (&quot;getting %s &quot;, port-&gt;name);
  132. printf (&quot;Baud %6d &quot;, dcbSerial.BaudRate);
  133. printf (&quot;Data %d &quot;, dcbSerial.ByteSize);
  134. printf (&quot;Stop %d &quot;, dcbSerial.StopBits);
  135. printf (&quot;Parity %d\n&quot;, dcbSerial.Parity);
  136. }
  137. dcbSerial.BaudRate = CBR_115200;
  138. dcbSerial.ByteSize = DATABITS_8;
  139. dcbSerial.StopBits = ONESTOPBIT;
  140. dcbSerial.Parity = NOPARITY;
  141. if (_anyset (flags, UART_VERBOSE))
  142. {
  143. printf (&quot;setting %s &quot;, port-&gt;name);
  144. printf (&quot;Baud %6d &quot;, dcbSerial.BaudRate);
  145. printf (&quot;Data %d &quot;, dcbSerial.ByteSize);
  146. printf (&quot;Stop %d &quot;, dcbSerial.StopBits);
  147. printf (&quot;Parity %d\n&quot;, dcbSerial.Parity);
  148. }
  149. if (!SetCommState (hSerial, &amp;dcbSerial))
  150. {
  151. error (1, errno, &quot;Can't save state: %s&quot;, port-&gt;name);
  152. }
  153. }
  154. timeouts.ReadIntervalTimeout = 10;
  155. timeouts.ReadTotalTimeoutConstant = 50;
  156. timeouts.ReadTotalTimeoutMultiplier = 10;
  157. timeouts.WriteTotalTimeoutConstant = 50;
  158. timeouts.WriteTotalTimeoutMultiplier = 10;
  159. if (!SetCommTimeouts (hSerial, &amp;timeouts))
  160. {
  161. error (1, errno, &quot;Can't set timeouts: %s&quot;, port-&gt;name);
  162. }
  163. CloseHandle (hSerial);
  164. if ((port-&gt;file = open (port-&gt;name, O_BINARY|O_RDWR)) == -1)
  165. {
  166. error (1, errno, &quot;%s&quot;, port-&gt;name);
  167. }
  168. #else
  169. if ((port-&gt;file = open (port-&gt;name, O_BINARY|O_RDWR)) == -1)
  170. {
  171. error (1, errno, &quot;%s&quot;, port-&gt;name);
  172. }
  173. if (_anyset (flags, UART_DEFAULT))
  174. {
  175. struct termios termios;
  176. #if 1
  177. /*
  178. * POSIX generic code;
  179. */
  180. tcgetattr (port-&gt;file, &amp;termios);
  181. cfmakeraw (&amp;termios);
  182. termios.c_cflag |= CS8;
  183. termios.c_cflag &amp;= ~(CSTOPB);
  184. cfsetospeed (&amp;termios, B115200);
  185. #else
  186. /*
  187. * Linux specific code;
  188. */
  189. termios.c_cflag = B115200 | CS8 | CLOCAL | CREAD;
  190. termios.c_iflag = IGNPAR;
  191. termios.c_oflag = 0;
  192. termios.c_lflag = 0;
  193. termios.c_cc [VTIME] = 0;
  194. termios.c_cc [VMIN] = 5;
  195. #endif
  196. tcsetattr (port-&gt;file, TCSANOW, &amp;termios);
  197. }
  198. #endif
  199. return;
  200. }
  201. #endif
  202. </pre>
  203. <div class='footerlink'>
  204. [<a href='openchannel.c.html' title=' openchannel.c '>PREV</a>]
  205. [<a href='toolkit.html' title=' Index '>HOME</a>]
  206. [<a href='output.c.html' title=' output.c '>NEXT</a>]
  207. </div>
  208. </body>
  209. </html>